remote_device.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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_ready() - This function is called by the ihost when the
  81. * remote device is ready. We mark the isci device as ready and signal the
  82. * waiting proccess.
  83. * @ihost: our valid isci_host
  84. * @idev: remote device
  85. *
  86. */
  87. static void isci_remote_device_ready(struct isci_host *ihost, struct isci_remote_device *idev)
  88. {
  89. dev_dbg(&ihost->pdev->dev,
  90. "%s: idev = %p\n", __func__, idev);
  91. clear_bit(IDEV_IO_NCQERROR, &idev->flags);
  92. set_bit(IDEV_IO_READY, &idev->flags);
  93. if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
  94. wake_up(&ihost->eventq);
  95. }
  96. static enum sci_status sci_remote_device_terminate_req(
  97. struct isci_host *ihost,
  98. struct isci_remote_device *idev,
  99. int check_abort,
  100. struct isci_request *ireq)
  101. {
  102. dev_dbg(&ihost->pdev->dev,
  103. "%s: idev=%p; flags=%lx; req=%p; req target=%p\n",
  104. __func__, idev, idev->flags, ireq, ireq->target_device);
  105. if (!test_bit(IREQ_ACTIVE, &ireq->flags) ||
  106. (ireq->target_device != idev) ||
  107. (check_abort && !test_bit(IREQ_PENDING_ABORT, &ireq->flags)))
  108. return SCI_SUCCESS;
  109. set_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags);
  110. return sci_controller_terminate_request(ihost, idev, ireq);
  111. }
  112. static enum sci_status sci_remote_device_terminate_reqs_checkabort(
  113. struct isci_remote_device *idev,
  114. int chk)
  115. {
  116. struct isci_host *ihost = idev->owning_port->owning_controller;
  117. enum sci_status status = SCI_SUCCESS;
  118. u32 i;
  119. for (i = 0; i < SCI_MAX_IO_REQUESTS; i++) {
  120. struct isci_request *ireq = ihost->reqs[i];
  121. enum sci_status s;
  122. s = sci_remote_device_terminate_req(ihost, idev, chk, ireq);
  123. if (s != SCI_SUCCESS)
  124. status = s;
  125. }
  126. return status;
  127. }
  128. enum sci_status isci_remote_device_terminate_requests(
  129. struct isci_host *ihost,
  130. struct isci_remote_device *idev,
  131. struct isci_request *ireq)
  132. {
  133. enum sci_status status = SCI_SUCCESS;
  134. unsigned long flags;
  135. spin_lock_irqsave(&ihost->scic_lock, flags);
  136. if (isci_get_device(idev) == NULL) {
  137. dev_dbg(&ihost->pdev->dev, "%s: failed isci_get_device(idev=%p)\n",
  138. __func__, idev);
  139. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  140. status = SCI_FAILURE;
  141. } else {
  142. dev_dbg(&ihost->pdev->dev,
  143. "%s: idev=%p, ireq=%p; started_request_count=%d, "
  144. "about to wait\n",
  145. __func__, idev, ireq, idev->started_request_count);
  146. if (ireq) {
  147. /* Terminate a specific TC. */
  148. sci_remote_device_terminate_req(ihost, idev, 0, ireq);
  149. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  150. wait_event(ihost->eventq, !test_bit(IREQ_ACTIVE,
  151. &ireq->flags));
  152. } else {
  153. /* Terminate all TCs. */
  154. sci_remote_device_terminate_requests(idev);
  155. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  156. wait_event(ihost->eventq,
  157. idev->started_request_count == 0);
  158. }
  159. dev_dbg(&ihost->pdev->dev, "%s: idev=%p, wait done\n",
  160. __func__, idev);
  161. isci_put_device(idev);
  162. }
  163. return status;
  164. }
  165. /**
  166. * isci_remote_device_not_ready() - This function is called by the ihost when
  167. * the remote device is not ready. We mark the isci device as ready (not
  168. * "ready_for_io") and signal the waiting proccess.
  169. * @isci_host: This parameter specifies the isci host object.
  170. * @isci_device: This parameter specifies the remote device
  171. *
  172. * sci_lock is held on entrance to this function.
  173. */
  174. static void isci_remote_device_not_ready(struct isci_host *ihost,
  175. struct isci_remote_device *idev,
  176. u32 reason)
  177. {
  178. dev_dbg(&ihost->pdev->dev,
  179. "%s: isci_device = %p; reason = %d\n", __func__, idev, reason);
  180. switch (reason) {
  181. case SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED:
  182. set_bit(IDEV_IO_NCQERROR, &idev->flags);
  183. /* Suspend the remote device so the I/O can be terminated. */
  184. sci_remote_device_suspend(idev);
  185. /* Kill all outstanding requests for the device. */
  186. sci_remote_device_terminate_requests(idev);
  187. /* Fall through into the default case... */
  188. default:
  189. clear_bit(IDEV_IO_READY, &idev->flags);
  190. break;
  191. }
  192. }
  193. /* called once the remote node context is ready to be freed.
  194. * The remote device can now report that its stop operation is complete. none
  195. */
  196. static void rnc_destruct_done(void *_dev)
  197. {
  198. struct isci_remote_device *idev = _dev;
  199. BUG_ON(idev->started_request_count != 0);
  200. sci_change_state(&idev->sm, SCI_DEV_STOPPED);
  201. }
  202. enum sci_status sci_remote_device_terminate_requests(
  203. struct isci_remote_device *idev)
  204. {
  205. return sci_remote_device_terminate_reqs_checkabort(idev, 0);
  206. }
  207. enum sci_status sci_remote_device_stop(struct isci_remote_device *idev,
  208. u32 timeout)
  209. {
  210. struct sci_base_state_machine *sm = &idev->sm;
  211. enum sci_remote_device_states state = sm->current_state_id;
  212. switch (state) {
  213. case SCI_DEV_INITIAL:
  214. case SCI_DEV_FAILED:
  215. case SCI_DEV_FINAL:
  216. default:
  217. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  218. __func__, dev_state_name(state));
  219. return SCI_FAILURE_INVALID_STATE;
  220. case SCI_DEV_STOPPED:
  221. return SCI_SUCCESS;
  222. case SCI_DEV_STARTING:
  223. /* device not started so there had better be no requests */
  224. BUG_ON(idev->started_request_count != 0);
  225. sci_remote_node_context_destruct(&idev->rnc,
  226. rnc_destruct_done, idev);
  227. /* Transition to the stopping state and wait for the
  228. * remote node to complete being posted and invalidated.
  229. */
  230. sci_change_state(sm, SCI_DEV_STOPPING);
  231. return SCI_SUCCESS;
  232. case SCI_DEV_READY:
  233. case SCI_STP_DEV_IDLE:
  234. case SCI_STP_DEV_CMD:
  235. case SCI_STP_DEV_NCQ:
  236. case SCI_STP_DEV_NCQ_ERROR:
  237. case SCI_STP_DEV_AWAIT_RESET:
  238. case SCI_SMP_DEV_IDLE:
  239. case SCI_SMP_DEV_CMD:
  240. sci_change_state(sm, SCI_DEV_STOPPING);
  241. if (idev->started_request_count == 0)
  242. sci_remote_node_context_destruct(&idev->rnc,
  243. rnc_destruct_done,
  244. idev);
  245. else {
  246. sci_remote_device_suspend(idev);
  247. sci_remote_device_terminate_requests(idev);
  248. }
  249. return SCI_SUCCESS;
  250. case SCI_DEV_STOPPING:
  251. /* All requests should have been terminated, but if there is an
  252. * attempt to stop a device already in the stopping state, then
  253. * try again to terminate.
  254. */
  255. return sci_remote_device_terminate_requests(idev);
  256. case SCI_DEV_RESETTING:
  257. sci_change_state(sm, SCI_DEV_STOPPING);
  258. return SCI_SUCCESS;
  259. }
  260. }
  261. enum sci_status sci_remote_device_reset(struct isci_remote_device *idev)
  262. {
  263. struct sci_base_state_machine *sm = &idev->sm;
  264. enum sci_remote_device_states state = sm->current_state_id;
  265. switch (state) {
  266. case SCI_DEV_INITIAL:
  267. case SCI_DEV_STOPPED:
  268. case SCI_DEV_STARTING:
  269. case SCI_SMP_DEV_IDLE:
  270. case SCI_SMP_DEV_CMD:
  271. case SCI_DEV_STOPPING:
  272. case SCI_DEV_FAILED:
  273. case SCI_DEV_RESETTING:
  274. case SCI_DEV_FINAL:
  275. default:
  276. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  277. __func__, dev_state_name(state));
  278. return SCI_FAILURE_INVALID_STATE;
  279. case SCI_DEV_READY:
  280. case SCI_STP_DEV_IDLE:
  281. case SCI_STP_DEV_CMD:
  282. case SCI_STP_DEV_NCQ:
  283. case SCI_STP_DEV_NCQ_ERROR:
  284. case SCI_STP_DEV_AWAIT_RESET:
  285. sci_change_state(sm, SCI_DEV_RESETTING);
  286. return SCI_SUCCESS;
  287. }
  288. }
  289. enum sci_status sci_remote_device_reset_complete(struct isci_remote_device *idev)
  290. {
  291. struct sci_base_state_machine *sm = &idev->sm;
  292. enum sci_remote_device_states state = sm->current_state_id;
  293. if (state != SCI_DEV_RESETTING) {
  294. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  295. __func__, dev_state_name(state));
  296. return SCI_FAILURE_INVALID_STATE;
  297. }
  298. sci_change_state(sm, SCI_DEV_READY);
  299. return SCI_SUCCESS;
  300. }
  301. enum sci_status sci_remote_device_frame_handler(struct isci_remote_device *idev,
  302. u32 frame_index)
  303. {
  304. struct sci_base_state_machine *sm = &idev->sm;
  305. enum sci_remote_device_states state = sm->current_state_id;
  306. struct isci_host *ihost = idev->owning_port->owning_controller;
  307. enum sci_status status;
  308. switch (state) {
  309. case SCI_DEV_INITIAL:
  310. case SCI_DEV_STOPPED:
  311. case SCI_DEV_STARTING:
  312. case SCI_STP_DEV_IDLE:
  313. case SCI_SMP_DEV_IDLE:
  314. case SCI_DEV_FINAL:
  315. default:
  316. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  317. __func__, dev_state_name(state));
  318. /* Return the frame back to the controller */
  319. sci_controller_release_frame(ihost, frame_index);
  320. return SCI_FAILURE_INVALID_STATE;
  321. case SCI_DEV_READY:
  322. case SCI_STP_DEV_NCQ_ERROR:
  323. case SCI_STP_DEV_AWAIT_RESET:
  324. case SCI_DEV_STOPPING:
  325. case SCI_DEV_FAILED:
  326. case SCI_DEV_RESETTING: {
  327. struct isci_request *ireq;
  328. struct ssp_frame_hdr hdr;
  329. void *frame_header;
  330. ssize_t word_cnt;
  331. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  332. frame_index,
  333. &frame_header);
  334. if (status != SCI_SUCCESS)
  335. return status;
  336. word_cnt = sizeof(hdr) / sizeof(u32);
  337. sci_swab32_cpy(&hdr, frame_header, word_cnt);
  338. ireq = sci_request_by_tag(ihost, be16_to_cpu(hdr.tag));
  339. if (ireq && ireq->target_device == idev) {
  340. /* The IO request is now in charge of releasing the frame */
  341. status = sci_io_request_frame_handler(ireq, frame_index);
  342. } else {
  343. /* We could not map this tag to a valid IO
  344. * request Just toss the frame and continue
  345. */
  346. sci_controller_release_frame(ihost, frame_index);
  347. }
  348. break;
  349. }
  350. case SCI_STP_DEV_NCQ: {
  351. struct dev_to_host_fis *hdr;
  352. status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
  353. frame_index,
  354. (void **)&hdr);
  355. if (status != SCI_SUCCESS)
  356. return status;
  357. if (hdr->fis_type == FIS_SETDEVBITS &&
  358. (hdr->status & ATA_ERR)) {
  359. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  360. /* TODO Check sactive and complete associated IO if any. */
  361. sci_change_state(sm, SCI_STP_DEV_NCQ_ERROR);
  362. } else if (hdr->fis_type == FIS_REGD2H &&
  363. (hdr->status & ATA_ERR)) {
  364. /*
  365. * Some devices return D2H FIS when an NCQ error is detected.
  366. * Treat this like an SDB error FIS ready reason.
  367. */
  368. idev->not_ready_reason = SCIC_REMOTE_DEVICE_NOT_READY_SATA_SDB_ERROR_FIS_RECEIVED;
  369. sci_change_state(&idev->sm, SCI_STP_DEV_NCQ_ERROR);
  370. } else
  371. status = SCI_FAILURE;
  372. sci_controller_release_frame(ihost, frame_index);
  373. break;
  374. }
  375. case SCI_STP_DEV_CMD:
  376. case SCI_SMP_DEV_CMD:
  377. /* The device does not process any UF received from the hardware while
  378. * in this state. All unsolicited frames are forwarded to the io request
  379. * object.
  380. */
  381. status = sci_io_request_frame_handler(idev->working_request, frame_index);
  382. break;
  383. }
  384. return status;
  385. }
  386. static bool is_remote_device_ready(struct isci_remote_device *idev)
  387. {
  388. struct sci_base_state_machine *sm = &idev->sm;
  389. enum sci_remote_device_states state = sm->current_state_id;
  390. switch (state) {
  391. case SCI_DEV_READY:
  392. case SCI_STP_DEV_IDLE:
  393. case SCI_STP_DEV_CMD:
  394. case SCI_STP_DEV_NCQ:
  395. case SCI_STP_DEV_NCQ_ERROR:
  396. case SCI_STP_DEV_AWAIT_RESET:
  397. case SCI_SMP_DEV_IDLE:
  398. case SCI_SMP_DEV_CMD:
  399. return true;
  400. default:
  401. return false;
  402. }
  403. }
  404. /*
  405. * called once the remote node context has transisitioned to a ready
  406. * state (after suspending RX and/or TX due to early D2H fis)
  407. */
  408. static void atapi_remote_device_resume_done(void *_dev)
  409. {
  410. struct isci_remote_device *idev = _dev;
  411. struct isci_request *ireq = idev->working_request;
  412. sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
  413. }
  414. enum sci_status sci_remote_device_event_handler(struct isci_remote_device *idev,
  415. u32 event_code)
  416. {
  417. enum sci_status status;
  418. switch (scu_get_event_type(event_code)) {
  419. case SCU_EVENT_TYPE_RNC_OPS_MISC:
  420. case SCU_EVENT_TYPE_RNC_SUSPEND_TX:
  421. case SCU_EVENT_TYPE_RNC_SUSPEND_TX_RX:
  422. status = sci_remote_node_context_event_handler(&idev->rnc, event_code);
  423. break;
  424. case SCU_EVENT_TYPE_PTX_SCHEDULE_EVENT:
  425. if (scu_get_event_code(event_code) == SCU_EVENT_IT_NEXUS_TIMEOUT) {
  426. status = SCI_SUCCESS;
  427. /* Suspend the associated RNC */
  428. sci_remote_node_context_suspend(
  429. &idev->rnc,
  430. SCI_SOFTWARE_SUSPENSION,
  431. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT,
  432. NULL, NULL);
  433. dev_dbg(scirdev_to_dev(idev),
  434. "%s: device: %p event code: %x: %s\n",
  435. __func__, idev, event_code,
  436. is_remote_device_ready(idev)
  437. ? "I_T_Nexus_Timeout event"
  438. : "I_T_Nexus_Timeout event in wrong state");
  439. break;
  440. }
  441. /* Else, fall through and treat as unhandled... */
  442. default:
  443. dev_dbg(scirdev_to_dev(idev),
  444. "%s: device: %p event code: %x: %s\n",
  445. __func__, idev, event_code,
  446. is_remote_device_ready(idev)
  447. ? "unexpected event"
  448. : "unexpected event in wrong state");
  449. status = SCI_FAILURE_INVALID_STATE;
  450. break;
  451. }
  452. if (status != SCI_SUCCESS)
  453. return status;
  454. return status;
  455. }
  456. static void sci_remote_device_start_request(struct isci_remote_device *idev,
  457. struct isci_request *ireq,
  458. enum sci_status status)
  459. {
  460. struct isci_port *iport = idev->owning_port;
  461. /* cleanup requests that failed after starting on the port */
  462. if (status != SCI_SUCCESS)
  463. sci_port_complete_io(iport, idev, ireq);
  464. else {
  465. kref_get(&idev->kref);
  466. idev->started_request_count++;
  467. }
  468. }
  469. enum sci_status sci_remote_device_start_io(struct isci_host *ihost,
  470. struct isci_remote_device *idev,
  471. struct isci_request *ireq)
  472. {
  473. struct sci_base_state_machine *sm = &idev->sm;
  474. enum sci_remote_device_states state = sm->current_state_id;
  475. struct isci_port *iport = idev->owning_port;
  476. enum sci_status status;
  477. switch (state) {
  478. case SCI_DEV_INITIAL:
  479. case SCI_DEV_STOPPED:
  480. case SCI_DEV_STARTING:
  481. case SCI_STP_DEV_NCQ_ERROR:
  482. case SCI_DEV_STOPPING:
  483. case SCI_DEV_FAILED:
  484. case SCI_DEV_RESETTING:
  485. case SCI_DEV_FINAL:
  486. default:
  487. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  488. __func__, dev_state_name(state));
  489. return SCI_FAILURE_INVALID_STATE;
  490. case SCI_DEV_READY:
  491. /* attempt to start an io request for this device object. The remote
  492. * device object will issue the start request for the io and if
  493. * successful it will start the request for the port object then
  494. * increment its own request count.
  495. */
  496. status = sci_port_start_io(iport, idev, ireq);
  497. if (status != SCI_SUCCESS)
  498. return status;
  499. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  500. if (status != SCI_SUCCESS)
  501. break;
  502. status = sci_request_start(ireq);
  503. break;
  504. case SCI_STP_DEV_IDLE: {
  505. /* handle the start io operation for a sata device that is in
  506. * the command idle state. - Evalute the type of IO request to
  507. * be started - If its an NCQ request change to NCQ substate -
  508. * If its any other command change to the CMD substate
  509. *
  510. * If this is a softreset we may want to have a different
  511. * substate.
  512. */
  513. enum sci_remote_device_states new_state;
  514. struct sas_task *task = isci_request_access_task(ireq);
  515. status = sci_port_start_io(iport, idev, ireq);
  516. if (status != SCI_SUCCESS)
  517. return status;
  518. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  519. if (status != SCI_SUCCESS)
  520. break;
  521. status = sci_request_start(ireq);
  522. if (status != SCI_SUCCESS)
  523. break;
  524. if (task->ata_task.use_ncq)
  525. new_state = SCI_STP_DEV_NCQ;
  526. else {
  527. idev->working_request = ireq;
  528. new_state = SCI_STP_DEV_CMD;
  529. }
  530. sci_change_state(sm, new_state);
  531. break;
  532. }
  533. case SCI_STP_DEV_NCQ: {
  534. struct sas_task *task = isci_request_access_task(ireq);
  535. if (task->ata_task.use_ncq) {
  536. status = sci_port_start_io(iport, idev, ireq);
  537. if (status != SCI_SUCCESS)
  538. return status;
  539. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  540. if (status != SCI_SUCCESS)
  541. break;
  542. status = sci_request_start(ireq);
  543. } else
  544. return SCI_FAILURE_INVALID_STATE;
  545. break;
  546. }
  547. case SCI_STP_DEV_AWAIT_RESET:
  548. return SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
  549. case SCI_SMP_DEV_IDLE:
  550. status = sci_port_start_io(iport, idev, ireq);
  551. if (status != SCI_SUCCESS)
  552. return status;
  553. status = sci_remote_node_context_start_io(&idev->rnc, ireq);
  554. if (status != SCI_SUCCESS)
  555. break;
  556. status = sci_request_start(ireq);
  557. if (status != SCI_SUCCESS)
  558. break;
  559. idev->working_request = ireq;
  560. sci_change_state(&idev->sm, SCI_SMP_DEV_CMD);
  561. break;
  562. case SCI_STP_DEV_CMD:
  563. case SCI_SMP_DEV_CMD:
  564. /* device is already handling a command it can not accept new commands
  565. * until this one is complete.
  566. */
  567. return SCI_FAILURE_INVALID_STATE;
  568. }
  569. sci_remote_device_start_request(idev, ireq, status);
  570. return status;
  571. }
  572. static enum sci_status common_complete_io(struct isci_port *iport,
  573. struct isci_remote_device *idev,
  574. struct isci_request *ireq)
  575. {
  576. enum sci_status status;
  577. status = sci_request_complete(ireq);
  578. if (status != SCI_SUCCESS)
  579. return status;
  580. status = sci_port_complete_io(iport, idev, ireq);
  581. if (status != SCI_SUCCESS)
  582. return status;
  583. sci_remote_device_decrement_request_count(idev);
  584. return status;
  585. }
  586. enum sci_status sci_remote_device_complete_io(struct isci_host *ihost,
  587. struct isci_remote_device *idev,
  588. struct isci_request *ireq)
  589. {
  590. struct sci_base_state_machine *sm = &idev->sm;
  591. enum sci_remote_device_states state = sm->current_state_id;
  592. struct isci_port *iport = idev->owning_port;
  593. enum sci_status status;
  594. switch (state) {
  595. case SCI_DEV_INITIAL:
  596. case SCI_DEV_STOPPED:
  597. case SCI_DEV_STARTING:
  598. case SCI_STP_DEV_IDLE:
  599. case SCI_SMP_DEV_IDLE:
  600. case SCI_DEV_FAILED:
  601. case SCI_DEV_FINAL:
  602. default:
  603. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  604. __func__, dev_state_name(state));
  605. return SCI_FAILURE_INVALID_STATE;
  606. case SCI_DEV_READY:
  607. case SCI_STP_DEV_AWAIT_RESET:
  608. case SCI_DEV_RESETTING:
  609. status = common_complete_io(iport, idev, ireq);
  610. break;
  611. case SCI_STP_DEV_CMD:
  612. case SCI_STP_DEV_NCQ:
  613. case SCI_STP_DEV_NCQ_ERROR:
  614. case SCI_STP_DEV_ATAPI_ERROR:
  615. status = common_complete_io(iport, idev, ireq);
  616. if (status != SCI_SUCCESS)
  617. break;
  618. if (ireq->sci_status == SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  619. /* This request causes hardware error, device needs to be Lun Reset.
  620. * So here we force the state machine to IDLE state so the rest IOs
  621. * can reach RNC state handler, these IOs will be completed by RNC with
  622. * status of "DEVICE_RESET_REQUIRED", instead of "INVALID STATE".
  623. */
  624. sci_change_state(sm, SCI_STP_DEV_AWAIT_RESET);
  625. } else if (idev->started_request_count == 0)
  626. sci_change_state(sm, SCI_STP_DEV_IDLE);
  627. break;
  628. case SCI_SMP_DEV_CMD:
  629. status = common_complete_io(iport, idev, ireq);
  630. if (status != SCI_SUCCESS)
  631. break;
  632. sci_change_state(sm, SCI_SMP_DEV_IDLE);
  633. break;
  634. case SCI_DEV_STOPPING:
  635. status = common_complete_io(iport, idev, ireq);
  636. if (status != SCI_SUCCESS)
  637. break;
  638. if (idev->started_request_count == 0)
  639. sci_remote_node_context_destruct(&idev->rnc,
  640. rnc_destruct_done,
  641. idev);
  642. break;
  643. }
  644. if (status != SCI_SUCCESS)
  645. dev_err(scirdev_to_dev(idev),
  646. "%s: Port:0x%p Device:0x%p Request:0x%p Status:0x%x "
  647. "could not complete\n", __func__, iport,
  648. idev, ireq, status);
  649. else
  650. isci_put_device(idev);
  651. return status;
  652. }
  653. static void sci_remote_device_continue_request(void *dev)
  654. {
  655. struct isci_remote_device *idev = dev;
  656. /* we need to check if this request is still valid to continue. */
  657. if (idev->working_request)
  658. sci_controller_continue_io(idev->working_request);
  659. }
  660. enum sci_status sci_remote_device_start_task(struct isci_host *ihost,
  661. struct isci_remote_device *idev,
  662. struct isci_request *ireq)
  663. {
  664. struct sci_base_state_machine *sm = &idev->sm;
  665. enum sci_remote_device_states state = sm->current_state_id;
  666. struct isci_port *iport = idev->owning_port;
  667. enum sci_status status;
  668. switch (state) {
  669. case SCI_DEV_INITIAL:
  670. case SCI_DEV_STOPPED:
  671. case SCI_DEV_STARTING:
  672. case SCI_SMP_DEV_IDLE:
  673. case SCI_SMP_DEV_CMD:
  674. case SCI_DEV_STOPPING:
  675. case SCI_DEV_FAILED:
  676. case SCI_DEV_RESETTING:
  677. case SCI_DEV_FINAL:
  678. default:
  679. dev_warn(scirdev_to_dev(idev), "%s: in wrong state: %s\n",
  680. __func__, dev_state_name(state));
  681. return SCI_FAILURE_INVALID_STATE;
  682. case SCI_STP_DEV_IDLE:
  683. case SCI_STP_DEV_CMD:
  684. case SCI_STP_DEV_NCQ:
  685. case SCI_STP_DEV_NCQ_ERROR:
  686. case SCI_STP_DEV_AWAIT_RESET:
  687. status = sci_port_start_io(iport, idev, ireq);
  688. if (status != SCI_SUCCESS)
  689. return status;
  690. status = sci_request_start(ireq);
  691. if (status != SCI_SUCCESS)
  692. goto out;
  693. /* Note: If the remote device state is not IDLE this will
  694. * replace the request that probably resulted in the task
  695. * management request.
  696. */
  697. idev->working_request = ireq;
  698. sci_change_state(sm, SCI_STP_DEV_CMD);
  699. /* The remote node context must cleanup the TCi to NCQ mapping
  700. * table. The only way to do this correctly is to either write
  701. * to the TLCR register or to invalidate and repost the RNC. In
  702. * either case the remote node context state machine will take
  703. * the correct action when the remote node context is suspended
  704. * and later resumed.
  705. */
  706. sci_remote_node_context_suspend(
  707. &idev->rnc, SCI_SOFTWARE_SUSPENSION,
  708. SCI_SOFTWARE_SUSPEND_EXPECTED_EVENT, NULL, NULL);
  709. status = sci_remote_node_context_start_task(&idev->rnc, ireq,
  710. sci_remote_device_continue_request, idev);
  711. out:
  712. sci_remote_device_start_request(idev, ireq, status);
  713. /* We need to let the controller start request handler know that
  714. * it can't post TC yet. We will provide a callback function to
  715. * post TC when RNC gets resumed.
  716. */
  717. return SCI_FAILURE_RESET_DEVICE_PARTIAL_SUCCESS;
  718. case SCI_DEV_READY:
  719. status = sci_port_start_io(iport, idev, ireq);
  720. if (status != SCI_SUCCESS)
  721. return status;
  722. /* Resume the RNC as needed: */
  723. status = sci_remote_node_context_start_task(&idev->rnc, ireq,
  724. NULL, NULL);
  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(idev->started_request_count > 0);
  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. /**
  1151. * This function builds the isci_remote_device when a libsas dev_found message
  1152. * is received.
  1153. * @isci_host: This parameter specifies the isci host object.
  1154. * @port: This parameter specifies the isci_port conected to this device.
  1155. *
  1156. * pointer to new isci_remote_device.
  1157. */
  1158. static struct isci_remote_device *
  1159. isci_remote_device_alloc(struct isci_host *ihost, struct isci_port *iport)
  1160. {
  1161. struct isci_remote_device *idev;
  1162. int i;
  1163. for (i = 0; i < SCI_MAX_REMOTE_DEVICES; i++) {
  1164. idev = &ihost->devices[i];
  1165. if (!test_and_set_bit(IDEV_ALLOCATED, &idev->flags))
  1166. break;
  1167. }
  1168. if (i >= SCI_MAX_REMOTE_DEVICES) {
  1169. dev_warn(&ihost->pdev->dev, "%s: failed\n", __func__);
  1170. return NULL;
  1171. }
  1172. if (WARN_ONCE(!list_empty(&idev->node), "found non-idle remote device\n"))
  1173. return NULL;
  1174. return idev;
  1175. }
  1176. void isci_remote_device_release(struct kref *kref)
  1177. {
  1178. struct isci_remote_device *idev = container_of(kref, typeof(*idev), kref);
  1179. struct isci_host *ihost = idev->isci_port->isci_host;
  1180. idev->domain_dev = NULL;
  1181. idev->isci_port = NULL;
  1182. clear_bit(IDEV_START_PENDING, &idev->flags);
  1183. clear_bit(IDEV_STOP_PENDING, &idev->flags);
  1184. clear_bit(IDEV_IO_READY, &idev->flags);
  1185. clear_bit(IDEV_GONE, &idev->flags);
  1186. smp_mb__before_clear_bit();
  1187. clear_bit(IDEV_ALLOCATED, &idev->flags);
  1188. wake_up(&ihost->eventq);
  1189. }
  1190. /**
  1191. * isci_remote_device_stop() - This function is called internally to stop the
  1192. * remote device.
  1193. * @isci_host: This parameter specifies the isci host object.
  1194. * @isci_device: This parameter specifies the remote device.
  1195. *
  1196. * The status of the ihost request to stop.
  1197. */
  1198. enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  1199. {
  1200. enum sci_status status;
  1201. unsigned long flags;
  1202. dev_dbg(&ihost->pdev->dev,
  1203. "%s: isci_device = %p\n", __func__, idev);
  1204. spin_lock_irqsave(&ihost->scic_lock, flags);
  1205. idev->domain_dev->lldd_dev = NULL; /* disable new lookups */
  1206. set_bit(IDEV_GONE, &idev->flags);
  1207. set_bit(IDEV_STOP_PENDING, &idev->flags);
  1208. status = sci_remote_device_stop(idev, 50);
  1209. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1210. /* Wait for the stop complete callback. */
  1211. if (WARN_ONCE(status != SCI_SUCCESS, "failed to stop device\n"))
  1212. /* nothing to wait for */;
  1213. else
  1214. wait_for_device_stop(ihost, idev);
  1215. dev_dbg(&ihost->pdev->dev,
  1216. "%s: isci_device = %p, waiting done.\n", __func__, idev);
  1217. return status;
  1218. }
  1219. /**
  1220. * isci_remote_device_gone() - This function is called by libsas when a domain
  1221. * device is removed.
  1222. * @domain_device: This parameter specifies the libsas domain device.
  1223. *
  1224. */
  1225. void isci_remote_device_gone(struct domain_device *dev)
  1226. {
  1227. struct isci_host *ihost = dev_to_ihost(dev);
  1228. struct isci_remote_device *idev = dev->lldd_dev;
  1229. dev_dbg(&ihost->pdev->dev,
  1230. "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
  1231. __func__, dev, idev, idev->isci_port);
  1232. isci_remote_device_stop(ihost, idev);
  1233. }
  1234. /**
  1235. * isci_remote_device_found() - This function is called by libsas when a remote
  1236. * device is discovered. A remote device object is created and started. the
  1237. * function then sleeps until the sci core device started message is
  1238. * received.
  1239. * @domain_device: This parameter specifies the libsas domain device.
  1240. *
  1241. * status, zero indicates success.
  1242. */
  1243. int isci_remote_device_found(struct domain_device *dev)
  1244. {
  1245. struct isci_host *isci_host = dev_to_ihost(dev);
  1246. struct isci_port *isci_port = dev->port->lldd_port;
  1247. struct isci_remote_device *isci_device;
  1248. enum sci_status status;
  1249. dev_dbg(&isci_host->pdev->dev,
  1250. "%s: domain_device = %p\n", __func__, dev);
  1251. if (!isci_port)
  1252. return -ENODEV;
  1253. isci_device = isci_remote_device_alloc(isci_host, isci_port);
  1254. if (!isci_device)
  1255. return -ENODEV;
  1256. kref_init(&isci_device->kref);
  1257. INIT_LIST_HEAD(&isci_device->node);
  1258. spin_lock_irq(&isci_host->scic_lock);
  1259. isci_device->domain_dev = dev;
  1260. isci_device->isci_port = isci_port;
  1261. list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
  1262. set_bit(IDEV_START_PENDING, &isci_device->flags);
  1263. status = isci_remote_device_construct(isci_port, isci_device);
  1264. dev_dbg(&isci_host->pdev->dev,
  1265. "%s: isci_device = %p\n",
  1266. __func__, isci_device);
  1267. if (status == SCI_SUCCESS) {
  1268. /* device came up, advertise it to the world */
  1269. dev->lldd_dev = isci_device;
  1270. } else
  1271. isci_put_device(isci_device);
  1272. spin_unlock_irq(&isci_host->scic_lock);
  1273. /* wait for the device ready callback. */
  1274. wait_for_device_start(isci_host, isci_device);
  1275. return status == SCI_SUCCESS ? 0 : -ENODEV;
  1276. }
  1277. enum sci_status isci_remote_device_suspend_terminate(
  1278. struct isci_host *ihost,
  1279. struct isci_remote_device *idev,
  1280. struct isci_request *ireq)
  1281. {
  1282. unsigned long flags;
  1283. enum sci_status status;
  1284. /* Put the device into suspension. */
  1285. spin_lock_irqsave(&ihost->scic_lock, flags);
  1286. sci_remote_device_suspend(idev);
  1287. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1288. /* Terminate and wait for the completions. */
  1289. status = isci_remote_device_terminate_requests(ihost, idev, ireq);
  1290. if (status != SCI_SUCCESS)
  1291. dev_dbg(&ihost->pdev->dev,
  1292. "%s: isci_remote_device_terminate_requests(%p) "
  1293. "returned %d!\n",
  1294. __func__, idev, status);
  1295. /* NOTE: RNC resumption is left to the caller! */
  1296. return status;
  1297. }
  1298. int isci_remote_device_is_safe_to_abort(
  1299. struct isci_remote_device *idev)
  1300. {
  1301. return sci_remote_node_context_is_safe_to_abort(&idev->rnc);
  1302. }
  1303. enum sci_status sci_remote_device_abort_requests_pending_abort(
  1304. struct isci_remote_device *idev)
  1305. {
  1306. return sci_remote_device_terminate_reqs_checkabort(idev, 1);
  1307. }
  1308. enum sci_status isci_remote_device_reset_complete(
  1309. struct isci_host *ihost,
  1310. struct isci_remote_device *idev)
  1311. {
  1312. unsigned long flags;
  1313. enum sci_status status;
  1314. spin_lock_irqsave(&ihost->scic_lock, flags);
  1315. status = sci_remote_device_reset_complete(idev);
  1316. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1317. return status;
  1318. }