James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | static 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 | |
| 56 | static 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 | |
| 70 | static 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 | |
| 83 | static 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; \ |
| 114 | out_err: \ |
| 115 | asd_ascb_free(ascb); \ |
| 116 | return res |
| 117 | |
| 118 | int 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 | |
| 130 | int 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 | |
James Bottomley | 63edf49 | 2008-02-23 23:37:26 -0600 | [diff] [blame^] | 143 | enum clear_nexus_phase { |
| 144 | NEXUS_PHASE_PRE, |
| 145 | NEXUS_PHASE_POST, |
| 146 | NEXUS_PHASE_RESUME, |
| 147 | }; |
| 148 | |
| 149 | static int asd_clear_nexus_I_T(struct domain_device *dev, |
| 150 | enum clear_nexus_phase phase) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 151 | { |
| 152 | struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha; |
| 153 | struct asd_ascb *ascb; |
| 154 | struct scb *scb; |
| 155 | int res; |
| 156 | |
| 157 | CLEAR_NEXUS_PRE; |
| 158 | scb->clear_nexus.nexus = NEXUS_I_T; |
James Bottomley | 63edf49 | 2008-02-23 23:37:26 -0600 | [diff] [blame^] | 159 | switch (phase) { |
| 160 | case NEXUS_PHASE_PRE: |
| 161 | scb->clear_nexus.flags = EXEC_Q | SUSPEND_TX; |
| 162 | break; |
| 163 | case NEXUS_PHASE_POST: |
| 164 | scb->clear_nexus.flags = SEND_Q | NOTINQ; |
| 165 | break; |
| 166 | case NEXUS_PHASE_RESUME: |
| 167 | scb->clear_nexus.flags = RESUME_TX; |
| 168 | } |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 169 | scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long) |
| 170 | dev->lldd_dev); |
| 171 | CLEAR_NEXUS_POST; |
| 172 | } |
James Bottomley | 63edf49 | 2008-02-23 23:37:26 -0600 | [diff] [blame^] | 173 | |
| 174 | int asd_I_T_nexus_reset(struct domain_device *dev) |
| 175 | { |
| 176 | int res, tmp_res, i; |
| 177 | struct sas_phy *phy = sas_find_local_phy(dev); |
| 178 | /* Standard mandates link reset for ATA (type 0) and |
| 179 | * hard reset for SSP (type 1) */ |
| 180 | int reset_type = (dev->dev_type == SATA_DEV || |
| 181 | (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1; |
| 182 | |
| 183 | asd_clear_nexus_I_T(dev, NEXUS_PHASE_PRE); |
| 184 | /* send a hard reset */ |
| 185 | ASD_DPRINTK("sending %s reset to %s\n", |
| 186 | reset_type ? "hard" : "soft", phy->dev.bus_id); |
| 187 | res = sas_phy_reset(phy, reset_type); |
| 188 | if (res == TMF_RESP_FUNC_COMPLETE) { |
| 189 | /* wait for the maximum settle time */ |
| 190 | msleep(500); |
| 191 | /* clear all outstanding commands (keep nexus suspended) */ |
| 192 | asd_clear_nexus_I_T(dev, NEXUS_PHASE_POST); |
| 193 | } |
| 194 | for (i = 0 ; i < 3; i++) { |
| 195 | tmp_res = asd_clear_nexus_I_T(dev, NEXUS_PHASE_RESUME); |
| 196 | if (tmp_res == TC_RESUME) |
| 197 | return res; |
| 198 | msleep(500); |
| 199 | } |
| 200 | |
| 201 | /* This is a bit of a problem: the sequencer is still suspended |
| 202 | * and is refusing to resume. Hope it will resume on a bigger hammer |
| 203 | * or the disk is lost */ |
| 204 | dev_printk(KERN_ERR, &phy->dev, |
| 205 | "Failed to resume nexus after reset 0x%x\n", tmp_res); |
| 206 | |
| 207 | return TMF_RESP_FUNC_FAILED; |
| 208 | } |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 209 | |
| 210 | static int asd_clear_nexus_I_T_L(struct domain_device *dev, u8 *lun) |
| 211 | { |
| 212 | struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha; |
| 213 | struct asd_ascb *ascb; |
| 214 | struct scb *scb; |
| 215 | int res; |
| 216 | |
| 217 | CLEAR_NEXUS_PRE; |
| 218 | scb->clear_nexus.nexus = NEXUS_I_T_L; |
| 219 | scb->clear_nexus.flags = SEND_Q | EXEC_Q | NOTINQ; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 220 | memcpy(scb->clear_nexus.ssp_task.lun, lun, 8); |
| 221 | scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long) |
| 222 | dev->lldd_dev); |
| 223 | CLEAR_NEXUS_POST; |
| 224 | } |
| 225 | |
| 226 | static int asd_clear_nexus_tag(struct sas_task *task) |
| 227 | { |
| 228 | struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha; |
| 229 | struct asd_ascb *tascb = task->lldd_task; |
| 230 | struct asd_ascb *ascb; |
| 231 | struct scb *scb; |
| 232 | int res; |
| 233 | |
| 234 | CLEAR_NEXUS_PRE; |
| 235 | scb->clear_nexus.nexus = NEXUS_TAG; |
| 236 | memcpy(scb->clear_nexus.ssp_task.lun, task->ssp_task.LUN, 8); |
| 237 | scb->clear_nexus.ssp_task.tag = tascb->tag; |
| 238 | if (task->dev->tproto) |
| 239 | scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long) |
| 240 | task->dev->lldd_dev); |
| 241 | CLEAR_NEXUS_POST; |
| 242 | } |
| 243 | |
| 244 | static int asd_clear_nexus_index(struct sas_task *task) |
| 245 | { |
| 246 | struct asd_ha_struct *asd_ha = task->dev->port->ha->lldd_ha; |
| 247 | struct asd_ascb *tascb = task->lldd_task; |
| 248 | struct asd_ascb *ascb; |
| 249 | struct scb *scb; |
| 250 | int res; |
| 251 | |
| 252 | CLEAR_NEXUS_PRE; |
| 253 | scb->clear_nexus.nexus = NEXUS_TRANS_CX; |
| 254 | if (task->dev->tproto) |
| 255 | scb->clear_nexus.conn_handle = cpu_to_le16((u16)(unsigned long) |
| 256 | task->dev->lldd_dev); |
| 257 | scb->clear_nexus.index = cpu_to_le16(tascb->tc_index); |
| 258 | CLEAR_NEXUS_POST; |
| 259 | } |
| 260 | |
| 261 | /* ---------- TMFs ---------- */ |
| 262 | |
| 263 | static void asd_tmf_timedout(unsigned long data) |
| 264 | { |
| 265 | struct asd_ascb *ascb = (void *) data; |
| 266 | |
| 267 | ASD_DPRINTK("tmf timed out\n"); |
| 268 | asd_timedout_common(data); |
| 269 | ascb->uldd_task = (void *) TMF_RESP_FUNC_FAILED; |
| 270 | complete(&ascb->completion); |
| 271 | } |
| 272 | |
| 273 | static int asd_get_tmf_resp_tasklet(struct asd_ascb *ascb, |
| 274 | struct done_list_struct *dl) |
| 275 | { |
| 276 | struct asd_ha_struct *asd_ha = ascb->ha; |
| 277 | unsigned long flags; |
| 278 | struct tc_resp_sb_struct { |
| 279 | __le16 index_escb; |
| 280 | u8 len_lsb; |
| 281 | u8 flags; |
| 282 | } __attribute__ ((packed)) *resp_sb = (void *) dl->status_block; |
| 283 | |
| 284 | int edb_id = ((resp_sb->flags & 0x70) >> 4)-1; |
| 285 | struct asd_ascb *escb; |
| 286 | struct asd_dma_tok *edb; |
| 287 | struct ssp_frame_hdr *fh; |
| 288 | struct ssp_response_iu *ru; |
| 289 | int res = TMF_RESP_FUNC_FAILED; |
| 290 | |
| 291 | ASD_DPRINTK("tmf resp tasklet\n"); |
| 292 | |
| 293 | spin_lock_irqsave(&asd_ha->seq.tc_index_lock, flags); |
| 294 | escb = asd_tc_index_find(&asd_ha->seq, |
| 295 | (int)le16_to_cpu(resp_sb->index_escb)); |
| 296 | spin_unlock_irqrestore(&asd_ha->seq.tc_index_lock, flags); |
| 297 | |
| 298 | if (!escb) { |
| 299 | ASD_DPRINTK("Uh-oh! No escb for this dl?!\n"); |
| 300 | return res; |
| 301 | } |
| 302 | |
| 303 | edb = asd_ha->seq.edb_arr[edb_id + escb->edb_index]; |
| 304 | ascb->tag = *(__be16 *)(edb->vaddr+4); |
| 305 | fh = edb->vaddr + 16; |
| 306 | ru = edb->vaddr + 16 + sizeof(*fh); |
| 307 | res = ru->status; |
| 308 | if (ru->datapres == 1) /* Response data present */ |
| 309 | res = ru->resp_data[3]; |
| 310 | #if 0 |
| 311 | ascb->tag = fh->tag; |
| 312 | #endif |
| 313 | ascb->tag_valid = 1; |
| 314 | |
| 315 | asd_invalidate_edb(escb, edb_id); |
| 316 | return res; |
| 317 | } |
| 318 | |
| 319 | static void asd_tmf_tasklet_complete(struct asd_ascb *ascb, |
| 320 | struct done_list_struct *dl) |
| 321 | { |
| 322 | if (!del_timer(&ascb->timer)) |
| 323 | return; |
| 324 | |
| 325 | ASD_DPRINTK("tmf tasklet complete\n"); |
| 326 | |
| 327 | if (dl->opcode == TC_SSP_RESP) |
| 328 | ascb->uldd_task = (void *) (unsigned long) |
| 329 | asd_get_tmf_resp_tasklet(ascb, dl); |
| 330 | else |
| 331 | ascb->uldd_task = (void *) 0xFF00 + (unsigned long) dl->opcode; |
| 332 | |
| 333 | complete(&ascb->completion); |
| 334 | } |
| 335 | |
| 336 | static inline int asd_clear_nexus(struct sas_task *task) |
| 337 | { |
| 338 | int res = TMF_RESP_FUNC_FAILED; |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 339 | int leftover; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 340 | struct asd_ascb *tascb = task->lldd_task; |
| 341 | unsigned long flags; |
| 342 | |
| 343 | ASD_DPRINTK("task not done, clearing nexus\n"); |
| 344 | if (tascb->tag_valid) |
| 345 | res = asd_clear_nexus_tag(task); |
| 346 | else |
| 347 | res = asd_clear_nexus_index(task); |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 348 | leftover = wait_for_completion_timeout(&tascb->completion, |
| 349 | AIC94XX_SCB_TIMEOUT); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 350 | ASD_DPRINTK("came back from clear nexus\n"); |
| 351 | spin_lock_irqsave(&task->task_state_lock, flags); |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 352 | if (leftover < 1) |
| 353 | res = TMF_RESP_FUNC_FAILED; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 354 | if (task->task_state_flags & SAS_TASK_STATE_DONE) |
| 355 | res = TMF_RESP_FUNC_COMPLETE; |
| 356 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 357 | |
| 358 | return res; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * asd_abort_task -- ABORT TASK TMF |
| 363 | * @task: the task to be aborted |
| 364 | * |
| 365 | * Before calling ABORT TASK the task state flags should be ORed with |
| 366 | * SAS_TASK_STATE_ABORTED (unless SAS_TASK_STATE_DONE is set) under |
| 367 | * the task_state_lock IRQ spinlock, then ABORT TASK *must* be called. |
| 368 | * |
| 369 | * Implements the ABORT TASK TMF, I_T_L_Q nexus. |
| 370 | * Returns: SAS TMF responses (see sas_task.h), |
| 371 | * -ENOMEM, |
| 372 | * -SAS_QUEUE_FULL. |
| 373 | * |
| 374 | * When ABORT TASK returns, the caller of ABORT TASK checks first the |
| 375 | * task->task_state_flags, and then the return value of ABORT TASK. |
| 376 | * |
| 377 | * If the task has task state bit SAS_TASK_STATE_DONE set, then the |
| 378 | * task was completed successfully prior to it being aborted. The |
| 379 | * caller of ABORT TASK has responsibility to call task->task_done() |
| 380 | * xor free the task, depending on their framework. The return code |
| 381 | * is TMF_RESP_FUNC_FAILED in this case. |
| 382 | * |
| 383 | * Else the SAS_TASK_STATE_DONE bit is not set, |
| 384 | * If the return code is TMF_RESP_FUNC_COMPLETE, then |
| 385 | * the task was aborted successfully. The caller of |
| 386 | * ABORT TASK has responsibility to call task->task_done() |
| 387 | * to finish the task, xor free the task depending on their |
| 388 | * framework. |
| 389 | * else |
| 390 | * the ABORT TASK returned some kind of error. The task |
| 391 | * was _not_ cancelled. Nothing can be assumed. |
| 392 | * The caller of ABORT TASK may wish to retry. |
| 393 | */ |
| 394 | int asd_abort_task(struct sas_task *task) |
| 395 | { |
| 396 | struct asd_ascb *tascb = task->lldd_task; |
| 397 | struct asd_ha_struct *asd_ha = tascb->ha; |
| 398 | int res = 1; |
| 399 | unsigned long flags; |
| 400 | struct asd_ascb *ascb = NULL; |
| 401 | struct scb *scb; |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 402 | int leftover; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 403 | |
| 404 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 405 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 406 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 407 | res = TMF_RESP_FUNC_COMPLETE; |
| 408 | ASD_DPRINTK("%s: task 0x%p done\n", __FUNCTION__, task); |
| 409 | goto out_done; |
| 410 | } |
| 411 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 412 | |
| 413 | ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL); |
| 414 | if (!ascb) |
| 415 | return -ENOMEM; |
| 416 | scb = ascb->scb; |
| 417 | |
Boaz Harrosh | 90b0c41 | 2008-02-06 15:38:33 +0200 | [diff] [blame] | 418 | scb->header.opcode = SCB_ABORT_TASK; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 419 | |
| 420 | switch (task->task_proto) { |
Darrick J. Wong | 5929faf | 2007-11-05 11:51:17 -0800 | [diff] [blame] | 421 | case SAS_PROTOCOL_SATA: |
| 422 | case SAS_PROTOCOL_STP: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 423 | scb->abort_task.proto_conn_rate = (1 << 5); /* STP */ |
| 424 | break; |
Darrick J. Wong | 5929faf | 2007-11-05 11:51:17 -0800 | [diff] [blame] | 425 | case SAS_PROTOCOL_SSP: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 426 | scb->abort_task.proto_conn_rate = (1 << 4); /* SSP */ |
| 427 | scb->abort_task.proto_conn_rate |= task->dev->linkrate; |
| 428 | break; |
Darrick J. Wong | 5929faf | 2007-11-05 11:51:17 -0800 | [diff] [blame] | 429 | case SAS_PROTOCOL_SMP: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | break; |
| 433 | } |
| 434 | |
Darrick J. Wong | 5929faf | 2007-11-05 11:51:17 -0800 | [diff] [blame] | 435 | if (task->task_proto == SAS_PROTOCOL_SSP) { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 436 | scb->abort_task.ssp_frame.frame_type = SSP_TASK; |
| 437 | memcpy(scb->abort_task.ssp_frame.hashed_dest_addr, |
| 438 | task->dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE); |
| 439 | memcpy(scb->abort_task.ssp_frame.hashed_src_addr, |
| 440 | task->dev->port->ha->hashed_sas_addr, |
| 441 | HASHED_SAS_ADDR_SIZE); |
| 442 | scb->abort_task.ssp_frame.tptt = cpu_to_be16(0xFFFF); |
| 443 | |
| 444 | memcpy(scb->abort_task.ssp_task.lun, task->ssp_task.LUN, 8); |
| 445 | scb->abort_task.ssp_task.tmf = TMF_ABORT_TASK; |
| 446 | scb->abort_task.ssp_task.tag = cpu_to_be16(0xFFFF); |
| 447 | } |
| 448 | |
| 449 | scb->abort_task.sister_scb = cpu_to_le16(0xFFFF); |
| 450 | scb->abort_task.conn_handle = cpu_to_le16( |
| 451 | (u16)(unsigned long)task->dev->lldd_dev); |
| 452 | scb->abort_task.retry_count = 1; |
| 453 | scb->abort_task.index = cpu_to_le16((u16)tascb->tc_index); |
| 454 | scb->abort_task.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST); |
| 455 | |
| 456 | res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete, |
| 457 | asd_tmf_timedout); |
| 458 | if (res) |
| 459 | goto out; |
| 460 | wait_for_completion(&ascb->completion); |
| 461 | ASD_DPRINTK("tmf came back\n"); |
| 462 | |
| 463 | res = (int) (unsigned long) ascb->uldd_task; |
| 464 | tascb->tag = ascb->tag; |
| 465 | tascb->tag_valid = ascb->tag_valid; |
| 466 | |
| 467 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 468 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 469 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 470 | res = TMF_RESP_FUNC_COMPLETE; |
| 471 | ASD_DPRINTK("%s: task 0x%p done\n", __FUNCTION__, task); |
| 472 | goto out_done; |
| 473 | } |
| 474 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 475 | |
| 476 | switch (res) { |
| 477 | /* The task to be aborted has been sent to the device. |
| 478 | * We got a Response IU for the ABORT TASK TMF. */ |
| 479 | case TC_NO_ERROR + 0xFF00: |
| 480 | case TMF_RESP_FUNC_COMPLETE: |
| 481 | case TMF_RESP_FUNC_FAILED: |
| 482 | res = asd_clear_nexus(task); |
| 483 | break; |
| 484 | case TMF_RESP_INVALID_FRAME: |
| 485 | case TMF_RESP_OVERLAPPED_TAG: |
| 486 | case TMF_RESP_FUNC_ESUPP: |
| 487 | case TMF_RESP_NO_LUN: |
| 488 | goto out_done; break; |
| 489 | } |
| 490 | /* In the following we assume that the managing layer |
| 491 | * will _never_ make a mistake, when issuing ABORT TASK. |
| 492 | */ |
| 493 | switch (res) { |
| 494 | default: |
| 495 | res = asd_clear_nexus(task); |
| 496 | /* fallthrough */ |
| 497 | case TC_NO_ERROR + 0xFF00: |
| 498 | case TMF_RESP_FUNC_COMPLETE: |
| 499 | break; |
| 500 | /* The task hasn't been sent to the device xor we never got |
| 501 | * a (sane) Response IU for the ABORT TASK TMF. |
| 502 | */ |
| 503 | case TF_NAK_RECV + 0xFF00: |
| 504 | res = TMF_RESP_INVALID_FRAME; |
| 505 | break; |
| 506 | case TF_TMF_TASK_DONE + 0xFF00: /* done but not reported yet */ |
| 507 | res = TMF_RESP_FUNC_FAILED; |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 508 | leftover = wait_for_completion_timeout(&tascb->completion, |
| 509 | AIC94XX_SCB_TIMEOUT); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 510 | spin_lock_irqsave(&task->task_state_lock, flags); |
Darrick J. Wong | 8fdcf86 | 2007-05-16 14:01:48 -0700 | [diff] [blame] | 511 | if (leftover < 1) |
| 512 | res = TMF_RESP_FUNC_FAILED; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 513 | if (task->task_state_flags & SAS_TASK_STATE_DONE) |
| 514 | res = TMF_RESP_FUNC_COMPLETE; |
| 515 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 516 | goto out_done; |
| 517 | case TF_TMF_NO_TAG + 0xFF00: |
| 518 | case TF_TMF_TAG_FREE + 0xFF00: /* the tag is in the free list */ |
| 519 | case TF_TMF_NO_CONN_HANDLE + 0xFF00: /* no such device */ |
| 520 | res = TMF_RESP_FUNC_COMPLETE; |
| 521 | goto out_done; |
| 522 | case TF_TMF_NO_CTX + 0xFF00: /* not in seq, or proto != SSP */ |
| 523 | res = TMF_RESP_FUNC_ESUPP; |
| 524 | goto out; |
| 525 | } |
| 526 | out_done: |
| 527 | if (res == TMF_RESP_FUNC_COMPLETE) { |
| 528 | task->lldd_task = NULL; |
| 529 | mb(); |
| 530 | asd_ascb_free(tascb); |
| 531 | } |
| 532 | out: |
| 533 | asd_ascb_free(ascb); |
| 534 | ASD_DPRINTK("task 0x%p aborted, res: 0x%x\n", task, res); |
| 535 | return res; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * asd_initiate_ssp_tmf -- send a TMF to an I_T_L or I_T_L_Q nexus |
| 540 | * @dev: pointer to struct domain_device of interest |
| 541 | * @lun: pointer to u8[8] which is the LUN |
| 542 | * @tmf: the TMF to be performed (see sas_task.h or the SAS spec) |
| 543 | * @index: the transaction context of the task to be queried if QT TMF |
| 544 | * |
| 545 | * This function is used to send ABORT TASK SET, CLEAR ACA, |
| 546 | * CLEAR TASK SET, LU RESET and QUERY TASK TMFs. |
| 547 | * |
| 548 | * No SCBs should be queued to the I_T_L nexus when this SCB is |
| 549 | * pending. |
| 550 | * |
| 551 | * Returns: TMF response code (see sas_task.h or the SAS spec) |
| 552 | */ |
| 553 | static int asd_initiate_ssp_tmf(struct domain_device *dev, u8 *lun, |
| 554 | int tmf, int index) |
| 555 | { |
| 556 | struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha; |
| 557 | struct asd_ascb *ascb; |
| 558 | int res = 1; |
| 559 | struct scb *scb; |
| 560 | |
Darrick J. Wong | 5929faf | 2007-11-05 11:51:17 -0800 | [diff] [blame] | 561 | if (!(dev->tproto & SAS_PROTOCOL_SSP)) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 562 | return TMF_RESP_FUNC_ESUPP; |
| 563 | |
| 564 | ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL); |
| 565 | if (!ascb) |
| 566 | return -ENOMEM; |
| 567 | scb = ascb->scb; |
| 568 | |
| 569 | if (tmf == TMF_QUERY_TASK) |
| 570 | scb->header.opcode = QUERY_SSP_TASK; |
| 571 | else |
| 572 | scb->header.opcode = INITIATE_SSP_TMF; |
| 573 | |
| 574 | scb->ssp_tmf.proto_conn_rate = (1 << 4); /* SSP */ |
| 575 | scb->ssp_tmf.proto_conn_rate |= dev->linkrate; |
| 576 | /* SSP frame header */ |
| 577 | scb->ssp_tmf.ssp_frame.frame_type = SSP_TASK; |
| 578 | memcpy(scb->ssp_tmf.ssp_frame.hashed_dest_addr, |
| 579 | dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE); |
| 580 | memcpy(scb->ssp_tmf.ssp_frame.hashed_src_addr, |
| 581 | dev->port->ha->hashed_sas_addr, HASHED_SAS_ADDR_SIZE); |
| 582 | scb->ssp_tmf.ssp_frame.tptt = cpu_to_be16(0xFFFF); |
| 583 | /* SSP Task IU */ |
| 584 | memcpy(scb->ssp_tmf.ssp_task.lun, lun, 8); |
| 585 | scb->ssp_tmf.ssp_task.tmf = tmf; |
| 586 | |
| 587 | scb->ssp_tmf.sister_scb = cpu_to_le16(0xFFFF); |
| 588 | scb->ssp_tmf.conn_handle= cpu_to_le16((u16)(unsigned long) |
| 589 | dev->lldd_dev); |
| 590 | scb->ssp_tmf.retry_count = 1; |
| 591 | scb->ssp_tmf.itnl_to = cpu_to_le16(ITNL_TIMEOUT_CONST); |
| 592 | if (tmf == TMF_QUERY_TASK) |
| 593 | scb->ssp_tmf.index = cpu_to_le16(index); |
| 594 | |
| 595 | res = asd_enqueue_internal(ascb, asd_tmf_tasklet_complete, |
| 596 | asd_tmf_timedout); |
| 597 | if (res) |
| 598 | goto out_err; |
| 599 | wait_for_completion(&ascb->completion); |
| 600 | res = (int) (unsigned long) ascb->uldd_task; |
| 601 | |
| 602 | switch (res) { |
| 603 | case TC_NO_ERROR + 0xFF00: |
| 604 | res = TMF_RESP_FUNC_COMPLETE; |
| 605 | break; |
| 606 | case TF_NAK_RECV + 0xFF00: |
| 607 | res = TMF_RESP_INVALID_FRAME; |
| 608 | break; |
| 609 | case TF_TMF_TASK_DONE + 0xFF00: |
| 610 | res = TMF_RESP_FUNC_FAILED; |
| 611 | break; |
| 612 | case TF_TMF_NO_TAG + 0xFF00: |
| 613 | case TF_TMF_TAG_FREE + 0xFF00: /* the tag is in the free list */ |
| 614 | case TF_TMF_NO_CONN_HANDLE + 0xFF00: /* no such device */ |
| 615 | res = TMF_RESP_FUNC_COMPLETE; |
| 616 | break; |
| 617 | case TF_TMF_NO_CTX + 0xFF00: /* not in seq, or proto != SSP */ |
| 618 | res = TMF_RESP_FUNC_ESUPP; |
| 619 | break; |
| 620 | default: |
Darrick J. Wong | 058e2c4 | 2007-01-29 23:48:22 -0800 | [diff] [blame] | 621 | /* Allow TMF response codes to propagate upwards */ |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 622 | break; |
| 623 | } |
| 624 | out_err: |
| 625 | asd_ascb_free(ascb); |
| 626 | return res; |
| 627 | } |
| 628 | |
| 629 | int asd_abort_task_set(struct domain_device *dev, u8 *lun) |
| 630 | { |
| 631 | int res = asd_initiate_ssp_tmf(dev, lun, TMF_ABORT_TASK_SET, 0); |
| 632 | |
| 633 | if (res == TMF_RESP_FUNC_COMPLETE) |
| 634 | asd_clear_nexus_I_T_L(dev, lun); |
| 635 | return res; |
| 636 | } |
| 637 | |
| 638 | int asd_clear_aca(struct domain_device *dev, u8 *lun) |
| 639 | { |
| 640 | int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_ACA, 0); |
| 641 | |
| 642 | if (res == TMF_RESP_FUNC_COMPLETE) |
| 643 | asd_clear_nexus_I_T_L(dev, lun); |
| 644 | return res; |
| 645 | } |
| 646 | |
| 647 | int asd_clear_task_set(struct domain_device *dev, u8 *lun) |
| 648 | { |
| 649 | int res = asd_initiate_ssp_tmf(dev, lun, TMF_CLEAR_TASK_SET, 0); |
| 650 | |
| 651 | if (res == TMF_RESP_FUNC_COMPLETE) |
| 652 | asd_clear_nexus_I_T_L(dev, lun); |
| 653 | return res; |
| 654 | } |
| 655 | |
| 656 | int asd_lu_reset(struct domain_device *dev, u8 *lun) |
| 657 | { |
| 658 | int res = asd_initiate_ssp_tmf(dev, lun, TMF_LU_RESET, 0); |
| 659 | |
| 660 | if (res == TMF_RESP_FUNC_COMPLETE) |
| 661 | asd_clear_nexus_I_T_L(dev, lun); |
| 662 | return res; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * asd_query_task -- send a QUERY TASK TMF to an I_T_L_Q nexus |
| 667 | * task: pointer to sas_task struct of interest |
| 668 | * |
| 669 | * Returns: TMF_RESP_FUNC_COMPLETE if the task is not in the task set, |
| 670 | * or TMF_RESP_FUNC_SUCC if the task is in the task set. |
| 671 | * |
| 672 | * Normally the management layer sets the task to aborted state, |
| 673 | * and then calls query task and then abort task. |
| 674 | */ |
| 675 | int asd_query_task(struct sas_task *task) |
| 676 | { |
| 677 | struct asd_ascb *ascb = task->lldd_task; |
| 678 | int index; |
| 679 | |
| 680 | if (ascb) { |
| 681 | index = ascb->tc_index; |
| 682 | return asd_initiate_ssp_tmf(task->dev, task->ssp_task.LUN, |
| 683 | TMF_QUERY_TASK, index); |
| 684 | } |
| 685 | return TMF_RESP_FUNC_COMPLETE; |
| 686 | } |