task.c 23 KB

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