task.c 46 KB

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