remote_device.c 45 KB

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