task.c 23 KB

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