remote_device.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  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 <scsi/sas.h>
  56. #include <linux/bitops.h>
  57. #include "isci.h"
  58. #include "port.h"
  59. #include "remote_device.h"
  60. #include "request.h"
  61. #include "remote_node_context.h"
  62. #include "scu_event_codes.h"
  63. #include "task.h"
  64. #undef C
  65. #define C(a) (#a)
  66. const char *dev_state_name(enum sci_remote_device_states state)
  67. {
  68. static const char * const strings[] = REMOTE_DEV_STATES;
  69. return strings[state];
  70. }
  71. #undef C
  72. static enum sci_status sci_remote_device_suspend(struct isci_remote_device *idev)
  73. {
  74. return sci_remote_node_context_suspend(&idev->rnc,
  75. SCI_SOFTWARE_SUSPENSION,
  76. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
  77. NULL, NULL);
  78. }
  79. /**
  80. * isci_remote_device_not_ready() - This function is called by the ihost when
  81. * the remote device is not ready. We mark the isci device as ready (not
  82. * "ready_for_io") and signal the waiting proccess.
  83. * @isci_host: This parameter specifies the isci host object.
  84. * @isci_device: This parameter specifies the remote device
  85. *
  86. * sci_lock is held on entrance to this function.
  87. */
  88. static void isci_remote_device_not_ready(struct isci_host *ihost,
  89. struct isci_remote_device *idev, u32 reason)
  90. {
  91. struct isci_request *ireq;
  92. dev_dbg(&ihost->pdev->dev,
  93. "%s: isci_device = %p\n", __func__, idev);
  94. switch (reason) {
  95. case SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED:
  96. set_bit(IDEV_GONE, &idev->flags);
  97. break;
  98. case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
  99. set_bit(IDEV_IO_NCQERROR, &idev->flags);
  100. /* Suspend the remote device so the I/O can be terminated. */
  101. sci_remote_device_suspend(idev);
  102. /* Kill all outstanding requests for the device. */
  103. list_for_each_entry(ireq, &idev->reqs_in_process, dev_node) {
  104. dev_dbg(&ihost->pdev->dev,
  105. "%s: isci_device = %p request = %p\n",
  106. __func__, idev, ireq);
  107. sci_controller_terminate_request(ihost, idev, ireq);
  108. }
  109. /* Fall through into the default case... */
  110. default:
  111. clear_bit(IDEV_IO_READY, &idev->flags);
  112. break;
  113. }
  114. }
  115. /**
  116. * isci_remote_device_ready() - This function is called by the ihost when the
  117. * remote device is ready. We mark the isci device as ready and signal the
  118. * waiting proccess.
  119. * @ihost: our valid isci_host
  120. * @idev: remote device
  121. *
  122. */
  123. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  124. {
  125. dev_dbg(&ihost->pdev->dev,
  126. "%s: idev = %p\n", __func__, idev);
  127. clear_bit(IDEV_IO_NCQERROR, &idev->flags);
  128. set_bit(IDEV_IO_READY, &idev->flags);
  129. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  130. wake_up(&ihost->eventq);
  131. }
  132. static int isci_remote_device_suspendcheck(struct isci_remote_device *idev)
  133. {
  134. return test_bit(IDEV_TXRX_SUSPENDED, &idev->flags)
  135. || !test_bit(IDEV_ALLOCATED, &idev->flags);
  136. }
  137. enum sci_status isci_remote_device_suspend(
  138. struct isci_host *ihost,
  139. struct isci_remote_device *idev)
  140. {
  141. enum sci_status status;
  142. unsigned long flags;
  143. spin_lock_irqsave(&ihost->scic_lock, flags);
  144. if (isci_get_device(idev->domain_dev) == NULL) {
  145. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  146. status = SCI_FAILURE;
  147. } else {
  148. status = sci_remote_device_suspend(idev);
  149. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  150. if (status == SCI_SUCCESS) {
  151. dev_dbg(&ihost->pdev->dev,
  152. "%s: idev=%p, about to wait\n",
  153. __func__, idev);
  154. wait_event(ihost->eventq,
  155. isci_remote_device_suspendcheck(idev));
  156. status = test_bit(IDEV_TXRX_SUSPENDED, &idev->flags)
  157. ? SCI_SUCCESS : SCI_FAILURE;
  158. dev_dbg(&ihost->pdev->dev,
  159. "%s: idev=%p, wait done, device is %s\n",
  160. __func__, idev,
  161. test_bit(IDEV_TXRX_SUSPENDED, &idev->flags)
  162. ? "<suspended>" : "<deallocated!>");
  163. } else
  164. dev_dbg(scirdev_to_dev(idev),
  165. "%s: sci_remote_device_suspend failed, "
  166. "status = %d\n", __func__, status);
  167. isci_put_device(idev);
  168. }
  169. return status;
  170. }
  171. /* called once the remote node context is ready to be freed.
  172. * The remote device can now report that its stop operation is complete. none
  173. */
  174. static void rnc_destruct_done(void *_dev)
  175. {
  176. struct isci_remote_device *idev = _dev;
  177. BUG_ON(idev->started_request_count != 0);
  178. sci_change_state(&idev->sm, SCI_DEV_STOPPED);
  179. }
  180. static enum sci_status sci_remote_device_terminate_requests_checkabort(
  181. struct isci_remote_device *idev,
  182. int check_abort_pending)
  183. {
  184. struct isci_host *ihost = idev->owning_port->owning_controller;
  185. enum sci_status status = SCI_SUCCESS;
  186. u32 i;
  187. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  188. struct isci_request *ireq = ihost->reqs[i];
  189. enum sci_status s;
  190. if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
  191. (ireq->target_device != idev) ||
  192. (check_abort_pending && !test_bit(IREQ_PENDING_ABORT,
  193. &ireq->flags)))
  194. continue;
  195. s = sci_controller_terminate_request(ihost, idev, ireq);
  196. if (s != SCI_SUCCESS)
  197. status = s;
  198. }
  199. return status;
  200. }
  201. enum sci_status sci_remote_device_terminate_requests(
  202. struct isci_remote_device *idev)
  203. {
  204. return sci_remote_device_terminate_requests_checkabort(idev, 0);
  205. }
  206. enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
  207. u32 timeout)
  208. {
  209. struct sci_base_state_machine *sm = &idev->sm;
  210. enum sci_remote_device_states state = sm->current_state_id;
  211. switch (state) {
  212. case SCI_DEV_INITIAL:
  213. case SCI_DEV_FAILED:
  214. case SCI_DEV_FINAL:
  215. default:
  216. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  217. __func__, dev_state_name(state));
  218. return SCI_FAILURE_INVALID_STATE;
  219. case SCI_DEV_STOPPED:
  220. return SCI_SUCCESS;
  221. case SCI_DEV_STARTING:
  222. /* device not started so there had better be no requests */
  223. BUG_ON(idev->started_request_count != 0);
  224. sci_remote_node_context_destruct(&idev->rnc,
  225. rnc_destruct_done, idev);
  226. /* Transition to the stopping state and wait for the
  227. * remote node to complete being posted and invalidated.
  228. */
  229. sci_change_state(sm, SCI_DEV_STOPPING);
  230. return SCI_SUCCESS;
  231. case SCI_DEV_READY:
  232. case SCI_STP_DEV_IDLE:
  233. case SCI_STP_DEV_CMD:
  234. case SCI_STP_DEV_NCQ:
  235. case SCI_STP_DEV_NCQ_ERROR:
  236. case SCI_STP_DEV_AWAIT_RESET:
  237. case SCI_SMP_DEV_IDLE:
  238. case SCI_SMP_DEV_CMD:
  239. sci_change_state(sm, SCI_DEV_STOPPING);
  240. if (idev->started_request_count == 0)
  241. sci_remote_node_context_destruct(&idev->rnc,
  242. rnc_destruct_done,
  243. idev);
  244. else {
  245. sci_remote_device_suspend(idev);
  246. sci_remote_device_terminate_requests(idev);
  247. }
  248. return SCI_SUCCESS;
  249. case SCI_DEV_STOPPING:
  250. /* All requests should have been terminated, but if there is an
  251. * attempt to stop a device already in the stopping state, then
  252. * try again to terminate.
  253. */
  254. return sci_remote_device_terminate_requests(idev);
  255. case SCI_DEV_RESETTING:
  256. sci_change_state(sm, SCI_DEV_STOPPING);
  257. return SCI_SUCCESS;
  258. }
  259. }
  260. enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
  261. {
  262. struct sci_base_state_machine *sm = &idev->sm;
  263. enum sci_remote_device_states state = sm->current_state_id;
  264. switch (state) {
  265. case SCI_DEV_INITIAL:
  266. case SCI_DEV_STOPPED:
  267. case SCI_DEV_STARTING:
  268. case SCI_SMP_DEV_IDLE:
  269. case SCI_SMP_DEV_CMD:
  270. case SCI_DEV_STOPPING:
  271. case SCI_DEV_FAILED:
  272. case SCI_DEV_RESETTING:
  273. case SCI_DEV_FINAL:
  274. default:
  275. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  276. __func__, dev_state_name(state));
  277. return SCI_FAILURE_INVALID_STATE;
  278. case SCI_DEV_READY:
  279. case SCI_STP_DEV_IDLE:
  280. case SCI_STP_DEV_CMD:
  281. case SCI_STP_DEV_NCQ:
  282. case SCI_STP_DEV_NCQ_ERROR:
  283. case SCI_STP_DEV_AWAIT_RESET:
  284. sci_change_state(sm, SCI_DEV_RESETTING);
  285. return SCI_SUCCESS;
  286. }
  287. }
  288. enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
  289. {
  290. struct sci_base_state_machine *sm = &idev->sm;
  291. enum sci_remote_device_states state = sm->current_state_id;
  292. if (state != SCI_DEV_RESETTING) {
  293. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  294. __func__, dev_state_name(state));
  295. return SCI_FAILURE_INVALID_STATE;
  296. }
  297. sci_change_state(sm, SCI_DEV_READY);
  298. return SCI_SUCCESS;
  299. }
  300. enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
  301. u32 frame_index)
  302. {
  303. struct sci_base_state_machine *sm = &idev->sm;
  304. enum sci_remote_device_states state = sm->current_state_id;
  305. struct isci_host *ihost = idev->owning_port->owning_controller;
  306. enum sci_status status;
  307. switch (state) {
  308. case SCI_DEV_INITIAL:
  309. case SCI_DEV_STOPPED:
  310. case SCI_DEV_STARTING:
  311. case SCI_STP_DEV_IDLE:
  312. case SCI_SMP_DEV_IDLE:
  313. case SCI_DEV_FINAL:
  314. default:
  315. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  316. __func__, dev_state_name(state));
  317. /* Return the frame back to the controller */
  318. sci_controller_release_frame(ihost, frame_index);
  319. return SCI_FAILURE_INVALID_STATE;
  320. case SCI_DEV_READY:
  321. case SCI_STP_DEV_NCQ_ERROR:
  322. case SCI_STP_DEV_AWAIT_RESET:
  323. case SCI_DEV_STOPPING:
  324. case SCI_DEV_FAILED:
  325. case SCI_DEV_RESETTING: {
  326. struct isci_request *ireq;
  327. struct ssp_frame_hdr hdr;
  328. void *frame_header;
  329. ssize_t word_cnt;
  330. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  331. frame_index,
  332. &frame_header);
  333. if (status != SCI_SUCCESS)
  334. return status;
  335. word_cnt = sizeof(hdr) / sizeof(u32);
  336. sci_swab32_cpy(&hdr, frame_header, word_cnt);
  337. ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
  338. if (ireq && ireq->target_device == idev) {
  339. /* The IO request is now in charge of releasing the frame */
  340. status = sci_io_request_frame_handler(ireq, frame_index);
  341. } else {
  342. /* We could not map this tag to a valid IO
  343. * request Just toss the frame and continue
  344. */
  345. sci_controller_release_frame(ihost, frame_index);
  346. }
  347. break;
  348. }
  349. case SCI_STP_DEV_NCQ: {
  350. struct dev_to_host_fis *hdr;
  351. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  352. frame_index,
  353. (void **)&hdr);
  354. if (status != SCI_SUCCESS)
  355. return status;
  356. if (hdr->fis_type == FIS_SETDEVBITS &&
  357. (hdr->status & ATA_ERR)) {
  358. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  359. /* TODO Check sactive and complete associated IO if any. */
  360. sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
  361. } else if (hdr->fis_type == FIS_REGD2H &&
  362. (hdr->status & ATA_ERR)) {
  363. /*
  364. * Some devices return D2H FIS when an NCQ error is detected.
  365. * Treat this like an SDB error FIS ready reason.
  366. */
  367. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  368. sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
  369. } else
  370. status = SCI_FAILURE;
  371. sci_controller_release_frame(ihost, frame_index);
  372. break;
  373. }
  374. case SCI_STP_DEV_CMD:
  375. case SCI_SMP_DEV_CMD:
  376. /* The device does not process any UF received from the hardware while
  377. * in this state. All unsolicited frames are forwarded to the io request
  378. * object.
  379. */
  380. status = sci_io_request_frame_handler(idev->working_request, frame_index);
  381. break;
  382. }
  383. return status;
  384. }
  385. static bool is_remote_device_ready(struct isci_remote_device *idev)
  386. {
  387. struct sci_base_state_machine *sm = &idev->sm;
  388. enum sci_remote_device_states state = sm->current_state_id;
  389. switch (state) {
  390. case SCI_DEV_READY:
  391. case SCI_STP_DEV_IDLE:
  392. case SCI_STP_DEV_CMD:
  393. case SCI_STP_DEV_NCQ:
  394. case SCI_STP_DEV_NCQ_ERROR:
  395. case SCI_STP_DEV_AWAIT_RESET:
  396. case SCI_SMP_DEV_IDLE:
  397. case SCI_SMP_DEV_CMD:
  398. return true;
  399. default:
  400. return false;
  401. }
  402. }
  403. /*
  404. * called once the remote node context has transisitioned to a ready
  405. * state (after suspending RX and/or TX due to early D2H fis)
  406. */
  407. static void atapi_remote_device_resume_done(void *_dev)
  408. {
  409. struct isci_remote_device *idev = _dev;
  410. struct isci_request *ireq = idev->working_request;
  411. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  412. }
  413. enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
  414. u32 event_code)
  415. {
  416. enum sci_status status;
  417. switch (scu_get_event_type(event_code)) {
  418. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  419. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  420. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  421. status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
  422. break;
  423. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  424. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  425. status = SCI_SUCCESS;
  426. /* Suspend the associated RNC */
  427. sci_remote_node_context_suspend(
  428. &idev->rnc,
  429. SCI_SOFTWARE_SUSPENSION,
  430. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
  431. NULL, NULL);
  432. dev_dbg(scirdev_to_dev(idev),
  433. "%s: device: %p event code: %x: %s\n",
  434. __func__, idev, event_code,
  435. is_remote_device_ready(idev)
  436. ? "I_T_Nexus_Timeout event"
  437. : "I_T_Nexus_Timeout event in wrong state");
  438. break;
  439. }
  440. /* Else, fall through and treat as unhandled... */
  441. default:
  442. dev_dbg(scirdev_to_dev(idev),
  443. "%s: device: %p event code: %x: %s\n",
  444. __func__, idev, event_code,
  445. is_remote_device_ready(idev)
  446. ? "unexpected event"
  447. : "unexpected event in wrong state");
  448. status = SCI_FAILURE_INVALID_STATE;
  449. break;
  450. }
  451. if (status != SCI_SUCCESS)
  452. return status;
  453. return status;
  454. }
  455. static void sci_remote_device_start_request(struct isci_remote_device *idev,
  456. struct isci_request *ireq,
  457. enum sci_status status)
  458. {
  459. struct isci_port *iport = idev->owning_port;
  460. /* cleanup requests that failed after starting on the port */
  461. if (status != SCI_SUCCESS)
  462. sci_port_complete_io(iport, idev, ireq);
  463. else {
  464. kref_get(&idev->kref);
  465. idev->started_request_count++;
  466. }
  467. }
  468. enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
  469. struct isci_remote_device *idev,
  470. struct isci_request *ireq)
  471. {
  472. struct sci_base_state_machine *sm = &idev->sm;
  473. enum sci_remote_device_states state = sm->current_state_id;
  474. struct isci_port *iport = idev->owning_port;
  475. enum sci_status status;
  476. switch (state) {
  477. case SCI_DEV_INITIAL:
  478. case SCI_DEV_STOPPED:
  479. case SCI_DEV_STARTING:
  480. case SCI_STP_DEV_NCQ_ERROR:
  481. case SCI_DEV_STOPPING:
  482. case SCI_DEV_FAILED:
  483. case SCI_DEV_RESETTING:
  484. case SCI_DEV_FINAL:
  485. default:
  486. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  487. __func__, dev_state_name(state));
  488. return SCI_FAILURE_INVALID_STATE;
  489. case SCI_DEV_READY:
  490. /* attempt to start an io request for this device object. The remote
  491. * device object will issue the start request for the io and if
  492. * successful it will start the request for the port object then
  493. * increment its own request count.
  494. */
  495. status = sci_port_start_io(iport, idev, ireq);
  496. if (status != SCI_SUCCESS)
  497. return status;
  498. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  499. if (status != SCI_SUCCESS)
  500. break;
  501. status = sci_request_start(ireq);
  502. break;
  503. case SCI_STP_DEV_IDLE: {
  504. /* handle the start io operation for a sata device that is in
  505. * the command idle state. - Evalute the type of IO request to
  506. * be started - If its an NCQ request change to NCQ substate -
  507. * If its any other command change to the CMD substate
  508. *
  509. * If this is a softreset we may want to have a different
  510. * substate.
  511. */
  512. enum sci_remote_device_states new_state;
  513. struct sas_task *task = isci_request_access_task(ireq);
  514. status = sci_port_start_io(iport, idev, ireq);
  515. if (status != SCI_SUCCESS)
  516. return status;
  517. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  518. if (status != SCI_SUCCESS)
  519. break;
  520. status = sci_request_start(ireq);
  521. if (status != SCI_SUCCESS)
  522. break;
  523. if (task->ata_task.use_ncq)
  524. new_state = SCI_STP_DEV_NCQ;
  525. else {
  526. idev->working_request = ireq;
  527. new_state = SCI_STP_DEV_CMD;
  528. }
  529. sci_change_state(sm, new_state);
  530. break;
  531. }
  532. case SCI_STP_DEV_NCQ: {
  533. struct sas_task *task = isci_request_access_task(ireq);
  534. if (task->ata_task.use_ncq) {
  535. status = sci_port_start_io(iport, idev, ireq);
  536. if (status != SCI_SUCCESS)
  537. return status;
  538. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  539. if (status != SCI_SUCCESS)
  540. break;
  541. status = sci_request_start(ireq);
  542. } else
  543. return SCI_FAILURE_INVALID_STATE;
  544. break;
  545. }
  546. case SCI_STP_DEV_AWAIT_RESET:
  547. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  548. case SCI_SMP_DEV_IDLE:
  549. status = sci_port_start_io(iport, idev, ireq);
  550. if (status != SCI_SUCCESS)
  551. return status;
  552. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  553. if (status != SCI_SUCCESS)
  554. break;
  555. status = sci_request_start(ireq);
  556. if (status != SCI_SUCCESS)
  557. break;
  558. idev->working_request = ireq;
  559. sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
  560. break;
  561. case SCI_STP_DEV_CMD:
  562. case SCI_SMP_DEV_CMD:
  563. /* device is already handling a command it can not accept new commands
  564. * until this one is complete.
  565. */
  566. return SCI_FAILURE_INVALID_STATE;
  567. }
  568. sci_remote_device_start_request(idev, ireq, status);
  569. return status;
  570. }
  571. static enum sci_status common_complete_io(struct isci_port *iport,
  572. struct isci_remote_device *idev,
  573. struct isci_request *ireq)
  574. {
  575. enum sci_status status;
  576. status = sci_request_complete(ireq);
  577. if (status != SCI_SUCCESS)
  578. return status;
  579. status = sci_port_complete_io(iport, idev, ireq);
  580. if (status != SCI_SUCCESS)
  581. return status;
  582. sci_remote_device_decrement_request_count(idev);
  583. return status;
  584. }
  585. enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
  586. struct isci_remote_device *idev,
  587. struct isci_request *ireq)
  588. {
  589. struct sci_base_state_machine *sm = &idev->sm;
  590. enum sci_remote_device_states state = sm->current_state_id;
  591. struct isci_port *iport = idev->owning_port;
  592. enum sci_status status;
  593. switch (state) {
  594. case SCI_DEV_INITIAL:
  595. case SCI_DEV_STOPPED:
  596. case SCI_DEV_STARTING:
  597. case SCI_STP_DEV_IDLE:
  598. case SCI_SMP_DEV_IDLE:
  599. case SCI_DEV_FAILED:
  600. case SCI_DEV_FINAL:
  601. default:
  602. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  603. __func__, dev_state_name(state));
  604. return SCI_FAILURE_INVALID_STATE;
  605. case SCI_DEV_READY:
  606. case SCI_STP_DEV_AWAIT_RESET:
  607. case SCI_DEV_RESETTING:
  608. status = common_complete_io(iport, idev, ireq);
  609. break;
  610. case SCI_STP_DEV_CMD:
  611. case SCI_STP_DEV_NCQ:
  612. case SCI_STP_DEV_NCQ_ERROR:
  613. case SCI_STP_DEV_ATAPI_ERROR:
  614. status = common_complete_io(iport, idev, ireq);
  615. if (status != SCI_SUCCESS)
  616. break;
  617. if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  618. /* This request causes hardware error, device needs to be Lun Reset.
  619. * So here we force the state machine to IDLE state so the rest IOs
  620. * can reach RNC state handler, these IOs will be completed by RNC with
  621. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  622. */
  623. sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
  624. } else if (idev->started_request_count == 0)
  625. sci_change_state(sm, SCI_STP_DEV_IDLE);
  626. break;
  627. case SCI_SMP_DEV_CMD:
  628. status = common_complete_io(iport, idev, ireq);
  629. if (status != SCI_SUCCESS)
  630. break;
  631. sci_change_state(sm, SCI_SMP_DEV_IDLE);
  632. break;
  633. case SCI_DEV_STOPPING:
  634. status = common_complete_io(iport, idev, ireq);
  635. if (status != SCI_SUCCESS)
  636. break;
  637. if (idev->started_request_count == 0)
  638. sci_remote_node_context_destruct(&idev->rnc,
  639. rnc_destruct_done,
  640. idev);
  641. break;
  642. }
  643. if (status != SCI_SUCCESS)
  644. dev_err(scirdev_to_dev(idev),
  645. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  646. "could not complete\n", __func__, iport,
  647. idev, ireq, status);
  648. else
  649. isci_put_device(idev);
  650. return status;
  651. }
  652. static void sci_remote_device_continue_request(void *dev)
  653. {
  654. struct isci_remote_device *idev = dev;
  655. /* we need to check if this request is still valid to continue. */
  656. if (idev->working_request)
  657. sci_controller_continue_io(idev->working_request);
  658. }
  659. enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
  660. struct isci_remote_device *idev,
  661. struct isci_request *ireq)
  662. {
  663. struct sci_base_state_machine *sm = &idev->sm;
  664. enum sci_remote_device_states state = sm->current_state_id;
  665. struct isci_port *iport = idev->owning_port;
  666. enum sci_status status;
  667. switch (state) {
  668. case SCI_DEV_INITIAL:
  669. case SCI_DEV_STOPPED:
  670. case SCI_DEV_STARTING:
  671. case SCI_SMP_DEV_IDLE:
  672. case SCI_SMP_DEV_CMD:
  673. case SCI_DEV_STOPPING:
  674. case SCI_DEV_FAILED:
  675. case SCI_DEV_RESETTING:
  676. case SCI_DEV_FINAL:
  677. default:
  678. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  679. __func__, dev_state_name(state));
  680. return SCI_FAILURE_INVALID_STATE;
  681. case SCI_STP_DEV_IDLE:
  682. case SCI_STP_DEV_CMD:
  683. case SCI_STP_DEV_NCQ:
  684. case SCI_STP_DEV_NCQ_ERROR:
  685. case SCI_STP_DEV_AWAIT_RESET:
  686. status = sci_port_start_io(iport, idev, ireq);
  687. if (status != SCI_SUCCESS)
  688. return status;
  689. status = sci_remote_node_context_start_task(&idev->rnc, ireq);
  690. if (status != SCI_SUCCESS)
  691. goto out;
  692. status = sci_request_start(ireq);
  693. if (status != SCI_SUCCESS)
  694. goto out;
  695. /* Note: If the remote device state is not IDLE this will
  696. * replace the request that probably resulted in the task
  697. * management request.
  698. */
  699. idev->working_request = ireq;
  700. sci_change_state(sm, SCI_STP_DEV_CMD);
  701. /* The remote node context must cleanup the TCi to NCQ mapping
  702. * table. The only way to do this correctly is to either write
  703. * to the TLCR register or to invalidate and repost the RNC. In
  704. * either case the remote node context state machine will take
  705. * the correct action when the remote node context is suspended
  706. * and later resumed.
  707. */
  708. sci_remote_node_context_suspend(
  709. &idev->rnc, SCI_SOFTWARE_SUSPENSION,
  710. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
  711. sci_remote_node_context_resume(
  712. &idev->rnc, sci_remote_device_continue_request, idev);
  713. out:
  714. sci_remote_device_start_request(idev, ireq, status);
  715. /* We need to let the controller start request handler know that
  716. * it can't post TC yet. We will provide a callback function to
  717. * post TC when RNC gets resumed.
  718. */
  719. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  720. case SCI_DEV_READY:
  721. status = sci_port_start_io(iport, idev, ireq);
  722. if (status != SCI_SUCCESS)
  723. return status;
  724. status = sci_remote_node_context_start_task(&idev->rnc, ireq);
  725. if (status != SCI_SUCCESS)
  726. break;
  727. status = sci_request_start(ireq);
  728. break;
  729. }
  730. sci_remote_device_start_request(idev, ireq, status);
  731. return status;
  732. }
  733. void sci_remote_device_post_request(struct isci_remote_device *idev, u32 request)
  734. {
  735. struct isci_port *iport = idev->owning_port;
  736. u32 context;
  737. context = request |
  738. (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
  739. (iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
  740. idev->rnc.remote_node_index;
  741. sci_controller_post_request(iport->owning_controller, context);
  742. }
  743. /* called once the remote node context has transisitioned to a
  744. * ready state. This is the indication that the remote device object can also
  745. * transition to ready.
  746. */
  747. static void remote_device_resume_done(void *_dev)
  748. {
  749. struct isci_remote_device *idev = _dev;
  750. if (is_remote_device_ready(idev))
  751. return;
  752. /* go 'ready' if we are not already in a ready state */
  753. sci_change_state(&idev->sm, SCI_DEV_READY);
  754. }
  755. static void sci_stp_remote_device_ready_idle_substate_resume_complete_handler(void *_dev)
  756. {
  757. struct isci_remote_device *idev = _dev;
  758. struct isci_host *ihost = idev->owning_port->owning_controller;
  759. /* For NCQ operation we do not issue a isci_remote_device_not_ready().
  760. * As a result, avoid sending the ready notification.
  761. */
  762. if (idev->sm.previous_state_id != SCI_STP_DEV_NCQ)
  763. isci_remote_device_ready(ihost, idev);
  764. }
  765. static void sci_remote_device_initial_state_enter(struct sci_base_state_machine *sm)
  766. {
  767. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  768. /* Initial state is a transitional state to the stopped state */
  769. sci_change_state(&idev->sm, SCI_DEV_STOPPED);
  770. }
  771. /**
  772. * sci_remote_device_destruct() - free remote node context and destruct
  773. * @remote_device: This parameter specifies the remote device to be destructed.
  774. *
  775. * Remote device objects are a limited resource. As such, they must be
  776. * protected. Thus calls to construct and destruct are mutually exclusive and
  777. * non-reentrant. The return value shall indicate if the device was
  778. * successfully destructed or if some failure occurred. enum sci_status This value
  779. * is returned if the device is successfully destructed.
  780. * SCI_FAILURE_INVALID_REMOTE_DEVICE This value is returned if the supplied
  781. * device isn't valid (e.g. it's already been destoryed, the handle isn't
  782. * valid, etc.).
  783. */
  784. static enum sci_status sci_remote_device_destruct(struct isci_remote_device *idev)
  785. {
  786. struct sci_base_state_machine *sm = &idev->sm;
  787. enum sci_remote_device_states state = sm->current_state_id;
  788. struct isci_host *ihost;
  789. if (state != SCI_DEV_STOPPED) {
  790. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  791. __func__, dev_state_name(state));
  792. return SCI_FAILURE_INVALID_STATE;
  793. }
  794. ihost = idev->owning_port->owning_controller;
  795. sci_controller_free_remote_node_context(ihost, idev,
  796. idev->rnc.remote_node_index);
  797. idev->rnc.remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
  798. sci_change_state(sm, SCI_DEV_FINAL);
  799. return SCI_SUCCESS;
  800. }
  801. /**
  802. * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
  803. * @ihost: This parameter specifies the isci host object.
  804. * @idev: This parameter specifies the remote device to be freed.
  805. *
  806. */
  807. static void isci_remote_device_deconstruct(struct isci_host *ihost, struct isci_remote_device *idev)
  808. {
  809. dev_dbg(&ihost->pdev->dev,
  810. "%s: isci_device = %p\n", __func__, idev);
  811. /* There should not be any outstanding io's. All paths to
  812. * here should go through isci_remote_device_nuke_requests.
  813. * If we hit this condition, we will need a way to complete
  814. * io requests in process */
  815. BUG_ON(!list_empty(&idev->reqs_in_process));
  816. sci_remote_device_destruct(idev);
  817. list_del_init(&idev->node);
  818. isci_put_device(idev);
  819. }
  820. static void sci_remote_device_stopped_state_enter(struct sci_base_state_machine *sm)
  821. {
  822. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  823. struct isci_host *ihost = idev->owning_port->owning_controller;
  824. u32 prev_state;
  825. /* If we are entering from the stopping state let the SCI User know that
  826. * the stop operation has completed.
  827. */
  828. prev_state = idev->sm.previous_state_id;
  829. if (prev_state == SCI_DEV_STOPPING)
  830. isci_remote_device_deconstruct(ihost, idev);
  831. sci_controller_remote_device_stopped(ihost, idev);
  832. }
  833. static void sci_remote_device_starting_state_enter(struct sci_base_state_machine *sm)
  834. {
  835. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  836. struct isci_host *ihost = idev->owning_port->owning_controller;
  837. isci_remote_device_not_ready(ihost, idev,
  838. SCIC_REMOTE_DEVICE_NOT_READY_START_REQUESTED);
  839. }
  840. static void sci_remote_device_ready_state_enter(struct sci_base_state_machine *sm)
  841. {
  842. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  843. struct isci_host *ihost = idev->owning_port->owning_controller;
  844. struct domain_device *dev = idev->domain_dev;
  845. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_SATA)) {
  846. sci_change_state(&idev->sm, SCI_STP_DEV_IDLE);
  847. } else if (dev_is_expander(dev)) {
  848. sci_change_state(&idev->sm, SCI_SMP_DEV_IDLE);
  849. } else
  850. isci_remote_device_ready(ihost, idev);
  851. }
  852. static void sci_remote_device_ready_state_exit(struct sci_base_state_machine *sm)
  853. {
  854. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  855. struct domain_device *dev = idev->domain_dev;
  856. if (dev->dev_type == SAS_END_DEV) {
  857. struct isci_host *ihost = idev->owning_port->owning_controller;
  858. isci_remote_device_not_ready(ihost, idev,
  859. SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED);
  860. }
  861. }
  862. static void sci_remote_device_resetting_state_enter(struct sci_base_state_machine *sm)
  863. {
  864. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  865. struct isci_host *ihost = idev->owning_port->owning_controller;
  866. dev_dbg(&ihost->pdev->dev,
  867. "%s: isci_device = %p\n", __func__, idev);
  868. sci_remote_node_context_suspend(
  869. &idev->rnc, SCI_SOFTWARE_SUSPENSION,
  870. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
  871. }
  872. static void sci_remote_device_resetting_state_exit(struct sci_base_state_machine *sm)
  873. {
  874. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  875. struct isci_host *ihost = idev->owning_port->owning_controller;
  876. dev_dbg(&ihost->pdev->dev,
  877. "%s: isci_device = %p\n", __func__, idev);
  878. sci_remote_node_context_resume(&idev->rnc, NULL, NULL);
  879. }
  880. static void sci_stp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  881. {
  882. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  883. idev->working_request = NULL;
  884. if (sci_remote_node_context_is_ready(&idev->rnc)) {
  885. /*
  886. * Since the RNC is ready, it's alright to finish completion
  887. * processing (e.g. signal the remote device is ready). */
  888. sci_stp_remote_device_ready_idle_substate_resume_complete_handler(idev);
  889. } else {
  890. sci_remote_node_context_resume(&idev->rnc,
  891. sci_stp_remote_device_ready_idle_substate_resume_complete_handler,
  892. idev);
  893. }
  894. }
  895. static void sci_stp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  896. {
  897. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  898. struct isci_host *ihost = idev->owning_port->owning_controller;
  899. BUG_ON(idev->working_request == NULL);
  900. isci_remote_device_not_ready(ihost, idev,
  901. SCIC_REMOTE_DEVICE_NOT_READY_SATA_REQUEST_STARTED);
  902. }
  903. static void sci_stp_remote_device_ready_ncq_error_substate_enter(struct sci_base_state_machine *sm)
  904. {
  905. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  906. struct isci_host *ihost = idev->owning_port->owning_controller;
  907. if (idev->not_ready_reason == SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED)
  908. isci_remote_device_not_ready(ihost, idev,
  909. idev->not_ready_reason);
  910. }
  911. static void sci_stp_remote_device_atapi_error_substate_enter(
  912. struct sci_base_state_machine *sm)
  913. {
  914. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  915. /* This state is entered when an I/O is decoded with an error
  916. * condition. By this point the RNC expected suspension state is set.
  917. * The error conditions suspend the device, so unsuspend here if
  918. * possible.
  919. */
  920. sci_remote_node_context_resume(&idev->rnc,
  921. atapi_remote_device_resume_done,
  922. idev);
  923. }
  924. static void sci_smp_remote_device_ready_idle_substate_enter(struct sci_base_state_machine *sm)
  925. {
  926. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  927. struct isci_host *ihost = idev->owning_port->owning_controller;
  928. isci_remote_device_ready(ihost, idev);
  929. }
  930. static void sci_smp_remote_device_ready_cmd_substate_enter(struct sci_base_state_machine *sm)
  931. {
  932. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  933. struct isci_host *ihost = idev->owning_port->owning_controller;
  934. BUG_ON(idev->working_request == NULL);
  935. isci_remote_device_not_ready(ihost, idev,
  936. SCIC_REMOTE_DEVICE_NOT_READY_SMP_REQUEST_STARTED);
  937. }
  938. static void sci_smp_remote_device_ready_cmd_substate_exit(struct sci_base_state_machine *sm)
  939. {
  940. struct isci_remote_device *idev = container_of(sm, typeof(*idev), sm);
  941. idev->working_request = NULL;
  942. }
  943. static const struct sci_base_state sci_remote_device_state_table[] = {
  944. [SCI_DEV_INITIAL] = {
  945. .enter_state = sci_remote_device_initial_state_enter,
  946. },
  947. [SCI_DEV_STOPPED] = {
  948. .enter_state = sci_remote_device_stopped_state_enter,
  949. },
  950. [SCI_DEV_STARTING] = {
  951. .enter_state = sci_remote_device_starting_state_enter,
  952. },
  953. [SCI_DEV_READY] = {
  954. .enter_state = sci_remote_device_ready_state_enter,
  955. .exit_state = sci_remote_device_ready_state_exit
  956. },
  957. [SCI_STP_DEV_IDLE] = {
  958. .enter_state = sci_stp_remote_device_ready_idle_substate_enter,
  959. },
  960. [SCI_STP_DEV_CMD] = {
  961. .enter_state = sci_stp_remote_device_ready_cmd_substate_enter,
  962. },
  963. [SCI_STP_DEV_NCQ] = { },
  964. [SCI_STP_DEV_NCQ_ERROR] = {
  965. .enter_state = sci_stp_remote_device_ready_ncq_error_substate_enter,
  966. },
  967. [SCI_STP_DEV_ATAPI_ERROR] = {
  968. .enter_state = sci_stp_remote_device_atapi_error_substate_enter,
  969. },
  970. [SCI_STP_DEV_AWAIT_RESET] = { },
  971. [SCI_SMP_DEV_IDLE] = {
  972. .enter_state = sci_smp_remote_device_ready_idle_substate_enter,
  973. },
  974. [SCI_SMP_DEV_CMD] = {
  975. .enter_state = sci_smp_remote_device_ready_cmd_substate_enter,
  976. .exit_state = sci_smp_remote_device_ready_cmd_substate_exit,
  977. },
  978. [SCI_DEV_STOPPING] = { },
  979. [SCI_DEV_FAILED] = { },
  980. [SCI_DEV_RESETTING] = {
  981. .enter_state = sci_remote_device_resetting_state_enter,
  982. .exit_state = sci_remote_device_resetting_state_exit
  983. },
  984. [SCI_DEV_FINAL] = { },
  985. };
  986. /**
  987. * sci_remote_device_construct() - common construction
  988. * @sci_port: SAS/SATA port through which this device is accessed.
  989. * @sci_dev: remote device to construct
  990. *
  991. * This routine just performs benign initialization and does not
  992. * allocate the remote_node_context which is left to
  993. * sci_remote_device_[de]a_construct(). sci_remote_device_destruct()
  994. * frees the remote_node_context(s) for the device.
  995. */
  996. static void sci_remote_device_construct(struct isci_port *iport,
  997. struct isci_remote_device *idev)
  998. {
  999. idev->owning_port = iport;
  1000. idev->started_request_count = 0;
  1001. sci_init_sm(&idev->sm, sci_remote_device_state_table, SCI_DEV_INITIAL);
  1002. sci_remote_node_context_construct(&idev->rnc,
  1003. SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX);
  1004. }
  1005. /**
  1006. * sci_remote_device_da_construct() - construct direct attached device.
  1007. *
  1008. * The information (e.g. IAF, Signature FIS, etc.) necessary to build
  1009. * the device is known to the SCI Core since it is contained in the
  1010. * sci_phy object. Remote node context(s) is/are a global resource
  1011. * allocated by this routine, freed by sci_remote_device_destruct().
  1012. *
  1013. * Returns:
  1014. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1015. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1016. * sata-only controller instance.
  1017. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1018. */
  1019. static enum sci_status sci_remote_device_da_construct(struct isci_port *iport,
  1020. struct isci_remote_device *idev)
  1021. {
  1022. enum sci_status status;
  1023. struct sci_port_properties properties;
  1024. sci_remote_device_construct(iport, idev);
  1025. sci_port_get_properties(iport, &properties);
  1026. /* Get accurate port width from port's phy mask for a DA device. */
  1027. idev->device_port_width = hweight32(properties.phy_mask);
  1028. status = sci_controller_allocate_remote_node_context(iport->owning_controller,
  1029. idev,
  1030. &idev->rnc.remote_node_index);
  1031. if (status != SCI_SUCCESS)
  1032. return status;
  1033. idev->connection_rate = sci_port_get_max_allowed_speed(iport);
  1034. return SCI_SUCCESS;
  1035. }
  1036. /**
  1037. * sci_remote_device_ea_construct() - construct expander attached device
  1038. *
  1039. * Remote node context(s) is/are a global resource allocated by this
  1040. * routine, freed by sci_remote_device_destruct().
  1041. *
  1042. * Returns:
  1043. * SCI_FAILURE_DEVICE_EXISTS - device has already been constructed.
  1044. * SCI_FAILURE_UNSUPPORTED_PROTOCOL - e.g. sas device attached to
  1045. * sata-only controller instance.
  1046. * SCI_FAILURE_INSUFFICIENT_RESOURCES - remote node contexts exhausted.
  1047. */
  1048. static enum sci_status sci_remote_device_ea_construct(struct isci_port *iport,
  1049. struct isci_remote_device *idev)
  1050. {
  1051. struct domain_device *dev = idev->domain_dev;
  1052. enum sci_status status;
  1053. sci_remote_device_construct(iport, idev);
  1054. status = sci_controller_allocate_remote_node_context(iport->owning_controller,
  1055. idev,
  1056. &idev->rnc.remote_node_index);
  1057. if (status != SCI_SUCCESS)
  1058. return status;
  1059. /* For SAS-2 the physical link rate is actually a logical link
  1060. * rate that incorporates multiplexing. The SCU doesn't
  1061. * incorporate multiplexing and for the purposes of the
  1062. * connection the logical link rate is that same as the
  1063. * physical. Furthermore, the SAS-2 and SAS-1.1 fields overlay
  1064. * one another, so this code works for both situations.
  1065. */
  1066. idev->connection_rate = min_t(u16, sci_port_get_max_allowed_speed(iport),
  1067. dev->linkrate);
  1068. /* / @todo Should I assign the port width by reading all of the phys on the port? */
  1069. idev->device_port_width = 1;
  1070. return SCI_SUCCESS;
  1071. }
  1072. enum sci_status sci_remote_device_resume(
  1073. struct isci_remote_device *idev,
  1074. scics_sds_remote_node_context_callback cb_fn,
  1075. void *cb_p)
  1076. {
  1077. enum sci_status status;
  1078. status = sci_remote_node_context_resume(&idev->rnc, cb_fn, cb_p);
  1079. if (status != SCI_SUCCESS)
  1080. dev_dbg(scirdev_to_dev(idev), "%s: failed to resume: %d\n",
  1081. __func__, status);
  1082. return status;
  1083. }
  1084. enum sci_status isci_remote_device_resume(
  1085. struct isci_host *ihost,
  1086. struct isci_remote_device *idev,
  1087. scics_sds_remote_node_context_callback cb_fn,
  1088. void *cb_p)
  1089. {
  1090. unsigned long flags;
  1091. enum sci_status status;
  1092. spin_lock_irqsave(&ihost->scic_lock, flags);
  1093. status = sci_remote_device_resume(idev, cb_fn, cb_p);
  1094. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1095. return status;
  1096. }
  1097. /**
  1098. * sci_remote_device_start() - This method will start the supplied remote
  1099. * device. This method enables normal IO requests to flow through to the
  1100. * remote device.
  1101. * @remote_device: This parameter specifies the device to be started.
  1102. * @timeout: This parameter specifies the number of milliseconds in which the
  1103. * start operation should complete.
  1104. *
  1105. * An indication of whether the device was successfully started. SCI_SUCCESS
  1106. * This value is returned if the device was successfully started.
  1107. * SCI_FAILURE_INVALID_PHY This value is returned if the user attempts to start
  1108. * the device when there have been no phys added to it.
  1109. */
  1110. static enum sci_status sci_remote_device_start(struct isci_remote_device *idev,
  1111. u32 timeout)
  1112. {
  1113. struct sci_base_state_machine *sm = &idev->sm;
  1114. enum sci_remote_device_states state = sm->current_state_id;
  1115. enum sci_status status;
  1116. if (state != SCI_DEV_STOPPED) {
  1117. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  1118. __func__, dev_state_name(state));
  1119. return SCI_FAILURE_INVALID_STATE;
  1120. }
  1121. status = sci_remote_device_resume(idev, remote_device_resume_done,
  1122. idev);
  1123. if (status != SCI_SUCCESS)
  1124. return status;
  1125. sci_change_state(sm, SCI_DEV_STARTING);
  1126. return SCI_SUCCESS;
  1127. }
  1128. static enum sci_status isci_remote_device_construct(struct isci_port *iport,
  1129. struct isci_remote_device *idev)
  1130. {
  1131. struct isci_host *ihost = iport->isci_host;
  1132. struct domain_device *dev = idev->domain_dev;
  1133. enum sci_status status;
  1134. if (dev->parent && dev_is_expander(dev->parent))
  1135. status = sci_remote_device_ea_construct(iport, idev);
  1136. else
  1137. status = sci_remote_device_da_construct(iport, idev);
  1138. if (status != SCI_SUCCESS) {
  1139. dev_dbg(&ihost->pdev->dev, "%s: construct failed: %d\n",
  1140. __func__, status);
  1141. return status;
  1142. }
  1143. /* start the device. */
  1144. status = sci_remote_device_start(idev, ISCI_REMOTE_DEVICE_START_TIMEOUT);
  1145. if (status != SCI_SUCCESS)
  1146. dev_warn(&ihost->pdev->dev, "remote device start failed: %d\n",
  1147. status);
  1148. return status;
  1149. }
  1150. void isci_remote_device_nuke_requests(struct isci_host *ihost, struct isci_remote_device *idev)
  1151. {
  1152. DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
  1153. dev_dbg(&ihost->pdev->dev,
  1154. "%s: idev = %p\n", __func__, idev);
  1155. /* Cleanup all requests pending for this device. */
  1156. isci_terminate_pending_requests(ihost, idev);
  1157. dev_dbg(&ihost->pdev->dev,
  1158. "%s: idev = %p, done\n", __func__, idev);
  1159. }
  1160. /**
  1161. * This function builds the isci_remote_device when a libsas dev_found message
  1162. * is received.
  1163. * @isci_host: This parameter specifies the isci host object.
  1164. * @port: This parameter specifies the isci_port conected to this device.
  1165. *
  1166. * pointer to new isci_remote_device.
  1167. */
  1168. static struct isci_remote_device *
  1169. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1170. {
  1171. struct isci_remote_device *idev;
  1172. int i;
  1173. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1174. idev = &ihost->devices[i];
  1175. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1176. break;
  1177. }
  1178. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1179. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1180. return NULL;
  1181. }
  1182. if (WARN_ONCE(!list_empty(&idev->reqs_in_process), "found requests in process\n"))
  1183. return NULL;
  1184. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1185. return NULL;
  1186. return idev;
  1187. }
  1188. void isci_remote_device_release(struct kref *kref)
  1189. {
  1190. struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
  1191. struct isci_host *ihost = idev->isci_port->isci_host;
  1192. idev->domain_dev = NULL;
  1193. idev->isci_port = NULL;
  1194. clear_bit(IDEV_START_PENDING, &idev->flags);
  1195. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1196. clear_bit(IDEV_IO_READY, &idev->flags);
  1197. clear_bit(IDEV_GONE, &idev->flags);
  1198. smp_mb__before_clear_bit();
  1199. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1200. wake_up(&ihost->eventq);
  1201. }
  1202. /**
  1203. * isci_remote_device_stop() - This function is called internally to stop the
  1204. * remote device.
  1205. * @isci_host: This parameter specifies the isci host object.
  1206. * @isci_device: This parameter specifies the remote device.
  1207. *
  1208. * The status of the ihost request to stop.
  1209. */
  1210. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1211. {
  1212. enum sci_status status;
  1213. unsigned long flags;
  1214. dev_dbg(&ihost->pdev->dev,
  1215. "%s: isci_device = %p\n", __func__, idev);
  1216. spin_lock_irqsave(&ihost->scic_lock, flags);
  1217. idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
  1218. set_bit(IDEV_GONE, &idev->flags);
  1219. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1220. status = sci_remote_device_stop(idev, 50);
  1221. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1222. /* Wait for the stop complete callback. */
  1223. if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
  1224. /* nothing to wait for */;
  1225. else
  1226. wait_for_device_stop(ihost, idev);
  1227. dev_dbg(&ihost->pdev->dev,
  1228. "%s: isci_device = %p, waiting done.\n", __func__, idev);
  1229. return status;
  1230. }
  1231. /**
  1232. * isci_remote_device_gone() - This function is called by libsas when a domain
  1233. * device is removed.
  1234. * @domain_device: This parameter specifies the libsas domain device.
  1235. *
  1236. */
  1237. void isci_remote_device_gone(struct domain_device *dev)
  1238. {
  1239. struct isci_host *ihost = dev_to_ihost(dev);
  1240. struct isci_remote_device *idev = dev->lldd_dev;
  1241. dev_dbg(&ihost->pdev->dev,
  1242. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1243. __func__, dev, idev, idev->isci_port);
  1244. isci_remote_device_stop(ihost, idev);
  1245. }
  1246. /**
  1247. * isci_remote_device_found() - This function is called by libsas when a remote
  1248. * device is discovered. A remote device object is created and started. the
  1249. * function then sleeps until the sci core device started message is
  1250. * received.
  1251. * @domain_device: This parameter specifies the libsas domain device.
  1252. *
  1253. * status, zero indicates success.
  1254. */
  1255. int isci_remote_device_found(struct domain_device *dev)
  1256. {
  1257. struct isci_host *isci_host = dev_to_ihost(dev);
  1258. struct isci_port *isci_port = dev->port->lldd_port;
  1259. struct isci_remote_device *isci_device;
  1260. enum sci_status status;
  1261. dev_dbg(&isci_host->pdev->dev,
  1262. "%s: domain_device = %p\n", __func__, dev);
  1263. if (!isci_port)
  1264. return -ENODEV;
  1265. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1266. if (!isci_device)
  1267. return -ENODEV;
  1268. kref_init(&isci_device->kref);
  1269. INIT_LIST_HEAD(&isci_device->node);
  1270. spin_lock_irq(&isci_host->scic_lock);
  1271. isci_device->domain_dev = dev;
  1272. isci_device->isci_port = isci_port;
  1273. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1274. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1275. status = isci_remote_device_construct(isci_port, isci_device);
  1276. dev_dbg(&isci_host->pdev->dev,
  1277. "%s: isci_device = %p\n",
  1278. __func__, isci_device);
  1279. if (status == SCI_SUCCESS) {
  1280. /* device came up, advertise it to the world */
  1281. dev->lldd_dev = isci_device;
  1282. } else
  1283. isci_put_device(isci_device);
  1284. spin_unlock_irq(&isci_host->scic_lock);
  1285. /* wait for the device ready callback. */
  1286. wait_for_device_start(isci_host, isci_device);
  1287. return status == SCI_SUCCESS ? 0 : -ENODEV;
  1288. }
  1289. enum sci_status isci_remote_device_reset(
  1290. struct isci_host *ihost,
  1291. struct isci_remote_device *idev)
  1292. {
  1293. unsigned long flags;
  1294. enum sci_status status;
  1295. /* Put the device into a reset state so the suspension will not
  1296. * automatically resume.
  1297. */
  1298. spin_lock_irqsave(&ihost->scic_lock, flags);
  1299. status = sci_remote_device_reset(idev);
  1300. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1301. if (status != SCI_SUCCESS) {
  1302. dev_dbg(&ihost->pdev->dev,
  1303. "%s: sci_remote_device_reset(%p) returned %d!\n",
  1304. __func__, idev, status);
  1305. return status;
  1306. }
  1307. /* Wait for the device suspend. */
  1308. status = isci_remote_device_suspend(ihost, idev);
  1309. if (status != SCI_SUCCESS) {
  1310. dev_dbg(&ihost->pdev->dev,
  1311. "%s: isci_remote_device_suspend(%p) returned %d!\n",
  1312. __func__, idev, status);
  1313. }
  1314. return status;
  1315. }
  1316. int isci_remote_device_is_safe_to_abort(
  1317. struct isci_remote_device *idev)
  1318. {
  1319. return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
  1320. }
  1321. enum sci_status sci_remote_device_abort_requests_pending_abort(
  1322. struct isci_remote_device *idev)
  1323. {
  1324. return sci_remote_device_terminate_requests_checkabort(idev, 1);
  1325. }
  1326. enum sci_status isci_remote_device_reset_complete(
  1327. struct isci_host *ihost,
  1328. struct isci_remote_device *idev)
  1329. {
  1330. unsigned long flags;
  1331. enum sci_status status;
  1332. spin_lock_irqsave(&ihost->scic_lock, flags);
  1333. status = sci_remote_device_reset_complete(idev);
  1334. sci_remote_device_resume(idev, NULL, NULL);
  1335. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1336. return status;
  1337. }