remote_device.c 43 KB

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