task.c 50 KB

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