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