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