task.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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 = task->dev->port->ha->lldd_ha;
  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 = isci_dev_from_domain_dev(task->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_host **isci_host,
  506. struct isci_remote_device **isci_device)
  507. {
  508. struct isci_request *request = NULL;
  509. unsigned long flags;
  510. spin_lock_irqsave(&task->task_state_lock, flags);
  511. request = task->lldd_task;
  512. /* If task is already done, the request isn't valid */
  513. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  514. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  515. (request != NULL)) {
  516. if (isci_host != NULL)
  517. *isci_host = request->isci_host;
  518. if (isci_device != NULL)
  519. *isci_device = request->isci_device;
  520. }
  521. spin_unlock_irqrestore(&task->task_state_lock, flags);
  522. return request;
  523. }
  524. /**
  525. * isci_task_validate_request_to_abort() - This function checks the given I/O
  526. * against the "started" state. If the request is still "started", it's
  527. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  528. * BEFORE CALLING THIS FUNCTION.
  529. * @isci_request: This parameter specifies the request object to control.
  530. * @isci_host: This parameter specifies the ISCI host object
  531. * @isci_device: This is the device to which the request is pending.
  532. * @aborted_io_completion: This is a completion structure that will be added to
  533. * the request in case it is changed to aborting; this completion is
  534. * triggered when the request is fully completed.
  535. *
  536. * Either "started" on successful change of the task status to "aborted", or
  537. * "unallocated" if the task cannot be controlled.
  538. */
  539. static enum isci_request_status isci_task_validate_request_to_abort(
  540. struct isci_request *isci_request,
  541. struct isci_host *isci_host,
  542. struct isci_remote_device *isci_device,
  543. struct completion *aborted_io_completion)
  544. {
  545. enum isci_request_status old_state = unallocated;
  546. /* Only abort the task if it's in the
  547. * device's request_in_process list
  548. */
  549. if (isci_request && !list_empty(&isci_request->dev_node)) {
  550. old_state = isci_request_change_started_to_aborted(
  551. isci_request, aborted_io_completion);
  552. }
  553. return old_state;
  554. }
  555. static void isci_request_cleanup_completed_loiterer(
  556. struct isci_host *isci_host,
  557. struct isci_remote_device *isci_device,
  558. struct isci_request *isci_request)
  559. {
  560. struct sas_task *task;
  561. unsigned long flags;
  562. task = (isci_request->ttype == io_task)
  563. ? isci_request_access_task(isci_request)
  564. : NULL;
  565. dev_dbg(&isci_host->pdev->dev,
  566. "%s: isci_device=%p, request=%p, task=%p\n",
  567. __func__, isci_device, isci_request, task);
  568. spin_lock_irqsave(&isci_host->scic_lock, flags);
  569. list_del_init(&isci_request->dev_node);
  570. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  571. if (task != NULL) {
  572. spin_lock_irqsave(&task->task_state_lock, flags);
  573. task->lldd_task = NULL;
  574. isci_set_task_doneflags(task);
  575. /* If this task is not in the abort path, call task_done. */
  576. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  577. spin_unlock_irqrestore(&task->task_state_lock, flags);
  578. task->task_done(task);
  579. } else
  580. spin_unlock_irqrestore(&task->task_state_lock, flags);
  581. }
  582. isci_request_free(isci_host, isci_request);
  583. }
  584. /**
  585. * @isci_termination_timed_out(): this function will deal with a request for
  586. * which the wait for termination has timed-out.
  587. *
  588. * @isci_host This SCU.
  589. * @isci_request The I/O request being terminated.
  590. */
  591. static void
  592. isci_termination_timed_out(
  593. struct isci_host * host,
  594. struct isci_request * request
  595. )
  596. {
  597. unsigned long state_flags;
  598. dev_warn(&host->pdev->dev,
  599. "%s: host = %p; request = %p\n",
  600. __func__, host, request);
  601. /* At this point, the request to terminate
  602. * has timed out. The best we can do is to
  603. * have the request die a silent death
  604. * if it ever completes.
  605. */
  606. spin_lock_irqsave(&request->state_lock, state_flags);
  607. if (request->status == started) {
  608. /* Set the request state to "dead",
  609. * and clear the task pointer so that an actual
  610. * completion event callback doesn't do
  611. * anything.
  612. */
  613. request->status = dead;
  614. /* Clear the timeout completion event pointer.*/
  615. request->io_request_completion = NULL;
  616. if (request->ttype == io_task) {
  617. /* Break links with the sas_task. */
  618. if (request->ttype_ptr.io_task_ptr != NULL) {
  619. request->ttype_ptr.io_task_ptr->lldd_task = NULL;
  620. request->ttype_ptr.io_task_ptr = NULL;
  621. }
  622. }
  623. }
  624. spin_unlock_irqrestore(&request->state_lock, state_flags);
  625. }
  626. /**
  627. * isci_terminate_request_core() - This function will terminate the given
  628. * request, and wait for it to complete. This function must only be called
  629. * from a thread that can wait. Note that the request is terminated and
  630. * completed (back to the host, if started there).
  631. * @isci_host: This SCU.
  632. * @isci_device: The target.
  633. * @isci_request: The I/O request to be terminated.
  634. *
  635. *
  636. */
  637. static void isci_terminate_request_core(
  638. struct isci_host *isci_host,
  639. struct isci_remote_device *isci_device,
  640. struct isci_request *isci_request)
  641. {
  642. enum sci_status status = SCI_SUCCESS;
  643. bool was_terminated = false;
  644. bool needs_cleanup_handling = false;
  645. enum isci_request_status request_status;
  646. unsigned long flags;
  647. unsigned long timeout_remaining;
  648. dev_dbg(&isci_host->pdev->dev,
  649. "%s: device = %p; request = %p\n",
  650. __func__, isci_device, isci_request);
  651. spin_lock_irqsave(&isci_host->scic_lock, flags);
  652. /* Note that we are not going to control
  653. * the target to abort the request.
  654. */
  655. isci_request->complete_in_target = true;
  656. /* Make sure the request wasn't just sitting around signalling
  657. * device condition (if the request handle is NULL, then the
  658. * request completed but needed additional handling here).
  659. */
  660. if (isci_request->sci_request_handle != NULL) {
  661. was_terminated = true;
  662. needs_cleanup_handling = true;
  663. status = scic_controller_terminate_request(
  664. isci_host->core_controller,
  665. to_sci_dev(isci_device),
  666. isci_request->sci_request_handle
  667. );
  668. }
  669. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  670. /*
  671. * The only time the request to terminate will
  672. * fail is when the io request is completed and
  673. * being aborted.
  674. */
  675. if (status != SCI_SUCCESS) {
  676. dev_err(&isci_host->pdev->dev,
  677. "%s: scic_controller_terminate_request"
  678. " returned = 0x%x\n",
  679. __func__,
  680. status);
  681. /* Clear the completion pointer from the request. */
  682. isci_request->io_request_completion = NULL;
  683. } else {
  684. if (was_terminated) {
  685. dev_dbg(&isci_host->pdev->dev,
  686. "%s: before completion wait (%p)\n",
  687. __func__,
  688. isci_request->io_request_completion);
  689. /* Wait here for the request to complete. */
  690. #define TERMINATION_TIMEOUT_MSEC 50
  691. timeout_remaining
  692. = wait_for_completion_timeout(
  693. isci_request->io_request_completion,
  694. msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
  695. if (!timeout_remaining) {
  696. isci_termination_timed_out(isci_host,
  697. isci_request);
  698. dev_err(&isci_host->pdev->dev,
  699. "%s: *** Timeout waiting for "
  700. "termination(%p/%p)\n",
  701. __func__,
  702. isci_request->io_request_completion,
  703. isci_request);
  704. } else
  705. dev_dbg(&isci_host->pdev->dev,
  706. "%s: after completion wait (%p)\n",
  707. __func__,
  708. isci_request->io_request_completion);
  709. }
  710. /* Clear the completion pointer from the request. */
  711. isci_request->io_request_completion = NULL;
  712. /* Peek at the status of the request. This will tell
  713. * us if there was special handling on the request such that it
  714. * needs to be detached and freed here.
  715. */
  716. spin_lock_irqsave(&isci_request->state_lock, flags);
  717. request_status = isci_request_get_state(isci_request);
  718. if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
  719. && ((request_status == aborted)
  720. || (request_status == aborting)
  721. || (request_status == terminating)
  722. || (request_status == completed)
  723. || (request_status == dead)
  724. )
  725. ) {
  726. /* The completion routine won't free a request in
  727. * the aborted/aborting/etc. states, so we do
  728. * it here.
  729. */
  730. needs_cleanup_handling = true;
  731. }
  732. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  733. if (needs_cleanup_handling)
  734. isci_request_cleanup_completed_loiterer(
  735. isci_host, isci_device, isci_request
  736. );
  737. }
  738. }
  739. static void isci_terminate_request(
  740. struct isci_host *isci_host,
  741. struct isci_remote_device *isci_device,
  742. struct isci_request *isci_request,
  743. enum isci_request_status new_request_state)
  744. {
  745. enum isci_request_status old_state;
  746. DECLARE_COMPLETION_ONSTACK(request_completion);
  747. /* Change state to "new_request_state" if it is currently "started" */
  748. old_state = isci_request_change_started_to_newstate(
  749. isci_request,
  750. &request_completion,
  751. new_request_state
  752. );
  753. if ((old_state == started) ||
  754. (old_state == completed) ||
  755. (old_state == aborting)) {
  756. /* If the old_state is started:
  757. * This request was not already being aborted. If it had been,
  758. * then the aborting I/O (ie. the TMF request) would not be in
  759. * the aborting state, and thus would be terminated here. Note
  760. * that since the TMF completion's call to the kernel function
  761. * "complete()" does not happen until the pending I/O request
  762. * terminate fully completes, we do not have to implement a
  763. * special wait here for already aborting requests - the
  764. * termination of the TMF request will force the request
  765. * to finish it's already started terminate.
  766. *
  767. * If old_state == completed:
  768. * This request completed from the SCU hardware perspective
  769. * and now just needs cleaning up in terms of freeing the
  770. * request and potentially calling up to libsas.
  771. *
  772. * If old_state == aborting:
  773. * This request has already gone through a TMF timeout, but may
  774. * not have been terminated; needs cleaning up at least.
  775. */
  776. isci_terminate_request_core(isci_host, isci_device,
  777. isci_request);
  778. }
  779. }
  780. /**
  781. * isci_terminate_pending_requests() - This function will change the all of the
  782. * requests on the given device's state to "aborting", will terminate the
  783. * requests, and wait for them to complete. This function must only be
  784. * called from a thread that can wait. Note that the requests are all
  785. * terminated and completed (back to the host, if started there).
  786. * @isci_host: This parameter specifies SCU.
  787. * @isci_device: This parameter specifies the target.
  788. *
  789. *
  790. */
  791. void isci_terminate_pending_requests(
  792. struct isci_host *isci_host,
  793. struct isci_remote_device *isci_device,
  794. enum isci_request_status new_request_state)
  795. {
  796. struct isci_request *request;
  797. struct isci_request *next_request;
  798. unsigned long flags;
  799. struct list_head aborted_request_list;
  800. INIT_LIST_HEAD(&aborted_request_list);
  801. dev_dbg(&isci_host->pdev->dev,
  802. "%s: isci_device = %p (new request state = %d)\n",
  803. __func__, isci_device, new_request_state);
  804. spin_lock_irqsave(&isci_host->scic_lock, flags);
  805. /* Move all of the pending requests off of the device list. */
  806. list_splice_init(&isci_device->reqs_in_process,
  807. &aborted_request_list);
  808. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  809. /* Iterate through the now-local list. */
  810. list_for_each_entry_safe(request, next_request,
  811. &aborted_request_list, dev_node) {
  812. dev_warn(&isci_host->pdev->dev,
  813. "%s: isci_device=%p request=%p; task=%p\n",
  814. __func__,
  815. isci_device, request,
  816. ((request->ttype == io_task)
  817. ? isci_request_access_task(request)
  818. : NULL));
  819. /* Mark all still pending I/O with the selected next
  820. * state, terminate and free it.
  821. */
  822. isci_terminate_request(isci_host, isci_device,
  823. request, new_request_state
  824. );
  825. }
  826. }
  827. /**
  828. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  829. * Template functions.
  830. * @lun: This parameter specifies the lun to be reset.
  831. *
  832. * status, zero indicates success.
  833. */
  834. static int isci_task_send_lu_reset_sas(
  835. struct isci_host *isci_host,
  836. struct isci_remote_device *isci_device,
  837. u8 *lun)
  838. {
  839. struct isci_tmf tmf;
  840. int ret = TMF_RESP_FUNC_FAILED;
  841. dev_dbg(&isci_host->pdev->dev,
  842. "%s: isci_host = %p, isci_device = %p\n",
  843. __func__, isci_host, isci_device);
  844. /* Send the LUN reset to the target. By the time the call returns,
  845. * the TMF has fully exected in the target (in which case the return
  846. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  847. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  848. */
  849. isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
  850. NULL);
  851. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  852. ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  853. if (ret == TMF_RESP_FUNC_COMPLETE)
  854. dev_dbg(&isci_host->pdev->dev,
  855. "%s: %p: TMF_LU_RESET passed\n",
  856. __func__, isci_device);
  857. else
  858. dev_dbg(&isci_host->pdev->dev,
  859. "%s: %p: TMF_LU_RESET failed (%x)\n",
  860. __func__, isci_device, ret);
  861. return ret;
  862. }
  863. /**
  864. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  865. * functions. This is one of the Task Management functoins called by libsas,
  866. * to reset the given lun. Note the assumption that while this call is
  867. * executing, no I/O will be sent by the host to the device.
  868. * @lun: This parameter specifies the lun to be reset.
  869. *
  870. * status, zero indicates success.
  871. */
  872. int isci_task_lu_reset(
  873. struct domain_device *domain_device,
  874. u8 *lun)
  875. {
  876. struct isci_host *isci_host = NULL;
  877. struct isci_remote_device *isci_device = NULL;
  878. int ret;
  879. bool device_stopping = false;
  880. if (domain_device == NULL) {
  881. pr_warn("%s: domain_device == NULL\n", __func__);
  882. return TMF_RESP_FUNC_FAILED;
  883. }
  884. isci_device = isci_dev_from_domain_dev(domain_device);
  885. if (domain_device->port != NULL)
  886. isci_host = isci_host_from_sas_ha(domain_device->port->ha);
  887. pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  888. __func__, domain_device, isci_host, isci_device);
  889. if (isci_device != NULL)
  890. device_stopping = (isci_device->status == isci_stopping)
  891. || (isci_device->status == isci_stopped);
  892. /* If there is a device reset pending on any request in the
  893. * device's list, fail this LUN reset request in order to
  894. * escalate to the device reset.
  895. */
  896. if ((isci_device == NULL) ||
  897. (isci_host == NULL) ||
  898. ((isci_host != NULL) &&
  899. (isci_device != NULL) &&
  900. (device_stopping ||
  901. (isci_device_is_reset_pending(isci_host, isci_device))))) {
  902. dev_warn(&isci_host->pdev->dev,
  903. "%s: No dev (%p), no host (%p), or "
  904. "RESET PENDING: domain_device=%p\n",
  905. __func__, isci_device, isci_host, domain_device);
  906. return TMF_RESP_FUNC_FAILED;
  907. }
  908. /* Send the task management part of the reset. */
  909. if (sas_protocol_ata(domain_device->tproto)) {
  910. ret = isci_task_send_lu_reset_sata(
  911. isci_host, isci_device, lun
  912. );
  913. } else
  914. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  915. /* If the LUN reset worked, all the I/O can now be terminated. */
  916. if (ret == TMF_RESP_FUNC_COMPLETE)
  917. /* Terminate all I/O now. */
  918. isci_terminate_pending_requests(isci_host,
  919. isci_device,
  920. terminating);
  921. return ret;
  922. }
  923. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  924. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  925. {
  926. return TMF_RESP_FUNC_FAILED;
  927. }
  928. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  929. {
  930. return TMF_RESP_FUNC_FAILED;
  931. }
  932. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  933. {
  934. return TMF_RESP_FUNC_FAILED;
  935. }
  936. /* Task Management Functions. Must be called from process context. */
  937. /**
  938. * isci_abort_task_process_cb() - This is a helper function for the abort task
  939. * TMF command. It manages the request state with respect to the successful
  940. * transmission / completion of the abort task request.
  941. * @cb_state: This parameter specifies when this function was called - after
  942. * the TMF request has been started and after it has timed-out.
  943. * @tmf: This parameter specifies the TMF in progress.
  944. *
  945. *
  946. */
  947. static void isci_abort_task_process_cb(
  948. enum isci_tmf_cb_state cb_state,
  949. struct isci_tmf *tmf,
  950. void *cb_data)
  951. {
  952. struct isci_request *old_request;
  953. old_request = (struct isci_request *)cb_data;
  954. dev_dbg(&old_request->isci_host->pdev->dev,
  955. "%s: tmf=%p, old_request=%p\n",
  956. __func__, tmf, old_request);
  957. switch (cb_state) {
  958. case isci_tmf_started:
  959. /* The TMF has been started. Nothing to do here, since the
  960. * request state was already set to "aborted" by the abort
  961. * task function.
  962. */
  963. BUG_ON((old_request->status != aborted)
  964. && (old_request->status != completed));
  965. break;
  966. case isci_tmf_timed_out:
  967. /* Set the task's state to "aborting", since the abort task
  968. * function thread set it to "aborted" (above) in anticipation
  969. * of the task management request working correctly. Since the
  970. * timeout has now fired, the TMF request failed. We set the
  971. * state such that the request completion will indicate the
  972. * device is no longer present.
  973. */
  974. isci_request_change_state(old_request, aborting);
  975. break;
  976. default:
  977. dev_err(&old_request->isci_host->pdev->dev,
  978. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  979. __func__, cb_state, tmf, old_request);
  980. break;
  981. }
  982. }
  983. /**
  984. * isci_task_abort_task() - This function is one of the SAS Domain Template
  985. * functions. This function is called by libsas to abort a specified task.
  986. * @task: This parameter specifies the SAS task to abort.
  987. *
  988. * status, zero indicates success.
  989. */
  990. int isci_task_abort_task(struct sas_task *task)
  991. {
  992. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  993. struct isci_request *old_request = NULL;
  994. enum isci_request_status old_state;
  995. struct isci_remote_device *isci_device = NULL;
  996. struct isci_host *isci_host = NULL;
  997. struct isci_tmf tmf;
  998. int ret = TMF_RESP_FUNC_FAILED;
  999. unsigned long flags;
  1000. bool any_dev_reset = false;
  1001. bool device_stopping;
  1002. /* Get the isci_request reference from the task. Note that
  1003. * this check does not depend on the pending request list
  1004. * in the device, because tasks driving resets may land here
  1005. * after completion in the core.
  1006. */
  1007. old_request = isci_task_get_request_from_task(task, &isci_host,
  1008. &isci_device);
  1009. dev_dbg(&isci_host->pdev->dev,
  1010. "%s: task = %p\n", __func__, task);
  1011. /* Check if the device has been / is currently being removed.
  1012. * If so, no task management will be done, and the I/O will
  1013. * be terminated.
  1014. */
  1015. device_stopping = (isci_device->status == isci_stopping)
  1016. || (isci_device->status == isci_stopped);
  1017. /* This version of the driver will fail abort requests for
  1018. * SATA/STP. Failing the abort request this way will cause the
  1019. * SCSI error handler thread to escalate to LUN reset
  1020. */
  1021. if (sas_protocol_ata(task->task_proto) && !device_stopping) {
  1022. dev_warn(&isci_host->pdev->dev,
  1023. " task %p is for a STP/SATA device;"
  1024. " returning TMF_RESP_FUNC_FAILED\n"
  1025. " to cause a LUN reset...\n", task);
  1026. return TMF_RESP_FUNC_FAILED;
  1027. }
  1028. dev_dbg(&isci_host->pdev->dev,
  1029. "%s: old_request == %p\n", __func__, old_request);
  1030. if (!device_stopping)
  1031. any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
  1032. spin_lock_irqsave(&task->task_state_lock, flags);
  1033. /* Don't do resets to stopping devices. */
  1034. if (device_stopping) {
  1035. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1036. any_dev_reset = false;
  1037. } else /* See if there is a pending device reset for this device. */
  1038. any_dev_reset = any_dev_reset
  1039. || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
  1040. /* If the extraction of the request reference from the task
  1041. * failed, then the request has been completed (or if there is a
  1042. * pending reset then this abort request function must be failed
  1043. * in order to escalate to the target reset).
  1044. */
  1045. if ((old_request == NULL) || any_dev_reset) {
  1046. /* If the device reset task flag is set, fail the task
  1047. * management request. Otherwise, the original request
  1048. * has completed.
  1049. */
  1050. if (any_dev_reset) {
  1051. /* Turn off the task's DONE to make sure this
  1052. * task is escalated to a target reset.
  1053. */
  1054. task->task_state_flags &= ~SAS_TASK_STATE_DONE;
  1055. /* Make the reset happen as soon as possible. */
  1056. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  1057. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1058. /* Fail the task management request in order to
  1059. * escalate to the target reset.
  1060. */
  1061. ret = TMF_RESP_FUNC_FAILED;
  1062. dev_dbg(&isci_host->pdev->dev,
  1063. "%s: Failing task abort in order to "
  1064. "escalate to target reset because\n"
  1065. "SAS_TASK_NEED_DEV_RESET is set for "
  1066. "task %p on dev %p\n",
  1067. __func__, task, isci_device);
  1068. } else {
  1069. /* The request has already completed and there
  1070. * is nothing to do here other than to set the task
  1071. * done bit, and indicate that the task abort function
  1072. * was sucessful.
  1073. */
  1074. isci_set_task_doneflags(task);
  1075. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1076. ret = TMF_RESP_FUNC_COMPLETE;
  1077. dev_dbg(&isci_host->pdev->dev,
  1078. "%s: abort task not needed for %p\n",
  1079. __func__, task);
  1080. }
  1081. return ret;
  1082. }
  1083. else
  1084. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1085. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1086. /* Check the request status and change to "aborted" if currently
  1087. * "starting"; if true then set the I/O kernel completion
  1088. * struct that will be triggered when the request completes.
  1089. */
  1090. old_state = isci_task_validate_request_to_abort(
  1091. old_request, isci_host, isci_device,
  1092. &aborted_io_completion);
  1093. if ((old_state != started) &&
  1094. (old_state != completed) &&
  1095. (old_state != aborting)) {
  1096. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1097. /* The request was already being handled by someone else (because
  1098. * they got to set the state away from started).
  1099. */
  1100. dev_dbg(&isci_host->pdev->dev,
  1101. "%s: device = %p; old_request %p already being aborted\n",
  1102. __func__,
  1103. isci_device, old_request);
  1104. return TMF_RESP_FUNC_COMPLETE;
  1105. }
  1106. if ((task->task_proto == SAS_PROTOCOL_SMP)
  1107. || device_stopping
  1108. || old_request->complete_in_target
  1109. ) {
  1110. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1111. dev_dbg(&isci_host->pdev->dev,
  1112. "%s: SMP request (%d)"
  1113. " or device is stopping (%d)"
  1114. " or complete_in_target (%d), thus no TMF\n",
  1115. __func__, (task->task_proto == SAS_PROTOCOL_SMP),
  1116. device_stopping, old_request->complete_in_target);
  1117. /* Set the state on the task. */
  1118. isci_task_all_done(task);
  1119. ret = TMF_RESP_FUNC_COMPLETE;
  1120. /* Stopping and SMP devices are not sent a TMF, and are not
  1121. * reset, but the outstanding I/O request is terminated below.
  1122. */
  1123. } else {
  1124. /* Fill in the tmf stucture */
  1125. isci_task_build_abort_task_tmf(&tmf, isci_device,
  1126. isci_tmf_ssp_task_abort,
  1127. isci_abort_task_process_cb,
  1128. old_request);
  1129. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1130. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
  1131. ret = isci_task_execute_tmf(isci_host, &tmf,
  1132. ISCI_ABORT_TASK_TIMEOUT_MS);
  1133. if (ret != TMF_RESP_FUNC_COMPLETE)
  1134. dev_err(&isci_host->pdev->dev,
  1135. "%s: isci_task_send_tmf failed\n",
  1136. __func__);
  1137. }
  1138. if (ret == TMF_RESP_FUNC_COMPLETE) {
  1139. old_request->complete_in_target = true;
  1140. /* Clean up the request on our side, and wait for the aborted I/O to
  1141. * complete.
  1142. */
  1143. isci_terminate_request_core(isci_host, isci_device, old_request);
  1144. }
  1145. /* Make sure we do not leave a reference to aborted_io_completion */
  1146. old_request->io_request_completion = NULL;
  1147. return ret;
  1148. }
  1149. /**
  1150. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1151. * functions. This is one of the Task Management functoins called by libsas,
  1152. * to abort all task for the given lun.
  1153. * @d_device: This parameter specifies the domain device associated with this
  1154. * request.
  1155. * @lun: This parameter specifies the lun associated with this request.
  1156. *
  1157. * status, zero indicates success.
  1158. */
  1159. int isci_task_abort_task_set(
  1160. struct domain_device *d_device,
  1161. u8 *lun)
  1162. {
  1163. return TMF_RESP_FUNC_FAILED;
  1164. }
  1165. /**
  1166. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1167. * functions. This is one of the Task Management functoins called by libsas.
  1168. * @d_device: This parameter specifies the domain device associated with this
  1169. * request.
  1170. * @lun: This parameter specifies the lun associated with this request.
  1171. *
  1172. * status, zero indicates success.
  1173. */
  1174. int isci_task_clear_aca(
  1175. struct domain_device *d_device,
  1176. u8 *lun)
  1177. {
  1178. return TMF_RESP_FUNC_FAILED;
  1179. }
  1180. /**
  1181. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1182. * functions. This is one of the Task Management functoins called by libsas.
  1183. * @d_device: This parameter specifies the domain device associated with this
  1184. * request.
  1185. * @lun: This parameter specifies the lun associated with this request.
  1186. *
  1187. * status, zero indicates success.
  1188. */
  1189. int isci_task_clear_task_set(
  1190. struct domain_device *d_device,
  1191. u8 *lun)
  1192. {
  1193. return TMF_RESP_FUNC_FAILED;
  1194. }
  1195. /**
  1196. * isci_task_query_task() - This function is implemented to cause libsas to
  1197. * correctly escalate the failed abort to a LUN or target reset (this is
  1198. * because sas_scsi_find_task libsas function does not correctly interpret
  1199. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1200. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1201. * returned, libsas will turn this into a target reset
  1202. * @task: This parameter specifies the sas task being queried.
  1203. * @lun: This parameter specifies the lun associated with this request.
  1204. *
  1205. * status, zero indicates success.
  1206. */
  1207. int isci_task_query_task(
  1208. struct sas_task *task)
  1209. {
  1210. /* See if there is a pending device reset for this device. */
  1211. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1212. return TMF_RESP_FUNC_FAILED;
  1213. else
  1214. return TMF_RESP_FUNC_SUCC;
  1215. }
  1216. /**
  1217. * isci_task_request_complete() - This function is called by the sci core when
  1218. * an task request completes.
  1219. * @isci_host: This parameter specifies the ISCI host object
  1220. * @request: This parameter is the completed isci_request object.
  1221. * @completion_status: This parameter specifies the completion status from the
  1222. * sci core.
  1223. *
  1224. * none.
  1225. */
  1226. void isci_task_request_complete(
  1227. struct isci_host *isci_host,
  1228. struct isci_request *request,
  1229. enum sci_task_status completion_status)
  1230. {
  1231. struct isci_remote_device *isci_device = request->isci_device;
  1232. enum isci_request_status old_state;
  1233. struct isci_tmf *tmf = isci_request_access_tmf(request);
  1234. struct completion *tmf_complete;
  1235. dev_dbg(&isci_host->pdev->dev,
  1236. "%s: request = %p, status=%d\n",
  1237. __func__, request, completion_status);
  1238. old_state = isci_request_change_state(request, completed);
  1239. tmf->status = completion_status;
  1240. request->complete_in_target = true;
  1241. if (SAS_PROTOCOL_SSP == tmf->proto) {
  1242. memcpy(&tmf->resp.resp_iu,
  1243. scic_io_request_get_response_iu_address(
  1244. request->sci_request_handle
  1245. ),
  1246. sizeof(struct sci_ssp_response_iu));
  1247. } else if (SAS_PROTOCOL_SATA == tmf->proto) {
  1248. memcpy(&tmf->resp.d2h_fis,
  1249. scic_stp_io_request_get_d2h_reg_address(
  1250. request->sci_request_handle
  1251. ),
  1252. sizeof(struct sata_fis_reg_d2h)
  1253. );
  1254. }
  1255. /* Manage the timer if it is still running. */
  1256. if (tmf->timeout_timer) {
  1257. isci_del_timer(isci_host, tmf->timeout_timer);
  1258. tmf->timeout_timer = NULL;
  1259. }
  1260. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1261. tmf_complete = tmf->complete;
  1262. scic_controller_complete_task(
  1263. isci_host->core_controller,
  1264. to_sci_dev(isci_device),
  1265. request->sci_request_handle
  1266. );
  1267. /* NULL the request handle to make sure it cannot be terminated
  1268. * or completed again.
  1269. */
  1270. request->sci_request_handle = NULL;
  1271. isci_request_change_state(request, unallocated);
  1272. list_del_init(&request->dev_node);
  1273. /* The task management part completes last. */
  1274. complete(tmf_complete);
  1275. }
  1276. /**
  1277. * isci_task_ssp_request_get_lun() - This function is called by the sci core to
  1278. * retrieve the lun for a given task request.
  1279. * @request: This parameter is the isci_request object.
  1280. *
  1281. * lun for specified task request.
  1282. */
  1283. /**
  1284. * isci_task_ssp_request_get_function() - This function is called by the sci
  1285. * core to retrieve the function for a given task request.
  1286. * @request: This parameter is the isci_request object.
  1287. *
  1288. * function code for specified task request.
  1289. */
  1290. u8 isci_task_ssp_request_get_function(struct isci_request *request)
  1291. {
  1292. struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
  1293. dev_dbg(&request->isci_host->pdev->dev,
  1294. "%s: func = %d\n", __func__, isci_tmf->tmf_code);
  1295. return isci_tmf->tmf_code;
  1296. }
  1297. /**
  1298. * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
  1299. * the sci core to retrieve the io tag for a given task request.
  1300. * @request: This parameter is the isci_request object.
  1301. *
  1302. * io tag for specified task request.
  1303. */
  1304. u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
  1305. {
  1306. u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
  1307. if (tmf_task == request->ttype) {
  1308. struct isci_tmf *tmf = isci_request_access_tmf(request);
  1309. io_tag = tmf->io_tag;
  1310. }
  1311. dev_dbg(&request->isci_host->pdev->dev,
  1312. "%s: request = %p, io_tag = %d\n",
  1313. __func__, request, io_tag);
  1314. return io_tag;
  1315. }
  1316. /**
  1317. * isci_task_ssp_request_get_response_data_address() - This function is called
  1318. * by the sci core to retrieve the response data address for a given task
  1319. * request.
  1320. * @request: This parameter is the isci_request object.
  1321. *
  1322. * response data address for specified task request.
  1323. */
  1324. void *isci_task_ssp_request_get_response_data_address(
  1325. struct isci_request *request)
  1326. {
  1327. struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
  1328. return &isci_tmf->resp.resp_iu;
  1329. }
  1330. /**
  1331. * isci_task_ssp_request_get_response_data_length() - This function is called
  1332. * by the sci core to retrieve the response data length for a given task
  1333. * request.
  1334. * @request: This parameter is the isci_request object.
  1335. *
  1336. * response data length for specified task request.
  1337. */
  1338. u32 isci_task_ssp_request_get_response_data_length(
  1339. struct isci_request *request)
  1340. {
  1341. struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
  1342. return sizeof(isci_tmf->resp.resp_iu);
  1343. }
  1344. /**
  1345. * isci_bus_reset_handler() - This function performs a target reset of the
  1346. * device referenced by "cmd'. This function is exported through the
  1347. * "struct scsi_host_template" structure such that it is called when an I/O
  1348. * recovery process has escalated to a target reset. Note that this function
  1349. * is called from the scsi error handler event thread, so may block on calls.
  1350. * @scsi_cmd: This parameter specifies the target to be reset.
  1351. *
  1352. * SUCCESS if the reset process was successful, else FAILED.
  1353. */
  1354. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1355. {
  1356. unsigned long flags = 0;
  1357. struct isci_host *isci_host = NULL;
  1358. enum sci_status status;
  1359. int base_status;
  1360. struct isci_remote_device *isci_dev
  1361. = isci_dev_from_domain_dev(
  1362. sdev_to_domain_dev(cmd->device));
  1363. dev_dbg(&cmd->device->sdev_gendev,
  1364. "%s: cmd %p, isci_dev %p\n",
  1365. __func__, cmd, isci_dev);
  1366. if (!isci_dev) {
  1367. dev_warn(&cmd->device->sdev_gendev,
  1368. "%s: isci_dev is GONE!\n",
  1369. __func__);
  1370. return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
  1371. }
  1372. if (isci_dev->isci_port != NULL)
  1373. isci_host = isci_dev->isci_port->isci_host;
  1374. if (isci_host != NULL)
  1375. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1376. status = scic_remote_device_reset(to_sci_dev(isci_dev));
  1377. if (status != SCI_SUCCESS) {
  1378. if (isci_host != NULL)
  1379. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1380. scmd_printk(KERN_WARNING, cmd,
  1381. "%s: scic_remote_device_reset(%p) returned %d!\n",
  1382. __func__, isci_dev, status);
  1383. return TMF_RESP_FUNC_FAILED;
  1384. }
  1385. if (isci_host != NULL)
  1386. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1387. /* Make sure all pending requests are able to be fully terminated. */
  1388. isci_device_clear_reset_pending(isci_dev);
  1389. /* Terminate in-progress I/O now. */
  1390. isci_remote_device_nuke_requests(isci_dev);
  1391. /* Call into the libsas default handler (which calls sas_phy_reset). */
  1392. base_status = sas_eh_bus_reset_handler(cmd);
  1393. if (base_status != SUCCESS) {
  1394. /* There can be cases where the resets to individual devices
  1395. * behind an expander will fail because of an unplug of the
  1396. * expander itself.
  1397. */
  1398. scmd_printk(KERN_WARNING, cmd,
  1399. "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
  1400. __func__, cmd, base_status);
  1401. }
  1402. /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
  1403. if (isci_host != NULL)
  1404. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1405. status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
  1406. if (isci_host != NULL)
  1407. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1408. if (status != SCI_SUCCESS) {
  1409. scmd_printk(KERN_WARNING, cmd,
  1410. "%s: scic_remote_device_reset_complete(%p) "
  1411. "returned %d!\n",
  1412. __func__, isci_dev, status);
  1413. }
  1414. /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
  1415. dev_dbg(&cmd->device->sdev_gendev,
  1416. "%s: cmd %p, isci_dev %p complete.\n",
  1417. __func__, cmd, isci_dev);
  1418. return TMF_RESP_FUNC_COMPLETE;
  1419. }