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