James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Serial Attached SCSI (SAS) class SCSI Host glue. |
| 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 program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 22 | * USA |
| 23 | * |
| 24 | */ |
| 25 | |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 26 | #include <linux/kthread.h> |
| 27 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 28 | #include "sas_internal.h" |
| 29 | |
| 30 | #include <scsi/scsi_host.h> |
| 31 | #include <scsi/scsi_device.h> |
| 32 | #include <scsi/scsi_tcq.h> |
| 33 | #include <scsi/scsi.h> |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 34 | #include <scsi/scsi_eh.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 35 | #include <scsi/scsi_transport.h> |
| 36 | #include <scsi/scsi_transport_sas.h> |
Darrick J. Wong | 338ec57 | 2006-10-18 14:43:37 -0700 | [diff] [blame] | 37 | #include <scsi/sas_ata.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 38 | #include "../scsi_sas_internal.h" |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 39 | #include "../scsi_transport_api.h" |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 40 | #include "../scsi_priv.h" |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 41 | |
| 42 | #include <linux/err.h> |
| 43 | #include <linux/blkdev.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 44 | #include <linux/freezer.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 45 | #include <linux/scatterlist.h> |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 46 | #include <linux/libata.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 47 | |
| 48 | /* ---------- SCSI Host glue ---------- */ |
| 49 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 50 | static void sas_scsi_task_done(struct sas_task *task) |
| 51 | { |
| 52 | struct task_status_struct *ts = &task->task_status; |
| 53 | struct scsi_cmnd *sc = task->uldd_task; |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 54 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 55 | unsigned ts_flags = task->task_state_flags; |
| 56 | int hs = 0, stat = 0; |
| 57 | |
| 58 | if (unlikely(!sc)) { |
| 59 | SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n"); |
| 60 | list_del_init(&task->list); |
| 61 | sas_free_task(task); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (ts->resp == SAS_TASK_UNDELIVERED) { |
| 66 | /* transport error */ |
| 67 | hs = DID_NO_CONNECT; |
| 68 | } else { /* ts->resp == SAS_TASK_COMPLETE */ |
| 69 | /* task delivered, what happened afterwards? */ |
| 70 | switch (ts->stat) { |
| 71 | case SAS_DEV_NO_RESPONSE: |
| 72 | case SAS_INTERRUPTED: |
| 73 | case SAS_PHY_DOWN: |
| 74 | case SAS_NAK_R_ERR: |
| 75 | case SAS_OPEN_TO: |
| 76 | hs = DID_NO_CONNECT; |
| 77 | break; |
| 78 | case SAS_DATA_UNDERRUN: |
FUJITA Tomonori | c13e556 | 2007-05-26 02:46:37 +0900 | [diff] [blame] | 79 | scsi_set_resid(sc, ts->residual); |
| 80 | if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 81 | hs = DID_ERROR; |
| 82 | break; |
| 83 | case SAS_DATA_OVERRUN: |
| 84 | hs = DID_ERROR; |
| 85 | break; |
| 86 | case SAS_QUEUE_FULL: |
| 87 | hs = DID_SOFT_ERROR; /* retry */ |
| 88 | break; |
| 89 | case SAS_DEVICE_UNKNOWN: |
| 90 | hs = DID_BAD_TARGET; |
| 91 | break; |
| 92 | case SAS_SG_ERR: |
| 93 | hs = DID_PARITY; |
| 94 | break; |
| 95 | case SAS_OPEN_REJECT: |
| 96 | if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY) |
| 97 | hs = DID_SOFT_ERROR; /* retry */ |
| 98 | else |
| 99 | hs = DID_ERROR; |
| 100 | break; |
| 101 | case SAS_PROTO_RESPONSE: |
| 102 | SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP " |
| 103 | "task; please report this\n", |
| 104 | task->dev->port->ha->sas_ha_name); |
| 105 | break; |
| 106 | case SAS_ABORTED_TASK: |
| 107 | hs = DID_ABORT; |
| 108 | break; |
| 109 | case SAM_CHECK_COND: |
| 110 | memcpy(sc->sense_buffer, ts->buf, |
FUJITA Tomonori | cc75e8a | 2008-01-13 02:20:18 +0900 | [diff] [blame] | 111 | min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size)); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 112 | stat = SAM_CHECK_COND; |
| 113 | break; |
| 114 | default: |
| 115 | stat = ts->stat; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | ASSIGN_SAS_TASK(sc, NULL); |
| 120 | sc->result = (hs << 16) | stat; |
| 121 | list_del_init(&task->list); |
| 122 | sas_free_task(task); |
| 123 | /* This is very ugly but this is how SCSI Core works. */ |
| 124 | if (ts_flags & SAS_TASK_STATE_ABORTED) |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 125 | scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 126 | else |
| 127 | sc->scsi_done(sc); |
| 128 | } |
| 129 | |
| 130 | static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd) |
| 131 | { |
| 132 | enum task_attribute ta = TASK_ATTR_SIMPLE; |
| 133 | if (cmd->request && blk_rq_tagged(cmd->request)) { |
| 134 | if (cmd->device->ordered_tags && |
Jeff Garzik | 1bdfd55 | 2006-09-30 21:28:22 -0400 | [diff] [blame] | 135 | (cmd->request->cmd_flags & REQ_HARDBARRIER)) |
FUJITA Tomonori | 63bb1bf | 2007-01-28 01:19:41 +0900 | [diff] [blame] | 136 | ta = TASK_ATTR_ORDERED; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 137 | } |
| 138 | return ta; |
| 139 | } |
| 140 | |
| 141 | static struct sas_task *sas_create_task(struct scsi_cmnd *cmd, |
| 142 | struct domain_device *dev, |
Al Viro | 3cc2754 | 2006-09-25 02:55:40 +0100 | [diff] [blame] | 143 | gfp_t gfp_flags) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 144 | { |
| 145 | struct sas_task *task = sas_alloc_task(gfp_flags); |
| 146 | struct scsi_lun lun; |
| 147 | |
| 148 | if (!task) |
| 149 | return NULL; |
| 150 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 151 | task->uldd_task = cmd; |
| 152 | ASSIGN_SAS_TASK(cmd, task); |
| 153 | |
| 154 | task->dev = dev; |
| 155 | task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */ |
| 156 | |
| 157 | task->ssp_task.retry_count = 1; |
| 158 | int_to_scsilun(cmd->device->lun, &lun); |
| 159 | memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8); |
| 160 | task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd); |
| 161 | memcpy(task->ssp_task.cdb, cmd->cmnd, 16); |
| 162 | |
FUJITA Tomonori | c13e556 | 2007-05-26 02:46:37 +0900 | [diff] [blame] | 163 | task->scatter = scsi_sglist(cmd); |
| 164 | task->num_scatter = scsi_sg_count(cmd); |
| 165 | task->total_xfer_len = scsi_bufflen(cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 166 | task->data_dir = cmd->sc_data_direction; |
| 167 | |
| 168 | task->task_done = sas_scsi_task_done; |
| 169 | |
| 170 | return task; |
| 171 | } |
| 172 | |
Darrick J. Wong | 338ec57 | 2006-10-18 14:43:37 -0700 | [diff] [blame] | 173 | int sas_queue_up(struct sas_task *task) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 174 | { |
| 175 | struct sas_ha_struct *sas_ha = task->dev->port->ha; |
| 176 | struct scsi_core *core = &sas_ha->core; |
| 177 | unsigned long flags; |
| 178 | LIST_HEAD(list); |
| 179 | |
| 180 | spin_lock_irqsave(&core->task_queue_lock, flags); |
| 181 | if (sas_ha->lldd_queue_size < core->task_queue_size + 1) { |
| 182 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
| 183 | return -SAS_QUEUE_FULL; |
| 184 | } |
| 185 | list_add_tail(&task->list, &core->task_queue); |
| 186 | core->task_queue_size += 1; |
| 187 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 188 | wake_up_process(core->queue_thread); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * sas_queuecommand -- Enqueue a command for processing |
| 195 | * @parameters: See SCSI Core documentation |
| 196 | * |
| 197 | * Note: XXX: Remove the host unlock/lock pair when SCSI Core can |
| 198 | * call us without holding an IRQ spinlock... |
| 199 | */ |
| 200 | int sas_queuecommand(struct scsi_cmnd *cmd, |
| 201 | void (*scsi_done)(struct scsi_cmnd *)) |
Darrick J. Wong | 8ee2402 | 2007-11-05 11:52:14 -0800 | [diff] [blame] | 202 | __releases(host->host_lock) |
| 203 | __acquires(dev->sata_dev.ap->lock) |
| 204 | __releases(dev->sata_dev.ap->lock) |
| 205 | __acquires(host->host_lock) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 206 | { |
| 207 | int res = 0; |
| 208 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
| 209 | struct Scsi_Host *host = cmd->device->host; |
| 210 | struct sas_internal *i = to_sas_internal(host->transportt); |
| 211 | |
| 212 | spin_unlock_irq(host->host_lock); |
| 213 | |
| 214 | { |
| 215 | struct sas_ha_struct *sas_ha = dev->port->ha; |
| 216 | struct sas_task *task; |
| 217 | |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 218 | if (dev_is_sata(dev)) { |
Darrick J. Wong | 3eb7a51 | 2007-01-30 01:18:35 -0800 | [diff] [blame] | 219 | unsigned long flags; |
| 220 | |
| 221 | spin_lock_irqsave(dev->sata_dev.ap->lock, flags); |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 222 | res = ata_sas_queuecmd(cmd, scsi_done, |
| 223 | dev->sata_dev.ap); |
Darrick J. Wong | 3eb7a51 | 2007-01-30 01:18:35 -0800 | [diff] [blame] | 224 | spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags); |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 225 | goto out; |
| 226 | } |
| 227 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 228 | res = -ENOMEM; |
| 229 | task = sas_create_task(cmd, dev, GFP_ATOMIC); |
| 230 | if (!task) |
| 231 | goto out; |
| 232 | |
| 233 | cmd->scsi_done = scsi_done; |
| 234 | /* Queue up, Direct Mode or Task Collector Mode. */ |
| 235 | if (sas_ha->lldd_max_execute_num < 2) |
| 236 | res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC); |
| 237 | else |
| 238 | res = sas_queue_up(task); |
| 239 | |
| 240 | /* Examine */ |
| 241 | if (res) { |
| 242 | SAS_DPRINTK("lldd_execute_task returned: %d\n", res); |
| 243 | ASSIGN_SAS_TASK(cmd, NULL); |
| 244 | sas_free_task(task); |
| 245 | if (res == -SAS_QUEUE_FULL) { |
| 246 | cmd->result = DID_SOFT_ERROR << 16; /* retry */ |
| 247 | res = 0; |
| 248 | scsi_done(cmd); |
| 249 | } |
| 250 | goto out; |
| 251 | } |
| 252 | } |
| 253 | out: |
| 254 | spin_lock_irq(host->host_lock); |
| 255 | return res; |
| 256 | } |
| 257 | |
| 258 | static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd) |
| 259 | { |
| 260 | struct scsi_cmnd *cmd, *n; |
| 261 | |
| 262 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { |
| 263 | if (cmd == my_cmd) |
| 264 | list_del_init(&cmd->eh_entry); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void sas_scsi_clear_queue_I_T(struct list_head *error_q, |
| 269 | struct domain_device *dev) |
| 270 | { |
| 271 | struct scsi_cmnd *cmd, *n; |
| 272 | |
| 273 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { |
| 274 | struct domain_device *x = cmd_to_domain_dev(cmd); |
| 275 | |
| 276 | if (x == dev) |
| 277 | list_del_init(&cmd->eh_entry); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static void sas_scsi_clear_queue_port(struct list_head *error_q, |
| 282 | struct asd_sas_port *port) |
| 283 | { |
| 284 | struct scsi_cmnd *cmd, *n; |
| 285 | |
| 286 | list_for_each_entry_safe(cmd, n, error_q, eh_entry) { |
| 287 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
| 288 | struct asd_sas_port *x = dev->port; |
| 289 | |
| 290 | if (x == port) |
| 291 | list_del_init(&cmd->eh_entry); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | enum task_disposition { |
| 296 | TASK_IS_DONE, |
| 297 | TASK_IS_ABORTED, |
| 298 | TASK_IS_AT_LU, |
| 299 | TASK_IS_NOT_AT_LU, |
Darrick J. Wong | 02cd743 | 2007-01-11 14:15:46 -0800 | [diff] [blame] | 300 | TASK_ABORT_FAILED, |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | static enum task_disposition sas_scsi_find_task(struct sas_task *task) |
| 304 | { |
| 305 | struct sas_ha_struct *ha = task->dev->port->ha; |
| 306 | unsigned long flags; |
| 307 | int i, res; |
| 308 | struct sas_internal *si = |
| 309 | to_sas_internal(task->dev->port->ha->core.shost->transportt); |
| 310 | |
| 311 | if (ha->lldd_max_execute_num > 1) { |
| 312 | struct scsi_core *core = &ha->core; |
| 313 | struct sas_task *t, *n; |
| 314 | |
| 315 | spin_lock_irqsave(&core->task_queue_lock, flags); |
| 316 | list_for_each_entry_safe(t, n, &core->task_queue, list) { |
| 317 | if (task == t) { |
| 318 | list_del_init(&t->list); |
| 319 | spin_unlock_irqrestore(&core->task_queue_lock, |
| 320 | flags); |
| 321 | SAS_DPRINTK("%s: task 0x%p aborted from " |
| 322 | "task_queue\n", |
| 323 | __FUNCTION__, task); |
| 324 | return TASK_IS_ABORTED; |
| 325 | } |
| 326 | } |
| 327 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
| 328 | } |
| 329 | |
| 330 | for (i = 0; i < 5; i++) { |
| 331 | SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task); |
| 332 | res = si->dft->lldd_abort_task(task); |
| 333 | |
| 334 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 335 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 336 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 337 | SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, |
| 338 | task); |
| 339 | return TASK_IS_DONE; |
| 340 | } |
| 341 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 342 | |
| 343 | if (res == TMF_RESP_FUNC_COMPLETE) { |
| 344 | SAS_DPRINTK("%s: task 0x%p is aborted\n", |
| 345 | __FUNCTION__, task); |
| 346 | return TASK_IS_ABORTED; |
| 347 | } else if (si->dft->lldd_query_task) { |
| 348 | SAS_DPRINTK("%s: querying task 0x%p\n", |
| 349 | __FUNCTION__, task); |
| 350 | res = si->dft->lldd_query_task(task); |
Darrick J. Wong | 02cd743 | 2007-01-11 14:15:46 -0800 | [diff] [blame] | 351 | switch (res) { |
| 352 | case TMF_RESP_FUNC_SUCC: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 353 | SAS_DPRINTK("%s: task 0x%p at LU\n", |
| 354 | __FUNCTION__, task); |
| 355 | return TASK_IS_AT_LU; |
Darrick J. Wong | 02cd743 | 2007-01-11 14:15:46 -0800 | [diff] [blame] | 356 | case TMF_RESP_FUNC_COMPLETE: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 357 | SAS_DPRINTK("%s: task 0x%p not at LU\n", |
| 358 | __FUNCTION__, task); |
| 359 | return TASK_IS_NOT_AT_LU; |
Darrick J. Wong | 02cd743 | 2007-01-11 14:15:46 -0800 | [diff] [blame] | 360 | case TMF_RESP_FUNC_FAILED: |
| 361 | SAS_DPRINTK("%s: task 0x%p failed to abort\n", |
| 362 | __FUNCTION__, task); |
| 363 | return TASK_ABORT_FAILED; |
| 364 | } |
| 365 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | return res; |
| 369 | } |
| 370 | |
| 371 | static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd) |
| 372 | { |
| 373 | int res = TMF_RESP_FUNC_FAILED; |
| 374 | struct scsi_lun lun; |
| 375 | struct sas_internal *i = |
| 376 | to_sas_internal(dev->port->ha->core.shost->transportt); |
| 377 | |
| 378 | int_to_scsilun(cmd->device->lun, &lun); |
| 379 | |
| 380 | SAS_DPRINTK("eh: device %llx LUN %x has the task\n", |
| 381 | SAS_ADDR(dev->sas_addr), |
| 382 | cmd->device->lun); |
| 383 | |
| 384 | if (i->dft->lldd_abort_task_set) |
| 385 | res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun); |
| 386 | |
| 387 | if (res == TMF_RESP_FUNC_FAILED) { |
| 388 | if (i->dft->lldd_clear_task_set) |
| 389 | res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun); |
| 390 | } |
| 391 | |
| 392 | if (res == TMF_RESP_FUNC_FAILED) { |
| 393 | if (i->dft->lldd_lu_reset) |
| 394 | res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); |
| 395 | } |
| 396 | |
| 397 | return res; |
| 398 | } |
| 399 | |
| 400 | static int sas_recover_I_T(struct domain_device *dev) |
| 401 | { |
| 402 | int res = TMF_RESP_FUNC_FAILED; |
| 403 | struct sas_internal *i = |
| 404 | to_sas_internal(dev->port->ha->core.shost->transportt); |
| 405 | |
| 406 | SAS_DPRINTK("I_T nexus reset for dev %016llx\n", |
| 407 | SAS_ADDR(dev->sas_addr)); |
| 408 | |
| 409 | if (i->dft->lldd_I_T_nexus_reset) |
| 410 | res = i->dft->lldd_I_T_nexus_reset(dev); |
| 411 | |
| 412 | return res; |
| 413 | } |
| 414 | |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 415 | /* Find the sas_phy that's attached to this device */ |
Darrick J. Wong | 8ee2402 | 2007-11-05 11:52:14 -0800 | [diff] [blame] | 416 | static struct sas_phy *find_local_sas_phy(struct domain_device *dev) |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 417 | { |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 418 | struct domain_device *pdev = dev->parent; |
| 419 | struct ex_phy *exphy = NULL; |
| 420 | int i; |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 421 | |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 422 | /* Directly attached device */ |
| 423 | if (!pdev) |
| 424 | return dev->port->phy; |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 425 | |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 426 | /* Otherwise look in the expander */ |
| 427 | for (i = 0; i < pdev->ex_dev.num_phys; i++) |
| 428 | if (!memcmp(dev->sas_addr, |
| 429 | pdev->ex_dev.ex_phy[i].attached_sas_addr, |
| 430 | SAS_ADDR_SIZE)) { |
| 431 | exphy = &pdev->ex_dev.ex_phy[i]; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | BUG_ON(!exphy); |
| 436 | return exphy->phy; |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 439 | /* Attempt to send a LUN reset message to a device */ |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 440 | int sas_eh_device_reset_handler(struct scsi_cmnd *cmd) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 441 | { |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 442 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 443 | struct sas_internal *i = |
| 444 | to_sas_internal(dev->port->ha->core.shost->transportt); |
| 445 | struct scsi_lun lun; |
| 446 | int res; |
| 447 | |
| 448 | int_to_scsilun(cmd->device->lun, &lun); |
| 449 | |
| 450 | if (!i->dft->lldd_lu_reset) |
| 451 | return FAILED; |
| 452 | |
| 453 | res = i->dft->lldd_lu_reset(dev, lun.scsi_lun); |
| 454 | if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) |
| 455 | return SUCCESS; |
| 456 | |
| 457 | return FAILED; |
| 458 | } |
| 459 | |
| 460 | /* Attempt to send a phy (bus) reset */ |
| 461 | int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd) |
| 462 | { |
| 463 | struct domain_device *dev = cmd_to_domain_dev(cmd); |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 464 | struct sas_phy *phy = find_local_sas_phy(dev); |
| 465 | int res; |
| 466 | |
| 467 | res = sas_phy_reset(phy, 1); |
| 468 | if (res) |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 469 | SAS_DPRINTK("Bus reset of %s failed 0x%x\n", |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 470 | kobject_name(&phy->dev.kobj), |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 471 | res); |
| 472 | if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE) |
| 473 | return SUCCESS; |
| 474 | |
| 475 | return FAILED; |
| 476 | } |
| 477 | |
| 478 | /* Try to reset a device */ |
| 479 | static int try_to_reset_cmd_device(struct Scsi_Host *shost, |
| 480 | struct scsi_cmnd *cmd) |
| 481 | { |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 482 | int res; |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 483 | |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 484 | if (!shost->hostt->eh_device_reset_handler) |
| 485 | goto try_bus_reset; |
| 486 | |
| 487 | res = shost->hostt->eh_device_reset_handler(cmd); |
| 488 | if (res == SUCCESS) |
| 489 | return res; |
| 490 | |
| 491 | try_bus_reset: |
| 492 | if (shost->hostt->eh_bus_reset_handler) |
| 493 | return shost->hostt->eh_bus_reset_handler(cmd); |
| 494 | |
| 495 | return FAILED; |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int sas_eh_handle_sas_errors(struct Scsi_Host *shost, |
| 499 | struct list_head *work_q, |
| 500 | struct list_head *done_q) |
| 501 | { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 502 | struct scsi_cmnd *cmd, *n; |
| 503 | enum task_disposition res = TASK_IS_DONE; |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 504 | int tmf_resp, need_reset; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 505 | struct sas_internal *i = to_sas_internal(shost->transportt); |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 506 | unsigned long flags; |
| 507 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 508 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 509 | Again: |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 510 | list_for_each_entry_safe(cmd, n, work_q, eh_entry) { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 511 | struct sas_task *task = TO_SAS_TASK(cmd); |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 512 | |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 513 | if (!task) |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 514 | continue; |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 515 | |
| 516 | list_del_init(&cmd->eh_entry); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 517 | |
| 518 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 519 | need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET; |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 520 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 521 | |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 522 | SAS_DPRINTK("trying to find task 0x%p\n", task); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 523 | res = sas_scsi_find_task(task); |
| 524 | |
| 525 | cmd->eh_eflags = 0; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 526 | |
| 527 | switch (res) { |
| 528 | case TASK_IS_DONE: |
| 529 | SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__, |
| 530 | task); |
| 531 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 532 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 533 | try_to_reset_cmd_device(shost, cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 534 | continue; |
| 535 | case TASK_IS_ABORTED: |
| 536 | SAS_DPRINTK("%s: task 0x%p is aborted\n", |
| 537 | __FUNCTION__, task); |
| 538 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 539 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 540 | try_to_reset_cmd_device(shost, cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 541 | continue; |
| 542 | case TASK_IS_AT_LU: |
| 543 | SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task); |
| 544 | tmf_resp = sas_recover_lu(task->dev, cmd); |
| 545 | if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { |
| 546 | SAS_DPRINTK("dev %016llx LU %x is " |
| 547 | "recovered\n", |
| 548 | SAS_ADDR(task->dev), |
| 549 | cmd->device->lun); |
| 550 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 551 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 552 | try_to_reset_cmd_device(shost, cmd); |
| 553 | sas_scsi_clear_queue_lu(work_q, cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 554 | goto Again; |
| 555 | } |
| 556 | /* fallthrough */ |
| 557 | case TASK_IS_NOT_AT_LU: |
Darrick J. Wong | 02cd743 | 2007-01-11 14:15:46 -0800 | [diff] [blame] | 558 | case TASK_ABORT_FAILED: |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 559 | SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n", |
| 560 | task); |
| 561 | tmf_resp = sas_recover_I_T(task->dev); |
| 562 | if (tmf_resp == TMF_RESP_FUNC_COMPLETE) { |
| 563 | SAS_DPRINTK("I_T %016llx recovered\n", |
| 564 | SAS_ADDR(task->dev->sas_addr)); |
| 565 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 566 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 567 | try_to_reset_cmd_device(shost, cmd); |
| 568 | sas_scsi_clear_queue_I_T(work_q, task->dev); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 569 | goto Again; |
| 570 | } |
| 571 | /* Hammer time :-) */ |
| 572 | if (i->dft->lldd_clear_nexus_port) { |
| 573 | struct asd_sas_port *port = task->dev->port; |
| 574 | SAS_DPRINTK("clearing nexus for port:%d\n", |
| 575 | port->id); |
| 576 | res = i->dft->lldd_clear_nexus_port(port); |
| 577 | if (res == TMF_RESP_FUNC_COMPLETE) { |
| 578 | SAS_DPRINTK("clear nexus port:%d " |
| 579 | "succeeded\n", port->id); |
| 580 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 581 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 582 | try_to_reset_cmd_device(shost, cmd); |
| 583 | sas_scsi_clear_queue_port(work_q, |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 584 | port); |
| 585 | goto Again; |
| 586 | } |
| 587 | } |
| 588 | if (i->dft->lldd_clear_nexus_ha) { |
| 589 | SAS_DPRINTK("clear nexus ha\n"); |
| 590 | res = i->dft->lldd_clear_nexus_ha(ha); |
| 591 | if (res == TMF_RESP_FUNC_COMPLETE) { |
| 592 | SAS_DPRINTK("clear nexus ha " |
| 593 | "succeeded\n"); |
| 594 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 595 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 596 | try_to_reset_cmd_device(shost, cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 597 | goto out; |
| 598 | } |
| 599 | } |
| 600 | /* If we are here -- this means that no amount |
| 601 | * of effort could recover from errors. Quite |
| 602 | * possibly the HA just disappeared. |
| 603 | */ |
| 604 | SAS_DPRINTK("error from device %llx, LUN %x " |
| 605 | "couldn't be recovered in any way\n", |
| 606 | SAS_ADDR(task->dev->sas_addr), |
| 607 | cmd->device->lun); |
| 608 | |
| 609 | task->task_done(task); |
Darrick J. Wong | 3ebf692 | 2007-01-11 14:15:17 -0800 | [diff] [blame] | 610 | if (need_reset) |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 611 | try_to_reset_cmd_device(shost, cmd); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 612 | goto clear_q; |
| 613 | } |
| 614 | } |
| 615 | out: |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 616 | return list_empty(work_q); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 617 | clear_q: |
| 618 | SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__); |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 619 | list_for_each_entry_safe(cmd, n, work_q, eh_entry) { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 620 | struct sas_task *task = TO_SAS_TASK(cmd); |
| 621 | list_del_init(&cmd->eh_entry); |
| 622 | task->task_done(task); |
| 623 | } |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 624 | return list_empty(work_q); |
| 625 | } |
| 626 | |
| 627 | void sas_scsi_recover_host(struct Scsi_Host *shost) |
| 628 | { |
| 629 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
| 630 | unsigned long flags; |
| 631 | LIST_HEAD(eh_work_q); |
| 632 | |
| 633 | spin_lock_irqsave(shost->host_lock, flags); |
| 634 | list_splice_init(&shost->eh_cmd_q, &eh_work_q); |
| 635 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 636 | |
| 637 | SAS_DPRINTK("Enter %s\n", __FUNCTION__); |
| 638 | /* |
| 639 | * Deal with commands that still have SAS tasks (i.e. they didn't |
| 640 | * complete via the normal sas_task completion mechanism) |
| 641 | */ |
| 642 | if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q)) |
| 643 | goto out; |
| 644 | |
| 645 | /* |
| 646 | * Now deal with SCSI commands that completed ok but have a an error |
| 647 | * code (and hopefully sense data) attached. This is roughly what |
| 648 | * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any |
| 649 | * command we see here has no sas_task and is thus unknown to the HA. |
| 650 | */ |
| 651 | if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q)) |
| 652 | scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q); |
| 653 | |
| 654 | out: |
| 655 | scsi_eh_flush_done_q(&ha->eh_done_q); |
| 656 | SAS_DPRINTK("--- Exit %s\n", __FUNCTION__); |
| 657 | return; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd) |
| 661 | { |
| 662 | struct sas_task *task = TO_SAS_TASK(cmd); |
| 663 | unsigned long flags; |
| 664 | |
| 665 | if (!task) { |
Darrick J. Wong | 6d4dcd4 | 2007-01-11 14:15:00 -0800 | [diff] [blame] | 666 | cmd->timeout_per_command /= 2; |
| 667 | SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n", |
| 668 | cmd, task, (cmd->timeout_per_command ? |
| 669 | "EH_RESET_TIMER" : "EH_NOT_HANDLED")); |
| 670 | if (!cmd->timeout_per_command) |
| 671 | return EH_NOT_HANDLED; |
| 672 | return EH_RESET_TIMER; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | spin_lock_irqsave(&task->task_state_lock, flags); |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 676 | BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 677 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 678 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 679 | SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n", |
| 680 | cmd, task); |
| 681 | return EH_HANDLED; |
| 682 | } |
Darrick J. Wong | b218a0d | 2007-01-11 14:14:55 -0800 | [diff] [blame] | 683 | if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) { |
| 684 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 685 | SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: " |
| 686 | "EH_RESET_TIMER\n", |
| 687 | cmd, task); |
| 688 | return EH_RESET_TIMER; |
| 689 | } |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 690 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
| 691 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 692 | |
| 693 | SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n", |
| 694 | cmd, task); |
| 695 | |
| 696 | return EH_NOT_HANDLED; |
| 697 | } |
| 698 | |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 699 | int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) |
| 700 | { |
| 701 | struct domain_device *dev = sdev_to_domain_dev(sdev); |
| 702 | |
| 703 | if (dev_is_sata(dev)) |
| 704 | return ata_scsi_ioctl(sdev, cmd, arg); |
| 705 | |
| 706 | return -EINVAL; |
| 707 | } |
| 708 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 709 | struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy) |
| 710 | { |
| 711 | struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent); |
| 712 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
| 713 | struct domain_device *found_dev = NULL; |
| 714 | int i; |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 715 | unsigned long flags; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 716 | |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 717 | spin_lock_irqsave(&ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 718 | for (i = 0; i < ha->num_phys; i++) { |
| 719 | struct asd_sas_port *port = ha->sas_port[i]; |
| 720 | struct domain_device *dev; |
| 721 | |
| 722 | spin_lock(&port->dev_list_lock); |
| 723 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { |
| 724 | if (rphy == dev->rphy) { |
| 725 | found_dev = dev; |
| 726 | spin_unlock(&port->dev_list_lock); |
| 727 | goto found; |
| 728 | } |
| 729 | } |
| 730 | spin_unlock(&port->dev_list_lock); |
| 731 | } |
| 732 | found: |
Darrick J. Wong | 980fa2f | 2007-01-11 14:15:40 -0800 | [diff] [blame] | 733 | spin_unlock_irqrestore(&ha->phy_port_lock, flags); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 734 | |
| 735 | return found_dev; |
| 736 | } |
| 737 | |
| 738 | static inline struct domain_device *sas_find_target(struct scsi_target *starget) |
| 739 | { |
| 740 | struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent); |
| 741 | |
| 742 | return sas_find_dev_by_rphy(rphy); |
| 743 | } |
| 744 | |
| 745 | int sas_target_alloc(struct scsi_target *starget) |
| 746 | { |
| 747 | struct domain_device *found_dev = sas_find_target(starget); |
Darrick J. Wong | 338ec57 | 2006-10-18 14:43:37 -0700 | [diff] [blame] | 748 | int res; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 749 | |
| 750 | if (!found_dev) |
| 751 | return -ENODEV; |
| 752 | |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 753 | if (dev_is_sata(found_dev)) { |
Darrick J. Wong | 338ec57 | 2006-10-18 14:43:37 -0700 | [diff] [blame] | 754 | res = sas_ata_init_host_and_port(found_dev, starget); |
| 755 | if (res) |
| 756 | return res; |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 757 | } |
| 758 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 759 | starget->hostdata = found_dev; |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | #define SAS_DEF_QD 32 |
| 764 | #define SAS_MAX_QD 64 |
| 765 | |
| 766 | int sas_slave_configure(struct scsi_device *scsi_dev) |
| 767 | { |
| 768 | struct domain_device *dev = sdev_to_domain_dev(scsi_dev); |
| 769 | struct sas_ha_struct *sas_ha; |
| 770 | |
| 771 | BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE); |
| 772 | |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 773 | if (dev_is_sata(dev)) { |
| 774 | ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap); |
| 775 | return 0; |
| 776 | } |
| 777 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 778 | sas_ha = dev->port->ha; |
| 779 | |
| 780 | sas_read_port_mode_page(scsi_dev); |
| 781 | |
| 782 | if (scsi_dev->tagged_supported) { |
| 783 | scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG); |
| 784 | scsi_activate_tcq(scsi_dev, SAS_DEF_QD); |
| 785 | } else { |
| 786 | SAS_DPRINTK("device %llx, LUN %x doesn't support " |
| 787 | "TCQ\n", SAS_ADDR(dev->sas_addr), |
| 788 | scsi_dev->lun); |
| 789 | scsi_dev->tagged_supported = 0; |
| 790 | scsi_set_tag_type(scsi_dev, 0); |
| 791 | scsi_deactivate_tcq(scsi_dev, 1); |
| 792 | } |
| 793 | |
Darrick J. Wong | f27708f | 2007-01-26 14:08:58 -0800 | [diff] [blame] | 794 | scsi_dev->allow_restart = 1; |
| 795 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | void sas_slave_destroy(struct scsi_device *scsi_dev) |
| 800 | { |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 801 | struct domain_device *dev = sdev_to_domain_dev(scsi_dev); |
| 802 | |
| 803 | if (dev_is_sata(dev)) |
| 804 | ata_port_disable(dev->sata_dev.ap); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth) |
| 808 | { |
| 809 | int res = min(new_depth, SAS_MAX_QD); |
| 810 | |
| 811 | if (scsi_dev->tagged_supported) |
| 812 | scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), |
| 813 | res); |
| 814 | else { |
| 815 | struct domain_device *dev = sdev_to_domain_dev(scsi_dev); |
| 816 | sas_printk("device %llx LUN %x queue depth changed to 1\n", |
| 817 | SAS_ADDR(dev->sas_addr), |
| 818 | scsi_dev->lun); |
| 819 | scsi_adjust_queue_depth(scsi_dev, 0, 1); |
| 820 | res = 1; |
| 821 | } |
| 822 | |
| 823 | return res; |
| 824 | } |
| 825 | |
| 826 | int sas_change_queue_type(struct scsi_device *scsi_dev, int qt) |
| 827 | { |
| 828 | if (!scsi_dev->tagged_supported) |
| 829 | return 0; |
| 830 | |
| 831 | scsi_deactivate_tcq(scsi_dev, 1); |
| 832 | |
| 833 | scsi_set_tag_type(scsi_dev, qt); |
| 834 | scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth); |
| 835 | |
| 836 | return qt; |
| 837 | } |
| 838 | |
| 839 | int sas_bios_param(struct scsi_device *scsi_dev, |
| 840 | struct block_device *bdev, |
| 841 | sector_t capacity, int *hsc) |
| 842 | { |
| 843 | hsc[0] = 255; |
| 844 | hsc[1] = 63; |
| 845 | sector_div(capacity, 255*63); |
| 846 | hsc[2] = capacity; |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | /* ---------- Task Collector Thread implementation ---------- */ |
| 852 | |
| 853 | static void sas_queue(struct sas_ha_struct *sas_ha) |
| 854 | { |
| 855 | struct scsi_core *core = &sas_ha->core; |
| 856 | unsigned long flags; |
| 857 | LIST_HEAD(q); |
| 858 | int can_queue; |
| 859 | int res; |
| 860 | struct sas_internal *i = to_sas_internal(core->shost->transportt); |
| 861 | |
| 862 | spin_lock_irqsave(&core->task_queue_lock, flags); |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 863 | while (!kthread_should_stop() && |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 864 | !list_empty(&core->task_queue)) { |
| 865 | |
| 866 | can_queue = sas_ha->lldd_queue_size - core->task_queue_size; |
| 867 | if (can_queue >= 0) { |
| 868 | can_queue = core->task_queue_size; |
| 869 | list_splice_init(&core->task_queue, &q); |
| 870 | } else { |
| 871 | struct list_head *a, *n; |
| 872 | |
| 873 | can_queue = sas_ha->lldd_queue_size; |
| 874 | list_for_each_safe(a, n, &core->task_queue) { |
| 875 | list_move_tail(a, &q); |
| 876 | if (--can_queue == 0) |
| 877 | break; |
| 878 | } |
| 879 | can_queue = sas_ha->lldd_queue_size; |
| 880 | } |
| 881 | core->task_queue_size -= can_queue; |
| 882 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
| 883 | { |
| 884 | struct sas_task *task = list_entry(q.next, |
| 885 | struct sas_task, |
| 886 | list); |
| 887 | list_del_init(&q); |
| 888 | res = i->dft->lldd_execute_task(task, can_queue, |
| 889 | GFP_KERNEL); |
| 890 | if (unlikely(res)) |
| 891 | __list_add(&q, task->list.prev, &task->list); |
| 892 | } |
| 893 | spin_lock_irqsave(&core->task_queue_lock, flags); |
| 894 | if (res) { |
| 895 | list_splice_init(&q, &core->task_queue); /*at head*/ |
| 896 | core->task_queue_size += can_queue; |
| 897 | } |
| 898 | } |
| 899 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
| 900 | } |
| 901 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 902 | /** |
| 903 | * sas_queue_thread -- The Task Collector thread |
| 904 | * @_sas_ha: pointer to struct sas_ha |
| 905 | */ |
| 906 | static int sas_queue_thread(void *_sas_ha) |
| 907 | { |
| 908 | struct sas_ha_struct *sas_ha = _sas_ha; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 909 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 910 | while (1) { |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 911 | set_current_state(TASK_INTERRUPTIBLE); |
| 912 | schedule(); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 913 | sas_queue(sas_ha); |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 914 | if (kthread_should_stop()) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 915 | break; |
| 916 | } |
| 917 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | int sas_init_queue(struct sas_ha_struct *sas_ha) |
| 922 | { |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 923 | struct scsi_core *core = &sas_ha->core; |
| 924 | |
| 925 | spin_lock_init(&core->task_queue_lock); |
| 926 | core->task_queue_size = 0; |
| 927 | INIT_LIST_HEAD(&core->task_queue); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 928 | |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 929 | core->queue_thread = kthread_run(sas_queue_thread, sas_ha, |
| 930 | "sas_queue_%d", core->shost->host_no); |
| 931 | if (IS_ERR(core->queue_thread)) |
| 932 | return PTR_ERR(core->queue_thread); |
| 933 | return 0; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void sas_shutdown_queue(struct sas_ha_struct *sas_ha) |
| 937 | { |
| 938 | unsigned long flags; |
| 939 | struct scsi_core *core = &sas_ha->core; |
| 940 | struct sas_task *task, *n; |
| 941 | |
Christoph Hellwig | d7a54e3 | 2007-04-26 09:38:01 -0400 | [diff] [blame] | 942 | kthread_stop(core->queue_thread); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 943 | |
| 944 | if (!list_empty(&core->task_queue)) |
| 945 | SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n", |
| 946 | SAS_ADDR(sas_ha->sas_addr)); |
| 947 | |
| 948 | spin_lock_irqsave(&core->task_queue_lock, flags); |
| 949 | list_for_each_entry_safe(task, n, &core->task_queue, list) { |
| 950 | struct scsi_cmnd *cmd = task->uldd_task; |
| 951 | |
| 952 | list_del_init(&task->list); |
| 953 | |
| 954 | ASSIGN_SAS_TASK(cmd, NULL); |
| 955 | sas_free_task(task); |
| 956 | cmd->result = DID_ABORT << 16; |
| 957 | cmd->scsi_done(cmd); |
| 958 | } |
| 959 | spin_unlock_irqrestore(&core->task_queue_lock, flags); |
| 960 | } |
| 961 | |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 962 | /* |
| 963 | * Call the LLDD task abort routine directly. This function is intended for |
| 964 | * use by upper layers that need to tell the LLDD to abort a task. |
| 965 | */ |
| 966 | int __sas_task_abort(struct sas_task *task) |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 967 | { |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 968 | struct sas_internal *si = |
| 969 | to_sas_internal(task->dev->port->ha->core.shost->transportt); |
| 970 | unsigned long flags; |
| 971 | int res; |
| 972 | |
| 973 | spin_lock_irqsave(&task->task_state_lock, flags); |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 974 | if (task->task_state_flags & SAS_TASK_STATE_ABORTED || |
| 975 | task->task_state_flags & SAS_TASK_STATE_DONE) { |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 976 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 977 | SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__, |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 978 | task); |
| 979 | return 0; |
| 980 | } |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 981 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 982 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 983 | |
| 984 | if (!si->dft->lldd_abort_task) |
| 985 | return -ENODEV; |
| 986 | |
| 987 | res = si->dft->lldd_abort_task(task); |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 988 | |
| 989 | spin_lock_irqsave(&task->task_state_lock, flags); |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 990 | if ((task->task_state_flags & SAS_TASK_STATE_DONE) || |
| 991 | (res == TMF_RESP_FUNC_COMPLETE)) |
| 992 | { |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 993 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 994 | task->task_done(task); |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 995 | return 0; |
| 996 | } |
| 997 | |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 998 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) |
| 999 | task->task_state_flags &= ~SAS_TASK_STATE_ABORTED; |
| 1000 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 1001 | |
| 1002 | return -EAGAIN; |
| 1003 | } |
| 1004 | |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1005 | /* |
| 1006 | * Tell an upper layer that it needs to initiate an abort for a given task. |
| 1007 | * This should only ever be called by an LLDD. |
| 1008 | */ |
| 1009 | void sas_task_abort(struct sas_task *task) |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1010 | { |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1011 | struct scsi_cmnd *sc = task->uldd_task; |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1012 | |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1013 | /* Escape for libsas internal commands */ |
| 1014 | if (!sc) { |
| 1015 | if (!del_timer(&task->timer)) |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1016 | return; |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1017 | task->timer.function(task->timer.data); |
| 1018 | return; |
| 1019 | } |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1020 | |
Darrick J. Wong | 3a2755a | 2007-01-30 01:18:58 -0800 | [diff] [blame] | 1021 | if (dev_is_sata(task->dev)) { |
| 1022 | sas_ata_task_abort(task); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1026 | scsi_req_abort_cmd(sc); |
| 1027 | scsi_schedule_eh(sc->device->host); |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 1030 | int sas_slave_alloc(struct scsi_device *scsi_dev) |
| 1031 | { |
| 1032 | struct domain_device *dev = sdev_to_domain_dev(scsi_dev); |
| 1033 | |
| 1034 | if (dev_is_sata(dev)) |
| 1035 | return ata_sas_port_init(dev->sata_dev.ap); |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | void sas_target_destroy(struct scsi_target *starget) |
| 1041 | { |
| 1042 | struct domain_device *found_dev = sas_find_target(starget); |
| 1043 | |
| 1044 | if (!found_dev) |
| 1045 | return; |
| 1046 | |
| 1047 | if (dev_is_sata(found_dev)) |
| 1048 | ata_sas_port_destroy(found_dev->sata_dev.ap); |
| 1049 | |
| 1050 | return; |
| 1051 | } |
| 1052 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1053 | EXPORT_SYMBOL_GPL(sas_queuecommand); |
| 1054 | EXPORT_SYMBOL_GPL(sas_target_alloc); |
| 1055 | EXPORT_SYMBOL_GPL(sas_slave_configure); |
| 1056 | EXPORT_SYMBOL_GPL(sas_slave_destroy); |
| 1057 | EXPORT_SYMBOL_GPL(sas_change_queue_depth); |
| 1058 | EXPORT_SYMBOL_GPL(sas_change_queue_type); |
| 1059 | EXPORT_SYMBOL_GPL(sas_bios_param); |
Darrick J. Wong | 396819f | 2007-01-11 14:15:20 -0800 | [diff] [blame] | 1060 | EXPORT_SYMBOL_GPL(__sas_task_abort); |
Darrick J. Wong | 79a5eb6 | 2006-10-30 15:18:50 -0800 | [diff] [blame] | 1061 | EXPORT_SYMBOL_GPL(sas_task_abort); |
Darrick J. Wong | dea2221 | 2006-11-07 17:28:55 -0800 | [diff] [blame] | 1062 | EXPORT_SYMBOL_GPL(sas_phy_reset); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 1063 | EXPORT_SYMBOL_GPL(sas_phy_enable); |
Darrick J. Wong | ad68923 | 2007-01-26 14:08:52 -0800 | [diff] [blame] | 1064 | EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler); |
Darrick J. Wong | a9344e6 | 2007-01-29 23:48:19 -0800 | [diff] [blame] | 1065 | EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler); |
Darrick J. Wong | fa1c1e8 | 2006-08-10 19:19:47 -0700 | [diff] [blame] | 1066 | EXPORT_SYMBOL_GPL(sas_slave_alloc); |
| 1067 | EXPORT_SYMBOL_GPL(sas_target_destroy); |
| 1068 | EXPORT_SYMBOL_GPL(sas_ioctl); |