task.c 48 KB

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