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