remote_device.c 44 KB

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