task.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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;
  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 (dev_is_sata(dev)) {
  389. sas_ata_schedule_reset(dev);
  390. ret = TMF_RESP_FUNC_COMPLETE;
  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, task = %p, old_request == %p\n",
  442. __func__, idev, task, old_request);
  443. /* Device reset conditions signalled in task_state_flags are the
  444. * responsbility of libsas to observe at the start of the error
  445. * handler thread.
  446. */
  447. if (!idev || !old_request) {
  448. /* The request has already completed and there
  449. * is nothing to do here other than to set the task
  450. * done bit, and indicate that the task abort function
  451. * was sucessful.
  452. */
  453. spin_lock_irqsave(&task->task_state_lock, flags);
  454. task->task_state_flags |= SAS_TASK_STATE_DONE;
  455. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  456. SAS_TASK_STATE_PENDING);
  457. spin_unlock_irqrestore(&task->task_state_lock, flags);
  458. ret = TMF_RESP_FUNC_COMPLETE;
  459. dev_warn(&ihost->pdev->dev,
  460. "%s: abort task not needed for %p\n",
  461. __func__, task);
  462. goto out;
  463. }
  464. /* Suspend the RNC, kill the TC */
  465. if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
  466. != SCI_SUCCESS) {
  467. dev_warn(&ihost->pdev->dev,
  468. "%s: isci_remote_device_reset_terminate(dev=%p, "
  469. "req=%p, task=%p) failed\n",
  470. __func__, idev, old_request, task);
  471. ret = TMF_RESP_FUNC_FAILED;
  472. goto out;
  473. }
  474. spin_lock_irqsave(&ihost->scic_lock, flags);
  475. if (task->task_proto == SAS_PROTOCOL_SMP ||
  476. sas_protocol_ata(task->task_proto) ||
  477. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  478. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  479. /* No task to send, so explicitly resume the device here */
  480. isci_remote_device_resume_from_abort(ihost, idev);
  481. dev_warn(&ihost->pdev->dev,
  482. "%s: %s request"
  483. " or complete_in_target (%d), thus no TMF\n",
  484. __func__,
  485. ((task->task_proto == SAS_PROTOCOL_SMP)
  486. ? "SMP"
  487. : (sas_protocol_ata(task->task_proto)
  488. ? "SATA/STP"
  489. : "<other>")
  490. ),
  491. test_bit(IREQ_COMPLETE_IN_TARGET,
  492. &old_request->flags));
  493. spin_lock_irqsave(&task->task_state_lock, flags);
  494. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  495. SAS_TASK_STATE_PENDING);
  496. task->task_state_flags |= SAS_TASK_STATE_DONE;
  497. spin_unlock_irqrestore(&task->task_state_lock, flags);
  498. ret = TMF_RESP_FUNC_COMPLETE;
  499. } else {
  500. /* Fill in the tmf stucture */
  501. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  502. old_request);
  503. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  504. /* Send the task management request. */
  505. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  506. ret = isci_task_execute_tmf(ihost, idev, &tmf,
  507. ISCI_ABORT_TASK_TIMEOUT_MS);
  508. }
  509. out:
  510. dev_warn(&ihost->pdev->dev,
  511. "%s: Done; dev = %p, task = %p , old_request == %p\n",
  512. __func__, idev, task, old_request);
  513. isci_put_device(idev);
  514. return ret;
  515. }
  516. /**
  517. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  518. * functions. This is one of the Task Management functoins called by libsas,
  519. * to abort all task for the given lun.
  520. * @d_device: This parameter specifies the domain device associated with this
  521. * request.
  522. * @lun: This parameter specifies the lun associated with this request.
  523. *
  524. * status, zero indicates success.
  525. */
  526. int isci_task_abort_task_set(
  527. struct domain_device *d_device,
  528. u8 *lun)
  529. {
  530. return TMF_RESP_FUNC_FAILED;
  531. }
  532. /**
  533. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  534. * functions. This is one of the Task Management functoins called by libsas.
  535. * @d_device: This parameter specifies the domain device associated with this
  536. * request.
  537. * @lun: This parameter specifies the lun associated with this request.
  538. *
  539. * status, zero indicates success.
  540. */
  541. int isci_task_clear_aca(
  542. struct domain_device *d_device,
  543. u8 *lun)
  544. {
  545. return TMF_RESP_FUNC_FAILED;
  546. }
  547. /**
  548. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  549. * functions. This is one of the Task Management functoins called by libsas.
  550. * @d_device: This parameter specifies the domain device associated with this
  551. * request.
  552. * @lun: This parameter specifies the lun associated with this request.
  553. *
  554. * status, zero indicates success.
  555. */
  556. int isci_task_clear_task_set(
  557. struct domain_device *d_device,
  558. u8 *lun)
  559. {
  560. return TMF_RESP_FUNC_FAILED;
  561. }
  562. /**
  563. * isci_task_query_task() - This function is implemented to cause libsas to
  564. * correctly escalate the failed abort to a LUN or target reset (this is
  565. * because sas_scsi_find_task libsas function does not correctly interpret
  566. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  567. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  568. * returned, libsas will turn this into a target reset
  569. * @task: This parameter specifies the sas task being queried.
  570. * @lun: This parameter specifies the lun associated with this request.
  571. *
  572. * status, zero indicates success.
  573. */
  574. int isci_task_query_task(
  575. struct sas_task *task)
  576. {
  577. /* See if there is a pending device reset for this device. */
  578. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  579. return TMF_RESP_FUNC_FAILED;
  580. else
  581. return TMF_RESP_FUNC_SUCC;
  582. }
  583. /*
  584. * isci_task_request_complete() - This function is called by the sci core when
  585. * an task request completes.
  586. * @ihost: This parameter specifies the ISCI host object
  587. * @ireq: This parameter is the completed isci_request object.
  588. * @completion_status: This parameter specifies the completion status from the
  589. * sci core.
  590. *
  591. * none.
  592. */
  593. void
  594. isci_task_request_complete(struct isci_host *ihost,
  595. struct isci_request *ireq,
  596. enum sci_task_status completion_status)
  597. {
  598. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  599. struct completion *tmf_complete = NULL;
  600. dev_dbg(&ihost->pdev->dev,
  601. "%s: request = %p, status=%d\n",
  602. __func__, ireq, completion_status);
  603. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  604. if (tmf) {
  605. tmf->status = completion_status;
  606. if (tmf->proto == SAS_PROTOCOL_SSP) {
  607. memcpy(&tmf->resp.resp_iu,
  608. &ireq->ssp.rsp,
  609. SSP_RESP_IU_MAX_SIZE);
  610. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  611. memcpy(&tmf->resp.d2h_fis,
  612. &ireq->stp.rsp,
  613. sizeof(struct dev_to_host_fis));
  614. }
  615. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  616. tmf_complete = tmf->complete;
  617. }
  618. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  619. /* set the 'terminated' flag handle to make sure it cannot be terminated
  620. * or completed again.
  621. */
  622. set_bit(IREQ_TERMINATED, &ireq->flags);
  623. isci_free_tag(ihost, ireq->io_tag);
  624. /* The task management part completes last. */
  625. if (tmf_complete)
  626. complete(tmf_complete);
  627. }
  628. static int isci_reset_device(struct isci_host *ihost,
  629. struct domain_device *dev,
  630. struct isci_remote_device *idev)
  631. {
  632. int rc = TMF_RESP_FUNC_COMPLETE, reset_stat;
  633. struct sas_phy *phy = sas_get_local_phy(dev);
  634. struct isci_port *iport = dev->port->lldd_port;
  635. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  636. /* Suspend the RNC, terminate all outstanding TCs. */
  637. if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
  638. != SCI_SUCCESS) {
  639. rc = TMF_RESP_FUNC_FAILED;
  640. goto out;
  641. }
  642. /* Note that since the termination for outstanding requests succeeded,
  643. * this function will return success. This is because the resets will
  644. * only fail if the device has been removed (ie. hotplug), and the
  645. * primary duty of this function is to cleanup tasks, so that is the
  646. * relevant status.
  647. */
  648. if (scsi_is_sas_phy_local(phy)) {
  649. struct isci_phy *iphy = &ihost->phys[phy->number];
  650. reset_stat = isci_port_perform_hard_reset(ihost, iport, iphy);
  651. } else
  652. reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
  653. /* Explicitly resume the RNC here, since there was no task sent. */
  654. isci_remote_device_resume_from_abort(ihost, idev);
  655. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
  656. __func__, idev, reset_stat);
  657. out:
  658. sas_put_local_phy(phy);
  659. return rc;
  660. }
  661. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  662. {
  663. struct isci_host *ihost = dev_to_ihost(dev);
  664. struct isci_remote_device *idev;
  665. unsigned long flags;
  666. int ret;
  667. spin_lock_irqsave(&ihost->scic_lock, flags);
  668. idev = isci_get_device(dev->lldd_dev);
  669. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  670. if (!idev) {
  671. /* XXX: need to cleanup any ireqs targeting this
  672. * domain_device
  673. */
  674. ret = TMF_RESP_FUNC_COMPLETE;
  675. goto out;
  676. }
  677. ret = isci_reset_device(ihost, dev, idev);
  678. out:
  679. isci_put_device(idev);
  680. return ret;
  681. }