remote_device.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
  245. u32 suspend_type)
  246. {
  247. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  248. enum scic_sds_remote_device_states state = sm->current_state_id;
  249. if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
  250. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  251. __func__, state);
  252. return SCI_FAILURE_INVALID_STATE;
  253. }
  254. return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  255. suspend_type, NULL, NULL);
  256. }
  257. enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
  258. u32 frame_index)
  259. {
  260. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  261. enum scic_sds_remote_device_states state = sm->current_state_id;
  262. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  263. enum sci_status status;
  264. switch (state) {
  265. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  266. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  267. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  268. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  269. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  270. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  271. default:
  272. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  273. __func__, state);
  274. /* Return the frame back to the controller */
  275. scic_sds_controller_release_frame(scic, frame_index);
  276. return SCI_FAILURE_INVALID_STATE;
  277. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  278. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  279. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  280. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  281. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  282. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING: {
  283. struct scic_sds_request *sci_req;
  284. struct sci_ssp_frame_header *hdr;
  285. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  286. frame_index,
  287. (void **)&hdr);
  288. if (status != SCI_SUCCESS)
  289. return status;
  290. sci_req = scic_sds_controller_get_io_request_from_tag(scic, hdr->tag);
  291. if (sci_req && sci_req->target_device == sci_dev) {
  292. /* The IO request is now in charge of releasing the frame */
  293. status = sci_req->state_handlers->frame_handler(sci_req,
  294. frame_index);
  295. } else {
  296. /* We could not map this tag to a valid IO
  297. * request Just toss the frame and continue
  298. */
  299. scic_sds_controller_release_frame(scic, frame_index);
  300. }
  301. break;
  302. }
  303. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ: {
  304. struct sata_fis_header *hdr;
  305. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  306. frame_index,
  307. (void **)&hdr);
  308. if (status != SCI_SUCCESS)
  309. return status;
  310. if (hdr->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
  311. (hdr->status & ATA_STATUS_REG_ERROR_BIT)) {
  312. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  313. /* TODO Check sactive and complete associated IO if any. */
  314. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  315. } else if (hdr->fis_type == SATA_FIS_TYPE_REGD2H &&
  316. (hdr->status & ATA_STATUS_REG_ERROR_BIT)) {
  317. /*
  318. * Some devices return D2H FIS when an NCQ error is detected.
  319. * Treat this like an SDB error FIS ready reason.
  320. */
  321. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  322. sci_base_state_machine_change_state(&sci_dev->state_machine,
  323. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  324. } else
  325. status = SCI_FAILURE;
  326. scic_sds_controller_release_frame(scic, frame_index);
  327. break;
  328. }
  329. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  330. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  331. /* The device does not process any UF received from the hardware while
  332. * in this state. All unsolicited frames are forwarded to the io request
  333. * object.
  334. */
  335. status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
  336. break;
  337. }
  338. return status;
  339. }
  340. static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
  341. {
  342. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  343. enum scic_sds_remote_device_states state = sm->current_state_id;
  344. switch (state) {
  345. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  346. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  347. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  348. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  349. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  350. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  351. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  352. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  353. return true;
  354. default:
  355. return false;
  356. }
  357. }
  358. enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
  359. u32 event_code)
  360. {
  361. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  362. enum scic_sds_remote_device_states state = sm->current_state_id;
  363. enum sci_status status;
  364. switch (scu_get_event_type(event_code)) {
  365. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  366. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  367. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  368. status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
  369. break;
  370. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  371. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  372. status = SCI_SUCCESS;
  373. /* Suspend the associated RNC */
  374. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  375. SCI_SOFTWARE_SUSPENSION,
  376. NULL, NULL);
  377. dev_dbg(scirdev_to_dev(sci_dev),
  378. "%s: device: %p event code: %x: %s\n",
  379. __func__, sci_dev, event_code,
  380. is_remote_device_ready(sci_dev)
  381. ? "I_T_Nexus_Timeout event"
  382. : "I_T_Nexus_Timeout event in wrong state");
  383. break;
  384. }
  385. /* Else, fall through and treat as unhandled... */
  386. default:
  387. dev_dbg(scirdev_to_dev(sci_dev),
  388. "%s: device: %p event code: %x: %s\n",
  389. __func__, sci_dev, event_code,
  390. is_remote_device_ready(sci_dev)
  391. ? "unexpected event"
  392. : "unexpected event in wrong state");
  393. status = SCI_FAILURE_INVALID_STATE;
  394. break;
  395. }
  396. if (status != SCI_SUCCESS)
  397. return status;
  398. if (state == SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE) {
  399. /* We pick up suspension events to handle specifically to this
  400. * state. We resume the RNC right away.
  401. */
  402. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  403. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  404. status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  405. }
  406. return status;
  407. }
  408. static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
  409. struct scic_sds_request *sci_req,
  410. enum sci_status status)
  411. {
  412. struct scic_sds_port *sci_port = sci_dev->owning_port;
  413. /* cleanup requests that failed after starting on the port */
  414. if (status != SCI_SUCCESS)
  415. scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  416. else
  417. scic_sds_remote_device_increment_request_count(sci_dev);
  418. }
  419. enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
  420. struct scic_sds_remote_device *sci_dev,
  421. struct scic_sds_request *sci_req)
  422. {
  423. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  424. enum scic_sds_remote_device_states state = sm->current_state_id;
  425. struct scic_sds_port *sci_port = sci_dev->owning_port;
  426. enum sci_status status;
  427. switch (state) {
  428. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  429. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  430. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  431. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  432. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  433. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  434. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  435. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  436. default:
  437. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  438. __func__, state);
  439. return SCI_FAILURE_INVALID_STATE;
  440. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  441. /* attempt to start an io request for this device object. The remote
  442. * device object will issue the start request for the io and if
  443. * successful it will start the request for the port object then
  444. * increment its own request count.
  445. */
  446. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  447. if (status != SCI_SUCCESS)
  448. return status;
  449. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  450. if (status != SCI_SUCCESS)
  451. break;
  452. status = scic_sds_request_start(sci_req);
  453. break;
  454. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
  455. /* handle the start io operation for a sata device that is in
  456. * the command idle state. - Evalute the type of IO request to
  457. * be started - If its an NCQ request change to NCQ substate -
  458. * If its any other command change to the CMD substate
  459. *
  460. * If this is a softreset we may want to have a different
  461. * substate.
  462. */
  463. enum scic_sds_remote_device_states new_state;
  464. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  465. if (status != SCI_SUCCESS)
  466. return status;
  467. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  468. if (status != SCI_SUCCESS)
  469. break;
  470. status = sci_req->state_handlers->start_handler(sci_req);
  471. if (status != SCI_SUCCESS)
  472. break;
  473. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
  474. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
  475. else {
  476. sci_dev->working_request = sci_req;
  477. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
  478. }
  479. sci_base_state_machine_change_state(sm, new_state);
  480. break;
  481. }
  482. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  483. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
  484. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  485. if (status != SCI_SUCCESS)
  486. return status;
  487. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  488. if (status != SCI_SUCCESS)
  489. break;
  490. status = sci_req->state_handlers->start_handler(sci_req);
  491. } else
  492. return SCI_FAILURE_INVALID_STATE;
  493. break;
  494. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  495. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  496. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  497. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  498. if (status != SCI_SUCCESS)
  499. return status;
  500. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  501. if (status != SCI_SUCCESS)
  502. break;
  503. status = scic_sds_request_start(sci_req);
  504. if (status != SCI_SUCCESS)
  505. break;
  506. sci_dev->working_request = sci_req;
  507. sci_base_state_machine_change_state(&sci_dev->state_machine,
  508. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  509. break;
  510. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  511. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  512. /* device is already handling a command it can not accept new commands
  513. * until this one is complete.
  514. */
  515. return SCI_FAILURE_INVALID_STATE;
  516. }
  517. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  518. return status;
  519. }
  520. static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
  521. struct scic_sds_remote_device *sci_dev,
  522. struct scic_sds_request *sci_req)
  523. {
  524. enum sci_status status;
  525. status = scic_sds_request_complete(sci_req);
  526. if (status != SCI_SUCCESS)
  527. return status;
  528. status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  529. if (status != SCI_SUCCESS)
  530. return status;
  531. scic_sds_remote_device_decrement_request_count(sci_dev);
  532. return status;
  533. }
  534. enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
  535. struct scic_sds_remote_device *sci_dev,
  536. struct scic_sds_request *sci_req)
  537. {
  538. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  539. enum scic_sds_remote_device_states state = sm->current_state_id;
  540. struct scic_sds_port *sci_port = sci_dev->owning_port;
  541. enum sci_status status;
  542. switch (state) {
  543. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  544. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  545. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  546. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  547. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  548. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  549. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  550. default:
  551. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  552. __func__, state);
  553. return SCI_FAILURE_INVALID_STATE;
  554. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  555. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  556. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  557. status = common_complete_io(sci_port, sci_dev, sci_req);
  558. break;
  559. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  560. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  561. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  562. status = common_complete_io(sci_port, sci_dev, sci_req);
  563. if (status != SCI_SUCCESS)
  564. break;
  565. if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  566. /* This request causes hardware error, device needs to be Lun Reset.
  567. * So here we force the state machine to IDLE state so the rest IOs
  568. * can reach RNC state handler, these IOs will be completed by RNC with
  569. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  570. */
  571. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  572. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  573. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  574. break;
  575. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  576. status = common_complete_io(sci_port, sci_dev, sci_req);
  577. if (status != SCI_SUCCESS)
  578. break;
  579. sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  580. break;
  581. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  582. status = common_complete_io(sci_port, sci_dev, sci_req);
  583. if (status != SCI_SUCCESS)
  584. break;
  585. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  586. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  587. rnc_destruct_done,
  588. sci_dev);
  589. break;
  590. }
  591. if (status != SCI_SUCCESS)
  592. dev_err(scirdev_to_dev(sci_dev),
  593. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  594. "could not complete\n", __func__, sci_port,
  595. sci_dev, sci_req, status);
  596. return status;
  597. }
  598. static void scic_sds_remote_device_continue_request(void *dev)
  599. {
  600. struct scic_sds_remote_device *sci_dev = dev;
  601. /* we need to check if this request is still valid to continue. */
  602. if (sci_dev->working_request)
  603. scic_controller_continue_io(sci_dev->working_request);
  604. }
  605. enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
  606. struct scic_sds_remote_device *sci_dev,
  607. struct scic_sds_request *sci_req)
  608. {
  609. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  610. enum scic_sds_remote_device_states state = sm->current_state_id;
  611. struct scic_sds_port *sci_port = sci_dev->owning_port;
  612. enum sci_status status;
  613. switch (state) {
  614. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  615. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  616. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  617. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  618. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  619. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  620. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  621. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  622. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  623. default:
  624. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  625. __func__, state);
  626. return SCI_FAILURE_INVALID_STATE;
  627. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  628. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  629. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  630. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  631. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  632. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  633. if (status != SCI_SUCCESS)
  634. return status;
  635. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  636. if (status != SCI_SUCCESS)
  637. goto out;
  638. status = sci_req->state_handlers->start_handler(sci_req);
  639. if (status != SCI_SUCCESS)
  640. goto out;
  641. /* Note: If the remote device state is not IDLE this will
  642. * replace the request that probably resulted in the task
  643. * management request.
  644. */
  645. sci_dev->working_request = sci_req;
  646. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  647. /* The remote node context must cleanup the TCi to NCQ mapping
  648. * table. The only way to do this correctly is to either write
  649. * to the TLCR register or to invalidate and repost the RNC. In
  650. * either case the remote node context state machine will take
  651. * the correct action when the remote node context is suspended
  652. * and later resumed.
  653. */
  654. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  655. SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  656. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  657. scic_sds_remote_device_continue_request,
  658. sci_dev);
  659. out:
  660. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  661. /* We need to let the controller start request handler know that
  662. * it can't post TC yet. We will provide a callback function to
  663. * post TC when RNC gets resumed.
  664. */
  665. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  666. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  667. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  668. if (status != SCI_SUCCESS)
  669. return status;
  670. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  671. if (status != SCI_SUCCESS)
  672. break;
  673. status = scic_sds_request_start(sci_req);
  674. break;
  675. }
  676. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  677. return status;
  678. }
  679. /**
  680. *
  681. * @sci_dev:
  682. * @request:
  683. *
  684. * This method takes the request and bulids an appropriate SCU context for the
  685. * request and then requests the controller to post the request. none
  686. */
  687. void scic_sds_remote_device_post_request(
  688. struct scic_sds_remote_device *sci_dev,
  689. u32 request)
  690. {
  691. u32 context;
  692. context = scic_sds_remote_device_build_command_context(sci_dev, request);
  693. scic_sds_controller_post_request(
  694. scic_sds_remote_device_get_controller(sci_dev),
  695. context
  696. );
  697. }
  698. /* called once the remote node context has transisitioned to a
  699. * ready state. This is the indication that the remote device object can also
  700. * transition to ready.
  701. */
  702. static void remote_device_resume_done(void *_dev)
  703. {
  704. struct scic_sds_remote_device *sci_dev = _dev;
  705. if (is_remote_device_ready(sci_dev))
  706. return;
  707. /* go 'ready' if we are not already in a ready state */
  708. sci_base_state_machine_change_state(&sci_dev->state_machine,
  709. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  710. }
  711. static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  712. {
  713. struct scic_sds_remote_device *sci_dev = _dev;
  714. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  715. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  716. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  717. * As a result, avoid sending the ready notification.
  718. */
  719. if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
  720. isci_remote_device_ready(scic->ihost, idev);
  721. }
  722. static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
  723. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  724. },
  725. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  726. },
  727. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  728. },
  729. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  730. },
  731. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  732. },
  733. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  734. },
  735. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  736. },
  737. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  738. },
  739. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  740. },
  741. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  742. },
  743. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  744. },
  745. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  746. },
  747. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  748. },
  749. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  750. },
  751. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  752. }
  753. };
  754. static void scic_sds_remote_device_initial_state_enter(void *object)
  755. {
  756. struct scic_sds_remote_device *sci_dev = object;
  757. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  758. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
  759. /* Initial state is a transitional state to the stopped state */
  760. sci_base_state_machine_change_state(&sci_dev->state_machine,
  761. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  762. }
  763. /**
  764. * scic_remote_device_destruct() - free remote node context and destruct
  765. * @remote_device: This parameter specifies the remote device to be destructed.
  766. *
  767. * Remote device objects are a limited resource. As such, they must be
  768. * protected. Thus calls to construct and destruct are mutually exclusive and
  769. * non-reentrant. The return value shall indicate if the device was
  770. * successfully destructed or if some failure occurred. enum sci_status This value
  771. * is returned if the device is successfully destructed.
  772. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  773. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  774. * valid, etc.).
  775. */
  776. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  777. {
  778. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  779. enum scic_sds_remote_device_states state = sm->current_state_id;
  780. struct scic_sds_controller *scic;
  781. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  782. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  783. __func__, state);
  784. return SCI_FAILURE_INVALID_STATE;
  785. }
  786. scic = sci_dev->owning_port->owning_controller;
  787. scic_sds_controller_free_remote_node_context(scic, sci_dev,
  788. sci_dev->rnc.remote_node_index);
  789. sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  790. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
  791. return SCI_SUCCESS;
  792. }
  793. /**
  794. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  795. * @ihost: This parameter specifies the isci host object.
  796. * @idev: This parameter specifies the remote device to be freed.
  797. *
  798. */
  799. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  800. {
  801. dev_dbg(&ihost->pdev->dev,
  802. "%s: isci_device = %p\n", __func__, idev);
  803. /* There should not be any outstanding io's. All paths to
  804. * here should go through isci_remote_device_nuke_requests.
  805. * If we hit this condition, we will need a way to complete
  806. * io requests in process */
  807. while (!list_empty(&idev->reqs_in_process)) {
  808. dev_err(&ihost->pdev->dev,
  809. "%s: ** request list not empty! **\n", __func__);
  810. BUG();
  811. }
  812. scic_remote_device_destruct(&idev->sci);
  813. idev->domain_dev->lldd_dev = NULL;
  814. idev->domain_dev = NULL;
  815. idev->isci_port = NULL;
  816. list_del_init(&idev->node);
  817. clear_bit(IDEV_START_PENDING, &idev->flags);
  818. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  819. wake_up(&ihost->eventq);
  820. }
  821. /**
  822. * isci_remote_device_stop_complete() - This function is called by the scic
  823. * when the remote device stop has completed. We mark the isci device as not
  824. * ready and remove the isci remote device.
  825. * @ihost: This parameter specifies the isci host object.
  826. * @idev: This parameter specifies the remote device.
  827. * @status: This parameter specifies status of the completion.
  828. *
  829. */
  830. static void isci_remote_device_stop_complete(struct isci_host *ihost,
  831. struct isci_remote_device *idev)
  832. {
  833. dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
  834. isci_remote_device_change_state(idev, isci_stopped);
  835. /* after stop, we can tear down resources. */
  836. isci_remote_device_deconstruct(ihost, idev);
  837. }
  838. static void scic_sds_remote_device_stopped_state_enter(void *object)
  839. {
  840. struct scic_sds_remote_device *sci_dev = object;
  841. struct scic_sds_controller *scic;
  842. struct isci_remote_device *idev;
  843. struct isci_host *ihost;
  844. u32 prev_state;
  845. scic = scic_sds_remote_device_get_controller(sci_dev);
  846. ihost = scic->ihost;
  847. idev = sci_dev_to_idev(sci_dev);
  848. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  849. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  850. /* If we are entering from the stopping state let the SCI User know that
  851. * the stop operation has completed.
  852. */
  853. prev_state = sci_dev->state_machine.previous_state_id;
  854. if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
  855. isci_remote_device_stop_complete(ihost, idev);
  856. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  857. }
  858. static void scic_sds_remote_device_starting_state_enter(void *object)
  859. {
  860. struct scic_sds_remote_device *sci_dev = object;
  861. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  862. struct isci_host *ihost = scic->ihost;
  863. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  864. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  865. SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  866. isci_remote_device_not_ready(ihost, idev,
  867. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  868. }
  869. static void scic_sds_remote_device_ready_state_enter(void *object)
  870. {
  871. struct scic_sds_remote_device *sci_dev = object;
  872. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  873. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  874. SET_STATE_HANDLER(sci_dev,
  875. scic_sds_remote_device_state_handler_table,
  876. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  877. scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
  878. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  879. sci_base_state_machine_change_state(&sci_dev->state_machine,
  880. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  881. } else if (dev_is_expander(dev)) {
  882. sci_base_state_machine_change_state(&sci_dev->state_machine,
  883. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  884. } else
  885. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  886. }
  887. static void scic_sds_remote_device_ready_state_exit(void *object)
  888. {
  889. struct scic_sds_remote_device *sci_dev = object;
  890. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  891. if (dev->dev_type == SAS_END_DEV) {
  892. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  893. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  894. isci_remote_device_not_ready(scic->ihost, idev,
  895. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  896. }
  897. }
  898. static void scic_sds_remote_device_stopping_state_enter(void *object)
  899. {
  900. struct scic_sds_remote_device *sci_dev = object;
  901. SET_STATE_HANDLER(
  902. sci_dev,
  903. scic_sds_remote_device_state_handler_table,
  904. SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
  905. );
  906. }
  907. static void scic_sds_remote_device_failed_state_enter(void *object)
  908. {
  909. struct scic_sds_remote_device *sci_dev = object;
  910. SET_STATE_HANDLER(
  911. sci_dev,
  912. scic_sds_remote_device_state_handler_table,
  913. SCI_BASE_REMOTE_DEVICE_STATE_FAILED
  914. );
  915. }
  916. static void scic_sds_remote_device_resetting_state_enter(void *object)
  917. {
  918. struct scic_sds_remote_device *sci_dev = object;
  919. SET_STATE_HANDLER(
  920. sci_dev,
  921. scic_sds_remote_device_state_handler_table,
  922. SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
  923. );
  924. scic_sds_remote_node_context_suspend(
  925. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  926. }
  927. static void scic_sds_remote_device_resetting_state_exit(void *object)
  928. {
  929. struct scic_sds_remote_device *sci_dev = object;
  930. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  931. }
  932. static void scic_sds_remote_device_final_state_enter(void *object)
  933. {
  934. struct scic_sds_remote_device *sci_dev = object;
  935. SET_STATE_HANDLER(
  936. sci_dev,
  937. scic_sds_remote_device_state_handler_table,
  938. SCI_BASE_REMOTE_DEVICE_STATE_FINAL
  939. );
  940. }
  941. static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
  942. {
  943. struct scic_sds_remote_device *sci_dev = object;
  944. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  945. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  946. sci_dev->working_request = NULL;
  947. if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
  948. /*
  949. * Since the RNC is ready, it's alright to finish completion
  950. * processing (e.g. signal the remote device is ready). */
  951. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
  952. } else {
  953. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  954. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
  955. sci_dev);
  956. }
  957. }
  958. static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
  959. {
  960. struct scic_sds_remote_device *sci_dev = object;
  961. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  962. BUG_ON(sci_dev->working_request == NULL);
  963. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  964. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  965. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  966. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  967. }
  968. static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
  969. {
  970. struct scic_sds_remote_device *sci_dev = object;
  971. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  972. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
  973. }
  974. static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
  975. {
  976. struct scic_sds_remote_device *sci_dev = object;
  977. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  978. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  979. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  980. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  981. if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  982. isci_remote_device_not_ready(scic->ihost, idev,
  983. sci_dev->not_ready_reason);
  984. }
  985. static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
  986. {
  987. struct scic_sds_remote_device *sci_dev = object;
  988. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  989. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  990. }
  991. static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
  992. {
  993. struct scic_sds_remote_device *sci_dev = object;
  994. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  995. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  996. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  997. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  998. }
  999. static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
  1000. {
  1001. struct scic_sds_remote_device *sci_dev = object;
  1002. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1003. BUG_ON(sci_dev->working_request == NULL);
  1004. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1005. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1006. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1007. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  1008. }
  1009. static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
  1010. {
  1011. struct scic_sds_remote_device *sci_dev = object;
  1012. sci_dev->working_request = NULL;
  1013. }
  1014. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  1015. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1016. .enter_state = scic_sds_remote_device_initial_state_enter,
  1017. },
  1018. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1019. .enter_state = scic_sds_remote_device_stopped_state_enter,
  1020. },
  1021. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1022. .enter_state = scic_sds_remote_device_starting_state_enter,
  1023. },
  1024. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1025. .enter_state = scic_sds_remote_device_ready_state_enter,
  1026. .exit_state = scic_sds_remote_device_ready_state_exit
  1027. },
  1028. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1029. .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
  1030. },
  1031. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1032. .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
  1033. },
  1034. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1035. .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
  1036. },
  1037. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1038. .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
  1039. },
  1040. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1041. .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
  1042. },
  1043. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1044. .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
  1045. },
  1046. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1047. .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
  1048. .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
  1049. },
  1050. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1051. .enter_state = scic_sds_remote_device_stopping_state_enter,
  1052. },
  1053. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1054. .enter_state = scic_sds_remote_device_failed_state_enter,
  1055. },
  1056. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1057. .enter_state = scic_sds_remote_device_resetting_state_enter,
  1058. .exit_state = scic_sds_remote_device_resetting_state_exit
  1059. },
  1060. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1061. .enter_state = scic_sds_remote_device_final_state_enter,
  1062. },
  1063. };
  1064. /**
  1065. * scic_remote_device_construct() - common construction
  1066. * @sci_port: SAS/SATA port through which this device is accessed.
  1067. * @sci_dev: remote device to construct
  1068. *
  1069. * This routine just performs benign initialization and does not
  1070. * allocate the remote_node_context which is left to
  1071. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  1072. * frees the remote_node_context(s) for the device.
  1073. */
  1074. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  1075. struct scic_sds_remote_device *sci_dev)
  1076. {
  1077. sci_dev->owning_port = sci_port;
  1078. sci_dev->started_request_count = 0;
  1079. sci_base_state_machine_construct(
  1080. &sci_dev->state_machine,
  1081. sci_dev,
  1082. scic_sds_remote_device_state_table,
  1083. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
  1084. );
  1085. sci_base_state_machine_start(
  1086. &sci_dev->state_machine
  1087. );
  1088. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  1089. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1090. }
  1091. /**
  1092. * scic_remote_device_da_construct() - construct direct attached device.
  1093. *
  1094. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1095. * the device is known to the SCI Core since it is contained in the
  1096. * scic_phy object. Remote node context(s) is/are a global resource
  1097. * allocated by this routine, freed by scic_remote_device_destruct().
  1098. *
  1099. * Returns:
  1100. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1101. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1102. * sata-only controller instance.
  1103. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1104. */
  1105. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  1106. struct scic_sds_remote_device *sci_dev)
  1107. {
  1108. enum sci_status status;
  1109. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1110. scic_remote_device_construct(sci_port, sci_dev);
  1111. /*
  1112. * This information is request to determine how many remote node context
  1113. * entries will be needed to store the remote node.
  1114. */
  1115. sci_dev->is_direct_attached = true;
  1116. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1117. sci_dev,
  1118. &sci_dev->rnc.remote_node_index);
  1119. if (status != SCI_SUCCESS)
  1120. return status;
  1121. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1122. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1123. /* pass */;
  1124. else
  1125. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1126. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  1127. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1128. sci_dev->device_port_width = 1;
  1129. return SCI_SUCCESS;
  1130. }
  1131. /**
  1132. * scic_remote_device_ea_construct() - construct expander attached device
  1133. *
  1134. * Remote node context(s) is/are a global resource allocated by this
  1135. * routine, freed by scic_remote_device_destruct().
  1136. *
  1137. * Returns:
  1138. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1139. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1140. * sata-only controller instance.
  1141. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1142. */
  1143. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  1144. struct scic_sds_remote_device *sci_dev)
  1145. {
  1146. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1147. enum sci_status status;
  1148. scic_remote_device_construct(sci_port, sci_dev);
  1149. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1150. sci_dev,
  1151. &sci_dev->rnc.remote_node_index);
  1152. if (status != SCI_SUCCESS)
  1153. return status;
  1154. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1155. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1156. /* pass */;
  1157. else
  1158. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1159. /*
  1160. * For SAS-2 the physical link rate is actually a logical link
  1161. * rate that incorporates multiplexing. The SCU doesn't
  1162. * incorporate multiplexing and for the purposes of the
  1163. * connection the logical link rate is that same as the
  1164. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1165. * one another, so this code works for both situations. */
  1166. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1167. dev->linkrate);
  1168. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1169. sci_dev->device_port_width = 1;
  1170. return SCI_SUCCESS;
  1171. }
  1172. /**
  1173. * scic_remote_device_start() - This method will start the supplied remote
  1174. * device. This method enables normal IO requests to flow through to the
  1175. * remote device.
  1176. * @remote_device: This parameter specifies the device to be started.
  1177. * @timeout: This parameter specifies the number of milliseconds in which the
  1178. * start operation should complete.
  1179. *
  1180. * An indication of whether the device was successfully started. SCI_SUCCESS
  1181. * This value is returned if the device was successfully started.
  1182. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1183. * the device when there have been no phys added to it.
  1184. */
  1185. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1186. u32 timeout)
  1187. {
  1188. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1189. enum scic_sds_remote_device_states state = sm->current_state_id;
  1190. enum sci_status status;
  1191. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1192. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1193. __func__, state);
  1194. return SCI_FAILURE_INVALID_STATE;
  1195. }
  1196. status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1197. remote_device_resume_done,
  1198. sci_dev);
  1199. if (status != SCI_SUCCESS)
  1200. return status;
  1201. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1202. return SCI_SUCCESS;
  1203. }
  1204. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1205. struct isci_remote_device *idev)
  1206. {
  1207. struct scic_sds_port *sci_port = iport->sci_port_handle;
  1208. struct isci_host *ihost = iport->isci_host;
  1209. struct domain_device *dev = idev->domain_dev;
  1210. enum sci_status status;
  1211. if (dev->parent && dev_is_expander(dev->parent))
  1212. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1213. else
  1214. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1215. if (status != SCI_SUCCESS) {
  1216. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1217. __func__, status);
  1218. return status;
  1219. }
  1220. /* start the device. */
  1221. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1222. if (status != SCI_SUCCESS)
  1223. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1224. status);
  1225. return status;
  1226. }
  1227. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1228. {
  1229. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1230. dev_dbg(&ihost->pdev->dev,
  1231. "%s: idev = %p\n", __func__, idev);
  1232. /* Cleanup all requests pending for this device. */
  1233. isci_terminate_pending_requests(ihost, idev, terminating);
  1234. dev_dbg(&ihost->pdev->dev,
  1235. "%s: idev = %p, done\n", __func__, idev);
  1236. }
  1237. /**
  1238. * This function builds the isci_remote_device when a libsas dev_found message
  1239. * is received.
  1240. * @isci_host: This parameter specifies the isci host object.
  1241. * @port: This parameter specifies the isci_port conected to this device.
  1242. *
  1243. * pointer to new isci_remote_device.
  1244. */
  1245. static struct isci_remote_device *
  1246. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1247. {
  1248. struct isci_remote_device *idev;
  1249. int i;
  1250. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1251. idev = &ihost->devices[i];
  1252. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1253. break;
  1254. }
  1255. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1256. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1257. return NULL;
  1258. }
  1259. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1260. return NULL;
  1261. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1262. return NULL;
  1263. isci_remote_device_change_state(idev, isci_freed);
  1264. return idev;
  1265. }
  1266. /**
  1267. * isci_remote_device_stop() - This function is called internally to stop the
  1268. * remote device.
  1269. * @isci_host: This parameter specifies the isci host object.
  1270. * @isci_device: This parameter specifies the remote device.
  1271. *
  1272. * The status of the scic request to stop.
  1273. */
  1274. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1275. {
  1276. enum sci_status status;
  1277. unsigned long flags;
  1278. dev_dbg(&ihost->pdev->dev,
  1279. "%s: isci_device = %p\n", __func__, idev);
  1280. isci_remote_device_change_state(idev, isci_stopping);
  1281. /* Kill all outstanding requests. */
  1282. isci_remote_device_nuke_requests(ihost, idev);
  1283. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1284. spin_lock_irqsave(&ihost->scic_lock, flags);
  1285. status = scic_remote_device_stop(&idev->sci, 50);
  1286. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1287. /* Wait for the stop complete callback. */
  1288. if (status == SCI_SUCCESS) {
  1289. wait_for_device_stop(ihost, idev);
  1290. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1291. }
  1292. dev_dbg(&ihost->pdev->dev,
  1293. "%s: idev = %p - after completion wait\n",
  1294. __func__, idev);
  1295. return status;
  1296. }
  1297. /**
  1298. * isci_remote_device_gone() - This function is called by libsas when a domain
  1299. * device is removed.
  1300. * @domain_device: This parameter specifies the libsas domain device.
  1301. *
  1302. */
  1303. void isci_remote_device_gone(struct domain_device *dev)
  1304. {
  1305. struct isci_host *ihost = dev_to_ihost(dev);
  1306. struct isci_remote_device *idev = dev->lldd_dev;
  1307. dev_dbg(&ihost->pdev->dev,
  1308. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1309. __func__, dev, idev, idev->isci_port);
  1310. isci_remote_device_stop(ihost, idev);
  1311. }
  1312. /**
  1313. * isci_remote_device_found() - This function is called by libsas when a remote
  1314. * device is discovered. A remote device object is created and started. the
  1315. * function then sleeps until the sci core device started message is
  1316. * received.
  1317. * @domain_device: This parameter specifies the libsas domain device.
  1318. *
  1319. * status, zero indicates success.
  1320. */
  1321. int isci_remote_device_found(struct domain_device *domain_dev)
  1322. {
  1323. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1324. struct isci_port *isci_port;
  1325. struct isci_phy *isci_phy;
  1326. struct asd_sas_port *sas_port;
  1327. struct asd_sas_phy *sas_phy;
  1328. struct isci_remote_device *isci_device;
  1329. enum sci_status status;
  1330. dev_dbg(&isci_host->pdev->dev,
  1331. "%s: domain_device = %p\n", __func__, domain_dev);
  1332. wait_for_start(isci_host);
  1333. sas_port = domain_dev->port;
  1334. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1335. port_phy_el);
  1336. isci_phy = to_isci_phy(sas_phy);
  1337. isci_port = isci_phy->isci_port;
  1338. /* we are being called for a device on this port,
  1339. * so it has to come up eventually
  1340. */
  1341. wait_for_completion(&isci_port->start_complete);
  1342. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1343. (isci_stopped == isci_port_get_state(isci_port)))
  1344. return -ENODEV;
  1345. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1346. if (!isci_device)
  1347. return -ENODEV;
  1348. INIT_LIST_HEAD(&isci_device->node);
  1349. domain_dev->lldd_dev = isci_device;
  1350. isci_device->domain_dev = domain_dev;
  1351. isci_device->isci_port = isci_port;
  1352. isci_remote_device_change_state(isci_device, isci_starting);
  1353. spin_lock_irq(&isci_host->scic_lock);
  1354. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1355. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1356. status = isci_remote_device_construct(isci_port, isci_device);
  1357. spin_unlock_irq(&isci_host->scic_lock);
  1358. dev_dbg(&isci_host->pdev->dev,
  1359. "%s: isci_device = %p\n",
  1360. __func__, isci_device);
  1361. if (status != SCI_SUCCESS) {
  1362. spin_lock_irq(&isci_host->scic_lock);
  1363. isci_remote_device_deconstruct(
  1364. isci_host,
  1365. isci_device
  1366. );
  1367. spin_unlock_irq(&isci_host->scic_lock);
  1368. return -ENODEV;
  1369. }
  1370. /* wait for the device ready callback. */
  1371. wait_for_device_start(isci_host, isci_device);
  1372. return 0;
  1373. }
  1374. /**
  1375. * isci_device_is_reset_pending() - This function will check if there is any
  1376. * pending reset condition on the device.
  1377. * @request: This parameter is the isci_device object.
  1378. *
  1379. * true if there is a reset pending for the device.
  1380. */
  1381. bool isci_device_is_reset_pending(
  1382. struct isci_host *isci_host,
  1383. struct isci_remote_device *isci_device)
  1384. {
  1385. struct isci_request *isci_request;
  1386. struct isci_request *tmp_req;
  1387. bool reset_is_pending = false;
  1388. unsigned long flags;
  1389. dev_dbg(&isci_host->pdev->dev,
  1390. "%s: isci_device = %p\n", __func__, isci_device);
  1391. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1392. /* Check for reset on all pending requests. */
  1393. list_for_each_entry_safe(isci_request, tmp_req,
  1394. &isci_device->reqs_in_process, dev_node) {
  1395. dev_dbg(&isci_host->pdev->dev,
  1396. "%s: isci_device = %p request = %p\n",
  1397. __func__, isci_device, isci_request);
  1398. if (isci_request->ttype == io_task) {
  1399. struct sas_task *task = isci_request_access_task(
  1400. isci_request);
  1401. spin_lock(&task->task_state_lock);
  1402. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1403. reset_is_pending = true;
  1404. spin_unlock(&task->task_state_lock);
  1405. }
  1406. }
  1407. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1408. dev_dbg(&isci_host->pdev->dev,
  1409. "%s: isci_device = %p reset_is_pending = %d\n",
  1410. __func__, isci_device, reset_is_pending);
  1411. return reset_is_pending;
  1412. }
  1413. /**
  1414. * isci_device_clear_reset_pending() - This function will clear if any pending
  1415. * reset condition flags on the device.
  1416. * @request: This parameter is the isci_device object.
  1417. *
  1418. * true if there is a reset pending for the device.
  1419. */
  1420. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1421. {
  1422. struct isci_request *isci_request;
  1423. struct isci_request *tmp_req;
  1424. unsigned long flags = 0;
  1425. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1426. __func__, idev, ihost);
  1427. spin_lock_irqsave(&ihost->scic_lock, flags);
  1428. /* Clear reset pending on all pending requests. */
  1429. list_for_each_entry_safe(isci_request, tmp_req,
  1430. &idev->reqs_in_process, dev_node) {
  1431. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1432. __func__, idev, isci_request);
  1433. if (isci_request->ttype == io_task) {
  1434. unsigned long flags2;
  1435. struct sas_task *task = isci_request_access_task(
  1436. isci_request);
  1437. spin_lock_irqsave(&task->task_state_lock, flags2);
  1438. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1439. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1440. }
  1441. }
  1442. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1443. }