task.c 44 KB

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