remote_device.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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(
  981. struct sci_base_object *object)
  982. {
  983. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  984. sci_dev = container_of(object, typeof(*sci_dev), parent);
  985. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  986. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL);
  987. /* Initial state is a transitional state to the stopped state */
  988. sci_base_state_machine_change_state(&sci_dev->state_machine,
  989. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  990. }
  991. /**
  992. * isci_remote_device_change_state() - This function gets the status of the
  993. * remote_device object.
  994. * @isci_device: This parameter points to the isci_remote_device object
  995. *
  996. * status of the object as a isci_status enum.
  997. */
  998. void isci_remote_device_change_state(
  999. struct isci_remote_device *isci_device,
  1000. enum isci_status status)
  1001. {
  1002. unsigned long flags;
  1003. spin_lock_irqsave(&isci_device->state_lock, flags);
  1004. isci_device->status = status;
  1005. spin_unlock_irqrestore(&isci_device->state_lock, flags);
  1006. }
  1007. /**
  1008. * scic_remote_device_destruct() - free remote node context and destruct
  1009. * @remote_device: This parameter specifies the remote device to be destructed.
  1010. *
  1011. * Remote device objects are a limited resource. As such, they must be
  1012. * protected. Thus calls to construct and destruct are mutually exclusive and
  1013. * non-reentrant. The return value shall indicate if the device was
  1014. * successfully destructed or if some failure occurred. enum sci_status This value
  1015. * is returned if the device is successfully destructed.
  1016. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  1017. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  1018. * valid, etc.).
  1019. */
  1020. static enum sci_status scic_remote_device_destruct(struct scic_sds_remote_device *sci_dev)
  1021. {
  1022. return sci_dev->state_handlers->destruct_handler(sci_dev);
  1023. }
  1024. /**
  1025. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  1026. * @ihost: This parameter specifies the isci host object.
  1027. * @idev: This parameter specifies the remote device to be freed.
  1028. *
  1029. */
  1030. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  1031. {
  1032. dev_dbg(&ihost->pdev->dev,
  1033. "%s: isci_device = %p\n", __func__, idev);
  1034. /* There should not be any outstanding io's. All paths to
  1035. * here should go through isci_remote_device_nuke_requests.
  1036. * If we hit this condition, we will need a way to complete
  1037. * io requests in process */
  1038. while (!list_empty(&idev->reqs_in_process)) {
  1039. dev_err(&ihost->pdev->dev,
  1040. "%s: ** request list not empty! **\n", __func__);
  1041. BUG();
  1042. }
  1043. scic_remote_device_destruct(&idev->sci);
  1044. idev->domain_dev->lldd_dev = NULL;
  1045. idev->domain_dev = NULL;
  1046. idev->isci_port = NULL;
  1047. list_del_init(&idev->node);
  1048. clear_bit(IDEV_START_PENDING, &idev->flags);
  1049. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1050. wake_up(&ihost->eventq);
  1051. }
  1052. /**
  1053. * isci_remote_device_stop_complete() - This function is called by the scic
  1054. * when the remote device stop has completed. We mark the isci device as not
  1055. * ready and remove the isci remote device.
  1056. * @ihost: This parameter specifies the isci host object.
  1057. * @idev: This parameter specifies the remote device.
  1058. * @status: This parameter specifies status of the completion.
  1059. *
  1060. */
  1061. static void isci_remote_device_stop_complete(struct isci_host *ihost,
  1062. struct isci_remote_device *idev)
  1063. {
  1064. dev_dbg(&ihost->pdev->dev, "%s: complete idev = %p\n", __func__, idev);
  1065. isci_remote_device_change_state(idev, isci_stopped);
  1066. /* after stop, we can tear down resources. */
  1067. isci_remote_device_deconstruct(ihost, idev);
  1068. }
  1069. static void scic_sds_remote_device_stopped_state_enter(
  1070. struct sci_base_object *object)
  1071. {
  1072. struct scic_sds_remote_device *sci_dev;
  1073. struct scic_sds_controller *scic;
  1074. struct isci_remote_device *idev;
  1075. struct isci_host *ihost;
  1076. u32 prev_state;
  1077. sci_dev = container_of(object, typeof(*sci_dev), parent);
  1078. scic = scic_sds_remote_device_get_controller(sci_dev);
  1079. ihost = sci_object_get_association(scic);
  1080. idev = sci_object_get_association(sci_dev);
  1081. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1082. SCI_BASE_REMOTE_DEVICE_STATE_STOPPED);
  1083. /* If we are entering from the stopping state let the SCI User know that
  1084. * the stop operation has completed.
  1085. */
  1086. prev_state = sci_dev->state_machine.previous_state_id;
  1087. if (prev_state == SCI_BASE_REMOTE_DEVICE_STATE_STOPPING)
  1088. isci_remote_device_stop_complete(ihost, idev);
  1089. scic_sds_controller_remote_device_stopped(scic, sci_dev);
  1090. }
  1091. static void scic_sds_remote_device_starting_state_enter(struct sci_base_object *object)
  1092. {
  1093. struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
  1094. parent);
  1095. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1096. struct isci_host *ihost = sci_object_get_association(scic);
  1097. struct isci_remote_device *idev = sci_object_get_association(sci_dev);
  1098. SET_STATE_HANDLER(sci_dev, scic_sds_remote_device_state_handler_table,
  1099. SCI_BASE_REMOTE_DEVICE_STATE_STARTING);
  1100. isci_remote_device_not_ready(ihost, idev,
  1101. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  1102. }
  1103. static void scic_sds_remote_device_ready_state_enter(struct sci_base_object *object)
  1104. {
  1105. struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
  1106. parent);
  1107. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1108. struct isci_host *ihost = sci_object_get_association(scic);
  1109. struct isci_remote_device *idev = sci_object_get_association(sci_dev);
  1110. SET_STATE_HANDLER(sci_dev,
  1111. scic_sds_remote_device_state_handler_table,
  1112. SCI_BASE_REMOTE_DEVICE_STATE_READY);
  1113. scic->remote_device_sequence[sci_dev->rnc.remote_node_index]++;
  1114. if (sci_dev->has_ready_substate_machine)
  1115. sci_base_state_machine_start(&sci_dev->ready_substate_machine);
  1116. else
  1117. isci_remote_device_ready(ihost, idev);
  1118. }
  1119. static void scic_sds_remote_device_ready_state_exit(
  1120. struct sci_base_object *object)
  1121. {
  1122. struct scic_sds_remote_device *sci_dev = container_of(object, typeof(*sci_dev),
  1123. parent);
  1124. if (sci_dev->has_ready_substate_machine)
  1125. sci_base_state_machine_stop(&sci_dev->ready_substate_machine);
  1126. else {
  1127. struct scic_sds_controller *scic = scic_sds_remote_device_get_controller(sci_dev);
  1128. struct isci_host *ihost = sci_object_get_association(scic);
  1129. struct isci_remote_device *idev = sci_object_get_association(sci_dev);
  1130. isci_remote_device_not_ready(ihost, idev,
  1131. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  1132. }
  1133. }
  1134. static void scic_sds_remote_device_stopping_state_enter(
  1135. struct sci_base_object *object)
  1136. {
  1137. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  1138. SET_STATE_HANDLER(
  1139. sci_dev,
  1140. scic_sds_remote_device_state_handler_table,
  1141. SCI_BASE_REMOTE_DEVICE_STATE_STOPPING
  1142. );
  1143. }
  1144. static void scic_sds_remote_device_failed_state_enter(
  1145. struct sci_base_object *object)
  1146. {
  1147. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  1148. SET_STATE_HANDLER(
  1149. sci_dev,
  1150. scic_sds_remote_device_state_handler_table,
  1151. SCI_BASE_REMOTE_DEVICE_STATE_FAILED
  1152. );
  1153. }
  1154. static void scic_sds_remote_device_resetting_state_enter(
  1155. struct sci_base_object *object)
  1156. {
  1157. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  1158. SET_STATE_HANDLER(
  1159. sci_dev,
  1160. scic_sds_remote_device_state_handler_table,
  1161. SCI_BASE_REMOTE_DEVICE_STATE_RESETTING
  1162. );
  1163. scic_sds_remote_node_context_suspend(
  1164. &sci_dev->rnc, SCI_SOFTWARE_SUSPENSION, NULL, NULL);
  1165. }
  1166. static void scic_sds_remote_device_resetting_state_exit(
  1167. struct sci_base_object *object)
  1168. {
  1169. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  1170. scic_sds_remote_node_context_resume(&sci_dev->rnc, NULL, NULL);
  1171. }
  1172. static void scic_sds_remote_device_final_state_enter(
  1173. struct sci_base_object *object)
  1174. {
  1175. struct scic_sds_remote_device *sci_dev = (struct scic_sds_remote_device *)object;
  1176. SET_STATE_HANDLER(
  1177. sci_dev,
  1178. scic_sds_remote_device_state_handler_table,
  1179. SCI_BASE_REMOTE_DEVICE_STATE_FINAL
  1180. );
  1181. }
  1182. static const struct sci_base_state scic_sds_remote_device_state_table[] = {
  1183. [SCI_BASE_REMOTE_DEVICE_STATE_INITIAL] = {
  1184. .enter_state = scic_sds_remote_device_initial_state_enter,
  1185. },
  1186. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPED] = {
  1187. .enter_state = scic_sds_remote_device_stopped_state_enter,
  1188. },
  1189. [SCI_BASE_REMOTE_DEVICE_STATE_STARTING] = {
  1190. .enter_state = scic_sds_remote_device_starting_state_enter,
  1191. },
  1192. [SCI_BASE_REMOTE_DEVICE_STATE_READY] = {
  1193. .enter_state = scic_sds_remote_device_ready_state_enter,
  1194. .exit_state = scic_sds_remote_device_ready_state_exit
  1195. },
  1196. [SCI_BASE_REMOTE_DEVICE_STATE_STOPPING] = {
  1197. .enter_state = scic_sds_remote_device_stopping_state_enter,
  1198. },
  1199. [SCI_BASE_REMOTE_DEVICE_STATE_FAILED] = {
  1200. .enter_state = scic_sds_remote_device_failed_state_enter,
  1201. },
  1202. [SCI_BASE_REMOTE_DEVICE_STATE_RESETTING] = {
  1203. .enter_state = scic_sds_remote_device_resetting_state_enter,
  1204. .exit_state = scic_sds_remote_device_resetting_state_exit
  1205. },
  1206. [SCI_BASE_REMOTE_DEVICE_STATE_FINAL] = {
  1207. .enter_state = scic_sds_remote_device_final_state_enter,
  1208. },
  1209. };
  1210. /**
  1211. * scic_remote_device_construct() - common construction
  1212. * @sci_port: SAS/SATA port through which this device is accessed.
  1213. * @sci_dev: remote device to construct
  1214. *
  1215. * This routine just performs benign initialization and does not
  1216. * allocate the remote_node_context which is left to
  1217. * scic_remote_device_[de]a_construct(). scic_remote_device_destruct()
  1218. * frees the remote_node_context(s) for the device.
  1219. */
  1220. static void scic_remote_device_construct(struct scic_sds_port *sci_port,
  1221. struct scic_sds_remote_device *sci_dev)
  1222. {
  1223. sci_dev->owning_port = sci_port;
  1224. sci_dev->started_request_count = 0;
  1225. sci_dev->parent.private = NULL;
  1226. sci_base_state_machine_construct(
  1227. &sci_dev->state_machine,
  1228. &sci_dev->parent,
  1229. scic_sds_remote_device_state_table,
  1230. SCI_BASE_REMOTE_DEVICE_STATE_INITIAL
  1231. );
  1232. sci_base_state_machine_start(
  1233. &sci_dev->state_machine
  1234. );
  1235. scic_sds_remote_node_context_construct(&sci_dev->rnc,
  1236. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1237. sci_object_set_association(&sci_dev->rnc, sci_dev);
  1238. }
  1239. /**
  1240. * scic_remote_device_da_construct() - construct direct attached device.
  1241. *
  1242. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1243. * the device is known to the SCI Core since it is contained in the
  1244. * scic_phy object. Remote node context(s) is/are a global resource
  1245. * allocated by this routine, freed by scic_remote_device_destruct().
  1246. *
  1247. * Returns:
  1248. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1249. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1250. * sata-only controller instance.
  1251. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1252. */
  1253. static enum sci_status scic_remote_device_da_construct(struct scic_sds_port *sci_port,
  1254. struct scic_sds_remote_device *sci_dev)
  1255. {
  1256. enum sci_status status;
  1257. u16 remote_node_index;
  1258. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1259. scic_remote_device_construct(sci_port, sci_dev);
  1260. /*
  1261. * This information is request to determine how many remote node context
  1262. * entries will be needed to store the remote node.
  1263. */
  1264. sci_dev->is_direct_attached = true;
  1265. status = scic_sds_controller_allocate_remote_node_context(sci_port->owning_controller,
  1266. sci_dev,
  1267. &remote_node_index);
  1268. if (status != SCI_SUCCESS)
  1269. return status;
  1270. sci_dev->rnc.remote_node_index = remote_node_index;
  1271. if (dev->dev_type == SAS_END_DEV)
  1272. sci_dev->has_ready_substate_machine = false;
  1273. else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  1274. sci_dev->has_ready_substate_machine = true;
  1275. sci_base_state_machine_construct(
  1276. &sci_dev->ready_substate_machine,
  1277. &sci_dev->parent,
  1278. scic_sds_stp_remote_device_ready_substate_table,
  1279. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1280. } else if (dev_is_expander(dev)) {
  1281. sci_dev->has_ready_substate_machine = true;
  1282. /* add the SMP ready substate machine construction here */
  1283. sci_base_state_machine_construct(
  1284. &sci_dev->ready_substate_machine,
  1285. &sci_dev->parent,
  1286. scic_sds_smp_remote_device_ready_substate_table,
  1287. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1288. } else
  1289. return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  1290. sci_dev->connection_rate = scic_sds_port_get_max_allowed_speed(sci_port);
  1291. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1292. sci_dev->device_port_width = 1;
  1293. return SCI_SUCCESS;
  1294. }
  1295. /**
  1296. * scic_remote_device_ea_construct() - construct expander attached device
  1297. *
  1298. * Remote node context(s) is/are a global resource allocated by this
  1299. * routine, freed by scic_remote_device_destruct().
  1300. *
  1301. * Returns:
  1302. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1303. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1304. * sata-only controller instance.
  1305. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1306. */
  1307. static enum sci_status scic_remote_device_ea_construct(struct scic_sds_port *sci_port,
  1308. struct scic_sds_remote_device *sci_dev)
  1309. {
  1310. struct scic_sds_controller *scic = sci_port->owning_controller;
  1311. struct domain_device *dev = sci_dev_to_domain(sci_dev);
  1312. enum sci_status status;
  1313. scic_remote_device_construct(sci_port, sci_dev);
  1314. status = scic_sds_controller_allocate_remote_node_context(
  1315. scic, sci_dev, &sci_dev->rnc.remote_node_index);
  1316. if (status != SCI_SUCCESS)
  1317. return status;
  1318. if (dev->dev_type == SAS_END_DEV)
  1319. sci_dev->has_ready_substate_machine = false;
  1320. else if (dev_is_expander(dev)) {
  1321. sci_dev->has_ready_substate_machine = true;
  1322. /* add the SMP ready substate machine construction here */
  1323. sci_base_state_machine_construct(
  1324. &sci_dev->ready_substate_machine,
  1325. &sci_dev->parent,
  1326. scic_sds_smp_remote_device_ready_substate_table,
  1327. SCIC_SDS_SMP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1328. } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  1329. sci_dev->has_ready_substate_machine = true;
  1330. sci_base_state_machine_construct(
  1331. &sci_dev->ready_substate_machine,
  1332. &sci_dev->parent,
  1333. scic_sds_stp_remote_device_ready_substate_table,
  1334. SCIC_SDS_STP_REMOTE_DEVICE_READY_SUBSTATE_IDLE);
  1335. }
  1336. /*
  1337. * For SAS-2 the physical link rate is actually a logical link
  1338. * rate that incorporates multiplexing. The SCU doesn't
  1339. * incorporate multiplexing and for the purposes of the
  1340. * connection the logical link rate is that same as the
  1341. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1342. * one another, so this code works for both situations. */
  1343. sci_dev->connection_rate = min_t(u16, scic_sds_port_get_max_allowed_speed(sci_port),
  1344. dev->linkrate);
  1345. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1346. sci_dev->device_port_width = 1;
  1347. return status;
  1348. }
  1349. /**
  1350. * scic_remote_device_start() - This method will start the supplied remote
  1351. * device. This method enables normal IO requests to flow through to the
  1352. * remote device.
  1353. * @remote_device: This parameter specifies the device to be started.
  1354. * @timeout: This parameter specifies the number of milliseconds in which the
  1355. * start operation should complete.
  1356. *
  1357. * An indication of whether the device was successfully started. SCI_SUCCESS
  1358. * This value is returned if the device was successfully started.
  1359. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1360. * the device when there have been no phys added to it.
  1361. */
  1362. static enum sci_status scic_remote_device_start(struct scic_sds_remote_device *sci_dev,
  1363. u32 timeout)
  1364. {
  1365. return sci_dev->state_handlers->start_handler(sci_dev);
  1366. }
  1367. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1368. struct isci_remote_device *idev)
  1369. {
  1370. struct scic_sds_port *sci_port = iport->sci_port_handle;
  1371. struct isci_host *ihost = iport->isci_host;
  1372. struct domain_device *dev = idev->domain_dev;
  1373. enum sci_status status;
  1374. if (dev->parent && dev_is_expander(dev->parent))
  1375. status = scic_remote_device_ea_construct(sci_port, &idev->sci);
  1376. else
  1377. status = scic_remote_device_da_construct(sci_port, &idev->sci);
  1378. if (status != SCI_SUCCESS) {
  1379. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1380. __func__, status);
  1381. return status;
  1382. }
  1383. /* XXX will be killed with sci_base_object removal */
  1384. sci_object_set_association(&idev->sci, idev);
  1385. /* start the device. */
  1386. status = scic_remote_device_start(&idev->sci, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1387. if (status != SCI_SUCCESS)
  1388. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1389. status);
  1390. return status;
  1391. }
  1392. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1393. {
  1394. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1395. dev_dbg(&ihost->pdev->dev,
  1396. "%s: idev = %p\n", __func__, idev);
  1397. /* Cleanup all requests pending for this device. */
  1398. isci_terminate_pending_requests(ihost, idev, terminating);
  1399. dev_dbg(&ihost->pdev->dev,
  1400. "%s: idev = %p, done\n", __func__, idev);
  1401. }
  1402. /**
  1403. * This function builds the isci_remote_device when a libsas dev_found message
  1404. * is received.
  1405. * @isci_host: This parameter specifies the isci host object.
  1406. * @port: This parameter specifies the isci_port conected to this device.
  1407. *
  1408. * pointer to new isci_remote_device.
  1409. */
  1410. static struct isci_remote_device *
  1411. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1412. {
  1413. struct isci_remote_device *idev;
  1414. int i;
  1415. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1416. idev = &ihost->devices[i];
  1417. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1418. break;
  1419. }
  1420. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1421. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1422. return NULL;
  1423. }
  1424. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1425. return NULL;
  1426. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1427. return NULL;
  1428. isci_remote_device_change_state(idev, isci_freed);
  1429. return idev;
  1430. }
  1431. /**
  1432. * isci_remote_device_ready() - This function is called by the scic when the
  1433. * remote device is ready. We mark the isci device as ready and signal the
  1434. * waiting proccess.
  1435. * @ihost: our valid isci_host
  1436. * @idev: remote device
  1437. *
  1438. */
  1439. void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  1440. {
  1441. dev_dbg(&ihost->pdev->dev,
  1442. "%s: idev = %p\n", __func__, idev);
  1443. isci_remote_device_change_state(idev, isci_ready_for_io);
  1444. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  1445. wake_up(&ihost->eventq);
  1446. }
  1447. /**
  1448. * isci_remote_device_not_ready() - This function is called by the scic when
  1449. * the remote device is not ready. We mark the isci device as ready (not
  1450. * "ready_for_io") and signal the waiting proccess.
  1451. * @isci_host: This parameter specifies the isci host object.
  1452. * @isci_device: This parameter specifies the remote device
  1453. *
  1454. */
  1455. void isci_remote_device_not_ready(struct isci_host *ihost,
  1456. struct isci_remote_device *idev, u32 reason)
  1457. {
  1458. dev_dbg(&ihost->pdev->dev,
  1459. "%s: isci_device = %p\n", __func__, idev);
  1460. if (reason == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
  1461. isci_remote_device_change_state(idev, isci_stopping);
  1462. else
  1463. /* device ready is actually a "not ready for io" state. */
  1464. isci_remote_device_change_state(idev, isci_ready);
  1465. }
  1466. /**
  1467. * isci_remote_device_stop() - This function is called internally to stop the
  1468. * remote device.
  1469. * @isci_host: This parameter specifies the isci host object.
  1470. * @isci_device: This parameter specifies the remote device.
  1471. *
  1472. * The status of the scic request to stop.
  1473. */
  1474. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1475. {
  1476. enum sci_status status;
  1477. unsigned long flags;
  1478. dev_dbg(&ihost->pdev->dev,
  1479. "%s: isci_device = %p\n", __func__, idev);
  1480. isci_remote_device_change_state(idev, isci_stopping);
  1481. /* Kill all outstanding requests. */
  1482. isci_remote_device_nuke_requests(ihost, idev);
  1483. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1484. spin_lock_irqsave(&ihost->scic_lock, flags);
  1485. status = scic_remote_device_stop(&idev->sci, 50);
  1486. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1487. /* Wait for the stop complete callback. */
  1488. if (status == SCI_SUCCESS) {
  1489. wait_for_device_stop(ihost, idev);
  1490. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1491. }
  1492. dev_dbg(&ihost->pdev->dev,
  1493. "%s: idev = %p - after completion wait\n",
  1494. __func__, idev);
  1495. return status;
  1496. }
  1497. /**
  1498. * isci_remote_device_gone() - This function is called by libsas when a domain
  1499. * device is removed.
  1500. * @domain_device: This parameter specifies the libsas domain device.
  1501. *
  1502. */
  1503. void isci_remote_device_gone(struct domain_device *dev)
  1504. {
  1505. struct isci_host *ihost = dev_to_ihost(dev);
  1506. struct isci_remote_device *idev = dev->lldd_dev;
  1507. dev_dbg(&ihost->pdev->dev,
  1508. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1509. __func__, dev, idev, idev->isci_port);
  1510. isci_remote_device_stop(ihost, idev);
  1511. }
  1512. /**
  1513. * isci_remote_device_found() - This function is called by libsas when a remote
  1514. * device is discovered. A remote device object is created and started. the
  1515. * function then sleeps until the sci core device started message is
  1516. * received.
  1517. * @domain_device: This parameter specifies the libsas domain device.
  1518. *
  1519. * status, zero indicates success.
  1520. */
  1521. int isci_remote_device_found(struct domain_device *domain_dev)
  1522. {
  1523. struct isci_host *isci_host = dev_to_ihost(domain_dev);
  1524. struct isci_port *isci_port;
  1525. struct isci_phy *isci_phy;
  1526. struct asd_sas_port *sas_port;
  1527. struct asd_sas_phy *sas_phy;
  1528. struct isci_remote_device *isci_device;
  1529. enum sci_status status;
  1530. dev_dbg(&isci_host->pdev->dev,
  1531. "%s: domain_device = %p\n", __func__, domain_dev);
  1532. wait_for_start(isci_host);
  1533. sas_port = domain_dev->port;
  1534. sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
  1535. port_phy_el);
  1536. isci_phy = to_isci_phy(sas_phy);
  1537. isci_port = isci_phy->isci_port;
  1538. /* we are being called for a device on this port,
  1539. * so it has to come up eventually
  1540. */
  1541. wait_for_completion(&isci_port->start_complete);
  1542. if ((isci_stopping == isci_port_get_state(isci_port)) ||
  1543. (isci_stopped == isci_port_get_state(isci_port)))
  1544. return -ENODEV;
  1545. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1546. if (!isci_device)
  1547. return -ENODEV;
  1548. INIT_LIST_HEAD(&isci_device->node);
  1549. domain_dev->lldd_dev = isci_device;
  1550. isci_device->domain_dev = domain_dev;
  1551. isci_device->isci_port = isci_port;
  1552. isci_remote_device_change_state(isci_device, isci_starting);
  1553. spin_lock_irq(&isci_host->scic_lock);
  1554. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1555. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1556. status = isci_remote_device_construct(isci_port, isci_device);
  1557. spin_unlock_irq(&isci_host->scic_lock);
  1558. dev_dbg(&isci_host->pdev->dev,
  1559. "%s: isci_device = %p\n",
  1560. __func__, isci_device);
  1561. if (status != SCI_SUCCESS) {
  1562. spin_lock_irq(&isci_host->scic_lock);
  1563. isci_remote_device_deconstruct(
  1564. isci_host,
  1565. isci_device
  1566. );
  1567. spin_unlock_irq(&isci_host->scic_lock);
  1568. return -ENODEV;
  1569. }
  1570. /* wait for the device ready callback. */
  1571. wait_for_device_start(isci_host, isci_device);
  1572. return 0;
  1573. }
  1574. /**
  1575. * isci_device_is_reset_pending() - This function will check if there is any
  1576. * pending reset condition on the device.
  1577. * @request: This parameter is the isci_device object.
  1578. *
  1579. * true if there is a reset pending for the device.
  1580. */
  1581. bool isci_device_is_reset_pending(
  1582. struct isci_host *isci_host,
  1583. struct isci_remote_device *isci_device)
  1584. {
  1585. struct isci_request *isci_request;
  1586. struct isci_request *tmp_req;
  1587. bool reset_is_pending = false;
  1588. unsigned long flags;
  1589. dev_dbg(&isci_host->pdev->dev,
  1590. "%s: isci_device = %p\n", __func__, isci_device);
  1591. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1592. /* Check for reset on all pending requests. */
  1593. list_for_each_entry_safe(isci_request, tmp_req,
  1594. &isci_device->reqs_in_process, dev_node) {
  1595. dev_dbg(&isci_host->pdev->dev,
  1596. "%s: isci_device = %p request = %p\n",
  1597. __func__, isci_device, isci_request);
  1598. if (isci_request->ttype == io_task) {
  1599. struct sas_task *task = isci_request_access_task(
  1600. isci_request);
  1601. spin_lock(&task->task_state_lock);
  1602. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1603. reset_is_pending = true;
  1604. spin_unlock(&task->task_state_lock);
  1605. }
  1606. }
  1607. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1608. dev_dbg(&isci_host->pdev->dev,
  1609. "%s: isci_device = %p reset_is_pending = %d\n",
  1610. __func__, isci_device, reset_is_pending);
  1611. return reset_is_pending;
  1612. }
  1613. /**
  1614. * isci_device_clear_reset_pending() - This function will clear if any pending
  1615. * reset condition flags on the device.
  1616. * @request: This parameter is the isci_device object.
  1617. *
  1618. * true if there is a reset pending for the device.
  1619. */
  1620. void isci_device_clear_reset_pending(struct isci_host *ihost, struct isci_remote_device *idev)
  1621. {
  1622. struct isci_request *isci_request;
  1623. struct isci_request *tmp_req;
  1624. unsigned long flags = 0;
  1625. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, ihost=%p\n",
  1626. __func__, idev, ihost);
  1627. spin_lock_irqsave(&ihost->scic_lock, flags);
  1628. /* Clear reset pending on all pending requests. */
  1629. list_for_each_entry_safe(isci_request, tmp_req,
  1630. &idev->reqs_in_process, dev_node) {
  1631. dev_dbg(&ihost->pdev->dev, "%s: idev = %p request = %p\n",
  1632. __func__, idev, isci_request);
  1633. if (isci_request->ttype == io_task) {
  1634. unsigned long flags2;
  1635. struct sas_task *task = isci_request_access_task(
  1636. isci_request);
  1637. spin_lock_irqsave(&task->task_state_lock, flags2);
  1638. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1639. spin_unlock_irqrestore(&task->task_state_lock, flags2);
  1640. }
  1641. }
  1642. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1643. }