remote_device.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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. #include <scsi/sas.h>
  56. #include <linux/bitops.h>
  57. #include "isci.h"
  58. #include "port.h"
  59. #include "remote_device.h"
  60. #include "request.h"
  61. #include "remote_node_context.h"
  62. #include "scu_event_codes.h"
  63. #include "task.h"
  64. #undef C
  65. #define C(a) (#a)
  66. const char *dev_state_name(enum sci_remote_device_states state)
  67. {
  68. static const char * const strings[] = REMOTE_DEV_STATES;
  69. return strings[state];
  70. }
  71. #undef C
  72. enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev,
  73. enum sci_remote_node_suspension_reasons reason)
  74. {
  75. return sci_remote_node_context_suspend(&idev->rnc, reason,
  76. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
  77. }
  78. /**
  79. * isci_remote_device_ready() - This function is called by the ihost when the
  80. * remote device is ready. We mark the isci device as ready and signal the
  81. * waiting proccess.
  82. * @ihost: our valid isci_host
  83. * @idev: remote device
  84. *
  85. */
  86. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  87. {
  88. dev_dbg(&ihost->pdev->dev,
  89. "%s: idev = %p\n", __func__, idev);
  90. clear_bit(IDEV_IO_NCQERROR, &idev->flags);
  91. set_bit(IDEV_IO_READY, &idev->flags);
  92. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  93. wake_up(&ihost->eventq);
  94. }
  95. static enum sci_status sci_remote_device_terminate_req(
  96. struct isci_host *ihost,
  97. struct isci_remote_device *idev,
  98. int check_abort,
  99. struct isci_request *ireq)
  100. {
  101. if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
  102. (ireq->target_device != idev) ||
  103. (check_abort && !test_bit(IREQ_PENDING_ABORT, &ireq->flags)))
  104. return SCI_SUCCESS;
  105. dev_dbg(&ihost->pdev->dev,
  106. "%s: idev=%p; flags=%lx; req=%p; req target=%p\n",
  107. __func__, idev, idev->flags, ireq, ireq->target_device);
  108. set_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
  109. return sci_controller_terminate_request(ihost, idev, ireq);
  110. }
  111. static enum sci_status sci_remote_device_terminate_reqs_checkabort(
  112. struct isci_remote_device *idev,
  113. int chk)
  114. {
  115. struct isci_host *ihost = idev->owning_port->owning_controller;
  116. enum sci_status status = SCI_SUCCESS;
  117. u32 i;
  118. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  119. struct isci_request *ireq = ihost->reqs[i];
  120. enum sci_status s;
  121. s = sci_remote_device_terminate_req(ihost, idev, chk, ireq);
  122. if (s != SCI_SUCCESS)
  123. status = s;
  124. }
  125. return status;
  126. }
  127. static bool isci_compare_suspendcount(
  128. struct isci_remote_device *idev,
  129. u32 localcount)
  130. {
  131. smp_rmb();
  132. /* Check for a change in the suspend count, or the RNC
  133. * being destroyed.
  134. */
  135. return (localcount != idev->rnc.suspend_count)
  136. || sci_remote_node_context_is_being_destroyed(&idev->rnc);
  137. }
  138. static bool isci_check_reqterm(
  139. struct isci_host *ihost,
  140. struct isci_remote_device *idev,
  141. struct isci_request *ireq,
  142. u32 localcount)
  143. {
  144. unsigned long flags;
  145. bool res;
  146. spin_lock_irqsave(&ihost->scic_lock, flags);
  147. res = isci_compare_suspendcount(idev, localcount)
  148. && !test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
  149. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  150. return res;
  151. }
  152. static bool isci_check_devempty(
  153. struct isci_host *ihost,
  154. struct isci_remote_device *idev,
  155. u32 localcount)
  156. {
  157. unsigned long flags;
  158. bool res;
  159. spin_lock_irqsave(&ihost->scic_lock, flags);
  160. res = isci_compare_suspendcount(idev, localcount)
  161. && idev->started_request_count == 0;
  162. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  163. return res;
  164. }
  165. enum sci_status isci_remote_device_terminate_requests(
  166. struct isci_host *ihost,
  167. struct isci_remote_device *idev,
  168. struct isci_request *ireq)
  169. {
  170. enum sci_status status = SCI_SUCCESS;
  171. unsigned long flags;
  172. u32 rnc_suspend_count;
  173. spin_lock_irqsave(&ihost->scic_lock, flags);
  174. if (isci_get_device(idev) == NULL) {
  175. dev_dbg(&ihost->pdev->dev, "%s: failed isci_get_device(idev=%p)\n",
  176. __func__, idev);
  177. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  178. status = SCI_FAILURE;
  179. } else {
  180. /* If already suspended, don't wait for another suspension. */
  181. smp_rmb();
  182. rnc_suspend_count
  183. = sci_remote_node_context_is_suspended(&idev->rnc)
  184. ? 0 : idev->rnc.suspend_count;
  185. dev_dbg(&ihost->pdev->dev,
  186. "%s: idev=%p, ireq=%p; started_request_count=%d, "
  187. "rnc_suspend_count=%d, rnc.suspend_count=%d"
  188. "about to wait\n",
  189. __func__, idev, ireq, idev->started_request_count,
  190. rnc_suspend_count, idev->rnc.suspend_count);
  191. #define MAX_SUSPEND_MSECS 10000
  192. if (ireq) {
  193. /* Terminate a specific TC. */
  194. set_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
  195. sci_remote_device_terminate_req(ihost, idev, 0, ireq);
  196. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  197. if (!wait_event_timeout(ihost->eventq,
  198. isci_check_reqterm(ihost, idev, ireq,
  199. rnc_suspend_count),
  200. msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
  201. dev_warn(&ihost->pdev->dev, "%s host%d timeout single\n",
  202. __func__, ihost->id);
  203. dev_dbg(&ihost->pdev->dev,
  204. "%s: ******* Timeout waiting for "
  205. "suspend; idev=%p, current state %s; "
  206. "started_request_count=%d, flags=%lx\n\t"
  207. "rnc_suspend_count=%d, rnc.suspend_count=%d "
  208. "RNC: current state %s, current "
  209. "suspend_type %x dest state %d;\n"
  210. "ireq=%p, ireq->flags = %lx\n",
  211. __func__, idev,
  212. dev_state_name(idev->sm.current_state_id),
  213. idev->started_request_count, idev->flags,
  214. rnc_suspend_count, idev->rnc.suspend_count,
  215. rnc_state_name(idev->rnc.sm.current_state_id),
  216. idev->rnc.suspend_type,
  217. idev->rnc.destination_state,
  218. ireq, ireq->flags);
  219. }
  220. spin_lock_irqsave(&ihost->scic_lock, flags);
  221. clear_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags);
  222. if (!test_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
  223. isci_free_tag(ihost, ireq->io_tag);
  224. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  225. } else {
  226. /* Terminate all TCs. */
  227. sci_remote_device_terminate_requests(idev);
  228. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  229. if (!wait_event_timeout(ihost->eventq,
  230. isci_check_devempty(ihost, idev,
  231. rnc_suspend_count),
  232. msecs_to_jiffies(MAX_SUSPEND_MSECS))) {
  233. dev_warn(&ihost->pdev->dev, "%s host%d timeout all\n",
  234. __func__, ihost->id);
  235. dev_dbg(&ihost->pdev->dev,
  236. "%s: ******* Timeout waiting for "
  237. "suspend; idev=%p, current state %s; "
  238. "started_request_count=%d, flags=%lx\n\t"
  239. "rnc_suspend_count=%d, "
  240. "RNC: current state %s, "
  241. "rnc.suspend_count=%d, current "
  242. "suspend_type %x dest state %d\n",
  243. __func__, idev,
  244. dev_state_name(idev->sm.current_state_id),
  245. idev->started_request_count, idev->flags,
  246. rnc_suspend_count,
  247. rnc_state_name(idev->rnc.sm.current_state_id),
  248. idev->rnc.suspend_count,
  249. idev->rnc.suspend_type,
  250. idev->rnc.destination_state);
  251. }
  252. }
  253. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, wait done\n",
  254. __func__, idev);
  255. isci_put_device(idev);
  256. }
  257. return status;
  258. }
  259. /**
  260. * isci_remote_device_not_ready() - This function is called by the ihost when
  261. * the remote device is not ready. We mark the isci device as ready (not
  262. * "ready_for_io") and signal the waiting proccess.
  263. * @isci_host: This parameter specifies the isci host object.
  264. * @isci_device: This parameter specifies the remote device
  265. *
  266. * sci_lock is held on entrance to this function.
  267. */
  268. static void isci_remote_device_not_ready(struct isci_host *ihost,
  269. struct isci_remote_device *idev,
  270. u32 reason)
  271. {
  272. dev_dbg(&ihost->pdev->dev,
  273. "%s: isci_device = %p; reason = %d\n", __func__, idev, reason);
  274. switch (reason) {
  275. case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
  276. set_bit(IDEV_IO_NCQERROR, &idev->flags);
  277. /* Suspend the remote device so the I/O can be terminated. */
  278. sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
  279. /* Kill all outstanding requests for the device. */
  280. sci_remote_device_terminate_requests(idev);
  281. /* Fall through into the default case... */
  282. default:
  283. clear_bit(IDEV_IO_READY, &idev->flags);
  284. break;
  285. }
  286. }
  287. /* called once the remote node context is ready to be freed.
  288. * The remote device can now report that its stop operation is complete. none
  289. */
  290. static void rnc_destruct_done(void *_dev)
  291. {
  292. struct isci_remote_device *idev = _dev;
  293. BUG_ON(idev->started_request_count != 0);
  294. sci_change_state(&idev->sm, SCI_DEV_STOPPED);
  295. }
  296. enum sci_status sci_remote_device_terminate_requests(
  297. struct isci_remote_device *idev)
  298. {
  299. return sci_remote_device_terminate_reqs_checkabort(idev, 0);
  300. }
  301. enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
  302. u32 timeout)
  303. {
  304. struct sci_base_state_machine *sm = &idev->sm;
  305. enum sci_remote_device_states state = sm->current_state_id;
  306. switch (state) {
  307. case SCI_DEV_INITIAL:
  308. case SCI_DEV_FAILED:
  309. case SCI_DEV_FINAL:
  310. default:
  311. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  312. __func__, dev_state_name(state));
  313. return SCI_FAILURE_INVALID_STATE;
  314. case SCI_DEV_STOPPED:
  315. return SCI_SUCCESS;
  316. case SCI_DEV_STARTING:
  317. /* device not started so there had better be no requests */
  318. BUG_ON(idev->started_request_count != 0);
  319. sci_remote_node_context_destruct(&idev->rnc,
  320. rnc_destruct_done, idev);
  321. /* Transition to the stopping state and wait for the
  322. * remote node to complete being posted and invalidated.
  323. */
  324. sci_change_state(sm, SCI_DEV_STOPPING);
  325. return SCI_SUCCESS;
  326. case SCI_DEV_READY:
  327. case SCI_STP_DEV_IDLE:
  328. case SCI_STP_DEV_CMD:
  329. case SCI_STP_DEV_NCQ:
  330. case SCI_STP_DEV_NCQ_ERROR:
  331. case SCI_STP_DEV_AWAIT_RESET:
  332. case SCI_SMP_DEV_IDLE:
  333. case SCI_SMP_DEV_CMD:
  334. sci_change_state(sm, SCI_DEV_STOPPING);
  335. if (idev->started_request_count == 0)
  336. sci_remote_node_context_destruct(&idev->rnc,
  337. rnc_destruct_done,
  338. idev);
  339. else {
  340. sci_remote_device_suspend(
  341. idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
  342. sci_remote_device_terminate_requests(idev);
  343. }
  344. return SCI_SUCCESS;
  345. case SCI_DEV_STOPPING:
  346. /* All requests should have been terminated, but if there is an
  347. * attempt to stop a device already in the stopping state, then
  348. * try again to terminate.
  349. */
  350. return sci_remote_device_terminate_requests(idev);
  351. case SCI_DEV_RESETTING:
  352. sci_change_state(sm, SCI_DEV_STOPPING);
  353. return SCI_SUCCESS;
  354. }
  355. }
  356. enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
  357. {
  358. struct sci_base_state_machine *sm = &idev->sm;
  359. enum sci_remote_device_states state = sm->current_state_id;
  360. switch (state) {
  361. case SCI_DEV_INITIAL:
  362. case SCI_DEV_STOPPED:
  363. case SCI_DEV_STARTING:
  364. case SCI_SMP_DEV_IDLE:
  365. case SCI_SMP_DEV_CMD:
  366. case SCI_DEV_STOPPING:
  367. case SCI_DEV_FAILED:
  368. case SCI_DEV_RESETTING:
  369. case SCI_DEV_FINAL:
  370. default:
  371. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  372. __func__, dev_state_name(state));
  373. return SCI_FAILURE_INVALID_STATE;
  374. case SCI_DEV_READY:
  375. case SCI_STP_DEV_IDLE:
  376. case SCI_STP_DEV_CMD:
  377. case SCI_STP_DEV_NCQ:
  378. case SCI_STP_DEV_NCQ_ERROR:
  379. case SCI_STP_DEV_AWAIT_RESET:
  380. sci_change_state(sm, SCI_DEV_RESETTING);
  381. return SCI_SUCCESS;
  382. }
  383. }
  384. enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
  385. {
  386. struct sci_base_state_machine *sm = &idev->sm;
  387. enum sci_remote_device_states state = sm->current_state_id;
  388. if (state != SCI_DEV_RESETTING) {
  389. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  390. __func__, dev_state_name(state));
  391. return SCI_FAILURE_INVALID_STATE;
  392. }
  393. sci_change_state(sm, SCI_DEV_READY);
  394. return SCI_SUCCESS;
  395. }
  396. enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
  397. u32 frame_index)
  398. {
  399. struct sci_base_state_machine *sm = &idev->sm;
  400. enum sci_remote_device_states state = sm->current_state_id;
  401. struct isci_host *ihost = idev->owning_port->owning_controller;
  402. enum sci_status status;
  403. switch (state) {
  404. case SCI_DEV_INITIAL:
  405. case SCI_DEV_STOPPED:
  406. case SCI_DEV_STARTING:
  407. case SCI_STP_DEV_IDLE:
  408. case SCI_SMP_DEV_IDLE:
  409. case SCI_DEV_FINAL:
  410. default:
  411. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  412. __func__, dev_state_name(state));
  413. /* Return the frame back to the controller */
  414. sci_controller_release_frame(ihost, frame_index);
  415. return SCI_FAILURE_INVALID_STATE;
  416. case SCI_DEV_READY:
  417. case SCI_STP_DEV_NCQ_ERROR:
  418. case SCI_STP_DEV_AWAIT_RESET:
  419. case SCI_DEV_STOPPING:
  420. case SCI_DEV_FAILED:
  421. case SCI_DEV_RESETTING: {
  422. struct isci_request *ireq;
  423. struct ssp_frame_hdr hdr;
  424. void *frame_header;
  425. ssize_t word_cnt;
  426. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  427. frame_index,
  428. &frame_header);
  429. if (status != SCI_SUCCESS)
  430. return status;
  431. word_cnt = sizeof(hdr) / sizeof(u32);
  432. sci_swab32_cpy(&hdr, frame_header, word_cnt);
  433. ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
  434. if (ireq && ireq->target_device == idev) {
  435. /* The IO request is now in charge of releasing the frame */
  436. status = sci_io_request_frame_handler(ireq, frame_index);
  437. } else {
  438. /* We could not map this tag to a valid IO
  439. * request Just toss the frame and continue
  440. */
  441. sci_controller_release_frame(ihost, frame_index);
  442. }
  443. break;
  444. }
  445. case SCI_STP_DEV_NCQ: {
  446. struct dev_to_host_fis *hdr;
  447. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  448. frame_index,
  449. (void **)&hdr);
  450. if (status != SCI_SUCCESS)
  451. return status;
  452. if (hdr->fis_type == FIS_SETDEVBITS &&
  453. (hdr->status & ATA_ERR)) {
  454. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  455. /* TODO Check sactive and complete associated IO if any. */
  456. sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
  457. } else if (hdr->fis_type == FIS_REGD2H &&
  458. (hdr->status & ATA_ERR)) {
  459. /*
  460. * Some devices return D2H FIS when an NCQ error is detected.
  461. * Treat this like an SDB error FIS ready reason.
  462. */
  463. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  464. sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
  465. } else
  466. status = SCI_FAILURE;
  467. sci_controller_release_frame(ihost, frame_index);
  468. break;
  469. }
  470. case SCI_STP_DEV_CMD:
  471. case SCI_SMP_DEV_CMD:
  472. /* The device does not process any UF received from the hardware while
  473. * in this state. All unsolicited frames are forwarded to the io request
  474. * object.
  475. */
  476. status = sci_io_request_frame_handler(idev->working_request, frame_index);
  477. break;
  478. }
  479. return status;
  480. }
  481. static bool is_remote_device_ready(struct isci_remote_device *idev)
  482. {
  483. struct sci_base_state_machine *sm = &idev->sm;
  484. enum sci_remote_device_states state = sm->current_state_id;
  485. switch (state) {
  486. case SCI_DEV_READY:
  487. case SCI_STP_DEV_IDLE:
  488. case SCI_STP_DEV_CMD:
  489. case SCI_STP_DEV_NCQ:
  490. case SCI_STP_DEV_NCQ_ERROR:
  491. case SCI_STP_DEV_AWAIT_RESET:
  492. case SCI_SMP_DEV_IDLE:
  493. case SCI_SMP_DEV_CMD:
  494. return true;
  495. default:
  496. return false;
  497. }
  498. }
  499. /*
  500. * called once the remote node context has transisitioned to a ready
  501. * state (after suspending RX and/or TX due to early D2H fis)
  502. */
  503. static void atapi_remote_device_resume_done(void *_dev)
  504. {
  505. struct isci_remote_device *idev = _dev;
  506. struct isci_request *ireq = idev->working_request;
  507. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  508. }
  509. enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
  510. u32 event_code)
  511. {
  512. enum sci_status status;
  513. struct sci_base_state_machine *sm = &idev->sm;
  514. enum sci_remote_device_states state = sm->current_state_id;
  515. switch (scu_get_event_type(event_code)) {
  516. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  517. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  518. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  519. status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
  520. break;
  521. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  522. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  523. status = SCI_SUCCESS;
  524. /* Suspend the associated RNC */
  525. sci_remote_device_suspend(idev, SCI_SW_SUSPEND_NORMAL);
  526. dev_dbg(scirdev_to_dev(idev),
  527. "%s: device: %p event code: %x: %s\n",
  528. __func__, idev, event_code,
  529. is_remote_device_ready(idev)
  530. ? "I_T_Nexus_Timeout event"
  531. : "I_T_Nexus_Timeout event in wrong state");
  532. break;
  533. }
  534. /* Else, fall through and treat as unhandled... */
  535. default:
  536. dev_dbg(scirdev_to_dev(idev),
  537. "%s: device: %p event code: %x: %s\n",
  538. __func__, idev, event_code,
  539. is_remote_device_ready(idev)
  540. ? "unexpected event"
  541. : "unexpected event in wrong state");
  542. status = SCI_FAILURE_INVALID_STATE;
  543. break;
  544. }
  545. if (status != SCI_SUCCESS)
  546. return status;
  547. /* Decode device-specific states that may require an RNC resume during
  548. * normal operation. When the abort path is active, these resumes are
  549. * managed when the abort path exits.
  550. */
  551. if (state == SCI_STP_DEV_ATAPI_ERROR) {
  552. /* For ATAPI error state resume the RNC right away. */
  553. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  554. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX) {
  555. return sci_remote_node_context_resume(&idev->rnc,
  556. atapi_remote_device_resume_done,
  557. idev);
  558. }
  559. }
  560. if (state == SCI_STP_DEV_IDLE) {
  561. /* We pick up suspension events to handle specifically to this
  562. * state. We resume the RNC right away.
  563. */
  564. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  565. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  566. status = sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
  567. }
  568. return status;
  569. }
  570. static void sci_remote_device_start_request(struct isci_remote_device *idev,
  571. struct isci_request *ireq,
  572. enum sci_status status)
  573. {
  574. struct isci_port *iport = idev->owning_port;
  575. /* cleanup requests that failed after starting on the port */
  576. if (status != SCI_SUCCESS)
  577. sci_port_complete_io(iport, idev, ireq);
  578. else {
  579. kref_get(&idev->kref);
  580. idev->started_request_count++;
  581. }
  582. }
  583. enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
  584. struct isci_remote_device *idev,
  585. struct isci_request *ireq)
  586. {
  587. struct sci_base_state_machine *sm = &idev->sm;
  588. enum sci_remote_device_states state = sm->current_state_id;
  589. struct isci_port *iport = idev->owning_port;
  590. enum sci_status status;
  591. switch (state) {
  592. case SCI_DEV_INITIAL:
  593. case SCI_DEV_STOPPED:
  594. case SCI_DEV_STARTING:
  595. case SCI_STP_DEV_NCQ_ERROR:
  596. case SCI_DEV_STOPPING:
  597. case SCI_DEV_FAILED:
  598. case SCI_DEV_RESETTING:
  599. case SCI_DEV_FINAL:
  600. default:
  601. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  602. __func__, dev_state_name(state));
  603. return SCI_FAILURE_INVALID_STATE;
  604. case SCI_DEV_READY:
  605. /* attempt to start an io request for this device object. The remote
  606. * device object will issue the start request for the io and if
  607. * successful it will start the request for the port object then
  608. * increment its own request count.
  609. */
  610. status = sci_port_start_io(iport, idev, ireq);
  611. if (status != SCI_SUCCESS)
  612. return status;
  613. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  614. if (status != SCI_SUCCESS)
  615. break;
  616. status = sci_request_start(ireq);
  617. break;
  618. case SCI_STP_DEV_IDLE: {
  619. /* handle the start io operation for a sata device that is in
  620. * the command idle state. - Evalute the type of IO request to
  621. * be started - If its an NCQ request change to NCQ substate -
  622. * If its any other command change to the CMD substate
  623. *
  624. * If this is a softreset we may want to have a different
  625. * substate.
  626. */
  627. enum sci_remote_device_states new_state;
  628. struct sas_task *task = isci_request_access_task(ireq);
  629. status = sci_port_start_io(iport, idev, ireq);
  630. if (status != SCI_SUCCESS)
  631. return status;
  632. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  633. if (status != SCI_SUCCESS)
  634. break;
  635. status = sci_request_start(ireq);
  636. if (status != SCI_SUCCESS)
  637. break;
  638. if (task->ata_task.use_ncq)
  639. new_state = SCI_STP_DEV_NCQ;
  640. else {
  641. idev->working_request = ireq;
  642. new_state = SCI_STP_DEV_CMD;
  643. }
  644. sci_change_state(sm, new_state);
  645. break;
  646. }
  647. case SCI_STP_DEV_NCQ: {
  648. struct sas_task *task = isci_request_access_task(ireq);
  649. if (task->ata_task.use_ncq) {
  650. status = sci_port_start_io(iport, idev, ireq);
  651. if (status != SCI_SUCCESS)
  652. return status;
  653. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  654. if (status != SCI_SUCCESS)
  655. break;
  656. status = sci_request_start(ireq);
  657. } else
  658. return SCI_FAILURE_INVALID_STATE;
  659. break;
  660. }
  661. case SCI_STP_DEV_AWAIT_RESET:
  662. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  663. case SCI_SMP_DEV_IDLE:
  664. status = sci_port_start_io(iport, idev, ireq);
  665. if (status != SCI_SUCCESS)
  666. return status;
  667. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  668. if (status != SCI_SUCCESS)
  669. break;
  670. status = sci_request_start(ireq);
  671. if (status != SCI_SUCCESS)
  672. break;
  673. idev->working_request = ireq;
  674. sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
  675. break;
  676. case SCI_STP_DEV_CMD:
  677. case SCI_SMP_DEV_CMD:
  678. /* device is already handling a command it can not accept new commands
  679. * until this one is complete.
  680. */
  681. return SCI_FAILURE_INVALID_STATE;
  682. }
  683. sci_remote_device_start_request(idev, ireq, status);
  684. return status;
  685. }
  686. static enum sci_status common_complete_io(struct isci_port *iport,
  687. struct isci_remote_device *idev,
  688. struct isci_request *ireq)
  689. {
  690. enum sci_status status;
  691. status = sci_request_complete(ireq);
  692. if (status != SCI_SUCCESS)
  693. return status;
  694. status = sci_port_complete_io(iport, idev, ireq);
  695. if (status != SCI_SUCCESS)
  696. return status;
  697. sci_remote_device_decrement_request_count(idev);
  698. return status;
  699. }
  700. enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
  701. struct isci_remote_device *idev,
  702. struct isci_request *ireq)
  703. {
  704. struct sci_base_state_machine *sm = &idev->sm;
  705. enum sci_remote_device_states state = sm->current_state_id;
  706. struct isci_port *iport = idev->owning_port;
  707. enum sci_status status;
  708. switch (state) {
  709. case SCI_DEV_INITIAL:
  710. case SCI_DEV_STOPPED:
  711. case SCI_DEV_STARTING:
  712. case SCI_STP_DEV_IDLE:
  713. case SCI_SMP_DEV_IDLE:
  714. case SCI_DEV_FAILED:
  715. case SCI_DEV_FINAL:
  716. default:
  717. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  718. __func__, dev_state_name(state));
  719. return SCI_FAILURE_INVALID_STATE;
  720. case SCI_DEV_READY:
  721. case SCI_STP_DEV_AWAIT_RESET:
  722. case SCI_DEV_RESETTING:
  723. status = common_complete_io(iport, idev, ireq);
  724. break;
  725. case SCI_STP_DEV_CMD:
  726. case SCI_STP_DEV_NCQ:
  727. case SCI_STP_DEV_NCQ_ERROR:
  728. case SCI_STP_DEV_ATAPI_ERROR:
  729. status = common_complete_io(iport, idev, ireq);
  730. if (status != SCI_SUCCESS)
  731. break;
  732. if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  733. /* This request causes hardware error, device needs to be Lun Reset.
  734. * So here we force the state machine to IDLE state so the rest IOs
  735. * can reach RNC state handler, these IOs will be completed by RNC with
  736. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  737. */
  738. sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
  739. } else if (idev->started_request_count == 0)
  740. sci_change_state(sm, SCI_STP_DEV_IDLE);
  741. break;
  742. case SCI_SMP_DEV_CMD:
  743. status = common_complete_io(iport, idev, ireq);
  744. if (status != SCI_SUCCESS)
  745. break;
  746. sci_change_state(sm, SCI_SMP_DEV_IDLE);
  747. break;
  748. case SCI_DEV_STOPPING:
  749. status = common_complete_io(iport, idev, ireq);
  750. if (status != SCI_SUCCESS)
  751. break;
  752. if (idev->started_request_count == 0)
  753. sci_remote_node_context_destruct(&idev->rnc,
  754. rnc_destruct_done,
  755. idev);
  756. break;
  757. }
  758. if (status != SCI_SUCCESS)
  759. dev_err(scirdev_to_dev(idev),
  760. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  761. "could not complete\n", __func__, iport,
  762. idev, ireq, status);
  763. else
  764. isci_put_device(idev);
  765. return status;
  766. }
  767. static void sci_remote_device_continue_request(void *dev)
  768. {
  769. struct isci_remote_device *idev = dev;
  770. /* we need to check if this request is still valid to continue. */
  771. if (idev->working_request)
  772. sci_controller_continue_io(idev->working_request);
  773. }
  774. enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
  775. struct isci_remote_device *idev,
  776. struct isci_request *ireq)
  777. {
  778. struct sci_base_state_machine *sm = &idev->sm;
  779. enum sci_remote_device_states state = sm->current_state_id;
  780. struct isci_port *iport = idev->owning_port;
  781. enum sci_status status;
  782. switch (state) {
  783. case SCI_DEV_INITIAL:
  784. case SCI_DEV_STOPPED:
  785. case SCI_DEV_STARTING:
  786. case SCI_SMP_DEV_IDLE:
  787. case SCI_SMP_DEV_CMD:
  788. case SCI_DEV_STOPPING:
  789. case SCI_DEV_FAILED:
  790. case SCI_DEV_RESETTING:
  791. case SCI_DEV_FINAL:
  792. default:
  793. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  794. __func__, dev_state_name(state));
  795. return SCI_FAILURE_INVALID_STATE;
  796. case SCI_STP_DEV_IDLE:
  797. case SCI_STP_DEV_CMD:
  798. case SCI_STP_DEV_NCQ:
  799. case SCI_STP_DEV_NCQ_ERROR:
  800. case SCI_STP_DEV_AWAIT_RESET:
  801. status = sci_port_start_io(iport, idev, ireq);
  802. if (status != SCI_SUCCESS)
  803. return status;
  804. status = sci_request_start(ireq);
  805. if (status != SCI_SUCCESS)
  806. goto out;
  807. /* Note: If the remote device state is not IDLE this will
  808. * replace the request that probably resulted in the task
  809. * management request.
  810. */
  811. idev->working_request = ireq;
  812. sci_change_state(sm, SCI_STP_DEV_CMD);
  813. /* The remote node context must cleanup the TCi to NCQ mapping
  814. * table. The only way to do this correctly is to either write
  815. * to the TLCR register or to invalidate and repost the RNC. In
  816. * either case the remote node context state machine will take
  817. * the correct action when the remote node context is suspended
  818. * and later resumed.
  819. */
  820. sci_remote_device_suspend(idev,
  821. SCI_SW_SUSPEND_LINKHANG_DETECT);
  822. status = sci_remote_node_context_start_task(&idev->rnc, ireq,
  823. sci_remote_device_continue_request, idev);
  824. out:
  825. sci_remote_device_start_request(idev, ireq, status);
  826. /* We need to let the controller start request handler know that
  827. * it can't post TC yet. We will provide a callback function to
  828. * post TC when RNC gets resumed.
  829. */
  830. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  831. case SCI_DEV_READY:
  832. status = sci_port_start_io(iport, idev, ireq);
  833. if (status != SCI_SUCCESS)
  834. return status;
  835. /* Resume the RNC as needed: */
  836. status = sci_remote_node_context_start_task(&idev->rnc, ireq,
  837. NULL, NULL);
  838. if (status != SCI_SUCCESS)
  839. break;
  840. status = sci_request_start(ireq);
  841. break;
  842. }
  843. sci_remote_device_start_request(idev, ireq, status);
  844. return status;
  845. }
  846. void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
  847. {
  848. struct isci_port *iport = idev->owning_port;
  849. u32 context;
  850. context = request |
  851. (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  852. (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  853. idev->rnc.remote_node_index;
  854. sci_controller_post_request(iport->owning_controller, context);
  855. }
  856. /* called once the remote node context has transisitioned to a
  857. * ready state. This is the indication that the remote device object can also
  858. * transition to ready.
  859. */
  860. static void remote_device_resume_done(void *_dev)
  861. {
  862. struct isci_remote_device *idev = _dev;
  863. if (is_remote_device_ready(idev))
  864. return;
  865. /* go 'ready' if we are not already in a ready state */
  866. sci_change_state(&idev->sm, SCI_DEV_READY);
  867. }
  868. static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  869. {
  870. struct isci_remote_device *idev = _dev;
  871. struct isci_host *ihost = idev->owning_port->owning_controller;
  872. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  873. * As a result, avoid sending the ready notification.
  874. */
  875. if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
  876. isci_remote_device_ready(ihost, idev);
  877. }
  878. static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
  879. {
  880. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  881. /* Initial state is a transitional state to the stopped state */
  882. sci_change_state(&idev->sm, SCI_DEV_STOPPED);
  883. }
  884. /**
  885. * sci_remote_device_destruct() - free remote node context and destruct
  886. * @remote_device: This parameter specifies the remote device to be destructed.
  887. *
  888. * Remote device objects are a limited resource. As such, they must be
  889. * protected. Thus calls to construct and destruct are mutually exclusive and
  890. * non-reentrant. The return value shall indicate if the device was
  891. * successfully destructed or if some failure occurred. enum sci_status This value
  892. * is returned if the device is successfully destructed.
  893. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  894. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  895. * valid, etc.).
  896. */
  897. static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
  898. {
  899. struct sci_base_state_machine *sm = &idev->sm;
  900. enum sci_remote_device_states state = sm->current_state_id;
  901. struct isci_host *ihost;
  902. if (state != SCI_DEV_STOPPED) {
  903. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  904. __func__, dev_state_name(state));
  905. return SCI_FAILURE_INVALID_STATE;
  906. }
  907. ihost = idev->owning_port->owning_controller;
  908. sci_controller_free_remote_node_context(ihost, idev,
  909. idev->rnc.remote_node_index);
  910. idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  911. sci_change_state(sm, SCI_DEV_FINAL);
  912. return SCI_SUCCESS;
  913. }
  914. /**
  915. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  916. * @ihost: This parameter specifies the isci host object.
  917. * @idev: This parameter specifies the remote device to be freed.
  918. *
  919. */
  920. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  921. {
  922. dev_dbg(&ihost->pdev->dev,
  923. "%s: isci_device = %p\n", __func__, idev);
  924. /* There should not be any outstanding io's. All paths to
  925. * here should go through isci_remote_device_nuke_requests.
  926. * If we hit this condition, we will need a way to complete
  927. * io requests in process */
  928. BUG_ON(idev->started_request_count > 0);
  929. sci_remote_device_destruct(idev);
  930. list_del_init(&idev->node);
  931. isci_put_device(idev);
  932. }
  933. static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
  934. {
  935. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  936. struct isci_host *ihost = idev->owning_port->owning_controller;
  937. u32 prev_state;
  938. /* If we are entering from the stopping state let the SCI User know that
  939. * the stop operation has completed.
  940. */
  941. prev_state = idev->sm.previous_state_id;
  942. if (prev_state == SCI_DEV_STOPPING)
  943. isci_remote_device_deconstruct(ihost, idev);
  944. sci_controller_remote_device_stopped(ihost, idev);
  945. }
  946. static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
  947. {
  948. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  949. struct isci_host *ihost = idev->owning_port->owning_controller;
  950. isci_remote_device_not_ready(ihost, idev,
  951. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  952. }
  953. static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
  954. {
  955. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  956. struct isci_host *ihost = idev->owning_port->owning_controller;
  957. struct domain_device *dev = idev->domain_dev;
  958. if (dev->dev_type == SAS_SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  959. sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
  960. } else if (dev_is_expander(dev)) {
  961. sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
  962. } else
  963. isci_remote_device_ready(ihost, idev);
  964. }
  965. static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
  966. {
  967. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  968. struct domain_device *dev = idev->domain_dev;
  969. if (dev->dev_type == SAS_END_DEVICE) {
  970. struct isci_host *ihost = idev->owning_port->owning_controller;
  971. isci_remote_device_not_ready(ihost, idev,
  972. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  973. }
  974. }
  975. static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
  976. {
  977. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  978. struct isci_host *ihost = idev->owning_port->owning_controller;
  979. dev_dbg(&ihost->pdev->dev,
  980. "%s: isci_device = %p\n", __func__, idev);
  981. sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
  982. }
  983. static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
  984. {
  985. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  986. struct isci_host *ihost = idev->owning_port->owning_controller;
  987. dev_dbg(&ihost->pdev->dev,
  988. "%s: isci_device = %p\n", __func__, idev);
  989. sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
  990. }
  991. static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  992. {
  993. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  994. idev->working_request = NULL;
  995. if (sci_remote_node_context_is_ready(&idev->rnc)) {
  996. /*
  997. * Since the RNC is ready, it's alright to finish completion
  998. * processing (e.g. signal the remote device is ready). */
  999. sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
  1000. } else {
  1001. sci_remote_node_context_resume(&idev->rnc,
  1002. sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
  1003. idev);
  1004. }
  1005. }
  1006. static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  1007. {
  1008. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  1009. struct isci_host *ihost = idev->owning_port->owning_controller;
  1010. BUG_ON(idev->working_request == NULL);
  1011. isci_remote_device_not_ready(ihost, idev,
  1012. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  1013. }
  1014. static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
  1015. {
  1016. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  1017. struct isci_host *ihost = idev->owning_port->owning_controller;
  1018. if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  1019. isci_remote_device_not_ready(ihost, idev,
  1020. idev->not_ready_reason);
  1021. }
  1022. static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  1023. {
  1024. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  1025. struct isci_host *ihost = idev->owning_port->owning_controller;
  1026. isci_remote_device_ready(ihost, idev);
  1027. }
  1028. static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  1029. {
  1030. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  1031. struct isci_host *ihost = idev->owning_port->owning_controller;
  1032. BUG_ON(idev->working_request == NULL);
  1033. isci_remote_device_not_ready(ihost, idev,
  1034. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  1035. }
  1036. static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
  1037. {
  1038. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  1039. idev->working_request = NULL;
  1040. }
  1041. static const struct sci_base_state sci_remote_device_state_table[] = {
  1042. [SCI_DEV_INITIAL] = {
  1043. .enter_state = sci_remote_device_initial_state_enter,
  1044. },
  1045. [SCI_DEV_STOPPED] = {
  1046. .enter_state = sci_remote_device_stopped_state_enter,
  1047. },
  1048. [SCI_DEV_STARTING] = {
  1049. .enter_state = sci_remote_device_starting_state_enter,
  1050. },
  1051. [SCI_DEV_READY] = {
  1052. .enter_state = sci_remote_device_ready_state_enter,
  1053. .exit_state = sci_remote_device_ready_state_exit
  1054. },
  1055. [SCI_STP_DEV_IDLE] = {
  1056. .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
  1057. },
  1058. [SCI_STP_DEV_CMD] = {
  1059. .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
  1060. },
  1061. [SCI_STP_DEV_NCQ] = { },
  1062. [SCI_STP_DEV_NCQ_ERROR] = {
  1063. .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
  1064. },
  1065. [SCI_STP_DEV_ATAPI_ERROR] = { },
  1066. [SCI_STP_DEV_AWAIT_RESET] = { },
  1067. [SCI_SMP_DEV_IDLE] = {
  1068. .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
  1069. },
  1070. [SCI_SMP_DEV_CMD] = {
  1071. .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
  1072. .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
  1073. },
  1074. [SCI_DEV_STOPPING] = { },
  1075. [SCI_DEV_FAILED] = { },
  1076. [SCI_DEV_RESETTING] = {
  1077. .enter_state = sci_remote_device_resetting_state_enter,
  1078. .exit_state = sci_remote_device_resetting_state_exit
  1079. },
  1080. [SCI_DEV_FINAL] = { },
  1081. };
  1082. /**
  1083. * sci_remote_device_construct() - common construction
  1084. * @sci_port: SAS/SATA port through which this device is accessed.
  1085. * @sci_dev: remote device to construct
  1086. *
  1087. * This routine just performs benign initialization and does not
  1088. * allocate the remote_node_context which is left to
  1089. * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
  1090. * frees the remote_node_context(s) for the device.
  1091. */
  1092. static void sci_remote_device_construct(struct isci_port *iport,
  1093. struct isci_remote_device *idev)
  1094. {
  1095. idev->owning_port = iport;
  1096. idev->started_request_count = 0;
  1097. sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
  1098. sci_remote_node_context_construct(&idev->rnc,
  1099. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1100. }
  1101. /**
  1102. * sci_remote_device_da_construct() - construct direct attached device.
  1103. *
  1104. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1105. * the device is known to the SCI Core since it is contained in the
  1106. * sci_phy object. Remote node context(s) is/are a global resource
  1107. * allocated by this routine, freed by sci_remote_device_destruct().
  1108. *
  1109. * Returns:
  1110. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1111. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1112. * sata-only controller instance.
  1113. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1114. */
  1115. static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
  1116. struct isci_remote_device *idev)
  1117. {
  1118. enum sci_status status;
  1119. struct sci_port_properties properties;
  1120. sci_remote_device_construct(iport, idev);
  1121. sci_port_get_properties(iport, &properties);
  1122. /* Get accurate port width from port's phy mask for a DA device. */
  1123. idev->device_port_width = hweight32(properties.phy_mask);
  1124. status = sci_controller_allocate_remote_node_context(iport->owning_controller,
  1125. idev,
  1126. &idev->rnc.remote_node_index);
  1127. if (status != SCI_SUCCESS)
  1128. return status;
  1129. idev->connection_rate = sci_port_get_max_allowed_speed(iport);
  1130. return SCI_SUCCESS;
  1131. }
  1132. /**
  1133. * sci_remote_device_ea_construct() - construct expander attached device
  1134. *
  1135. * Remote node context(s) is/are a global resource allocated by this
  1136. * routine, freed by sci_remote_device_destruct().
  1137. *
  1138. * Returns:
  1139. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1140. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1141. * sata-only controller instance.
  1142. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1143. */
  1144. static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
  1145. struct isci_remote_device *idev)
  1146. {
  1147. struct domain_device *dev = idev->domain_dev;
  1148. enum sci_status status;
  1149. sci_remote_device_construct(iport, idev);
  1150. status = sci_controller_allocate_remote_node_context(iport->owning_controller,
  1151. idev,
  1152. &idev->rnc.remote_node_index);
  1153. if (status != SCI_SUCCESS)
  1154. return status;
  1155. /* For SAS-2 the physical link rate is actually a logical link
  1156. * rate that incorporates multiplexing. The SCU doesn't
  1157. * incorporate multiplexing and for the purposes of the
  1158. * connection the logical link rate is that same as the
  1159. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1160. * one another, so this code works for both situations.
  1161. */
  1162. idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
  1163. dev->linkrate);
  1164. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1165. idev->device_port_width = 1;
  1166. return SCI_SUCCESS;
  1167. }
  1168. enum sci_status sci_remote_device_resume(
  1169. struct isci_remote_device *idev,
  1170. scics_sds_remote_node_context_callback cb_fn,
  1171. void *cb_p)
  1172. {
  1173. enum sci_status status;
  1174. status = sci_remote_node_context_resume(&idev->rnc, cb_fn, cb_p);
  1175. if (status != SCI_SUCCESS)
  1176. dev_dbg(scirdev_to_dev(idev), "%s: failed to resume: %d\n",
  1177. __func__, status);
  1178. return status;
  1179. }
  1180. static void isci_remote_device_resume_from_abort_complete(void *cbparam)
  1181. {
  1182. struct isci_remote_device *idev = cbparam;
  1183. struct isci_host *ihost = idev->owning_port->owning_controller;
  1184. scics_sds_remote_node_context_callback abort_resume_cb =
  1185. idev->abort_resume_cb;
  1186. dev_dbg(scirdev_to_dev(idev), "%s: passing-along resume: %p\n",
  1187. __func__, abort_resume_cb);
  1188. if (abort_resume_cb != NULL) {
  1189. idev->abort_resume_cb = NULL;
  1190. abort_resume_cb(idev->abort_resume_cbparam);
  1191. }
  1192. clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
  1193. wake_up(&ihost->eventq);
  1194. }
  1195. static bool isci_remote_device_test_resume_done(
  1196. struct isci_host *ihost,
  1197. struct isci_remote_device *idev)
  1198. {
  1199. unsigned long flags;
  1200. bool done;
  1201. spin_lock_irqsave(&ihost->scic_lock, flags);
  1202. done = !test_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags)
  1203. || test_bit(IDEV_STOP_PENDING, &idev->flags)
  1204. || sci_remote_node_context_is_being_destroyed(&idev->rnc);
  1205. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1206. return done;
  1207. }
  1208. void isci_remote_device_wait_for_resume_from_abort(
  1209. struct isci_host *ihost,
  1210. struct isci_remote_device *idev)
  1211. {
  1212. dev_dbg(&ihost->pdev->dev, "%s: starting resume wait: %p\n",
  1213. __func__, idev);
  1214. #define MAX_RESUME_MSECS 10000
  1215. if (!wait_event_timeout(ihost->eventq,
  1216. isci_remote_device_test_resume_done(ihost, idev),
  1217. msecs_to_jiffies(MAX_RESUME_MSECS))) {
  1218. dev_warn(&ihost->pdev->dev, "%s: #### Timeout waiting for "
  1219. "resume: %p\n", __func__, idev);
  1220. }
  1221. clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
  1222. dev_dbg(&ihost->pdev->dev, "%s: resume wait done: %p\n",
  1223. __func__, idev);
  1224. }
  1225. enum sci_status isci_remote_device_resume_from_abort(
  1226. struct isci_host *ihost,
  1227. struct isci_remote_device *idev)
  1228. {
  1229. unsigned long flags;
  1230. enum sci_status status = SCI_SUCCESS;
  1231. int destroyed;
  1232. spin_lock_irqsave(&ihost->scic_lock, flags);
  1233. /* Preserve any current resume callbacks, for instance from other
  1234. * resumptions.
  1235. */
  1236. idev->abort_resume_cb = idev->rnc.user_callback;
  1237. idev->abort_resume_cbparam = idev->rnc.user_cookie;
  1238. set_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
  1239. clear_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
  1240. destroyed = sci_remote_node_context_is_being_destroyed(&idev->rnc);
  1241. if (!destroyed)
  1242. status = sci_remote_device_resume(
  1243. idev, isci_remote_device_resume_from_abort_complete,
  1244. idev);
  1245. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1246. if (!destroyed && (status == SCI_SUCCESS))
  1247. isci_remote_device_wait_for_resume_from_abort(ihost, idev);
  1248. else
  1249. clear_bit(IDEV_ABORT_PATH_RESUME_PENDING, &idev->flags);
  1250. return status;
  1251. }
  1252. /**
  1253. * sci_remote_device_start() - This method will start the supplied remote
  1254. * device. This method enables normal IO requests to flow through to the
  1255. * remote device.
  1256. * @remote_device: This parameter specifies the device to be started.
  1257. * @timeout: This parameter specifies the number of milliseconds in which the
  1258. * start operation should complete.
  1259. *
  1260. * An indication of whether the device was successfully started. SCI_SUCCESS
  1261. * This value is returned if the device was successfully started.
  1262. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1263. * the device when there have been no phys added to it.
  1264. */
  1265. static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
  1266. u32 timeout)
  1267. {
  1268. struct sci_base_state_machine *sm = &idev->sm;
  1269. enum sci_remote_device_states state = sm->current_state_id;
  1270. enum sci_status status;
  1271. if (state != SCI_DEV_STOPPED) {
  1272. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  1273. __func__, dev_state_name(state));
  1274. return SCI_FAILURE_INVALID_STATE;
  1275. }
  1276. status = sci_remote_device_resume(idev, remote_device_resume_done,
  1277. idev);
  1278. if (status != SCI_SUCCESS)
  1279. return status;
  1280. sci_change_state(sm, SCI_DEV_STARTING);
  1281. return SCI_SUCCESS;
  1282. }
  1283. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1284. struct isci_remote_device *idev)
  1285. {
  1286. struct isci_host *ihost = iport->isci_host;
  1287. struct domain_device *dev = idev->domain_dev;
  1288. enum sci_status status;
  1289. if (dev->parent && dev_is_expander(dev->parent))
  1290. status = sci_remote_device_ea_construct(iport, idev);
  1291. else
  1292. status = sci_remote_device_da_construct(iport, idev);
  1293. if (status != SCI_SUCCESS) {
  1294. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1295. __func__, status);
  1296. return status;
  1297. }
  1298. /* start the device. */
  1299. status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1300. if (status != SCI_SUCCESS)
  1301. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1302. status);
  1303. return status;
  1304. }
  1305. /**
  1306. * This function builds the isci_remote_device when a libsas dev_found message
  1307. * is received.
  1308. * @isci_host: This parameter specifies the isci host object.
  1309. * @port: This parameter specifies the isci_port conected to this device.
  1310. *
  1311. * pointer to new isci_remote_device.
  1312. */
  1313. static struct isci_remote_device *
  1314. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1315. {
  1316. struct isci_remote_device *idev;
  1317. int i;
  1318. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1319. idev = &ihost->devices[i];
  1320. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1321. break;
  1322. }
  1323. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1324. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1325. return NULL;
  1326. }
  1327. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1328. return NULL;
  1329. return idev;
  1330. }
  1331. void isci_remote_device_release(struct kref *kref)
  1332. {
  1333. struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
  1334. struct isci_host *ihost = idev->isci_port->isci_host;
  1335. idev->domain_dev = NULL;
  1336. idev->isci_port = NULL;
  1337. clear_bit(IDEV_START_PENDING, &idev->flags);
  1338. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1339. clear_bit(IDEV_IO_READY, &idev->flags);
  1340. clear_bit(IDEV_GONE, &idev->flags);
  1341. smp_mb__before_atomic();
  1342. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1343. wake_up(&ihost->eventq);
  1344. }
  1345. /**
  1346. * isci_remote_device_stop() - This function is called internally to stop the
  1347. * remote device.
  1348. * @isci_host: This parameter specifies the isci host object.
  1349. * @isci_device: This parameter specifies the remote device.
  1350. *
  1351. * The status of the ihost request to stop.
  1352. */
  1353. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1354. {
  1355. enum sci_status status;
  1356. unsigned long flags;
  1357. dev_dbg(&ihost->pdev->dev,
  1358. "%s: isci_device = %p\n", __func__, idev);
  1359. spin_lock_irqsave(&ihost->scic_lock, flags);
  1360. idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
  1361. set_bit(IDEV_GONE, &idev->flags);
  1362. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1363. status = sci_remote_device_stop(idev, 50);
  1364. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1365. /* Wait for the stop complete callback. */
  1366. if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
  1367. /* nothing to wait for */;
  1368. else
  1369. wait_for_device_stop(ihost, idev);
  1370. dev_dbg(&ihost->pdev->dev,
  1371. "%s: isci_device = %p, waiting done.\n", __func__, idev);
  1372. return status;
  1373. }
  1374. /**
  1375. * isci_remote_device_gone() - This function is called by libsas when a domain
  1376. * device is removed.
  1377. * @domain_device: This parameter specifies the libsas domain device.
  1378. *
  1379. */
  1380. void isci_remote_device_gone(struct domain_device *dev)
  1381. {
  1382. struct isci_host *ihost = dev_to_ihost(dev);
  1383. struct isci_remote_device *idev = dev->lldd_dev;
  1384. dev_dbg(&ihost->pdev->dev,
  1385. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1386. __func__, dev, idev, idev->isci_port);
  1387. isci_remote_device_stop(ihost, idev);
  1388. }
  1389. /**
  1390. * isci_remote_device_found() - This function is called by libsas when a remote
  1391. * device is discovered. A remote device object is created and started. the
  1392. * function then sleeps until the sci core device started message is
  1393. * received.
  1394. * @domain_device: This parameter specifies the libsas domain device.
  1395. *
  1396. * status, zero indicates success.
  1397. */
  1398. int isci_remote_device_found(struct domain_device *dev)
  1399. {
  1400. struct isci_host *isci_host = dev_to_ihost(dev);
  1401. struct isci_port *isci_port = dev->port->lldd_port;
  1402. struct isci_remote_device *isci_device;
  1403. enum sci_status status;
  1404. dev_dbg(&isci_host->pdev->dev,
  1405. "%s: domain_device = %p\n", __func__, dev);
  1406. if (!isci_port)
  1407. return -ENODEV;
  1408. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1409. if (!isci_device)
  1410. return -ENODEV;
  1411. kref_init(&isci_device->kref);
  1412. INIT_LIST_HEAD(&isci_device->node);
  1413. spin_lock_irq(&isci_host->scic_lock);
  1414. isci_device->domain_dev = dev;
  1415. isci_device->isci_port = isci_port;
  1416. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1417. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1418. status = isci_remote_device_construct(isci_port, isci_device);
  1419. dev_dbg(&isci_host->pdev->dev,
  1420. "%s: isci_device = %p\n",
  1421. __func__, isci_device);
  1422. if (status == SCI_SUCCESS) {
  1423. /* device came up, advertise it to the world */
  1424. dev->lldd_dev = isci_device;
  1425. } else
  1426. isci_put_device(isci_device);
  1427. spin_unlock_irq(&isci_host->scic_lock);
  1428. /* wait for the device ready callback. */
  1429. wait_for_device_start(isci_host, isci_device);
  1430. return status == SCI_SUCCESS ? 0 : -ENODEV;
  1431. }
  1432. enum sci_status isci_remote_device_suspend_terminate(
  1433. struct isci_host *ihost,
  1434. struct isci_remote_device *idev,
  1435. struct isci_request *ireq)
  1436. {
  1437. unsigned long flags;
  1438. enum sci_status status;
  1439. /* Put the device into suspension. */
  1440. spin_lock_irqsave(&ihost->scic_lock, flags);
  1441. set_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags);
  1442. sci_remote_device_suspend(idev, SCI_SW_SUSPEND_LINKHANG_DETECT);
  1443. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1444. /* Terminate and wait for the completions. */
  1445. status = isci_remote_device_terminate_requests(ihost, idev, ireq);
  1446. if (status != SCI_SUCCESS)
  1447. dev_dbg(&ihost->pdev->dev,
  1448. "%s: isci_remote_device_terminate_requests(%p) "
  1449. "returned %d!\n",
  1450. __func__, idev, status);
  1451. /* NOTE: RNC resumption is left to the caller! */
  1452. return status;
  1453. }
  1454. int isci_remote_device_is_safe_to_abort(
  1455. struct isci_remote_device *idev)
  1456. {
  1457. return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
  1458. }
  1459. enum sci_status sci_remote_device_abort_requests_pending_abort(
  1460. struct isci_remote_device *idev)
  1461. {
  1462. return sci_remote_device_terminate_reqs_checkabort(idev, 1);
  1463. }
  1464. enum sci_status isci_remote_device_reset_complete(
  1465. struct isci_host *ihost,
  1466. struct isci_remote_device *idev)
  1467. {
  1468. unsigned long flags;
  1469. enum sci_status status;
  1470. spin_lock_irqsave(&ihost->scic_lock, flags);
  1471. status = sci_remote_device_reset_complete(idev);
  1472. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1473. return status;
  1474. }
  1475. void isci_dev_set_hang_detection_timeout(
  1476. struct isci_remote_device *idev,
  1477. u32 timeout)
  1478. {
  1479. if (dev_is_sata(idev->domain_dev)) {
  1480. if (timeout) {
  1481. if (test_and_set_bit(IDEV_RNC_LLHANG_ENABLED,
  1482. &idev->flags))
  1483. return; /* Already enabled. */
  1484. } else if (!test_and_clear_bit(IDEV_RNC_LLHANG_ENABLED,
  1485. &idev->flags))
  1486. return; /* Not enabled. */
  1487. sci_port_set_hang_detection_timeout(idev->owning_port,
  1488. timeout);
  1489. }
  1490. }