remote_device.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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 "intel_sas.h"
  56. #include "intel_ata.h"
  57. #include "isci.h"
  58. #include "port.h"
  59. #include "remote_device.h"
  60. #include "request.h"
  61. #include "scic_controller.h"
  62. #include "scic_io_request.h"
  63. #include "scic_phy.h"
  64. #include "scic_port.h"
  65. #include "scic_sds_controller.h"
  66. #include "scic_sds_phy.h"
  67. #include "scic_sds_port.h"
  68. #include "remote_node_context.h"
  69. #include "scic_sds_request.h"
  70. #include "sci_environment.h"
  71. #include "sci_util.h"
  72. #include "scu_event_codes.h"
  73. #include "task.h"
  74. /**
  75. * isci_remote_device_change_state() - This function gets the status of the
  76. * remote_device object.
  77. * @isci_device: This parameter points to the isci_remote_device object
  78. *
  79. * status of the object as a isci_status enum.
  80. */
  81. void isci_remote_device_change_state(
  82. struct isci_remote_device *isci_device,
  83. enum isci_status status)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&isci_device->state_lock, flags);
  87. isci_device->status = status;
  88. spin_unlock_irqrestore(&isci_device->state_lock, flags);
  89. }
  90. /**
  91. * isci_remote_device_not_ready() - This function is called by the scic when
  92. * the remote device is not ready. We mark the isci device as ready (not
  93. * "ready_for_io") and signal the waiting proccess.
  94. * @isci_host: This parameter specifies the isci host object.
  95. * @isci_device: This parameter specifies the remote device
  96. *
  97. */
  98. static void isci_remote_device_not_ready(struct isci_host *ihost,
  99. struct isci_remote_device *idev, u32 reason)
  100. {
  101. dev_dbg(&ihost->pdev->dev,
  102. "%s: isci_device = %p\n", __func__, idev);
  103. if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
  104. isci_remote_device_change_state(idev, isci_stopping);
  105. else
  106. /* device ready is actually a "not ready for io" state. */
  107. isci_remote_device_change_state(idev, isci_ready);
  108. }
  109. /**
  110. * isci_remote_device_ready() - This function is called by the scic when the
  111. * remote device is ready. We mark the isci device as ready and signal the
  112. * waiting proccess.
  113. * @ihost: our valid isci_host
  114. * @idev: remote device
  115. *
  116. */
  117. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  118. {
  119. dev_dbg(&ihost->pdev->dev,
  120. "%s: idev = %p\n", __func__, idev);
  121. isci_remote_device_change_state(idev, isci_ready_for_io);
  122. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  123. wake_up(&ihost->eventq);
  124. }
  125. /* called once the remote node context is ready to be freed.
  126. * The remote device can now report that its stop operation is complete. none
  127. */
  128. static void rnc_destruct_done(void *_dev)
  129. {
  130. struct scic_sds_remote_device *sci_dev = _dev;
  131. BUG_ON(sci_dev->started_request_count != 0);
  132. sci_base_state_machine_change_state(&sci_dev->state_machine,
  133. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  134. }
  135. static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
  136. {
  137. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  138. u32 i, request_count = sci_dev->started_request_count;
  139. enum sci_status status = SCI_SUCCESS;
  140. for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
  141. struct scic_sds_request *sci_req;
  142. enum sci_status s;
  143. sci_req = scic->io_request_table[i];
  144. if (!sci_req || sci_req->target_device != sci_dev)
  145. continue;
  146. s = scic_controller_terminate_request(scic, sci_dev, sci_req);
  147. if (s != SCI_SUCCESS)
  148. status = s;
  149. }
  150. return status;
  151. }
  152. enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
  153. u32 timeout)
  154. {
  155. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  156. enum scic_sds_remote_device_states state = sm->current_state_id;
  157. switch (state) {
  158. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  159. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  160. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  161. default:
  162. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  163. __func__, state);
  164. return SCI_FAILURE_INVALID_STATE;
  165. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  166. return SCI_SUCCESS;
  167. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  168. /* device not started so there had better be no requests */
  169. BUG_ON(sci_dev->started_request_count != 0);
  170. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  171. rnc_destruct_done, sci_dev);
  172. /* Transition to the stopping state and wait for the
  173. * remote node to complete being posted and invalidated.
  174. */
  175. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  176. return SCI_SUCCESS;
  177. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  178. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  179. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  180. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  181. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  182. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  183. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  184. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  185. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  186. if (sci_dev->started_request_count == 0) {
  187. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  188. rnc_destruct_done, sci_dev);
  189. return SCI_SUCCESS;
  190. } else
  191. return scic_sds_remote_device_terminate_requests(sci_dev);
  192. break;
  193. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  194. /* All requests should have been terminated, but if there is an
  195. * attempt to stop a device already in the stopping state, then
  196. * try again to terminate.
  197. */
  198. return scic_sds_remote_device_terminate_requests(sci_dev);
  199. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  200. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  201. return SCI_SUCCESS;
  202. }
  203. }
  204. enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
  205. {
  206. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  207. enum scic_sds_remote_device_states state = sm->current_state_id;
  208. switch (state) {
  209. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  210. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  211. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  212. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  213. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  214. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  215. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  216. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  217. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  218. default:
  219. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  220. __func__, state);
  221. return SCI_FAILURE_INVALID_STATE;
  222. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  223. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  224. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  225. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  226. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  227. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  228. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
  229. return SCI_SUCCESS;
  230. }
  231. }
  232. enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
  233. {
  234. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  235. enum scic_sds_remote_device_states state = sm->current_state_id;
  236. if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
  237. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  238. __func__, state);
  239. return SCI_FAILURE_INVALID_STATE;
  240. }
  241. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
  242. return SCI_SUCCESS;
  243. }
  244. /**
  245. *
  246. * @sci_dev: The remote device for which the suspend is being requested.
  247. *
  248. * This method invokes the remote device suspend state handler. enum sci_status
  249. */
  250. enum sci_status scic_sds_remote_device_suspend(
  251. struct scic_sds_remote_device *sci_dev,
  252. u32 suspend_type)
  253. {
  254. return sci_dev->state_handlers->suspend_handler(sci_dev, suspend_type);
  255. }
  256. /**
  257. *
  258. * @sci_dev: The remote device for which the event handling is being
  259. * requested.
  260. * @frame_index: This is the frame index that is being processed.
  261. *
  262. * This method invokes the frame handler for the remote device state machine
  263. * enum sci_status
  264. */
  265. enum sci_status scic_sds_remote_device_frame_handler(
  266. struct scic_sds_remote_device *sci_dev,
  267. u32 frame_index)
  268. {
  269. return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
  270. }
  271. /**
  272. *
  273. * @sci_dev: The remote device for which the event handling is being
  274. * requested.
  275. * @event_code: This is the event code that is to be processed.
  276. *
  277. * This method invokes the remote device event handler. enum sci_status
  278. */
  279. enum sci_status scic_sds_remote_device_event_handler(
  280. struct scic_sds_remote_device *sci_dev,
  281. u32 event_code)
  282. {
  283. return sci_dev->state_handlers->event_handler(sci_dev, event_code);
  284. }
  285. static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
  286. struct scic_sds_request *sci_req,
  287. enum sci_status status)
  288. {
  289. struct scic_sds_port *sci_port = sci_dev->owning_port;
  290. /* cleanup requests that failed after starting on the port */
  291. if (status != SCI_SUCCESS)
  292. scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  293. else
  294. scic_sds_remote_device_increment_request_count(sci_dev);
  295. }
  296. enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
  297. struct scic_sds_remote_device *sci_dev,
  298. struct scic_sds_request *sci_req)
  299. {
  300. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  301. enum scic_sds_remote_device_states state = sm->current_state_id;
  302. struct scic_sds_port *sci_port = sci_dev->owning_port;
  303. enum sci_status status;
  304. switch (state) {
  305. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  306. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  307. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  308. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  309. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  310. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  311. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  312. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  313. default:
  314. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  315. __func__, state);
  316. return SCI_FAILURE_INVALID_STATE;
  317. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  318. /* attempt to start an io request for this device object. The remote
  319. * device object will issue the start request for the io and if
  320. * successful it will start the request for the port object then
  321. * increment its own request count.
  322. */
  323. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  324. if (status != SCI_SUCCESS)
  325. return status;
  326. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  327. if (status != SCI_SUCCESS)
  328. break;
  329. status = scic_sds_request_start(sci_req);
  330. break;
  331. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
  332. /* handle the start io operation for a sata device that is in
  333. * the command idle state. - Evalute the type of IO request to
  334. * be started - If its an NCQ request change to NCQ substate -
  335. * If its any other command change to the CMD substate
  336. *
  337. * If this is a softreset we may want to have a different
  338. * substate.
  339. */
  340. enum scic_sds_remote_device_states new_state;
  341. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  342. if (status != SCI_SUCCESS)
  343. return status;
  344. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  345. if (status != SCI_SUCCESS)
  346. break;
  347. status = sci_req->state_handlers->start_handler(sci_req);
  348. if (status != SCI_SUCCESS)
  349. break;
  350. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
  351. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
  352. else {
  353. sci_dev->working_request = sci_req;
  354. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
  355. }
  356. sci_base_state_machine_change_state(sm, new_state);
  357. break;
  358. }
  359. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  360. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
  361. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  362. if (status != SCI_SUCCESS)
  363. return status;
  364. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  365. if (status != SCI_SUCCESS)
  366. break;
  367. status = sci_req->state_handlers->start_handler(sci_req);
  368. } else
  369. return SCI_FAILURE_INVALID_STATE;
  370. break;
  371. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  372. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  373. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  374. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  375. if (status != SCI_SUCCESS)
  376. return status;
  377. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  378. if (status != SCI_SUCCESS)
  379. break;
  380. status = scic_sds_request_start(sci_req);
  381. if (status != SCI_SUCCESS)
  382. break;
  383. sci_dev->working_request = sci_req;
  384. sci_base_state_machine_change_state(&sci_dev->state_machine,
  385. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  386. break;
  387. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  388. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  389. /* device is already handling a command it can not accept new commands
  390. * until this one is complete.
  391. */
  392. return SCI_FAILURE_INVALID_STATE;
  393. }
  394. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  395. return status;
  396. }
  397. static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
  398. struct scic_sds_remote_device *sci_dev,
  399. struct scic_sds_request *sci_req)
  400. {
  401. enum sci_status status;
  402. status = scic_sds_request_complete(sci_req);
  403. if (status != SCI_SUCCESS)
  404. return status;
  405. status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  406. if (status != SCI_SUCCESS)
  407. return status;
  408. scic_sds_remote_device_decrement_request_count(sci_dev);
  409. return status;
  410. }
  411. enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
  412. struct scic_sds_remote_device *sci_dev,
  413. struct scic_sds_request *sci_req)
  414. {
  415. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  416. enum scic_sds_remote_device_states state = sm->current_state_id;
  417. struct scic_sds_port *sci_port = sci_dev->owning_port;
  418. enum sci_status status;
  419. switch (state) {
  420. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  421. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  422. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  423. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  424. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  425. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  426. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  427. default:
  428. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  429. __func__, state);
  430. return SCI_FAILURE_INVALID_STATE;
  431. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  432. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  433. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  434. status = common_complete_io(sci_port, sci_dev, sci_req);
  435. break;
  436. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  437. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  438. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  439. status = common_complete_io(sci_port, sci_dev, sci_req);
  440. if (status != SCI_SUCCESS)
  441. break;
  442. if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  443. /* This request causes hardware error, device needs to be Lun Reset.
  444. * So here we force the state machine to IDLE state so the rest IOs
  445. * can reach RNC state handler, these IOs will be completed by RNC with
  446. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  447. */
  448. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  449. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  450. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  451. break;
  452. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  453. status = common_complete_io(sci_port, sci_dev, sci_req);
  454. if (status != SCI_SUCCESS)
  455. break;
  456. sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  457. break;
  458. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  459. status = common_complete_io(sci_port, sci_dev, sci_req);
  460. if (status != SCI_SUCCESS)
  461. break;
  462. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  463. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  464. rnc_destruct_done,
  465. sci_dev);
  466. break;
  467. }
  468. if (status != SCI_SUCCESS)
  469. dev_err(scirdev_to_dev(sci_dev),
  470. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  471. "could not complete\n", __func__, sci_port,
  472. sci_dev, sci_req, status);
  473. return status;
  474. }
  475. static void scic_sds_remote_device_continue_request(void *dev)
  476. {
  477. struct scic_sds_remote_device *sci_dev = dev;
  478. /* we need to check if this request is still valid to continue. */
  479. if (sci_dev->working_request)
  480. scic_controller_continue_io(sci_dev->working_request);
  481. }
  482. enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
  483. struct scic_sds_remote_device *sci_dev,
  484. struct scic_sds_request *sci_req)
  485. {
  486. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  487. enum scic_sds_remote_device_states state = sm->current_state_id;
  488. struct scic_sds_port *sci_port = sci_dev->owning_port;
  489. enum sci_status status;
  490. switch (state) {
  491. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  492. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  493. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  494. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  495. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  496. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  497. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  498. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  499. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  500. default:
  501. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  502. __func__, state);
  503. return SCI_FAILURE_INVALID_STATE;
  504. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  505. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  506. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  507. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  508. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  509. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  510. if (status != SCI_SUCCESS)
  511. return status;
  512. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  513. if (status != SCI_SUCCESS)
  514. goto out;
  515. status = sci_req->state_handlers->start_handler(sci_req);
  516. if (status != SCI_SUCCESS)
  517. goto out;
  518. /* Note: If the remote device state is not IDLE this will
  519. * replace the request that probably resulted in the task
  520. * management request.
  521. */
  522. sci_dev->working_request = sci_req;
  523. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  524. /* The remote node context must cleanup the TCi to NCQ mapping
  525. * table. The only way to do this correctly is to either write
  526. * to the TLCR register or to invalidate and repost the RNC. In
  527. * either case the remote node context state machine will take
  528. * the correct action when the remote node context is suspended
  529. * and later resumed.
  530. */
  531. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  532. SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  533. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  534. scic_sds_remote_device_continue_request,
  535. sci_dev);
  536. out:
  537. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  538. /* We need to let the controller start request handler know that
  539. * it can't post TC yet. We will provide a callback function to
  540. * post TC when RNC gets resumed.
  541. */
  542. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  543. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  544. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  545. if (status != SCI_SUCCESS)
  546. return status;
  547. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  548. if (status != SCI_SUCCESS)
  549. break;
  550. status = scic_sds_request_start(sci_req);
  551. break;
  552. }
  553. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  554. return status;
  555. }
  556. /**
  557. *
  558. * @sci_dev:
  559. * @request:
  560. *
  561. * This method takes the request and bulids an appropriate SCU context for the
  562. * request and then requests the controller to post the request. none
  563. */
  564. void scic_sds_remote_device_post_request(
  565. struct scic_sds_remote_device *sci_dev,
  566. u32 request)
  567. {
  568. u32 context;
  569. context = scic_sds_remote_device_build_command_context(sci_dev, request);
  570. scic_sds_controller_post_request(
  571. scic_sds_remote_device_get_controller(sci_dev),
  572. context
  573. );
  574. }
  575. /* called once the remote node context has transisitioned to a
  576. * ready state. This is the indication that the remote device object can also
  577. * transition to ready.
  578. */
  579. static void remote_device_resume_done(void *_dev)
  580. {
  581. struct scic_sds_remote_device *sci_dev = _dev;
  582. enum scic_sds_remote_device_states state;
  583. state = sci_dev->state_machine.current_state_id;
  584. switch (state) {
  585. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  586. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  587. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  588. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  589. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  590. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  591. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  592. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  593. break;
  594. default:
  595. /* go 'ready' if we are not already in a ready state */
  596. sci_base_state_machine_change_state(&sci_dev->state_machine,
  597. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  598. break;
  599. }
  600. }
  601. static enum sci_status
  602. default_device_handler(struct scic_sds_remote_device *sci_dev,
  603. const char *func)
  604. {
  605. dev_warn(scirdev_to_dev(sci_dev),
  606. "%s: in wrong state: %d\n", func,
  607. sci_base_state_machine_get_state(&sci_dev->state_machine));
  608. return SCI_FAILURE_INVALID_STATE;
  609. }
  610. static enum sci_status scic_sds_remote_device_default_suspend_handler(
  611. struct scic_sds_remote_device *sci_dev, u32 suspend_type)
  612. {
  613. return default_device_handler(sci_dev, __func__);
  614. }
  615. static enum sci_status scic_sds_remote_device_default_resume_handler(
  616. struct scic_sds_remote_device *sci_dev)
  617. {
  618. return default_device_handler(sci_dev, __func__);
  619. }
  620. /**
  621. *
  622. * @device: The struct scic_sds_remote_device which is then cast into a
  623. * struct scic_sds_remote_device.
  624. * @event_code: The event code that the struct scic_sds_controller wants the device
  625. * object to process.
  626. *
  627. * This method is the default event handler. It will call the RNC state
  628. * machine handler for any RNC events otherwise it will log a warning and
  629. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  630. */
  631. static enum sci_status scic_sds_remote_device_core_event_handler(
  632. struct scic_sds_remote_device *sci_dev,
  633. u32 event_code,
  634. bool is_ready_state)
  635. {
  636. enum sci_status status;
  637. switch (scu_get_event_type(event_code)) {
  638. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  639. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  640. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  641. status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
  642. break;
  643. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  644. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  645. status = SCI_SUCCESS;
  646. /* Suspend the associated RNC */
  647. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  648. SCI_SOFTWARE_SUSPENSION,
  649. NULL, NULL);
  650. dev_dbg(scirdev_to_dev(sci_dev),
  651. "%s: device: %p event code: %x: %s\n",
  652. __func__, sci_dev, event_code,
  653. (is_ready_state)
  654. ? "I_T_Nexus_Timeout event"
  655. : "I_T_Nexus_Timeout event in wrong state");
  656. break;
  657. }
  658. /* Else, fall through and treat as unhandled... */
  659. default:
  660. dev_dbg(scirdev_to_dev(sci_dev),
  661. "%s: device: %p event code: %x: %s\n",
  662. __func__, sci_dev, event_code,
  663. (is_ready_state)
  664. ? "unexpected event"
  665. : "unexpected event in wrong state");
  666. status = SCI_FAILURE_INVALID_STATE;
  667. break;
  668. }
  669. return status;
  670. }
  671. /**
  672. *
  673. * @device: The struct scic_sds_remote_device which is then cast into a
  674. * struct scic_sds_remote_device.
  675. * @event_code: The event code that the struct scic_sds_controller wants the device
  676. * object to process.
  677. *
  678. * This method is the default event handler. It will call the RNC state
  679. * machine handler for any RNC events otherwise it will log a warning and
  680. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  681. */
  682. static enum sci_status scic_sds_remote_device_default_event_handler(
  683. struct scic_sds_remote_device *sci_dev,
  684. u32 event_code)
  685. {
  686. return scic_sds_remote_device_core_event_handler(sci_dev,
  687. event_code,
  688. false);
  689. }
  690. /**
  691. *
  692. * @device: The struct scic_sds_remote_device which is then cast into a
  693. * struct scic_sds_remote_device.
  694. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  695. * device object to process.
  696. *
  697. * This method is the default unsolicited frame handler. It logs a warning,
  698. * releases the frame and returns a failure. enum sci_status
  699. * SCI_FAILURE_INVALID_STATE
  700. */
  701. static enum sci_status scic_sds_remote_device_default_frame_handler(
  702. struct scic_sds_remote_device *sci_dev,
  703. u32 frame_index)
  704. {
  705. dev_warn(scirdev_to_dev(sci_dev),
  706. "%s: SCIC Remote Device requested to handle frame %x "
  707. "while in wrong state %d\n",
  708. __func__,
  709. frame_index,
  710. sci_base_state_machine_get_state(
  711. &sci_dev->state_machine));
  712. /* Return the frame back to the controller */
  713. scic_sds_controller_release_frame(
  714. scic_sds_remote_device_get_controller(sci_dev), frame_index
  715. );
  716. return SCI_FAILURE_INVALID_STATE;
  717. }
  718. static enum sci_status scic_sds_remote_device_default_complete_request_handler(
  719. struct scic_sds_remote_device *sci_dev,
  720. struct scic_sds_request *request)
  721. {
  722. return default_device_handler(sci_dev, __func__);
  723. }
  724. /**
  725. *
  726. * @device: The struct scic_sds_remote_device which is then cast into a
  727. * struct scic_sds_remote_device.
  728. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  729. * device object to process.
  730. *
  731. * This method is a general ssp frame handler. In most cases the device object
  732. * needs to route the unsolicited frame processing to the io request object.
  733. * This method decodes the tag for the io request object and routes the
  734. * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
  735. */
  736. static enum sci_status scic_sds_remote_device_general_frame_handler(
  737. struct scic_sds_remote_device *sci_dev,
  738. u32 frame_index)
  739. {
  740. enum sci_status result;
  741. struct sci_ssp_frame_header *frame_header;
  742. struct scic_sds_request *io_request;
  743. result = scic_sds_unsolicited_frame_control_get_header(
  744. &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
  745. frame_index,
  746. (void **)&frame_header
  747. );
  748. if (SCI_SUCCESS == result) {
  749. io_request = scic_sds_controller_get_io_request_from_tag(
  750. scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
  751. if ((io_request == NULL)
  752. || (io_request->target_device != sci_dev)) {
  753. /*
  754. * We could not map this tag to a valid IO request
  755. * Just toss the frame and continue */
  756. scic_sds_controller_release_frame(
  757. scic_sds_remote_device_get_controller(sci_dev), frame_index
  758. );
  759. } else {
  760. /* The IO request is now in charge of releasing the frame */
  761. result = io_request->state_handlers->frame_handler(
  762. io_request, frame_index);
  763. }
  764. }
  765. return result;
  766. }
  767. /**
  768. *
  769. * @[in]: sci_dev This is the device object that is receiving the event.
  770. * @[in]: event_code The event code to process.
  771. *
  772. * This is a common method for handling events reported to the remote device
  773. * from the controller object. enum sci_status
  774. */
  775. static enum sci_status scic_sds_remote_device_general_event_handler(
  776. struct scic_sds_remote_device *sci_dev,
  777. u32 event_code)
  778. {
  779. return scic_sds_remote_device_core_event_handler(sci_dev,
  780. event_code,
  781. true);
  782. }
  783. /*
  784. * This method will complete the request for the remote device object. The
  785. * method will call the completion handler for the request object and if
  786. * successful it will complete the request on the port object then decrement
  787. * its own started_request_count. enum sci_status
  788. */
  789. static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
  790. struct scic_sds_remote_device *sci_dev,
  791. struct scic_sds_request *request)
  792. {
  793. enum sci_status result;
  794. result = scic_sds_request_complete(request);
  795. if (result != SCI_SUCCESS)
  796. return result;
  797. /* See if the port is in a state
  798. * where we can start the IO request */
  799. result = scic_sds_port_complete_io(
  800. scic_sds_remote_device_get_port(sci_dev),
  801. sci_dev, request);
  802. if (result == SCI_SUCCESS)
  803. scic_sds_remote_device_decrement_request_count(sci_dev);
  804. return result;
  805. }
  806. /**
  807. *
  808. * @device: The device object for which the request is completing.
  809. * @request: The task request that is being completed.
  810. *
  811. * This method completes requests for this struct scic_sds_remote_device while it is
  812. * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
  813. * complete method for the request object and if that is successful the port
  814. * object is called to complete the task request. Then the device object itself
  815. * completes the task request. If struct scic_sds_remote_device started_request_count
  816. * goes to 0 and the invalidate RNC request has completed the device object can
  817. * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
  818. */
  819. static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
  820. struct scic_sds_remote_device *sci_dev,
  821. struct scic_sds_request *request)
  822. {
  823. enum sci_status status = SCI_SUCCESS;
  824. status = scic_sds_request_complete(request);
  825. if (status != SCI_SUCCESS)
  826. return status;
  827. status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
  828. sci_dev, request);
  829. if (status != SCI_SUCCESS)
  830. return status;
  831. scic_sds_remote_device_decrement_request_count(sci_dev);
  832. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  833. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  834. rnc_destruct_done, sci_dev);
  835. return SCI_SUCCESS;
  836. }
  837. /* complete requests for this device while it is in the
  838. * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
  839. * method for the request object and if that is successful the port object is
  840. * called to complete the task request. Then the device object itself completes
  841. * the task request. enum sci_status
  842. */
  843. static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
  844. struct scic_sds_remote_device *sci_dev,
  845. struct scic_sds_request *request)
  846. {
  847. enum sci_status status = SCI_SUCCESS;
  848. status = scic_sds_request_complete(request);
  849. if (status == SCI_SUCCESS) {
  850. status = scic_sds_port_complete_io(
  851. scic_sds_remote_device_get_port(sci_dev),
  852. sci_dev, request);
  853. if (status == SCI_SUCCESS) {
  854. scic_sds_remote_device_decrement_request_count(sci_dev);
  855. }
  856. }
  857. return status;
  858. }
  859. static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
  860. struct scic_sds_request *sci_req)
  861. {
  862. enum sci_status status;
  863. status = scic_sds_io_request_complete(sci_req);
  864. if (status != SCI_SUCCESS)
  865. goto out;
  866. status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
  867. if (status != SCI_SUCCESS)
  868. goto out;
  869. scic_sds_remote_device_decrement_request_count(sci_dev);
  870. if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  871. /* This request causes hardware error, device needs to be Lun Reset.
  872. * So here we force the state machine to IDLE state so the rest IOs
  873. * can reach RNC state handler, these IOs will be completed by RNC with
  874. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  875. */
  876. sci_base_state_machine_change_state(&sci_dev->state_machine,
  877. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  878. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  879. sci_base_state_machine_change_state(&sci_dev->state_machine,
  880. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  881. out:
  882. if (status != SCI_SUCCESS)
  883. dev_err(scirdev_to_dev(sci_dev),
  884. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  885. "could not complete\n", __func__, sci_dev->owning_port,
  886. sci_dev, sci_req, status);
  887. return status;
  888. }
  889. static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
  890. struct scic_sds_remote_device *sci_dev,
  891. u32 event_code)
  892. {
  893. enum sci_status status;
  894. status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
  895. if (status != SCI_SUCCESS)
  896. return status;
  897. /* We pick up suspension events to handle specifically to this state. We
  898. * resume the RNC right away. enum sci_status
  899. */
  900. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  901. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  902. status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  903. return status;
  904. }
  905. static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
  906. u32 frame_index)
  907. {
  908. enum sci_status status;
  909. struct sata_fis_header *frame_header;
  910. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  911. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  912. frame_index,
  913. (void **)&frame_header);
  914. if (status != SCI_SUCCESS)
  915. return status;
  916. if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
  917. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  918. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  919. /* TODO Check sactive and complete associated IO if any. */
  920. sci_base_state_machine_change_state(&sci_dev->state_machine,
  921. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  922. } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
  923. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  924. /*
  925. * Some devices return D2H FIS when an NCQ error is detected.
  926. * Treat this like an SDB error FIS ready reason.
  927. */
  928. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  929. sci_base_state_machine_change_state(&sci_dev->state_machine,
  930. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  931. } else
  932. status = SCI_FAILURE;
  933. scic_sds_controller_release_frame(scic, frame_index);
  934. return status;
  935. }
  936. static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
  937. struct scic_sds_remote_device *sci_dev,
  938. u32 suspend_type)
  939. {
  940. enum sci_status status;
  941. status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  942. suspend_type, NULL, NULL);
  943. return status;
  944. }
  945. static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
  946. struct scic_sds_remote_device *sci_dev,
  947. u32 frame_index)
  948. {
  949. /* The device doe not process any UF received from the hardware while
  950. * in this state. All unsolicited frames are forwarded to the io
  951. * request object.
  952. */
  953. return scic_sds_io_request_frame_handler(sci_dev->working_request,
  954. frame_index);
  955. }
  956. static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  957. {
  958. struct scic_sds_remote_device *sci_dev = _dev;
  959. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  960. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  961. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  962. * As a result, avoid sending the ready notification.
  963. */
  964. if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
  965. isci_remote_device_ready(scic->ihost, idev);
  966. }
  967. static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
  968. struct scic_sds_remote_device *sci_dev,
  969. u32 frame_index)
  970. {
  971. enum sci_status status;
  972. /* The device does not process any UF received from the hardware while
  973. * in this state. All unsolicited frames are forwarded to the io request
  974. * object.
  975. */
  976. status = scic_sds_io_request_frame_handler(
  977. sci_dev->working_request,
  978. frame_index
  979. );
  980. return status;
  981. }
  982. static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
  983. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  984. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  985. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  986. .resume_handler = scic_sds_remote_device_default_resume_handler,
  987. .event_handler = scic_sds_remote_device_default_event_handler,
  988. .frame_handler = scic_sds_remote_device_default_frame_handler
  989. },
  990. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  991. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  992. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  993. .resume_handler = scic_sds_remote_device_default_resume_handler,
  994. .event_handler = scic_sds_remote_device_default_event_handler,
  995. .frame_handler = scic_sds_remote_device_default_frame_handler
  996. },
  997. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  998. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  999. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1000. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1001. .event_handler = scic_sds_remote_device_general_event_handler,
  1002. .frame_handler = scic_sds_remote_device_default_frame_handler
  1003. },
  1004. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1005. .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
  1006. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1007. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1008. .event_handler = scic_sds_remote_device_general_event_handler,
  1009. .frame_handler = scic_sds_remote_device_general_frame_handler,
  1010. },
  1011. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1012. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1013. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1014. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1015. .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
  1016. .frame_handler = scic_sds_remote_device_default_frame_handler
  1017. },
  1018. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1019. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1020. .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
  1021. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1022. .event_handler = scic_sds_remote_device_general_event_handler,
  1023. .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
  1024. },
  1025. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1026. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1027. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1028. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1029. .event_handler = scic_sds_remote_device_general_event_handler,
  1030. .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
  1031. },
  1032. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1033. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1034. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1035. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1036. .event_handler = scic_sds_remote_device_general_event_handler,
  1037. .frame_handler = scic_sds_remote_device_general_frame_handler
  1038. },
  1039. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1040. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1041. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1042. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1043. .event_handler = scic_sds_remote_device_general_event_handler,
  1044. .frame_handler = scic_sds_remote_device_general_frame_handler
  1045. },
  1046. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1047. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1048. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1049. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1050. .event_handler = scic_sds_remote_device_general_event_handler,
  1051. .frame_handler = scic_sds_remote_device_default_frame_handler
  1052. },
  1053. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1054. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1055. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1056. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1057. .event_handler = scic_sds_remote_device_general_event_handler,
  1058. .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
  1059. },
  1060. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1061. .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
  1062. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1063. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1064. .event_handler = scic_sds_remote_device_general_event_handler,
  1065. .frame_handler = scic_sds_remote_device_general_frame_handler
  1066. },
  1067. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1068. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1069. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1070. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1071. .event_handler = scic_sds_remote_device_default_event_handler,
  1072. .frame_handler = scic_sds_remote_device_general_frame_handler
  1073. },
  1074. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1075. .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
  1076. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1077. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1078. .event_handler = scic_sds_remote_device_default_event_handler,
  1079. .frame_handler = scic_sds_remote_device_general_frame_handler
  1080. },
  1081. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1082. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1083. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1084. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1085. .event_handler = scic_sds_remote_device_default_event_handler,
  1086. .frame_handler = scic_sds_remote_device_default_frame_handler
  1087. }
  1088. };
  1089. static void scic_sds_remote_device_initial_state_enter(void *object)
  1090. {
  1091. struct scic_sds_remote_device *sci_dev = object;
  1092. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1093. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
  1094. /* Initial state is a transitional state to the stopped state */
  1095. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1096. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1097. }
  1098. /**
  1099. * scic_remote_device_destruct() - free remote node context and destruct
  1100. * @remote_device: This parameter specifies the remote device to be destructed.
  1101. *
  1102. * Remote device objects are a limited resource. As such, they must be
  1103. * protected. Thus calls to construct and destruct are mutually exclusive and
  1104. * non-reentrant. The return value shall indicate if the device was
  1105. * successfully destructed or if some failure occurred. enum sci_status This value
  1106. * is returned if the device is successfully destructed.
  1107. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  1108. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  1109. * valid, etc.).
  1110. */
  1111. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  1112. {
  1113. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1114. enum scic_sds_remote_device_states state = sm->current_state_id;
  1115. struct scic_sds_controller *scic;
  1116. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1117. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1118. __func__, state);
  1119. return SCI_FAILURE_INVALID_STATE;
  1120. }
  1121. scic = sci_dev->owning_port->owning_controller;
  1122. scic_sds_controller_free_remote_node_context(scic, sci_dev,
  1123. sci_dev->rnc.remote_node_index);
  1124. sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  1125. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
  1126. return SCI_SUCCESS;
  1127. }
  1128. /**
  1129. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  1130. * @ihost: This parameter specifies the isci host object.
  1131. * @idev: This parameter specifies the remote device to be freed.
  1132. *
  1133. */
  1134. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  1135. {
  1136. dev_dbg(&ihost->pdev->dev,
  1137. "%s: isci_device = %p\n", __func__, idev);
  1138. /* There should not be any outstanding io's. All paths to
  1139. * here should go through isci_remote_device_nuke_requests.
  1140. * If we hit this condition, we will need a way to complete
  1141. * io requests in process */
  1142. while (!list_empty(&idev->reqs_in_process)) {
  1143. dev_err(&ihost->pdev->dev,
  1144. "%s: ** request list not empty! **\n", __func__);
  1145. BUG();
  1146. }
  1147. scic_remote_device_destruct(&idev->sci);
  1148. idev->domain_dev->lldd_dev = NULL;
  1149. idev->domain_dev = NULL;
  1150. idev->isci_port = NULL;
  1151. list_del_init(&idev->node);
  1152. clear_bit(IDEV_START_PENDING, &idev->flags);
  1153. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1154. wake_up(&ihost->eventq);
  1155. }
  1156. /**
  1157. * isci_remote_device_stop_complete() - This function is called by the scic
  1158. * when the remote device stop has completed. We mark the isci device as not
  1159. * ready and remove the isci remote device.
  1160. * @ihost: This parameter specifies the isci host object.
  1161. * @idev: This parameter specifies the remote device.
  1162. * @status: This parameter specifies status of the completion.
  1163. *
  1164. */
  1165. static void isci_remote_device_stop_complete(struct isci_host *ihost,
  1166. struct isci_remote_device *idev)
  1167. {
  1168. dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
  1169. isci_remote_device_change_state(idev, isci_stopped);
  1170. /* after stop, we can tear down resources. */
  1171. isci_remote_device_deconstruct(ihost, idev);
  1172. }
  1173. static void scic_sds_remote_device_stopped_state_enter(void *object)
  1174. {
  1175. struct scic_sds_remote_device *sci_dev = object;
  1176. struct scic_sds_controller *scic;
  1177. struct isci_remote_device *idev;
  1178. struct isci_host *ihost;
  1179. u32 prev_state;
  1180. scic = scic_sds_remote_device_get_controller(sci_dev);
  1181. ihost = scic->ihost;
  1182. idev = sci_dev_to_idev(sci_dev);
  1183. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1184. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1185. /* If we are entering from the stopping state let the SCI User know that
  1186. * the stop operation has completed.
  1187. */
  1188. prev_state = sci_dev->state_machine.previous_state_id;
  1189. if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
  1190. isci_remote_device_stop_complete(ihost, idev);
  1191. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  1192. }
  1193. static void scic_sds_remote_device_starting_state_enter(void *object)
  1194. {
  1195. struct scic_sds_remote_device *sci_dev = object;
  1196. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1197. struct isci_host *ihost = scic->ihost;
  1198. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1199. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1200. SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1201. isci_remote_device_not_ready(ihost, idev,
  1202. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  1203. }
  1204. static void scic_sds_remote_device_ready_state_enter(void *object)
  1205. {
  1206. struct scic_sds_remote_device *sci_dev = object;
  1207. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1208. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1209. SET_STATE_HANDLER(sci_dev,
  1210. scic_sds_remote_device_state_handler_table,
  1211. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  1212. scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
  1213. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  1214. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1215. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1216. } else if (dev_is_expander(dev)) {
  1217. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1218. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1219. } else
  1220. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1221. }
  1222. static void scic_sds_remote_device_ready_state_exit(void *object)
  1223. {
  1224. struct scic_sds_remote_device *sci_dev = object;
  1225. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1226. if (dev->dev_type == SAS_END_DEV) {
  1227. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1228. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1229. isci_remote_device_not_ready(scic->ihost, idev,
  1230. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  1231. }
  1232. }
  1233. static void scic_sds_remote_device_stopping_state_enter(void *object)
  1234. {
  1235. struct scic_sds_remote_device *sci_dev = object;
  1236. SET_STATE_HANDLER(
  1237. sci_dev,
  1238. scic_sds_remote_device_state_handler_table,
  1239. SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
  1240. );
  1241. }
  1242. static void scic_sds_remote_device_failed_state_enter(void *object)
  1243. {
  1244. struct scic_sds_remote_device *sci_dev = object;
  1245. SET_STATE_HANDLER(
  1246. sci_dev,
  1247. scic_sds_remote_device_state_handler_table,
  1248. SCI_BASE_REMOTE_DEVICE_STATE_FAILED
  1249. );
  1250. }
  1251. static void scic_sds_remote_device_resetting_state_enter(void *object)
  1252. {
  1253. struct scic_sds_remote_device *sci_dev = object;
  1254. SET_STATE_HANDLER(
  1255. sci_dev,
  1256. scic_sds_remote_device_state_handler_table,
  1257. SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
  1258. );
  1259. scic_sds_remote_node_context_suspend(
  1260. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  1261. }
  1262. static void scic_sds_remote_device_resetting_state_exit(void *object)
  1263. {
  1264. struct scic_sds_remote_device *sci_dev = object;
  1265. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  1266. }
  1267. static void scic_sds_remote_device_final_state_enter(void *object)
  1268. {
  1269. struct scic_sds_remote_device *sci_dev = object;
  1270. SET_STATE_HANDLER(
  1271. sci_dev,
  1272. scic_sds_remote_device_state_handler_table,
  1273. SCI_BASE_REMOTE_DEVICE_STATE_FINAL
  1274. );
  1275. }
  1276. static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
  1277. {
  1278. struct scic_sds_remote_device *sci_dev = object;
  1279. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1280. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1281. sci_dev->working_request = NULL;
  1282. if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
  1283. /*
  1284. * Since the RNC is ready, it's alright to finish completion
  1285. * processing (e.g. signal the remote device is ready). */
  1286. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
  1287. } else {
  1288. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1289. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
  1290. sci_dev);
  1291. }
  1292. }
  1293. static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
  1294. {
  1295. struct scic_sds_remote_device *sci_dev = object;
  1296. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1297. BUG_ON(sci_dev->working_request == NULL);
  1298. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1299. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1300. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1301. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  1302. }
  1303. static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
  1304. {
  1305. struct scic_sds_remote_device *sci_dev = object;
  1306. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1307. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
  1308. }
  1309. static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
  1310. {
  1311. struct scic_sds_remote_device *sci_dev = object;
  1312. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1313. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1314. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1315. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  1316. if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  1317. isci_remote_device_not_ready(scic->ihost, idev,
  1318. sci_dev->not_ready_reason);
  1319. }
  1320. static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
  1321. {
  1322. struct scic_sds_remote_device *sci_dev = object;
  1323. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1324. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  1325. }
  1326. static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
  1327. {
  1328. struct scic_sds_remote_device *sci_dev = object;
  1329. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1330. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1331. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1332. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1333. }
  1334. static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
  1335. {
  1336. struct scic_sds_remote_device *sci_dev = object;
  1337. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1338. BUG_ON(sci_dev->working_request == NULL);
  1339. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1340. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1341. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1342. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  1343. }
  1344. static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
  1345. {
  1346. struct scic_sds_remote_device *sci_dev = object;
  1347. sci_dev->working_request = NULL;
  1348. }
  1349. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  1350. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1351. .enter_state = scic_sds_remote_device_initial_state_enter,
  1352. },
  1353. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1354. .enter_state = scic_sds_remote_device_stopped_state_enter,
  1355. },
  1356. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1357. .enter_state = scic_sds_remote_device_starting_state_enter,
  1358. },
  1359. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1360. .enter_state = scic_sds_remote_device_ready_state_enter,
  1361. .exit_state = scic_sds_remote_device_ready_state_exit
  1362. },
  1363. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1364. .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
  1365. },
  1366. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1367. .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
  1368. },
  1369. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1370. .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
  1371. },
  1372. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1373. .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
  1374. },
  1375. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1376. .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
  1377. },
  1378. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1379. .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
  1380. },
  1381. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1382. .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
  1383. .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
  1384. },
  1385. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1386. .enter_state = scic_sds_remote_device_stopping_state_enter,
  1387. },
  1388. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1389. .enter_state = scic_sds_remote_device_failed_state_enter,
  1390. },
  1391. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1392. .enter_state = scic_sds_remote_device_resetting_state_enter,
  1393. .exit_state = scic_sds_remote_device_resetting_state_exit
  1394. },
  1395. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1396. .enter_state = scic_sds_remote_device_final_state_enter,
  1397. },
  1398. };
  1399. /**
  1400. * scic_remote_device_construct() - common construction
  1401. * @sci_port: SAS/SATA port through which this device is accessed.
  1402. * @sci_dev: remote device to construct
  1403. *
  1404. * This routine just performs benign initialization and does not
  1405. * allocate the remote_node_context which is left to
  1406. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  1407. * frees the remote_node_context(s) for the device.
  1408. */
  1409. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  1410. struct scic_sds_remote_device *sci_dev)
  1411. {
  1412. sci_dev->owning_port = sci_port;
  1413. sci_dev->started_request_count = 0;
  1414. sci_base_state_machine_construct(
  1415. &sci_dev->state_machine,
  1416. sci_dev,
  1417. scic_sds_remote_device_state_table,
  1418. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
  1419. );
  1420. sci_base_state_machine_start(
  1421. &sci_dev->state_machine
  1422. );
  1423. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  1424. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1425. }
  1426. /**
  1427. * scic_remote_device_da_construct() - construct direct attached device.
  1428. *
  1429. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1430. * the device is known to the SCI Core since it is contained in the
  1431. * scic_phy object. Remote node context(s) is/are a global resource
  1432. * allocated by this routine, freed by scic_remote_device_destruct().
  1433. *
  1434. * Returns:
  1435. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1436. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1437. * sata-only controller instance.
  1438. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1439. */
  1440. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  1441. struct scic_sds_remote_device *sci_dev)
  1442. {
  1443. enum sci_status status;
  1444. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1445. scic_remote_device_construct(sci_port, sci_dev);
  1446. /*
  1447. * This information is request to determine how many remote node context
  1448. * entries will be needed to store the remote node.
  1449. */
  1450. sci_dev->is_direct_attached = true;
  1451. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1452. sci_dev,
  1453. &sci_dev->rnc.remote_node_index);
  1454. if (status != SCI_SUCCESS)
  1455. return status;
  1456. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1457. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1458. /* pass */;
  1459. else
  1460. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1461. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  1462. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1463. sci_dev->device_port_width = 1;
  1464. return SCI_SUCCESS;
  1465. }
  1466. /**
  1467. * scic_remote_device_ea_construct() - construct expander attached device
  1468. *
  1469. * Remote node context(s) is/are a global resource allocated by this
  1470. * routine, freed by scic_remote_device_destruct().
  1471. *
  1472. * Returns:
  1473. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1474. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1475. * sata-only controller instance.
  1476. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1477. */
  1478. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  1479. struct scic_sds_remote_device *sci_dev)
  1480. {
  1481. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1482. enum sci_status status;
  1483. scic_remote_device_construct(sci_port, sci_dev);
  1484. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1485. sci_dev,
  1486. &sci_dev->rnc.remote_node_index);
  1487. if (status != SCI_SUCCESS)
  1488. return status;
  1489. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1490. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1491. /* pass */;
  1492. else
  1493. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1494. /*
  1495. * For SAS-2 the physical link rate is actually a logical link
  1496. * rate that incorporates multiplexing. The SCU doesn't
  1497. * incorporate multiplexing and for the purposes of the
  1498. * connection the logical link rate is that same as the
  1499. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1500. * one another, so this code works for both situations. */
  1501. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1502. dev->linkrate);
  1503. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1504. sci_dev->device_port_width = 1;
  1505. return SCI_SUCCESS;
  1506. }
  1507. /**
  1508. * scic_remote_device_start() - This method will start the supplied remote
  1509. * device. This method enables normal IO requests to flow through to the
  1510. * remote device.
  1511. * @remote_device: This parameter specifies the device to be started.
  1512. * @timeout: This parameter specifies the number of milliseconds in which the
  1513. * start operation should complete.
  1514. *
  1515. * An indication of whether the device was successfully started. SCI_SUCCESS
  1516. * This value is returned if the device was successfully started.
  1517. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1518. * the device when there have been no phys added to it.
  1519. */
  1520. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1521. u32 timeout)
  1522. {
  1523. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1524. enum scic_sds_remote_device_states state = sm->current_state_id;
  1525. enum sci_status status;
  1526. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1527. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1528. __func__, state);
  1529. return SCI_FAILURE_INVALID_STATE;
  1530. }
  1531. status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1532. remote_device_resume_done,
  1533. sci_dev);
  1534. if (status != SCI_SUCCESS)
  1535. return status;
  1536. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1537. return SCI_SUCCESS;
  1538. }
  1539. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1540. struct isci_remote_device *idev)
  1541. {
  1542. struct scic_sds_port *sci_port = iport->sci_port_handle;
  1543. struct isci_host *ihost = iport->isci_host;
  1544. struct domain_device *dev = idev->domain_dev;
  1545. enum sci_status status;
  1546. if (dev->parent && dev_is_expander(dev->parent))
  1547. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1548. else
  1549. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1550. if (status != SCI_SUCCESS) {
  1551. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1552. __func__, status);
  1553. return status;
  1554. }
  1555. /* start the device. */
  1556. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1557. if (status != SCI_SUCCESS)
  1558. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1559. status);
  1560. return status;
  1561. }
  1562. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1563. {
  1564. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1565. dev_dbg(&ihost->pdev->dev,
  1566. "%s: idev = %p\n", __func__, idev);
  1567. /* Cleanup all requests pending for this device. */
  1568. isci_terminate_pending_requests(ihost, idev, terminating);
  1569. dev_dbg(&ihost->pdev->dev,
  1570. "%s: idev = %p, done\n", __func__, idev);
  1571. }
  1572. /**
  1573. * This function builds the isci_remote_device when a libsas dev_found message
  1574. * is received.
  1575. * @isci_host: This parameter specifies the isci host object.
  1576. * @port: This parameter specifies the isci_port conected to this device.
  1577. *
  1578. * pointer to new isci_remote_device.
  1579. */
  1580. static struct isci_remote_device *
  1581. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1582. {
  1583. struct isci_remote_device *idev;
  1584. int i;
  1585. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1586. idev = &ihost->devices[i];
  1587. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1588. break;
  1589. }
  1590. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1591. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1592. return NULL;
  1593. }
  1594. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1595. return NULL;
  1596. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1597. return NULL;
  1598. isci_remote_device_change_state(idev, isci_freed);
  1599. return idev;
  1600. }
  1601. /**
  1602. * isci_remote_device_stop() - This function is called internally to stop the
  1603. * remote device.
  1604. * @isci_host: This parameter specifies the isci host object.
  1605. * @isci_device: This parameter specifies the remote device.
  1606. *
  1607. * The status of the scic request to stop.
  1608. */
  1609. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1610. {
  1611. enum sci_status status;
  1612. unsigned long flags;
  1613. dev_dbg(&ihost->pdev->dev,
  1614. "%s: isci_device = %p\n", __func__, idev);
  1615. isci_remote_device_change_state(idev, isci_stopping);
  1616. /* Kill all outstanding requests. */
  1617. isci_remote_device_nuke_requests(ihost, idev);
  1618. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1619. spin_lock_irqsave(&ihost->scic_lock, flags);
  1620. status = scic_remote_device_stop(&idev->sci, 50);
  1621. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1622. /* Wait for the stop complete callback. */
  1623. if (status == SCI_SUCCESS) {
  1624. wait_for_device_stop(ihost, idev);
  1625. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1626. }
  1627. dev_dbg(&ihost->pdev->dev,
  1628. "%s: idev = %p - after completion wait\n",
  1629. __func__, idev);
  1630. return status;
  1631. }
  1632. /**
  1633. * isci_remote_device_gone() - This function is called by libsas when a domain
  1634. * device is removed.
  1635. * @domain_device: This parameter specifies the libsas domain device.
  1636. *
  1637. */
  1638. void isci_remote_device_gone(struct domain_device *dev)
  1639. {
  1640. struct isci_host *ihost = dev_to_ihost(dev);
  1641. struct isci_remote_device *idev = dev->lldd_dev;
  1642. dev_dbg(&ihost->pdev->dev,
  1643. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1644. __func__, dev, idev, idev->isci_port);
  1645. isci_remote_device_stop(ihost, idev);
  1646. }
  1647. /**
  1648. * isci_remote_device_found() - This function is called by libsas when a remote
  1649. * device is discovered. A remote device object is created and started. the
  1650. * function then sleeps until the sci core device started message is
  1651. * received.
  1652. * @domain_device: This parameter specifies the libsas domain device.
  1653. *
  1654. * status, zero indicates success.
  1655. */
  1656. int isci_remote_device_found(struct domain_device *domain_dev)
  1657. {
  1658. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1659. struct isci_port *isci_port;
  1660. struct isci_phy *isci_phy;
  1661. struct asd_sas_port *sas_port;
  1662. struct asd_sas_phy *sas_phy;
  1663. struct isci_remote_device *isci_device;
  1664. enum sci_status status;
  1665. dev_dbg(&isci_host->pdev->dev,
  1666. "%s: domain_device = %p\n", __func__, domain_dev);
  1667. wait_for_start(isci_host);
  1668. sas_port = domain_dev->port;
  1669. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1670. port_phy_el);
  1671. isci_phy = to_isci_phy(sas_phy);
  1672. isci_port = isci_phy->isci_port;
  1673. /* we are being called for a device on this port,
  1674. * so it has to come up eventually
  1675. */
  1676. wait_for_completion(&isci_port->start_complete);
  1677. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1678. (isci_stopped == isci_port_get_state(isci_port)))
  1679. return -ENODEV;
  1680. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1681. if (!isci_device)
  1682. return -ENODEV;
  1683. INIT_LIST_HEAD(&isci_device->node);
  1684. domain_dev->lldd_dev = isci_device;
  1685. isci_device->domain_dev = domain_dev;
  1686. isci_device->isci_port = isci_port;
  1687. isci_remote_device_change_state(isci_device, isci_starting);
  1688. spin_lock_irq(&isci_host->scic_lock);
  1689. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1690. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1691. status = isci_remote_device_construct(isci_port, isci_device);
  1692. spin_unlock_irq(&isci_host->scic_lock);
  1693. dev_dbg(&isci_host->pdev->dev,
  1694. "%s: isci_device = %p\n",
  1695. __func__, isci_device);
  1696. if (status != SCI_SUCCESS) {
  1697. spin_lock_irq(&isci_host->scic_lock);
  1698. isci_remote_device_deconstruct(
  1699. isci_host,
  1700. isci_device
  1701. );
  1702. spin_unlock_irq(&isci_host->scic_lock);
  1703. return -ENODEV;
  1704. }
  1705. /* wait for the device ready callback. */
  1706. wait_for_device_start(isci_host, isci_device);
  1707. return 0;
  1708. }
  1709. /**
  1710. * isci_device_is_reset_pending() - This function will check if there is any
  1711. * pending reset condition on the device.
  1712. * @request: This parameter is the isci_device object.
  1713. *
  1714. * true if there is a reset pending for the device.
  1715. */
  1716. bool isci_device_is_reset_pending(
  1717. struct isci_host *isci_host,
  1718. struct isci_remote_device *isci_device)
  1719. {
  1720. struct isci_request *isci_request;
  1721. struct isci_request *tmp_req;
  1722. bool reset_is_pending = false;
  1723. unsigned long flags;
  1724. dev_dbg(&isci_host->pdev->dev,
  1725. "%s: isci_device = %p\n", __func__, isci_device);
  1726. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1727. /* Check for reset on all pending requests. */
  1728. list_for_each_entry_safe(isci_request, tmp_req,
  1729. &isci_device->reqs_in_process, dev_node) {
  1730. dev_dbg(&isci_host->pdev->dev,
  1731. "%s: isci_device = %p request = %p\n",
  1732. __func__, isci_device, isci_request);
  1733. if (isci_request->ttype == io_task) {
  1734. struct sas_task *task = isci_request_access_task(
  1735. isci_request);
  1736. spin_lock(&task->task_state_lock);
  1737. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1738. reset_is_pending = true;
  1739. spin_unlock(&task->task_state_lock);
  1740. }
  1741. }
  1742. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1743. dev_dbg(&isci_host->pdev->dev,
  1744. "%s: isci_device = %p reset_is_pending = %d\n",
  1745. __func__, isci_device, reset_is_pending);
  1746. return reset_is_pending;
  1747. }
  1748. /**
  1749. * isci_device_clear_reset_pending() - This function will clear if any pending
  1750. * reset condition flags on the device.
  1751. * @request: This parameter is the isci_device object.
  1752. *
  1753. * true if there is a reset pending for the device.
  1754. */
  1755. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1756. {
  1757. struct isci_request *isci_request;
  1758. struct isci_request *tmp_req;
  1759. unsigned long flags = 0;
  1760. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1761. __func__, idev, ihost);
  1762. spin_lock_irqsave(&ihost->scic_lock, flags);
  1763. /* Clear reset pending on all pending requests. */
  1764. list_for_each_entry_safe(isci_request, tmp_req,
  1765. &idev->reqs_in_process, dev_node) {
  1766. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1767. __func__, idev, isci_request);
  1768. if (isci_request->ttype == io_task) {
  1769. unsigned long flags2;
  1770. struct sas_task *task = isci_request_access_task(
  1771. isci_request);
  1772. spin_lock_irqsave(&task->task_state_lock, flags2);
  1773. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1774. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1775. }
  1776. }
  1777. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1778. }