remote_device.c 46 KB

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