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