task.c 23 KB

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