task.c 47 KB

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