task.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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 "scic_task_request.h"
  59. #include "scic_io_request.h"
  60. #include "remote_device.h"
  61. #include "remote_node_context.h"
  62. #include "isci.h"
  63. #include "request.h"
  64. #include "sata.h"
  65. #include "task.h"
  66. #include "scic_sds_request.h"
  67. /**
  68. * isci_task_refuse() - complete the request to the upper layer driver in
  69. * the case where an I/O needs to be completed back in the submit path.
  70. * @ihost: host on which the the request was queued
  71. * @task: request to complete
  72. * @response: response code for the completed task.
  73. * @status: status code for the completed task.
  74. *
  75. */
  76. static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
  77. enum service_response response,
  78. enum exec_status status)
  79. {
  80. enum isci_completion_selection disposition;
  81. disposition = isci_perform_normal_io_completion;
  82. disposition = isci_task_set_completion_status(task, response, status,
  83. disposition);
  84. /* Tasks aborted specifically by a call to the lldd_abort_task
  85. * function should not be completed to the host in the regular path.
  86. */
  87. switch (disposition) {
  88. case isci_perform_normal_io_completion:
  89. /* Normal notification (task_done) */
  90. dev_dbg(&ihost->pdev->dev,
  91. "%s: Normal - task = %p, response=%d, "
  92. "status=%d\n",
  93. __func__, task, response, status);
  94. task->lldd_task = NULL;
  95. isci_execpath_callback(ihost, task, task->task_done);
  96. break;
  97. case isci_perform_aborted_io_completion:
  98. /* No notification because this request is already in the
  99. * abort path.
  100. */
  101. dev_warn(&ihost->pdev->dev,
  102. "%s: Aborted - task = %p, response=%d, "
  103. "status=%d\n",
  104. __func__, task, response, status);
  105. break;
  106. case isci_perform_error_io_completion:
  107. /* Use sas_task_abort */
  108. dev_warn(&ihost->pdev->dev,
  109. "%s: Error - task = %p, response=%d, "
  110. "status=%d\n",
  111. __func__, task, response, status);
  112. isci_execpath_callback(ihost, task, sas_task_abort);
  113. break;
  114. default:
  115. dev_warn(&ihost->pdev->dev,
  116. "%s: isci task notification default case!",
  117. __func__);
  118. sas_task_abort(task);
  119. break;
  120. }
  121. }
  122. #define for_each_sas_task(num, task) \
  123. for (; num > 0; num--,\
  124. task = list_entry(task->list.next, struct sas_task, list))
  125. /**
  126. * isci_task_execute_task() - This function is one of the SAS Domain Template
  127. * functions. This function is called by libsas to send a task down to
  128. * hardware.
  129. * @task: This parameter specifies the SAS task to send.
  130. * @num: This parameter specifies the number of tasks to queue.
  131. * @gfp_flags: This parameter specifies the context of this call.
  132. *
  133. * status, zero indicates success.
  134. */
  135. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  136. {
  137. struct isci_host *ihost = dev_to_ihost(task->dev);
  138. struct isci_request *request = NULL;
  139. struct isci_remote_device *device;
  140. unsigned long flags;
  141. int ret;
  142. enum sci_status status;
  143. enum isci_status device_status;
  144. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  145. /* Check if we have room for more tasks */
  146. ret = isci_host_can_queue(ihost, num);
  147. if (ret) {
  148. dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
  149. return ret;
  150. }
  151. for_each_sas_task(num, task) {
  152. dev_dbg(&ihost->pdev->dev,
  153. "task = %p, num = %d; dev = %p; cmd = %p\n",
  154. task, num, task->dev, task->uldd_task);
  155. device = task->dev->lldd_dev;
  156. if (device)
  157. device_status = device->status;
  158. else
  159. device_status = isci_freed;
  160. /* From this point onward, any process that needs to guarantee
  161. * that there is no kernel I/O being started will have to wait
  162. * for the quiesce spinlock.
  163. */
  164. if (device_status != isci_ready_for_io) {
  165. /* Forces a retry from scsi mid layer. */
  166. dev_dbg(&ihost->pdev->dev,
  167. "%s: task %p: isci_host->status = %d, "
  168. "device = %p; device_status = 0x%x\n\n",
  169. __func__,
  170. task,
  171. isci_host_get_state(ihost),
  172. device,
  173. device_status);
  174. if (device_status == isci_ready) {
  175. /* Indicate QUEUE_FULL so that the scsi midlayer
  176. * retries.
  177. */
  178. isci_task_refuse(ihost, task,
  179. SAS_TASK_COMPLETE,
  180. SAS_QUEUE_FULL);
  181. } else {
  182. /* Else, the device is going down. */
  183. isci_task_refuse(ihost, task,
  184. SAS_TASK_UNDELIVERED,
  185. SAS_DEVICE_UNKNOWN);
  186. }
  187. isci_host_can_dequeue(ihost, 1);
  188. } else {
  189. /* There is a device and it's ready for I/O. */
  190. spin_lock_irqsave(&task->task_state_lock, flags);
  191. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  192. spin_unlock_irqrestore(&task->task_state_lock,
  193. flags);
  194. isci_task_refuse(ihost, task,
  195. SAS_TASK_UNDELIVERED,
  196. SAM_STAT_TASK_ABORTED);
  197. /* The I/O was aborted. */
  198. } else {
  199. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  200. spin_unlock_irqrestore(&task->task_state_lock, flags);
  201. /* build and send the request. */
  202. status = isci_request_execute(ihost, task, &request,
  203. gfp_flags);
  204. if (status != SCI_SUCCESS) {
  205. spin_lock_irqsave(&task->task_state_lock, flags);
  206. /* Did not really start this command. */
  207. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  208. spin_unlock_irqrestore(&task->task_state_lock, flags);
  209. /* Indicate QUEUE_FULL so that the scsi
  210. * midlayer retries. if the request
  211. * failed for remote device reasons,
  212. * it gets returned as
  213. * SAS_TASK_UNDELIVERED next time
  214. * through.
  215. */
  216. isci_task_refuse(ihost, task,
  217. SAS_TASK_COMPLETE,
  218. SAS_QUEUE_FULL);
  219. isci_host_can_dequeue(ihost, 1);
  220. }
  221. }
  222. }
  223. }
  224. return 0;
  225. }
  226. /**
  227. * isci_task_request_build() - This function builds the task request object.
  228. * @isci_host: This parameter specifies the ISCI host object
  229. * @request: This parameter points to the isci_request object allocated in the
  230. * request construct function.
  231. * @tmf: This parameter is the task management struct to be built
  232. *
  233. * SCI_SUCCESS on successfull completion, or specific failure code.
  234. */
  235. static enum sci_status isci_task_request_build(
  236. struct isci_host *isci_host,
  237. struct isci_request **isci_request,
  238. struct isci_tmf *isci_tmf)
  239. {
  240. struct scic_sds_remote_device *sci_device;
  241. enum sci_status status = SCI_FAILURE;
  242. struct isci_request *request = NULL;
  243. struct isci_remote_device *isci_device;
  244. struct domain_device *dev;
  245. dev_dbg(&isci_host->pdev->dev,
  246. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  247. isci_device = isci_tmf->device;
  248. sci_device = &isci_device->sci;
  249. dev = isci_device->domain_dev;
  250. /* do common allocation and init of request object. */
  251. status = isci_request_alloc_tmf(
  252. isci_host,
  253. isci_tmf,
  254. &request,
  255. isci_device,
  256. GFP_ATOMIC
  257. );
  258. if (status != SCI_SUCCESS)
  259. goto out;
  260. /* let the core do it's construct. */
  261. status = scic_task_request_construct(&isci_host->sci, sci_device,
  262. SCI_CONTROLLER_INVALID_IO_TAG,
  263. request, &request->sci_req,
  264. &request->sci_request_handle);
  265. if (status != SCI_SUCCESS) {
  266. dev_warn(&isci_host->pdev->dev,
  267. "%s: scic_task_request_construct failed - "
  268. "status = 0x%x\n",
  269. __func__,
  270. status);
  271. goto errout;
  272. }
  273. request->sci_request_handle->ireq = request;
  274. /* XXX convert to get this from task->tproto like other drivers */
  275. if (dev->dev_type == SAS_END_DEV) {
  276. isci_tmf->proto = SAS_PROTOCOL_SSP;
  277. status = scic_task_request_construct_ssp(
  278. request->sci_request_handle
  279. );
  280. if (status != SCI_SUCCESS)
  281. goto errout;
  282. }
  283. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  284. isci_tmf->proto = SAS_PROTOCOL_SATA;
  285. status = isci_sata_management_task_request_build(request);
  286. if (status != SCI_SUCCESS)
  287. goto errout;
  288. }
  289. goto out;
  290. errout:
  291. /* release the dma memory if we fail. */
  292. isci_request_free(isci_host, request);
  293. request = NULL;
  294. out:
  295. *isci_request = request;
  296. return status;
  297. }
  298. /**
  299. * isci_tmf_timeout_cb() - This function is called as a kernel callback when
  300. * the timeout period for the TMF has expired.
  301. *
  302. *
  303. */
  304. static void isci_tmf_timeout_cb(void *tmf_request_arg)
  305. {
  306. struct isci_request *request = (struct isci_request *)tmf_request_arg;
  307. struct isci_tmf *tmf = isci_request_access_tmf(request);
  308. enum sci_status status;
  309. /* This task management request has timed-out. Terminate the request
  310. * so that the request eventually completes to the requestor in the
  311. * request completion callback path.
  312. */
  313. /* Note - the timer callback function itself has provided spinlock
  314. * exclusion from the start and completion paths. No need to take
  315. * the request->isci_host->scic_lock here.
  316. */
  317. if (tmf->timeout_timer != NULL) {
  318. /* Call the users callback, if any. */
  319. if (tmf->cb_state_func != NULL)
  320. tmf->cb_state_func(isci_tmf_timed_out, tmf,
  321. tmf->cb_data);
  322. /* Terminate the TMF transmit request. */
  323. status = scic_controller_terminate_request(
  324. &request->isci_host->sci,
  325. &request->isci_device->sci,
  326. request->sci_request_handle
  327. );
  328. dev_dbg(&request->isci_host->pdev->dev,
  329. "%s: tmf_request = %p; tmf = %p; status = %d\n",
  330. __func__, request, tmf, status);
  331. } else
  332. dev_dbg(&request->isci_host->pdev->dev,
  333. "%s: timer already canceled! "
  334. "tmf_request = %p; tmf = %p\n",
  335. __func__, request, tmf);
  336. /* No need to unlock since the caller to this callback is doing it for
  337. * us.
  338. * request->isci_host->scic_lock
  339. */
  340. }
  341. /**
  342. * isci_task_execute_tmf() - This function builds and sends a task request,
  343. * then waits for the completion.
  344. * @isci_host: This parameter specifies the ISCI host object
  345. * @tmf: This parameter is the pointer to the task management structure for
  346. * this request.
  347. * @timeout_ms: This parameter specifies the timeout period for the task
  348. * management request.
  349. *
  350. * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
  351. * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
  352. */
  353. int isci_task_execute_tmf(
  354. struct isci_host *isci_host,
  355. struct isci_tmf *tmf,
  356. unsigned long timeout_ms)
  357. {
  358. DECLARE_COMPLETION_ONSTACK(completion);
  359. enum sci_task_status status = SCI_TASK_FAILURE;
  360. struct scic_sds_remote_device *sci_device;
  361. struct isci_remote_device *isci_device = tmf->device;
  362. struct isci_request *request;
  363. int ret = TMF_RESP_FUNC_FAILED;
  364. unsigned long flags;
  365. /* sanity check, return TMF_RESP_FUNC_FAILED
  366. * if the device is not there and ready.
  367. */
  368. if (!isci_device || isci_device->status != isci_ready_for_io) {
  369. dev_dbg(&isci_host->pdev->dev,
  370. "%s: isci_device = %p not ready (%d)\n",
  371. __func__,
  372. isci_device, isci_device->status);
  373. return TMF_RESP_FUNC_FAILED;
  374. } else
  375. dev_dbg(&isci_host->pdev->dev,
  376. "%s: isci_device = %p\n",
  377. __func__, isci_device);
  378. sci_device = &isci_device->sci;
  379. /* Assign the pointer to the TMF's completion kernel wait structure. */
  380. tmf->complete = &completion;
  381. isci_task_request_build(
  382. isci_host,
  383. &request,
  384. tmf
  385. );
  386. if (!request) {
  387. dev_warn(&isci_host->pdev->dev,
  388. "%s: isci_task_request_build failed\n",
  389. __func__);
  390. return TMF_RESP_FUNC_FAILED;
  391. }
  392. /* Allocate the TMF timeout timer. */
  393. spin_lock_irqsave(&isci_host->scic_lock, flags);
  394. tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
  395. /* Start the timer. */
  396. if (tmf->timeout_timer)
  397. isci_timer_start(tmf->timeout_timer, timeout_ms);
  398. else
  399. dev_warn(&isci_host->pdev->dev,
  400. "%s: isci_timer_create failed!!!!\n",
  401. __func__);
  402. /* start the TMF io. */
  403. status = scic_controller_start_task(
  404. &isci_host->sci,
  405. sci_device,
  406. request->sci_request_handle,
  407. SCI_CONTROLLER_INVALID_IO_TAG
  408. );
  409. if (status != SCI_TASK_SUCCESS) {
  410. dev_warn(&isci_host->pdev->dev,
  411. "%s: start_io failed - status = 0x%x, request = %p\n",
  412. __func__,
  413. status,
  414. request);
  415. goto cleanup_request;
  416. }
  417. /* Call the users callback, if any. */
  418. if (tmf->cb_state_func != NULL)
  419. tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
  420. /* Change the state of the TMF-bearing request to "started". */
  421. isci_request_change_state(request, started);
  422. /* add the request to the remote device request list. */
  423. list_add(&request->dev_node, &isci_device->reqs_in_process);
  424. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  425. /* Wait for the TMF to complete, or a timeout. */
  426. wait_for_completion(&completion);
  427. isci_print_tmf(tmf);
  428. if (tmf->status == SCI_SUCCESS)
  429. ret = TMF_RESP_FUNC_COMPLETE;
  430. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  431. dev_dbg(&isci_host->pdev->dev,
  432. "%s: tmf.status == "
  433. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  434. __func__);
  435. ret = TMF_RESP_FUNC_COMPLETE;
  436. }
  437. /* Else - leave the default "failed" status alone. */
  438. dev_dbg(&isci_host->pdev->dev,
  439. "%s: completed request = %p\n",
  440. __func__,
  441. request);
  442. if (request->io_request_completion != NULL) {
  443. /* The fact that this is non-NULL for a TMF request
  444. * means there is a thread waiting for this TMF to
  445. * finish.
  446. */
  447. complete(request->io_request_completion);
  448. }
  449. spin_lock_irqsave(&isci_host->scic_lock, flags);
  450. cleanup_request:
  451. /* Clean up the timer if needed. */
  452. if (tmf->timeout_timer) {
  453. isci_del_timer(isci_host, tmf->timeout_timer);
  454. tmf->timeout_timer = NULL;
  455. }
  456. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  457. isci_request_free(isci_host, request);
  458. return ret;
  459. }
  460. void isci_task_build_tmf(
  461. struct isci_tmf *tmf,
  462. struct isci_remote_device *isci_device,
  463. enum isci_tmf_function_codes code,
  464. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  465. struct isci_tmf *,
  466. void *),
  467. void *cb_data)
  468. {
  469. dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
  470. "%s: isci_device = %p\n", __func__, isci_device);
  471. memset(tmf, 0, sizeof(*tmf));
  472. tmf->device = isci_device;
  473. tmf->tmf_code = code;
  474. tmf->timeout_timer = NULL;
  475. tmf->cb_state_func = tmf_sent_cb;
  476. tmf->cb_data = cb_data;
  477. }
  478. static void isci_task_build_abort_task_tmf(
  479. struct isci_tmf *tmf,
  480. struct isci_remote_device *isci_device,
  481. enum isci_tmf_function_codes code,
  482. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  483. struct isci_tmf *,
  484. void *),
  485. struct isci_request *old_request)
  486. {
  487. isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
  488. (void *)old_request);
  489. tmf->io_tag = old_request->io_tag;
  490. }
  491. static struct isci_request *isci_task_get_request_from_task(
  492. struct sas_task *task,
  493. struct isci_remote_device **isci_device)
  494. {
  495. struct isci_request *request = NULL;
  496. unsigned long flags;
  497. spin_lock_irqsave(&task->task_state_lock, flags);
  498. request = task->lldd_task;
  499. /* If task is already done, the request isn't valid */
  500. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  501. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  502. (request != NULL)) {
  503. if (isci_device != NULL)
  504. *isci_device = request->isci_device;
  505. }
  506. spin_unlock_irqrestore(&task->task_state_lock, flags);
  507. return request;
  508. }
  509. /**
  510. * isci_task_validate_request_to_abort() - This function checks the given I/O
  511. * against the "started" state. If the request is still "started", it's
  512. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  513. * BEFORE CALLING THIS FUNCTION.
  514. * @isci_request: This parameter specifies the request object to control.
  515. * @isci_host: This parameter specifies the ISCI host object
  516. * @isci_device: This is the device to which the request is pending.
  517. * @aborted_io_completion: This is a completion structure that will be added to
  518. * the request in case it is changed to aborting; this completion is
  519. * triggered when the request is fully completed.
  520. *
  521. * Either "started" on successful change of the task status to "aborted", or
  522. * "unallocated" if the task cannot be controlled.
  523. */
  524. static enum isci_request_status isci_task_validate_request_to_abort(
  525. struct isci_request *isci_request,
  526. struct isci_host *isci_host,
  527. struct isci_remote_device *isci_device,
  528. struct completion *aborted_io_completion)
  529. {
  530. enum isci_request_status old_state = unallocated;
  531. /* Only abort the task if it's in the
  532. * device's request_in_process list
  533. */
  534. if (isci_request && !list_empty(&isci_request->dev_node)) {
  535. old_state = isci_request_change_started_to_aborted(
  536. isci_request, aborted_io_completion);
  537. }
  538. return old_state;
  539. }
  540. static void isci_request_cleanup_completed_loiterer(
  541. struct isci_host *isci_host,
  542. struct isci_remote_device *isci_device,
  543. struct isci_request *isci_request)
  544. {
  545. struct sas_task *task;
  546. unsigned long flags;
  547. task = (isci_request->ttype == io_task)
  548. ? isci_request_access_task(isci_request)
  549. : NULL;
  550. dev_dbg(&isci_host->pdev->dev,
  551. "%s: isci_device=%p, request=%p, task=%p\n",
  552. __func__, isci_device, isci_request, task);
  553. spin_lock_irqsave(&isci_host->scic_lock, flags);
  554. list_del_init(&isci_request->dev_node);
  555. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  556. if (task != NULL) {
  557. spin_lock_irqsave(&task->task_state_lock, flags);
  558. task->lldd_task = NULL;
  559. isci_set_task_doneflags(task);
  560. /* If this task is not in the abort path, call task_done. */
  561. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  562. spin_unlock_irqrestore(&task->task_state_lock, flags);
  563. task->task_done(task);
  564. } else
  565. spin_unlock_irqrestore(&task->task_state_lock, flags);
  566. }
  567. isci_request_free(isci_host, isci_request);
  568. }
  569. /**
  570. * @isci_termination_timed_out(): this function will deal with a request for
  571. * which the wait for termination has timed-out.
  572. *
  573. * @isci_host This SCU.
  574. * @isci_request The I/O request being terminated.
  575. */
  576. static void
  577. isci_termination_timed_out(
  578. struct isci_host * host,
  579. struct isci_request * request
  580. )
  581. {
  582. unsigned long state_flags;
  583. dev_warn(&host->pdev->dev,
  584. "%s: host = %p; request = %p\n",
  585. __func__, host, request);
  586. /* At this point, the request to terminate
  587. * has timed out. The best we can do is to
  588. * have the request die a silent death
  589. * if it ever completes.
  590. */
  591. spin_lock_irqsave(&request->state_lock, state_flags);
  592. if (request->status == started) {
  593. /* Set the request state to "dead",
  594. * and clear the task pointer so that an actual
  595. * completion event callback doesn't do
  596. * anything.
  597. */
  598. request->status = dead;
  599. /* Clear the timeout completion event pointer.*/
  600. request->io_request_completion = NULL;
  601. if (request->ttype == io_task) {
  602. /* Break links with the sas_task. */
  603. if (request->ttype_ptr.io_task_ptr != NULL) {
  604. request->ttype_ptr.io_task_ptr->lldd_task = NULL;
  605. request->ttype_ptr.io_task_ptr = NULL;
  606. }
  607. }
  608. }
  609. spin_unlock_irqrestore(&request->state_lock, state_flags);
  610. }
  611. /**
  612. * isci_terminate_request_core() - This function will terminate the given
  613. * request, and wait for it to complete. This function must only be called
  614. * from a thread that can wait. Note that the request is terminated and
  615. * completed (back to the host, if started there).
  616. * @isci_host: This SCU.
  617. * @isci_device: The target.
  618. * @isci_request: The I/O request to be terminated.
  619. *
  620. *
  621. */
  622. static void isci_terminate_request_core(
  623. struct isci_host *isci_host,
  624. struct isci_remote_device *isci_device,
  625. struct isci_request *isci_request)
  626. {
  627. enum sci_status status = SCI_SUCCESS;
  628. bool was_terminated = false;
  629. bool needs_cleanup_handling = false;
  630. enum isci_request_status request_status;
  631. unsigned long flags;
  632. unsigned long timeout_remaining;
  633. dev_dbg(&isci_host->pdev->dev,
  634. "%s: device = %p; request = %p\n",
  635. __func__, isci_device, isci_request);
  636. spin_lock_irqsave(&isci_host->scic_lock, flags);
  637. /* Note that we are not going to control
  638. * the target to abort the request.
  639. */
  640. isci_request->complete_in_target = true;
  641. /* Make sure the request wasn't just sitting around signalling
  642. * device condition (if the request handle is NULL, then the
  643. * request completed but needed additional handling here).
  644. */
  645. if (isci_request->sci_request_handle != NULL) {
  646. was_terminated = true;
  647. needs_cleanup_handling = true;
  648. status = scic_controller_terminate_request(
  649. &isci_host->sci,
  650. &isci_device->sci,
  651. isci_request->sci_request_handle);
  652. }
  653. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  654. /*
  655. * The only time the request to terminate will
  656. * fail is when the io request is completed and
  657. * being aborted.
  658. */
  659. if (status != SCI_SUCCESS) {
  660. dev_err(&isci_host->pdev->dev,
  661. "%s: scic_controller_terminate_request"
  662. " returned = 0x%x\n",
  663. __func__,
  664. status);
  665. /* Clear the completion pointer from the request. */
  666. isci_request->io_request_completion = NULL;
  667. } else {
  668. if (was_terminated) {
  669. dev_dbg(&isci_host->pdev->dev,
  670. "%s: before completion wait (%p)\n",
  671. __func__,
  672. isci_request->io_request_completion);
  673. /* Wait here for the request to complete. */
  674. #define TERMINATION_TIMEOUT_MSEC 50
  675. timeout_remaining
  676. = wait_for_completion_timeout(
  677. isci_request->io_request_completion,
  678. msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
  679. if (!timeout_remaining) {
  680. isci_termination_timed_out(isci_host,
  681. isci_request);
  682. dev_err(&isci_host->pdev->dev,
  683. "%s: *** Timeout waiting for "
  684. "termination(%p/%p)\n",
  685. __func__,
  686. isci_request->io_request_completion,
  687. isci_request);
  688. } else
  689. dev_dbg(&isci_host->pdev->dev,
  690. "%s: after completion wait (%p)\n",
  691. __func__,
  692. isci_request->io_request_completion);
  693. }
  694. /* Clear the completion pointer from the request. */
  695. isci_request->io_request_completion = NULL;
  696. /* Peek at the status of the request. This will tell
  697. * us if there was special handling on the request such that it
  698. * needs to be detached and freed here.
  699. */
  700. spin_lock_irqsave(&isci_request->state_lock, flags);
  701. request_status = isci_request_get_state(isci_request);
  702. if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
  703. && ((request_status == aborted)
  704. || (request_status == aborting)
  705. || (request_status == terminating)
  706. || (request_status == completed)
  707. || (request_status == dead)
  708. )
  709. ) {
  710. /* The completion routine won't free a request in
  711. * the aborted/aborting/etc. states, so we do
  712. * it here.
  713. */
  714. needs_cleanup_handling = true;
  715. }
  716. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  717. if (needs_cleanup_handling)
  718. isci_request_cleanup_completed_loiterer(
  719. isci_host, isci_device, isci_request
  720. );
  721. }
  722. }
  723. static void isci_terminate_request(
  724. struct isci_host *isci_host,
  725. struct isci_remote_device *isci_device,
  726. struct isci_request *isci_request,
  727. enum isci_request_status new_request_state)
  728. {
  729. enum isci_request_status old_state;
  730. DECLARE_COMPLETION_ONSTACK(request_completion);
  731. /* Change state to "new_request_state" if it is currently "started" */
  732. old_state = isci_request_change_started_to_newstate(
  733. isci_request,
  734. &request_completion,
  735. new_request_state
  736. );
  737. if ((old_state == started) ||
  738. (old_state == completed) ||
  739. (old_state == aborting)) {
  740. /* If the old_state is started:
  741. * This request was not already being aborted. If it had been,
  742. * then the aborting I/O (ie. the TMF request) would not be in
  743. * the aborting state, and thus would be terminated here. Note
  744. * that since the TMF completion's call to the kernel function
  745. * "complete()" does not happen until the pending I/O request
  746. * terminate fully completes, we do not have to implement a
  747. * special wait here for already aborting requests - the
  748. * termination of the TMF request will force the request
  749. * to finish it's already started terminate.
  750. *
  751. * If old_state == completed:
  752. * This request completed from the SCU hardware perspective
  753. * and now just needs cleaning up in terms of freeing the
  754. * request and potentially calling up to libsas.
  755. *
  756. * If old_state == aborting:
  757. * This request has already gone through a TMF timeout, but may
  758. * not have been terminated; needs cleaning up at least.
  759. */
  760. isci_terminate_request_core(isci_host, isci_device,
  761. isci_request);
  762. }
  763. }
  764. /**
  765. * isci_terminate_pending_requests() - This function will change the all of the
  766. * requests on the given device's state to "aborting", will terminate the
  767. * requests, and wait for them to complete. This function must only be
  768. * called from a thread that can wait. Note that the requests are all
  769. * terminated and completed (back to the host, if started there).
  770. * @isci_host: This parameter specifies SCU.
  771. * @isci_device: This parameter specifies the target.
  772. *
  773. *
  774. */
  775. void isci_terminate_pending_requests(
  776. struct isci_host *isci_host,
  777. struct isci_remote_device *isci_device,
  778. enum isci_request_status new_request_state)
  779. {
  780. struct isci_request *request;
  781. struct isci_request *next_request;
  782. unsigned long flags;
  783. struct list_head aborted_request_list;
  784. INIT_LIST_HEAD(&aborted_request_list);
  785. dev_dbg(&isci_host->pdev->dev,
  786. "%s: isci_device = %p (new request state = %d)\n",
  787. __func__, isci_device, new_request_state);
  788. spin_lock_irqsave(&isci_host->scic_lock, flags);
  789. /* Move all of the pending requests off of the device list. */
  790. list_splice_init(&isci_device->reqs_in_process,
  791. &aborted_request_list);
  792. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  793. /* Iterate through the now-local list. */
  794. list_for_each_entry_safe(request, next_request,
  795. &aborted_request_list, dev_node) {
  796. dev_warn(&isci_host->pdev->dev,
  797. "%s: isci_device=%p request=%p; task=%p\n",
  798. __func__,
  799. isci_device, request,
  800. ((request->ttype == io_task)
  801. ? isci_request_access_task(request)
  802. : NULL));
  803. /* Mark all still pending I/O with the selected next
  804. * state, terminate and free it.
  805. */
  806. isci_terminate_request(isci_host, isci_device,
  807. request, new_request_state
  808. );
  809. }
  810. }
  811. /**
  812. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  813. * Template functions.
  814. * @lun: This parameter specifies the lun to be reset.
  815. *
  816. * status, zero indicates success.
  817. */
  818. static int isci_task_send_lu_reset_sas(
  819. struct isci_host *isci_host,
  820. struct isci_remote_device *isci_device,
  821. u8 *lun)
  822. {
  823. struct isci_tmf tmf;
  824. int ret = TMF_RESP_FUNC_FAILED;
  825. dev_dbg(&isci_host->pdev->dev,
  826. "%s: isci_host = %p, isci_device = %p\n",
  827. __func__, isci_host, isci_device);
  828. /* Send the LUN reset to the target. By the time the call returns,
  829. * the TMF has fully exected in the target (in which case the return
  830. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  831. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  832. */
  833. isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
  834. NULL);
  835. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  836. ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  837. if (ret == TMF_RESP_FUNC_COMPLETE)
  838. dev_dbg(&isci_host->pdev->dev,
  839. "%s: %p: TMF_LU_RESET passed\n",
  840. __func__, isci_device);
  841. else
  842. dev_dbg(&isci_host->pdev->dev,
  843. "%s: %p: TMF_LU_RESET failed (%x)\n",
  844. __func__, isci_device, ret);
  845. return ret;
  846. }
  847. /**
  848. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  849. * functions. This is one of the Task Management functoins called by libsas,
  850. * to reset the given lun. Note the assumption that while this call is
  851. * executing, no I/O will be sent by the host to the device.
  852. * @lun: This parameter specifies the lun to be reset.
  853. *
  854. * status, zero indicates success.
  855. */
  856. int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
  857. {
  858. struct isci_host *isci_host = dev_to_ihost(domain_device);
  859. struct isci_remote_device *isci_device = NULL;
  860. int ret;
  861. bool device_stopping = false;
  862. isci_device = domain_device->lldd_dev;
  863. dev_dbg(&isci_host->pdev->dev,
  864. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  865. __func__, domain_device, isci_host, isci_device);
  866. if (isci_device != NULL) {
  867. device_stopping = (isci_device->status == isci_stopping)
  868. || (isci_device->status == isci_stopped);
  869. set_bit(IDEV_EH, &isci_device->flags);
  870. }
  871. /* If there is a device reset pending on any request in the
  872. * device's list, fail this LUN reset request in order to
  873. * escalate to the device reset.
  874. */
  875. if (!isci_device || device_stopping ||
  876. isci_device_is_reset_pending(isci_host, isci_device)) {
  877. dev_warn(&isci_host->pdev->dev,
  878. "%s: No dev (%p), or "
  879. "RESET PENDING: domain_device=%p\n",
  880. __func__, isci_device, domain_device);
  881. return TMF_RESP_FUNC_FAILED;
  882. }
  883. /* Send the task management part of the reset. */
  884. if (sas_protocol_ata(domain_device->tproto)) {
  885. ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
  886. } else
  887. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  888. /* If the LUN reset worked, all the I/O can now be terminated. */
  889. if (ret == TMF_RESP_FUNC_COMPLETE)
  890. /* Terminate all I/O now. */
  891. isci_terminate_pending_requests(isci_host,
  892. isci_device,
  893. terminating);
  894. return ret;
  895. }
  896. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  897. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  898. {
  899. return TMF_RESP_FUNC_FAILED;
  900. }
  901. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  902. {
  903. return TMF_RESP_FUNC_FAILED;
  904. }
  905. /* Task Management Functions. Must be called from process context. */
  906. /**
  907. * isci_abort_task_process_cb() - This is a helper function for the abort task
  908. * TMF command. It manages the request state with respect to the successful
  909. * transmission / completion of the abort task request.
  910. * @cb_state: This parameter specifies when this function was called - after
  911. * the TMF request has been started and after it has timed-out.
  912. * @tmf: This parameter specifies the TMF in progress.
  913. *
  914. *
  915. */
  916. static void isci_abort_task_process_cb(
  917. enum isci_tmf_cb_state cb_state,
  918. struct isci_tmf *tmf,
  919. void *cb_data)
  920. {
  921. struct isci_request *old_request;
  922. old_request = (struct isci_request *)cb_data;
  923. dev_dbg(&old_request->isci_host->pdev->dev,
  924. "%s: tmf=%p, old_request=%p\n",
  925. __func__, tmf, old_request);
  926. switch (cb_state) {
  927. case isci_tmf_started:
  928. /* The TMF has been started. Nothing to do here, since the
  929. * request state was already set to "aborted" by the abort
  930. * task function.
  931. */
  932. if ((old_request->status != aborted)
  933. && (old_request->status != completed))
  934. dev_err(&old_request->isci_host->pdev->dev,
  935. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  936. __func__, old_request->status, tmf, old_request);
  937. break;
  938. case isci_tmf_timed_out:
  939. /* Set the task's state to "aborting", since the abort task
  940. * function thread set it to "aborted" (above) in anticipation
  941. * of the task management request working correctly. Since the
  942. * timeout has now fired, the TMF request failed. We set the
  943. * state such that the request completion will indicate the
  944. * device is no longer present.
  945. */
  946. isci_request_change_state(old_request, aborting);
  947. break;
  948. default:
  949. dev_err(&old_request->isci_host->pdev->dev,
  950. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  951. __func__, cb_state, tmf, old_request);
  952. break;
  953. }
  954. }
  955. /**
  956. * isci_task_abort_task() - This function is one of the SAS Domain Template
  957. * functions. This function is called by libsas to abort a specified task.
  958. * @task: This parameter specifies the SAS task to abort.
  959. *
  960. * status, zero indicates success.
  961. */
  962. int isci_task_abort_task(struct sas_task *task)
  963. {
  964. struct isci_host *isci_host = dev_to_ihost(task->dev);
  965. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  966. struct isci_request *old_request = NULL;
  967. enum isci_request_status old_state;
  968. struct isci_remote_device *isci_device = NULL;
  969. struct isci_tmf tmf;
  970. int ret = TMF_RESP_FUNC_FAILED;
  971. unsigned long flags;
  972. bool any_dev_reset = false;
  973. bool device_stopping;
  974. /* Get the isci_request reference from the task. Note that
  975. * this check does not depend on the pending request list
  976. * in the device, because tasks driving resets may land here
  977. * after completion in the core.
  978. */
  979. old_request = isci_task_get_request_from_task(task, &isci_device);
  980. dev_dbg(&isci_host->pdev->dev,
  981. "%s: task = %p\n", __func__, task);
  982. /* Check if the device has been / is currently being removed.
  983. * If so, no task management will be done, and the I/O will
  984. * be terminated.
  985. */
  986. device_stopping = (isci_device->status == isci_stopping)
  987. || (isci_device->status == isci_stopped);
  988. /* XXX need to fix device lookup lifetime (needs to be done
  989. * under scic_lock, among other things...), but for now assume
  990. * the device is available like the above code
  991. */
  992. set_bit(IDEV_EH, &isci_device->flags);
  993. /* This version of the driver will fail abort requests for
  994. * SATA/STP. Failing the abort request this way will cause the
  995. * SCSI error handler thread to escalate to LUN reset
  996. */
  997. if (sas_protocol_ata(task->task_proto) && !device_stopping) {
  998. dev_warn(&isci_host->pdev->dev,
  999. " task %p is for a STP/SATA device;"
  1000. " returning TMF_RESP_FUNC_FAILED\n"
  1001. " to cause a LUN reset...\n", task);
  1002. return TMF_RESP_FUNC_FAILED;
  1003. }
  1004. dev_dbg(&isci_host->pdev->dev,
  1005. "%s: old_request == %p\n", __func__, old_request);
  1006. if (!device_stopping)
  1007. any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
  1008. spin_lock_irqsave(&task->task_state_lock, flags);
  1009. /* Don't do resets to stopping devices. */
  1010. if (device_stopping) {
  1011. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1012. any_dev_reset = false;
  1013. } else /* See if there is a pending device reset for this device. */
  1014. any_dev_reset = any_dev_reset
  1015. || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
  1016. /* If the extraction of the request reference from the task
  1017. * failed, then the request has been completed (or if there is a
  1018. * pending reset then this abort request function must be failed
  1019. * in order to escalate to the target reset).
  1020. */
  1021. if ((old_request == NULL) || any_dev_reset) {
  1022. /* If the device reset task flag is set, fail the task
  1023. * management request. Otherwise, the original request
  1024. * has completed.
  1025. */
  1026. if (any_dev_reset) {
  1027. /* Turn off the task's DONE to make sure this
  1028. * task is escalated to a target reset.
  1029. */
  1030. task->task_state_flags &= ~SAS_TASK_STATE_DONE;
  1031. /* Make the reset happen as soon as possible. */
  1032. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  1033. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1034. /* Fail the task management request in order to
  1035. * escalate to the target reset.
  1036. */
  1037. ret = TMF_RESP_FUNC_FAILED;
  1038. dev_dbg(&isci_host->pdev->dev,
  1039. "%s: Failing task abort in order to "
  1040. "escalate to target reset because\n"
  1041. "SAS_TASK_NEED_DEV_RESET is set for "
  1042. "task %p on dev %p\n",
  1043. __func__, task, isci_device);
  1044. } else {
  1045. /* The request has already completed and there
  1046. * is nothing to do here other than to set the task
  1047. * done bit, and indicate that the task abort function
  1048. * was sucessful.
  1049. */
  1050. isci_set_task_doneflags(task);
  1051. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1052. ret = TMF_RESP_FUNC_COMPLETE;
  1053. dev_dbg(&isci_host->pdev->dev,
  1054. "%s: abort task not needed for %p\n",
  1055. __func__, task);
  1056. }
  1057. return ret;
  1058. }
  1059. else
  1060. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1061. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1062. /* Check the request status and change to "aborted" if currently
  1063. * "starting"; if true then set the I/O kernel completion
  1064. * struct that will be triggered when the request completes.
  1065. */
  1066. old_state = isci_task_validate_request_to_abort(
  1067. old_request, isci_host, isci_device,
  1068. &aborted_io_completion);
  1069. if ((old_state != started) &&
  1070. (old_state != completed) &&
  1071. (old_state != aborting)) {
  1072. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1073. /* The request was already being handled by someone else (because
  1074. * they got to set the state away from started).
  1075. */
  1076. dev_dbg(&isci_host->pdev->dev,
  1077. "%s: device = %p; old_request %p already being aborted\n",
  1078. __func__,
  1079. isci_device, old_request);
  1080. return TMF_RESP_FUNC_COMPLETE;
  1081. }
  1082. if ((task->task_proto == SAS_PROTOCOL_SMP)
  1083. || device_stopping
  1084. || old_request->complete_in_target
  1085. ) {
  1086. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1087. dev_dbg(&isci_host->pdev->dev,
  1088. "%s: SMP request (%d)"
  1089. " or device is stopping (%d)"
  1090. " or complete_in_target (%d), thus no TMF\n",
  1091. __func__, (task->task_proto == SAS_PROTOCOL_SMP),
  1092. device_stopping, old_request->complete_in_target);
  1093. /* Set the state on the task. */
  1094. isci_task_all_done(task);
  1095. ret = TMF_RESP_FUNC_COMPLETE;
  1096. /* Stopping and SMP devices are not sent a TMF, and are not
  1097. * reset, but the outstanding I/O request is terminated below.
  1098. */
  1099. } else {
  1100. /* Fill in the tmf stucture */
  1101. isci_task_build_abort_task_tmf(&tmf, isci_device,
  1102. isci_tmf_ssp_task_abort,
  1103. isci_abort_task_process_cb,
  1104. old_request);
  1105. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1106. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
  1107. ret = isci_task_execute_tmf(isci_host, &tmf,
  1108. ISCI_ABORT_TASK_TIMEOUT_MS);
  1109. if (ret != TMF_RESP_FUNC_COMPLETE)
  1110. dev_err(&isci_host->pdev->dev,
  1111. "%s: isci_task_send_tmf failed\n",
  1112. __func__);
  1113. }
  1114. if (ret == TMF_RESP_FUNC_COMPLETE) {
  1115. old_request->complete_in_target = true;
  1116. /* Clean up the request on our side, and wait for the aborted I/O to
  1117. * complete.
  1118. */
  1119. isci_terminate_request_core(isci_host, isci_device, old_request);
  1120. }
  1121. /* Make sure we do not leave a reference to aborted_io_completion */
  1122. old_request->io_request_completion = NULL;
  1123. return ret;
  1124. }
  1125. /**
  1126. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1127. * functions. This is one of the Task Management functoins called by libsas,
  1128. * to abort all task for the given lun.
  1129. * @d_device: This parameter specifies the domain device associated with this
  1130. * request.
  1131. * @lun: This parameter specifies the lun associated with this request.
  1132. *
  1133. * status, zero indicates success.
  1134. */
  1135. int isci_task_abort_task_set(
  1136. struct domain_device *d_device,
  1137. u8 *lun)
  1138. {
  1139. return TMF_RESP_FUNC_FAILED;
  1140. }
  1141. /**
  1142. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1143. * functions. This is one of the Task Management functoins called by libsas.
  1144. * @d_device: This parameter specifies the domain device associated with this
  1145. * request.
  1146. * @lun: This parameter specifies the lun associated with this request.
  1147. *
  1148. * status, zero indicates success.
  1149. */
  1150. int isci_task_clear_aca(
  1151. struct domain_device *d_device,
  1152. u8 *lun)
  1153. {
  1154. return TMF_RESP_FUNC_FAILED;
  1155. }
  1156. /**
  1157. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1158. * functions. This is one of the Task Management functoins called by libsas.
  1159. * @d_device: This parameter specifies the domain device associated with this
  1160. * request.
  1161. * @lun: This parameter specifies the lun associated with this request.
  1162. *
  1163. * status, zero indicates success.
  1164. */
  1165. int isci_task_clear_task_set(
  1166. struct domain_device *d_device,
  1167. u8 *lun)
  1168. {
  1169. return TMF_RESP_FUNC_FAILED;
  1170. }
  1171. /**
  1172. * isci_task_query_task() - This function is implemented to cause libsas to
  1173. * correctly escalate the failed abort to a LUN or target reset (this is
  1174. * because sas_scsi_find_task libsas function does not correctly interpret
  1175. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1176. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1177. * returned, libsas will turn this into a target reset
  1178. * @task: This parameter specifies the sas task being queried.
  1179. * @lun: This parameter specifies the lun associated with this request.
  1180. *
  1181. * status, zero indicates success.
  1182. */
  1183. int isci_task_query_task(
  1184. struct sas_task *task)
  1185. {
  1186. /* See if there is a pending device reset for this device. */
  1187. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1188. return TMF_RESP_FUNC_FAILED;
  1189. else
  1190. return TMF_RESP_FUNC_SUCC;
  1191. }
  1192. /*
  1193. * isci_task_request_complete() - This function is called by the sci core when
  1194. * an task request completes.
  1195. * @ihost: This parameter specifies the ISCI host object
  1196. * @ireq: This parameter is the completed isci_request object.
  1197. * @completion_status: This parameter specifies the completion status from the
  1198. * sci core.
  1199. *
  1200. * none.
  1201. */
  1202. void
  1203. isci_task_request_complete(struct isci_host *ihost,
  1204. struct isci_request *ireq,
  1205. enum sci_task_status completion_status)
  1206. {
  1207. struct isci_remote_device *idev = ireq->isci_device;
  1208. enum isci_request_status old_state;
  1209. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1210. struct completion *tmf_complete;
  1211. struct scic_sds_request *sci_req = ireq->sci_request_handle;
  1212. dev_dbg(&ihost->pdev->dev,
  1213. "%s: request = %p, status=%d\n",
  1214. __func__, ireq, completion_status);
  1215. old_state = isci_request_change_state(ireq, completed);
  1216. tmf->status = completion_status;
  1217. ireq->complete_in_target = true;
  1218. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1219. memcpy(&tmf->resp.resp_iu,
  1220. &sci_req->ssp.rsp,
  1221. SSP_RESP_IU_MAX_SIZE);
  1222. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1223. memcpy(&tmf->resp.d2h_fis,
  1224. &sci_req->stp.rsp,
  1225. sizeof(struct dev_to_host_fis));
  1226. }
  1227. /* Manage the timer if it is still running. */
  1228. if (tmf->timeout_timer) {
  1229. isci_del_timer(ihost, tmf->timeout_timer);
  1230. tmf->timeout_timer = NULL;
  1231. }
  1232. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1233. tmf_complete = tmf->complete;
  1234. scic_controller_complete_io(&ihost->sci, &idev->sci,
  1235. ireq->sci_request_handle);
  1236. /* NULL the request handle to make sure it cannot be terminated
  1237. * or completed again.
  1238. */
  1239. ireq->sci_request_handle = NULL;
  1240. isci_request_change_state(ireq, unallocated);
  1241. list_del_init(&ireq->dev_node);
  1242. /* The task management part completes last. */
  1243. complete(tmf_complete);
  1244. }
  1245. static int isci_reset_device(struct domain_device *dev, int hard_reset)
  1246. {
  1247. struct isci_remote_device *idev = dev->lldd_dev;
  1248. struct sas_phy *phy = sas_find_local_phy(dev);
  1249. struct isci_host *ihost = dev_to_ihost(dev);
  1250. enum sci_status status;
  1251. unsigned long flags;
  1252. int rc;
  1253. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1254. if (!idev) {
  1255. dev_warn(&ihost->pdev->dev,
  1256. "%s: idev is GONE!\n",
  1257. __func__);
  1258. return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
  1259. }
  1260. spin_lock_irqsave(&ihost->scic_lock, flags);
  1261. status = scic_remote_device_reset(&idev->sci);
  1262. if (status != SCI_SUCCESS) {
  1263. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1264. dev_warn(&ihost->pdev->dev,
  1265. "%s: scic_remote_device_reset(%p) returned %d!\n",
  1266. __func__, idev, status);
  1267. return TMF_RESP_FUNC_FAILED;
  1268. }
  1269. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1270. /* Make sure all pending requests are able to be fully terminated. */
  1271. isci_device_clear_reset_pending(ihost, idev);
  1272. rc = sas_phy_reset(phy, hard_reset);
  1273. msleep(2000); /* just like mvsas */
  1274. /* Terminate in-progress I/O now. */
  1275. isci_remote_device_nuke_requests(ihost, idev);
  1276. spin_lock_irqsave(&ihost->scic_lock, flags);
  1277. status = scic_remote_device_reset_complete(&idev->sci);
  1278. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1279. if (status != SCI_SUCCESS) {
  1280. dev_warn(&ihost->pdev->dev,
  1281. "%s: scic_remote_device_reset_complete(%p) "
  1282. "returned %d!\n", __func__, idev, status);
  1283. }
  1284. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1285. return rc;
  1286. }
  1287. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1288. {
  1289. struct isci_host *ihost = dev_to_ihost(dev);
  1290. int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
  1291. struct isci_remote_device *idev;
  1292. unsigned long flags;
  1293. /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
  1294. * being too paranoid, or is mvsas busted?!
  1295. */
  1296. spin_lock_irqsave(&ihost->scic_lock, flags);
  1297. idev = dev->lldd_dev;
  1298. if (!idev || !test_bit(IDEV_EH, &idev->flags))
  1299. ret = TMF_RESP_FUNC_COMPLETE;
  1300. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1301. if (ret == TMF_RESP_FUNC_COMPLETE)
  1302. return ret;
  1303. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1304. hard_reset = 0;
  1305. return isci_reset_device(dev, hard_reset);
  1306. }
  1307. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1308. {
  1309. struct domain_device *dev = sdev_to_domain_dev(cmd->device);
  1310. int hard_reset = 1;
  1311. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1312. hard_reset = 0;
  1313. return isci_reset_device(dev, hard_reset);
  1314. }