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