request.c 35 KB

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