task.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. unsigned long flags;
  79. /* Normal notification (task_done) */
  80. dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
  81. __func__, task, response, status);
  82. spin_lock_irqsave(&task->task_state_lock, flags);
  83. task->task_status.resp = response;
  84. task->task_status.stat = status;
  85. /* Normal notification (task_done) */
  86. task->task_state_flags |= SAS_TASK_STATE_DONE;
  87. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  88. SAS_TASK_STATE_PENDING);
  89. task->lldd_task = NULL;
  90. spin_unlock_irqrestore(&task->task_state_lock, flags);
  91. task->task_done(task);
  92. }
  93. #define for_each_sas_task(num, task) \
  94. for (; num > 0; num--,\
  95. task = list_entry(task->list.next, struct sas_task, list))
  96. static inline int isci_device_io_ready(struct isci_remote_device *idev,
  97. struct sas_task *task)
  98. {
  99. return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
  100. (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
  101. isci_task_is_ncq_recovery(task))
  102. : 0;
  103. }
  104. /**
  105. * isci_task_execute_task() - This function is one of the SAS Domain Template
  106. * functions. This function is called by libsas to send a task down to
  107. * hardware.
  108. * @task: This parameter specifies the SAS task to send.
  109. * @num: This parameter specifies the number of tasks to queue.
  110. * @gfp_flags: This parameter specifies the context of this call.
  111. *
  112. * status, zero indicates success.
  113. */
  114. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  115. {
  116. struct isci_host *ihost = dev_to_ihost(task->dev);
  117. struct isci_remote_device *idev;
  118. unsigned long flags;
  119. bool io_ready;
  120. u16 tag;
  121. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  122. for_each_sas_task(num, task) {
  123. enum sci_status status = SCI_FAILURE;
  124. spin_lock_irqsave(&ihost->scic_lock, flags);
  125. idev = isci_lookup_device(task->dev);
  126. io_ready = isci_device_io_ready(idev, task);
  127. tag = isci_alloc_tag(ihost);
  128. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  129. dev_dbg(&ihost->pdev->dev,
  130. "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
  131. task, num, task->dev, idev, idev ? idev->flags : 0,
  132. task->uldd_task);
  133. if (!idev) {
  134. isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
  135. SAS_DEVICE_UNKNOWN);
  136. } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  137. /* Indicate QUEUE_FULL so that the scsi midlayer
  138. * retries.
  139. */
  140. isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
  141. SAS_QUEUE_FULL);
  142. } else {
  143. /* There is a device and it's ready for I/O. */
  144. spin_lock_irqsave(&task->task_state_lock, flags);
  145. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  146. /* The I/O was aborted. */
  147. spin_unlock_irqrestore(&task->task_state_lock,
  148. flags);
  149. isci_task_refuse(ihost, task,
  150. SAS_TASK_UNDELIVERED,
  151. SAM_STAT_TASK_ABORTED);
  152. } else {
  153. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  154. spin_unlock_irqrestore(&task->task_state_lock, flags);
  155. /* build and send the request. */
  156. status = isci_request_execute(ihost, idev, task, tag);
  157. if (status != SCI_SUCCESS) {
  158. spin_lock_irqsave(&task->task_state_lock, flags);
  159. /* Did not really start this command. */
  160. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  161. spin_unlock_irqrestore(&task->task_state_lock, flags);
  162. if (test_bit(IDEV_GONE, &idev->flags)) {
  163. /* Indicate that the device
  164. * is gone.
  165. */
  166. isci_task_refuse(ihost, task,
  167. SAS_TASK_UNDELIVERED,
  168. SAS_DEVICE_UNKNOWN);
  169. } else {
  170. /* Indicate QUEUE_FULL so that
  171. * the scsi midlayer retries.
  172. * If the request failed for
  173. * remote device reasons, it
  174. * gets returned as
  175. * SAS_TASK_UNDELIVERED next
  176. * time through.
  177. */
  178. isci_task_refuse(ihost, task,
  179. SAS_TASK_COMPLETE,
  180. SAS_QUEUE_FULL);
  181. }
  182. }
  183. }
  184. }
  185. if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  186. spin_lock_irqsave(&ihost->scic_lock, flags);
  187. /* command never hit the device, so just free
  188. * the tci and skip the sequence increment
  189. */
  190. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  191. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  192. }
  193. isci_put_device(idev);
  194. }
  195. return 0;
  196. }
  197. static struct isci_request *isci_task_request_build(struct isci_host *ihost,
  198. struct isci_remote_device *idev,
  199. u16 tag, struct isci_tmf *isci_tmf)
  200. {
  201. enum sci_status status = SCI_FAILURE;
  202. struct isci_request *ireq = NULL;
  203. struct domain_device *dev;
  204. dev_dbg(&ihost->pdev->dev,
  205. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  206. dev = idev->domain_dev;
  207. /* do common allocation and init of request object. */
  208. ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
  209. if (!ireq)
  210. return NULL;
  211. /* let the core do it's construct. */
  212. status = sci_task_request_construct(ihost, idev, tag,
  213. ireq);
  214. if (status != SCI_SUCCESS) {
  215. dev_warn(&ihost->pdev->dev,
  216. "%s: sci_task_request_construct failed - "
  217. "status = 0x%x\n",
  218. __func__,
  219. status);
  220. return NULL;
  221. }
  222. /* XXX convert to get this from task->tproto like other drivers */
  223. if (dev->dev_type == SAS_END_DEV) {
  224. isci_tmf->proto = SAS_PROTOCOL_SSP;
  225. status = sci_task_request_construct_ssp(ireq);
  226. if (status != SCI_SUCCESS)
  227. return NULL;
  228. }
  229. return ireq;
  230. }
  231. static int isci_task_execute_tmf(struct isci_host *ihost,
  232. struct isci_remote_device *idev,
  233. struct isci_tmf *tmf, unsigned long timeout_ms)
  234. {
  235. DECLARE_COMPLETION_ONSTACK(completion);
  236. enum sci_task_status status = SCI_TASK_FAILURE;
  237. struct isci_request *ireq;
  238. int ret = TMF_RESP_FUNC_FAILED;
  239. unsigned long flags;
  240. unsigned long timeleft;
  241. u16 tag;
  242. spin_lock_irqsave(&ihost->scic_lock, flags);
  243. tag = isci_alloc_tag(ihost);
  244. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  245. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  246. return ret;
  247. /* sanity check, return TMF_RESP_FUNC_FAILED
  248. * if the device is not there and ready.
  249. */
  250. if (!idev ||
  251. (!test_bit(IDEV_IO_READY, &idev->flags) &&
  252. !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
  253. dev_dbg(&ihost->pdev->dev,
  254. "%s: idev = %p not ready (%#lx)\n",
  255. __func__,
  256. idev, idev ? idev->flags : 0);
  257. goto err_tci;
  258. } else
  259. dev_dbg(&ihost->pdev->dev,
  260. "%s: idev = %p\n",
  261. __func__, idev);
  262. /* Assign the pointer to the TMF's completion kernel wait structure. */
  263. tmf->complete = &completion;
  264. tmf->status = SCI_FAILURE_TIMEOUT;
  265. ireq = isci_task_request_build(ihost, idev, tag, tmf);
  266. if (!ireq)
  267. goto err_tci;
  268. spin_lock_irqsave(&ihost->scic_lock, flags);
  269. /* start the TMF io. */
  270. status = sci_controller_start_task(ihost, idev, ireq);
  271. if (status != SCI_TASK_SUCCESS) {
  272. dev_dbg(&ihost->pdev->dev,
  273. "%s: start_io failed - status = 0x%x, request = %p\n",
  274. __func__,
  275. status,
  276. ireq);
  277. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  278. goto err_tci;
  279. }
  280. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  281. /* The RNC must be unsuspended before the TMF can get a response. */
  282. isci_remote_device_resume_from_abort(ihost, idev);
  283. /* Wait for the TMF to complete, or a timeout. */
  284. timeleft = wait_for_completion_timeout(&completion,
  285. msecs_to_jiffies(timeout_ms));
  286. if (timeleft == 0) {
  287. /* The TMF did not complete - this could be because
  288. * of an unplug. Terminate the TMF request now.
  289. */
  290. isci_remote_device_suspend_terminate(ihost, idev, ireq);
  291. }
  292. isci_print_tmf(ihost, tmf);
  293. if (tmf->status == SCI_SUCCESS)
  294. ret = TMF_RESP_FUNC_COMPLETE;
  295. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  296. dev_dbg(&ihost->pdev->dev,
  297. "%s: tmf.status == "
  298. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  299. __func__);
  300. ret = TMF_RESP_FUNC_COMPLETE;
  301. }
  302. /* Else - leave the default "failed" status alone. */
  303. dev_dbg(&ihost->pdev->dev,
  304. "%s: completed request = %p\n",
  305. __func__,
  306. ireq);
  307. return ret;
  308. err_tci:
  309. spin_lock_irqsave(&ihost->scic_lock, flags);
  310. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  311. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  312. return ret;
  313. }
  314. static void isci_task_build_tmf(struct isci_tmf *tmf,
  315. enum isci_tmf_function_codes code)
  316. {
  317. memset(tmf, 0, sizeof(*tmf));
  318. tmf->tmf_code = code;
  319. }
  320. static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
  321. enum isci_tmf_function_codes code,
  322. struct isci_request *old_request)
  323. {
  324. isci_task_build_tmf(tmf, code);
  325. tmf->io_tag = old_request->io_tag;
  326. }
  327. /**
  328. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  329. * Template functions.
  330. * @lun: This parameter specifies the lun to be reset.
  331. *
  332. * status, zero indicates success.
  333. */
  334. static int isci_task_send_lu_reset_sas(
  335. struct isci_host *isci_host,
  336. struct isci_remote_device *isci_device,
  337. u8 *lun)
  338. {
  339. struct isci_tmf tmf;
  340. int ret = TMF_RESP_FUNC_FAILED;
  341. dev_dbg(&isci_host->pdev->dev,
  342. "%s: isci_host = %p, isci_device = %p\n",
  343. __func__, isci_host, isci_device);
  344. /* Send the LUN reset to the target. By the time the call returns,
  345. * the TMF has fully exected in the target (in which case the return
  346. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  347. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  348. */
  349. isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
  350. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  351. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  352. if (ret == TMF_RESP_FUNC_COMPLETE)
  353. dev_dbg(&isci_host->pdev->dev,
  354. "%s: %p: TMF_LU_RESET passed\n",
  355. __func__, isci_device);
  356. else
  357. dev_dbg(&isci_host->pdev->dev,
  358. "%s: %p: TMF_LU_RESET failed (%x)\n",
  359. __func__, isci_device, ret);
  360. return ret;
  361. }
  362. int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
  363. {
  364. struct isci_host *ihost = dev_to_ihost(dev);
  365. struct isci_remote_device *idev;
  366. unsigned long flags;
  367. int ret = TMF_RESP_FUNC_COMPLETE;
  368. spin_lock_irqsave(&ihost->scic_lock, flags);
  369. idev = isci_get_device(dev->lldd_dev);
  370. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  371. dev_dbg(&ihost->pdev->dev,
  372. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  373. __func__, dev, ihost, idev);
  374. if (!idev) {
  375. /* If the device is gone, escalate to I_T_Nexus_Reset. */
  376. dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
  377. ret = TMF_RESP_FUNC_FAILED;
  378. goto out;
  379. }
  380. /* Suspend the RNC, kill all TCs */
  381. if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
  382. != SCI_SUCCESS) {
  383. /* The suspend/terminate only fails if isci_get_device fails */
  384. ret = TMF_RESP_FUNC_FAILED;
  385. goto out;
  386. }
  387. /* All pending I/Os have been terminated and cleaned up. */
  388. if (!test_bit(IDEV_GONE, &idev->flags)) {
  389. if (dev_is_sata(dev))
  390. sas_ata_schedule_reset(dev);
  391. else
  392. /* Send the task management part of the reset. */
  393. ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
  394. }
  395. out:
  396. isci_put_device(idev);
  397. return ret;
  398. }
  399. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  400. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  401. {
  402. return TMF_RESP_FUNC_FAILED;
  403. }
  404. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  405. {
  406. return TMF_RESP_FUNC_FAILED;
  407. }
  408. /* Task Management Functions. Must be called from process context. */
  409. /**
  410. * isci_task_abort_task() - This function is one of the SAS Domain Template
  411. * functions. This function is called by libsas to abort a specified task.
  412. * @task: This parameter specifies the SAS task to abort.
  413. *
  414. * status, zero indicates success.
  415. */
  416. int isci_task_abort_task(struct sas_task *task)
  417. {
  418. struct isci_host *ihost = dev_to_ihost(task->dev);
  419. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  420. struct isci_request *old_request = NULL;
  421. struct isci_remote_device *idev = NULL;
  422. struct isci_tmf tmf;
  423. int ret = TMF_RESP_FUNC_FAILED;
  424. unsigned long flags;
  425. /* Get the isci_request reference from the task. Note that
  426. * this check does not depend on the pending request list
  427. * in the device, because tasks driving resets may land here
  428. * after completion in the core.
  429. */
  430. spin_lock_irqsave(&ihost->scic_lock, flags);
  431. spin_lock(&task->task_state_lock);
  432. old_request = task->lldd_task;
  433. /* If task is already done, the request isn't valid */
  434. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  435. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  436. old_request)
  437. idev = isci_get_device(task->dev->lldd_dev);
  438. spin_unlock(&task->task_state_lock);
  439. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  440. dev_warn(&ihost->pdev->dev,
  441. "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
  442. __func__, idev,
  443. (dev_is_sata(task->dev) ? "STP/SATA"
  444. : ((dev_is_expander(task->dev))
  445. ? "SMP"
  446. : "SSP")),
  447. ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
  448. ? " IDEV_GONE"
  449. : "")
  450. : " <NULL>"),
  451. task, old_request);
  452. /* Device reset conditions signalled in task_state_flags are the
  453. * responsbility of libsas to observe at the start of the error
  454. * handler thread.
  455. */
  456. if (!idev || !old_request) {
  457. /* The request has already completed and there
  458. * is nothing to do here other than to set the task
  459. * done bit, and indicate that the task abort function
  460. * was successful.
  461. */
  462. spin_lock_irqsave(&task->task_state_lock, flags);
  463. task->task_state_flags |= SAS_TASK_STATE_DONE;
  464. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  465. SAS_TASK_STATE_PENDING);
  466. spin_unlock_irqrestore(&task->task_state_lock, flags);
  467. ret = TMF_RESP_FUNC_COMPLETE;
  468. dev_warn(&ihost->pdev->dev,
  469. "%s: abort task not needed for %p\n",
  470. __func__, task);
  471. goto out;
  472. }
  473. /* Suspend the RNC, kill the TC */
  474. if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
  475. != SCI_SUCCESS) {
  476. dev_warn(&ihost->pdev->dev,
  477. "%s: isci_remote_device_reset_terminate(dev=%p, "
  478. "req=%p, task=%p) failed\n",
  479. __func__, idev, old_request, task);
  480. ret = TMF_RESP_FUNC_FAILED;
  481. goto out;
  482. }
  483. spin_lock_irqsave(&ihost->scic_lock, flags);
  484. if (task->task_proto == SAS_PROTOCOL_SMP ||
  485. sas_protocol_ata(task->task_proto) ||
  486. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags) ||
  487. test_bit(IDEV_GONE, &idev->flags)) {
  488. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  489. /* No task to send, so explicitly resume the device here */
  490. isci_remote_device_resume_from_abort(ihost, idev);
  491. dev_warn(&ihost->pdev->dev,
  492. "%s: %s request"
  493. " or complete_in_target (%d), "
  494. "or IDEV_GONE (%d), thus no TMF\n",
  495. __func__,
  496. ((task->task_proto == SAS_PROTOCOL_SMP)
  497. ? "SMP"
  498. : (sas_protocol_ata(task->task_proto)
  499. ? "SATA/STP"
  500. : "<other>")
  501. ),
  502. test_bit(IREQ_COMPLETE_IN_TARGET,
  503. &old_request->flags),
  504. test_bit(IDEV_GONE, &idev->flags));
  505. spin_lock_irqsave(&task->task_state_lock, flags);
  506. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  507. SAS_TASK_STATE_PENDING);
  508. task->task_state_flags |= SAS_TASK_STATE_DONE;
  509. spin_unlock_irqrestore(&task->task_state_lock, flags);
  510. ret = TMF_RESP_FUNC_COMPLETE;
  511. } else {
  512. /* Fill in the tmf stucture */
  513. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  514. old_request);
  515. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  516. /* Send the task management request. */
  517. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  518. ret = isci_task_execute_tmf(ihost, idev, &tmf,
  519. ISCI_ABORT_TASK_TIMEOUT_MS);
  520. }
  521. out:
  522. dev_warn(&ihost->pdev->dev,
  523. "%s: Done; dev = %p, task = %p , old_request == %p\n",
  524. __func__, idev, task, old_request);
  525. isci_put_device(idev);
  526. return ret;
  527. }
  528. /**
  529. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  530. * functions. This is one of the Task Management functoins called by libsas,
  531. * to abort all task for the given lun.
  532. * @d_device: This parameter specifies the domain device associated with this
  533. * request.
  534. * @lun: This parameter specifies the lun associated with this request.
  535. *
  536. * status, zero indicates success.
  537. */
  538. int isci_task_abort_task_set(
  539. struct domain_device *d_device,
  540. u8 *lun)
  541. {
  542. return TMF_RESP_FUNC_FAILED;
  543. }
  544. /**
  545. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  546. * functions. This is one of the Task Management functoins called by libsas.
  547. * @d_device: This parameter specifies the domain device associated with this
  548. * request.
  549. * @lun: This parameter specifies the lun associated with this request.
  550. *
  551. * status, zero indicates success.
  552. */
  553. int isci_task_clear_aca(
  554. struct domain_device *d_device,
  555. u8 *lun)
  556. {
  557. return TMF_RESP_FUNC_FAILED;
  558. }
  559. /**
  560. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  561. * functions. This is one of the Task Management functoins called by libsas.
  562. * @d_device: This parameter specifies the domain device associated with this
  563. * request.
  564. * @lun: This parameter specifies the lun associated with this request.
  565. *
  566. * status, zero indicates success.
  567. */
  568. int isci_task_clear_task_set(
  569. struct domain_device *d_device,
  570. u8 *lun)
  571. {
  572. return TMF_RESP_FUNC_FAILED;
  573. }
  574. /**
  575. * isci_task_query_task() - This function is implemented to cause libsas to
  576. * correctly escalate the failed abort to a LUN or target reset (this is
  577. * because sas_scsi_find_task libsas function does not correctly interpret
  578. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  579. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  580. * returned, libsas will turn this into a target reset
  581. * @task: This parameter specifies the sas task being queried.
  582. * @lun: This parameter specifies the lun associated with this request.
  583. *
  584. * status, zero indicates success.
  585. */
  586. int isci_task_query_task(
  587. struct sas_task *task)
  588. {
  589. /* See if there is a pending device reset for this device. */
  590. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  591. return TMF_RESP_FUNC_FAILED;
  592. else
  593. return TMF_RESP_FUNC_SUCC;
  594. }
  595. /*
  596. * isci_task_request_complete() - This function is called by the sci core when
  597. * an task request completes.
  598. * @ihost: This parameter specifies the ISCI host object
  599. * @ireq: This parameter is the completed isci_request object.
  600. * @completion_status: This parameter specifies the completion status from the
  601. * sci core.
  602. *
  603. * none.
  604. */
  605. void
  606. isci_task_request_complete(struct isci_host *ihost,
  607. struct isci_request *ireq,
  608. enum sci_task_status completion_status)
  609. {
  610. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  611. struct completion *tmf_complete = NULL;
  612. dev_dbg(&ihost->pdev->dev,
  613. "%s: request = %p, status=%d\n",
  614. __func__, ireq, completion_status);
  615. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  616. if (tmf) {
  617. tmf->status = completion_status;
  618. if (tmf->proto == SAS_PROTOCOL_SSP) {
  619. memcpy(&tmf->resp.resp_iu,
  620. &ireq->ssp.rsp,
  621. SSP_RESP_IU_MAX_SIZE);
  622. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  623. memcpy(&tmf->resp.d2h_fis,
  624. &ireq->stp.rsp,
  625. sizeof(struct dev_to_host_fis));
  626. }
  627. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  628. tmf_complete = tmf->complete;
  629. }
  630. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  631. /* set the 'terminated' flag handle to make sure it cannot be terminated
  632. * or completed again.
  633. */
  634. set_bit(IREQ_TERMINATED, &ireq->flags);
  635. if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
  636. wake_up_all(&ihost->eventq);
  637. if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
  638. isci_free_tag(ihost, ireq->io_tag);
  639. /* The task management part completes last. */
  640. if (tmf_complete)
  641. complete(tmf_complete);
  642. }
  643. static int isci_reset_device(struct isci_host *ihost,
  644. struct domain_device *dev,
  645. struct isci_remote_device *idev)
  646. {
  647. int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
  648. struct sas_phy *phy = sas_get_local_phy(dev);
  649. struct isci_port *iport = dev->port->lldd_port;
  650. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  651. /* Suspend the RNC, terminate all outstanding TCs. */
  652. if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
  653. != SCI_SUCCESS) {
  654. rc = TMF_RESP_FUNC_FAILED;
  655. goto out;
  656. }
  657. /* Note that since the termination for outstanding requests succeeded,
  658. * this function will return success. This is because the resets will
  659. * only fail if the device has been removed (ie. hotplug), and the
  660. * primary duty of this function is to cleanup tasks, so that is the
  661. * relevant status.
  662. */
  663. if (!test_bit(IDEV_GONE, &idev->flags)) {
  664. if (scsi_is_sas_phy_local(phy)) {
  665. struct isci_phy *iphy = &ihost->phys[phy->number];
  666. reset_stat = isci_port_perform_hard_reset(ihost, iport,
  667. iphy);
  668. } else
  669. reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
  670. }
  671. /* Explicitly resume the RNC here, since there was no task sent. */
  672. isci_remote_device_resume_from_abort(ihost, idev);
  673. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
  674. __func__, idev, reset_stat);
  675. out:
  676. sas_put_local_phy(phy);
  677. return rc;
  678. }
  679. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  680. {
  681. struct isci_host *ihost = dev_to_ihost(dev);
  682. struct isci_remote_device *idev;
  683. unsigned long flags;
  684. int ret;
  685. spin_lock_irqsave(&ihost->scic_lock, flags);
  686. idev = isci_get_device(dev->lldd_dev);
  687. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  688. if (!idev) {
  689. /* XXX: need to cleanup any ireqs targeting this
  690. * domain_device
  691. */
  692. ret = TMF_RESP_FUNC_COMPLETE;
  693. goto out;
  694. }
  695. ret = isci_reset_device(ihost, dev, idev);
  696. out:
  697. isci_put_device(idev);
  698. return ret;
  699. }