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