remote_device.c 63 KB

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