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