task.c 48 KB

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