remote_device.c 52 KB

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