blob: 5e80e844922393f6e6738274d75b3749c7a2500b [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 <linux/completion.h>
57#include "scic_task_request.h"
58#include "scic_remote_device.h"
59#include "scic_io_request.h"
60#include "scic_sds_remote_device.h"
61#include "scic_sds_remote_node_context.h"
62#include "isci.h"
63#include "request.h"
64#include "sata.h"
65#include "task.h"
66
67
68/**
69 * isci_task_execute_task() - This function is one of the SAS Domain Template
70 * functions. This function is called by libsas to send a task down to
71 * hardware.
72 * @task: This parameter specifies the SAS task to send.
73 * @num: This parameter specifies the number of tasks to queue.
74 * @gfp_flags: This parameter specifies the context of this call.
75 *
76 * status, zero indicates success.
77 */
78int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
79{
80 struct isci_host *isci_host;
81 struct isci_request *request = NULL;
82 struct isci_remote_device *device;
83 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -070084 int ret;
85 enum sci_status status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -070086 enum isci_status device_status;
Dan Williams6f231dd2011-07-02 22:56:22 -070087
88 dev_dbg(task->dev->port->ha->dev, "%s: num=%d\n", __func__, num);
89
Dan Williams6f231dd2011-07-02 22:56:22 -070090 if ((task->dev == NULL) || (task->dev->port == NULL)) {
91
92 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi midlayer
93 * removes the target.
94 */
95 isci_task_complete_for_upper_layer(
96 task,
97 SAS_TASK_UNDELIVERED,
98 SAS_DEVICE_UNKNOWN,
99 isci_perform_normal_io_completion
100 );
101 return 0; /* The I/O was accepted (and failed). */
102 }
103 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
104
105 /* Check if we have room for more tasks */
106 ret = isci_host_can_queue(isci_host, num);
107
108 if (ret) {
109 dev_warn(task->dev->port->ha->dev, "%s: queue full\n", __func__);
110 return ret;
111 }
112
113 do {
114 dev_dbg(task->dev->port->ha->dev,
115 "task = %p, num = %d; dev = %p; cmd = %p\n",
116 task, num, task->dev, task->uldd_task);
117
118 if ((task->dev == NULL) || (task->dev->port == NULL)) {
119 dev_warn(task->dev->port->ha->dev,
120 "%s: task %p's port or dev == NULL!\n",
121 __func__, task);
122
123 /* Indicate SAS_TASK_UNDELIVERED, so that the scsi
124 * midlayer removes the target.
125 */
126 isci_task_complete_for_upper_layer(
127 task,
128 SAS_TASK_UNDELIVERED,
129 SAS_DEVICE_UNKNOWN,
130 isci_perform_normal_io_completion
131 );
132 /* We don't have a valid host reference, so we
133 * can't control the host queueing condition.
134 */
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700135 goto next_task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700136 }
137
138 device = isci_dev_from_domain_dev(task->dev);
139
140 isci_host = isci_host_from_sas_ha(task->dev->port->ha);
141
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700142 if (device)
143 device_status = device->status;
144 else
145 device_status = isci_freed;
146
147 /* From this point onward, any process that needs to guarantee
148 * that there is no kernel I/O being started will have to wait
149 * for the quiesce spinlock.
150 */
151
152 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700153
154 /* Forces a retry from scsi mid layer. */
155 dev_warn(task->dev->port->ha->dev,
156 "%s: task %p: isci_host->status = %d, "
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700157 "device = %p; device_status = 0x%x\n\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700158 __func__,
159 task,
160 isci_host_get_state(isci_host),
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700161 device, device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700162
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700163 if (device_status == isci_ready) {
164 /* Indicate QUEUE_FULL so that the scsi midlayer
165 * retries.
166 */
167 isci_task_complete_for_upper_layer(
168 task,
169 SAS_TASK_COMPLETE,
170 SAS_QUEUE_FULL,
171 isci_perform_normal_io_completion
172 );
173 } else {
174 /* Else, the device is going down. */
175 isci_task_complete_for_upper_layer(
176 task,
177 SAS_TASK_UNDELIVERED,
178 SAS_DEVICE_UNKNOWN,
179 isci_perform_normal_io_completion
180 );
181 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700182 isci_host_can_dequeue(isci_host, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700183 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700184 /* There is a device and it's ready for I/O. */
185 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700186
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700187 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
188
189 spin_unlock_irqrestore(&task->task_state_lock,
190 flags);
191
192 isci_task_complete_for_upper_layer(
193 task,
194 SAS_TASK_UNDELIVERED,
195 SAM_STAT_TASK_ABORTED,
196 isci_perform_normal_io_completion
197 );
198
199 /* The I/O was aborted. */
200
201 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700202 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
203 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700204
205 /* build and send the request. */
206 status = isci_request_execute(isci_host, task, &request,
207 gfp_flags);
208
209 if (status != SCI_SUCCESS) {
210
211 spin_lock_irqsave(&task->task_state_lock, flags);
212 /* Did not really start this command. */
213 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
214 spin_unlock_irqrestore(&task->task_state_lock, flags);
215
216 /* Indicate QUEUE_FULL so that the scsi
217 * midlayer retries. if the request
218 * failed for remote device reasons,
219 * it gets returned as
220 * SAS_TASK_UNDELIVERED next time
221 * through.
222 */
223 isci_task_complete_for_upper_layer(
Dan Williams6f231dd2011-07-02 22:56:22 -0700224 task,
225 SAS_TASK_COMPLETE,
226 SAS_QUEUE_FULL,
227 isci_perform_normal_io_completion
228 );
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700229 isci_host_can_dequeue(isci_host, 1);
230 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700231 }
232 }
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700233next_task:
Dan Williams6f231dd2011-07-02 22:56:22 -0700234 task = list_entry(task->list.next, struct sas_task, list);
235 } while (--num > 0);
236 return 0;
237}
238
239
240
241/**
242 * isci_task_request_build() - This function builds the task request object.
243 * @isci_host: This parameter specifies the ISCI host object
244 * @request: This parameter points to the isci_request object allocated in the
245 * request construct function.
246 * @tmf: This parameter is the task management struct to be built
247 *
248 * SCI_SUCCESS on successfull completion, or specific failure code.
249 */
250static enum sci_status isci_task_request_build(
251 struct isci_host *isci_host,
252 struct isci_request **isci_request,
253 struct isci_tmf *isci_tmf)
254{
255 struct scic_sds_remote_device *sci_device;
256 enum sci_status status = SCI_FAILURE;
257 struct isci_request *request;
258 struct isci_remote_device *isci_device;
259/* struct sci_sas_identify_address_frame_protocols dev_protocols; */
260 struct smp_discover_response_protocols dev_protocols;
261
262
263 dev_dbg(&isci_host->pdev->dev,
264 "%s: isci_tmf = %p\n", __func__, isci_tmf);
265
266 isci_device = isci_tmf->device;
Dan Williams3a97eec2011-03-04 11:51:43 -0800267 sci_device = to_sci_dev(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700268
269 /* do common allocation and init of request object. */
270 status = isci_request_alloc_tmf(
271 isci_host,
272 isci_tmf,
273 &request,
274 isci_device,
275 GFP_ATOMIC
276 );
277
278 if (status != SCI_SUCCESS)
279 goto out;
280
281 /* let the core do it's construct. */
282 status = scic_task_request_construct(
283 isci_host->core_controller,
284 sci_device,
285 SCI_CONTROLLER_INVALID_IO_TAG,
286 request,
287 request->sci_request_mem_ptr,
288 &request->sci_request_handle
289 );
290
291 if (status != SCI_SUCCESS) {
292 dev_warn(&isci_host->pdev->dev,
293 "%s: scic_task_request_construct failed - "
294 "status = 0x%x\n",
295 __func__,
296 status);
297 goto errout;
298 }
299
300 sci_object_set_association(
301 request->sci_request_handle,
302 request
303 );
304
305 scic_remote_device_get_protocols(
306 sci_device,
307 &dev_protocols
308 );
309
310 /* let the core do it's protocol
311 * specific construction.
312 */
313 if (dev_protocols.u.bits.attached_ssp_target) {
314
315 isci_tmf->proto = SAS_PROTOCOL_SSP;
316 status = scic_task_request_construct_ssp(
317 request->sci_request_handle
318 );
319 if (status != SCI_SUCCESS)
320 goto errout;
321 }
322
323 if (dev_protocols.u.bits.attached_stp_target) {
324
325 isci_tmf->proto = SAS_PROTOCOL_SATA;
326 status = isci_sata_management_task_request_build(request);
327
328 if (status != SCI_SUCCESS)
329 goto errout;
330 }
331
332 goto out;
333
334 errout:
335
336 /* release the dma memory if we fail. */
337 isci_request_free(isci_host, request);
338 request = NULL;
339
340 out:
341 *isci_request = request;
342 return status;
343}
344
345/**
346 * isci_tmf_timeout_cb() - This function is called as a kernel callback when
347 * the timeout period for the TMF has expired.
348 *
349 *
350 */
351static void isci_tmf_timeout_cb(void *tmf_request_arg)
352{
353 struct isci_request *request = (struct isci_request *)tmf_request_arg;
354 struct isci_tmf *tmf = isci_request_access_tmf(request);
355 enum sci_status status;
356
357 BUG_ON(request->ttype != tmf_task);
358
359 /* This task management request has timed-out. Terminate the request
360 * so that the request eventually completes to the requestor in the
361 * request completion callback path.
362 */
363 /* Note - the timer callback function itself has provided spinlock
364 * exclusion from the start and completion paths. No need to take
365 * the request->isci_host->scic_lock here.
366 */
367
368 if (tmf->timeout_timer != NULL) {
369 /* Call the users callback, if any. */
370 if (tmf->cb_state_func != NULL)
371 tmf->cb_state_func(isci_tmf_timed_out, tmf,
372 tmf->cb_data);
373
374 /* Terminate the TMF transmit request. */
375 status = scic_controller_terminate_request(
376 request->isci_host->core_controller,
Dan Williams3a97eec2011-03-04 11:51:43 -0800377 to_sci_dev(request->isci_device),
Dan Williams6f231dd2011-07-02 22:56:22 -0700378 request->sci_request_handle
379 );
380
381 dev_dbg(&request->isci_host->pdev->dev,
382 "%s: tmf_request = %p; tmf = %p; status = %d\n",
383 __func__, request, tmf, status);
384 } else
385 dev_dbg(&request->isci_host->pdev->dev,
386 "%s: timer already canceled! "
387 "tmf_request = %p; tmf = %p\n",
388 __func__, request, tmf);
389
390 /* No need to unlock since the caller to this callback is doing it for
391 * us.
392 * request->isci_host->scic_lock
393 */
394}
395
396/**
397 * isci_task_execute_tmf() - This function builds and sends a task request,
398 * then waits for the completion.
399 * @isci_host: This parameter specifies the ISCI host object
400 * @tmf: This parameter is the pointer to the task management structure for
401 * this request.
402 * @timeout_ms: This parameter specifies the timeout period for the task
403 * management request.
404 *
405 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
406 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
407 */
408int isci_task_execute_tmf(
409 struct isci_host *isci_host,
410 struct isci_tmf *tmf,
411 unsigned long timeout_ms)
412{
413 DECLARE_COMPLETION_ONSTACK(completion);
414 enum sci_status status = SCI_FAILURE;
415 struct scic_sds_remote_device *sci_device;
416 struct isci_remote_device *isci_device = tmf->device;
417 struct isci_request *request;
418 int ret = TMF_RESP_FUNC_FAILED;
419 unsigned long flags;
420
421 /* sanity check, return TMF_RESP_FUNC_FAILED
422 * if the device is not there and ready.
423 */
Dan Williams8acaec12011-03-07 14:47:35 -0800424 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700425 dev_dbg(&isci_host->pdev->dev,
426 "%s: isci_device = %p not ready (%d)\n",
427 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800428 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700429 return TMF_RESP_FUNC_FAILED;
430 } else
431 dev_dbg(&isci_host->pdev->dev,
432 "%s: isci_device = %p\n",
433 __func__, isci_device);
434
Dan Williams3a97eec2011-03-04 11:51:43 -0800435 sci_device = to_sci_dev(isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700436
437 /* Assign the pointer to the TMF's completion kernel wait structure. */
438 tmf->complete = &completion;
439
440 isci_task_request_build(
441 isci_host,
442 &request,
443 tmf
444 );
445
446 if (!request) {
447 dev_warn(&isci_host->pdev->dev,
448 "%s: isci_task_request_build failed\n",
449 __func__);
450 return TMF_RESP_FUNC_FAILED;
451 }
452
453 /* Allocate the TMF timeout timer. */
Dan Williams6f231dd2011-07-02 22:56:22 -0700454 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams7c40a802011-03-02 11:49:26 -0800455 tmf->timeout_timer = isci_timer_create(isci_host, request, isci_tmf_timeout_cb);
Dan Williams6f231dd2011-07-02 22:56:22 -0700456
457 /* Start the timer. */
458 if (tmf->timeout_timer)
459 isci_timer_start(tmf->timeout_timer, timeout_ms);
460 else
461 dev_warn(&isci_host->pdev->dev,
462 "%s: isci_timer_create failed!!!!\n",
463 __func__);
464
465 /* start the TMF io. */
466 status = scic_controller_start_task(
467 isci_host->core_controller,
468 sci_device,
469 request->sci_request_handle,
470 SCI_CONTROLLER_INVALID_IO_TAG
471 );
472
473 if (status != SCI_SUCCESS) {
474 dev_warn(&isci_host->pdev->dev,
475 "%s: start_io failed - status = 0x%x, request = %p\n",
476 __func__,
477 status,
478 request);
479 goto cleanup_request;
480 }
481
482 /* Call the users callback, if any. */
483 if (tmf->cb_state_func != NULL)
484 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
485
486 /* Change the state of the TMF-bearing request to "started". */
487 isci_request_change_state(request, started);
488
489 /* add the request to the remote device request list. */
490 list_add(&request->dev_node, &isci_device->reqs_in_process);
491
492 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
493
494 /* Wait for the TMF to complete, or a timeout. */
495 wait_for_completion(&completion);
496
497 isci_print_tmf(tmf);
498
499 if (tmf->status == SCI_SUCCESS)
500 ret = TMF_RESP_FUNC_COMPLETE;
501 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
502 dev_dbg(&isci_host->pdev->dev,
503 "%s: tmf.status == "
504 "SCI_FAILURE_IO_RESPONSE_VALID\n",
505 __func__);
506 ret = TMF_RESP_FUNC_COMPLETE;
507 }
508 /* Else - leave the default "failed" status alone. */
509
510 dev_dbg(&isci_host->pdev->dev,
511 "%s: completed request = %p\n",
512 __func__,
513 request);
514
515 if (request->io_request_completion != NULL) {
516
517 /* The fact that this is non-NULL for a TMF request
518 * means there is a thread waiting for this TMF to
519 * finish.
520 */
521 complete(request->io_request_completion);
522 }
523
524 spin_lock_irqsave(&isci_host->scic_lock, flags);
525
526 cleanup_request:
527
528 /* Clean up the timer if needed. */
529 if (tmf->timeout_timer) {
Dan Williams7c40a802011-03-02 11:49:26 -0800530 isci_del_timer(isci_host, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -0700531 tmf->timeout_timer = NULL;
532 }
533
534 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
535
536 isci_request_free(isci_host, request);
537
538 return ret;
539}
540
541void isci_task_build_tmf(
542 struct isci_tmf *tmf,
543 struct isci_remote_device *isci_device,
544 enum isci_tmf_function_codes code,
545 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
546 struct isci_tmf *,
547 void *),
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800548 struct isci_request *old_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700549{
550 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
551 "%s: isci_device = %p\n", __func__, isci_device);
552
553 memset(tmf, 0, sizeof(*tmf));
554
555 tmf->device = isci_device;
556 tmf->tmf_code = code;
557 tmf->timeout_timer = NULL;
558 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800559 tmf->cb_data = old_request;
560 tmf->io_tag = old_request->io_tag;
561
Dan Williams6f231dd2011-07-02 22:56:22 -0700562}
563
564static struct isci_request *isci_task_get_request_from_task(
565 struct sas_task *task,
566 struct isci_host **isci_host,
567 struct isci_remote_device **isci_device)
568{
569
570 struct isci_request *request = NULL;
571 unsigned long flags;
572
573 spin_lock_irqsave(&task->task_state_lock, flags);
574
575 request = task->lldd_task;
576
577 /* If task is already done, the request isn't valid */
578 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
579 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
580 (request != NULL)) {
581
582 if (isci_host != NULL)
583 *isci_host = request->isci_host;
584
585 if (isci_device != NULL)
586 *isci_device = request->isci_device;
587 }
588
589 spin_unlock_irqrestore(&task->task_state_lock, flags);
590
591 return request;
592}
593
594/**
595 * isci_task_validate_request_to_abort() - This function checks the given I/O
596 * against the "started" state. If the request is still "started", it's
597 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
598 * BEFORE CALLING THIS FUNCTION.
599 * @isci_request: This parameter specifies the request object to control.
600 * @isci_host: This parameter specifies the ISCI host object
601 * @isci_device: This is the device to which the request is pending.
602 * @aborted_io_completion: This is a completion structure that will be added to
603 * the request in case it is changed to aborting; this completion is
604 * triggered when the request is fully completed.
605 *
606 * Either "started" on successful change of the task status to "aborted", or
607 * "unallocated" if the task cannot be controlled.
608 */
609static enum isci_request_status isci_task_validate_request_to_abort(
610 struct isci_request *isci_request,
611 struct isci_host *isci_host,
612 struct isci_remote_device *isci_device,
613 struct completion *aborted_io_completion)
614{
615 enum isci_request_status old_state = unallocated;
616
617 /* Only abort the task if it's in the
618 * device's request_in_process list
619 */
620 if (isci_request && !list_empty(&isci_request->dev_node)) {
621 old_state = isci_request_change_started_to_aborted(
622 isci_request, aborted_io_completion);
623
Dan Williams6f231dd2011-07-02 22:56:22 -0700624 }
625
626 return old_state;
627}
628
629static void isci_request_cleanup_completed_loiterer(
630 struct isci_host *isci_host,
631 struct isci_remote_device *isci_device,
632 struct isci_request *isci_request)
633{
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800634 struct sas_task *task;
635 unsigned long flags;
636
637 task = (isci_request->ttype == io_task)
638 ? isci_request_access_task(isci_request)
639 : NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700640
641 dev_dbg(&isci_host->pdev->dev,
642 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800643 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700644
645 spin_lock_irqsave(&isci_host->scic_lock, flags);
646 list_del_init(&isci_request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700647 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
648
Jeff Skirvina5fde222011-03-04 14:06:42 -0800649 if (task != NULL) {
650
651 spin_lock_irqsave(&task->task_state_lock, flags);
652 task->lldd_task = NULL;
653
654 isci_set_task_doneflags(task);
655
656 /* If this task is not in the abort path, call task_done. */
657 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
658
659 spin_unlock_irqrestore(&task->task_state_lock, flags);
660 task->task_done(task);
661 } else
662 spin_unlock_irqrestore(&task->task_state_lock, flags);
663 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700664 isci_request_free(isci_host, isci_request);
665}
666/**
667 * isci_terminate_request_core() - This function will terminate the given
668 * request, and wait for it to complete. This function must only be called
669 * from a thread that can wait. Note that the request is terminated and
670 * completed (back to the host, if started there).
671 * @isci_host: This SCU.
672 * @isci_device: The target.
673 * @isci_request: The I/O request to be terminated.
674 *
675 *
676 */
677static void isci_terminate_request_core(
678 struct isci_host *isci_host,
679 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800680 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700681{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800682 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700683 bool was_terminated = false;
684 bool needs_cleanup_handling = false;
685 enum isci_request_status request_status;
686 unsigned long flags;
687
688 dev_dbg(&isci_host->pdev->dev,
689 "%s: device = %p; request = %p\n",
690 __func__, isci_device, isci_request);
691
692 /* Peek at the current status of the request. This will tell
693 * us if there was special handling on the request such that it
694 * needs to be detached and freed here.
695 */
696 spin_lock_irqsave(&isci_request->state_lock, flags);
697 request_status = isci_request_get_state(isci_request);
698
Jeff Skirvina5fde222011-03-04 14:06:42 -0800699 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
700 && ((request_status == aborted)
701 || (request_status == aborting)
702 || (request_status == terminating)
703 || (request_status == completed)
704 )
705 ) {
706
Dan Williams6f231dd2011-07-02 22:56:22 -0700707 /* The completion routine won't free a request in
708 * the aborted/aborting/terminating state, so we do
709 * it here.
710 */
711 needs_cleanup_handling = true;
Jeff Skirvina5fde222011-03-04 14:06:42 -0800712 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700713 spin_unlock_irqrestore(&isci_request->state_lock, flags);
714
715 spin_lock_irqsave(&isci_host->scic_lock, flags);
716 /* Make sure the request wasn't just sitting around signalling
717 * device condition (if the request handle is NULL, then the
718 * request completed but needed additional handling here).
719 */
720 if (isci_request->sci_request_handle != NULL) {
721 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800722 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700723 status = scic_controller_terminate_request(
724 isci_host->core_controller,
Dan Williams3a97eec2011-03-04 11:51:43 -0800725 to_sci_dev(isci_device),
Dan Williams6f231dd2011-07-02 22:56:22 -0700726 isci_request->sci_request_handle
727 );
728 }
729 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
730
731 /*
732 * The only time the request to terminate will
733 * fail is when the io request is completed and
734 * being aborted.
735 */
736 if (status != SCI_SUCCESS)
737 dev_err(&isci_host->pdev->dev,
738 "%s: scic_controller_terminate_request"
739 " returned = 0x%x\n",
740 __func__,
741 status);
742 else {
743 if (was_terminated) {
744 dev_dbg(&isci_host->pdev->dev,
745 "%s: before completion wait (%p)\n",
746 __func__,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800747 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700748
749 /* Wait here for the request to complete. */
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800750 wait_for_completion(isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700751
752 dev_dbg(&isci_host->pdev->dev,
753 "%s: after completion wait (%p)\n",
754 __func__,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800755 isci_request->io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700756 }
757
758 if (needs_cleanup_handling)
759 isci_request_cleanup_completed_loiterer(
760 isci_host, isci_device, isci_request
761 );
762 }
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800763 /* Clear the completion pointer from the request. */
764 isci_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700765}
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800766
Dan Williams6f231dd2011-07-02 22:56:22 -0700767static void isci_terminate_request(
768 struct isci_host *isci_host,
769 struct isci_remote_device *isci_device,
770 struct isci_request *isci_request,
771 enum isci_request_status new_request_state)
772{
773 enum isci_request_status old_state;
774
775 DECLARE_COMPLETION_ONSTACK(request_completion);
776 unsigned long flags;
777
778 spin_lock_irqsave(&isci_host->scic_lock, flags);
779
780 /* Change state to "new_request_state" if it is currently "started" */
781 old_state = isci_request_change_started_to_newstate(
782 isci_request,
783 &request_completion,
784 new_request_state
785 );
786
Jeff Skirvina5fde222011-03-04 14:06:42 -0800787 if ((old_state == started) || (old_state == completed)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700788
Jeff Skirvina5fde222011-03-04 14:06:42 -0800789 /* If the old_state is started:
790 * This request was not already being aborted. If it had been,
Dan Williams6f231dd2011-07-02 22:56:22 -0700791 * then the aborting I/O (ie. the TMF request) would not be in
792 * the aborting state, and thus would be terminated here. Note
793 * that since the TMF completion's call to the kernel function
794 * "complete()" does not happen until the pending I/O request
795 * terminate fully completes, we do not have to implement a
796 * special wait here for already aborting requests - the
797 * termination of the TMF request will force the request
798 * to finish it's already started terminate.
Jeff Skirvina5fde222011-03-04 14:06:42 -0800799 *
800 * If old_state == completed:
801 * This request completed from the SCU hardware perspective
802 * and now just needs cleaning up in terms of freeing the
803 * request and potentially calling up to libsas.
Dan Williams6f231dd2011-07-02 22:56:22 -0700804 */
805 isci_terminate_request_core(isci_host, isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800806 isci_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800807 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700808}
809
810/**
811 * isci_terminate_pending_requests() - This function will change the all of the
812 * requests on the given device's state to "aborting", will terminate the
813 * requests, and wait for them to complete. This function must only be
814 * called from a thread that can wait. Note that the requests are all
815 * terminated and completed (back to the host, if started there).
816 * @isci_host: This parameter specifies SCU.
817 * @isci_device: This parameter specifies the target.
818 *
819 *
820 */
821void isci_terminate_pending_requests(
822 struct isci_host *isci_host,
823 struct isci_remote_device *isci_device,
824 enum isci_request_status new_request_state)
825{
826 struct isci_request *isci_request;
827 struct sas_task *task;
828 bool done = false;
829 unsigned long flags;
830
831 dev_dbg(&isci_host->pdev->dev,
832 "%s: isci_device = %p (new request state = %d)\n",
833 __func__, isci_device, new_request_state);
834
835 #define ISCI_TERMINATE_SHOW_PENDING_REQUESTS
836 #ifdef ISCI_TERMINATE_SHOW_PENDING_REQUESTS
837 {
838 struct isci_request *request;
839
840 /* Only abort the task if it's in the
841 * device's request_in_process list
842 */
843 list_for_each_entry(request,
844 &isci_device->reqs_in_process,
845 dev_node)
846 dev_dbg(&isci_host->pdev->dev,
847 "%s: isci_device = %p; request is on "
848 "reqs_in_process list: %p\n",
849 __func__, isci_device, request);
850 }
851 #endif /* ISCI_TERMINATE_SHOW_PENDING_REQUESTS */
852
853 /* Clean up all pending requests. */
854 do {
855 spin_lock_irqsave(&isci_host->scic_lock, flags);
856
857 if (list_empty(&isci_device->reqs_in_process)) {
858
859 done = true;
860 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
861
862 dev_dbg(&isci_host->pdev->dev,
863 "%s: isci_device = %p; done.\n",
864 __func__, isci_device);
865 } else {
866 /* The list was not empty - grab the first request. */
867 isci_request = list_first_entry(
868 &isci_device->reqs_in_process,
869 struct isci_request, dev_node
870 );
871 /* Note that we are not expecting to have to control
872 * the target to abort the request.
873 */
874 isci_request->complete_in_target = true;
875
876 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
877
878 /* Get the libsas task reference. */
879 task = isci_request_access_task(isci_request);
880
881 dev_dbg(&isci_host->pdev->dev,
882 "%s: isci_device=%p request=%p; task=%p\n",
883 __func__, isci_device, isci_request, task);
884
885 /* Mark all still pending I/O with the selected next
886 * state.
887 */
888 isci_terminate_request(isci_host, isci_device,
889 isci_request, new_request_state
890 );
Dan Williams6f231dd2011-07-02 22:56:22 -0700891 }
892 } while (!done);
893}
894
895/**
896 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
897 * Template functions.
898 * @lun: This parameter specifies the lun to be reset.
899 *
900 * status, zero indicates success.
901 */
902static int isci_task_send_lu_reset_sas(
903 struct isci_host *isci_host,
904 struct isci_remote_device *isci_device,
905 u8 *lun)
906{
907 struct isci_tmf tmf;
908 int ret = TMF_RESP_FUNC_FAILED;
909
910 dev_dbg(&isci_host->pdev->dev,
911 "%s: isci_host = %p, isci_device = %p\n",
912 __func__, isci_host, isci_device);
913 /* Send the LUN reset to the target. By the time the call returns,
914 * the TMF has fully exected in the target (in which case the return
915 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
916 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
917 */
918 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
919 NULL);
920
921 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
922 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
923
924 if (ret == TMF_RESP_FUNC_COMPLETE)
925 dev_dbg(&isci_host->pdev->dev,
926 "%s: %p: TMF_LU_RESET passed\n",
927 __func__, isci_device);
928 else
929 dev_dbg(&isci_host->pdev->dev,
930 "%s: %p: TMF_LU_RESET failed (%x)\n",
931 __func__, isci_device, ret);
932
933 return ret;
934}
935
936/**
937 * isci_task_lu_reset() - This function is one of the SAS Domain Template
938 * functions. This is one of the Task Management functoins called by libsas,
939 * to reset the given lun. Note the assumption that while this call is
940 * executing, no I/O will be sent by the host to the device.
941 * @lun: This parameter specifies the lun to be reset.
942 *
943 * status, zero indicates success.
944 */
945int isci_task_lu_reset(
946 struct domain_device *domain_device,
947 u8 *lun)
948{
949 struct isci_host *isci_host = NULL;
950 struct isci_remote_device *isci_device = NULL;
951 int ret;
952 bool device_stopping = false;
953
954 if (domain_device == NULL) {
955 pr_warn("%s: domain_device == NULL\n", __func__);
956 return TMF_RESP_FUNC_FAILED;
957 }
958
959 isci_device = isci_dev_from_domain_dev(domain_device);
960
961 if (domain_device->port != NULL)
962 isci_host = isci_host_from_sas_ha(domain_device->port->ha);
963
964 pr_debug("%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
965 __func__, domain_device, isci_host, isci_device);
966
967 if (isci_device != NULL)
968 device_stopping = (isci_device->status == isci_stopping)
969 || (isci_device->status == isci_stopped);
970
971 /* If there is a device reset pending on any request in the
972 * device's list, fail this LUN reset request in order to
973 * escalate to the device reset.
974 */
975 if ((isci_device == NULL) ||
976 (isci_host == NULL) ||
977 ((isci_host != NULL) &&
978 (isci_device != NULL) &&
979 (device_stopping ||
980 (isci_device_is_reset_pending(isci_host, isci_device))))) {
981 dev_warn(&isci_host->pdev->dev,
982 "%s: No dev (%p), no host (%p), or "
983 "RESET PENDING: domain_device=%p\n",
984 __func__, isci_device, isci_host, domain_device);
985 return TMF_RESP_FUNC_FAILED;
986 }
987
Dan Williams6f231dd2011-07-02 22:56:22 -0700988 /* Send the task management part of the reset. */
989 if (sas_protocol_ata(domain_device->tproto)) {
990 ret = isci_task_send_lu_reset_sata(
991 isci_host, isci_device, lun
992 );
993 } else
994 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
995
996 /* If the LUN reset worked, all the I/O can now be terminated. */
997 if (ret == TMF_RESP_FUNC_COMPLETE)
998 /* Terminate all I/O now. */
999 isci_terminate_pending_requests(isci_host,
1000 isci_device,
1001 terminating);
1002
Dan Williams6f231dd2011-07-02 22:56:22 -07001003 return ret;
1004}
1005
1006
1007/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
1008int isci_task_clear_nexus_port(struct asd_sas_port *port)
1009{
1010 return TMF_RESP_FUNC_FAILED;
1011}
1012
1013
1014
1015int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
1016{
1017 return TMF_RESP_FUNC_FAILED;
1018}
1019
1020int isci_task_I_T_nexus_reset(struct domain_device *dev)
1021{
1022 return TMF_RESP_FUNC_FAILED;
1023}
1024
1025
1026/* Task Management Functions. Must be called from process context. */
1027
1028/**
1029 * isci_abort_task_process_cb() - This is a helper function for the abort task
1030 * TMF command. It manages the request state with respect to the successful
1031 * transmission / completion of the abort task request.
1032 * @cb_state: This parameter specifies when this function was called - after
1033 * the TMF request has been started and after it has timed-out.
1034 * @tmf: This parameter specifies the TMF in progress.
1035 *
1036 *
1037 */
1038static void isci_abort_task_process_cb(
1039 enum isci_tmf_cb_state cb_state,
1040 struct isci_tmf *tmf,
1041 void *cb_data)
1042{
1043 struct isci_request *old_request;
1044
1045 old_request = (struct isci_request *)cb_data;
1046
1047 dev_dbg(&old_request->isci_host->pdev->dev,
1048 "%s: tmf=%p, old_request=%p\n",
1049 __func__, tmf, old_request);
1050
1051 switch (cb_state) {
1052
1053 case isci_tmf_started:
1054 /* The TMF has been started. Nothing to do here, since the
1055 * request state was already set to "aborted" by the abort
1056 * task function.
1057 */
1058 BUG_ON(old_request->status != aborted);
1059 break;
1060
1061 case isci_tmf_timed_out:
1062
1063 /* Set the task's state to "aborting", since the abort task
1064 * function thread set it to "aborted" (above) in anticipation
1065 * of the task management request working correctly. Since the
1066 * timeout has now fired, the TMF request failed. We set the
1067 * state such that the request completion will indicate the
1068 * device is no longer present.
1069 */
1070 isci_request_change_state(old_request, aborting);
1071 break;
1072
1073 default:
1074 dev_err(&old_request->isci_host->pdev->dev,
1075 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1076 __func__, cb_state, tmf, old_request);
1077 break;
1078 }
1079}
1080
1081/**
1082 * isci_task_abort_task() - This function is one of the SAS Domain Template
1083 * functions. This function is called by libsas to abort a specified task.
1084 * @task: This parameter specifies the SAS task to abort.
1085 *
1086 * status, zero indicates success.
1087 */
1088int isci_task_abort_task(struct sas_task *task)
1089{
1090 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001091 struct isci_request *old_request = NULL;
1092 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001093 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001094 struct isci_host *isci_host = NULL;
1095 struct isci_tmf tmf;
1096 int ret = TMF_RESP_FUNC_FAILED;
1097 unsigned long flags;
1098 bool any_dev_reset = false;
1099 bool device_stopping;
Dan Williams6f231dd2011-07-02 22:56:22 -07001100
1101 /* Get the isci_request reference from the task. Note that
1102 * this check does not depend on the pending request list
1103 * in the device, because tasks driving resets may land here
1104 * after completion in the core.
1105 */
1106 old_request = isci_task_get_request_from_task(task, &isci_host,
1107 &isci_device);
1108
1109 dev_dbg(&isci_host->pdev->dev,
1110 "%s: task = %p\n", __func__, task);
1111
1112 /* Check if the device has been / is currently being removed.
1113 * If so, no task management will be done, and the I/O will
1114 * be terminated.
1115 */
1116 device_stopping = (isci_device->status == isci_stopping)
1117 || (isci_device->status == isci_stopped);
1118
Dan Williams6f231dd2011-07-02 22:56:22 -07001119 /* This version of the driver will fail abort requests for
1120 * SATA/STP. Failing the abort request this way will cause the
1121 * SCSI error handler thread to escalate to LUN reset
1122 */
1123 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1124 dev_warn(&isci_host->pdev->dev,
1125 " task %p is for a STP/SATA device;"
1126 " returning TMF_RESP_FUNC_FAILED\n"
1127 " to cause a LUN reset...\n", task);
1128 return TMF_RESP_FUNC_FAILED;
1129 }
1130
1131 dev_dbg(&isci_host->pdev->dev,
1132 "%s: old_request == %p\n", __func__, old_request);
1133
Jeff Skirvina5fde222011-03-04 14:06:42 -08001134 if (!device_stopping)
1135 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1136
Dan Williams6f231dd2011-07-02 22:56:22 -07001137 spin_lock_irqsave(&task->task_state_lock, flags);
1138
1139 /* Don't do resets to stopping devices. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001140 if (device_stopping) {
1141
Dan Williams6f231dd2011-07-02 22:56:22 -07001142 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001143 any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001144
Jeff Skirvina5fde222011-03-04 14:06:42 -08001145 } else /* See if there is a pending device reset for this device. */
Dan Williams6f231dd2011-07-02 22:56:22 -07001146 any_dev_reset = any_dev_reset
Jeff Skirvina5fde222011-03-04 14:06:42 -08001147 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001148
1149 /* If the extraction of the request reference from the task
1150 * failed, then the request has been completed (or if there is a
1151 * pending reset then this abort request function must be failed
1152 * in order to escalate to the target reset).
1153 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001154 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001155
1156 /* If the device reset task flag is set, fail the task
1157 * management request. Otherwise, the original request
1158 * has completed.
1159 */
1160 if (any_dev_reset) {
1161
1162 /* Turn off the task's DONE to make sure this
1163 * task is escalated to a target reset.
1164 */
1165 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1166
Jeff Skirvina5fde222011-03-04 14:06:42 -08001167 /* Make the reset happen as soon as possible. */
1168 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1169
1170 spin_unlock_irqrestore(&task->task_state_lock, flags);
1171
Dan Williams6f231dd2011-07-02 22:56:22 -07001172 /* Fail the task management request in order to
1173 * escalate to the target reset.
1174 */
1175 ret = TMF_RESP_FUNC_FAILED;
1176
1177 dev_dbg(&isci_host->pdev->dev,
1178 "%s: Failing task abort in order to "
1179 "escalate to target reset because\n"
1180 "SAS_TASK_NEED_DEV_RESET is set for "
1181 "task %p on dev %p\n",
1182 __func__, task, isci_device);
1183
Jeff Skirvina5fde222011-03-04 14:06:42 -08001184
Dan Williams6f231dd2011-07-02 22:56:22 -07001185 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001186 /* The request has already completed and there
1187 * is nothing to do here other than to set the task
1188 * done bit, and indicate that the task abort function
1189 * was sucessful.
1190 */
1191 isci_set_task_doneflags(task);
1192
Jeff Skirvina5fde222011-03-04 14:06:42 -08001193 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001194
Jeff Skirvina5fde222011-03-04 14:06:42 -08001195 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001196
Jeff Skirvina5fde222011-03-04 14:06:42 -08001197 dev_dbg(&isci_host->pdev->dev,
1198 "%s: abort task not needed for %p\n",
1199 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001200 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001201
1202 return ret;
1203 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001204 else
1205 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001206
1207 spin_lock_irqsave(&isci_host->scic_lock, flags);
1208
Jeff Skirvina5fde222011-03-04 14:06:42 -08001209 /* Check the request status and change to "aborting" if currently
1210 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001211 * struct that will be triggered when the request completes.
1212 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001213 old_state = isci_task_validate_request_to_abort(
1214 old_request, isci_host, isci_device,
1215 &aborted_io_completion);
1216 if ((old_state != started) && (old_state != completed)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001217
1218 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1219
Jeff Skirvina5fde222011-03-04 14:06:42 -08001220 /* The request was already being handled by someone else (because
1221 * they got to set the state away from started).
1222 */
1223 dev_dbg(&isci_host->pdev->dev,
1224 "%s: device = %p; old_request %p already being aborted\n",
1225 __func__,
1226 isci_device, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001227
1228 return TMF_RESP_FUNC_COMPLETE;
1229 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001230 if ((task->task_proto == SAS_PROTOCOL_SMP)
1231 || device_stopping
1232 || old_request->complete_in_target
1233 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001234
1235 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1236
Jeff Skirvina5fde222011-03-04 14:06:42 -08001237 dev_dbg(&isci_host->pdev->dev,
1238 "%s: SMP request (%d)"
1239 " or device is stopping (%d)"
1240 " or complete_in_target (%d), thus no TMF\n",
1241 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1242 device_stopping, old_request->complete_in_target);
1243
Dan Williams6f231dd2011-07-02 22:56:22 -07001244 /* Set the state on the task. */
1245 isci_task_all_done(task);
1246
1247 ret = TMF_RESP_FUNC_COMPLETE;
1248
1249 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001250 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001251 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001252 } else {
1253 /* Fill in the tmf stucture */
1254 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_task_abort,
1255 isci_abort_task_process_cb, old_request);
1256
Dan Williams6f231dd2011-07-02 22:56:22 -07001257 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1258
1259 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1260 ret = isci_task_execute_tmf(isci_host, &tmf,
1261 ISCI_ABORT_TASK_TIMEOUT_MS);
1262
Jeff Skirvina5fde222011-03-04 14:06:42 -08001263 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001264 dev_err(&isci_host->pdev->dev,
1265 "%s: isci_task_send_tmf failed\n",
1266 __func__);
1267 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001268 if (ret == TMF_RESP_FUNC_COMPLETE) {
1269 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001270
Jeff Skirvina5fde222011-03-04 14:06:42 -08001271 /* Clean up the request on our side, and wait for the aborted I/O to
1272 * complete.
1273 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001274 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001275 }
1276
1277 /* Make sure we do not leave a reference to aborted_io_completion */
1278 old_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -07001279 return ret;
1280}
1281
1282/**
1283 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1284 * functions. This is one of the Task Management functoins called by libsas,
1285 * to abort all task for the given lun.
1286 * @d_device: This parameter specifies the domain device associated with this
1287 * request.
1288 * @lun: This parameter specifies the lun associated with this request.
1289 *
1290 * status, zero indicates success.
1291 */
1292int isci_task_abort_task_set(
1293 struct domain_device *d_device,
1294 u8 *lun)
1295{
1296 return TMF_RESP_FUNC_FAILED;
1297}
1298
1299
1300/**
1301 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1302 * functions. This is one of the Task Management functoins called by libsas.
1303 * @d_device: This parameter specifies the domain device associated with this
1304 * request.
1305 * @lun: This parameter specifies the lun associated with this request.
1306 *
1307 * status, zero indicates success.
1308 */
1309int isci_task_clear_aca(
1310 struct domain_device *d_device,
1311 u8 *lun)
1312{
1313 return TMF_RESP_FUNC_FAILED;
1314}
1315
1316
1317
1318/**
1319 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1320 * functions. This is one of the Task Management functoins called by libsas.
1321 * @d_device: This parameter specifies the domain device associated with this
1322 * request.
1323 * @lun: This parameter specifies the lun associated with this request.
1324 *
1325 * status, zero indicates success.
1326 */
1327int isci_task_clear_task_set(
1328 struct domain_device *d_device,
1329 u8 *lun)
1330{
1331 return TMF_RESP_FUNC_FAILED;
1332}
1333
1334
1335/**
1336 * isci_task_query_task() - This function is implemented to cause libsas to
1337 * correctly escalate the failed abort to a LUN or target reset (this is
1338 * because sas_scsi_find_task libsas function does not correctly interpret
1339 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1340 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1341 * returned, libsas will turn this into a target reset
1342 * @task: This parameter specifies the sas task being queried.
1343 * @lun: This parameter specifies the lun associated with this request.
1344 *
1345 * status, zero indicates success.
1346 */
1347int isci_task_query_task(
1348 struct sas_task *task)
1349{
1350 /* See if there is a pending device reset for this device. */
1351 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1352 return TMF_RESP_FUNC_FAILED;
1353 else
1354 return TMF_RESP_FUNC_SUCC;
1355}
1356
1357/**
1358 * isci_task_request_complete() - This function is called by the sci core when
1359 * an task request completes.
1360 * @isci_host: This parameter specifies the ISCI host object
1361 * @request: This parameter is the completed isci_request object.
1362 * @completion_status: This parameter specifies the completion status from the
1363 * sci core.
1364 *
1365 * none.
1366 */
1367void isci_task_request_complete(
1368 struct isci_host *isci_host,
1369 struct isci_request *request,
1370 enum sci_task_status completion_status)
1371{
1372 struct isci_remote_device *isci_device = request->isci_device;
1373 enum isci_request_status old_state;
1374 struct isci_tmf *tmf = isci_request_access_tmf(request);
1375 struct completion *tmf_complete;
1376
1377 dev_dbg(&isci_host->pdev->dev,
1378 "%s: request = %p, status=%d\n",
1379 __func__, request, completion_status);
1380
1381 old_state = isci_request_change_state(request, completed);
1382
1383 tmf->status = completion_status;
1384 request->complete_in_target = true;
1385
1386 if (SAS_PROTOCOL_SSP == tmf->proto) {
1387
1388 memcpy(&tmf->resp.resp_iu,
1389 scic_io_request_get_response_iu_address(
1390 request->sci_request_handle
1391 ),
1392 sizeof(struct sci_ssp_response_iu));
1393
1394 } else if (SAS_PROTOCOL_SATA == tmf->proto) {
1395
1396 memcpy(&tmf->resp.d2h_fis,
1397 scic_stp_io_request_get_d2h_reg_address(
1398 request->sci_request_handle
1399 ),
1400 sizeof(struct sata_fis_reg_d2h)
1401 );
1402 }
1403
1404 /* Manage the timer if it is still running. */
1405 if (tmf->timeout_timer) {
Dan Williams7c40a802011-03-02 11:49:26 -08001406 isci_del_timer(isci_host, tmf->timeout_timer);
Dan Williams6f231dd2011-07-02 22:56:22 -07001407 tmf->timeout_timer = NULL;
1408 }
1409
1410 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1411 tmf_complete = tmf->complete;
1412
1413 scic_controller_complete_task(
1414 isci_host->core_controller,
Dan Williams3a97eec2011-03-04 11:51:43 -08001415 to_sci_dev(isci_device),
Dan Williams6f231dd2011-07-02 22:56:22 -07001416 request->sci_request_handle
1417 );
1418 /* NULL the request handle to make sure it cannot be terminated
1419 * or completed again.
1420 */
1421 request->sci_request_handle = NULL;
1422
1423 isci_request_change_state(request, unallocated);
1424 list_del_init(&request->dev_node);
1425
1426 /* The task management part completes last. */
1427 complete(tmf_complete);
1428}
1429
1430
1431/**
1432 * isci_task_ssp_request_get_lun() - This function is called by the sci core to
1433 * retrieve the lun for a given task request.
1434 * @request: This parameter is the isci_request object.
1435 *
1436 * lun for specified task request.
1437 */
1438u32 isci_task_ssp_request_get_lun(struct isci_request *request)
1439{
1440 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1441
1442 dev_dbg(&request->isci_host->pdev->dev,
1443 "%s: lun = %d\n", __func__, isci_tmf->lun[0]);
1444/* @todo: build lun from array of bytes to 32 bit */
1445 return isci_tmf->lun[0];
1446}
1447
1448/**
1449 * isci_task_ssp_request_get_function() - This function is called by the sci
1450 * core to retrieve the function for a given task request.
1451 * @request: This parameter is the isci_request object.
1452 *
1453 * function code for specified task request.
1454 */
1455u8 isci_task_ssp_request_get_function(struct isci_request *request)
1456{
1457 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1458
1459 dev_dbg(&request->isci_host->pdev->dev,
1460 "%s: func = %d\n", __func__, isci_tmf->tmf_code);
1461
1462 return isci_tmf->tmf_code;
1463}
1464
1465/**
1466 * isci_task_ssp_request_get_io_tag_to_manage() - This function is called by
1467 * the sci core to retrieve the io tag for a given task request.
1468 * @request: This parameter is the isci_request object.
1469 *
1470 * io tag for specified task request.
1471 */
1472u16 isci_task_ssp_request_get_io_tag_to_manage(struct isci_request *request)
1473{
1474 u16 io_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1475
1476 if (tmf_task == request->ttype) {
1477 struct isci_tmf *tmf = isci_request_access_tmf(request);
1478 io_tag = tmf->io_tag;
1479 }
1480
1481 dev_dbg(&request->isci_host->pdev->dev,
1482 "%s: request = %p, io_tag = %d\n",
1483 __func__, request, io_tag);
1484
1485 return io_tag;
1486}
1487
1488/**
1489 * isci_task_ssp_request_get_response_data_address() - This function is called
1490 * by the sci core to retrieve the response data address for a given task
1491 * request.
1492 * @request: This parameter is the isci_request object.
1493 *
1494 * response data address for specified task request.
1495 */
1496void *isci_task_ssp_request_get_response_data_address(
1497 struct isci_request *request)
1498{
1499 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1500
1501 return &isci_tmf->resp.resp_iu;
1502}
1503
1504/**
1505 * isci_task_ssp_request_get_response_data_length() - This function is called
1506 * by the sci core to retrieve the response data length for a given task
1507 * request.
1508 * @request: This parameter is the isci_request object.
1509 *
1510 * response data length for specified task request.
1511 */
1512u32 isci_task_ssp_request_get_response_data_length(
1513 struct isci_request *request)
1514{
1515 struct isci_tmf *isci_tmf = isci_request_access_tmf(request);
1516
1517 return sizeof(isci_tmf->resp.resp_iu);
1518}
1519
1520/**
1521 * isci_bus_reset_handler() - This function performs a target reset of the
1522 * device referenced by "cmd'. This function is exported through the
1523 * "struct scsi_host_template" structure such that it is called when an I/O
1524 * recovery process has escalated to a target reset. Note that this function
1525 * is called from the scsi error handler event thread, so may block on calls.
1526 * @scsi_cmd: This parameter specifies the target to be reset.
1527 *
1528 * SUCCESS if the reset process was successful, else FAILED.
1529 */
1530int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1531{
1532 unsigned long flags = 0;
1533 struct isci_host *isci_host = NULL;
1534 enum sci_status status;
1535 int base_status;
1536 struct isci_remote_device *isci_dev
1537 = isci_dev_from_domain_dev(
1538 sdev_to_domain_dev(cmd->device));
1539
1540 dev_dbg(&cmd->device->sdev_gendev,
1541 "%s: cmd %p, isci_dev %p\n",
1542 __func__, cmd, isci_dev);
1543
1544 if (!isci_dev) {
1545 dev_warn(&cmd->device->sdev_gendev,
1546 "%s: isci_dev is GONE!\n",
1547 __func__);
1548
1549 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1550 }
1551
1552 if (isci_dev->isci_port != NULL)
1553 isci_host = isci_dev->isci_port->isci_host;
1554
1555 if (isci_host != NULL)
1556 spin_lock_irqsave(&isci_host->scic_lock, flags);
1557
Dan Williams3a97eec2011-03-04 11:51:43 -08001558 status = scic_remote_device_reset(to_sci_dev(isci_dev));
Dan Williams6f231dd2011-07-02 22:56:22 -07001559 if (status != SCI_SUCCESS) {
1560
1561 if (isci_host != NULL)
1562 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1563
1564 scmd_printk(KERN_WARNING, cmd,
1565 "%s: scic_remote_device_reset(%p) returned %d!\n",
1566 __func__, isci_dev, status);
1567
1568 return TMF_RESP_FUNC_FAILED;
1569 }
1570 if (isci_host != NULL)
1571 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1572
Dan Williams6f231dd2011-07-02 22:56:22 -07001573 /* Make sure all pending requests are able to be fully terminated. */
1574 isci_device_clear_reset_pending(isci_dev);
1575
1576 /* Terminate in-progress I/O now. */
1577 isci_remote_device_nuke_requests(isci_dev);
1578
1579 /* Call into the libsas default handler (which calls sas_phy_reset). */
1580 base_status = sas_eh_bus_reset_handler(cmd);
1581
1582 if (base_status != SUCCESS) {
1583
1584 /* There can be cases where the resets to individual devices
1585 * behind an expander will fail because of an unplug of the
1586 * expander itself.
1587 */
1588 scmd_printk(KERN_WARNING, cmd,
1589 "%s: sas_eh_bus_reset_handler(%p) returned %d!\n",
1590 __func__, cmd, base_status);
1591 }
1592
1593 /* WHAT TO DO HERE IF sas_phy_reset FAILS? */
1594
1595 if (isci_host != NULL)
1596 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams3a97eec2011-03-04 11:51:43 -08001597 status = scic_remote_device_reset_complete(to_sci_dev(isci_dev));
Dan Williams6f231dd2011-07-02 22:56:22 -07001598
1599 if (isci_host != NULL)
1600 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1601
1602 if (status != SCI_SUCCESS) {
1603 scmd_printk(KERN_WARNING, cmd,
1604 "%s: scic_remote_device_reset_complete(%p) "
1605 "returned %d!\n",
1606 __func__, isci_dev, status);
1607 }
1608 /* WHAT TO DO HERE IF scic_remote_device_reset_complete FAILS? */
1609
1610 dev_dbg(&cmd->device->sdev_gendev,
1611 "%s: cmd %p, isci_dev %p complete.\n",
1612 __func__, cmd, isci_dev);
1613
Dan Williams6f231dd2011-07-02 22:56:22 -07001614 return TMF_RESP_FUNC_COMPLETE;
1615}