request.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "isci.h"
  56. #include "scic_io_request.h"
  57. #include "scic_task_request.h"
  58. #include "scic_port.h"
  59. #include "task.h"
  60. #include "request.h"
  61. #include "sata.h"
  62. #include "scu_completion_codes.h"
  63. #include "scic_sds_request.h"
  64. #include "scic_controller.h"
  65. #include "sas.h"
  66. static enum sci_status isci_request_ssp_request_construct(
  67. struct isci_request *request)
  68. {
  69. enum sci_status status;
  70. dev_dbg(&request->isci_host->pdev->dev,
  71. "%s: request = %p\n",
  72. __func__,
  73. request);
  74. status = scic_io_request_construct_basic_ssp(&request->sci);
  75. return status;
  76. }
  77. static enum sci_status isci_request_stp_request_construct(
  78. struct isci_request *request)
  79. {
  80. struct sas_task *task = isci_request_access_task(request);
  81. enum sci_status status;
  82. struct host_to_dev_fis *register_fis;
  83. dev_dbg(&request->isci_host->pdev->dev,
  84. "%s: request = %p\n",
  85. __func__,
  86. request);
  87. /* Get the host_to_dev_fis from the core and copy
  88. * the fis from the task into it.
  89. */
  90. register_fis = isci_sata_task_to_fis_copy(task);
  91. status = scic_io_request_construct_basic_sata(&request->sci);
  92. /* Set the ncq tag in the fis, from the queue
  93. * command in the task.
  94. */
  95. if (isci_sata_is_task_ncq(task)) {
  96. isci_sata_set_ncq_tag(
  97. register_fis,
  98. task
  99. );
  100. }
  101. return status;
  102. }
  103. /*
  104. * isci_smp_request_build() - This function builds the smp request.
  105. * @ireq: This parameter points to the isci_request allocated in the
  106. * request construct function.
  107. *
  108. * SCI_SUCCESS on successfull completion, or specific failure code.
  109. */
  110. static enum sci_status isci_smp_request_build(struct isci_request *ireq)
  111. {
  112. enum sci_status status = SCI_FAILURE;
  113. struct sas_task *task = isci_request_access_task(ireq);
  114. struct scic_sds_request *sci_req = &ireq->sci;
  115. dev_dbg(&ireq->isci_host->pdev->dev,
  116. "%s: request = %p\n", __func__, ireq);
  117. dev_dbg(&ireq->isci_host->pdev->dev,
  118. "%s: smp_req len = %d\n",
  119. __func__,
  120. task->smp_task.smp_req.length);
  121. /* copy the smp_command to the address; */
  122. sg_copy_to_buffer(&task->smp_task.smp_req, 1,
  123. &sci_req->smp.cmd,
  124. sizeof(struct smp_req));
  125. status = scic_io_request_construct_smp(sci_req);
  126. if (status != SCI_SUCCESS)
  127. dev_warn(&ireq->isci_host->pdev->dev,
  128. "%s: failed with status = %d\n",
  129. __func__,
  130. status);
  131. return status;
  132. }
  133. /**
  134. * isci_io_request_build() - This function builds the io request object.
  135. * @isci_host: This parameter specifies the ISCI host object
  136. * @request: This parameter points to the isci_request object allocated in the
  137. * request construct function.
  138. * @sci_device: This parameter is the handle for the sci core's remote device
  139. * object that is the destination for this request.
  140. *
  141. * SCI_SUCCESS on successfull completion, or specific failure code.
  142. */
  143. static enum sci_status isci_io_request_build(
  144. struct isci_host *isci_host,
  145. struct isci_request *request,
  146. struct isci_remote_device *isci_device)
  147. {
  148. enum sci_status status = SCI_SUCCESS;
  149. struct sas_task *task = isci_request_access_task(request);
  150. struct scic_sds_remote_device *sci_device = &isci_device->sci;
  151. dev_dbg(&isci_host->pdev->dev,
  152. "%s: isci_device = 0x%p; request = %p, "
  153. "num_scatter = %d\n",
  154. __func__,
  155. isci_device,
  156. request,
  157. task->num_scatter);
  158. /* map the sgl addresses, if present.
  159. * libata does the mapping for sata devices
  160. * before we get the request.
  161. */
  162. if (task->num_scatter &&
  163. !sas_protocol_ata(task->task_proto) &&
  164. !(SAS_PROTOCOL_SMP & task->task_proto)) {
  165. request->num_sg_entries = dma_map_sg(
  166. &isci_host->pdev->dev,
  167. task->scatter,
  168. task->num_scatter,
  169. task->data_dir
  170. );
  171. if (request->num_sg_entries == 0)
  172. return SCI_FAILURE_INSUFFICIENT_RESOURCES;
  173. }
  174. /* build the common request object. For now,
  175. * we will let the core allocate the IO tag.
  176. */
  177. status = scic_io_request_construct(&isci_host->sci, sci_device,
  178. SCI_CONTROLLER_INVALID_IO_TAG,
  179. &request->sci);
  180. if (status != SCI_SUCCESS) {
  181. dev_warn(&isci_host->pdev->dev,
  182. "%s: failed request construct\n",
  183. __func__);
  184. return SCI_FAILURE;
  185. }
  186. switch (task->task_proto) {
  187. case SAS_PROTOCOL_SMP:
  188. status = isci_smp_request_build(request);
  189. break;
  190. case SAS_PROTOCOL_SSP:
  191. status = isci_request_ssp_request_construct(request);
  192. break;
  193. case SAS_PROTOCOL_SATA:
  194. case SAS_PROTOCOL_STP:
  195. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  196. status = isci_request_stp_request_construct(request);
  197. break;
  198. default:
  199. dev_warn(&isci_host->pdev->dev,
  200. "%s: unknown protocol\n", __func__);
  201. return SCI_FAILURE;
  202. }
  203. return SCI_SUCCESS;
  204. }
  205. /**
  206. * isci_request_alloc_core() - This function gets the request object from the
  207. * isci_host dma cache.
  208. * @isci_host: This parameter specifies the ISCI host object
  209. * @isci_request: This parameter will contain the pointer to the new
  210. * isci_request object.
  211. * @isci_device: This parameter is the pointer to the isci remote device object
  212. * that is the destination for this request.
  213. * @gfp_flags: This parameter specifies the os allocation flags.
  214. *
  215. * SCI_SUCCESS on successfull completion, or specific failure code.
  216. */
  217. static int isci_request_alloc_core(
  218. struct isci_host *isci_host,
  219. struct isci_request **isci_request,
  220. struct isci_remote_device *isci_device,
  221. gfp_t gfp_flags)
  222. {
  223. int ret = 0;
  224. dma_addr_t handle;
  225. struct isci_request *request;
  226. /* get pointer to dma memory. This actually points
  227. * to both the isci_remote_device object and the
  228. * sci object. The isci object is at the beginning
  229. * of the memory allocated here.
  230. */
  231. request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
  232. if (!request) {
  233. dev_warn(&isci_host->pdev->dev,
  234. "%s: dma_pool_alloc returned NULL\n", __func__);
  235. return -ENOMEM;
  236. }
  237. /* initialize the request object. */
  238. spin_lock_init(&request->state_lock);
  239. request->request_daddr = handle;
  240. request->isci_host = isci_host;
  241. request->isci_device = isci_device;
  242. request->io_request_completion = NULL;
  243. request->terminated = false;
  244. request->num_sg_entries = 0;
  245. request->complete_in_target = false;
  246. INIT_LIST_HEAD(&request->completed_node);
  247. INIT_LIST_HEAD(&request->dev_node);
  248. *isci_request = request;
  249. isci_request_change_state(request, allocated);
  250. return ret;
  251. }
  252. static int isci_request_alloc_io(
  253. struct isci_host *isci_host,
  254. struct sas_task *task,
  255. struct isci_request **isci_request,
  256. struct isci_remote_device *isci_device,
  257. gfp_t gfp_flags)
  258. {
  259. int retval = isci_request_alloc_core(isci_host, isci_request,
  260. isci_device, gfp_flags);
  261. if (!retval) {
  262. (*isci_request)->ttype_ptr.io_task_ptr = task;
  263. (*isci_request)->ttype = io_task;
  264. task->lldd_task = *isci_request;
  265. }
  266. return retval;
  267. }
  268. /**
  269. * isci_request_alloc_tmf() - This function gets the request object from the
  270. * isci_host dma cache and initializes the relevant fields as a sas_task.
  271. * @isci_host: This parameter specifies the ISCI host object
  272. * @sas_task: This parameter is the task struct from the upper layer driver.
  273. * @isci_request: This parameter will contain the pointer to the new
  274. * isci_request object.
  275. * @isci_device: This parameter is the pointer to the isci remote device object
  276. * that is the destination for this request.
  277. * @gfp_flags: This parameter specifies the os allocation flags.
  278. *
  279. * SCI_SUCCESS on successfull completion, or specific failure code.
  280. */
  281. int isci_request_alloc_tmf(
  282. struct isci_host *isci_host,
  283. struct isci_tmf *isci_tmf,
  284. struct isci_request **isci_request,
  285. struct isci_remote_device *isci_device,
  286. gfp_t gfp_flags)
  287. {
  288. int retval = isci_request_alloc_core(isci_host, isci_request,
  289. isci_device, gfp_flags);
  290. if (!retval) {
  291. (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
  292. (*isci_request)->ttype = tmf_task;
  293. }
  294. return retval;
  295. }
  296. /**
  297. * isci_request_execute() - This function allocates the isci_request object,
  298. * all fills in some common fields.
  299. * @isci_host: This parameter specifies the ISCI host object
  300. * @sas_task: This parameter is the task struct from the upper layer driver.
  301. * @isci_request: This parameter will contain the pointer to the new
  302. * isci_request object.
  303. * @gfp_flags: This parameter specifies the os allocation flags.
  304. *
  305. * SCI_SUCCESS on successfull completion, or specific failure code.
  306. */
  307. int isci_request_execute(
  308. struct isci_host *isci_host,
  309. struct sas_task *task,
  310. struct isci_request **isci_request,
  311. gfp_t gfp_flags)
  312. {
  313. int ret = 0;
  314. struct scic_sds_remote_device *sci_device;
  315. enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
  316. struct isci_remote_device *isci_device;
  317. struct isci_request *request;
  318. unsigned long flags;
  319. isci_device = task->dev->lldd_dev;
  320. sci_device = &isci_device->sci;
  321. /* do common allocation and init of request object. */
  322. ret = isci_request_alloc_io(
  323. isci_host,
  324. task,
  325. &request,
  326. isci_device,
  327. gfp_flags
  328. );
  329. if (ret)
  330. goto out;
  331. status = isci_io_request_build(isci_host, request, isci_device);
  332. if (status != SCI_SUCCESS) {
  333. dev_warn(&isci_host->pdev->dev,
  334. "%s: request_construct failed - status = 0x%x\n",
  335. __func__,
  336. status);
  337. goto out;
  338. }
  339. spin_lock_irqsave(&isci_host->scic_lock, flags);
  340. /* send the request, let the core assign the IO TAG. */
  341. status = scic_controller_start_io(&isci_host->sci, sci_device,
  342. &request->sci,
  343. SCI_CONTROLLER_INVALID_IO_TAG);
  344. if (status != SCI_SUCCESS &&
  345. status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  346. dev_warn(&isci_host->pdev->dev,
  347. "%s: failed request start (0x%x)\n",
  348. __func__, status);
  349. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  350. goto out;
  351. }
  352. /* Either I/O started OK, or the core has signaled that
  353. * the device needs a target reset.
  354. *
  355. * In either case, hold onto the I/O for later.
  356. *
  357. * Update it's status and add it to the list in the
  358. * remote device object.
  359. */
  360. isci_request_change_state(request, started);
  361. list_add(&request->dev_node, &isci_device->reqs_in_process);
  362. if (status == SCI_SUCCESS) {
  363. /* Save the tag for possible task mgmt later. */
  364. request->io_tag = scic_io_request_get_io_tag(&request->sci);
  365. } else {
  366. /* The request did not really start in the
  367. * hardware, so clear the request handle
  368. * here so no terminations will be done.
  369. */
  370. request->terminated = true;
  371. }
  372. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  373. if (status ==
  374. SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
  375. /* Signal libsas that we need the SCSI error
  376. * handler thread to work on this I/O and that
  377. * we want a device reset.
  378. */
  379. spin_lock_irqsave(&task->task_state_lock, flags);
  380. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  381. spin_unlock_irqrestore(&task->task_state_lock, flags);
  382. /* Cause this task to be scheduled in the SCSI error
  383. * handler thread.
  384. */
  385. isci_execpath_callback(isci_host, task,
  386. sas_task_abort);
  387. /* Change the status, since we are holding
  388. * the I/O until it is managed by the SCSI
  389. * error handler.
  390. */
  391. status = SCI_SUCCESS;
  392. }
  393. out:
  394. if (status != SCI_SUCCESS) {
  395. /* release dma memory on failure. */
  396. isci_request_free(isci_host, request);
  397. request = NULL;
  398. ret = SCI_FAILURE;
  399. }
  400. *isci_request = request;
  401. return ret;
  402. }
  403. /**
  404. * isci_request_process_response_iu() - This function sets the status and
  405. * response iu, in the task struct, from the request object for the upper
  406. * layer driver.
  407. * @sas_task: This parameter is the task struct from the upper layer driver.
  408. * @resp_iu: This parameter points to the response iu of the completed request.
  409. * @dev: This parameter specifies the linux device struct.
  410. *
  411. * none.
  412. */
  413. static void isci_request_process_response_iu(
  414. struct sas_task *task,
  415. struct ssp_response_iu *resp_iu,
  416. struct device *dev)
  417. {
  418. dev_dbg(dev,
  419. "%s: resp_iu = %p "
  420. "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
  421. "resp_iu->response_data_len = %x, "
  422. "resp_iu->sense_data_len = %x\nrepsonse data: ",
  423. __func__,
  424. resp_iu,
  425. resp_iu->status,
  426. resp_iu->datapres,
  427. resp_iu->response_data_len,
  428. resp_iu->sense_data_len);
  429. task->task_status.stat = resp_iu->status;
  430. /* libsas updates the task status fields based on the response iu. */
  431. sas_ssp_task_response(dev, task, resp_iu);
  432. }
  433. /**
  434. * isci_request_set_open_reject_status() - This function prepares the I/O
  435. * completion for OPEN_REJECT conditions.
  436. * @request: This parameter is the completed isci_request object.
  437. * @response_ptr: This parameter specifies the service response for the I/O.
  438. * @status_ptr: This parameter specifies the exec status for the I/O.
  439. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  440. * the LLDD with respect to completing this request or forcing an abort
  441. * condition on the I/O.
  442. * @open_rej_reason: This parameter specifies the encoded reason for the
  443. * abandon-class reject.
  444. *
  445. * none.
  446. */
  447. static void isci_request_set_open_reject_status(
  448. struct isci_request *request,
  449. struct sas_task *task,
  450. enum service_response *response_ptr,
  451. enum exec_status *status_ptr,
  452. enum isci_completion_selection *complete_to_host_ptr,
  453. enum sas_open_rej_reason open_rej_reason)
  454. {
  455. /* Task in the target is done. */
  456. request->complete_in_target = true;
  457. *response_ptr = SAS_TASK_UNDELIVERED;
  458. *status_ptr = SAS_OPEN_REJECT;
  459. *complete_to_host_ptr = isci_perform_normal_io_completion;
  460. task->task_status.open_rej_reason = open_rej_reason;
  461. }
  462. /**
  463. * isci_request_handle_controller_specific_errors() - This function decodes
  464. * controller-specific I/O completion error conditions.
  465. * @request: This parameter is the completed isci_request object.
  466. * @response_ptr: This parameter specifies the service response for the I/O.
  467. * @status_ptr: This parameter specifies the exec status for the I/O.
  468. * @complete_to_host_ptr: This parameter specifies the action to be taken by
  469. * the LLDD with respect to completing this request or forcing an abort
  470. * condition on the I/O.
  471. *
  472. * none.
  473. */
  474. static void isci_request_handle_controller_specific_errors(
  475. struct isci_remote_device *isci_device,
  476. struct isci_request *request,
  477. struct sas_task *task,
  478. enum service_response *response_ptr,
  479. enum exec_status *status_ptr,
  480. enum isci_completion_selection *complete_to_host_ptr)
  481. {
  482. unsigned int cstatus;
  483. cstatus = scic_request_get_controller_status(&request->sci);
  484. dev_dbg(&request->isci_host->pdev->dev,
  485. "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
  486. "- controller status = 0x%x\n",
  487. __func__, request, cstatus);
  488. /* Decode the controller-specific errors; most
  489. * important is to recognize those conditions in which
  490. * the target may still have a task outstanding that
  491. * must be aborted.
  492. *
  493. * Note that there are SCU completion codes being
  494. * named in the decode below for which SCIC has already
  495. * done work to handle them in a way other than as
  496. * a controller-specific completion code; these are left
  497. * in the decode below for completeness sake.
  498. */
  499. switch (cstatus) {
  500. case SCU_TASK_DONE_DMASETUP_DIRERR:
  501. /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
  502. case SCU_TASK_DONE_XFERCNT_ERR:
  503. /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
  504. if (task->task_proto == SAS_PROTOCOL_SMP) {
  505. /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
  506. *response_ptr = SAS_TASK_COMPLETE;
  507. /* See if the device has been/is being stopped. Note
  508. * that we ignore the quiesce state, since we are
  509. * concerned about the actual device state.
  510. */
  511. if ((isci_device->status == isci_stopping) ||
  512. (isci_device->status == isci_stopped))
  513. *status_ptr = SAS_DEVICE_UNKNOWN;
  514. else
  515. *status_ptr = SAS_ABORTED_TASK;
  516. request->complete_in_target = true;
  517. *complete_to_host_ptr =
  518. isci_perform_normal_io_completion;
  519. } else {
  520. /* Task in the target is not done. */
  521. *response_ptr = SAS_TASK_UNDELIVERED;
  522. if ((isci_device->status == isci_stopping) ||
  523. (isci_device->status == isci_stopped))
  524. *status_ptr = SAS_DEVICE_UNKNOWN;
  525. else
  526. *status_ptr = SAM_STAT_TASK_ABORTED;
  527. request->complete_in_target = false;
  528. *complete_to_host_ptr =
  529. isci_perform_error_io_completion;
  530. }
  531. break;
  532. case SCU_TASK_DONE_CRC_ERR:
  533. case SCU_TASK_DONE_NAK_CMD_ERR:
  534. case SCU_TASK_DONE_EXCESS_DATA:
  535. case SCU_TASK_DONE_UNEXP_FIS:
  536. /* Also SCU_TASK_DONE_UNEXP_RESP: */
  537. case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
  538. case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
  539. case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
  540. /* These are conditions in which the target
  541. * has completed the task, so that no cleanup
  542. * is necessary.
  543. */
  544. *response_ptr = SAS_TASK_COMPLETE;
  545. /* See if the device has been/is being stopped. Note
  546. * that we ignore the quiesce state, since we are
  547. * concerned about the actual device state.
  548. */
  549. if ((isci_device->status == isci_stopping) ||
  550. (isci_device->status == isci_stopped))
  551. *status_ptr = SAS_DEVICE_UNKNOWN;
  552. else
  553. *status_ptr = SAS_ABORTED_TASK;
  554. request->complete_in_target = true;
  555. *complete_to_host_ptr = isci_perform_normal_io_completion;
  556. break;
  557. /* Note that the only open reject completion codes seen here will be
  558. * abandon-class codes; all others are automatically retried in the SCU.
  559. */
  560. case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
  561. isci_request_set_open_reject_status(
  562. request, task, response_ptr, status_ptr,
  563. complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
  564. break;
  565. case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
  566. /* Note - the return of AB0 will change when
  567. * libsas implements detection of zone violations.
  568. */
  569. isci_request_set_open_reject_status(
  570. request, task, response_ptr, status_ptr,
  571. complete_to_host_ptr, SAS_OREJ_RESV_AB0);
  572. break;
  573. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
  574. isci_request_set_open_reject_status(
  575. request, task, response_ptr, status_ptr,
  576. complete_to_host_ptr, SAS_OREJ_RESV_AB1);
  577. break;
  578. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
  579. isci_request_set_open_reject_status(
  580. request, task, response_ptr, status_ptr,
  581. complete_to_host_ptr, SAS_OREJ_RESV_AB2);
  582. break;
  583. case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
  584. isci_request_set_open_reject_status(
  585. request, task, response_ptr, status_ptr,
  586. complete_to_host_ptr, SAS_OREJ_RESV_AB3);
  587. break;
  588. case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
  589. isci_request_set_open_reject_status(
  590. request, task, response_ptr, status_ptr,
  591. complete_to_host_ptr, SAS_OREJ_BAD_DEST);
  592. break;
  593. case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
  594. isci_request_set_open_reject_status(
  595. request, task, response_ptr, status_ptr,
  596. complete_to_host_ptr, SAS_OREJ_STP_NORES);
  597. break;
  598. case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
  599. isci_request_set_open_reject_status(
  600. request, task, response_ptr, status_ptr,
  601. complete_to_host_ptr, SAS_OREJ_EPROTO);
  602. break;
  603. case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
  604. isci_request_set_open_reject_status(
  605. request, task, response_ptr, status_ptr,
  606. complete_to_host_ptr, SAS_OREJ_CONN_RATE);
  607. break;
  608. case SCU_TASK_DONE_LL_R_ERR:
  609. /* Also SCU_TASK_DONE_ACK_NAK_TO: */
  610. case SCU_TASK_DONE_LL_PERR:
  611. case SCU_TASK_DONE_LL_SY_TERM:
  612. /* Also SCU_TASK_DONE_NAK_ERR:*/
  613. case SCU_TASK_DONE_LL_LF_TERM:
  614. /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
  615. case SCU_TASK_DONE_LL_ABORT_ERR:
  616. case SCU_TASK_DONE_SEQ_INV_TYPE:
  617. /* Also SCU_TASK_DONE_UNEXP_XR: */
  618. case SCU_TASK_DONE_XR_IU_LEN_ERR:
  619. case SCU_TASK_DONE_INV_FIS_LEN:
  620. /* Also SCU_TASK_DONE_XR_WD_LEN: */
  621. case SCU_TASK_DONE_SDMA_ERR:
  622. case SCU_TASK_DONE_OFFSET_ERR:
  623. case SCU_TASK_DONE_MAX_PLD_ERR:
  624. case SCU_TASK_DONE_LF_ERR:
  625. case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
  626. case SCU_TASK_DONE_SMP_LL_RX_ERR:
  627. case SCU_TASK_DONE_UNEXP_DATA:
  628. case SCU_TASK_DONE_UNEXP_SDBFIS:
  629. case SCU_TASK_DONE_REG_ERR:
  630. case SCU_TASK_DONE_SDB_ERR:
  631. case SCU_TASK_DONE_TASK_ABORT:
  632. default:
  633. /* Task in the target is not done. */
  634. *response_ptr = SAS_TASK_UNDELIVERED;
  635. *status_ptr = SAM_STAT_TASK_ABORTED;
  636. request->complete_in_target = false;
  637. *complete_to_host_ptr = isci_perform_error_io_completion;
  638. break;
  639. }
  640. }
  641. /**
  642. * isci_task_save_for_upper_layer_completion() - This function saves the
  643. * request for later completion to the upper layer driver.
  644. * @host: This parameter is a pointer to the host on which the the request
  645. * should be queued (either as an error or success).
  646. * @request: This parameter is the completed request.
  647. * @response: This parameter is the response code for the completed task.
  648. * @status: This parameter is the status code for the completed task.
  649. *
  650. * none.
  651. */
  652. static void isci_task_save_for_upper_layer_completion(
  653. struct isci_host *host,
  654. struct isci_request *request,
  655. enum service_response response,
  656. enum exec_status status,
  657. enum isci_completion_selection task_notification_selection)
  658. {
  659. struct sas_task *task = isci_request_access_task(request);
  660. task_notification_selection
  661. = isci_task_set_completion_status(task, response, status,
  662. task_notification_selection);
  663. /* Tasks aborted specifically by a call to the lldd_abort_task
  664. * function should not be completed to the host in the regular path.
  665. */
  666. switch (task_notification_selection) {
  667. case isci_perform_normal_io_completion:
  668. /* Normal notification (task_done) */
  669. dev_dbg(&host->pdev->dev,
  670. "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
  671. __func__,
  672. task,
  673. task->task_status.resp, response,
  674. task->task_status.stat, status);
  675. /* Add to the completed list. */
  676. list_add(&request->completed_node,
  677. &host->requests_to_complete);
  678. /* Take the request off the device's pending request list. */
  679. list_del_init(&request->dev_node);
  680. break;
  681. case isci_perform_aborted_io_completion:
  682. /* No notification to libsas because this request is
  683. * already in the abort path.
  684. */
  685. dev_warn(&host->pdev->dev,
  686. "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
  687. __func__,
  688. task,
  689. task->task_status.resp, response,
  690. task->task_status.stat, status);
  691. /* Wake up whatever process was waiting for this
  692. * request to complete.
  693. */
  694. WARN_ON(request->io_request_completion == NULL);
  695. if (request->io_request_completion != NULL) {
  696. /* Signal whoever is waiting that this
  697. * request is complete.
  698. */
  699. complete(request->io_request_completion);
  700. }
  701. break;
  702. case isci_perform_error_io_completion:
  703. /* Use sas_task_abort */
  704. dev_warn(&host->pdev->dev,
  705. "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
  706. __func__,
  707. task,
  708. task->task_status.resp, response,
  709. task->task_status.stat, status);
  710. /* Add to the aborted list. */
  711. list_add(&request->completed_node,
  712. &host->requests_to_errorback);
  713. break;
  714. default:
  715. dev_warn(&host->pdev->dev,
  716. "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
  717. __func__,
  718. task,
  719. task->task_status.resp, response,
  720. task->task_status.stat, status);
  721. /* Add to the error to libsas list. */
  722. list_add(&request->completed_node,
  723. &host->requests_to_errorback);
  724. break;
  725. }
  726. }
  727. /**
  728. * isci_request_io_request_complete() - This function is called by the sci core
  729. * when an io request completes.
  730. * @isci_host: This parameter specifies the ISCI host object
  731. * @request: This parameter is the completed isci_request object.
  732. * @completion_status: This parameter specifies the completion status from the
  733. * sci core.
  734. *
  735. * none.
  736. */
  737. void isci_request_io_request_complete(
  738. struct isci_host *isci_host,
  739. struct isci_request *request,
  740. enum sci_io_status completion_status)
  741. {
  742. struct sas_task *task = isci_request_access_task(request);
  743. struct ssp_response_iu *resp_iu;
  744. void *resp_buf;
  745. unsigned long task_flags;
  746. struct isci_remote_device *isci_device = request->isci_device;
  747. enum service_response response = SAS_TASK_UNDELIVERED;
  748. enum exec_status status = SAS_ABORTED_TASK;
  749. enum isci_request_status request_status;
  750. enum isci_completion_selection complete_to_host
  751. = isci_perform_normal_io_completion;
  752. dev_dbg(&isci_host->pdev->dev,
  753. "%s: request = %p, task = %p,\n"
  754. "task->data_dir = %d completion_status = 0x%x\n",
  755. __func__,
  756. request,
  757. task,
  758. task->data_dir,
  759. completion_status);
  760. spin_lock(&request->state_lock);
  761. request_status = isci_request_get_state(request);
  762. /* Decode the request status. Note that if the request has been
  763. * aborted by a task management function, we don't care
  764. * what the status is.
  765. */
  766. switch (request_status) {
  767. case aborted:
  768. /* "aborted" indicates that the request was aborted by a task
  769. * management function, since once a task management request is
  770. * perfomed by the device, the request only completes because
  771. * of the subsequent driver terminate.
  772. *
  773. * Aborted also means an external thread is explicitly managing
  774. * this request, so that we do not complete it up the stack.
  775. *
  776. * The target is still there (since the TMF was successful).
  777. */
  778. request->complete_in_target = true;
  779. response = SAS_TASK_COMPLETE;
  780. /* See if the device has been/is being stopped. Note
  781. * that we ignore the quiesce state, since we are
  782. * concerned about the actual device state.
  783. */
  784. if ((isci_device->status == isci_stopping)
  785. || (isci_device->status == isci_stopped)
  786. )
  787. status = SAS_DEVICE_UNKNOWN;
  788. else
  789. status = SAS_ABORTED_TASK;
  790. complete_to_host = isci_perform_aborted_io_completion;
  791. /* This was an aborted request. */
  792. spin_unlock(&request->state_lock);
  793. break;
  794. case aborting:
  795. /* aborting means that the task management function tried and
  796. * failed to abort the request. We need to note the request
  797. * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
  798. * target as down.
  799. *
  800. * Aborting also means an external thread is explicitly managing
  801. * this request, so that we do not complete it up the stack.
  802. */
  803. request->complete_in_target = true;
  804. response = SAS_TASK_UNDELIVERED;
  805. if ((isci_device->status == isci_stopping) ||
  806. (isci_device->status == isci_stopped))
  807. /* The device has been /is being stopped. Note that
  808. * we ignore the quiesce state, since we are
  809. * concerned about the actual device state.
  810. */
  811. status = SAS_DEVICE_UNKNOWN;
  812. else
  813. status = SAS_PHY_DOWN;
  814. complete_to_host = isci_perform_aborted_io_completion;
  815. /* This was an aborted request. */
  816. spin_unlock(&request->state_lock);
  817. break;
  818. case terminating:
  819. /* This was an terminated request. This happens when
  820. * the I/O is being terminated because of an action on
  821. * the device (reset, tear down, etc.), and the I/O needs
  822. * to be completed up the stack.
  823. */
  824. request->complete_in_target = true;
  825. response = SAS_TASK_UNDELIVERED;
  826. /* See if the device has been/is being stopped. Note
  827. * that we ignore the quiesce state, since we are
  828. * concerned about the actual device state.
  829. */
  830. if ((isci_device->status == isci_stopping) ||
  831. (isci_device->status == isci_stopped))
  832. status = SAS_DEVICE_UNKNOWN;
  833. else
  834. status = SAS_ABORTED_TASK;
  835. complete_to_host = isci_perform_aborted_io_completion;
  836. /* This was a terminated request. */
  837. spin_unlock(&request->state_lock);
  838. break;
  839. default:
  840. /* The request is done from an SCU HW perspective. */
  841. request->status = completed;
  842. spin_unlock(&request->state_lock);
  843. /* This is an active request being completed from the core. */
  844. switch (completion_status) {
  845. case SCI_IO_FAILURE_RESPONSE_VALID:
  846. dev_dbg(&isci_host->pdev->dev,
  847. "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
  848. __func__,
  849. request,
  850. task);
  851. if (sas_protocol_ata(task->task_proto)) {
  852. resp_buf = &request->sci.stp.rsp;
  853. isci_request_process_stp_response(task,
  854. resp_buf);
  855. } else if (SAS_PROTOCOL_SSP == task->task_proto) {
  856. /* crack the iu response buffer. */
  857. resp_iu = &request->sci.ssp.rsp;
  858. isci_request_process_response_iu(task, resp_iu,
  859. &isci_host->pdev->dev);
  860. } else if (SAS_PROTOCOL_SMP == task->task_proto) {
  861. dev_err(&isci_host->pdev->dev,
  862. "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
  863. "SAS_PROTOCOL_SMP protocol\n",
  864. __func__);
  865. } else
  866. dev_err(&isci_host->pdev->dev,
  867. "%s: unknown protocol\n", __func__);
  868. /* use the task status set in the task struct by the
  869. * isci_request_process_response_iu call.
  870. */
  871. request->complete_in_target = true;
  872. response = task->task_status.resp;
  873. status = task->task_status.stat;
  874. break;
  875. case SCI_IO_SUCCESS:
  876. case SCI_IO_SUCCESS_IO_DONE_EARLY:
  877. response = SAS_TASK_COMPLETE;
  878. status = SAM_STAT_GOOD;
  879. request->complete_in_target = true;
  880. if (task->task_proto == SAS_PROTOCOL_SMP) {
  881. void *rsp = &request->sci.smp.rsp;
  882. dev_dbg(&isci_host->pdev->dev,
  883. "%s: SMP protocol completion\n",
  884. __func__);
  885. sg_copy_from_buffer(
  886. &task->smp_task.smp_resp, 1,
  887. rsp, sizeof(struct smp_resp));
  888. } else if (completion_status
  889. == SCI_IO_SUCCESS_IO_DONE_EARLY) {
  890. /* This was an SSP / STP / SATA transfer.
  891. * There is a possibility that less data than
  892. * the maximum was transferred.
  893. */
  894. u32 transferred_length
  895. = scic_io_request_get_number_of_bytes_transferred(&request->sci);
  896. task->task_status.residual
  897. = task->total_xfer_len - transferred_length;
  898. /* If there were residual bytes, call this an
  899. * underrun.
  900. */
  901. if (task->task_status.residual != 0)
  902. status = SAS_DATA_UNDERRUN;
  903. dev_dbg(&isci_host->pdev->dev,
  904. "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
  905. __func__,
  906. status);
  907. } else
  908. dev_dbg(&isci_host->pdev->dev,
  909. "%s: SCI_IO_SUCCESS\n",
  910. __func__);
  911. break;
  912. case SCI_IO_FAILURE_TERMINATED:
  913. dev_dbg(&isci_host->pdev->dev,
  914. "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
  915. __func__,
  916. request,
  917. task);
  918. /* The request was terminated explicitly. No handling
  919. * is needed in the SCSI error handler path.
  920. */
  921. request->complete_in_target = true;
  922. response = SAS_TASK_UNDELIVERED;
  923. /* See if the device has been/is being stopped. Note
  924. * that we ignore the quiesce state, since we are
  925. * concerned about the actual device state.
  926. */
  927. if ((isci_device->status == isci_stopping) ||
  928. (isci_device->status == isci_stopped))
  929. status = SAS_DEVICE_UNKNOWN;
  930. else
  931. status = SAS_ABORTED_TASK;
  932. complete_to_host = isci_perform_normal_io_completion;
  933. break;
  934. case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
  935. isci_request_handle_controller_specific_errors(
  936. isci_device, request, task, &response, &status,
  937. &complete_to_host);
  938. break;
  939. case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
  940. /* This is a special case, in that the I/O completion
  941. * is telling us that the device needs a reset.
  942. * In order for the device reset condition to be
  943. * noticed, the I/O has to be handled in the error
  944. * handler. Set the reset flag and cause the
  945. * SCSI error thread to be scheduled.
  946. */
  947. spin_lock_irqsave(&task->task_state_lock, task_flags);
  948. task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
  949. spin_unlock_irqrestore(&task->task_state_lock, task_flags);
  950. /* Fail the I/O. */
  951. response = SAS_TASK_UNDELIVERED;
  952. status = SAM_STAT_TASK_ABORTED;
  953. complete_to_host = isci_perform_error_io_completion;
  954. request->complete_in_target = false;
  955. break;
  956. default:
  957. /* Catch any otherwise unhandled error codes here. */
  958. dev_warn(&isci_host->pdev->dev,
  959. "%s: invalid completion code: 0x%x - "
  960. "isci_request = %p\n",
  961. __func__, completion_status, request);
  962. response = SAS_TASK_UNDELIVERED;
  963. /* See if the device has been/is being stopped. Note
  964. * that we ignore the quiesce state, since we are
  965. * concerned about the actual device state.
  966. */
  967. if ((isci_device->status == isci_stopping) ||
  968. (isci_device->status == isci_stopped))
  969. status = SAS_DEVICE_UNKNOWN;
  970. else
  971. status = SAS_ABORTED_TASK;
  972. complete_to_host = isci_perform_error_io_completion;
  973. request->complete_in_target = false;
  974. break;
  975. }
  976. break;
  977. }
  978. isci_request_unmap_sgl(request, isci_host->pdev);
  979. /* Put the completed request on the correct list */
  980. isci_task_save_for_upper_layer_completion(isci_host, request, response,
  981. status, complete_to_host
  982. );
  983. /* complete the io request to the core. */
  984. scic_controller_complete_io(&isci_host->sci,
  985. &isci_device->sci,
  986. &request->sci);
  987. /* set terminated handle so it cannot be completed or
  988. * terminated again, and to cause any calls into abort
  989. * task to recognize the already completed case.
  990. */
  991. request->terminated = true;
  992. isci_host_can_dequeue(isci_host, 1);
  993. }