task.c 48 KB

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