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