remote_device.c 85 KB

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