remote_device.c 65 KB

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