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