task.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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 <linux/completion.h>
  56. #include <linux/irqflags.h>
  57. #include "sas.h"
  58. #include <scsi/libsas.h>
  59. #include "remote_device.h"
  60. #include "remote_node_context.h"
  61. #include "isci.h"
  62. #include "request.h"
  63. #include "task.h"
  64. #include "host.h"
  65. /**
  66. * isci_task_refuse() - complete the request to the upper layer driver in
  67. * the case where an I/O needs to be completed back in the submit path.
  68. * @ihost: host on which the the request was queued
  69. * @task: request to complete
  70. * @response: response code for the completed task.
  71. * @status: status code for the completed task.
  72. *
  73. */
  74. static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
  75. enum service_response response,
  76. enum exec_status status)
  77. {
  78. enum isci_completion_selection disposition;
  79. disposition = isci_perform_normal_io_completion;
  80. disposition = isci_task_set_completion_status(task, response, status,
  81. disposition);
  82. /* Tasks aborted specifically by a call to the lldd_abort_task
  83. * function should not be completed to the host in the regular path.
  84. */
  85. switch (disposition) {
  86. case isci_perform_normal_io_completion:
  87. /* Normal notification (task_done) */
  88. dev_dbg(&ihost->pdev->dev,
  89. "%s: Normal - task = %p, response=%d, "
  90. "status=%d\n",
  91. __func__, task, response, status);
  92. task->lldd_task = NULL;
  93. task->task_done(task);
  94. break;
  95. case isci_perform_aborted_io_completion:
  96. /*
  97. * No notification because this request is already in the
  98. * abort path.
  99. */
  100. dev_dbg(&ihost->pdev->dev,
  101. "%s: Aborted - task = %p, response=%d, "
  102. "status=%d\n",
  103. __func__, task, response, status);
  104. break;
  105. case isci_perform_error_io_completion:
  106. /* Use sas_task_abort */
  107. dev_dbg(&ihost->pdev->dev,
  108. "%s: Error - task = %p, response=%d, "
  109. "status=%d\n",
  110. __func__, task, response, status);
  111. sas_task_abort(task);
  112. break;
  113. default:
  114. dev_dbg(&ihost->pdev->dev,
  115. "%s: isci task notification default case!",
  116. __func__);
  117. sas_task_abort(task);
  118. break;
  119. }
  120. }
  121. #define for_each_sas_task(num, task) \
  122. for (; num > 0; num--,\
  123. task = list_entry(task->list.next, struct sas_task, list))
  124. static inline int isci_device_io_ready(struct isci_remote_device *idev,
  125. struct sas_task *task)
  126. {
  127. return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
  128. (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
  129. isci_task_is_ncq_recovery(task))
  130. : 0;
  131. }
  132. /**
  133. * isci_task_execute_task() - This function is one of the SAS Domain Template
  134. * functions. This function is called by libsas to send a task down to
  135. * hardware.
  136. * @task: This parameter specifies the SAS task to send.
  137. * @num: This parameter specifies the number of tasks to queue.
  138. * @gfp_flags: This parameter specifies the context of this call.
  139. *
  140. * status, zero indicates success.
  141. */
  142. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  143. {
  144. struct isci_host *ihost = dev_to_ihost(task->dev);
  145. struct isci_remote_device *idev;
  146. unsigned long flags;
  147. bool io_ready;
  148. u16 tag;
  149. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  150. for_each_sas_task(num, task) {
  151. enum sci_status status = SCI_FAILURE;
  152. spin_lock_irqsave(&ihost->scic_lock, flags);
  153. idev = isci_lookup_device(task->dev);
  154. io_ready = isci_device_io_ready(idev, task);
  155. tag = isci_alloc_tag(ihost);
  156. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  157. dev_dbg(&ihost->pdev->dev,
  158. "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
  159. task, num, task->dev, idev, idev ? idev->flags : 0,
  160. task->uldd_task);
  161. if (!idev) {
  162. isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
  163. SAS_DEVICE_UNKNOWN);
  164. } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  165. /* Indicate QUEUE_FULL so that the scsi midlayer
  166. * retries.
  167. */
  168. isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
  169. SAS_QUEUE_FULL);
  170. } else {
  171. /* There is a device and it's ready for I/O. */
  172. spin_lock_irqsave(&task->task_state_lock, flags);
  173. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  174. /* The I/O was aborted. */
  175. spin_unlock_irqrestore(&task->task_state_lock,
  176. flags);
  177. isci_task_refuse(ihost, task,
  178. SAS_TASK_UNDELIVERED,
  179. SAM_STAT_TASK_ABORTED);
  180. } else {
  181. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  182. spin_unlock_irqrestore(&task->task_state_lock, flags);
  183. /* build and send the request. */
  184. status = isci_request_execute(ihost, idev, task, tag);
  185. if (status != SCI_SUCCESS) {
  186. spin_lock_irqsave(&task->task_state_lock, flags);
  187. /* Did not really start this command. */
  188. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  189. spin_unlock_irqrestore(&task->task_state_lock, flags);
  190. if (test_bit(IDEV_GONE, &idev->flags)) {
  191. /* Indicate that the device
  192. * is gone.
  193. */
  194. isci_task_refuse(ihost, task,
  195. SAS_TASK_UNDELIVERED,
  196. SAS_DEVICE_UNKNOWN);
  197. } else {
  198. /* Indicate QUEUE_FULL so that
  199. * the scsi midlayer retries.
  200. * If the request failed for
  201. * remote device reasons, it
  202. * gets returned as
  203. * SAS_TASK_UNDELIVERED next
  204. * time through.
  205. */
  206. isci_task_refuse(ihost, task,
  207. SAS_TASK_COMPLETE,
  208. SAS_QUEUE_FULL);
  209. }
  210. }
  211. }
  212. }
  213. if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  214. spin_lock_irqsave(&ihost->scic_lock, flags);
  215. /* command never hit the device, so just free
  216. * the tci and skip the sequence increment
  217. */
  218. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  219. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  220. }
  221. isci_put_device(idev);
  222. }
  223. return 0;
  224. }
  225. static struct isci_request *isci_task_request_build(struct isci_host *ihost,
  226. struct isci_remote_device *idev,
  227. u16 tag, struct isci_tmf *isci_tmf)
  228. {
  229. enum sci_status status = SCI_FAILURE;
  230. struct isci_request *ireq = NULL;
  231. struct domain_device *dev;
  232. dev_dbg(&ihost->pdev->dev,
  233. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  234. dev = idev->domain_dev;
  235. /* do common allocation and init of request object. */
  236. ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
  237. if (!ireq)
  238. return NULL;
  239. /* let the core do it's construct. */
  240. status = sci_task_request_construct(ihost, idev, tag,
  241. ireq);
  242. if (status != SCI_SUCCESS) {
  243. dev_warn(&ihost->pdev->dev,
  244. "%s: sci_task_request_construct failed - "
  245. "status = 0x%x\n",
  246. __func__,
  247. status);
  248. return NULL;
  249. }
  250. /* XXX convert to get this from task->tproto like other drivers */
  251. if (dev->dev_type == SAS_END_DEV) {
  252. isci_tmf->proto = SAS_PROTOCOL_SSP;
  253. status = sci_task_request_construct_ssp(ireq);
  254. if (status != SCI_SUCCESS)
  255. return NULL;
  256. }
  257. return ireq;
  258. }
  259. /**
  260. * isci_request_mark_zombie() - This function must be called with scic_lock held.
  261. */
  262. static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
  263. {
  264. struct completion *tmf_completion = NULL;
  265. struct completion *req_completion;
  266. /* Set the request state to "dead". */
  267. ireq->status = dead;
  268. req_completion = ireq->io_request_completion;
  269. ireq->io_request_completion = NULL;
  270. if (test_bit(IREQ_TMF, &ireq->flags)) {
  271. /* Break links with the TMF request. */
  272. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  273. /* In the case where a task request is dying,
  274. * the thread waiting on the complete will sit and
  275. * timeout unless we wake it now. Since the TMF
  276. * has a default error status, complete it here
  277. * to wake the waiting thread.
  278. */
  279. if (tmf) {
  280. tmf_completion = tmf->complete;
  281. tmf->complete = NULL;
  282. }
  283. ireq->ttype_ptr.tmf_task_ptr = NULL;
  284. dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
  285. __func__, tmf->tmf_code, tmf->io_tag);
  286. } else {
  287. /* Break links with the sas_task - the callback is done
  288. * elsewhere.
  289. */
  290. struct sas_task *task = isci_request_access_task(ireq);
  291. if (task)
  292. task->lldd_task = NULL;
  293. ireq->ttype_ptr.io_task_ptr = NULL;
  294. }
  295. dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
  296. ireq->io_tag);
  297. /* Don't force waiting threads to timeout. */
  298. if (req_completion)
  299. complete(req_completion);
  300. if (tmf_completion != NULL)
  301. complete(tmf_completion);
  302. }
  303. static int isci_task_execute_tmf(struct isci_host *ihost,
  304. struct isci_remote_device *idev,
  305. struct isci_tmf *tmf, unsigned long timeout_ms)
  306. {
  307. DECLARE_COMPLETION_ONSTACK(completion);
  308. enum sci_task_status status = SCI_TASK_FAILURE;
  309. struct isci_request *ireq;
  310. int ret = TMF_RESP_FUNC_FAILED;
  311. unsigned long flags;
  312. unsigned long timeleft;
  313. u16 tag;
  314. spin_lock_irqsave(&ihost->scic_lock, flags);
  315. tag = isci_alloc_tag(ihost);
  316. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  317. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  318. return ret;
  319. /* sanity check, return TMF_RESP_FUNC_FAILED
  320. * if the device is not there and ready.
  321. */
  322. if (!idev ||
  323. (!test_bit(IDEV_IO_READY, &idev->flags) &&
  324. !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
  325. dev_dbg(&ihost->pdev->dev,
  326. "%s: idev = %p not ready (%#lx)\n",
  327. __func__,
  328. idev, idev ? idev->flags : 0);
  329. goto err_tci;
  330. } else
  331. dev_dbg(&ihost->pdev->dev,
  332. "%s: idev = %p\n",
  333. __func__, idev);
  334. /* Assign the pointer to the TMF's completion kernel wait structure. */
  335. tmf->complete = &completion;
  336. tmf->status = SCI_FAILURE_TIMEOUT;
  337. ireq = isci_task_request_build(ihost, idev, tag, tmf);
  338. if (!ireq)
  339. goto err_tci;
  340. spin_lock_irqsave(&ihost->scic_lock, flags);
  341. /* start the TMF io. */
  342. status = sci_controller_start_task(ihost, idev, ireq);
  343. if (status != SCI_TASK_SUCCESS) {
  344. dev_dbg(&ihost->pdev->dev,
  345. "%s: start_io failed - status = 0x%x, request = %p\n",
  346. __func__,
  347. status,
  348. ireq);
  349. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  350. goto err_tci;
  351. }
  352. if (tmf->cb_state_func != NULL)
  353. tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
  354. isci_request_change_state(ireq, started);
  355. /* add the request to the remote device request list. */
  356. list_add(&ireq->dev_node, &idev->reqs_in_process);
  357. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  358. /* Wait for the TMF to complete, or a timeout. */
  359. timeleft = wait_for_completion_timeout(&completion,
  360. msecs_to_jiffies(timeout_ms));
  361. if (timeleft == 0) {
  362. /* The TMF did not complete - this could be because
  363. * of an unplug. Terminate the TMF request now.
  364. */
  365. spin_lock_irqsave(&ihost->scic_lock, flags);
  366. if (tmf->cb_state_func != NULL)
  367. tmf->cb_state_func(isci_tmf_timed_out, tmf,
  368. tmf->cb_data);
  369. sci_controller_terminate_request(ihost, idev, ireq);
  370. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  371. timeleft = wait_for_completion_timeout(
  372. &completion,
  373. msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
  374. if (!timeleft) {
  375. /* Strange condition - the termination of the TMF
  376. * request timed-out.
  377. */
  378. spin_lock_irqsave(&ihost->scic_lock, flags);
  379. /* If the TMF status has not changed, kill it. */
  380. if (tmf->status == SCI_FAILURE_TIMEOUT)
  381. isci_request_mark_zombie(ihost, ireq);
  382. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  383. }
  384. }
  385. isci_print_tmf(ihost, tmf);
  386. if (tmf->status == SCI_SUCCESS)
  387. ret = TMF_RESP_FUNC_COMPLETE;
  388. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  389. dev_dbg(&ihost->pdev->dev,
  390. "%s: tmf.status == "
  391. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  392. __func__);
  393. ret = TMF_RESP_FUNC_COMPLETE;
  394. }
  395. /* Else - leave the default "failed" status alone. */
  396. dev_dbg(&ihost->pdev->dev,
  397. "%s: completed request = %p\n",
  398. __func__,
  399. ireq);
  400. return ret;
  401. err_tci:
  402. spin_lock_irqsave(&ihost->scic_lock, flags);
  403. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  404. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  405. return ret;
  406. }
  407. static void isci_task_build_tmf(struct isci_tmf *tmf,
  408. enum isci_tmf_function_codes code,
  409. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  410. struct isci_tmf *,
  411. void *),
  412. void *cb_data)
  413. {
  414. memset(tmf, 0, sizeof(*tmf));
  415. tmf->tmf_code = code;
  416. tmf->cb_state_func = tmf_sent_cb;
  417. tmf->cb_data = cb_data;
  418. }
  419. static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
  420. enum isci_tmf_function_codes code,
  421. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  422. struct isci_tmf *,
  423. void *),
  424. struct isci_request *old_request)
  425. {
  426. isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
  427. tmf->io_tag = old_request->io_tag;
  428. }
  429. /**
  430. * isci_task_validate_request_to_abort() - This function checks the given I/O
  431. * against the "started" state. If the request is still "started", it's
  432. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  433. * BEFORE CALLING THIS FUNCTION.
  434. * @isci_request: This parameter specifies the request object to control.
  435. * @isci_host: This parameter specifies the ISCI host object
  436. * @isci_device: This is the device to which the request is pending.
  437. * @aborted_io_completion: This is a completion structure that will be added to
  438. * the request in case it is changed to aborting; this completion is
  439. * triggered when the request is fully completed.
  440. *
  441. * Either "started" on successful change of the task status to "aborted", or
  442. * "unallocated" if the task cannot be controlled.
  443. */
  444. static enum isci_request_status isci_task_validate_request_to_abort(
  445. struct isci_request *isci_request,
  446. struct isci_host *isci_host,
  447. struct isci_remote_device *isci_device,
  448. struct completion *aborted_io_completion)
  449. {
  450. enum isci_request_status old_state = unallocated;
  451. /* Only abort the task if it's in the
  452. * device's request_in_process list
  453. */
  454. if (isci_request && !list_empty(&isci_request->dev_node)) {
  455. old_state = isci_request_change_started_to_aborted(
  456. isci_request, aborted_io_completion);
  457. }
  458. return old_state;
  459. }
  460. static int isci_request_is_dealloc_managed(enum isci_request_status stat)
  461. {
  462. switch (stat) {
  463. case aborted:
  464. case aborting:
  465. case terminating:
  466. case completed:
  467. case dead:
  468. return true;
  469. default:
  470. return false;
  471. }
  472. }
  473. /**
  474. * isci_terminate_request_core() - This function will terminate the given
  475. * request, and wait for it to complete. This function must only be called
  476. * from a thread that can wait. Note that the request is terminated and
  477. * completed (back to the host, if started there).
  478. * @ihost: This SCU.
  479. * @idev: The target.
  480. * @isci_request: The I/O request to be terminated.
  481. *
  482. */
  483. static void isci_terminate_request_core(struct isci_host *ihost,
  484. struct isci_remote_device *idev,
  485. struct isci_request *isci_request)
  486. {
  487. enum sci_status status = SCI_SUCCESS;
  488. bool was_terminated = false;
  489. bool needs_cleanup_handling = false;
  490. unsigned long flags;
  491. unsigned long termination_completed = 1;
  492. struct completion *io_request_completion;
  493. dev_dbg(&ihost->pdev->dev,
  494. "%s: device = %p; request = %p\n",
  495. __func__, idev, isci_request);
  496. spin_lock_irqsave(&ihost->scic_lock, flags);
  497. io_request_completion = isci_request->io_request_completion;
  498. /* Note that we are not going to control
  499. * the target to abort the request.
  500. */
  501. set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
  502. /* Make sure the request wasn't just sitting around signalling
  503. * device condition (if the request handle is NULL, then the
  504. * request completed but needed additional handling here).
  505. */
  506. if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
  507. was_terminated = true;
  508. needs_cleanup_handling = true;
  509. status = sci_controller_terminate_request(ihost,
  510. idev,
  511. isci_request);
  512. }
  513. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  514. /*
  515. * The only time the request to terminate will
  516. * fail is when the io request is completed and
  517. * being aborted.
  518. */
  519. if (status != SCI_SUCCESS) {
  520. dev_dbg(&ihost->pdev->dev,
  521. "%s: sci_controller_terminate_request"
  522. " returned = 0x%x\n",
  523. __func__, status);
  524. isci_request->io_request_completion = NULL;
  525. } else {
  526. if (was_terminated) {
  527. dev_dbg(&ihost->pdev->dev,
  528. "%s: before completion wait (%p/%p)\n",
  529. __func__, isci_request, io_request_completion);
  530. /* Wait here for the request to complete. */
  531. termination_completed
  532. = wait_for_completion_timeout(
  533. io_request_completion,
  534. msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
  535. if (!termination_completed) {
  536. /* The request to terminate has timed out. */
  537. spin_lock_irqsave(&ihost->scic_lock, flags);
  538. /* Check for state changes. */
  539. if (!test_bit(IREQ_TERMINATED,
  540. &isci_request->flags)) {
  541. /* The best we can do is to have the
  542. * request die a silent death if it
  543. * ever really completes.
  544. */
  545. isci_request_mark_zombie(ihost,
  546. isci_request);
  547. needs_cleanup_handling = true;
  548. } else
  549. termination_completed = 1;
  550. spin_unlock_irqrestore(&ihost->scic_lock,
  551. flags);
  552. if (!termination_completed) {
  553. dev_dbg(&ihost->pdev->dev,
  554. "%s: *** Timeout waiting for "
  555. "termination(%p/%p)\n",
  556. __func__, io_request_completion,
  557. isci_request);
  558. /* The request can no longer be referenced
  559. * safely since it may go away if the
  560. * termination every really does complete.
  561. */
  562. isci_request = NULL;
  563. }
  564. }
  565. if (termination_completed)
  566. dev_dbg(&ihost->pdev->dev,
  567. "%s: after completion wait (%p/%p)\n",
  568. __func__, isci_request, io_request_completion);
  569. }
  570. if (termination_completed) {
  571. isci_request->io_request_completion = NULL;
  572. /* Peek at the status of the request. This will tell
  573. * us if there was special handling on the request such that it
  574. * needs to be detached and freed here.
  575. */
  576. spin_lock_irqsave(&isci_request->state_lock, flags);
  577. needs_cleanup_handling
  578. = isci_request_is_dealloc_managed(
  579. isci_request->status);
  580. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  581. }
  582. if (needs_cleanup_handling) {
  583. dev_dbg(&ihost->pdev->dev,
  584. "%s: cleanup isci_device=%p, request=%p\n",
  585. __func__, idev, isci_request);
  586. if (isci_request != NULL) {
  587. spin_lock_irqsave(&ihost->scic_lock, flags);
  588. isci_free_tag(ihost, isci_request->io_tag);
  589. isci_request_change_state(isci_request, unallocated);
  590. list_del_init(&isci_request->dev_node);
  591. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  592. }
  593. }
  594. }
  595. }
  596. /**
  597. * isci_terminate_pending_requests() - This function will change the all of the
  598. * requests on the given device's state to "aborting", will terminate the
  599. * requests, and wait for them to complete. This function must only be
  600. * called from a thread that can wait. Note that the requests are all
  601. * terminated and completed (back to the host, if started there).
  602. * @isci_host: This parameter specifies SCU.
  603. * @idev: This parameter specifies the target.
  604. *
  605. */
  606. void isci_terminate_pending_requests(struct isci_host *ihost,
  607. struct isci_remote_device *idev)
  608. {
  609. struct completion request_completion;
  610. enum isci_request_status old_state;
  611. unsigned long flags;
  612. LIST_HEAD(list);
  613. spin_lock_irqsave(&ihost->scic_lock, flags);
  614. list_splice_init(&idev->reqs_in_process, &list);
  615. /* assumes that isci_terminate_request_core deletes from the list */
  616. while (!list_empty(&list)) {
  617. struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
  618. /* Change state to "terminating" if it is currently
  619. * "started".
  620. */
  621. old_state = isci_request_change_started_to_newstate(ireq,
  622. &request_completion,
  623. terminating);
  624. switch (old_state) {
  625. case started:
  626. case completed:
  627. case aborting:
  628. break;
  629. default:
  630. /* termination in progress, or otherwise dispositioned.
  631. * We know the request was on 'list' so should be safe
  632. * to move it back to reqs_in_process
  633. */
  634. list_move(&ireq->dev_node, &idev->reqs_in_process);
  635. ireq = NULL;
  636. break;
  637. }
  638. if (!ireq)
  639. continue;
  640. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  641. init_completion(&request_completion);
  642. dev_dbg(&ihost->pdev->dev,
  643. "%s: idev=%p request=%p; task=%p old_state=%d\n",
  644. __func__, idev, ireq,
  645. (!test_bit(IREQ_TMF, &ireq->flags)
  646. ? isci_request_access_task(ireq)
  647. : NULL),
  648. old_state);
  649. /* If the old_state is started:
  650. * This request was not already being aborted. If it had been,
  651. * then the aborting I/O (ie. the TMF request) would not be in
  652. * the aborting state, and thus would be terminated here. Note
  653. * that since the TMF completion's call to the kernel function
  654. * "complete()" does not happen until the pending I/O request
  655. * terminate fully completes, we do not have to implement a
  656. * special wait here for already aborting requests - the
  657. * termination of the TMF request will force the request
  658. * to finish it's already started terminate.
  659. *
  660. * If old_state == completed:
  661. * This request completed from the SCU hardware perspective
  662. * and now just needs cleaning up in terms of freeing the
  663. * request and potentially calling up to libsas.
  664. *
  665. * If old_state == aborting:
  666. * This request has already gone through a TMF timeout, but may
  667. * not have been terminated; needs cleaning up at least.
  668. */
  669. isci_terminate_request_core(ihost, idev, ireq);
  670. spin_lock_irqsave(&ihost->scic_lock, flags);
  671. }
  672. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  673. }
  674. /**
  675. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  676. * Template functions.
  677. * @lun: This parameter specifies the lun to be reset.
  678. *
  679. * status, zero indicates success.
  680. */
  681. static int isci_task_send_lu_reset_sas(
  682. struct isci_host *isci_host,
  683. struct isci_remote_device *isci_device,
  684. u8 *lun)
  685. {
  686. struct isci_tmf tmf;
  687. int ret = TMF_RESP_FUNC_FAILED;
  688. dev_dbg(&isci_host->pdev->dev,
  689. "%s: isci_host = %p, isci_device = %p\n",
  690. __func__, isci_host, isci_device);
  691. /* Send the LUN reset to the target. By the time the call returns,
  692. * the TMF has fully exected in the target (in which case the return
  693. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  694. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  695. */
  696. isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
  697. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  698. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  699. if (ret == TMF_RESP_FUNC_COMPLETE)
  700. dev_dbg(&isci_host->pdev->dev,
  701. "%s: %p: TMF_LU_RESET passed\n",
  702. __func__, isci_device);
  703. else
  704. dev_dbg(&isci_host->pdev->dev,
  705. "%s: %p: TMF_LU_RESET failed (%x)\n",
  706. __func__, isci_device, ret);
  707. return ret;
  708. }
  709. int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
  710. {
  711. struct isci_host *isci_host = dev_to_ihost(dev);
  712. struct isci_remote_device *isci_device;
  713. unsigned long flags;
  714. int ret;
  715. spin_lock_irqsave(&isci_host->scic_lock, flags);
  716. isci_device = isci_lookup_device(dev);
  717. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  718. dev_dbg(&isci_host->pdev->dev,
  719. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  720. __func__, dev, isci_host, isci_device);
  721. if (!isci_device) {
  722. /* If the device is gone, stop the escalations. */
  723. dev_dbg(&isci_host->pdev->dev, "%s: No dev\n", __func__);
  724. ret = TMF_RESP_FUNC_COMPLETE;
  725. goto out;
  726. }
  727. /* Send the task management part of the reset. */
  728. if (dev_is_sata(dev)) {
  729. sas_ata_schedule_reset(dev);
  730. ret = TMF_RESP_FUNC_COMPLETE;
  731. } else
  732. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  733. /* If the LUN reset worked, all the I/O can now be terminated. */
  734. if (ret == TMF_RESP_FUNC_COMPLETE)
  735. /* Terminate all I/O now. */
  736. isci_terminate_pending_requests(isci_host,
  737. isci_device);
  738. out:
  739. isci_put_device(isci_device);
  740. return ret;
  741. }
  742. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  743. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  744. {
  745. return TMF_RESP_FUNC_FAILED;
  746. }
  747. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  748. {
  749. return TMF_RESP_FUNC_FAILED;
  750. }
  751. /* Task Management Functions. Must be called from process context. */
  752. /**
  753. * isci_abort_task_process_cb() - This is a helper function for the abort task
  754. * TMF command. It manages the request state with respect to the successful
  755. * transmission / completion of the abort task request.
  756. * @cb_state: This parameter specifies when this function was called - after
  757. * the TMF request has been started and after it has timed-out.
  758. * @tmf: This parameter specifies the TMF in progress.
  759. *
  760. *
  761. */
  762. static void isci_abort_task_process_cb(
  763. enum isci_tmf_cb_state cb_state,
  764. struct isci_tmf *tmf,
  765. void *cb_data)
  766. {
  767. struct isci_request *old_request;
  768. old_request = (struct isci_request *)cb_data;
  769. dev_dbg(&old_request->isci_host->pdev->dev,
  770. "%s: tmf=%p, old_request=%p\n",
  771. __func__, tmf, old_request);
  772. switch (cb_state) {
  773. case isci_tmf_started:
  774. /* The TMF has been started. Nothing to do here, since the
  775. * request state was already set to "aborted" by the abort
  776. * task function.
  777. */
  778. if ((old_request->status != aborted)
  779. && (old_request->status != completed))
  780. dev_dbg(&old_request->isci_host->pdev->dev,
  781. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  782. __func__, old_request->status, tmf, old_request);
  783. break;
  784. case isci_tmf_timed_out:
  785. /* Set the task's state to "aborting", since the abort task
  786. * function thread set it to "aborted" (above) in anticipation
  787. * of the task management request working correctly. Since the
  788. * timeout has now fired, the TMF request failed. We set the
  789. * state such that the request completion will indicate the
  790. * device is no longer present.
  791. */
  792. isci_request_change_state(old_request, aborting);
  793. break;
  794. default:
  795. dev_dbg(&old_request->isci_host->pdev->dev,
  796. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  797. __func__, cb_state, tmf, old_request);
  798. break;
  799. }
  800. }
  801. /**
  802. * isci_task_abort_task() - This function is one of the SAS Domain Template
  803. * functions. This function is called by libsas to abort a specified task.
  804. * @task: This parameter specifies the SAS task to abort.
  805. *
  806. * status, zero indicates success.
  807. */
  808. int isci_task_abort_task(struct sas_task *task)
  809. {
  810. struct isci_host *isci_host = dev_to_ihost(task->dev);
  811. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  812. struct isci_request *old_request = NULL;
  813. enum isci_request_status old_state;
  814. struct isci_remote_device *isci_device = NULL;
  815. struct isci_tmf tmf;
  816. int ret = TMF_RESP_FUNC_FAILED;
  817. unsigned long flags;
  818. int perform_termination = 0;
  819. int target_done_already = 0;
  820. /* Get the isci_request reference from the task. Note that
  821. * this check does not depend on the pending request list
  822. * in the device, because tasks driving resets may land here
  823. * after completion in the core.
  824. */
  825. spin_lock_irqsave(&isci_host->scic_lock, flags);
  826. spin_lock(&task->task_state_lock);
  827. old_request = task->lldd_task;
  828. /* If task is already done, the request isn't valid */
  829. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  830. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  831. old_request) {
  832. isci_device = isci_lookup_device(task->dev);
  833. target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
  834. &old_request->flags);
  835. }
  836. spin_unlock(&task->task_state_lock);
  837. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  838. dev_dbg(&isci_host->pdev->dev,
  839. "%s: dev = %p, task = %p, old_request == %p\n",
  840. __func__, isci_device, task, old_request);
  841. /* Device reset conditions signalled in task_state_flags are the
  842. * responsbility of libsas to observe at the start of the error
  843. * handler thread.
  844. */
  845. if (!isci_device || !old_request) {
  846. /* The request has already completed and there
  847. * is nothing to do here other than to set the task
  848. * done bit, and indicate that the task abort function
  849. * was sucessful.
  850. */
  851. spin_lock_irqsave(&task->task_state_lock, flags);
  852. task->task_state_flags |= SAS_TASK_STATE_DONE;
  853. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  854. SAS_TASK_STATE_PENDING);
  855. spin_unlock_irqrestore(&task->task_state_lock, flags);
  856. ret = TMF_RESP_FUNC_COMPLETE;
  857. dev_dbg(&isci_host->pdev->dev,
  858. "%s: abort task not needed for %p\n",
  859. __func__, task);
  860. goto out;
  861. }
  862. spin_lock_irqsave(&isci_host->scic_lock, flags);
  863. /* Check the request status and change to "aborted" if currently
  864. * "starting"; if true then set the I/O kernel completion
  865. * struct that will be triggered when the request completes.
  866. */
  867. old_state = isci_task_validate_request_to_abort(
  868. old_request, isci_host, isci_device,
  869. &aborted_io_completion);
  870. if ((old_state != started) &&
  871. (old_state != completed) &&
  872. (old_state != aborting)) {
  873. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  874. /* The request was already being handled by someone else (because
  875. * they got to set the state away from started).
  876. */
  877. dev_dbg(&isci_host->pdev->dev,
  878. "%s: device = %p; old_request %p already being aborted\n",
  879. __func__,
  880. isci_device, old_request);
  881. ret = TMF_RESP_FUNC_COMPLETE;
  882. goto out;
  883. }
  884. if (task->task_proto == SAS_PROTOCOL_SMP ||
  885. sas_protocol_ata(task->task_proto) ||
  886. target_done_already) {
  887. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  888. dev_dbg(&isci_host->pdev->dev,
  889. "%s: %s request"
  890. " or complete_in_target (%d), thus no TMF\n",
  891. __func__,
  892. ((task->task_proto == SAS_PROTOCOL_SMP)
  893. ? "SMP"
  894. : (sas_protocol_ata(task->task_proto)
  895. ? "SATA/STP"
  896. : "<other>")
  897. ),
  898. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
  899. if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  900. spin_lock_irqsave(&task->task_state_lock, flags);
  901. task->task_state_flags |= SAS_TASK_STATE_DONE;
  902. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  903. SAS_TASK_STATE_PENDING);
  904. spin_unlock_irqrestore(&task->task_state_lock, flags);
  905. ret = TMF_RESP_FUNC_COMPLETE;
  906. } else {
  907. spin_lock_irqsave(&task->task_state_lock, flags);
  908. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  909. SAS_TASK_STATE_PENDING);
  910. spin_unlock_irqrestore(&task->task_state_lock, flags);
  911. }
  912. /* STP and SMP devices are not sent a TMF, but the
  913. * outstanding I/O request is terminated below. This is
  914. * because SATA/STP and SMP discovery path timeouts directly
  915. * call the abort task interface for cleanup.
  916. */
  917. perform_termination = 1;
  918. } else {
  919. /* Fill in the tmf stucture */
  920. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  921. isci_abort_task_process_cb,
  922. old_request);
  923. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  924. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  925. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
  926. ISCI_ABORT_TASK_TIMEOUT_MS);
  927. if (ret == TMF_RESP_FUNC_COMPLETE)
  928. perform_termination = 1;
  929. else
  930. dev_dbg(&isci_host->pdev->dev,
  931. "%s: isci_task_send_tmf failed\n", __func__);
  932. }
  933. if (perform_termination) {
  934. set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
  935. /* Clean up the request on our side, and wait for the aborted
  936. * I/O to complete.
  937. */
  938. isci_terminate_request_core(isci_host, isci_device,
  939. old_request);
  940. }
  941. /* Make sure we do not leave a reference to aborted_io_completion */
  942. old_request->io_request_completion = NULL;
  943. out:
  944. isci_put_device(isci_device);
  945. return ret;
  946. }
  947. /**
  948. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  949. * functions. This is one of the Task Management functoins called by libsas,
  950. * to abort all task for the given lun.
  951. * @d_device: This parameter specifies the domain device associated with this
  952. * request.
  953. * @lun: This parameter specifies the lun associated with this request.
  954. *
  955. * status, zero indicates success.
  956. */
  957. int isci_task_abort_task_set(
  958. struct domain_device *d_device,
  959. u8 *lun)
  960. {
  961. return TMF_RESP_FUNC_FAILED;
  962. }
  963. /**
  964. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  965. * functions. This is one of the Task Management functoins called by libsas.
  966. * @d_device: This parameter specifies the domain device associated with this
  967. * request.
  968. * @lun: This parameter specifies the lun associated with this request.
  969. *
  970. * status, zero indicates success.
  971. */
  972. int isci_task_clear_aca(
  973. struct domain_device *d_device,
  974. u8 *lun)
  975. {
  976. return TMF_RESP_FUNC_FAILED;
  977. }
  978. /**
  979. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  980. * functions. This is one of the Task Management functoins called by libsas.
  981. * @d_device: This parameter specifies the domain device associated with this
  982. * request.
  983. * @lun: This parameter specifies the lun associated with this request.
  984. *
  985. * status, zero indicates success.
  986. */
  987. int isci_task_clear_task_set(
  988. struct domain_device *d_device,
  989. u8 *lun)
  990. {
  991. return TMF_RESP_FUNC_FAILED;
  992. }
  993. /**
  994. * isci_task_query_task() - This function is implemented to cause libsas to
  995. * correctly escalate the failed abort to a LUN or target reset (this is
  996. * because sas_scsi_find_task libsas function does not correctly interpret
  997. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  998. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  999. * returned, libsas will turn this into a target reset
  1000. * @task: This parameter specifies the sas task being queried.
  1001. * @lun: This parameter specifies the lun associated with this request.
  1002. *
  1003. * status, zero indicates success.
  1004. */
  1005. int isci_task_query_task(
  1006. struct sas_task *task)
  1007. {
  1008. /* See if there is a pending device reset for this device. */
  1009. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1010. return TMF_RESP_FUNC_FAILED;
  1011. else
  1012. return TMF_RESP_FUNC_SUCC;
  1013. }
  1014. /*
  1015. * isci_task_request_complete() - This function is called by the sci core when
  1016. * an task request completes.
  1017. * @ihost: This parameter specifies the ISCI host object
  1018. * @ireq: This parameter is the completed isci_request object.
  1019. * @completion_status: This parameter specifies the completion status from the
  1020. * sci core.
  1021. *
  1022. * none.
  1023. */
  1024. void
  1025. isci_task_request_complete(struct isci_host *ihost,
  1026. struct isci_request *ireq,
  1027. enum sci_task_status completion_status)
  1028. {
  1029. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1030. struct completion *tmf_complete = NULL;
  1031. struct completion *request_complete = ireq->io_request_completion;
  1032. dev_dbg(&ihost->pdev->dev,
  1033. "%s: request = %p, status=%d\n",
  1034. __func__, ireq, completion_status);
  1035. isci_request_change_state(ireq, completed);
  1036. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  1037. if (tmf) {
  1038. tmf->status = completion_status;
  1039. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1040. memcpy(&tmf->resp.resp_iu,
  1041. &ireq->ssp.rsp,
  1042. SSP_RESP_IU_MAX_SIZE);
  1043. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1044. memcpy(&tmf->resp.d2h_fis,
  1045. &ireq->stp.rsp,
  1046. sizeof(struct dev_to_host_fis));
  1047. }
  1048. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1049. tmf_complete = tmf->complete;
  1050. }
  1051. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  1052. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1053. * or completed again.
  1054. */
  1055. set_bit(IREQ_TERMINATED, &ireq->flags);
  1056. /* As soon as something is in the terminate path, deallocation is
  1057. * managed there. Note that the final non-managed state of a task
  1058. * request is "completed".
  1059. */
  1060. if ((ireq->status == completed) ||
  1061. !isci_request_is_dealloc_managed(ireq->status)) {
  1062. isci_request_change_state(ireq, unallocated);
  1063. isci_free_tag(ihost, ireq->io_tag);
  1064. list_del_init(&ireq->dev_node);
  1065. }
  1066. /* "request_complete" is set if the task was being terminated. */
  1067. if (request_complete)
  1068. complete(request_complete);
  1069. /* The task management part completes last. */
  1070. if (tmf_complete)
  1071. complete(tmf_complete);
  1072. }
  1073. static int isci_reset_device(struct isci_host *ihost,
  1074. struct domain_device *dev,
  1075. struct isci_remote_device *idev)
  1076. {
  1077. int rc;
  1078. unsigned long flags;
  1079. enum sci_status status;
  1080. struct sas_phy *phy = sas_get_local_phy(dev);
  1081. struct isci_port *iport = dev->port->lldd_port;
  1082. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1083. spin_lock_irqsave(&ihost->scic_lock, flags);
  1084. status = sci_remote_device_reset(idev);
  1085. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1086. if (status != SCI_SUCCESS) {
  1087. dev_dbg(&ihost->pdev->dev,
  1088. "%s: sci_remote_device_reset(%p) returned %d!\n",
  1089. __func__, idev, status);
  1090. rc = TMF_RESP_FUNC_FAILED;
  1091. goto out;
  1092. }
  1093. if (scsi_is_sas_phy_local(phy)) {
  1094. struct isci_phy *iphy = &ihost->phys[phy->number];
  1095. rc = isci_port_perform_hard_reset(ihost, iport, iphy);
  1096. } else
  1097. rc = sas_phy_reset(phy, !dev_is_sata(dev));
  1098. /* Terminate in-progress I/O now. */
  1099. isci_remote_device_nuke_requests(ihost, idev);
  1100. /* Since all pending TCs have been cleaned, resume the RNC. */
  1101. spin_lock_irqsave(&ihost->scic_lock, flags);
  1102. status = sci_remote_device_reset_complete(idev);
  1103. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1104. if (status != SCI_SUCCESS) {
  1105. dev_dbg(&ihost->pdev->dev,
  1106. "%s: sci_remote_device_reset_complete(%p) "
  1107. "returned %d!\n", __func__, idev, status);
  1108. }
  1109. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1110. out:
  1111. sas_put_local_phy(phy);
  1112. return rc;
  1113. }
  1114. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1115. {
  1116. struct isci_host *ihost = dev_to_ihost(dev);
  1117. struct isci_remote_device *idev;
  1118. unsigned long flags;
  1119. int ret;
  1120. spin_lock_irqsave(&ihost->scic_lock, flags);
  1121. idev = isci_lookup_device(dev);
  1122. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1123. if (!idev) {
  1124. /* XXX: need to cleanup any ireqs targeting this
  1125. * domain_device
  1126. */
  1127. ret = -ENODEV;
  1128. goto out;
  1129. }
  1130. ret = isci_reset_device(ihost, dev, idev);
  1131. out:
  1132. isci_put_device(idev);
  1133. return ret;
  1134. }