remote_device.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  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 "intel_sas.h"
  56. #include "intel_ata.h"
  57. #include "isci.h"
  58. #include "port.h"
  59. #include "remote_device.h"
  60. #include "request.h"
  61. #include "scic_controller.h"
  62. #include "scic_io_request.h"
  63. #include "scic_phy.h"
  64. #include "scic_port.h"
  65. #include "scic_sds_controller.h"
  66. #include "scic_sds_phy.h"
  67. #include "scic_sds_port.h"
  68. #include "remote_node_context.h"
  69. #include "scic_sds_request.h"
  70. #include "sci_environment.h"
  71. #include "sci_util.h"
  72. #include "scu_event_codes.h"
  73. #include "task.h"
  74. /**
  75. * isci_remote_device_change_state() - This function gets the status of the
  76. * remote_device object.
  77. * @isci_device: This parameter points to the isci_remote_device object
  78. *
  79. * status of the object as a isci_status enum.
  80. */
  81. void isci_remote_device_change_state(
  82. struct isci_remote_device *isci_device,
  83. enum isci_status status)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&isci_device->state_lock, flags);
  87. isci_device->status = status;
  88. spin_unlock_irqrestore(&isci_device->state_lock, flags);
  89. }
  90. /**
  91. * isci_remote_device_not_ready() - This function is called by the scic when
  92. * the remote device is not ready. We mark the isci device as ready (not
  93. * "ready_for_io") and signal the waiting proccess.
  94. * @isci_host: This parameter specifies the isci host object.
  95. * @isci_device: This parameter specifies the remote device
  96. *
  97. */
  98. static void isci_remote_device_not_ready(struct isci_host *ihost,
  99. struct isci_remote_device *idev, u32 reason)
  100. {
  101. dev_dbg(&ihost->pdev->dev,
  102. "%s: isci_device = %p\n", __func__, idev);
  103. if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
  104. isci_remote_device_change_state(idev, isci_stopping);
  105. else
  106. /* device ready is actually a "not ready for io" state. */
  107. isci_remote_device_change_state(idev, isci_ready);
  108. }
  109. /**
  110. * isci_remote_device_ready() - This function is called by the scic when the
  111. * remote device is ready. We mark the isci device as ready and signal the
  112. * waiting proccess.
  113. * @ihost: our valid isci_host
  114. * @idev: remote device
  115. *
  116. */
  117. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  118. {
  119. dev_dbg(&ihost->pdev->dev,
  120. "%s: idev = %p\n", __func__, idev);
  121. isci_remote_device_change_state(idev, isci_ready_for_io);
  122. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  123. wake_up(&ihost->eventq);
  124. }
  125. /* called once the remote node context is ready to be freed.
  126. * The remote device can now report that its stop operation is complete. none
  127. */
  128. static void rnc_destruct_done(void *_dev)
  129. {
  130. struct scic_sds_remote_device *sci_dev = _dev;
  131. BUG_ON(sci_dev->started_request_count != 0);
  132. sci_base_state_machine_change_state(&sci_dev->state_machine,
  133. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  134. }
  135. static enum sci_status scic_sds_remote_device_terminate_requests(struct scic_sds_remote_device *sci_dev)
  136. {
  137. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  138. u32 i, request_count = sci_dev->started_request_count;
  139. enum sci_status status = SCI_SUCCESS;
  140. for (i = 0; i < SCI_MAX_IO_REQUESTS && i < request_count; i++) {
  141. struct scic_sds_request *sci_req;
  142. enum sci_status s;
  143. sci_req = scic->io_request_table[i];
  144. if (!sci_req || sci_req->target_device != sci_dev)
  145. continue;
  146. s = scic_controller_terminate_request(scic, sci_dev, sci_req);
  147. if (s != SCI_SUCCESS)
  148. status = s;
  149. }
  150. return status;
  151. }
  152. enum sci_status scic_remote_device_stop(struct scic_sds_remote_device *sci_dev,
  153. u32 timeout)
  154. {
  155. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  156. enum scic_sds_remote_device_states state = sm->current_state_id;
  157. switch (state) {
  158. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  159. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  160. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  161. default:
  162. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  163. __func__, state);
  164. return SCI_FAILURE_INVALID_STATE;
  165. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  166. return SCI_SUCCESS;
  167. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  168. /* device not started so there had better be no requests */
  169. BUG_ON(sci_dev->started_request_count != 0);
  170. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  171. rnc_destruct_done, sci_dev);
  172. /* Transition to the stopping state and wait for the
  173. * remote node to complete being posted and invalidated.
  174. */
  175. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  176. return SCI_SUCCESS;
  177. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  178. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  179. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  180. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  181. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  182. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  183. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  184. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  185. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  186. if (sci_dev->started_request_count == 0) {
  187. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  188. rnc_destruct_done, sci_dev);
  189. return SCI_SUCCESS;
  190. } else
  191. return scic_sds_remote_device_terminate_requests(sci_dev);
  192. break;
  193. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  194. /* All requests should have been terminated, but if there is an
  195. * attempt to stop a device already in the stopping state, then
  196. * try again to terminate.
  197. */
  198. return scic_sds_remote_device_terminate_requests(sci_dev);
  199. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  200. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STOPPING);
  201. return SCI_SUCCESS;
  202. }
  203. }
  204. enum sci_status scic_remote_device_reset(struct scic_sds_remote_device *sci_dev)
  205. {
  206. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  207. enum scic_sds_remote_device_states state = sm->current_state_id;
  208. switch (state) {
  209. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  210. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  211. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  212. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  213. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  214. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  215. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  216. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  217. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  218. default:
  219. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  220. __func__, state);
  221. return SCI_FAILURE_INVALID_STATE;
  222. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  223. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  224. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  225. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  226. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  227. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  228. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_RESETTING);
  229. return SCI_SUCCESS;
  230. }
  231. }
  232. enum sci_status scic_remote_device_reset_complete(struct scic_sds_remote_device *sci_dev)
  233. {
  234. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  235. enum scic_sds_remote_device_states state = sm->current_state_id;
  236. if (state != SCI_BASE_REMOTE_DEVICE_STATE_RESETTING) {
  237. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  238. __func__, state);
  239. return SCI_FAILURE_INVALID_STATE;
  240. }
  241. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_READY);
  242. return SCI_SUCCESS;
  243. }
  244. enum sci_status scic_sds_remote_device_suspend(struct scic_sds_remote_device *sci_dev,
  245. u32 suspend_type)
  246. {
  247. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  248. enum scic_sds_remote_device_states state = sm->current_state_id;
  249. if (state != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD) {
  250. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  251. __func__, state);
  252. return SCI_FAILURE_INVALID_STATE;
  253. }
  254. return scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  255. suspend_type, NULL, NULL);
  256. }
  257. /**
  258. *
  259. * @sci_dev: The remote device for which the event handling is being
  260. * requested.
  261. * @frame_index: This is the frame index that is being processed.
  262. *
  263. * This method invokes the frame handler for the remote device state machine
  264. * enum sci_status
  265. */
  266. enum sci_status scic_sds_remote_device_frame_handler(
  267. struct scic_sds_remote_device *sci_dev,
  268. u32 frame_index)
  269. {
  270. return sci_dev->state_handlers->frame_handler(sci_dev, frame_index);
  271. }
  272. /**
  273. *
  274. * @sci_dev: The remote device for which the event handling is being
  275. * requested.
  276. * @event_code: This is the event code that is to be processed.
  277. *
  278. * This method invokes the remote device event handler. enum sci_status
  279. */
  280. enum sci_status scic_sds_remote_device_event_handler(
  281. struct scic_sds_remote_device *sci_dev,
  282. u32 event_code)
  283. {
  284. return sci_dev->state_handlers->event_handler(sci_dev, event_code);
  285. }
  286. static void scic_sds_remote_device_start_request(struct scic_sds_remote_device *sci_dev,
  287. struct scic_sds_request *sci_req,
  288. enum sci_status status)
  289. {
  290. struct scic_sds_port *sci_port = sci_dev->owning_port;
  291. /* cleanup requests that failed after starting on the port */
  292. if (status != SCI_SUCCESS)
  293. scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  294. else
  295. scic_sds_remote_device_increment_request_count(sci_dev);
  296. }
  297. enum sci_status scic_sds_remote_device_start_io(struct scic_sds_controller *scic,
  298. struct scic_sds_remote_device *sci_dev,
  299. struct scic_sds_request *sci_req)
  300. {
  301. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  302. enum scic_sds_remote_device_states state = sm->current_state_id;
  303. struct scic_sds_port *sci_port = sci_dev->owning_port;
  304. enum sci_status status;
  305. switch (state) {
  306. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  307. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  308. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  309. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  310. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  311. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  312. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  313. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  314. default:
  315. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  316. __func__, state);
  317. return SCI_FAILURE_INVALID_STATE;
  318. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  319. /* attempt to start an io request for this device object. The remote
  320. * device object will issue the start request for the io and if
  321. * successful it will start the request for the port object then
  322. * increment its own request count.
  323. */
  324. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  325. if (status != SCI_SUCCESS)
  326. return status;
  327. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  328. if (status != SCI_SUCCESS)
  329. break;
  330. status = scic_sds_request_start(sci_req);
  331. break;
  332. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE: {
  333. /* handle the start io operation for a sata device that is in
  334. * the command idle state. - Evalute the type of IO request to
  335. * be started - If its an NCQ request change to NCQ substate -
  336. * If its any other command change to the CMD substate
  337. *
  338. * If this is a softreset we may want to have a different
  339. * substate.
  340. */
  341. enum scic_sds_remote_device_states new_state;
  342. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  343. if (status != SCI_SUCCESS)
  344. return status;
  345. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  346. if (status != SCI_SUCCESS)
  347. break;
  348. status = sci_req->state_handlers->start_handler(sci_req);
  349. if (status != SCI_SUCCESS)
  350. break;
  351. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA)
  352. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ;
  353. else {
  354. sci_dev->working_request = sci_req;
  355. new_state = SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD;
  356. }
  357. sci_base_state_machine_change_state(sm, new_state);
  358. break;
  359. }
  360. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  361. if (isci_sata_get_sat_protocol(sci_req->ireq) == SAT_PROTOCOL_FPDMA) {
  362. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  363. if (status != SCI_SUCCESS)
  364. return status;
  365. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  366. if (status != SCI_SUCCESS)
  367. break;
  368. status = sci_req->state_handlers->start_handler(sci_req);
  369. } else
  370. return SCI_FAILURE_INVALID_STATE;
  371. break;
  372. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  373. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  374. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  375. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  376. if (status != SCI_SUCCESS)
  377. return status;
  378. status = scic_sds_remote_node_context_start_io(&sci_dev->rnc, sci_req);
  379. if (status != SCI_SUCCESS)
  380. break;
  381. status = scic_sds_request_start(sci_req);
  382. if (status != SCI_SUCCESS)
  383. break;
  384. sci_dev->working_request = sci_req;
  385. sci_base_state_machine_change_state(&sci_dev->state_machine,
  386. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  387. break;
  388. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  389. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  390. /* device is already handling a command it can not accept new commands
  391. * until this one is complete.
  392. */
  393. return SCI_FAILURE_INVALID_STATE;
  394. }
  395. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  396. return status;
  397. }
  398. static enum sci_status common_complete_io(struct scic_sds_port *sci_port,
  399. struct scic_sds_remote_device *sci_dev,
  400. struct scic_sds_request *sci_req)
  401. {
  402. enum sci_status status;
  403. status = scic_sds_request_complete(sci_req);
  404. if (status != SCI_SUCCESS)
  405. return status;
  406. status = scic_sds_port_complete_io(sci_port, sci_dev, sci_req);
  407. if (status != SCI_SUCCESS)
  408. return status;
  409. scic_sds_remote_device_decrement_request_count(sci_dev);
  410. return status;
  411. }
  412. enum sci_status scic_sds_remote_device_complete_io(struct scic_sds_controller *scic,
  413. struct scic_sds_remote_device *sci_dev,
  414. struct scic_sds_request *sci_req)
  415. {
  416. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  417. enum scic_sds_remote_device_states state = sm->current_state_id;
  418. struct scic_sds_port *sci_port = sci_dev->owning_port;
  419. enum sci_status status;
  420. switch (state) {
  421. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  422. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  423. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  424. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  425. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  426. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  427. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  428. default:
  429. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  430. __func__, state);
  431. return SCI_FAILURE_INVALID_STATE;
  432. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  433. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  434. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  435. status = common_complete_io(sci_port, sci_dev, sci_req);
  436. break;
  437. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  438. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  439. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  440. status = common_complete_io(sci_port, sci_dev, sci_req);
  441. if (status != SCI_SUCCESS)
  442. break;
  443. if (sci_req->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  444. /* This request causes hardware error, device needs to be Lun Reset.
  445. * So here we force the state machine to IDLE state so the rest IOs
  446. * can reach RNC state handler, these IOs will be completed by RNC with
  447. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  448. */
  449. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  450. } else if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  451. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  452. break;
  453. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  454. status = common_complete_io(sci_port, sci_dev, sci_req);
  455. if (status != SCI_SUCCESS)
  456. break;
  457. sci_base_state_machine_change_state(sm, SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  458. break;
  459. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  460. status = common_complete_io(sci_port, sci_dev, sci_req);
  461. if (status != SCI_SUCCESS)
  462. break;
  463. if (scic_sds_remote_device_get_request_count(sci_dev) == 0)
  464. scic_sds_remote_node_context_destruct(&sci_dev->rnc,
  465. rnc_destruct_done,
  466. sci_dev);
  467. break;
  468. }
  469. if (status != SCI_SUCCESS)
  470. dev_err(scirdev_to_dev(sci_dev),
  471. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  472. "could not complete\n", __func__, sci_port,
  473. sci_dev, sci_req, status);
  474. return status;
  475. }
  476. static void scic_sds_remote_device_continue_request(void *dev)
  477. {
  478. struct scic_sds_remote_device *sci_dev = dev;
  479. /* we need to check if this request is still valid to continue. */
  480. if (sci_dev->working_request)
  481. scic_controller_continue_io(sci_dev->working_request);
  482. }
  483. enum sci_status scic_sds_remote_device_start_task(struct scic_sds_controller *scic,
  484. struct scic_sds_remote_device *sci_dev,
  485. struct scic_sds_request *sci_req)
  486. {
  487. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  488. enum scic_sds_remote_device_states state = sm->current_state_id;
  489. struct scic_sds_port *sci_port = sci_dev->owning_port;
  490. enum sci_status status;
  491. switch (state) {
  492. case SCI_BASE_REMOTE_DEVICE_STATE_INITIAL:
  493. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPED:
  494. case SCI_BASE_REMOTE_DEVICE_STATE_STARTING:
  495. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  496. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  497. case SCI_BASE_REMOTE_DEVICE_STATE_STOPPING:
  498. case SCI_BASE_REMOTE_DEVICE_STATE_FAILED:
  499. case SCI_BASE_REMOTE_DEVICE_STATE_RESETTING:
  500. case SCI_BASE_REMOTE_DEVICE_STATE_FINAL:
  501. default:
  502. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  503. __func__, state);
  504. return SCI_FAILURE_INVALID_STATE;
  505. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  506. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  507. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  508. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  509. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  510. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  511. if (status != SCI_SUCCESS)
  512. return status;
  513. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  514. if (status != SCI_SUCCESS)
  515. goto out;
  516. status = sci_req->state_handlers->start_handler(sci_req);
  517. if (status != SCI_SUCCESS)
  518. goto out;
  519. /* Note: If the remote device state is not IDLE this will
  520. * replace the request that probably resulted in the task
  521. * management request.
  522. */
  523. sci_dev->working_request = sci_req;
  524. sci_base_state_machine_change_state(sm, SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  525. /* The remote node context must cleanup the TCi to NCQ mapping
  526. * table. The only way to do this correctly is to either write
  527. * to the TLCR register or to invalidate and repost the RNC. In
  528. * either case the remote node context state machine will take
  529. * the correct action when the remote node context is suspended
  530. * and later resumed.
  531. */
  532. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  533. SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  534. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  535. scic_sds_remote_device_continue_request,
  536. sci_dev);
  537. out:
  538. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  539. /* We need to let the controller start request handler know that
  540. * it can't post TC yet. We will provide a callback function to
  541. * post TC when RNC gets resumed.
  542. */
  543. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  544. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  545. status = scic_sds_port_start_io(sci_port, sci_dev, sci_req);
  546. if (status != SCI_SUCCESS)
  547. return status;
  548. status = scic_sds_remote_node_context_start_task(&sci_dev->rnc, sci_req);
  549. if (status != SCI_SUCCESS)
  550. break;
  551. status = scic_sds_request_start(sci_req);
  552. break;
  553. }
  554. scic_sds_remote_device_start_request(sci_dev, sci_req, status);
  555. return status;
  556. }
  557. /**
  558. *
  559. * @sci_dev:
  560. * @request:
  561. *
  562. * This method takes the request and bulids an appropriate SCU context for the
  563. * request and then requests the controller to post the request. none
  564. */
  565. void scic_sds_remote_device_post_request(
  566. struct scic_sds_remote_device *sci_dev,
  567. u32 request)
  568. {
  569. u32 context;
  570. context = scic_sds_remote_device_build_command_context(sci_dev, request);
  571. scic_sds_controller_post_request(
  572. scic_sds_remote_device_get_controller(sci_dev),
  573. context
  574. );
  575. }
  576. /* called once the remote node context has transisitioned to a
  577. * ready state. This is the indication that the remote device object can also
  578. * transition to ready.
  579. */
  580. static void remote_device_resume_done(void *_dev)
  581. {
  582. struct scic_sds_remote_device *sci_dev = _dev;
  583. enum scic_sds_remote_device_states state;
  584. state = sci_dev->state_machine.current_state_id;
  585. switch (state) {
  586. case SCI_BASE_REMOTE_DEVICE_STATE_READY:
  587. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  588. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  589. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ:
  590. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR:
  591. case SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET:
  592. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE:
  593. case SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD:
  594. break;
  595. default:
  596. /* go 'ready' if we are not already in a ready state */
  597. sci_base_state_machine_change_state(&sci_dev->state_machine,
  598. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  599. break;
  600. }
  601. }
  602. /**
  603. *
  604. * @device: The struct scic_sds_remote_device which is then cast into a
  605. * struct scic_sds_remote_device.
  606. * @event_code: The event code that the struct scic_sds_controller wants the device
  607. * object to process.
  608. *
  609. * This method is the default event handler. It will call the RNC state
  610. * machine handler for any RNC events otherwise it will log a warning and
  611. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  612. */
  613. static enum sci_status scic_sds_remote_device_core_event_handler(
  614. struct scic_sds_remote_device *sci_dev,
  615. u32 event_code,
  616. bool is_ready_state)
  617. {
  618. enum sci_status status;
  619. switch (scu_get_event_type(event_code)) {
  620. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  621. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  622. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  623. status = scic_sds_remote_node_context_event_handler(&sci_dev->rnc, event_code);
  624. break;
  625. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  626. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  627. status = SCI_SUCCESS;
  628. /* Suspend the associated RNC */
  629. scic_sds_remote_node_context_suspend(&sci_dev->rnc,
  630. SCI_SOFTWARE_SUSPENSION,
  631. NULL, NULL);
  632. dev_dbg(scirdev_to_dev(sci_dev),
  633. "%s: device: %p event code: %x: %s\n",
  634. __func__, sci_dev, event_code,
  635. (is_ready_state)
  636. ? "I_T_Nexus_Timeout event"
  637. : "I_T_Nexus_Timeout event in wrong state");
  638. break;
  639. }
  640. /* Else, fall through and treat as unhandled... */
  641. default:
  642. dev_dbg(scirdev_to_dev(sci_dev),
  643. "%s: device: %p event code: %x: %s\n",
  644. __func__, sci_dev, event_code,
  645. (is_ready_state)
  646. ? "unexpected event"
  647. : "unexpected event in wrong state");
  648. status = SCI_FAILURE_INVALID_STATE;
  649. break;
  650. }
  651. return status;
  652. }
  653. /**
  654. *
  655. * @device: The struct scic_sds_remote_device which is then cast into a
  656. * struct scic_sds_remote_device.
  657. * @event_code: The event code that the struct scic_sds_controller wants the device
  658. * object to process.
  659. *
  660. * This method is the default event handler. It will call the RNC state
  661. * machine handler for any RNC events otherwise it will log a warning and
  662. * returns a failure. enum sci_status SCI_FAILURE_INVALID_STATE
  663. */
  664. static enum sci_status scic_sds_remote_device_default_event_handler(
  665. struct scic_sds_remote_device *sci_dev,
  666. u32 event_code)
  667. {
  668. return scic_sds_remote_device_core_event_handler(sci_dev,
  669. event_code,
  670. false);
  671. }
  672. /**
  673. *
  674. * @device: The struct scic_sds_remote_device which is then cast into a
  675. * struct scic_sds_remote_device.
  676. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  677. * device object to process.
  678. *
  679. * This method is the default unsolicited frame handler. It logs a warning,
  680. * releases the frame and returns a failure. enum sci_status
  681. * SCI_FAILURE_INVALID_STATE
  682. */
  683. static enum sci_status scic_sds_remote_device_default_frame_handler(
  684. struct scic_sds_remote_device *sci_dev,
  685. u32 frame_index)
  686. {
  687. dev_warn(scirdev_to_dev(sci_dev),
  688. "%s: SCIC Remote Device requested to handle frame %x "
  689. "while in wrong state %d\n",
  690. __func__,
  691. frame_index,
  692. sci_base_state_machine_get_state(
  693. &sci_dev->state_machine));
  694. /* Return the frame back to the controller */
  695. scic_sds_controller_release_frame(
  696. scic_sds_remote_device_get_controller(sci_dev), frame_index
  697. );
  698. return SCI_FAILURE_INVALID_STATE;
  699. }
  700. /**
  701. *
  702. * @device: The struct scic_sds_remote_device which is then cast into a
  703. * struct scic_sds_remote_device.
  704. * @frame_index: The frame index for which the struct scic_sds_controller wants this
  705. * device object to process.
  706. *
  707. * This method is a general ssp frame handler. In most cases the device object
  708. * needs to route the unsolicited frame processing to the io request object.
  709. * This method decodes the tag for the io request object and routes the
  710. * unsolicited frame to that object. enum sci_status SCI_FAILURE_INVALID_STATE
  711. */
  712. static enum sci_status scic_sds_remote_device_general_frame_handler(
  713. struct scic_sds_remote_device *sci_dev,
  714. u32 frame_index)
  715. {
  716. enum sci_status result;
  717. struct sci_ssp_frame_header *frame_header;
  718. struct scic_sds_request *io_request;
  719. result = scic_sds_unsolicited_frame_control_get_header(
  720. &(scic_sds_remote_device_get_controller(sci_dev)->uf_control),
  721. frame_index,
  722. (void **)&frame_header
  723. );
  724. if (SCI_SUCCESS == result) {
  725. io_request = scic_sds_controller_get_io_request_from_tag(
  726. scic_sds_remote_device_get_controller(sci_dev), frame_header->tag);
  727. if ((io_request == NULL)
  728. || (io_request->target_device != sci_dev)) {
  729. /*
  730. * We could not map this tag to a valid IO request
  731. * Just toss the frame and continue */
  732. scic_sds_controller_release_frame(
  733. scic_sds_remote_device_get_controller(sci_dev), frame_index
  734. );
  735. } else {
  736. /* The IO request is now in charge of releasing the frame */
  737. result = io_request->state_handlers->frame_handler(
  738. io_request, frame_index);
  739. }
  740. }
  741. return result;
  742. }
  743. /**
  744. *
  745. * @[in]: sci_dev This is the device object that is receiving the event.
  746. * @[in]: event_code The event code to process.
  747. *
  748. * This is a common method for handling events reported to the remote device
  749. * from the controller object. enum sci_status
  750. */
  751. static enum sci_status scic_sds_remote_device_general_event_handler(
  752. struct scic_sds_remote_device *sci_dev,
  753. u32 event_code)
  754. {
  755. return scic_sds_remote_device_core_event_handler(sci_dev,
  756. event_code,
  757. true);
  758. }
  759. static enum sci_status scic_sds_stp_remote_device_ready_idle_substate_event_handler(
  760. struct scic_sds_remote_device *sci_dev,
  761. u32 event_code)
  762. {
  763. enum sci_status status;
  764. status = scic_sds_remote_device_general_event_handler(sci_dev, event_code);
  765. if (status != SCI_SUCCESS)
  766. return status;
  767. /* We pick up suspension events to handle specifically to this state. We
  768. * resume the RNC right away. enum sci_status
  769. */
  770. if (scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX ||
  771. scu_get_event_type(event_code) == SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX)
  772. status = scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  773. return status;
  774. }
  775. static enum sci_status scic_sds_stp_remote_device_ready_ncq_substate_frame_handler(struct scic_sds_remote_device *sci_dev,
  776. u32 frame_index)
  777. {
  778. enum sci_status status;
  779. struct sata_fis_header *frame_header;
  780. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  781. status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
  782. frame_index,
  783. (void **)&frame_header);
  784. if (status != SCI_SUCCESS)
  785. return status;
  786. if (frame_header->fis_type == SATA_FIS_TYPE_SETDEVBITS &&
  787. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  788. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  789. /* TODO Check sactive and complete associated IO if any. */
  790. sci_base_state_machine_change_state(&sci_dev->state_machine,
  791. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  792. } else if (frame_header->fis_type == SATA_FIS_TYPE_REGD2H &&
  793. (frame_header->status & ATA_STATUS_REG_ERROR_BIT)) {
  794. /*
  795. * Some devices return D2H FIS when an NCQ error is detected.
  796. * Treat this like an SDB error FIS ready reason.
  797. */
  798. sci_dev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  799. sci_base_state_machine_change_state(&sci_dev->state_machine,
  800. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  801. } else
  802. status = SCI_FAILURE;
  803. scic_sds_controller_release_frame(scic, frame_index);
  804. return status;
  805. }
  806. static enum sci_status scic_sds_stp_remote_device_ready_cmd_substate_frame_handler(
  807. struct scic_sds_remote_device *sci_dev,
  808. u32 frame_index)
  809. {
  810. /* The device doe not process any UF received from the hardware while
  811. * in this state. All unsolicited frames are forwarded to the io
  812. * request object.
  813. */
  814. return scic_sds_io_request_frame_handler(sci_dev->working_request,
  815. frame_index);
  816. }
  817. static void scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  818. {
  819. struct scic_sds_remote_device *sci_dev = _dev;
  820. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  821. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  822. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  823. * As a result, avoid sending the ready notification.
  824. */
  825. if (sci_dev->state_machine.previous_state_id != SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ)
  826. isci_remote_device_ready(scic->ihost, idev);
  827. }
  828. static enum sci_status scic_sds_smp_remote_device_ready_cmd_substate_frame_handler(
  829. struct scic_sds_remote_device *sci_dev,
  830. u32 frame_index)
  831. {
  832. enum sci_status status;
  833. /* The device does not process any UF received from the hardware while
  834. * in this state. All unsolicited frames are forwarded to the io request
  835. * object.
  836. */
  837. status = scic_sds_io_request_frame_handler(
  838. sci_dev->working_request,
  839. frame_index
  840. );
  841. return status;
  842. }
  843. static const struct scic_sds_remote_device_state_handler scic_sds_remote_device_state_handler_table[] = {
  844. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  845. .event_handler = scic_sds_remote_device_default_event_handler,
  846. .frame_handler = scic_sds_remote_device_default_frame_handler
  847. },
  848. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  849. .event_handler = scic_sds_remote_device_default_event_handler,
  850. .frame_handler = scic_sds_remote_device_default_frame_handler
  851. },
  852. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  853. .event_handler = scic_sds_remote_device_general_event_handler,
  854. .frame_handler = scic_sds_remote_device_default_frame_handler
  855. },
  856. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  857. .event_handler = scic_sds_remote_device_general_event_handler,
  858. .frame_handler = scic_sds_remote_device_general_frame_handler,
  859. },
  860. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  861. .event_handler = scic_sds_stp_remote_device_ready_idle_substate_event_handler,
  862. .frame_handler = scic_sds_remote_device_default_frame_handler
  863. },
  864. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  865. .event_handler = scic_sds_remote_device_general_event_handler,
  866. .frame_handler = scic_sds_stp_remote_device_ready_cmd_substate_frame_handler
  867. },
  868. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  869. .event_handler = scic_sds_remote_device_general_event_handler,
  870. .frame_handler = scic_sds_stp_remote_device_ready_ncq_substate_frame_handler
  871. },
  872. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  873. .event_handler = scic_sds_remote_device_general_event_handler,
  874. .frame_handler = scic_sds_remote_device_general_frame_handler
  875. },
  876. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  877. .event_handler = scic_sds_remote_device_general_event_handler,
  878. .frame_handler = scic_sds_remote_device_general_frame_handler
  879. },
  880. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  881. .event_handler = scic_sds_remote_device_general_event_handler,
  882. .frame_handler = scic_sds_remote_device_default_frame_handler
  883. },
  884. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  885. .event_handler = scic_sds_remote_device_general_event_handler,
  886. .frame_handler = scic_sds_smp_remote_device_ready_cmd_substate_frame_handler
  887. },
  888. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  889. .event_handler = scic_sds_remote_device_general_event_handler,
  890. .frame_handler = scic_sds_remote_device_general_frame_handler
  891. },
  892. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  893. .event_handler = scic_sds_remote_device_default_event_handler,
  894. .frame_handler = scic_sds_remote_device_general_frame_handler
  895. },
  896. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  897. .event_handler = scic_sds_remote_device_default_event_handler,
  898. .frame_handler = scic_sds_remote_device_general_frame_handler
  899. },
  900. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  901. .event_handler = scic_sds_remote_device_default_event_handler,
  902. .frame_handler = scic_sds_remote_device_default_frame_handler
  903. }
  904. };
  905. static void scic_sds_remote_device_initial_state_enter(void *object)
  906. {
  907. struct scic_sds_remote_device *sci_dev = object;
  908. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  909. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
  910. /* Initial state is a transitional state to the stopped state */
  911. sci_base_state_machine_change_state(&sci_dev->state_machine,
  912. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  913. }
  914. /**
  915. * scic_remote_device_destruct() - free remote node context and destruct
  916. * @remote_device: This parameter specifies the remote device to be destructed.
  917. *
  918. * Remote device objects are a limited resource. As such, they must be
  919. * protected. Thus calls to construct and destruct are mutually exclusive and
  920. * non-reentrant. The return value shall indicate if the device was
  921. * successfully destructed or if some failure occurred. enum sci_status This value
  922. * is returned if the device is successfully destructed.
  923. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  924. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  925. * valid, etc.).
  926. */
  927. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  928. {
  929. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  930. enum scic_sds_remote_device_states state = sm->current_state_id;
  931. struct scic_sds_controller *scic;
  932. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  933. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  934. __func__, state);
  935. return SCI_FAILURE_INVALID_STATE;
  936. }
  937. scic = sci_dev->owning_port->owning_controller;
  938. scic_sds_controller_free_remote_node_context(scic, sci_dev,
  939. sci_dev->rnc.remote_node_index);
  940. sci_dev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  941. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_FINAL);
  942. return SCI_SUCCESS;
  943. }
  944. /**
  945. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  946. * @ihost: This parameter specifies the isci host object.
  947. * @idev: This parameter specifies the remote device to be freed.
  948. *
  949. */
  950. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  951. {
  952. dev_dbg(&ihost->pdev->dev,
  953. "%s: isci_device = %p\n", __func__, idev);
  954. /* There should not be any outstanding io's. All paths to
  955. * here should go through isci_remote_device_nuke_requests.
  956. * If we hit this condition, we will need a way to complete
  957. * io requests in process */
  958. while (!list_empty(&idev->reqs_in_process)) {
  959. dev_err(&ihost->pdev->dev,
  960. "%s: ** request list not empty! **\n", __func__);
  961. BUG();
  962. }
  963. scic_remote_device_destruct(&idev->sci);
  964. idev->domain_dev->lldd_dev = NULL;
  965. idev->domain_dev = NULL;
  966. idev->isci_port = NULL;
  967. list_del_init(&idev->node);
  968. clear_bit(IDEV_START_PENDING, &idev->flags);
  969. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  970. wake_up(&ihost->eventq);
  971. }
  972. /**
  973. * isci_remote_device_stop_complete() - This function is called by the scic
  974. * when the remote device stop has completed. We mark the isci device as not
  975. * ready and remove the isci remote device.
  976. * @ihost: This parameter specifies the isci host object.
  977. * @idev: This parameter specifies the remote device.
  978. * @status: This parameter specifies status of the completion.
  979. *
  980. */
  981. static void isci_remote_device_stop_complete(struct isci_host *ihost,
  982. struct isci_remote_device *idev)
  983. {
  984. dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
  985. isci_remote_device_change_state(idev, isci_stopped);
  986. /* after stop, we can tear down resources. */
  987. isci_remote_device_deconstruct(ihost, idev);
  988. }
  989. static void scic_sds_remote_device_stopped_state_enter(void *object)
  990. {
  991. struct scic_sds_remote_device *sci_dev = object;
  992. struct scic_sds_controller *scic;
  993. struct isci_remote_device *idev;
  994. struct isci_host *ihost;
  995. u32 prev_state;
  996. scic = scic_sds_remote_device_get_controller(sci_dev);
  997. ihost = scic->ihost;
  998. idev = sci_dev_to_idev(sci_dev);
  999. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1000. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1001. /* If we are entering from the stopping state let the SCI User know that
  1002. * the stop operation has completed.
  1003. */
  1004. prev_state = sci_dev->state_machine.previous_state_id;
  1005. if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
  1006. isci_remote_device_stop_complete(ihost, idev);
  1007. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  1008. }
  1009. static void scic_sds_remote_device_starting_state_enter(void *object)
  1010. {
  1011. struct scic_sds_remote_device *sci_dev = object;
  1012. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1013. struct isci_host *ihost = scic->ihost;
  1014. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1015. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1016. SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1017. isci_remote_device_not_ready(ihost, idev,
  1018. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  1019. }
  1020. static void scic_sds_remote_device_ready_state_enter(void *object)
  1021. {
  1022. struct scic_sds_remote_device *sci_dev = object;
  1023. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1024. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1025. SET_STATE_HANDLER(sci_dev,
  1026. scic_sds_remote_device_state_handler_table,
  1027. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  1028. scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
  1029. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  1030. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1031. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1032. } else if (dev_is_expander(dev)) {
  1033. sci_base_state_machine_change_state(&sci_dev->state_machine,
  1034. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1035. } else
  1036. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1037. }
  1038. static void scic_sds_remote_device_ready_state_exit(void *object)
  1039. {
  1040. struct scic_sds_remote_device *sci_dev = object;
  1041. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1042. if (dev->dev_type == SAS_END_DEV) {
  1043. struct scic_sds_controller *scic = sci_dev->owning_port->owning_controller;
  1044. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1045. isci_remote_device_not_ready(scic->ihost, idev,
  1046. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  1047. }
  1048. }
  1049. static void scic_sds_remote_device_stopping_state_enter(void *object)
  1050. {
  1051. struct scic_sds_remote_device *sci_dev = object;
  1052. SET_STATE_HANDLER(
  1053. sci_dev,
  1054. scic_sds_remote_device_state_handler_table,
  1055. SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
  1056. );
  1057. }
  1058. static void scic_sds_remote_device_failed_state_enter(void *object)
  1059. {
  1060. struct scic_sds_remote_device *sci_dev = object;
  1061. SET_STATE_HANDLER(
  1062. sci_dev,
  1063. scic_sds_remote_device_state_handler_table,
  1064. SCI_BASE_REMOTE_DEVICE_STATE_FAILED
  1065. );
  1066. }
  1067. static void scic_sds_remote_device_resetting_state_enter(void *object)
  1068. {
  1069. struct scic_sds_remote_device *sci_dev = object;
  1070. SET_STATE_HANDLER(
  1071. sci_dev,
  1072. scic_sds_remote_device_state_handler_table,
  1073. SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
  1074. );
  1075. scic_sds_remote_node_context_suspend(
  1076. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  1077. }
  1078. static void scic_sds_remote_device_resetting_state_exit(void *object)
  1079. {
  1080. struct scic_sds_remote_device *sci_dev = object;
  1081. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  1082. }
  1083. static void scic_sds_remote_device_final_state_enter(void *object)
  1084. {
  1085. struct scic_sds_remote_device *sci_dev = object;
  1086. SET_STATE_HANDLER(
  1087. sci_dev,
  1088. scic_sds_remote_device_state_handler_table,
  1089. SCI_BASE_REMOTE_DEVICE_STATE_FINAL
  1090. );
  1091. }
  1092. static void scic_sds_stp_remote_device_ready_idle_substate_enter(void *object)
  1093. {
  1094. struct scic_sds_remote_device *sci_dev = object;
  1095. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1096. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1097. sci_dev->working_request = NULL;
  1098. if (scic_sds_remote_node_context_is_ready(&sci_dev->rnc)) {
  1099. /*
  1100. * Since the RNC is ready, it's alright to finish completion
  1101. * processing (e.g. signal the remote device is ready). */
  1102. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler(sci_dev);
  1103. } else {
  1104. scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1105. scic_sds_stp_remote_device_ready_idle_substate_resume_complete_handler,
  1106. sci_dev);
  1107. }
  1108. }
  1109. static void scic_sds_stp_remote_device_ready_cmd_substate_enter(void *object)
  1110. {
  1111. struct scic_sds_remote_device *sci_dev = object;
  1112. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1113. BUG_ON(sci_dev->working_request == NULL);
  1114. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1115. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1116. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1117. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  1118. }
  1119. static void scic_sds_stp_remote_device_ready_ncq_substate_enter(void *object)
  1120. {
  1121. struct scic_sds_remote_device *sci_dev = object;
  1122. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1123. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ);
  1124. }
  1125. static void scic_sds_stp_remote_device_ready_ncq_error_substate_enter(void *object)
  1126. {
  1127. struct scic_sds_remote_device *sci_dev = object;
  1128. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1129. struct isci_remote_device *idev = sci_dev_to_idev(sci_dev);
  1130. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1131. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR);
  1132. if (sci_dev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  1133. isci_remote_device_not_ready(scic->ihost, idev,
  1134. sci_dev->not_ready_reason);
  1135. }
  1136. static void scic_sds_stp_remote_device_ready_await_reset_substate_enter(void *object)
  1137. {
  1138. struct scic_sds_remote_device *sci_dev = object;
  1139. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1140. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET);
  1141. }
  1142. static void scic_sds_smp_remote_device_ready_idle_substate_enter(void *object)
  1143. {
  1144. struct scic_sds_remote_device *sci_dev = object;
  1145. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1146. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1147. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1148. isci_remote_device_ready(scic->ihost, sci_dev_to_idev(sci_dev));
  1149. }
  1150. static void scic_sds_smp_remote_device_ready_cmd_substate_enter(void *object)
  1151. {
  1152. struct scic_sds_remote_device *sci_dev = object;
  1153. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1154. BUG_ON(sci_dev->working_request == NULL);
  1155. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1156. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD);
  1157. isci_remote_device_not_ready(scic->ihost, sci_dev_to_idev(sci_dev),
  1158. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  1159. }
  1160. static void scic_sds_smp_remote_device_ready_cmd_substate_exit(void *object)
  1161. {
  1162. struct scic_sds_remote_device *sci_dev = object;
  1163. sci_dev->working_request = NULL;
  1164. }
  1165. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  1166. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1167. .enter_state = scic_sds_remote_device_initial_state_enter,
  1168. },
  1169. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1170. .enter_state = scic_sds_remote_device_stopped_state_enter,
  1171. },
  1172. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1173. .enter_state = scic_sds_remote_device_starting_state_enter,
  1174. },
  1175. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1176. .enter_state = scic_sds_remote_device_ready_state_enter,
  1177. .exit_state = scic_sds_remote_device_ready_state_exit
  1178. },
  1179. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1180. .enter_state = scic_sds_stp_remote_device_ready_idle_substate_enter,
  1181. },
  1182. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1183. .enter_state = scic_sds_stp_remote_device_ready_cmd_substate_enter,
  1184. },
  1185. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ] = {
  1186. .enter_state = scic_sds_stp_remote_device_ready_ncq_substate_enter,
  1187. },
  1188. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_NCQ_ERROR] = {
  1189. .enter_state = scic_sds_stp_remote_device_ready_ncq_error_substate_enter,
  1190. },
  1191. [SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_AWAIT_RESET] = {
  1192. .enter_state = scic_sds_stp_remote_device_ready_await_reset_substate_enter,
  1193. },
  1194. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE] = {
  1195. .enter_state = scic_sds_smp_remote_device_ready_idle_substate_enter,
  1196. },
  1197. [SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_CMD] = {
  1198. .enter_state = scic_sds_smp_remote_device_ready_cmd_substate_enter,
  1199. .exit_state = scic_sds_smp_remote_device_ready_cmd_substate_exit,
  1200. },
  1201. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1202. .enter_state = scic_sds_remote_device_stopping_state_enter,
  1203. },
  1204. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1205. .enter_state = scic_sds_remote_device_failed_state_enter,
  1206. },
  1207. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1208. .enter_state = scic_sds_remote_device_resetting_state_enter,
  1209. .exit_state = scic_sds_remote_device_resetting_state_exit
  1210. },
  1211. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1212. .enter_state = scic_sds_remote_device_final_state_enter,
  1213. },
  1214. };
  1215. /**
  1216. * scic_remote_device_construct() - common construction
  1217. * @sci_port: SAS/SATA port through which this device is accessed.
  1218. * @sci_dev: remote device to construct
  1219. *
  1220. * This routine just performs benign initialization and does not
  1221. * allocate the remote_node_context which is left to
  1222. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  1223. * frees the remote_node_context(s) for the device.
  1224. */
  1225. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  1226. struct scic_sds_remote_device *sci_dev)
  1227. {
  1228. sci_dev->owning_port = sci_port;
  1229. sci_dev->started_request_count = 0;
  1230. sci_base_state_machine_construct(
  1231. &sci_dev->state_machine,
  1232. sci_dev,
  1233. scic_sds_remote_device_state_table,
  1234. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
  1235. );
  1236. sci_base_state_machine_start(
  1237. &sci_dev->state_machine
  1238. );
  1239. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  1240. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1241. }
  1242. /**
  1243. * scic_remote_device_da_construct() - construct direct attached device.
  1244. *
  1245. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1246. * the device is known to the SCI Core since it is contained in the
  1247. * scic_phy object. Remote node context(s) is/are a global resource
  1248. * allocated by this routine, freed by scic_remote_device_destruct().
  1249. *
  1250. * Returns:
  1251. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1252. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1253. * sata-only controller instance.
  1254. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1255. */
  1256. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  1257. struct scic_sds_remote_device *sci_dev)
  1258. {
  1259. enum sci_status status;
  1260. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1261. scic_remote_device_construct(sci_port, sci_dev);
  1262. /*
  1263. * This information is request to determine how many remote node context
  1264. * entries will be needed to store the remote node.
  1265. */
  1266. sci_dev->is_direct_attached = true;
  1267. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1268. sci_dev,
  1269. &sci_dev->rnc.remote_node_index);
  1270. if (status != SCI_SUCCESS)
  1271. return status;
  1272. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1273. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1274. /* pass */;
  1275. else
  1276. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1277. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  1278. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1279. sci_dev->device_port_width = 1;
  1280. return SCI_SUCCESS;
  1281. }
  1282. /**
  1283. * scic_remote_device_ea_construct() - construct expander attached device
  1284. *
  1285. * Remote node context(s) is/are a global resource allocated by this
  1286. * routine, freed by scic_remote_device_destruct().
  1287. *
  1288. * Returns:
  1289. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1290. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1291. * sata-only controller instance.
  1292. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1293. */
  1294. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  1295. struct scic_sds_remote_device *sci_dev)
  1296. {
  1297. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1298. enum sci_status status;
  1299. scic_remote_device_construct(sci_port, sci_dev);
  1300. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1301. sci_dev,
  1302. &sci_dev->rnc.remote_node_index);
  1303. if (status != SCI_SUCCESS)
  1304. return status;
  1305. if (dev->dev_type == SAS_END_DEV || dev->dev_type == SATA_DEV ||
  1306. (dev->tproto & SAS_PROTOCOL_STP) || dev_is_expander(dev))
  1307. /* pass */;
  1308. else
  1309. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1310. /*
  1311. * For SAS-2 the physical link rate is actually a logical link
  1312. * rate that incorporates multiplexing. The SCU doesn't
  1313. * incorporate multiplexing and for the purposes of the
  1314. * connection the logical link rate is that same as the
  1315. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1316. * one another, so this code works for both situations. */
  1317. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1318. dev->linkrate);
  1319. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1320. sci_dev->device_port_width = 1;
  1321. return SCI_SUCCESS;
  1322. }
  1323. /**
  1324. * scic_remote_device_start() - This method will start the supplied remote
  1325. * device. This method enables normal IO requests to flow through to the
  1326. * remote device.
  1327. * @remote_device: This parameter specifies the device to be started.
  1328. * @timeout: This parameter specifies the number of milliseconds in which the
  1329. * start operation should complete.
  1330. *
  1331. * An indication of whether the device was successfully started. SCI_SUCCESS
  1332. * This value is returned if the device was successfully started.
  1333. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1334. * the device when there have been no phys added to it.
  1335. */
  1336. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1337. u32 timeout)
  1338. {
  1339. struct sci_base_state_machine *sm = &sci_dev->state_machine;
  1340. enum scic_sds_remote_device_states state = sm->current_state_id;
  1341. enum sci_status status;
  1342. if (state != SCI_BASE_REMOTE_DEVICE_STATE_STOPPED) {
  1343. dev_warn(scirdev_to_dev(sci_dev), "%s: in wrong state: %d\n",
  1344. __func__, state);
  1345. return SCI_FAILURE_INVALID_STATE;
  1346. }
  1347. status = scic_sds_remote_node_context_resume(&sci_dev->rnc,
  1348. remote_device_resume_done,
  1349. sci_dev);
  1350. if (status != SCI_SUCCESS)
  1351. return status;
  1352. sci_base_state_machine_change_state(sm, SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1353. return SCI_SUCCESS;
  1354. }
  1355. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1356. struct isci_remote_device *idev)
  1357. {
  1358. struct scic_sds_port *sci_port = iport->sci_port_handle;
  1359. struct isci_host *ihost = iport->isci_host;
  1360. struct domain_device *dev = idev->domain_dev;
  1361. enum sci_status status;
  1362. if (dev->parent && dev_is_expander(dev->parent))
  1363. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1364. else
  1365. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1366. if (status != SCI_SUCCESS) {
  1367. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1368. __func__, status);
  1369. return status;
  1370. }
  1371. /* start the device. */
  1372. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1373. if (status != SCI_SUCCESS)
  1374. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1375. status);
  1376. return status;
  1377. }
  1378. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1379. {
  1380. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1381. dev_dbg(&ihost->pdev->dev,
  1382. "%s: idev = %p\n", __func__, idev);
  1383. /* Cleanup all requests pending for this device. */
  1384. isci_terminate_pending_requests(ihost, idev, terminating);
  1385. dev_dbg(&ihost->pdev->dev,
  1386. "%s: idev = %p, done\n", __func__, idev);
  1387. }
  1388. /**
  1389. * This function builds the isci_remote_device when a libsas dev_found message
  1390. * is received.
  1391. * @isci_host: This parameter specifies the isci host object.
  1392. * @port: This parameter specifies the isci_port conected to this device.
  1393. *
  1394. * pointer to new isci_remote_device.
  1395. */
  1396. static struct isci_remote_device *
  1397. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1398. {
  1399. struct isci_remote_device *idev;
  1400. int i;
  1401. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1402. idev = &ihost->devices[i];
  1403. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1404. break;
  1405. }
  1406. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1407. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1408. return NULL;
  1409. }
  1410. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1411. return NULL;
  1412. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1413. return NULL;
  1414. isci_remote_device_change_state(idev, isci_freed);
  1415. return idev;
  1416. }
  1417. /**
  1418. * isci_remote_device_stop() - This function is called internally to stop the
  1419. * remote device.
  1420. * @isci_host: This parameter specifies the isci host object.
  1421. * @isci_device: This parameter specifies the remote device.
  1422. *
  1423. * The status of the scic request to stop.
  1424. */
  1425. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1426. {
  1427. enum sci_status status;
  1428. unsigned long flags;
  1429. dev_dbg(&ihost->pdev->dev,
  1430. "%s: isci_device = %p\n", __func__, idev);
  1431. isci_remote_device_change_state(idev, isci_stopping);
  1432. /* Kill all outstanding requests. */
  1433. isci_remote_device_nuke_requests(ihost, idev);
  1434. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1435. spin_lock_irqsave(&ihost->scic_lock, flags);
  1436. status = scic_remote_device_stop(&idev->sci, 50);
  1437. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1438. /* Wait for the stop complete callback. */
  1439. if (status == SCI_SUCCESS) {
  1440. wait_for_device_stop(ihost, idev);
  1441. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1442. }
  1443. dev_dbg(&ihost->pdev->dev,
  1444. "%s: idev = %p - after completion wait\n",
  1445. __func__, idev);
  1446. return status;
  1447. }
  1448. /**
  1449. * isci_remote_device_gone() - This function is called by libsas when a domain
  1450. * device is removed.
  1451. * @domain_device: This parameter specifies the libsas domain device.
  1452. *
  1453. */
  1454. void isci_remote_device_gone(struct domain_device *dev)
  1455. {
  1456. struct isci_host *ihost = dev_to_ihost(dev);
  1457. struct isci_remote_device *idev = dev->lldd_dev;
  1458. dev_dbg(&ihost->pdev->dev,
  1459. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1460. __func__, dev, idev, idev->isci_port);
  1461. isci_remote_device_stop(ihost, idev);
  1462. }
  1463. /**
  1464. * isci_remote_device_found() - This function is called by libsas when a remote
  1465. * device is discovered. A remote device object is created and started. the
  1466. * function then sleeps until the sci core device started message is
  1467. * received.
  1468. * @domain_device: This parameter specifies the libsas domain device.
  1469. *
  1470. * status, zero indicates success.
  1471. */
  1472. int isci_remote_device_found(struct domain_device *domain_dev)
  1473. {
  1474. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1475. struct isci_port *isci_port;
  1476. struct isci_phy *isci_phy;
  1477. struct asd_sas_port *sas_port;
  1478. struct asd_sas_phy *sas_phy;
  1479. struct isci_remote_device *isci_device;
  1480. enum sci_status status;
  1481. dev_dbg(&isci_host->pdev->dev,
  1482. "%s: domain_device = %p\n", __func__, domain_dev);
  1483. wait_for_start(isci_host);
  1484. sas_port = domain_dev->port;
  1485. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1486. port_phy_el);
  1487. isci_phy = to_isci_phy(sas_phy);
  1488. isci_port = isci_phy->isci_port;
  1489. /* we are being called for a device on this port,
  1490. * so it has to come up eventually
  1491. */
  1492. wait_for_completion(&isci_port->start_complete);
  1493. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1494. (isci_stopped == isci_port_get_state(isci_port)))
  1495. return -ENODEV;
  1496. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1497. if (!isci_device)
  1498. return -ENODEV;
  1499. INIT_LIST_HEAD(&isci_device->node);
  1500. domain_dev->lldd_dev = isci_device;
  1501. isci_device->domain_dev = domain_dev;
  1502. isci_device->isci_port = isci_port;
  1503. isci_remote_device_change_state(isci_device, isci_starting);
  1504. spin_lock_irq(&isci_host->scic_lock);
  1505. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1506. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1507. status = isci_remote_device_construct(isci_port, isci_device);
  1508. spin_unlock_irq(&isci_host->scic_lock);
  1509. dev_dbg(&isci_host->pdev->dev,
  1510. "%s: isci_device = %p\n",
  1511. __func__, isci_device);
  1512. if (status != SCI_SUCCESS) {
  1513. spin_lock_irq(&isci_host->scic_lock);
  1514. isci_remote_device_deconstruct(
  1515. isci_host,
  1516. isci_device
  1517. );
  1518. spin_unlock_irq(&isci_host->scic_lock);
  1519. return -ENODEV;
  1520. }
  1521. /* wait for the device ready callback. */
  1522. wait_for_device_start(isci_host, isci_device);
  1523. return 0;
  1524. }
  1525. /**
  1526. * isci_device_is_reset_pending() - This function will check if there is any
  1527. * pending reset condition on the device.
  1528. * @request: This parameter is the isci_device object.
  1529. *
  1530. * true if there is a reset pending for the device.
  1531. */
  1532. bool isci_device_is_reset_pending(
  1533. struct isci_host *isci_host,
  1534. struct isci_remote_device *isci_device)
  1535. {
  1536. struct isci_request *isci_request;
  1537. struct isci_request *tmp_req;
  1538. bool reset_is_pending = false;
  1539. unsigned long flags;
  1540. dev_dbg(&isci_host->pdev->dev,
  1541. "%s: isci_device = %p\n", __func__, isci_device);
  1542. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1543. /* Check for reset on all pending requests. */
  1544. list_for_each_entry_safe(isci_request, tmp_req,
  1545. &isci_device->reqs_in_process, dev_node) {
  1546. dev_dbg(&isci_host->pdev->dev,
  1547. "%s: isci_device = %p request = %p\n",
  1548. __func__, isci_device, isci_request);
  1549. if (isci_request->ttype == io_task) {
  1550. struct sas_task *task = isci_request_access_task(
  1551. isci_request);
  1552. spin_lock(&task->task_state_lock);
  1553. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1554. reset_is_pending = true;
  1555. spin_unlock(&task->task_state_lock);
  1556. }
  1557. }
  1558. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1559. dev_dbg(&isci_host->pdev->dev,
  1560. "%s: isci_device = %p reset_is_pending = %d\n",
  1561. __func__, isci_device, reset_is_pending);
  1562. return reset_is_pending;
  1563. }
  1564. /**
  1565. * isci_device_clear_reset_pending() - This function will clear if any pending
  1566. * reset condition flags on the device.
  1567. * @request: This parameter is the isci_device object.
  1568. *
  1569. * true if there is a reset pending for the device.
  1570. */
  1571. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1572. {
  1573. struct isci_request *isci_request;
  1574. struct isci_request *tmp_req;
  1575. unsigned long flags = 0;
  1576. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1577. __func__, idev, ihost);
  1578. spin_lock_irqsave(&ihost->scic_lock, flags);
  1579. /* Clear reset pending on all pending requests. */
  1580. list_for_each_entry_safe(isci_request, tmp_req,
  1581. &idev->reqs_in_process, dev_node) {
  1582. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1583. __func__, idev, isci_request);
  1584. if (isci_request->ttype == io_task) {
  1585. unsigned long flags2;
  1586. struct sas_task *task = isci_request_access_task(
  1587. isci_request);
  1588. spin_lock_irqsave(&task->task_state_lock, flags2);
  1589. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1590. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1591. }
  1592. }
  1593. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1594. }