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