request.c 36 KB

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