task.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. /* Get the isci_request reference from the task. Note that
  820. * this check does not depend on the pending request list
  821. * in the device, because tasks driving resets may land here
  822. * after completion in the core.
  823. */
  824. spin_lock_irqsave(&isci_host->scic_lock, flags);
  825. spin_lock(&task->task_state_lock);
  826. old_request = task->lldd_task;
  827. /* If task is already done, the request isn't valid */
  828. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  829. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  830. old_request)
  831. isci_device = isci_lookup_device(task->dev);
  832. spin_unlock(&task->task_state_lock);
  833. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  834. dev_dbg(&isci_host->pdev->dev,
  835. "%s: dev = %p, task = %p, old_request == %p\n",
  836. __func__, isci_device, task, old_request);
  837. /* Device reset conditions signalled in task_state_flags are the
  838. * responsbility of libsas to observe at the start of the error
  839. * handler thread.
  840. */
  841. if (!isci_device || !old_request) {
  842. /* The request has already completed and there
  843. * is nothing to do here other than to set the task
  844. * done bit, and indicate that the task abort function
  845. * was sucessful.
  846. */
  847. spin_lock_irqsave(&task->task_state_lock, flags);
  848. task->task_state_flags |= SAS_TASK_STATE_DONE;
  849. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  850. SAS_TASK_STATE_PENDING);
  851. spin_unlock_irqrestore(&task->task_state_lock, flags);
  852. ret = TMF_RESP_FUNC_COMPLETE;
  853. dev_dbg(&isci_host->pdev->dev,
  854. "%s: abort task not needed for %p\n",
  855. __func__, task);
  856. goto out;
  857. }
  858. spin_lock_irqsave(&isci_host->scic_lock, flags);
  859. /* Check the request status and change to "aborted" if currently
  860. * "starting"; if true then set the I/O kernel completion
  861. * struct that will be triggered when the request completes.
  862. */
  863. old_state = isci_task_validate_request_to_abort(
  864. old_request, isci_host, isci_device,
  865. &aborted_io_completion);
  866. if ((old_state != started) &&
  867. (old_state != completed) &&
  868. (old_state != aborting)) {
  869. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  870. /* The request was already being handled by someone else (because
  871. * they got to set the state away from started).
  872. */
  873. dev_dbg(&isci_host->pdev->dev,
  874. "%s: device = %p; old_request %p already being aborted\n",
  875. __func__,
  876. isci_device, old_request);
  877. ret = TMF_RESP_FUNC_COMPLETE;
  878. goto out;
  879. }
  880. if (task->task_proto == SAS_PROTOCOL_SMP ||
  881. sas_protocol_ata(task->task_proto) ||
  882. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  883. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  884. dev_dbg(&isci_host->pdev->dev,
  885. "%s: %s request"
  886. " or complete_in_target (%d), thus no TMF\n",
  887. __func__,
  888. ((task->task_proto == SAS_PROTOCOL_SMP)
  889. ? "SMP"
  890. : (sas_protocol_ata(task->task_proto)
  891. ? "SATA/STP"
  892. : "<other>")
  893. ),
  894. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
  895. if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  896. spin_lock_irqsave(&task->task_state_lock, flags);
  897. task->task_state_flags |= SAS_TASK_STATE_DONE;
  898. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  899. SAS_TASK_STATE_PENDING);
  900. spin_unlock_irqrestore(&task->task_state_lock, flags);
  901. ret = TMF_RESP_FUNC_COMPLETE;
  902. } else {
  903. spin_lock_irqsave(&task->task_state_lock, flags);
  904. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  905. SAS_TASK_STATE_PENDING);
  906. spin_unlock_irqrestore(&task->task_state_lock, flags);
  907. }
  908. /* STP and SMP devices are not sent a TMF, but the
  909. * outstanding I/O request is terminated below. This is
  910. * because SATA/STP and SMP discovery path timeouts directly
  911. * call the abort task interface for cleanup.
  912. */
  913. perform_termination = 1;
  914. } else {
  915. /* Fill in the tmf stucture */
  916. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  917. isci_abort_task_process_cb,
  918. old_request);
  919. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  920. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  921. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
  922. ISCI_ABORT_TASK_TIMEOUT_MS);
  923. if (ret == TMF_RESP_FUNC_COMPLETE)
  924. perform_termination = 1;
  925. else
  926. dev_dbg(&isci_host->pdev->dev,
  927. "%s: isci_task_send_tmf failed\n", __func__);
  928. }
  929. if (perform_termination) {
  930. set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
  931. /* Clean up the request on our side, and wait for the aborted
  932. * I/O to complete.
  933. */
  934. isci_terminate_request_core(isci_host, isci_device,
  935. old_request);
  936. }
  937. /* Make sure we do not leave a reference to aborted_io_completion */
  938. old_request->io_request_completion = NULL;
  939. out:
  940. isci_put_device(isci_device);
  941. return ret;
  942. }
  943. /**
  944. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  945. * functions. This is one of the Task Management functoins called by libsas,
  946. * to abort all task for the given lun.
  947. * @d_device: This parameter specifies the domain device associated with this
  948. * request.
  949. * @lun: This parameter specifies the lun associated with this request.
  950. *
  951. * status, zero indicates success.
  952. */
  953. int isci_task_abort_task_set(
  954. struct domain_device *d_device,
  955. u8 *lun)
  956. {
  957. return TMF_RESP_FUNC_FAILED;
  958. }
  959. /**
  960. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  961. * functions. This is one of the Task Management functoins called by libsas.
  962. * @d_device: This parameter specifies the domain device associated with this
  963. * request.
  964. * @lun: This parameter specifies the lun associated with this request.
  965. *
  966. * status, zero indicates success.
  967. */
  968. int isci_task_clear_aca(
  969. struct domain_device *d_device,
  970. u8 *lun)
  971. {
  972. return TMF_RESP_FUNC_FAILED;
  973. }
  974. /**
  975. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  976. * functions. This is one of the Task Management functoins called by libsas.
  977. * @d_device: This parameter specifies the domain device associated with this
  978. * request.
  979. * @lun: This parameter specifies the lun associated with this request.
  980. *
  981. * status, zero indicates success.
  982. */
  983. int isci_task_clear_task_set(
  984. struct domain_device *d_device,
  985. u8 *lun)
  986. {
  987. return TMF_RESP_FUNC_FAILED;
  988. }
  989. /**
  990. * isci_task_query_task() - This function is implemented to cause libsas to
  991. * correctly escalate the failed abort to a LUN or target reset (this is
  992. * because sas_scsi_find_task libsas function does not correctly interpret
  993. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  994. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  995. * returned, libsas will turn this into a target reset
  996. * @task: This parameter specifies the sas task being queried.
  997. * @lun: This parameter specifies the lun associated with this request.
  998. *
  999. * status, zero indicates success.
  1000. */
  1001. int isci_task_query_task(
  1002. struct sas_task *task)
  1003. {
  1004. /* See if there is a pending device reset for this device. */
  1005. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1006. return TMF_RESP_FUNC_FAILED;
  1007. else
  1008. return TMF_RESP_FUNC_SUCC;
  1009. }
  1010. /*
  1011. * isci_task_request_complete() - This function is called by the sci core when
  1012. * an task request completes.
  1013. * @ihost: This parameter specifies the ISCI host object
  1014. * @ireq: This parameter is the completed isci_request object.
  1015. * @completion_status: This parameter specifies the completion status from the
  1016. * sci core.
  1017. *
  1018. * none.
  1019. */
  1020. void
  1021. isci_task_request_complete(struct isci_host *ihost,
  1022. struct isci_request *ireq,
  1023. enum sci_task_status completion_status)
  1024. {
  1025. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1026. struct completion *tmf_complete = NULL;
  1027. struct completion *request_complete = ireq->io_request_completion;
  1028. dev_dbg(&ihost->pdev->dev,
  1029. "%s: request = %p, status=%d\n",
  1030. __func__, ireq, completion_status);
  1031. isci_request_change_state(ireq, completed);
  1032. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  1033. if (tmf) {
  1034. tmf->status = completion_status;
  1035. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1036. memcpy(&tmf->resp.resp_iu,
  1037. &ireq->ssp.rsp,
  1038. SSP_RESP_IU_MAX_SIZE);
  1039. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1040. memcpy(&tmf->resp.d2h_fis,
  1041. &ireq->stp.rsp,
  1042. sizeof(struct dev_to_host_fis));
  1043. }
  1044. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1045. tmf_complete = tmf->complete;
  1046. }
  1047. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  1048. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1049. * or completed again.
  1050. */
  1051. set_bit(IREQ_TERMINATED, &ireq->flags);
  1052. /* As soon as something is in the terminate path, deallocation is
  1053. * managed there. Note that the final non-managed state of a task
  1054. * request is "completed".
  1055. */
  1056. if ((ireq->status == completed) ||
  1057. !isci_request_is_dealloc_managed(ireq->status)) {
  1058. isci_request_change_state(ireq, unallocated);
  1059. isci_free_tag(ihost, ireq->io_tag);
  1060. list_del_init(&ireq->dev_node);
  1061. }
  1062. /* "request_complete" is set if the task was being terminated. */
  1063. if (request_complete)
  1064. complete(request_complete);
  1065. /* The task management part completes last. */
  1066. if (tmf_complete)
  1067. complete(tmf_complete);
  1068. }
  1069. static int isci_reset_device(struct isci_host *ihost,
  1070. struct domain_device *dev,
  1071. struct isci_remote_device *idev)
  1072. {
  1073. int rc;
  1074. unsigned long flags;
  1075. enum sci_status status;
  1076. struct sas_phy *phy = sas_get_local_phy(dev);
  1077. struct isci_port *iport = dev->port->lldd_port;
  1078. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1079. spin_lock_irqsave(&ihost->scic_lock, flags);
  1080. status = sci_remote_device_reset(idev);
  1081. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1082. if (status != SCI_SUCCESS) {
  1083. dev_dbg(&ihost->pdev->dev,
  1084. "%s: sci_remote_device_reset(%p) returned %d!\n",
  1085. __func__, idev, status);
  1086. rc = TMF_RESP_FUNC_FAILED;
  1087. goto out;
  1088. }
  1089. if (scsi_is_sas_phy_local(phy)) {
  1090. struct isci_phy *iphy = &ihost->phys[phy->number];
  1091. rc = isci_port_perform_hard_reset(ihost, iport, iphy);
  1092. } else
  1093. rc = sas_phy_reset(phy, !dev_is_sata(dev));
  1094. /* Terminate in-progress I/O now. */
  1095. isci_remote_device_nuke_requests(ihost, idev);
  1096. /* Since all pending TCs have been cleaned, resume the RNC. */
  1097. spin_lock_irqsave(&ihost->scic_lock, flags);
  1098. status = sci_remote_device_reset_complete(idev);
  1099. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1100. if (status != SCI_SUCCESS) {
  1101. dev_dbg(&ihost->pdev->dev,
  1102. "%s: sci_remote_device_reset_complete(%p) "
  1103. "returned %d!\n", __func__, idev, status);
  1104. }
  1105. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1106. out:
  1107. sas_put_local_phy(phy);
  1108. return rc;
  1109. }
  1110. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1111. {
  1112. struct isci_host *ihost = dev_to_ihost(dev);
  1113. struct isci_remote_device *idev;
  1114. unsigned long flags;
  1115. int ret;
  1116. spin_lock_irqsave(&ihost->scic_lock, flags);
  1117. idev = isci_lookup_device(dev);
  1118. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1119. if (!idev) {
  1120. /* XXX: need to cleanup any ireqs targeting this
  1121. * domain_device
  1122. */
  1123. ret = TMF_RESP_FUNC_COMPLETE;
  1124. goto out;
  1125. }
  1126. ret = isci_reset_device(ihost, dev, idev);
  1127. out:
  1128. isci_put_device(idev);
  1129. return ret;
  1130. }