task.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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 "remote_device.h"
  59. #include "remote_node_context.h"
  60. #include "isci.h"
  61. #include "request.h"
  62. #include "sata.h"
  63. #include "task.h"
  64. #include "timers.h"
  65. /**
  66. * isci_task_refuse() - complete the request to the upper layer driver in
  67. * the case where an I/O needs to be completed back in the submit path.
  68. * @ihost: host on which the the request was queued
  69. * @task: request to complete
  70. * @response: response code for the completed task.
  71. * @status: status code for the completed task.
  72. *
  73. */
  74. static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
  75. enum service_response response,
  76. enum exec_status status)
  77. {
  78. enum isci_completion_selection disposition;
  79. disposition = isci_perform_normal_io_completion;
  80. disposition = isci_task_set_completion_status(task, response, status,
  81. disposition);
  82. /* Tasks aborted specifically by a call to the lldd_abort_task
  83. * function should not be completed to the host in the regular path.
  84. */
  85. switch (disposition) {
  86. case isci_perform_normal_io_completion:
  87. /* Normal notification (task_done) */
  88. dev_dbg(&ihost->pdev->dev,
  89. "%s: Normal - task = %p, response=%d, "
  90. "status=%d\n",
  91. __func__, task, response, status);
  92. task->lldd_task = NULL;
  93. isci_execpath_callback(ihost, task, task->task_done);
  94. break;
  95. case isci_perform_aborted_io_completion:
  96. /* No notification because this request is already in the
  97. * abort path.
  98. */
  99. dev_warn(&ihost->pdev->dev,
  100. "%s: Aborted - task = %p, response=%d, "
  101. "status=%d\n",
  102. __func__, task, response, status);
  103. break;
  104. case isci_perform_error_io_completion:
  105. /* Use sas_task_abort */
  106. dev_warn(&ihost->pdev->dev,
  107. "%s: Error - task = %p, response=%d, "
  108. "status=%d\n",
  109. __func__, task, response, status);
  110. isci_execpath_callback(ihost, task, sas_task_abort);
  111. break;
  112. default:
  113. dev_warn(&ihost->pdev->dev,
  114. "%s: isci task notification default case!",
  115. __func__);
  116. sas_task_abort(task);
  117. break;
  118. }
  119. }
  120. #define for_each_sas_task(num, task) \
  121. for (; num > 0; num--,\
  122. task = list_entry(task->list.next, struct sas_task, list))
  123. /**
  124. * isci_task_execute_task() - This function is one of the SAS Domain Template
  125. * functions. This function is called by libsas to send a task down to
  126. * hardware.
  127. * @task: This parameter specifies the SAS task to send.
  128. * @num: This parameter specifies the number of tasks to queue.
  129. * @gfp_flags: This parameter specifies the context of this call.
  130. *
  131. * status, zero indicates success.
  132. */
  133. int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
  134. {
  135. struct isci_host *ihost = dev_to_ihost(task->dev);
  136. struct isci_request *request = NULL;
  137. struct isci_remote_device *device;
  138. unsigned long flags;
  139. int ret;
  140. enum sci_status status;
  141. enum isci_status device_status;
  142. dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
  143. /* Check if we have room for more tasks */
  144. ret = isci_host_can_queue(ihost, num);
  145. if (ret) {
  146. dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
  147. return ret;
  148. }
  149. for_each_sas_task(num, task) {
  150. dev_dbg(&ihost->pdev->dev,
  151. "task = %p, num = %d; dev = %p; cmd = %p\n",
  152. task, num, task->dev, task->uldd_task);
  153. device = task->dev->lldd_dev;
  154. if (device)
  155. device_status = device->status;
  156. else
  157. device_status = isci_freed;
  158. /* From this point onward, any process that needs to guarantee
  159. * that there is no kernel I/O being started will have to wait
  160. * for the quiesce spinlock.
  161. */
  162. if (device_status != isci_ready_for_io) {
  163. /* Forces a retry from scsi mid layer. */
  164. dev_dbg(&ihost->pdev->dev,
  165. "%s: task %p: isci_host->status = %d, "
  166. "device = %p; device_status = 0x%x\n\n",
  167. __func__,
  168. task,
  169. isci_host_get_state(ihost),
  170. device,
  171. device_status);
  172. if (device_status == isci_ready) {
  173. /* Indicate QUEUE_FULL so that the scsi midlayer
  174. * retries.
  175. */
  176. isci_task_refuse(ihost, task,
  177. SAS_TASK_COMPLETE,
  178. SAS_QUEUE_FULL);
  179. } else {
  180. /* Else, the device is going down. */
  181. isci_task_refuse(ihost, task,
  182. SAS_TASK_UNDELIVERED,
  183. SAS_DEVICE_UNKNOWN);
  184. }
  185. isci_host_can_dequeue(ihost, 1);
  186. } else {
  187. /* There is a device and it's ready for I/O. */
  188. spin_lock_irqsave(&task->task_state_lock, flags);
  189. if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
  190. spin_unlock_irqrestore(&task->task_state_lock,
  191. flags);
  192. isci_task_refuse(ihost, task,
  193. SAS_TASK_UNDELIVERED,
  194. SAM_STAT_TASK_ABORTED);
  195. /* The I/O was aborted. */
  196. } else {
  197. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  198. spin_unlock_irqrestore(&task->task_state_lock, flags);
  199. /* build and send the request. */
  200. status = isci_request_execute(ihost, task, &request,
  201. gfp_flags);
  202. if (status != SCI_SUCCESS) {
  203. spin_lock_irqsave(&task->task_state_lock, flags);
  204. /* Did not really start this command. */
  205. task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
  206. spin_unlock_irqrestore(&task->task_state_lock, flags);
  207. /* Indicate QUEUE_FULL so that the scsi
  208. * midlayer retries. if the request
  209. * failed for remote device reasons,
  210. * it gets returned as
  211. * SAS_TASK_UNDELIVERED next time
  212. * through.
  213. */
  214. isci_task_refuse(ihost, task,
  215. SAS_TASK_COMPLETE,
  216. SAS_QUEUE_FULL);
  217. isci_host_can_dequeue(ihost, 1);
  218. }
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. /**
  225. * isci_task_request_build() - This function builds the task request object.
  226. * @isci_host: This parameter specifies the ISCI host object
  227. * @request: This parameter points to the isci_request object allocated in the
  228. * request construct function.
  229. * @tmf: This parameter is the task management struct to be built
  230. *
  231. * SCI_SUCCESS on successfull completion, or specific failure code.
  232. */
  233. static enum sci_status isci_task_request_build(
  234. struct isci_host *isci_host,
  235. struct isci_request **isci_request,
  236. struct isci_tmf *isci_tmf)
  237. {
  238. struct scic_sds_remote_device *sci_device;
  239. enum sci_status status = SCI_FAILURE;
  240. struct isci_request *request = NULL;
  241. struct isci_remote_device *isci_device;
  242. struct domain_device *dev;
  243. dev_dbg(&isci_host->pdev->dev,
  244. "%s: isci_tmf = %p\n", __func__, isci_tmf);
  245. isci_device = isci_tmf->device;
  246. sci_device = &isci_device->sci;
  247. dev = isci_device->domain_dev;
  248. /* do common allocation and init of request object. */
  249. status = isci_request_alloc_tmf(
  250. isci_host,
  251. isci_tmf,
  252. &request,
  253. isci_device,
  254. GFP_ATOMIC
  255. );
  256. if (status != SCI_SUCCESS)
  257. goto out;
  258. /* let the core do it's construct. */
  259. status = scic_task_request_construct(&isci_host->sci, sci_device,
  260. SCI_CONTROLLER_INVALID_IO_TAG,
  261. &request->sci);
  262. if (status != SCI_SUCCESS) {
  263. dev_warn(&isci_host->pdev->dev,
  264. "%s: scic_task_request_construct failed - "
  265. "status = 0x%x\n",
  266. __func__,
  267. status);
  268. goto errout;
  269. }
  270. /* XXX convert to get this from task->tproto like other drivers */
  271. if (dev->dev_type == SAS_END_DEV) {
  272. isci_tmf->proto = SAS_PROTOCOL_SSP;
  273. status = scic_task_request_construct_ssp(&request->sci);
  274. if (status != SCI_SUCCESS)
  275. goto errout;
  276. }
  277. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  278. isci_tmf->proto = SAS_PROTOCOL_SATA;
  279. status = isci_sata_management_task_request_build(request);
  280. if (status != SCI_SUCCESS)
  281. goto errout;
  282. }
  283. goto out;
  284. errout:
  285. /* release the dma memory if we fail. */
  286. isci_request_free(isci_host, request);
  287. request = NULL;
  288. out:
  289. *isci_request = request;
  290. return status;
  291. }
  292. /**
  293. * isci_tmf_timeout_cb() - This function is called as a kernel callback when
  294. * the timeout period for the TMF has expired.
  295. *
  296. *
  297. */
  298. static void isci_tmf_timeout_cb(void *tmf_request_arg)
  299. {
  300. struct isci_request *request = (struct isci_request *)tmf_request_arg;
  301. struct isci_tmf *tmf = isci_request_access_tmf(request);
  302. enum sci_status status;
  303. /* This task management request has timed-out. Terminate the request
  304. * so that the request eventually completes to the requestor in the
  305. * request completion callback path.
  306. */
  307. /* Note - the timer callback function itself has provided spinlock
  308. * exclusion from the start and completion paths. No need to take
  309. * the request->isci_host->scic_lock here.
  310. */
  311. if (tmf->timeout_timer != NULL) {
  312. /* Call the users callback, if any. */
  313. if (tmf->cb_state_func != NULL)
  314. tmf->cb_state_func(isci_tmf_timed_out, tmf,
  315. tmf->cb_data);
  316. /* Terminate the TMF transmit request. */
  317. status = scic_controller_terminate_request(
  318. &request->isci_host->sci,
  319. &request->isci_device->sci,
  320. &request->sci);
  321. dev_dbg(&request->isci_host->pdev->dev,
  322. "%s: tmf_request = %p; tmf = %p; status = %d\n",
  323. __func__, request, tmf, status);
  324. } else
  325. dev_dbg(&request->isci_host->pdev->dev,
  326. "%s: timer already canceled! "
  327. "tmf_request = %p; tmf = %p\n",
  328. __func__, request, tmf);
  329. /* No need to unlock since the caller to this callback is doing it for
  330. * us.
  331. * request->isci_host->scic_lock
  332. */
  333. }
  334. /**
  335. * isci_task_execute_tmf() - This function builds and sends a task request,
  336. * then waits for the completion.
  337. * @isci_host: This parameter specifies the ISCI host object
  338. * @tmf: This parameter is the pointer to the task management structure for
  339. * this request.
  340. * @timeout_ms: This parameter specifies the timeout period for the task
  341. * management request.
  342. *
  343. * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
  344. * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
  345. */
  346. int isci_task_execute_tmf(
  347. struct isci_host *isci_host,
  348. struct isci_tmf *tmf,
  349. unsigned long timeout_ms)
  350. {
  351. DECLARE_COMPLETION_ONSTACK(completion);
  352. enum sci_task_status status = SCI_TASK_FAILURE;
  353. struct scic_sds_remote_device *sci_device;
  354. struct isci_remote_device *isci_device = tmf->device;
  355. struct isci_request *request;
  356. int ret = TMF_RESP_FUNC_FAILED;
  357. unsigned long flags;
  358. /* sanity check, return TMF_RESP_FUNC_FAILED
  359. * if the device is not there and ready.
  360. */
  361. if (!isci_device || isci_device->status != isci_ready_for_io) {
  362. dev_dbg(&isci_host->pdev->dev,
  363. "%s: isci_device = %p not ready (%d)\n",
  364. __func__,
  365. isci_device, isci_device->status);
  366. return TMF_RESP_FUNC_FAILED;
  367. } else
  368. dev_dbg(&isci_host->pdev->dev,
  369. "%s: isci_device = %p\n",
  370. __func__, isci_device);
  371. sci_device = &isci_device->sci;
  372. /* Assign the pointer to the TMF's completion kernel wait structure. */
  373. tmf->complete = &completion;
  374. isci_task_request_build(
  375. isci_host,
  376. &request,
  377. tmf
  378. );
  379. if (!request) {
  380. dev_warn(&isci_host->pdev->dev,
  381. "%s: isci_task_request_build failed\n",
  382. __func__);
  383. return TMF_RESP_FUNC_FAILED;
  384. }
  385. /* Allocate the TMF timeout timer. */
  386. spin_lock_irqsave(&isci_host->scic_lock, flags);
  387. tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
  388. /* Start the timer. */
  389. if (tmf->timeout_timer)
  390. isci_timer_start(tmf->timeout_timer, timeout_ms);
  391. else
  392. dev_warn(&isci_host->pdev->dev,
  393. "%s: isci_timer_create failed!!!!\n",
  394. __func__);
  395. /* start the TMF io. */
  396. status = scic_controller_start_task(
  397. &isci_host->sci,
  398. sci_device,
  399. &request->sci,
  400. SCI_CONTROLLER_INVALID_IO_TAG);
  401. if (status != SCI_TASK_SUCCESS) {
  402. dev_warn(&isci_host->pdev->dev,
  403. "%s: start_io failed - status = 0x%x, request = %p\n",
  404. __func__,
  405. status,
  406. request);
  407. goto cleanup_request;
  408. }
  409. /* Call the users callback, if any. */
  410. if (tmf->cb_state_func != NULL)
  411. tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
  412. /* Change the state of the TMF-bearing request to "started". */
  413. isci_request_change_state(request, started);
  414. /* add the request to the remote device request list. */
  415. list_add(&request->dev_node, &isci_device->reqs_in_process);
  416. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  417. /* Wait for the TMF to complete, or a timeout. */
  418. wait_for_completion(&completion);
  419. isci_print_tmf(tmf);
  420. if (tmf->status == SCI_SUCCESS)
  421. ret = TMF_RESP_FUNC_COMPLETE;
  422. else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
  423. dev_dbg(&isci_host->pdev->dev,
  424. "%s: tmf.status == "
  425. "SCI_FAILURE_IO_RESPONSE_VALID\n",
  426. __func__);
  427. ret = TMF_RESP_FUNC_COMPLETE;
  428. }
  429. /* Else - leave the default "failed" status alone. */
  430. dev_dbg(&isci_host->pdev->dev,
  431. "%s: completed request = %p\n",
  432. __func__,
  433. request);
  434. if (request->io_request_completion != NULL) {
  435. /* The fact that this is non-NULL for a TMF request
  436. * means there is a thread waiting for this TMF to
  437. * finish.
  438. */
  439. complete(request->io_request_completion);
  440. }
  441. spin_lock_irqsave(&isci_host->scic_lock, flags);
  442. cleanup_request:
  443. /* Clean up the timer if needed. */
  444. if (tmf->timeout_timer) {
  445. isci_del_timer(isci_host, tmf->timeout_timer);
  446. tmf->timeout_timer = NULL;
  447. }
  448. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  449. isci_request_free(isci_host, request);
  450. return ret;
  451. }
  452. void isci_task_build_tmf(
  453. struct isci_tmf *tmf,
  454. struct isci_remote_device *isci_device,
  455. enum isci_tmf_function_codes code,
  456. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  457. struct isci_tmf *,
  458. void *),
  459. void *cb_data)
  460. {
  461. dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
  462. "%s: isci_device = %p\n", __func__, isci_device);
  463. memset(tmf, 0, sizeof(*tmf));
  464. tmf->device = isci_device;
  465. tmf->tmf_code = code;
  466. tmf->timeout_timer = NULL;
  467. tmf->cb_state_func = tmf_sent_cb;
  468. tmf->cb_data = cb_data;
  469. }
  470. static void isci_task_build_abort_task_tmf(
  471. struct isci_tmf *tmf,
  472. struct isci_remote_device *isci_device,
  473. enum isci_tmf_function_codes code,
  474. void (*tmf_sent_cb)(enum isci_tmf_cb_state,
  475. struct isci_tmf *,
  476. void *),
  477. struct isci_request *old_request)
  478. {
  479. isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
  480. (void *)old_request);
  481. tmf->io_tag = old_request->io_tag;
  482. }
  483. static struct isci_request *isci_task_get_request_from_task(
  484. struct sas_task *task,
  485. struct isci_remote_device **isci_device)
  486. {
  487. struct isci_request *request = NULL;
  488. unsigned long flags;
  489. spin_lock_irqsave(&task->task_state_lock, flags);
  490. request = task->lldd_task;
  491. /* If task is already done, the request isn't valid */
  492. if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
  493. (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
  494. (request != NULL)) {
  495. if (isci_device != NULL)
  496. *isci_device = request->isci_device;
  497. }
  498. spin_unlock_irqrestore(&task->task_state_lock, flags);
  499. return request;
  500. }
  501. /**
  502. * isci_task_validate_request_to_abort() - This function checks the given I/O
  503. * against the "started" state. If the request is still "started", it's
  504. * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
  505. * BEFORE CALLING THIS FUNCTION.
  506. * @isci_request: This parameter specifies the request object to control.
  507. * @isci_host: This parameter specifies the ISCI host object
  508. * @isci_device: This is the device to which the request is pending.
  509. * @aborted_io_completion: This is a completion structure that will be added to
  510. * the request in case it is changed to aborting; this completion is
  511. * triggered when the request is fully completed.
  512. *
  513. * Either "started" on successful change of the task status to "aborted", or
  514. * "unallocated" if the task cannot be controlled.
  515. */
  516. static enum isci_request_status isci_task_validate_request_to_abort(
  517. struct isci_request *isci_request,
  518. struct isci_host *isci_host,
  519. struct isci_remote_device *isci_device,
  520. struct completion *aborted_io_completion)
  521. {
  522. enum isci_request_status old_state = unallocated;
  523. /* Only abort the task if it's in the
  524. * device's request_in_process list
  525. */
  526. if (isci_request && !list_empty(&isci_request->dev_node)) {
  527. old_state = isci_request_change_started_to_aborted(
  528. isci_request, aborted_io_completion);
  529. }
  530. return old_state;
  531. }
  532. static void isci_request_cleanup_completed_loiterer(
  533. struct isci_host *isci_host,
  534. struct isci_remote_device *isci_device,
  535. struct isci_request *isci_request)
  536. {
  537. struct sas_task *task;
  538. unsigned long flags;
  539. task = (isci_request->ttype == io_task)
  540. ? isci_request_access_task(isci_request)
  541. : NULL;
  542. dev_dbg(&isci_host->pdev->dev,
  543. "%s: isci_device=%p, request=%p, task=%p\n",
  544. __func__, isci_device, isci_request, task);
  545. spin_lock_irqsave(&isci_host->scic_lock, flags);
  546. list_del_init(&isci_request->dev_node);
  547. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  548. if (task != NULL) {
  549. spin_lock_irqsave(&task->task_state_lock, flags);
  550. task->lldd_task = NULL;
  551. isci_set_task_doneflags(task);
  552. /* If this task is not in the abort path, call task_done. */
  553. if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  554. spin_unlock_irqrestore(&task->task_state_lock, flags);
  555. task->task_done(task);
  556. } else
  557. spin_unlock_irqrestore(&task->task_state_lock, flags);
  558. }
  559. isci_request_free(isci_host, isci_request);
  560. }
  561. /**
  562. * @isci_termination_timed_out(): this function will deal with a request for
  563. * which the wait for termination has timed-out.
  564. *
  565. * @isci_host This SCU.
  566. * @isci_request The I/O request being terminated.
  567. */
  568. static void
  569. isci_termination_timed_out(
  570. struct isci_host * host,
  571. struct isci_request * request
  572. )
  573. {
  574. unsigned long state_flags;
  575. dev_warn(&host->pdev->dev,
  576. "%s: host = %p; request = %p\n",
  577. __func__, host, request);
  578. /* At this point, the request to terminate
  579. * has timed out. The best we can do is to
  580. * have the request die a silent death
  581. * if it ever completes.
  582. */
  583. spin_lock_irqsave(&request->state_lock, state_flags);
  584. if (request->status == started) {
  585. /* Set the request state to "dead",
  586. * and clear the task pointer so that an actual
  587. * completion event callback doesn't do
  588. * anything.
  589. */
  590. request->status = dead;
  591. /* Clear the timeout completion event pointer.*/
  592. request->io_request_completion = NULL;
  593. if (request->ttype == io_task) {
  594. /* Break links with the sas_task. */
  595. if (request->ttype_ptr.io_task_ptr != NULL) {
  596. request->ttype_ptr.io_task_ptr->lldd_task = NULL;
  597. request->ttype_ptr.io_task_ptr = NULL;
  598. }
  599. }
  600. }
  601. spin_unlock_irqrestore(&request->state_lock, state_flags);
  602. }
  603. /**
  604. * isci_terminate_request_core() - This function will terminate the given
  605. * request, and wait for it to complete. This function must only be called
  606. * from a thread that can wait. Note that the request is terminated and
  607. * completed (back to the host, if started there).
  608. * @isci_host: This SCU.
  609. * @isci_device: The target.
  610. * @isci_request: The I/O request to be terminated.
  611. *
  612. *
  613. */
  614. static void isci_terminate_request_core(
  615. struct isci_host *isci_host,
  616. struct isci_remote_device *isci_device,
  617. struct isci_request *isci_request)
  618. {
  619. enum sci_status status = SCI_SUCCESS;
  620. bool was_terminated = false;
  621. bool needs_cleanup_handling = false;
  622. enum isci_request_status request_status;
  623. unsigned long flags;
  624. unsigned long timeout_remaining;
  625. dev_dbg(&isci_host->pdev->dev,
  626. "%s: device = %p; request = %p\n",
  627. __func__, isci_device, isci_request);
  628. spin_lock_irqsave(&isci_host->scic_lock, flags);
  629. /* Note that we are not going to control
  630. * the target to abort the request.
  631. */
  632. isci_request->complete_in_target = true;
  633. /* Make sure the request wasn't just sitting around signalling
  634. * device condition (if the request handle is NULL, then the
  635. * request completed but needed additional handling here).
  636. */
  637. if (!isci_request->terminated) {
  638. was_terminated = true;
  639. needs_cleanup_handling = true;
  640. status = scic_controller_terminate_request(
  641. &isci_host->sci,
  642. &isci_device->sci,
  643. &isci_request->sci);
  644. }
  645. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  646. /*
  647. * The only time the request to terminate will
  648. * fail is when the io request is completed and
  649. * being aborted.
  650. */
  651. if (status != SCI_SUCCESS) {
  652. dev_err(&isci_host->pdev->dev,
  653. "%s: scic_controller_terminate_request"
  654. " returned = 0x%x\n",
  655. __func__,
  656. status);
  657. /* Clear the completion pointer from the request. */
  658. isci_request->io_request_completion = NULL;
  659. } else {
  660. if (was_terminated) {
  661. dev_dbg(&isci_host->pdev->dev,
  662. "%s: before completion wait (%p)\n",
  663. __func__,
  664. isci_request->io_request_completion);
  665. /* Wait here for the request to complete. */
  666. #define TERMINATION_TIMEOUT_MSEC 50
  667. timeout_remaining
  668. = wait_for_completion_timeout(
  669. isci_request->io_request_completion,
  670. msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
  671. if (!timeout_remaining) {
  672. isci_termination_timed_out(isci_host,
  673. isci_request);
  674. dev_err(&isci_host->pdev->dev,
  675. "%s: *** Timeout waiting for "
  676. "termination(%p/%p)\n",
  677. __func__,
  678. isci_request->io_request_completion,
  679. isci_request);
  680. } else
  681. dev_dbg(&isci_host->pdev->dev,
  682. "%s: after completion wait (%p)\n",
  683. __func__,
  684. isci_request->io_request_completion);
  685. }
  686. /* Clear the completion pointer from the request. */
  687. isci_request->io_request_completion = NULL;
  688. /* Peek at the status of the request. This will tell
  689. * us if there was special handling on the request such that it
  690. * needs to be detached and freed here.
  691. */
  692. spin_lock_irqsave(&isci_request->state_lock, flags);
  693. request_status = isci_request_get_state(isci_request);
  694. if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
  695. && ((request_status == aborted)
  696. || (request_status == aborting)
  697. || (request_status == terminating)
  698. || (request_status == completed)
  699. || (request_status == dead)
  700. )
  701. ) {
  702. /* The completion routine won't free a request in
  703. * the aborted/aborting/etc. states, so we do
  704. * it here.
  705. */
  706. needs_cleanup_handling = true;
  707. }
  708. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  709. if (needs_cleanup_handling)
  710. isci_request_cleanup_completed_loiterer(
  711. isci_host, isci_device, isci_request
  712. );
  713. }
  714. }
  715. static void isci_terminate_request(
  716. struct isci_host *isci_host,
  717. struct isci_remote_device *isci_device,
  718. struct isci_request *isci_request,
  719. enum isci_request_status new_request_state)
  720. {
  721. enum isci_request_status old_state;
  722. DECLARE_COMPLETION_ONSTACK(request_completion);
  723. /* Change state to "new_request_state" if it is currently "started" */
  724. old_state = isci_request_change_started_to_newstate(
  725. isci_request,
  726. &request_completion,
  727. new_request_state
  728. );
  729. if ((old_state == started) ||
  730. (old_state == completed) ||
  731. (old_state == aborting)) {
  732. /* If the old_state is started:
  733. * This request was not already being aborted. If it had been,
  734. * then the aborting I/O (ie. the TMF request) would not be in
  735. * the aborting state, and thus would be terminated here. Note
  736. * that since the TMF completion's call to the kernel function
  737. * "complete()" does not happen until the pending I/O request
  738. * terminate fully completes, we do not have to implement a
  739. * special wait here for already aborting requests - the
  740. * termination of the TMF request will force the request
  741. * to finish it's already started terminate.
  742. *
  743. * If old_state == completed:
  744. * This request completed from the SCU hardware perspective
  745. * and now just needs cleaning up in terms of freeing the
  746. * request and potentially calling up to libsas.
  747. *
  748. * If old_state == aborting:
  749. * This request has already gone through a TMF timeout, but may
  750. * not have been terminated; needs cleaning up at least.
  751. */
  752. isci_terminate_request_core(isci_host, isci_device,
  753. isci_request);
  754. }
  755. }
  756. /**
  757. * isci_terminate_pending_requests() - This function will change the all of the
  758. * requests on the given device's state to "aborting", will terminate the
  759. * requests, and wait for them to complete. This function must only be
  760. * called from a thread that can wait. Note that the requests are all
  761. * terminated and completed (back to the host, if started there).
  762. * @isci_host: This parameter specifies SCU.
  763. * @isci_device: This parameter specifies the target.
  764. *
  765. *
  766. */
  767. void isci_terminate_pending_requests(
  768. struct isci_host *isci_host,
  769. struct isci_remote_device *isci_device,
  770. enum isci_request_status new_request_state)
  771. {
  772. struct isci_request *request;
  773. struct isci_request *next_request;
  774. unsigned long flags;
  775. struct list_head aborted_request_list;
  776. INIT_LIST_HEAD(&aborted_request_list);
  777. dev_dbg(&isci_host->pdev->dev,
  778. "%s: isci_device = %p (new request state = %d)\n",
  779. __func__, isci_device, new_request_state);
  780. spin_lock_irqsave(&isci_host->scic_lock, flags);
  781. /* Move all of the pending requests off of the device list. */
  782. list_splice_init(&isci_device->reqs_in_process,
  783. &aborted_request_list);
  784. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  785. /* Iterate through the now-local list. */
  786. list_for_each_entry_safe(request, next_request,
  787. &aborted_request_list, dev_node) {
  788. dev_warn(&isci_host->pdev->dev,
  789. "%s: isci_device=%p request=%p; task=%p\n",
  790. __func__,
  791. isci_device, request,
  792. ((request->ttype == io_task)
  793. ? isci_request_access_task(request)
  794. : NULL));
  795. /* Mark all still pending I/O with the selected next
  796. * state, terminate and free it.
  797. */
  798. isci_terminate_request(isci_host, isci_device,
  799. request, new_request_state
  800. );
  801. }
  802. }
  803. /**
  804. * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
  805. * Template functions.
  806. * @lun: This parameter specifies the lun to be reset.
  807. *
  808. * status, zero indicates success.
  809. */
  810. static int isci_task_send_lu_reset_sas(
  811. struct isci_host *isci_host,
  812. struct isci_remote_device *isci_device,
  813. u8 *lun)
  814. {
  815. struct isci_tmf tmf;
  816. int ret = TMF_RESP_FUNC_FAILED;
  817. dev_dbg(&isci_host->pdev->dev,
  818. "%s: isci_host = %p, isci_device = %p\n",
  819. __func__, isci_host, isci_device);
  820. /* Send the LUN reset to the target. By the time the call returns,
  821. * the TMF has fully exected in the target (in which case the return
  822. * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
  823. * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
  824. */
  825. isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
  826. NULL);
  827. #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
  828. ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
  829. if (ret == TMF_RESP_FUNC_COMPLETE)
  830. dev_dbg(&isci_host->pdev->dev,
  831. "%s: %p: TMF_LU_RESET passed\n",
  832. __func__, isci_device);
  833. else
  834. dev_dbg(&isci_host->pdev->dev,
  835. "%s: %p: TMF_LU_RESET failed (%x)\n",
  836. __func__, isci_device, ret);
  837. return ret;
  838. }
  839. /**
  840. * isci_task_lu_reset() - This function is one of the SAS Domain Template
  841. * functions. This is one of the Task Management functoins called by libsas,
  842. * to reset the given lun. Note the assumption that while this call is
  843. * executing, no I/O will be sent by the host to the device.
  844. * @lun: This parameter specifies the lun to be reset.
  845. *
  846. * status, zero indicates success.
  847. */
  848. int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
  849. {
  850. struct isci_host *isci_host = dev_to_ihost(domain_device);
  851. struct isci_remote_device *isci_device = NULL;
  852. int ret;
  853. bool device_stopping = false;
  854. isci_device = domain_device->lldd_dev;
  855. dev_dbg(&isci_host->pdev->dev,
  856. "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
  857. __func__, domain_device, isci_host, isci_device);
  858. if (isci_device != NULL) {
  859. device_stopping = (isci_device->status == isci_stopping)
  860. || (isci_device->status == isci_stopped);
  861. set_bit(IDEV_EH, &isci_device->flags);
  862. }
  863. /* If there is a device reset pending on any request in the
  864. * device's list, fail this LUN reset request in order to
  865. * escalate to the device reset.
  866. */
  867. if (!isci_device || device_stopping ||
  868. isci_device_is_reset_pending(isci_host, isci_device)) {
  869. dev_warn(&isci_host->pdev->dev,
  870. "%s: No dev (%p), or "
  871. "RESET PENDING: domain_device=%p\n",
  872. __func__, isci_device, domain_device);
  873. return TMF_RESP_FUNC_FAILED;
  874. }
  875. /* Send the task management part of the reset. */
  876. if (sas_protocol_ata(domain_device->tproto)) {
  877. ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
  878. } else
  879. ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
  880. /* If the LUN reset worked, all the I/O can now be terminated. */
  881. if (ret == TMF_RESP_FUNC_COMPLETE)
  882. /* Terminate all I/O now. */
  883. isci_terminate_pending_requests(isci_host,
  884. isci_device,
  885. terminating);
  886. return ret;
  887. }
  888. /* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
  889. int isci_task_clear_nexus_port(struct asd_sas_port *port)
  890. {
  891. return TMF_RESP_FUNC_FAILED;
  892. }
  893. int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
  894. {
  895. return TMF_RESP_FUNC_FAILED;
  896. }
  897. /* Task Management Functions. Must be called from process context. */
  898. /**
  899. * isci_abort_task_process_cb() - This is a helper function for the abort task
  900. * TMF command. It manages the request state with respect to the successful
  901. * transmission / completion of the abort task request.
  902. * @cb_state: This parameter specifies when this function was called - after
  903. * the TMF request has been started and after it has timed-out.
  904. * @tmf: This parameter specifies the TMF in progress.
  905. *
  906. *
  907. */
  908. static void isci_abort_task_process_cb(
  909. enum isci_tmf_cb_state cb_state,
  910. struct isci_tmf *tmf,
  911. void *cb_data)
  912. {
  913. struct isci_request *old_request;
  914. old_request = (struct isci_request *)cb_data;
  915. dev_dbg(&old_request->isci_host->pdev->dev,
  916. "%s: tmf=%p, old_request=%p\n",
  917. __func__, tmf, old_request);
  918. switch (cb_state) {
  919. case isci_tmf_started:
  920. /* The TMF has been started. Nothing to do here, since the
  921. * request state was already set to "aborted" by the abort
  922. * task function.
  923. */
  924. if ((old_request->status != aborted)
  925. && (old_request->status != completed))
  926. dev_err(&old_request->isci_host->pdev->dev,
  927. "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
  928. __func__, old_request->status, tmf, old_request);
  929. break;
  930. case isci_tmf_timed_out:
  931. /* Set the task's state to "aborting", since the abort task
  932. * function thread set it to "aborted" (above) in anticipation
  933. * of the task management request working correctly. Since the
  934. * timeout has now fired, the TMF request failed. We set the
  935. * state such that the request completion will indicate the
  936. * device is no longer present.
  937. */
  938. isci_request_change_state(old_request, aborting);
  939. break;
  940. default:
  941. dev_err(&old_request->isci_host->pdev->dev,
  942. "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
  943. __func__, cb_state, tmf, old_request);
  944. break;
  945. }
  946. }
  947. /**
  948. * isci_task_abort_task() - This function is one of the SAS Domain Template
  949. * functions. This function is called by libsas to abort a specified task.
  950. * @task: This parameter specifies the SAS task to abort.
  951. *
  952. * status, zero indicates success.
  953. */
  954. int isci_task_abort_task(struct sas_task *task)
  955. {
  956. struct isci_host *isci_host = dev_to_ihost(task->dev);
  957. DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
  958. struct isci_request *old_request = NULL;
  959. enum isci_request_status old_state;
  960. struct isci_remote_device *isci_device = NULL;
  961. struct isci_tmf tmf;
  962. int ret = TMF_RESP_FUNC_FAILED;
  963. unsigned long flags;
  964. bool any_dev_reset = false;
  965. bool device_stopping;
  966. /* Get the isci_request reference from the task. Note that
  967. * this check does not depend on the pending request list
  968. * in the device, because tasks driving resets may land here
  969. * after completion in the core.
  970. */
  971. old_request = isci_task_get_request_from_task(task, &isci_device);
  972. dev_dbg(&isci_host->pdev->dev,
  973. "%s: task = %p\n", __func__, task);
  974. /* Check if the device has been / is currently being removed.
  975. * If so, no task management will be done, and the I/O will
  976. * be terminated.
  977. */
  978. device_stopping = (isci_device->status == isci_stopping)
  979. || (isci_device->status == isci_stopped);
  980. /* XXX need to fix device lookup lifetime (needs to be done
  981. * under scic_lock, among other things...), but for now assume
  982. * the device is available like the above code
  983. */
  984. set_bit(IDEV_EH, &isci_device->flags);
  985. /* This version of the driver will fail abort requests for
  986. * SATA/STP. Failing the abort request this way will cause the
  987. * SCSI error handler thread to escalate to LUN reset
  988. */
  989. if (sas_protocol_ata(task->task_proto) && !device_stopping) {
  990. dev_warn(&isci_host->pdev->dev,
  991. " task %p is for a STP/SATA device;"
  992. " returning TMF_RESP_FUNC_FAILED\n"
  993. " to cause a LUN reset...\n", task);
  994. return TMF_RESP_FUNC_FAILED;
  995. }
  996. dev_dbg(&isci_host->pdev->dev,
  997. "%s: old_request == %p\n", __func__, old_request);
  998. if (!device_stopping)
  999. any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
  1000. spin_lock_irqsave(&task->task_state_lock, flags);
  1001. /* Don't do resets to stopping devices. */
  1002. if (device_stopping) {
  1003. task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
  1004. any_dev_reset = false;
  1005. } else /* See if there is a pending device reset for this device. */
  1006. any_dev_reset = any_dev_reset
  1007. || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
  1008. /* If the extraction of the request reference from the task
  1009. * failed, then the request has been completed (or if there is a
  1010. * pending reset then this abort request function must be failed
  1011. * in order to escalate to the target reset).
  1012. */
  1013. if ((old_request == NULL) || any_dev_reset) {
  1014. /* If the device reset task flag is set, fail the task
  1015. * management request. Otherwise, the original request
  1016. * has completed.
  1017. */
  1018. if (any_dev_reset) {
  1019. /* Turn off the task's DONE to make sure this
  1020. * task is escalated to a target reset.
  1021. */
  1022. task->task_state_flags &= ~SAS_TASK_STATE_DONE;
  1023. /* Make the reset happen as soon as possible. */
  1024. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  1025. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1026. /* Fail the task management request in order to
  1027. * escalate to the target reset.
  1028. */
  1029. ret = TMF_RESP_FUNC_FAILED;
  1030. dev_dbg(&isci_host->pdev->dev,
  1031. "%s: Failing task abort in order to "
  1032. "escalate to target reset because\n"
  1033. "SAS_TASK_NEED_DEV_RESET is set for "
  1034. "task %p on dev %p\n",
  1035. __func__, task, isci_device);
  1036. } else {
  1037. /* The request has already completed and there
  1038. * is nothing to do here other than to set the task
  1039. * done bit, and indicate that the task abort function
  1040. * was sucessful.
  1041. */
  1042. isci_set_task_doneflags(task);
  1043. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1044. ret = TMF_RESP_FUNC_COMPLETE;
  1045. dev_dbg(&isci_host->pdev->dev,
  1046. "%s: abort task not needed for %p\n",
  1047. __func__, task);
  1048. }
  1049. return ret;
  1050. }
  1051. else
  1052. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1053. spin_lock_irqsave(&isci_host->scic_lock, flags);
  1054. /* Check the request status and change to "aborted" if currently
  1055. * "starting"; if true then set the I/O kernel completion
  1056. * struct that will be triggered when the request completes.
  1057. */
  1058. old_state = isci_task_validate_request_to_abort(
  1059. old_request, isci_host, isci_device,
  1060. &aborted_io_completion);
  1061. if ((old_state != started) &&
  1062. (old_state != completed) &&
  1063. (old_state != aborting)) {
  1064. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1065. /* The request was already being handled by someone else (because
  1066. * they got to set the state away from started).
  1067. */
  1068. dev_dbg(&isci_host->pdev->dev,
  1069. "%s: device = %p; old_request %p already being aborted\n",
  1070. __func__,
  1071. isci_device, old_request);
  1072. return TMF_RESP_FUNC_COMPLETE;
  1073. }
  1074. if ((task->task_proto == SAS_PROTOCOL_SMP)
  1075. || device_stopping
  1076. || old_request->complete_in_target
  1077. ) {
  1078. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1079. dev_dbg(&isci_host->pdev->dev,
  1080. "%s: SMP request (%d)"
  1081. " or device is stopping (%d)"
  1082. " or complete_in_target (%d), thus no TMF\n",
  1083. __func__, (task->task_proto == SAS_PROTOCOL_SMP),
  1084. device_stopping, old_request->complete_in_target);
  1085. /* Set the state on the task. */
  1086. isci_task_all_done(task);
  1087. ret = TMF_RESP_FUNC_COMPLETE;
  1088. /* Stopping and SMP devices are not sent a TMF, and are not
  1089. * reset, but the outstanding I/O request is terminated below.
  1090. */
  1091. } else {
  1092. /* Fill in the tmf stucture */
  1093. isci_task_build_abort_task_tmf(&tmf, isci_device,
  1094. isci_tmf_ssp_task_abort,
  1095. isci_abort_task_process_cb,
  1096. old_request);
  1097. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  1098. #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
  1099. ret = isci_task_execute_tmf(isci_host, &tmf,
  1100. ISCI_ABORT_TASK_TIMEOUT_MS);
  1101. if (ret != TMF_RESP_FUNC_COMPLETE)
  1102. dev_err(&isci_host->pdev->dev,
  1103. "%s: isci_task_send_tmf failed\n",
  1104. __func__);
  1105. }
  1106. if (ret == TMF_RESP_FUNC_COMPLETE) {
  1107. old_request->complete_in_target = true;
  1108. /* Clean up the request on our side, and wait for the aborted I/O to
  1109. * complete.
  1110. */
  1111. isci_terminate_request_core(isci_host, isci_device, old_request);
  1112. }
  1113. /* Make sure we do not leave a reference to aborted_io_completion */
  1114. old_request->io_request_completion = NULL;
  1115. return ret;
  1116. }
  1117. /**
  1118. * isci_task_abort_task_set() - This function is one of the SAS Domain Template
  1119. * functions. This is one of the Task Management functoins called by libsas,
  1120. * to abort all task for the given lun.
  1121. * @d_device: This parameter specifies the domain device associated with this
  1122. * request.
  1123. * @lun: This parameter specifies the lun associated with this request.
  1124. *
  1125. * status, zero indicates success.
  1126. */
  1127. int isci_task_abort_task_set(
  1128. struct domain_device *d_device,
  1129. u8 *lun)
  1130. {
  1131. return TMF_RESP_FUNC_FAILED;
  1132. }
  1133. /**
  1134. * isci_task_clear_aca() - This function is one of the SAS Domain Template
  1135. * functions. This is one of the Task Management functoins called by libsas.
  1136. * @d_device: This parameter specifies the domain device associated with this
  1137. * request.
  1138. * @lun: This parameter specifies the lun associated with this request.
  1139. *
  1140. * status, zero indicates success.
  1141. */
  1142. int isci_task_clear_aca(
  1143. struct domain_device *d_device,
  1144. u8 *lun)
  1145. {
  1146. return TMF_RESP_FUNC_FAILED;
  1147. }
  1148. /**
  1149. * isci_task_clear_task_set() - This function is one of the SAS Domain Template
  1150. * functions. This is one of the Task Management functoins called by libsas.
  1151. * @d_device: This parameter specifies the domain device associated with this
  1152. * request.
  1153. * @lun: This parameter specifies the lun associated with this request.
  1154. *
  1155. * status, zero indicates success.
  1156. */
  1157. int isci_task_clear_task_set(
  1158. struct domain_device *d_device,
  1159. u8 *lun)
  1160. {
  1161. return TMF_RESP_FUNC_FAILED;
  1162. }
  1163. /**
  1164. * isci_task_query_task() - This function is implemented to cause libsas to
  1165. * correctly escalate the failed abort to a LUN or target reset (this is
  1166. * because sas_scsi_find_task libsas function does not correctly interpret
  1167. * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
  1168. * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
  1169. * returned, libsas will turn this into a target reset
  1170. * @task: This parameter specifies the sas task being queried.
  1171. * @lun: This parameter specifies the lun associated with this request.
  1172. *
  1173. * status, zero indicates success.
  1174. */
  1175. int isci_task_query_task(
  1176. struct sas_task *task)
  1177. {
  1178. /* See if there is a pending device reset for this device. */
  1179. if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
  1180. return TMF_RESP_FUNC_FAILED;
  1181. else
  1182. return TMF_RESP_FUNC_SUCC;
  1183. }
  1184. /*
  1185. * isci_task_request_complete() - This function is called by the sci core when
  1186. * an task request completes.
  1187. * @ihost: This parameter specifies the ISCI host object
  1188. * @ireq: This parameter is the completed isci_request object.
  1189. * @completion_status: This parameter specifies the completion status from the
  1190. * sci core.
  1191. *
  1192. * none.
  1193. */
  1194. void
  1195. isci_task_request_complete(struct isci_host *ihost,
  1196. struct isci_request *ireq,
  1197. enum sci_task_status completion_status)
  1198. {
  1199. struct isci_remote_device *idev = ireq->isci_device;
  1200. enum isci_request_status old_state;
  1201. struct isci_tmf *tmf = isci_request_access_tmf(ireq);
  1202. struct completion *tmf_complete;
  1203. struct scic_sds_request *sci_req = &ireq->sci;
  1204. dev_dbg(&ihost->pdev->dev,
  1205. "%s: request = %p, status=%d\n",
  1206. __func__, ireq, completion_status);
  1207. old_state = isci_request_change_state(ireq, completed);
  1208. tmf->status = completion_status;
  1209. ireq->complete_in_target = true;
  1210. if (tmf->proto == SAS_PROTOCOL_SSP) {
  1211. memcpy(&tmf->resp.resp_iu,
  1212. &sci_req->ssp.rsp,
  1213. SSP_RESP_IU_MAX_SIZE);
  1214. } else if (tmf->proto == SAS_PROTOCOL_SATA) {
  1215. memcpy(&tmf->resp.d2h_fis,
  1216. &sci_req->stp.rsp,
  1217. sizeof(struct dev_to_host_fis));
  1218. }
  1219. /* Manage the timer if it is still running. */
  1220. if (tmf->timeout_timer) {
  1221. isci_del_timer(ihost, tmf->timeout_timer);
  1222. tmf->timeout_timer = NULL;
  1223. }
  1224. /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
  1225. tmf_complete = tmf->complete;
  1226. scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
  1227. /* set the 'terminated' flag handle to make sure it cannot be terminated
  1228. * or completed again.
  1229. */
  1230. ireq->terminated = true;;
  1231. isci_request_change_state(ireq, unallocated);
  1232. list_del_init(&ireq->dev_node);
  1233. /* The task management part completes last. */
  1234. complete(tmf_complete);
  1235. }
  1236. static int isci_reset_device(struct domain_device *dev, int hard_reset)
  1237. {
  1238. struct isci_remote_device *idev = dev->lldd_dev;
  1239. struct sas_phy *phy = sas_find_local_phy(dev);
  1240. struct isci_host *ihost = dev_to_ihost(dev);
  1241. enum sci_status status;
  1242. unsigned long flags;
  1243. int rc;
  1244. dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
  1245. if (!idev) {
  1246. dev_warn(&ihost->pdev->dev,
  1247. "%s: idev is GONE!\n",
  1248. __func__);
  1249. return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
  1250. }
  1251. spin_lock_irqsave(&ihost->scic_lock, flags);
  1252. status = scic_remote_device_reset(&idev->sci);
  1253. if (status != SCI_SUCCESS) {
  1254. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1255. dev_warn(&ihost->pdev->dev,
  1256. "%s: scic_remote_device_reset(%p) returned %d!\n",
  1257. __func__, idev, status);
  1258. return TMF_RESP_FUNC_FAILED;
  1259. }
  1260. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1261. /* Make sure all pending requests are able to be fully terminated. */
  1262. isci_device_clear_reset_pending(ihost, idev);
  1263. rc = sas_phy_reset(phy, hard_reset);
  1264. msleep(2000); /* just like mvsas */
  1265. /* Terminate in-progress I/O now. */
  1266. isci_remote_device_nuke_requests(ihost, idev);
  1267. spin_lock_irqsave(&ihost->scic_lock, flags);
  1268. status = scic_remote_device_reset_complete(&idev->sci);
  1269. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1270. if (status != SCI_SUCCESS) {
  1271. dev_warn(&ihost->pdev->dev,
  1272. "%s: scic_remote_device_reset_complete(%p) "
  1273. "returned %d!\n", __func__, idev, status);
  1274. }
  1275. dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
  1276. return rc;
  1277. }
  1278. int isci_task_I_T_nexus_reset(struct domain_device *dev)
  1279. {
  1280. struct isci_host *ihost = dev_to_ihost(dev);
  1281. int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
  1282. struct isci_remote_device *idev;
  1283. unsigned long flags;
  1284. /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
  1285. * being too paranoid, or is mvsas busted?!
  1286. */
  1287. spin_lock_irqsave(&ihost->scic_lock, flags);
  1288. idev = dev->lldd_dev;
  1289. if (!idev || !test_bit(IDEV_EH, &idev->flags))
  1290. ret = TMF_RESP_FUNC_COMPLETE;
  1291. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  1292. if (ret == TMF_RESP_FUNC_COMPLETE)
  1293. return ret;
  1294. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1295. hard_reset = 0;
  1296. return isci_reset_device(dev, hard_reset);
  1297. }
  1298. int isci_bus_reset_handler(struct scsi_cmnd *cmd)
  1299. {
  1300. struct domain_device *dev = sdev_to_domain_dev(cmd->device);
  1301. int hard_reset = 1;
  1302. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
  1303. hard_reset = 0;
  1304. return isci_reset_device(dev, hard_reset);
  1305. }