remote_device.c 86 KB

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