remote_device.c 65 KB

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