remote_node_context.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 "host.h"
  56. #include "state_machine.h"
  57. #include "remote_device.h"
  58. #include "remote_node_context.h"
  59. #include "scu_event_codes.h"
  60. #include "scu_task_context.h"
  61. /**
  62. *
  63. * @sci_rnc: The RNC for which the is posted request is being made.
  64. *
  65. * This method will return true if the RNC is not in the initial state. In all
  66. * other states the RNC is considered active and this will return true. The
  67. * destroy request of the state machine drives the RNC back to the initial
  68. * state. If the state machine changes then this routine will also have to be
  69. * changed. bool true if the state machine is not in the initial state false if
  70. * the state machine is in the initial state
  71. */
  72. /**
  73. *
  74. * @sci_rnc: The state of the remote node context object to check.
  75. *
  76. * This method will return true if the remote node context is in a READY state
  77. * otherwise it will return false bool true if the remote node context is in
  78. * the ready state. false if the remote node context is not in the ready state.
  79. */
  80. bool scic_sds_remote_node_context_is_ready(
  81. struct scic_sds_remote_node_context *sci_rnc)
  82. {
  83. u32 current_state = sci_rnc->sm.current_state_id;
  84. if (current_state == SCI_RNC_READY) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. *
  91. * @sci_dev: The remote device to use to construct the RNC buffer.
  92. * @rnc: The buffer into which the remote device data will be copied.
  93. *
  94. * This method will construct the RNC buffer for this remote device object. none
  95. */
  96. static void scic_sds_remote_node_context_construct_buffer(
  97. struct scic_sds_remote_node_context *sci_rnc)
  98. {
  99. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  100. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  101. int rni = sci_rnc->remote_node_index;
  102. union scu_remote_node_context *rnc;
  103. struct scic_sds_controller *scic;
  104. __le64 sas_addr;
  105. scic = scic_sds_remote_device_get_controller(sci_dev);
  106. rnc = scic_sds_controller_get_remote_node_context_buffer(scic, rni);
  107. memset(rnc, 0, sizeof(union scu_remote_node_context)
  108. * scic_sds_remote_device_node_count(sci_dev));
  109. rnc->ssp.remote_node_index = rni;
  110. rnc->ssp.remote_node_port_width = sci_dev->device_port_width;
  111. rnc->ssp.logical_port_index = sci_dev->owning_port->physical_port_index;
  112. /* sas address is __be64, context ram format is __le64 */
  113. sas_addr = cpu_to_le64(SAS_ADDR(dev->sas_addr));
  114. rnc->ssp.remote_sas_address_hi = upper_32_bits(sas_addr);
  115. rnc->ssp.remote_sas_address_lo = lower_32_bits(sas_addr);
  116. rnc->ssp.nexus_loss_timer_enable = true;
  117. rnc->ssp.check_bit = false;
  118. rnc->ssp.is_valid = false;
  119. rnc->ssp.is_remote_node_context = true;
  120. rnc->ssp.function_number = 0;
  121. rnc->ssp.arbitration_wait_time = 0;
  122. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  123. rnc->ssp.connection_occupancy_timeout =
  124. scic->user_parameters.sds1.stp_max_occupancy_timeout;
  125. rnc->ssp.connection_inactivity_timeout =
  126. scic->user_parameters.sds1.stp_inactivity_timeout;
  127. } else {
  128. rnc->ssp.connection_occupancy_timeout =
  129. scic->user_parameters.sds1.ssp_max_occupancy_timeout;
  130. rnc->ssp.connection_inactivity_timeout =
  131. scic->user_parameters.sds1.ssp_inactivity_timeout;
  132. }
  133. rnc->ssp.initial_arbitration_wait_time = 0;
  134. /* Open Address Frame Parameters */
  135. rnc->ssp.oaf_connection_rate = sci_dev->connection_rate;
  136. rnc->ssp.oaf_features = 0;
  137. rnc->ssp.oaf_source_zone_group = 0;
  138. rnc->ssp.oaf_more_compatibility_features = 0;
  139. }
  140. /**
  141. *
  142. * @sci_rnc:
  143. * @callback:
  144. * @callback_parameter:
  145. *
  146. * This method will setup the remote node context object so it will transition
  147. * to its ready state. If the remote node context is already setup to
  148. * transition to its final state then this function does nothing. none
  149. */
  150. static void scic_sds_remote_node_context_setup_to_resume(
  151. struct scic_sds_remote_node_context *sci_rnc,
  152. scics_sds_remote_node_context_callback callback,
  153. void *callback_parameter)
  154. {
  155. if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL) {
  156. sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY;
  157. sci_rnc->user_callback = callback;
  158. sci_rnc->user_cookie = callback_parameter;
  159. }
  160. }
  161. static void scic_sds_remote_node_context_setup_to_destory(
  162. struct scic_sds_remote_node_context *sci_rnc,
  163. scics_sds_remote_node_context_callback callback,
  164. void *callback_parameter)
  165. {
  166. sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL;
  167. sci_rnc->user_callback = callback;
  168. sci_rnc->user_cookie = callback_parameter;
  169. }
  170. /**
  171. *
  172. *
  173. * This method just calls the user callback function and then resets the
  174. * callback.
  175. */
  176. static void scic_sds_remote_node_context_notify_user(
  177. struct scic_sds_remote_node_context *rnc)
  178. {
  179. if (rnc->user_callback != NULL) {
  180. (*rnc->user_callback)(rnc->user_cookie);
  181. rnc->user_callback = NULL;
  182. rnc->user_cookie = NULL;
  183. }
  184. }
  185. static void scic_sds_remote_node_context_continue_state_transitions(struct scic_sds_remote_node_context *rnc)
  186. {
  187. if (rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
  188. scic_sds_remote_node_context_resume(rnc, rnc->user_callback,
  189. rnc->user_cookie);
  190. }
  191. /**
  192. *
  193. * @sci_rnc: The remote node context object that is to be validated.
  194. *
  195. * This method will mark the rnc buffer as being valid and post the request to
  196. * the hardware. none
  197. */
  198. static void scic_sds_remote_node_context_validate_context_buffer(
  199. struct scic_sds_remote_node_context *sci_rnc)
  200. {
  201. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  202. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  203. union scu_remote_node_context *rnc_buffer;
  204. rnc_buffer = scic_sds_controller_get_remote_node_context_buffer(
  205. scic_sds_remote_device_get_controller(sci_dev),
  206. sci_rnc->remote_node_index
  207. );
  208. rnc_buffer->ssp.is_valid = true;
  209. if (!sci_dev->is_direct_attached &&
  210. (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))) {
  211. scic_sds_remote_device_post_request(sci_dev,
  212. SCU_CONTEXT_COMMAND_POST_RNC_96);
  213. } else {
  214. scic_sds_remote_device_post_request(sci_dev, SCU_CONTEXT_COMMAND_POST_RNC_32);
  215. if (sci_dev->is_direct_attached) {
  216. scic_sds_port_setup_transports(sci_dev->owning_port,
  217. sci_rnc->remote_node_index);
  218. }
  219. }
  220. }
  221. /**
  222. *
  223. * @sci_rnc: The remote node context object that is to be invalidated.
  224. *
  225. * This method will update the RNC buffer and post the invalidate request. none
  226. */
  227. static void scic_sds_remote_node_context_invalidate_context_buffer(
  228. struct scic_sds_remote_node_context *sci_rnc)
  229. {
  230. union scu_remote_node_context *rnc_buffer;
  231. rnc_buffer = scic_sds_controller_get_remote_node_context_buffer(
  232. scic_sds_remote_device_get_controller(rnc_to_dev(sci_rnc)),
  233. sci_rnc->remote_node_index);
  234. rnc_buffer->ssp.is_valid = false;
  235. scic_sds_remote_device_post_request(rnc_to_dev(sci_rnc),
  236. SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
  237. }
  238. static void scic_sds_remote_node_context_initial_state_enter(struct sci_base_state_machine *sm)
  239. {
  240. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  241. /* Check to see if we have gotten back to the initial state because
  242. * someone requested to destroy the remote node context object.
  243. */
  244. if (sm->previous_state_id == SCI_RNC_INVALIDATING) {
  245. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  246. scic_sds_remote_node_context_notify_user(rnc);
  247. }
  248. }
  249. static void scic_sds_remote_node_context_posting_state_enter(struct sci_base_state_machine *sm)
  250. {
  251. struct scic_sds_remote_node_context *sci_rnc = container_of(sm, typeof(*sci_rnc), sm);
  252. scic_sds_remote_node_context_validate_context_buffer(sci_rnc);
  253. }
  254. static void scic_sds_remote_node_context_invalidating_state_enter(struct sci_base_state_machine *sm)
  255. {
  256. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  257. scic_sds_remote_node_context_invalidate_context_buffer(rnc);
  258. }
  259. static void scic_sds_remote_node_context_resuming_state_enter(struct sci_base_state_machine *sm)
  260. {
  261. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  262. struct scic_sds_remote_device *sci_dev;
  263. struct domain_device *dev;
  264. sci_dev = rnc_to_dev(rnc);
  265. dev = sci_dev_to_domain(sci_dev);
  266. /*
  267. * For direct attached SATA devices we need to clear the TLCR
  268. * NCQ to TCi tag mapping on the phy and in cases where we
  269. * resume because of a target reset we also need to update
  270. * the STPTLDARNI register with the RNi of the device
  271. */
  272. if ((dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) &&
  273. sci_dev->is_direct_attached)
  274. scic_sds_port_setup_transports(sci_dev->owning_port,
  275. rnc->remote_node_index);
  276. scic_sds_remote_device_post_request(sci_dev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
  277. }
  278. static void scic_sds_remote_node_context_ready_state_enter(struct sci_base_state_machine *sm)
  279. {
  280. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  281. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  282. if (rnc->user_callback)
  283. scic_sds_remote_node_context_notify_user(rnc);
  284. }
  285. static void scic_sds_remote_node_context_tx_suspended_state_enter(struct sci_base_state_machine *sm)
  286. {
  287. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  288. scic_sds_remote_node_context_continue_state_transitions(rnc);
  289. }
  290. static void scic_sds_remote_node_context_tx_rx_suspended_state_enter(struct sci_base_state_machine *sm)
  291. {
  292. struct scic_sds_remote_node_context *rnc = container_of(sm, typeof(*rnc), sm);
  293. scic_sds_remote_node_context_continue_state_transitions(rnc);
  294. }
  295. static const struct sci_base_state scic_sds_remote_node_context_state_table[] = {
  296. [SCI_RNC_INITIAL] = {
  297. .enter_state = scic_sds_remote_node_context_initial_state_enter,
  298. },
  299. [SCI_RNC_POSTING] = {
  300. .enter_state = scic_sds_remote_node_context_posting_state_enter,
  301. },
  302. [SCI_RNC_INVALIDATING] = {
  303. .enter_state = scic_sds_remote_node_context_invalidating_state_enter,
  304. },
  305. [SCI_RNC_RESUMING] = {
  306. .enter_state = scic_sds_remote_node_context_resuming_state_enter,
  307. },
  308. [SCI_RNC_READY] = {
  309. .enter_state = scic_sds_remote_node_context_ready_state_enter,
  310. },
  311. [SCI_RNC_TX_SUSPENDED] = {
  312. .enter_state = scic_sds_remote_node_context_tx_suspended_state_enter,
  313. },
  314. [SCI_RNC_TX_RX_SUSPENDED] = {
  315. .enter_state = scic_sds_remote_node_context_tx_rx_suspended_state_enter,
  316. },
  317. [SCI_RNC_AWAIT_SUSPENSION] = { },
  318. };
  319. void scic_sds_remote_node_context_construct(struct scic_sds_remote_node_context *rnc,
  320. u16 remote_node_index)
  321. {
  322. memset(rnc, 0, sizeof(struct scic_sds_remote_node_context));
  323. rnc->remote_node_index = remote_node_index;
  324. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  325. sci_base_state_machine_construct(&rnc->sm,
  326. scic_sds_remote_node_context_state_table,
  327. SCI_RNC_INITIAL);
  328. sci_base_state_machine_start(&rnc->sm);
  329. }
  330. enum sci_status scic_sds_remote_node_context_event_handler(struct scic_sds_remote_node_context *sci_rnc,
  331. u32 event_code)
  332. {
  333. enum scis_sds_remote_node_context_states state;
  334. state = sci_rnc->sm.current_state_id;
  335. switch (state) {
  336. case SCI_RNC_POSTING:
  337. switch (scu_get_event_code(event_code)) {
  338. case SCU_EVENT_POST_RNC_COMPLETE:
  339. sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
  340. break;
  341. default:
  342. goto out;
  343. }
  344. break;
  345. case SCI_RNC_INVALIDATING:
  346. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
  347. if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
  348. state = SCI_RNC_INITIAL;
  349. else
  350. state = SCI_RNC_POSTING;
  351. sci_change_state(&sci_rnc->sm, state);
  352. } else {
  353. switch (scu_get_event_type(event_code)) {
  354. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  355. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  356. /* We really dont care if the hardware is going to suspend
  357. * the device since it's being invalidated anyway */
  358. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  359. "%s: SCIC Remote Node Context 0x%p was "
  360. "suspeneded by hardware while being "
  361. "invalidated.\n", __func__, sci_rnc);
  362. break;
  363. default:
  364. goto out;
  365. }
  366. }
  367. break;
  368. case SCI_RNC_RESUMING:
  369. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
  370. sci_change_state(&sci_rnc->sm, SCI_RNC_READY);
  371. } else {
  372. switch (scu_get_event_type(event_code)) {
  373. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  374. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  375. /* We really dont care if the hardware is going to suspend
  376. * the device since it's being resumed anyway */
  377. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  378. "%s: SCIC Remote Node Context 0x%p was "
  379. "suspeneded by hardware while being resumed.\n",
  380. __func__, sci_rnc);
  381. break;
  382. default:
  383. goto out;
  384. }
  385. }
  386. break;
  387. case SCI_RNC_READY:
  388. switch (scu_get_event_type(event_code)) {
  389. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  390. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
  391. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  392. break;
  393. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  394. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
  395. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  396. break;
  397. default:
  398. goto out;
  399. }
  400. break;
  401. case SCI_RNC_AWAIT_SUSPENSION:
  402. switch (scu_get_event_type(event_code)) {
  403. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  404. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_SUSPENDED);
  405. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  406. break;
  407. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  408. sci_change_state(&sci_rnc->sm, SCI_RNC_TX_RX_SUSPENDED);
  409. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  410. break;
  411. default:
  412. goto out;
  413. }
  414. break;
  415. default:
  416. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  417. "%s: invalid state %d\n", __func__, state);
  418. return SCI_FAILURE_INVALID_STATE;
  419. }
  420. return SCI_SUCCESS;
  421. out:
  422. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  423. "%s: code: %#x state: %d\n", __func__, event_code, state);
  424. return SCI_FAILURE;
  425. }
  426. enum sci_status scic_sds_remote_node_context_destruct(struct scic_sds_remote_node_context *sci_rnc,
  427. scics_sds_remote_node_context_callback cb_fn,
  428. void *cb_p)
  429. {
  430. enum scis_sds_remote_node_context_states state;
  431. state = sci_rnc->sm.current_state_id;
  432. switch (state) {
  433. case SCI_RNC_INVALIDATING:
  434. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  435. return SCI_SUCCESS;
  436. case SCI_RNC_POSTING:
  437. case SCI_RNC_RESUMING:
  438. case SCI_RNC_READY:
  439. case SCI_RNC_TX_SUSPENDED:
  440. case SCI_RNC_TX_RX_SUSPENDED:
  441. case SCI_RNC_AWAIT_SUSPENSION:
  442. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  443. sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
  444. return SCI_SUCCESS;
  445. case SCI_RNC_INITIAL:
  446. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  447. "%s: invalid state %d\n", __func__, state);
  448. /* We have decided that the destruct request on the remote node context
  449. * can not fail since it is either in the initial/destroyed state or is
  450. * can be destroyed.
  451. */
  452. return SCI_SUCCESS;
  453. default:
  454. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  455. "%s: invalid state %d\n", __func__, state);
  456. return SCI_FAILURE_INVALID_STATE;
  457. }
  458. }
  459. enum sci_status scic_sds_remote_node_context_suspend(struct scic_sds_remote_node_context *sci_rnc,
  460. u32 suspend_type,
  461. scics_sds_remote_node_context_callback cb_fn,
  462. void *cb_p)
  463. {
  464. enum scis_sds_remote_node_context_states state;
  465. state = sci_rnc->sm.current_state_id;
  466. if (state != SCI_RNC_READY) {
  467. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  468. "%s: invalid state %d\n", __func__, state);
  469. return SCI_FAILURE_INVALID_STATE;
  470. }
  471. sci_rnc->user_callback = cb_fn;
  472. sci_rnc->user_cookie = cb_p;
  473. sci_rnc->suspension_code = suspend_type;
  474. if (suspend_type == SCI_SOFTWARE_SUSPENSION) {
  475. scic_sds_remote_device_post_request(rnc_to_dev(sci_rnc),
  476. SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX);
  477. }
  478. sci_change_state(&sci_rnc->sm, SCI_RNC_AWAIT_SUSPENSION);
  479. return SCI_SUCCESS;
  480. }
  481. enum sci_status scic_sds_remote_node_context_resume(struct scic_sds_remote_node_context *sci_rnc,
  482. scics_sds_remote_node_context_callback cb_fn,
  483. void *cb_p)
  484. {
  485. enum scis_sds_remote_node_context_states state;
  486. state = sci_rnc->sm.current_state_id;
  487. switch (state) {
  488. case SCI_RNC_INITIAL:
  489. if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  490. return SCI_FAILURE_INVALID_STATE;
  491. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  492. scic_sds_remote_node_context_construct_buffer(sci_rnc);
  493. sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
  494. return SCI_SUCCESS;
  495. case SCI_RNC_POSTING:
  496. case SCI_RNC_INVALIDATING:
  497. case SCI_RNC_RESUMING:
  498. if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
  499. return SCI_FAILURE_INVALID_STATE;
  500. sci_rnc->user_callback = cb_fn;
  501. sci_rnc->user_cookie = cb_p;
  502. return SCI_SUCCESS;
  503. case SCI_RNC_TX_SUSPENDED: {
  504. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  505. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  506. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  507. /* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
  508. if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
  509. sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
  510. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  511. if (sci_dev->is_direct_attached) {
  512. /* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
  513. sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
  514. } else {
  515. sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
  516. }
  517. } else
  518. return SCI_FAILURE;
  519. return SCI_SUCCESS;
  520. }
  521. case SCI_RNC_TX_RX_SUSPENDED:
  522. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  523. sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
  524. return SCI_FAILURE_INVALID_STATE;
  525. case SCI_RNC_AWAIT_SUSPENSION:
  526. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  527. return SCI_SUCCESS;
  528. default:
  529. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  530. "%s: invalid state %d\n", __func__, state);
  531. return SCI_FAILURE_INVALID_STATE;
  532. }
  533. }
  534. enum sci_status scic_sds_remote_node_context_start_io(struct scic_sds_remote_node_context *sci_rnc,
  535. struct scic_sds_request *sci_req)
  536. {
  537. enum scis_sds_remote_node_context_states state;
  538. state = sci_rnc->sm.current_state_id;
  539. if (state != SCI_RNC_READY) {
  540. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  541. "%s: invalid state %d\n", __func__, state);
  542. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  543. }
  544. return SCI_SUCCESS;
  545. }
  546. enum sci_status scic_sds_remote_node_context_start_task(struct scic_sds_remote_node_context *sci_rnc,
  547. struct scic_sds_request *sci_req)
  548. {
  549. enum scis_sds_remote_node_context_states state;
  550. state = sci_rnc->sm.current_state_id;
  551. switch (state) {
  552. case SCI_RNC_RESUMING:
  553. case SCI_RNC_READY:
  554. case SCI_RNC_AWAIT_SUSPENSION:
  555. return SCI_SUCCESS;
  556. case SCI_RNC_TX_SUSPENDED:
  557. case SCI_RNC_TX_RX_SUSPENDED:
  558. scic_sds_remote_node_context_resume(sci_rnc, NULL, NULL);
  559. return SCI_SUCCESS;
  560. default:
  561. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  562. "%s: invalid state %d\n", __func__, state);
  563. return SCI_FAILURE_INVALID_STATE;
  564. }
  565. }