blob: 01032dc2c116f0b46c875a8ba951fe6b1137c979 [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>
Dan Williams50e7f9b2011-03-09 21:27:46 -080057#include <linux/irqflags.h>
Dave Jiangaf5ae892011-05-04 17:53:24 -070058#include "sas.h"
Jeff Skirvin61aaff42011-06-21 12:16:33 -070059#include <scsi/libsas.h>
Dan Williams88f3b622011-04-22 19:18:03 -070060#include "remote_device.h"
61#include "remote_node_context.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070062#include "isci.h"
63#include "request.h"
64#include "sata.h"
65#include "task.h"
Dave Jiangaf5ae892011-05-04 17:53:24 -070066
Dan Williams50e7f9b2011-03-09 21:27:46 -080067/**
Dan Williams1077a572011-03-11 10:13:51 -080068* isci_task_refuse() - complete the request to the upper layer driver in
69* the case where an I/O needs to be completed back in the submit path.
70* @ihost: host on which the the request was queued
71* @task: request to complete
72* @response: response code for the completed task.
73* @status: status code for the completed task.
Dan Williams50e7f9b2011-03-09 21:27:46 -080074*
Dan Williams50e7f9b2011-03-09 21:27:46 -080075*/
Dan Williams1077a572011-03-11 10:13:51 -080076static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
Dan Williams50e7f9b2011-03-09 21:27:46 -080079
Dan Williams1077a572011-03-11 10:13:51 -080080{
81 enum isci_completion_selection disposition;
82
83 disposition = isci_perform_normal_io_completion;
84 disposition = isci_task_set_completion_status(task, response, status,
85 disposition);
Dan Williams50e7f9b2011-03-09 21:27:46 -080086
87 /* Tasks aborted specifically by a call to the lldd_abort_task
Dan Williams1077a572011-03-11 10:13:51 -080088 * function should not be completed to the host in the regular path.
89 */
90 switch (disposition) {
Dan Williams50e7f9b2011-03-09 21:27:46 -080091 case isci_perform_normal_io_completion:
92 /* Normal notification (task_done) */
Dan Williams1077a572011-03-11 10:13:51 -080093 dev_dbg(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -070094 "%s: Normal - task = %p, response=%d, "
95 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -080096 __func__, task, response, status);
97
Dan Williams1077a572011-03-11 10:13:51 -080098 task->lldd_task = NULL;
Dan Williams50e7f9b2011-03-09 21:27:46 -080099
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700100 isci_execpath_callback(ihost, task, task->task_done);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800101 break;
102
103 case isci_perform_aborted_io_completion:
104 /* No notification because this request is already in the
105 * abort path.
106 */
Dan Williams1077a572011-03-11 10:13:51 -0800107 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700108 "%s: Aborted - task = %p, response=%d, "
109 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800110 __func__, task, response, status);
111 break;
112
113 case isci_perform_error_io_completion:
114 /* Use sas_task_abort */
Dan Williams1077a572011-03-11 10:13:51 -0800115 dev_warn(&ihost->pdev->dev,
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700116 "%s: Error - task = %p, response=%d, "
117 "status=%d\n",
Dan Williams50e7f9b2011-03-09 21:27:46 -0800118 __func__, task, response, status);
Jeff Skirvined8a72d2011-03-31 13:10:40 -0700119
120 isci_execpath_callback(ihost, task, sas_task_abort);
Dan Williams50e7f9b2011-03-09 21:27:46 -0800121 break;
122
123 default:
Dan Williams1077a572011-03-11 10:13:51 -0800124 dev_warn(&ihost->pdev->dev,
Dan Williams50e7f9b2011-03-09 21:27:46 -0800125 "%s: isci task notification default case!",
126 __func__);
127 sas_task_abort(task);
128 break;
129 }
130}
Dan Williams6f231dd2011-07-02 22:56:22 -0700131
Dan Williams1077a572011-03-11 10:13:51 -0800132#define for_each_sas_task(num, task) \
133 for (; num > 0; num--,\
134 task = list_entry(task->list.next, struct sas_task, list))
135
Dan Williams6f231dd2011-07-02 22:56:22 -0700136/**
137 * isci_task_execute_task() - This function is one of the SAS Domain Template
138 * functions. This function is called by libsas to send a task down to
139 * hardware.
140 * @task: This parameter specifies the SAS task to send.
141 * @num: This parameter specifies the number of tasks to queue.
142 * @gfp_flags: This parameter specifies the context of this call.
143 *
144 * status, zero indicates success.
145 */
146int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
147{
Dan Williams4393aa42011-03-31 13:10:44 -0700148 struct isci_host *ihost = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -0700149 struct isci_request *request = NULL;
150 struct isci_remote_device *device;
151 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700152 int ret;
153 enum sci_status status;
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700154 enum isci_status device_status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700155
Dan Williams1077a572011-03-11 10:13:51 -0800156 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700157
158 /* Check if we have room for more tasks */
Dan Williams1077a572011-03-11 10:13:51 -0800159 ret = isci_host_can_queue(ihost, num);
Dan Williams6f231dd2011-07-02 22:56:22 -0700160
161 if (ret) {
Dan Williams1077a572011-03-11 10:13:51 -0800162 dev_warn(&ihost->pdev->dev, "%s: queue full\n", __func__);
Dan Williams6f231dd2011-07-02 22:56:22 -0700163 return ret;
164 }
165
Dan Williams1077a572011-03-11 10:13:51 -0800166 for_each_sas_task(num, task) {
167 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700168 "task = %p, num = %d; dev = %p; cmd = %p\n",
169 task, num, task->dev, task->uldd_task);
170
Dan Williams4393aa42011-03-31 13:10:44 -0700171 device = task->dev->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700173 if (device)
174 device_status = device->status;
175 else
176 device_status = isci_freed;
177
178 /* From this point onward, any process that needs to guarantee
179 * that there is no kernel I/O being started will have to wait
180 * for the quiesce spinlock.
181 */
182
183 if (device_status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700184
185 /* Forces a retry from scsi mid layer. */
Dave Jiangd2d61432011-04-21 05:36:23 +0000186 dev_dbg(&ihost->pdev->dev,
187 "%s: task %p: isci_host->status = %d, "
188 "device = %p; device_status = 0x%x\n\n",
189 __func__,
190 task,
191 isci_host_get_state(ihost),
192 device,
193 device_status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700194
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700195 if (device_status == isci_ready) {
196 /* Indicate QUEUE_FULL so that the scsi midlayer
197 * retries.
198 */
Dan Williams1077a572011-03-11 10:13:51 -0800199 isci_task_refuse(ihost, task,
200 SAS_TASK_COMPLETE,
201 SAS_QUEUE_FULL);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700202 } else {
203 /* Else, the device is going down. */
Dan Williams1077a572011-03-11 10:13:51 -0800204 isci_task_refuse(ihost, task,
205 SAS_TASK_UNDELIVERED,
206 SAS_DEVICE_UNKNOWN);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700207 }
Dan Williams1077a572011-03-11 10:13:51 -0800208 isci_host_can_dequeue(ihost, 1);
Dan Williams6f231dd2011-07-02 22:56:22 -0700209 } else {
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700210 /* There is a device and it's ready for I/O. */
211 spin_lock_irqsave(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700213 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
214
215 spin_unlock_irqrestore(&task->task_state_lock,
216 flags);
217
Dan Williams1077a572011-03-11 10:13:51 -0800218 isci_task_refuse(ihost, task,
219 SAS_TASK_UNDELIVERED,
220 SAM_STAT_TASK_ABORTED);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700221
222 /* The I/O was aborted. */
223
224 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700225 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
226 spin_unlock_irqrestore(&task->task_state_lock, flags);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700227
228 /* build and send the request. */
Dan Williams1077a572011-03-11 10:13:51 -0800229 status = isci_request_execute(ihost, task, &request,
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700230 gfp_flags);
231
232 if (status != SCI_SUCCESS) {
233
234 spin_lock_irqsave(&task->task_state_lock, flags);
235 /* Did not really start this command. */
236 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
237 spin_unlock_irqrestore(&task->task_state_lock, flags);
238
239 /* Indicate QUEUE_FULL so that the scsi
240 * midlayer retries. if the request
241 * failed for remote device reasons,
242 * it gets returned as
243 * SAS_TASK_UNDELIVERED next time
244 * through.
245 */
Dan Williams1077a572011-03-11 10:13:51 -0800246 isci_task_refuse(ihost, task,
247 SAS_TASK_COMPLETE,
248 SAS_QUEUE_FULL);
249 isci_host_can_dequeue(ihost, 1);
Jeff Skirvinf0846c62011-03-08 19:22:07 -0700250 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700251 }
252 }
Dan Williams1077a572011-03-11 10:13:51 -0800253 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700254 return 0;
255}
256
257
258
259/**
260 * isci_task_request_build() - This function builds the task request object.
261 * @isci_host: This parameter specifies the ISCI host object
262 * @request: This parameter points to the isci_request object allocated in the
263 * request construct function.
264 * @tmf: This parameter is the task management struct to be built
265 *
266 * SCI_SUCCESS on successfull completion, or specific failure code.
267 */
268static enum sci_status isci_task_request_build(
269 struct isci_host *isci_host,
270 struct isci_request **isci_request,
271 struct isci_tmf *isci_tmf)
272{
273 struct scic_sds_remote_device *sci_device;
274 enum sci_status status = SCI_FAILURE;
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700275 struct isci_request *request = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -0700276 struct isci_remote_device *isci_device;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700277 struct domain_device *dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700278
279 dev_dbg(&isci_host->pdev->dev,
280 "%s: isci_tmf = %p\n", __func__, isci_tmf);
281
282 isci_device = isci_tmf->device;
Dan Williams57f20f42011-04-21 18:14:45 -0700283 sci_device = &isci_device->sci;
Dan Williamsa1a113b2011-04-21 18:44:45 -0700284 dev = isci_device->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700285
286 /* do common allocation and init of request object. */
287 status = isci_request_alloc_tmf(
288 isci_host,
289 isci_tmf,
290 &request,
291 isci_device,
292 GFP_ATOMIC
293 );
294
295 if (status != SCI_SUCCESS)
296 goto out;
297
298 /* let the core do it's construct. */
Dan Williams0d843662011-05-08 01:56:57 -0700299 status = scic_task_request_construct(&isci_host->sci, sci_device,
300 SCI_CONTROLLER_INVALID_IO_TAG,
Dan Williams67ea8382011-05-08 11:47:15 -0700301 &request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700302
303 if (status != SCI_SUCCESS) {
304 dev_warn(&isci_host->pdev->dev,
305 "%s: scic_task_request_construct failed - "
306 "status = 0x%x\n",
307 __func__,
308 status);
309 goto errout;
310 }
311
Dan Williamsa1a113b2011-04-21 18:44:45 -0700312 /* XXX convert to get this from task->tproto like other drivers */
313 if (dev->dev_type == SAS_END_DEV) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700314 isci_tmf->proto = SAS_PROTOCOL_SSP;
Dan Williams67ea8382011-05-08 11:47:15 -0700315 status = scic_task_request_construct_ssp(&request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700316 if (status != SCI_SUCCESS)
317 goto errout;
318 }
319
Dan Williamsa1a113b2011-04-21 18:44:45 -0700320 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700321 isci_tmf->proto = SAS_PROTOCOL_SATA;
322 status = isci_sata_management_task_request_build(request);
323
324 if (status != SCI_SUCCESS)
325 goto errout;
326 }
327
328 goto out;
329
330 errout:
331
332 /* release the dma memory if we fail. */
333 isci_request_free(isci_host, request);
334 request = NULL;
335
336 out:
337 *isci_request = request;
338 return status;
339}
340
341/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700342 * isci_task_execute_tmf() - This function builds and sends a task request,
343 * then waits for the completion.
344 * @isci_host: This parameter specifies the ISCI host object
345 * @tmf: This parameter is the pointer to the task management structure for
346 * this request.
347 * @timeout_ms: This parameter specifies the timeout period for the task
348 * management request.
349 *
350 * TMF_RESP_FUNC_COMPLETE on successful completion of the TMF (this includes
351 * error conditions reported in the IU status), or TMF_RESP_FUNC_FAILED.
352 */
353int isci_task_execute_tmf(
354 struct isci_host *isci_host,
355 struct isci_tmf *tmf,
356 unsigned long timeout_ms)
357{
358 DECLARE_COMPLETION_ONSTACK(completion);
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700359 enum sci_task_status status = SCI_TASK_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700360 struct scic_sds_remote_device *sci_device;
361 struct isci_remote_device *isci_device = tmf->device;
362 struct isci_request *request;
363 int ret = TMF_RESP_FUNC_FAILED;
364 unsigned long flags;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000365 unsigned long timeleft;
Dan Williams6f231dd2011-07-02 22:56:22 -0700366
367 /* sanity check, return TMF_RESP_FUNC_FAILED
368 * if the device is not there and ready.
369 */
Dan Williams8acaec12011-03-07 14:47:35 -0800370 if (!isci_device || isci_device->status != isci_ready_for_io) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700371 dev_dbg(&isci_host->pdev->dev,
372 "%s: isci_device = %p not ready (%d)\n",
373 __func__,
Dan Williams8acaec12011-03-07 14:47:35 -0800374 isci_device, isci_device->status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700375 return TMF_RESP_FUNC_FAILED;
376 } else
377 dev_dbg(&isci_host->pdev->dev,
378 "%s: isci_device = %p\n",
379 __func__, isci_device);
380
Dan Williams57f20f42011-04-21 18:14:45 -0700381 sci_device = &isci_device->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -0700382
383 /* Assign the pointer to the TMF's completion kernel wait structure. */
384 tmf->complete = &completion;
385
386 isci_task_request_build(
387 isci_host,
388 &request,
389 tmf
390 );
391
392 if (!request) {
393 dev_warn(&isci_host->pdev->dev,
394 "%s: isci_task_request_build failed\n",
395 __func__);
396 return TMF_RESP_FUNC_FAILED;
397 }
398
Dan Williams6f231dd2011-07-02 22:56:22 -0700399 spin_lock_irqsave(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700400
401 /* start the TMF io. */
402 status = scic_controller_start_task(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000403 &isci_host->sci,
Dan Williams6f231dd2011-07-02 22:56:22 -0700404 sci_device,
Dan Williams67ea8382011-05-08 11:47:15 -0700405 &request->sci,
406 SCI_CONTROLLER_INVALID_IO_TAG);
Dan Williams6f231dd2011-07-02 22:56:22 -0700407
Bartosz Barcinski467e8552011-04-12 17:28:41 -0700408 if (status != SCI_TASK_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700409 dev_warn(&isci_host->pdev->dev,
410 "%s: start_io failed - status = 0x%x, request = %p\n",
411 __func__,
412 status,
413 request);
Edmund Nadolskifd183882011-05-19 12:00:15 +0000414 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700415 goto cleanup_request;
416 }
417
Dan Williams6f231dd2011-07-02 22:56:22 -0700418 if (tmf->cb_state_func != NULL)
419 tmf->cb_state_func(isci_tmf_started, tmf, tmf->cb_data);
420
Dan Williams6f231dd2011-07-02 22:56:22 -0700421 isci_request_change_state(request, started);
422
423 /* add the request to the remote device request list. */
424 list_add(&request->dev_node, &isci_device->reqs_in_process);
425
426 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
427
428 /* Wait for the TMF to complete, or a timeout. */
Edmund Nadolskifd183882011-05-19 12:00:15 +0000429 timeleft = wait_for_completion_timeout(&completion,
430 jiffies + msecs_to_jiffies(timeout_ms));
431
432 if (timeleft == 0) {
433 spin_lock_irqsave(&isci_host->scic_lock, flags);
434
435 if (tmf->cb_state_func != NULL)
436 tmf->cb_state_func(isci_tmf_timed_out, tmf, tmf->cb_data);
437
438 status = scic_controller_terminate_request(
439 &request->isci_host->sci,
440 &request->isci_device->sci,
441 &request->sci);
442
443 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
444 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700445
446 isci_print_tmf(tmf);
447
448 if (tmf->status == SCI_SUCCESS)
449 ret = TMF_RESP_FUNC_COMPLETE;
450 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
451 dev_dbg(&isci_host->pdev->dev,
452 "%s: tmf.status == "
453 "SCI_FAILURE_IO_RESPONSE_VALID\n",
454 __func__);
455 ret = TMF_RESP_FUNC_COMPLETE;
456 }
457 /* Else - leave the default "failed" status alone. */
458
459 dev_dbg(&isci_host->pdev->dev,
460 "%s: completed request = %p\n",
461 __func__,
462 request);
463
464 if (request->io_request_completion != NULL) {
Edmund Nadolskifd183882011-05-19 12:00:15 +0000465 /* A thread is waiting for this TMF to finish. */
Dan Williams6f231dd2011-07-02 22:56:22 -0700466 complete(request->io_request_completion);
467 }
468
Dan Williams6f231dd2011-07-02 22:56:22 -0700469 cleanup_request:
Dan Williams6f231dd2011-07-02 22:56:22 -0700470 isci_request_free(isci_host, request);
Dan Williams6f231dd2011-07-02 22:56:22 -0700471 return ret;
472}
473
474void isci_task_build_tmf(
475 struct isci_tmf *tmf,
476 struct isci_remote_device *isci_device,
477 enum isci_tmf_function_codes code,
478 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
479 struct isci_tmf *,
480 void *),
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800481 void *cb_data)
Dan Williams6f231dd2011-07-02 22:56:22 -0700482{
483 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
484 "%s: isci_device = %p\n", __func__, isci_device);
485
486 memset(tmf, 0, sizeof(*tmf));
487
488 tmf->device = isci_device;
489 tmf->tmf_code = code;
Edmund Nadolskifd183882011-05-19 12:00:15 +0000490
Dan Williams6f231dd2011-07-02 22:56:22 -0700491 tmf->cb_state_func = tmf_sent_cb;
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800492 tmf->cb_data = cb_data;
493}
Jeff Skirvin1fad9e92011-03-04 14:06:46 -0800494
Dan Williams35173d52011-03-26 16:43:01 -0700495static void isci_task_build_abort_task_tmf(
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -0800496 struct isci_tmf *tmf,
497 struct isci_remote_device *isci_device,
498 enum isci_tmf_function_codes code,
499 void (*tmf_sent_cb)(enum isci_tmf_cb_state,
500 struct isci_tmf *,
501 void *),
502 struct isci_request *old_request)
503{
504 isci_task_build_tmf(tmf, isci_device, code, tmf_sent_cb,
505 (void *)old_request);
506 tmf->io_tag = old_request->io_tag;
Dan Williams6f231dd2011-07-02 22:56:22 -0700507}
508
509static struct isci_request *isci_task_get_request_from_task(
510 struct sas_task *task,
Dan Williams6f231dd2011-07-02 22:56:22 -0700511 struct isci_remote_device **isci_device)
512{
513
514 struct isci_request *request = NULL;
515 unsigned long flags;
516
517 spin_lock_irqsave(&task->task_state_lock, flags);
518
519 request = task->lldd_task;
520
521 /* If task is already done, the request isn't valid */
522 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
523 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
524 (request != NULL)) {
525
Dan Williams6f231dd2011-07-02 22:56:22 -0700526 if (isci_device != NULL)
527 *isci_device = request->isci_device;
528 }
529
530 spin_unlock_irqrestore(&task->task_state_lock, flags);
531
532 return request;
533}
534
535/**
536 * isci_task_validate_request_to_abort() - This function checks the given I/O
537 * against the "started" state. If the request is still "started", it's
538 * state is changed to aborted. NOTE: isci_host->scic_lock MUST BE HELD
539 * BEFORE CALLING THIS FUNCTION.
540 * @isci_request: This parameter specifies the request object to control.
541 * @isci_host: This parameter specifies the ISCI host object
542 * @isci_device: This is the device to which the request is pending.
543 * @aborted_io_completion: This is a completion structure that will be added to
544 * the request in case it is changed to aborting; this completion is
545 * triggered when the request is fully completed.
546 *
547 * Either "started" on successful change of the task status to "aborted", or
548 * "unallocated" if the task cannot be controlled.
549 */
550static enum isci_request_status isci_task_validate_request_to_abort(
551 struct isci_request *isci_request,
552 struct isci_host *isci_host,
553 struct isci_remote_device *isci_device,
554 struct completion *aborted_io_completion)
555{
556 enum isci_request_status old_state = unallocated;
557
558 /* Only abort the task if it's in the
559 * device's request_in_process list
560 */
561 if (isci_request && !list_empty(&isci_request->dev_node)) {
562 old_state = isci_request_change_started_to_aborted(
563 isci_request, aborted_io_completion);
564
Dan Williams6f231dd2011-07-02 22:56:22 -0700565 }
566
567 return old_state;
568}
569
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700570/**
571* isci_request_cleanup_completed_loiterer() - This function will take care of
572* the final cleanup on any request which has been explicitly terminated.
573* @isci_host: This parameter specifies the ISCI host object
574* @isci_device: This is the device to which the request is pending.
575* @isci_request: This parameter specifies the terminated request object.
576* @task: This parameter is the libsas I/O request.
577*/
Dan Williams6f231dd2011-07-02 22:56:22 -0700578static void isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700579 struct isci_host *isci_host,
Dan Williams6f231dd2011-07-02 22:56:22 -0700580 struct isci_remote_device *isci_device,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700581 struct isci_request *isci_request,
582 struct sas_task *task)
Dan Williams6f231dd2011-07-02 22:56:22 -0700583{
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700584 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700585
586 dev_dbg(&isci_host->pdev->dev,
587 "%s: isci_device=%p, request=%p, task=%p\n",
Jeff Skirvin18d3d722011-03-04 14:06:38 -0800588 __func__, isci_device, isci_request, task);
Dan Williams6f231dd2011-07-02 22:56:22 -0700589
Jeff Skirvina5fde222011-03-04 14:06:42 -0800590 if (task != NULL) {
591
592 spin_lock_irqsave(&task->task_state_lock, flags);
593 task->lldd_task = NULL;
594
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700595 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
596
Jeff Skirvina5fde222011-03-04 14:06:42 -0800597 isci_set_task_doneflags(task);
598
599 /* If this task is not in the abort path, call task_done. */
600 if (!(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
601
602 spin_unlock_irqrestore(&task->task_state_lock, flags);
603 task->task_done(task);
604 } else
605 spin_unlock_irqrestore(&task->task_state_lock, flags);
606 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800607
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700608 if (isci_request != NULL) {
609 spin_lock_irqsave(&isci_host->scic_lock, flags);
610 list_del_init(&isci_request->dev_node);
611 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800612
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700613 isci_request_free(isci_host, isci_request);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800614 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800615}
616
Dan Williams6f231dd2011-07-02 22:56:22 -0700617/**
618 * isci_terminate_request_core() - This function will terminate the given
619 * request, and wait for it to complete. This function must only be called
620 * from a thread that can wait. Note that the request is terminated and
621 * completed (back to the host, if started there).
622 * @isci_host: This SCU.
623 * @isci_device: The target.
624 * @isci_request: The I/O request to be terminated.
625 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700626 */
627static void isci_terminate_request_core(
628 struct isci_host *isci_host,
629 struct isci_remote_device *isci_device,
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800630 struct isci_request *isci_request)
Dan Williams6f231dd2011-07-02 22:56:22 -0700631{
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800632 enum sci_status status = SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700633 bool was_terminated = false;
634 bool needs_cleanup_handling = false;
635 enum isci_request_status request_status;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700636 unsigned long flags;
637 unsigned long termination_completed = 1;
638 struct completion *io_request_completion;
639 struct sas_task *task;
Dan Williams6f231dd2011-07-02 22:56:22 -0700640
641 dev_dbg(&isci_host->pdev->dev,
642 "%s: device = %p; request = %p\n",
643 __func__, isci_device, isci_request);
644
Dan Williams6f231dd2011-07-02 22:56:22 -0700645 spin_lock_irqsave(&isci_host->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800646
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700647 io_request_completion = isci_request->io_request_completion;
648
649 task = (isci_request->ttype == io_task)
650 ? isci_request_access_task(isci_request)
651 : NULL;
652
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800653 /* Note that we are not going to control
654 * the target to abort the request.
655 */
656 isci_request->complete_in_target = true;
657
Dan Williams6f231dd2011-07-02 22:56:22 -0700658 /* Make sure the request wasn't just sitting around signalling
659 * device condition (if the request handle is NULL, then the
660 * request completed but needed additional handling here).
661 */
Dan Williams67ea8382011-05-08 11:47:15 -0700662 if (!isci_request->terminated) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700663 was_terminated = true;
Jeff Skirvincbb65c62011-03-04 14:06:50 -0800664 needs_cleanup_handling = true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700665 status = scic_controller_terminate_request(
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000666 &isci_host->sci,
Dan Williams57f20f42011-04-21 18:14:45 -0700667 &isci_device->sci,
Dan Williams67ea8382011-05-08 11:47:15 -0700668 &isci_request->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -0700669 }
670 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
671
672 /*
673 * The only time the request to terminate will
674 * fail is when the io request is completed and
675 * being aborted.
676 */
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800677 if (status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700678 dev_err(&isci_host->pdev->dev,
679 "%s: scic_controller_terminate_request"
680 " returned = 0x%x\n",
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700681 __func__, status);
682
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800683 isci_request->io_request_completion = NULL;
684
685 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -0700686 if (was_terminated) {
687 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700688 "%s: before completion wait (%p/%p)\n",
689 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700690
691 /* Wait here for the request to complete. */
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700692 #define TERMINATION_TIMEOUT_MSEC 500
693 termination_completed
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800694 = wait_for_completion_timeout(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700695 io_request_completion,
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800696 msecs_to_jiffies(TERMINATION_TIMEOUT_MSEC));
Dan Williams6f231dd2011-07-02 22:56:22 -0700697
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700698 if (!termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800699
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700700 /* The request to terminate has timed out. */
701 spin_lock_irqsave(&isci_host->scic_lock,
702 flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800703
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700704 /* Check for state changes. */
705 if (!isci_request->terminated) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800706
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700707 /* The best we can do is to have the
708 * request die a silent death if it
709 * ever really completes.
710 *
711 * Set the request state to "dead",
712 * and clear the task pointer so that
713 * an actual completion event callback
714 * doesn't do anything.
715 */
716 isci_request->status = dead;
717 isci_request->io_request_completion
718 = NULL;
719
720 if (isci_request->ttype == io_task) {
721
722 /* Break links with the
723 * sas_task.
724 */
725 isci_request->ttype_ptr.io_task_ptr
726 = NULL;
727 }
728 } else
729 termination_completed = 1;
730
731 spin_unlock_irqrestore(&isci_host->scic_lock,
732 flags);
733
734 if (!termination_completed) {
735
736 dev_err(&isci_host->pdev->dev,
737 "%s: *** Timeout waiting for "
738 "termination(%p/%p)\n",
739 __func__, io_request_completion,
740 isci_request);
741
742 /* The request can no longer be referenced
743 * safely since it may go away if the
744 * termination every really does complete.
745 */
746 isci_request = NULL;
747 }
748 }
749 if (termination_completed)
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800750 dev_dbg(&isci_host->pdev->dev,
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700751 "%s: after completion wait (%p/%p)\n",
752 __func__, isci_request, io_request_completion);
Dan Williams6f231dd2011-07-02 22:56:22 -0700753 }
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800754
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700755 if (termination_completed) {
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800756
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700757 isci_request->io_request_completion = NULL;
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800758
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700759 /* Peek at the status of the request. This will tell
760 * us if there was special handling on the request such that it
761 * needs to be detached and freed here.
762 */
763 spin_lock_irqsave(&isci_request->state_lock, flags);
764 request_status = isci_request_get_state(isci_request);
765
766 if ((isci_request->ttype == io_task) /* TMFs are in their own thread */
767 && ((request_status == aborted)
768 || (request_status == aborting)
769 || (request_status == terminating)
770 || (request_status == completed)
771 || (request_status == dead)
772 )
773 ) {
774
775 /* The completion routine won't free a request in
776 * the aborted/aborting/etc. states, so we do
777 * it here.
778 */
779 needs_cleanup_handling = true;
780 }
781 spin_unlock_irqrestore(&isci_request->state_lock, flags);
782
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800783 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700784 if (needs_cleanup_handling)
785 isci_request_cleanup_completed_loiterer(
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700786 isci_host, isci_device, isci_request, task);
Jeff Skirvina5fde222011-03-04 14:06:42 -0800787 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700788}
789
790/**
791 * isci_terminate_pending_requests() - This function will change the all of the
792 * requests on the given device's state to "aborting", will terminate the
793 * requests, and wait for them to complete. This function must only be
794 * called from a thread that can wait. Note that the requests are all
795 * terminated and completed (back to the host, if started there).
796 * @isci_host: This parameter specifies SCU.
797 * @isci_device: This parameter specifies the target.
798 *
Dan Williams6f231dd2011-07-02 22:56:22 -0700799 */
Dan Williams980d3ae2011-06-20 15:11:22 -0700800void isci_terminate_pending_requests(struct isci_host *ihost,
801 struct isci_remote_device *idev)
Dan Williams6f231dd2011-07-02 22:56:22 -0700802{
Dan Williams980d3ae2011-06-20 15:11:22 -0700803 struct completion request_completion;
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700804 enum isci_request_status old_state;
Dan Williams980d3ae2011-06-20 15:11:22 -0700805 unsigned long flags;
806 LIST_HEAD(list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700807
Dan Williams980d3ae2011-06-20 15:11:22 -0700808 spin_lock_irqsave(&ihost->scic_lock, flags);
809 list_splice_init(&idev->reqs_in_process, &list);
Dan Williams6f231dd2011-07-02 22:56:22 -0700810
Dan Williams980d3ae2011-06-20 15:11:22 -0700811 /* assumes that isci_terminate_request_core deletes from the list */
812 while (!list_empty(&list)) {
813 struct isci_request *ireq = list_entry(list.next, typeof(*ireq), dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -0700814
Dan Williams980d3ae2011-06-20 15:11:22 -0700815 /* Change state to "terminating" if it is currently
816 * "started".
817 */
818 old_state = isci_request_change_started_to_newstate(ireq,
819 &request_completion,
820 terminating);
821 switch (old_state) {
822 case started:
823 case completed:
824 case aborting:
825 break;
826 default:
827 /* termination in progress, or otherwise dispositioned.
828 * We know the request was on 'list' so should be safe
829 * to move it back to reqs_in_process
830 */
831 list_move(&ireq->dev_node, &idev->reqs_in_process);
832 ireq = NULL;
833 break;
834 }
835
836 if (!ireq)
837 continue;
838 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800839
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700840 init_completion(&request_completion);
Jeff Skirvin4dc043c2011-03-04 14:06:52 -0800841
Dan Williams980d3ae2011-06-20 15:11:22 -0700842 dev_dbg(&ihost->pdev->dev,
843 "%s: idev=%p request=%p; task=%p old_state=%d\n",
844 __func__, idev, ireq,
845 ireq->ttype == io_task ? isci_request_access_task(ireq) : NULL,
846 old_state);
Jeff Skirvin77c852f2011-06-20 14:09:16 -0700847
Dan Williams980d3ae2011-06-20 15:11:22 -0700848 /* If the old_state is started:
849 * This request was not already being aborted. If it had been,
850 * then the aborting I/O (ie. the TMF request) would not be in
851 * the aborting state, and thus would be terminated here. Note
852 * that since the TMF completion's call to the kernel function
853 * "complete()" does not happen until the pending I/O request
854 * terminate fully completes, we do not have to implement a
855 * special wait here for already aborting requests - the
856 * termination of the TMF request will force the request
857 * to finish it's already started terminate.
858 *
859 * If old_state == completed:
860 * This request completed from the SCU hardware perspective
861 * and now just needs cleaning up in terms of freeing the
862 * request and potentially calling up to libsas.
863 *
864 * If old_state == aborting:
865 * This request has already gone through a TMF timeout, but may
866 * not have been terminated; needs cleaning up at least.
867 */
868 isci_terminate_request_core(ihost, idev, ireq);
869 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700870 }
Dan Williams980d3ae2011-06-20 15:11:22 -0700871 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700872}
873
874/**
875 * isci_task_send_lu_reset_sas() - This function is called by of the SAS Domain
876 * Template functions.
877 * @lun: This parameter specifies the lun to be reset.
878 *
879 * status, zero indicates success.
880 */
881static int isci_task_send_lu_reset_sas(
882 struct isci_host *isci_host,
883 struct isci_remote_device *isci_device,
884 u8 *lun)
885{
886 struct isci_tmf tmf;
887 int ret = TMF_RESP_FUNC_FAILED;
888
889 dev_dbg(&isci_host->pdev->dev,
890 "%s: isci_host = %p, isci_device = %p\n",
891 __func__, isci_host, isci_device);
892 /* Send the LUN reset to the target. By the time the call returns,
893 * the TMF has fully exected in the target (in which case the return
894 * value is "TMF_RESP_FUNC_COMPLETE", or the request timed-out (or
895 * was otherwise unable to be executed ("TMF_RESP_FUNC_FAILED").
896 */
897 isci_task_build_tmf(&tmf, isci_device, isci_tmf_ssp_lun_reset, NULL,
898 NULL);
899
900 #define ISCI_LU_RESET_TIMEOUT_MS 2000 /* 2 second timeout. */
901 ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
902
903 if (ret == TMF_RESP_FUNC_COMPLETE)
904 dev_dbg(&isci_host->pdev->dev,
905 "%s: %p: TMF_LU_RESET passed\n",
906 __func__, isci_device);
907 else
908 dev_dbg(&isci_host->pdev->dev,
909 "%s: %p: TMF_LU_RESET failed (%x)\n",
910 __func__, isci_device, ret);
911
912 return ret;
913}
914
915/**
916 * isci_task_lu_reset() - This function is one of the SAS Domain Template
917 * functions. This is one of the Task Management functoins called by libsas,
918 * to reset the given lun. Note the assumption that while this call is
919 * executing, no I/O will be sent by the host to the device.
920 * @lun: This parameter specifies the lun to be reset.
921 *
922 * status, zero indicates success.
923 */
Dan Williams4393aa42011-03-31 13:10:44 -0700924int isci_task_lu_reset(struct domain_device *domain_device, u8 *lun)
Dan Williams6f231dd2011-07-02 22:56:22 -0700925{
Dan Williams4393aa42011-03-31 13:10:44 -0700926 struct isci_host *isci_host = dev_to_ihost(domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700927 struct isci_remote_device *isci_device = NULL;
928 int ret;
929 bool device_stopping = false;
930
Dan Williams4393aa42011-03-31 13:10:44 -0700931 isci_device = domain_device->lldd_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700932
Dan Williams4393aa42011-03-31 13:10:44 -0700933 dev_dbg(&isci_host->pdev->dev,
934 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
Dan Williams6f231dd2011-07-02 22:56:22 -0700935 __func__, domain_device, isci_host, isci_device);
936
Dan Williamsd06b4872011-05-02 13:59:25 -0700937 if (isci_device != NULL) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700938 device_stopping = (isci_device->status == isci_stopping)
939 || (isci_device->status == isci_stopped);
Dan Williamsd06b4872011-05-02 13:59:25 -0700940 set_bit(IDEV_EH, &isci_device->flags);
941 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700942
943 /* If there is a device reset pending on any request in the
944 * device's list, fail this LUN reset request in order to
945 * escalate to the device reset.
946 */
Dan Williams4393aa42011-03-31 13:10:44 -0700947 if (!isci_device || device_stopping ||
948 isci_device_is_reset_pending(isci_host, isci_device)) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700949 dev_warn(&isci_host->pdev->dev,
Dan Williams4393aa42011-03-31 13:10:44 -0700950 "%s: No dev (%p), or "
Dan Williams6f231dd2011-07-02 22:56:22 -0700951 "RESET PENDING: domain_device=%p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700952 __func__, isci_device, domain_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700953 return TMF_RESP_FUNC_FAILED;
954 }
955
Dan Williams6f231dd2011-07-02 22:56:22 -0700956 /* Send the task management part of the reset. */
957 if (sas_protocol_ata(domain_device->tproto)) {
Dan Williams4393aa42011-03-31 13:10:44 -0700958 ret = isci_task_send_lu_reset_sata(isci_host, isci_device, lun);
Dan Williams6f231dd2011-07-02 22:56:22 -0700959 } else
960 ret = isci_task_send_lu_reset_sas(isci_host, isci_device, lun);
961
962 /* If the LUN reset worked, all the I/O can now be terminated. */
963 if (ret == TMF_RESP_FUNC_COMPLETE)
964 /* Terminate all I/O now. */
965 isci_terminate_pending_requests(isci_host,
Dan Williams980d3ae2011-06-20 15:11:22 -0700966 isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -0700967
Dan Williams6f231dd2011-07-02 22:56:22 -0700968 return ret;
969}
970
971
972/* int (*lldd_clear_nexus_port)(struct asd_sas_port *); */
973int isci_task_clear_nexus_port(struct asd_sas_port *port)
974{
975 return TMF_RESP_FUNC_FAILED;
976}
977
978
979
980int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
981{
982 return TMF_RESP_FUNC_FAILED;
983}
984
Dan Williams6f231dd2011-07-02 22:56:22 -0700985/* Task Management Functions. Must be called from process context. */
986
987/**
988 * isci_abort_task_process_cb() - This is a helper function for the abort task
989 * TMF command. It manages the request state with respect to the successful
990 * transmission / completion of the abort task request.
991 * @cb_state: This parameter specifies when this function was called - after
992 * the TMF request has been started and after it has timed-out.
993 * @tmf: This parameter specifies the TMF in progress.
994 *
995 *
996 */
997static void isci_abort_task_process_cb(
998 enum isci_tmf_cb_state cb_state,
999 struct isci_tmf *tmf,
1000 void *cb_data)
1001{
1002 struct isci_request *old_request;
1003
1004 old_request = (struct isci_request *)cb_data;
1005
1006 dev_dbg(&old_request->isci_host->pdev->dev,
1007 "%s: tmf=%p, old_request=%p\n",
1008 __func__, tmf, old_request);
1009
1010 switch (cb_state) {
1011
1012 case isci_tmf_started:
1013 /* The TMF has been started. Nothing to do here, since the
1014 * request state was already set to "aborted" by the abort
1015 * task function.
1016 */
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -07001017 if ((old_request->status != aborted)
1018 && (old_request->status != completed))
1019 dev_err(&old_request->isci_host->pdev->dev,
1020 "%s: Bad request status (%d): tmf=%p, old_request=%p\n",
1021 __func__, old_request->status, tmf, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001022 break;
1023
1024 case isci_tmf_timed_out:
1025
1026 /* Set the task's state to "aborting", since the abort task
1027 * function thread set it to "aborted" (above) in anticipation
1028 * of the task management request working correctly. Since the
1029 * timeout has now fired, the TMF request failed. We set the
1030 * state such that the request completion will indicate the
1031 * device is no longer present.
1032 */
1033 isci_request_change_state(old_request, aborting);
1034 break;
1035
1036 default:
1037 dev_err(&old_request->isci_host->pdev->dev,
1038 "%s: Bad cb_state (%d): tmf=%p, old_request=%p\n",
1039 __func__, cb_state, tmf, old_request);
1040 break;
1041 }
1042}
1043
1044/**
1045 * isci_task_abort_task() - This function is one of the SAS Domain Template
1046 * functions. This function is called by libsas to abort a specified task.
1047 * @task: This parameter specifies the SAS task to abort.
1048 *
1049 * status, zero indicates success.
1050 */
1051int isci_task_abort_task(struct sas_task *task)
1052{
Dan Williams4393aa42011-03-31 13:10:44 -07001053 struct isci_host *isci_host = dev_to_ihost(task->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001054 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001055 struct isci_request *old_request = NULL;
1056 enum isci_request_status old_state;
Dan Williams6f231dd2011-07-02 22:56:22 -07001057 struct isci_remote_device *isci_device = NULL;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001058 struct isci_tmf tmf;
1059 int ret = TMF_RESP_FUNC_FAILED;
1060 unsigned long flags;
1061 bool any_dev_reset = false;
1062 bool device_stopping;
Dan Williams6f231dd2011-07-02 22:56:22 -07001063
1064 /* Get the isci_request reference from the task. Note that
1065 * this check does not depend on the pending request list
1066 * in the device, because tasks driving resets may land here
1067 * after completion in the core.
1068 */
Dan Williams4393aa42011-03-31 13:10:44 -07001069 old_request = isci_task_get_request_from_task(task, &isci_device);
Dan Williams6f231dd2011-07-02 22:56:22 -07001070
1071 dev_dbg(&isci_host->pdev->dev,
1072 "%s: task = %p\n", __func__, task);
1073
1074 /* Check if the device has been / is currently being removed.
1075 * If so, no task management will be done, and the I/O will
1076 * be terminated.
1077 */
1078 device_stopping = (isci_device->status == isci_stopping)
1079 || (isci_device->status == isci_stopped);
1080
Dan Williamsd06b4872011-05-02 13:59:25 -07001081 /* XXX need to fix device lookup lifetime (needs to be done
1082 * under scic_lock, among other things...), but for now assume
1083 * the device is available like the above code
1084 */
1085 set_bit(IDEV_EH, &isci_device->flags);
1086
Dan Williams6f231dd2011-07-02 22:56:22 -07001087 /* This version of the driver will fail abort requests for
1088 * SATA/STP. Failing the abort request this way will cause the
1089 * SCSI error handler thread to escalate to LUN reset
1090 */
1091 if (sas_protocol_ata(task->task_proto) && !device_stopping) {
1092 dev_warn(&isci_host->pdev->dev,
1093 " task %p is for a STP/SATA device;"
1094 " returning TMF_RESP_FUNC_FAILED\n"
1095 " to cause a LUN reset...\n", task);
1096 return TMF_RESP_FUNC_FAILED;
1097 }
1098
1099 dev_dbg(&isci_host->pdev->dev,
1100 "%s: old_request == %p\n", __func__, old_request);
1101
Jeff Skirvina5fde222011-03-04 14:06:42 -08001102 if (!device_stopping)
1103 any_dev_reset = isci_device_is_reset_pending(isci_host,isci_device);
1104
Dan Williams6f231dd2011-07-02 22:56:22 -07001105 spin_lock_irqsave(&task->task_state_lock, flags);
1106
1107 /* Don't do resets to stopping devices. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001108 if (device_stopping) {
1109
Dan Williams6f231dd2011-07-02 22:56:22 -07001110 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
Jeff Skirvina5fde222011-03-04 14:06:42 -08001111 any_dev_reset = false;
Dan Williams6f231dd2011-07-02 22:56:22 -07001112
Jeff Skirvina5fde222011-03-04 14:06:42 -08001113 } else /* See if there is a pending device reset for this device. */
Dan Williams6f231dd2011-07-02 22:56:22 -07001114 any_dev_reset = any_dev_reset
Jeff Skirvina5fde222011-03-04 14:06:42 -08001115 || (task->task_state_flags & SAS_TASK_NEED_DEV_RESET);
Dan Williams6f231dd2011-07-02 22:56:22 -07001116
1117 /* If the extraction of the request reference from the task
1118 * failed, then the request has been completed (or if there is a
1119 * pending reset then this abort request function must be failed
1120 * in order to escalate to the target reset).
1121 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001122 if ((old_request == NULL) || any_dev_reset) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001123
1124 /* If the device reset task flag is set, fail the task
1125 * management request. Otherwise, the original request
1126 * has completed.
1127 */
1128 if (any_dev_reset) {
1129
1130 /* Turn off the task's DONE to make sure this
1131 * task is escalated to a target reset.
1132 */
1133 task->task_state_flags &= ~SAS_TASK_STATE_DONE;
1134
Jeff Skirvina5fde222011-03-04 14:06:42 -08001135 /* Make the reset happen as soon as possible. */
1136 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
1137
1138 spin_unlock_irqrestore(&task->task_state_lock, flags);
1139
Dan Williams6f231dd2011-07-02 22:56:22 -07001140 /* Fail the task management request in order to
1141 * escalate to the target reset.
1142 */
1143 ret = TMF_RESP_FUNC_FAILED;
1144
1145 dev_dbg(&isci_host->pdev->dev,
1146 "%s: Failing task abort in order to "
1147 "escalate to target reset because\n"
1148 "SAS_TASK_NEED_DEV_RESET is set for "
1149 "task %p on dev %p\n",
1150 __func__, task, isci_device);
1151
Jeff Skirvina5fde222011-03-04 14:06:42 -08001152
Dan Williams6f231dd2011-07-02 22:56:22 -07001153 } else {
Dan Williams6f231dd2011-07-02 22:56:22 -07001154 /* The request has already completed and there
1155 * is nothing to do here other than to set the task
1156 * done bit, and indicate that the task abort function
1157 * was sucessful.
1158 */
1159 isci_set_task_doneflags(task);
1160
Jeff Skirvina5fde222011-03-04 14:06:42 -08001161 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001162
Jeff Skirvina5fde222011-03-04 14:06:42 -08001163 ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001164
Jeff Skirvina5fde222011-03-04 14:06:42 -08001165 dev_dbg(&isci_host->pdev->dev,
1166 "%s: abort task not needed for %p\n",
1167 __func__, task);
Dan Williams6f231dd2011-07-02 22:56:22 -07001168 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001169
1170 return ret;
1171 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001172 else
1173 spin_unlock_irqrestore(&task->task_state_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001174
1175 spin_lock_irqsave(&isci_host->scic_lock, flags);
1176
Jeff Skirvinf219f012011-03-31 13:10:34 -07001177 /* Check the request status and change to "aborted" if currently
Jeff Skirvina5fde222011-03-04 14:06:42 -08001178 * "starting"; if true then set the I/O kernel completion
Dan Williams6f231dd2011-07-02 22:56:22 -07001179 * struct that will be triggered when the request completes.
1180 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08001181 old_state = isci_task_validate_request_to_abort(
1182 old_request, isci_host, isci_device,
1183 &aborted_io_completion);
Jeff Skirvinf219f012011-03-31 13:10:34 -07001184 if ((old_state != started) &&
1185 (old_state != completed) &&
1186 (old_state != aborting)) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001187
1188 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1189
Jeff Skirvina5fde222011-03-04 14:06:42 -08001190 /* The request was already being handled by someone else (because
1191 * they got to set the state away from started).
1192 */
1193 dev_dbg(&isci_host->pdev->dev,
1194 "%s: device = %p; old_request %p already being aborted\n",
1195 __func__,
1196 isci_device, old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001197
1198 return TMF_RESP_FUNC_COMPLETE;
1199 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001200 if ((task->task_proto == SAS_PROTOCOL_SMP)
1201 || device_stopping
1202 || old_request->complete_in_target
1203 ) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001204
1205 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1206
Jeff Skirvina5fde222011-03-04 14:06:42 -08001207 dev_dbg(&isci_host->pdev->dev,
1208 "%s: SMP request (%d)"
1209 " or device is stopping (%d)"
1210 " or complete_in_target (%d), thus no TMF\n",
1211 __func__, (task->task_proto == SAS_PROTOCOL_SMP),
1212 device_stopping, old_request->complete_in_target);
1213
Dan Williams6f231dd2011-07-02 22:56:22 -07001214 /* Set the state on the task. */
1215 isci_task_all_done(task);
1216
1217 ret = TMF_RESP_FUNC_COMPLETE;
1218
1219 /* Stopping and SMP devices are not sent a TMF, and are not
Jeff Skirvina5fde222011-03-04 14:06:42 -08001220 * reset, but the outstanding I/O request is terminated below.
Dan Williams6f231dd2011-07-02 22:56:22 -07001221 */
Dan Williams6f231dd2011-07-02 22:56:22 -07001222 } else {
1223 /* Fill in the tmf stucture */
Jeff Skirvinc3f42fe2011-03-04 14:06:56 -08001224 isci_task_build_abort_task_tmf(&tmf, isci_device,
1225 isci_tmf_ssp_task_abort,
1226 isci_abort_task_process_cb,
1227 old_request);
Dan Williams6f231dd2011-07-02 22:56:22 -07001228
Dan Williams6f231dd2011-07-02 22:56:22 -07001229 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
1230
1231 #define ISCI_ABORT_TASK_TIMEOUT_MS 500 /* half second timeout. */
1232 ret = isci_task_execute_tmf(isci_host, &tmf,
1233 ISCI_ABORT_TASK_TIMEOUT_MS);
1234
Jeff Skirvina5fde222011-03-04 14:06:42 -08001235 if (ret != TMF_RESP_FUNC_COMPLETE)
Dan Williams6f231dd2011-07-02 22:56:22 -07001236 dev_err(&isci_host->pdev->dev,
1237 "%s: isci_task_send_tmf failed\n",
1238 __func__);
1239 }
Jeff Skirvina5fde222011-03-04 14:06:42 -08001240 if (ret == TMF_RESP_FUNC_COMPLETE) {
1241 old_request->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001242
Jeff Skirvin77c852f2011-06-20 14:09:16 -07001243 /* Clean up the request on our side, and wait for the aborted
1244 * I/O to complete.
1245 */
Jeff Skirvincbb65c62011-03-04 14:06:50 -08001246 isci_terminate_request_core(isci_host, isci_device, old_request);
Jeff Skirvina5fde222011-03-04 14:06:42 -08001247 }
1248
1249 /* Make sure we do not leave a reference to aborted_io_completion */
1250 old_request->io_request_completion = NULL;
Dan Williams6f231dd2011-07-02 22:56:22 -07001251 return ret;
1252}
1253
1254/**
1255 * isci_task_abort_task_set() - This function is one of the SAS Domain Template
1256 * functions. This is one of the Task Management functoins called by libsas,
1257 * to abort all task for the given lun.
1258 * @d_device: This parameter specifies the domain device associated with this
1259 * request.
1260 * @lun: This parameter specifies the lun associated with this request.
1261 *
1262 * status, zero indicates success.
1263 */
1264int isci_task_abort_task_set(
1265 struct domain_device *d_device,
1266 u8 *lun)
1267{
1268 return TMF_RESP_FUNC_FAILED;
1269}
1270
1271
1272/**
1273 * isci_task_clear_aca() - This function is one of the SAS Domain Template
1274 * functions. This is one of the Task Management functoins called by libsas.
1275 * @d_device: This parameter specifies the domain device associated with this
1276 * request.
1277 * @lun: This parameter specifies the lun associated with this request.
1278 *
1279 * status, zero indicates success.
1280 */
1281int isci_task_clear_aca(
1282 struct domain_device *d_device,
1283 u8 *lun)
1284{
1285 return TMF_RESP_FUNC_FAILED;
1286}
1287
1288
1289
1290/**
1291 * isci_task_clear_task_set() - This function is one of the SAS Domain Template
1292 * functions. This is one of the Task Management functoins called by libsas.
1293 * @d_device: This parameter specifies the domain device associated with this
1294 * request.
1295 * @lun: This parameter specifies the lun associated with this request.
1296 *
1297 * status, zero indicates success.
1298 */
1299int isci_task_clear_task_set(
1300 struct domain_device *d_device,
1301 u8 *lun)
1302{
1303 return TMF_RESP_FUNC_FAILED;
1304}
1305
1306
1307/**
1308 * isci_task_query_task() - This function is implemented to cause libsas to
1309 * correctly escalate the failed abort to a LUN or target reset (this is
1310 * because sas_scsi_find_task libsas function does not correctly interpret
1311 * all return codes from the abort task call). When TMF_RESP_FUNC_SUCC is
1312 * returned, libsas turns this into a LUN reset; when FUNC_FAILED is
1313 * returned, libsas will turn this into a target reset
1314 * @task: This parameter specifies the sas task being queried.
1315 * @lun: This parameter specifies the lun associated with this request.
1316 *
1317 * status, zero indicates success.
1318 */
1319int isci_task_query_task(
1320 struct sas_task *task)
1321{
1322 /* See if there is a pending device reset for this device. */
1323 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
1324 return TMF_RESP_FUNC_FAILED;
1325 else
1326 return TMF_RESP_FUNC_SUCC;
1327}
1328
Dave Jiangaf5ae892011-05-04 17:53:24 -07001329/*
Dan Williams6f231dd2011-07-02 22:56:22 -07001330 * isci_task_request_complete() - This function is called by the sci core when
1331 * an task request completes.
Dave Jiangaf5ae892011-05-04 17:53:24 -07001332 * @ihost: This parameter specifies the ISCI host object
1333 * @ireq: This parameter is the completed isci_request object.
Dan Williams6f231dd2011-07-02 22:56:22 -07001334 * @completion_status: This parameter specifies the completion status from the
1335 * sci core.
1336 *
1337 * none.
1338 */
Dave Jiangaf5ae892011-05-04 17:53:24 -07001339void
1340isci_task_request_complete(struct isci_host *ihost,
1341 struct isci_request *ireq,
1342 enum sci_task_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07001343{
Dave Jiangaf5ae892011-05-04 17:53:24 -07001344 struct isci_remote_device *idev = ireq->isci_device;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001345 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001346 struct completion *tmf_complete;
Dan Williams67ea8382011-05-08 11:47:15 -07001347 struct scic_sds_request *sci_req = &ireq->sci;
Dan Williams6f231dd2011-07-02 22:56:22 -07001348
Dave Jiangaf5ae892011-05-04 17:53:24 -07001349 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07001350 "%s: request = %p, status=%d\n",
Dave Jiangaf5ae892011-05-04 17:53:24 -07001351 __func__, ireq, completion_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001352
Dave Jiang8d2c65c2011-06-01 09:03:08 +00001353 isci_request_change_state(ireq, completed);
Dan Williams6f231dd2011-07-02 22:56:22 -07001354
1355 tmf->status = completion_status;
Dave Jiangaf5ae892011-05-04 17:53:24 -07001356 ireq->complete_in_target = true;
Dan Williams6f231dd2011-07-02 22:56:22 -07001357
Dave Jiangaf5ae892011-05-04 17:53:24 -07001358 if (tmf->proto == SAS_PROTOCOL_SSP) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001359 memcpy(&tmf->resp.resp_iu,
Dan Williamsb7645812011-05-08 02:35:32 -07001360 &sci_req->ssp.rsp,
Dave Jiangaf5ae892011-05-04 17:53:24 -07001361 SSP_RESP_IU_MAX_SIZE);
1362 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001363 memcpy(&tmf->resp.d2h_fis,
Dan Williamsb7645812011-05-08 02:35:32 -07001364 &sci_req->stp.rsp,
Dave Jiangf2f30082011-05-04 15:02:02 -07001365 sizeof(struct dev_to_host_fis));
Dan Williams6f231dd2011-07-02 22:56:22 -07001366 }
1367
Dan Williams6f231dd2011-07-02 22:56:22 -07001368 /* PRINT_TMF( ((struct isci_tmf *)request->task)); */
1369 tmf_complete = tmf->complete;
1370
Dan Williams67ea8382011-05-08 11:47:15 -07001371 scic_controller_complete_io(&ihost->sci, &idev->sci, &ireq->sci);
1372 /* set the 'terminated' flag handle to make sure it cannot be terminated
Dan Williams6f231dd2011-07-02 22:56:22 -07001373 * or completed again.
1374 */
Dan Williams67ea8382011-05-08 11:47:15 -07001375 ireq->terminated = true;;
Dan Williams6f231dd2011-07-02 22:56:22 -07001376
Dave Jiangaf5ae892011-05-04 17:53:24 -07001377 isci_request_change_state(ireq, unallocated);
1378 list_del_init(&ireq->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07001379
1380 /* The task management part completes last. */
1381 complete(tmf_complete);
1382}
1383
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001384static void isci_smp_task_timedout(unsigned long _task)
1385{
1386 struct sas_task *task = (void *) _task;
1387 unsigned long flags;
1388
1389 spin_lock_irqsave(&task->task_state_lock, flags);
1390 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1391 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1392 spin_unlock_irqrestore(&task->task_state_lock, flags);
1393
1394 complete(&task->completion);
1395}
1396
1397static void isci_smp_task_done(struct sas_task *task)
1398{
1399 if (!del_timer(&task->timer))
1400 return;
1401 complete(&task->completion);
1402}
1403
1404static struct sas_task *isci_alloc_task(void)
1405{
1406 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
1407
1408 if (task) {
1409 INIT_LIST_HEAD(&task->list);
1410 spin_lock_init(&task->task_state_lock);
1411 task->task_state_flags = SAS_TASK_STATE_PENDING;
1412 init_timer(&task->timer);
1413 init_completion(&task->completion);
1414 }
1415
1416 return task;
1417}
1418
1419static void isci_free_task(struct isci_host *ihost, struct sas_task *task)
1420{
1421 if (task) {
1422 BUG_ON(!list_empty(&task->list));
1423 kfree(task);
1424 }
1425}
1426
1427static int isci_smp_execute_task(struct isci_host *ihost,
1428 struct domain_device *dev, void *req,
1429 int req_size, void *resp, int resp_size)
1430{
1431 int res, retry;
1432 struct sas_task *task = NULL;
1433
1434 for (retry = 0; retry < 3; retry++) {
1435 task = isci_alloc_task();
1436 if (!task)
1437 return -ENOMEM;
1438
1439 task->dev = dev;
1440 task->task_proto = dev->tproto;
1441 sg_init_one(&task->smp_task.smp_req, req, req_size);
1442 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
1443
1444 task->task_done = isci_smp_task_done;
1445
1446 task->timer.data = (unsigned long) task;
1447 task->timer.function = isci_smp_task_timedout;
1448 task->timer.expires = jiffies + 10*HZ;
1449 add_timer(&task->timer);
1450
1451 res = isci_task_execute_task(task, 1, GFP_KERNEL);
1452
1453 if (res) {
1454 del_timer(&task->timer);
1455 dev_err(&ihost->pdev->dev,
1456 "%s: executing SMP task failed:%d\n",
1457 __func__, res);
1458 goto ex_err;
1459 }
1460
1461 wait_for_completion(&task->completion);
1462 res = -ECOMM;
1463 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1464 dev_err(&ihost->pdev->dev,
1465 "%s: smp task timed out or aborted\n",
1466 __func__);
1467 isci_task_abort_task(task);
1468 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1469 dev_err(&ihost->pdev->dev,
1470 "%s: SMP task aborted and not done\n",
1471 __func__);
1472 goto ex_err;
1473 }
1474 }
1475 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1476 task->task_status.stat == SAM_STAT_GOOD) {
1477 res = 0;
1478 break;
1479 }
1480 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1481 task->task_status.stat == SAS_DATA_UNDERRUN) {
1482 /* no error, but return the number of bytes of
1483 * underrun */
1484 res = task->task_status.residual;
1485 break;
1486 }
1487 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1488 task->task_status.stat == SAS_DATA_OVERRUN) {
1489 res = -EMSGSIZE;
1490 break;
1491 } else {
1492 dev_err(&ihost->pdev->dev,
1493 "%s: task to dev %016llx response: 0x%x "
1494 "status 0x%x\n", __func__,
1495 SAS_ADDR(dev->sas_addr),
1496 task->task_status.resp,
1497 task->task_status.stat);
1498 isci_free_task(ihost, task);
1499 task = NULL;
1500 }
1501 }
1502ex_err:
1503 BUG_ON(retry == 3 && task != NULL);
1504 isci_free_task(ihost, task);
1505 return res;
1506}
1507
1508#define DISCOVER_REQ_SIZE 16
1509#define DISCOVER_RESP_SIZE 56
1510
1511int isci_smp_get_phy_attached_dev_type(struct isci_host *ihost,
1512 struct domain_device *dev,
1513 int phy_id, int *adt)
1514{
1515 struct smp_resp *disc_resp;
1516 u8 *disc_req;
1517 int res;
1518
1519 disc_resp = kzalloc(DISCOVER_RESP_SIZE, GFP_KERNEL);
1520 if (!disc_resp)
1521 return -ENOMEM;
1522
1523 disc_req = kzalloc(DISCOVER_REQ_SIZE, GFP_KERNEL);
1524 if (disc_req) {
1525 disc_req[0] = SMP_REQUEST;
1526 disc_req[1] = SMP_DISCOVER;
1527 disc_req[9] = phy_id;
1528 } else {
1529 kfree(disc_resp);
1530 return -ENOMEM;
1531 }
1532 res = isci_smp_execute_task(ihost, dev, disc_req, DISCOVER_REQ_SIZE,
1533 disc_resp, DISCOVER_RESP_SIZE);
1534 if (!res) {
1535 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1536 res = disc_resp->result;
1537 else
1538 *adt = disc_resp->disc.attached_dev_type;
1539 }
1540 kfree(disc_req);
1541 kfree(disc_resp);
1542
1543 return res;
1544}
1545
1546static void isci_wait_for_smp_phy_reset(struct isci_remote_device *idev, int phy_num)
1547{
1548 struct domain_device *dev = idev->domain_dev;
1549 struct isci_port *iport = idev->isci_port;
1550 struct isci_host *ihost = iport->isci_host;
1551 int res, iteration = 0, attached_device_type;
1552 #define STP_WAIT_MSECS 25000
1553 unsigned long tmo = msecs_to_jiffies(STP_WAIT_MSECS);
1554 unsigned long deadline = jiffies + tmo;
1555 enum {
1556 SMP_PHYWAIT_PHYDOWN,
1557 SMP_PHYWAIT_PHYUP,
1558 SMP_PHYWAIT_DONE
1559 } phy_state = SMP_PHYWAIT_PHYDOWN;
1560
1561 /* While there is time, wait for the phy to go away and come back */
1562 while (time_is_after_jiffies(deadline) && phy_state != SMP_PHYWAIT_DONE) {
1563 int event = atomic_read(&iport->event);
1564
1565 ++iteration;
1566
1567 tmo = wait_event_timeout(ihost->eventq,
1568 event != atomic_read(&iport->event) ||
1569 !test_bit(IPORT_BCN_BLOCKED, &iport->flags),
1570 tmo);
1571 /* link down, stop polling */
1572 if (!test_bit(IPORT_BCN_BLOCKED, &iport->flags))
1573 break;
1574
1575 dev_dbg(&ihost->pdev->dev,
1576 "%s: iport %p, iteration %d,"
1577 " phase %d: time_remaining %lu, bcns = %d\n",
1578 __func__, iport, iteration, phy_state,
1579 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags));
1580
1581 res = isci_smp_get_phy_attached_dev_type(ihost, dev, phy_num,
1582 &attached_device_type);
1583 tmo = deadline - jiffies;
1584
1585 if (res) {
1586 dev_warn(&ihost->pdev->dev,
1587 "%s: iteration %d, phase %d:"
1588 " SMP error=%d, time_remaining=%lu\n",
1589 __func__, iteration, phy_state, res, tmo);
1590 break;
1591 }
1592 dev_dbg(&ihost->pdev->dev,
1593 "%s: iport %p, iteration %d,"
1594 " phase %d: time_remaining %lu, bcns = %d, "
1595 "attdevtype = %x\n",
1596 __func__, iport, iteration, phy_state,
1597 tmo, test_bit(IPORT_BCN_PENDING, &iport->flags),
1598 attached_device_type);
1599
1600 switch (phy_state) {
1601 case SMP_PHYWAIT_PHYDOWN:
1602 /* Has the device gone away? */
1603 if (!attached_device_type)
1604 phy_state = SMP_PHYWAIT_PHYUP;
1605
1606 break;
1607
1608 case SMP_PHYWAIT_PHYUP:
1609 /* Has the device come back? */
1610 if (attached_device_type)
1611 phy_state = SMP_PHYWAIT_DONE;
1612 break;
1613
1614 case SMP_PHYWAIT_DONE:
1615 break;
1616 }
1617
1618 }
1619 dev_dbg(&ihost->pdev->dev, "%s: done\n", __func__);
1620}
1621
Dan Williamsd06b4872011-05-02 13:59:25 -07001622static int isci_reset_device(struct domain_device *dev, int hard_reset)
Dan Williams6f231dd2011-07-02 22:56:22 -07001623{
Dan Williamsd06b4872011-05-02 13:59:25 -07001624 struct isci_remote_device *idev = dev->lldd_dev;
1625 struct sas_phy *phy = sas_find_local_phy(dev);
1626 struct isci_host *ihost = dev_to_ihost(dev);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001627 struct isci_port *iport = idev->isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -07001628 enum sci_status status;
Dan Williamsd06b4872011-05-02 13:59:25 -07001629 unsigned long flags;
1630 int rc;
Dan Williams6f231dd2011-07-02 22:56:22 -07001631
Dan Williamsd06b4872011-05-02 13:59:25 -07001632 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001633
Dan Williamsd06b4872011-05-02 13:59:25 -07001634 if (!idev) {
1635 dev_warn(&ihost->pdev->dev,
1636 "%s: idev is GONE!\n",
Dan Williams6f231dd2011-07-02 22:56:22 -07001637 __func__);
1638
1639 return TMF_RESP_FUNC_COMPLETE; /* Nothing to reset. */
1640 }
1641
Dan Williamsd06b4872011-05-02 13:59:25 -07001642 spin_lock_irqsave(&ihost->scic_lock, flags);
1643 status = scic_remote_device_reset(&idev->sci);
Dan Williams6f231dd2011-07-02 22:56:22 -07001644 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001645 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001646
Dan Williamsd06b4872011-05-02 13:59:25 -07001647 dev_warn(&ihost->pdev->dev,
1648 "%s: scic_remote_device_reset(%p) returned %d!\n",
1649 __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001650
1651 return TMF_RESP_FUNC_FAILED;
1652 }
Dan Williamsd06b4872011-05-02 13:59:25 -07001653 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001654
Dan Williams6f231dd2011-07-02 22:56:22 -07001655 /* Make sure all pending requests are able to be fully terminated. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001656 isci_device_clear_reset_pending(ihost, idev);
1657
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001658 /* If this is a device on an expander, disable BCN processing. */
1659 if (!scsi_is_sas_phy_local(phy))
1660 set_bit(IPORT_BCN_BLOCKED, &iport->flags);
1661
Dan Williamsd06b4872011-05-02 13:59:25 -07001662 rc = sas_phy_reset(phy, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001663
1664 /* Terminate in-progress I/O now. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001665 isci_remote_device_nuke_requests(ihost, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001666
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001667 /* Since all pending TCs have been cleaned, resume the RNC. */
Dan Williamsd06b4872011-05-02 13:59:25 -07001668 spin_lock_irqsave(&ihost->scic_lock, flags);
1669 status = scic_remote_device_reset_complete(&idev->sci);
1670 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001671
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001672 /* If this is a device on an expander, bring the phy back up. */
1673 if (!scsi_is_sas_phy_local(phy)) {
1674 /* A phy reset will cause the device to go away then reappear.
1675 * Since libsas will take action on incoming BCNs (eg. remove
1676 * a device going through an SMP phy-control driven reset),
1677 * we need to wait until the phy comes back up before letting
1678 * discovery proceed in libsas.
1679 */
1680 isci_wait_for_smp_phy_reset(idev, phy->number);
1681
1682 spin_lock_irqsave(&ihost->scic_lock, flags);
1683 isci_port_bcn_enable(ihost, idev->isci_port);
1684 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1685 }
Jeff Skirvinff717ab2011-06-20 14:08:51 -07001686
Dan Williams6f231dd2011-07-02 22:56:22 -07001687 if (status != SCI_SUCCESS) {
Dan Williamsd06b4872011-05-02 13:59:25 -07001688 dev_warn(&ihost->pdev->dev,
1689 "%s: scic_remote_device_reset_complete(%p) "
1690 "returned %d!\n", __func__, idev, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001691 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001692
Dan Williamsd06b4872011-05-02 13:59:25 -07001693 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete.\n", __func__, idev);
Dan Williams6f231dd2011-07-02 22:56:22 -07001694
Dan Williamsd06b4872011-05-02 13:59:25 -07001695 return rc;
1696}
1697
1698int isci_task_I_T_nexus_reset(struct domain_device *dev)
1699{
1700 struct isci_host *ihost = dev_to_ihost(dev);
1701 int ret = TMF_RESP_FUNC_FAILED, hard_reset = 1;
1702 struct isci_remote_device *idev;
1703 unsigned long flags;
1704
1705 /* XXX mvsas is not protecting against ->lldd_dev_gone(), are we
1706 * being too paranoid, or is mvsas busted?!
1707 */
1708 spin_lock_irqsave(&ihost->scic_lock, flags);
1709 idev = dev->lldd_dev;
1710 if (!idev || !test_bit(IDEV_EH, &idev->flags))
1711 ret = TMF_RESP_FUNC_COMPLETE;
1712 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1713
1714 if (ret == TMF_RESP_FUNC_COMPLETE)
1715 return ret;
1716
1717 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1718 hard_reset = 0;
1719
1720 return isci_reset_device(dev, hard_reset);
1721}
1722
1723int isci_bus_reset_handler(struct scsi_cmnd *cmd)
1724{
1725 struct domain_device *dev = sdev_to_domain_dev(cmd->device);
1726 int hard_reset = 1;
1727
1728 if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1729 hard_reset = 0;
1730
1731 return isci_reset_device(dev, hard_reset);
Dan Williams6f231dd2011-07-02 22:56:22 -07001732}