remote_node_context.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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_base_state_machine_get_state(&sci_rnc->state_machine);
  84. if (current_state == SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE) {
  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(void *object)
  239. {
  240. struct scic_sds_remote_node_context *rnc = object;
  241. struct sci_base_state_machine *sm = &rnc->state_machine;
  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 == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE) {
  246. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  247. scic_sds_remote_node_context_notify_user(rnc);
  248. }
  249. }
  250. static void scic_sds_remote_node_context_posting_state_enter(void *object)
  251. {
  252. struct scic_sds_remote_node_context *sci_rnc = object;
  253. scic_sds_remote_node_context_validate_context_buffer(sci_rnc);
  254. }
  255. static void scic_sds_remote_node_context_invalidating_state_enter(void *object)
  256. {
  257. struct scic_sds_remote_node_context *rnc = object;
  258. scic_sds_remote_node_context_invalidate_context_buffer(rnc);
  259. }
  260. static void scic_sds_remote_node_context_resuming_state_enter(void *object)
  261. {
  262. struct scic_sds_remote_node_context *rnc = object;
  263. struct scic_sds_remote_device *sci_dev;
  264. struct domain_device *dev;
  265. sci_dev = rnc_to_dev(rnc);
  266. dev = sci_dev_to_domain(sci_dev);
  267. /*
  268. * For direct attached SATA devices we need to clear the TLCR
  269. * NCQ to TCi tag mapping on the phy and in cases where we
  270. * resume because of a target reset we also need to update
  271. * the STPTLDARNI register with the RNi of the device
  272. */
  273. if ((dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) &&
  274. sci_dev->is_direct_attached)
  275. scic_sds_port_setup_transports(sci_dev->owning_port,
  276. rnc->remote_node_index);
  277. scic_sds_remote_device_post_request(sci_dev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
  278. }
  279. static void scic_sds_remote_node_context_ready_state_enter(void *object)
  280. {
  281. struct scic_sds_remote_node_context *rnc = object;
  282. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  283. if (rnc->user_callback)
  284. scic_sds_remote_node_context_notify_user(rnc);
  285. }
  286. static void scic_sds_remote_node_context_tx_suspended_state_enter(void *object)
  287. {
  288. struct scic_sds_remote_node_context *rnc = object;
  289. scic_sds_remote_node_context_continue_state_transitions(rnc);
  290. }
  291. static void scic_sds_remote_node_context_tx_rx_suspended_state_enter(
  292. void *object)
  293. {
  294. struct scic_sds_remote_node_context *rnc = object;
  295. scic_sds_remote_node_context_continue_state_transitions(rnc);
  296. }
  297. static const struct sci_base_state scic_sds_remote_node_context_state_table[] = {
  298. [SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE] = {
  299. .enter_state = scic_sds_remote_node_context_initial_state_enter,
  300. },
  301. [SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE] = {
  302. .enter_state = scic_sds_remote_node_context_posting_state_enter,
  303. },
  304. [SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE] = {
  305. .enter_state = scic_sds_remote_node_context_invalidating_state_enter,
  306. },
  307. [SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE] = {
  308. .enter_state = scic_sds_remote_node_context_resuming_state_enter,
  309. },
  310. [SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE] = {
  311. .enter_state = scic_sds_remote_node_context_ready_state_enter,
  312. },
  313. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE] = {
  314. .enter_state = scic_sds_remote_node_context_tx_suspended_state_enter,
  315. },
  316. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE] = {
  317. .enter_state = scic_sds_remote_node_context_tx_rx_suspended_state_enter,
  318. },
  319. [SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE] = { },
  320. };
  321. void scic_sds_remote_node_context_construct(struct scic_sds_remote_node_context *rnc,
  322. u16 remote_node_index)
  323. {
  324. memset(rnc, 0, sizeof(struct scic_sds_remote_node_context));
  325. rnc->remote_node_index = remote_node_index;
  326. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  327. sci_base_state_machine_construct(
  328. &rnc->state_machine,
  329. rnc,
  330. scic_sds_remote_node_context_state_table,
  331. SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE
  332. );
  333. sci_base_state_machine_start(&rnc->state_machine);
  334. }
  335. enum sci_status scic_sds_remote_node_context_event_handler(struct scic_sds_remote_node_context *sci_rnc,
  336. u32 event_code)
  337. {
  338. enum scis_sds_remote_node_context_states state;
  339. state = sci_rnc->state_machine.current_state_id;
  340. switch (state) {
  341. case SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE:
  342. switch (scu_get_event_code(event_code)) {
  343. case SCU_EVENT_POST_RNC_COMPLETE:
  344. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  345. SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE);
  346. break;
  347. default:
  348. goto out;
  349. }
  350. break;
  351. case SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE:
  352. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
  353. if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
  354. state = SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE;
  355. else
  356. state = SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE;
  357. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  358. state);
  359. } else {
  360. switch (scu_get_event_type(event_code)) {
  361. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  362. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  363. /* We really dont care if the hardware is going to suspend
  364. * the device since it's being invalidated anyway */
  365. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  366. "%s: SCIC Remote Node Context 0x%p was "
  367. "suspeneded by hardware while being "
  368. "invalidated.\n", __func__, sci_rnc);
  369. break;
  370. default:
  371. goto out;
  372. }
  373. }
  374. break;
  375. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  376. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
  377. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  378. SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE);
  379. } else {
  380. switch (scu_get_event_type(event_code)) {
  381. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  382. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  383. /* We really dont care if the hardware is going to suspend
  384. * the device since it's being resumed anyway */
  385. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  386. "%s: SCIC Remote Node Context 0x%p was "
  387. "suspeneded by hardware while being resumed.\n",
  388. __func__, sci_rnc);
  389. break;
  390. default:
  391. goto out;
  392. }
  393. }
  394. break;
  395. case SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE:
  396. switch (scu_get_event_type(event_code)) {
  397. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  398. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  399. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE);
  400. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  401. break;
  402. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  403. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  404. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE);
  405. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  406. break;
  407. default:
  408. goto out;
  409. }
  410. break;
  411. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  412. switch (scu_get_event_type(event_code)) {
  413. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  414. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  415. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE);
  416. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  417. break;
  418. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  419. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  420. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE);
  421. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  422. break;
  423. default:
  424. goto out;
  425. }
  426. break;
  427. default:
  428. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  429. "%s: invalid state %d\n", __func__, state);
  430. return SCI_FAILURE_INVALID_STATE;
  431. }
  432. return SCI_SUCCESS;
  433. out:
  434. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  435. "%s: code: %#x state: %d\n", __func__, event_code, state);
  436. return SCI_FAILURE;
  437. }
  438. enum sci_status scic_sds_remote_node_context_destruct(struct scic_sds_remote_node_context *sci_rnc,
  439. scics_sds_remote_node_context_callback cb_fn,
  440. void *cb_p)
  441. {
  442. enum scis_sds_remote_node_context_states state;
  443. state = sci_rnc->state_machine.current_state_id;
  444. switch (state) {
  445. case SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE:
  446. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  447. return SCI_SUCCESS;
  448. case SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE:
  449. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  450. case SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE:
  451. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE:
  452. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE:
  453. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  454. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  455. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  456. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE);
  457. return SCI_SUCCESS;
  458. case SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE:
  459. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  460. "%s: invalid state %d\n", __func__, state);
  461. /* We have decided that the destruct request on the remote node context
  462. * can not fail since it is either in the initial/destroyed state or is
  463. * can be destroyed.
  464. */
  465. return SCI_SUCCESS;
  466. default:
  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. }
  472. enum sci_status scic_sds_remote_node_context_suspend(struct scic_sds_remote_node_context *sci_rnc,
  473. u32 suspend_type,
  474. scics_sds_remote_node_context_callback cb_fn,
  475. void *cb_p)
  476. {
  477. enum scis_sds_remote_node_context_states state;
  478. state = sci_rnc->state_machine.current_state_id;
  479. if (state != SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE) {
  480. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  481. "%s: invalid state %d\n", __func__, state);
  482. return SCI_FAILURE_INVALID_STATE;
  483. }
  484. sci_rnc->user_callback = cb_fn;
  485. sci_rnc->user_cookie = cb_p;
  486. sci_rnc->suspension_code = suspend_type;
  487. if (suspend_type == SCI_SOFTWARE_SUSPENSION) {
  488. scic_sds_remote_device_post_request(rnc_to_dev(sci_rnc),
  489. SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX);
  490. }
  491. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  492. SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE);
  493. return SCI_SUCCESS;
  494. }
  495. enum sci_status scic_sds_remote_node_context_resume(struct scic_sds_remote_node_context *sci_rnc,
  496. scics_sds_remote_node_context_callback cb_fn,
  497. void *cb_p)
  498. {
  499. enum scis_sds_remote_node_context_states state;
  500. state = sci_rnc->state_machine.current_state_id;
  501. switch (state) {
  502. case SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE:
  503. if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
  504. return SCI_FAILURE_INVALID_STATE;
  505. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  506. scic_sds_remote_node_context_construct_buffer(sci_rnc);
  507. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  508. SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE);
  509. return SCI_SUCCESS;
  510. case SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE:
  511. case SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE:
  512. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  513. if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
  514. return SCI_FAILURE_INVALID_STATE;
  515. sci_rnc->user_callback = cb_fn;
  516. sci_rnc->user_cookie = cb_p;
  517. return SCI_SUCCESS;
  518. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE: {
  519. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  520. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  521. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  522. /* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
  523. if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
  524. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  525. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE);
  526. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  527. if (sci_dev->is_direct_attached) {
  528. /* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
  529. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  530. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE);
  531. } else {
  532. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  533. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE);
  534. }
  535. } else
  536. return SCI_FAILURE;
  537. return SCI_SUCCESS;
  538. }
  539. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE:
  540. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  541. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  542. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE);
  543. return SCI_FAILURE_INVALID_STATE;
  544. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  545. scic_sds_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
  546. return SCI_SUCCESS;
  547. default:
  548. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  549. "%s: invalid state %d\n", __func__, state);
  550. return SCI_FAILURE_INVALID_STATE;
  551. }
  552. }
  553. enum sci_status scic_sds_remote_node_context_start_io(struct scic_sds_remote_node_context *sci_rnc,
  554. struct scic_sds_request *sci_req)
  555. {
  556. enum scis_sds_remote_node_context_states state;
  557. state = sci_rnc->state_machine.current_state_id;
  558. if (state != SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE) {
  559. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  560. "%s: invalid state %d\n", __func__, state);
  561. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  562. }
  563. return SCI_SUCCESS;
  564. }
  565. enum sci_status scic_sds_remote_node_context_start_task(struct scic_sds_remote_node_context *sci_rnc,
  566. struct scic_sds_request *sci_req)
  567. {
  568. enum scis_sds_remote_node_context_states state;
  569. state = sci_rnc->state_machine.current_state_id;
  570. switch (state) {
  571. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  572. case SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE:
  573. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  574. return SCI_SUCCESS;
  575. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE:
  576. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE:
  577. scic_sds_remote_node_context_resume(sci_rnc, NULL, NULL);
  578. return SCI_SUCCESS;
  579. default:
  580. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  581. "%s: invalid state %d\n", __func__, state);
  582. return SCI_FAILURE_INVALID_STATE;
  583. }
  584. }