remote_device.c 81 KB

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