remote_device.c 82 KB

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