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