remote_device.c 79 KB

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