request.c 35 KB

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