request.c 34 KB

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