isci: add "isci_id" attribute
[deliverable/linux.git] / drivers / scsi / isci / task.c
CommitLineData
6f231dda
DW
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
56#include <linux/completion.h>
50e7f9b5
DW
57#include <linux/irqflags.h>
58#include <scsi/sas_ata.h>
6f231dda
DW
59#include "scic_task_request.h"
60#include "scic_remote_device.h"
61#include "scic_io_request.h"
62#include "scic_sds_remote_device.h"
63#include "scic_sds_remote_node_context.h"
64#include "isci.h"
65#include "request.h"
66#include "sata.h"
67#include "task.h"
68
50e7f9b5
DW
69/**
70* isci_task_complete_for_upper_layer() - This function completes the request
71* to the upper layer driver in the case where an I/O needs to be completed
72* back in the submit path.
73* @host: This parameter is a pointer to the host on which the the request
74* should be queued (either as an error or success).
75* @task: This parameter is the completed request.
76* @response: This parameter is the response code for the completed task.
77* @status: This parameter is the status code for the completed task.
78*
79* none.
80*/
81static void isci_task_complete_for_upper_layer(struct sas_task *task,
82 enum service_response response,
83 enum exec_status status,
84 enum isci_completion_selection task_notification_selection)
85{
86 unsigned long flags = 0;
87 struct Scsi_Host *host = NULL;
88
89 task_notification_selection
90 = isci_task_set_completion_status(task, response, status,
91 task_notification_selection);
92
93 /* Tasks aborted specifically by a call to the lldd_abort_task
94 * function should not be completed to the host in the regular path.
95 */
96 switch (task_notification_selection) {
97 case isci_perform_normal_io_completion:
98 /* Normal notification (task_done) */
99 dev_dbg(task->dev->port->ha->dev,
100 "%s: Normal - task = %p, response=%d, status=%d\n",
101 __func__, task, response, status);
102
103 if (dev_is_sata(task->dev)) {
104 /* Since we are still in the submit path, and since
105 * libsas takes the host lock on behalf of SATA
106 * devices before I/O starts, we need to unlock
107 * before we can call back and report the I/O
108 * submission error.
109 */
110 if (task->dev
111 && task->dev->port
112 && task->dev->port->ha) {
113
114 host = task->dev->port->ha->core.shost;
115 raw_local_irq_save(flags);
116 spin_unlock(host->host_lock);
117 }
118 task->task_done(task);
119 if (host) {
120 spin_lock(host->host_lock);
121 raw_local_irq_restore(flags);
122 }
123 } else
124 task->task_done(task);
125
126 task->lldd_task = NULL;
127 break;
128
129 case isci_perform_aborted_io_completion:
130 /* No notification because this request is already in the
131 * abort path.
132 */
133 dev_warn(task->dev->port->ha->dev,
134 "%s: Aborted - task = %p, response=%d, status=%d\n",
135 __func__, task, response, status);
136 break;
137
138 case isci_perform_error_io_completion:
139 /* Use sas_task_abort */
140 dev_warn(task->dev->port->ha->dev,
141 "%s: Error - task = %p, response=%d, status=%d\n",
142 __func__, task, response, status);
143 sas_task_abort(task);
144 break;
145
146 default:
147 dev_warn(task->dev->port->ha->dev,
148 "%s: isci task notification default case!",
149 __func__);
150 sas_task_abort(task);
151 break;
152 }
153}
6f231dda
DW
154
155/**
156 * isci_task_execute_task() - This function is one of the SAS Domain Template
157 * functions. This function is called by libsas to send a task down to
158 * hardware.
159 * @task: This parameter specifies the SAS task to send.
160 * @num: This parameter specifies the number of tasks to queue.
161 * @gfp_flags: This parameter specifies the context of this call.
162 *
163 * status, zero indicates success.
164 */
165int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
166{
167 struct isci_host *isci_host;
168 struct isci_request *request = NULL;
169 struct isci_remote_device *device;
170 unsigned long flags;
6f231dda
DW
171 int ret;
172 enum sci_status status;
f0846c68 173 enum isci_status device_status;
6f231dda
DW
174
175 dev_dbg(task->dev->port->ha->dev, "%s: num=%d\n", __func__, num);
176
6f231dda
DW
177 if ((task->dev == NULL) || (task->dev->port == NULL)) {
178
179 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi midlayer
180 * removes the target.
181 */
182 isci_task_complete_for_upper_layer(
183 task,
184 SAS_TASK_UNDELIVERED,
185 SAS_DEVICE_UNKNOWN,
186 isci_perform_normal_io_completion
187 );
188 return 0; /* The I/O was accepted (and failed). */
189 }
190 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
191
192 /* Check if we have room for more tasks */
193 ret = isci_host_can_queue(isci_host, num);
194
195 if (ret) {
196 dev_warn(task->dev->port->ha->dev, "%s: queue full\n", __func__);
197 return ret;
198 }
199
200 do {
201 dev_dbg(task->dev->port->ha->dev,
202 "task = %p, num = %d; dev = %p; cmd = %p\n",
203 task, num, task->dev, task->uldd_task);
204
205 if ((task->dev == NULL) || (task->dev->port == NULL)) {
206 dev_warn(task->dev->port->ha->dev,
207 "%s: task %p's port or dev == NULL!\n",
208 __func__, task);
209
210 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi
211 * midlayer removes the target.
212 */
213 isci_task_complete_for_upper_layer(
214 task,
215 SAS_TASK_UNDELIVERED,
216 SAS_DEVICE_UNKNOWN,
217 isci_perform_normal_io_completion
218 );
219 /* We don't have a valid host reference, so we
220 * can't control the host queueing condition.
221 */
f0846c68 222 goto next_task;
6f231dda
DW
223 }
224
225 device = isci_dev_from_domain_dev(task->dev);
226
227 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
228
f0846c68
JS
229 if (device)
230 device_status = device->status;
231 else
232 device_status = isci_freed;
233
234 /* From this point onward, any process that needs to guarantee
235 * that there is no kernel I/O being started will have to wait
236 * for the quiesce spinlock.
237 */
238
239 if (device_status != isci_ready_for_io) {
6f231dda
DW
240
241 /* Forces a retry from scsi mid layer. */
242 dev_warn(task->dev->port->ha->dev,
243 "%s: task %p: isci_host->status = %d, "
f0846c68 244 "device = %p; device_status = 0x%x\n\n",
6f231dda
DW
245 __func__,
246 task,
247 isci_host_get_state(isci_host),
f0846c68 248 device, device_status);
6f231dda 249
f0846c68
JS
250 if (device_status == isci_ready) {
251 /* Indicate QUEUE_FULL so that the scsi midlayer
252 * retries.
253 */
254 isci_task_complete_for_upper_layer(
255 task,
256 SAS_TASK_COMPLETE,
257 SAS_QUEUE_FULL,
258 isci_perform_normal_io_completion
259 );
260 } else {
261 /* Else, the device is going down. */
262 isci_task_complete_for_upper_layer(
263 task,
264 SAS_TASK_UNDELIVERED,
265 SAS_DEVICE_UNKNOWN,
266 isci_perform_normal_io_completion
267 );
268 }
6f231dda 269 isci_host_can_dequeue(isci_host, 1);
f0846c68
JS
270 } else {
271 /* There is a device and it's ready for I/O. */
272 spin_lock_irqsave(&task->task_state_lock, flags);
6f231dda 273
f0846c68 274 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
6f231dda 275
f0846c68
JS
276 spin_unlock_irqrestore(&task->task_state_lock,
277 flags);
6f231dda 278
f0846c68
JS
279 isci_task_complete_for_upper_layer(
280 task,
281 SAS_TASK_UNDELIVERED,
282 SAM_STAT_TASK_ABORTED,
283 isci_perform_normal_io_completion
284 );
6f231dda 285
f0846c68 286 /* The I/O was aborted. */
6f231dda 287
f0846c68 288 } else {
6f231dda
DW
289 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
290 spin_unlock_irqrestore(&task->task_state_lock, flags);
f0846c68
JS
291
292 /* build and send the request. */
293 status = isci_request_execute(isci_host, task, &request,
294 gfp_flags);
295
296 if (status != SCI_SUCCESS) {
297
298 spin_lock_irqsave(&task->task_state_lock, flags);
299 /* Did not really start this command. */
300 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
301 spin_unlock_irqrestore(&task->task_state_lock, flags);
302
303 /* Indicate QUEUE_FULL so that the scsi
304 * midlayer retries. if the request
305 * failed for remote device reasons,
306 * it gets returned as
307 * SAS_TASK_UNDELIVERED next time
308 * through.
309 */
310 isci_task_complete_for_upper_layer(
6f231dda
DW
311 task,
312 SAS_TASK_COMPLETE,
313 SAS_QUEUE_FULL,
314 isci_perform_normal_io_completion
315 );
f0846c68
JS
316 isci_host_can_dequeue(isci_host, 1);
317 }
6f231dda
DW
318 }
319 }
f0846c68 320next_task:
6f231dda
DW
321 task = list_entry(task->list.next, struct sas_task, list);
322 } while (--num > 0);
323 return 0;
324}
325
326
327
328/**
329 * isci_task_request_build() - This function builds the task request object.
330 * @isci_host: This parameter specifies the ISCI host object
331 * @request: This parameter points to the isci_request object allocated in the
332 * request construct function.
333 * @tmf: This parameter is the task management struct to be built
334 *
335 * SCI_SUCCESS on successfull completion, or specific failure code.
336 */
337static enum sci_status isci_task_request_build(
338 struct isci_host *isci_host,
339 struct isci_request **isci_request,
340 struct isci_tmf *isci_tmf)
341{
342 struct scic_sds_remote_device *sci_device;
343 enum sci_status status = SCI_FAILURE;
344 struct isci_request *request;
345 struct isci_remote_device *isci_device;
346/* struct sci_sas_identify_address_frame_protocols dev_protocols; */
347 struct smp_discover_response_protocols dev_protocols;
348
349
350 dev_dbg(&isci_host->pdev->dev,
351 "%s: isci_tmf = %p\n", __func__, isci_tmf);
352
353 isci_device = isci_tmf->device;
3a97eec6 354 sci_device = to_sci_dev(isci_device);
6f231dda
DW
355
356 /* do common allocation and init of request object. */
357 status = isci_request_alloc_tmf(
358 isci_host,
359 isci_tmf,
360 &request,
361 isci_device,
362 GFP_ATOMIC
363 );
364
365 if (status != SCI_SUCCESS)
366 goto out;
367
368 /* let the core do it's construct. */
369 status = scic_task_request_construct(
370 isci_host->core_controller,
371 sci_device,
372 SCI_CONTROLLER_INVALID_IO_TAG,
373 request,
374 request->sci_request_mem_ptr,
375 &request->sci_request_handle
376 );
377
378 if (status != SCI_SUCCESS) {
379 dev_warn(&isci_host->pdev->dev,
380 "%s: scic_task_request_construct failed - "
381 "status = 0x%x\n",
382 __func__,
383 status);
384 goto errout;
385 }
386
387 sci_object_set_association(
388 request->sci_request_handle,
389 request
390 );
391
392 scic_remote_device_get_protocols(
393 sci_device,
394 &dev_protocols
395 );
396
397 /* let the core do it's protocol
398 * specific construction.
399 */
400 if (dev_protocols.u.bits.attached_ssp_target) {
401
402 isci_tmf->proto = SAS_PROTOCOL_SSP;
403 status = scic_task_request_construct_ssp(
404 request->sci_request_handle
405 );
406 if (status != SCI_SUCCESS)
407 goto errout;
408 }
409
410 if (dev_protocols.u.bits.attached_stp_target) {
411
412 isci_tmf->proto = SAS_PROTOCOL_SATA;
413 status = isci_sata_management_task_request_build(request);
414
415 if (status != SCI_SUCCESS)
416 goto errout;
417 }
418
419 goto out;
420
421 errout:
422
423 /* release the dma memory if we fail. */
424 isci_request_free(isci_host, request);
425 request = NULL;
426
427 out:
428 *isci_request = request;
429 return status;
430}
431
432/**
433 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
434 * the timeout period for the TMF has expired.
435 *
436 *
437 */
438static void isci_tmf_timeout_cb(void *tmf_request_arg)
439{
440 struct isci_request *request = (struct isci_request *)tmf_request_arg;
441 struct isci_tmf *tmf = isci_request_access_tmf(request);
442 enum sci_status status;
443
444 BUG_ON(request->ttype != tmf_task);
445
446 /* This task management request has timed-out. Terminate the request
447 * so that the request eventually completes to the requestor in the
448 * request completion callback path.
449 */
450 /* Note - the timer callback function itself has provided spinlock
451 * exclusion from the start and completion paths. No need to take
452 * the request->isci_host->scic_lock here.
453 */
454
455 if (tmf->timeout_timer != NULL) {
456 /* Call the users callback, if any. */
457 if (tmf->cb_state_func != NULL)
458 tmf->cb_state_func(isci_tmf_timed_out, tmf,
459 tmf->cb_data);
460
461 /* Terminate the TMF transmit request. */
462 status = scic_controller_terminate_request(
463 request->isci_host->core_controller,
3a97eec6 464 to_sci_dev(request->isci_device),
6f231dda
DW
465 request->sci_request_handle
466 );
467
468 dev_dbg(&request->isci_host->pdev->dev,
469 "%s: tmf_request = %p; tmf = %p; status = %d\n",
470 __func__, request, tmf, status);
471 } else
472 dev_dbg(&request->isci_host->pdev->dev,
473 "%s: timer already canceled! "
474 "tmf_request = %p; tmf = %p\n",
475 __func__, request, tmf);
476
477 /* No need to unlock since the caller to this callback is doing it for
478 * us.
479 * request->isci_host->scic_lock
480 */
481}
482
483/**
484 * isci_task_execute_tmf() - This function builds and sends a task request,
485 * then waits for the completion.
486 * @isci_host: This parameter specifies the ISCI host object
487 * @tmf: This parameter is the pointer to the task management structure for
488 * this request.
489 * @timeout_ms: This parameter specifies the timeout period for the task
490 * management request.
491 *
492 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
493 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
494 */
495int isci_task_execute_tmf(
496 struct isci_host *isci_host,
497 struct isci_tmf *tmf,
498 unsigned long timeout_ms)
499{
500 DECLARE_COMPLETION_ONSTACK(completion);
501 enum sci_status status = SCI_FAILURE;
502 struct scic_sds_remote_device *sci_device;
503 struct isci_remote_device *isci_device = tmf->device;
504 struct isci_request *request;
505 int ret = TMF_RESP_FUNC_FAILED;
506 unsigned long flags;
507
508 /* sanity check, return TMF_RESP_FUNC_FAILED
509 * if the device is not there and ready.
510 */
8acaec15 511 if (!isci_device || isci_device->status != isci_ready_for_io) {
6f231dda
DW
512 dev_dbg(&isci_host->pdev->dev,
513 "%s: isci_device = %p not ready (%d)\n",
514 __func__,
8acaec15 515 isci_device, isci_device->status);
6f231dda
DW
516 return TMF_RESP_FUNC_FAILED;
517 } else
518 dev_dbg(&isci_host->pdev->dev,
519 "%s: isci_device = %p\n",
520 __func__, isci_device);
521
3a97eec6 522 sci_device = to_sci_dev(isci_device);
6f231dda
DW
523
524 /* Assign the pointer to the TMF's completion kernel wait structure. */
525 tmf->complete = &completion;
526
527 isci_task_request_build(
528 isci_host,
529 &request,
530 tmf
531 );
532
533 if (!request) {
534 dev_warn(&isci_host->pdev->dev,
535 "%s: isci_task_request_build failed\n",
536 __func__);
537 return TMF_RESP_FUNC_FAILED;
538 }
539
540 /* Allocate the TMF timeout timer. */
6f231dda 541 spin_lock_irqsave(&isci_host->scic_lock, flags);
7c40a803 542 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
6f231dda
DW
543
544 /* Start the timer. */
545 if (tmf->timeout_timer)
546 isci_timer_start(tmf->timeout_timer, timeout_ms);
547 else
548 dev_warn(&isci_host->pdev->dev,
549 "%s: isci_timer_create failed!!!!\n",
550 __func__);
551
552 /* start the TMF io. */
553 status = scic_controller_start_task(
554 isci_host->core_controller,
555 sci_device,
556 request->sci_request_handle,
557 SCI_CONTROLLER_INVALID_IO_TAG
558 );
559
560 if (status != SCI_SUCCESS) {
561 dev_warn(&isci_host->pdev->dev,
562 "%s: start_io failed - status = 0x%x, request = %p\n",
563 __func__,
564 status,
565 request);
566 goto cleanup_request;
567 }
568
569 /* Call the users callback, if any. */
570 if (tmf->cb_state_func != NULL)
571 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
572
573 /* Change the state of the TMF-bearing request to "started". */
574 isci_request_change_state(request, started);
575
576 /* add the request to the remote device request list. */
577 list_add(&request->dev_node, &isci_device->reqs_in_process);
578
579 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
580
581 /* Wait for the TMF to complete, or a timeout. */
582 wait_for_completion(&completion);
583
584 isci_print_tmf(tmf);
585
586 if (tmf->status == SCI_SUCCESS)
587 ret = TMF_RESP_FUNC_COMPLETE;
588 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
589 dev_dbg(&isci_host->pdev->dev,
590 "%s: tmf.status == "
591 "SCI_FAILURE_IO_RESPONSE_VALID\n",
592 __func__);
593 ret = TMF_RESP_FUNC_COMPLETE;
594 }
595 /* Else - leave the default "failed" status alone. */
596
597 dev_dbg(&isci_host->pdev->dev,
598 "%s: completed request = %p\n",
599 __func__,
600 request);
601
602 if (request->io_request_completion != NULL) {
603
604 /* The fact that this is non-NULL for a TMF request
605 * means there is a thread waiting for this TMF to
606 * finish.
607 */
608 complete(request->io_request_completion);
609 }
610
611 spin_lock_irqsave(&isci_host->scic_lock, flags);
612
613 cleanup_request:
614
615 /* Clean up the timer if needed. */
616 if (tmf->timeout_timer) {
7c40a803 617 isci_del_timer(isci_host, tmf->timeout_timer);
6f231dda
DW
618 tmf->timeout_timer = NULL;
619 }
620
621 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
622
623 isci_request_free(isci_host, request);
624
625 return ret;
626}
627
628void isci_task_build_tmf(
629 struct isci_tmf *tmf,
630 struct isci_remote_device *isci_device,
631 enum isci_tmf_function_codes code,
632 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
633 struct isci_tmf *,
634 void *),
c3f42feb 635 void *cb_data)
6f231dda
DW
636{
637 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
638 "%s: isci_device = %p\n", __func__, isci_device);
639
640 memset(tmf, 0, sizeof(*tmf));
641
642 tmf->device = isci_device;
643 tmf->tmf_code = code;
644 tmf->timeout_timer = NULL;
645 tmf->cb_state_func = tmf_sent_cb;
c3f42feb
JS
646 tmf->cb_data = cb_data;
647}
1fad9e93 648
c3f42feb
JS
649void isci_task_build_abort_task_tmf(
650 struct isci_tmf *tmf,
651 struct isci_remote_device *isci_device,
652 enum isci_tmf_function_codes code,
653 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
654 struct isci_tmf *,
655 void *),
656 struct isci_request *old_request)
657{
658 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
659 (void *)old_request);
660 tmf->io_tag = old_request->io_tag;
6f231dda
DW
661}
662
663static struct isci_request *isci_task_get_request_from_task(
664 struct sas_task *task,
665 struct isci_host **isci_host,
666 struct isci_remote_device **isci_device)
667{
668
669 struct isci_request *request = NULL;
670 unsigned long flags;
671
672 spin_lock_irqsave(&task->task_state_lock, flags);
673
674 request = task->lldd_task;
675
676 /* If task is already done, the request isn't valid */
677 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
678 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
679 (request != NULL)) {
680
681 if (isci_host != NULL)
682 *isci_host = request->isci_host;
683
684 if (isci_device != NULL)
685 *isci_device = request->isci_device;
686 }
687
688 spin_unlock_irqrestore(&task->task_state_lock, flags);
689
690 return request;
691}
692
693/**
694 * isci_task_validate_request_to_abort() - This function checks the given I/O
695 * against the "started" state. If the request is still "started", it's
696 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
697 * BEFORE CALLING THIS FUNCTION.
698 * @isci_request: This parameter specifies the request object to control.
699 * @isci_host: This parameter specifies the ISCI host object
700 * @isci_device: This is the device to which the request is pending.
701 * @aborted_io_completion: This is a completion structure that will be added to
702 * the request in case it is changed to aborting; this completion is
703 * triggered when the request is fully completed.
704 *
705 * Either "started" on successful change of the task status to "aborted", or
706 * "unallocated" if the task cannot be controlled.
707 */
708static enum isci_request_status isci_task_validate_request_to_abort(
709 struct isci_request *isci_request,
710 struct isci_host *isci_host,
711 struct isci_remote_device *isci_device,
712 struct completion *aborted_io_completion)
713{
714 enum isci_request_status old_state = unallocated;
715
716 /* Only abort the task if it's in the
717 * device's request_in_process list
718 */
719 if (isci_request && !list_empty(&isci_request->dev_node)) {
720 old_state = isci_request_change_started_to_aborted(
721 isci_request, aborted_io_completion);
722
6f231dda
DW
723 }
724
725 return old_state;
726}
727
728static void isci_request_cleanup_completed_loiterer(
729 struct isci_host *isci_host,
730 struct isci_remote_device *isci_device,
731 struct isci_request *isci_request)
732{
18d3d72a
JS
733 struct sas_task *task;
734 unsigned long flags;
735
736 task = (isci_request->ttype == io_task)
737 ? isci_request_access_task(isci_request)
738 : NULL;
6f231dda
DW
739
740 dev_dbg(&isci_host->pdev->dev,
741 "%s: isci_device=%p, request=%p, task=%p\n",
18d3d72a 742 __func__, isci_device, isci_request, task);
6f231dda
DW
743
744 spin_lock_irqsave(&isci_host->scic_lock, flags);
745 list_del_init(&isci_request->dev_node);
6f231dda
DW
746 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
747
a5fde225
JS
748 if (task != NULL) {
749
750 spin_lock_irqsave(&task->task_state_lock, flags);
751 task->lldd_task = NULL;
752
753 isci_set_task_doneflags(task);
754
755 /* If this task is not in the abort path, call task_done. */
756 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
757
758 spin_unlock_irqrestore(&task->task_state_lock, flags);
759 task->task_done(task);
760 } else
761 spin_unlock_irqrestore(&task->task_state_lock, flags);
762 }
6f231dda
DW
763 isci_request_free(isci_host, isci_request);
764}
4dc043c4
JS
765
766/**
767* @isci_termination_timed_out(): this function will deal with a request for
768* which the wait for termination has timed-out.
769*
770* @isci_host This SCU.
771* @isci_request The I/O request being terminated.
772*/
773static void
774isci_termination_timed_out(
775 struct isci_host * host,
776 struct isci_request * request
777 )
778{
779 unsigned long state_flags;
780
781 dev_warn(&host->pdev->dev,
782 "%s: host = %p; request = %p\n",
783 __func__, host, request);
784
785 /* At this point, the request to terminate
786 * has timed out. The best we can do is to
787 * have the request die a silent death
788 * if it ever completes.
789 */
790 spin_lock_irqsave(&request->state_lock, state_flags);
791
792 if (request->status == started) {
793
794 /* Set the request state to "dead",
795 * and clear the task pointer so that an actual
796 * completion event callback doesn't do
797 * anything.
798 */
799 request->status = dead;
800
801 /* Clear the timeout completion event pointer.*/
802 request->io_request_completion = NULL;
803
804 if (request->ttype == io_task) {
805
806 /* Break links with the sas_task. */
807 if (request->ttype_ptr.io_task_ptr != NULL) {
808
809 request->ttype_ptr.io_task_ptr->lldd_task = NULL;
810 request->ttype_ptr.io_task_ptr = NULL;
811 }
812 }
813 }
814 spin_unlock_irqrestore(&request->state_lock, state_flags);
815}
816
817
6f231dda
DW
818/**
819 * isci_terminate_request_core() - This function will terminate the given
820 * request, and wait for it to complete. This function must only be called
821 * from a thread that can wait. Note that the request is terminated and
822 * completed (back to the host, if started there).
823 * @isci_host: This SCU.
824 * @isci_device: The target.
825 * @isci_request: The I/O request to be terminated.
826 *
827 *
828 */
829static void isci_terminate_request_core(
830 struct isci_host *isci_host,
831 struct isci_remote_device *isci_device,
cbb65c66 832 struct isci_request *isci_request)
6f231dda 833{
cbb65c66 834 enum sci_status status = SCI_SUCCESS;
6f231dda
DW
835 bool was_terminated = false;
836 bool needs_cleanup_handling = false;
837 enum isci_request_status request_status;
838 unsigned long flags;
4dc043c4
JS
839 unsigned long timeout_remaining;
840
6f231dda
DW
841
842 dev_dbg(&isci_host->pdev->dev,
843 "%s: device = %p; request = %p\n",
844 __func__, isci_device, isci_request);
845
6f231dda 846 spin_lock_irqsave(&isci_host->scic_lock, flags);
4dc043c4
JS
847
848 /* Note that we are not going to control
849 * the target to abort the request.
850 */
851 isci_request->complete_in_target = true;
852
6f231dda
DW
853 /* Make sure the request wasn't just sitting around signalling
854 * device condition (if the request handle is NULL, then the
855 * request completed but needed additional handling here).
856 */
857 if (isci_request->sci_request_handle != NULL) {
858 was_terminated = true;
cbb65c66 859 needs_cleanup_handling = true;
6f231dda
DW
860 status = scic_controller_terminate_request(
861 isci_host->core_controller,
3a97eec6 862 to_sci_dev(isci_device),
6f231dda
DW
863 isci_request->sci_request_handle
864 );
865 }
866 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
867
868 /*
869 * The only time the request to terminate will
870 * fail is when the io request is completed and
871 * being aborted.
872 */
4dc043c4 873 if (status != SCI_SUCCESS) {
6f231dda
DW
874 dev_err(&isci_host->pdev->dev,
875 "%s: scic_controller_terminate_request"
876 " returned = 0x%x\n",
877 __func__,
878 status);
4dc043c4
JS
879 /* Clear the completion pointer from the request. */
880 isci_request->io_request_completion = NULL;
881
882 } else {
6f231dda
DW
883 if (was_terminated) {
884 dev_dbg(&isci_host->pdev->dev,
885 "%s: before completion wait (%p)\n",
886 __func__,
cbb65c66 887 isci_request->io_request_completion);
6f231dda
DW
888
889 /* Wait here for the request to complete. */
4dc043c4
JS
890 #define TERMINATION_TIMEOUT_MSEC 50
891 timeout_remaining
892 = wait_for_completion_timeout(
893 isci_request->io_request_completion,
894 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
895
896 if (!timeout_remaining) {
897
898 isci_termination_timed_out(isci_host,
899 isci_request);
900
901 dev_err(&isci_host->pdev->dev,
902 "%s: *** Timeout waiting for "
903 "termination(%p/%p)\n",
904 __func__,
905 isci_request->io_request_completion,
906 isci_request);
907
908 } else
909 dev_dbg(&isci_host->pdev->dev,
910 "%s: after completion wait (%p)\n",
911 __func__,
912 isci_request->io_request_completion);
913 }
914 /* Clear the completion pointer from the request. */
915 isci_request->io_request_completion = NULL;
916
917 /* Peek at the status of the request. This will tell
918 * us if there was special handling on the request such that it
919 * needs to be detached and freed here.
920 */
921 spin_lock_irqsave(&isci_request->state_lock, flags);
922 request_status = isci_request_get_state(isci_request);
923
924 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
925 && ((request_status == aborted)
926 || (request_status == aborting)
927 || (request_status == terminating)
928 || (request_status == completed)
929 || (request_status == dead)
930 )
931 ) {
932
933 /* The completion routine won't free a request in
934 * the aborted/aborting/etc. states, so we do
935 * it here.
936 */
937 needs_cleanup_handling = true;
6f231dda 938 }
4dc043c4 939 spin_unlock_irqrestore(&isci_request->state_lock, flags);
6f231dda
DW
940
941 if (needs_cleanup_handling)
942 isci_request_cleanup_completed_loiterer(
943 isci_host, isci_device, isci_request
944 );
945 }
946}
cbb65c66 947
6f231dda
DW
948static void isci_terminate_request(
949 struct isci_host *isci_host,
950 struct isci_remote_device *isci_device,
951 struct isci_request *isci_request,
952 enum isci_request_status new_request_state)
953{
954 enum isci_request_status old_state;
6f231dda 955 DECLARE_COMPLETION_ONSTACK(request_completion);
6f231dda
DW
956
957 /* Change state to "new_request_state" if it is currently "started" */
958 old_state = isci_request_change_started_to_newstate(
959 isci_request,
960 &request_completion,
961 new_request_state
962 );
963
a5fde225 964 if ((old_state == started) || (old_state == completed)) {
6f231dda 965
a5fde225
JS
966 /* If the old_state is started:
967 * This request was not already being aborted. If it had been,
6f231dda
DW
968 * then the aborting I/O (ie. the TMF request) would not be in
969 * the aborting state, and thus would be terminated here. Note
970 * that since the TMF completion's call to the kernel function
971 * "complete()" does not happen until the pending I/O request
972 * terminate fully completes, we do not have to implement a
973 * special wait here for already aborting requests - the
974 * termination of the TMF request will force the request
975 * to finish it's already started terminate.
a5fde225
JS
976 *
977 * If old_state == completed:
978 * This request completed from the SCU hardware perspective
979 * and now just needs cleaning up in terms of freeing the
980 * request and potentially calling up to libsas.
6f231dda
DW
981 */
982 isci_terminate_request_core(isci_host, isci_device,
cbb65c66 983 isci_request);
a5fde225 984 }
6f231dda
DW
985}
986
987/**
988 * isci_terminate_pending_requests() - This function will change the all of the
989 * requests on the given device's state to "aborting", will terminate the
990 * requests, and wait for them to complete. This function must only be
991 * called from a thread that can wait. Note that the requests are all
992 * terminated and completed (back to the host, if started there).
993 * @isci_host: This parameter specifies SCU.
994 * @isci_device: This parameter specifies the target.
995 *
996 *
997 */
998void isci_terminate_pending_requests(
999 struct isci_host *isci_host,
1000 struct isci_remote_device *isci_device,
1001 enum isci_request_status new_request_state)
1002{
4dc043c4
JS
1003 struct isci_request *request;
1004 struct isci_request *next_request;
1005 unsigned long flags;
1006 struct list_head aborted_request_list;
1007
1008 INIT_LIST_HEAD(&aborted_request_list);
6f231dda
DW
1009
1010 dev_dbg(&isci_host->pdev->dev,
1011 "%s: isci_device = %p (new request state = %d)\n",
1012 __func__, isci_device, new_request_state);
1013
4dc043c4 1014 spin_lock_irqsave(&isci_host->scic_lock, flags);
6f231dda 1015
4dc043c4
JS
1016 /* Move all of the pending requests off of the device list. */
1017 list_splice_init(&isci_device->reqs_in_process,
1018 &aborted_request_list);
6f231dda 1019
4dc043c4 1020 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
6f231dda 1021
4dc043c4
JS
1022 /* Iterate through the now-local list. */
1023 list_for_each_entry_safe(request, next_request,
1024 &aborted_request_list, dev_node) {
6f231dda 1025
4dc043c4
JS
1026 dev_warn(&isci_host->pdev->dev,
1027 "%s: isci_device=%p request=%p; task=%p\n",
1028 __func__,
1029 isci_device, request,
1030 ((request->ttype == io_task)
1031 ? isci_request_access_task(request)
1032 : NULL));
1033
1034 /* Mark all still pending I/O with the selected next
1035 * state, terminate and free it.
1036 */
1037 isci_terminate_request(isci_host, isci_device,
1038 request, new_request_state
1039 );
1040 }
6f231dda
DW
1041}
1042
1043/**
1044 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
1045 * Template functions.
1046 * @lun: This parameter specifies the lun to be reset.
1047 *
1048 * status, zero indicates success.
1049 */
1050static int isci_task_send_lu_reset_sas(
1051 struct isci_host *isci_host,
1052 struct isci_remote_device *isci_device,
1053 u8 *lun)
1054{
1055 struct isci_tmf tmf;
1056 int ret = TMF_RESP_FUNC_FAILED;
1057
1058 dev_dbg(&isci_host->pdev->dev,
1059 "%s: isci_host = %p, isci_device = %p\n",
1060 __func__, isci_host, isci_device);
1061 /* Send the LUN reset to the target. By the time the call returns,
1062 * the TMF has fully exected in the target (in which case the return
1063 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
1064 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
1065 */
1066 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
1067 NULL);
1068
1069 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
1070 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
1071
1072 if (ret == TMF_RESP_FUNC_COMPLETE)
1073 dev_dbg(&isci_host->pdev->dev,
1074 "%s: %p: TMF_LU_RESET passed\n",
1075 __func__, isci_device);
1076 else
1077 dev_dbg(&isci_host->pdev->dev,
1078 "%s: %p: TMF_LU_RESET failed (%x)\n",
1079 __func__, isci_device, ret);
1080
1081 return ret;
1082}
1083
1084/**
1085 * isci_task_lu_reset() - This function is one of the SAS Domain Template
1086 * functions. This is one of the Task Management functoins called by libsas,
1087 * to reset the given lun. Note the assumption that while this call is
1088 * executing, no I/O will be sent by the host to the device.
1089 * @lun: This parameter specifies the lun to be reset.
1090 *
1091 * status, zero indicates success.
1092 */
1093int isci_task_lu_reset(
1094 struct domain_device *domain_device,
1095 u8 *lun)
1096{
1097 struct isci_host *isci_host = NULL;
1098 struct isci_remote_device *isci_device = NULL;
1099 int ret;
1100 bool device_stopping = false;
1101
1102 if (domain_device == NULL) {
1103 pr_warn("%s: domain_device == NULL\n", __func__);
1104 return TMF_RESP_FUNC_FAILED;
1105 }
1106
1107 isci_device = isci_dev_from_domain_dev(domain_device);
1108
1109 if (domain_device->port != NULL)
1110 isci_host = isci_host_from_sas_ha(domain_device->port->ha);
1111
1112 pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
1113 __func__, domain_device, isci_host, isci_device);
1114
1115 if (isci_device != NULL)
1116 device_stopping = (isci_device->status == isci_stopping)
1117 || (isci_device->status == isci_stopped);
1118
1119 /* If there is a device reset pending on any request in the
1120 * device's list, fail this LUN reset request in order to
1121 * escalate to the device reset.
1122 */
1123 if ((isci_device == NULL) ||
1124 (isci_host == NULL) ||
1125 ((isci_host != NULL) &&
1126 (isci_device != NULL) &&
1127 (device_stopping ||
1128 (isci_device_is_reset_pending(isci_host, isci_device))))) {
1129 dev_warn(&isci_host->pdev->dev,
1130 "%s: No dev (%p), no host (%p), or "
1131 "RESET PENDING: domain_device=%p\n",
1132 __func__, isci_device, isci_host, domain_device);
1133 return TMF_RESP_FUNC_FAILED;
1134 }
1135
6f231dda
DW
1136 /* Send the task management part of the reset. */
1137 if (sas_protocol_ata(domain_device->tproto)) {
1138 ret = isci_task_send_lu_reset_sata(
1139 isci_host, isci_device, lun
1140 );
1141 } else
1142 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
1143
1144 /* If the LUN reset worked, all the I/O can now be terminated. */
1145 if (ret == TMF_RESP_FUNC_COMPLETE)
1146 /* Terminate all I/O now. */
1147 isci_terminate_pending_requests(isci_host,
1148 isci_device,
1149 terminating);
1150
6f231dda
DW
1151 return ret;
1152}
1153
1154
1155/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1156int isci_task_clear_nexus_port(struct asd_sas_port *port)
1157{
1158 return TMF_RESP_FUNC_FAILED;
1159}
1160
1161
1162
1163int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1164{
1165 return TMF_RESP_FUNC_FAILED;
1166}
1167
1168int isci_task_I_T_nexus_reset(struct domain_device *dev)
1169{
1170 return TMF_RESP_FUNC_FAILED;
1171}
1172
1173
1174/* Task Management Functions. Must be called from process context. */
1175
1176/**
1177 * isci_abort_task_process_cb() - This is a helper function for the abort task
1178 * TMF command. It manages the request state with respect to the successful
1179 * transmission / completion of the abort task request.
1180 * @cb_state: This parameter specifies when this function was called - after
1181 * the TMF request has been started and after it has timed-out.
1182 * @tmf: This parameter specifies the TMF in progress.
1183 *
1184 *
1185 */
1186static void isci_abort_task_process_cb(
1187 enum isci_tmf_cb_state cb_state,
1188 struct isci_tmf *tmf,
1189 void *cb_data)
1190{
1191 struct isci_request *old_request;
1192
1193 old_request = (struct isci_request *)cb_data;
1194
1195 dev_dbg(&old_request->isci_host->pdev->dev,
1196 "%s: tmf=%p, old_request=%p\n",
1197 __func__, tmf, old_request);
1198
1199 switch (cb_state) {
1200
1201 case isci_tmf_started:
1202 /* The TMF has been started. Nothing to do here, since the
1203 * request state was already set to "aborted" by the abort
1204 * task function.
1205 */
70957a94
JS
1206 BUG_ON((old_request->status != aborted)
1207 && (old_request->status != completed));
6f231dda
DW
1208 break;
1209
1210 case isci_tmf_timed_out:
1211
1212 /* Set the task's state to "aborting", since the abort task
1213 * function thread set it to "aborted" (above) in anticipation
1214 * of the task management request working correctly. Since the
1215 * timeout has now fired, the TMF request failed. We set the
1216 * state such that the request completion will indicate the
1217 * device is no longer present.
1218 */
1219 isci_request_change_state(old_request, aborting);
1220 break;
1221
1222 default:
1223 dev_err(&old_request->isci_host->pdev->dev,
1224 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1225 __func__, cb_state, tmf, old_request);
1226 break;
1227 }
1228}
1229
1230/**
1231 * isci_task_abort_task() - This function is one of the SAS Domain Template
1232 * functions. This function is called by libsas to abort a specified task.
1233 * @task: This parameter specifies the SAS task to abort.
1234 *
1235 * status, zero indicates success.
1236 */
1237int isci_task_abort_task(struct sas_task *task)
1238{
1239 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
a5fde225
JS
1240 struct isci_request *old_request = NULL;
1241 enum isci_request_status old_state;
6f231dda 1242 struct isci_remote_device *isci_device = NULL;
a5fde225
JS
1243 struct isci_host *isci_host = NULL;
1244 struct isci_tmf tmf;
1245 int ret = TMF_RESP_FUNC_FAILED;
1246 unsigned long flags;
1247 bool any_dev_reset = false;
1248 bool device_stopping;
6f231dda
DW
1249
1250 /* Get the isci_request reference from the task. Note that
1251 * this check does not depend on the pending request list
1252 * in the device, because tasks driving resets may land here
1253 * after completion in the core.
1254 */
1255 old_request = isci_task_get_request_from_task(task, &isci_host,
1256 &isci_device);
1257
1258 dev_dbg(&isci_host->pdev->dev,
1259 "%s: task = %p\n", __func__, task);
1260
1261 /* Check if the device has been / is currently being removed.
1262 * If so, no task management will be done, and the I/O will
1263 * be terminated.
1264 */
1265 device_stopping = (isci_device->status == isci_stopping)
1266 || (isci_device->status == isci_stopped);
1267
6f231dda
DW
1268 /* This version of the driver will fail abort requests for
1269 * SATA/STP. Failing the abort request this way will cause the
1270 * SCSI error handler thread to escalate to LUN reset
1271 */
1272 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1273 dev_warn(&isci_host->pdev->dev,
1274 " task %p is for a STP/SATA device;"
1275 " returning TMF_RESP_FUNC_FAILED\n"
1276 " to cause a LUN reset...\n", task);
1277 return TMF_RESP_FUNC_FAILED;
1278 }
1279
1280 dev_dbg(&isci_host->pdev->dev,
1281 "%s: old_request == %p\n", __func__, old_request);
1282
a5fde225
JS
1283 if (!device_stopping)
1284 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1285
6f231dda
DW
1286 spin_lock_irqsave(&task->task_state_lock, flags);
1287
1288 /* Don't do resets to stopping devices. */
a5fde225 1289 if (device_stopping) {
6f231dda 1290
a5fde225
JS
1291 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
1292 any_dev_reset = false;
6f231dda 1293
a5fde225 1294 } else /* See if there is a pending device reset for this device. */
6f231dda 1295 any_dev_reset = any_dev_reset
a5fde225 1296 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
6f231dda
DW
1297
1298 /* If the extraction of the request reference from the task
1299 * failed, then the request has been completed (or if there is a
1300 * pending reset then this abort request function must be failed
1301 * in order to escalate to the target reset).
1302 */
a5fde225 1303 if ((old_request == NULL) || any_dev_reset) {
6f231dda
DW
1304
1305 /* If the device reset task flag is set, fail the task
1306 * management request. Otherwise, the original request
1307 * has completed.
1308 */
1309 if (any_dev_reset) {
1310
1311 /* Turn off the task's DONE to make sure this
1312 * task is escalated to a target reset.
1313 */
1314 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1315
a5fde225
JS
1316 /* Make the reset happen as soon as possible. */
1317 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1318
1319 spin_unlock_irqrestore(&task->task_state_lock, flags);
1320
6f231dda
DW
1321 /* Fail the task management request in order to
1322 * escalate to the target reset.
1323 */
1324 ret = TMF_RESP_FUNC_FAILED;
1325
1326 dev_dbg(&isci_host->pdev->dev,
1327 "%s: Failing task abort in order to "
1328 "escalate to target reset because\n"
1329 "SAS_TASK_NEED_DEV_RESET is set for "
1330 "task %p on dev %p\n",
1331 __func__, task, isci_device);
1332
6f231dda 1333
a5fde225 1334 } else {
6f231dda
DW
1335 /* The request has already completed and there
1336 * is nothing to do here other than to set the task
1337 * done bit, and indicate that the task abort function
1338 * was sucessful.
1339 */
1340 isci_set_task_doneflags(task);
1341
a5fde225 1342 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda 1343
a5fde225 1344 ret = TMF_RESP_FUNC_COMPLETE;
6f231dda 1345
a5fde225
JS
1346 dev_dbg(&isci_host->pdev->dev,
1347 "%s: abort task not needed for %p\n",
1348 __func__, task);
6f231dda 1349 }
6f231dda
DW
1350
1351 return ret;
1352 }
a5fde225
JS
1353 else
1354 spin_unlock_irqrestore(&task->task_state_lock, flags);
6f231dda
DW
1355
1356 spin_lock_irqsave(&isci_host->scic_lock, flags);
1357
a5fde225
JS
1358 /* Check the request status and change to "aborting" if currently
1359 * "starting"; if true then set the I/O kernel completion
6f231dda
DW
1360 * struct that will be triggered when the request completes.
1361 */
a5fde225
JS
1362 old_state = isci_task_validate_request_to_abort(
1363 old_request, isci_host, isci_device,
1364 &aborted_io_completion);
1365 if ((old_state != started) && (old_state != completed)) {
6f231dda
DW
1366
1367 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1368
a5fde225
JS
1369 /* The request was already being handled by someone else (because
1370 * they got to set the state away from started).
1371 */
1372 dev_dbg(&isci_host->pdev->dev,
1373 "%s: device = %p; old_request %p already being aborted\n",
1374 __func__,
1375 isci_device, old_request);
6f231dda
DW
1376
1377 return TMF_RESP_FUNC_COMPLETE;
1378 }
a5fde225
JS
1379 if ((task->task_proto == SAS_PROTOCOL_SMP)
1380 || device_stopping
1381 || old_request->complete_in_target
1382 ) {
6f231dda
DW
1383
1384 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1385
a5fde225
JS
1386 dev_dbg(&isci_host->pdev->dev,
1387 "%s: SMP request (%d)"
1388 " or device is stopping (%d)"
1389 " or complete_in_target (%d), thus no TMF\n",
1390 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1391 device_stopping, old_request->complete_in_target);
1392
6f231dda
DW
1393 /* Set the state on the task. */
1394 isci_task_all_done(task);
1395
1396 ret = TMF_RESP_FUNC_COMPLETE;
1397
1398 /* Stopping and SMP devices are not sent a TMF, and are not
a5fde225 1399 * reset, but the outstanding I/O request is terminated below.
6f231dda 1400 */
6f231dda
DW
1401 } else {
1402 /* Fill in the tmf stucture */
c3f42feb
JS
1403 isci_task_build_abort_task_tmf(&tmf, isci_device,
1404 isci_tmf_ssp_task_abort,
1405 isci_abort_task_process_cb,
1406 old_request);
6f231dda 1407
6f231dda
DW
1408 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1409
1410 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1411 ret = isci_task_execute_tmf(isci_host, &tmf,
1412 ISCI_ABORT_TASK_TIMEOUT_MS);
1413
a5fde225 1414 if (ret != TMF_RESP_FUNC_COMPLETE)
6f231dda
DW
1415 dev_err(&isci_host->pdev->dev,
1416 "%s: isci_task_send_tmf failed\n",
1417 __func__);
1418 }
a5fde225
JS
1419 if (ret == TMF_RESP_FUNC_COMPLETE) {
1420 old_request->complete_in_target = true;
1421
1422 /* Clean up the request on our side, and wait for the aborted I/O to
1423 * complete.
1424 */
cbb65c66 1425 isci_terminate_request_core(isci_host, isci_device, old_request);
a5fde225 1426 }
6f231dda 1427
a5fde225
JS
1428 /* Make sure we do not leave a reference to aborted_io_completion */
1429 old_request->io_request_completion = NULL;
6f231dda
DW
1430 return ret;
1431}
1432
1433/**
1434 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1435 * functions. This is one of the Task Management functoins called by libsas,
1436 * to abort all task for the given lun.
1437 * @d_device: This parameter specifies the domain device associated with this
1438 * request.
1439 * @lun: This parameter specifies the lun associated with this request.
1440 *
1441 * status, zero indicates success.
1442 */
1443int isci_task_abort_task_set(
1444 struct domain_device *d_device,
1445 u8 *lun)
1446{
1447 return TMF_RESP_FUNC_FAILED;
1448}
1449
1450
1451/**
1452 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1453 * functions. This is one of the Task Management functoins called by libsas.
1454 * @d_device: This parameter specifies the domain device associated with this
1455 * request.
1456 * @lun: This parameter specifies the lun associated with this request.
1457 *
1458 * status, zero indicates success.
1459 */
1460int isci_task_clear_aca(
1461 struct domain_device *d_device,
1462 u8 *lun)
1463{
1464 return TMF_RESP_FUNC_FAILED;
1465}
1466
1467
1468
1469/**
1470 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1471 * functions. This is one of the Task Management functoins called by libsas.
1472 * @d_device: This parameter specifies the domain device associated with this
1473 * request.
1474 * @lun: This parameter specifies the lun associated with this request.
1475 *
1476 * status, zero indicates success.
1477 */
1478int isci_task_clear_task_set(
1479 struct domain_device *d_device,
1480 u8 *lun)
1481{
1482 return TMF_RESP_FUNC_FAILED;
1483}
1484
1485
1486/**
1487 * isci_task_query_task() - This function is implemented to cause libsas to
1488 * correctly escalate the failed abort to a LUN or target reset (this is
1489 * because sas_scsi_find_task libsas function does not correctly interpret
1490 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1491 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1492 * returned, libsas will turn this into a target reset
1493 * @task: This parameter specifies the sas task being queried.
1494 * @lun: This parameter specifies the lun associated with this request.
1495 *
1496 * status, zero indicates success.
1497 */
1498int isci_task_query_task(
1499 struct sas_task *task)
1500{
1501 /* See if there is a pending device reset for this device. */
1502 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1503 return TMF_RESP_FUNC_FAILED;
1504 else
1505 return TMF_RESP_FUNC_SUCC;
1506}
1507
1508/**
1509 * isci_task_request_complete() - This function is called by the sci core when
1510 * an task request completes.
1511 * @isci_host: This parameter specifies the ISCI host object
1512 * @request: This parameter is the completed isci_request object.
1513 * @completion_status: This parameter specifies the completion status from the
1514 * sci core.
1515 *
1516 * none.
1517 */
1518void isci_task_request_complete(
1519 struct isci_host *isci_host,
1520 struct isci_request *request,
1521 enum sci_task_status completion_status)
1522{
1523 struct isci_remote_device *isci_device = request->isci_device;
1524 enum isci_request_status old_state;
1525 struct isci_tmf *tmf = isci_request_access_tmf(request);
1526 struct completion *tmf_complete;
1527
1528 dev_dbg(&isci_host->pdev->dev,
1529 "%s: request = %p, status=%d\n",
1530 __func__, request, completion_status);
1531
1532 old_state = isci_request_change_state(request, completed);
1533
1534 tmf->status = completion_status;
1535 request->complete_in_target = true;
1536
1537 if (SAS_PROTOCOL_SSP == tmf->proto) {
1538
1539 memcpy(&tmf->resp.resp_iu,
1540 scic_io_request_get_response_iu_address(
1541 request->sci_request_handle
1542 ),
1543 sizeof(struct sci_ssp_response_iu));
1544
1545 } else if (SAS_PROTOCOL_SATA == tmf->proto) {
1546
1547 memcpy(&tmf->resp.d2h_fis,
1548 scic_stp_io_request_get_d2h_reg_address(
1549 request->sci_request_handle
1550 ),
1551 sizeof(struct sata_fis_reg_d2h)
1552 );
1553 }
1554
1555 /* Manage the timer if it is still running. */
1556 if (tmf->timeout_timer) {
7c40a803 1557 isci_del_timer(isci_host, tmf->timeout_timer);
6f231dda
DW
1558 tmf->timeout_timer = NULL;
1559 }
1560
1561 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1562 tmf_complete = tmf->complete;
1563
1564 scic_controller_complete_task(
1565 isci_host->core_controller,
3a97eec6 1566 to_sci_dev(isci_device),
6f231dda
DW
1567 request->sci_request_handle
1568 );
1569 /* NULL the request handle to make sure it cannot be terminated
1570 * or completed again.
1571 */
1572 request->sci_request_handle = NULL;
1573
1574 isci_request_change_state(request, unallocated);
1575 list_del_init(&request->dev_node);
1576
1577 /* The task management part completes last. */
1578 complete(tmf_complete);
1579}
1580
1581
1582/**
1583 * isci_task_ssp_request_get_lun() - This function is called by the sci core to
1584 * retrieve the lun for a given task request.
1585 * @request: This parameter is the isci_request object.
1586 *
1587 * lun for specified task request.
1588 */
1589u32 isci_task_ssp_request_get_lun(struct isci_request *request)
1590{
1591 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1592
1593 dev_dbg(&request->isci_host->pdev->dev,
1594 "%s: lun = %d\n", __func__, isci_tmf->lun[0]);
1595/* @todo: build lun from array of bytes to 32 bit */
1596 return isci_tmf->lun[0];
1597}
1598
1599/**
1600 * isci_task_ssp_request_get_function() - This function is called by the sci
1601 * core to retrieve the function for a given task request.
1602 * @request: This parameter is the isci_request object.
1603 *
1604 * function code for specified task request.
1605 */
1606u8 isci_task_ssp_request_get_function(struct isci_request *request)
1607{
1608 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1609
1610 dev_dbg(&request->isci_host->pdev->dev,
1611 "%s: func = %d\n", __func__, isci_tmf->tmf_code);
1612
1613 return isci_tmf->tmf_code;
1614}
1615
1616/**
1617 * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
1618 * the sci core to retrieve the io tag for a given task request.
1619 * @request: This parameter is the isci_request object.
1620 *
1621 * io tag for specified task request.
1622 */
1623u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
1624{
1625 u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1626
1627 if (tmf_task == request->ttype) {
1628 struct isci_tmf *tmf = isci_request_access_tmf(request);
1629 io_tag = tmf->io_tag;
1630 }
1631
1632 dev_dbg(&request->isci_host->pdev->dev,
1633 "%s: request = %p, io_tag = %d\n",
1634 __func__, request, io_tag);
1635
1636 return io_tag;
1637}
1638
1639/**
1640 * isci_task_ssp_request_get_response_data_address() - This function is called
1641 * by the sci core to retrieve the response data address for a given task
1642 * request.
1643 * @request: This parameter is the isci_request object.
1644 *
1645 * response data address for specified task request.
1646 */
1647void *isci_task_ssp_request_get_response_data_address(
1648 struct isci_request *request)
1649{
1650 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1651
1652 return &isci_tmf->resp.resp_iu;
1653}
1654
1655/**
1656 * isci_task_ssp_request_get_response_data_length() - This function is called
1657 * by the sci core to retrieve the response data length for a given task
1658 * request.
1659 * @request: This parameter is the isci_request object.
1660 *
1661 * response data length for specified task request.
1662 */
1663u32 isci_task_ssp_request_get_response_data_length(
1664 struct isci_request *request)
1665{
1666 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1667
1668 return sizeof(isci_tmf->resp.resp_iu);
1669}
1670
1671/**
1672 * isci_bus_reset_handler() - This function performs a target reset of the
1673 * device referenced by "cmd'. This function is exported through the
1674 * "struct scsi_host_template" structure such that it is called when an I/O
1675 * recovery process has escalated to a target reset. Note that this function
1676 * is called from the scsi error handler event thread, so may block on calls.
1677 * @scsi_cmd: This parameter specifies the target to be reset.
1678 *
1679 * SUCCESS if the reset process was successful, else FAILED.
1680 */
1681int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1682{
1683 unsigned long flags = 0;
1684 struct isci_host *isci_host = NULL;
1685 enum sci_status status;
1686 int base_status;
1687 struct isci_remote_device *isci_dev
1688 = isci_dev_from_domain_dev(
1689 sdev_to_domain_dev(cmd->device));
1690
1691 dev_dbg(&cmd->device->sdev_gendev,
1692 "%s: cmd %p, isci_dev %p\n",
1693 __func__, cmd, isci_dev);
1694
1695 if (!isci_dev) {
1696 dev_warn(&cmd->device->sdev_gendev,
1697 "%s: isci_dev is GONE!\n",
1698 __func__);
1699
1700 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1701 }
1702
1703 if (isci_dev->isci_port != NULL)
1704 isci_host = isci_dev->isci_port->isci_host;
1705
1706 if (isci_host != NULL)
1707 spin_lock_irqsave(&isci_host->scic_lock, flags);
1708
3a97eec6 1709 status = scic_remote_device_reset(to_sci_dev(isci_dev));
6f231dda
DW
1710 if (status != SCI_SUCCESS) {
1711
1712 if (isci_host != NULL)
1713 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1714
1715 scmd_printk(KERN_WARNING, cmd,
1716 "%s: scic_remote_device_reset(%p) returned %d!\n",
1717 __func__, isci_dev, status);
1718
1719 return TMF_RESP_FUNC_FAILED;
1720 }
1721 if (isci_host != NULL)
1722 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1723
6f231dda
DW
1724 /* Make sure all pending requests are able to be fully terminated. */
1725 isci_device_clear_reset_pending(isci_dev);
1726
1727 /* Terminate in-progress I/O now. */
1728 isci_remote_device_nuke_requests(isci_dev);
1729
1730 /* Call into the libsas default handler (which calls sas_phy_reset). */
1731 base_status = sas_eh_bus_reset_handler(cmd);
1732
1733 if (base_status != SUCCESS) {
1734
1735 /* There can be cases where the resets to individual devices
1736 * behind an expander will fail because of an unplug of the
1737 * expander itself.
1738 */
1739 scmd_printk(KERN_WARNING, cmd,
1740 "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
1741 __func__, cmd, base_status);
1742 }
1743
1744 /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
1745
1746 if (isci_host != NULL)
1747 spin_lock_irqsave(&isci_host->scic_lock, flags);
3a97eec6 1748 status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
6f231dda
DW
1749
1750 if (isci_host != NULL)
1751 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1752
1753 if (status != SCI_SUCCESS) {
1754 scmd_printk(KERN_WARNING, cmd,
1755 "%s: scic_remote_device_reset_complete(%p) "
1756 "returned %d!\n",
1757 __func__, isci_dev, status);
1758 }
1759 /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
1760
1761 dev_dbg(&cmd->device->sdev_gendev,
1762 "%s: cmd %p, isci_dev %p complete.\n",
1763 __func__, cmd, isci_dev);
1764
6f231dda
DW
1765 return TMF_RESP_FUNC_COMPLETE;
1766}
This page took 0.101095 seconds and 5 git commands to generate.