remote_node_context.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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_ata.h>
  56. #include "host.h"
  57. #include "isci.h"
  58. #include "remote_device.h"
  59. #include "remote_node_context.h"
  60. #include "scu_event_codes.h"
  61. #include "scu_task_context.h"
  62. #undef C
  63. #define C(a) (#a)
  64. const char *rnc_state_name(enum scis_sds_remote_node_context_states state)
  65. {
  66. static const char * const strings[] = RNC_STATES;
  67. return strings[state];
  68. }
  69. #undef C
  70. /**
  71. *
  72. * @sci_rnc: The state of the remote node context object to check.
  73. *
  74. * This method will return true if the remote node context is in a READY state
  75. * otherwise it will return false bool true if the remote node context is in
  76. * the ready state. false if the remote node context is not in the ready state.
  77. */
  78. bool sci_remote_node_context_is_ready(
  79. struct sci_remote_node_context *sci_rnc)
  80. {
  81. u32 current_state = sci_rnc->sm.current_state_id;
  82. if (current_state == SCI_RNC_READY) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. bool sci_remote_node_context_is_suspended(struct sci_remote_node_context *sci_rnc)
  88. {
  89. u32 current_state = sci_rnc->sm.current_state_id;
  90. if (current_state == SCI_RNC_TX_RX_SUSPENDED)
  91. return true;
  92. return false;
  93. }
  94. static union scu_remote_node_context *sci_rnc_by_id(struct isci_host *ihost, u16 id)
  95. {
  96. if (id < ihost->remote_node_entries &&
  97. ihost->device_table[id])
  98. return &ihost->remote_node_context_table[id];
  99. return NULL;
  100. }
  101. static void sci_remote_node_context_construct_buffer(struct sci_remote_node_context *sci_rnc)
  102. {
  103. struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
  104. struct domain_device *dev = idev->domain_dev;
  105. int rni = sci_rnc->remote_node_index;
  106. union scu_remote_node_context *rnc;
  107. struct isci_host *ihost;
  108. __le64 sas_addr;
  109. ihost = idev->owning_port->owning_controller;
  110. rnc = sci_rnc_by_id(ihost, rni);
  111. memset(rnc, 0, sizeof(union scu_remote_node_context)
  112. * sci_remote_device_node_count(idev));
  113. rnc->ssp.remote_node_index = rni;
  114. rnc->ssp.remote_node_port_width = idev->device_port_width;
  115. rnc->ssp.logical_port_index = idev->owning_port->physical_port_index;
  116. /* sas address is __be64, context ram format is __le64 */
  117. sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
  118. rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
  119. rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
  120. rnc->ssp.nexus_loss_timer_enable = true;
  121. rnc->ssp.check_bit = false;
  122. rnc->ssp.is_valid = false;
  123. rnc->ssp.is_remote_node_context = true;
  124. rnc->ssp.function_number = 0;
  125. rnc->ssp.arbitration_wait_time = 0;
  126. if (dev_is_sata(dev)) {
  127. rnc->ssp.connection_occupancy_timeout =
  128. ihost->user_parameters.stp_max_occupancy_timeout;
  129. rnc->ssp.connection_inactivity_timeout =
  130. ihost->user_parameters.stp_inactivity_timeout;
  131. } else {
  132. rnc->ssp.connection_occupancy_timeout =
  133. ihost->user_parameters.ssp_max_occupancy_timeout;
  134. rnc->ssp.connection_inactivity_timeout =
  135. ihost->user_parameters.ssp_inactivity_timeout;
  136. }
  137. rnc->ssp.initial_arbitration_wait_time = 0;
  138. /* Open Address Frame Parameters */
  139. rnc->ssp.oaf_connection_rate = idev->connection_rate;
  140. rnc->ssp.oaf_features = 0;
  141. rnc->ssp.oaf_source_zone_group = 0;
  142. rnc->ssp.oaf_more_compatibility_features = 0;
  143. }
  144. /**
  145. *
  146. * @sci_rnc:
  147. * @callback:
  148. * @callback_parameter:
  149. *
  150. * This method will setup the remote node context object so it will transition
  151. * to its ready state. If the remote node context is already setup to
  152. * transition to its final state then this function does nothing. none
  153. */
  154. static void sci_remote_node_context_setup_to_resume(
  155. struct sci_remote_node_context *sci_rnc,
  156. scics_sds_remote_node_context_callback callback,
  157. void *callback_parameter,
  158. enum sci_remote_node_context_destination_state dest_param)
  159. {
  160. if (sci_rnc->destination_state != RNC_DEST_FINAL) {
  161. sci_rnc->destination_state = dest_param;
  162. if (callback != NULL) {
  163. sci_rnc->user_callback = callback;
  164. sci_rnc->user_cookie = callback_parameter;
  165. }
  166. }
  167. }
  168. static void sci_remote_node_context_setup_to_destroy(
  169. struct sci_remote_node_context *sci_rnc,
  170. scics_sds_remote_node_context_callback callback,
  171. void *callback_parameter)
  172. {
  173. struct isci_host *ihost = idev_to_ihost(rnc_to_dev(sci_rnc));
  174. sci_rnc->destination_state = RNC_DEST_FINAL;
  175. sci_rnc->user_callback = callback;
  176. sci_rnc->user_cookie = callback_parameter;
  177. wake_up(&ihost->eventq);
  178. }
  179. /**
  180. *
  181. *
  182. * This method just calls the user callback function and then resets the
  183. * callback.
  184. */
  185. static void sci_remote_node_context_notify_user(
  186. struct sci_remote_node_context *rnc)
  187. {
  188. if (rnc->user_callback != NULL) {
  189. (*rnc->user_callback)(rnc->user_cookie);
  190. rnc->user_callback = NULL;
  191. rnc->user_cookie = NULL;
  192. }
  193. }
  194. static void sci_remote_node_context_continue_state_transitions(struct sci_remote_node_context *rnc)
  195. {
  196. switch (rnc->destination_state) {
  197. case RNC_DEST_READY:
  198. case RNC_DEST_SUSPENDED_RESUME:
  199. rnc->destination_state = RNC_DEST_READY;
  200. /* Fall through... */
  201. case RNC_DEST_FINAL:
  202. sci_remote_node_context_resume(rnc, rnc->user_callback,
  203. rnc->user_cookie);
  204. break;
  205. default:
  206. rnc->destination_state = RNC_DEST_UNSPECIFIED;
  207. break;
  208. }
  209. }
  210. static void sci_remote_node_context_validate_context_buffer(struct sci_remote_node_context *sci_rnc)
  211. {
  212. union scu_remote_node_context *rnc_buffer;
  213. struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
  214. struct domain_device *dev = idev->domain_dev;
  215. struct isci_host *ihost = idev->owning_port->owning_controller;
  216. rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
  217. rnc_buffer->ssp.is_valid = true;
  218. if (dev_is_sata(dev) && dev->parent) {
  219. sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_96);
  220. } else {
  221. sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_32);
  222. if (!dev->parent)
  223. sci_port_setup_transports(idev->owning_port,
  224. sci_rnc->remote_node_index);
  225. }
  226. }
  227. static void sci_remote_node_context_invalidate_context_buffer(struct sci_remote_node_context *sci_rnc)
  228. {
  229. union scu_remote_node_context *rnc_buffer;
  230. struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
  231. struct isci_host *ihost = idev->owning_port->owning_controller;
  232. rnc_buffer = sci_rnc_by_id(ihost, sci_rnc->remote_node_index);
  233. rnc_buffer->ssp.is_valid = false;
  234. sci_remote_device_post_request(rnc_to_dev(sci_rnc),
  235. SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
  236. }
  237. static void sci_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
  238. {
  239. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  240. struct isci_remote_device *idev = rnc_to_dev(rnc);
  241. struct isci_host *ihost = idev->owning_port->owning_controller;
  242. /* Check to see if we have gotten back to the initial state because
  243. * someone requested to destroy the remote node context object.
  244. */
  245. if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
  246. rnc->destination_state = RNC_DEST_UNSPECIFIED;
  247. sci_remote_node_context_notify_user(rnc);
  248. smp_wmb();
  249. wake_up(&ihost->eventq);
  250. }
  251. }
  252. static void sci_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
  253. {
  254. struct sci_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
  255. sci_remote_node_context_validate_context_buffer(sci_rnc);
  256. }
  257. static void sci_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
  258. {
  259. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  260. /* Terminate all outstanding requests. */
  261. sci_remote_device_terminate_requests(rnc_to_dev(rnc));
  262. sci_remote_node_context_invalidate_context_buffer(rnc);
  263. }
  264. static void sci_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
  265. {
  266. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  267. struct isci_remote_device *idev;
  268. struct domain_device *dev;
  269. idev = rnc_to_dev(rnc);
  270. dev = idev->domain_dev;
  271. /*
  272. * For direct attached SATA devices we need to clear the TLCR
  273. * NCQ to TCi tag mapping on the phy and in cases where we
  274. * resume because of a target reset we also need to update
  275. * the STPTLDARNI register with the RNi of the device
  276. */
  277. if (dev_is_sata(dev) && !dev->parent)
  278. sci_port_setup_transports(idev->owning_port, rnc->remote_node_index);
  279. sci_remote_device_post_request(idev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
  280. }
  281. static void sci_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
  282. {
  283. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  284. enum sci_remote_node_context_destination_state dest_select;
  285. int tell_user = 1;
  286. dest_select = rnc->destination_state;
  287. rnc->destination_state = RNC_DEST_UNSPECIFIED;
  288. if ((dest_select == RNC_DEST_SUSPENDED) ||
  289. (dest_select == RNC_DEST_SUSPENDED_RESUME)) {
  290. sci_remote_node_context_suspend(
  291. rnc, rnc->suspend_reason,
  292. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT);
  293. if (dest_select == RNC_DEST_SUSPENDED_RESUME)
  294. tell_user = 0; /* Wait until ready again. */
  295. }
  296. if (tell_user)
  297. sci_remote_node_context_notify_user(rnc);
  298. }
  299. static void sci_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
  300. {
  301. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  302. sci_remote_node_context_continue_state_transitions(rnc);
  303. }
  304. static void sci_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
  305. {
  306. struct sci_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  307. struct isci_remote_device *idev = rnc_to_dev(rnc);
  308. struct isci_host *ihost = idev->owning_port->owning_controller;
  309. u32 new_count = rnc->suspend_count + 1;
  310. if (new_count == 0)
  311. rnc->suspend_count = 1;
  312. else
  313. rnc->suspend_count = new_count;
  314. smp_wmb();
  315. /* Terminate outstanding requests pending abort. */
  316. sci_remote_device_abort_requests_pending_abort(idev);
  317. wake_up(&ihost->eventq);
  318. sci_remote_node_context_continue_state_transitions(rnc);
  319. }
  320. static void sci_remote_node_context_await_suspend_state_exit(
  321. struct sci_base_state_machine *sm)
  322. {
  323. struct sci_remote_node_context *rnc
  324. = container_of(sm, typeof(*rnc), sm);
  325. struct isci_remote_device *idev = rnc_to_dev(rnc);
  326. if (dev_is_sata(idev->domain_dev))
  327. isci_dev_set_hang_detection_timeout(idev, 0);
  328. }
  329. static const struct sci_base_state sci_remote_node_context_state_table[] = {
  330. [SCI_RNC_INITIAL] = {
  331. .enter_state = sci_remote_node_context_initial_state_enter,
  332. },
  333. [SCI_RNC_POSTING] = {
  334. .enter_state = sci_remote_node_context_posting_state_enter,
  335. },
  336. [SCI_RNC_INVALIDATING] = {
  337. .enter_state = sci_remote_node_context_invalidating_state_enter,
  338. },
  339. [SCI_RNC_RESUMING] = {
  340. .enter_state = sci_remote_node_context_resuming_state_enter,
  341. },
  342. [SCI_RNC_READY] = {
  343. .enter_state = sci_remote_node_context_ready_state_enter,
  344. },
  345. [SCI_RNC_TX_SUSPENDED] = {
  346. .enter_state = sci_remote_node_context_tx_suspended_state_enter,
  347. },
  348. [SCI_RNC_TX_RX_SUSPENDED] = {
  349. .enter_state = sci_remote_node_context_tx_rx_suspended_state_enter,
  350. },
  351. [SCI_RNC_AWAIT_SUSPENSION] = {
  352. .exit_state = sci_remote_node_context_await_suspend_state_exit,
  353. },
  354. };
  355. void sci_remote_node_context_construct(struct sci_remote_node_context *rnc,
  356. u16 remote_node_index)
  357. {
  358. memset(rnc, 0, sizeof(struct sci_remote_node_context));
  359. rnc->remote_node_index = remote_node_index;
  360. rnc->destination_state = RNC_DEST_UNSPECIFIED;
  361. sci_init_sm(&rnc->sm, sci_remote_node_context_state_table, SCI_RNC_INITIAL);
  362. }
  363. enum sci_status sci_remote_node_context_event_handler(struct sci_remote_node_context *sci_rnc,
  364. u32 event_code)
  365. {
  366. enum scis_sds_remote_node_context_states state;
  367. u32 next_state;
  368. state = sci_rnc->sm.current_state_id;
  369. switch (state) {
  370. case SCI_RNC_POSTING:
  371. switch (scu_get_event_code(event_code)) {
  372. case SCU_EVENT_POST_RNC_COMPLETE:
  373. sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
  374. break;
  375. default:
  376. goto out;
  377. }
  378. break;
  379. case SCI_RNC_INVALIDATING:
  380. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
  381. if (sci_rnc->destination_state == RNC_DEST_FINAL)
  382. next_state = SCI_RNC_INITIAL;
  383. else
  384. next_state = SCI_RNC_POSTING;
  385. sci_change_state(&sci_rnc->sm, next_state);
  386. } else {
  387. switch (scu_get_event_type(event_code)) {
  388. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  389. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  390. /* We really dont care if the hardware is going to suspend
  391. * the device since it's being invalidated anyway */
  392. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  393. "%s: SCIC Remote Node Context 0x%p was "
  394. "suspeneded by hardware while being "
  395. "invalidated.\n", __func__, sci_rnc);
  396. break;
  397. default:
  398. goto out;
  399. }
  400. }
  401. break;
  402. case SCI_RNC_RESUMING:
  403. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
  404. sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
  405. } else {
  406. switch (scu_get_event_type(event_code)) {
  407. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  408. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  409. /* We really dont care if the hardware is going to suspend
  410. * the device since it's being resumed anyway */
  411. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  412. "%s: SCIC Remote Node Context 0x%p was "
  413. "suspeneded by hardware while being resumed.\n",
  414. __func__, sci_rnc);
  415. break;
  416. default:
  417. goto out;
  418. }
  419. }
  420. break;
  421. case SCI_RNC_READY:
  422. switch (scu_get_event_type(event_code)) {
  423. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  424. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
  425. sci_rnc->suspend_type = scu_get_event_type(event_code);
  426. break;
  427. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  428. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
  429. sci_rnc->suspend_type = scu_get_event_type(event_code);
  430. break;
  431. default:
  432. goto out;
  433. }
  434. break;
  435. case SCI_RNC_AWAIT_SUSPENSION:
  436. switch (scu_get_event_type(event_code)) {
  437. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  438. next_state = SCI_RNC_TX_SUSPENDED;
  439. break;
  440. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  441. next_state = SCI_RNC_TX_RX_SUSPENDED;
  442. break;
  443. default:
  444. goto out;
  445. }
  446. if (sci_rnc->suspend_type == scu_get_event_type(event_code))
  447. sci_change_state(&sci_rnc->sm, next_state);
  448. break;
  449. default:
  450. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  451. "%s: invalid state: %s\n", __func__,
  452. rnc_state_name(state));
  453. return SCI_FAILURE_INVALID_STATE;
  454. }
  455. return SCI_SUCCESS;
  456. out:
  457. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  458. "%s: code: %#x state: %s\n", __func__, event_code,
  459. rnc_state_name(state));
  460. return SCI_FAILURE;
  461. }
  462. enum sci_status sci_remote_node_context_destruct(struct sci_remote_node_context *sci_rnc,
  463. scics_sds_remote_node_context_callback cb_fn,
  464. void *cb_p)
  465. {
  466. enum scis_sds_remote_node_context_states state;
  467. state = sci_rnc->sm.current_state_id;
  468. switch (state) {
  469. case SCI_RNC_INVALIDATING:
  470. sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
  471. return SCI_SUCCESS;
  472. case SCI_RNC_POSTING:
  473. case SCI_RNC_RESUMING:
  474. case SCI_RNC_READY:
  475. case SCI_RNC_TX_SUSPENDED:
  476. case SCI_RNC_TX_RX_SUSPENDED:
  477. sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
  478. sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
  479. return SCI_SUCCESS;
  480. case SCI_RNC_AWAIT_SUSPENSION:
  481. sci_remote_node_context_setup_to_destroy(sci_rnc, cb_fn, cb_p);
  482. return SCI_SUCCESS;
  483. case SCI_RNC_INITIAL:
  484. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  485. "%s: invalid state: %s\n", __func__,
  486. rnc_state_name(state));
  487. /* We have decided that the destruct request on the remote node context
  488. * can not fail since it is either in the initial/destroyed state or is
  489. * can be destroyed.
  490. */
  491. return SCI_SUCCESS;
  492. default:
  493. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  494. "%s: invalid state %s\n", __func__,
  495. rnc_state_name(state));
  496. return SCI_FAILURE_INVALID_STATE;
  497. }
  498. }
  499. enum sci_status sci_remote_node_context_suspend(
  500. struct sci_remote_node_context *sci_rnc,
  501. enum sci_remote_node_suspension_reasons suspend_reason,
  502. u32 suspend_type)
  503. {
  504. enum scis_sds_remote_node_context_states state
  505. = sci_rnc->sm.current_state_id;
  506. struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
  507. enum sci_status status = SCI_FAILURE_INVALID_STATE;
  508. enum sci_remote_node_context_destination_state dest_param =
  509. RNC_DEST_UNSPECIFIED;
  510. dev_dbg(scirdev_to_dev(idev),
  511. "%s: current state %s, current suspend_type %x dest state %d,"
  512. " arg suspend_reason %d, arg suspend_type %x",
  513. __func__, rnc_state_name(state), sci_rnc->suspend_type,
  514. sci_rnc->destination_state, suspend_reason,
  515. suspend_type);
  516. /* Disable automatic state continuations if explicitly suspending. */
  517. if ((suspend_reason == SCI_HW_SUSPEND) ||
  518. (sci_rnc->destination_state == RNC_DEST_FINAL))
  519. dest_param = sci_rnc->destination_state;
  520. switch (state) {
  521. case SCI_RNC_READY:
  522. break;
  523. case SCI_RNC_INVALIDATING:
  524. if (sci_rnc->destination_state == RNC_DEST_FINAL) {
  525. dev_warn(scirdev_to_dev(idev),
  526. "%s: already destroying %p\n",
  527. __func__, sci_rnc);
  528. return SCI_FAILURE_INVALID_STATE;
  529. }
  530. /* Fall through and handle like SCI_RNC_POSTING */
  531. case SCI_RNC_RESUMING:
  532. /* Fall through and handle like SCI_RNC_POSTING */
  533. case SCI_RNC_POSTING:
  534. /* Set the destination state to AWAIT - this signals the
  535. * entry into the SCI_RNC_READY state that a suspension
  536. * needs to be done immediately.
  537. */
  538. if (sci_rnc->destination_state != RNC_DEST_FINAL)
  539. sci_rnc->destination_state = RNC_DEST_SUSPENDED;
  540. sci_rnc->suspend_type = suspend_type;
  541. sci_rnc->suspend_reason = suspend_reason;
  542. return SCI_SUCCESS;
  543. case SCI_RNC_TX_SUSPENDED:
  544. if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX)
  545. status = SCI_SUCCESS;
  546. break;
  547. case SCI_RNC_TX_RX_SUSPENDED:
  548. if (suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
  549. status = SCI_SUCCESS;
  550. break;
  551. case SCI_RNC_AWAIT_SUSPENSION:
  552. if ((sci_rnc->suspend_type == SCU_EVENT_TL_RNC_SUSPEND_TX_RX)
  553. || (suspend_type == sci_rnc->suspend_type))
  554. return SCI_SUCCESS;
  555. break;
  556. default:
  557. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  558. "%s: invalid state %s\n", __func__,
  559. rnc_state_name(state));
  560. return SCI_FAILURE_INVALID_STATE;
  561. }
  562. sci_rnc->destination_state = dest_param;
  563. sci_rnc->suspend_type = suspend_type;
  564. sci_rnc->suspend_reason = suspend_reason;
  565. if (status == SCI_SUCCESS) { /* Already in the destination state? */
  566. struct isci_host *ihost = idev->owning_port->owning_controller;
  567. wake_up_all(&ihost->eventq); /* Let observers look. */
  568. return SCI_SUCCESS;
  569. }
  570. if ((suspend_reason == SCI_SW_SUSPEND_NORMAL) ||
  571. (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)) {
  572. if (suspend_reason == SCI_SW_SUSPEND_LINKHANG_DETECT)
  573. isci_dev_set_hang_detection_timeout(idev, 0x00000001);
  574. sci_remote_device_post_request(
  575. idev, SCI_SOFTWARE_SUSPEND_CMD);
  576. }
  577. if (state != SCI_RNC_AWAIT_SUSPENSION)
  578. sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
  579. return SCI_SUCCESS;
  580. }
  581. enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
  582. scics_sds_remote_node_context_callback cb_fn,
  583. void *cb_p)
  584. {
  585. enum scis_sds_remote_node_context_states state;
  586. struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
  587. state = sci_rnc->sm.current_state_id;
  588. dev_dbg(scirdev_to_dev(idev),
  589. "%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
  590. "dev resume path %s\n",
  591. __func__, rnc_state_name(state), cb_fn, cb_p,
  592. sci_rnc->destination_state,
  593. test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
  594. ? "<abort active>" : "<normal>");
  595. switch (state) {
  596. case SCI_RNC_INITIAL:
  597. if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  598. return SCI_FAILURE_INVALID_STATE;
  599. sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p,
  600. RNC_DEST_READY);
  601. if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
  602. sci_remote_node_context_construct_buffer(sci_rnc);
  603. sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
  604. }
  605. return SCI_SUCCESS;
  606. case SCI_RNC_POSTING:
  607. case SCI_RNC_INVALIDATING:
  608. case SCI_RNC_RESUMING:
  609. /* We are still waiting to post when a resume was
  610. * requested.
  611. */
  612. switch (sci_rnc->destination_state) {
  613. case RNC_DEST_SUSPENDED:
  614. case RNC_DEST_SUSPENDED_RESUME:
  615. /* Previously waiting to suspend after posting.
  616. * Now continue onto resumption.
  617. */
  618. sci_remote_node_context_setup_to_resume(
  619. sci_rnc, cb_fn, cb_p,
  620. RNC_DEST_SUSPENDED_RESUME);
  621. break;
  622. default:
  623. sci_remote_node_context_setup_to_resume(
  624. sci_rnc, cb_fn, cb_p,
  625. RNC_DEST_READY);
  626. break;
  627. }
  628. return SCI_SUCCESS;
  629. case SCI_RNC_TX_SUSPENDED:
  630. case SCI_RNC_TX_RX_SUSPENDED:
  631. {
  632. struct domain_device *dev = idev->domain_dev;
  633. /* If this is an expander attached SATA device we must
  634. * invalidate and repost the RNC since this is the only
  635. * way to clear the TCi to NCQ tag mapping table for
  636. * the RNi. All other device types we can just resume.
  637. */
  638. sci_remote_node_context_setup_to_resume(
  639. sci_rnc, cb_fn, cb_p, RNC_DEST_READY);
  640. if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
  641. if ((dev_is_sata(dev) && dev->parent) ||
  642. (sci_rnc->destination_state == RNC_DEST_FINAL))
  643. sci_change_state(&sci_rnc->sm,
  644. SCI_RNC_INVALIDATING);
  645. else
  646. sci_change_state(&sci_rnc->sm,
  647. SCI_RNC_RESUMING);
  648. }
  649. }
  650. return SCI_SUCCESS;
  651. case SCI_RNC_AWAIT_SUSPENSION:
  652. sci_remote_node_context_setup_to_resume(
  653. sci_rnc, cb_fn, cb_p, RNC_DEST_SUSPENDED_RESUME);
  654. return SCI_SUCCESS;
  655. default:
  656. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  657. "%s: invalid state %s\n", __func__,
  658. rnc_state_name(state));
  659. return SCI_FAILURE_INVALID_STATE;
  660. }
  661. }
  662. enum sci_status sci_remote_node_context_start_io(struct sci_remote_node_context *sci_rnc,
  663. struct isci_request *ireq)
  664. {
  665. enum scis_sds_remote_node_context_states state;
  666. state = sci_rnc->sm.current_state_id;
  667. switch (state) {
  668. case SCI_RNC_READY:
  669. return SCI_SUCCESS;
  670. case SCI_RNC_TX_SUSPENDED:
  671. case SCI_RNC_TX_RX_SUSPENDED:
  672. case SCI_RNC_AWAIT_SUSPENSION:
  673. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  674. "%s: invalid state %s\n", __func__,
  675. rnc_state_name(state));
  676. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  677. default:
  678. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  679. "%s: invalid state %s\n", __func__,
  680. rnc_state_name(state));
  681. return SCI_FAILURE_INVALID_STATE;
  682. }
  683. }
  684. enum sci_status sci_remote_node_context_start_task(
  685. struct sci_remote_node_context *sci_rnc,
  686. struct isci_request *ireq,
  687. scics_sds_remote_node_context_callback cb_fn,
  688. void *cb_p)
  689. {
  690. enum sci_status status = sci_remote_node_context_resume(sci_rnc,
  691. cb_fn, cb_p);
  692. if (status != SCI_SUCCESS)
  693. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  694. "%s: resume failed: %d\n", __func__, status);
  695. return status;
  696. }
  697. int sci_remote_node_context_is_safe_to_abort(
  698. struct sci_remote_node_context *sci_rnc)
  699. {
  700. enum scis_sds_remote_node_context_states state;
  701. state = sci_rnc->sm.current_state_id;
  702. switch (state) {
  703. case SCI_RNC_INVALIDATING:
  704. case SCI_RNC_TX_RX_SUSPENDED:
  705. return 1;
  706. case SCI_RNC_POSTING:
  707. case SCI_RNC_RESUMING:
  708. case SCI_RNC_READY:
  709. case SCI_RNC_TX_SUSPENDED:
  710. case SCI_RNC_AWAIT_SUSPENSION:
  711. case SCI_RNC_INITIAL:
  712. return 0;
  713. default:
  714. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  715. "%s: invalid state %d\n", __func__, state);
  716. return 0;
  717. }
  718. }