ata_xpt.c revision 198748
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 198748 2009-11-01 11:31:06Z 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 * 512; 367 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 368 } else { 369 cts.xport_specific.sata.bytecount = sectors * 512; 370 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 371 } 372 xpt_action((union ccb *)&cts); 373 374 cam_fill_ataio(ataio, 375 1, 376 probedone, 377 CAM_DIR_NONE, 378 0, 379 NULL, 380 0, 381 30*1000); 382 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors); 383 break; 384 } 385 case PROBE_INQUIRY: 386 case PROBE_FULL_INQUIRY: 387 { 388 u_int inquiry_len; 389 struct scsi_inquiry_data *inq_buf = 390 &periph->path->device->inq_data; 391 392 if (softc->action == PROBE_INQUIRY) 393 inquiry_len = SHORT_INQUIRY_LENGTH; 394 else 395 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf); 396 /* 397 * Some parallel SCSI devices fail to send an 398 * ignore wide residue message when dealing with 399 * odd length inquiry requests. Round up to be 400 * safe. 401 */ 402 inquiry_len = roundup2(inquiry_len, 2); 403 scsi_inquiry(csio, 404 /*retries*/1, 405 probedone, 406 MSG_SIMPLE_Q_TAG, 407 (u_int8_t *)inq_buf, 408 inquiry_len, 409 /*evpd*/FALSE, 410 /*page_code*/0, 411 SSD_MIN_SIZE, 412 /*timeout*/60 * 1000); 413 break; 414 } 415 case PROBE_PM_PID: 416 cam_fill_ataio(ataio, 417 1, 418 probedone, 419 /*flags*/CAM_DIR_NONE, 420 0, 421 /*data_ptr*/NULL, 422 /*dxfer_len*/0, 423 10 * 1000); 424 ata_pm_read_cmd(ataio, 0, 15); 425 break; 426 case PROBE_PM_PRV: 427 cam_fill_ataio(ataio, 428 1, 429 probedone, 430 /*flags*/CAM_DIR_NONE, 431 0, 432 /*data_ptr*/NULL, 433 /*dxfer_len*/0, 434 10 * 1000); 435 ata_pm_read_cmd(ataio, 1, 15); 436 break; 437 case PROBE_INVALID: 438 CAM_DEBUG(path, CAM_DEBUG_INFO, 439 ("probestart: invalid action state\n")); 440 default: 441 break; 442 } 443 xpt_action(start_ccb); 444} 445#if 0 446static void 447proberequestdefaultnegotiation(struct cam_periph *periph) 448{ 449 struct ccb_trans_settings cts; 450 451 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 452 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 453 cts.type = CTS_TYPE_USER_SETTINGS; 454 xpt_action((union ccb *)&cts); 455 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 456 return; 457 } 458 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 459 cts.type = CTS_TYPE_CURRENT_SETTINGS; 460 xpt_action((union ccb *)&cts); 461} 462 463/* 464 * Backoff Negotiation Code- only pertinent for SPI devices. 465 */ 466static int 467proberequestbackoff(struct cam_periph *periph, struct cam_ed *device) 468{ 469 struct ccb_trans_settings cts; 470 struct ccb_trans_settings_spi *spi; 471 472 memset(&cts, 0, sizeof (cts)); 473 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 474 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 475 cts.type = CTS_TYPE_CURRENT_SETTINGS; 476 xpt_action((union ccb *)&cts); 477 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 478 if (bootverbose) { 479 xpt_print(periph->path, 480 "failed to get current device settings\n"); 481 } 482 return (0); 483 } 484 if (cts.transport != XPORT_SPI) { 485 if (bootverbose) { 486 xpt_print(periph->path, "not SPI transport\n"); 487 } 488 return (0); 489 } 490 spi = &cts.xport_specific.spi; 491 492 /* 493 * We cannot renegotiate sync rate if we don't have one. 494 */ 495 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) { 496 if (bootverbose) { 497 xpt_print(periph->path, "no sync rate known\n"); 498 } 499 return (0); 500 } 501 502 /* 503 * We'll assert that we don't have to touch PPR options- the 504 * SIM will see what we do with period and offset and adjust 505 * the PPR options as appropriate. 506 */ 507 508 /* 509 * A sync rate with unknown or zero offset is nonsensical. 510 * A sync period of zero means Async. 511 */ 512 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0 513 || spi->sync_offset == 0 || spi->sync_period == 0) { 514 if (bootverbose) { 515 xpt_print(periph->path, "no sync rate available\n"); 516 } 517 return (0); 518 } 519 520 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) { 521 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 522 ("hit async: giving up on DV\n")); 523 return (0); 524 } 525 526 527 /* 528 * Jump sync_period up by one, but stop at 5MHz and fall back to Async. 529 * We don't try to remember 'last' settings to see if the SIM actually 530 * gets into the speed we want to set. We check on the SIM telling 531 * us that a requested speed is bad, but otherwise don't try and 532 * check the speed due to the asynchronous and handshake nature 533 * of speed setting. 534 */ 535 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET; 536 for (;;) { 537 spi->sync_period++; 538 if (spi->sync_period >= 0xf) { 539 spi->sync_period = 0; 540 spi->sync_offset = 0; 541 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 542 ("setting to async for DV\n")); 543 /* 544 * Once we hit async, we don't want to try 545 * any more settings. 546 */ 547 device->flags |= CAM_DEV_DV_HIT_BOTTOM; 548 } else if (bootverbose) { 549 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 550 ("DV: period 0x%x\n", spi->sync_period)); 551 printf("setting period to 0x%x\n", spi->sync_period); 552 } 553 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 554 cts.type = CTS_TYPE_CURRENT_SETTINGS; 555 xpt_action((union ccb *)&cts); 556 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 557 break; 558 } 559 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 560 ("DV: failed to set period 0x%x\n", spi->sync_period)); 561 if (spi->sync_period == 0) { 562 return (0); 563 } 564 } 565 return (1); 566} 567#endif 568static void 569probedone(struct cam_periph *periph, union ccb *done_ccb) 570{ 571 struct ata_params *ident_buf; 572 probe_softc *softc; 573 struct cam_path *path; 574 u_int32_t priority; 575 int found = 1; 576 577 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 578 579 softc = (probe_softc *)periph->softc; 580 path = done_ccb->ccb_h.path; 581 priority = done_ccb->ccb_h.pinfo.priority; 582 ident_buf = &path->device->ident_data; 583 584 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 585device_fail: if (cam_periph_error(done_ccb, 0, 0, 586 &softc->saved_ccb) == ERESTART) { 587 return; 588 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 589 /* Don't wedge the queue */ 590 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 591 /*run_queue*/TRUE); 592 } 593 /* Old PIO2 devices may not support mode setting. */ 594 if (softc->action == PROBE_SETMODE && 595 ata_max_pmode(ident_buf) <= ATA_PIO2 && 596 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) 597 goto noerror; 598 /* 599 * If we get to this point, we got an error status back 600 * from the inquiry and the error status doesn't require 601 * automatically retrying the command. Therefore, the 602 * inquiry failed. If we had inquiry information before 603 * for this device, but this latest inquiry command failed, 604 * the device has probably gone away. If this device isn't 605 * already marked unconfigured, notify the peripheral 606 * drivers that this device is no more. 607 */ 608 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 609 xpt_async(AC_LOST_DEVICE, path, NULL); 610 found = 0; 611 goto done; 612 } 613noerror: 614 switch (softc->action) { 615 case PROBE_RESET: 616 { 617 int sign = (done_ccb->ataio.res.lba_high << 8) + 618 done_ccb->ataio.res.lba_mid; 619 xpt_print(path, "SIGNATURE: %04x\n", sign); 620 if (sign == 0x0000 && 621 done_ccb->ccb_h.target_id != 15) { 622 path->device->protocol = PROTO_ATA; 623 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 624 } else if (sign == 0x9669 && 625 done_ccb->ccb_h.target_id == 15) { 626 struct ccb_trans_settings cts; 627 628 /* Report SIM that PM is present. */ 629 bzero(&cts, sizeof(cts)); 630 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 631 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 632 cts.type = CTS_TYPE_CURRENT_SETTINGS; 633 cts.xport_specific.sata.pm_present = 1; 634 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 635 xpt_action((union ccb *)&cts); 636 path->device->protocol = PROTO_SATAPM; 637 PROBE_SET_ACTION(softc, PROBE_PM_PID); 638 } else if (sign == 0xeb14 && 639 done_ccb->ccb_h.target_id != 15) { 640 path->device->protocol = PROTO_SCSI; 641 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 642 } else { 643 if (done_ccb->ccb_h.target_id != 15) { 644 xpt_print(path, 645 "Unexpected signature 0x%04x\n", sign); 646 } 647 goto device_fail; 648 } 649 xpt_release_ccb(done_ccb); 650 xpt_schedule(periph, priority); 651 return; 652 } 653 case PROBE_IDENTIFY: 654 { 655 int16_t *ptr; 656 657 for (ptr = (int16_t *)ident_buf; 658 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) { 659 *ptr = le16toh(*ptr); 660 } 661 if (strncmp(ident_buf->model, "FX", 2) && 662 strncmp(ident_buf->model, "NEC", 3) && 663 strncmp(ident_buf->model, "Pioneer", 7) && 664 strncmp(ident_buf->model, "SHARP", 5)) { 665 ata_bswap(ident_buf->model, sizeof(ident_buf->model)); 666 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision)); 667 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial)); 668 } 669 ata_btrim(ident_buf->model, sizeof(ident_buf->model)); 670 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model)); 671 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision)); 672 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision)); 673 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial)); 674 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial)); 675 676 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 677 /* Check that it is the same device. */ 678 MD5_CTX context; 679 u_int8_t digest[16]; 680 681 MD5Init(&context); 682 MD5Update(&context, 683 (unsigned char *)ident_buf->model, 684 sizeof(ident_buf->model)); 685 MD5Update(&context, 686 (unsigned char *)ident_buf->revision, 687 sizeof(ident_buf->revision)); 688 MD5Update(&context, 689 (unsigned char *)ident_buf->serial, 690 sizeof(ident_buf->serial)); 691 MD5Final(digest, &context); 692 if (bcmp(digest, softc->digest, sizeof(digest))) { 693 /* Device changed. */ 694 xpt_async(AC_LOST_DEVICE, path, NULL); 695 } 696 } else { 697 /* Clean up from previous instance of this device */ 698 if (path->device->serial_num != NULL) { 699 free(path->device->serial_num, M_CAMXPT); 700 path->device->serial_num = NULL; 701 path->device->serial_num_len = 0; 702 } 703 path->device->serial_num = 704 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1), 705 M_CAMXPT, M_NOWAIT); 706 if (path->device->serial_num != NULL) { 707 bcopy(ident_buf->serial, 708 path->device->serial_num, 709 sizeof(ident_buf->serial)); 710 path->device->serial_num[sizeof(ident_buf->serial)] 711 = '\0'; 712 path->device->serial_num_len = 713 strlen(path->device->serial_num); 714 } 715 716 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 717 } 718 ata_device_transport(path); 719 PROBE_SET_ACTION(softc, PROBE_SETMODE); 720 xpt_release_ccb(done_ccb); 721 xpt_schedule(periph, priority); 722 return; 723 } 724 case PROBE_SETMODE: 725 if (path->device->protocol == PROTO_ATA) { 726 PROBE_SET_ACTION(softc, PROBE_SET_MULTI); 727 } else { 728 PROBE_SET_ACTION(softc, PROBE_INQUIRY); 729 } 730 xpt_release_ccb(done_ccb); 731 xpt_schedule(periph, priority); 732 return; 733 case PROBE_SET_MULTI: 734 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 735 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 736 xpt_acquire_device(path->device); 737 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 738 xpt_action(done_ccb); 739 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 740 done_ccb); 741 } 742 break; 743 case PROBE_INQUIRY: 744 case PROBE_FULL_INQUIRY: 745 { 746 struct scsi_inquiry_data *inq_buf; 747 u_int8_t periph_qual, len; 748 749 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 750 inq_buf = &path->device->inq_data; 751 752 periph_qual = SID_QUAL(inq_buf); 753 754 if (periph_qual != SID_QUAL_LU_CONNECTED) 755 break; 756 757 /* 758 * We conservatively request only 759 * SHORT_INQUIRY_LEN bytes of inquiry 760 * information during our first try 761 * at sending an INQUIRY. If the device 762 * has more information to give, 763 * perform a second request specifying 764 * the amount of information the device 765 * is willing to give. 766 */ 767 len = inq_buf->additional_length 768 + offsetof(struct scsi_inquiry_data, additional_length) + 1; 769 if (softc->action == PROBE_INQUIRY 770 && len > SHORT_INQUIRY_LENGTH) { 771 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY); 772 xpt_release_ccb(done_ccb); 773 xpt_schedule(periph, priority); 774 return; 775 } 776 777 scsi_find_quirk(path->device); 778 ata_device_transport(path); 779 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 780 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 781 xpt_acquire_device(path->device); 782 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 783 xpt_action(done_ccb); 784 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb); 785 } 786 break; 787 } 788 case PROBE_PM_PID: 789 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0) 790 bzero(ident_buf, sizeof(*ident_buf)); 791 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) + 792 (done_ccb->ataio.res.lba_mid << 16) + 793 (done_ccb->ataio.res.lba_low << 8) + 794 done_ccb->ataio.res.sector_count; 795 ((uint32_t *)ident_buf)[0] = softc->pm_pid; 796 printf("PM Product ID: %08x\n", softc->pm_pid); 797 snprintf(ident_buf->model, sizeof(ident_buf->model), 798 "Port Multiplier %08x", softc->pm_pid); 799 PROBE_SET_ACTION(softc, PROBE_PM_PRV); 800 xpt_release_ccb(done_ccb); 801 xpt_schedule(periph, priority); 802 return; 803 case PROBE_PM_PRV: 804 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) + 805 (done_ccb->ataio.res.lba_mid << 16) + 806 (done_ccb->ataio.res.lba_low << 8) + 807 done_ccb->ataio.res.sector_count; 808 ((uint32_t *)ident_buf)[1] = softc->pm_prv; 809 printf("PM Revision: %08x\n", softc->pm_prv); 810 snprintf(ident_buf->revision, sizeof(ident_buf->revision), 811 "%04x", softc->pm_prv); 812 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 813 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 814 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 815 xpt_acquire_device(path->device); 816 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 817 xpt_action(done_ccb); 818 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 819 done_ccb); 820 } else { 821 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 822 xpt_action(done_ccb); 823 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb); 824 } 825 break; 826 case PROBE_INVALID: 827 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO, 828 ("probedone: invalid action state\n")); 829 default: 830 break; 831 } 832done: 833 xpt_release_ccb(done_ccb); 834 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 835 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe); 836 done_ccb->ccb_h.status = CAM_REQ_CMP; 837 done_ccb->ccb_h.ppriv_field1 = found; 838 xpt_done(done_ccb); 839 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) { 840 cam_periph_invalidate(periph); 841 cam_periph_release_locked(periph); 842 } else { 843 probeschedule(periph); 844 } 845} 846 847static void 848probecleanup(struct cam_periph *periph) 849{ 850 free(periph->softc, M_CAMXPT); 851} 852 853static void 854scsi_find_quirk(struct cam_ed *device) 855{ 856 struct scsi_quirk_entry *quirk; 857 caddr_t match; 858 859 match = cam_quirkmatch((caddr_t)&device->inq_data, 860 (caddr_t)scsi_quirk_table, 861 sizeof(scsi_quirk_table) / 862 sizeof(*scsi_quirk_table), 863 sizeof(*scsi_quirk_table), scsi_inquiry_match); 864 865 if (match == NULL) 866 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 867 868 quirk = (struct scsi_quirk_entry *)match; 869 device->quirk = quirk; 870 device->mintags = quirk->mintags; 871 device->maxtags = quirk->maxtags; 872} 873 874typedef struct { 875 union ccb *request_ccb; 876 struct ccb_pathinq *cpi; 877 int counter; 878 int found; 879} ata_scan_bus_info; 880 881/* 882 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 883 * As the scan progresses, xpt_scan_bus is used as the 884 * callback on completion function. 885 */ 886static void 887ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 888{ 889 struct cam_path *path; 890 ata_scan_bus_info *scan_info; 891 union ccb *work_ccb; 892 cam_status status; 893 894 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 895 ("xpt_scan_bus\n")); 896 switch (request_ccb->ccb_h.func_code) { 897 case XPT_SCAN_BUS: 898 /* Find out the characteristics of the bus */ 899 work_ccb = xpt_alloc_ccb_nowait(); 900 if (work_ccb == NULL) { 901 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 902 xpt_done(request_ccb); 903 return; 904 } 905 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 906 request_ccb->ccb_h.pinfo.priority); 907 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 908 xpt_action(work_ccb); 909 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 910 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 911 xpt_free_ccb(work_ccb); 912 xpt_done(request_ccb); 913 return; 914 } 915 916 /* Save some state for use while we probe for devices */ 917 scan_info = (ata_scan_bus_info *) 918 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT); 919 if (scan_info == NULL) { 920 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 921 xpt_done(request_ccb); 922 return; 923 } 924 scan_info->request_ccb = request_ccb; 925 scan_info->cpi = &work_ccb->cpi; 926 if (scan_info->cpi->transport == XPORT_ATA) 927 scan_info->found = 0x0003; 928 else 929 scan_info->found = 0x8001; 930 scan_info->counter = 0; 931 /* If PM supported, probe it first. */ 932 if (scan_info->cpi->hba_inquiry & PI_SATAPM) 933 scan_info->counter = 15; 934 935 work_ccb = xpt_alloc_ccb_nowait(); 936 if (work_ccb == NULL) { 937 free(scan_info, M_CAMXPT); 938 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 939 xpt_done(request_ccb); 940 break; 941 } 942 goto scan_next; 943 case XPT_SCAN_LUN: 944 work_ccb = request_ccb; 945 /* Reuse the same CCB to query if a device was really found */ 946 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0; 947 /* Free the current request path- we're done with it. */ 948 xpt_free_path(work_ccb->ccb_h.path); 949 /* If there is PMP... */ 950 if (scan_info->counter == 15) { 951 if (work_ccb->ccb_h.ppriv_field1 != 0) { 952 /* everything else willbe probed by it */ 953 scan_info->found = 0x8000; 954 } else { 955 struct ccb_trans_settings cts; 956 957 /* Report SIM that PM is absent. */ 958 bzero(&cts, sizeof(cts)); 959 xpt_setup_ccb(&cts.ccb_h, 960 scan_info->request_ccb->ccb_h.path, 1); 961 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 962 cts.type = CTS_TYPE_CURRENT_SETTINGS; 963 cts.xport_specific.sata.pm_present = 0; 964 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 965 xpt_action((union ccb *)&cts); 966 } 967 } 968take_next: 969 /* Take next device. Wrap from 15 (PM) to 0. */ 970 scan_info->counter = (scan_info->counter + 1 ) & 0x0f; 971 if (scan_info->counter > scan_info->cpi->max_target - 972 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) { 973 xpt_free_ccb(work_ccb); 974 xpt_free_ccb((union ccb *)scan_info->cpi); 975 request_ccb = scan_info->request_ccb; 976 free(scan_info, M_CAMXPT); 977 request_ccb->ccb_h.status = CAM_REQ_CMP; 978 xpt_done(request_ccb); 979 break; 980 } 981scan_next: 982 if ((scan_info->found & (1 << scan_info->counter)) == 0) 983 goto take_next; 984 status = xpt_create_path(&path, xpt_periph, 985 scan_info->request_ccb->ccb_h.path_id, 986 scan_info->counter, 0); 987 if (status != CAM_REQ_CMP) { 988 printf("xpt_scan_bus: xpt_create_path failed" 989 " with status %#x, bus scan halted\n", 990 status); 991 xpt_free_ccb(work_ccb); 992 xpt_free_ccb((union ccb *)scan_info->cpi); 993 request_ccb = scan_info->request_ccb; 994 free(scan_info, M_CAMXPT); 995 request_ccb->ccb_h.status = status; 996 xpt_done(request_ccb); 997 break; 998 } 999 xpt_setup_ccb(&work_ccb->ccb_h, path, 1000 scan_info->request_ccb->ccb_h.pinfo.priority); 1001 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1002 work_ccb->ccb_h.cbfcnp = ata_scan_bus; 1003 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 1004 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags; 1005 xpt_action(work_ccb); 1006 break; 1007 default: 1008 break; 1009 } 1010} 1011 1012static void 1013ata_scan_lun(struct cam_periph *periph, struct cam_path *path, 1014 cam_flags flags, union ccb *request_ccb) 1015{ 1016 struct ccb_pathinq cpi; 1017 cam_status status; 1018 struct cam_path *new_path; 1019 struct cam_periph *old_periph; 1020 1021 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 1022 ("xpt_scan_lun\n")); 1023 1024 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 1025 cpi.ccb_h.func_code = XPT_PATH_INQ; 1026 xpt_action((union ccb *)&cpi); 1027 1028 if (cpi.ccb_h.status != CAM_REQ_CMP) { 1029 if (request_ccb != NULL) { 1030 request_ccb->ccb_h.status = cpi.ccb_h.status; 1031 xpt_done(request_ccb); 1032 } 1033 return; 1034 } 1035 1036 if (request_ccb == NULL) { 1037 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT); 1038 if (request_ccb == NULL) { 1039 xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 1040 "can't continue\n"); 1041 return; 1042 } 1043 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT); 1044 if (new_path == NULL) { 1045 xpt_print(path, "xpt_scan_lun: can't allocate path, " 1046 "can't continue\n"); 1047 free(request_ccb, M_CAMXPT); 1048 return; 1049 } 1050 status = xpt_compile_path(new_path, xpt_periph, 1051 path->bus->path_id, 1052 path->target->target_id, 1053 path->device->lun_id); 1054 1055 if (status != CAM_REQ_CMP) { 1056 xpt_print(path, "xpt_scan_lun: can't compile path, " 1057 "can't continue\n"); 1058 free(request_ccb, M_CAMXPT); 1059 free(new_path, M_CAMXPT); 1060 return; 1061 } 1062 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL); 1063 request_ccb->ccb_h.cbfcnp = xptscandone; 1064 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1065 request_ccb->crcn.flags = flags; 1066 } 1067 1068 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) { 1069 probe_softc *softc; 1070 1071 softc = (probe_softc *)old_periph->softc; 1072 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 1073 periph_links.tqe); 1074 } else { 1075 status = cam_periph_alloc(proberegister, NULL, probecleanup, 1076 probestart, "aprobe", 1077 CAM_PERIPH_BIO, 1078 request_ccb->ccb_h.path, NULL, 0, 1079 request_ccb); 1080 1081 if (status != CAM_REQ_CMP) { 1082 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 1083 "returned an error, can't continue probe\n"); 1084 request_ccb->ccb_h.status = status; 1085 xpt_done(request_ccb); 1086 } 1087 } 1088} 1089 1090static void 1091xptscandone(struct cam_periph *periph, union ccb *done_ccb) 1092{ 1093 xpt_release_path(done_ccb->ccb_h.path); 1094 free(done_ccb->ccb_h.path, M_CAMXPT); 1095 free(done_ccb, M_CAMXPT); 1096} 1097 1098static struct cam_ed * 1099ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 1100{ 1101 struct cam_path path; 1102 struct scsi_quirk_entry *quirk; 1103 struct cam_ed *device; 1104 struct cam_ed *cur_device; 1105 1106 device = xpt_alloc_device(bus, target, lun_id); 1107 if (device == NULL) 1108 return (NULL); 1109 1110 /* 1111 * Take the default quirk entry until we have inquiry 1112 * data and can determine a better quirk to use. 1113 */ 1114 quirk = &scsi_quirk_table[scsi_quirk_table_size - 1]; 1115 device->quirk = (void *)quirk; 1116 device->mintags = quirk->mintags; 1117 device->maxtags = quirk->maxtags; 1118 bzero(&device->inq_data, sizeof(device->inq_data)); 1119 device->inq_flags = 0; 1120 device->queue_flags = 0; 1121 device->serial_num = NULL; 1122 device->serial_num_len = 0; 1123 1124 /* 1125 * XXX should be limited by number of CCBs this bus can 1126 * do. 1127 */ 1128 bus->sim->max_ccbs += device->ccbq.devq_openings; 1129 /* Insertion sort into our target's device list */ 1130 cur_device = TAILQ_FIRST(&target->ed_entries); 1131 while (cur_device != NULL && cur_device->lun_id < lun_id) 1132 cur_device = TAILQ_NEXT(cur_device, links); 1133 if (cur_device != NULL) { 1134 TAILQ_INSERT_BEFORE(cur_device, device, links); 1135 } else { 1136 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 1137 } 1138 target->generation++; 1139 if (lun_id != CAM_LUN_WILDCARD) { 1140 xpt_compile_path(&path, 1141 NULL, 1142 bus->path_id, 1143 target->target_id, 1144 lun_id); 1145 ata_device_transport(&path); 1146 xpt_release_path(&path); 1147 } 1148 1149 return (device); 1150} 1151 1152static void 1153ata_device_transport(struct cam_path *path) 1154{ 1155 struct ccb_pathinq cpi; 1156 struct ccb_trans_settings cts; 1157 struct scsi_inquiry_data *inq_buf = NULL; 1158 struct ata_params *ident_buf = NULL; 1159 1160 /* Get transport information from the SIM */ 1161 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 1162 cpi.ccb_h.func_code = XPT_PATH_INQ; 1163 xpt_action((union ccb *)&cpi); 1164 1165 path->device->transport = cpi.transport; 1166 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 1167 inq_buf = &path->device->inq_data; 1168 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0) 1169 ident_buf = &path->device->ident_data; 1170 if (path->device->protocol == PROTO_ATA) { 1171 path->device->protocol_version = ident_buf ? 1172 ata_version(ident_buf->version_major) : cpi.protocol_version; 1173 } else if (path->device->protocol == PROTO_SCSI) { 1174 path->device->protocol_version = inq_buf ? 1175 SID_ANSI_REV(inq_buf) : cpi.protocol_version; 1176 } 1177 path->device->transport_version = ident_buf ? 1178 ata_version(ident_buf->version_major) : cpi.transport_version; 1179 1180 /* Tell the controller what we think */ 1181 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 1182 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1183 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1184 cts.transport = path->device->transport; 1185 cts.transport_version = path->device->transport_version; 1186 cts.protocol = path->device->protocol; 1187 cts.protocol_version = path->device->protocol_version; 1188 cts.proto_specific.valid = 0; 1189 cts.xport_specific.valid = 0; 1190 xpt_action((union ccb *)&cts); 1191} 1192 1193static void 1194ata_action(union ccb *start_ccb) 1195{ 1196 1197 switch (start_ccb->ccb_h.func_code) { 1198 case XPT_SET_TRAN_SETTINGS: 1199 { 1200 scsi_set_transfer_settings(&start_ccb->cts, 1201 start_ccb->ccb_h.path->device, 1202 /*async_update*/FALSE); 1203 break; 1204 } 1205 case XPT_SCAN_BUS: 1206 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 1207 break; 1208 case XPT_SCAN_LUN: 1209 ata_scan_lun(start_ccb->ccb_h.path->periph, 1210 start_ccb->ccb_h.path, start_ccb->crcn.flags, 1211 start_ccb); 1212 break; 1213 case XPT_GET_TRAN_SETTINGS: 1214 { 1215 struct cam_sim *sim; 1216 1217 sim = start_ccb->ccb_h.path->bus->sim; 1218 (*(sim->sim_action))(sim, start_ccb); 1219 break; 1220 } 1221 default: 1222 xpt_action_default(start_ccb); 1223 break; 1224 } 1225} 1226 1227static void 1228scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 1229 int async_update) 1230{ 1231 struct ccb_pathinq cpi; 1232 struct ccb_trans_settings cur_cts; 1233 struct ccb_trans_settings_scsi *scsi; 1234 struct ccb_trans_settings_scsi *cur_scsi; 1235 struct cam_sim *sim; 1236 struct scsi_inquiry_data *inq_data; 1237 1238 if (device == NULL) { 1239 cts->ccb_h.status = CAM_PATH_INVALID; 1240 xpt_done((union ccb *)cts); 1241 return; 1242 } 1243 1244 if (cts->protocol == PROTO_UNKNOWN 1245 || cts->protocol == PROTO_UNSPECIFIED) { 1246 cts->protocol = device->protocol; 1247 cts->protocol_version = device->protocol_version; 1248 } 1249 1250 if (cts->protocol_version == PROTO_VERSION_UNKNOWN 1251 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 1252 cts->protocol_version = device->protocol_version; 1253 1254 if (cts->protocol != device->protocol) { 1255 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 1256 cts->protocol, device->protocol); 1257 cts->protocol = device->protocol; 1258 } 1259 1260 if (cts->protocol_version > device->protocol_version) { 1261 if (bootverbose) { 1262 xpt_print(cts->ccb_h.path, "Down reving Protocol " 1263 "Version from %d to %d?\n", cts->protocol_version, 1264 device->protocol_version); 1265 } 1266 cts->protocol_version = device->protocol_version; 1267 } 1268 1269 if (cts->transport == XPORT_UNKNOWN 1270 || cts->transport == XPORT_UNSPECIFIED) { 1271 cts->transport = device->transport; 1272 cts->transport_version = device->transport_version; 1273 } 1274 1275 if (cts->transport_version == XPORT_VERSION_UNKNOWN 1276 || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 1277 cts->transport_version = device->transport_version; 1278 1279 if (cts->transport != device->transport) { 1280 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 1281 cts->transport, device->transport); 1282 cts->transport = device->transport; 1283 } 1284 1285 if (cts->transport_version > device->transport_version) { 1286 if (bootverbose) { 1287 xpt_print(cts->ccb_h.path, "Down reving Transport " 1288 "Version from %d to %d?\n", cts->transport_version, 1289 device->transport_version); 1290 } 1291 cts->transport_version = device->transport_version; 1292 } 1293 1294 sim = cts->ccb_h.path->bus->sim; 1295 1296 /* 1297 * Nothing more of interest to do unless 1298 * this is a device connected via the 1299 * SCSI protocol. 1300 */ 1301 if (cts->protocol != PROTO_SCSI) { 1302 if (async_update == FALSE) 1303 (*(sim->sim_action))(sim, (union ccb *)cts); 1304 return; 1305 } 1306 1307 inq_data = &device->inq_data; 1308 scsi = &cts->proto_specific.scsi; 1309 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL); 1310 cpi.ccb_h.func_code = XPT_PATH_INQ; 1311 xpt_action((union ccb *)&cpi); 1312 1313 /* SCSI specific sanity checking */ 1314 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 1315 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0 1316 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 1317 || (device->mintags == 0)) { 1318 /* 1319 * Can't tag on hardware that doesn't support tags, 1320 * doesn't have it enabled, or has broken tag support. 1321 */ 1322 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1323 } 1324 1325 if (async_update == FALSE) { 1326 /* 1327 * Perform sanity checking against what the 1328 * controller and device can do. 1329 */ 1330 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL); 1331 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1332 cur_cts.type = cts->type; 1333 xpt_action((union ccb *)&cur_cts); 1334 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1335 return; 1336 } 1337 cur_scsi = &cur_cts.proto_specific.scsi; 1338 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 1339 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1340 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB; 1341 } 1342 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0) 1343 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1344 } 1345 1346 if (cts->type == CTS_TYPE_CURRENT_SETTINGS 1347 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 1348 int device_tagenb; 1349 1350 /* 1351 * If we are transitioning from tags to no-tags or 1352 * vice-versa, we need to carefully freeze and restart 1353 * the queue so that we don't overlap tagged and non-tagged 1354 * commands. We also temporarily stop tags if there is 1355 * a change in transfer negotiation settings to allow 1356 * "tag-less" negotiation. 1357 */ 1358 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 1359 || (device->inq_flags & SID_CmdQue) != 0) 1360 device_tagenb = TRUE; 1361 else 1362 device_tagenb = FALSE; 1363 1364 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 1365 && device_tagenb == FALSE) 1366 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0 1367 && device_tagenb == TRUE)) { 1368 1369 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) { 1370 /* 1371 * Delay change to use tags until after a 1372 * few commands have gone to this device so 1373 * the controller has time to perform transfer 1374 * negotiations without tagged messages getting 1375 * in the way. 1376 */ 1377 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 1378 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 1379 } else { 1380 struct ccb_relsim crs; 1381 1382 xpt_freeze_devq(cts->ccb_h.path, /*count*/1); 1383 device->inq_flags &= ~SID_CmdQue; 1384 xpt_dev_ccbq_resize(cts->ccb_h.path, 1385 sim->max_dev_openings); 1386 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 1387 device->tag_delay_count = 0; 1388 1389 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path, 1390 CAM_PRIORITY_NORMAL); 1391 crs.ccb_h.func_code = XPT_REL_SIMQ; 1392 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 1393 crs.openings 1394 = crs.release_timeout 1395 = crs.qfrozen_cnt 1396 = 0; 1397 xpt_action((union ccb *)&crs); 1398 } 1399 } 1400 } 1401 if (async_update == FALSE) 1402 (*(sim->sim_action))(sim, (union ccb *)cts); 1403} 1404 1405static void 1406scsi_toggle_tags(struct cam_path *path) 1407{ 1408 struct cam_ed *dev; 1409 1410 /* 1411 * Give controllers a chance to renegotiate 1412 * before starting tag operations. We 1413 * "toggle" tagged queuing off then on 1414 * which causes the tag enable command delay 1415 * counter to come into effect. 1416 */ 1417 dev = path->device; 1418 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 1419 || ((dev->inq_flags & SID_CmdQue) != 0 1420 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) { 1421 struct ccb_trans_settings cts; 1422 1423 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 1424 cts.protocol = PROTO_SCSI; 1425 cts.protocol_version = PROTO_VERSION_UNSPECIFIED; 1426 cts.transport = XPORT_UNSPECIFIED; 1427 cts.transport_version = XPORT_VERSION_UNSPECIFIED; 1428 cts.proto_specific.scsi.flags = 0; 1429 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ; 1430 scsi_set_transfer_settings(&cts, path->device, 1431 /*async_update*/TRUE); 1432 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB; 1433 scsi_set_transfer_settings(&cts, path->device, 1434 /*async_update*/TRUE); 1435 } 1436} 1437 1438/* 1439 * Handle any per-device event notifications that require action by the XPT. 1440 */ 1441static void 1442ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 1443 struct cam_ed *device, void *async_arg) 1444{ 1445 cam_status status; 1446 struct cam_path newpath; 1447 1448 /* 1449 * We only need to handle events for real devices. 1450 */ 1451 if (target->target_id == CAM_TARGET_WILDCARD 1452 || device->lun_id == CAM_LUN_WILDCARD) 1453 return; 1454 1455 /* 1456 * We need our own path with wildcards expanded to 1457 * handle certain types of events. 1458 */ 1459 if ((async_code == AC_SENT_BDR) 1460 || (async_code == AC_BUS_RESET) 1461 || (async_code == AC_INQ_CHANGED)) 1462 status = xpt_compile_path(&newpath, NULL, 1463 bus->path_id, 1464 target->target_id, 1465 device->lun_id); 1466 else 1467 status = CAM_REQ_CMP_ERR; 1468 1469 if (status == CAM_REQ_CMP) { 1470 1471 /* 1472 * Allow transfer negotiation to occur in a 1473 * tag free environment. 1474 */ 1475 if (async_code == AC_SENT_BDR 1476 || async_code == AC_BUS_RESET) 1477 scsi_toggle_tags(&newpath); 1478 1479 if (async_code == AC_INQ_CHANGED) { 1480 /* 1481 * We've sent a start unit command, or 1482 * something similar to a device that 1483 * may have caused its inquiry data to 1484 * change. So we re-scan the device to 1485 * refresh the inquiry data for it. 1486 */ 1487 ata_scan_lun(newpath.periph, &newpath, 1488 CAM_EXPECT_INQ_CHANGE, NULL); 1489 } 1490 xpt_release_path(&newpath); 1491 } else if (async_code == AC_LOST_DEVICE && 1492 (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1493 device->flags |= CAM_DEV_UNCONFIGURED; 1494 xpt_release_device(device); 1495 } else if (async_code == AC_TRANSFER_NEG) { 1496 struct ccb_trans_settings *settings; 1497 1498 settings = (struct ccb_trans_settings *)async_arg; 1499 scsi_set_transfer_settings(settings, device, 1500 /*async_update*/TRUE); 1501 } 1502} 1503 1504