blob: a58072807a37a34c97a2f9c7c8cbddb7a5d26337 [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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
56#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "scic_io_request.h"
58#include "scic_task_request.h"
59#include "scic_port.h"
60#include "task.h"
61#include "request.h"
62#include "sata.h"
63#include "scu_completion_codes.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070064#include "scic_sds_request.h"
Dan Williamsce2b3262011-05-08 15:49:15 -070065#include "scic_controller.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070066#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070067
68static enum sci_status isci_request_ssp_request_construct(
69 struct isci_request *request)
70{
71 enum sci_status status;
72
73 dev_dbg(&request->isci_host->pdev->dev,
74 "%s: request = %p\n",
75 __func__,
76 request);
Dan Williams67ea8382011-05-08 11:47:15 -070077 status = scic_io_request_construct_basic_ssp(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -070078 return status;
79}
80
81static enum sci_status isci_request_stp_request_construct(
82 struct isci_request *request)
83{
84 struct sas_task *task = isci_request_access_task(request);
85 enum sci_status status;
86 struct host_to_dev_fis *register_fis;
87
88 dev_dbg(&request->isci_host->pdev->dev,
89 "%s: request = %p\n",
90 __func__,
91 request);
92
93 /* Get the host_to_dev_fis from the core and copy
94 * the fis from the task into it.
95 */
96 register_fis = isci_sata_task_to_fis_copy(task);
97
Dan Williams67ea8382011-05-08 11:47:15 -070098 status = scic_io_request_construct_basic_sata(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -070099
100 /* Set the ncq tag in the fis, from the queue
101 * command in the task.
102 */
103 if (isci_sata_is_task_ncq(task)) {
104
105 isci_sata_set_ncq_tag(
106 register_fis,
107 task
108 );
109 }
110
111 return status;
112}
113
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700114/*
115 * isci_smp_request_build() - This function builds the smp request.
116 * @ireq: This parameter points to the isci_request allocated in the
Dan Williams6f231dd2011-07-02 22:56:22 -0700117 * request construct function.
Dan Williams6f231dd2011-07-02 22:56:22 -0700118 *
119 * SCI_SUCCESS on successfull completion, or specific failure code.
120 */
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700121static enum sci_status isci_smp_request_build(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700122{
123 enum sci_status status = SCI_FAILURE;
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700124 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams67ea8382011-05-08 11:47:15 -0700125 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700126
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700127 dev_dbg(&ireq->isci_host->pdev->dev,
128 "%s: request = %p\n", __func__, ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700129
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700130 dev_dbg(&ireq->isci_host->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700131 "%s: smp_req len = %d\n",
132 __func__,
133 task->smp_task.smp_req.length);
134
135 /* copy the smp_command to the address; */
136 sg_copy_to_buffer(&task->smp_task.smp_req, 1,
Dan Williamsb7645812011-05-08 02:35:32 -0700137 &sci_req->smp.cmd,
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700138 sizeof(struct smp_req));
Dan Williams6f231dd2011-07-02 22:56:22 -0700139
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700140 status = scic_io_request_construct_smp(sci_req);
Dan Williams6f231dd2011-07-02 22:56:22 -0700141 if (status != SCI_SUCCESS)
Dave Jiang2ec53eb2011-05-04 18:01:22 -0700142 dev_warn(&ireq->isci_host->pdev->dev,
143 "%s: failed with status = %d\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700144 __func__,
145 status);
146
147 return status;
148}
149
150/**
151 * isci_io_request_build() - This function builds the io request object.
152 * @isci_host: This parameter specifies the ISCI host object
153 * @request: This parameter points to the isci_request object allocated in the
154 * request construct function.
155 * @sci_device: This parameter is the handle for the sci core's remote device
156 * object that is the destination for this request.
157 *
158 * SCI_SUCCESS on successfull completion, or specific failure code.
159 */
160static enum sci_status isci_io_request_build(
161 struct isci_host *isci_host,
162 struct isci_request *request,
163 struct isci_remote_device *isci_device)
164{
Dan Williams6f231dd2011-07-02 22:56:22 -0700165 enum sci_status status = SCI_SUCCESS;
166 struct sas_task *task = isci_request_access_task(request);
Dan Williams57f20f42011-04-21 18:14:45 -0700167 struct scic_sds_remote_device *sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700168
169 dev_dbg(&isci_host->pdev->dev,
170 "%s: isci_device = 0x%p; request = %p, "
171 "num_scatter = %d\n",
172 __func__,
173 isci_device,
174 request,
175 task->num_scatter);
176
177 /* map the sgl addresses, if present.
178 * libata does the mapping for sata devices
179 * before we get the request.
180 */
181 if (task->num_scatter &&
182 !sas_protocol_ata(task->task_proto) &&
183 !(SAS_PROTOCOL_SMP & task->task_proto)) {
184
185 request->num_sg_entries = dma_map_sg(
186 &isci_host->pdev->dev,
187 task->scatter,
188 task->num_scatter,
189 task->data_dir
190 );
191
192 if (request->num_sg_entries == 0)
193 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
194 }
195
196 /* build the common request object. For now,
197 * we will let the core allocate the IO tag.
198 */
Dan Williams0d843662011-05-08 01:56:57 -0700199 status = scic_io_request_construct(&isci_host->sci, sci_device,
200 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams67ea8382011-05-08 11:47:15 -0700201 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700202
203 if (status != SCI_SUCCESS) {
204 dev_warn(&isci_host->pdev->dev,
205 "%s: failed request construct\n",
206 __func__);
207 return SCI_FAILURE;
208 }
209
Dan Williamsa1a113b2011-04-21 18:44:45 -0700210 switch (task->task_proto) {
211 case SAS_PROTOCOL_SMP:
Dan Williams6f231dd2011-07-02 22:56:22 -0700212 status = isci_smp_request_build(request);
Dan Williamsa1a113b2011-04-21 18:44:45 -0700213 break;
214 case SAS_PROTOCOL_SSP:
215 status = isci_request_ssp_request_construct(request);
216 break;
217 case SAS_PROTOCOL_SATA:
218 case SAS_PROTOCOL_STP:
219 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
220 status = isci_request_stp_request_construct(request);
221 break;
222 default:
Dan Williams6f231dd2011-07-02 22:56:22 -0700223 dev_warn(&isci_host->pdev->dev,
224 "%s: unknown protocol\n", __func__);
225 return SCI_FAILURE;
226 }
227
228 return SCI_SUCCESS;
229}
230
231
232/**
233 * isci_request_alloc_core() - This function gets the request object from the
234 * isci_host dma cache.
235 * @isci_host: This parameter specifies the ISCI host object
236 * @isci_request: This parameter will contain the pointer to the new
237 * isci_request object.
238 * @isci_device: This parameter is the pointer to the isci remote device object
239 * that is the destination for this request.
240 * @gfp_flags: This parameter specifies the os allocation flags.
241 *
242 * SCI_SUCCESS on successfull completion, or specific failure code.
243 */
244static int isci_request_alloc_core(
245 struct isci_host *isci_host,
246 struct isci_request **isci_request,
247 struct isci_remote_device *isci_device,
248 gfp_t gfp_flags)
249{
250 int ret = 0;
251 dma_addr_t handle;
252 struct isci_request *request;
253
254
255 /* get pointer to dma memory. This actually points
256 * to both the isci_remote_device object and the
257 * sci object. The isci object is at the beginning
258 * of the memory allocated here.
259 */
260 request = dma_pool_alloc(isci_host->dma_pool, gfp_flags, &handle);
261 if (!request) {
262 dev_warn(&isci_host->pdev->dev,
263 "%s: dma_pool_alloc returned NULL\n", __func__);
264 return -ENOMEM;
265 }
266
267 /* initialize the request object. */
268 spin_lock_init(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700269 request->request_daddr = handle;
270 request->isci_host = isci_host;
271 request->isci_device = isci_device;
272 request->io_request_completion = NULL;
Dan Williams67ea8382011-05-08 11:47:15 -0700273 request->terminated = false;
Dan Williams6f231dd2011-07-02 22:56:22 -0700274
Dan Williams6f231dd2011-07-02 22:56:22 -0700275 request->num_sg_entries = 0;
276
277 request->complete_in_target = false;
278
279 INIT_LIST_HEAD(&request->completed_node);
280 INIT_LIST_HEAD(&request->dev_node);
281
282 *isci_request = request;
Dan Williams83f5eee2011-02-18 09:25:15 -0800283 isci_request_change_state(request, allocated);
Dan Williams6f231dd2011-07-02 22:56:22 -0700284
285 return ret;
286}
287
288static int isci_request_alloc_io(
289 struct isci_host *isci_host,
290 struct sas_task *task,
291 struct isci_request **isci_request,
292 struct isci_remote_device *isci_device,
293 gfp_t gfp_flags)
294{
295 int retval = isci_request_alloc_core(isci_host, isci_request,
296 isci_device, gfp_flags);
297
298 if (!retval) {
299 (*isci_request)->ttype_ptr.io_task_ptr = task;
300 (*isci_request)->ttype = io_task;
301
302 task->lldd_task = *isci_request;
303 }
304 return retval;
305}
306
307/**
308 * isci_request_alloc_tmf() - This function gets the request object from the
309 * isci_host dma cache and initializes the relevant fields as a sas_task.
310 * @isci_host: This parameter specifies the ISCI host object
311 * @sas_task: This parameter is the task struct from the upper layer driver.
312 * @isci_request: This parameter will contain the pointer to the new
313 * isci_request object.
314 * @isci_device: This parameter is the pointer to the isci remote device object
315 * that is the destination for this request.
316 * @gfp_flags: This parameter specifies the os allocation flags.
317 *
318 * SCI_SUCCESS on successfull completion, or specific failure code.
319 */
320int isci_request_alloc_tmf(
321 struct isci_host *isci_host,
322 struct isci_tmf *isci_tmf,
323 struct isci_request **isci_request,
324 struct isci_remote_device *isci_device,
325 gfp_t gfp_flags)
326{
327 int retval = isci_request_alloc_core(isci_host, isci_request,
328 isci_device, gfp_flags);
329
330 if (!retval) {
331
332 (*isci_request)->ttype_ptr.tmf_task_ptr = isci_tmf;
333 (*isci_request)->ttype = tmf_task;
334 }
335 return retval;
336}
337
338/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700339 * isci_request_execute() - This function allocates the isci_request object,
340 * all fills in some common fields.
341 * @isci_host: This parameter specifies the ISCI host object
342 * @sas_task: This parameter is the task struct from the upper layer driver.
343 * @isci_request: This parameter will contain the pointer to the new
344 * isci_request object.
345 * @gfp_flags: This parameter specifies the os allocation flags.
346 *
347 * SCI_SUCCESS on successfull completion, or specific failure code.
348 */
349int isci_request_execute(
350 struct isci_host *isci_host,
351 struct sas_task *task,
352 struct isci_request **isci_request,
353 gfp_t gfp_flags)
354{
355 int ret = 0;
356 struct scic_sds_remote_device *sci_device;
357 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
358 struct isci_remote_device *isci_device;
359 struct isci_request *request;
360 unsigned long flags;
361
Dan Williams4393aa42011-03-31 13:10:44 -0700362 isci_device = task->dev->lldd_dev;
Dan Williams57f20f42011-04-21 18:14:45 -0700363 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700364
365 /* do common allocation and init of request object. */
366 ret = isci_request_alloc_io(
367 isci_host,
368 task,
369 &request,
370 isci_device,
371 gfp_flags
372 );
373
374 if (ret)
375 goto out;
376
377 status = isci_io_request_build(isci_host, request, isci_device);
Dan Williams67ea8382011-05-08 11:47:15 -0700378 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700379 dev_warn(&isci_host->pdev->dev,
380 "%s: request_construct failed - status = 0x%x\n",
381 __func__,
382 status);
Dan Williams67ea8382011-05-08 11:47:15 -0700383 goto out;
384 }
385
386 spin_lock_irqsave(&isci_host->scic_lock, flags);
387
388 /* send the request, let the core assign the IO TAG. */
389 status = scic_controller_start_io(&isci_host->sci, sci_device,
390 &request->sci,
391 SCI_CONTROLLER_INVALID_IO_TAG);
392 if (status != SCI_SUCCESS &&
393 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
394 dev_warn(&isci_host->pdev->dev,
395 "%s: failed request start (0x%x)\n",
396 __func__, status);
397 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
398 goto out;
399 }
400
401 /* Either I/O started OK, or the core has signaled that
402 * the device needs a target reset.
403 *
404 * In either case, hold onto the I/O for later.
405 *
406 * Update it's status and add it to the list in the
407 * remote device object.
408 */
409 isci_request_change_state(request, started);
410 list_add(&request->dev_node, &isci_device->reqs_in_process);
411
412 if (status == SCI_SUCCESS) {
413 /* Save the tag for possible task mgmt later. */
414 request->io_tag = scic_io_request_get_io_tag(&request->sci);
415 } else {
416 /* The request did not really start in the
417 * hardware, so clear the request handle
418 * here so no terminations will be done.
419 */
420 request->terminated = true;
421 }
422 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
423
424 if (status ==
425 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
426 /* Signal libsas that we need the SCSI error
427 * handler thread to work on this I/O and that
428 * we want a device reset.
429 */
430 spin_lock_irqsave(&task->task_state_lock, flags);
431 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
432 spin_unlock_irqrestore(&task->task_state_lock, flags);
433
434 /* Cause this task to be scheduled in the SCSI error
435 * handler thread.
436 */
437 isci_execpath_callback(isci_host, task,
438 sas_task_abort);
439
440 /* Change the status, since we are holding
441 * the I/O until it is managed by the SCSI
442 * error handler.
443 */
444 status = SCI_SUCCESS;
445 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700446
447 out:
448 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700449 /* release dma memory on failure. */
450 isci_request_free(isci_host, request);
451 request = NULL;
452 ret = SCI_FAILURE;
453 }
454
455 *isci_request = request;
456 return ret;
457}
458
459
460/**
461 * isci_request_process_response_iu() - This function sets the status and
462 * response iu, in the task struct, from the request object for the upper
463 * layer driver.
464 * @sas_task: This parameter is the task struct from the upper layer driver.
465 * @resp_iu: This parameter points to the response iu of the completed request.
466 * @dev: This parameter specifies the linux device struct.
467 *
468 * none.
469 */
470static void isci_request_process_response_iu(
471 struct sas_task *task,
472 struct ssp_response_iu *resp_iu,
473 struct device *dev)
474{
475 dev_dbg(dev,
476 "%s: resp_iu = %p "
477 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
478 "resp_iu->response_data_len = %x, "
479 "resp_iu->sense_data_len = %x\nrepsonse data: ",
480 __func__,
481 resp_iu,
482 resp_iu->status,
483 resp_iu->datapres,
484 resp_iu->response_data_len,
485 resp_iu->sense_data_len);
486
487 task->task_status.stat = resp_iu->status;
488
489 /* libsas updates the task status fields based on the response iu. */
490 sas_ssp_task_response(dev, task, resp_iu);
491}
492
493/**
494 * isci_request_set_open_reject_status() - This function prepares the I/O
495 * completion for OPEN_REJECT conditions.
496 * @request: This parameter is the completed isci_request object.
497 * @response_ptr: This parameter specifies the service response for the I/O.
498 * @status_ptr: This parameter specifies the exec status for the I/O.
499 * @complete_to_host_ptr: This parameter specifies the action to be taken by
500 * the LLDD with respect to completing this request or forcing an abort
501 * condition on the I/O.
502 * @open_rej_reason: This parameter specifies the encoded reason for the
503 * abandon-class reject.
504 *
505 * none.
506 */
507static void isci_request_set_open_reject_status(
508 struct isci_request *request,
509 struct sas_task *task,
510 enum service_response *response_ptr,
511 enum exec_status *status_ptr,
512 enum isci_completion_selection *complete_to_host_ptr,
513 enum sas_open_rej_reason open_rej_reason)
514{
515 /* Task in the target is done. */
516 request->complete_in_target = true;
517 *response_ptr = SAS_TASK_UNDELIVERED;
518 *status_ptr = SAS_OPEN_REJECT;
519 *complete_to_host_ptr = isci_perform_normal_io_completion;
520 task->task_status.open_rej_reason = open_rej_reason;
521}
522
523/**
524 * isci_request_handle_controller_specific_errors() - This function decodes
525 * controller-specific I/O completion error conditions.
526 * @request: This parameter is the completed isci_request object.
527 * @response_ptr: This parameter specifies the service response for the I/O.
528 * @status_ptr: This parameter specifies the exec status for the I/O.
529 * @complete_to_host_ptr: This parameter specifies the action to be taken by
530 * the LLDD with respect to completing this request or forcing an abort
531 * condition on the I/O.
532 *
533 * none.
534 */
535static void isci_request_handle_controller_specific_errors(
536 struct isci_remote_device *isci_device,
537 struct isci_request *request,
538 struct sas_task *task,
539 enum service_response *response_ptr,
540 enum exec_status *status_ptr,
541 enum isci_completion_selection *complete_to_host_ptr)
542{
543 unsigned int cstatus;
544
Dan Williams67ea8382011-05-08 11:47:15 -0700545 cstatus = scic_request_get_controller_status(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700546
547 dev_dbg(&request->isci_host->pdev->dev,
548 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
549 "- controller status = 0x%x\n",
550 __func__, request, cstatus);
551
552 /* Decode the controller-specific errors; most
553 * important is to recognize those conditions in which
554 * the target may still have a task outstanding that
555 * must be aborted.
556 *
557 * Note that there are SCU completion codes being
558 * named in the decode below for which SCIC has already
559 * done work to handle them in a way other than as
560 * a controller-specific completion code; these are left
561 * in the decode below for completeness sake.
562 */
563 switch (cstatus) {
564 case SCU_TASK_DONE_DMASETUP_DIRERR:
565 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
566 case SCU_TASK_DONE_XFERCNT_ERR:
567 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
568 if (task->task_proto == SAS_PROTOCOL_SMP) {
569 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
570 *response_ptr = SAS_TASK_COMPLETE;
571
572 /* See if the device has been/is being stopped. Note
573 * that we ignore the quiesce state, since we are
574 * concerned about the actual device state.
575 */
576 if ((isci_device->status == isci_stopping) ||
577 (isci_device->status == isci_stopped))
578 *status_ptr = SAS_DEVICE_UNKNOWN;
579 else
580 *status_ptr = SAS_ABORTED_TASK;
581
582 request->complete_in_target = true;
583
584 *complete_to_host_ptr =
585 isci_perform_normal_io_completion;
586 } else {
587 /* Task in the target is not done. */
588 *response_ptr = SAS_TASK_UNDELIVERED;
589
590 if ((isci_device->status == isci_stopping) ||
591 (isci_device->status == isci_stopped))
592 *status_ptr = SAS_DEVICE_UNKNOWN;
593 else
594 *status_ptr = SAM_STAT_TASK_ABORTED;
595
596 request->complete_in_target = false;
597
598 *complete_to_host_ptr =
599 isci_perform_error_io_completion;
600 }
601
602 break;
603
604 case SCU_TASK_DONE_CRC_ERR:
605 case SCU_TASK_DONE_NAK_CMD_ERR:
606 case SCU_TASK_DONE_EXCESS_DATA:
607 case SCU_TASK_DONE_UNEXP_FIS:
608 /* Also SCU_TASK_DONE_UNEXP_RESP: */
609 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
610 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
611 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
612 /* These are conditions in which the target
613 * has completed the task, so that no cleanup
614 * is necessary.
615 */
616 *response_ptr = SAS_TASK_COMPLETE;
617
618 /* See if the device has been/is being stopped. Note
619 * that we ignore the quiesce state, since we are
620 * concerned about the actual device state.
621 */
622 if ((isci_device->status == isci_stopping) ||
623 (isci_device->status == isci_stopped))
624 *status_ptr = SAS_DEVICE_UNKNOWN;
625 else
626 *status_ptr = SAS_ABORTED_TASK;
627
628 request->complete_in_target = true;
629
630 *complete_to_host_ptr = isci_perform_normal_io_completion;
631 break;
632
633
634 /* Note that the only open reject completion codes seen here will be
635 * abandon-class codes; all others are automatically retried in the SCU.
636 */
637 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
638
639 isci_request_set_open_reject_status(
640 request, task, response_ptr, status_ptr,
641 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
642 break;
643
644 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
645
646 /* Note - the return of AB0 will change when
647 * libsas implements detection of zone violations.
648 */
649 isci_request_set_open_reject_status(
650 request, task, response_ptr, status_ptr,
651 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
652 break;
653
654 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
655
656 isci_request_set_open_reject_status(
657 request, task, response_ptr, status_ptr,
658 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
659 break;
660
661 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
662
663 isci_request_set_open_reject_status(
664 request, task, response_ptr, status_ptr,
665 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
666 break;
667
668 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
669
670 isci_request_set_open_reject_status(
671 request, task, response_ptr, status_ptr,
672 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
673 break;
674
675 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
676
677 isci_request_set_open_reject_status(
678 request, task, response_ptr, status_ptr,
679 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
680 break;
681
682 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
683
684 isci_request_set_open_reject_status(
685 request, task, response_ptr, status_ptr,
686 complete_to_host_ptr, SAS_OREJ_STP_NORES);
687 break;
688
689 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
690
691 isci_request_set_open_reject_status(
692 request, task, response_ptr, status_ptr,
693 complete_to_host_ptr, SAS_OREJ_EPROTO);
694 break;
695
696 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
697
698 isci_request_set_open_reject_status(
699 request, task, response_ptr, status_ptr,
700 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
701 break;
702
703 case SCU_TASK_DONE_LL_R_ERR:
704 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
705 case SCU_TASK_DONE_LL_PERR:
706 case SCU_TASK_DONE_LL_SY_TERM:
707 /* Also SCU_TASK_DONE_NAK_ERR:*/
708 case SCU_TASK_DONE_LL_LF_TERM:
709 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
710 case SCU_TASK_DONE_LL_ABORT_ERR:
711 case SCU_TASK_DONE_SEQ_INV_TYPE:
712 /* Also SCU_TASK_DONE_UNEXP_XR: */
713 case SCU_TASK_DONE_XR_IU_LEN_ERR:
714 case SCU_TASK_DONE_INV_FIS_LEN:
715 /* Also SCU_TASK_DONE_XR_WD_LEN: */
716 case SCU_TASK_DONE_SDMA_ERR:
717 case SCU_TASK_DONE_OFFSET_ERR:
718 case SCU_TASK_DONE_MAX_PLD_ERR:
719 case SCU_TASK_DONE_LF_ERR:
720 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
721 case SCU_TASK_DONE_SMP_LL_RX_ERR:
722 case SCU_TASK_DONE_UNEXP_DATA:
723 case SCU_TASK_DONE_UNEXP_SDBFIS:
724 case SCU_TASK_DONE_REG_ERR:
725 case SCU_TASK_DONE_SDB_ERR:
726 case SCU_TASK_DONE_TASK_ABORT:
727 default:
728 /* Task in the target is not done. */
729 *response_ptr = SAS_TASK_UNDELIVERED;
730 *status_ptr = SAM_STAT_TASK_ABORTED;
731 request->complete_in_target = false;
732
733 *complete_to_host_ptr = isci_perform_error_io_completion;
734 break;
735 }
736}
737
738/**
739 * isci_task_save_for_upper_layer_completion() - This function saves the
740 * request for later completion to the upper layer driver.
741 * @host: This parameter is a pointer to the host on which the the request
742 * should be queued (either as an error or success).
743 * @request: This parameter is the completed request.
744 * @response: This parameter is the response code for the completed task.
745 * @status: This parameter is the status code for the completed task.
746 *
747 * none.
748 */
749static void isci_task_save_for_upper_layer_completion(
750 struct isci_host *host,
751 struct isci_request *request,
752 enum service_response response,
753 enum exec_status status,
754 enum isci_completion_selection task_notification_selection)
755{
756 struct sas_task *task = isci_request_access_task(request);
757
Jeff Skirvinec6c9632011-03-04 14:06:44 -0800758 task_notification_selection
759 = isci_task_set_completion_status(task, response, status,
760 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -0700761
762 /* Tasks aborted specifically by a call to the lldd_abort_task
763 * function should not be completed to the host in the regular path.
764 */
765 switch (task_notification_selection) {
766
767 case isci_perform_normal_io_completion:
768
769 /* Normal notification (task_done) */
770 dev_dbg(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700771 "%s: Normal - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700772 __func__,
773 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700774 task->task_status.resp, response,
775 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700776 /* Add to the completed list. */
777 list_add(&request->completed_node,
778 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -0800779
780 /* Take the request off the device's pending request list. */
781 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700782 break;
783
784 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -0800785 /* No notification to libsas because this request is
786 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -0700787 */
788 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700789 "%s: Aborted - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700790 __func__,
791 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700792 task->task_status.resp, response,
793 task->task_status.stat, status);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800794
795 /* Wake up whatever process was waiting for this
796 * request to complete.
797 */
798 WARN_ON(request->io_request_completion == NULL);
799
800 if (request->io_request_completion != NULL) {
801
802 /* Signal whoever is waiting that this
803 * request is complete.
804 */
805 complete(request->io_request_completion);
806 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700807 break;
808
809 case isci_perform_error_io_completion:
810 /* Use sas_task_abort */
811 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700812 "%s: Error - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700813 __func__,
814 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700815 task->task_status.resp, response,
816 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700817 /* Add to the aborted list. */
818 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -0800819 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -0700820 break;
821
822 default:
823 dev_warn(&host->pdev->dev,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700824 "%s: Unknown - task = %p, response=%d (%d), status=%d (%d)\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700825 __func__,
826 task,
Jeff Skirvinaa145102011-03-07 16:40:47 -0700827 task->task_status.resp, response,
828 task->task_status.stat, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700829
Jeff Skirvina5fde222011-03-04 14:06:42 -0800830 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -0700831 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -0800832 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -0700833 break;
834 }
835}
836
837/**
838 * isci_request_io_request_complete() - This function is called by the sci core
839 * when an io request completes.
840 * @isci_host: This parameter specifies the ISCI host object
841 * @request: This parameter is the completed isci_request object.
842 * @completion_status: This parameter specifies the completion status from the
843 * sci core.
844 *
845 * none.
846 */
847void isci_request_io_request_complete(
848 struct isci_host *isci_host,
849 struct isci_request *request,
850 enum sci_io_status completion_status)
851{
852 struct sas_task *task = isci_request_access_task(request);
853 struct ssp_response_iu *resp_iu;
854 void *resp_buf;
855 unsigned long task_flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700856 struct isci_remote_device *isci_device = request->isci_device;
857 enum service_response response = SAS_TASK_UNDELIVERED;
858 enum exec_status status = SAS_ABORTED_TASK;
859 enum isci_request_status request_status;
860 enum isci_completion_selection complete_to_host
861 = isci_perform_normal_io_completion;
862
863 dev_dbg(&isci_host->pdev->dev,
864 "%s: request = %p, task = %p,\n"
865 "task->data_dir = %d completion_status = 0x%x\n",
866 __func__,
867 request,
868 task,
869 task->data_dir,
870 completion_status);
871
Jeff Skirvina5fde222011-03-04 14:06:42 -0800872 spin_lock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700873 request_status = isci_request_get_state(request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700874
875 /* Decode the request status. Note that if the request has been
876 * aborted by a task management function, we don't care
877 * what the status is.
878 */
879 switch (request_status) {
880
881 case aborted:
882 /* "aborted" indicates that the request was aborted by a task
883 * management function, since once a task management request is
884 * perfomed by the device, the request only completes because
885 * of the subsequent driver terminate.
886 *
887 * Aborted also means an external thread is explicitly managing
888 * this request, so that we do not complete it up the stack.
889 *
890 * The target is still there (since the TMF was successful).
891 */
892 request->complete_in_target = true;
893 response = SAS_TASK_COMPLETE;
894
895 /* See if the device has been/is being stopped. Note
896 * that we ignore the quiesce state, since we are
897 * concerned about the actual device state.
898 */
899 if ((isci_device->status == isci_stopping)
900 || (isci_device->status == isci_stopped)
901 )
902 status = SAS_DEVICE_UNKNOWN;
903 else
904 status = SAS_ABORTED_TASK;
905
906 complete_to_host = isci_perform_aborted_io_completion;
907 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -0800908
909 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700910 break;
911
912 case aborting:
913 /* aborting means that the task management function tried and
914 * failed to abort the request. We need to note the request
915 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
916 * target as down.
917 *
918 * Aborting also means an external thread is explicitly managing
919 * this request, so that we do not complete it up the stack.
920 */
921 request->complete_in_target = true;
922 response = SAS_TASK_UNDELIVERED;
923
924 if ((isci_device->status == isci_stopping) ||
925 (isci_device->status == isci_stopped))
926 /* The device has been /is being stopped. Note that
927 * we ignore the quiesce state, since we are
928 * concerned about the actual device state.
929 */
930 status = SAS_DEVICE_UNKNOWN;
931 else
932 status = SAS_PHY_DOWN;
933
934 complete_to_host = isci_perform_aborted_io_completion;
935
936 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -0800937
938 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700939 break;
940
941 case terminating:
942
943 /* This was an terminated request. This happens when
944 * the I/O is being terminated because of an action on
945 * the device (reset, tear down, etc.), and the I/O needs
946 * to be completed up the stack.
947 */
948 request->complete_in_target = true;
949 response = SAS_TASK_UNDELIVERED;
950
951 /* See if the device has been/is being stopped. Note
952 * that we ignore the quiesce state, since we are
953 * concerned about the actual device state.
954 */
955 if ((isci_device->status == isci_stopping) ||
956 (isci_device->status == isci_stopped))
957 status = SAS_DEVICE_UNKNOWN;
958 else
959 status = SAS_ABORTED_TASK;
960
Jeff Skirvina5fde222011-03-04 14:06:42 -0800961 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -0700962
963 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -0800964
965 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -0700966 break;
967
968 default:
969
Jeff Skirvina5fde222011-03-04 14:06:42 -0800970 /* The request is done from an SCU HW perspective. */
971 request->status = completed;
972
973 spin_unlock(&request->state_lock);
974
Dan Williams6f231dd2011-07-02 22:56:22 -0700975 /* This is an active request being completed from the core. */
976 switch (completion_status) {
977
978 case SCI_IO_FAILURE_RESPONSE_VALID:
979 dev_dbg(&isci_host->pdev->dev,
980 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
981 __func__,
982 request,
983 task);
984
985 if (sas_protocol_ata(task->task_proto)) {
Dan Williams67ea8382011-05-08 11:47:15 -0700986 resp_buf = &request->sci.stp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -0700987 isci_request_process_stp_response(task,
Dan Williamsb7645812011-05-08 02:35:32 -0700988 resp_buf);
Dan Williams6f231dd2011-07-02 22:56:22 -0700989 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
990
991 /* crack the iu response buffer. */
Dan Williams67ea8382011-05-08 11:47:15 -0700992 resp_iu = &request->sci.ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -0700993 isci_request_process_response_iu(task, resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -0700994 &isci_host->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700995
996 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
997
998 dev_err(&isci_host->pdev->dev,
999 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
1000 "SAS_PROTOCOL_SMP protocol\n",
1001 __func__);
1002
1003 } else
1004 dev_err(&isci_host->pdev->dev,
1005 "%s: unknown protocol\n", __func__);
1006
1007 /* use the task status set in the task struct by the
1008 * isci_request_process_response_iu call.
1009 */
1010 request->complete_in_target = true;
1011 response = task->task_status.resp;
1012 status = task->task_status.stat;
1013 break;
1014
1015 case SCI_IO_SUCCESS:
1016 case SCI_IO_SUCCESS_IO_DONE_EARLY:
1017
1018 response = SAS_TASK_COMPLETE;
1019 status = SAM_STAT_GOOD;
1020 request->complete_in_target = true;
1021
1022 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams67ea8382011-05-08 11:47:15 -07001023 void *rsp = &request->sci.smp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07001024
1025 dev_dbg(&isci_host->pdev->dev,
1026 "%s: SMP protocol completion\n",
1027 __func__);
1028
1029 sg_copy_from_buffer(
1030 &task->smp_task.smp_resp, 1,
Dan Williamsb7645812011-05-08 02:35:32 -07001031 rsp, sizeof(struct smp_resp));
Dan Williams6f231dd2011-07-02 22:56:22 -07001032 } else if (completion_status
1033 == SCI_IO_SUCCESS_IO_DONE_EARLY) {
1034
1035 /* This was an SSP / STP / SATA transfer.
1036 * There is a possibility that less data than
1037 * the maximum was transferred.
1038 */
1039 u32 transferred_length
Dan Williams67ea8382011-05-08 11:47:15 -07001040 = scic_io_request_get_number_of_bytes_transferred(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001041
1042 task->task_status.residual
1043 = task->total_xfer_len - transferred_length;
1044
1045 /* If there were residual bytes, call this an
1046 * underrun.
1047 */
1048 if (task->task_status.residual != 0)
1049 status = SAS_DATA_UNDERRUN;
1050
1051 dev_dbg(&isci_host->pdev->dev,
1052 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
1053 __func__,
1054 status);
1055
1056 } else
1057 dev_dbg(&isci_host->pdev->dev,
1058 "%s: SCI_IO_SUCCESS\n",
1059 __func__);
1060
1061 break;
1062
1063 case SCI_IO_FAILURE_TERMINATED:
1064 dev_dbg(&isci_host->pdev->dev,
1065 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
1066 __func__,
1067 request,
1068 task);
1069
1070 /* The request was terminated explicitly. No handling
1071 * is needed in the SCSI error handler path.
1072 */
1073 request->complete_in_target = true;
1074 response = SAS_TASK_UNDELIVERED;
1075
1076 /* See if the device has been/is being stopped. Note
1077 * that we ignore the quiesce state, since we are
1078 * concerned about the actual device state.
1079 */
1080 if ((isci_device->status == isci_stopping) ||
1081 (isci_device->status == isci_stopped))
1082 status = SAS_DEVICE_UNKNOWN;
1083 else
1084 status = SAS_ABORTED_TASK;
1085
1086 complete_to_host = isci_perform_normal_io_completion;
1087 break;
1088
1089 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
1090
1091 isci_request_handle_controller_specific_errors(
1092 isci_device, request, task, &response, &status,
1093 &complete_to_host);
1094
1095 break;
1096
1097 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
1098 /* This is a special case, in that the I/O completion
1099 * is telling us that the device needs a reset.
1100 * In order for the device reset condition to be
1101 * noticed, the I/O has to be handled in the error
1102 * handler. Set the reset flag and cause the
1103 * SCSI error thread to be scheduled.
1104 */
1105 spin_lock_irqsave(&task->task_state_lock, task_flags);
1106 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1107 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
1108
Jeff Skirvinaa145102011-03-07 16:40:47 -07001109 /* Fail the I/O. */
1110 response = SAS_TASK_UNDELIVERED;
1111 status = SAM_STAT_TASK_ABORTED;
1112
Dan Williams6f231dd2011-07-02 22:56:22 -07001113 complete_to_host = isci_perform_error_io_completion;
1114 request->complete_in_target = false;
1115 break;
1116
1117 default:
1118 /* Catch any otherwise unhandled error codes here. */
1119 dev_warn(&isci_host->pdev->dev,
1120 "%s: invalid completion code: 0x%x - "
1121 "isci_request = %p\n",
1122 __func__, completion_status, request);
1123
1124 response = SAS_TASK_UNDELIVERED;
1125
1126 /* See if the device has been/is being stopped. Note
1127 * that we ignore the quiesce state, since we are
1128 * concerned about the actual device state.
1129 */
1130 if ((isci_device->status == isci_stopping) ||
1131 (isci_device->status == isci_stopped))
1132 status = SAS_DEVICE_UNKNOWN;
1133 else
1134 status = SAS_ABORTED_TASK;
1135
1136 complete_to_host = isci_perform_error_io_completion;
1137 request->complete_in_target = false;
1138 break;
1139 }
1140 break;
1141 }
1142
1143 isci_request_unmap_sgl(request, isci_host->pdev);
1144
1145 /* Put the completed request on the correct list */
1146 isci_task_save_for_upper_layer_completion(isci_host, request, response,
1147 status, complete_to_host
1148 );
1149
1150 /* complete the io request to the core. */
Artur Wojcikcc3dbd02011-05-04 07:58:16 +00001151 scic_controller_complete_io(&isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -07001152 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -07001153 &request->sci);
1154 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07001155 * terminated again, and to cause any calls into abort
1156 * task to recognize the already completed case.
1157 */
Dan Williams67ea8382011-05-08 11:47:15 -07001158 request->terminated = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001159
Dan Williams6f231dd2011-07-02 22:56:22 -07001160 isci_host_can_dequeue(isci_host, 1);
1161}