remote_device.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  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. /**
  476. *
  477. * @controller: The controller that is starting the task request.
  478. * @sci_dev: The remote device for which the start task handling is being
  479. * requested.
  480. * @io_request: The task request that is being started.
  481. *
  482. * This method invokes the remote device start task handler. enum sci_status
  483. */
  484. enum sci_status scic_sds_remote_device_start_task(
  485. struct scic_sds_controller *controller,
  486. struct scic_sds_remote_device *sci_dev,
  487. struct scic_sds_request *io_request)
  488. {
  489. return sci_dev->state_handlers->start_task_handler(
  490. sci_dev, io_request);
  491. }
  492. /**
  493. *
  494. * @sci_dev:
  495. * @request:
  496. *
  497. * This method takes the request and bulids an appropriate SCU context for the
  498. * request and then requests the controller to post the request. none
  499. */
  500. void scic_sds_remote_device_post_request(
  501. struct scic_sds_remote_device *sci_dev,
  502. u32 request)
  503. {
  504. u32 context;
  505. context = scic_sds_remote_device_build_command_context(sci_dev, request);
  506. scic_sds_controller_post_request(
  507. scic_sds_remote_device_get_controller(sci_dev),
  508. context
  509. );
  510. }
  511. /* called once the remote node context has transisitioned to a
  512. * ready state. This is the indication that the remote device object can also
  513. * transition to ready.
  514. */
  515. static void remote_device_resume_done(void *_dev)
  516. {
  517. struct scic_sds_remote_device *sci_dev = _dev;
  518. enum scic_sds_remote_device_states state;
  519. state = sci_dev->state_machine.current_state_id;
  520. switch (state) {
  521. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  522. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  523. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  524. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  525. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  526. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  527. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  528. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  529. break;
  530. default:
  531. /* go 'ready' if we are not already in a ready state */
  532. sci_base_state_machine_change_state(&sci_dev->state_machine,
  533. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  534. break;
  535. }
  536. }
  537. /**
  538. *
  539. * @request: This parameter specifies the request being continued.
  540. *
  541. * This method will continue to post tc for a STP request. This method usually
  542. * serves as a callback when RNC gets resumed during a task management
  543. * sequence. none
  544. */
  545. static void scic_sds_remote_device_continue_request(void *dev)
  546. {
  547. struct scic_sds_remote_device *sci_dev = dev;
  548. /* we need to check if this request is still valid to continue. */
  549. if (sci_dev->working_request)
  550. scic_controller_continue_io(sci_dev->working_request);
  551. }
  552. static enum sci_status
  553. default_device_handler(struct scic_sds_remote_device *sci_dev,
  554. const char *func)
  555. {
  556. dev_warn(scirdev_to_dev(sci_dev),
  557. "%s: in wrong state: %d\n", func,
  558. sci_base_state_machine_get_state(&sci_dev->state_machine));
  559. return SCI_FAILURE_INVALID_STATE;
  560. }
  561. static enum sci_status scic_sds_remote_device_default_suspend_handler(
  562. struct scic_sds_remote_device *sci_dev, u32 suspend_type)
  563. {
  564. return default_device_handler(sci_dev, __func__);
  565. }
  566. static enum sci_status scic_sds_remote_device_default_resume_handler(
  567. struct scic_sds_remote_device *sci_dev)
  568. {
  569. return default_device_handler(sci_dev, __func__);
  570. }
  571. /**
  572. *
  573. * @device: The struct scic_sds_remote_device which is then cast into a
  574. * struct scic_sds_remote_device.
  575. * @event_code: The event code that the struct scic_sds_controller wants the device
  576. * object to process.
  577. *
  578. * This method is the default event handler. It will call the RNC state
  579. * machine handler for any RNC events otherwise it will log a warning and
  580. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  581. */
  582. static enum sci_status scic_sds_remote_device_core_event_handler(
  583. struct scic_sds_remote_device *sci_dev,
  584. u32 event_code,
  585. bool is_ready_state)
  586. {
  587. enum sci_status status;
  588. switch (scu_get_event_type(event_code)) {
  589. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  590. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  591. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  592. status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
  593. break;
  594. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  595. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  596. status = SCI_SUCCESS;
  597. /* Suspend the associated RNC */
  598. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  599. SCI_SOFTWARE_SUSPENSION,
  600. NULL, NULL);
  601. dev_dbg(scirdev_to_dev(sci_dev),
  602. "%s: device: %p event code: %x: %s\n",
  603. __func__, sci_dev, event_code,
  604. (is_ready_state)
  605. ? "I_T_Nexus_Timeout event"
  606. : "I_T_Nexus_Timeout event in wrong state");
  607. break;
  608. }
  609. /* Else, fall through and treat as unhandled... */
  610. default:
  611. dev_dbg(scirdev_to_dev(sci_dev),
  612. "%s: device: %p event code: %x: %s\n",
  613. __func__, sci_dev, event_code,
  614. (is_ready_state)
  615. ? "unexpected event"
  616. : "unexpected event in wrong state");
  617. status = SCI_FAILURE_INVALID_STATE;
  618. break;
  619. }
  620. return status;
  621. }
  622. /**
  623. *
  624. * @device: The struct scic_sds_remote_device which is then cast into a
  625. * struct scic_sds_remote_device.
  626. * @event_code: The event code that the struct scic_sds_controller wants the device
  627. * object to process.
  628. *
  629. * This method is the default event handler. It will call the RNC state
  630. * machine handler for any RNC events otherwise it will log a warning and
  631. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  632. */
  633. static enum sci_status scic_sds_remote_device_default_event_handler(
  634. struct scic_sds_remote_device *sci_dev,
  635. u32 event_code)
  636. {
  637. return scic_sds_remote_device_core_event_handler(sci_dev,
  638. event_code,
  639. false);
  640. }
  641. /**
  642. *
  643. * @device: The struct scic_sds_remote_device which is then cast into a
  644. * struct scic_sds_remote_device.
  645. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  646. * device object to process.
  647. *
  648. * This method is the default unsolicited frame handler. It logs a warning,
  649. * releases the frame and returns a failure. enum sci_status
  650. * SCI_FAILURE_INVALID_STATE
  651. */
  652. static enum sci_status scic_sds_remote_device_default_frame_handler(
  653. struct scic_sds_remote_device *sci_dev,
  654. u32 frame_index)
  655. {
  656. dev_warn(scirdev_to_dev(sci_dev),
  657. "%s: SCIC Remote Device requested to handle frame %x "
  658. "while in wrong state %d\n",
  659. __func__,
  660. frame_index,
  661. sci_base_state_machine_get_state(
  662. &sci_dev->state_machine));
  663. /* Return the frame back to the controller */
  664. scic_sds_controller_release_frame(
  665. scic_sds_remote_device_get_controller(sci_dev), frame_index
  666. );
  667. return SCI_FAILURE_INVALID_STATE;
  668. }
  669. static enum sci_status scic_sds_remote_device_default_start_request_handler(
  670. struct scic_sds_remote_device *sci_dev,
  671. struct scic_sds_request *request)
  672. {
  673. return default_device_handler(sci_dev, __func__);
  674. }
  675. static enum sci_status scic_sds_remote_device_default_complete_request_handler(
  676. struct scic_sds_remote_device *sci_dev,
  677. struct scic_sds_request *request)
  678. {
  679. return default_device_handler(sci_dev, __func__);
  680. }
  681. /**
  682. *
  683. * @device: The struct scic_sds_remote_device which is then cast into a
  684. * struct scic_sds_remote_device.
  685. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  686. * device object to process.
  687. *
  688. * This method is a general ssp frame handler. In most cases the device object
  689. * needs to route the unsolicited frame processing to the io request object.
  690. * This method decodes the tag for the io request object and routes the
  691. * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
  692. */
  693. static enum sci_status scic_sds_remote_device_general_frame_handler(
  694. struct scic_sds_remote_device *sci_dev,
  695. u32 frame_index)
  696. {
  697. enum sci_status result;
  698. struct sci_ssp_frame_header *frame_header;
  699. struct scic_sds_request *io_request;
  700. result = scic_sds_unsolicited_frame_control_get_header(
  701. &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
  702. frame_index,
  703. (void **)&frame_header
  704. );
  705. if (SCI_SUCCESS == result) {
  706. io_request = scic_sds_controller_get_io_request_from_tag(
  707. scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
  708. if ((io_request == NULL)
  709. || (io_request->target_device != sci_dev)) {
  710. /*
  711. * We could not map this tag to a valid IO request
  712. * Just toss the frame and continue */
  713. scic_sds_controller_release_frame(
  714. scic_sds_remote_device_get_controller(sci_dev), frame_index
  715. );
  716. } else {
  717. /* The IO request is now in charge of releasing the frame */
  718. result = io_request->state_handlers->frame_handler(
  719. io_request, frame_index);
  720. }
  721. }
  722. return result;
  723. }
  724. /**
  725. *
  726. * @[in]: sci_dev This is the device object that is receiving the event.
  727. * @[in]: event_code The event code to process.
  728. *
  729. * This is a common method for handling events reported to the remote device
  730. * from the controller object. enum sci_status
  731. */
  732. static enum sci_status scic_sds_remote_device_general_event_handler(
  733. struct scic_sds_remote_device *sci_dev,
  734. u32 event_code)
  735. {
  736. return scic_sds_remote_device_core_event_handler(sci_dev,
  737. event_code,
  738. true);
  739. }
  740. /*
  741. * This method will attempt to start a task request for this device object. The
  742. * remote device object will issue the start request for the task and if
  743. * successful it will start the request for the port object then increment its
  744. * own requet count. enum sci_status SCI_SUCCESS if the task request is started for
  745. * this device object. SCI_FAILURE_INSUFFICIENT_RESOURCES if the io request
  746. * object could not get the resources to start.
  747. */
  748. static enum sci_status scic_sds_remote_device_ready_state_start_task_handler(
  749. struct scic_sds_remote_device *sci_dev,
  750. struct scic_sds_request *request)
  751. {
  752. enum sci_status result;
  753. /* See if the port is in a state where we can start the IO request */
  754. result = scic_sds_port_start_io(
  755. scic_sds_remote_device_get_port(sci_dev), sci_dev, request);
  756. if (result == SCI_SUCCESS) {
  757. result = scic_sds_remote_node_context_start_task(&sci_dev->rnc,
  758. request);
  759. if (result == SCI_SUCCESS)
  760. result = scic_sds_request_start(request);
  761. scic_sds_remote_device_start_request(sci_dev, request, result);
  762. }
  763. return result;
  764. }
  765. /*
  766. * This method will complete the request for the remote device object. The
  767. * method will call the completion handler for the request object and if
  768. * successful it will complete the request on the port object then decrement
  769. * its own started_request_count. enum sci_status
  770. */
  771. static enum sci_status scic_sds_remote_device_ready_state_complete_request_handler(
  772. struct scic_sds_remote_device *sci_dev,
  773. struct scic_sds_request *request)
  774. {
  775. enum sci_status result;
  776. result = scic_sds_request_complete(request);
  777. if (result != SCI_SUCCESS)
  778. return result;
  779. /* See if the port is in a state
  780. * where we can start the IO request */
  781. result = scic_sds_port_complete_io(
  782. scic_sds_remote_device_get_port(sci_dev),
  783. sci_dev, request);
  784. if (result == SCI_SUCCESS)
  785. scic_sds_remote_device_decrement_request_count(sci_dev);
  786. return result;
  787. }
  788. /**
  789. *
  790. * @device: The device object for which the request is completing.
  791. * @request: The task request that is being completed.
  792. *
  793. * This method completes requests for this struct scic_sds_remote_device while it is
  794. * in the SCI_BASE_REMOTE_DEVICE_STATE_STOPPING state. This method calls the
  795. * complete method for the request object and if that is successful the port
  796. * object is called to complete the task request. Then the device object itself
  797. * completes the task request. If struct scic_sds_remote_device started_request_count
  798. * goes to 0 and the invalidate RNC request has completed the device object can
  799. * transition to the SCI_BASE_REMOTE_DEVICE_STATE_STOPPED. enum sci_status
  800. */
  801. static enum sci_status scic_sds_remote_device_stopping_state_complete_request_handler(
  802. struct scic_sds_remote_device *sci_dev,
  803. struct scic_sds_request *request)
  804. {
  805. enum sci_status status = SCI_SUCCESS;
  806. status = scic_sds_request_complete(request);
  807. if (status != SCI_SUCCESS)
  808. return status;
  809. status = scic_sds_port_complete_io(scic_sds_remote_device_get_port(sci_dev),
  810. sci_dev, request);
  811. if (status != SCI_SUCCESS)
  812. return status;
  813. scic_sds_remote_device_decrement_request_count(sci_dev);
  814. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  815. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  816. rnc_destruct_done, sci_dev);
  817. return SCI_SUCCESS;
  818. }
  819. /* complete requests for this device while it is in the
  820. * SCI_BASE_REMOTE_DEVICE_STATE_RESETTING state. This method calls the complete
  821. * method for the request object and if that is successful the port object is
  822. * called to complete the task request. Then the device object itself completes
  823. * the task request. enum sci_status
  824. */
  825. static enum sci_status scic_sds_remote_device_resetting_state_complete_request_handler(
  826. struct scic_sds_remote_device *sci_dev,
  827. struct scic_sds_request *request)
  828. {
  829. enum sci_status status = SCI_SUCCESS;
  830. status = scic_sds_request_complete(request);
  831. if (status == SCI_SUCCESS) {
  832. status = scic_sds_port_complete_io(
  833. scic_sds_remote_device_get_port(sci_dev),
  834. sci_dev, request);
  835. if (status == SCI_SUCCESS) {
  836. scic_sds_remote_device_decrement_request_count(sci_dev);
  837. }
  838. }
  839. return status;
  840. }
  841. static enum sci_status scic_sds_stp_remote_device_complete_request(struct scic_sds_remote_device *sci_dev,
  842. struct scic_sds_request *sci_req)
  843. {
  844. enum sci_status status;
  845. status = scic_sds_io_request_complete(sci_req);
  846. if (status != SCI_SUCCESS)
  847. goto out;
  848. status = scic_sds_port_complete_io(sci_dev->owning_port, sci_dev, sci_req);
  849. if (status != SCI_SUCCESS)
  850. goto out;
  851. scic_sds_remote_device_decrement_request_count(sci_dev);
  852. if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  853. /* This request causes hardware error, device needs to be Lun Reset.
  854. * So here we force the state machine to IDLE state so the rest IOs
  855. * can reach RNC state handler, these IOs will be completed by RNC with
  856. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  857. */
  858. sci_base_state_machine_change_state(&sci_dev->state_machine,
  859. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  860. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  861. sci_base_state_machine_change_state(&sci_dev->state_machine,
  862. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  863. out:
  864. if (status != SCI_SUCCESS)
  865. dev_err(scirdev_to_dev(sci_dev),
  866. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  867. "could not complete\n", __func__, sci_dev->owning_port,
  868. sci_dev, sci_req, status);
  869. return status;
  870. }
  871. /* scic_sds_stp_remote_device_ready_substate_start_request_handler - start stp
  872. * @device: The target device a task management request towards to.
  873. * @request: The task request.
  874. *
  875. * This is the READY NCQ substate handler to start task management request. In
  876. * this routine, we suspend and resume the RNC. enum sci_status Always return
  877. * SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS status to let
  878. * controller_start_task_handler know that the controller can't post TC for
  879. * task request yet, instead, when RNC gets resumed, a controller_continue_task
  880. * callback will be called.
  881. */
  882. static enum sci_status scic_sds_stp_remote_device_ready_substate_start_request_handler(
  883. struct scic_sds_remote_device *device,
  884. struct scic_sds_request *request)
  885. {
  886. enum sci_status status;
  887. /* Will the port allow the io request to start? */
  888. status = device->owning_port->state_handlers->start_io_handler(
  889. device->owning_port, device, request);
  890. if (status != SCI_SUCCESS)
  891. return status;
  892. status = scic_sds_remote_node_context_start_task(&device->rnc, request);
  893. if (status != SCI_SUCCESS)
  894. goto out;
  895. status = request->state_handlers->start_handler(request);
  896. if (status != SCI_SUCCESS)
  897. goto out;
  898. /*
  899. * Note: If the remote device state is not IDLE this will replace
  900. * the request that probably resulted in the task management request.
  901. */
  902. device->working_request = request;
  903. sci_base_state_machine_change_state(&device->state_machine,
  904. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  905. /*
  906. * The remote node context must cleanup the TCi to NCQ mapping table.
  907. * The only way to do this correctly is to either write to the TLCR
  908. * register or to invalidate and repost the RNC. In either case the
  909. * remote node context state machine will take the correct action when
  910. * the remote node context is suspended and later resumed.
  911. */
  912. scic_sds_remote_node_context_suspend(&device->rnc,
  913. SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  914. scic_sds_remote_node_context_resume(&device->rnc,
  915. scic_sds_remote_device_continue_request,
  916. device);
  917. out:
  918. scic_sds_remote_device_start_request(device, request, status);
  919. /*
  920. * We need to let the controller start request handler know that it can't
  921. * post TC yet. We will provide a callback function to post TC when RNC gets
  922. * resumed.
  923. */
  924. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  925. }
  926. static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
  927. struct scic_sds_remote_device *sci_dev,
  928. u32 event_code)
  929. {
  930. enum sci_status status;
  931. status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
  932. if (status != SCI_SUCCESS)
  933. return status;
  934. /* We pick up suspension events to handle specifically to this state. We
  935. * resume the RNC right away. enum sci_status
  936. */
  937. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  938. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  939. status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  940. return status;
  941. }
  942. static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
  943. u32 frame_index)
  944. {
  945. enum sci_status status;
  946. struct sata_fis_header *frame_header;
  947. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  948. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  949. frame_index,
  950. (void **)&frame_header);
  951. if (status != SCI_SUCCESS)
  952. return status;
  953. if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
  954. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  955. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  956. /* TODO Check sactive and complete associated IO if any. */
  957. sci_base_state_machine_change_state(&sci_dev->state_machine,
  958. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  959. } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
  960. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  961. /*
  962. * Some devices return D2H FIS when an NCQ error is detected.
  963. * Treat this like an SDB error FIS ready reason.
  964. */
  965. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  966. sci_base_state_machine_change_state(&sci_dev->state_machine,
  967. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  968. } else
  969. status = SCI_FAILURE;
  970. scic_sds_controller_release_frame(scic, frame_index);
  971. return status;
  972. }
  973. static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler(
  974. struct scic_sds_remote_device *sci_dev,
  975. u32 suspend_type)
  976. {
  977. enum sci_status status;
  978. status = scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  979. suspend_type, NULL, NULL);
  980. return status;
  981. }
  982. static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
  983. struct scic_sds_remote_device *sci_dev,
  984. u32 frame_index)
  985. {
  986. /* The device doe not process any UF received from the hardware while
  987. * in this state. All unsolicited frames are forwarded to the io
  988. * request object.
  989. */
  990. return scic_sds_io_request_frame_handler(sci_dev->working_request,
  991. frame_index);
  992. }
  993. static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  994. {
  995. struct scic_sds_remote_device *sci_dev = _dev;
  996. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  997. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  998. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  999. * As a result, avoid sending the ready notification.
  1000. */
  1001. if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
  1002. isci_remote_device_ready(scic->ihost, idev);
  1003. }
  1004. static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
  1005. struct scic_sds_remote_device *sci_dev,
  1006. u32 frame_index)
  1007. {
  1008. enum sci_status status;
  1009. /* The device does not process any UF received from the hardware while
  1010. * in this state. All unsolicited frames are forwarded to the io request
  1011. * object.
  1012. */
  1013. status = scic_sds_io_request_frame_handler(
  1014. sci_dev->working_request,
  1015. frame_index
  1016. );
  1017. return status;
  1018. }
  1019. static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
  1020. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1021. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1022. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1023. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1024. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1025. .event_handler = scic_sds_remote_device_default_event_handler,
  1026. .frame_handler = scic_sds_remote_device_default_frame_handler
  1027. },
  1028. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1029. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1030. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1031. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1032. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1033. .event_handler = scic_sds_remote_device_default_event_handler,
  1034. .frame_handler = scic_sds_remote_device_default_frame_handler
  1035. },
  1036. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1037. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1038. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1039. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1040. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1041. .event_handler = scic_sds_remote_device_general_event_handler,
  1042. .frame_handler = scic_sds_remote_device_default_frame_handler
  1043. },
  1044. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1045. .start_task_handler = scic_sds_remote_device_ready_state_start_task_handler,
  1046. .complete_task_handler = scic_sds_remote_device_ready_state_complete_request_handler,
  1047. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1048. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1049. .event_handler = scic_sds_remote_device_general_event_handler,
  1050. .frame_handler = scic_sds_remote_device_general_frame_handler,
  1051. },
  1052. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1053. .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
  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_stp_remote_device_ready_idle_substate_event_handler,
  1058. .frame_handler = scic_sds_remote_device_default_frame_handler
  1059. },
  1060. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1061. .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
  1062. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1063. .suspend_handler = scic_sds_stp_remote_device_ready_cmd_substate_suspend_handler,
  1064. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1065. .event_handler = scic_sds_remote_device_general_event_handler,
  1066. .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
  1067. },
  1068. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1069. .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
  1070. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1071. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1072. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1073. .event_handler = scic_sds_remote_device_general_event_handler,
  1074. .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
  1075. },
  1076. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1077. .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
  1078. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1079. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1080. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1081. .event_handler = scic_sds_remote_device_general_event_handler,
  1082. .frame_handler = scic_sds_remote_device_general_frame_handler
  1083. },
  1084. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1085. .start_task_handler = scic_sds_stp_remote_device_ready_substate_start_request_handler,
  1086. .complete_task_handler = scic_sds_stp_remote_device_complete_request,
  1087. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1088. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1089. .event_handler = scic_sds_remote_device_general_event_handler,
  1090. .frame_handler = scic_sds_remote_device_general_frame_handler
  1091. },
  1092. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1093. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1094. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1095. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1096. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1097. .event_handler = scic_sds_remote_device_general_event_handler,
  1098. .frame_handler = scic_sds_remote_device_default_frame_handler
  1099. },
  1100. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1101. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1102. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1103. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1104. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1105. .event_handler = scic_sds_remote_device_general_event_handler,
  1106. .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
  1107. },
  1108. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1109. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1110. .complete_task_handler = scic_sds_remote_device_stopping_state_complete_request_handler,
  1111. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1112. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1113. .event_handler = scic_sds_remote_device_general_event_handler,
  1114. .frame_handler = scic_sds_remote_device_general_frame_handler
  1115. },
  1116. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1117. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1118. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1119. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1120. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1121. .event_handler = scic_sds_remote_device_default_event_handler,
  1122. .frame_handler = scic_sds_remote_device_general_frame_handler
  1123. },
  1124. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1125. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1126. .complete_task_handler = scic_sds_remote_device_resetting_state_complete_request_handler,
  1127. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1128. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1129. .event_handler = scic_sds_remote_device_default_event_handler,
  1130. .frame_handler = scic_sds_remote_device_general_frame_handler
  1131. },
  1132. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1133. .start_task_handler = scic_sds_remote_device_default_start_request_handler,
  1134. .complete_task_handler = scic_sds_remote_device_default_complete_request_handler,
  1135. .suspend_handler = scic_sds_remote_device_default_suspend_handler,
  1136. .resume_handler = scic_sds_remote_device_default_resume_handler,
  1137. .event_handler = scic_sds_remote_device_default_event_handler,
  1138. .frame_handler = scic_sds_remote_device_default_frame_handler
  1139. }
  1140. };
  1141. static void scic_sds_remote_device_initial_state_enter(void *object)
  1142. {
  1143. struct scic_sds_remote_device *sci_dev = object;
  1144. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1145. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
  1146. /* Initial state is a transitional state to the stopped state */
  1147. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1148. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1149. }
  1150. /**
  1151. * scic_remote_device_destruct() - free remote node context and destruct
  1152. * @remote_device: This parameter specifies the remote device to be destructed.
  1153. *
  1154. * Remote device objects are a limited resource. As such, they must be
  1155. * protected. Thus calls to construct and destruct are mutually exclusive and
  1156. * non-reentrant. The return value shall indicate if the device was
  1157. * successfully destructed or if some failure occurred. enum sci_status This value
  1158. * is returned if the device is successfully destructed.
  1159. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  1160. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  1161. * valid, etc.).
  1162. */
  1163. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  1164. {
  1165. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1166. enum scic_sds_remote_device_states state = sm->current_state_id;
  1167. struct scic_sds_controller *scic;
  1168. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1169. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1170. __func__, state);
  1171. return SCI_FAILURE_INVALID_STATE;
  1172. }
  1173. scic = sci_dev->owning_port->owning_controller;
  1174. scic_sds_controller_free_remote_node_context(scic, sci_dev,
  1175. sci_dev->rnc.remote_node_index);
  1176. sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  1177. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
  1178. return SCI_SUCCESS;
  1179. }
  1180. /**
  1181. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  1182. * @ihost: This parameter specifies the isci host object.
  1183. * @idev: This parameter specifies the remote device to be freed.
  1184. *
  1185. */
  1186. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  1187. {
  1188. dev_dbg(&ihost->pdev->dev,
  1189. "%s: isci_device = %p\n", __func__, idev);
  1190. /* There should not be any outstanding io's. All paths to
  1191. * here should go through isci_remote_device_nuke_requests.
  1192. * If we hit this condition, we will need a way to complete
  1193. * io requests in process */
  1194. while (!list_empty(&idev->reqs_in_process)) {
  1195. dev_err(&ihost->pdev->dev,
  1196. "%s: ** request list not empty! **\n", __func__);
  1197. BUG();
  1198. }
  1199. scic_remote_device_destruct(&idev->sci);
  1200. idev->domain_dev->lldd_dev = NULL;
  1201. idev->domain_dev = NULL;
  1202. idev->isci_port = NULL;
  1203. list_del_init(&idev->node);
  1204. clear_bit(IDEV_START_PENDING, &idev->flags);
  1205. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1206. wake_up(&ihost->eventq);
  1207. }
  1208. /**
  1209. * isci_remote_device_stop_complete() - This function is called by the scic
  1210. * when the remote device stop has completed. We mark the isci device as not
  1211. * ready and remove the isci remote device.
  1212. * @ihost: This parameter specifies the isci host object.
  1213. * @idev: This parameter specifies the remote device.
  1214. * @status: This parameter specifies status of the completion.
  1215. *
  1216. */
  1217. static void isci_remote_device_stop_complete(struct isci_host *ihost,
  1218. struct isci_remote_device *idev)
  1219. {
  1220. dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
  1221. isci_remote_device_change_state(idev, isci_stopped);
  1222. /* after stop, we can tear down resources. */
  1223. isci_remote_device_deconstruct(ihost, idev);
  1224. }
  1225. static void scic_sds_remote_device_stopped_state_enter(void *object)
  1226. {
  1227. struct scic_sds_remote_device *sci_dev = object;
  1228. struct scic_sds_controller *scic;
  1229. struct isci_remote_device *idev;
  1230. struct isci_host *ihost;
  1231. u32 prev_state;
  1232. scic = scic_sds_remote_device_get_controller(sci_dev);
  1233. ihost = scic->ihost;
  1234. idev = sci_dev_to_idev(sci_dev);
  1235. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1236. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1237. /* If we are entering from the stopping state let the SCI User know that
  1238. * the stop operation has completed.
  1239. */
  1240. prev_state = sci_dev->state_machine.previous_state_id;
  1241. if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
  1242. isci_remote_device_stop_complete(ihost, idev);
  1243. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  1244. }
  1245. static void scic_sds_remote_device_starting_state_enter(void *object)
  1246. {
  1247. struct scic_sds_remote_device *sci_dev = object;
  1248. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1249. struct isci_host *ihost = scic->ihost;
  1250. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1251. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1252. SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1253. isci_remote_device_not_ready(ihost, idev,
  1254. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  1255. }
  1256. static void scic_sds_remote_device_ready_state_enter(void *object)
  1257. {
  1258. struct scic_sds_remote_device *sci_dev = object;
  1259. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1260. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1261. SET_STATE_HANDLER(sci_dev,
  1262. scic_sds_remote_device_state_handler_table,
  1263. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  1264. scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
  1265. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  1266. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1267. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1268. } else if (dev_is_expander(dev)) {
  1269. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1270. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1271. } else
  1272. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1273. }
  1274. static void scic_sds_remote_device_ready_state_exit(void *object)
  1275. {
  1276. struct scic_sds_remote_device *sci_dev = object;
  1277. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1278. if (dev->dev_type == SAS_END_DEV) {
  1279. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1280. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1281. isci_remote_device_not_ready(scic->ihost, idev,
  1282. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  1283. }
  1284. }
  1285. static void scic_sds_remote_device_stopping_state_enter(void *object)
  1286. {
  1287. struct scic_sds_remote_device *sci_dev = object;
  1288. SET_STATE_HANDLER(
  1289. sci_dev,
  1290. scic_sds_remote_device_state_handler_table,
  1291. SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
  1292. );
  1293. }
  1294. static void scic_sds_remote_device_failed_state_enter(void *object)
  1295. {
  1296. struct scic_sds_remote_device *sci_dev = object;
  1297. SET_STATE_HANDLER(
  1298. sci_dev,
  1299. scic_sds_remote_device_state_handler_table,
  1300. SCI_BASE_REMOTE_DEVICE_STATE_FAILED
  1301. );
  1302. }
  1303. static void scic_sds_remote_device_resetting_state_enter(void *object)
  1304. {
  1305. struct scic_sds_remote_device *sci_dev = object;
  1306. SET_STATE_HANDLER(
  1307. sci_dev,
  1308. scic_sds_remote_device_state_handler_table,
  1309. SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
  1310. );
  1311. scic_sds_remote_node_context_suspend(
  1312. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  1313. }
  1314. static void scic_sds_remote_device_resetting_state_exit(void *object)
  1315. {
  1316. struct scic_sds_remote_device *sci_dev = object;
  1317. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  1318. }
  1319. static void scic_sds_remote_device_final_state_enter(void *object)
  1320. {
  1321. struct scic_sds_remote_device *sci_dev = object;
  1322. SET_STATE_HANDLER(
  1323. sci_dev,
  1324. scic_sds_remote_device_state_handler_table,
  1325. SCI_BASE_REMOTE_DEVICE_STATE_FINAL
  1326. );
  1327. }
  1328. static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
  1329. {
  1330. struct scic_sds_remote_device *sci_dev = object;
  1331. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1332. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1333. sci_dev->working_request = NULL;
  1334. if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
  1335. /*
  1336. * Since the RNC is ready, it's alright to finish completion
  1337. * processing (e.g. signal the remote device is ready). */
  1338. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
  1339. } else {
  1340. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1341. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
  1342. sci_dev);
  1343. }
  1344. }
  1345. static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
  1346. {
  1347. struct scic_sds_remote_device *sci_dev = object;
  1348. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1349. BUG_ON(sci_dev->working_request == NULL);
  1350. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1351. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1352. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1353. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  1354. }
  1355. static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
  1356. {
  1357. struct scic_sds_remote_device *sci_dev = object;
  1358. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1359. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
  1360. }
  1361. static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
  1362. {
  1363. struct scic_sds_remote_device *sci_dev = object;
  1364. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1365. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1366. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1367. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  1368. if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  1369. isci_remote_device_not_ready(scic->ihost, idev,
  1370. sci_dev->not_ready_reason);
  1371. }
  1372. static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
  1373. {
  1374. struct scic_sds_remote_device *sci_dev = object;
  1375. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1376. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  1377. }
  1378. static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
  1379. {
  1380. struct scic_sds_remote_device *sci_dev = object;
  1381. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1382. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1383. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1384. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1385. }
  1386. static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
  1387. {
  1388. struct scic_sds_remote_device *sci_dev = object;
  1389. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1390. BUG_ON(sci_dev->working_request == NULL);
  1391. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1392. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1393. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1394. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  1395. }
  1396. static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
  1397. {
  1398. struct scic_sds_remote_device *sci_dev = object;
  1399. sci_dev->working_request = NULL;
  1400. }
  1401. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  1402. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1403. .enter_state = scic_sds_remote_device_initial_state_enter,
  1404. },
  1405. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1406. .enter_state = scic_sds_remote_device_stopped_state_enter,
  1407. },
  1408. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1409. .enter_state = scic_sds_remote_device_starting_state_enter,
  1410. },
  1411. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1412. .enter_state = scic_sds_remote_device_ready_state_enter,
  1413. .exit_state = scic_sds_remote_device_ready_state_exit
  1414. },
  1415. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1416. .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
  1417. },
  1418. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1419. .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
  1420. },
  1421. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1422. .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
  1423. },
  1424. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1425. .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
  1426. },
  1427. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1428. .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
  1429. },
  1430. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1431. .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
  1432. },
  1433. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1434. .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
  1435. .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
  1436. },
  1437. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1438. .enter_state = scic_sds_remote_device_stopping_state_enter,
  1439. },
  1440. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1441. .enter_state = scic_sds_remote_device_failed_state_enter,
  1442. },
  1443. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1444. .enter_state = scic_sds_remote_device_resetting_state_enter,
  1445. .exit_state = scic_sds_remote_device_resetting_state_exit
  1446. },
  1447. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1448. .enter_state = scic_sds_remote_device_final_state_enter,
  1449. },
  1450. };
  1451. /**
  1452. * scic_remote_device_construct() - common construction
  1453. * @sci_port: SAS/SATA port through which this device is accessed.
  1454. * @sci_dev: remote device to construct
  1455. *
  1456. * This routine just performs benign initialization and does not
  1457. * allocate the remote_node_context which is left to
  1458. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  1459. * frees the remote_node_context(s) for the device.
  1460. */
  1461. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  1462. struct scic_sds_remote_device *sci_dev)
  1463. {
  1464. sci_dev->owning_port = sci_port;
  1465. sci_dev->started_request_count = 0;
  1466. sci_base_state_machine_construct(
  1467. &sci_dev->state_machine,
  1468. sci_dev,
  1469. scic_sds_remote_device_state_table,
  1470. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
  1471. );
  1472. sci_base_state_machine_start(
  1473. &sci_dev->state_machine
  1474. );
  1475. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  1476. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1477. }
  1478. /**
  1479. * scic_remote_device_da_construct() - construct direct attached device.
  1480. *
  1481. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1482. * the device is known to the SCI Core since it is contained in the
  1483. * scic_phy object. Remote node context(s) is/are a global resource
  1484. * allocated by this routine, freed by scic_remote_device_destruct().
  1485. *
  1486. * Returns:
  1487. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1488. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1489. * sata-only controller instance.
  1490. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1491. */
  1492. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  1493. struct scic_sds_remote_device *sci_dev)
  1494. {
  1495. enum sci_status status;
  1496. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1497. scic_remote_device_construct(sci_port, sci_dev);
  1498. /*
  1499. * This information is request to determine how many remote node context
  1500. * entries will be needed to store the remote node.
  1501. */
  1502. sci_dev->is_direct_attached = true;
  1503. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1504. sci_dev,
  1505. &sci_dev->rnc.remote_node_index);
  1506. if (status != SCI_SUCCESS)
  1507. return status;
  1508. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1509. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1510. /* pass */;
  1511. else
  1512. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1513. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  1514. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1515. sci_dev->device_port_width = 1;
  1516. return SCI_SUCCESS;
  1517. }
  1518. /**
  1519. * scic_remote_device_ea_construct() - construct expander attached device
  1520. *
  1521. * Remote node context(s) is/are a global resource allocated by this
  1522. * routine, freed by scic_remote_device_destruct().
  1523. *
  1524. * Returns:
  1525. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1526. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1527. * sata-only controller instance.
  1528. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1529. */
  1530. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  1531. struct scic_sds_remote_device *sci_dev)
  1532. {
  1533. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1534. enum sci_status status;
  1535. scic_remote_device_construct(sci_port, sci_dev);
  1536. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1537. sci_dev,
  1538. &sci_dev->rnc.remote_node_index);
  1539. if (status != SCI_SUCCESS)
  1540. return status;
  1541. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1542. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1543. /* pass */;
  1544. else
  1545. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1546. /*
  1547. * For SAS-2 the physical link rate is actually a logical link
  1548. * rate that incorporates multiplexing. The SCU doesn't
  1549. * incorporate multiplexing and for the purposes of the
  1550. * connection the logical link rate is that same as the
  1551. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1552. * one another, so this code works for both situations. */
  1553. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1554. dev->linkrate);
  1555. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1556. sci_dev->device_port_width = 1;
  1557. return SCI_SUCCESS;
  1558. }
  1559. /**
  1560. * scic_remote_device_start() - This method will start the supplied remote
  1561. * device. This method enables normal IO requests to flow through to the
  1562. * remote device.
  1563. * @remote_device: This parameter specifies the device to be started.
  1564. * @timeout: This parameter specifies the number of milliseconds in which the
  1565. * start operation should complete.
  1566. *
  1567. * An indication of whether the device was successfully started. SCI_SUCCESS
  1568. * This value is returned if the device was successfully started.
  1569. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1570. * the device when there have been no phys added to it.
  1571. */
  1572. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1573. u32 timeout)
  1574. {
  1575. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1576. enum scic_sds_remote_device_states state = sm->current_state_id;
  1577. enum sci_status status;
  1578. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1579. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1580. __func__, state);
  1581. return SCI_FAILURE_INVALID_STATE;
  1582. }
  1583. status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1584. remote_device_resume_done,
  1585. sci_dev);
  1586. if (status != SCI_SUCCESS)
  1587. return status;
  1588. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1589. return SCI_SUCCESS;
  1590. }
  1591. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1592. struct isci_remote_device *idev)
  1593. {
  1594. struct scic_sds_port *sci_port = iport->sci_port_handle;
  1595. struct isci_host *ihost = iport->isci_host;
  1596. struct domain_device *dev = idev->domain_dev;
  1597. enum sci_status status;
  1598. if (dev->parent && dev_is_expander(dev->parent))
  1599. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1600. else
  1601. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1602. if (status != SCI_SUCCESS) {
  1603. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1604. __func__, status);
  1605. return status;
  1606. }
  1607. /* start the device. */
  1608. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1609. if (status != SCI_SUCCESS)
  1610. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1611. status);
  1612. return status;
  1613. }
  1614. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1615. {
  1616. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1617. dev_dbg(&ihost->pdev->dev,
  1618. "%s: idev = %p\n", __func__, idev);
  1619. /* Cleanup all requests pending for this device. */
  1620. isci_terminate_pending_requests(ihost, idev, terminating);
  1621. dev_dbg(&ihost->pdev->dev,
  1622. "%s: idev = %p, done\n", __func__, idev);
  1623. }
  1624. /**
  1625. * This function builds the isci_remote_device when a libsas dev_found message
  1626. * is received.
  1627. * @isci_host: This parameter specifies the isci host object.
  1628. * @port: This parameter specifies the isci_port conected to this device.
  1629. *
  1630. * pointer to new isci_remote_device.
  1631. */
  1632. static struct isci_remote_device *
  1633. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1634. {
  1635. struct isci_remote_device *idev;
  1636. int i;
  1637. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1638. idev = &ihost->devices[i];
  1639. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1640. break;
  1641. }
  1642. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1643. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1644. return NULL;
  1645. }
  1646. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1647. return NULL;
  1648. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1649. return NULL;
  1650. isci_remote_device_change_state(idev, isci_freed);
  1651. return idev;
  1652. }
  1653. /**
  1654. * isci_remote_device_stop() - This function is called internally to stop the
  1655. * remote device.
  1656. * @isci_host: This parameter specifies the isci host object.
  1657. * @isci_device: This parameter specifies the remote device.
  1658. *
  1659. * The status of the scic request to stop.
  1660. */
  1661. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1662. {
  1663. enum sci_status status;
  1664. unsigned long flags;
  1665. dev_dbg(&ihost->pdev->dev,
  1666. "%s: isci_device = %p\n", __func__, idev);
  1667. isci_remote_device_change_state(idev, isci_stopping);
  1668. /* Kill all outstanding requests. */
  1669. isci_remote_device_nuke_requests(ihost, idev);
  1670. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1671. spin_lock_irqsave(&ihost->scic_lock, flags);
  1672. status = scic_remote_device_stop(&idev->sci, 50);
  1673. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1674. /* Wait for the stop complete callback. */
  1675. if (status == SCI_SUCCESS) {
  1676. wait_for_device_stop(ihost, idev);
  1677. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1678. }
  1679. dev_dbg(&ihost->pdev->dev,
  1680. "%s: idev = %p - after completion wait\n",
  1681. __func__, idev);
  1682. return status;
  1683. }
  1684. /**
  1685. * isci_remote_device_gone() - This function is called by libsas when a domain
  1686. * device is removed.
  1687. * @domain_device: This parameter specifies the libsas domain device.
  1688. *
  1689. */
  1690. void isci_remote_device_gone(struct domain_device *dev)
  1691. {
  1692. struct isci_host *ihost = dev_to_ihost(dev);
  1693. struct isci_remote_device *idev = dev->lldd_dev;
  1694. dev_dbg(&ihost->pdev->dev,
  1695. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1696. __func__, dev, idev, idev->isci_port);
  1697. isci_remote_device_stop(ihost, idev);
  1698. }
  1699. /**
  1700. * isci_remote_device_found() - This function is called by libsas when a remote
  1701. * device is discovered. A remote device object is created and started. the
  1702. * function then sleeps until the sci core device started message is
  1703. * received.
  1704. * @domain_device: This parameter specifies the libsas domain device.
  1705. *
  1706. * status, zero indicates success.
  1707. */
  1708. int isci_remote_device_found(struct domain_device *domain_dev)
  1709. {
  1710. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1711. struct isci_port *isci_port;
  1712. struct isci_phy *isci_phy;
  1713. struct asd_sas_port *sas_port;
  1714. struct asd_sas_phy *sas_phy;
  1715. struct isci_remote_device *isci_device;
  1716. enum sci_status status;
  1717. dev_dbg(&isci_host->pdev->dev,
  1718. "%s: domain_device = %p\n", __func__, domain_dev);
  1719. wait_for_start(isci_host);
  1720. sas_port = domain_dev->port;
  1721. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1722. port_phy_el);
  1723. isci_phy = to_isci_phy(sas_phy);
  1724. isci_port = isci_phy->isci_port;
  1725. /* we are being called for a device on this port,
  1726. * so it has to come up eventually
  1727. */
  1728. wait_for_completion(&isci_port->start_complete);
  1729. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1730. (isci_stopped == isci_port_get_state(isci_port)))
  1731. return -ENODEV;
  1732. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1733. if (!isci_device)
  1734. return -ENODEV;
  1735. INIT_LIST_HEAD(&isci_device->node);
  1736. domain_dev->lldd_dev = isci_device;
  1737. isci_device->domain_dev = domain_dev;
  1738. isci_device->isci_port = isci_port;
  1739. isci_remote_device_change_state(isci_device, isci_starting);
  1740. spin_lock_irq(&isci_host->scic_lock);
  1741. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1742. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1743. status = isci_remote_device_construct(isci_port, isci_device);
  1744. spin_unlock_irq(&isci_host->scic_lock);
  1745. dev_dbg(&isci_host->pdev->dev,
  1746. "%s: isci_device = %p\n",
  1747. __func__, isci_device);
  1748. if (status != SCI_SUCCESS) {
  1749. spin_lock_irq(&isci_host->scic_lock);
  1750. isci_remote_device_deconstruct(
  1751. isci_host,
  1752. isci_device
  1753. );
  1754. spin_unlock_irq(&isci_host->scic_lock);
  1755. return -ENODEV;
  1756. }
  1757. /* wait for the device ready callback. */
  1758. wait_for_device_start(isci_host, isci_device);
  1759. return 0;
  1760. }
  1761. /**
  1762. * isci_device_is_reset_pending() - This function will check if there is any
  1763. * pending reset condition on the device.
  1764. * @request: This parameter is the isci_device object.
  1765. *
  1766. * true if there is a reset pending for the device.
  1767. */
  1768. bool isci_device_is_reset_pending(
  1769. struct isci_host *isci_host,
  1770. struct isci_remote_device *isci_device)
  1771. {
  1772. struct isci_request *isci_request;
  1773. struct isci_request *tmp_req;
  1774. bool reset_is_pending = false;
  1775. unsigned long flags;
  1776. dev_dbg(&isci_host->pdev->dev,
  1777. "%s: isci_device = %p\n", __func__, isci_device);
  1778. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1779. /* Check for reset on all pending requests. */
  1780. list_for_each_entry_safe(isci_request, tmp_req,
  1781. &isci_device->reqs_in_process, dev_node) {
  1782. dev_dbg(&isci_host->pdev->dev,
  1783. "%s: isci_device = %p request = %p\n",
  1784. __func__, isci_device, isci_request);
  1785. if (isci_request->ttype == io_task) {
  1786. struct sas_task *task = isci_request_access_task(
  1787. isci_request);
  1788. spin_lock(&task->task_state_lock);
  1789. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1790. reset_is_pending = true;
  1791. spin_unlock(&task->task_state_lock);
  1792. }
  1793. }
  1794. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1795. dev_dbg(&isci_host->pdev->dev,
  1796. "%s: isci_device = %p reset_is_pending = %d\n",
  1797. __func__, isci_device, reset_is_pending);
  1798. return reset_is_pending;
  1799. }
  1800. /**
  1801. * isci_device_clear_reset_pending() - This function will clear if any pending
  1802. * reset condition flags on the device.
  1803. * @request: This parameter is the isci_device object.
  1804. *
  1805. * true if there is a reset pending for the device.
  1806. */
  1807. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1808. {
  1809. struct isci_request *isci_request;
  1810. struct isci_request *tmp_req;
  1811. unsigned long flags = 0;
  1812. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1813. __func__, idev, ihost);
  1814. spin_lock_irqsave(&ihost->scic_lock, flags);
  1815. /* Clear reset pending on all pending requests. */
  1816. list_for_each_entry_safe(isci_request, tmp_req,
  1817. &idev->reqs_in_process, dev_node) {
  1818. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1819. __func__, idev, isci_request);
  1820. if (isci_request->ttype == io_task) {
  1821. unsigned long flags2;
  1822. struct sas_task *task = isci_request_access_task(
  1823. isci_request);
  1824. spin_lock_irqsave(&task->task_state_lock, flags2);
  1825. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1826. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1827. }
  1828. }
  1829. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1830. }