remote_node_context.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. /**
  162. *
  163. * @sci_rnc:
  164. * @callback:
  165. * @callback_parameter:
  166. *
  167. * This method will setup the remote node context object so it will transistion
  168. * to its final state. none
  169. */
  170. static void scic_sds_remote_node_context_setup_to_destory(
  171. struct scic_sds_remote_node_context *sci_rnc,
  172. scics_sds_remote_node_context_callback callback,
  173. void *callback_parameter)
  174. {
  175. sci_rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL;
  176. sci_rnc->user_callback = callback;
  177. sci_rnc->user_cookie = callback_parameter;
  178. }
  179. /**
  180. *
  181. * @sci_rnc:
  182. * @callback:
  183. *
  184. * This method will continue to resume a remote node context. This is used in
  185. * the states where a resume is requested while a resume is in progress.
  186. */
  187. static enum sci_status scic_sds_remote_node_context_continue_to_resume_handler(
  188. struct scic_sds_remote_node_context *sci_rnc,
  189. scics_sds_remote_node_context_callback callback,
  190. void *callback_parameter)
  191. {
  192. if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY) {
  193. sci_rnc->user_callback = callback;
  194. sci_rnc->user_cookie = callback_parameter;
  195. return SCI_SUCCESS;
  196. }
  197. return SCI_FAILURE_INVALID_STATE;
  198. }
  199. static enum sci_status scic_sds_remote_node_context_default_suspend_handler(
  200. struct scic_sds_remote_node_context *sci_rnc,
  201. u32 suspend_type,
  202. scics_sds_remote_node_context_callback callback,
  203. void *callback_parameter)
  204. {
  205. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  206. "%s: SCIC Remote Node Context 0x%p requested to suspend "
  207. "while in wrong state %d\n",
  208. __func__,
  209. sci_rnc,
  210. sci_base_state_machine_get_state(&sci_rnc->state_machine));
  211. return SCI_FAILURE_INVALID_STATE;
  212. }
  213. static enum sci_status scic_sds_remote_node_context_default_resume_handler(
  214. struct scic_sds_remote_node_context *sci_rnc,
  215. scics_sds_remote_node_context_callback callback,
  216. void *callback_parameter)
  217. {
  218. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  219. "%s: SCIC Remote Node Context 0x%p requested to resume "
  220. "while in wrong state %d\n",
  221. __func__,
  222. sci_rnc,
  223. sci_base_state_machine_get_state(&sci_rnc->state_machine));
  224. return SCI_FAILURE_INVALID_STATE;
  225. }
  226. static enum sci_status scic_sds_remote_node_context_default_start_io_handler(
  227. struct scic_sds_remote_node_context *sci_rnc,
  228. struct scic_sds_request *sci_req)
  229. {
  230. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  231. "%s: SCIC Remote Node Context 0x%p requested to start io "
  232. "0x%p while in wrong state %d\n",
  233. __func__,
  234. sci_rnc,
  235. sci_req,
  236. sci_base_state_machine_get_state(&sci_rnc->state_machine));
  237. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  238. }
  239. static enum sci_status scic_sds_remote_node_context_default_start_task_handler(
  240. struct scic_sds_remote_node_context *sci_rnc,
  241. struct scic_sds_request *sci_req)
  242. {
  243. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  244. "%s: SCIC Remote Node Context 0x%p requested to start "
  245. "task 0x%p while in wrong state %d\n",
  246. __func__,
  247. sci_rnc,
  248. sci_req,
  249. sci_base_state_machine_get_state(&sci_rnc->state_machine));
  250. return SCI_FAILURE;
  251. }
  252. /**
  253. *
  254. * @sci_rnc: The rnc for which the task request is targeted.
  255. * @sci_req: The request which is going to be started.
  256. *
  257. * This method determines if the task request can be started by the SCU
  258. * hardware. When the RNC is in the ready state any task can be started.
  259. * enum sci_status SCI_SUCCESS
  260. */
  261. static enum sci_status scic_sds_remote_node_context_success_start_task_handler(
  262. struct scic_sds_remote_node_context *sci_rnc,
  263. struct scic_sds_request *sci_req)
  264. {
  265. return SCI_SUCCESS;
  266. }
  267. static enum sci_status scic_sds_remote_node_context_initial_state_resume_handler(
  268. struct scic_sds_remote_node_context *sci_rnc,
  269. scics_sds_remote_node_context_callback callback,
  270. void *callback_parameter)
  271. {
  272. if (sci_rnc->remote_node_index != SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX) {
  273. scic_sds_remote_node_context_setup_to_resume(
  274. sci_rnc, callback, callback_parameter
  275. );
  276. scic_sds_remote_node_context_construct_buffer(sci_rnc);
  277. sci_base_state_machine_change_state(
  278. &sci_rnc->state_machine,
  279. SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE
  280. );
  281. return SCI_SUCCESS;
  282. }
  283. return SCI_FAILURE_INVALID_STATE;
  284. }
  285. /**
  286. *
  287. * @sci_rnc: The remote node context object being suspended.
  288. * @callback: The callback when the suspension is complete.
  289. * @callback_parameter: The parameter that is to be passed into the callback.
  290. *
  291. * This method will handle the suspend requests from the ready state.
  292. * SCI_SUCCESS
  293. */
  294. static enum sci_status scic_sds_remote_node_context_ready_state_suspend_handler(
  295. struct scic_sds_remote_node_context *sci_rnc,
  296. u32 suspend_type,
  297. scics_sds_remote_node_context_callback callback,
  298. void *callback_parameter)
  299. {
  300. sci_rnc->user_callback = callback;
  301. sci_rnc->user_cookie = callback_parameter;
  302. sci_rnc->suspension_code = suspend_type;
  303. if (suspend_type == SCI_SOFTWARE_SUSPENSION) {
  304. scic_sds_remote_device_post_request(rnc_to_dev(sci_rnc),
  305. SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX);
  306. }
  307. sci_base_state_machine_change_state(
  308. &sci_rnc->state_machine,
  309. SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE
  310. );
  311. return SCI_SUCCESS;
  312. }
  313. /**
  314. *
  315. * @sci_rnc: The rnc for which the io request is targeted.
  316. * @sci_req: The request which is going to be started.
  317. *
  318. * This method determines if the io request can be started by the SCU hardware.
  319. * When the RNC is in the ready state any io request can be started. enum sci_status
  320. * SCI_SUCCESS
  321. */
  322. static enum sci_status scic_sds_remote_node_context_ready_state_start_io_handler(
  323. struct scic_sds_remote_node_context *sci_rnc,
  324. struct scic_sds_request *sci_req)
  325. {
  326. return SCI_SUCCESS;
  327. }
  328. static enum sci_status scic_sds_remote_node_context_tx_suspended_state_resume_handler(
  329. struct scic_sds_remote_node_context *sci_rnc,
  330. scics_sds_remote_node_context_callback callback,
  331. void *callback_parameter)
  332. {
  333. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  334. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  335. enum sci_status status = SCI_SUCCESS;
  336. scic_sds_remote_node_context_setup_to_resume(sci_rnc, callback,
  337. callback_parameter);
  338. /* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
  339. if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
  340. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  341. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE);
  342. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  343. if (sci_dev->is_direct_attached) {
  344. /* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
  345. sci_base_state_machine_change_state(
  346. &sci_rnc->state_machine,
  347. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE);
  348. } else {
  349. sci_base_state_machine_change_state(
  350. &sci_rnc->state_machine,
  351. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE);
  352. }
  353. } else
  354. status = SCI_FAILURE;
  355. return status;
  356. }
  357. /**
  358. *
  359. * @sci_rnc: The remote node context which is to receive the task request.
  360. * @sci_req: The task request to be transmitted to to the remote target
  361. * device.
  362. *
  363. * This method will report a success or failure attempt to start a new task
  364. * request to the hardware. Since all task requests are sent on the high
  365. * priority queue they can be sent when the RCN is in a TX suspend state.
  366. * enum sci_status SCI_SUCCESS
  367. */
  368. static enum sci_status scic_sds_remote_node_context_suspended_start_task_handler(
  369. struct scic_sds_remote_node_context *sci_rnc,
  370. struct scic_sds_request *sci_req)
  371. {
  372. scic_sds_remote_node_context_resume(sci_rnc, NULL, NULL);
  373. return SCI_SUCCESS;
  374. }
  375. /* --------------------------------------------------------------------------- */
  376. static enum sci_status scic_sds_remote_node_context_tx_rx_suspended_state_resume_handler(
  377. struct scic_sds_remote_node_context *sci_rnc,
  378. scics_sds_remote_node_context_callback callback,
  379. void *callback_parameter)
  380. {
  381. scic_sds_remote_node_context_setup_to_resume(
  382. sci_rnc, callback, callback_parameter
  383. );
  384. sci_base_state_machine_change_state(
  385. &sci_rnc->state_machine,
  386. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE
  387. );
  388. return SCI_FAILURE_INVALID_STATE;
  389. }
  390. /* --------------------------------------------------------------------------- */
  391. /**
  392. *
  393. *
  394. *
  395. */
  396. static enum sci_status scic_sds_remote_node_context_await_suspension_state_resume_handler(
  397. struct scic_sds_remote_node_context *sci_rnc,
  398. scics_sds_remote_node_context_callback callback,
  399. void *callback_parameter)
  400. {
  401. scic_sds_remote_node_context_setup_to_resume(
  402. sci_rnc, callback, callback_parameter
  403. );
  404. return SCI_SUCCESS;
  405. }
  406. /**
  407. *
  408. * @sci_rnc: The remote node context which is to receive the task request.
  409. * @sci_req: The task request to be transmitted to to the remote target
  410. * device.
  411. *
  412. * This method will report a success or failure attempt to start a new task
  413. * request to the hardware. Since all task requests are sent on the high
  414. * priority queue they can be sent when the RCN is in a TX suspend state.
  415. * enum sci_status SCI_SUCCESS
  416. */
  417. static enum sci_status scic_sds_remote_node_context_await_suspension_state_start_task_handler(
  418. struct scic_sds_remote_node_context *sci_rnc,
  419. struct scic_sds_request *sci_req)
  420. {
  421. return SCI_SUCCESS;
  422. }
  423. static struct scic_sds_remote_node_context_handlers scic_sds_remote_node_context_state_handler_table[] = {
  424. [SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE] = {
  425. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  426. .resume_handler = scic_sds_remote_node_context_initial_state_resume_handler,
  427. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  428. .start_task_handler = scic_sds_remote_node_context_default_start_task_handler,
  429. },
  430. [SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE] = {
  431. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  432. .resume_handler = scic_sds_remote_node_context_continue_to_resume_handler,
  433. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  434. .start_task_handler = scic_sds_remote_node_context_default_start_task_handler,
  435. },
  436. [SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE] = {
  437. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  438. .resume_handler = scic_sds_remote_node_context_continue_to_resume_handler,
  439. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  440. .start_task_handler = scic_sds_remote_node_context_default_start_task_handler,
  441. },
  442. [SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE] = {
  443. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  444. .resume_handler = scic_sds_remote_node_context_continue_to_resume_handler,
  445. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  446. .start_task_handler = scic_sds_remote_node_context_success_start_task_handler,
  447. },
  448. [SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE] = {
  449. .suspend_handler = scic_sds_remote_node_context_ready_state_suspend_handler,
  450. .resume_handler = scic_sds_remote_node_context_default_resume_handler,
  451. .start_io_handler = scic_sds_remote_node_context_ready_state_start_io_handler,
  452. .start_task_handler = scic_sds_remote_node_context_success_start_task_handler,
  453. },
  454. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE] = {
  455. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  456. .resume_handler = scic_sds_remote_node_context_tx_suspended_state_resume_handler,
  457. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  458. .start_task_handler = scic_sds_remote_node_context_suspended_start_task_handler,
  459. },
  460. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE] = {
  461. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  462. .resume_handler = scic_sds_remote_node_context_tx_rx_suspended_state_resume_handler,
  463. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  464. .start_task_handler = scic_sds_remote_node_context_suspended_start_task_handler,
  465. },
  466. [SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE] = {
  467. .suspend_handler = scic_sds_remote_node_context_default_suspend_handler,
  468. .resume_handler = scic_sds_remote_node_context_await_suspension_state_resume_handler,
  469. .start_io_handler = scic_sds_remote_node_context_default_start_io_handler,
  470. .start_task_handler = scic_sds_remote_node_context_await_suspension_state_start_task_handler,
  471. }
  472. };
  473. /*
  474. * *****************************************************************************
  475. * * REMOTE NODE CONTEXT PRIVATE METHODS
  476. * ***************************************************************************** */
  477. /**
  478. *
  479. *
  480. * This method just calls the user callback function and then resets the
  481. * callback.
  482. */
  483. static void scic_sds_remote_node_context_notify_user(
  484. struct scic_sds_remote_node_context *rnc)
  485. {
  486. if (rnc->user_callback != NULL) {
  487. (*rnc->user_callback)(rnc->user_cookie);
  488. rnc->user_callback = NULL;
  489. rnc->user_cookie = NULL;
  490. }
  491. }
  492. /**
  493. *
  494. *
  495. * This method will continue the remote node context state machine by
  496. * requesting to resume the remote node context state machine from its current
  497. * state.
  498. */
  499. static void scic_sds_remote_node_context_continue_state_transitions(
  500. struct scic_sds_remote_node_context *rnc)
  501. {
  502. if (rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY) {
  503. rnc->state_handlers->resume_handler(
  504. rnc, rnc->user_callback, rnc->user_cookie
  505. );
  506. }
  507. }
  508. /**
  509. *
  510. * @sci_rnc: The remote node context object that is to be validated.
  511. *
  512. * This method will mark the rnc buffer as being valid and post the request to
  513. * the hardware. none
  514. */
  515. static void scic_sds_remote_node_context_validate_context_buffer(
  516. struct scic_sds_remote_node_context *sci_rnc)
  517. {
  518. struct scic_sds_remote_device *sci_dev = rnc_to_dev(sci_rnc);
  519. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  520. union scu_remote_node_context *rnc_buffer;
  521. rnc_buffer = scic_sds_controller_get_remote_node_context_buffer(
  522. scic_sds_remote_device_get_controller(sci_dev),
  523. sci_rnc->remote_node_index
  524. );
  525. rnc_buffer->ssp.is_valid = true;
  526. if (!sci_dev->is_direct_attached &&
  527. (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))) {
  528. scic_sds_remote_device_post_request(sci_dev,
  529. SCU_CONTEXT_COMMAND_POST_RNC_96);
  530. } else {
  531. scic_sds_remote_device_post_request(sci_dev, SCU_CONTEXT_COMMAND_POST_RNC_32);
  532. if (sci_dev->is_direct_attached) {
  533. scic_sds_port_setup_transports(sci_dev->owning_port,
  534. sci_rnc->remote_node_index);
  535. }
  536. }
  537. }
  538. /**
  539. *
  540. * @sci_rnc: The remote node context object that is to be invalidated.
  541. *
  542. * This method will update the RNC buffer and post the invalidate request. none
  543. */
  544. static void scic_sds_remote_node_context_invalidate_context_buffer(
  545. struct scic_sds_remote_node_context *sci_rnc)
  546. {
  547. union scu_remote_node_context *rnc_buffer;
  548. rnc_buffer = scic_sds_controller_get_remote_node_context_buffer(
  549. scic_sds_remote_device_get_controller(rnc_to_dev(sci_rnc)),
  550. sci_rnc->remote_node_index);
  551. rnc_buffer->ssp.is_valid = false;
  552. scic_sds_remote_device_post_request(rnc_to_dev(sci_rnc),
  553. SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE);
  554. }
  555. /*
  556. * *****************************************************************************
  557. * * REMOTE NODE CONTEXT STATE ENTER AND EXIT METHODS
  558. * ***************************************************************************** */
  559. /**
  560. *
  561. *
  562. *
  563. */
  564. static void scic_sds_remote_node_context_initial_state_enter(void *object)
  565. {
  566. struct scic_sds_remote_node_context *rnc = object;
  567. SET_STATE_HANDLER(
  568. rnc,
  569. scic_sds_remote_node_context_state_handler_table,
  570. SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE
  571. );
  572. /*
  573. * Check to see if we have gotten back to the initial state because someone
  574. * requested to destroy the remote node context object. */
  575. if (
  576. rnc->state_machine.previous_state_id
  577. == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE
  578. ) {
  579. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  580. scic_sds_remote_node_context_notify_user(rnc);
  581. }
  582. }
  583. /**
  584. *
  585. *
  586. *
  587. */
  588. static void scic_sds_remote_node_context_posting_state_enter(void *object)
  589. {
  590. struct scic_sds_remote_node_context *sci_rnc = object;
  591. SET_STATE_HANDLER(
  592. sci_rnc,
  593. scic_sds_remote_node_context_state_handler_table,
  594. SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE
  595. );
  596. scic_sds_remote_node_context_validate_context_buffer(sci_rnc);
  597. }
  598. /**
  599. *
  600. *
  601. *
  602. */
  603. static void scic_sds_remote_node_context_invalidating_state_enter(void *object)
  604. {
  605. struct scic_sds_remote_node_context *rnc = object;
  606. SET_STATE_HANDLER(
  607. rnc,
  608. scic_sds_remote_node_context_state_handler_table,
  609. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE
  610. );
  611. scic_sds_remote_node_context_invalidate_context_buffer(rnc);
  612. }
  613. /**
  614. *
  615. *
  616. *
  617. */
  618. static void scic_sds_remote_node_context_resuming_state_enter(void *object)
  619. {
  620. struct scic_sds_remote_node_context *rnc = object;
  621. struct scic_sds_remote_device *sci_dev;
  622. struct domain_device *dev;
  623. sci_dev = rnc_to_dev(rnc);
  624. dev = sci_dev_to_domain(sci_dev);
  625. SET_STATE_HANDLER(
  626. rnc,
  627. scic_sds_remote_node_context_state_handler_table,
  628. SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE
  629. );
  630. /*
  631. * For direct attached SATA devices we need to clear the TLCR
  632. * NCQ to TCi tag mapping on the phy and in cases where we
  633. * resume because of a target reset we also need to update
  634. * the STPTLDARNI register with the RNi of the device
  635. */
  636. if ((dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) &&
  637. sci_dev->is_direct_attached)
  638. scic_sds_port_setup_transports(sci_dev->owning_port,
  639. rnc->remote_node_index);
  640. scic_sds_remote_device_post_request(sci_dev, SCU_CONTEXT_COMMAND_POST_RNC_RESUME);
  641. }
  642. /**
  643. *
  644. *
  645. *
  646. */
  647. static void scic_sds_remote_node_context_ready_state_enter(void *object)
  648. {
  649. struct scic_sds_remote_node_context *rnc = object;
  650. SET_STATE_HANDLER(
  651. rnc,
  652. scic_sds_remote_node_context_state_handler_table,
  653. SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE
  654. );
  655. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  656. if (rnc->user_callback != NULL) {
  657. scic_sds_remote_node_context_notify_user(rnc);
  658. }
  659. }
  660. /**
  661. *
  662. *
  663. *
  664. */
  665. static void scic_sds_remote_node_context_tx_suspended_state_enter(void *object)
  666. {
  667. struct scic_sds_remote_node_context *rnc = object;
  668. SET_STATE_HANDLER(
  669. rnc,
  670. scic_sds_remote_node_context_state_handler_table,
  671. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE
  672. );
  673. scic_sds_remote_node_context_continue_state_transitions(rnc);
  674. }
  675. /**
  676. *
  677. *
  678. *
  679. */
  680. static void scic_sds_remote_node_context_tx_rx_suspended_state_enter(
  681. void *object)
  682. {
  683. struct scic_sds_remote_node_context *rnc = object;
  684. SET_STATE_HANDLER(
  685. rnc,
  686. scic_sds_remote_node_context_state_handler_table,
  687. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE
  688. );
  689. scic_sds_remote_node_context_continue_state_transitions(rnc);
  690. }
  691. /**
  692. *
  693. *
  694. *
  695. */
  696. static void scic_sds_remote_node_context_await_suspension_state_enter(
  697. void *object)
  698. {
  699. struct scic_sds_remote_node_context *rnc = object;
  700. SET_STATE_HANDLER(
  701. rnc,
  702. scic_sds_remote_node_context_state_handler_table,
  703. SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE
  704. );
  705. }
  706. /* --------------------------------------------------------------------------- */
  707. static const struct sci_base_state scic_sds_remote_node_context_state_table[] = {
  708. [SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE] = {
  709. .enter_state = scic_sds_remote_node_context_initial_state_enter,
  710. },
  711. [SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE] = {
  712. .enter_state = scic_sds_remote_node_context_posting_state_enter,
  713. },
  714. [SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE] = {
  715. .enter_state = scic_sds_remote_node_context_invalidating_state_enter,
  716. },
  717. [SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE] = {
  718. .enter_state = scic_sds_remote_node_context_resuming_state_enter,
  719. },
  720. [SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE] = {
  721. .enter_state = scic_sds_remote_node_context_ready_state_enter,
  722. },
  723. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE] = {
  724. .enter_state = scic_sds_remote_node_context_tx_suspended_state_enter,
  725. },
  726. [SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE] = {
  727. .enter_state = scic_sds_remote_node_context_tx_rx_suspended_state_enter,
  728. },
  729. [SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE] = {
  730. .enter_state = scic_sds_remote_node_context_await_suspension_state_enter,
  731. },
  732. };
  733. void scic_sds_remote_node_context_construct(struct scic_sds_remote_node_context *rnc,
  734. u16 remote_node_index)
  735. {
  736. memset(rnc, 0, sizeof(struct scic_sds_remote_node_context));
  737. rnc->remote_node_index = remote_node_index;
  738. rnc->destination_state = SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_UNSPECIFIED;
  739. sci_base_state_machine_construct(
  740. &rnc->state_machine,
  741. rnc,
  742. scic_sds_remote_node_context_state_table,
  743. SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE
  744. );
  745. sci_base_state_machine_start(&rnc->state_machine);
  746. }
  747. enum sci_status scic_sds_remote_node_context_event_handler(struct scic_sds_remote_node_context *sci_rnc,
  748. u32 event_code)
  749. {
  750. enum scis_sds_remote_node_context_states state;
  751. state = sci_rnc->state_machine.current_state_id;
  752. switch (state) {
  753. case SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE:
  754. switch (scu_get_event_code(event_code)) {
  755. case SCU_EVENT_POST_RNC_COMPLETE:
  756. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  757. SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE);
  758. break;
  759. default:
  760. goto out;
  761. }
  762. break;
  763. case SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE:
  764. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RNC_INVALIDATE_COMPLETE) {
  765. if (sci_rnc->destination_state == SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_FINAL)
  766. state = SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE;
  767. else
  768. state = SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE;
  769. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  770. state);
  771. } else {
  772. switch (scu_get_event_type(event_code)) {
  773. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  774. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  775. /* We really dont care if the hardware is going to suspend
  776. * the device since it's being invalidated anyway */
  777. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  778. "%s: SCIC Remote Node Context 0x%p was "
  779. "suspeneded by hardware while being "
  780. "invalidated.\n", __func__, sci_rnc);
  781. break;
  782. default:
  783. goto out;
  784. }
  785. }
  786. break;
  787. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  788. if (scu_get_event_code(event_code) == SCU_EVENT_POST_RCN_RELEASE) {
  789. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  790. SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE);
  791. } else {
  792. switch (scu_get_event_type(event_code)) {
  793. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  794. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  795. /* We really dont care if the hardware is going to suspend
  796. * the device since it's being resumed anyway */
  797. dev_dbg(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  798. "%s: SCIC Remote Node Context 0x%p was "
  799. "suspeneded by hardware while being resumed.\n",
  800. __func__, sci_rnc);
  801. break;
  802. default:
  803. goto out;
  804. }
  805. }
  806. break;
  807. case SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE:
  808. switch (scu_get_event_type(event_code)) {
  809. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  810. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  811. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE);
  812. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  813. break;
  814. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  815. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  816. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE);
  817. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  818. break;
  819. default:
  820. goto out;
  821. }
  822. break;
  823. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  824. switch (scu_get_event_type(event_code)) {
  825. case SCU_EVENT_TL_RNC_SUSPEND_TX:
  826. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  827. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE);
  828. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  829. break;
  830. case SCU_EVENT_TL_RNC_SUSPEND_TX_RX:
  831. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  832. SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE);
  833. sci_rnc->suspension_code = scu_get_event_specifier(event_code);
  834. break;
  835. default:
  836. goto out;
  837. }
  838. break;
  839. default:
  840. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  841. "%s: invalid state %d\n", __func__, state);
  842. return SCI_FAILURE_INVALID_STATE;
  843. }
  844. return SCI_SUCCESS;
  845. out:
  846. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  847. "%s: code: %#x state: %d\n", __func__, event_code, state);
  848. return SCI_FAILURE;
  849. }
  850. enum sci_status scic_sds_remote_node_context_destruct(struct scic_sds_remote_node_context *sci_rnc,
  851. scics_sds_remote_node_context_callback cb_fn,
  852. void *cb_p)
  853. {
  854. enum scis_sds_remote_node_context_states state;
  855. state = sci_rnc->state_machine.current_state_id;
  856. switch (state) {
  857. case SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE:
  858. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  859. return SCI_SUCCESS;
  860. case SCIC_SDS_REMOTE_NODE_CONTEXT_POSTING_STATE:
  861. case SCIC_SDS_REMOTE_NODE_CONTEXT_RESUMING_STATE:
  862. case SCIC_SDS_REMOTE_NODE_CONTEXT_READY_STATE:
  863. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_SUSPENDED_STATE:
  864. case SCIC_SDS_REMOTE_NODE_CONTEXT_TX_RX_SUSPENDED_STATE:
  865. case SCIC_SDS_REMOTE_NODE_CONTEXT_AWAIT_SUSPENSION_STATE:
  866. scic_sds_remote_node_context_setup_to_destory(sci_rnc, cb_fn, cb_p);
  867. sci_base_state_machine_change_state(&sci_rnc->state_machine,
  868. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALIDATING_STATE);
  869. return SCI_SUCCESS;
  870. case SCIC_SDS_REMOTE_NODE_CONTEXT_INITIAL_STATE:
  871. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  872. "%s: invalid state %d\n", __func__, state);
  873. /* We have decided that the destruct request on the remote node context
  874. * can not fail since it is either in the initial/destroyed state or is
  875. * can be destroyed.
  876. */
  877. return SCI_SUCCESS;
  878. default:
  879. dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
  880. "%s: invalid state %d\n", __func__, state);
  881. return SCI_FAILURE_INVALID_STATE;
  882. }
  883. }