task.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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 <scsi/libsas.h>
  59. #include "remote_device.h"
  60. #include "remote_node_context.h"
  61. #include "isci.h"
  62. #include "request.h"
  63. #include "sata.h"
  64. #include "task.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. /**
  482. * isci_request_cleanup_completed_loiterer() - This function will take care of
  483. * the final cleanup on any request which has been explicitly terminated.
  484. * @isci_host: This parameter specifies the ISCI host object
  485. * @isci_device: This is the device to which the request is pending.
  486. * @isci_request: This parameter specifies the terminated request object.
  487. * @task: This parameter is the libsas I/O request.
  488. */
  489. static void isci_request_cleanup_completed_loiterer(
  490. struct isci_host *isci_host,
  491. struct isci_remote_device *isci_device,
  492. struct isci_request *isci_request,
  493. struct sas_task *task)
  494. {
  495. unsigned long flags;
  496. dev_dbg(&isci_host->pdev->dev,
  497. "%s: isci_device=%p, request=%p, task=%p\n",
  498. __func__, isci_device, isci_request, task);
  499. if (task != NULL) {
  500. spin_lock_irqsave(&task->task_state_lock, flags);
  501. task->lldd_task = NULL;
  502. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  503. isci_set_task_doneflags(task);
  504. /* If this task is not in the abort path, call task_done. */
  505. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  506. spin_unlock_irqrestore(&task->task_state_lock, flags);
  507. task->task_done(task);
  508. } else
  509. spin_unlock_irqrestore(&task->task_state_lock, flags);
  510. }
  511. if (isci_request != NULL) {
  512. spin_lock_irqsave(&isci_host->scic_lock, flags);
  513. list_del_init(&isci_request->dev_node);
  514. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  515. isci_request_free(isci_host, isci_request);
  516. }
  517. }
  518. /**
  519. * isci_terminate_request_core() - This function will terminate the given
  520. * request, and wait for it to complete. This function must only be called
  521. * from a thread that can wait. Note that the request is terminated and
  522. * completed (back to the host, if started there).
  523. * @isci_host: This SCU.
  524. * @isci_device: The target.
  525. * @isci_request: The I/O request to be terminated.
  526. *
  527. */
  528. static void isci_terminate_request_core(
  529. struct isci_host *isci_host,
  530. struct isci_remote_device *isci_device,
  531. struct isci_request *isci_request)
  532. {
  533. enum sci_status status = SCI_SUCCESS;
  534. bool was_terminated = false;
  535. bool needs_cleanup_handling = false;
  536. enum isci_request_status request_status;
  537. unsigned long flags;
  538. unsigned long termination_completed = 1;
  539. struct completion *io_request_completion;
  540. struct sas_task *task;
  541. dev_dbg(&isci_host->pdev->dev,
  542. "%s: device = %p; request = %p\n",
  543. __func__, isci_device, isci_request);
  544. spin_lock_irqsave(&isci_host->scic_lock, flags);
  545. io_request_completion = isci_request->io_request_completion;
  546. task = (isci_request->ttype == io_task)
  547. ? isci_request_access_task(isci_request)
  548. : NULL;
  549. /* Note that we are not going to control
  550. * the target to abort the request.
  551. */
  552. isci_request->complete_in_target = true;
  553. /* Make sure the request wasn't just sitting around signalling
  554. * device condition (if the request handle is NULL, then the
  555. * request completed but needed additional handling here).
  556. */
  557. if (!isci_request->terminated) {
  558. was_terminated = true;
  559. needs_cleanup_handling = true;
  560. status = scic_controller_terminate_request(
  561. &isci_host->sci,
  562. &isci_device->sci,
  563. &isci_request->sci);
  564. }
  565. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  566. /*
  567. * The only time the request to terminate will
  568. * fail is when the io request is completed and
  569. * being aborted.
  570. */
  571. if (status != SCI_SUCCESS) {
  572. dev_err(&isci_host->pdev->dev,
  573. "%s: scic_controller_terminate_request"
  574. " returned = 0x%x\n",
  575. __func__, status);
  576. isci_request->io_request_completion = NULL;
  577. } else {
  578. if (was_terminated) {
  579. dev_dbg(&isci_host->pdev->dev,
  580. "%s: before completion wait (%p/%p)\n",
  581. __func__, isci_request, io_request_completion);
  582. /* Wait here for the request to complete. */
  583. #define TERMINATION_TIMEOUT_MSEC 500
  584. termination_completed
  585. = wait_for_completion_timeout(
  586. io_request_completion,
  587. msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
  588. if (!termination_completed) {
  589. /* The request to terminate has timed out. */
  590. spin_lock_irqsave(&isci_host->scic_lock,
  591. flags);
  592. /* Check for state changes. */
  593. if (!isci_request->terminated) {
  594. /* The best we can do is to have the
  595. * request die a silent death if it
  596. * ever really completes.
  597. *
  598. * Set the request state to "dead",
  599. * and clear the task pointer so that
  600. * an actual completion event callback
  601. * doesn't do anything.
  602. */
  603. isci_request->status = dead;
  604. isci_request->io_request_completion
  605. = NULL;
  606. if (isci_request->ttype == io_task) {
  607. /* Break links with the
  608. * sas_task.
  609. */
  610. isci_request->ttype_ptr.io_task_ptr
  611. = NULL;
  612. }
  613. } else
  614. termination_completed = 1;
  615. spin_unlock_irqrestore(&isci_host->scic_lock,
  616. flags);
  617. if (!termination_completed) {
  618. dev_err(&isci_host->pdev->dev,
  619. "%s: *** Timeout waiting for "
  620. "termination(%p/%p)\n",
  621. __func__, io_request_completion,
  622. isci_request);
  623. /* The request can no longer be referenced
  624. * safely since it may go away if the
  625. * termination every really does complete.
  626. */
  627. isci_request = NULL;
  628. }
  629. }
  630. if (termination_completed)
  631. dev_dbg(&isci_host->pdev->dev,
  632. "%s: after completion wait (%p/%p)\n",
  633. __func__, isci_request, io_request_completion);
  634. }
  635. if (termination_completed) {
  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. }
  659. if (needs_cleanup_handling)
  660. isci_request_cleanup_completed_loiterer(
  661. isci_host, isci_device, isci_request, task);
  662. }
  663. }
  664. /**
  665. * isci_terminate_pending_requests() - This function will change the all of the
  666. * requests on the given device's state to "aborting", will terminate the
  667. * requests, and wait for them to complete. This function must only be
  668. * called from a thread that can wait. Note that the requests are all
  669. * terminated and completed (back to the host, if started there).
  670. * @isci_host: This parameter specifies SCU.
  671. * @isci_device: This parameter specifies the target.
  672. *
  673. */
  674. void isci_terminate_pending_requests(struct isci_host *ihost,
  675. struct isci_remote_device *idev)
  676. {
  677. struct completion request_completion;
  678. enum isci_request_status old_state;
  679. unsigned long flags;
  680. LIST_HEAD(list);
  681. spin_lock_irqsave(&ihost->scic_lock, flags);
  682. list_splice_init(&idev->reqs_in_process, &list);
  683. /* assumes that isci_terminate_request_core deletes from the list */
  684. while (!list_empty(&list)) {
  685. struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
  686. /* Change state to "terminating" if it is currently
  687. * "started".
  688. */
  689. old_state = isci_request_change_started_to_newstate(ireq,
  690. &request_completion,
  691. terminating);
  692. switch (old_state) {
  693. case started:
  694. case completed:
  695. case aborting:
  696. break;
  697. default:
  698. /* termination in progress, or otherwise dispositioned.
  699. * We know the request was on 'list' so should be safe
  700. * to move it back to reqs_in_process
  701. */
  702. list_move(&ireq->dev_node, &idev->reqs_in_process);
  703. ireq = NULL;
  704. break;
  705. }
  706. if (!ireq)
  707. continue;
  708. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  709. init_completion(&request_completion);
  710. dev_dbg(&ihost->pdev->dev,
  711. "%s: idev=%p request=%p; task=%p old_state=%d\n",
  712. __func__, idev, ireq,
  713. ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
  714. old_state);
  715. /* If the old_state is started:
  716. * This request was not already being aborted. If it had been,
  717. * then the aborting I/O (ie. the TMF request) would not be in
  718. * the aborting state, and thus would be terminated here. Note
  719. * that since the TMF completion's call to the kernel function
  720. * "complete()" does not happen until the pending I/O request
  721. * terminate fully completes, we do not have to implement a
  722. * special wait here for already aborting requests - the
  723. * termination of the TMF request will force the request
  724. * to finish it's already started terminate.
  725. *
  726. * If old_state == completed:
  727. * This request completed from the SCU hardware perspective
  728. * and now just needs cleaning up in terms of freeing the
  729. * request and potentially calling up to libsas.
  730. *
  731. * If old_state == aborting:
  732. * This request has already gone through a TMF timeout, but may
  733. * not have been terminated; needs cleaning up at least.
  734. */
  735. isci_terminate_request_core(ihost, idev, ireq);
  736. spin_lock_irqsave(&ihost->scic_lock, flags);
  737. }
  738. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  739. }
  740. /**
  741. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  742. * Template functions.
  743. * @lun: This parameter specifies the lun to be reset.
  744. *
  745. * status, zero indicates success.
  746. */
  747. static int isci_task_send_lu_reset_sas(
  748. struct isci_host *isci_host,
  749. struct isci_remote_device *isci_device,
  750. u8 *lun)
  751. {
  752. struct isci_tmf tmf;
  753. int ret = TMF_RESP_FUNC_FAILED;
  754. dev_dbg(&isci_host->pdev->dev,
  755. "%s: isci_host = %p, isci_device = %p\n",
  756. __func__, isci_host, isci_device);
  757. /* Send the LUN reset to the target. By the time the call returns,
  758. * the TMF has fully exected in the target (in which case the return
  759. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  760. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  761. */
  762. isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
  763. NULL);
  764. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  765. ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  766. if (ret == TMF_RESP_FUNC_COMPLETE)
  767. dev_dbg(&isci_host->pdev->dev,
  768. "%s: %p: TMF_LU_RESET passed\n",
  769. __func__, isci_device);
  770. else
  771. dev_dbg(&isci_host->pdev->dev,
  772. "%s: %p: TMF_LU_RESET failed (%x)\n",
  773. __func__, isci_device, ret);
  774. return ret;
  775. }
  776. /**
  777. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  778. * functions. This is one of the Task Management functoins called by libsas,
  779. * to reset the given lun. Note the assumption that while this call is
  780. * executing, no I/O will be sent by the host to the device.
  781. * @lun: This parameter specifies the lun to be reset.
  782. *
  783. * status, zero indicates success.
  784. */
  785. int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
  786. {
  787. struct isci_host *isci_host = dev_to_ihost(domain_device);
  788. struct isci_remote_device *isci_device = NULL;
  789. int ret;
  790. bool device_stopping = false;
  791. isci_device = domain_device->lldd_dev;
  792. dev_dbg(&isci_host->pdev->dev,
  793. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  794. __func__, domain_device, isci_host, isci_device);
  795. if (isci_device != NULL) {
  796. device_stopping = (isci_device->status == isci_stopping)
  797. || (isci_device->status == isci_stopped);
  798. set_bit(IDEV_EH, &isci_device->flags);
  799. }
  800. /* If there is a device reset pending on any request in the
  801. * device's list, fail this LUN reset request in order to
  802. * escalate to the device reset.
  803. */
  804. if (!isci_device || device_stopping ||
  805. isci_device_is_reset_pending(isci_host, isci_device)) {
  806. dev_warn(&isci_host->pdev->dev,
  807. "%s: No dev (%p), or "
  808. "RESET PENDING: domain_device=%p\n",
  809. __func__, isci_device, domain_device);
  810. return TMF_RESP_FUNC_FAILED;
  811. }
  812. /* Send the task management part of the reset. */
  813. if (sas_protocol_ata(domain_device->tproto)) {
  814. ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
  815. } else
  816. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  817. /* If the LUN reset worked, all the I/O can now be terminated. */
  818. if (ret == TMF_RESP_FUNC_COMPLETE)
  819. /* Terminate all I/O now. */
  820. isci_terminate_pending_requests(isci_host,
  821. isci_device);
  822. return ret;
  823. }
  824. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  825. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  826. {
  827. return TMF_RESP_FUNC_FAILED;
  828. }
  829. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  830. {
  831. return TMF_RESP_FUNC_FAILED;
  832. }
  833. /* Task Management Functions. Must be called from process context. */
  834. /**
  835. * isci_abort_task_process_cb() - This is a helper function for the abort task
  836. * TMF command. It manages the request state with respect to the successful
  837. * transmission / completion of the abort task request.
  838. * @cb_state: This parameter specifies when this function was called - after
  839. * the TMF request has been started and after it has timed-out.
  840. * @tmf: This parameter specifies the TMF in progress.
  841. *
  842. *
  843. */
  844. static void isci_abort_task_process_cb(
  845. enum isci_tmf_cb_state cb_state,
  846. struct isci_tmf *tmf,
  847. void *cb_data)
  848. {
  849. struct isci_request *old_request;
  850. old_request = (struct isci_request *)cb_data;
  851. dev_dbg(&old_request->isci_host->pdev->dev,
  852. "%s: tmf=%p, old_request=%p\n",
  853. __func__, tmf, old_request);
  854. switch (cb_state) {
  855. case isci_tmf_started:
  856. /* The TMF has been started. Nothing to do here, since the
  857. * request state was already set to "aborted" by the abort
  858. * task function.
  859. */
  860. if ((old_request->status != aborted)
  861. && (old_request->status != completed))
  862. dev_err(&old_request->isci_host->pdev->dev,
  863. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  864. __func__, old_request->status, tmf, old_request);
  865. break;
  866. case isci_tmf_timed_out:
  867. /* Set the task's state to "aborting", since the abort task
  868. * function thread set it to "aborted" (above) in anticipation
  869. * of the task management request working correctly. Since the
  870. * timeout has now fired, the TMF request failed. We set the
  871. * state such that the request completion will indicate the
  872. * device is no longer present.
  873. */
  874. isci_request_change_state(old_request, aborting);
  875. break;
  876. default:
  877. dev_err(&old_request->isci_host->pdev->dev,
  878. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  879. __func__, cb_state, tmf, old_request);
  880. break;
  881. }
  882. }
  883. /**
  884. * isci_task_abort_task() - This function is one of the SAS Domain Template
  885. * functions. This function is called by libsas to abort a specified task.
  886. * @task: This parameter specifies the SAS task to abort.
  887. *
  888. * status, zero indicates success.
  889. */
  890. int isci_task_abort_task(struct sas_task *task)
  891. {
  892. struct isci_host *isci_host = dev_to_ihost(task->dev);
  893. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  894. struct isci_request *old_request = NULL;
  895. enum isci_request_status old_state;
  896. struct isci_remote_device *isci_device = NULL;
  897. struct isci_tmf tmf;
  898. int ret = TMF_RESP_FUNC_FAILED;
  899. unsigned long flags;
  900. bool any_dev_reset = false;
  901. bool device_stopping;
  902. /* Get the isci_request reference from the task. Note that
  903. * this check does not depend on the pending request list
  904. * in the device, because tasks driving resets may land here
  905. * after completion in the core.
  906. */
  907. old_request = isci_task_get_request_from_task(task, &isci_device);
  908. dev_dbg(&isci_host->pdev->dev,
  909. "%s: task = %p\n", __func__, task);
  910. /* Check if the device has been / is currently being removed.
  911. * If so, no task management will be done, and the I/O will
  912. * be terminated.
  913. */
  914. device_stopping = (isci_device->status == isci_stopping)
  915. || (isci_device->status == isci_stopped);
  916. /* XXX need to fix device lookup lifetime (needs to be done
  917. * under scic_lock, among other things...), but for now assume
  918. * the device is available like the above code
  919. */
  920. set_bit(IDEV_EH, &isci_device->flags);
  921. /* This version of the driver will fail abort requests for
  922. * SATA/STP. Failing the abort request this way will cause the
  923. * SCSI error handler thread to escalate to LUN reset
  924. */
  925. if (sas_protocol_ata(task->task_proto) && !device_stopping) {
  926. dev_warn(&isci_host->pdev->dev,
  927. " task %p is for a STP/SATA device;"
  928. " returning TMF_RESP_FUNC_FAILED\n"
  929. " to cause a LUN reset...\n", task);
  930. return TMF_RESP_FUNC_FAILED;
  931. }
  932. dev_dbg(&isci_host->pdev->dev,
  933. "%s: old_request == %p\n", __func__, old_request);
  934. if (!device_stopping)
  935. any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
  936. spin_lock_irqsave(&task->task_state_lock, flags);
  937. /* Don't do resets to stopping devices. */
  938. if (device_stopping) {
  939. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  940. any_dev_reset = false;
  941. } else /* See if there is a pending device reset for this device. */
  942. any_dev_reset = any_dev_reset
  943. || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
  944. /* If the extraction of the request reference from the task
  945. * failed, then the request has been completed (or if there is a
  946. * pending reset then this abort request function must be failed
  947. * in order to escalate to the target reset).
  948. */
  949. if ((old_request == NULL) || any_dev_reset) {
  950. /* If the device reset task flag is set, fail the task
  951. * management request. Otherwise, the original request
  952. * has completed.
  953. */
  954. if (any_dev_reset) {
  955. /* Turn off the task's DONE to make sure this
  956. * task is escalated to a target reset.
  957. */
  958. task->task_state_flags &= ~SAS_TASK_STATE_DONE;
  959. /* Make the reset happen as soon as possible. */
  960. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  961. spin_unlock_irqrestore(&task->task_state_lock, flags);
  962. /* Fail the task management request in order to
  963. * escalate to the target reset.
  964. */
  965. ret = TMF_RESP_FUNC_FAILED;
  966. dev_dbg(&isci_host->pdev->dev,
  967. "%s: Failing task abort in order to "
  968. "escalate to target reset because\n"
  969. "SAS_TASK_NEED_DEV_RESET is set for "
  970. "task %p on dev %p\n",
  971. __func__, task, isci_device);
  972. } else {
  973. /* The request has already completed and there
  974. * is nothing to do here other than to set the task
  975. * done bit, and indicate that the task abort function
  976. * was sucessful.
  977. */
  978. isci_set_task_doneflags(task);
  979. spin_unlock_irqrestore(&task->task_state_lock, flags);
  980. ret = TMF_RESP_FUNC_COMPLETE;
  981. dev_dbg(&isci_host->pdev->dev,
  982. "%s: abort task not needed for %p\n",
  983. __func__, task);
  984. }
  985. return ret;
  986. }
  987. else
  988. spin_unlock_irqrestore(&task->task_state_lock, flags);
  989. spin_lock_irqsave(&isci_host->scic_lock, flags);
  990. /* Check the request status and change to "aborted" if currently
  991. * "starting"; if true then set the I/O kernel completion
  992. * struct that will be triggered when the request completes.
  993. */
  994. old_state = isci_task_validate_request_to_abort(
  995. old_request, isci_host, isci_device,
  996. &aborted_io_completion);
  997. if ((old_state != started) &&
  998. (old_state != completed) &&
  999. (old_state != aborting)) {
  1000. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1001. /* The request was already being handled by someone else (because
  1002. * they got to set the state away from started).
  1003. */
  1004. dev_dbg(&isci_host->pdev->dev,
  1005. "%s: device = %p; old_request %p already being aborted\n",
  1006. __func__,
  1007. isci_device, old_request);
  1008. return TMF_RESP_FUNC_COMPLETE;
  1009. }
  1010. if ((task->task_proto == SAS_PROTOCOL_SMP)
  1011. || device_stopping
  1012. || old_request->complete_in_target
  1013. ) {
  1014. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1015. dev_dbg(&isci_host->pdev->dev,
  1016. "%s: SMP request (%d)"
  1017. " or device is stopping (%d)"
  1018. " or complete_in_target (%d), thus no TMF\n",
  1019. __func__, (task->task_proto == SAS_PROTOCOL_SMP),
  1020. device_stopping, old_request->complete_in_target);
  1021. /* Set the state on the task. */
  1022. isci_task_all_done(task);
  1023. ret = TMF_RESP_FUNC_COMPLETE;
  1024. /* Stopping and SMP devices are not sent a TMF, and are not
  1025. * reset, but the outstanding I/O request is terminated below.
  1026. */
  1027. } else {
  1028. /* Fill in the tmf stucture */
  1029. isci_task_build_abort_task_tmf(&tmf, isci_device,
  1030. isci_tmf_ssp_task_abort,
  1031. isci_abort_task_process_cb,
  1032. old_request);
  1033. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1034. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
  1035. ret = isci_task_execute_tmf(isci_host, &tmf,
  1036. ISCI_ABORT_TASK_TIMEOUT_MS);
  1037. if (ret != TMF_RESP_FUNC_COMPLETE)
  1038. dev_err(&isci_host->pdev->dev,
  1039. "%s: isci_task_send_tmf failed\n",
  1040. __func__);
  1041. }
  1042. if (ret == TMF_RESP_FUNC_COMPLETE) {
  1043. old_request->complete_in_target = true;
  1044. /* Clean up the request on our side, and wait for the aborted
  1045. * I/O to complete.
  1046. */
  1047. isci_terminate_request_core(isci_host, isci_device, old_request);
  1048. }
  1049. /* Make sure we do not leave a reference to aborted_io_completion */
  1050. old_request->io_request_completion = NULL;
  1051. return ret;
  1052. }
  1053. /**
  1054. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1055. * functions. This is one of the Task Management functoins called by libsas,
  1056. * to abort all task for the given lun.
  1057. * @d_device: This parameter specifies the domain device associated with this
  1058. * request.
  1059. * @lun: This parameter specifies the lun associated with this request.
  1060. *
  1061. * status, zero indicates success.
  1062. */
  1063. int isci_task_abort_task_set(
  1064. struct domain_device *d_device,
  1065. u8 *lun)
  1066. {
  1067. return TMF_RESP_FUNC_FAILED;
  1068. }
  1069. /**
  1070. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1071. * functions. This is one of the Task Management functoins called by libsas.
  1072. * @d_device: This parameter specifies the domain device associated with this
  1073. * request.
  1074. * @lun: This parameter specifies the lun associated with this request.
  1075. *
  1076. * status, zero indicates success.
  1077. */
  1078. int isci_task_clear_aca(
  1079. struct domain_device *d_device,
  1080. u8 *lun)
  1081. {
  1082. return TMF_RESP_FUNC_FAILED;
  1083. }
  1084. /**
  1085. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1086. * functions. This is one of the Task Management functoins called by libsas.
  1087. * @d_device: This parameter specifies the domain device associated with this
  1088. * request.
  1089. * @lun: This parameter specifies the lun associated with this request.
  1090. *
  1091. * status, zero indicates success.
  1092. */
  1093. int isci_task_clear_task_set(
  1094. struct domain_device *d_device,
  1095. u8 *lun)
  1096. {
  1097. return TMF_RESP_FUNC_FAILED;
  1098. }
  1099. /**
  1100. * isci_task_query_task() - This function is implemented to cause libsas to
  1101. * correctly escalate the failed abort to a LUN or target reset (this is
  1102. * because sas_scsi_find_task libsas function does not correctly interpret
  1103. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1104. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1105. * returned, libsas will turn this into a target reset
  1106. * @task: This parameter specifies the sas task being queried.
  1107. * @lun: This parameter specifies the lun associated with this request.
  1108. *
  1109. * status, zero indicates success.
  1110. */
  1111. int isci_task_query_task(
  1112. struct sas_task *task)
  1113. {
  1114. /* See if there is a pending device reset for this device. */
  1115. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1116. return TMF_RESP_FUNC_FAILED;
  1117. else
  1118. return TMF_RESP_FUNC_SUCC;
  1119. }
  1120. /*
  1121. * isci_task_request_complete() - This function is called by the sci core when
  1122. * an task request completes.
  1123. * @ihost: This parameter specifies the ISCI host object
  1124. * @ireq: This parameter is the completed isci_request object.
  1125. * @completion_status: This parameter specifies the completion status from the
  1126. * sci core.
  1127. *
  1128. * none.
  1129. */
  1130. void
  1131. isci_task_request_complete(struct isci_host *ihost,
  1132. struct isci_request *ireq,
  1133. enum sci_task_status completion_status)
  1134. {
  1135. struct isci_remote_device *idev = ireq->isci_device;
  1136. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1137. struct completion *tmf_complete;
  1138. struct scic_sds_request *sci_req = &ireq->sci;
  1139. dev_dbg(&ihost->pdev->dev,
  1140. "%s: request = %p, status=%d\n",
  1141. __func__, ireq, completion_status);
  1142. isci_request_change_state(ireq, completed);
  1143. tmf->status = completion_status;
  1144. ireq->complete_in_target = true;
  1145. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1146. memcpy(&tmf->resp.resp_iu,
  1147. &sci_req->ssp.rsp,
  1148. SSP_RESP_IU_MAX_SIZE);
  1149. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1150. memcpy(&tmf->resp.d2h_fis,
  1151. &sci_req->stp.rsp,
  1152. sizeof(struct dev_to_host_fis));
  1153. }
  1154. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1155. tmf_complete = tmf->complete;
  1156. scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
  1157. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1158. * or completed again.
  1159. */
  1160. ireq->terminated = true;;
  1161. isci_request_change_state(ireq, unallocated);
  1162. list_del_init(&ireq->dev_node);
  1163. /* The task management part completes last. */
  1164. complete(tmf_complete);
  1165. }
  1166. static void isci_smp_task_timedout(unsigned long _task)
  1167. {
  1168. struct sas_task *task = (void *) _task;
  1169. unsigned long flags;
  1170. spin_lock_irqsave(&task->task_state_lock, flags);
  1171. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  1172. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1173. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1174. complete(&task->completion);
  1175. }
  1176. static void isci_smp_task_done(struct sas_task *task)
  1177. {
  1178. if (!del_timer(&task->timer))
  1179. return;
  1180. complete(&task->completion);
  1181. }
  1182. static struct sas_task *isci_alloc_task(void)
  1183. {
  1184. struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
  1185. if (task) {
  1186. INIT_LIST_HEAD(&task->list);
  1187. spin_lock_init(&task->task_state_lock);
  1188. task->task_state_flags = SAS_TASK_STATE_PENDING;
  1189. init_timer(&task->timer);
  1190. init_completion(&task->completion);
  1191. }
  1192. return task;
  1193. }
  1194. static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
  1195. {
  1196. if (task) {
  1197. BUG_ON(!list_empty(&task->list));
  1198. kfree(task);
  1199. }
  1200. }
  1201. static int isci_smp_execute_task(struct isci_host *ihost,
  1202. struct domain_device *dev, void *req,
  1203. int req_size, void *resp, int resp_size)
  1204. {
  1205. int res, retry;
  1206. struct sas_task *task = NULL;
  1207. for (retry = 0; retry < 3; retry++) {
  1208. task = isci_alloc_task();
  1209. if (!task)
  1210. return -ENOMEM;
  1211. task->dev = dev;
  1212. task->task_proto = dev->tproto;
  1213. sg_init_one(&task->smp_task.smp_req, req, req_size);
  1214. sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
  1215. task->task_done = isci_smp_task_done;
  1216. task->timer.data = (unsigned long) task;
  1217. task->timer.function = isci_smp_task_timedout;
  1218. task->timer.expires = jiffies + 10*HZ;
  1219. add_timer(&task->timer);
  1220. res = isci_task_execute_task(task, 1, GFP_KERNEL);
  1221. if (res) {
  1222. del_timer(&task->timer);
  1223. dev_err(&ihost->pdev->dev,
  1224. "%s: executing SMP task failed:%d\n",
  1225. __func__, res);
  1226. goto ex_err;
  1227. }
  1228. wait_for_completion(&task->completion);
  1229. res = -ECOMM;
  1230. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1231. dev_err(&ihost->pdev->dev,
  1232. "%s: smp task timed out or aborted\n",
  1233. __func__);
  1234. isci_task_abort_task(task);
  1235. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1236. dev_err(&ihost->pdev->dev,
  1237. "%s: SMP task aborted and not done\n",
  1238. __func__);
  1239. goto ex_err;
  1240. }
  1241. }
  1242. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1243. task->task_status.stat == SAM_STAT_GOOD) {
  1244. res = 0;
  1245. break;
  1246. }
  1247. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1248. task->task_status.stat == SAS_DATA_UNDERRUN) {
  1249. /* no error, but return the number of bytes of
  1250. * underrun */
  1251. res = task->task_status.residual;
  1252. break;
  1253. }
  1254. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1255. task->task_status.stat == SAS_DATA_OVERRUN) {
  1256. res = -EMSGSIZE;
  1257. break;
  1258. } else {
  1259. dev_err(&ihost->pdev->dev,
  1260. "%s: task to dev %016llx response: 0x%x "
  1261. "status 0x%x\n", __func__,
  1262. SAS_ADDR(dev->sas_addr),
  1263. task->task_status.resp,
  1264. task->task_status.stat);
  1265. isci_free_task(ihost, task);
  1266. task = NULL;
  1267. }
  1268. }
  1269. ex_err:
  1270. BUG_ON(retry == 3 && task != NULL);
  1271. isci_free_task(ihost, task);
  1272. return res;
  1273. }
  1274. #define DISCOVER_REQ_SIZE 16
  1275. #define DISCOVER_RESP_SIZE 56
  1276. int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
  1277. struct domain_device *dev,
  1278. int phy_id, int *adt)
  1279. {
  1280. struct smp_resp *disc_resp;
  1281. u8 *disc_req;
  1282. int res;
  1283. disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
  1284. if (!disc_resp)
  1285. return -ENOMEM;
  1286. disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
  1287. if (disc_req) {
  1288. disc_req[0] = SMP_REQUEST;
  1289. disc_req[1] = SMP_DISCOVER;
  1290. disc_req[9] = phy_id;
  1291. } else {
  1292. kfree(disc_resp);
  1293. return -ENOMEM;
  1294. }
  1295. res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
  1296. disc_resp, DISCOVER_RESP_SIZE);
  1297. if (!res) {
  1298. if (disc_resp->result != SMP_RESP_FUNC_ACC)
  1299. res = disc_resp->result;
  1300. else
  1301. *adt = disc_resp->disc.attached_dev_type;
  1302. }
  1303. kfree(disc_req);
  1304. kfree(disc_resp);
  1305. return res;
  1306. }
  1307. static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
  1308. {
  1309. struct domain_device *dev = idev->domain_dev;
  1310. struct isci_port *iport = idev->isci_port;
  1311. struct isci_host *ihost = iport->isci_host;
  1312. int res, iteration = 0, attached_device_type;
  1313. #define STP_WAIT_MSECS 25000
  1314. unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
  1315. unsigned long deadline = jiffies + tmo;
  1316. enum {
  1317. SMP_PHYWAIT_PHYDOWN,
  1318. SMP_PHYWAIT_PHYUP,
  1319. SMP_PHYWAIT_DONE
  1320. } phy_state = SMP_PHYWAIT_PHYDOWN;
  1321. /* While there is time, wait for the phy to go away and come back */
  1322. while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
  1323. int event = atomic_read(&iport->event);
  1324. ++iteration;
  1325. tmo = wait_event_timeout(ihost->eventq,
  1326. event != atomic_read(&iport->event) ||
  1327. !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
  1328. tmo);
  1329. /* link down, stop polling */
  1330. if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
  1331. break;
  1332. dev_dbg(&ihost->pdev->dev,
  1333. "%s: iport %p, iteration %d,"
  1334. " phase %d: time_remaining %lu, bcns = %d\n",
  1335. __func__, iport, iteration, phy_state,
  1336. tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
  1337. res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
  1338. &attached_device_type);
  1339. tmo = deadline - jiffies;
  1340. if (res) {
  1341. dev_warn(&ihost->pdev->dev,
  1342. "%s: iteration %d, phase %d:"
  1343. " SMP error=%d, time_remaining=%lu\n",
  1344. __func__, iteration, phy_state, res, tmo);
  1345. break;
  1346. }
  1347. dev_dbg(&ihost->pdev->dev,
  1348. "%s: iport %p, iteration %d,"
  1349. " phase %d: time_remaining %lu, bcns = %d, "
  1350. "attdevtype = %x\n",
  1351. __func__, iport, iteration, phy_state,
  1352. tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
  1353. attached_device_type);
  1354. switch (phy_state) {
  1355. case SMP_PHYWAIT_PHYDOWN:
  1356. /* Has the device gone away? */
  1357. if (!attached_device_type)
  1358. phy_state = SMP_PHYWAIT_PHYUP;
  1359. break;
  1360. case SMP_PHYWAIT_PHYUP:
  1361. /* Has the device come back? */
  1362. if (attached_device_type)
  1363. phy_state = SMP_PHYWAIT_DONE;
  1364. break;
  1365. case SMP_PHYWAIT_DONE:
  1366. break;
  1367. }
  1368. }
  1369. dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
  1370. }
  1371. static int isci_reset_device(struct domain_device *dev, int hard_reset)
  1372. {
  1373. struct isci_remote_device *idev = dev->lldd_dev;
  1374. struct sas_phy *phy = sas_find_local_phy(dev);
  1375. struct isci_host *ihost = dev_to_ihost(dev);
  1376. struct isci_port *iport = idev->isci_port;
  1377. enum sci_status status;
  1378. unsigned long flags;
  1379. int rc;
  1380. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1381. if (!idev) {
  1382. dev_warn(&ihost->pdev->dev,
  1383. "%s: idev is GONE!\n",
  1384. __func__);
  1385. return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
  1386. }
  1387. spin_lock_irqsave(&ihost->scic_lock, flags);
  1388. status = scic_remote_device_reset(&idev->sci);
  1389. if (status != SCI_SUCCESS) {
  1390. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1391. dev_warn(&ihost->pdev->dev,
  1392. "%s: scic_remote_device_reset(%p) returned %d!\n",
  1393. __func__, idev, status);
  1394. return TMF_RESP_FUNC_FAILED;
  1395. }
  1396. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1397. /* Make sure all pending requests are able to be fully terminated. */
  1398. isci_device_clear_reset_pending(ihost, idev);
  1399. /* If this is a device on an expander, disable BCN processing. */
  1400. if (!scsi_is_sas_phy_local(phy))
  1401. set_bit(IPORT_BCN_BLOCKED, &iport->flags);
  1402. rc = sas_phy_reset(phy, hard_reset);
  1403. /* Terminate in-progress I/O now. */
  1404. isci_remote_device_nuke_requests(ihost, idev);
  1405. /* Since all pending TCs have been cleaned, resume the RNC. */
  1406. spin_lock_irqsave(&ihost->scic_lock, flags);
  1407. status = scic_remote_device_reset_complete(&idev->sci);
  1408. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1409. /* If this is a device on an expander, bring the phy back up. */
  1410. if (!scsi_is_sas_phy_local(phy)) {
  1411. /* A phy reset will cause the device to go away then reappear.
  1412. * Since libsas will take action on incoming BCNs (eg. remove
  1413. * a device going through an SMP phy-control driven reset),
  1414. * we need to wait until the phy comes back up before letting
  1415. * discovery proceed in libsas.
  1416. */
  1417. isci_wait_for_smp_phy_reset(idev, phy->number);
  1418. spin_lock_irqsave(&ihost->scic_lock, flags);
  1419. isci_port_bcn_enable(ihost, idev->isci_port);
  1420. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1421. }
  1422. if (status != SCI_SUCCESS) {
  1423. dev_warn(&ihost->pdev->dev,
  1424. "%s: scic_remote_device_reset_complete(%p) "
  1425. "returned %d!\n", __func__, idev, status);
  1426. }
  1427. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1428. return rc;
  1429. }
  1430. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1431. {
  1432. struct isci_host *ihost = dev_to_ihost(dev);
  1433. int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
  1434. struct isci_remote_device *idev;
  1435. unsigned long flags;
  1436. /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
  1437. * being too paranoid, or is mvsas busted?!
  1438. */
  1439. spin_lock_irqsave(&ihost->scic_lock, flags);
  1440. idev = dev->lldd_dev;
  1441. if (!idev || !test_bit(IDEV_EH, &idev->flags))
  1442. ret = TMF_RESP_FUNC_COMPLETE;
  1443. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1444. if (ret == TMF_RESP_FUNC_COMPLETE)
  1445. return ret;
  1446. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1447. hard_reset = 0;
  1448. return isci_reset_device(dev, hard_reset);
  1449. }
  1450. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1451. {
  1452. struct domain_device *dev = sdev_to_domain_dev(cmd->device);
  1453. int hard_reset = 1;
  1454. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1455. hard_reset = 0;
  1456. return isci_reset_device(dev, hard_reset);
  1457. }