remote_device.c 47 KB

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