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/interrupt.h> 41 #include <sys/sbuf.h> 42 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/sysctl.h> 46 47 #ifdef PC98 48 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */ 49 #endif 50 51 #include <cam/cam.h> 52 #include <cam/cam_ccb.h> 53 #include <cam/cam_queue.h> 54 #include <cam/cam_periph.h> 55 #include <cam/cam_sim.h> 56 #include <cam/cam_xpt.h> 57 #include <cam/cam_xpt_sim.h> 58 #include <cam/cam_xpt_periph.h> 59 #include <cam/cam_xpt_internal.h> 60 #include <cam/cam_debug.h> 61 62 #include <cam/scsi/scsi_all.h> 63 #include <cam/scsi/scsi_message.h> 64 #include <cam/ata/ata_all.h> 65 #include <machine/stdarg.h> /* for xpt_print below */ 66 #include "opt_cam.h" 67 68 struct ata_quirk_entry { 69 struct scsi_inquiry_pattern inq_pat; 70 u_int8_t quirks; 71 #define CAM_QUIRK_MAXTAGS 0x01 72 u_int maxtags; 73 }; 74 75 static periph_init_t probe_periph_init; 76 77 static struct periph_driver probe_driver = 78 { 79 probe_periph_init, "aprobe", 80 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0, 81 CAM_PERIPH_DRV_EARLY 82 }; 83 84 PERIPHDRIVER_DECLARE(aprobe, probe_driver); 85 86 typedef enum { 87 PROBE_RESET, 88 PROBE_IDENTIFY, 89 PROBE_SPINUP, 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_SPINUP", 103 "PROBE_SETMODE", 104 "PROBE_SET_MULTI", 105 "PROBE_INQUIRY", 106 "PROBE_FULL_INQUIRY", 107 "PROBE_PM_PID", 108 "PROBE_PM_PRV", 109 "PROBE_INVALID" 110 }; 111 112 #define PROBE_SET_ACTION(softc, newaction) \ 113 do { \ 114 char **text; \ 115 text = probe_action_text; \ 116 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \ 117 ("Probe %s to %s\n", text[(softc)->action], \ 118 text[(newaction)])); \ 119 (softc)->action = (newaction); \ 120 } while(0) 121 122 typedef enum { 123 PROBE_NO_ANNOUNCE = 0x04 124 } probe_flags; 125 126 typedef struct { 127 TAILQ_HEAD(, ccb_hdr) request_ccbs; 128 struct ata_params ident_data; 129 probe_action action; 130 probe_flags flags; 131 uint32_t pm_pid; 132 uint32_t pm_prv; 133 int restart; 134 int spinup; 135 struct cam_periph *periph; 136 } probe_softc; 137 138 static struct ata_quirk_entry ata_quirk_table[] = 139 { 140 { 141 /* Default tagged queuing parameters for all devices */ 142 { 143 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 144 /*vendor*/"*", /*product*/"*", /*revision*/"*" 145 }, 146 /*quirks*/0, /*maxtags*/0 147 }, 148 }; 149 150 static const int ata_quirk_table_size = 151 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table); 152 153 static cam_status proberegister(struct cam_periph *periph, 154 void *arg); 155 static void probeschedule(struct cam_periph *probe_periph); 156 static void probestart(struct cam_periph *periph, union ccb *start_ccb); 157 //static void proberequestdefaultnegotiation(struct cam_periph *periph); 158 //static int proberequestbackoff(struct cam_periph *periph, 159 // struct cam_ed *device); 160 static void probedone(struct cam_periph *periph, union ccb *done_ccb); 161 static void probecleanup(struct cam_periph *periph); 162 static void ata_find_quirk(struct cam_ed *device); 163 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb); 164 static void ata_scan_lun(struct cam_periph *periph, 165 struct cam_path *path, cam_flags flags, 166 union ccb *ccb); 167 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb); 168 static struct cam_ed * 169 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, 170 lun_id_t lun_id); 171 static void ata_device_transport(struct cam_path *path); 172 static void ata_set_transfer_settings(struct ccb_trans_settings *cts, 173 struct cam_ed *device, 174 int async_update); 175 static void ata_dev_async(u_int32_t async_code, 176 struct cam_eb *bus, 177 struct cam_et *target, 178 struct cam_ed *device, 179 void *async_arg); 180 static void ata_action(union ccb *start_ccb); 181 static void ata_announce_periph(struct cam_periph *periph); 182 183 static struct xpt_xport ata_xport = { 184 .alloc_device = ata_alloc_device, 185 .action = ata_action, 186 .async = ata_dev_async, 187 .announce = ata_announce_periph, 188 }; 189 190 struct xpt_xport * 191 ata_get_xport(void) 192 { 193 return (&ata_xport); 194 } 195 196 static void 197 probe_periph_init() 198 { 199 } 200 201 static cam_status 202 proberegister(struct cam_periph *periph, void *arg) 203 { 204 union ccb *request_ccb; /* CCB representing the probe request */ 205 cam_status status; 206 probe_softc *softc; 207 208 request_ccb = (union ccb *)arg; 209 if (periph == NULL) { 210 printf("proberegister: periph was NULL!!\n"); 211 return(CAM_REQ_CMP_ERR); 212 } 213 214 if (request_ccb == NULL) { 215 printf("proberegister: no probe CCB, " 216 "can't register device\n"); 217 return(CAM_REQ_CMP_ERR); 218 } 219 220 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT); 221 222 if (softc == NULL) { 223 printf("proberegister: Unable to probe new device. " 224 "Unable to allocate softc\n"); 225 return(CAM_REQ_CMP_ERR); 226 } 227 TAILQ_INIT(&softc->request_ccbs); 228 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 229 periph_links.tqe); 230 softc->flags = 0; 231 periph->softc = softc; 232 softc->periph = periph; 233 softc->action = PROBE_INVALID; 234 status = cam_periph_acquire(periph); 235 if (status != CAM_REQ_CMP) { 236 return (status); 237 } 238 /* 239 * Ensure nobody slip in until probe finish. 240 */ 241 cam_freeze_devq_arg(periph->path, 242 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1); 243 probeschedule(periph); 244 return(CAM_REQ_CMP); 245 } 246 247 static void 248 probeschedule(struct cam_periph *periph) 249 { 250 union ccb *ccb; 251 probe_softc *softc; 252 253 softc = (probe_softc *)periph->softc; 254 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 255 256 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) || 257 periph->path->device->protocol == PROTO_SATAPM) 258 PROBE_SET_ACTION(softc, PROBE_RESET); 259 else 260 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 261 262 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 263 softc->flags |= PROBE_NO_ANNOUNCE; 264 else 265 softc->flags &= ~PROBE_NO_ANNOUNCE; 266 267 xpt_schedule(periph, CAM_PRIORITY_XPT); 268 } 269 270 static void 271 probestart(struct cam_periph *periph, union ccb *start_ccb) 272 { 273 struct ccb_trans_settings cts; 274 struct ccb_ataio *ataio; 275 struct ccb_scsiio *csio; 276 probe_softc *softc; 277 struct cam_path *path; 278 struct ata_params *ident_buf; 279 280 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 281 282 softc = (probe_softc *)periph->softc; 283 path = start_ccb->ccb_h.path; 284 ataio = &start_ccb->ataio; 285 csio = &start_ccb->csio; 286 ident_buf = &periph->path->device->ident_data; 287 288 if (softc->restart) { 289 softc->restart = 0; 290 if ((path->device->flags & CAM_DEV_UNCONFIGURED) || 291 path->device->protocol == PROTO_SATAPM) 292 softc->action = PROBE_RESET; 293 else 294 softc->action = PROBE_IDENTIFY; 295 } 296 switch (softc->action) { 297 case PROBE_RESET: 298 cam_fill_ataio(ataio, 299 0, 300 probedone, 301 /*flags*/CAM_DIR_NONE, 302 0, 303 /*data_ptr*/NULL, 304 /*dxfer_len*/0, 305 15 * 1000); 306 ata_reset_cmd(ataio); 307 break; 308 case PROBE_IDENTIFY: 309 cam_fill_ataio(ataio, 310 1, 311 probedone, 312 /*flags*/CAM_DIR_IN, 313 0, 314 /*data_ptr*/(u_int8_t *)&softc->ident_data, 315 /*dxfer_len*/sizeof(softc->ident_data), 316 30 * 1000); 317 if (periph->path->device->protocol == PROTO_ATA) 318 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0); 319 else 320 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0); 321 break; 322 case PROBE_SPINUP: 323 if (bootverbose) 324 xpt_print(path, "Spinning up device\n"); 325 cam_fill_ataio(ataio, 326 1, 327 probedone, 328 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER, 329 0, 330 /*data_ptr*/NULL, 331 /*dxfer_len*/0, 332 30 * 1000); 333 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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_NONE); 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 ((!softc->restart) && 699 cam_periph_error(done_ccb, 0, 0, NULL) == 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 if (softc->restart) 728 goto done; 729 switch (softc->action) { 730 case PROBE_RESET: 731 { 732 int sign = (done_ccb->ataio.res.lba_high << 8) + 733 done_ccb->ataio.res.lba_mid; 734 if (bootverbose) 735 xpt_print(path, "SIGNATURE: %04x\n", sign); 736 if (sign == 0x0000 && 737 done_ccb->ccb_h.target_id != 15) { 738 path->device->protocol = PROTO_ATA; 739 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 740 } else if (sign == 0x9669 && 741 done_ccb->ccb_h.target_id == 15) { 742 /* Report SIM that PM is present. */ 743 bzero(&cts, sizeof(cts)); 744 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 745 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 746 cts.type = CTS_TYPE_CURRENT_SETTINGS; 747 cts.xport_specific.sata.pm_present = 1; 748 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 749 xpt_action((union ccb *)&cts); 750 path->device->protocol = PROTO_SATAPM; 751 PROBE_SET_ACTION(softc, PROBE_PM_PID); 752 } else if (sign == 0xeb14 && 753 done_ccb->ccb_h.target_id != 15) { 754 path->device->protocol = PROTO_SCSI; 755 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 756 } else { 757 if (done_ccb->ccb_h.target_id != 15) { 758 xpt_print(path, 759 "Unexpected signature 0x%04x\n", sign); 760 } 761 goto device_fail; 762 } 763 xpt_release_ccb(done_ccb); 764 xpt_schedule(periph, priority); 765 return; 766 } 767 case PROBE_IDENTIFY: 768 { 769 int16_t *ptr; 770 771 ident_buf = &softc->ident_data; 772 for (ptr = (int16_t *)ident_buf; 773 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) { 774 *ptr = le16toh(*ptr); 775 } 776 if (strncmp(ident_buf->model, "FX", 2) && 777 strncmp(ident_buf->model, "NEC", 3) && 778 strncmp(ident_buf->model, "Pioneer", 7) && 779 strncmp(ident_buf->model, "SHARP", 5)) { 780 ata_bswap(ident_buf->model, sizeof(ident_buf->model)); 781 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision)); 782 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial)); 783 } 784 ata_btrim(ident_buf->model, sizeof(ident_buf->model)); 785 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model)); 786 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision)); 787 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision)); 788 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial)); 789 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial)); 790 /* Device may need spin-up before IDENTIFY become valid. */ 791 if ((ident_buf->specconf == 0x37c8 || 792 ident_buf->specconf == 0x738c) && 793 ((ident_buf->config & ATA_RESP_INCOMPLETE) || 794 softc->spinup == 0)) { 795 PROBE_SET_ACTION(softc, PROBE_SPINUP); 796 xpt_release_ccb(done_ccb); 797 xpt_schedule(periph, priority); 798 return; 799 } 800 ident_buf = &path->device->ident_data; 801 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 802 /* Check that it is the same device. */ 803 if (bcmp(softc->ident_data.model, ident_buf->model, 804 sizeof(ident_buf->model)) || 805 bcmp(softc->ident_data.revision, ident_buf->revision, 806 sizeof(ident_buf->revision)) || 807 bcmp(softc->ident_data.serial, ident_buf->serial, 808 sizeof(ident_buf->serial))) { 809 /* Device changed. */ 810 xpt_async(AC_LOST_DEVICE, path, NULL); 811 } else 812 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 813 } else { 814 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 815 /* Clean up from previous instance of this device */ 816 if (path->device->serial_num != NULL) { 817 free(path->device->serial_num, M_CAMXPT); 818 path->device->serial_num = NULL; 819 path->device->serial_num_len = 0; 820 } 821 path->device->serial_num = 822 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1), 823 M_CAMXPT, M_NOWAIT); 824 if (path->device->serial_num != NULL) { 825 bcopy(ident_buf->serial, 826 path->device->serial_num, 827 sizeof(ident_buf->serial)); 828 path->device->serial_num[sizeof(ident_buf->serial)] 829 = '\0'; 830 path->device->serial_num_len = 831 strlen(path->device->serial_num); 832 } 833 834 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 835 } 836 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) { 837 path->device->mintags = path->device->maxtags = 838 ATA_QUEUE_LEN(ident_buf->queue) + 1; 839 } 840 ata_find_quirk(path->device); 841 if (path->device->mintags != 0 && 842 path->bus->sim->max_tagged_dev_openings != 0) { 843 /* Report SIM which tags are allowed. */ 844 bzero(&cts, sizeof(cts)); 845 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 846 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 847 cts.type = CTS_TYPE_CURRENT_SETTINGS; 848 cts.xport_specific.sata.tags = path->device->maxtags; 849 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS; 850 xpt_action((union ccb *)&cts); 851 /* Reconfigure queues for tagged queueing. */ 852 xpt_start_tags(path); 853 } 854 ata_device_transport(path); 855 PROBE_SET_ACTION(softc, PROBE_SETMODE); 856 xpt_release_ccb(done_ccb); 857 xpt_schedule(periph, priority); 858 return; 859 } 860 case PROBE_SPINUP: 861 if (bootverbose) 862 xpt_print(path, "Spin-up done\n"); 863 softc->spinup = 1; 864 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 865 xpt_release_ccb(done_ccb); 866 xpt_schedule(periph, priority); 867 return; 868 case PROBE_SETMODE: 869 if (path->device->protocol == PROTO_ATA) { 870 PROBE_SET_ACTION(softc, PROBE_SET_MULTI); 871 } else { 872 PROBE_SET_ACTION(softc, PROBE_INQUIRY); 873 } 874 xpt_release_ccb(done_ccb); 875 xpt_schedule(periph, priority); 876 return; 877 case PROBE_SET_MULTI: 878 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 879 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 880 xpt_acquire_device(path->device); 881 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 882 xpt_action(done_ccb); 883 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 884 done_ccb); 885 } 886 break; 887 case PROBE_INQUIRY: 888 case PROBE_FULL_INQUIRY: 889 { 890 struct scsi_inquiry_data *inq_buf; 891 u_int8_t periph_qual, len; 892 893 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 894 inq_buf = &path->device->inq_data; 895 896 periph_qual = SID_QUAL(inq_buf); 897 898 if (periph_qual != SID_QUAL_LU_CONNECTED) 899 break; 900 901 /* 902 * We conservatively request only 903 * SHORT_INQUIRY_LEN bytes of inquiry 904 * information during our first try 905 * at sending an INQUIRY. If the device 906 * has more information to give, 907 * perform a second request specifying 908 * the amount of information the device 909 * is willing to give. 910 */ 911 len = inq_buf->additional_length 912 + offsetof(struct scsi_inquiry_data, additional_length) + 1; 913 if (softc->action == PROBE_INQUIRY 914 && len > SHORT_INQUIRY_LENGTH) { 915 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY); 916 xpt_release_ccb(done_ccb); 917 xpt_schedule(periph, priority); 918 return; 919 } 920 921 ata_device_transport(path); 922 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 923 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 924 xpt_acquire_device(path->device); 925 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 926 xpt_action(done_ccb); 927 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb); 928 } 929 break; 930 } 931 case PROBE_PM_PID: 932 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0) 933 bzero(ident_buf, sizeof(*ident_buf)); 934 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) + 935 (done_ccb->ataio.res.lba_mid << 16) + 936 (done_ccb->ataio.res.lba_low << 8) + 937 done_ccb->ataio.res.sector_count; 938 ((uint32_t *)ident_buf)[0] = softc->pm_pid; 939 snprintf(ident_buf->model, sizeof(ident_buf->model), 940 "Port Multiplier %08x", softc->pm_pid); 941 PROBE_SET_ACTION(softc, PROBE_PM_PRV); 942 xpt_release_ccb(done_ccb); 943 xpt_schedule(periph, priority); 944 return; 945 case PROBE_PM_PRV: 946 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) + 947 (done_ccb->ataio.res.lba_mid << 16) + 948 (done_ccb->ataio.res.lba_low << 8) + 949 done_ccb->ataio.res.sector_count; 950 ((uint32_t *)ident_buf)[1] = softc->pm_prv; 951 snprintf(ident_buf->revision, sizeof(ident_buf->revision), 952 "%04x", softc->pm_prv); 953 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 954 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 955 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 956 xpt_acquire_device(path->device); 957 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 958 xpt_action(done_ccb); 959 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 960 done_ccb); 961 } else { 962 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 963 xpt_action(done_ccb); 964 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb); 965 } 966 break; 967 case PROBE_INVALID: 968 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO, 969 ("probedone: invalid action state\n")); 970 default: 971 break; 972 } 973 done: 974 if (softc->restart) { 975 softc->restart = 0; 976 xpt_release_ccb(done_ccb); 977 probeschedule(periph); 978 return; 979 } 980 xpt_release_ccb(done_ccb); 981 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) { 982 TAILQ_REMOVE(&softc->request_ccbs, 983 &done_ccb->ccb_h, periph_links.tqe); 984 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR; 985 xpt_done(done_ccb); 986 } 987 cam_release_devq(periph->path, 988 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE); 989 cam_periph_invalidate(periph); 990 cam_periph_release_locked(periph); 991 } 992 993 static void 994 probecleanup(struct cam_periph *periph) 995 { 996 free(periph->softc, M_CAMXPT); 997 } 998 999 static void 1000 ata_find_quirk(struct cam_ed *device) 1001 { 1002 struct ata_quirk_entry *quirk; 1003 caddr_t match; 1004 1005 match = cam_quirkmatch((caddr_t)&device->ident_data, 1006 (caddr_t)ata_quirk_table, 1007 ata_quirk_table_size, 1008 sizeof(*ata_quirk_table), ata_identify_match); 1009 1010 if (match == NULL) 1011 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 1012 1013 quirk = (struct ata_quirk_entry *)match; 1014 device->quirk = quirk; 1015 if (quirk->quirks & CAM_QUIRK_MAXTAGS) 1016 device->mintags = device->maxtags = quirk->maxtags; 1017 } 1018 1019 typedef struct { 1020 union ccb *request_ccb; 1021 struct ccb_pathinq *cpi; 1022 int counter; 1023 } ata_scan_bus_info; 1024 1025 /* 1026 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 1027 * As the scan progresses, xpt_scan_bus is used as the 1028 * callback on completion function. 1029 */ 1030 static void 1031 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 1032 { 1033 struct cam_path *path; 1034 ata_scan_bus_info *scan_info; 1035 union ccb *work_ccb, *reset_ccb; 1036 cam_status status; 1037 1038 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 1039 ("xpt_scan_bus\n")); 1040 switch (request_ccb->ccb_h.func_code) { 1041 case XPT_SCAN_BUS: 1042 /* Find out the characteristics of the bus */ 1043 work_ccb = xpt_alloc_ccb_nowait(); 1044 if (work_ccb == NULL) { 1045 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1046 xpt_done(request_ccb); 1047 return; 1048 } 1049 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 1050 request_ccb->ccb_h.pinfo.priority); 1051 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 1052 xpt_action(work_ccb); 1053 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 1054 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 1055 xpt_free_ccb(work_ccb); 1056 xpt_done(request_ccb); 1057 return; 1058 } 1059 1060 /* We may need to reset bus first, if we haven't done it yet. */ 1061 if ((work_ccb->cpi.hba_inquiry & 1062 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) && 1063 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) && 1064 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) { 1065 reset_ccb = xpt_alloc_ccb_nowait(); 1066 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path, 1067 CAM_PRIORITY_NONE); 1068 reset_ccb->ccb_h.func_code = XPT_RESET_BUS; 1069 xpt_action(reset_ccb); 1070 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) { 1071 request_ccb->ccb_h.status = reset_ccb->ccb_h.status; 1072 xpt_free_ccb(reset_ccb); 1073 xpt_free_ccb(work_ccb); 1074 xpt_done(request_ccb); 1075 return; 1076 } 1077 xpt_free_ccb(reset_ccb); 1078 } 1079 1080 /* Save some state for use while we probe for devices */ 1081 scan_info = (ata_scan_bus_info *) 1082 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT); 1083 if (scan_info == NULL) { 1084 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1085 xpt_done(request_ccb); 1086 return; 1087 } 1088 scan_info->request_ccb = request_ccb; 1089 scan_info->cpi = &work_ccb->cpi; 1090 /* If PM supported, probe it first. */ 1091 if (scan_info->cpi->hba_inquiry & PI_SATAPM) 1092 scan_info->counter = scan_info->cpi->max_target; 1093 else 1094 scan_info->counter = 0; 1095 1096 work_ccb = xpt_alloc_ccb_nowait(); 1097 if (work_ccb == NULL) { 1098 free(scan_info, M_CAMXPT); 1099 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1100 xpt_done(request_ccb); 1101 break; 1102 } 1103 goto scan_next; 1104 case XPT_SCAN_LUN: 1105 work_ccb = request_ccb; 1106 /* Reuse the same CCB to query if a device was really found */ 1107 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0; 1108 /* Free the current request path- we're done with it. */ 1109 xpt_free_path(work_ccb->ccb_h.path); 1110 /* If there is PMP... */ 1111 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) && 1112 (scan_info->counter == scan_info->cpi->max_target)) { 1113 if (work_ccb->ccb_h.status == CAM_REQ_CMP) { 1114 /* everything else willbe probed by it */ 1115 goto done; 1116 } else { 1117 struct ccb_trans_settings cts; 1118 1119 /* Report SIM that PM is absent. */ 1120 bzero(&cts, sizeof(cts)); 1121 xpt_setup_ccb(&cts.ccb_h, 1122 scan_info->request_ccb->ccb_h.path, 1); 1123 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1124 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1125 cts.xport_specific.sata.pm_present = 0; 1126 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 1127 xpt_action((union ccb *)&cts); 1128 } 1129 } 1130 if (scan_info->counter == 1131 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1132 0 : scan_info->cpi->max_target)) { 1133 done: 1134 xpt_free_ccb(work_ccb); 1135 xpt_free_ccb((union ccb *)scan_info->cpi); 1136 request_ccb = scan_info->request_ccb; 1137 free(scan_info, M_CAMXPT); 1138 request_ccb->ccb_h.status = CAM_REQ_CMP; 1139 xpt_done(request_ccb); 1140 break; 1141 } 1142 /* Take next device. Wrap from max (PMP) to 0. */ 1143 scan_info->counter = (scan_info->counter + 1 ) % 1144 (scan_info->cpi->max_target + 1); 1145 scan_next: 1146 status = xpt_create_path(&path, xpt_periph, 1147 scan_info->request_ccb->ccb_h.path_id, 1148 scan_info->counter, 0); 1149 if (status != CAM_REQ_CMP) { 1150 printf("xpt_scan_bus: xpt_create_path failed" 1151 " with status %#x, bus scan halted\n", 1152 status); 1153 xpt_free_ccb(work_ccb); 1154 xpt_free_ccb((union ccb *)scan_info->cpi); 1155 request_ccb = scan_info->request_ccb; 1156 free(scan_info, M_CAMXPT); 1157 request_ccb->ccb_h.status = status; 1158 xpt_done(request_ccb); 1159 break; 1160 } 1161 xpt_setup_ccb(&work_ccb->ccb_h, path, 1162 scan_info->request_ccb->ccb_h.pinfo.priority); 1163 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1164 work_ccb->ccb_h.cbfcnp = ata_scan_bus; 1165 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 1166 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags; 1167 xpt_action(work_ccb); 1168 break; 1169 default: 1170 break; 1171 } 1172 } 1173 1174 static void 1175 ata_scan_lun(struct cam_periph *periph, struct cam_path *path, 1176 cam_flags flags, union ccb *request_ccb) 1177 { 1178 struct ccb_pathinq cpi; 1179 cam_status status; 1180 struct cam_path *new_path; 1181 struct cam_periph *old_periph; 1182 1183 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n")); 1184 1185 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 1186 cpi.ccb_h.func_code = XPT_PATH_INQ; 1187 xpt_action((union ccb *)&cpi); 1188 1189 if (cpi.ccb_h.status != CAM_REQ_CMP) { 1190 if (request_ccb != NULL) { 1191 request_ccb->ccb_h.status = cpi.ccb_h.status; 1192 xpt_done(request_ccb); 1193 } 1194 return; 1195 } 1196 1197 if (request_ccb == NULL) { 1198 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT); 1199 if (request_ccb == NULL) { 1200 xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 1201 "can't continue\n"); 1202 return; 1203 } 1204 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT); 1205 if (new_path == NULL) { 1206 xpt_print(path, "xpt_scan_lun: can't allocate path, " 1207 "can't continue\n"); 1208 free(request_ccb, M_CAMXPT); 1209 return; 1210 } 1211 status = xpt_compile_path(new_path, xpt_periph, 1212 path->bus->path_id, 1213 path->target->target_id, 1214 path->device->lun_id); 1215 1216 if (status != CAM_REQ_CMP) { 1217 xpt_print(path, "xpt_scan_lun: can't compile path, " 1218 "can't continue\n"); 1219 free(request_ccb, M_CAMXPT); 1220 free(new_path, M_CAMXPT); 1221 return; 1222 } 1223 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT); 1224 request_ccb->ccb_h.cbfcnp = xptscandone; 1225 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1226 request_ccb->crcn.flags = flags; 1227 } 1228 1229 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) { 1230 probe_softc *softc; 1231 1232 softc = (probe_softc *)old_periph->softc; 1233 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 1234 periph_links.tqe); 1235 softc->restart = 1; 1236 } else { 1237 status = cam_periph_alloc(proberegister, NULL, probecleanup, 1238 probestart, "aprobe", 1239 CAM_PERIPH_BIO, 1240 request_ccb->ccb_h.path, NULL, 0, 1241 request_ccb); 1242 1243 if (status != CAM_REQ_CMP) { 1244 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 1245 "returned an error, can't continue probe\n"); 1246 request_ccb->ccb_h.status = status; 1247 xpt_done(request_ccb); 1248 } 1249 } 1250 } 1251 1252 static void 1253 xptscandone(struct cam_periph *periph, union ccb *done_ccb) 1254 { 1255 xpt_release_path(done_ccb->ccb_h.path); 1256 free(done_ccb->ccb_h.path, M_CAMXPT); 1257 free(done_ccb, M_CAMXPT); 1258 } 1259 1260 static struct cam_ed * 1261 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 1262 { 1263 struct cam_path path; 1264 struct ata_quirk_entry *quirk; 1265 struct cam_ed *device; 1266 struct cam_ed *cur_device; 1267 1268 device = xpt_alloc_device(bus, target, lun_id); 1269 if (device == NULL) 1270 return (NULL); 1271 1272 /* 1273 * Take the default quirk entry until we have inquiry 1274 * data and can determine a better quirk to use. 1275 */ 1276 quirk = &ata_quirk_table[ata_quirk_table_size - 1]; 1277 device->quirk = (void *)quirk; 1278 device->mintags = 0; 1279 device->maxtags = 0; 1280 bzero(&device->inq_data, sizeof(device->inq_data)); 1281 device->inq_flags = 0; 1282 device->queue_flags = 0; 1283 device->serial_num = NULL; 1284 device->serial_num_len = 0; 1285 1286 /* 1287 * XXX should be limited by number of CCBs this bus can 1288 * do. 1289 */ 1290 bus->sim->max_ccbs += device->ccbq.devq_openings; 1291 /* Insertion sort into our target's device list */ 1292 cur_device = TAILQ_FIRST(&target->ed_entries); 1293 while (cur_device != NULL && cur_device->lun_id < lun_id) 1294 cur_device = TAILQ_NEXT(cur_device, links); 1295 if (cur_device != NULL) { 1296 TAILQ_INSERT_BEFORE(cur_device, device, links); 1297 } else { 1298 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 1299 } 1300 target->generation++; 1301 if (lun_id != CAM_LUN_WILDCARD) { 1302 xpt_compile_path(&path, 1303 NULL, 1304 bus->path_id, 1305 target->target_id, 1306 lun_id); 1307 ata_device_transport(&path); 1308 xpt_release_path(&path); 1309 } 1310 1311 return (device); 1312 } 1313 1314 static void 1315 ata_device_transport(struct cam_path *path) 1316 { 1317 struct ccb_pathinq cpi; 1318 struct ccb_trans_settings cts; 1319 struct scsi_inquiry_data *inq_buf = NULL; 1320 struct ata_params *ident_buf = NULL; 1321 1322 /* Get transport information from the SIM */ 1323 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 1324 cpi.ccb_h.func_code = XPT_PATH_INQ; 1325 xpt_action((union ccb *)&cpi); 1326 1327 path->device->transport = cpi.transport; 1328 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 1329 inq_buf = &path->device->inq_data; 1330 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0) 1331 ident_buf = &path->device->ident_data; 1332 if (path->device->protocol == PROTO_ATA) { 1333 path->device->protocol_version = ident_buf ? 1334 ata_version(ident_buf->version_major) : cpi.protocol_version; 1335 } else if (path->device->protocol == PROTO_SCSI) { 1336 path->device->protocol_version = inq_buf ? 1337 SID_ANSI_REV(inq_buf) : cpi.protocol_version; 1338 } 1339 path->device->transport_version = ident_buf ? 1340 ata_version(ident_buf->version_major) : cpi.transport_version; 1341 1342 /* Tell the controller what we think */ 1343 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 1344 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1345 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1346 cts.transport = path->device->transport; 1347 cts.transport_version = path->device->transport_version; 1348 cts.protocol = path->device->protocol; 1349 cts.protocol_version = path->device->protocol_version; 1350 cts.proto_specific.valid = 0; 1351 if (ident_buf) { 1352 if (path->device->transport == XPORT_ATA) { 1353 cts.xport_specific.ata.atapi = 1354 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 1355 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 1356 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI; 1357 } else { 1358 cts.xport_specific.sata.atapi = 1359 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 1360 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 1361 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI; 1362 } 1363 } else 1364 cts.xport_specific.valid = 0; 1365 xpt_action((union ccb *)&cts); 1366 } 1367 1368 static void 1369 ata_action(union ccb *start_ccb) 1370 { 1371 1372 switch (start_ccb->ccb_h.func_code) { 1373 case XPT_SET_TRAN_SETTINGS: 1374 { 1375 ata_set_transfer_settings(&start_ccb->cts, 1376 start_ccb->ccb_h.path->device, 1377 /*async_update*/FALSE); 1378 break; 1379 } 1380 case XPT_SCAN_BUS: 1381 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 1382 break; 1383 case XPT_SCAN_LUN: 1384 ata_scan_lun(start_ccb->ccb_h.path->periph, 1385 start_ccb->ccb_h.path, start_ccb->crcn.flags, 1386 start_ccb); 1387 break; 1388 case XPT_GET_TRAN_SETTINGS: 1389 { 1390 struct cam_sim *sim; 1391 1392 sim = start_ccb->ccb_h.path->bus->sim; 1393 (*(sim->sim_action))(sim, start_ccb); 1394 break; 1395 } 1396 case XPT_SCSI_IO: 1397 { 1398 struct cam_ed *device; 1399 u_int maxlen = 0; 1400 1401 device = start_ccb->ccb_h.path->device; 1402 if (device->protocol == PROTO_SCSI && 1403 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) { 1404 uint16_t p = 1405 device->ident_data.config & ATA_PROTO_MASK; 1406 1407 maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 : 1408 (p == ATA_PROTO_ATAPI_12) ? 12 : 0; 1409 } 1410 if (start_ccb->csio.cdb_len > maxlen) { 1411 start_ccb->ccb_h.status = CAM_REQ_INVALID; 1412 xpt_done(start_ccb); 1413 break; 1414 } 1415 /* FALLTHROUGH */ 1416 } 1417 default: 1418 xpt_action_default(start_ccb); 1419 break; 1420 } 1421 } 1422 1423 static void 1424 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 1425 int async_update) 1426 { 1427 struct ccb_pathinq cpi; 1428 struct ccb_trans_settings cur_cts; 1429 struct ccb_trans_settings_scsi *scsi; 1430 struct ccb_trans_settings_scsi *cur_scsi; 1431 struct cam_sim *sim; 1432 struct scsi_inquiry_data *inq_data; 1433 1434 if (device == NULL) { 1435 cts->ccb_h.status = CAM_PATH_INVALID; 1436 xpt_done((union ccb *)cts); 1437 return; 1438 } 1439 1440 if (cts->protocol == PROTO_UNKNOWN 1441 || cts->protocol == PROTO_UNSPECIFIED) { 1442 cts->protocol = device->protocol; 1443 cts->protocol_version = device->protocol_version; 1444 } 1445 1446 if (cts->protocol_version == PROTO_VERSION_UNKNOWN 1447 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 1448 cts->protocol_version = device->protocol_version; 1449 1450 if (cts->protocol != device->protocol) { 1451 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 1452 cts->protocol, device->protocol); 1453 cts->protocol = device->protocol; 1454 } 1455 1456 if (cts->protocol_version > device->protocol_version) { 1457 if (bootverbose) { 1458 xpt_print(cts->ccb_h.path, "Down reving Protocol " 1459 "Version from %d to %d?\n", cts->protocol_version, 1460 device->protocol_version); 1461 } 1462 cts->protocol_version = device->protocol_version; 1463 } 1464 1465 if (cts->transport == XPORT_UNKNOWN 1466 || cts->transport == XPORT_UNSPECIFIED) { 1467 cts->transport = device->transport; 1468 cts->transport_version = device->transport_version; 1469 } 1470 1471 if (cts->transport_version == XPORT_VERSION_UNKNOWN 1472 || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 1473 cts->transport_version = device->transport_version; 1474 1475 if (cts->transport != device->transport) { 1476 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 1477 cts->transport, device->transport); 1478 cts->transport = device->transport; 1479 } 1480 1481 if (cts->transport_version > device->transport_version) { 1482 if (bootverbose) { 1483 xpt_print(cts->ccb_h.path, "Down reving Transport " 1484 "Version from %d to %d?\n", cts->transport_version, 1485 device->transport_version); 1486 } 1487 cts->transport_version = device->transport_version; 1488 } 1489 1490 sim = cts->ccb_h.path->bus->sim; 1491 1492 /* 1493 * Nothing more of interest to do unless 1494 * this is a device connected via the 1495 * SCSI protocol. 1496 */ 1497 if (cts->protocol != PROTO_SCSI) { 1498 if (async_update == FALSE) 1499 (*(sim->sim_action))(sim, (union ccb *)cts); 1500 return; 1501 } 1502 1503 inq_data = &device->inq_data; 1504 scsi = &cts->proto_specific.scsi; 1505 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE); 1506 cpi.ccb_h.func_code = XPT_PATH_INQ; 1507 xpt_action((union ccb *)&cpi); 1508 1509 /* SCSI specific sanity checking */ 1510 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 1511 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0 1512 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 1513 || (device->mintags == 0)) { 1514 /* 1515 * Can't tag on hardware that doesn't support tags, 1516 * doesn't have it enabled, or has broken tag support. 1517 */ 1518 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1519 } 1520 1521 if (async_update == FALSE) { 1522 /* 1523 * Perform sanity checking against what the 1524 * controller and device can do. 1525 */ 1526 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE); 1527 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1528 cur_cts.type = cts->type; 1529 xpt_action((union ccb *)&cur_cts); 1530 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1531 return; 1532 } 1533 cur_scsi = &cur_cts.proto_specific.scsi; 1534 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 1535 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1536 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB; 1537 } 1538 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0) 1539 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1540 } 1541 1542 if (cts->type == CTS_TYPE_CURRENT_SETTINGS 1543 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 1544 int device_tagenb; 1545 1546 /* 1547 * If we are transitioning from tags to no-tags or 1548 * vice-versa, we need to carefully freeze and restart 1549 * the queue so that we don't overlap tagged and non-tagged 1550 * commands. We also temporarily stop tags if there is 1551 * a change in transfer negotiation settings to allow 1552 * "tag-less" negotiation. 1553 */ 1554 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 1555 || (device->inq_flags & SID_CmdQue) != 0) 1556 device_tagenb = TRUE; 1557 else 1558 device_tagenb = FALSE; 1559 1560 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 1561 && device_tagenb == FALSE) 1562 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0 1563 && device_tagenb == TRUE)) { 1564 1565 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) { 1566 /* 1567 * Delay change to use tags until after a 1568 * few commands have gone to this device so 1569 * the controller has time to perform transfer 1570 * negotiations without tagged messages getting 1571 * in the way. 1572 */ 1573 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 1574 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 1575 } else { 1576 xpt_stop_tags(cts->ccb_h.path); 1577 } 1578 } 1579 } 1580 if (async_update == FALSE) 1581 (*(sim->sim_action))(sim, (union ccb *)cts); 1582 } 1583 1584 /* 1585 * Handle any per-device event notifications that require action by the XPT. 1586 */ 1587 static void 1588 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 1589 struct cam_ed *device, void *async_arg) 1590 { 1591 cam_status status; 1592 struct cam_path newpath; 1593 1594 /* 1595 * We only need to handle events for real devices. 1596 */ 1597 if (target->target_id == CAM_TARGET_WILDCARD 1598 || device->lun_id == CAM_LUN_WILDCARD) 1599 return; 1600 1601 /* 1602 * We need our own path with wildcards expanded to 1603 * handle certain types of events. 1604 */ 1605 if ((async_code == AC_SENT_BDR) 1606 || (async_code == AC_BUS_RESET) 1607 || (async_code == AC_INQ_CHANGED)) 1608 status = xpt_compile_path(&newpath, NULL, 1609 bus->path_id, 1610 target->target_id, 1611 device->lun_id); 1612 else 1613 status = CAM_REQ_CMP_ERR; 1614 1615 if (status == CAM_REQ_CMP) { 1616 if (async_code == AC_INQ_CHANGED) { 1617 /* 1618 * We've sent a start unit command, or 1619 * something similar to a device that 1620 * may have caused its inquiry data to 1621 * change. So we re-scan the device to 1622 * refresh the inquiry data for it. 1623 */ 1624 ata_scan_lun(newpath.periph, &newpath, 1625 CAM_EXPECT_INQ_CHANGE, NULL); 1626 } else { 1627 /* We need to reinitialize device after reset. */ 1628 ata_scan_lun(newpath.periph, &newpath, 1629 0, NULL); 1630 } 1631 xpt_release_path(&newpath); 1632 } else if (async_code == AC_LOST_DEVICE && 1633 (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1634 device->flags |= CAM_DEV_UNCONFIGURED; 1635 xpt_release_device(device); 1636 } else if (async_code == AC_TRANSFER_NEG) { 1637 struct ccb_trans_settings *settings; 1638 1639 settings = (struct ccb_trans_settings *)async_arg; 1640 ata_set_transfer_settings(settings, device, 1641 /*async_update*/TRUE); 1642 } 1643 } 1644 1645 static void 1646 ata_announce_periph(struct cam_periph *periph) 1647 { 1648 struct ccb_pathinq cpi; 1649 struct ccb_trans_settings cts; 1650 struct cam_path *path = periph->path; 1651 u_int speed; 1652 u_int mb; 1653 1654 mtx_assert(periph->sim->mtx, MA_OWNED); 1655 1656 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 1657 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1658 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1659 xpt_action((union ccb*)&cts); 1660 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 1661 return; 1662 /* Ask the SIM for its base transfer speed */ 1663 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 1664 cpi.ccb_h.func_code = XPT_PATH_INQ; 1665 xpt_action((union ccb *)&cpi); 1666 /* Report connection speed */ 1667 speed = cpi.base_transfer_speed; 1668 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 1669 struct ccb_trans_settings_ata *ata = 1670 &cts.xport_specific.ata; 1671 1672 if (ata->valid & CTS_ATA_VALID_MODE) 1673 speed = ata_mode2speed(ata->mode); 1674 } 1675 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 1676 struct ccb_trans_settings_sata *sata = 1677 &cts.xport_specific.sata; 1678 1679 if (sata->valid & CTS_SATA_VALID_REVISION) 1680 speed = ata_revision2speed(sata->revision); 1681 } 1682 mb = speed / 1000; 1683 if (mb > 0) 1684 printf("%s%d: %d.%03dMB/s transfers", 1685 periph->periph_name, periph->unit_number, 1686 mb, speed % 1000); 1687 else 1688 printf("%s%d: %dKB/s transfers", periph->periph_name, 1689 periph->unit_number, speed); 1690 /* Report additional information about connection */ 1691 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 1692 struct ccb_trans_settings_ata *ata = 1693 &cts.xport_specific.ata; 1694 1695 printf(" ("); 1696 if (ata->valid & CTS_ATA_VALID_MODE) 1697 printf("%s, ", ata_mode2string(ata->mode)); 1698 if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0) 1699 printf("ATAPI %dbytes, ", ata->atapi); 1700 if (ata->valid & CTS_ATA_VALID_BYTECOUNT) 1701 printf("PIO %dbytes", ata->bytecount); 1702 printf(")"); 1703 } 1704 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 1705 struct ccb_trans_settings_sata *sata = 1706 &cts.xport_specific.sata; 1707 1708 printf(" ("); 1709 if (sata->valid & CTS_SATA_VALID_REVISION) 1710 printf("SATA %d.x, ", sata->revision); 1711 else 1712 printf("SATA, "); 1713 if (sata->valid & CTS_SATA_VALID_MODE) 1714 printf("%s, ", ata_mode2string(sata->mode)); 1715 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0) 1716 printf("ATAPI %dbytes, ", sata->atapi); 1717 if (sata->valid & CTS_SATA_VALID_BYTECOUNT) 1718 printf("PIO %dbytes", sata->bytecount); 1719 printf(")"); 1720 } 1721 printf("\n"); 1722 } 1723 1724