task.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. isci_remote_device_suspend(ihost, idev);
  614. spin_lock_irqsave(&ihost->scic_lock, flags);
  615. list_splice_init(&idev->reqs_in_process, &list);
  616. /* assumes that isci_terminate_request_core deletes from the list */
  617. while (!list_empty(&list)) {
  618. struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
  619. /* Change state to "terminating" if it is currently
  620. * "started".
  621. */
  622. old_state = isci_request_change_started_to_newstate(ireq,
  623. &request_completion,
  624. terminating);
  625. switch (old_state) {
  626. case started:
  627. case completed:
  628. case aborting:
  629. break;
  630. default:
  631. /* termination in progress, or otherwise dispositioned.
  632. * We know the request was on 'list' so should be safe
  633. * to move it back to reqs_in_process
  634. */
  635. list_move(&ireq->dev_node, &idev->reqs_in_process);
  636. ireq = NULL;
  637. break;
  638. }
  639. if (!ireq)
  640. continue;
  641. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  642. init_completion(&request_completion);
  643. dev_dbg(&ihost->pdev->dev,
  644. "%s: idev=%p request=%p; task=%p old_state=%d\n",
  645. __func__, idev, ireq,
  646. (!test_bit(IREQ_TMF, &ireq->flags)
  647. ? isci_request_access_task(ireq)
  648. : NULL),
  649. old_state);
  650. /* If the old_state is started:
  651. * This request was not already being aborted. If it had been,
  652. * then the aborting I/O (ie. the TMF request) would not be in
  653. * the aborting state, and thus would be terminated here. Note
  654. * that since the TMF completion's call to the kernel function
  655. * "complete()" does not happen until the pending I/O request
  656. * terminate fully completes, we do not have to implement a
  657. * special wait here for already aborting requests - the
  658. * termination of the TMF request will force the request
  659. * to finish it's already started terminate.
  660. *
  661. * If old_state == completed:
  662. * This request completed from the SCU hardware perspective
  663. * and now just needs cleaning up in terms of freeing the
  664. * request and potentially calling up to libsas.
  665. *
  666. * If old_state == aborting:
  667. * This request has already gone through a TMF timeout, but may
  668. * not have been terminated; needs cleaning up at least.
  669. */
  670. isci_terminate_request_core(ihost, idev, ireq);
  671. spin_lock_irqsave(&ihost->scic_lock, flags);
  672. }
  673. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  674. }
  675. /**
  676. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  677. * Template functions.
  678. * @lun: This parameter specifies the lun to be reset.
  679. *
  680. * status, zero indicates success.
  681. */
  682. static int isci_task_send_lu_reset_sas(
  683. struct isci_host *isci_host,
  684. struct isci_remote_device *isci_device,
  685. u8 *lun)
  686. {
  687. struct isci_tmf tmf;
  688. int ret = TMF_RESP_FUNC_FAILED;
  689. dev_dbg(&isci_host->pdev->dev,
  690. "%s: isci_host = %p, isci_device = %p\n",
  691. __func__, isci_host, isci_device);
  692. /* Send the LUN reset to the target. By the time the call returns,
  693. * the TMF has fully exected in the target (in which case the return
  694. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  695. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  696. */
  697. isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
  698. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  699. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  700. if (ret == TMF_RESP_FUNC_COMPLETE)
  701. dev_dbg(&isci_host->pdev->dev,
  702. "%s: %p: TMF_LU_RESET passed\n",
  703. __func__, isci_device);
  704. else
  705. dev_dbg(&isci_host->pdev->dev,
  706. "%s: %p: TMF_LU_RESET failed (%x)\n",
  707. __func__, isci_device, ret);
  708. return ret;
  709. }
  710. int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
  711. {
  712. struct isci_host *ihost = dev_to_ihost(dev);
  713. struct isci_remote_device *isci_device;
  714. unsigned long flags;
  715. int ret;
  716. spin_lock_irqsave(&ihost->scic_lock, flags);
  717. isci_device = isci_lookup_device(dev);
  718. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  719. dev_dbg(&ihost->pdev->dev,
  720. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  721. __func__, dev, ihost, isci_device);
  722. if (!isci_device) {
  723. /* If the device is gone, escalate to I_T_Nexus_Reset. */
  724. dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
  725. ret = TMF_RESP_FUNC_FAILED;
  726. goto out;
  727. }
  728. if (isci_remote_device_suspend(ihost, isci_device) != SCI_SUCCESS) {
  729. dev_dbg(&ihost->pdev->dev,
  730. "%s: device = %p; failed to suspend\n",
  731. __func__, isci_device);
  732. ret = TMF_RESP_FUNC_FAILED;
  733. goto out;
  734. }
  735. /* Send the task management part of the reset. */
  736. if (dev_is_sata(dev)) {
  737. sas_ata_schedule_reset(dev);
  738. ret = TMF_RESP_FUNC_COMPLETE;
  739. } else
  740. ret = isci_task_send_lu_reset_sas(ihost, isci_device, lun);
  741. /* If the LUN reset worked, all the I/O can now be terminated. */
  742. if (ret == TMF_RESP_FUNC_COMPLETE) {
  743. /* Terminate all I/O now. */
  744. isci_terminate_pending_requests(ihost, isci_device);
  745. isci_remote_device_resume(ihost, isci_device, NULL, NULL);
  746. }
  747. out:
  748. isci_put_device(isci_device);
  749. return ret;
  750. }
  751. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  752. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  753. {
  754. return TMF_RESP_FUNC_FAILED;
  755. }
  756. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  757. {
  758. return TMF_RESP_FUNC_FAILED;
  759. }
  760. /* Task Management Functions. Must be called from process context. */
  761. /**
  762. * isci_abort_task_process_cb() - This is a helper function for the abort task
  763. * TMF command. It manages the request state with respect to the successful
  764. * transmission / completion of the abort task request.
  765. * @cb_state: This parameter specifies when this function was called - after
  766. * the TMF request has been started and after it has timed-out.
  767. * @tmf: This parameter specifies the TMF in progress.
  768. *
  769. *
  770. */
  771. static void isci_abort_task_process_cb(
  772. enum isci_tmf_cb_state cb_state,
  773. struct isci_tmf *tmf,
  774. void *cb_data)
  775. {
  776. struct isci_request *old_request;
  777. old_request = (struct isci_request *)cb_data;
  778. dev_dbg(&old_request->isci_host->pdev->dev,
  779. "%s: tmf=%p, old_request=%p\n",
  780. __func__, tmf, old_request);
  781. switch (cb_state) {
  782. case isci_tmf_started:
  783. /* The TMF has been started. Nothing to do here, since the
  784. * request state was already set to "aborted" by the abort
  785. * task function.
  786. */
  787. if ((old_request->status != aborted)
  788. && (old_request->status != completed))
  789. dev_dbg(&old_request->isci_host->pdev->dev,
  790. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  791. __func__, old_request->status, tmf, old_request);
  792. break;
  793. case isci_tmf_timed_out:
  794. /* Set the task's state to "aborting", since the abort task
  795. * function thread set it to "aborted" (above) in anticipation
  796. * of the task management request working correctly. Since the
  797. * timeout has now fired, the TMF request failed. We set the
  798. * state such that the request completion will indicate the
  799. * device is no longer present.
  800. */
  801. isci_request_change_state(old_request, aborting);
  802. break;
  803. default:
  804. dev_dbg(&old_request->isci_host->pdev->dev,
  805. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  806. __func__, cb_state, tmf, old_request);
  807. break;
  808. }
  809. }
  810. /**
  811. * isci_task_abort_task() - This function is one of the SAS Domain Template
  812. * functions. This function is called by libsas to abort a specified task.
  813. * @task: This parameter specifies the SAS task to abort.
  814. *
  815. * status, zero indicates success.
  816. */
  817. int isci_task_abort_task(struct sas_task *task)
  818. {
  819. struct isci_host *isci_host = dev_to_ihost(task->dev);
  820. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  821. struct isci_request *old_request = NULL;
  822. enum isci_request_status old_state;
  823. struct isci_remote_device *isci_device = NULL;
  824. struct isci_tmf tmf;
  825. int ret = TMF_RESP_FUNC_FAILED;
  826. unsigned long flags;
  827. int perform_termination = 0;
  828. /* Get the isci_request reference from the task. Note that
  829. * this check does not depend on the pending request list
  830. * in the device, because tasks driving resets may land here
  831. * after completion in the core.
  832. */
  833. spin_lock_irqsave(&isci_host->scic_lock, flags);
  834. spin_lock(&task->task_state_lock);
  835. old_request = task->lldd_task;
  836. /* If task is already done, the request isn't valid */
  837. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  838. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  839. old_request)
  840. isci_device = isci_lookup_device(task->dev);
  841. spin_unlock(&task->task_state_lock);
  842. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  843. dev_warn(&isci_host->pdev->dev,
  844. "%s: dev = %p, task = %p, old_request == %p\n",
  845. __func__, isci_device, task, old_request);
  846. /* Device reset conditions signalled in task_state_flags are the
  847. * responsbility of libsas to observe at the start of the error
  848. * handler thread.
  849. */
  850. if (!isci_device || !old_request) {
  851. /* The request has already completed and there
  852. * is nothing to do here other than to set the task
  853. * done bit, and indicate that the task abort function
  854. * was sucessful.
  855. */
  856. spin_lock_irqsave(&task->task_state_lock, flags);
  857. task->task_state_flags |= SAS_TASK_STATE_DONE;
  858. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  859. SAS_TASK_STATE_PENDING);
  860. spin_unlock_irqrestore(&task->task_state_lock, flags);
  861. ret = TMF_RESP_FUNC_COMPLETE;
  862. dev_warn(&isci_host->pdev->dev,
  863. "%s: abort task not needed for %p\n",
  864. __func__, task);
  865. goto out;
  866. }
  867. spin_lock_irqsave(&isci_host->scic_lock, flags);
  868. /* Check the request status and change to "aborted" if currently
  869. * "starting"; if true then set the I/O kernel completion
  870. * struct that will be triggered when the request completes.
  871. */
  872. old_state = isci_task_validate_request_to_abort(
  873. old_request, isci_host, isci_device,
  874. &aborted_io_completion);
  875. if ((old_state != started) &&
  876. (old_state != completed) &&
  877. (old_state != aborting)) {
  878. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  879. /* The request was already being handled by someone else (because
  880. * they got to set the state away from started).
  881. */
  882. dev_warn(&isci_host->pdev->dev,
  883. "%s: device = %p; old_request %p already being aborted\n",
  884. __func__,
  885. isci_device, old_request);
  886. ret = TMF_RESP_FUNC_COMPLETE;
  887. goto out;
  888. }
  889. if (task->task_proto == SAS_PROTOCOL_SMP ||
  890. sas_protocol_ata(task->task_proto) ||
  891. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  892. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  893. dev_warn(&isci_host->pdev->dev,
  894. "%s: %s request"
  895. " or complete_in_target (%d), thus no TMF\n",
  896. __func__,
  897. ((task->task_proto == SAS_PROTOCOL_SMP)
  898. ? "SMP"
  899. : (sas_protocol_ata(task->task_proto)
  900. ? "SATA/STP"
  901. : "<other>")
  902. ),
  903. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
  904. if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  905. spin_lock_irqsave(&task->task_state_lock, flags);
  906. task->task_state_flags |= SAS_TASK_STATE_DONE;
  907. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  908. SAS_TASK_STATE_PENDING);
  909. spin_unlock_irqrestore(&task->task_state_lock, flags);
  910. ret = TMF_RESP_FUNC_COMPLETE;
  911. } else {
  912. spin_lock_irqsave(&task->task_state_lock, flags);
  913. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  914. SAS_TASK_STATE_PENDING);
  915. spin_unlock_irqrestore(&task->task_state_lock, flags);
  916. }
  917. /* STP and SMP devices are not sent a TMF, but the
  918. * outstanding I/O request is terminated below. This is
  919. * because SATA/STP and SMP discovery path timeouts directly
  920. * call the abort task interface for cleanup.
  921. */
  922. perform_termination = 1;
  923. if (isci_device && !test_bit(IDEV_GONE, &isci_device->flags) &&
  924. (isci_remote_device_suspend(isci_host, isci_device)
  925. != SCI_SUCCESS)) {
  926. dev_warn(&isci_host->pdev->dev,
  927. "%s: device = %p; failed to suspend\n",
  928. __func__, isci_device);
  929. goto out;
  930. }
  931. } else {
  932. /* Fill in the tmf stucture */
  933. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  934. isci_abort_task_process_cb,
  935. old_request);
  936. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  937. if (isci_remote_device_suspend(isci_host, isci_device)
  938. != SCI_SUCCESS) {
  939. dev_warn(&isci_host->pdev->dev,
  940. "%s: device = %p; failed to suspend\n",
  941. __func__, isci_device);
  942. goto out;
  943. }
  944. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  945. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
  946. ISCI_ABORT_TASK_TIMEOUT_MS);
  947. if (ret == TMF_RESP_FUNC_COMPLETE)
  948. perform_termination = 1;
  949. else
  950. dev_warn(&isci_host->pdev->dev,
  951. "%s: isci_task_send_tmf failed\n", __func__);
  952. }
  953. if (perform_termination) {
  954. set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
  955. /* Clean up the request on our side, and wait for the aborted
  956. * I/O to complete.
  957. */
  958. isci_terminate_request_core(isci_host, isci_device,
  959. old_request);
  960. isci_remote_device_resume(isci_host, isci_device, NULL, NULL);
  961. }
  962. /* Make sure we do not leave a reference to aborted_io_completion */
  963. old_request->io_request_completion = NULL;
  964. out:
  965. isci_put_device(isci_device);
  966. return ret;
  967. }
  968. /**
  969. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  970. * functions. This is one of the Task Management functoins called by libsas,
  971. * to abort all task for the given lun.
  972. * @d_device: This parameter specifies the domain device associated with this
  973. * request.
  974. * @lun: This parameter specifies the lun associated with this request.
  975. *
  976. * status, zero indicates success.
  977. */
  978. int isci_task_abort_task_set(
  979. struct domain_device *d_device,
  980. u8 *lun)
  981. {
  982. return TMF_RESP_FUNC_FAILED;
  983. }
  984. /**
  985. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  986. * functions. This is one of the Task Management functoins called by libsas.
  987. * @d_device: This parameter specifies the domain device associated with this
  988. * request.
  989. * @lun: This parameter specifies the lun associated with this request.
  990. *
  991. * status, zero indicates success.
  992. */
  993. int isci_task_clear_aca(
  994. struct domain_device *d_device,
  995. u8 *lun)
  996. {
  997. return TMF_RESP_FUNC_FAILED;
  998. }
  999. /**
  1000. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1001. * functions. This is one of the Task Management functoins called by libsas.
  1002. * @d_device: This parameter specifies the domain device associated with this
  1003. * request.
  1004. * @lun: This parameter specifies the lun associated with this request.
  1005. *
  1006. * status, zero indicates success.
  1007. */
  1008. int isci_task_clear_task_set(
  1009. struct domain_device *d_device,
  1010. u8 *lun)
  1011. {
  1012. return TMF_RESP_FUNC_FAILED;
  1013. }
  1014. /**
  1015. * isci_task_query_task() - This function is implemented to cause libsas to
  1016. * correctly escalate the failed abort to a LUN or target reset (this is
  1017. * because sas_scsi_find_task libsas function does not correctly interpret
  1018. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1019. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1020. * returned, libsas will turn this into a target reset
  1021. * @task: This parameter specifies the sas task being queried.
  1022. * @lun: This parameter specifies the lun associated with this request.
  1023. *
  1024. * status, zero indicates success.
  1025. */
  1026. int isci_task_query_task(
  1027. struct sas_task *task)
  1028. {
  1029. /* See if there is a pending device reset for this device. */
  1030. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1031. return TMF_RESP_FUNC_FAILED;
  1032. else
  1033. return TMF_RESP_FUNC_SUCC;
  1034. }
  1035. /*
  1036. * isci_task_request_complete() - This function is called by the sci core when
  1037. * an task request completes.
  1038. * @ihost: This parameter specifies the ISCI host object
  1039. * @ireq: This parameter is the completed isci_request object.
  1040. * @completion_status: This parameter specifies the completion status from the
  1041. * sci core.
  1042. *
  1043. * none.
  1044. */
  1045. void
  1046. isci_task_request_complete(struct isci_host *ihost,
  1047. struct isci_request *ireq,
  1048. enum sci_task_status completion_status)
  1049. {
  1050. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1051. struct completion *tmf_complete = NULL;
  1052. struct completion *request_complete = ireq->io_request_completion;
  1053. dev_dbg(&ihost->pdev->dev,
  1054. "%s: request = %p, status=%d\n",
  1055. __func__, ireq, completion_status);
  1056. isci_request_change_state(ireq, completed);
  1057. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  1058. if (tmf) {
  1059. tmf->status = completion_status;
  1060. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1061. memcpy(&tmf->resp.resp_iu,
  1062. &ireq->ssp.rsp,
  1063. SSP_RESP_IU_MAX_SIZE);
  1064. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1065. memcpy(&tmf->resp.d2h_fis,
  1066. &ireq->stp.rsp,
  1067. sizeof(struct dev_to_host_fis));
  1068. }
  1069. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1070. tmf_complete = tmf->complete;
  1071. }
  1072. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  1073. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1074. * or completed again.
  1075. */
  1076. set_bit(IREQ_TERMINATED, &ireq->flags);
  1077. /* As soon as something is in the terminate path, deallocation is
  1078. * managed there. Note that the final non-managed state of a task
  1079. * request is "completed".
  1080. */
  1081. if ((ireq->status == completed) ||
  1082. !isci_request_is_dealloc_managed(ireq->status)) {
  1083. isci_request_change_state(ireq, unallocated);
  1084. isci_free_tag(ihost, ireq->io_tag);
  1085. list_del_init(&ireq->dev_node);
  1086. }
  1087. /* "request_complete" is set if the task was being terminated. */
  1088. if (request_complete)
  1089. complete(request_complete);
  1090. /* The task management part completes last. */
  1091. if (tmf_complete)
  1092. complete(tmf_complete);
  1093. }
  1094. static int isci_reset_device(struct isci_host *ihost,
  1095. struct domain_device *dev,
  1096. struct isci_remote_device *idev)
  1097. {
  1098. int rc;
  1099. enum sci_status status;
  1100. struct sas_phy *phy = sas_get_local_phy(dev);
  1101. struct isci_port *iport = dev->port->lldd_port;
  1102. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1103. if (isci_remote_device_reset(ihost, idev) != SCI_SUCCESS) {
  1104. rc = TMF_RESP_FUNC_FAILED;
  1105. goto out;
  1106. }
  1107. if (scsi_is_sas_phy_local(phy)) {
  1108. struct isci_phy *iphy = &ihost->phys[phy->number];
  1109. rc = isci_port_perform_hard_reset(ihost, iport, iphy);
  1110. } else
  1111. rc = sas_phy_reset(phy, !dev_is_sata(dev));
  1112. /* Terminate in-progress I/O now. */
  1113. isci_remote_device_nuke_requests(ihost, idev);
  1114. /* Since all pending TCs have been cleaned, resume the RNC. */
  1115. status = isci_remote_device_reset_complete(ihost, idev);
  1116. if (status != SCI_SUCCESS)
  1117. dev_dbg(&ihost->pdev->dev,
  1118. "%s: isci_remote_device_reset_complete(%p) "
  1119. "returned %d!\n", __func__, idev, status);
  1120. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1121. out:
  1122. sas_put_local_phy(phy);
  1123. return rc;
  1124. }
  1125. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1126. {
  1127. struct isci_host *ihost = dev_to_ihost(dev);
  1128. struct isci_remote_device *idev;
  1129. unsigned long flags;
  1130. int ret;
  1131. spin_lock_irqsave(&ihost->scic_lock, flags);
  1132. idev = isci_get_device(dev);
  1133. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1134. if (!idev) {
  1135. /* XXX: need to cleanup any ireqs targeting this
  1136. * domain_device
  1137. */
  1138. ret = TMF_RESP_FUNC_COMPLETE;
  1139. goto out;
  1140. }
  1141. ret = isci_reset_device(ihost, dev, idev);
  1142. out:
  1143. isci_put_device(idev);
  1144. return ret;
  1145. }