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 #include <cam/cam.h> 48 #include <cam/cam_ccb.h> 49 #include <cam/cam_queue.h> 50 #include <cam/cam_periph.h> 51 #include <cam/cam_sim.h> 52 #include <cam/cam_xpt.h> 53 #include <cam/cam_xpt_sim.h> 54 #include <cam/cam_xpt_periph.h> 55 #include <cam/cam_xpt_internal.h> 56 #include <cam/cam_debug.h> 57 58 #include <cam/scsi/scsi_all.h> 59 #include <cam/scsi/scsi_message.h> 60 #include <cam/ata/ata_all.h> 61 #include <machine/stdarg.h> /* for xpt_print below */ 62 #include "opt_cam.h" 63 64 struct ata_quirk_entry { 65 struct scsi_inquiry_pattern inq_pat; 66 u_int8_t quirks; 67 #define CAM_QUIRK_MAXTAGS 0x01 68 u_int mintags; 69 u_int maxtags; 70 }; 71 72 static periph_init_t probe_periph_init; 73 74 static struct periph_driver probe_driver = 75 { 76 probe_periph_init, "aprobe", 77 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0, 78 CAM_PERIPH_DRV_EARLY 79 }; 80 81 PERIPHDRIVER_DECLARE(aprobe, probe_driver); 82 83 typedef enum { 84 PROBE_RESET, 85 PROBE_IDENTIFY, 86 PROBE_SPINUP, 87 PROBE_SETMODE, 88 PROBE_SETPM, 89 PROBE_SETAPST, 90 PROBE_SETDMAAA, 91 PROBE_SETAN, 92 PROBE_SET_MULTI, 93 PROBE_INQUIRY, 94 PROBE_FULL_INQUIRY, 95 PROBE_PM_PID, 96 PROBE_PM_PRV, 97 PROBE_IDENTIFY_SES, 98 PROBE_IDENTIFY_SAFTE, 99 PROBE_INVALID 100 } probe_action; 101 102 static char *probe_action_text[] = { 103 "PROBE_RESET", 104 "PROBE_IDENTIFY", 105 "PROBE_SPINUP", 106 "PROBE_SETMODE", 107 "PROBE_SETPM", 108 "PROBE_SETAPST", 109 "PROBE_SETDMAAA", 110 "PROBE_SETAN", 111 "PROBE_SET_MULTI", 112 "PROBE_INQUIRY", 113 "PROBE_FULL_INQUIRY", 114 "PROBE_PM_PID", 115 "PROBE_PM_PRV", 116 "PROBE_IDENTIFY_SES", 117 "PROBE_IDENTIFY_SAFTE", 118 "PROBE_INVALID" 119 }; 120 121 #define PROBE_SET_ACTION(softc, newaction) \ 122 do { \ 123 char **text; \ 124 text = probe_action_text; \ 125 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \ 126 ("Probe %s to %s\n", text[(softc)->action], \ 127 text[(newaction)])); \ 128 (softc)->action = (newaction); \ 129 } while(0) 130 131 typedef enum { 132 PROBE_NO_ANNOUNCE = 0x04 133 } probe_flags; 134 135 typedef struct { 136 TAILQ_HEAD(, ccb_hdr) request_ccbs; 137 struct ata_params ident_data; 138 probe_action action; 139 probe_flags flags; 140 uint32_t pm_pid; 141 uint32_t pm_prv; 142 int restart; 143 int spinup; 144 int faults; 145 u_int caps; 146 struct cam_periph *periph; 147 } probe_softc; 148 149 static struct ata_quirk_entry ata_quirk_table[] = 150 { 151 { 152 /* Default tagged queuing parameters for all devices */ 153 { 154 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 155 /*vendor*/"*", /*product*/"*", /*revision*/"*" 156 }, 157 /*quirks*/0, /*mintags*/0, /*maxtags*/0 158 }, 159 }; 160 161 static const int ata_quirk_table_size = 162 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table); 163 164 static cam_status proberegister(struct cam_periph *periph, 165 void *arg); 166 static void probeschedule(struct cam_periph *probe_periph); 167 static void probestart(struct cam_periph *periph, union ccb *start_ccb); 168 static void proberequestdefaultnegotiation(struct cam_periph *periph); 169 static void probedone(struct cam_periph *periph, union ccb *done_ccb); 170 static void probecleanup(struct cam_periph *periph); 171 static void ata_find_quirk(struct cam_ed *device); 172 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb); 173 static void ata_scan_lun(struct cam_periph *periph, 174 struct cam_path *path, cam_flags flags, 175 union ccb *ccb); 176 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb); 177 static struct cam_ed * 178 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, 179 lun_id_t lun_id); 180 static void ata_device_transport(struct cam_path *path); 181 static void ata_get_transfer_settings(struct ccb_trans_settings *cts); 182 static void ata_set_transfer_settings(struct ccb_trans_settings *cts, 183 struct cam_ed *device, 184 int async_update); 185 static void ata_dev_async(u_int32_t async_code, 186 struct cam_eb *bus, 187 struct cam_et *target, 188 struct cam_ed *device, 189 void *async_arg); 190 static void ata_action(union ccb *start_ccb); 191 static void ata_announce_periph(struct cam_periph *periph); 192 193 static int ata_dma = 1; 194 static int atapi_dma = 1; 195 196 TUNABLE_INT("hw.ata.ata_dma", &ata_dma); 197 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma); 198 199 static struct xpt_xport ata_xport = { 200 .alloc_device = ata_alloc_device, 201 .action = ata_action, 202 .async = ata_dev_async, 203 .announce = ata_announce_periph, 204 }; 205 206 struct xpt_xport * 207 ata_get_xport(void) 208 { 209 return (&ata_xport); 210 } 211 212 static void 213 probe_periph_init() 214 { 215 } 216 217 static cam_status 218 proberegister(struct cam_periph *periph, void *arg) 219 { 220 union ccb *request_ccb; /* CCB representing the probe request */ 221 cam_status status; 222 probe_softc *softc; 223 224 request_ccb = (union ccb *)arg; 225 if (periph == NULL) { 226 printf("proberegister: periph was NULL!!\n"); 227 return(CAM_REQ_CMP_ERR); 228 } 229 230 if (request_ccb == NULL) { 231 printf("proberegister: no probe CCB, " 232 "can't register device\n"); 233 return(CAM_REQ_CMP_ERR); 234 } 235 236 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT); 237 238 if (softc == NULL) { 239 printf("proberegister: Unable to probe new device. " 240 "Unable to allocate softc\n"); 241 return(CAM_REQ_CMP_ERR); 242 } 243 TAILQ_INIT(&softc->request_ccbs); 244 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 245 periph_links.tqe); 246 softc->flags = 0; 247 periph->softc = softc; 248 softc->periph = periph; 249 softc->action = PROBE_INVALID; 250 status = cam_periph_acquire(periph); 251 if (status != CAM_REQ_CMP) { 252 return (status); 253 } 254 /* 255 * Ensure nobody slip in until probe finish. 256 */ 257 cam_freeze_devq_arg(periph->path, 258 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1); 259 probeschedule(periph); 260 return(CAM_REQ_CMP); 261 } 262 263 static void 264 probeschedule(struct cam_periph *periph) 265 { 266 union ccb *ccb; 267 probe_softc *softc; 268 269 softc = (probe_softc *)periph->softc; 270 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 271 272 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) || 273 periph->path->device->protocol == PROTO_SATAPM || 274 periph->path->device->protocol == PROTO_SEMB) 275 PROBE_SET_ACTION(softc, PROBE_RESET); 276 else 277 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 278 279 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 280 softc->flags |= PROBE_NO_ANNOUNCE; 281 else 282 softc->flags &= ~PROBE_NO_ANNOUNCE; 283 284 xpt_schedule(periph, CAM_PRIORITY_XPT); 285 } 286 287 static void 288 probestart(struct cam_periph *periph, union ccb *start_ccb) 289 { 290 struct ccb_trans_settings cts; 291 struct ccb_ataio *ataio; 292 struct ccb_scsiio *csio; 293 probe_softc *softc; 294 struct cam_path *path; 295 struct ata_params *ident_buf; 296 297 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 298 299 softc = (probe_softc *)periph->softc; 300 path = start_ccb->ccb_h.path; 301 ataio = &start_ccb->ataio; 302 csio = &start_ccb->csio; 303 ident_buf = &periph->path->device->ident_data; 304 305 if (softc->restart) { 306 softc->restart = 0; 307 if ((path->device->flags & CAM_DEV_UNCONFIGURED) || 308 path->device->protocol == PROTO_SATAPM || 309 path->device->protocol == PROTO_SEMB) 310 softc->action = PROBE_RESET; 311 else 312 softc->action = PROBE_IDENTIFY; 313 } 314 switch (softc->action) { 315 case PROBE_RESET: 316 cam_fill_ataio(ataio, 317 0, 318 probedone, 319 /*flags*/CAM_DIR_NONE, 320 0, 321 /*data_ptr*/NULL, 322 /*dxfer_len*/0, 323 15 * 1000); 324 ata_reset_cmd(ataio); 325 break; 326 case PROBE_IDENTIFY: 327 cam_fill_ataio(ataio, 328 1, 329 probedone, 330 /*flags*/CAM_DIR_IN, 331 0, 332 /*data_ptr*/(u_int8_t *)&softc->ident_data, 333 /*dxfer_len*/sizeof(softc->ident_data), 334 30 * 1000); 335 if (periph->path->device->protocol == PROTO_ATA) 336 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0); 337 else 338 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0); 339 break; 340 case PROBE_SPINUP: 341 if (bootverbose) 342 xpt_print(path, "Spinning up device\n"); 343 cam_fill_ataio(ataio, 344 1, 345 probedone, 346 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER, 347 0, 348 /*data_ptr*/NULL, 349 /*dxfer_len*/0, 350 30 * 1000); 351 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0); 352 break; 353 case PROBE_SETMODE: 354 { 355 int mode, wantmode; 356 357 mode = 0; 358 /* Fetch user modes from SIM. */ 359 bzero(&cts, sizeof(cts)); 360 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 361 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 362 cts.type = CTS_TYPE_USER_SETTINGS; 363 xpt_action((union ccb *)&cts); 364 if (path->device->transport == XPORT_ATA) { 365 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE) 366 mode = cts.xport_specific.ata.mode; 367 } else { 368 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE) 369 mode = cts.xport_specific.sata.mode; 370 } 371 if (periph->path->device->protocol == PROTO_ATA) { 372 if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX)) 373 mode = ATA_PIO_MAX; 374 } else { 375 if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX)) 376 mode = ATA_PIO_MAX; 377 } 378 negotiate: 379 /* Honor device capabilities. */ 380 wantmode = mode = ata_max_mode(ident_buf, mode); 381 /* Report modes to SIM. */ 382 bzero(&cts, sizeof(cts)); 383 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 384 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 385 cts.type = CTS_TYPE_CURRENT_SETTINGS; 386 if (path->device->transport == XPORT_ATA) { 387 cts.xport_specific.ata.mode = mode; 388 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE; 389 } else { 390 cts.xport_specific.sata.mode = mode; 391 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE; 392 } 393 xpt_action((union ccb *)&cts); 394 /* Fetch current modes from SIM. */ 395 bzero(&cts, sizeof(cts)); 396 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 397 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 398 cts.type = CTS_TYPE_CURRENT_SETTINGS; 399 xpt_action((union ccb *)&cts); 400 if (path->device->transport == XPORT_ATA) { 401 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE) 402 mode = cts.xport_specific.ata.mode; 403 } else { 404 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE) 405 mode = cts.xport_specific.sata.mode; 406 } 407 /* If SIM disagree - renegotiate. */ 408 if (mode != wantmode) 409 goto negotiate; 410 /* Remember what transport thinks about DMA. */ 411 if (mode < ATA_DMA) 412 path->device->inq_flags &= ~SID_DMA; 413 else 414 path->device->inq_flags |= SID_DMA; 415 xpt_async(AC_GETDEV_CHANGED, path, NULL); 416 cam_fill_ataio(ataio, 417 1, 418 probedone, 419 /*flags*/CAM_DIR_NONE, 420 0, 421 /*data_ptr*/NULL, 422 /*dxfer_len*/0, 423 30 * 1000); 424 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 425 break; 426 } 427 case PROBE_SETPM: 428 cam_fill_ataio(ataio, 429 1, 430 probedone, 431 CAM_DIR_NONE, 432 0, 433 NULL, 434 0, 435 30*1000); 436 ata_28bit_cmd(ataio, ATA_SETFEATURES, 437 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90, 438 0, 0x03); 439 break; 440 case PROBE_SETAPST: 441 cam_fill_ataio(ataio, 442 1, 443 probedone, 444 CAM_DIR_NONE, 445 0, 446 NULL, 447 0, 448 30*1000); 449 ata_28bit_cmd(ataio, ATA_SETFEATURES, 450 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90, 451 0, 0x07); 452 break; 453 case PROBE_SETDMAAA: 454 cam_fill_ataio(ataio, 455 1, 456 probedone, 457 CAM_DIR_NONE, 458 0, 459 NULL, 460 0, 461 30*1000); 462 ata_28bit_cmd(ataio, ATA_SETFEATURES, 463 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90, 464 0, 0x02); 465 break; 466 case PROBE_SETAN: 467 cam_fill_ataio(ataio, 468 1, 469 probedone, 470 CAM_DIR_NONE, 471 0, 472 NULL, 473 0, 474 30*1000); 475 ata_28bit_cmd(ataio, ATA_SETFEATURES, 476 (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90, 477 0, 0x05); 478 break; 479 case PROBE_SET_MULTI: 480 { 481 u_int sectors, bytecount; 482 483 bytecount = 8192; /* SATA maximum */ 484 /* Fetch user bytecount from SIM. */ 485 bzero(&cts, sizeof(cts)); 486 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 487 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 488 cts.type = CTS_TYPE_USER_SETTINGS; 489 xpt_action((union ccb *)&cts); 490 if (path->device->transport == XPORT_ATA) { 491 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 492 bytecount = cts.xport_specific.ata.bytecount; 493 } else { 494 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 495 bytecount = cts.xport_specific.sata.bytecount; 496 } 497 /* Honor device capabilities. */ 498 sectors = max(1, min(ident_buf->sectors_intr & 0xff, 499 bytecount / ata_logical_sector_size(ident_buf))); 500 /* Report bytecount to SIM. */ 501 bzero(&cts, sizeof(cts)); 502 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 503 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 504 cts.type = CTS_TYPE_CURRENT_SETTINGS; 505 if (path->device->transport == XPORT_ATA) { 506 cts.xport_specific.ata.bytecount = sectors * 507 ata_logical_sector_size(ident_buf); 508 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 509 } else { 510 cts.xport_specific.sata.bytecount = sectors * 511 ata_logical_sector_size(ident_buf); 512 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 513 } 514 xpt_action((union ccb *)&cts); 515 /* Fetch current bytecount from SIM. */ 516 bzero(&cts, sizeof(cts)); 517 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 518 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 519 cts.type = CTS_TYPE_CURRENT_SETTINGS; 520 xpt_action((union ccb *)&cts); 521 if (path->device->transport == XPORT_ATA) { 522 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 523 bytecount = cts.xport_specific.ata.bytecount; 524 } else { 525 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 526 bytecount = cts.xport_specific.sata.bytecount; 527 } 528 sectors = bytecount / ata_logical_sector_size(ident_buf); 529 530 cam_fill_ataio(ataio, 531 1, 532 probedone, 533 CAM_DIR_NONE, 534 0, 535 NULL, 536 0, 537 30*1000); 538 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors); 539 break; 540 } 541 case PROBE_INQUIRY: 542 { 543 u_int bytecount; 544 545 bytecount = 8192; /* SATA maximum */ 546 /* Fetch user bytecount from SIM. */ 547 bzero(&cts, sizeof(cts)); 548 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 549 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 550 cts.type = CTS_TYPE_USER_SETTINGS; 551 xpt_action((union ccb *)&cts); 552 if (path->device->transport == XPORT_ATA) { 553 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 554 bytecount = cts.xport_specific.ata.bytecount; 555 } else { 556 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 557 bytecount = cts.xport_specific.sata.bytecount; 558 } 559 /* Honor device capabilities. */ 560 bytecount &= ~1; 561 bytecount = max(2, min(65534, bytecount)); 562 if (ident_buf->satacapabilities != 0x0000 && 563 ident_buf->satacapabilities != 0xffff) { 564 bytecount = min(8192, bytecount); 565 } 566 /* Report bytecount to SIM. */ 567 bzero(&cts, sizeof(cts)); 568 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 569 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 570 cts.type = CTS_TYPE_CURRENT_SETTINGS; 571 if (path->device->transport == XPORT_ATA) { 572 cts.xport_specific.ata.bytecount = bytecount; 573 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 574 } else { 575 cts.xport_specific.sata.bytecount = bytecount; 576 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 577 } 578 xpt_action((union ccb *)&cts); 579 /* FALLTHROUGH */ 580 } 581 case PROBE_FULL_INQUIRY: 582 { 583 u_int inquiry_len; 584 struct scsi_inquiry_data *inq_buf = 585 &periph->path->device->inq_data; 586 587 if (softc->action == PROBE_INQUIRY) 588 inquiry_len = SHORT_INQUIRY_LENGTH; 589 else 590 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf); 591 /* 592 * Some parallel SCSI devices fail to send an 593 * ignore wide residue message when dealing with 594 * odd length inquiry requests. Round up to be 595 * safe. 596 */ 597 inquiry_len = roundup2(inquiry_len, 2); 598 scsi_inquiry(csio, 599 /*retries*/1, 600 probedone, 601 MSG_SIMPLE_Q_TAG, 602 (u_int8_t *)inq_buf, 603 inquiry_len, 604 /*evpd*/FALSE, 605 /*page_code*/0, 606 SSD_MIN_SIZE, 607 /*timeout*/60 * 1000); 608 break; 609 } 610 case PROBE_PM_PID: 611 cam_fill_ataio(ataio, 612 1, 613 probedone, 614 /*flags*/CAM_DIR_NONE, 615 0, 616 /*data_ptr*/NULL, 617 /*dxfer_len*/0, 618 10 * 1000); 619 ata_pm_read_cmd(ataio, 0, 15); 620 break; 621 case PROBE_PM_PRV: 622 cam_fill_ataio(ataio, 623 1, 624 probedone, 625 /*flags*/CAM_DIR_NONE, 626 0, 627 /*data_ptr*/NULL, 628 /*dxfer_len*/0, 629 10 * 1000); 630 ata_pm_read_cmd(ataio, 1, 15); 631 break; 632 case PROBE_IDENTIFY_SES: 633 cam_fill_ataio(ataio, 634 1, 635 probedone, 636 /*flags*/CAM_DIR_IN, 637 0, 638 /*data_ptr*/(u_int8_t *)&softc->ident_data, 639 /*dxfer_len*/sizeof(softc->ident_data), 640 30 * 1000); 641 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x02, 642 sizeof(softc->ident_data) / 4); 643 break; 644 case PROBE_IDENTIFY_SAFTE: 645 cam_fill_ataio(ataio, 646 1, 647 probedone, 648 /*flags*/CAM_DIR_IN, 649 0, 650 /*data_ptr*/(u_int8_t *)&softc->ident_data, 651 /*dxfer_len*/sizeof(softc->ident_data), 652 30 * 1000); 653 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x00, 654 sizeof(softc->ident_data) / 4); 655 break; 656 case PROBE_INVALID: 657 CAM_DEBUG(path, CAM_DEBUG_INFO, 658 ("probestart: invalid action state\n")); 659 default: 660 break; 661 } 662 xpt_action(start_ccb); 663 } 664 665 static void 666 proberequestdefaultnegotiation(struct cam_periph *periph) 667 { 668 struct ccb_trans_settings cts; 669 670 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE); 671 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 672 cts.type = CTS_TYPE_USER_SETTINGS; 673 xpt_action((union ccb *)&cts); 674 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 675 return; 676 cts.xport_specific.valid = 0; 677 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 678 cts.type = CTS_TYPE_CURRENT_SETTINGS; 679 xpt_action((union ccb *)&cts); 680 } 681 682 static void 683 probedone(struct cam_periph *periph, union ccb *done_ccb) 684 { 685 struct ccb_trans_settings cts; 686 struct ata_params *ident_buf; 687 struct scsi_inquiry_data *inq_buf; 688 probe_softc *softc; 689 struct cam_path *path; 690 cam_status status; 691 u_int32_t priority; 692 u_int caps; 693 int changed = 1, found = 1; 694 static const uint8_t fake_device_id_hdr[8] = 695 {0, SVPD_DEVICE_ID, 0, 12, 696 SVPD_ID_CODESET_BINARY, SVPD_ID_TYPE_NAA, 0, 8}; 697 698 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 699 700 softc = (probe_softc *)periph->softc; 701 path = done_ccb->ccb_h.path; 702 priority = done_ccb->ccb_h.pinfo.priority; 703 ident_buf = &path->device->ident_data; 704 inq_buf = &path->device->inq_data; 705 706 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 707 if (softc->restart) { 708 if (bootverbose) { 709 cam_error_print(done_ccb, 710 CAM_ESF_ALL, CAM_EPF_ALL); 711 } 712 } else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) 713 return; 714 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 715 /* Don't wedge the queue */ 716 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 717 /*run_queue*/TRUE); 718 } 719 status = done_ccb->ccb_h.status & CAM_STATUS_MASK; 720 if (softc->restart) { 721 softc->faults++; 722 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == 723 CAM_CMD_TIMEOUT) 724 softc->faults += 4; 725 if (softc->faults < 10) 726 goto done; 727 else 728 softc->restart = 0; 729 730 /* Old PIO2 devices may not support mode setting. */ 731 } else if (softc->action == PROBE_SETMODE && 732 status == CAM_ATA_STATUS_ERROR && 733 ata_max_pmode(ident_buf) <= ATA_PIO2 && 734 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) { 735 goto noerror; 736 737 /* 738 * Some old WD SATA disks report supported and enabled 739 * device-initiated interface power management, but return 740 * ABORT on attempt to disable it. 741 */ 742 } else if (softc->action == PROBE_SETPM && 743 status == CAM_ATA_STATUS_ERROR) { 744 goto noerror; 745 746 /* 747 * Some HP SATA disks report supported DMA Auto-Activation, 748 * but return ABORT on attempt to enable it. 749 */ 750 } else if (softc->action == PROBE_SETDMAAA && 751 status == CAM_ATA_STATUS_ERROR) { 752 goto noerror; 753 754 /* 755 * SES and SAF-TE SEPs have different IDENTIFY commands, 756 * but SATA specification doesn't tell how to identify them. 757 * Until better way found, just try another if first fail. 758 */ 759 } else if (softc->action == PROBE_IDENTIFY_SES && 760 status == CAM_ATA_STATUS_ERROR) { 761 PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE); 762 xpt_release_ccb(done_ccb); 763 xpt_schedule(periph, priority); 764 return; 765 } 766 767 /* 768 * If we get to this point, we got an error status back 769 * from the inquiry and the error status doesn't require 770 * automatically retrying the command. Therefore, the 771 * inquiry failed. If we had inquiry information before 772 * for this device, but this latest inquiry command failed, 773 * the device has probably gone away. If this device isn't 774 * already marked unconfigured, notify the peripheral 775 * drivers that this device is no more. 776 */ 777 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 778 xpt_async(AC_LOST_DEVICE, path, NULL); 779 found = 0; 780 goto done; 781 } 782 noerror: 783 if (softc->restart) 784 goto done; 785 switch (softc->action) { 786 case PROBE_RESET: 787 { 788 int sign = (done_ccb->ataio.res.lba_high << 8) + 789 done_ccb->ataio.res.lba_mid; 790 if (bootverbose) 791 xpt_print(path, "SIGNATURE: %04x\n", sign); 792 if (sign == 0x0000 && 793 done_ccb->ccb_h.target_id != 15) { 794 path->device->protocol = PROTO_ATA; 795 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 796 } else if (sign == 0x9669 && 797 done_ccb->ccb_h.target_id == 15) { 798 /* Report SIM that PM is present. */ 799 bzero(&cts, sizeof(cts)); 800 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 801 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 802 cts.type = CTS_TYPE_CURRENT_SETTINGS; 803 cts.xport_specific.sata.pm_present = 1; 804 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 805 xpt_action((union ccb *)&cts); 806 path->device->protocol = PROTO_SATAPM; 807 PROBE_SET_ACTION(softc, PROBE_PM_PID); 808 } else if (sign == 0xc33c && 809 done_ccb->ccb_h.target_id != 15) { 810 path->device->protocol = PROTO_SEMB; 811 PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES); 812 } else if (sign == 0xeb14 && 813 done_ccb->ccb_h.target_id != 15) { 814 path->device->protocol = PROTO_SCSI; 815 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 816 } else { 817 if (done_ccb->ccb_h.target_id != 15) { 818 xpt_print(path, 819 "Unexpected signature 0x%04x\n", sign); 820 } 821 goto device_fail; 822 } 823 xpt_release_ccb(done_ccb); 824 xpt_schedule(periph, priority); 825 return; 826 } 827 case PROBE_IDENTIFY: 828 { 829 struct ccb_pathinq cpi; 830 int16_t *ptr; 831 832 ident_buf = &softc->ident_data; 833 for (ptr = (int16_t *)ident_buf; 834 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) { 835 *ptr = le16toh(*ptr); 836 } 837 if (strncmp(ident_buf->model, "FX", 2) && 838 strncmp(ident_buf->model, "NEC", 3) && 839 strncmp(ident_buf->model, "Pioneer", 7) && 840 strncmp(ident_buf->model, "SHARP", 5)) { 841 ata_bswap(ident_buf->model, sizeof(ident_buf->model)); 842 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision)); 843 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial)); 844 } 845 ata_btrim(ident_buf->model, sizeof(ident_buf->model)); 846 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model)); 847 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision)); 848 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision)); 849 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial)); 850 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial)); 851 /* Device may need spin-up before IDENTIFY become valid. */ 852 if ((ident_buf->specconf == 0x37c8 || 853 ident_buf->specconf == 0x738c) && 854 ((ident_buf->config & ATA_RESP_INCOMPLETE) || 855 softc->spinup == 0)) { 856 PROBE_SET_ACTION(softc, PROBE_SPINUP); 857 xpt_release_ccb(done_ccb); 858 xpt_schedule(periph, priority); 859 return; 860 } 861 ident_buf = &path->device->ident_data; 862 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 863 /* Check that it is the same device. */ 864 if (bcmp(softc->ident_data.model, ident_buf->model, 865 sizeof(ident_buf->model)) || 866 bcmp(softc->ident_data.revision, ident_buf->revision, 867 sizeof(ident_buf->revision)) || 868 bcmp(softc->ident_data.serial, ident_buf->serial, 869 sizeof(ident_buf->serial))) { 870 /* Device changed. */ 871 xpt_async(AC_LOST_DEVICE, path, NULL); 872 } else { 873 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 874 changed = 0; 875 } 876 } 877 if (changed) { 878 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 879 /* Clean up from previous instance of this device */ 880 if (path->device->serial_num != NULL) { 881 free(path->device->serial_num, M_CAMXPT); 882 path->device->serial_num = NULL; 883 path->device->serial_num_len = 0; 884 } 885 if (path->device->device_id != NULL) { 886 free(path->device->device_id, M_CAMXPT); 887 path->device->device_id = NULL; 888 path->device->device_id_len = 0; 889 } 890 path->device->serial_num = 891 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1), 892 M_CAMXPT, M_NOWAIT); 893 if (path->device->serial_num != NULL) { 894 bcopy(ident_buf->serial, 895 path->device->serial_num, 896 sizeof(ident_buf->serial)); 897 path->device->serial_num[sizeof(ident_buf->serial)] 898 = '\0'; 899 path->device->serial_num_len = 900 strlen(path->device->serial_num); 901 } 902 if (ident_buf->enabled.extension & 903 ATA_SUPPORT_64BITWWN) { 904 path->device->device_id = 905 malloc(16, M_CAMXPT, M_NOWAIT); 906 if (path->device->device_id != NULL) { 907 path->device->device_id_len = 16; 908 bcopy(&fake_device_id_hdr, 909 path->device->device_id, 8); 910 bcopy(ident_buf->wwn, 911 path->device->device_id + 8, 8); 912 } 913 } 914 915 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 916 xpt_async(AC_GETDEV_CHANGED, path, NULL); 917 } 918 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) { 919 path->device->mintags = 2; 920 path->device->maxtags = 921 ATA_QUEUE_LEN(ident_buf->queue) + 1; 922 } 923 ata_find_quirk(path->device); 924 if (path->device->mintags != 0 && 925 path->bus->sim->max_tagged_dev_openings != 0) { 926 /* Check if the SIM does not want queued commands. */ 927 bzero(&cpi, sizeof(cpi)); 928 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 929 cpi.ccb_h.func_code = XPT_PATH_INQ; 930 xpt_action((union ccb *)&cpi); 931 if (cpi.ccb_h.status == CAM_REQ_CMP && 932 (cpi.hba_inquiry & PI_TAG_ABLE)) { 933 /* Report SIM which tags are allowed. */ 934 bzero(&cts, sizeof(cts)); 935 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 936 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 937 cts.type = CTS_TYPE_CURRENT_SETTINGS; 938 cts.xport_specific.sata.tags = path->device->maxtags; 939 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS; 940 xpt_action((union ccb *)&cts); 941 } 942 } 943 if (changed) 944 proberequestdefaultnegotiation(periph); 945 ata_device_transport(path); 946 PROBE_SET_ACTION(softc, PROBE_SETMODE); 947 xpt_release_ccb(done_ccb); 948 xpt_schedule(periph, priority); 949 return; 950 } 951 case PROBE_SPINUP: 952 if (bootverbose) 953 xpt_print(path, "Spin-up done\n"); 954 softc->spinup = 1; 955 PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 956 xpt_release_ccb(done_ccb); 957 xpt_schedule(periph, priority); 958 return; 959 case PROBE_SETMODE: 960 if (path->device->transport != XPORT_SATA) 961 goto notsata; 962 /* Set supported bits. */ 963 bzero(&cts, sizeof(cts)); 964 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 965 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 966 cts.type = CTS_TYPE_CURRENT_SETTINGS; 967 xpt_action((union ccb *)&cts); 968 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 969 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H; 970 else 971 caps = 0; 972 if (ident_buf->satacapabilities != 0xffff) { 973 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV) 974 caps |= CTS_SATA_CAPS_D_PMREQ; 975 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST) 976 caps |= CTS_SATA_CAPS_D_APST; 977 } 978 /* Mask unwanted bits. */ 979 bzero(&cts, sizeof(cts)); 980 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 981 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 982 cts.type = CTS_TYPE_USER_SETTINGS; 983 xpt_action((union ccb *)&cts); 984 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 985 caps &= cts.xport_specific.sata.caps; 986 else 987 caps = 0; 988 /* Store result to SIM. */ 989 bzero(&cts, sizeof(cts)); 990 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 991 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 992 cts.type = CTS_TYPE_CURRENT_SETTINGS; 993 cts.xport_specific.sata.caps = caps; 994 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS; 995 xpt_action((union ccb *)&cts); 996 softc->caps = caps; 997 if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) && 998 (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) != 999 (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) { 1000 PROBE_SET_ACTION(softc, PROBE_SETPM); 1001 xpt_release_ccb(done_ccb); 1002 xpt_schedule(periph, priority); 1003 return; 1004 } 1005 /* FALLTHROUGH */ 1006 case PROBE_SETPM: 1007 if (ident_buf->satacapabilities != 0xffff && 1008 (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) && 1009 (!(softc->caps & CTS_SATA_CAPS_H_APST)) != 1010 (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) { 1011 PROBE_SET_ACTION(softc, PROBE_SETAPST); 1012 xpt_release_ccb(done_ccb); 1013 xpt_schedule(periph, priority); 1014 return; 1015 } 1016 /* FALLTHROUGH */ 1017 case PROBE_SETAPST: 1018 if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) && 1019 (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) != 1020 (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) { 1021 PROBE_SET_ACTION(softc, PROBE_SETDMAAA); 1022 xpt_release_ccb(done_ccb); 1023 xpt_schedule(periph, priority); 1024 return; 1025 } 1026 /* FALLTHROUGH */ 1027 case PROBE_SETDMAAA: 1028 if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) && 1029 (!(softc->caps & CTS_SATA_CAPS_H_AN)) != 1030 (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) { 1031 PROBE_SET_ACTION(softc, PROBE_SETAN); 1032 xpt_release_ccb(done_ccb); 1033 xpt_schedule(periph, priority); 1034 return; 1035 } 1036 /* FALLTHROUGH */ 1037 case PROBE_SETAN: 1038 notsata: 1039 if (path->device->protocol == PROTO_ATA) { 1040 PROBE_SET_ACTION(softc, PROBE_SET_MULTI); 1041 } else { 1042 PROBE_SET_ACTION(softc, PROBE_INQUIRY); 1043 } 1044 xpt_release_ccb(done_ccb); 1045 xpt_schedule(periph, priority); 1046 return; 1047 case PROBE_SET_MULTI: 1048 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 1049 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 1050 xpt_acquire_device(path->device); 1051 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1052 xpt_action(done_ccb); 1053 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 1054 done_ccb); 1055 } 1056 break; 1057 case PROBE_INQUIRY: 1058 case PROBE_FULL_INQUIRY: 1059 { 1060 u_int8_t periph_qual, len; 1061 1062 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 1063 1064 periph_qual = SID_QUAL(inq_buf); 1065 1066 if (periph_qual != SID_QUAL_LU_CONNECTED) 1067 break; 1068 1069 /* 1070 * We conservatively request only 1071 * SHORT_INQUIRY_LEN bytes of inquiry 1072 * information during our first try 1073 * at sending an INQUIRY. If the device 1074 * has more information to give, 1075 * perform a second request specifying 1076 * the amount of information the device 1077 * is willing to give. 1078 */ 1079 len = inq_buf->additional_length 1080 + offsetof(struct scsi_inquiry_data, additional_length) + 1; 1081 if (softc->action == PROBE_INQUIRY 1082 && len > SHORT_INQUIRY_LENGTH) { 1083 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY); 1084 xpt_release_ccb(done_ccb); 1085 xpt_schedule(periph, priority); 1086 return; 1087 } 1088 1089 ata_device_transport(path); 1090 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 1091 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 1092 xpt_acquire_device(path->device); 1093 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1094 xpt_action(done_ccb); 1095 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb); 1096 } 1097 break; 1098 } 1099 case PROBE_PM_PID: 1100 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0) 1101 bzero(ident_buf, sizeof(*ident_buf)); 1102 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) + 1103 (done_ccb->ataio.res.lba_mid << 16) + 1104 (done_ccb->ataio.res.lba_low << 8) + 1105 done_ccb->ataio.res.sector_count; 1106 ((uint32_t *)ident_buf)[0] = softc->pm_pid; 1107 snprintf(ident_buf->model, sizeof(ident_buf->model), 1108 "Port Multiplier %08x", softc->pm_pid); 1109 PROBE_SET_ACTION(softc, PROBE_PM_PRV); 1110 xpt_release_ccb(done_ccb); 1111 xpt_schedule(periph, priority); 1112 return; 1113 case PROBE_PM_PRV: 1114 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) + 1115 (done_ccb->ataio.res.lba_mid << 16) + 1116 (done_ccb->ataio.res.lba_low << 8) + 1117 done_ccb->ataio.res.sector_count; 1118 ((uint32_t *)ident_buf)[1] = softc->pm_prv; 1119 snprintf(ident_buf->revision, sizeof(ident_buf->revision), 1120 "%04x", softc->pm_prv); 1121 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 1122 /* Set supported bits. */ 1123 bzero(&cts, sizeof(cts)); 1124 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 1125 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1126 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1127 xpt_action((union ccb *)&cts); 1128 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1129 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H; 1130 else 1131 caps = 0; 1132 /* All PMPs must support PM requests. */ 1133 caps |= CTS_SATA_CAPS_D_PMREQ; 1134 /* Mask unwanted bits. */ 1135 bzero(&cts, sizeof(cts)); 1136 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 1137 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 1138 cts.type = CTS_TYPE_USER_SETTINGS; 1139 xpt_action((union ccb *)&cts); 1140 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS) 1141 caps &= cts.xport_specific.sata.caps; 1142 else 1143 caps = 0; 1144 /* Store result to SIM. */ 1145 bzero(&cts, sizeof(cts)); 1146 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 1147 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1148 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1149 cts.xport_specific.sata.caps = caps; 1150 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS; 1151 xpt_action((union ccb *)&cts); 1152 softc->caps = caps; 1153 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 1154 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 1155 xpt_acquire_device(path->device); 1156 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1157 xpt_action(done_ccb); 1158 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 1159 done_ccb); 1160 } else { 1161 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1162 xpt_action(done_ccb); 1163 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb); 1164 } 1165 break; 1166 case PROBE_IDENTIFY_SES: 1167 case PROBE_IDENTIFY_SAFTE: 1168 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1169 /* Check that it is the same device. */ 1170 if (bcmp(&softc->ident_data, ident_buf, 53)) { 1171 /* Device changed. */ 1172 xpt_async(AC_LOST_DEVICE, path, NULL); 1173 } else { 1174 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 1175 changed = 0; 1176 } 1177 } 1178 if (changed) { 1179 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 1180 /* Clean up from previous instance of this device */ 1181 if (path->device->device_id != NULL) { 1182 free(path->device->device_id, M_CAMXPT); 1183 path->device->device_id = NULL; 1184 path->device->device_id_len = 0; 1185 } 1186 path->device->device_id = 1187 malloc(16, M_CAMXPT, M_NOWAIT); 1188 if (path->device->device_id != NULL) { 1189 path->device->device_id_len = 16; 1190 bcopy(&fake_device_id_hdr, 1191 path->device->device_id, 8); 1192 bcopy(((uint8_t*)ident_buf) + 2, 1193 path->device->device_id + 8, 8); 1194 } 1195 1196 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 1197 } 1198 1199 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 1200 path->device->flags &= ~CAM_DEV_UNCONFIGURED; 1201 xpt_acquire_device(path->device); 1202 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1203 xpt_action(done_ccb); 1204 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 1205 done_ccb); 1206 } 1207 break; 1208 case PROBE_INVALID: 1209 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO, 1210 ("probedone: invalid action state\n")); 1211 default: 1212 break; 1213 } 1214 done: 1215 if (softc->restart) { 1216 softc->restart = 0; 1217 xpt_release_ccb(done_ccb); 1218 probeschedule(periph); 1219 return; 1220 } 1221 xpt_release_ccb(done_ccb); 1222 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) { 1223 TAILQ_REMOVE(&softc->request_ccbs, 1224 &done_ccb->ccb_h, periph_links.tqe); 1225 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR; 1226 xpt_done(done_ccb); 1227 } 1228 cam_periph_invalidate(periph); 1229 cam_release_devq(periph->path, 1230 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE); 1231 cam_periph_release_locked(periph); 1232 } 1233 1234 static void 1235 probecleanup(struct cam_periph *periph) 1236 { 1237 free(periph->softc, M_CAMXPT); 1238 } 1239 1240 static void 1241 ata_find_quirk(struct cam_ed *device) 1242 { 1243 struct ata_quirk_entry *quirk; 1244 caddr_t match; 1245 1246 match = cam_quirkmatch((caddr_t)&device->ident_data, 1247 (caddr_t)ata_quirk_table, 1248 ata_quirk_table_size, 1249 sizeof(*ata_quirk_table), ata_identify_match); 1250 1251 if (match == NULL) 1252 panic("xpt_find_quirk: device didn't match wildcard entry!!"); 1253 1254 quirk = (struct ata_quirk_entry *)match; 1255 device->quirk = quirk; 1256 if (quirk->quirks & CAM_QUIRK_MAXTAGS) { 1257 device->mintags = quirk->mintags; 1258 device->maxtags = quirk->maxtags; 1259 } 1260 } 1261 1262 typedef struct { 1263 union ccb *request_ccb; 1264 struct ccb_pathinq *cpi; 1265 int counter; 1266 } ata_scan_bus_info; 1267 1268 /* 1269 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 1270 * As the scan progresses, xpt_scan_bus is used as the 1271 * callback on completion function. 1272 */ 1273 static void 1274 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 1275 { 1276 struct cam_path *path; 1277 ata_scan_bus_info *scan_info; 1278 union ccb *work_ccb, *reset_ccb; 1279 cam_status status; 1280 1281 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 1282 ("xpt_scan_bus\n")); 1283 switch (request_ccb->ccb_h.func_code) { 1284 case XPT_SCAN_BUS: 1285 case XPT_SCAN_TGT: 1286 /* Find out the characteristics of the bus */ 1287 work_ccb = xpt_alloc_ccb_nowait(); 1288 if (work_ccb == NULL) { 1289 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1290 xpt_done(request_ccb); 1291 return; 1292 } 1293 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 1294 request_ccb->ccb_h.pinfo.priority); 1295 work_ccb->ccb_h.func_code = XPT_PATH_INQ; 1296 xpt_action(work_ccb); 1297 if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 1298 request_ccb->ccb_h.status = work_ccb->ccb_h.status; 1299 xpt_free_ccb(work_ccb); 1300 xpt_done(request_ccb); 1301 return; 1302 } 1303 1304 /* We may need to reset bus first, if we haven't done it yet. */ 1305 if ((work_ccb->cpi.hba_inquiry & 1306 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) && 1307 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) && 1308 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) { 1309 reset_ccb = xpt_alloc_ccb_nowait(); 1310 if (reset_ccb == NULL) { 1311 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1312 xpt_free_ccb(work_ccb); 1313 xpt_done(request_ccb); 1314 return; 1315 } 1316 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path, 1317 CAM_PRIORITY_NONE); 1318 reset_ccb->ccb_h.func_code = XPT_RESET_BUS; 1319 xpt_action(reset_ccb); 1320 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) { 1321 request_ccb->ccb_h.status = reset_ccb->ccb_h.status; 1322 xpt_free_ccb(reset_ccb); 1323 xpt_free_ccb(work_ccb); 1324 xpt_done(request_ccb); 1325 return; 1326 } 1327 xpt_free_ccb(reset_ccb); 1328 } 1329 1330 /* Save some state for use while we probe for devices */ 1331 scan_info = (ata_scan_bus_info *) 1332 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT); 1333 if (scan_info == NULL) { 1334 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1335 xpt_free_ccb(work_ccb); 1336 xpt_done(request_ccb); 1337 return; 1338 } 1339 scan_info->request_ccb = request_ccb; 1340 scan_info->cpi = &work_ccb->cpi; 1341 /* If PM supported, probe it first. */ 1342 if (scan_info->cpi->hba_inquiry & PI_SATAPM) 1343 scan_info->counter = scan_info->cpi->max_target; 1344 else 1345 scan_info->counter = 0; 1346 1347 work_ccb = xpt_alloc_ccb_nowait(); 1348 if (work_ccb == NULL) { 1349 free(scan_info, M_CAMXPT); 1350 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1351 xpt_done(request_ccb); 1352 break; 1353 } 1354 goto scan_next; 1355 case XPT_SCAN_LUN: 1356 work_ccb = request_ccb; 1357 /* Reuse the same CCB to query if a device was really found */ 1358 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0; 1359 /* If there is PMP... */ 1360 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) && 1361 (scan_info->counter == scan_info->cpi->max_target)) { 1362 if (work_ccb->ccb_h.status == CAM_REQ_CMP) { 1363 /* everything else will be probed by it */ 1364 /* Free the current request path- we're done with it. */ 1365 xpt_free_path(work_ccb->ccb_h.path); 1366 goto done; 1367 } else { 1368 struct ccb_trans_settings cts; 1369 1370 /* Report SIM that PM is absent. */ 1371 bzero(&cts, sizeof(cts)); 1372 xpt_setup_ccb(&cts.ccb_h, 1373 work_ccb->ccb_h.path, CAM_PRIORITY_NONE); 1374 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1375 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1376 cts.xport_specific.sata.pm_present = 0; 1377 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 1378 xpt_action((union ccb *)&cts); 1379 } 1380 } 1381 /* Free the current request path- we're done with it. */ 1382 xpt_free_path(work_ccb->ccb_h.path); 1383 if (scan_info->counter == 1384 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1385 0 : scan_info->cpi->max_target)) { 1386 done: 1387 xpt_free_ccb(work_ccb); 1388 xpt_free_ccb((union ccb *)scan_info->cpi); 1389 request_ccb = scan_info->request_ccb; 1390 free(scan_info, M_CAMXPT); 1391 request_ccb->ccb_h.status = CAM_REQ_CMP; 1392 xpt_done(request_ccb); 1393 break; 1394 } 1395 /* Take next device. Wrap from max (PMP) to 0. */ 1396 scan_info->counter = (scan_info->counter + 1 ) % 1397 (scan_info->cpi->max_target + 1); 1398 scan_next: 1399 status = xpt_create_path(&path, xpt_periph, 1400 scan_info->request_ccb->ccb_h.path_id, 1401 scan_info->counter, 0); 1402 if (status != CAM_REQ_CMP) { 1403 printf("xpt_scan_bus: xpt_create_path failed" 1404 " with status %#x, bus scan halted\n", 1405 status); 1406 xpt_free_ccb(work_ccb); 1407 xpt_free_ccb((union ccb *)scan_info->cpi); 1408 request_ccb = scan_info->request_ccb; 1409 free(scan_info, M_CAMXPT); 1410 request_ccb->ccb_h.status = status; 1411 xpt_done(request_ccb); 1412 break; 1413 } 1414 xpt_setup_ccb(&work_ccb->ccb_h, path, 1415 scan_info->request_ccb->ccb_h.pinfo.priority); 1416 work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1417 work_ccb->ccb_h.cbfcnp = ata_scan_bus; 1418 work_ccb->ccb_h.ppriv_ptr0 = scan_info; 1419 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags; 1420 xpt_action(work_ccb); 1421 break; 1422 default: 1423 break; 1424 } 1425 } 1426 1427 static void 1428 ata_scan_lun(struct cam_periph *periph, struct cam_path *path, 1429 cam_flags flags, union ccb *request_ccb) 1430 { 1431 struct ccb_pathinq cpi; 1432 cam_status status; 1433 struct cam_path *new_path; 1434 struct cam_periph *old_periph; 1435 1436 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n")); 1437 1438 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 1439 cpi.ccb_h.func_code = XPT_PATH_INQ; 1440 xpt_action((union ccb *)&cpi); 1441 1442 if (cpi.ccb_h.status != CAM_REQ_CMP) { 1443 if (request_ccb != NULL) { 1444 request_ccb->ccb_h.status = cpi.ccb_h.status; 1445 xpt_done(request_ccb); 1446 } 1447 return; 1448 } 1449 1450 if (request_ccb == NULL) { 1451 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT); 1452 if (request_ccb == NULL) { 1453 xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 1454 "can't continue\n"); 1455 return; 1456 } 1457 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT); 1458 if (new_path == NULL) { 1459 xpt_print(path, "xpt_scan_lun: can't allocate path, " 1460 "can't continue\n"); 1461 free(request_ccb, M_CAMXPT); 1462 return; 1463 } 1464 status = xpt_compile_path(new_path, xpt_periph, 1465 path->bus->path_id, 1466 path->target->target_id, 1467 path->device->lun_id); 1468 1469 if (status != CAM_REQ_CMP) { 1470 xpt_print(path, "xpt_scan_lun: can't compile path, " 1471 "can't continue\n"); 1472 free(request_ccb, M_CAMXPT); 1473 free(new_path, M_CAMXPT); 1474 return; 1475 } 1476 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT); 1477 request_ccb->ccb_h.cbfcnp = xptscandone; 1478 request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 1479 request_ccb->crcn.flags = flags; 1480 } 1481 1482 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) { 1483 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) { 1484 probe_softc *softc; 1485 1486 softc = (probe_softc *)old_periph->softc; 1487 TAILQ_INSERT_TAIL(&softc->request_ccbs, 1488 &request_ccb->ccb_h, periph_links.tqe); 1489 softc->restart = 1; 1490 } else { 1491 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1492 xpt_done(request_ccb); 1493 } 1494 } else { 1495 status = cam_periph_alloc(proberegister, NULL, probecleanup, 1496 probestart, "aprobe", 1497 CAM_PERIPH_BIO, 1498 request_ccb->ccb_h.path, NULL, 0, 1499 request_ccb); 1500 1501 if (status != CAM_REQ_CMP) { 1502 xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 1503 "returned an error, can't continue probe\n"); 1504 request_ccb->ccb_h.status = status; 1505 xpt_done(request_ccb); 1506 } 1507 } 1508 } 1509 1510 static void 1511 xptscandone(struct cam_periph *periph, union ccb *done_ccb) 1512 { 1513 xpt_release_path(done_ccb->ccb_h.path); 1514 free(done_ccb->ccb_h.path, M_CAMXPT); 1515 free(done_ccb, M_CAMXPT); 1516 } 1517 1518 static struct cam_ed * 1519 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 1520 { 1521 struct cam_path path; 1522 struct ata_quirk_entry *quirk; 1523 struct cam_ed *device; 1524 struct cam_ed *cur_device; 1525 1526 device = xpt_alloc_device(bus, target, lun_id); 1527 if (device == NULL) 1528 return (NULL); 1529 1530 /* 1531 * Take the default quirk entry until we have inquiry 1532 * data and can determine a better quirk to use. 1533 */ 1534 quirk = &ata_quirk_table[ata_quirk_table_size - 1]; 1535 device->quirk = (void *)quirk; 1536 device->mintags = 0; 1537 device->maxtags = 0; 1538 bzero(&device->inq_data, sizeof(device->inq_data)); 1539 device->inq_flags = 0; 1540 device->queue_flags = 0; 1541 device->serial_num = NULL; 1542 device->serial_num_len = 0; 1543 1544 /* 1545 * XXX should be limited by number of CCBs this bus can 1546 * do. 1547 */ 1548 bus->sim->max_ccbs += device->ccbq.devq_openings; 1549 /* Insertion sort into our target's device list */ 1550 cur_device = TAILQ_FIRST(&target->ed_entries); 1551 while (cur_device != NULL && cur_device->lun_id < lun_id) 1552 cur_device = TAILQ_NEXT(cur_device, links); 1553 if (cur_device != NULL) { 1554 TAILQ_INSERT_BEFORE(cur_device, device, links); 1555 } else { 1556 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 1557 } 1558 target->generation++; 1559 if (lun_id != CAM_LUN_WILDCARD) { 1560 xpt_compile_path(&path, 1561 NULL, 1562 bus->path_id, 1563 target->target_id, 1564 lun_id); 1565 ata_device_transport(&path); 1566 xpt_release_path(&path); 1567 } 1568 1569 return (device); 1570 } 1571 1572 static void 1573 ata_device_transport(struct cam_path *path) 1574 { 1575 struct ccb_pathinq cpi; 1576 struct ccb_trans_settings cts; 1577 struct scsi_inquiry_data *inq_buf = NULL; 1578 struct ata_params *ident_buf = NULL; 1579 1580 /* Get transport information from the SIM */ 1581 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 1582 cpi.ccb_h.func_code = XPT_PATH_INQ; 1583 xpt_action((union ccb *)&cpi); 1584 1585 path->device->transport = cpi.transport; 1586 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 1587 inq_buf = &path->device->inq_data; 1588 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0) 1589 ident_buf = &path->device->ident_data; 1590 if (path->device->protocol == PROTO_ATA) { 1591 path->device->protocol_version = ident_buf ? 1592 ata_version(ident_buf->version_major) : cpi.protocol_version; 1593 } else if (path->device->protocol == PROTO_SCSI) { 1594 path->device->protocol_version = inq_buf ? 1595 SID_ANSI_REV(inq_buf) : cpi.protocol_version; 1596 } 1597 path->device->transport_version = ident_buf ? 1598 ata_version(ident_buf->version_major) : cpi.transport_version; 1599 1600 /* Tell the controller what we think */ 1601 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 1602 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 1603 cts.type = CTS_TYPE_CURRENT_SETTINGS; 1604 cts.transport = path->device->transport; 1605 cts.transport_version = path->device->transport_version; 1606 cts.protocol = path->device->protocol; 1607 cts.protocol_version = path->device->protocol_version; 1608 cts.proto_specific.valid = 0; 1609 if (ident_buf) { 1610 if (path->device->transport == XPORT_ATA) { 1611 cts.xport_specific.ata.atapi = 1612 (ident_buf->config == ATA_PROTO_CFA) ? 0 : 1613 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 1614 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 1615 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI; 1616 } else { 1617 cts.xport_specific.sata.atapi = 1618 (ident_buf->config == ATA_PROTO_CFA) ? 0 : 1619 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 1620 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 1621 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI; 1622 } 1623 } else 1624 cts.xport_specific.valid = 0; 1625 xpt_action((union ccb *)&cts); 1626 } 1627 1628 static void 1629 ata_dev_advinfo(union ccb *start_ccb) 1630 { 1631 struct cam_ed *device; 1632 struct ccb_dev_advinfo *cdai; 1633 off_t amt; 1634 1635 start_ccb->ccb_h.status = CAM_REQ_INVALID; 1636 device = start_ccb->ccb_h.path->device; 1637 cdai = &start_ccb->cdai; 1638 switch(cdai->buftype) { 1639 case CDAI_TYPE_SCSI_DEVID: 1640 if (cdai->flags & CDAI_FLAG_STORE) 1641 return; 1642 cdai->provsiz = device->device_id_len; 1643 if (device->device_id_len == 0) 1644 break; 1645 amt = device->device_id_len; 1646 if (cdai->provsiz > cdai->bufsiz) 1647 amt = cdai->bufsiz; 1648 memcpy(cdai->buf, device->device_id, amt); 1649 break; 1650 case CDAI_TYPE_SERIAL_NUM: 1651 if (cdai->flags & CDAI_FLAG_STORE) 1652 return; 1653 cdai->provsiz = device->serial_num_len; 1654 if (device->serial_num_len == 0) 1655 break; 1656 amt = device->serial_num_len; 1657 if (cdai->provsiz > cdai->bufsiz) 1658 amt = cdai->bufsiz; 1659 memcpy(cdai->buf, device->serial_num, amt); 1660 break; 1661 case CDAI_TYPE_PHYS_PATH: 1662 if (cdai->flags & CDAI_FLAG_STORE) { 1663 if (device->physpath != NULL) 1664 free(device->physpath, M_CAMXPT); 1665 device->physpath_len = cdai->bufsiz; 1666 /* Clear existing buffer if zero length */ 1667 if (cdai->bufsiz == 0) 1668 break; 1669 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT); 1670 if (device->physpath == NULL) { 1671 start_ccb->ccb_h.status = CAM_REQ_ABORTED; 1672 return; 1673 } 1674 memcpy(device->physpath, cdai->buf, cdai->bufsiz); 1675 } else { 1676 cdai->provsiz = device->physpath_len; 1677 if (device->physpath_len == 0) 1678 break; 1679 amt = device->physpath_len; 1680 if (cdai->provsiz > cdai->bufsiz) 1681 amt = cdai->bufsiz; 1682 memcpy(cdai->buf, device->physpath, amt); 1683 } 1684 break; 1685 default: 1686 return; 1687 } 1688 start_ccb->ccb_h.status = CAM_REQ_CMP; 1689 1690 if (cdai->flags & CDAI_FLAG_STORE) { 1691 int owned; 1692 1693 owned = mtx_owned(start_ccb->ccb_h.path->bus->sim->mtx); 1694 if (owned == 0) 1695 mtx_lock(start_ccb->ccb_h.path->bus->sim->mtx); 1696 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path, 1697 (void *)(uintptr_t)cdai->buftype); 1698 if (owned == 0) 1699 mtx_unlock(start_ccb->ccb_h.path->bus->sim->mtx); 1700 } 1701 } 1702 1703 static void 1704 ata_action(union ccb *start_ccb) 1705 { 1706 1707 switch (start_ccb->ccb_h.func_code) { 1708 case XPT_SET_TRAN_SETTINGS: 1709 { 1710 ata_set_transfer_settings(&start_ccb->cts, 1711 start_ccb->ccb_h.path->device, 1712 /*async_update*/FALSE); 1713 break; 1714 } 1715 case XPT_SCAN_BUS: 1716 case XPT_SCAN_TGT: 1717 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 1718 break; 1719 case XPT_SCAN_LUN: 1720 ata_scan_lun(start_ccb->ccb_h.path->periph, 1721 start_ccb->ccb_h.path, start_ccb->crcn.flags, 1722 start_ccb); 1723 break; 1724 case XPT_GET_TRAN_SETTINGS: 1725 { 1726 ata_get_transfer_settings(&start_ccb->cts); 1727 break; 1728 } 1729 case XPT_SCSI_IO: 1730 { 1731 struct cam_ed *device; 1732 u_int maxlen = 0; 1733 1734 device = start_ccb->ccb_h.path->device; 1735 if (device->protocol == PROTO_SCSI && 1736 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) { 1737 uint16_t p = 1738 device->ident_data.config & ATA_PROTO_MASK; 1739 1740 maxlen = 1741 (device->ident_data.config == ATA_PROTO_CFA) ? 0 : 1742 (p == ATA_PROTO_ATAPI_16) ? 16 : 1743 (p == ATA_PROTO_ATAPI_12) ? 12 : 0; 1744 } 1745 if (start_ccb->csio.cdb_len > maxlen) { 1746 start_ccb->ccb_h.status = CAM_REQ_INVALID; 1747 xpt_done(start_ccb); 1748 break; 1749 } 1750 xpt_action_default(start_ccb); 1751 break; 1752 } 1753 case XPT_DEV_ADVINFO: 1754 { 1755 ata_dev_advinfo(start_ccb); 1756 break; 1757 } 1758 default: 1759 xpt_action_default(start_ccb); 1760 break; 1761 } 1762 } 1763 1764 static void 1765 ata_get_transfer_settings(struct ccb_trans_settings *cts) 1766 { 1767 struct ccb_trans_settings_ata *ata; 1768 struct ccb_trans_settings_scsi *scsi; 1769 struct cam_ed *device; 1770 struct cam_sim *sim; 1771 1772 device = cts->ccb_h.path->device; 1773 sim = cts->ccb_h.path->bus->sim; 1774 (*(sim->sim_action))(sim, (union ccb *)cts); 1775 1776 if (cts->protocol == PROTO_ATA) { 1777 ata = &cts->proto_specific.ata; 1778 if ((ata->valid & CTS_ATA_VALID_TQ) == 0) { 1779 ata->valid |= CTS_ATA_VALID_TQ; 1780 if (cts->type == CTS_TYPE_USER_SETTINGS || 1781 (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 || 1782 (device->inq_flags & SID_CmdQue) != 0) 1783 ata->flags |= CTS_ATA_FLAGS_TAG_ENB; 1784 } 1785 } 1786 if (cts->protocol == PROTO_SCSI) { 1787 scsi = &cts->proto_specific.scsi; 1788 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 1789 scsi->valid |= CTS_SCSI_VALID_TQ; 1790 if (cts->type == CTS_TYPE_USER_SETTINGS || 1791 (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 || 1792 (device->inq_flags & SID_CmdQue) != 0) 1793 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 1794 } 1795 } 1796 } 1797 1798 static void 1799 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 1800 int async_update) 1801 { 1802 struct ccb_pathinq cpi; 1803 struct ccb_trans_settings_ata *ata; 1804 struct ccb_trans_settings_scsi *scsi; 1805 struct cam_sim *sim; 1806 struct ata_params *ident_data; 1807 struct scsi_inquiry_data *inq_data; 1808 1809 if (device == NULL) { 1810 cts->ccb_h.status = CAM_PATH_INVALID; 1811 xpt_done((union ccb *)cts); 1812 return; 1813 } 1814 1815 if (cts->protocol == PROTO_UNKNOWN 1816 || cts->protocol == PROTO_UNSPECIFIED) { 1817 cts->protocol = device->protocol; 1818 cts->protocol_version = device->protocol_version; 1819 } 1820 1821 if (cts->protocol_version == PROTO_VERSION_UNKNOWN 1822 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 1823 cts->protocol_version = device->protocol_version; 1824 1825 if (cts->protocol != device->protocol) { 1826 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 1827 cts->protocol, device->protocol); 1828 cts->protocol = device->protocol; 1829 } 1830 1831 if (cts->protocol_version > device->protocol_version) { 1832 if (bootverbose) { 1833 xpt_print(cts->ccb_h.path, "Down reving Protocol " 1834 "Version from %d to %d?\n", cts->protocol_version, 1835 device->protocol_version); 1836 } 1837 cts->protocol_version = device->protocol_version; 1838 } 1839 1840 if (cts->transport == XPORT_UNKNOWN 1841 || cts->transport == XPORT_UNSPECIFIED) { 1842 cts->transport = device->transport; 1843 cts->transport_version = device->transport_version; 1844 } 1845 1846 if (cts->transport_version == XPORT_VERSION_UNKNOWN 1847 || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 1848 cts->transport_version = device->transport_version; 1849 1850 if (cts->transport != device->transport) { 1851 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 1852 cts->transport, device->transport); 1853 cts->transport = device->transport; 1854 } 1855 1856 if (cts->transport_version > device->transport_version) { 1857 if (bootverbose) { 1858 xpt_print(cts->ccb_h.path, "Down reving Transport " 1859 "Version from %d to %d?\n", cts->transport_version, 1860 device->transport_version); 1861 } 1862 cts->transport_version = device->transport_version; 1863 } 1864 1865 sim = cts->ccb_h.path->bus->sim; 1866 ident_data = &device->ident_data; 1867 inq_data = &device->inq_data; 1868 if (cts->protocol == PROTO_ATA) 1869 ata = &cts->proto_specific.ata; 1870 else 1871 ata = NULL; 1872 if (cts->protocol == PROTO_SCSI) 1873 scsi = &cts->proto_specific.scsi; 1874 else 1875 scsi = NULL; 1876 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE); 1877 cpi.ccb_h.func_code = XPT_PATH_INQ; 1878 xpt_action((union ccb *)&cpi); 1879 1880 /* Sanity checking */ 1881 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 1882 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0) 1883 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0) 1884 || (device->queue_flags & SCP_QUEUE_DQUE) != 0 1885 || (device->mintags == 0)) { 1886 /* 1887 * Can't tag on hardware that doesn't support tags, 1888 * doesn't have it enabled, or has broken tag support. 1889 */ 1890 if (ata) 1891 ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB; 1892 if (scsi) 1893 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1894 } 1895 1896 /* Start/stop tags use. */ 1897 if (cts->type == CTS_TYPE_CURRENT_SETTINGS && 1898 ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) || 1899 (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) { 1900 int nowt, newt = 0; 1901 1902 nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 || 1903 (device->inq_flags & SID_CmdQue) != 0); 1904 if (ata) 1905 newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0; 1906 if (scsi) 1907 newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0; 1908 1909 if (newt && !nowt) { 1910 /* 1911 * Delay change to use tags until after a 1912 * few commands have gone to this device so 1913 * the controller has time to perform transfer 1914 * negotiations without tagged messages getting 1915 * in the way. 1916 */ 1917 device->tag_delay_count = CAM_TAG_DELAY_COUNT; 1918 device->flags |= CAM_DEV_TAG_AFTER_COUNT; 1919 } else if (nowt && !newt) 1920 xpt_stop_tags(cts->ccb_h.path); 1921 } 1922 1923 if (async_update == FALSE) 1924 (*(sim->sim_action))(sim, (union ccb *)cts); 1925 } 1926 1927 /* 1928 * Handle any per-device event notifications that require action by the XPT. 1929 */ 1930 static void 1931 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 1932 struct cam_ed *device, void *async_arg) 1933 { 1934 cam_status status; 1935 struct cam_path newpath; 1936 1937 /* 1938 * We only need to handle events for real devices. 1939 */ 1940 if (target->target_id == CAM_TARGET_WILDCARD 1941 || device->lun_id == CAM_LUN_WILDCARD) 1942 return; 1943 1944 /* 1945 * We need our own path with wildcards expanded to 1946 * handle certain types of events. 1947 */ 1948 if ((async_code == AC_SENT_BDR) 1949 || (async_code == AC_BUS_RESET) 1950 || (async_code == AC_INQ_CHANGED)) 1951 status = xpt_compile_path(&newpath, NULL, 1952 bus->path_id, 1953 target->target_id, 1954 device->lun_id); 1955 else 1956 status = CAM_REQ_CMP_ERR; 1957 1958 if (status == CAM_REQ_CMP) { 1959 if (async_code == AC_INQ_CHANGED) { 1960 /* 1961 * We've sent a start unit command, or 1962 * something similar to a device that 1963 * may have caused its inquiry data to 1964 * change. So we re-scan the device to 1965 * refresh the inquiry data for it. 1966 */ 1967 ata_scan_lun(newpath.periph, &newpath, 1968 CAM_EXPECT_INQ_CHANGE, NULL); 1969 } else { 1970 /* We need to reinitialize device after reset. */ 1971 ata_scan_lun(newpath.periph, &newpath, 1972 0, NULL); 1973 } 1974 xpt_release_path(&newpath); 1975 } else if (async_code == AC_LOST_DEVICE && 1976 (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 1977 device->flags |= CAM_DEV_UNCONFIGURED; 1978 xpt_release_device(device); 1979 } else if (async_code == AC_TRANSFER_NEG) { 1980 struct ccb_trans_settings *settings; 1981 1982 settings = (struct ccb_trans_settings *)async_arg; 1983 ata_set_transfer_settings(settings, device, 1984 /*async_update*/TRUE); 1985 } 1986 } 1987 1988 static void 1989 ata_announce_periph(struct cam_periph *periph) 1990 { 1991 struct ccb_pathinq cpi; 1992 struct ccb_trans_settings cts; 1993 struct cam_path *path = periph->path; 1994 u_int speed; 1995 u_int mb; 1996 1997 mtx_assert(periph->sim->mtx, MA_OWNED); 1998 1999 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 2000 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 2001 cts.type = CTS_TYPE_CURRENT_SETTINGS; 2002 xpt_action((union ccb*)&cts); 2003 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 2004 return; 2005 /* Ask the SIM for its base transfer speed */ 2006 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 2007 cpi.ccb_h.func_code = XPT_PATH_INQ; 2008 xpt_action((union ccb *)&cpi); 2009 /* Report connection speed */ 2010 speed = cpi.base_transfer_speed; 2011 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 2012 struct ccb_trans_settings_pata *pata = 2013 &cts.xport_specific.ata; 2014 2015 if (pata->valid & CTS_ATA_VALID_MODE) 2016 speed = ata_mode2speed(pata->mode); 2017 } 2018 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 2019 struct ccb_trans_settings_sata *sata = 2020 &cts.xport_specific.sata; 2021 2022 if (sata->valid & CTS_SATA_VALID_REVISION) 2023 speed = ata_revision2speed(sata->revision); 2024 } 2025 mb = speed / 1000; 2026 if (mb > 0) 2027 printf("%s%d: %d.%03dMB/s transfers", 2028 periph->periph_name, periph->unit_number, 2029 mb, speed % 1000); 2030 else 2031 printf("%s%d: %dKB/s transfers", periph->periph_name, 2032 periph->unit_number, speed); 2033 /* Report additional information about connection */ 2034 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 2035 struct ccb_trans_settings_pata *pata = 2036 &cts.xport_specific.ata; 2037 2038 printf(" ("); 2039 if (pata->valid & CTS_ATA_VALID_MODE) 2040 printf("%s, ", ata_mode2string(pata->mode)); 2041 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0) 2042 printf("ATAPI %dbytes, ", pata->atapi); 2043 if (pata->valid & CTS_ATA_VALID_BYTECOUNT) 2044 printf("PIO %dbytes", pata->bytecount); 2045 printf(")"); 2046 } 2047 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 2048 struct ccb_trans_settings_sata *sata = 2049 &cts.xport_specific.sata; 2050 2051 printf(" ("); 2052 if (sata->valid & CTS_SATA_VALID_REVISION) 2053 printf("SATA %d.x, ", sata->revision); 2054 else 2055 printf("SATA, "); 2056 if (sata->valid & CTS_SATA_VALID_MODE) 2057 printf("%s, ", ata_mode2string(sata->mode)); 2058 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0) 2059 printf("ATAPI %dbytes, ", sata->atapi); 2060 if (sata->valid & CTS_SATA_VALID_BYTECOUNT) 2061 printf("PIO %dbytes", sata->bytecount); 2062 printf(")"); 2063 } 2064 printf("\n"); 2065 } 2066 2067