ata_xpt.c revision 198897
1/*- 2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/cam/ata/ata_xpt.c 198897 2009-11-04 15:24:32Z mav $"); 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/endian.h> 33#include <sys/systm.h> 34#include <sys/types.h> 35#include <sys/malloc.h> 36#include <sys/kernel.h> 37#include <sys/time.h> 38#include <sys/conf.h> 39#include <sys/fcntl.h> 40#include <sys/md5.h> 41#include <sys/interrupt.h> 42#include <sys/sbuf.h> 43 44#include <sys/lock.h> 45#include <sys/mutex.h> 46#include <sys/sysctl.h> 47 48#ifdef PC98 49#include <pc98/pc98/pc98_machdep.h> /* geometry translation */ 50#endif 51 52#include <cam/cam.h> 53#include <cam/cam_ccb.h> 54#include <cam/cam_queue.h> 55#include <cam/cam_periph.h> 56#include <cam/cam_sim.h> 57#include <cam/cam_xpt.h> 58#include <cam/cam_xpt_sim.h> 59#include <cam/cam_xpt_periph.h> 60#include <cam/cam_xpt_internal.h> 61#include <cam/cam_debug.h> 62 63#include <cam/scsi/scsi_all.h> 64#include <cam/scsi/scsi_message.h> 65#include <cam/ata/ata_all.h> 66#include <machine/stdarg.h> /* for xpt_print below */ 67#include "opt_cam.h" 68 69struct scsi_quirk_entry { 70 struct scsi_inquiry_pattern inq_pat; 71 u_int8_t quirks; 72#define CAM_QUIRK_NOLUNS 0x01 73#define CAM_QUIRK_NOSERIAL 0x02 74#define CAM_QUIRK_HILUNS 0x04 75#define CAM_QUIRK_NOHILUNS 0x08 76 u_int mintags; 77 u_int maxtags; 78}; 79#define SCSI_QUIRK(dev) ((struct scsi_quirk_entry *)((dev)->quirk)) 80 81static periph_init_t probe_periph_init; 82 83static struct periph_driver probe_driver = 84{ 85 probe_periph_init, "aprobe", 86 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0, 87 CAM_PERIPH_DRV_EARLY 88}; 89 90PERIPHDRIVER_DECLARE(aprobe, probe_driver); 91 92typedef enum { 93 PROBE_RESET, 94 PROBE_IDENTIFY, 95 PROBE_SETMODE, 96 PROBE_SET_MULTI, 97 PROBE_INQUIRY, 98 PROBE_FULL_INQUIRY, 99 PROBE_PM_PID, 100 PROBE_PM_PRV, 101 PROBE_INVALID 102} probe_action; 103 104static char *probe_action_text[] = { 105 "PROBE_RESET", 106 "PROBE_IDENTIFY", 107 "PROBE_SETMODE", 108 "PROBE_SET_MULTI", 109 "PROBE_INQUIRY", 110 "PROBE_FULL_INQUIRY", 111 "PROBE_PM_PID", 112 "PROBE_PM_PRV", 113 "PROBE_INVALID" 114}; 115 116#define PROBE_SET_ACTION(softc, newaction) \ 117do { \ 118 char **text; \ 119 text = probe_action_text; \ 120 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \ 121 ("Probe %s to %s\n", text[(softc)->action], \ 122 text[(newaction)])); \ 123 (softc)->action = (newaction); \ 124} while(0) 125 126typedef enum { 127 PROBE_NO_ANNOUNCE = 0x04 128} probe_flags; 129 130typedef struct { 131 TAILQ_HEAD(, ccb_hdr) request_ccbs; 132 probe_action action; 133 union ccb saved_ccb; 134 probe_flags flags; 135 u_int8_t digest[16]; 136 uint32_t pm_pid; 137 uint32_t pm_prv; 138 struct cam_periph *periph; 139} probe_softc; 140 141static struct scsi_quirk_entry scsi_quirk_table[] = 142{ 143 { 144 /* Default tagged queuing parameters for all devices */ 145 { 146 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 147 /*vendor*/"*", /*product*/"*", /*revision*/"*" 148 }, 149 /*quirks*/0, /*mintags*/2, /*maxtags*/32 150 }, 151}; 152 153static const int scsi_quirk_table_size = 154 sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table); 155 156static cam_status proberegister(struct cam_periph *periph, 157 void *arg); 158static void probeschedule(struct cam_periph *probe_periph); 159static void probestart(struct cam_periph *periph, union ccb *start_ccb); 160//static void proberequestdefaultnegotiation(struct cam_periph *periph); 161//static int proberequestbackoff(struct cam_periph *periph, 162// struct cam_ed *device); 163static void probedone(struct cam_periph *periph, union ccb *done_ccb); 164static void probecleanup(struct cam_periph *periph); 165static void scsi_find_quirk(struct cam_ed *device); 166static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb); 167static void ata_scan_lun(struct cam_periph *periph, 168 struct cam_path *path, cam_flags flags, 169 union ccb *ccb); 170static void xptscandone(struct cam_periph *periph, union ccb *done_ccb); 171static struct cam_ed * 172 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, 173 lun_id_t lun_id); 174static void ata_device_transport(struct cam_path *path); 175static void scsi_set_transfer_settings(struct ccb_trans_settings *cts, 176 struct cam_ed *device, 177 int async_update); 178static void scsi_toggle_tags(struct cam_path *path); 179static void ata_dev_async(u_int32_t async_code, 180 struct cam_eb *bus, 181 struct cam_et *target, 182 struct cam_ed *device, 183 void *async_arg); 184static void ata_action(union ccb *start_ccb); 185 186static struct xpt_xport ata_xport = { 187 .alloc_device = ata_alloc_device, 188 .action = ata_action, 189 .async = ata_dev_async, 190}; 191 192struct xpt_xport * 193ata_get_xport(void) 194{ 195 return (&ata_xport); 196} 197 198static void 199probe_periph_init() 200{ 201} 202 203static cam_status 204proberegister(struct cam_periph *periph, void *arg) 205{ 206 union ccb *request_ccb; /* CCB representing the probe request */ 207 cam_status status; 208 probe_softc *softc; 209 210 request_ccb = (union ccb *)arg; 211 if (periph == NULL) { 212 printf("proberegister: periph was NULL!!\n"); 213 return(CAM_REQ_CMP_ERR); 214 } 215 216 if (request_ccb == NULL) { 217 printf("proberegister: no probe CCB, " 218 "can't register device\n"); 219 return(CAM_REQ_CMP_ERR); 220 } 221 222 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT); 223 224 if (softc == NULL) { 225 printf("proberegister: Unable to probe new device. " 226 "Unable to allocate softc\n"); 227 return(CAM_REQ_CMP_ERR); 228 } 229 TAILQ_INIT(&softc->request_ccbs); 230 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 231 periph_links.tqe); 232 softc->flags = 0; 233 periph->softc = softc; 234 softc->periph = periph; 235 softc->action = PROBE_INVALID; 236 status = cam_periph_acquire(periph); 237 if (status != CAM_REQ_CMP) { 238 return (status); 239 } 240 241 242 /* 243 * Ensure we've waited at least a bus settle 244 * delay before attempting to probe the device. 245 * For HBAs that don't do bus resets, this won't make a difference. 246 */ 247 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset, 248 scsi_delay); 249 probeschedule(periph); 250 return(CAM_REQ_CMP); 251} 252 253static void 254probeschedule(struct cam_periph *periph) 255{ 256 struct ccb_pathinq cpi; 257 union ccb *ccb; 258 probe_softc *softc; 259 260 softc = (probe_softc *)periph->softc; 261 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 262 263 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 264 cpi.ccb_h.func_code = XPT_PATH_INQ; 265 xpt_action((union ccb *)&cpi); 266 267 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) || 268 periph->path->device->protocol == PROTO_SATAPM) 269 PROBE_SET_ACTION(softc, PROBE_RESET); 270 else 271 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 272 273 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 274 softc->flags |= PROBE_NO_ANNOUNCE; 275 else 276 softc->flags &= ~PROBE_NO_ANNOUNCE; 277 278 xpt_schedule(periph, ccb->ccb_h.pinfo.priority); 279} 280 281static void 282probestart(struct cam_periph *periph, union ccb *start_ccb) 283{ 284 /* Probe the device that our peripheral driver points to */ 285 struct ccb_ataio *ataio; 286 struct ccb_scsiio *csio; 287 probe_softc *softc; 288 struct cam_path *path; 289 struct ata_params *ident_buf; 290 291 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 292 293 softc = (probe_softc *)periph->softc; 294 path = start_ccb->ccb_h.path; 295 ataio = &start_ccb->ataio; 296 csio = &start_ccb->csio; 297 ident_buf = &periph->path->device->ident_data; 298 299 switch (softc->action) { 300 case PROBE_RESET: 301 cam_fill_ataio(ataio, 302 0, 303 probedone, 304 /*flags*/CAM_DIR_NONE, 305 0, 306 /*data_ptr*/NULL, 307 /*dxfer_len*/0, 308 (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000); 309 ata_reset_cmd(ataio); 310 break; 311 case PROBE_IDENTIFY: 312 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 313 /* Prepare check that it is the same device. */ 314 MD5_CTX context; 315 316 MD5Init(&context); 317 MD5Update(&context, 318 (unsigned char *)ident_buf->model, 319 sizeof(ident_buf->model)); 320 MD5Update(&context, 321 (unsigned char *)ident_buf->revision, 322 sizeof(ident_buf->revision)); 323 MD5Update(&context, 324 (unsigned char *)ident_buf->serial, 325 sizeof(ident_buf->serial)); 326 MD5Final(softc->digest, &context); 327 } 328 cam_fill_ataio(ataio, 329 1, 330 probedone, 331 /*flags*/CAM_DIR_IN, 332 0, 333 /*data_ptr*/(u_int8_t *)ident_buf, 334 /*dxfer_len*/sizeof(struct ata_params), 335 30 * 1000); 336 if (periph->path->device->protocol == PROTO_ATA) 337 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0); 338 else 339 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0); 340 break; 341 case PROBE_SETMODE: 342 cam_fill_ataio(ataio, 343 1, 344 probedone, 345 /*flags*/CAM_DIR_NONE, 346 0, 347 /*data_ptr*/NULL, 348 /*dxfer_len*/0, 349 30 * 1000); 350 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, 351 ata_max_mode(ident_buf, ATA_UDMA6, ATA_UDMA6)); 352 break; 353 case PROBE_SET_MULTI: 354 { 355 struct ccb_trans_settings cts; 356 u_int sectors; 357 358 sectors = max(1, min(ident_buf->sectors_intr & 0xff, 16)); 359 360 /* Report bytecount to SIM. */ 361 bzero(&cts, sizeof(cts)); 362 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 363 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 364 cts.type = CTS_TYPE_CURRENT_SETTINGS; 365 if (path->device->transport == XPORT_ATA) { 366 cts.xport_specific.ata.bytecount = sectors * 367 ata_logical_sector_size(ident_buf); 368 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 369 } else { 370 cts.xport_specific.sata.bytecount = sectors * 371 ata_logical_sector_size(ident_buf); 372 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 373 } 374 xpt_action((union ccb *)&cts); 375 376 cam_fill_ataio(ataio, 377 1, 378 probedone, 379 CAM_DIR_NONE, 380 0, 381 NULL, 382 0, 383 30*1000); 384 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors); 385 break; 386 } 387 case PROBE_INQUIRY: 388 case PROBE_FULL_INQUIRY: 389 { 390 u_int inquiry_len; 391 struct scsi_inquiry_data *inq_buf = 392 &periph->path->device->inq_data; 393 394 if (softc->action == PROBE_INQUIRY) 395 inquiry_len = SHORT_INQUIRY_LENGTH; 396 else 397 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf); 398 /* 399 * Some parallel SCSI devices fail to send an 400 * ignore wide residue message when dealing with 401 * odd length inquiry requests. Round up to be 402 * safe. 403 */ 404 inquiry_len = roundup2(inquiry_len, 2); 405 scsi_inquiry(csio, 406 /*retries*/1, 407 probedone, 408 MSG_SIMPLE_Q_TAG, 409 (u_int8_t *)inq_buf, 410 inquiry_len, 411 /*evpd*/FALSE, 412 /*page_code*/0, 413 SSD_MIN_SIZE, 414 /*timeout*/60 * 1000); 415 break; 416 } 417 case PROBE_PM_PID: 418 cam_fill_ataio(ataio, 419 1, 420 probedone, 421 /*flags*/CAM_DIR_NONE, 422 0, 423 /*data_ptr*/NULL, 424 /*dxfer_len*/0, 425 10 * 1000); 426 ata_pm_read_cmd(ataio, 0, 15); 427 break; 428 case PROBE_PM_PRV: 429 cam_fill_ataio(ataio, 430 1, 431 probedone, 432 /*flags*/CAM_DIR_NONE, 433 0, 434 /*data_ptr*/NULL, 435 /*dxfer_len*/0, 436 10 * 1000); 437 ata_pm_read_cmd(ataio, 1, 15); 438 break; 439 case PROBE_INVALID: 440 CAM_DEBUG(path, CAM_DEBUG_INFO, 441 ("probestart: invalid action state\n")); 442 default: 443 break; 444 } 445 xpt_action(start_ccb); 446} 447#if 0 448static void 449proberequestdefaultnegotiation(struct cam_periph *periph) 450{ 451 struct ccb_trans_settings cts; 452 453 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 454 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 455 cts.type = CTS_TYPE_USER_SETTINGS; 456 xpt_action((union ccb *)&cts); 457 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 458 return; 459 } 460 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 461 cts.type = CTS_TYPE_CURRENT_SETTINGS; 462 xpt_action((union ccb *)&cts); 463} 464 465/* 466 * Backoff Negotiation Code- only pertinent for SPI devices. 467 */ 468static int 469proberequestbackoff(struct cam_periph *periph, struct cam_ed *device) 470{ 471 struct ccb_trans_settings cts; 472 struct ccb_trans_settings_spi *spi; 473 474 memset(&cts, 0, sizeof (cts)); 475 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 476 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 477 cts.type = CTS_TYPE_CURRENT_SETTINGS; 478 xpt_action((union ccb *)&cts); 479 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 480 if (bootverbose) { 481 xpt_print(periph->path, 482 "failed to get current device settings\n"); 483 } 484 return (0); 485 } 486 if (cts.transport != XPORT_SPI) { 487 if (bootverbose) { 488 xpt_print(periph->path, "not SPI transport\n"); 489 } 490 return (0); 491 } 492 spi = &cts.xport_specific.spi; 493 494 /* 495 * We cannot renegotiate sync rate if we don't have one. 496 */ 497 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) { 498 if (bootverbose) { 499 xpt_print(periph->path, "no sync rate known\n"); 500 } 501 return (0); 502 } 503 504 /* 505 * We'll assert that we don't have to touch PPR options- the 506 * SIM will see what we do with period and offset and adjust 507 * the PPR options as appropriate. 508 */ 509 510 /* 511 * A sync rate with unknown or zero offset is nonsensical. 512 * A sync period of zero means Async. 513 */ 514 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0 515 || spi->sync_offset == 0 || spi->sync_period == 0) { 516 if (bootverbose) { 517 xpt_print(periph->path, "no sync rate available\n"); 518 } 519 return (0); 520 } 521 522 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) { 523 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 524 ("hit async: giving up on DV\n")); 525 return (0); 526 } 527 528 529 /* 530 * Jump sync_period up by one, but stop at 5MHz and fall back to Async. 531 * We don't try to remember 'last' settings to see if the SIM actually 532 * gets into the speed we want to set. We check on the SIM telling 533 * us that a requested speed is bad, but otherwise don't try and 534 * check the speed due to the asynchronous and handshake nature 535 * of speed setting. 536 */ 537 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET; 538 for (;;) { 539 spi->sync_period++; 540 if (spi->sync_period >= 0xf) { 541 spi->sync_period = 0; 542 spi->sync_offset = 0; 543 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 544 ("setting to async for DV\n")); 545 /* 546 * Once we hit async, we don't want to try 547 * any more settings. 548 */ 549 device->flags |= CAM_DEV_DV_HIT_BOTTOM; 550 } else if (bootverbose) { 551 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 552 ("DV: period 0x%x\n", spi->sync_period)); 553 printf("setting period to 0x%x\n", spi->sync_period); 554 } 555 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 556 cts.type = CTS_TYPE_CURRENT_SETTINGS; 557 xpt_action((union ccb *)&cts); 558 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 559 break; 560 } 561 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 562 ("DV: failed to set period 0x%x\n", spi->sync_period)); 563 if (spi->sync_period == 0) { 564 return (0); 565 } 566 } 567 return (1); 568} 569#endif 570static void 571probedone(struct cam_periph *periph, union ccb *done_ccb) 572{ 573 struct ata_params *ident_buf; 574 probe_softc *softc; 575 struct cam_path *path; 576 u_int32_t priority; 577 int found = 1; 578 579 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 580 581 softc = (probe_softc *)periph->softc; 582 path = done_ccb->ccb_h.path; 583 priority = done_ccb->ccb_h.pinfo.priority; 584 ident_buf = &path->device->ident_data; 585 586 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 587device_fail: if (cam_periph_error(done_ccb, 0, 0, 588 &softc->saved_ccb) == ERESTART) { 589 return; 590 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 591 /* Don't wedge the queue */ 592 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 593 /*run_queue*/TRUE); 594 } 595 /* Old PIO2 devices may not support mode setting. */ 596 if (softc->action == PROBE_SETMODE && 597 ata_max_pmode(ident_buf) <= ATA_PIO2 && 598 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) 599 goto noerror; 600 /* 601 * If we get to this point, we got an error status back 602 * from the inquiry and the error status doesn't require 603 * automatically retrying the command. Therefore, the 604 * inquiry failed. If we had inquiry information before 605 * for this device, but this latest inquiry command failed, 606 * the device has probably gone away. If this device isn't 607 * already marked unconfigured, notify the peripheral 608 * drivers that this device is no more. 609 */ 610 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 611 xpt_async(AC_LOST_DEVICE, path, NULL); 612 found = 0; 613 goto done; 614 } 615noerror: 616 switch (softc->action) { 617 case PROBE_RESET: 618 { 619 int sign = (done_ccb->ataio.res.lba_high << 8) + 620 done_ccb->ataio.res.lba_mid; 621 xpt_print(path, "SIGNATURE: %04x\n", sign); 622 if (sign == 0x0000 && 623 done_ccb->ccb_h.target_id != 15) { 624 path->device->protocol = PROTO_ATA; 625 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 626 } else if (sign == 0x9669 && 627 done_ccb->ccb_h.target_id == 15) { 628 struct ccb_trans_settings cts; 629 630 /* Report SIM that PM is present. */ 631 bzero(&cts, sizeof(cts)); 632 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 633 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 634 cts.type = CTS_TYPE_CURRENT_SETTINGS; 635 cts.xport_specific.sata.pm_present = 1; 636 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 637 xpt_action((union ccb *)&cts); 638 path->device->protocol = PROTO_SATAPM; 639 PROBE_SET_ACTION(softc, PROBE_PM_PID); 640 } else if (sign == 0xeb14 && 641 done_ccb->ccb_h.target_id != 15) { 642 path->device->protocol = PROTO_SCSI; 643 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 644 } else { 645 if (done_ccb->ccb_h.target_id != 15) { 646 xpt_print(path, 647 "Unexpected signature 0x%04x\n", sign); 648 } 649 goto device_fail; 650 } 651 xpt_release_ccb(done_ccb); 652 xpt_schedule(periph, priority); 653 return; 654 } 655 case PROBE_IDENTIFY: 656 { 657 int16_t *ptr; 658 659 for (ptr = (int16_t *)ident_buf; 660 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) { 661 *ptr = le16toh(*ptr); 662 } 663 if (strncmp(ident_buf->model, "FX", 2) && 664 strncmp(ident_buf->model, "NEC", 3) && 665 strncmp(ident_buf->model, "Pioneer", 7) && 666 strncmp(ident_buf->model, "SHARP", 5)) { 667 ata_bswap(ident_buf->model, sizeof(ident_buf->model)); 668 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision)); 669 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial)); 670 } 671 ata_btrim(ident_buf->model, sizeof(ident_buf->model)); 672 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model)); 673 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision)); 674 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision)); 675 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial)); 676 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial)); 677 678 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 679 /* Check that it is the same device. */ 680 MD5_CTX context; 681 u_int8_t digest[16]; 682 683 MD5Init(&context); 684 MD5Update(&context, 685 (unsigned char *)ident_buf->model, 686 sizeof(ident_buf->model)); 687 MD5Update(&context, 688 (unsigned char *)ident_buf->revision, 689 sizeof(ident_buf->revision)); 690 MD5Update(&context, 691 (unsigned char *)ident_buf->serial, 692 sizeof(ident_buf->serial)); 693 MD5Final(digest, &context); 694 if (bcmp(digest, softc->digest, sizeof(digest))) { 695 /* Device changed. */ 696 xpt_async(AC_LOST_DEVICE, path, NULL); 697 } 698 } else { 699 /* Clean up from previous instance of this device */ 700 if (path->device->serial_num != NULL) { 701 free(path->device->serial_num, M_CAMXPT); 702 path->device->serial_num = NULL; 703 path->device->serial_num_len = 0; 704 } 705 path->device->serial_num = 706 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1), 707 M_CAMXPT, M_NOWAIT); 708 if (path->device->serial_num != NULL) { 709 bcopy(ident_buf->serial, 710 path->device->serial_num, 711 sizeof(ident_buf->serial)); 712 path->device->serial_num[sizeof(ident_buf->serial)] 713 = '\0'; 714 path->device->serial_num_len = 715 strlen(path->device->serial_num); 716 } 717 718 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 719 } 720 ata_device_transport(path); 721 PROBE_SET_ACTION(softc, PROBE_SETMODE); 722 xpt_release_ccb(done_ccb); 723 xpt_schedule(periph, priority); 724 return; 725 } 726 case PROBE_SETMODE: 727 if (path->device->protocol == PROTO_ATA) { 728 PROBE_SET_ACTION(softc, PROBE_SET_MULTI); 729 } else { 730 PROBE_SET_ACTION(softc, PROBE_INQUIRY); 731 } 732 xpt_release_ccb(done_ccb); 733 xpt_schedule(periph, priority); 734 return; 735 case PROBE_SET_MULTI: 736 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 737 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 738 xpt_acquire_device(path->device); 739 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 740 xpt_action(done_ccb); 741 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 742 done_ccb); 743 } 744 break; 745 case PROBE_INQUIRY: 746 case PROBE_FULL_INQUIRY: 747 { 748 struct scsi_inquiry_data *inq_buf; 749 u_int8_t periph_qual, len; 750 751 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 752 inq_buf = &path->device->inq_data; 753 754 periph_qual = SID_QUAL(inq_buf); 755 756 if (periph_qual != SID_QUAL_LU_CONNECTED) 757 break; 758 759 /* 760 * We conservatively request only 761 * SHORT_INQUIRY_LEN bytes of inquiry 762 * information during our first try 763 * at sending an INQUIRY. If the device 764 * has more information to give, 765 * perform a second request specifying 766 * the amount of information the device 767 * is willing to give. 768 */ 769 len = inq_buf->additional_length 770 + offsetof(struct scsi_inquiry_data, additional_length) + 1; 771 if (softc->action == PROBE_INQUIRY 772 && len > SHORT_INQUIRY_LENGTH) { 773 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY); 774 xpt_release_ccb(done_ccb); 775 xpt_schedule(periph, priority); 776 return; 777 } 778 779 scsi_find_quirk(path->device); 780 ata_device_transport(path); 781 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 782 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 783 xpt_acquire_device(path->device); 784 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 785 xpt_action(done_ccb); 786 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb); 787 } 788 break; 789 } 790 case PROBE_PM_PID: 791 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0) 792 bzero(ident_buf, sizeof(*ident_buf)); 793 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) + 794 (done_ccb->ataio.res.lba_mid << 16) + 795 (done_ccb->ataio.res.lba_low << 8) + 796 done_ccb->ataio.res.sector_count; 797 ((uint32_t *)ident_buf)[0] = softc->pm_pid; 798 printf("PM Product ID: %08x\n", softc->pm_pid); 799 snprintf(ident_buf->model, sizeof(ident_buf->model), 800 "Port Multiplier %08x", softc->pm_pid); 801 PROBE_SET_ACTION(softc, PROBE_PM_PRV); 802 xpt_release_ccb(done_ccb); 803 xpt_schedule(periph, priority); 804 return; 805 case PROBE_PM_PRV: 806 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) + 807 (done_ccb->ataio.res.lba_mid << 16) + 808 (done_ccb->ataio.res.lba_low << 8) + 809 done_ccb->ataio.res.sector_count; 810 ((uint32_t *)ident_buf)[1] = softc->pm_prv; 811 printf("PM Revision: %08x\n", softc->pm_prv); 812 snprintf(ident_buf->revision, sizeof(ident_buf->revision), 813 "%04x", softc->pm_prv); 814 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 815 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 816 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 817 xpt_acquire_device(path->device); 818 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 819 xpt_action(done_ccb); 820 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 821 done_ccb); 822 } else { 823 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 824 xpt_action(done_ccb); 825 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb); 826 } 827 break; 828 case PROBE_INVALID: 829 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO, 830 ("probedone: invalid action state\n")); 831 default: 832 break; 833 } 834done: 835 xpt_release_ccb(done_ccb); 836 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 837 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe); 838 done_ccb->ccb_h.status = CAM_REQ_CMP; 839 done_ccb->ccb_h.ppriv_field1 = found; 840 xpt_done(done_ccb); 841 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) { 842 cam_periph_invalidate(periph); 843 cam_periph_release_locked(periph); 844 } else { 845 probeschedule(periph); 846 } 847} 848 849static void 850probecleanup(struct cam_periph *periph) 851{ 852 free(periph->softc, M_CAMXPT); 853} 854 855static void 856scsi_find_quirk(struct cam_ed *device) 857{ 858 struct scsi_quirk_entry *quirk; 859 caddr_t match; 860 861 match = cam_quirkmatch((caddr_t)&device->inq_data, 862 (caddr_t)scsi_quirk_table, 863 sizeof(scsi_quirk_table) / 864 sizeof(*scsi_quirk_table), 865 sizeof(*scsi_quirk_table), scsi_inquiry_match); 866 867 if (match == NULL) 868 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 869 870 quirk = (struct scsi_quirk_entry *)match; 871 device->quirk = quirk; 872 device->mintags = quirk->mintags; 873 device->maxtags = quirk->maxtags; 874} 875 876typedef struct { 877 union ccb *request_ccb; 878 struct ccb_pathinq *cpi; 879 int counter; 880 int found; 881} ata_scan_bus_info; 882 883/* 884 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 885 * As the scan progresses, xpt_scan_bus is used as the 886 * callback on completion function. 887 */ 888static void 889ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 890{ 891 struct cam_path *path; 892 ata_scan_bus_info *scan_info; 893 union ccb *work_ccb; 894 cam_status status; 895 896 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 897 ("xpt_scan_bus\n")); 898 switch (request_ccb->ccb_h.func_code) { 899 case XPT_SCAN_BUS: 900 /* Find out the characteristics of the bus */ 901 work_ccb = xpt_alloc_ccb_nowait(); 902 if (work_ccb == NULL) { 903 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 904 xpt_done(request_ccb); 905 return; 906 } 907 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 908 request_ccb->ccb_h.pinfo.priority); 909 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 910 xpt_action(work_ccb); 911 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 912 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 913 xpt_free_ccb(work_ccb); 914 xpt_done(request_ccb); 915 return; 916 } 917 918 /* Save some state for use while we probe for devices */ 919 scan_info = (ata_scan_bus_info *) 920 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT); 921 if (scan_info == NULL) { 922 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 923 xpt_done(request_ccb); 924 return; 925 } 926 scan_info->request_ccb = request_ccb; 927 scan_info->cpi = &work_ccb->cpi; 928 if (scan_info->cpi->transport == XPORT_ATA) 929 scan_info->found = 0x0003; 930 else 931 scan_info->found = 0x8001; 932 scan_info->counter = 0; 933 /* If PM supported, probe it first. */ 934 if (scan_info->cpi->hba_inquiry & PI_SATAPM) 935 scan_info->counter = 15; 936 937 work_ccb = xpt_alloc_ccb_nowait(); 938 if (work_ccb == NULL) { 939 free(scan_info, M_CAMXPT); 940 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 941 xpt_done(request_ccb); 942 break; 943 } 944 goto scan_next; 945 case XPT_SCAN_LUN: 946 work_ccb = request_ccb; 947 /* Reuse the same CCB to query if a device was really found */ 948 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0; 949 /* Free the current request path- we're done with it. */ 950 xpt_free_path(work_ccb->ccb_h.path); 951 /* If there is PMP... */ 952 if (scan_info->counter == 15) { 953 if (work_ccb->ccb_h.ppriv_field1 != 0) { 954 /* everything else willbe probed by it */ 955 scan_info->found = 0x8000; 956 } else { 957 struct ccb_trans_settings cts; 958 959 /* Report SIM that PM is absent. */ 960 bzero(&cts, sizeof(cts)); 961 xpt_setup_ccb(&cts.ccb_h, 962 scan_info->request_ccb->ccb_h.path, 1); 963 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 964 cts.type = CTS_TYPE_CURRENT_SETTINGS; 965 cts.xport_specific.sata.pm_present = 0; 966 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 967 xpt_action((union ccb *)&cts); 968 } 969 } 970take_next: 971 /* Take next device. Wrap from 15 (PM) to 0. */ 972 scan_info->counter = (scan_info->counter + 1 ) & 0x0f; 973 if (scan_info->counter > scan_info->cpi->max_target - 974 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) { 975 xpt_free_ccb(work_ccb); 976 xpt_free_ccb((union ccb *)scan_info->cpi); 977 request_ccb = scan_info->request_ccb; 978 free(scan_info, M_CAMXPT); 979 request_ccb->ccb_h.status = CAM_REQ_CMP; 980 xpt_done(request_ccb); 981 break; 982 } 983scan_next: 984 if ((scan_info->found & (1 << scan_info->counter)) == 0) 985 goto take_next; 986 status = xpt_create_path(&path, xpt_periph, 987 scan_info->request_ccb->ccb_h.path_id, 988 scan_info->counter, 0); 989 if (status != CAM_REQ_CMP) { 990 printf("xpt_scan_bus: xpt_create_path failed" 991 " with status %#x, bus scan halted\n", 992 status); 993 xpt_free_ccb(work_ccb); 994 xpt_free_ccb((union ccb *)scan_info->cpi); 995 request_ccb = scan_info->request_ccb; 996 free(scan_info, M_CAMXPT); 997 request_ccb->ccb_h.status = status; 998 xpt_done(request_ccb); 999 break; 1000 } 1001 xpt_setup_ccb(&work_ccb->ccb_h, path, 1002 scan_info->request_ccb->ccb_h.pinfo.priority); 1003 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1004 work_ccb->ccb_h.cbfcnp = ata_scan_bus; 1005 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 1006 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags; 1007 xpt_action(work_ccb); 1008 break; 1009 default: 1010 break; 1011 } 1012} 1013 1014static void 1015ata_scan_lun(struct cam_periph *periph, struct cam_path *path, 1016 cam_flags flags, union ccb *request_ccb) 1017{ 1018 struct ccb_pathinq cpi; 1019 cam_status status; 1020 struct cam_path *new_path; 1021 struct cam_periph *old_periph; 1022 1023 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 1024 ("xpt_scan_lun\n")); 1025 1026 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 1027 cpi.ccb_h.func_code = XPT_PATH_INQ; 1028 xpt_action((union ccb *)&cpi); 1029 1030 if (cpi.ccb_h.status != CAM_REQ_CMP) { 1031 if (request_ccb != NULL) { 1032 request_ccb->ccb_h.status = cpi.ccb_h.status; 1033 xpt_done(request_ccb); 1034 } 1035 return; 1036 } 1037 1038 if (request_ccb == NULL) { 1039 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT); 1040 if (request_ccb == NULL) { 1041 xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 1042 "can't continue\n"); 1043 return; 1044 } 1045 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT); 1046 if (new_path == NULL) { 1047 xpt_print(path, "xpt_scan_lun: can't allocate path, " 1048 "can't continue\n"); 1049 free(request_ccb, M_CAMXPT); 1050 return; 1051 } 1052 status = xpt_compile_path(new_path, xpt_periph, 1053 path->bus->path_id, 1054 path->target->target_id, 1055 path->device->lun_id); 1056 1057 if (status != CAM_REQ_CMP) { 1058 xpt_print(path, "xpt_scan_lun: can't compile path, " 1059 "can't continue\n"); 1060 free(request_ccb, M_CAMXPT); 1061 free(new_path, M_CAMXPT); 1062 return; 1063 } 1064 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL); 1065 request_ccb->ccb_h.cbfcnp = xptscandone; 1066 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1067 request_ccb->crcn.flags = flags; 1068 } 1069 1070 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) { 1071 probe_softc *softc; 1072 1073 softc = (probe_softc *)old_periph->softc; 1074 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 1075 periph_links.tqe); 1076 } else { 1077 status = cam_periph_alloc(proberegister, NULL, probecleanup, 1078 probestart, "aprobe", 1079 CAM_PERIPH_BIO, 1080 request_ccb->ccb_h.path, NULL, 0, 1081 request_ccb); 1082 1083 if (status != CAM_REQ_CMP) { 1084 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 1085 "returned an error, can't continue probe\n"); 1086 request_ccb->ccb_h.status = status; 1087 xpt_done(request_ccb); 1088 } 1089 } 1090} 1091 1092static void 1093xptscandone(struct cam_periph *periph, union ccb *done_ccb) 1094{ 1095 xpt_release_path(done_ccb->ccb_h.path); 1096 free(done_ccb->ccb_h.path, M_CAMXPT); 1097 free(done_ccb, M_CAMXPT); 1098} 1099 1100static struct cam_ed * 1101ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 1102{ 1103 struct cam_path path; 1104 struct scsi_quirk_entry *quirk; 1105 struct cam_ed *device; 1106 struct cam_ed *cur_device; 1107 1108 device = xpt_alloc_device(bus, target, lun_id); 1109 if (device == NULL) 1110 return (NULL); 1111 1112 /* 1113 * Take the default quirk entry until we have inquiry 1114 * data and can determine a better quirk to use. 1115 */ 1116 quirk = &scsi_quirk_table[scsi_quirk_table_size - 1]; 1117 device->quirk = (void *)quirk; 1118 device->mintags = quirk->mintags; 1119 device->maxtags = quirk->maxtags; 1120 bzero(&device->inq_data, sizeof(device->inq_data)); 1121 device->inq_flags = 0; 1122 device->queue_flags = 0; 1123 device->serial_num = NULL; 1124 device->serial_num_len = 0; 1125 1126 /* 1127 * XXX should be limited by number of CCBs this bus can 1128 * do. 1129 */ 1130 bus->sim->max_ccbs += device->ccbq.devq_openings; 1131 /* Insertion sort into our target's device list */ 1132 cur_device = TAILQ_FIRST(&target->ed_entries); 1133 while (cur_device != NULL && cur_device->lun_id < lun_id) 1134 cur_device = TAILQ_NEXT(cur_device, links); 1135 if (cur_device != NULL) { 1136 TAILQ_INSERT_BEFORE(cur_device, device, links); 1137 } else { 1138 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 1139 } 1140 target->generation++; 1141 if (lun_id != CAM_LUN_WILDCARD) { 1142 xpt_compile_path(&path, 1143 NULL, 1144 bus->path_id, 1145 target->target_id, 1146 lun_id); 1147 ata_device_transport(&path); 1148 xpt_release_path(&path); 1149 } 1150 1151 return (device); 1152} 1153 1154static void 1155ata_device_transport(struct cam_path *path) 1156{ 1157 struct ccb_pathinq cpi; 1158 struct ccb_trans_settings cts; 1159 struct scsi_inquiry_data *inq_buf = NULL; 1160 struct ata_params *ident_buf = NULL; 1161 1162 /* Get transport information from the SIM */ 1163 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 1164 cpi.ccb_h.func_code = XPT_PATH_INQ; 1165 xpt_action((union ccb *)&cpi); 1166 1167 path->device->transport = cpi.transport; 1168 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 1169 inq_buf = &path->device->inq_data; 1170 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0) 1171 ident_buf = &path->device->ident_data; 1172 if (path->device->protocol == PROTO_ATA) { 1173 path->device->protocol_version = ident_buf ? 1174 ata_version(ident_buf->version_major) : cpi.protocol_version; 1175 } else if (path->device->protocol == PROTO_SCSI) { 1176 path->device->protocol_version = inq_buf ? 1177 SID_ANSI_REV(inq_buf) : cpi.protocol_version; 1178 } 1179 path->device->transport_version = ident_buf ? 1180 ata_version(ident_buf->version_major) : cpi.transport_version; 1181 1182 /* Tell the controller what we think */ 1183 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 1184 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1185 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1186 cts.transport = path->device->transport; 1187 cts.transport_version = path->device->transport_version; 1188 cts.protocol = path->device->protocol; 1189 cts.protocol_version = path->device->protocol_version; 1190 cts.proto_specific.valid = 0; 1191 cts.xport_specific.valid = 0; 1192 xpt_action((union ccb *)&cts); 1193} 1194 1195static void 1196ata_action(union ccb *start_ccb) 1197{ 1198 1199 switch (start_ccb->ccb_h.func_code) { 1200 case XPT_SET_TRAN_SETTINGS: 1201 { 1202 scsi_set_transfer_settings(&start_ccb->cts, 1203 start_ccb->ccb_h.path->device, 1204 /*async_update*/FALSE); 1205 break; 1206 } 1207 case XPT_SCAN_BUS: 1208 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 1209 break; 1210 case XPT_SCAN_LUN: 1211 ata_scan_lun(start_ccb->ccb_h.path->periph, 1212 start_ccb->ccb_h.path, start_ccb->crcn.flags, 1213 start_ccb); 1214 break; 1215 case XPT_GET_TRAN_SETTINGS: 1216 { 1217 struct cam_sim *sim; 1218 1219 sim = start_ccb->ccb_h.path->bus->sim; 1220 (*(sim->sim_action))(sim, start_ccb); 1221 break; 1222 } 1223 default: 1224 xpt_action_default(start_ccb); 1225 break; 1226 } 1227} 1228 1229static void 1230scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 1231 int async_update) 1232{ 1233 struct ccb_pathinq cpi; 1234 struct ccb_trans_settings cur_cts; 1235 struct ccb_trans_settings_scsi *scsi; 1236 struct ccb_trans_settings_scsi *cur_scsi; 1237 struct cam_sim *sim; 1238 struct scsi_inquiry_data *inq_data; 1239 1240 if (device == NULL) { 1241 cts->ccb_h.status = CAM_PATH_INVALID; 1242 xpt_done((union ccb *)cts); 1243 return; 1244 } 1245 1246 if (cts->protocol == PROTO_UNKNOWN 1247 || cts->protocol == PROTO_UNSPECIFIED) { 1248 cts->protocol = device->protocol; 1249 cts->protocol_version = device->protocol_version; 1250 } 1251 1252 if (cts->protocol_version == PROTO_VERSION_UNKNOWN 1253 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 1254 cts->protocol_version = device->protocol_version; 1255 1256 if (cts->protocol != device->protocol) { 1257 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 1258 cts->protocol, device->protocol); 1259 cts->protocol = device->protocol; 1260 } 1261 1262 if (cts->protocol_version > device->protocol_version) { 1263 if (bootverbose) { 1264 xpt_print(cts->ccb_h.path, "Down reving Protocol " 1265 "Version from %d to %d?\n", cts->protocol_version, 1266 device->protocol_version); 1267 } 1268 cts->protocol_version = device->protocol_version; 1269 } 1270 1271 if (cts->transport == XPORT_UNKNOWN 1272 || cts->transport == XPORT_UNSPECIFIED) { 1273 cts->transport = device->transport; 1274 cts->transport_version = device->transport_version; 1275 } 1276 1277 if (cts->transport_version == XPORT_VERSION_UNKNOWN 1278 || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 1279 cts->transport_version = device->transport_version; 1280 1281 if (cts->transport != device->transport) { 1282 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 1283 cts->transport, device->transport); 1284 cts->transport = device->transport; 1285 } 1286 1287 if (cts->transport_version > device->transport_version) { 1288 if (bootverbose) { 1289 xpt_print(cts->ccb_h.path, "Down reving Transport " 1290 "Version from %d to %d?\n", cts->transport_version, 1291 device->transport_version); 1292 } 1293 cts->transport_version = device->transport_version; 1294 } 1295 1296 sim = cts->ccb_h.path->bus->sim; 1297 1298 /* 1299 * Nothing more of interest to do unless 1300 * this is a device connected via the 1301 * SCSI protocol. 1302 */ 1303 if (cts->protocol != PROTO_SCSI) { 1304 if (async_update == FALSE) 1305 (*(sim->sim_action))(sim, (union ccb *)cts); 1306 return; 1307 } 1308 1309 inq_data = &device->inq_data; 1310 scsi = &cts->proto_specific.scsi; 1311 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL); 1312 cpi.ccb_h.func_code = XPT_PATH_INQ; 1313 xpt_action((union ccb *)&cpi); 1314 1315 /* SCSI specific sanity checking */ 1316 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 1317 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0 1318 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 1319 || (device->mintags == 0)) { 1320 /* 1321 * Can't tag on hardware that doesn't support tags, 1322 * doesn't have it enabled, or has broken tag support. 1323 */ 1324 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1325 } 1326 1327 if (async_update == FALSE) { 1328 /* 1329 * Perform sanity checking against what the 1330 * controller and device can do. 1331 */ 1332 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL); 1333 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1334 cur_cts.type = cts->type; 1335 xpt_action((union ccb *)&cur_cts); 1336 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1337 return; 1338 } 1339 cur_scsi = &cur_cts.proto_specific.scsi; 1340 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 1341 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1342 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB; 1343 } 1344 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0) 1345 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1346 } 1347 1348 if (cts->type == CTS_TYPE_CURRENT_SETTINGS 1349 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 1350 int device_tagenb; 1351 1352 /* 1353 * If we are transitioning from tags to no-tags or 1354 * vice-versa, we need to carefully freeze and restart 1355 * the queue so that we don't overlap tagged and non-tagged 1356 * commands. We also temporarily stop tags if there is 1357 * a change in transfer negotiation settings to allow 1358 * "tag-less" negotiation. 1359 */ 1360 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 1361 || (device->inq_flags & SID_CmdQue) != 0) 1362 device_tagenb = TRUE; 1363 else 1364 device_tagenb = FALSE; 1365 1366 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 1367 && device_tagenb == FALSE) 1368 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0 1369 && device_tagenb == TRUE)) { 1370 1371 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) { 1372 /* 1373 * Delay change to use tags until after a 1374 * few commands have gone to this device so 1375 * the controller has time to perform transfer 1376 * negotiations without tagged messages getting 1377 * in the way. 1378 */ 1379 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 1380 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 1381 } else { 1382 struct ccb_relsim crs; 1383 1384 xpt_freeze_devq(cts->ccb_h.path, /*count*/1); 1385 device->inq_flags &= ~SID_CmdQue; 1386 xpt_dev_ccbq_resize(cts->ccb_h.path, 1387 sim->max_dev_openings); 1388 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 1389 device->tag_delay_count = 0; 1390 1391 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path, 1392 CAM_PRIORITY_NORMAL); 1393 crs.ccb_h.func_code = XPT_REL_SIMQ; 1394 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 1395 crs.openings 1396 = crs.release_timeout 1397 = crs.qfrozen_cnt 1398 = 0; 1399 xpt_action((union ccb *)&crs); 1400 } 1401 } 1402 } 1403 if (async_update == FALSE) 1404 (*(sim->sim_action))(sim, (union ccb *)cts); 1405} 1406 1407static void 1408scsi_toggle_tags(struct cam_path *path) 1409{ 1410 struct cam_ed *dev; 1411 1412 /* 1413 * Give controllers a chance to renegotiate 1414 * before starting tag operations. We 1415 * "toggle" tagged queuing off then on 1416 * which causes the tag enable command delay 1417 * counter to come into effect. 1418 */ 1419 dev = path->device; 1420 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 1421 || ((dev->inq_flags & SID_CmdQue) != 0 1422 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) { 1423 struct ccb_trans_settings cts; 1424 1425 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 1426 cts.protocol = PROTO_SCSI; 1427 cts.protocol_version = PROTO_VERSION_UNSPECIFIED; 1428 cts.transport = XPORT_UNSPECIFIED; 1429 cts.transport_version = XPORT_VERSION_UNSPECIFIED; 1430 cts.proto_specific.scsi.flags = 0; 1431 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ; 1432 scsi_set_transfer_settings(&cts, path->device, 1433 /*async_update*/TRUE); 1434 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB; 1435 scsi_set_transfer_settings(&cts, path->device, 1436 /*async_update*/TRUE); 1437 } 1438} 1439 1440/* 1441 * Handle any per-device event notifications that require action by the XPT. 1442 */ 1443static void 1444ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 1445 struct cam_ed *device, void *async_arg) 1446{ 1447 cam_status status; 1448 struct cam_path newpath; 1449 1450 /* 1451 * We only need to handle events for real devices. 1452 */ 1453 if (target->target_id == CAM_TARGET_WILDCARD 1454 || device->lun_id == CAM_LUN_WILDCARD) 1455 return; 1456 1457 /* 1458 * We need our own path with wildcards expanded to 1459 * handle certain types of events. 1460 */ 1461 if ((async_code == AC_SENT_BDR) 1462 || (async_code == AC_BUS_RESET) 1463 || (async_code == AC_INQ_CHANGED)) 1464 status = xpt_compile_path(&newpath, NULL, 1465 bus->path_id, 1466 target->target_id, 1467 device->lun_id); 1468 else 1469 status = CAM_REQ_CMP_ERR; 1470 1471 if (status == CAM_REQ_CMP) { 1472 1473 /* 1474 * Allow transfer negotiation to occur in a 1475 * tag free environment. 1476 */ 1477 if (async_code == AC_SENT_BDR 1478 || async_code == AC_BUS_RESET) 1479 scsi_toggle_tags(&newpath); 1480 1481 if (async_code == AC_INQ_CHANGED) { 1482 /* 1483 * We've sent a start unit command, or 1484 * something similar to a device that 1485 * may have caused its inquiry data to 1486 * change. So we re-scan the device to 1487 * refresh the inquiry data for it. 1488 */ 1489 ata_scan_lun(newpath.periph, &newpath, 1490 CAM_EXPECT_INQ_CHANGE, NULL); 1491 } 1492 xpt_release_path(&newpath); 1493 } else if (async_code == AC_LOST_DEVICE && 1494 (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1495 device->flags |= CAM_DEV_UNCONFIGURED; 1496 xpt_release_device(device); 1497 } else if (async_code == AC_TRANSFER_NEG) { 1498 struct ccb_trans_settings *settings; 1499 1500 settings = (struct ccb_trans_settings *)async_arg; 1501 scsi_set_transfer_settings(settings, device, 1502 /*async_update*/TRUE); 1503 } 1504} 1505 1506