remote_device.c 46 KB

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