remote_device.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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 <scsi/sas.h>
  56. #include "isci.h"
  57. #include "port.h"
  58. #include "remote_device.h"
  59. #include "request.h"
  60. #include "remote_node_context.h"
  61. #include "scu_event_codes.h"
  62. #include "task.h"
  63. /**
  64. * isci_remote_device_not_ready() - This function is called by the scic when
  65. * the remote device is not ready. We mark the isci device as ready (not
  66. * "ready_for_io") and signal the waiting proccess.
  67. * @isci_host: This parameter specifies the isci host object.
  68. * @isci_device: This parameter specifies the remote device
  69. *
  70. * scic_lock is held on entrance to this function.
  71. */
  72. static void isci_remote_device_not_ready(struct isci_host *ihost,
  73. struct isci_remote_device *idev, u32 reason)
  74. {
  75. struct isci_request * ireq;
  76. dev_dbg(&ihost->pdev->dev,
  77. "%s: isci_device = %p\n", __func__, idev);
  78. switch (reason) {
  79. case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
  80. set_bit(IDEV_GONE, &idev->flags);
  81. break;
  82. case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
  83. set_bit(IDEV_IO_NCQERROR, &idev->flags);
  84. /* Kill all outstanding requests for the device. */
  85. list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
  86. dev_dbg(&ihost->pdev->dev,
  87. "%s: isci_device = %p request = %p\n",
  88. __func__, idev, ireq);
  89. scic_controller_terminate_request(&ihost->sci,
  90. &idev->sci,
  91. ireq);
  92. }
  93. /* Fall through into the default case... */
  94. default:
  95. clear_bit(IDEV_IO_READY, &idev->flags);
  96. break;
  97. }
  98. }
  99. /**
  100. * isci_remote_device_ready() - This function is called by the scic when the
  101. * remote device is ready. We mark the isci device as ready and signal the
  102. * waiting proccess.
  103. * @ihost: our valid isci_host
  104. * @idev: remote device
  105. *
  106. */
  107. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  108. {
  109. dev_dbg(&ihost->pdev->dev,
  110. "%s: idev = %p\n", __func__, idev);
  111. clear_bit(IDEV_IO_NCQERROR, &idev->flags);
  112. set_bit(IDEV_IO_READY, &idev->flags);
  113. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  114. wake_up(&ihost->eventq);
  115. }
  116. /* called once the remote node context is ready to be freed.
  117. * The remote device can now report that its stop operation is complete. none
  118. */
  119. static void rnc_destruct_done(void *_dev)
  120. {
  121. struct scic_sds_remote_device *sci_dev = _dev;
  122. BUG_ON(sci_dev->started_request_count != 0);
  123. sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
  124. }
  125. static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
  126. {
  127. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  128. struct isci_host *ihost = scic_to_ihost(scic);
  129. u32 i, request_count = sci_dev->started_request_count;
  130. enum sci_status status = SCI_SUCCESS;
  131. for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
  132. struct isci_request *ireq = ihost->reqs[i];
  133. enum sci_status s;
  134. if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
  135. ireq->target_device != sci_dev)
  136. continue;
  137. s = scic_controller_terminate_request(scic, sci_dev, ireq);
  138. if (s != SCI_SUCCESS)
  139. status = s;
  140. }
  141. return status;
  142. }
  143. enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
  144. u32 timeout)
  145. {
  146. struct sci_base_state_machine *sm = &sci_dev->sm;
  147. enum scic_sds_remote_device_states state = sm->current_state_id;
  148. switch (state) {
  149. case SCI_DEV_INITIAL:
  150. case SCI_DEV_FAILED:
  151. case SCI_DEV_FINAL:
  152. default:
  153. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  154. __func__, state);
  155. return SCI_FAILURE_INVALID_STATE;
  156. case SCI_DEV_STOPPED:
  157. return SCI_SUCCESS;
  158. case SCI_DEV_STARTING:
  159. /* device not started so there had better be no requests */
  160. BUG_ON(sci_dev->started_request_count != 0);
  161. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  162. rnc_destruct_done, sci_dev);
  163. /* Transition to the stopping state and wait for the
  164. * remote node to complete being posted and invalidated.
  165. */
  166. sci_change_state(sm, SCI_DEV_STOPPING);
  167. return SCI_SUCCESS;
  168. case SCI_DEV_READY:
  169. case SCI_STP_DEV_IDLE:
  170. case SCI_STP_DEV_CMD:
  171. case SCI_STP_DEV_NCQ:
  172. case SCI_STP_DEV_NCQ_ERROR:
  173. case SCI_STP_DEV_AWAIT_RESET:
  174. case SCI_SMP_DEV_IDLE:
  175. case SCI_SMP_DEV_CMD:
  176. sci_change_state(sm, SCI_DEV_STOPPING);
  177. if (sci_dev->started_request_count == 0) {
  178. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  179. rnc_destruct_done, sci_dev);
  180. return SCI_SUCCESS;
  181. } else
  182. return scic_sds_remote_device_terminate_requests(sci_dev);
  183. break;
  184. case SCI_DEV_STOPPING:
  185. /* All requests should have been terminated, but if there is an
  186. * attempt to stop a device already in the stopping state, then
  187. * try again to terminate.
  188. */
  189. return scic_sds_remote_device_terminate_requests(sci_dev);
  190. case SCI_DEV_RESETTING:
  191. sci_change_state(sm, SCI_DEV_STOPPING);
  192. return SCI_SUCCESS;
  193. }
  194. }
  195. enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
  196. {
  197. struct sci_base_state_machine *sm = &sci_dev->sm;
  198. enum scic_sds_remote_device_states state = sm->current_state_id;
  199. switch (state) {
  200. case SCI_DEV_INITIAL:
  201. case SCI_DEV_STOPPED:
  202. case SCI_DEV_STARTING:
  203. case SCI_SMP_DEV_IDLE:
  204. case SCI_SMP_DEV_CMD:
  205. case SCI_DEV_STOPPING:
  206. case SCI_DEV_FAILED:
  207. case SCI_DEV_RESETTING:
  208. case SCI_DEV_FINAL:
  209. default:
  210. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  211. __func__, state);
  212. return SCI_FAILURE_INVALID_STATE;
  213. case SCI_DEV_READY:
  214. case SCI_STP_DEV_IDLE:
  215. case SCI_STP_DEV_CMD:
  216. case SCI_STP_DEV_NCQ:
  217. case SCI_STP_DEV_NCQ_ERROR:
  218. case SCI_STP_DEV_AWAIT_RESET:
  219. sci_change_state(sm, SCI_DEV_RESETTING);
  220. return SCI_SUCCESS;
  221. }
  222. }
  223. enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
  224. {
  225. struct sci_base_state_machine *sm = &sci_dev->sm;
  226. enum scic_sds_remote_device_states state = sm->current_state_id;
  227. if (state != SCI_DEV_RESETTING) {
  228. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  229. __func__, state);
  230. return SCI_FAILURE_INVALID_STATE;
  231. }
  232. sci_change_state(sm, SCI_DEV_READY);
  233. return SCI_SUCCESS;
  234. }
  235. enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
  236. u32 suspend_type)
  237. {
  238. struct sci_base_state_machine *sm = &sci_dev->sm;
  239. enum scic_sds_remote_device_states state = sm->current_state_id;
  240. if (state != SCI_STP_DEV_CMD) {
  241. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  242. __func__, state);
  243. return SCI_FAILURE_INVALID_STATE;
  244. }
  245. return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  246. suspend_type, NULL, NULL);
  247. }
  248. enum sci_status scic_sds_remote_device_frame_handler(struct scic_sds_remote_device *sci_dev,
  249. u32 frame_index)
  250. {
  251. struct sci_base_state_machine *sm = &sci_dev->sm;
  252. enum scic_sds_remote_device_states state = sm->current_state_id;
  253. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  254. enum sci_status status;
  255. switch (state) {
  256. case SCI_DEV_INITIAL:
  257. case SCI_DEV_STOPPED:
  258. case SCI_DEV_STARTING:
  259. case SCI_STP_DEV_IDLE:
  260. case SCI_SMP_DEV_IDLE:
  261. case SCI_DEV_FINAL:
  262. default:
  263. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  264. __func__, state);
  265. /* Return the frame back to the controller */
  266. scic_sds_controller_release_frame(scic, frame_index);
  267. return SCI_FAILURE_INVALID_STATE;
  268. case SCI_DEV_READY:
  269. case SCI_STP_DEV_NCQ_ERROR:
  270. case SCI_STP_DEV_AWAIT_RESET:
  271. case SCI_DEV_STOPPING:
  272. case SCI_DEV_FAILED:
  273. case SCI_DEV_RESETTING: {
  274. struct isci_request *ireq;
  275. struct ssp_frame_hdr hdr;
  276. void *frame_header;
  277. ssize_t word_cnt;
  278. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  279. frame_index,
  280. &frame_header);
  281. if (status != SCI_SUCCESS)
  282. return status;
  283. word_cnt = sizeof(hdr) / sizeof(u32);
  284. sci_swab32_cpy(&hdr, frame_header, word_cnt);
  285. ireq = scic_request_by_tag(scic, be16_to_cpu(hdr.tag));
  286. if (ireq && ireq->target_device == sci_dev) {
  287. /* The IO request is now in charge of releasing the frame */
  288. status = scic_sds_io_request_frame_handler(ireq, frame_index);
  289. } else {
  290. /* We could not map this tag to a valid IO
  291. * request Just toss the frame and continue
  292. */
  293. scic_sds_controller_release_frame(scic, frame_index);
  294. }
  295. break;
  296. }
  297. case SCI_STP_DEV_NCQ: {
  298. struct dev_to_host_fis *hdr;
  299. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  300. frame_index,
  301. (void **)&hdr);
  302. if (status != SCI_SUCCESS)
  303. return status;
  304. if (hdr->fis_type == FIS_SETDEVBITS &&
  305. (hdr->status & ATA_ERR)) {
  306. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  307. /* TODO Check sactive and complete associated IO if any. */
  308. sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
  309. } else if (hdr->fis_type == FIS_REGD2H &&
  310. (hdr->status & ATA_ERR)) {
  311. /*
  312. * Some devices return D2H FIS when an NCQ error is detected.
  313. * Treat this like an SDB error FIS ready reason.
  314. */
  315. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  316. sci_change_state(&sci_dev->sm, SCI_STP_DEV_NCQ_ERROR);
  317. } else
  318. status = SCI_FAILURE;
  319. scic_sds_controller_release_frame(scic, frame_index);
  320. break;
  321. }
  322. case SCI_STP_DEV_CMD:
  323. case SCI_SMP_DEV_CMD:
  324. /* The device does not process any UF received from the hardware while
  325. * in this state. All unsolicited frames are forwarded to the io request
  326. * object.
  327. */
  328. status = scic_sds_io_request_frame_handler(sci_dev->working_request, frame_index);
  329. break;
  330. }
  331. return status;
  332. }
  333. static bool is_remote_device_ready(struct scic_sds_remote_device *sci_dev)
  334. {
  335. struct sci_base_state_machine *sm = &sci_dev->sm;
  336. enum scic_sds_remote_device_states state = sm->current_state_id;
  337. switch (state) {
  338. case SCI_DEV_READY:
  339. case SCI_STP_DEV_IDLE:
  340. case SCI_STP_DEV_CMD:
  341. case SCI_STP_DEV_NCQ:
  342. case SCI_STP_DEV_NCQ_ERROR:
  343. case SCI_STP_DEV_AWAIT_RESET:
  344. case SCI_SMP_DEV_IDLE:
  345. case SCI_SMP_DEV_CMD:
  346. return true;
  347. default:
  348. return false;
  349. }
  350. }
  351. enum sci_status scic_sds_remote_device_event_handler(struct scic_sds_remote_device *sci_dev,
  352. u32 event_code)
  353. {
  354. struct sci_base_state_machine *sm = &sci_dev->sm;
  355. enum scic_sds_remote_device_states state = sm->current_state_id;
  356. enum sci_status status;
  357. switch (scu_get_event_type(event_code)) {
  358. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  359. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  360. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  361. status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
  362. break;
  363. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  364. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  365. status = SCI_SUCCESS;
  366. /* Suspend the associated RNC */
  367. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  368. SCI_SOFTWARE_SUSPENSION,
  369. NULL, NULL);
  370. dev_dbg(scirdev_to_dev(sci_dev),
  371. "%s: device: %p event code: %x: %s\n",
  372. __func__, sci_dev, event_code,
  373. is_remote_device_ready(sci_dev)
  374. ? "I_T_Nexus_Timeout event"
  375. : "I_T_Nexus_Timeout event in wrong state");
  376. break;
  377. }
  378. /* Else, fall through and treat as unhandled... */
  379. default:
  380. dev_dbg(scirdev_to_dev(sci_dev),
  381. "%s: device: %p event code: %x: %s\n",
  382. __func__, sci_dev, event_code,
  383. is_remote_device_ready(sci_dev)
  384. ? "unexpected event"
  385. : "unexpected event in wrong state");
  386. status = SCI_FAILURE_INVALID_STATE;
  387. break;
  388. }
  389. if (status != SCI_SUCCESS)
  390. return status;
  391. if (state == SCI_STP_DEV_IDLE) {
  392. /* We pick up suspension events to handle specifically to this
  393. * state. We resume the RNC right away.
  394. */
  395. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  396. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  397. status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  398. }
  399. return status;
  400. }
  401. static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
  402. struct isci_request *ireq,
  403. enum sci_status status)
  404. {
  405. struct scic_sds_port *sci_port = sci_dev->owning_port;
  406. /* cleanup requests that failed after starting on the port */
  407. if (status != SCI_SUCCESS)
  408. scic_sds_port_complete_io(sci_port, sci_dev, ireq);
  409. else {
  410. kref_get(&sci_dev_to_idev(sci_dev)->kref);
  411. scic_sds_remote_device_increment_request_count(sci_dev);
  412. }
  413. }
  414. enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
  415. struct scic_sds_remote_device *sci_dev,
  416. struct isci_request *ireq)
  417. {
  418. struct sci_base_state_machine *sm = &sci_dev->sm;
  419. enum scic_sds_remote_device_states state = sm->current_state_id;
  420. struct scic_sds_port *sci_port = sci_dev->owning_port;
  421. enum sci_status status;
  422. switch (state) {
  423. case SCI_DEV_INITIAL:
  424. case SCI_DEV_STOPPED:
  425. case SCI_DEV_STARTING:
  426. case SCI_STP_DEV_NCQ_ERROR:
  427. case SCI_DEV_STOPPING:
  428. case SCI_DEV_FAILED:
  429. case SCI_DEV_RESETTING:
  430. case SCI_DEV_FINAL:
  431. default:
  432. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  433. __func__, state);
  434. return SCI_FAILURE_INVALID_STATE;
  435. case SCI_DEV_READY:
  436. /* attempt to start an io request for this device object. The remote
  437. * device object will issue the start request for the io and if
  438. * successful it will start the request for the port object then
  439. * increment its own request count.
  440. */
  441. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  442. if (status != SCI_SUCCESS)
  443. return status;
  444. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, ireq);
  445. if (status != SCI_SUCCESS)
  446. break;
  447. status = scic_sds_request_start(ireq);
  448. break;
  449. case SCI_STP_DEV_IDLE: {
  450. /* handle the start io operation for a sata device that is in
  451. * the command idle state. - Evalute the type of IO request to
  452. * be started - If its an NCQ request change to NCQ substate -
  453. * If its any other command change to the CMD substate
  454. *
  455. * If this is a softreset we may want to have a different
  456. * substate.
  457. */
  458. enum scic_sds_remote_device_states new_state;
  459. struct sas_task *task = isci_request_access_task(ireq);
  460. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  461. if (status != SCI_SUCCESS)
  462. return status;
  463. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, ireq);
  464. if (status != SCI_SUCCESS)
  465. break;
  466. status = scic_sds_request_start(ireq);
  467. if (status != SCI_SUCCESS)
  468. break;
  469. if (task->ata_task.use_ncq)
  470. new_state = SCI_STP_DEV_NCQ;
  471. else {
  472. sci_dev->working_request = ireq;
  473. new_state = SCI_STP_DEV_CMD;
  474. }
  475. sci_change_state(sm, new_state);
  476. break;
  477. }
  478. case SCI_STP_DEV_NCQ: {
  479. struct sas_task *task = isci_request_access_task(ireq);
  480. if (task->ata_task.use_ncq) {
  481. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  482. if (status != SCI_SUCCESS)
  483. return status;
  484. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, ireq);
  485. if (status != SCI_SUCCESS)
  486. break;
  487. status = scic_sds_request_start(ireq);
  488. } else
  489. return SCI_FAILURE_INVALID_STATE;
  490. break;
  491. }
  492. case SCI_STP_DEV_AWAIT_RESET:
  493. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  494. case SCI_SMP_DEV_IDLE:
  495. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  496. if (status != SCI_SUCCESS)
  497. return status;
  498. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, ireq);
  499. if (status != SCI_SUCCESS)
  500. break;
  501. status = scic_sds_request_start(ireq);
  502. if (status != SCI_SUCCESS)
  503. break;
  504. sci_dev->working_request = ireq;
  505. sci_change_state(&sci_dev->sm, SCI_SMP_DEV_CMD);
  506. break;
  507. case SCI_STP_DEV_CMD:
  508. case SCI_SMP_DEV_CMD:
  509. /* device is already handling a command it can not accept new commands
  510. * until this one is complete.
  511. */
  512. return SCI_FAILURE_INVALID_STATE;
  513. }
  514. scic_sds_remote_device_start_request(sci_dev, ireq, status);
  515. return status;
  516. }
  517. static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
  518. struct scic_sds_remote_device *sci_dev,
  519. struct isci_request *ireq)
  520. {
  521. enum sci_status status;
  522. status = scic_sds_request_complete(ireq);
  523. if (status != SCI_SUCCESS)
  524. return status;
  525. status = scic_sds_port_complete_io(sci_port, sci_dev, ireq);
  526. if (status != SCI_SUCCESS)
  527. return status;
  528. scic_sds_remote_device_decrement_request_count(sci_dev);
  529. return status;
  530. }
  531. enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
  532. struct scic_sds_remote_device *sci_dev,
  533. struct isci_request *ireq)
  534. {
  535. struct sci_base_state_machine *sm = &sci_dev->sm;
  536. enum scic_sds_remote_device_states state = sm->current_state_id;
  537. struct scic_sds_port *sci_port = sci_dev->owning_port;
  538. enum sci_status status;
  539. switch (state) {
  540. case SCI_DEV_INITIAL:
  541. case SCI_DEV_STOPPED:
  542. case SCI_DEV_STARTING:
  543. case SCI_STP_DEV_IDLE:
  544. case SCI_SMP_DEV_IDLE:
  545. case SCI_DEV_FAILED:
  546. case SCI_DEV_FINAL:
  547. default:
  548. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  549. __func__, state);
  550. return SCI_FAILURE_INVALID_STATE;
  551. case SCI_DEV_READY:
  552. case SCI_STP_DEV_AWAIT_RESET:
  553. case SCI_DEV_RESETTING:
  554. status = common_complete_io(sci_port, sci_dev, ireq);
  555. break;
  556. case SCI_STP_DEV_CMD:
  557. case SCI_STP_DEV_NCQ:
  558. case SCI_STP_DEV_NCQ_ERROR:
  559. status = common_complete_io(sci_port, sci_dev, ireq);
  560. if (status != SCI_SUCCESS)
  561. break;
  562. if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  563. /* This request causes hardware error, device needs to be Lun Reset.
  564. * So here we force the state machine to IDLE state so the rest IOs
  565. * can reach RNC state handler, these IOs will be completed by RNC with
  566. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  567. */
  568. sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
  569. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  570. sci_change_state(sm, SCI_STP_DEV_IDLE);
  571. break;
  572. case SCI_SMP_DEV_CMD:
  573. status = common_complete_io(sci_port, sci_dev, ireq);
  574. if (status != SCI_SUCCESS)
  575. break;
  576. sci_change_state(sm, SCI_SMP_DEV_IDLE);
  577. break;
  578. case SCI_DEV_STOPPING:
  579. status = common_complete_io(sci_port, sci_dev, ireq);
  580. if (status != SCI_SUCCESS)
  581. break;
  582. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  583. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  584. rnc_destruct_done,
  585. sci_dev);
  586. break;
  587. }
  588. if (status != SCI_SUCCESS)
  589. dev_err(scirdev_to_dev(sci_dev),
  590. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  591. "could not complete\n", __func__, sci_port,
  592. sci_dev, ireq, status);
  593. else
  594. isci_put_device(sci_dev_to_idev(sci_dev));
  595. return status;
  596. }
  597. static void scic_sds_remote_device_continue_request(void *dev)
  598. {
  599. struct scic_sds_remote_device *sci_dev = dev;
  600. /* we need to check if this request is still valid to continue. */
  601. if (sci_dev->working_request)
  602. scic_controller_continue_io(sci_dev->working_request);
  603. }
  604. enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
  605. struct scic_sds_remote_device *sci_dev,
  606. struct isci_request *ireq)
  607. {
  608. struct sci_base_state_machine *sm = &sci_dev->sm;
  609. enum scic_sds_remote_device_states state = sm->current_state_id;
  610. struct scic_sds_port *sci_port = sci_dev->owning_port;
  611. enum sci_status status;
  612. switch (state) {
  613. case SCI_DEV_INITIAL:
  614. case SCI_DEV_STOPPED:
  615. case SCI_DEV_STARTING:
  616. case SCI_SMP_DEV_IDLE:
  617. case SCI_SMP_DEV_CMD:
  618. case SCI_DEV_STOPPING:
  619. case SCI_DEV_FAILED:
  620. case SCI_DEV_RESETTING:
  621. case SCI_DEV_FINAL:
  622. default:
  623. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  624. __func__, state);
  625. return SCI_FAILURE_INVALID_STATE;
  626. case SCI_STP_DEV_IDLE:
  627. case SCI_STP_DEV_CMD:
  628. case SCI_STP_DEV_NCQ:
  629. case SCI_STP_DEV_NCQ_ERROR:
  630. case SCI_STP_DEV_AWAIT_RESET:
  631. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  632. if (status != SCI_SUCCESS)
  633. return status;
  634. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, ireq);
  635. if (status != SCI_SUCCESS)
  636. goto out;
  637. status = scic_sds_request_start(ireq);
  638. if (status != SCI_SUCCESS)
  639. goto out;
  640. /* Note: If the remote device state is not IDLE this will
  641. * replace the request that probably resulted in the task
  642. * management request.
  643. */
  644. sci_dev->working_request = ireq;
  645. sci_change_state(sm, SCI_STP_DEV_CMD);
  646. /* The remote node context must cleanup the TCi to NCQ mapping
  647. * table. The only way to do this correctly is to either write
  648. * to the TLCR register or to invalidate and repost the RNC. In
  649. * either case the remote node context state machine will take
  650. * the correct action when the remote node context is suspended
  651. * and later resumed.
  652. */
  653. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  654. SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  655. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  656. scic_sds_remote_device_continue_request,
  657. sci_dev);
  658. out:
  659. scic_sds_remote_device_start_request(sci_dev, ireq, status);
  660. /* We need to let the controller start request handler know that
  661. * it can't post TC yet. We will provide a callback function to
  662. * post TC when RNC gets resumed.
  663. */
  664. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  665. case SCI_DEV_READY:
  666. status = scic_sds_port_start_io(sci_port, sci_dev, ireq);
  667. if (status != SCI_SUCCESS)
  668. return status;
  669. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, ireq);
  670. if (status != SCI_SUCCESS)
  671. break;
  672. status = scic_sds_request_start(ireq);
  673. break;
  674. }
  675. scic_sds_remote_device_start_request(sci_dev, ireq, status);
  676. return status;
  677. }
  678. /**
  679. *
  680. * @sci_dev:
  681. * @request:
  682. *
  683. * This method takes the request and bulids an appropriate SCU context for the
  684. * request and then requests the controller to post the request. none
  685. */
  686. void scic_sds_remote_device_post_request(
  687. struct scic_sds_remote_device *sci_dev,
  688. u32 request)
  689. {
  690. u32 context;
  691. context = scic_sds_remote_device_build_command_context(sci_dev, request);
  692. scic_sds_controller_post_request(
  693. scic_sds_remote_device_get_controller(sci_dev),
  694. context
  695. );
  696. }
  697. /* called once the remote node context has transisitioned to a
  698. * ready state. This is the indication that the remote device object can also
  699. * transition to ready.
  700. */
  701. static void remote_device_resume_done(void *_dev)
  702. {
  703. struct scic_sds_remote_device *sci_dev = _dev;
  704. if (is_remote_device_ready(sci_dev))
  705. return;
  706. /* go 'ready' if we are not already in a ready state */
  707. sci_change_state(&sci_dev->sm, SCI_DEV_READY);
  708. }
  709. static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  710. {
  711. struct scic_sds_remote_device *sci_dev = _dev;
  712. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  713. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  714. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  715. * As a result, avoid sending the ready notification.
  716. */
  717. if (sci_dev->sm.previous_state_id != SCI_STP_DEV_NCQ)
  718. isci_remote_device_ready(scic_to_ihost(scic), idev);
  719. }
  720. static void scic_sds_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
  721. {
  722. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  723. /* Initial state is a transitional state to the stopped state */
  724. sci_change_state(&sci_dev->sm, SCI_DEV_STOPPED);
  725. }
  726. /**
  727. * scic_remote_device_destruct() - free remote node context and destruct
  728. * @remote_device: This parameter specifies the remote device to be destructed.
  729. *
  730. * Remote device objects are a limited resource. As such, they must be
  731. * protected. Thus calls to construct and destruct are mutually exclusive and
  732. * non-reentrant. The return value shall indicate if the device was
  733. * successfully destructed or if some failure occurred. enum sci_status This value
  734. * is returned if the device is successfully destructed.
  735. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  736. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  737. * valid, etc.).
  738. */
  739. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  740. {
  741. struct sci_base_state_machine *sm = &sci_dev->sm;
  742. enum scic_sds_remote_device_states state = sm->current_state_id;
  743. struct scic_sds_controller *scic;
  744. if (state != SCI_DEV_STOPPED) {
  745. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  746. __func__, state);
  747. return SCI_FAILURE_INVALID_STATE;
  748. }
  749. scic = sci_dev->owning_port->owning_controller;
  750. scic_sds_controller_free_remote_node_context(scic, sci_dev,
  751. sci_dev->rnc.remote_node_index);
  752. sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  753. sci_change_state(sm, SCI_DEV_FINAL);
  754. return SCI_SUCCESS;
  755. }
  756. /**
  757. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  758. * @ihost: This parameter specifies the isci host object.
  759. * @idev: This parameter specifies the remote device to be freed.
  760. *
  761. */
  762. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  763. {
  764. dev_dbg(&ihost->pdev->dev,
  765. "%s: isci_device = %p\n", __func__, idev);
  766. /* There should not be any outstanding io's. All paths to
  767. * here should go through isci_remote_device_nuke_requests.
  768. * If we hit this condition, we will need a way to complete
  769. * io requests in process */
  770. BUG_ON(!list_empty(&idev->reqs_in_process));
  771. scic_remote_device_destruct(&idev->sci);
  772. list_del_init(&idev->node);
  773. isci_put_device(idev);
  774. }
  775. static void scic_sds_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
  776. {
  777. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  778. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  779. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  780. u32 prev_state;
  781. /* If we are entering from the stopping state let the SCI User know that
  782. * the stop operation has completed.
  783. */
  784. prev_state = sci_dev->sm.previous_state_id;
  785. if (prev_state == SCI_DEV_STOPPING)
  786. isci_remote_device_deconstruct(scic_to_ihost(scic), idev);
  787. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  788. }
  789. static void scic_sds_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
  790. {
  791. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  792. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  793. struct isci_host *ihost = scic_to_ihost(scic);
  794. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  795. isci_remote_device_not_ready(ihost, idev,
  796. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  797. }
  798. static void scic_sds_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
  799. {
  800. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  801. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  802. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  803. struct domain_device *dev = idev->domain_dev;
  804. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  805. sci_change_state(&sci_dev->sm, SCI_STP_DEV_IDLE);
  806. } else if (dev_is_expander(dev)) {
  807. sci_change_state(&sci_dev->sm, SCI_SMP_DEV_IDLE);
  808. } else
  809. isci_remote_device_ready(scic_to_ihost(scic), idev);
  810. }
  811. static void scic_sds_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
  812. {
  813. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  814. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  815. if (dev->dev_type == SAS_END_DEV) {
  816. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  817. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  818. isci_remote_device_not_ready(scic_to_ihost(scic), idev,
  819. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  820. }
  821. }
  822. static void scic_sds_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
  823. {
  824. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  825. scic_sds_remote_node_context_suspend(
  826. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  827. }
  828. static void scic_sds_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
  829. {
  830. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  831. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  832. }
  833. static void scic_sds_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  834. {
  835. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  836. sci_dev->working_request = NULL;
  837. if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
  838. /*
  839. * Since the RNC is ready, it's alright to finish completion
  840. * processing (e.g. signal the remote device is ready). */
  841. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
  842. } else {
  843. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  844. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
  845. sci_dev);
  846. }
  847. }
  848. static void scic_sds_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  849. {
  850. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  851. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  852. BUG_ON(sci_dev->working_request == NULL);
  853. isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
  854. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  855. }
  856. static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
  857. {
  858. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  859. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  860. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  861. if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  862. isci_remote_device_not_ready(scic_to_ihost(scic), idev,
  863. sci_dev->not_ready_reason);
  864. }
  865. static void scic_sds_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  866. {
  867. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  868. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  869. isci_remote_device_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev));
  870. }
  871. static void scic_sds_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  872. {
  873. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  874. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  875. BUG_ON(sci_dev->working_request == NULL);
  876. isci_remote_device_not_ready(scic_to_ihost(scic), sci_dev_to_idev(sci_dev),
  877. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  878. }
  879. static void scic_sds_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
  880. {
  881. struct scic_sds_remote_device *sci_dev = container_of(sm, typeof(*sci_dev), sm);
  882. sci_dev->working_request = NULL;
  883. }
  884. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  885. [SCI_DEV_INITIAL] = {
  886. .enter_state = scic_sds_remote_device_initial_state_enter,
  887. },
  888. [SCI_DEV_STOPPED] = {
  889. .enter_state = scic_sds_remote_device_stopped_state_enter,
  890. },
  891. [SCI_DEV_STARTING] = {
  892. .enter_state = scic_sds_remote_device_starting_state_enter,
  893. },
  894. [SCI_DEV_READY] = {
  895. .enter_state = scic_sds_remote_device_ready_state_enter,
  896. .exit_state = scic_sds_remote_device_ready_state_exit
  897. },
  898. [SCI_STP_DEV_IDLE] = {
  899. .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
  900. },
  901. [SCI_STP_DEV_CMD] = {
  902. .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
  903. },
  904. [SCI_STP_DEV_NCQ] = { },
  905. [SCI_STP_DEV_NCQ_ERROR] = {
  906. .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
  907. },
  908. [SCI_STP_DEV_AWAIT_RESET] = { },
  909. [SCI_SMP_DEV_IDLE] = {
  910. .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
  911. },
  912. [SCI_SMP_DEV_CMD] = {
  913. .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
  914. .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
  915. },
  916. [SCI_DEV_STOPPING] = { },
  917. [SCI_DEV_FAILED] = { },
  918. [SCI_DEV_RESETTING] = {
  919. .enter_state = scic_sds_remote_device_resetting_state_enter,
  920. .exit_state = scic_sds_remote_device_resetting_state_exit
  921. },
  922. [SCI_DEV_FINAL] = { },
  923. };
  924. /**
  925. * scic_remote_device_construct() - common construction
  926. * @sci_port: SAS/SATA port through which this device is accessed.
  927. * @sci_dev: remote device to construct
  928. *
  929. * This routine just performs benign initialization and does not
  930. * allocate the remote_node_context which is left to
  931. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  932. * frees the remote_node_context(s) for the device.
  933. */
  934. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  935. struct scic_sds_remote_device *sci_dev)
  936. {
  937. sci_dev->owning_port = sci_port;
  938. sci_dev->started_request_count = 0;
  939. sci_init_sm(&sci_dev->sm, scic_sds_remote_device_state_table, SCI_DEV_INITIAL);
  940. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  941. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  942. }
  943. /**
  944. * scic_remote_device_da_construct() - construct direct attached device.
  945. *
  946. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  947. * the device is known to the SCI Core since it is contained in the
  948. * scic_phy object. Remote node context(s) is/are a global resource
  949. * allocated by this routine, freed by scic_remote_device_destruct().
  950. *
  951. * Returns:
  952. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  953. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  954. * sata-only controller instance.
  955. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  956. */
  957. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  958. struct scic_sds_remote_device *sci_dev)
  959. {
  960. enum sci_status status;
  961. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  962. scic_remote_device_construct(sci_port, sci_dev);
  963. /*
  964. * This information is request to determine how many remote node context
  965. * entries will be needed to store the remote node.
  966. */
  967. sci_dev->is_direct_attached = true;
  968. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  969. sci_dev,
  970. &sci_dev->rnc.remote_node_index);
  971. if (status != SCI_SUCCESS)
  972. return status;
  973. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  974. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  975. /* pass */;
  976. else
  977. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  978. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  979. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  980. sci_dev->device_port_width = 1;
  981. return SCI_SUCCESS;
  982. }
  983. /**
  984. * scic_remote_device_ea_construct() - construct expander attached device
  985. *
  986. * Remote node context(s) is/are a global resource allocated by this
  987. * routine, freed by scic_remote_device_destruct().
  988. *
  989. * Returns:
  990. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  991. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  992. * sata-only controller instance.
  993. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  994. */
  995. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  996. struct scic_sds_remote_device *sci_dev)
  997. {
  998. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  999. enum sci_status status;
  1000. scic_remote_device_construct(sci_port, sci_dev);
  1001. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1002. sci_dev,
  1003. &sci_dev->rnc.remote_node_index);
  1004. if (status != SCI_SUCCESS)
  1005. return status;
  1006. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1007. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1008. /* pass */;
  1009. else
  1010. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1011. /*
  1012. * For SAS-2 the physical link rate is actually a logical link
  1013. * rate that incorporates multiplexing. The SCU doesn't
  1014. * incorporate multiplexing and for the purposes of the
  1015. * connection the logical link rate is that same as the
  1016. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1017. * one another, so this code works for both situations. */
  1018. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1019. dev->linkrate);
  1020. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1021. sci_dev->device_port_width = 1;
  1022. return SCI_SUCCESS;
  1023. }
  1024. /**
  1025. * scic_remote_device_start() - This method will start the supplied remote
  1026. * device. This method enables normal IO requests to flow through to the
  1027. * remote device.
  1028. * @remote_device: This parameter specifies the device to be started.
  1029. * @timeout: This parameter specifies the number of milliseconds in which the
  1030. * start operation should complete.
  1031. *
  1032. * An indication of whether the device was successfully started. SCI_SUCCESS
  1033. * This value is returned if the device was successfully started.
  1034. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1035. * the device when there have been no phys added to it.
  1036. */
  1037. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1038. u32 timeout)
  1039. {
  1040. struct sci_base_state_machine *sm = &sci_dev->sm;
  1041. enum scic_sds_remote_device_states state = sm->current_state_id;
  1042. enum sci_status status;
  1043. if (state != SCI_DEV_STOPPED) {
  1044. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1045. __func__, state);
  1046. return SCI_FAILURE_INVALID_STATE;
  1047. }
  1048. status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1049. remote_device_resume_done,
  1050. sci_dev);
  1051. if (status != SCI_SUCCESS)
  1052. return status;
  1053. sci_change_state(sm, SCI_DEV_STARTING);
  1054. return SCI_SUCCESS;
  1055. }
  1056. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1057. struct isci_remote_device *idev)
  1058. {
  1059. struct scic_sds_port *sci_port = &iport->sci;
  1060. struct isci_host *ihost = iport->isci_host;
  1061. struct domain_device *dev = idev->domain_dev;
  1062. enum sci_status status;
  1063. if (dev->parent && dev_is_expander(dev->parent))
  1064. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1065. else
  1066. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1067. if (status != SCI_SUCCESS) {
  1068. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1069. __func__, status);
  1070. return status;
  1071. }
  1072. /* start the device. */
  1073. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1074. if (status != SCI_SUCCESS)
  1075. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1076. status);
  1077. return status;
  1078. }
  1079. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1080. {
  1081. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1082. dev_dbg(&ihost->pdev->dev,
  1083. "%s: idev = %p\n", __func__, idev);
  1084. /* Cleanup all requests pending for this device. */
  1085. isci_terminate_pending_requests(ihost, idev);
  1086. dev_dbg(&ihost->pdev->dev,
  1087. "%s: idev = %p, done\n", __func__, idev);
  1088. }
  1089. /**
  1090. * This function builds the isci_remote_device when a libsas dev_found message
  1091. * is received.
  1092. * @isci_host: This parameter specifies the isci host object.
  1093. * @port: This parameter specifies the isci_port conected to this device.
  1094. *
  1095. * pointer to new isci_remote_device.
  1096. */
  1097. static struct isci_remote_device *
  1098. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1099. {
  1100. struct isci_remote_device *idev;
  1101. int i;
  1102. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1103. idev = &ihost->devices[i];
  1104. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1105. break;
  1106. }
  1107. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1108. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1109. return NULL;
  1110. }
  1111. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1112. return NULL;
  1113. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1114. return NULL;
  1115. return idev;
  1116. }
  1117. void isci_remote_device_release(struct kref *kref)
  1118. {
  1119. struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
  1120. struct isci_host *ihost = idev->isci_port->isci_host;
  1121. idev->domain_dev = NULL;
  1122. idev->isci_port = NULL;
  1123. clear_bit(IDEV_START_PENDING, &idev->flags);
  1124. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1125. clear_bit(IDEV_IO_READY, &idev->flags);
  1126. clear_bit(IDEV_GONE, &idev->flags);
  1127. clear_bit(IDEV_EH, &idev->flags);
  1128. smp_mb__before_clear_bit();
  1129. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1130. wake_up(&ihost->eventq);
  1131. }
  1132. /**
  1133. * isci_remote_device_stop() - This function is called internally to stop the
  1134. * remote device.
  1135. * @isci_host: This parameter specifies the isci host object.
  1136. * @isci_device: This parameter specifies the remote device.
  1137. *
  1138. * The status of the scic request to stop.
  1139. */
  1140. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1141. {
  1142. enum sci_status status;
  1143. unsigned long flags;
  1144. dev_dbg(&ihost->pdev->dev,
  1145. "%s: isci_device = %p\n", __func__, idev);
  1146. spin_lock_irqsave(&ihost->scic_lock, flags);
  1147. idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
  1148. set_bit(IDEV_GONE, &idev->flags);
  1149. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1150. /* Kill all outstanding requests. */
  1151. isci_remote_device_nuke_requests(ihost, idev);
  1152. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1153. spin_lock_irqsave(&ihost->scic_lock, flags);
  1154. status = scic_remote_device_stop(&idev->sci, 50);
  1155. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1156. /* Wait for the stop complete callback. */
  1157. if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
  1158. /* nothing to wait for */;
  1159. else
  1160. wait_for_device_stop(ihost, idev);
  1161. return status;
  1162. }
  1163. /**
  1164. * isci_remote_device_gone() - This function is called by libsas when a domain
  1165. * device is removed.
  1166. * @domain_device: This parameter specifies the libsas domain device.
  1167. *
  1168. */
  1169. void isci_remote_device_gone(struct domain_device *dev)
  1170. {
  1171. struct isci_host *ihost = dev_to_ihost(dev);
  1172. struct isci_remote_device *idev = dev->lldd_dev;
  1173. dev_dbg(&ihost->pdev->dev,
  1174. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1175. __func__, dev, idev, idev->isci_port);
  1176. isci_remote_device_stop(ihost, idev);
  1177. }
  1178. /**
  1179. * isci_remote_device_found() - This function is called by libsas when a remote
  1180. * device is discovered. A remote device object is created and started. the
  1181. * function then sleeps until the sci core device started message is
  1182. * received.
  1183. * @domain_device: This parameter specifies the libsas domain device.
  1184. *
  1185. * status, zero indicates success.
  1186. */
  1187. int isci_remote_device_found(struct domain_device *domain_dev)
  1188. {
  1189. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1190. struct isci_port *isci_port;
  1191. struct isci_phy *isci_phy;
  1192. struct asd_sas_port *sas_port;
  1193. struct asd_sas_phy *sas_phy;
  1194. struct isci_remote_device *isci_device;
  1195. enum sci_status status;
  1196. dev_dbg(&isci_host->pdev->dev,
  1197. "%s: domain_device = %p\n", __func__, domain_dev);
  1198. wait_for_start(isci_host);
  1199. sas_port = domain_dev->port;
  1200. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1201. port_phy_el);
  1202. isci_phy = to_isci_phy(sas_phy);
  1203. isci_port = isci_phy->isci_port;
  1204. /* we are being called for a device on this port,
  1205. * so it has to come up eventually
  1206. */
  1207. wait_for_completion(&isci_port->start_complete);
  1208. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1209. (isci_stopped == isci_port_get_state(isci_port)))
  1210. return -ENODEV;
  1211. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1212. if (!isci_device)
  1213. return -ENODEV;
  1214. kref_init(&isci_device->kref);
  1215. INIT_LIST_HEAD(&isci_device->node);
  1216. spin_lock_irq(&isci_host->scic_lock);
  1217. isci_device->domain_dev = domain_dev;
  1218. isci_device->isci_port = isci_port;
  1219. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1220. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1221. status = isci_remote_device_construct(isci_port, isci_device);
  1222. dev_dbg(&isci_host->pdev->dev,
  1223. "%s: isci_device = %p\n",
  1224. __func__, isci_device);
  1225. if (status == SCI_SUCCESS) {
  1226. /* device came up, advertise it to the world */
  1227. domain_dev->lldd_dev = isci_device;
  1228. } else
  1229. isci_put_device(isci_device);
  1230. spin_unlock_irq(&isci_host->scic_lock);
  1231. /* wait for the device ready callback. */
  1232. wait_for_device_start(isci_host, isci_device);
  1233. return status == SCI_SUCCESS ? 0 : -ENODEV;
  1234. }
  1235. /**
  1236. * isci_device_is_reset_pending() - This function will check if there is any
  1237. * pending reset condition on the device.
  1238. * @request: This parameter is the isci_device object.
  1239. *
  1240. * true if there is a reset pending for the device.
  1241. */
  1242. bool isci_device_is_reset_pending(
  1243. struct isci_host *isci_host,
  1244. struct isci_remote_device *isci_device)
  1245. {
  1246. struct isci_request *isci_request;
  1247. struct isci_request *tmp_req;
  1248. bool reset_is_pending = false;
  1249. unsigned long flags;
  1250. dev_dbg(&isci_host->pdev->dev,
  1251. "%s: isci_device = %p\n", __func__, isci_device);
  1252. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1253. /* Check for reset on all pending requests. */
  1254. list_for_each_entry_safe(isci_request, tmp_req,
  1255. &isci_device->reqs_in_process, dev_node) {
  1256. dev_dbg(&isci_host->pdev->dev,
  1257. "%s: isci_device = %p request = %p\n",
  1258. __func__, isci_device, isci_request);
  1259. if (isci_request->ttype == io_task) {
  1260. struct sas_task *task = isci_request_access_task(
  1261. isci_request);
  1262. spin_lock(&task->task_state_lock);
  1263. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1264. reset_is_pending = true;
  1265. spin_unlock(&task->task_state_lock);
  1266. }
  1267. }
  1268. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1269. dev_dbg(&isci_host->pdev->dev,
  1270. "%s: isci_device = %p reset_is_pending = %d\n",
  1271. __func__, isci_device, reset_is_pending);
  1272. return reset_is_pending;
  1273. }
  1274. /**
  1275. * isci_device_clear_reset_pending() - This function will clear if any pending
  1276. * reset condition flags on the device.
  1277. * @request: This parameter is the isci_device object.
  1278. *
  1279. * true if there is a reset pending for the device.
  1280. */
  1281. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1282. {
  1283. struct isci_request *isci_request;
  1284. struct isci_request *tmp_req;
  1285. unsigned long flags = 0;
  1286. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1287. __func__, idev, ihost);
  1288. spin_lock_irqsave(&ihost->scic_lock, flags);
  1289. /* Clear reset pending on all pending requests. */
  1290. list_for_each_entry_safe(isci_request, tmp_req,
  1291. &idev->reqs_in_process, dev_node) {
  1292. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1293. __func__, idev, isci_request);
  1294. if (isci_request->ttype == io_task) {
  1295. unsigned long flags2;
  1296. struct sas_task *task = isci_request_access_task(
  1297. isci_request);
  1298. spin_lock_irqsave(&task->task_state_lock, flags2);
  1299. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1300. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1301. }
  1302. }
  1303. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1304. }