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