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