blob: 144f5ad20453bbc56ec010d8e870c881c0ef0fd2 [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
2 * Aic94xx Task Management Functions
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This file is part of the aic94xx driver.
10 *
11 * The aic94xx driver is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; version 2 of the
14 * License.
15 *
16 * The aic94xx driver is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with the aic94xx driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27#include <linux/spinlock.h>
28#include "aic94xx.h"
29#include "aic94xx_sas.h"
30#include "aic94xx_hwi.h"
31
32/* ---------- Internal enqueue ---------- */
33
34static int asd_enqueue_internal(struct asd_ascb *ascb,
35 void (*tasklet_complete)(struct asd_ascb *,
36 struct done_list_struct *),
37 void (*timed_out)(unsigned long))
38{
39 int res;
40
41 ascb->tasklet_complete = tasklet_complete;
42 ascb->uldd_timer = 1;
43
44 ascb->timer.data = (unsigned long) ascb;
45 ascb->timer.function = timed_out;
46 ascb->timer.expires = jiffies + AIC94XX_SCB_TIMEOUT;
47
48 add_timer(&ascb->timer);
49
50 res = asd_post_ascb_list(ascb->ha, ascb, 1);
51 if (unlikely(res))
52 del_timer(&ascb->timer);
53 return res;
54}
55
56static inline void asd_timedout_common(unsigned long data)
57{
58 struct asd_ascb *ascb = (void *) data;
59 struct asd_seq_data *seq = &ascb->ha->seq;
60 unsigned long flags;
61
62 spin_lock_irqsave(&seq->pend_q_lock, flags);
63 seq->pending--;
64 list_del_init(&ascb->list);
65 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
66}
67
68/* ---------- CLEAR NEXUS ---------- */
69
70static void asd_clear_nexus_tasklet_complete(struct asd_ascb *ascb,
71 struct done_list_struct *dl)
72{
73 ASD_DPRINTK("%s: here\n", __FUNCTION__);
74 if (!del_timer(&ascb->timer)) {
75 ASD_DPRINTK("%s: couldn't delete timer\n", __FUNCTION__);
76 return;
77 }
78 ASD_DPRINTK("%s: opcode: 0x%x\n", __FUNCTION__, dl->opcode);
79 ascb->uldd_task = (void *) (unsigned long) dl->opcode;
80 complete(&ascb->completion);
81}
82
83static void asd_clear_nexus_timedout(unsigned long data)
84{
85 struct asd_ascb *ascb = (void *) data;
86
87 ASD_DPRINTK("%s: here\n", __FUNCTION__);
88 asd_timedout_common(data);
89 ascb->uldd_task = (void *) TMF_RESP_FUNC_FAILED;
90 complete(&ascb->completion);
91}
92
93#define CLEAR_NEXUS_PRE \
94 ASD_DPRINTK("%s: PRE\n", __FUNCTION__); \
95 res = 1; \
96 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL); \
97 if (!ascb) \
98 return -ENOMEM; \
99 \
100 scb = ascb->scb; \
101 scb->header.opcode = CLEAR_NEXUS
102
103#define CLEAR_NEXUS_POST \
104 ASD_DPRINTK("%s: POST\n", __FUNCTION__); \
105 res = asd_enqueue_internal(ascb, asd_clear_nexus_tasklet_complete, \
106 asd_clear_nexus_timedout); \
107 if (res) \
108 goto out_err; \
109 ASD_DPRINTK("%s: clear nexus posted, waiting...\n", __FUNCTION__); \
110 wait_for_completion(&ascb->completion); \
111 res = (int) (unsigned long) ascb->uldd_task; \
112 if (res == TC_NO_ERROR) \
113 res = TMF_RESP_FUNC_COMPLETE; \
114out_err: \
115 asd_ascb_free(ascb); \
116 return res
117
118int asd_clear_nexus_ha(struct sas_ha_struct *sas_ha)
119{
120 struct asd_ha_struct *asd_ha = sas_ha->lldd_ha;
121 struct asd_ascb *ascb;
122 struct scb *scb;
123 int res;
124
125 CLEAR_NEXUS_PRE;
126 scb->clear_nexus.nexus = NEXUS_ADAPTER;
127 CLEAR_NEXUS_POST;
128}
129
130int asd_clear_nexus_port(struct asd_sas_port *port)
131{
132 struct asd_ha_struct *asd_ha = port->ha->lldd_ha;
133 struct asd_ascb *ascb;
134 struct scb *scb;
135 int res;
136
137 CLEAR_NEXUS_PRE;
138 scb->clear_nexus.nexus = NEXUS_PORT;
139 scb->clear_nexus.conn_mask = port->phy_mask;
140 CLEAR_NEXUS_POST;
141}
142
143#if 0
144static int asd_clear_nexus_I_T(struct domain_device *dev)
145{
146 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
147 struct asd_ascb *ascb;
148 struct scb *scb;
149 int res;
150
151 CLEAR_NEXUS_PRE;
152 scb->clear_nexus.nexus = NEXUS_I_T;
153 scb->clear_nexus.flags = SEND_Q | EXEC_Q | NOTINQ;
James Bottomley2908d772006-08-29 09:22:51 -0500154 scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
155 dev->lldd_dev);
156 CLEAR_NEXUS_POST;
157}
158#endif
159
160static int asd_clear_nexus_I_T_L(struct domain_device *dev, u8 *lun)
161{
162 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
163 struct asd_ascb *ascb;
164 struct scb *scb;
165 int res;
166
167 CLEAR_NEXUS_PRE;
168 scb->clear_nexus.nexus = NEXUS_I_T_L;
169 scb->clear_nexus.flags = SEND_Q | EXEC_Q | NOTINQ;
James Bottomley2908d772006-08-29 09:22:51 -0500170 memcpy(scb->clear_nexus.ssp_task.lun, lun, 8);
171 scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
172 dev->lldd_dev);
173 CLEAR_NEXUS_POST;
174}
175
176static int asd_clear_nexus_tag(struct sas_task *task)
177{
178 struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
179 struct asd_ascb *tascb = task->lldd_task;
180 struct asd_ascb *ascb;
181 struct scb *scb;
182 int res;
183
184 CLEAR_NEXUS_PRE;
185 scb->clear_nexus.nexus = NEXUS_TAG;
186 memcpy(scb->clear_nexus.ssp_task.lun, task->ssp_task.LUN, 8);
187 scb->clear_nexus.ssp_task.tag = tascb->tag;
188 if (task->dev->tproto)
189 scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
190 task->dev->lldd_dev);
191 CLEAR_NEXUS_POST;
192}
193
194static int asd_clear_nexus_index(struct sas_task *task)
195{
196 struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha;
197 struct asd_ascb *tascb = task->lldd_task;
198 struct asd_ascb *ascb;
199 struct scb *scb;
200 int res;
201
202 CLEAR_NEXUS_PRE;
203 scb->clear_nexus.nexus = NEXUS_TRANS_CX;
204 if (task->dev->tproto)
205 scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long)
206 task->dev->lldd_dev);
207 scb->clear_nexus.index = cpu_to_le16(tascb->tc_index);
208 CLEAR_NEXUS_POST;
209}
210
211/* ---------- TMFs ---------- */
212
213static void asd_tmf_timedout(unsigned long data)
214{
215 struct asd_ascb *ascb = (void *) data;
216
217 ASD_DPRINTK("tmf timed out\n");
218 asd_timedout_common(data);
219 ascb->uldd_task = (void *) TMF_RESP_FUNC_FAILED;
220 complete(&ascb->completion);
221}
222
223static int asd_get_tmf_resp_tasklet(struct asd_ascb *ascb,
224 struct done_list_struct *dl)
225{
226 struct asd_ha_struct *asd_ha = ascb->ha;
227 unsigned long flags;
228 struct tc_resp_sb_struct {
229 __le16 index_escb;
230 u8 len_lsb;
231 u8 flags;
232 } __attribute__ ((packed)) *resp_sb = (void *) dl->status_block;
233
234 int edb_id = ((resp_sb->flags & 0x70) >> 4)-1;
235 struct asd_ascb *escb;
236 struct asd_dma_tok *edb;
237 struct ssp_frame_hdr *fh;
238 struct ssp_response_iu *ru;
239 int res = TMF_RESP_FUNC_FAILED;
240
241 ASD_DPRINTK("tmf resp tasklet\n");
242
243 spin_lock_irqsave(&asd_ha->seq.tc_index_lock, flags);
244 escb = asd_tc_index_find(&asd_ha->seq,
245 (int)le16_to_cpu(resp_sb->index_escb));
246 spin_unlock_irqrestore(&asd_ha->seq.tc_index_lock, flags);
247
248 if (!escb) {
249 ASD_DPRINTK("Uh-oh! No escb for this dl?!\n");
250 return res;
251 }
252
253 edb = asd_ha->seq.edb_arr[edb_id + escb->edb_index];
254 ascb->tag = *(__be16 *)(edb->vaddr+4);
255 fh = edb->vaddr + 16;
256 ru = edb->vaddr + 16 + sizeof(*fh);
257 res = ru->status;
258 if (ru->datapres == 1) /* Response data present */
259 res = ru->resp_data[3];
260#if 0
261 ascb->tag = fh->tag;
262#endif
263 ascb->tag_valid = 1;
264
265 asd_invalidate_edb(escb, edb_id);
266 return res;
267}
268
269static void asd_tmf_tasklet_complete(struct asd_ascb *ascb,
270 struct done_list_struct *dl)
271{
272 if (!del_timer(&ascb->timer))
273 return;
274
275 ASD_DPRINTK("tmf tasklet complete\n");
276
277 if (dl->opcode == TC_SSP_RESP)
278 ascb->uldd_task = (void *) (unsigned long)
279 asd_get_tmf_resp_tasklet(ascb, dl);
280 else
281 ascb->uldd_task = (void *) 0xFF00 + (unsigned long) dl->opcode;
282
283 complete(&ascb->completion);
284}
285
286static inline int asd_clear_nexus(struct sas_task *task)
287{
288 int res = TMF_RESP_FUNC_FAILED;
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700289 int leftover;
James Bottomley2908d772006-08-29 09:22:51 -0500290 struct asd_ascb *tascb = task->lldd_task;
291 unsigned long flags;
292
293 ASD_DPRINTK("task not done, clearing nexus\n");
294 if (tascb->tag_valid)
295 res = asd_clear_nexus_tag(task);
296 else
297 res = asd_clear_nexus_index(task);
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700298 leftover = wait_for_completion_timeout(&tascb->completion,
299 AIC94XX_SCB_TIMEOUT);
James Bottomley2908d772006-08-29 09:22:51 -0500300 ASD_DPRINTK("came back from clear nexus\n");
301 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700302 if (leftover < 1)
303 res = TMF_RESP_FUNC_FAILED;
James Bottomley2908d772006-08-29 09:22:51 -0500304 if (task->task_state_flags & SAS_TASK_STATE_DONE)
305 res = TMF_RESP_FUNC_COMPLETE;
306 spin_unlock_irqrestore(&task->task_state_lock, flags);
307
308 return res;
309}
310
311/**
312 * asd_abort_task -- ABORT TASK TMF
313 * @task: the task to be aborted
314 *
315 * Before calling ABORT TASK the task state flags should be ORed with
316 * SAS_TASK_STATE_ABORTED (unless SAS_TASK_STATE_DONE is set) under
317 * the task_state_lock IRQ spinlock, then ABORT TASK *must* be called.
318 *
319 * Implements the ABORT TASK TMF, I_T_L_Q nexus.
320 * Returns: SAS TMF responses (see sas_task.h),
321 * -ENOMEM,
322 * -SAS_QUEUE_FULL.
323 *
324 * When ABORT TASK returns, the caller of ABORT TASK checks first the
325 * task->task_state_flags, and then the return value of ABORT TASK.
326 *
327 * If the task has task state bit SAS_TASK_STATE_DONE set, then the
328 * task was completed successfully prior to it being aborted. The
329 * caller of ABORT TASK has responsibility to call task->task_done()
330 * xor free the task, depending on their framework. The return code
331 * is TMF_RESP_FUNC_FAILED in this case.
332 *
333 * Else the SAS_TASK_STATE_DONE bit is not set,
334 * If the return code is TMF_RESP_FUNC_COMPLETE, then
335 * the task was aborted successfully. The caller of
336 * ABORT TASK has responsibility to call task->task_done()
337 * to finish the task, xor free the task depending on their
338 * framework.
339 * else
340 * the ABORT TASK returned some kind of error. The task
341 * was _not_ cancelled. Nothing can be assumed.
342 * The caller of ABORT TASK may wish to retry.
343 */
344int asd_abort_task(struct sas_task *task)
345{
346 struct asd_ascb *tascb = task->lldd_task;
347 struct asd_ha_struct *asd_ha = tascb->ha;
348 int res = 1;
349 unsigned long flags;
350 struct asd_ascb *ascb = NULL;
351 struct scb *scb;
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700352 int leftover;
James Bottomley2908d772006-08-29 09:22:51 -0500353
354 spin_lock_irqsave(&task->task_state_lock, flags);
355 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
356 spin_unlock_irqrestore(&task->task_state_lock, flags);
357 res = TMF_RESP_FUNC_COMPLETE;
358 ASD_DPRINTK("%s: task 0x%p done\n", __FUNCTION__, task);
359 goto out_done;
360 }
361 spin_unlock_irqrestore(&task->task_state_lock, flags);
362
363 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
364 if (!ascb)
365 return -ENOMEM;
366 scb = ascb->scb;
367
Boaz Harrosh90b0c412008-02-06 15:38:33 +0200368 scb->header.opcode = SCB_ABORT_TASK;
James Bottomley2908d772006-08-29 09:22:51 -0500369
370 switch (task->task_proto) {
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800371 case SAS_PROTOCOL_SATA:
372 case SAS_PROTOCOL_STP:
James Bottomley2908d772006-08-29 09:22:51 -0500373 scb->abort_task.proto_conn_rate = (1 << 5); /* STP */
374 break;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800375 case SAS_PROTOCOL_SSP:
James Bottomley2908d772006-08-29 09:22:51 -0500376 scb->abort_task.proto_conn_rate = (1 << 4); /* SSP */
377 scb->abort_task.proto_conn_rate |= task->dev->linkrate;
378 break;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800379 case SAS_PROTOCOL_SMP:
James Bottomley2908d772006-08-29 09:22:51 -0500380 break;
381 default:
382 break;
383 }
384
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800385 if (task->task_proto == SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500386 scb->abort_task.ssp_frame.frame_type = SSP_TASK;
387 memcpy(scb->abort_task.ssp_frame.hashed_dest_addr,
388 task->dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
389 memcpy(scb->abort_task.ssp_frame.hashed_src_addr,
390 task->dev->port->ha->hashed_sas_addr,
391 HASHED_SAS_ADDR_SIZE);
392 scb->abort_task.ssp_frame.tptt = cpu_to_be16(0xFFFF);
393
394 memcpy(scb->abort_task.ssp_task.lun, task->ssp_task.LUN, 8);
395 scb->abort_task.ssp_task.tmf = TMF_ABORT_TASK;
396 scb->abort_task.ssp_task.tag = cpu_to_be16(0xFFFF);
397 }
398
399 scb->abort_task.sister_scb = cpu_to_le16(0xFFFF);
400 scb->abort_task.conn_handle = cpu_to_le16(
401 (u16)(unsigned long)task->dev->lldd_dev);
402 scb->abort_task.retry_count = 1;
403 scb->abort_task.index = cpu_to_le16((u16)tascb->tc_index);
404 scb->abort_task.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST);
405
406 res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete,
407 asd_tmf_timedout);
408 if (res)
409 goto out;
410 wait_for_completion(&ascb->completion);
411 ASD_DPRINTK("tmf came back\n");
412
413 res = (int) (unsigned long) ascb->uldd_task;
414 tascb->tag = ascb->tag;
415 tascb->tag_valid = ascb->tag_valid;
416
417 spin_lock_irqsave(&task->task_state_lock, flags);
418 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
419 spin_unlock_irqrestore(&task->task_state_lock, flags);
420 res = TMF_RESP_FUNC_COMPLETE;
421 ASD_DPRINTK("%s: task 0x%p done\n", __FUNCTION__, task);
422 goto out_done;
423 }
424 spin_unlock_irqrestore(&task->task_state_lock, flags);
425
426 switch (res) {
427 /* The task to be aborted has been sent to the device.
428 * We got a Response IU for the ABORT TASK TMF. */
429 case TC_NO_ERROR + 0xFF00:
430 case TMF_RESP_FUNC_COMPLETE:
431 case TMF_RESP_FUNC_FAILED:
432 res = asd_clear_nexus(task);
433 break;
434 case TMF_RESP_INVALID_FRAME:
435 case TMF_RESP_OVERLAPPED_TAG:
436 case TMF_RESP_FUNC_ESUPP:
437 case TMF_RESP_NO_LUN:
438 goto out_done; break;
439 }
440 /* In the following we assume that the managing layer
441 * will _never_ make a mistake, when issuing ABORT TASK.
442 */
443 switch (res) {
444 default:
445 res = asd_clear_nexus(task);
446 /* fallthrough */
447 case TC_NO_ERROR + 0xFF00:
448 case TMF_RESP_FUNC_COMPLETE:
449 break;
450 /* The task hasn't been sent to the device xor we never got
451 * a (sane) Response IU for the ABORT TASK TMF.
452 */
453 case TF_NAK_RECV + 0xFF00:
454 res = TMF_RESP_INVALID_FRAME;
455 break;
456 case TF_TMF_TASK_DONE + 0xFF00: /* done but not reported yet */
457 res = TMF_RESP_FUNC_FAILED;
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700458 leftover = wait_for_completion_timeout(&tascb->completion,
459 AIC94XX_SCB_TIMEOUT);
James Bottomley2908d772006-08-29 09:22:51 -0500460 spin_lock_irqsave(&task->task_state_lock, flags);
Darrick J. Wong8fdcf862007-05-16 14:01:48 -0700461 if (leftover < 1)
462 res = TMF_RESP_FUNC_FAILED;
James Bottomley2908d772006-08-29 09:22:51 -0500463 if (task->task_state_flags & SAS_TASK_STATE_DONE)
464 res = TMF_RESP_FUNC_COMPLETE;
465 spin_unlock_irqrestore(&task->task_state_lock, flags);
466 goto out_done;
467 case TF_TMF_NO_TAG + 0xFF00:
468 case TF_TMF_TAG_FREE + 0xFF00: /* the tag is in the free list */
469 case TF_TMF_NO_CONN_HANDLE + 0xFF00: /* no such device */
470 res = TMF_RESP_FUNC_COMPLETE;
471 goto out_done;
472 case TF_TMF_NO_CTX + 0xFF00: /* not in seq, or proto != SSP */
473 res = TMF_RESP_FUNC_ESUPP;
474 goto out;
475 }
476out_done:
477 if (res == TMF_RESP_FUNC_COMPLETE) {
478 task->lldd_task = NULL;
479 mb();
480 asd_ascb_free(tascb);
481 }
482out:
483 asd_ascb_free(ascb);
484 ASD_DPRINTK("task 0x%p aborted, res: 0x%x\n", task, res);
485 return res;
486}
487
488/**
489 * asd_initiate_ssp_tmf -- send a TMF to an I_T_L or I_T_L_Q nexus
490 * @dev: pointer to struct domain_device of interest
491 * @lun: pointer to u8[8] which is the LUN
492 * @tmf: the TMF to be performed (see sas_task.h or the SAS spec)
493 * @index: the transaction context of the task to be queried if QT TMF
494 *
495 * This function is used to send ABORT TASK SET, CLEAR ACA,
496 * CLEAR TASK SET, LU RESET and QUERY TASK TMFs.
497 *
498 * No SCBs should be queued to the I_T_L nexus when this SCB is
499 * pending.
500 *
501 * Returns: TMF response code (see sas_task.h or the SAS spec)
502 */
503static int asd_initiate_ssp_tmf(struct domain_device *dev, u8 *lun,
504 int tmf, int index)
505{
506 struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
507 struct asd_ascb *ascb;
508 int res = 1;
509 struct scb *scb;
510
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800511 if (!(dev->tproto & SAS_PROTOCOL_SSP))
James Bottomley2908d772006-08-29 09:22:51 -0500512 return TMF_RESP_FUNC_ESUPP;
513
514 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
515 if (!ascb)
516 return -ENOMEM;
517 scb = ascb->scb;
518
519 if (tmf == TMF_QUERY_TASK)
520 scb->header.opcode = QUERY_SSP_TASK;
521 else
522 scb->header.opcode = INITIATE_SSP_TMF;
523
524 scb->ssp_tmf.proto_conn_rate = (1 << 4); /* SSP */
525 scb->ssp_tmf.proto_conn_rate |= dev->linkrate;
526 /* SSP frame header */
527 scb->ssp_tmf.ssp_frame.frame_type = SSP_TASK;
528 memcpy(scb->ssp_tmf.ssp_frame.hashed_dest_addr,
529 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
530 memcpy(scb->ssp_tmf.ssp_frame.hashed_src_addr,
531 dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
532 scb->ssp_tmf.ssp_frame.tptt = cpu_to_be16(0xFFFF);
533 /* SSP Task IU */
534 memcpy(scb->ssp_tmf.ssp_task.lun, lun, 8);
535 scb->ssp_tmf.ssp_task.tmf = tmf;
536
537 scb->ssp_tmf.sister_scb = cpu_to_le16(0xFFFF);
538 scb->ssp_tmf.conn_handle= cpu_to_le16((u16)(unsigned long)
539 dev->lldd_dev);
540 scb->ssp_tmf.retry_count = 1;
541 scb->ssp_tmf.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST);
542 if (tmf == TMF_QUERY_TASK)
543 scb->ssp_tmf.index = cpu_to_le16(index);
544
545 res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete,
546 asd_tmf_timedout);
547 if (res)
548 goto out_err;
549 wait_for_completion(&ascb->completion);
550 res = (int) (unsigned long) ascb->uldd_task;
551
552 switch (res) {
553 case TC_NO_ERROR + 0xFF00:
554 res = TMF_RESP_FUNC_COMPLETE;
555 break;
556 case TF_NAK_RECV + 0xFF00:
557 res = TMF_RESP_INVALID_FRAME;
558 break;
559 case TF_TMF_TASK_DONE + 0xFF00:
560 res = TMF_RESP_FUNC_FAILED;
561 break;
562 case TF_TMF_NO_TAG + 0xFF00:
563 case TF_TMF_TAG_FREE + 0xFF00: /* the tag is in the free list */
564 case TF_TMF_NO_CONN_HANDLE + 0xFF00: /* no such device */
565 res = TMF_RESP_FUNC_COMPLETE;
566 break;
567 case TF_TMF_NO_CTX + 0xFF00: /* not in seq, or proto != SSP */
568 res = TMF_RESP_FUNC_ESUPP;
569 break;
570 default:
Darrick J. Wong058e2c42007-01-29 23:48:22 -0800571 /* Allow TMF response codes to propagate upwards */
James Bottomley2908d772006-08-29 09:22:51 -0500572 break;
573 }
574out_err:
575 asd_ascb_free(ascb);
576 return res;
577}
578
579int asd_abort_task_set(struct domain_device *dev, u8 *lun)
580{
581 int res = asd_initiate_ssp_tmf(dev, lun, TMF_ABORT_TASK_SET, 0);
582
583 if (res == TMF_RESP_FUNC_COMPLETE)
584 asd_clear_nexus_I_T_L(dev, lun);
585 return res;
586}
587
588int asd_clear_aca(struct domain_device *dev, u8 *lun)
589{
590 int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_ACA, 0);
591
592 if (res == TMF_RESP_FUNC_COMPLETE)
593 asd_clear_nexus_I_T_L(dev, lun);
594 return res;
595}
596
597int asd_clear_task_set(struct domain_device *dev, u8 *lun)
598{
599 int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_TASK_SET, 0);
600
601 if (res == TMF_RESP_FUNC_COMPLETE)
602 asd_clear_nexus_I_T_L(dev, lun);
603 return res;
604}
605
606int asd_lu_reset(struct domain_device *dev, u8 *lun)
607{
608 int res = asd_initiate_ssp_tmf(dev, lun, TMF_LU_RESET, 0);
609
610 if (res == TMF_RESP_FUNC_COMPLETE)
611 asd_clear_nexus_I_T_L(dev, lun);
612 return res;
613}
614
615/**
616 * asd_query_task -- send a QUERY TASK TMF to an I_T_L_Q nexus
617 * task: pointer to sas_task struct of interest
618 *
619 * Returns: TMF_RESP_FUNC_COMPLETE if the task is not in the task set,
620 * or TMF_RESP_FUNC_SUCC if the task is in the task set.
621 *
622 * Normally the management layer sets the task to aborted state,
623 * and then calls query task and then abort task.
624 */
625int asd_query_task(struct sas_task *task)
626{
627 struct asd_ascb *ascb = task->lldd_task;
628 int index;
629
630 if (ascb) {
631 index = ascb->tc_index;
632 return asd_initiate_ssp_tmf(task->dev, task->ssp_task.LUN,
633 TMF_QUERY_TASK, index);
634 }
635 return TMF_RESP_FUNC_COMPLETE;
636}