remote_device.c 44 KB

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