task.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. enum isci_completion_selection disposition;
  79. disposition = isci_perform_normal_io_completion;
  80. disposition = isci_task_set_completion_status(task, response, status,
  81. disposition);
  82. /* Tasks aborted specifically by a call to the lldd_abort_task
  83. * function should not be completed to the host in the regular path.
  84. */
  85. switch (disposition) {
  86. case isci_perform_normal_io_completion:
  87. /* Normal notification (task_done) */
  88. dev_dbg(&ihost->pdev->dev,
  89. "%s: Normal - task = %p, response=%d, "
  90. "status=%d\n",
  91. __func__, task, response, status);
  92. task->lldd_task = NULL;
  93. isci_execpath_callback(ihost, task, task->task_done);
  94. break;
  95. case isci_perform_aborted_io_completion:
  96. /*
  97. * No notification because this request is already in the
  98. * abort path.
  99. */
  100. dev_dbg(&ihost->pdev->dev,
  101. "%s: Aborted - task = %p, response=%d, "
  102. "status=%d\n",
  103. __func__, task, response, status);
  104. break;
  105. case isci_perform_error_io_completion:
  106. /* Use sas_task_abort */
  107. dev_dbg(&ihost->pdev->dev,
  108. "%s: Error - task = %p, response=%d, "
  109. "status=%d\n",
  110. __func__, task, response, status);
  111. isci_execpath_callback(ihost, task, sas_task_abort);
  112. break;
  113. default:
  114. dev_dbg(&ihost->pdev->dev,
  115. "%s: isci task notification default case!",
  116. __func__);
  117. sas_task_abort(task);
  118. break;
  119. }
  120. }
  121. #define for_each_sas_task(num, task) \
  122. for (; num > 0; num--,\
  123. task = list_entry(task->list.next, struct sas_task, list))
  124. static inline int isci_device_io_ready(struct isci_remote_device *idev,
  125. struct sas_task *task)
  126. {
  127. return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
  128. (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
  129. isci_task_is_ncq_recovery(task))
  130. : 0;
  131. }
  132. /**
  133. * isci_task_execute_task() - This function is one of the SAS Domain Template
  134. * functions. This function is called by libsas to send a task down to
  135. * hardware.
  136. * @task: This parameter specifies the SAS task to send.
  137. * @num: This parameter specifies the number of tasks to queue.
  138. * @gfp_flags: This parameter specifies the context of this call.
  139. *
  140. * status, zero indicates success.
  141. */
  142. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  143. {
  144. struct isci_host *ihost = dev_to_ihost(task->dev);
  145. struct isci_remote_device *idev;
  146. unsigned long flags;
  147. bool io_ready;
  148. u16 tag;
  149. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  150. for_each_sas_task(num, task) {
  151. enum sci_status status = SCI_FAILURE;
  152. spin_lock_irqsave(&ihost->scic_lock, flags);
  153. idev = isci_lookup_device(task->dev);
  154. io_ready = isci_device_io_ready(idev, task);
  155. tag = isci_alloc_tag(ihost);
  156. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  157. dev_dbg(&ihost->pdev->dev,
  158. "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
  159. task, num, task->dev, idev, idev ? idev->flags : 0,
  160. task->uldd_task);
  161. if (!idev) {
  162. isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
  163. SAS_DEVICE_UNKNOWN);
  164. } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
  165. /* Indicate QUEUE_FULL so that the scsi midlayer
  166. * retries.
  167. */
  168. isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
  169. SAS_QUEUE_FULL);
  170. } else {
  171. /* There is a device and it's ready for I/O. */
  172. spin_lock_irqsave(&task->task_state_lock, flags);
  173. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  174. /* The I/O was aborted. */
  175. spin_unlock_irqrestore(&task->task_state_lock,
  176. flags);
  177. isci_task_refuse(ihost, task,
  178. SAS_TASK_UNDELIVERED,
  179. SAM_STAT_TASK_ABORTED);
  180. } else {
  181. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  182. spin_unlock_irqrestore(&task->task_state_lock, flags);
  183. /* build and send the request. */
  184. status = isci_request_execute(ihost, idev, task, tag);
  185. if (status != SCI_SUCCESS) {
  186. spin_lock_irqsave(&task->task_state_lock, flags);
  187. /* Did not really start this command. */
  188. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  189. spin_unlock_irqrestore(&task->task_state_lock, flags);
  190. if (test_bit(IDEV_GONE, &idev->flags)) {
  191. /* Indicate that the device
  192. * is gone.
  193. */
  194. isci_task_refuse(ihost, task,
  195. SAS_TASK_UNDELIVERED,
  196. SAS_DEVICE_UNKNOWN);
  197. } else {
  198. /* Indicate QUEUE_FULL so that
  199. * the scsi midlayer retries.
  200. * If the request failed for
  201. * remote device reasons, it
  202. * gets returned as
  203. * SAS_TASK_UNDELIVERED next
  204. * time through.
  205. */
  206. isci_task_refuse(ihost, task,
  207. SAS_TASK_COMPLETE,
  208. SAS_QUEUE_FULL);
  209. }
  210. }
  211. }
  212. }
  213. if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
  214. spin_lock_irqsave(&ihost->scic_lock, flags);
  215. /* command never hit the device, so just free
  216. * the tci and skip the sequence increment
  217. */
  218. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  219. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  220. }
  221. isci_put_device(idev);
  222. }
  223. return 0;
  224. }
  225. static enum sci_status isci_sata_management_task_request_build(struct isci_request *ireq)
  226. {
  227. struct isci_tmf *isci_tmf;
  228. enum sci_status status;
  229. if (!test_bit(IREQ_TMF, &ireq->flags))
  230. return SCI_FAILURE;
  231. isci_tmf = isci_request_access_tmf(ireq);
  232. switch (isci_tmf->tmf_code) {
  233. case isci_tmf_sata_srst_high:
  234. case isci_tmf_sata_srst_low: {
  235. struct host_to_dev_fis *fis = &ireq->stp.cmd;
  236. memset(fis, 0, sizeof(*fis));
  237. fis->fis_type = 0x27;
  238. fis->flags &= ~0x80;
  239. fis->flags &= 0xF0;
  240. if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
  241. fis->control |= ATA_SRST;
  242. else
  243. fis->control &= ~ATA_SRST;
  244. break;
  245. }
  246. /* other management commnd go here... */
  247. default:
  248. return SCI_FAILURE;
  249. }
  250. /* core builds the protocol specific request
  251. * based on the h2d fis.
  252. */
  253. status = sci_task_request_construct_sata(ireq);
  254. return status;
  255. }
  256. static struct isci_request *isci_task_request_build(struct isci_host *ihost,
  257. struct isci_remote_device *idev,
  258. u16 tag, struct isci_tmf *isci_tmf)
  259. {
  260. enum sci_status status = SCI_FAILURE;
  261. struct isci_request *ireq = NULL;
  262. struct domain_device *dev;
  263. dev_dbg(&ihost->pdev->dev,
  264. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  265. dev = idev->domain_dev;
  266. /* do common allocation and init of request object. */
  267. ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
  268. if (!ireq)
  269. return NULL;
  270. /* let the core do it's construct. */
  271. status = sci_task_request_construct(ihost, idev, tag,
  272. ireq);
  273. if (status != SCI_SUCCESS) {
  274. dev_warn(&ihost->pdev->dev,
  275. "%s: sci_task_request_construct failed - "
  276. "status = 0x%x\n",
  277. __func__,
  278. status);
  279. return NULL;
  280. }
  281. /* XXX convert to get this from task->tproto like other drivers */
  282. if (dev->dev_type == SAS_END_DEV) {
  283. isci_tmf->proto = SAS_PROTOCOL_SSP;
  284. status = sci_task_request_construct_ssp(ireq);
  285. if (status != SCI_SUCCESS)
  286. return NULL;
  287. }
  288. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  289. isci_tmf->proto = SAS_PROTOCOL_SATA;
  290. status = isci_sata_management_task_request_build(ireq);
  291. if (status != SCI_SUCCESS)
  292. return NULL;
  293. }
  294. return ireq;
  295. }
  296. /**
  297. * isci_request_mark_zombie() - This function must be called with scic_lock held.
  298. */
  299. static void isci_request_mark_zombie(struct isci_host *ihost, struct isci_request *ireq)
  300. {
  301. struct completion *tmf_completion = NULL;
  302. struct completion *req_completion;
  303. /* Set the request state to "dead". */
  304. ireq->status = dead;
  305. req_completion = ireq->io_request_completion;
  306. ireq->io_request_completion = NULL;
  307. if (test_bit(IREQ_TMF, &ireq->flags)) {
  308. /* Break links with the TMF request. */
  309. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  310. /* In the case where a task request is dying,
  311. * the thread waiting on the complete will sit and
  312. * timeout unless we wake it now. Since the TMF
  313. * has a default error status, complete it here
  314. * to wake the waiting thread.
  315. */
  316. if (tmf) {
  317. tmf_completion = tmf->complete;
  318. tmf->complete = NULL;
  319. }
  320. ireq->ttype_ptr.tmf_task_ptr = NULL;
  321. dev_dbg(&ihost->pdev->dev, "%s: tmf_code %d, managed tag %#x\n",
  322. __func__, tmf->tmf_code, tmf->io_tag);
  323. } else {
  324. /* Break links with the sas_task - the callback is done
  325. * elsewhere.
  326. */
  327. struct sas_task *task = isci_request_access_task(ireq);
  328. if (task)
  329. task->lldd_task = NULL;
  330. ireq->ttype_ptr.io_task_ptr = NULL;
  331. }
  332. dev_warn(&ihost->pdev->dev, "task context unrecoverable (tag: %#x)\n",
  333. ireq->io_tag);
  334. /* Don't force waiting threads to timeout. */
  335. if (req_completion)
  336. complete(req_completion);
  337. if (tmf_completion != NULL)
  338. complete(tmf_completion);
  339. }
  340. static int isci_task_execute_tmf(struct isci_host *ihost,
  341. struct isci_remote_device *idev,
  342. struct isci_tmf *tmf, unsigned long timeout_ms)
  343. {
  344. DECLARE_COMPLETION_ONSTACK(completion);
  345. enum sci_task_status status = SCI_TASK_FAILURE;
  346. struct isci_request *ireq;
  347. int ret = TMF_RESP_FUNC_FAILED;
  348. unsigned long flags;
  349. unsigned long timeleft;
  350. u16 tag;
  351. spin_lock_irqsave(&ihost->scic_lock, flags);
  352. tag = isci_alloc_tag(ihost);
  353. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  354. if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
  355. return ret;
  356. /* sanity check, return TMF_RESP_FUNC_FAILED
  357. * if the device is not there and ready.
  358. */
  359. if (!idev ||
  360. (!test_bit(IDEV_IO_READY, &idev->flags) &&
  361. !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
  362. dev_dbg(&ihost->pdev->dev,
  363. "%s: idev = %p not ready (%#lx)\n",
  364. __func__,
  365. idev, idev ? idev->flags : 0);
  366. goto err_tci;
  367. } else
  368. dev_dbg(&ihost->pdev->dev,
  369. "%s: idev = %p\n",
  370. __func__, idev);
  371. /* Assign the pointer to the TMF's completion kernel wait structure. */
  372. tmf->complete = &completion;
  373. tmf->status = SCI_FAILURE_TIMEOUT;
  374. ireq = isci_task_request_build(ihost, idev, tag, tmf);
  375. if (!ireq)
  376. goto err_tci;
  377. spin_lock_irqsave(&ihost->scic_lock, flags);
  378. /* start the TMF io. */
  379. status = sci_controller_start_task(ihost, idev, ireq);
  380. if (status != SCI_TASK_SUCCESS) {
  381. dev_dbg(&ihost->pdev->dev,
  382. "%s: start_io failed - status = 0x%x, request = %p\n",
  383. __func__,
  384. status,
  385. ireq);
  386. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  387. goto err_tci;
  388. }
  389. if (tmf->cb_state_func != NULL)
  390. tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
  391. isci_request_change_state(ireq, started);
  392. /* add the request to the remote device request list. */
  393. list_add(&ireq->dev_node, &idev->reqs_in_process);
  394. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  395. /* Wait for the TMF to complete, or a timeout. */
  396. timeleft = wait_for_completion_timeout(&completion,
  397. msecs_to_jiffies(timeout_ms));
  398. if (timeleft == 0) {
  399. /* The TMF did not complete - this could be because
  400. * of an unplug. Terminate the TMF request now.
  401. */
  402. spin_lock_irqsave(&ihost->scic_lock, flags);
  403. if (tmf->cb_state_func != NULL)
  404. tmf->cb_state_func(isci_tmf_timed_out, tmf,
  405. tmf->cb_data);
  406. sci_controller_terminate_request(ihost, idev, ireq);
  407. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  408. timeleft = wait_for_completion_timeout(
  409. &completion,
  410. msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
  411. if (!timeleft) {
  412. /* Strange condition - the termination of the TMF
  413. * request timed-out.
  414. */
  415. spin_lock_irqsave(&ihost->scic_lock, flags);
  416. /* If the TMF status has not changed, kill it. */
  417. if (tmf->status == SCI_FAILURE_TIMEOUT)
  418. isci_request_mark_zombie(ihost, ireq);
  419. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  420. }
  421. }
  422. isci_print_tmf(tmf);
  423. if (tmf->status == SCI_SUCCESS)
  424. ret = TMF_RESP_FUNC_COMPLETE;
  425. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  426. dev_dbg(&ihost->pdev->dev,
  427. "%s: tmf.status == "
  428. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  429. __func__);
  430. ret = TMF_RESP_FUNC_COMPLETE;
  431. }
  432. /* Else - leave the default "failed" status alone. */
  433. dev_dbg(&ihost->pdev->dev,
  434. "%s: completed request = %p\n",
  435. __func__,
  436. ireq);
  437. return ret;
  438. err_tci:
  439. spin_lock_irqsave(&ihost->scic_lock, flags);
  440. isci_tci_free(ihost, ISCI_TAG_TCI(tag));
  441. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  442. return ret;
  443. }
  444. static void isci_task_build_tmf(struct isci_tmf *tmf,
  445. enum isci_tmf_function_codes code,
  446. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  447. struct isci_tmf *,
  448. void *),
  449. void *cb_data)
  450. {
  451. memset(tmf, 0, sizeof(*tmf));
  452. tmf->tmf_code = code;
  453. tmf->cb_state_func = tmf_sent_cb;
  454. tmf->cb_data = cb_data;
  455. }
  456. static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
  457. enum isci_tmf_function_codes code,
  458. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  459. struct isci_tmf *,
  460. void *),
  461. struct isci_request *old_request)
  462. {
  463. isci_task_build_tmf(tmf, code, tmf_sent_cb, old_request);
  464. tmf->io_tag = old_request->io_tag;
  465. }
  466. /**
  467. * isci_task_validate_request_to_abort() - This function checks the given I/O
  468. * against the "started" state. If the request is still "started", it's
  469. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  470. * BEFORE CALLING THIS FUNCTION.
  471. * @isci_request: This parameter specifies the request object to control.
  472. * @isci_host: This parameter specifies the ISCI host object
  473. * @isci_device: This is the device to which the request is pending.
  474. * @aborted_io_completion: This is a completion structure that will be added to
  475. * the request in case it is changed to aborting; this completion is
  476. * triggered when the request is fully completed.
  477. *
  478. * Either "started" on successful change of the task status to "aborted", or
  479. * "unallocated" if the task cannot be controlled.
  480. */
  481. static enum isci_request_status isci_task_validate_request_to_abort(
  482. struct isci_request *isci_request,
  483. struct isci_host *isci_host,
  484. struct isci_remote_device *isci_device,
  485. struct completion *aborted_io_completion)
  486. {
  487. enum isci_request_status old_state = unallocated;
  488. /* Only abort the task if it's in the
  489. * device's request_in_process list
  490. */
  491. if (isci_request && !list_empty(&isci_request->dev_node)) {
  492. old_state = isci_request_change_started_to_aborted(
  493. isci_request, aborted_io_completion);
  494. }
  495. return old_state;
  496. }
  497. static int isci_request_is_dealloc_managed(enum isci_request_status stat)
  498. {
  499. switch (stat) {
  500. case aborted:
  501. case aborting:
  502. case terminating:
  503. case completed:
  504. case dead:
  505. return true;
  506. default:
  507. return false;
  508. }
  509. }
  510. /**
  511. * isci_terminate_request_core() - This function will terminate the given
  512. * request, and wait for it to complete. This function must only be called
  513. * from a thread that can wait. Note that the request is terminated and
  514. * completed (back to the host, if started there).
  515. * @ihost: This SCU.
  516. * @idev: The target.
  517. * @isci_request: The I/O request to be terminated.
  518. *
  519. */
  520. static void isci_terminate_request_core(struct isci_host *ihost,
  521. struct isci_remote_device *idev,
  522. struct isci_request *isci_request)
  523. {
  524. enum sci_status status = SCI_SUCCESS;
  525. bool was_terminated = false;
  526. bool needs_cleanup_handling = false;
  527. unsigned long flags;
  528. unsigned long termination_completed = 1;
  529. struct completion *io_request_completion;
  530. dev_dbg(&ihost->pdev->dev,
  531. "%s: device = %p; request = %p\n",
  532. __func__, idev, isci_request);
  533. spin_lock_irqsave(&ihost->scic_lock, flags);
  534. io_request_completion = isci_request->io_request_completion;
  535. /* Note that we are not going to control
  536. * the target to abort the request.
  537. */
  538. set_bit(IREQ_COMPLETE_IN_TARGET, &isci_request->flags);
  539. /* Make sure the request wasn't just sitting around signalling
  540. * device condition (if the request handle is NULL, then the
  541. * request completed but needed additional handling here).
  542. */
  543. if (!test_bit(IREQ_TERMINATED, &isci_request->flags)) {
  544. was_terminated = true;
  545. needs_cleanup_handling = true;
  546. status = sci_controller_terminate_request(ihost,
  547. idev,
  548. isci_request);
  549. }
  550. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  551. /*
  552. * The only time the request to terminate will
  553. * fail is when the io request is completed and
  554. * being aborted.
  555. */
  556. if (status != SCI_SUCCESS) {
  557. dev_dbg(&ihost->pdev->dev,
  558. "%s: sci_controller_terminate_request"
  559. " returned = 0x%x\n",
  560. __func__, status);
  561. isci_request->io_request_completion = NULL;
  562. } else {
  563. if (was_terminated) {
  564. dev_dbg(&ihost->pdev->dev,
  565. "%s: before completion wait (%p/%p)\n",
  566. __func__, isci_request, io_request_completion);
  567. /* Wait here for the request to complete. */
  568. termination_completed
  569. = wait_for_completion_timeout(
  570. io_request_completion,
  571. msecs_to_jiffies(ISCI_TERMINATION_TIMEOUT_MSEC));
  572. if (!termination_completed) {
  573. /* The request to terminate has timed out. */
  574. spin_lock_irqsave(&ihost->scic_lock, flags);
  575. /* Check for state changes. */
  576. if (!test_bit(IREQ_TERMINATED,
  577. &isci_request->flags)) {
  578. /* The best we can do is to have the
  579. * request die a silent death if it
  580. * ever really completes.
  581. */
  582. isci_request_mark_zombie(ihost,
  583. isci_request);
  584. needs_cleanup_handling = true;
  585. } else
  586. termination_completed = 1;
  587. spin_unlock_irqrestore(&ihost->scic_lock,
  588. flags);
  589. if (!termination_completed) {
  590. dev_dbg(&ihost->pdev->dev,
  591. "%s: *** Timeout waiting for "
  592. "termination(%p/%p)\n",
  593. __func__, io_request_completion,
  594. isci_request);
  595. /* The request can no longer be referenced
  596. * safely since it may go away if the
  597. * termination every really does complete.
  598. */
  599. isci_request = NULL;
  600. }
  601. }
  602. if (termination_completed)
  603. dev_dbg(&ihost->pdev->dev,
  604. "%s: after completion wait (%p/%p)\n",
  605. __func__, isci_request, io_request_completion);
  606. }
  607. if (termination_completed) {
  608. isci_request->io_request_completion = NULL;
  609. /* Peek at the status of the request. This will tell
  610. * us if there was special handling on the request such that it
  611. * needs to be detached and freed here.
  612. */
  613. spin_lock_irqsave(&isci_request->state_lock, flags);
  614. needs_cleanup_handling
  615. = isci_request_is_dealloc_managed(
  616. isci_request->status);
  617. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  618. }
  619. if (needs_cleanup_handling) {
  620. dev_dbg(&ihost->pdev->dev,
  621. "%s: cleanup isci_device=%p, request=%p\n",
  622. __func__, idev, isci_request);
  623. if (isci_request != NULL) {
  624. spin_lock_irqsave(&ihost->scic_lock, flags);
  625. isci_free_tag(ihost, isci_request->io_tag);
  626. isci_request_change_state(isci_request, unallocated);
  627. list_del_init(&isci_request->dev_node);
  628. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  629. }
  630. }
  631. }
  632. }
  633. /**
  634. * isci_terminate_pending_requests() - This function will change the all of the
  635. * requests on the given device's state to "aborting", will terminate the
  636. * requests, and wait for them to complete. This function must only be
  637. * called from a thread that can wait. Note that the requests are all
  638. * terminated and completed (back to the host, if started there).
  639. * @isci_host: This parameter specifies SCU.
  640. * @idev: This parameter specifies the target.
  641. *
  642. */
  643. void isci_terminate_pending_requests(struct isci_host *ihost,
  644. struct isci_remote_device *idev)
  645. {
  646. struct completion request_completion;
  647. enum isci_request_status old_state;
  648. unsigned long flags;
  649. LIST_HEAD(list);
  650. spin_lock_irqsave(&ihost->scic_lock, flags);
  651. list_splice_init(&idev->reqs_in_process, &list);
  652. /* assumes that isci_terminate_request_core deletes from the list */
  653. while (!list_empty(&list)) {
  654. struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
  655. /* Change state to "terminating" if it is currently
  656. * "started".
  657. */
  658. old_state = isci_request_change_started_to_newstate(ireq,
  659. &request_completion,
  660. terminating);
  661. switch (old_state) {
  662. case started:
  663. case completed:
  664. case aborting:
  665. break;
  666. default:
  667. /* termination in progress, or otherwise dispositioned.
  668. * We know the request was on 'list' so should be safe
  669. * to move it back to reqs_in_process
  670. */
  671. list_move(&ireq->dev_node, &idev->reqs_in_process);
  672. ireq = NULL;
  673. break;
  674. }
  675. if (!ireq)
  676. continue;
  677. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  678. init_completion(&request_completion);
  679. dev_dbg(&ihost->pdev->dev,
  680. "%s: idev=%p request=%p; task=%p old_state=%d\n",
  681. __func__, idev, ireq,
  682. (!test_bit(IREQ_TMF, &ireq->flags)
  683. ? isci_request_access_task(ireq)
  684. : NULL),
  685. old_state);
  686. /* If the old_state is started:
  687. * This request was not already being aborted. If it had been,
  688. * then the aborting I/O (ie. the TMF request) would not be in
  689. * the aborting state, and thus would be terminated here. Note
  690. * that since the TMF completion's call to the kernel function
  691. * "complete()" does not happen until the pending I/O request
  692. * terminate fully completes, we do not have to implement a
  693. * special wait here for already aborting requests - the
  694. * termination of the TMF request will force the request
  695. * to finish it's already started terminate.
  696. *
  697. * If old_state == completed:
  698. * This request completed from the SCU hardware perspective
  699. * and now just needs cleaning up in terms of freeing the
  700. * request and potentially calling up to libsas.
  701. *
  702. * If old_state == aborting:
  703. * This request has already gone through a TMF timeout, but may
  704. * not have been terminated; needs cleaning up at least.
  705. */
  706. isci_terminate_request_core(ihost, idev, ireq);
  707. spin_lock_irqsave(&ihost->scic_lock, flags);
  708. }
  709. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  710. }
  711. /**
  712. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  713. * Template functions.
  714. * @lun: This parameter specifies the lun to be reset.
  715. *
  716. * status, zero indicates success.
  717. */
  718. static int isci_task_send_lu_reset_sas(
  719. struct isci_host *isci_host,
  720. struct isci_remote_device *isci_device,
  721. u8 *lun)
  722. {
  723. struct isci_tmf tmf;
  724. int ret = TMF_RESP_FUNC_FAILED;
  725. dev_dbg(&isci_host->pdev->dev,
  726. "%s: isci_host = %p, isci_device = %p\n",
  727. __func__, isci_host, isci_device);
  728. /* Send the LUN reset to the target. By the time the call returns,
  729. * the TMF has fully exected in the target (in which case the return
  730. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  731. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  732. */
  733. isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset, NULL, NULL);
  734. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  735. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  736. if (ret == TMF_RESP_FUNC_COMPLETE)
  737. dev_dbg(&isci_host->pdev->dev,
  738. "%s: %p: TMF_LU_RESET passed\n",
  739. __func__, isci_device);
  740. else
  741. dev_dbg(&isci_host->pdev->dev,
  742. "%s: %p: TMF_LU_RESET failed (%x)\n",
  743. __func__, isci_device, ret);
  744. return ret;
  745. }
  746. static int isci_task_send_lu_reset_sata(struct isci_host *ihost,
  747. struct isci_remote_device *idev, u8 *lun)
  748. {
  749. int ret = TMF_RESP_FUNC_FAILED;
  750. struct isci_tmf tmf;
  751. /* Send the soft reset to the target */
  752. #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
  753. isci_task_build_tmf(&tmf, isci_tmf_sata_srst_high, NULL, NULL);
  754. ret = isci_task_execute_tmf(ihost, idev, &tmf, ISCI_SRST_TIMEOUT_MS);
  755. if (ret != TMF_RESP_FUNC_COMPLETE) {
  756. dev_dbg(&ihost->pdev->dev,
  757. "%s: Assert SRST failed (%p) = %x",
  758. __func__, idev, ret);
  759. /* Return the failure so that the LUN reset is escalated
  760. * to a target reset.
  761. */
  762. }
  763. return ret;
  764. }
  765. /**
  766. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  767. * functions. This is one of the Task Management functoins called by libsas,
  768. * to reset the given lun. Note the assumption that while this call is
  769. * executing, no I/O will be sent by the host to the device.
  770. * @lun: This parameter specifies the lun to be reset.
  771. *
  772. * status, zero indicates success.
  773. */
  774. int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
  775. {
  776. struct isci_host *isci_host = dev_to_ihost(domain_device);
  777. struct isci_remote_device *isci_device;
  778. unsigned long flags;
  779. int ret;
  780. spin_lock_irqsave(&isci_host->scic_lock, flags);
  781. isci_device = isci_lookup_device(domain_device);
  782. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  783. dev_dbg(&isci_host->pdev->dev,
  784. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  785. __func__, domain_device, isci_host, isci_device);
  786. if (!isci_device) {
  787. /* If the device is gone, stop the escalations. */
  788. dev_dbg(&isci_host->pdev->dev, "%s: No dev\n", __func__);
  789. ret = TMF_RESP_FUNC_COMPLETE;
  790. goto out;
  791. }
  792. set_bit(IDEV_EH, &isci_device->flags);
  793. /* Send the task management part of the reset. */
  794. if (sas_protocol_ata(domain_device->tproto)) {
  795. ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
  796. } else
  797. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  798. /* If the LUN reset worked, all the I/O can now be terminated. */
  799. if (ret == TMF_RESP_FUNC_COMPLETE)
  800. /* Terminate all I/O now. */
  801. isci_terminate_pending_requests(isci_host,
  802. isci_device);
  803. out:
  804. isci_put_device(isci_device);
  805. return ret;
  806. }
  807. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  808. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  809. {
  810. return TMF_RESP_FUNC_FAILED;
  811. }
  812. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  813. {
  814. return TMF_RESP_FUNC_FAILED;
  815. }
  816. /* Task Management Functions. Must be called from process context. */
  817. /**
  818. * isci_abort_task_process_cb() - This is a helper function for the abort task
  819. * TMF command. It manages the request state with respect to the successful
  820. * transmission / completion of the abort task request.
  821. * @cb_state: This parameter specifies when this function was called - after
  822. * the TMF request has been started and after it has timed-out.
  823. * @tmf: This parameter specifies the TMF in progress.
  824. *
  825. *
  826. */
  827. static void isci_abort_task_process_cb(
  828. enum isci_tmf_cb_state cb_state,
  829. struct isci_tmf *tmf,
  830. void *cb_data)
  831. {
  832. struct isci_request *old_request;
  833. old_request = (struct isci_request *)cb_data;
  834. dev_dbg(&old_request->isci_host->pdev->dev,
  835. "%s: tmf=%p, old_request=%p\n",
  836. __func__, tmf, old_request);
  837. switch (cb_state) {
  838. case isci_tmf_started:
  839. /* The TMF has been started. Nothing to do here, since the
  840. * request state was already set to "aborted" by the abort
  841. * task function.
  842. */
  843. if ((old_request->status != aborted)
  844. && (old_request->status != completed))
  845. dev_dbg(&old_request->isci_host->pdev->dev,
  846. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  847. __func__, old_request->status, tmf, old_request);
  848. break;
  849. case isci_tmf_timed_out:
  850. /* Set the task's state to "aborting", since the abort task
  851. * function thread set it to "aborted" (above) in anticipation
  852. * of the task management request working correctly. Since the
  853. * timeout has now fired, the TMF request failed. We set the
  854. * state such that the request completion will indicate the
  855. * device is no longer present.
  856. */
  857. isci_request_change_state(old_request, aborting);
  858. break;
  859. default:
  860. dev_dbg(&old_request->isci_host->pdev->dev,
  861. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  862. __func__, cb_state, tmf, old_request);
  863. break;
  864. }
  865. }
  866. /**
  867. * isci_task_abort_task() - This function is one of the SAS Domain Template
  868. * functions. This function is called by libsas to abort a specified task.
  869. * @task: This parameter specifies the SAS task to abort.
  870. *
  871. * status, zero indicates success.
  872. */
  873. int isci_task_abort_task(struct sas_task *task)
  874. {
  875. struct isci_host *isci_host = dev_to_ihost(task->dev);
  876. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  877. struct isci_request *old_request = NULL;
  878. enum isci_request_status old_state;
  879. struct isci_remote_device *isci_device = NULL;
  880. struct isci_tmf tmf;
  881. int ret = TMF_RESP_FUNC_FAILED;
  882. unsigned long flags;
  883. int perform_termination = 0;
  884. /* Get the isci_request reference from the task. Note that
  885. * this check does not depend on the pending request list
  886. * in the device, because tasks driving resets may land here
  887. * after completion in the core.
  888. */
  889. spin_lock_irqsave(&isci_host->scic_lock, flags);
  890. spin_lock(&task->task_state_lock);
  891. old_request = task->lldd_task;
  892. /* If task is already done, the request isn't valid */
  893. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  894. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  895. old_request)
  896. isci_device = isci_lookup_device(task->dev);
  897. spin_unlock(&task->task_state_lock);
  898. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  899. dev_dbg(&isci_host->pdev->dev,
  900. "%s: dev = %p, task = %p, old_request == %p\n",
  901. __func__, isci_device, task, old_request);
  902. if (isci_device)
  903. set_bit(IDEV_EH, &isci_device->flags);
  904. /* Device reset conditions signalled in task_state_flags are the
  905. * responsbility of libsas to observe at the start of the error
  906. * handler thread.
  907. */
  908. if (!isci_device || !old_request) {
  909. /* The request has already completed and there
  910. * is nothing to do here other than to set the task
  911. * done bit, and indicate that the task abort function
  912. * was sucessful.
  913. */
  914. spin_lock_irqsave(&task->task_state_lock, flags);
  915. task->task_state_flags |= SAS_TASK_STATE_DONE;
  916. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  917. SAS_TASK_STATE_PENDING);
  918. spin_unlock_irqrestore(&task->task_state_lock, flags);
  919. ret = TMF_RESP_FUNC_COMPLETE;
  920. dev_dbg(&isci_host->pdev->dev,
  921. "%s: abort task not needed for %p\n",
  922. __func__, task);
  923. goto out;
  924. }
  925. spin_lock_irqsave(&isci_host->scic_lock, flags);
  926. /* Check the request status and change to "aborted" if currently
  927. * "starting"; if true then set the I/O kernel completion
  928. * struct that will be triggered when the request completes.
  929. */
  930. old_state = isci_task_validate_request_to_abort(
  931. old_request, isci_host, isci_device,
  932. &aborted_io_completion);
  933. if ((old_state != started) &&
  934. (old_state != completed) &&
  935. (old_state != aborting)) {
  936. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  937. /* The request was already being handled by someone else (because
  938. * they got to set the state away from started).
  939. */
  940. dev_dbg(&isci_host->pdev->dev,
  941. "%s: device = %p; old_request %p already being aborted\n",
  942. __func__,
  943. isci_device, old_request);
  944. ret = TMF_RESP_FUNC_COMPLETE;
  945. goto out;
  946. }
  947. if (task->task_proto == SAS_PROTOCOL_SMP ||
  948. sas_protocol_ata(task->task_proto) ||
  949. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  950. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  951. dev_dbg(&isci_host->pdev->dev,
  952. "%s: %s request"
  953. " or complete_in_target (%d), thus no TMF\n",
  954. __func__,
  955. ((task->task_proto == SAS_PROTOCOL_SMP)
  956. ? "SMP"
  957. : (sas_protocol_ata(task->task_proto)
  958. ? "SATA/STP"
  959. : "<other>")
  960. ),
  961. test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags));
  962. if (test_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags)) {
  963. spin_lock_irqsave(&task->task_state_lock, flags);
  964. task->task_state_flags |= SAS_TASK_STATE_DONE;
  965. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  966. SAS_TASK_STATE_PENDING);
  967. spin_unlock_irqrestore(&task->task_state_lock, flags);
  968. ret = TMF_RESP_FUNC_COMPLETE;
  969. } else {
  970. spin_lock_irqsave(&task->task_state_lock, flags);
  971. task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
  972. SAS_TASK_STATE_PENDING);
  973. spin_unlock_irqrestore(&task->task_state_lock, flags);
  974. }
  975. /* STP and SMP devices are not sent a TMF, but the
  976. * outstanding I/O request is terminated below. This is
  977. * because SATA/STP and SMP discovery path timeouts directly
  978. * call the abort task interface for cleanup.
  979. */
  980. perform_termination = 1;
  981. } else {
  982. /* Fill in the tmf stucture */
  983. isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
  984. isci_abort_task_process_cb,
  985. old_request);
  986. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  987. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* 1/2 second timeout */
  988. ret = isci_task_execute_tmf(isci_host, isci_device, &tmf,
  989. ISCI_ABORT_TASK_TIMEOUT_MS);
  990. if (ret == TMF_RESP_FUNC_COMPLETE)
  991. perform_termination = 1;
  992. else
  993. dev_dbg(&isci_host->pdev->dev,
  994. "%s: isci_task_send_tmf failed\n", __func__);
  995. }
  996. if (perform_termination) {
  997. set_bit(IREQ_COMPLETE_IN_TARGET, &old_request->flags);
  998. /* Clean up the request on our side, and wait for the aborted
  999. * I/O to complete.
  1000. */
  1001. isci_terminate_request_core(isci_host, isci_device,
  1002. old_request);
  1003. }
  1004. /* Make sure we do not leave a reference to aborted_io_completion */
  1005. old_request->io_request_completion = NULL;
  1006. out:
  1007. isci_put_device(isci_device);
  1008. return ret;
  1009. }
  1010. /**
  1011. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1012. * functions. This is one of the Task Management functoins called by libsas,
  1013. * to abort all task for the given lun.
  1014. * @d_device: This parameter specifies the domain device associated with this
  1015. * request.
  1016. * @lun: This parameter specifies the lun associated with this request.
  1017. *
  1018. * status, zero indicates success.
  1019. */
  1020. int isci_task_abort_task_set(
  1021. struct domain_device *d_device,
  1022. u8 *lun)
  1023. {
  1024. return TMF_RESP_FUNC_FAILED;
  1025. }
  1026. /**
  1027. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1028. * functions. This is one of the Task Management functoins called by libsas.
  1029. * @d_device: This parameter specifies the domain device associated with this
  1030. * request.
  1031. * @lun: This parameter specifies the lun associated with this request.
  1032. *
  1033. * status, zero indicates success.
  1034. */
  1035. int isci_task_clear_aca(
  1036. struct domain_device *d_device,
  1037. u8 *lun)
  1038. {
  1039. return TMF_RESP_FUNC_FAILED;
  1040. }
  1041. /**
  1042. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1043. * functions. This is one of the Task Management functoins called by libsas.
  1044. * @d_device: This parameter specifies the domain device associated with this
  1045. * request.
  1046. * @lun: This parameter specifies the lun associated with this request.
  1047. *
  1048. * status, zero indicates success.
  1049. */
  1050. int isci_task_clear_task_set(
  1051. struct domain_device *d_device,
  1052. u8 *lun)
  1053. {
  1054. return TMF_RESP_FUNC_FAILED;
  1055. }
  1056. /**
  1057. * isci_task_query_task() - This function is implemented to cause libsas to
  1058. * correctly escalate the failed abort to a LUN or target reset (this is
  1059. * because sas_scsi_find_task libsas function does not correctly interpret
  1060. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1061. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1062. * returned, libsas will turn this into a target reset
  1063. * @task: This parameter specifies the sas task being queried.
  1064. * @lun: This parameter specifies the lun associated with this request.
  1065. *
  1066. * status, zero indicates success.
  1067. */
  1068. int isci_task_query_task(
  1069. struct sas_task *task)
  1070. {
  1071. /* See if there is a pending device reset for this device. */
  1072. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1073. return TMF_RESP_FUNC_FAILED;
  1074. else
  1075. return TMF_RESP_FUNC_SUCC;
  1076. }
  1077. /*
  1078. * isci_task_request_complete() - This function is called by the sci core when
  1079. * an task request completes.
  1080. * @ihost: This parameter specifies the ISCI host object
  1081. * @ireq: This parameter is the completed isci_request object.
  1082. * @completion_status: This parameter specifies the completion status from the
  1083. * sci core.
  1084. *
  1085. * none.
  1086. */
  1087. void
  1088. isci_task_request_complete(struct isci_host *ihost,
  1089. struct isci_request *ireq,
  1090. enum sci_task_status completion_status)
  1091. {
  1092. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1093. struct completion *tmf_complete = NULL;
  1094. struct completion *request_complete = ireq->io_request_completion;
  1095. dev_dbg(&ihost->pdev->dev,
  1096. "%s: request = %p, status=%d\n",
  1097. __func__, ireq, completion_status);
  1098. isci_request_change_state(ireq, completed);
  1099. set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
  1100. if (tmf) {
  1101. tmf->status = completion_status;
  1102. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1103. memcpy(&tmf->resp.resp_iu,
  1104. &ireq->ssp.rsp,
  1105. SSP_RESP_IU_MAX_SIZE);
  1106. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1107. memcpy(&tmf->resp.d2h_fis,
  1108. &ireq->stp.rsp,
  1109. sizeof(struct dev_to_host_fis));
  1110. }
  1111. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1112. tmf_complete = tmf->complete;
  1113. }
  1114. sci_controller_complete_io(ihost, ireq->target_device, ireq);
  1115. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1116. * or completed again.
  1117. */
  1118. set_bit(IREQ_TERMINATED, &ireq->flags);
  1119. /* As soon as something is in the terminate path, deallocation is
  1120. * managed there. Note that the final non-managed state of a task
  1121. * request is "completed".
  1122. */
  1123. if ((ireq->status == completed) ||
  1124. !isci_request_is_dealloc_managed(ireq->status)) {
  1125. isci_request_change_state(ireq, unallocated);
  1126. isci_free_tag(ihost, ireq->io_tag);
  1127. list_del_init(&ireq->dev_node);
  1128. }
  1129. /* "request_complete" is set if the task was being terminated. */
  1130. if (request_complete)
  1131. complete(request_complete);
  1132. /* The task management part completes last. */
  1133. if (tmf_complete)
  1134. complete(tmf_complete);
  1135. }
  1136. static int isci_reset_device(struct isci_host *ihost,
  1137. struct isci_remote_device *idev)
  1138. {
  1139. struct sas_phy *phy = sas_find_local_phy(idev->domain_dev);
  1140. enum sci_status status;
  1141. unsigned long flags;
  1142. int rc;
  1143. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1144. spin_lock_irqsave(&ihost->scic_lock, flags);
  1145. status = sci_remote_device_reset(idev);
  1146. if (status != SCI_SUCCESS) {
  1147. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1148. dev_dbg(&ihost->pdev->dev,
  1149. "%s: sci_remote_device_reset(%p) returned %d!\n",
  1150. __func__, idev, status);
  1151. return TMF_RESP_FUNC_FAILED;
  1152. }
  1153. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1154. rc = sas_phy_reset(phy, true);
  1155. /* Terminate in-progress I/O now. */
  1156. isci_remote_device_nuke_requests(ihost, idev);
  1157. /* Since all pending TCs have been cleaned, resume the RNC. */
  1158. spin_lock_irqsave(&ihost->scic_lock, flags);
  1159. status = sci_remote_device_reset_complete(idev);
  1160. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1161. if (status != SCI_SUCCESS) {
  1162. dev_dbg(&ihost->pdev->dev,
  1163. "%s: sci_remote_device_reset_complete(%p) "
  1164. "returned %d!\n", __func__, idev, status);
  1165. }
  1166. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1167. return rc;
  1168. }
  1169. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1170. {
  1171. struct isci_host *ihost = dev_to_ihost(dev);
  1172. struct isci_remote_device *idev;
  1173. unsigned long flags;
  1174. int ret;
  1175. spin_lock_irqsave(&ihost->scic_lock, flags);
  1176. idev = isci_lookup_device(dev);
  1177. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1178. if (!idev || !test_bit(IDEV_EH, &idev->flags)) {
  1179. ret = TMF_RESP_FUNC_COMPLETE;
  1180. goto out;
  1181. }
  1182. ret = isci_reset_device(ihost, idev);
  1183. out:
  1184. isci_put_device(idev);
  1185. return ret;
  1186. }
  1187. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1188. {
  1189. struct domain_device *dev = sdev_to_domain_dev(cmd->device);
  1190. struct isci_host *ihost = dev_to_ihost(dev);
  1191. struct isci_remote_device *idev;
  1192. unsigned long flags;
  1193. int ret;
  1194. spin_lock_irqsave(&ihost->scic_lock, flags);
  1195. idev = isci_lookup_device(dev);
  1196. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1197. if (!idev) {
  1198. ret = TMF_RESP_FUNC_COMPLETE;
  1199. goto out;
  1200. }
  1201. ret = isci_reset_device(ihost, idev);
  1202. out:
  1203. isci_put_device(idev);
  1204. return ret;
  1205. }