remote_device.c 48 KB

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