task.c 48 KB

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