task.c 24 KB

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