remote_device.c 52 KB

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