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