task.c 44 KB

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