task.c 47 KB

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