remote_device.c 44 KB

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