task.c 49 KB

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