task.c 48 KB

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