remote_device.c 47 KB

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