152c9ce25SScott Long /*- 252c9ce25SScott Long * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 352c9ce25SScott Long * All rights reserved. 452c9ce25SScott Long * 552c9ce25SScott Long * Redistribution and use in source and binary forms, with or without 652c9ce25SScott Long * modification, are permitted provided that the following conditions 752c9ce25SScott Long * are met: 852c9ce25SScott Long * 1. Redistributions of source code must retain the above copyright 952c9ce25SScott Long * notice, this list of conditions and the following disclaimer, 1052c9ce25SScott Long * without modification, immediately at the beginning of the file. 1152c9ce25SScott Long * 2. Redistributions in binary form must reproduce the above copyright 1252c9ce25SScott Long * notice, this list of conditions and the following disclaimer in the 1352c9ce25SScott Long * documentation and/or other materials provided with the distribution. 1452c9ce25SScott Long * 1552c9ce25SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1652c9ce25SScott Long * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1752c9ce25SScott Long * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1852c9ce25SScott Long * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1952c9ce25SScott Long * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2052c9ce25SScott Long * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2152c9ce25SScott Long * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2252c9ce25SScott Long * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2352c9ce25SScott Long * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2452c9ce25SScott Long * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2552c9ce25SScott Long */ 2652c9ce25SScott Long 2752c9ce25SScott Long #include <sys/cdefs.h> 2852c9ce25SScott Long __FBSDID("$FreeBSD$"); 2952c9ce25SScott Long 3052c9ce25SScott Long #include <sys/param.h> 3152c9ce25SScott Long #include <sys/bus.h> 3252c9ce25SScott Long #include <sys/endian.h> 3352c9ce25SScott Long #include <sys/systm.h> 3452c9ce25SScott Long #include <sys/types.h> 3552c9ce25SScott Long #include <sys/malloc.h> 3652c9ce25SScott Long #include <sys/kernel.h> 3752c9ce25SScott Long #include <sys/time.h> 3852c9ce25SScott Long #include <sys/conf.h> 3952c9ce25SScott Long #include <sys/fcntl.h> 4052c9ce25SScott Long #include <sys/interrupt.h> 4152c9ce25SScott Long #include <sys/sbuf.h> 4252c9ce25SScott Long 4352c9ce25SScott Long #include <sys/lock.h> 4452c9ce25SScott Long #include <sys/mutex.h> 4552c9ce25SScott Long #include <sys/sysctl.h> 4652c9ce25SScott Long 4752c9ce25SScott Long #ifdef PC98 4852c9ce25SScott Long #include <pc98/pc98/pc98_machdep.h> /* geometry translation */ 4952c9ce25SScott Long #endif 5052c9ce25SScott Long 5152c9ce25SScott Long #include <cam/cam.h> 5252c9ce25SScott Long #include <cam/cam_ccb.h> 5352c9ce25SScott Long #include <cam/cam_queue.h> 5452c9ce25SScott Long #include <cam/cam_periph.h> 5552c9ce25SScott Long #include <cam/cam_sim.h> 5652c9ce25SScott Long #include <cam/cam_xpt.h> 5752c9ce25SScott Long #include <cam/cam_xpt_sim.h> 5852c9ce25SScott Long #include <cam/cam_xpt_periph.h> 5952c9ce25SScott Long #include <cam/cam_xpt_internal.h> 6052c9ce25SScott Long #include <cam/cam_debug.h> 6152c9ce25SScott Long 6252c9ce25SScott Long #include <cam/scsi/scsi_all.h> 6352c9ce25SScott Long #include <cam/scsi/scsi_message.h> 6452c9ce25SScott Long #include <cam/ata/ata_all.h> 6552c9ce25SScott Long #include <machine/stdarg.h> /* for xpt_print below */ 6652c9ce25SScott Long #include "opt_cam.h" 6752c9ce25SScott Long 6830a4094fSAlexander Motin struct ata_quirk_entry { 6952c9ce25SScott Long struct scsi_inquiry_pattern inq_pat; 7052c9ce25SScott Long u_int8_t quirks; 7130a4094fSAlexander Motin #define CAM_QUIRK_MAXTAGS 0x01 7252c9ce25SScott Long u_int maxtags; 7352c9ce25SScott Long }; 7452c9ce25SScott Long 7552c9ce25SScott Long static periph_init_t probe_periph_init; 7652c9ce25SScott Long 7752c9ce25SScott Long static struct periph_driver probe_driver = 7852c9ce25SScott Long { 79f09f8e3eSAlexander Motin probe_periph_init, "aprobe", 801e637ba6SAlexander Motin TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0, 811e637ba6SAlexander Motin CAM_PERIPH_DRV_EARLY 8252c9ce25SScott Long }; 8352c9ce25SScott Long 84f09f8e3eSAlexander Motin PERIPHDRIVER_DECLARE(aprobe, probe_driver); 8552c9ce25SScott Long 8652c9ce25SScott Long typedef enum { 8752c9ce25SScott Long PROBE_RESET, 8852c9ce25SScott Long PROBE_IDENTIFY, 894ef08dc5SAlexander Motin PROBE_SPINUP, 9052c9ce25SScott Long PROBE_SETMODE, 911e637ba6SAlexander Motin PROBE_SET_MULTI, 9252c9ce25SScott Long PROBE_INQUIRY, 9352c9ce25SScott Long PROBE_FULL_INQUIRY, 9452c9ce25SScott Long PROBE_PM_PID, 9552c9ce25SScott Long PROBE_PM_PRV, 9652c9ce25SScott Long PROBE_INVALID 9752c9ce25SScott Long } probe_action; 9852c9ce25SScott Long 9952c9ce25SScott Long static char *probe_action_text[] = { 10052c9ce25SScott Long "PROBE_RESET", 10152c9ce25SScott Long "PROBE_IDENTIFY", 1024ef08dc5SAlexander Motin "PROBE_SPINUP", 10352c9ce25SScott Long "PROBE_SETMODE", 1041e637ba6SAlexander Motin "PROBE_SET_MULTI", 10552c9ce25SScott Long "PROBE_INQUIRY", 10652c9ce25SScott Long "PROBE_FULL_INQUIRY", 10752c9ce25SScott Long "PROBE_PM_PID", 10852c9ce25SScott Long "PROBE_PM_PRV", 10952c9ce25SScott Long "PROBE_INVALID" 11052c9ce25SScott Long }; 11152c9ce25SScott Long 11252c9ce25SScott Long #define PROBE_SET_ACTION(softc, newaction) \ 11352c9ce25SScott Long do { \ 11452c9ce25SScott Long char **text; \ 11552c9ce25SScott Long text = probe_action_text; \ 11652c9ce25SScott Long CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \ 11752c9ce25SScott Long ("Probe %s to %s\n", text[(softc)->action], \ 11852c9ce25SScott Long text[(newaction)])); \ 11952c9ce25SScott Long (softc)->action = (newaction); \ 12052c9ce25SScott Long } while(0) 12152c9ce25SScott Long 12252c9ce25SScott Long typedef enum { 12352c9ce25SScott Long PROBE_NO_ANNOUNCE = 0x04 12452c9ce25SScott Long } probe_flags; 12552c9ce25SScott Long 12652c9ce25SScott Long typedef struct { 12752c9ce25SScott Long TAILQ_HEAD(, ccb_hdr) request_ccbs; 128a9b8edb1SAlexander Motin struct ata_params ident_data; 12952c9ce25SScott Long probe_action action; 13052c9ce25SScott Long probe_flags flags; 13152c9ce25SScott Long uint32_t pm_pid; 13252c9ce25SScott Long uint32_t pm_prv; 13383c5d981SAlexander Motin int restart; 1344ef08dc5SAlexander Motin int spinup; 13552c9ce25SScott Long struct cam_periph *periph; 13652c9ce25SScott Long } probe_softc; 13752c9ce25SScott Long 13830a4094fSAlexander Motin static struct ata_quirk_entry ata_quirk_table[] = 13952c9ce25SScott Long { 14052c9ce25SScott Long { 14152c9ce25SScott Long /* Default tagged queuing parameters for all devices */ 14252c9ce25SScott Long { 14352c9ce25SScott Long T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, 14452c9ce25SScott Long /*vendor*/"*", /*product*/"*", /*revision*/"*" 14552c9ce25SScott Long }, 14630a4094fSAlexander Motin /*quirks*/0, /*maxtags*/0 14752c9ce25SScott Long }, 14852c9ce25SScott Long }; 14952c9ce25SScott Long 15030a4094fSAlexander Motin static const int ata_quirk_table_size = 15130a4094fSAlexander Motin sizeof(ata_quirk_table) / sizeof(*ata_quirk_table); 15252c9ce25SScott Long 15352c9ce25SScott Long static cam_status proberegister(struct cam_periph *periph, 15452c9ce25SScott Long void *arg); 15552c9ce25SScott Long static void probeschedule(struct cam_periph *probe_periph); 15652c9ce25SScott Long static void probestart(struct cam_periph *periph, union ccb *start_ccb); 15752c9ce25SScott Long //static void proberequestdefaultnegotiation(struct cam_periph *periph); 15852c9ce25SScott Long //static int proberequestbackoff(struct cam_periph *periph, 15952c9ce25SScott Long // struct cam_ed *device); 16052c9ce25SScott Long static void probedone(struct cam_periph *periph, union ccb *done_ccb); 16152c9ce25SScott Long static void probecleanup(struct cam_periph *periph); 16230a4094fSAlexander Motin static void ata_find_quirk(struct cam_ed *device); 16352c9ce25SScott Long static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb); 16452c9ce25SScott Long static void ata_scan_lun(struct cam_periph *periph, 16552c9ce25SScott Long struct cam_path *path, cam_flags flags, 16652c9ce25SScott Long union ccb *ccb); 16752c9ce25SScott Long static void xptscandone(struct cam_periph *periph, union ccb *done_ccb); 16852c9ce25SScott Long static struct cam_ed * 16952c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, 17052c9ce25SScott Long lun_id_t lun_id); 17152c9ce25SScott Long static void ata_device_transport(struct cam_path *path); 17230a4094fSAlexander Motin static void ata_set_transfer_settings(struct ccb_trans_settings *cts, 17352c9ce25SScott Long struct cam_ed *device, 17452c9ce25SScott Long int async_update); 17552c9ce25SScott Long static void ata_dev_async(u_int32_t async_code, 17652c9ce25SScott Long struct cam_eb *bus, 17752c9ce25SScott Long struct cam_et *target, 17852c9ce25SScott Long struct cam_ed *device, 17952c9ce25SScott Long void *async_arg); 18052c9ce25SScott Long static void ata_action(union ccb *start_ccb); 18157079b17SAlexander Motin static void ata_announce_periph(struct cam_periph *periph); 18252c9ce25SScott Long 18352c9ce25SScott Long static struct xpt_xport ata_xport = { 18452c9ce25SScott Long .alloc_device = ata_alloc_device, 18552c9ce25SScott Long .action = ata_action, 18652c9ce25SScott Long .async = ata_dev_async, 18757079b17SAlexander Motin .announce = ata_announce_periph, 18852c9ce25SScott Long }; 18952c9ce25SScott Long 19052c9ce25SScott Long struct xpt_xport * 19152c9ce25SScott Long ata_get_xport(void) 19252c9ce25SScott Long { 19352c9ce25SScott Long return (&ata_xport); 19452c9ce25SScott Long } 19552c9ce25SScott Long 19652c9ce25SScott Long static void 19752c9ce25SScott Long probe_periph_init() 19852c9ce25SScott Long { 19952c9ce25SScott Long } 20052c9ce25SScott Long 20152c9ce25SScott Long static cam_status 20252c9ce25SScott Long proberegister(struct cam_periph *periph, void *arg) 20352c9ce25SScott Long { 20452c9ce25SScott Long union ccb *request_ccb; /* CCB representing the probe request */ 20552c9ce25SScott Long cam_status status; 20652c9ce25SScott Long probe_softc *softc; 20752c9ce25SScott Long 20852c9ce25SScott Long request_ccb = (union ccb *)arg; 20952c9ce25SScott Long if (periph == NULL) { 21052c9ce25SScott Long printf("proberegister: periph was NULL!!\n"); 21152c9ce25SScott Long return(CAM_REQ_CMP_ERR); 21252c9ce25SScott Long } 21352c9ce25SScott Long 21452c9ce25SScott Long if (request_ccb == NULL) { 21552c9ce25SScott Long printf("proberegister: no probe CCB, " 21652c9ce25SScott Long "can't register device\n"); 21752c9ce25SScott Long return(CAM_REQ_CMP_ERR); 21852c9ce25SScott Long } 21952c9ce25SScott Long 2204ef08dc5SAlexander Motin softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT); 22152c9ce25SScott Long 22252c9ce25SScott Long if (softc == NULL) { 22352c9ce25SScott Long printf("proberegister: Unable to probe new device. " 22452c9ce25SScott Long "Unable to allocate softc\n"); 22552c9ce25SScott Long return(CAM_REQ_CMP_ERR); 22652c9ce25SScott Long } 22752c9ce25SScott Long TAILQ_INIT(&softc->request_ccbs); 22852c9ce25SScott Long TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 22952c9ce25SScott Long periph_links.tqe); 23052c9ce25SScott Long softc->flags = 0; 23152c9ce25SScott Long periph->softc = softc; 23252c9ce25SScott Long softc->periph = periph; 23352c9ce25SScott Long softc->action = PROBE_INVALID; 23452c9ce25SScott Long status = cam_periph_acquire(periph); 23552c9ce25SScott Long if (status != CAM_REQ_CMP) { 23652c9ce25SScott Long return (status); 23752c9ce25SScott Long } 23852c9ce25SScott Long /* 23983c5d981SAlexander Motin * Ensure nobody slip in until probe finish. 24052c9ce25SScott Long */ 24183c5d981SAlexander Motin cam_freeze_devq_arg(periph->path, 24283c5d981SAlexander Motin RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1); 24352c9ce25SScott Long probeschedule(periph); 24452c9ce25SScott Long return(CAM_REQ_CMP); 24552c9ce25SScott Long } 24652c9ce25SScott Long 24752c9ce25SScott Long static void 24852c9ce25SScott Long probeschedule(struct cam_periph *periph) 24952c9ce25SScott Long { 25052c9ce25SScott Long union ccb *ccb; 25152c9ce25SScott Long probe_softc *softc; 25252c9ce25SScott Long 25352c9ce25SScott Long softc = (probe_softc *)periph->softc; 25452c9ce25SScott Long ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs); 25552c9ce25SScott Long 25665d0fb03SAlexander Motin if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) || 25765d0fb03SAlexander Motin periph->path->device->protocol == PROTO_SATAPM) 25852c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_RESET); 25952c9ce25SScott Long else 26052c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 26152c9ce25SScott Long 26252c9ce25SScott Long if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE) 26352c9ce25SScott Long softc->flags |= PROBE_NO_ANNOUNCE; 26452c9ce25SScott Long else 26552c9ce25SScott Long softc->flags &= ~PROBE_NO_ANNOUNCE; 26652c9ce25SScott Long 26783c5d981SAlexander Motin xpt_schedule(periph, CAM_PRIORITY_XPT); 26852c9ce25SScott Long } 26952c9ce25SScott Long 27052c9ce25SScott Long static void 27152c9ce25SScott Long probestart(struct cam_periph *periph, union ccb *start_ccb) 27252c9ce25SScott Long { 273c8039fc6SAlexander Motin struct ccb_trans_settings cts; 27452c9ce25SScott Long struct ccb_ataio *ataio; 27552c9ce25SScott Long struct ccb_scsiio *csio; 27652c9ce25SScott Long probe_softc *softc; 2771e637ba6SAlexander Motin struct cam_path *path; 2781e637ba6SAlexander Motin struct ata_params *ident_buf; 27952c9ce25SScott Long 28052c9ce25SScott Long CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n")); 28152c9ce25SScott Long 28252c9ce25SScott Long softc = (probe_softc *)periph->softc; 2831e637ba6SAlexander Motin path = start_ccb->ccb_h.path; 28452c9ce25SScott Long ataio = &start_ccb->ataio; 28552c9ce25SScott Long csio = &start_ccb->csio; 2861e637ba6SAlexander Motin ident_buf = &periph->path->device->ident_data; 28752c9ce25SScott Long 28883c5d981SAlexander Motin if (softc->restart) { 28983c5d981SAlexander Motin softc->restart = 0; 29083c5d981SAlexander Motin if ((path->device->flags & CAM_DEV_UNCONFIGURED) || 29183c5d981SAlexander Motin path->device->protocol == PROTO_SATAPM) 29283c5d981SAlexander Motin softc->action = PROBE_RESET; 29383c5d981SAlexander Motin else 29483c5d981SAlexander Motin softc->action = PROBE_IDENTIFY; 29583c5d981SAlexander Motin } 29652c9ce25SScott Long switch (softc->action) { 29752c9ce25SScott Long case PROBE_RESET: 29852c9ce25SScott Long cam_fill_ataio(ataio, 29952c9ce25SScott Long 0, 30052c9ce25SScott Long probedone, 30152c9ce25SScott Long /*flags*/CAM_DIR_NONE, 30265d0fb03SAlexander Motin 0, 30352c9ce25SScott Long /*data_ptr*/NULL, 30452c9ce25SScott Long /*dxfer_len*/0, 30583c5d981SAlexander Motin 15 * 1000); 30652c9ce25SScott Long ata_reset_cmd(ataio); 30752c9ce25SScott Long break; 30852c9ce25SScott Long case PROBE_IDENTIFY: 30952c9ce25SScott Long cam_fill_ataio(ataio, 31052c9ce25SScott Long 1, 31152c9ce25SScott Long probedone, 31252c9ce25SScott Long /*flags*/CAM_DIR_IN, 31365d0fb03SAlexander Motin 0, 314a9b8edb1SAlexander Motin /*data_ptr*/(u_int8_t *)&softc->ident_data, 315a9b8edb1SAlexander Motin /*dxfer_len*/sizeof(softc->ident_data), 31652c9ce25SScott Long 30 * 1000); 31752c9ce25SScott Long if (periph->path->device->protocol == PROTO_ATA) 3187606b445SAlexander Motin ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0); 31952c9ce25SScott Long else 3207606b445SAlexander Motin ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0); 32152c9ce25SScott Long break; 3224ef08dc5SAlexander Motin case PROBE_SPINUP: 3234ef08dc5SAlexander Motin if (bootverbose) 3244ef08dc5SAlexander Motin xpt_print(path, "Spinning up device\n"); 3254ef08dc5SAlexander Motin cam_fill_ataio(ataio, 3264ef08dc5SAlexander Motin 1, 3274ef08dc5SAlexander Motin probedone, 3284ef08dc5SAlexander Motin /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER, 3294ef08dc5SAlexander Motin 0, 3304ef08dc5SAlexander Motin /*data_ptr*/NULL, 3314ef08dc5SAlexander Motin /*dxfer_len*/0, 3324ef08dc5SAlexander Motin 30 * 1000); 3334ef08dc5SAlexander Motin ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0); 3344ef08dc5SAlexander Motin break; 33552c9ce25SScott Long case PROBE_SETMODE: 336c8039fc6SAlexander Motin { 337c8039fc6SAlexander Motin int mode, wantmode; 338c8039fc6SAlexander Motin 339c8039fc6SAlexander Motin mode = 0; 340c8039fc6SAlexander Motin /* Fetch user modes from SIM. */ 341c8039fc6SAlexander Motin bzero(&cts, sizeof(cts)); 34283c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 343c8039fc6SAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 344c8039fc6SAlexander Motin cts.type = CTS_TYPE_USER_SETTINGS; 345c8039fc6SAlexander Motin xpt_action((union ccb *)&cts); 346c8039fc6SAlexander Motin if (path->device->transport == XPORT_ATA) { 347c8039fc6SAlexander Motin if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE) 348c8039fc6SAlexander Motin mode = cts.xport_specific.ata.mode; 349c8039fc6SAlexander Motin } else { 3501a6f09b8SAlexander Motin if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE) 351c8039fc6SAlexander Motin mode = cts.xport_specific.sata.mode; 352c8039fc6SAlexander Motin } 353c8039fc6SAlexander Motin negotiate: 354c8039fc6SAlexander Motin /* Honor device capabilities. */ 355c8039fc6SAlexander Motin wantmode = mode = ata_max_mode(ident_buf, mode); 356c8039fc6SAlexander Motin /* Report modes to SIM. */ 357c8039fc6SAlexander Motin bzero(&cts, sizeof(cts)); 35883c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 359c8039fc6SAlexander Motin cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 360c8039fc6SAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 361c8039fc6SAlexander Motin if (path->device->transport == XPORT_ATA) { 362c8039fc6SAlexander Motin cts.xport_specific.ata.mode = mode; 363c8039fc6SAlexander Motin cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE; 364c8039fc6SAlexander Motin } else { 365c8039fc6SAlexander Motin cts.xport_specific.sata.mode = mode; 366c8039fc6SAlexander Motin cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE; 367c8039fc6SAlexander Motin } 368c8039fc6SAlexander Motin xpt_action((union ccb *)&cts); 369066f913aSAlexander Motin /* Fetch current modes from SIM. */ 370c8039fc6SAlexander Motin bzero(&cts, sizeof(cts)); 37183c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 372c8039fc6SAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 373c8039fc6SAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 374c8039fc6SAlexander Motin xpt_action((union ccb *)&cts); 375c8039fc6SAlexander Motin if (path->device->transport == XPORT_ATA) { 376c8039fc6SAlexander Motin if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE) 377c8039fc6SAlexander Motin mode = cts.xport_specific.ata.mode; 378c8039fc6SAlexander Motin } else { 379c8039fc6SAlexander Motin if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE) 380c8039fc6SAlexander Motin mode = cts.xport_specific.sata.mode; 381c8039fc6SAlexander Motin } 382c8039fc6SAlexander Motin /* If SIM disagree - renegotiate. */ 383c8039fc6SAlexander Motin if (mode != wantmode) 384c8039fc6SAlexander Motin goto negotiate; 38552c9ce25SScott Long cam_fill_ataio(ataio, 38652c9ce25SScott Long 1, 38752c9ce25SScott Long probedone, 3885daa555dSAlexander Motin /*flags*/CAM_DIR_NONE, 3895daa555dSAlexander Motin 0, 3905daa555dSAlexander Motin /*data_ptr*/NULL, 3915daa555dSAlexander Motin /*dxfer_len*/0, 39252c9ce25SScott Long 30 * 1000); 393c8039fc6SAlexander Motin ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode); 39452c9ce25SScott Long break; 395c8039fc6SAlexander Motin } 3961e637ba6SAlexander Motin case PROBE_SET_MULTI: 3971e637ba6SAlexander Motin { 398066f913aSAlexander Motin u_int sectors, bytecount; 3991e637ba6SAlexander Motin 400066f913aSAlexander Motin bytecount = 8192; /* SATA maximum */ 401066f913aSAlexander Motin /* Fetch user bytecount from SIM. */ 402066f913aSAlexander Motin bzero(&cts, sizeof(cts)); 40383c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 404066f913aSAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 405066f913aSAlexander Motin cts.type = CTS_TYPE_USER_SETTINGS; 406066f913aSAlexander Motin xpt_action((union ccb *)&cts); 407066f913aSAlexander Motin if (path->device->transport == XPORT_ATA) { 408066f913aSAlexander Motin if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 409066f913aSAlexander Motin bytecount = cts.xport_specific.ata.bytecount; 410066f913aSAlexander Motin } else { 411066f913aSAlexander Motin if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 412066f913aSAlexander Motin bytecount = cts.xport_specific.sata.bytecount; 413066f913aSAlexander Motin } 414066f913aSAlexander Motin /* Honor device capabilities. */ 415066f913aSAlexander Motin sectors = max(1, min(ident_buf->sectors_intr & 0xff, 416066f913aSAlexander Motin bytecount / ata_logical_sector_size(ident_buf))); 4171e637ba6SAlexander Motin /* Report bytecount to SIM. */ 4181e637ba6SAlexander Motin bzero(&cts, sizeof(cts)); 41983c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 4201e637ba6SAlexander Motin cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 4211e637ba6SAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 4221e637ba6SAlexander Motin if (path->device->transport == XPORT_ATA) { 423c1bd46c2SAlexander Motin cts.xport_specific.ata.bytecount = sectors * 424c1bd46c2SAlexander Motin ata_logical_sector_size(ident_buf); 4251e637ba6SAlexander Motin cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 4261e637ba6SAlexander Motin } else { 427c1bd46c2SAlexander Motin cts.xport_specific.sata.bytecount = sectors * 428c1bd46c2SAlexander Motin ata_logical_sector_size(ident_buf); 4291e637ba6SAlexander Motin cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 4301e637ba6SAlexander Motin } 4311e637ba6SAlexander Motin xpt_action((union ccb *)&cts); 432066f913aSAlexander Motin /* Fetch current bytecount from SIM. */ 433066f913aSAlexander Motin bzero(&cts, sizeof(cts)); 43483c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 435066f913aSAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 436066f913aSAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 437066f913aSAlexander Motin xpt_action((union ccb *)&cts); 438066f913aSAlexander Motin if (path->device->transport == XPORT_ATA) { 439066f913aSAlexander Motin if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 440066f913aSAlexander Motin bytecount = cts.xport_specific.ata.bytecount; 441066f913aSAlexander Motin } else { 442066f913aSAlexander Motin if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 443066f913aSAlexander Motin bytecount = cts.xport_specific.sata.bytecount; 444066f913aSAlexander Motin } 445066f913aSAlexander Motin sectors = bytecount / ata_logical_sector_size(ident_buf); 4461e637ba6SAlexander Motin 4471e637ba6SAlexander Motin cam_fill_ataio(ataio, 4481e637ba6SAlexander Motin 1, 4491e637ba6SAlexander Motin probedone, 4501e637ba6SAlexander Motin CAM_DIR_NONE, 4511e637ba6SAlexander Motin 0, 4521e637ba6SAlexander Motin NULL, 4531e637ba6SAlexander Motin 0, 4541e637ba6SAlexander Motin 30*1000); 4551e637ba6SAlexander Motin ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors); 4561e637ba6SAlexander Motin break; 45752c9ce25SScott Long } 45852c9ce25SScott Long case PROBE_INQUIRY: 459066f913aSAlexander Motin { 460066f913aSAlexander Motin u_int bytecount; 461066f913aSAlexander Motin 462066f913aSAlexander Motin bytecount = 8192; /* SATA maximum */ 463066f913aSAlexander Motin /* Fetch user bytecount from SIM. */ 464066f913aSAlexander Motin bzero(&cts, sizeof(cts)); 46583c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 466066f913aSAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 467066f913aSAlexander Motin cts.type = CTS_TYPE_USER_SETTINGS; 468066f913aSAlexander Motin xpt_action((union ccb *)&cts); 469066f913aSAlexander Motin if (path->device->transport == XPORT_ATA) { 470066f913aSAlexander Motin if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT) 471066f913aSAlexander Motin bytecount = cts.xport_specific.ata.bytecount; 472066f913aSAlexander Motin } else { 473066f913aSAlexander Motin if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) 474066f913aSAlexander Motin bytecount = cts.xport_specific.sata.bytecount; 475066f913aSAlexander Motin } 476066f913aSAlexander Motin /* Honor device capabilities. */ 477066f913aSAlexander Motin bytecount &= ~1; 478066f913aSAlexander Motin bytecount = max(2, min(65534, bytecount)); 479066f913aSAlexander Motin if (ident_buf->satacapabilities != 0x0000 && 480066f913aSAlexander Motin ident_buf->satacapabilities != 0xffff) { 481066f913aSAlexander Motin bytecount = min(8192, bytecount); 482066f913aSAlexander Motin } 483066f913aSAlexander Motin /* Report bytecount to SIM. */ 484066f913aSAlexander Motin bzero(&cts, sizeof(cts)); 48583c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 486066f913aSAlexander Motin cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 487066f913aSAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 488066f913aSAlexander Motin if (path->device->transport == XPORT_ATA) { 489066f913aSAlexander Motin cts.xport_specific.ata.bytecount = bytecount; 490066f913aSAlexander Motin cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT; 491066f913aSAlexander Motin } else { 492066f913aSAlexander Motin cts.xport_specific.sata.bytecount = bytecount; 493066f913aSAlexander Motin cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT; 494066f913aSAlexander Motin } 495066f913aSAlexander Motin xpt_action((union ccb *)&cts); 496066f913aSAlexander Motin /* FALLTHROUGH */ 497066f913aSAlexander Motin } 49852c9ce25SScott Long case PROBE_FULL_INQUIRY: 49952c9ce25SScott Long { 50052c9ce25SScott Long u_int inquiry_len; 50152c9ce25SScott Long struct scsi_inquiry_data *inq_buf = 50252c9ce25SScott Long &periph->path->device->inq_data; 50352c9ce25SScott Long 50452c9ce25SScott Long if (softc->action == PROBE_INQUIRY) 50552c9ce25SScott Long inquiry_len = SHORT_INQUIRY_LENGTH; 50652c9ce25SScott Long else 50752c9ce25SScott Long inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf); 50852c9ce25SScott Long /* 50952c9ce25SScott Long * Some parallel SCSI devices fail to send an 51052c9ce25SScott Long * ignore wide residue message when dealing with 51152c9ce25SScott Long * odd length inquiry requests. Round up to be 51252c9ce25SScott Long * safe. 51352c9ce25SScott Long */ 51452c9ce25SScott Long inquiry_len = roundup2(inquiry_len, 2); 51552c9ce25SScott Long scsi_inquiry(csio, 51652c9ce25SScott Long /*retries*/1, 51752c9ce25SScott Long probedone, 51852c9ce25SScott Long MSG_SIMPLE_Q_TAG, 51952c9ce25SScott Long (u_int8_t *)inq_buf, 52052c9ce25SScott Long inquiry_len, 52152c9ce25SScott Long /*evpd*/FALSE, 52252c9ce25SScott Long /*page_code*/0, 52352c9ce25SScott Long SSD_MIN_SIZE, 52452c9ce25SScott Long /*timeout*/60 * 1000); 52552c9ce25SScott Long break; 52652c9ce25SScott Long } 52752c9ce25SScott Long case PROBE_PM_PID: 52852c9ce25SScott Long cam_fill_ataio(ataio, 52952c9ce25SScott Long 1, 53052c9ce25SScott Long probedone, 53152c9ce25SScott Long /*flags*/CAM_DIR_NONE, 53265d0fb03SAlexander Motin 0, 53352c9ce25SScott Long /*data_ptr*/NULL, 53452c9ce25SScott Long /*dxfer_len*/0, 53552c9ce25SScott Long 10 * 1000); 53652c9ce25SScott Long ata_pm_read_cmd(ataio, 0, 15); 53752c9ce25SScott Long break; 53852c9ce25SScott Long case PROBE_PM_PRV: 53952c9ce25SScott Long cam_fill_ataio(ataio, 54052c9ce25SScott Long 1, 54152c9ce25SScott Long probedone, 54252c9ce25SScott Long /*flags*/CAM_DIR_NONE, 54365d0fb03SAlexander Motin 0, 54452c9ce25SScott Long /*data_ptr*/NULL, 54552c9ce25SScott Long /*dxfer_len*/0, 54652c9ce25SScott Long 10 * 1000); 54752c9ce25SScott Long ata_pm_read_cmd(ataio, 1, 15); 54852c9ce25SScott Long break; 54952c9ce25SScott Long case PROBE_INVALID: 5501e637ba6SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_INFO, 55152c9ce25SScott Long ("probestart: invalid action state\n")); 55252c9ce25SScott Long default: 55352c9ce25SScott Long break; 55452c9ce25SScott Long } 55552c9ce25SScott Long xpt_action(start_ccb); 55652c9ce25SScott Long } 55752c9ce25SScott Long #if 0 55852c9ce25SScott Long static void 55952c9ce25SScott Long proberequestdefaultnegotiation(struct cam_periph *periph) 56052c9ce25SScott Long { 56152c9ce25SScott Long struct ccb_trans_settings cts; 56252c9ce25SScott Long 56383c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE); 56452c9ce25SScott Long cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 56552c9ce25SScott Long cts.type = CTS_TYPE_USER_SETTINGS; 56652c9ce25SScott Long xpt_action((union ccb *)&cts); 56752c9ce25SScott Long if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 56852c9ce25SScott Long return; 56952c9ce25SScott Long } 57052c9ce25SScott Long cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 57152c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 57252c9ce25SScott Long xpt_action((union ccb *)&cts); 57352c9ce25SScott Long } 57452c9ce25SScott Long 57552c9ce25SScott Long /* 57652c9ce25SScott Long * Backoff Negotiation Code- only pertinent for SPI devices. 57752c9ce25SScott Long */ 57852c9ce25SScott Long static int 57952c9ce25SScott Long proberequestbackoff(struct cam_periph *periph, struct cam_ed *device) 58052c9ce25SScott Long { 58152c9ce25SScott Long struct ccb_trans_settings cts; 58252c9ce25SScott Long struct ccb_trans_settings_spi *spi; 58352c9ce25SScott Long 58452c9ce25SScott Long memset(&cts, 0, sizeof (cts)); 58583c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE); 58652c9ce25SScott Long cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 58752c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 58852c9ce25SScott Long xpt_action((union ccb *)&cts); 58952c9ce25SScott Long if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 59052c9ce25SScott Long if (bootverbose) { 59152c9ce25SScott Long xpt_print(periph->path, 59252c9ce25SScott Long "failed to get current device settings\n"); 59352c9ce25SScott Long } 59452c9ce25SScott Long return (0); 59552c9ce25SScott Long } 59652c9ce25SScott Long if (cts.transport != XPORT_SPI) { 59752c9ce25SScott Long if (bootverbose) { 59852c9ce25SScott Long xpt_print(periph->path, "not SPI transport\n"); 59952c9ce25SScott Long } 60052c9ce25SScott Long return (0); 60152c9ce25SScott Long } 60252c9ce25SScott Long spi = &cts.xport_specific.spi; 60352c9ce25SScott Long 60452c9ce25SScott Long /* 60552c9ce25SScott Long * We cannot renegotiate sync rate if we don't have one. 60652c9ce25SScott Long */ 60752c9ce25SScott Long if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) { 60852c9ce25SScott Long if (bootverbose) { 60952c9ce25SScott Long xpt_print(periph->path, "no sync rate known\n"); 61052c9ce25SScott Long } 61152c9ce25SScott Long return (0); 61252c9ce25SScott Long } 61352c9ce25SScott Long 61452c9ce25SScott Long /* 61552c9ce25SScott Long * We'll assert that we don't have to touch PPR options- the 61652c9ce25SScott Long * SIM will see what we do with period and offset and adjust 61752c9ce25SScott Long * the PPR options as appropriate. 61852c9ce25SScott Long */ 61952c9ce25SScott Long 62052c9ce25SScott Long /* 62152c9ce25SScott Long * A sync rate with unknown or zero offset is nonsensical. 62252c9ce25SScott Long * A sync period of zero means Async. 62352c9ce25SScott Long */ 62452c9ce25SScott Long if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0 62552c9ce25SScott Long || spi->sync_offset == 0 || spi->sync_period == 0) { 62652c9ce25SScott Long if (bootverbose) { 62752c9ce25SScott Long xpt_print(periph->path, "no sync rate available\n"); 62852c9ce25SScott Long } 62952c9ce25SScott Long return (0); 63052c9ce25SScott Long } 63152c9ce25SScott Long 63252c9ce25SScott Long if (device->flags & CAM_DEV_DV_HIT_BOTTOM) { 63352c9ce25SScott Long CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 63452c9ce25SScott Long ("hit async: giving up on DV\n")); 63552c9ce25SScott Long return (0); 63652c9ce25SScott Long } 63752c9ce25SScott Long 63852c9ce25SScott Long 63952c9ce25SScott Long /* 64052c9ce25SScott Long * Jump sync_period up by one, but stop at 5MHz and fall back to Async. 64152c9ce25SScott Long * We don't try to remember 'last' settings to see if the SIM actually 64252c9ce25SScott Long * gets into the speed we want to set. We check on the SIM telling 64352c9ce25SScott Long * us that a requested speed is bad, but otherwise don't try and 64452c9ce25SScott Long * check the speed due to the asynchronous and handshake nature 64552c9ce25SScott Long * of speed setting. 64652c9ce25SScott Long */ 64752c9ce25SScott Long spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET; 64852c9ce25SScott Long for (;;) { 64952c9ce25SScott Long spi->sync_period++; 65052c9ce25SScott Long if (spi->sync_period >= 0xf) { 65152c9ce25SScott Long spi->sync_period = 0; 65252c9ce25SScott Long spi->sync_offset = 0; 65352c9ce25SScott Long CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 65452c9ce25SScott Long ("setting to async for DV\n")); 65552c9ce25SScott Long /* 65652c9ce25SScott Long * Once we hit async, we don't want to try 65752c9ce25SScott Long * any more settings. 65852c9ce25SScott Long */ 65952c9ce25SScott Long device->flags |= CAM_DEV_DV_HIT_BOTTOM; 66052c9ce25SScott Long } else if (bootverbose) { 66152c9ce25SScott Long CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 66252c9ce25SScott Long ("DV: period 0x%x\n", spi->sync_period)); 66352c9ce25SScott Long printf("setting period to 0x%x\n", spi->sync_period); 66452c9ce25SScott Long } 66552c9ce25SScott Long cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 66652c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 66752c9ce25SScott Long xpt_action((union ccb *)&cts); 66852c9ce25SScott Long if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 66952c9ce25SScott Long break; 67052c9ce25SScott Long } 67152c9ce25SScott Long CAM_DEBUG(periph->path, CAM_DEBUG_INFO, 67252c9ce25SScott Long ("DV: failed to set period 0x%x\n", spi->sync_period)); 67352c9ce25SScott Long if (spi->sync_period == 0) { 67452c9ce25SScott Long return (0); 67552c9ce25SScott Long } 67652c9ce25SScott Long } 67752c9ce25SScott Long return (1); 67852c9ce25SScott Long } 67952c9ce25SScott Long #endif 68052c9ce25SScott Long static void 68152c9ce25SScott Long probedone(struct cam_periph *periph, union ccb *done_ccb) 68252c9ce25SScott Long { 683c8039fc6SAlexander Motin struct ccb_trans_settings cts; 68452c9ce25SScott Long struct ata_params *ident_buf; 68552c9ce25SScott Long probe_softc *softc; 68652c9ce25SScott Long struct cam_path *path; 68752c9ce25SScott Long u_int32_t priority; 68865d0fb03SAlexander Motin int found = 1; 68952c9ce25SScott Long 69052c9ce25SScott Long CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n")); 69152c9ce25SScott Long 69252c9ce25SScott Long softc = (probe_softc *)periph->softc; 69352c9ce25SScott Long path = done_ccb->ccb_h.path; 69452c9ce25SScott Long priority = done_ccb->ccb_h.pinfo.priority; 69552c9ce25SScott Long ident_buf = &path->device->ident_data; 69652c9ce25SScott Long 6971e637ba6SAlexander Motin if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 698639c2d4fSAlexander Motin device_fail: if ((!softc->restart) && 699639c2d4fSAlexander Motin cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) { 7001e637ba6SAlexander Motin return; 7011e637ba6SAlexander Motin } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 7021e637ba6SAlexander Motin /* Don't wedge the queue */ 7031e637ba6SAlexander Motin xpt_release_devq(done_ccb->ccb_h.path, /*count*/1, 7041e637ba6SAlexander Motin /*run_queue*/TRUE); 7051e637ba6SAlexander Motin } 7061e637ba6SAlexander Motin /* Old PIO2 devices may not support mode setting. */ 7071e637ba6SAlexander Motin if (softc->action == PROBE_SETMODE && 7081e637ba6SAlexander Motin ata_max_pmode(ident_buf) <= ATA_PIO2 && 7091e637ba6SAlexander Motin (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) 7101e637ba6SAlexander Motin goto noerror; 7111e637ba6SAlexander Motin /* 7121e637ba6SAlexander Motin * If we get to this point, we got an error status back 7131e637ba6SAlexander Motin * from the inquiry and the error status doesn't require 7141e637ba6SAlexander Motin * automatically retrying the command. Therefore, the 7151e637ba6SAlexander Motin * inquiry failed. If we had inquiry information before 7161e637ba6SAlexander Motin * for this device, but this latest inquiry command failed, 7171e637ba6SAlexander Motin * the device has probably gone away. If this device isn't 7181e637ba6SAlexander Motin * already marked unconfigured, notify the peripheral 7191e637ba6SAlexander Motin * drivers that this device is no more. 7201e637ba6SAlexander Motin */ 7211e637ba6SAlexander Motin if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) 7221e637ba6SAlexander Motin xpt_async(AC_LOST_DEVICE, path, NULL); 7231e637ba6SAlexander Motin found = 0; 7241e637ba6SAlexander Motin goto done; 7251e637ba6SAlexander Motin } 7261e637ba6SAlexander Motin noerror: 727a9b8edb1SAlexander Motin if (softc->restart) 728a9b8edb1SAlexander Motin goto done; 72952c9ce25SScott Long switch (softc->action) { 73052c9ce25SScott Long case PROBE_RESET: 7311e637ba6SAlexander Motin { 73252c9ce25SScott Long int sign = (done_ccb->ataio.res.lba_high << 8) + 73352c9ce25SScott Long done_ccb->ataio.res.lba_mid; 7347bdb664eSAlexander Motin if (bootverbose) 73552c9ce25SScott Long xpt_print(path, "SIGNATURE: %04x\n", sign); 73652c9ce25SScott Long if (sign == 0x0000 && 73752c9ce25SScott Long done_ccb->ccb_h.target_id != 15) { 73852c9ce25SScott Long path->device->protocol = PROTO_ATA; 73952c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 74052c9ce25SScott Long } else if (sign == 0x9669 && 74152c9ce25SScott Long done_ccb->ccb_h.target_id == 15) { 74252c9ce25SScott Long /* Report SIM that PM is present. */ 74352c9ce25SScott Long bzero(&cts, sizeof(cts)); 74483c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 74552c9ce25SScott Long cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 74652c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 74752c9ce25SScott Long cts.xport_specific.sata.pm_present = 1; 74852c9ce25SScott Long cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 74952c9ce25SScott Long xpt_action((union ccb *)&cts); 75052c9ce25SScott Long path->device->protocol = PROTO_SATAPM; 75152c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_PM_PID); 75252c9ce25SScott Long } else if (sign == 0xeb14 && 75352c9ce25SScott Long done_ccb->ccb_h.target_id != 15) { 75452c9ce25SScott Long path->device->protocol = PROTO_SCSI; 75552c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 75652c9ce25SScott Long } else { 75752c9ce25SScott Long if (done_ccb->ccb_h.target_id != 15) { 75852c9ce25SScott Long xpt_print(path, 75952c9ce25SScott Long "Unexpected signature 0x%04x\n", sign); 76052c9ce25SScott Long } 76165d0fb03SAlexander Motin goto device_fail; 76252c9ce25SScott Long } 76352c9ce25SScott Long xpt_release_ccb(done_ccb); 76452c9ce25SScott Long xpt_schedule(periph, priority); 76552c9ce25SScott Long return; 76652c9ce25SScott Long } 76752c9ce25SScott Long case PROBE_IDENTIFY: 76852c9ce25SScott Long { 769699f853bSAlexander Motin struct ccb_pathinq cpi; 77052c9ce25SScott Long int16_t *ptr; 7712eb4a8dcSAlexander Motin int changed = 1; 77252c9ce25SScott Long 773a9b8edb1SAlexander Motin ident_buf = &softc->ident_data; 77452c9ce25SScott Long for (ptr = (int16_t *)ident_buf; 77552c9ce25SScott Long ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) { 77652c9ce25SScott Long *ptr = le16toh(*ptr); 77752c9ce25SScott Long } 77852c9ce25SScott Long if (strncmp(ident_buf->model, "FX", 2) && 77952c9ce25SScott Long strncmp(ident_buf->model, "NEC", 3) && 78052c9ce25SScott Long strncmp(ident_buf->model, "Pioneer", 7) && 78152c9ce25SScott Long strncmp(ident_buf->model, "SHARP", 5)) { 78252c9ce25SScott Long ata_bswap(ident_buf->model, sizeof(ident_buf->model)); 78352c9ce25SScott Long ata_bswap(ident_buf->revision, sizeof(ident_buf->revision)); 78452c9ce25SScott Long ata_bswap(ident_buf->serial, sizeof(ident_buf->serial)); 78552c9ce25SScott Long } 78652c9ce25SScott Long ata_btrim(ident_buf->model, sizeof(ident_buf->model)); 78752c9ce25SScott Long ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model)); 78852c9ce25SScott Long ata_btrim(ident_buf->revision, sizeof(ident_buf->revision)); 78952c9ce25SScott Long ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision)); 79052c9ce25SScott Long ata_btrim(ident_buf->serial, sizeof(ident_buf->serial)); 79152c9ce25SScott Long ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial)); 7924ef08dc5SAlexander Motin /* Device may need spin-up before IDENTIFY become valid. */ 7931254b680SAlexander Motin if ((ident_buf->specconf == 0x37c8 || 7941254b680SAlexander Motin ident_buf->specconf == 0x738c) && 7951254b680SAlexander Motin ((ident_buf->config & ATA_RESP_INCOMPLETE) || 7964ef08dc5SAlexander Motin softc->spinup == 0)) { 7974ef08dc5SAlexander Motin PROBE_SET_ACTION(softc, PROBE_SPINUP); 7984ef08dc5SAlexander Motin xpt_release_ccb(done_ccb); 7994ef08dc5SAlexander Motin xpt_schedule(periph, priority); 8004ef08dc5SAlexander Motin return; 8014ef08dc5SAlexander Motin } 802a9b8edb1SAlexander Motin ident_buf = &path->device->ident_data; 80352c9ce25SScott Long if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) { 80452c9ce25SScott Long /* Check that it is the same device. */ 805a9b8edb1SAlexander Motin if (bcmp(softc->ident_data.model, ident_buf->model, 806a9b8edb1SAlexander Motin sizeof(ident_buf->model)) || 807a9b8edb1SAlexander Motin bcmp(softc->ident_data.revision, ident_buf->revision, 808a9b8edb1SAlexander Motin sizeof(ident_buf->revision)) || 809a9b8edb1SAlexander Motin bcmp(softc->ident_data.serial, ident_buf->serial, 810a9b8edb1SAlexander Motin sizeof(ident_buf->serial))) { 81152c9ce25SScott Long /* Device changed. */ 81252c9ce25SScott Long xpt_async(AC_LOST_DEVICE, path, NULL); 8131e637ba6SAlexander Motin } else { 814a9b8edb1SAlexander Motin bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 8152eb4a8dcSAlexander Motin changed = 0; 8162eb4a8dcSAlexander Motin } 8172eb4a8dcSAlexander Motin } 8182eb4a8dcSAlexander Motin if (changed) { 8192eb4a8dcSAlexander Motin bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params)); 82052c9ce25SScott Long /* Clean up from previous instance of this device */ 82152c9ce25SScott Long if (path->device->serial_num != NULL) { 82252c9ce25SScott Long free(path->device->serial_num, M_CAMXPT); 82352c9ce25SScott Long path->device->serial_num = NULL; 82452c9ce25SScott Long path->device->serial_num_len = 0; 82552c9ce25SScott Long } 82652c9ce25SScott Long path->device->serial_num = 82752c9ce25SScott Long (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1), 82852c9ce25SScott Long M_CAMXPT, M_NOWAIT); 82952c9ce25SScott Long if (path->device->serial_num != NULL) { 83052c9ce25SScott Long bcopy(ident_buf->serial, 83152c9ce25SScott Long path->device->serial_num, 83252c9ce25SScott Long sizeof(ident_buf->serial)); 83352c9ce25SScott Long path->device->serial_num[sizeof(ident_buf->serial)] 83452c9ce25SScott Long = '\0'; 83552c9ce25SScott Long path->device->serial_num_len = 83652c9ce25SScott Long strlen(path->device->serial_num); 83752c9ce25SScott Long } 83852c9ce25SScott Long 8394b997c49SAlexander Motin path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 8401e637ba6SAlexander Motin } 84130a4094fSAlexander Motin if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) { 84230a4094fSAlexander Motin path->device->mintags = path->device->maxtags = 84330a4094fSAlexander Motin ATA_QUEUE_LEN(ident_buf->queue) + 1; 84430a4094fSAlexander Motin } 84530a4094fSAlexander Motin ata_find_quirk(path->device); 846507e5811SAlexander Motin if (path->device->mintags != 0 && 847507e5811SAlexander Motin path->bus->sim->max_tagged_dev_openings != 0) { 848699f853bSAlexander Motin /* Check if the SIM does not want queued commands. */ 849699f853bSAlexander Motin bzero(&cpi, sizeof(cpi)); 850699f853bSAlexander Motin xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 851699f853bSAlexander Motin cpi.ccb_h.func_code = XPT_PATH_INQ; 852699f853bSAlexander Motin xpt_action((union ccb *)&cpi); 853699f853bSAlexander Motin if (cpi.ccb_h.status == CAM_REQ_CMP && 854699f853bSAlexander Motin (cpi.hba_inquiry & PI_TAG_ABLE)) { 855c8039fc6SAlexander Motin /* Report SIM which tags are allowed. */ 856c8039fc6SAlexander Motin bzero(&cts, sizeof(cts)); 85783c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 858c8039fc6SAlexander Motin cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 859c8039fc6SAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 860c8039fc6SAlexander Motin cts.xport_specific.sata.tags = path->device->maxtags; 861c8039fc6SAlexander Motin cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS; 862c8039fc6SAlexander Motin xpt_action((union ccb *)&cts); 863c8039fc6SAlexander Motin /* Reconfigure queues for tagged queueing. */ 86430a4094fSAlexander Motin xpt_start_tags(path); 86530a4094fSAlexander Motin } 866699f853bSAlexander Motin } 86752c9ce25SScott Long ata_device_transport(path); 86852c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_SETMODE); 86952c9ce25SScott Long xpt_release_ccb(done_ccb); 87052c9ce25SScott Long xpt_schedule(periph, priority); 87152c9ce25SScott Long return; 87252c9ce25SScott Long } 8734ef08dc5SAlexander Motin case PROBE_SPINUP: 8744ef08dc5SAlexander Motin if (bootverbose) 8754ef08dc5SAlexander Motin xpt_print(path, "Spin-up done\n"); 8764ef08dc5SAlexander Motin softc->spinup = 1; 8774ef08dc5SAlexander Motin PROBE_SET_ACTION(softc, PROBE_IDENTIFY); 8784ef08dc5SAlexander Motin xpt_release_ccb(done_ccb); 8794ef08dc5SAlexander Motin xpt_schedule(periph, priority); 8804ef08dc5SAlexander Motin return; 88152c9ce25SScott Long case PROBE_SETMODE: 8821e637ba6SAlexander Motin if (path->device->protocol == PROTO_ATA) { 8831e637ba6SAlexander Motin PROBE_SET_ACTION(softc, PROBE_SET_MULTI); 8841e637ba6SAlexander Motin } else { 8851e637ba6SAlexander Motin PROBE_SET_ACTION(softc, PROBE_INQUIRY); 8861e637ba6SAlexander Motin } 8871e637ba6SAlexander Motin xpt_release_ccb(done_ccb); 8881e637ba6SAlexander Motin xpt_schedule(periph, priority); 8891e637ba6SAlexander Motin return; 8901e637ba6SAlexander Motin case PROBE_SET_MULTI: 8911e637ba6SAlexander Motin if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 89252c9ce25SScott Long path->device->flags &= ~CAM_DEV_UNCONFIGURED; 893f98d7a47SAlexander Motin xpt_acquire_device(path->device); 89452c9ce25SScott Long done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 89552c9ce25SScott Long xpt_action(done_ccb); 89652c9ce25SScott Long xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 89752c9ce25SScott Long done_ccb); 8981e637ba6SAlexander Motin } 89952c9ce25SScott Long break; 90052c9ce25SScott Long case PROBE_INQUIRY: 90152c9ce25SScott Long case PROBE_FULL_INQUIRY: 90252c9ce25SScott Long { 90352c9ce25SScott Long struct scsi_inquiry_data *inq_buf; 9041e637ba6SAlexander Motin u_int8_t periph_qual, len; 90552c9ce25SScott Long 90652c9ce25SScott Long path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID; 90752c9ce25SScott Long inq_buf = &path->device->inq_data; 90852c9ce25SScott Long 90952c9ce25SScott Long periph_qual = SID_QUAL(inq_buf); 91052c9ce25SScott Long 9111e637ba6SAlexander Motin if (periph_qual != SID_QUAL_LU_CONNECTED) 9121e637ba6SAlexander Motin break; 91352c9ce25SScott Long 91452c9ce25SScott Long /* 91552c9ce25SScott Long * We conservatively request only 91652c9ce25SScott Long * SHORT_INQUIRY_LEN bytes of inquiry 91752c9ce25SScott Long * information during our first try 91852c9ce25SScott Long * at sending an INQUIRY. If the device 91952c9ce25SScott Long * has more information to give, 92052c9ce25SScott Long * perform a second request specifying 92152c9ce25SScott Long * the amount of information the device 92252c9ce25SScott Long * is willing to give. 92352c9ce25SScott Long */ 92452c9ce25SScott Long len = inq_buf->additional_length 9251e637ba6SAlexander Motin + offsetof(struct scsi_inquiry_data, additional_length) + 1; 92652c9ce25SScott Long if (softc->action == PROBE_INQUIRY 92752c9ce25SScott Long && len > SHORT_INQUIRY_LENGTH) { 92852c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY); 92952c9ce25SScott Long xpt_release_ccb(done_ccb); 93052c9ce25SScott Long xpt_schedule(periph, priority); 93152c9ce25SScott Long return; 93252c9ce25SScott Long } 93352c9ce25SScott Long 9344b997c49SAlexander Motin ata_device_transport(path); 9351e637ba6SAlexander Motin if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 93652c9ce25SScott Long path->device->flags &= ~CAM_DEV_UNCONFIGURED; 937f98d7a47SAlexander Motin xpt_acquire_device(path->device); 93852c9ce25SScott Long done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 93952c9ce25SScott Long xpt_action(done_ccb); 9401e637ba6SAlexander Motin xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb); 9411e637ba6SAlexander Motin } 94252c9ce25SScott Long break; 94352c9ce25SScott Long } 94452c9ce25SScott Long case PROBE_PM_PID: 9454b997c49SAlexander Motin if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0) 94652c9ce25SScott Long bzero(ident_buf, sizeof(*ident_buf)); 94752c9ce25SScott Long softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) + 94852c9ce25SScott Long (done_ccb->ataio.res.lba_mid << 16) + 94952c9ce25SScott Long (done_ccb->ataio.res.lba_low << 8) + 95052c9ce25SScott Long done_ccb->ataio.res.sector_count; 95165d0fb03SAlexander Motin ((uint32_t *)ident_buf)[0] = softc->pm_pid; 95252c9ce25SScott Long snprintf(ident_buf->model, sizeof(ident_buf->model), 95352c9ce25SScott Long "Port Multiplier %08x", softc->pm_pid); 95452c9ce25SScott Long PROBE_SET_ACTION(softc, PROBE_PM_PRV); 95552c9ce25SScott Long xpt_release_ccb(done_ccb); 95652c9ce25SScott Long xpt_schedule(periph, priority); 95752c9ce25SScott Long return; 95852c9ce25SScott Long case PROBE_PM_PRV: 95952c9ce25SScott Long softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) + 96052c9ce25SScott Long (done_ccb->ataio.res.lba_mid << 16) + 96152c9ce25SScott Long (done_ccb->ataio.res.lba_low << 8) + 96252c9ce25SScott Long done_ccb->ataio.res.sector_count; 96365d0fb03SAlexander Motin ((uint32_t *)ident_buf)[1] = softc->pm_prv; 96452c9ce25SScott Long snprintf(ident_buf->revision, sizeof(ident_buf->revision), 96552c9ce25SScott Long "%04x", softc->pm_prv); 9664b997c49SAlexander Motin path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID; 96765d0fb03SAlexander Motin if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) { 96852c9ce25SScott Long path->device->flags &= ~CAM_DEV_UNCONFIGURED; 969f98d7a47SAlexander Motin xpt_acquire_device(path->device); 97052c9ce25SScott Long done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 97152c9ce25SScott Long xpt_action(done_ccb); 97252c9ce25SScott Long xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, 97352c9ce25SScott Long done_ccb); 97465d0fb03SAlexander Motin } else { 97565d0fb03SAlexander Motin done_ccb->ccb_h.func_code = XPT_GDEV_TYPE; 97665d0fb03SAlexander Motin xpt_action(done_ccb); 9771e637ba6SAlexander Motin xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb); 97852c9ce25SScott Long } 97952c9ce25SScott Long break; 98052c9ce25SScott Long case PROBE_INVALID: 98152c9ce25SScott Long CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO, 98252c9ce25SScott Long ("probedone: invalid action state\n")); 98352c9ce25SScott Long default: 98452c9ce25SScott Long break; 98552c9ce25SScott Long } 9861e637ba6SAlexander Motin done: 98783c5d981SAlexander Motin if (softc->restart) { 98883c5d981SAlexander Motin softc->restart = 0; 9891e637ba6SAlexander Motin xpt_release_ccb(done_ccb); 99083c5d981SAlexander Motin probeschedule(periph); 99183c5d981SAlexander Motin return; 99283c5d981SAlexander Motin } 99383c5d981SAlexander Motin xpt_release_ccb(done_ccb); 99483c5d981SAlexander Motin while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) { 99583c5d981SAlexander Motin TAILQ_REMOVE(&softc->request_ccbs, 99683c5d981SAlexander Motin &done_ccb->ccb_h, periph_links.tqe); 99783c5d981SAlexander Motin done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR; 99852c9ce25SScott Long xpt_done(done_ccb); 99983c5d981SAlexander Motin } 100083c5d981SAlexander Motin cam_release_devq(periph->path, 100183c5d981SAlexander Motin RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE); 100252c9ce25SScott Long cam_periph_invalidate(periph); 100352c9ce25SScott Long cam_periph_release_locked(periph); 100452c9ce25SScott Long } 100552c9ce25SScott Long 100652c9ce25SScott Long static void 100752c9ce25SScott Long probecleanup(struct cam_periph *periph) 100852c9ce25SScott Long { 100952c9ce25SScott Long free(periph->softc, M_CAMXPT); 101052c9ce25SScott Long } 101152c9ce25SScott Long 101252c9ce25SScott Long static void 101330a4094fSAlexander Motin ata_find_quirk(struct cam_ed *device) 101452c9ce25SScott Long { 101530a4094fSAlexander Motin struct ata_quirk_entry *quirk; 101652c9ce25SScott Long caddr_t match; 101752c9ce25SScott Long 101830a4094fSAlexander Motin match = cam_quirkmatch((caddr_t)&device->ident_data, 101930a4094fSAlexander Motin (caddr_t)ata_quirk_table, 102030a4094fSAlexander Motin ata_quirk_table_size, 102130a4094fSAlexander Motin sizeof(*ata_quirk_table), ata_identify_match); 102252c9ce25SScott Long 102352c9ce25SScott Long if (match == NULL) 102452c9ce25SScott Long panic("xpt_find_quirk: device didn't match wildcard entry!!"); 102552c9ce25SScott Long 102630a4094fSAlexander Motin quirk = (struct ata_quirk_entry *)match; 102752c9ce25SScott Long device->quirk = quirk; 102830a4094fSAlexander Motin if (quirk->quirks & CAM_QUIRK_MAXTAGS) 102930a4094fSAlexander Motin device->mintags = device->maxtags = quirk->maxtags; 103052c9ce25SScott Long } 103152c9ce25SScott Long 103252c9ce25SScott Long typedef struct { 103352c9ce25SScott Long union ccb *request_ccb; 103452c9ce25SScott Long struct ccb_pathinq *cpi; 103552c9ce25SScott Long int counter; 103652c9ce25SScott Long } ata_scan_bus_info; 103752c9ce25SScott Long 103852c9ce25SScott Long /* 103952c9ce25SScott Long * To start a scan, request_ccb is an XPT_SCAN_BUS ccb. 104052c9ce25SScott Long * As the scan progresses, xpt_scan_bus is used as the 104152c9ce25SScott Long * callback on completion function. 104252c9ce25SScott Long */ 104352c9ce25SScott Long static void 104452c9ce25SScott Long ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb) 104552c9ce25SScott Long { 104652c9ce25SScott Long struct cam_path *path; 104752c9ce25SScott Long ata_scan_bus_info *scan_info; 104883c5d981SAlexander Motin union ccb *work_ccb, *reset_ccb; 104952c9ce25SScott Long cam_status status; 105052c9ce25SScott Long 105152c9ce25SScott Long CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE, 105252c9ce25SScott Long ("xpt_scan_bus\n")); 105352c9ce25SScott Long switch (request_ccb->ccb_h.func_code) { 105452c9ce25SScott Long case XPT_SCAN_BUS: 105552c9ce25SScott Long /* Find out the characteristics of the bus */ 105652c9ce25SScott Long work_ccb = xpt_alloc_ccb_nowait(); 105752c9ce25SScott Long if (work_ccb == NULL) { 105852c9ce25SScott Long request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 105952c9ce25SScott Long xpt_done(request_ccb); 106052c9ce25SScott Long return; 106152c9ce25SScott Long } 106252c9ce25SScott Long xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path, 106352c9ce25SScott Long request_ccb->ccb_h.pinfo.priority); 106452c9ce25SScott Long work_ccb->ccb_h.func_code = XPT_PATH_INQ; 106552c9ce25SScott Long xpt_action(work_ccb); 106652c9ce25SScott Long if (work_ccb->ccb_h.status != CAM_REQ_CMP) { 106752c9ce25SScott Long request_ccb->ccb_h.status = work_ccb->ccb_h.status; 106852c9ce25SScott Long xpt_free_ccb(work_ccb); 106952c9ce25SScott Long xpt_done(request_ccb); 107052c9ce25SScott Long return; 107152c9ce25SScott Long } 107252c9ce25SScott Long 107383c5d981SAlexander Motin /* We may need to reset bus first, if we haven't done it yet. */ 107483c5d981SAlexander Motin if ((work_ccb->cpi.hba_inquiry & 107583c5d981SAlexander Motin (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) && 107683c5d981SAlexander Motin !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) && 107783c5d981SAlexander Motin !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) { 107883c5d981SAlexander Motin reset_ccb = xpt_alloc_ccb_nowait(); 107983c5d981SAlexander Motin xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path, 108083c5d981SAlexander Motin CAM_PRIORITY_NONE); 108183c5d981SAlexander Motin reset_ccb->ccb_h.func_code = XPT_RESET_BUS; 108283c5d981SAlexander Motin xpt_action(reset_ccb); 108383c5d981SAlexander Motin if (reset_ccb->ccb_h.status != CAM_REQ_CMP) { 108483c5d981SAlexander Motin request_ccb->ccb_h.status = reset_ccb->ccb_h.status; 108583c5d981SAlexander Motin xpt_free_ccb(reset_ccb); 108683c5d981SAlexander Motin xpt_free_ccb(work_ccb); 108783c5d981SAlexander Motin xpt_done(request_ccb); 108883c5d981SAlexander Motin return; 108983c5d981SAlexander Motin } 109083c5d981SAlexander Motin xpt_free_ccb(reset_ccb); 109183c5d981SAlexander Motin } 109283c5d981SAlexander Motin 109352c9ce25SScott Long /* Save some state for use while we probe for devices */ 109452c9ce25SScott Long scan_info = (ata_scan_bus_info *) 109552c9ce25SScott Long malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT); 109652c9ce25SScott Long if (scan_info == NULL) { 109752c9ce25SScott Long request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 109852c9ce25SScott Long xpt_done(request_ccb); 109952c9ce25SScott Long return; 110052c9ce25SScott Long } 110152c9ce25SScott Long scan_info->request_ccb = request_ccb; 110252c9ce25SScott Long scan_info->cpi = &work_ccb->cpi; 110352c9ce25SScott Long /* If PM supported, probe it first. */ 110452c9ce25SScott Long if (scan_info->cpi->hba_inquiry & PI_SATAPM) 11050025eb12SAlexander Motin scan_info->counter = scan_info->cpi->max_target; 11060025eb12SAlexander Motin else 11070025eb12SAlexander Motin scan_info->counter = 0; 110852c9ce25SScott Long 110952c9ce25SScott Long work_ccb = xpt_alloc_ccb_nowait(); 111052c9ce25SScott Long if (work_ccb == NULL) { 111152c9ce25SScott Long free(scan_info, M_CAMXPT); 111252c9ce25SScott Long request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 111352c9ce25SScott Long xpt_done(request_ccb); 111452c9ce25SScott Long break; 111552c9ce25SScott Long } 111652c9ce25SScott Long goto scan_next; 111752c9ce25SScott Long case XPT_SCAN_LUN: 111852c9ce25SScott Long work_ccb = request_ccb; 111952c9ce25SScott Long /* Reuse the same CCB to query if a device was really found */ 112052c9ce25SScott Long scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0; 112165d0fb03SAlexander Motin /* If there is PMP... */ 11220025eb12SAlexander Motin if ((scan_info->cpi->hba_inquiry & PI_SATAPM) && 11230025eb12SAlexander Motin (scan_info->counter == scan_info->cpi->max_target)) { 112483c5d981SAlexander Motin if (work_ccb->ccb_h.status == CAM_REQ_CMP) { 112565d0fb03SAlexander Motin /* everything else will be probed by it */ 1126e2a75189SAlexander Motin /* Free the current request path- we're done with it. */ 1127e2a75189SAlexander Motin xpt_free_path(work_ccb->ccb_h.path); 11280025eb12SAlexander Motin goto done; 112952c9ce25SScott Long } else { 113052c9ce25SScott Long struct ccb_trans_settings cts; 113152c9ce25SScott Long 113252c9ce25SScott Long /* Report SIM that PM is absent. */ 113352c9ce25SScott Long bzero(&cts, sizeof(cts)); 113452c9ce25SScott Long xpt_setup_ccb(&cts.ccb_h, 1135e2a75189SAlexander Motin work_ccb->ccb_h.path, CAM_PRIORITY_NONE); 113652c9ce25SScott Long cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 113752c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 11383ccda2f3SAlexander Motin cts.xport_specific.sata.pm_present = 0; 113952c9ce25SScott Long cts.xport_specific.sata.valid = CTS_SATA_VALID_PM; 114052c9ce25SScott Long xpt_action((union ccb *)&cts); 114152c9ce25SScott Long } 114252c9ce25SScott Long } 1143e2a75189SAlexander Motin /* Free the current request path- we're done with it. */ 1144e2a75189SAlexander Motin xpt_free_path(work_ccb->ccb_h.path); 11450025eb12SAlexander Motin if (scan_info->counter == 11460025eb12SAlexander Motin ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 11470025eb12SAlexander Motin 0 : scan_info->cpi->max_target)) { 11480025eb12SAlexander Motin done: 114952c9ce25SScott Long xpt_free_ccb(work_ccb); 115052c9ce25SScott Long xpt_free_ccb((union ccb *)scan_info->cpi); 115152c9ce25SScott Long request_ccb = scan_info->request_ccb; 115252c9ce25SScott Long free(scan_info, M_CAMXPT); 115352c9ce25SScott Long request_ccb->ccb_h.status = CAM_REQ_CMP; 115452c9ce25SScott Long xpt_done(request_ccb); 115552c9ce25SScott Long break; 115652c9ce25SScott Long } 11570025eb12SAlexander Motin /* Take next device. Wrap from max (PMP) to 0. */ 11580025eb12SAlexander Motin scan_info->counter = (scan_info->counter + 1 ) % 11590025eb12SAlexander Motin (scan_info->cpi->max_target + 1); 116052c9ce25SScott Long scan_next: 116152c9ce25SScott Long status = xpt_create_path(&path, xpt_periph, 116252c9ce25SScott Long scan_info->request_ccb->ccb_h.path_id, 116352c9ce25SScott Long scan_info->counter, 0); 116452c9ce25SScott Long if (status != CAM_REQ_CMP) { 116552c9ce25SScott Long printf("xpt_scan_bus: xpt_create_path failed" 116652c9ce25SScott Long " with status %#x, bus scan halted\n", 116752c9ce25SScott Long status); 116852c9ce25SScott Long xpt_free_ccb(work_ccb); 116952c9ce25SScott Long xpt_free_ccb((union ccb *)scan_info->cpi); 117052c9ce25SScott Long request_ccb = scan_info->request_ccb; 117152c9ce25SScott Long free(scan_info, M_CAMXPT); 117252c9ce25SScott Long request_ccb->ccb_h.status = status; 117352c9ce25SScott Long xpt_done(request_ccb); 117452c9ce25SScott Long break; 117552c9ce25SScott Long } 117652c9ce25SScott Long xpt_setup_ccb(&work_ccb->ccb_h, path, 117752c9ce25SScott Long scan_info->request_ccb->ccb_h.pinfo.priority); 117852c9ce25SScott Long work_ccb->ccb_h.func_code = XPT_SCAN_LUN; 117952c9ce25SScott Long work_ccb->ccb_h.cbfcnp = ata_scan_bus; 118052c9ce25SScott Long work_ccb->ccb_h.ppriv_ptr0 = scan_info; 118152c9ce25SScott Long work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags; 118252c9ce25SScott Long xpt_action(work_ccb); 118352c9ce25SScott Long break; 118452c9ce25SScott Long default: 118552c9ce25SScott Long break; 118652c9ce25SScott Long } 118752c9ce25SScott Long } 118852c9ce25SScott Long 118952c9ce25SScott Long static void 119052c9ce25SScott Long ata_scan_lun(struct cam_periph *periph, struct cam_path *path, 119152c9ce25SScott Long cam_flags flags, union ccb *request_ccb) 119252c9ce25SScott Long { 119352c9ce25SScott Long struct ccb_pathinq cpi; 119452c9ce25SScott Long cam_status status; 119552c9ce25SScott Long struct cam_path *new_path; 119652c9ce25SScott Long struct cam_periph *old_periph; 119752c9ce25SScott Long 119883c5d981SAlexander Motin CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n")); 119952c9ce25SScott Long 120083c5d981SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 120152c9ce25SScott Long cpi.ccb_h.func_code = XPT_PATH_INQ; 120252c9ce25SScott Long xpt_action((union ccb *)&cpi); 120352c9ce25SScott Long 120452c9ce25SScott Long if (cpi.ccb_h.status != CAM_REQ_CMP) { 120552c9ce25SScott Long if (request_ccb != NULL) { 120652c9ce25SScott Long request_ccb->ccb_h.status = cpi.ccb_h.status; 120752c9ce25SScott Long xpt_done(request_ccb); 120852c9ce25SScott Long } 120952c9ce25SScott Long return; 121052c9ce25SScott Long } 121152c9ce25SScott Long 121252c9ce25SScott Long if (request_ccb == NULL) { 121352c9ce25SScott Long request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT); 121452c9ce25SScott Long if (request_ccb == NULL) { 121552c9ce25SScott Long xpt_print(path, "xpt_scan_lun: can't allocate CCB, " 121652c9ce25SScott Long "can't continue\n"); 121752c9ce25SScott Long return; 121852c9ce25SScott Long } 121952c9ce25SScott Long new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT); 122052c9ce25SScott Long if (new_path == NULL) { 122152c9ce25SScott Long xpt_print(path, "xpt_scan_lun: can't allocate path, " 122252c9ce25SScott Long "can't continue\n"); 122352c9ce25SScott Long free(request_ccb, M_CAMXPT); 122452c9ce25SScott Long return; 122552c9ce25SScott Long } 122652c9ce25SScott Long status = xpt_compile_path(new_path, xpt_periph, 122752c9ce25SScott Long path->bus->path_id, 122852c9ce25SScott Long path->target->target_id, 122952c9ce25SScott Long path->device->lun_id); 123052c9ce25SScott Long 123152c9ce25SScott Long if (status != CAM_REQ_CMP) { 123252c9ce25SScott Long xpt_print(path, "xpt_scan_lun: can't compile path, " 123352c9ce25SScott Long "can't continue\n"); 123452c9ce25SScott Long free(request_ccb, M_CAMXPT); 123552c9ce25SScott Long free(new_path, M_CAMXPT); 123652c9ce25SScott Long return; 123752c9ce25SScott Long } 123883c5d981SAlexander Motin xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT); 123952c9ce25SScott Long request_ccb->ccb_h.cbfcnp = xptscandone; 124052c9ce25SScott Long request_ccb->ccb_h.func_code = XPT_SCAN_LUN; 124152c9ce25SScott Long request_ccb->crcn.flags = flags; 124252c9ce25SScott Long } 124352c9ce25SScott Long 1244f09f8e3eSAlexander Motin if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) { 124552c9ce25SScott Long probe_softc *softc; 124652c9ce25SScott Long 124752c9ce25SScott Long softc = (probe_softc *)old_periph->softc; 124852c9ce25SScott Long TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h, 124952c9ce25SScott Long periph_links.tqe); 125083c5d981SAlexander Motin softc->restart = 1; 125152c9ce25SScott Long } else { 125252c9ce25SScott Long status = cam_periph_alloc(proberegister, NULL, probecleanup, 1253f09f8e3eSAlexander Motin probestart, "aprobe", 125452c9ce25SScott Long CAM_PERIPH_BIO, 125552c9ce25SScott Long request_ccb->ccb_h.path, NULL, 0, 125652c9ce25SScott Long request_ccb); 125752c9ce25SScott Long 125852c9ce25SScott Long if (status != CAM_REQ_CMP) { 125952c9ce25SScott Long xpt_print(path, "xpt_scan_lun: cam_alloc_periph " 126052c9ce25SScott Long "returned an error, can't continue probe\n"); 126152c9ce25SScott Long request_ccb->ccb_h.status = status; 126252c9ce25SScott Long xpt_done(request_ccb); 126352c9ce25SScott Long } 126452c9ce25SScott Long } 126552c9ce25SScott Long } 126652c9ce25SScott Long 126752c9ce25SScott Long static void 126852c9ce25SScott Long xptscandone(struct cam_periph *periph, union ccb *done_ccb) 126952c9ce25SScott Long { 127052c9ce25SScott Long xpt_release_path(done_ccb->ccb_h.path); 127152c9ce25SScott Long free(done_ccb->ccb_h.path, M_CAMXPT); 127252c9ce25SScott Long free(done_ccb, M_CAMXPT); 127352c9ce25SScott Long } 127452c9ce25SScott Long 127552c9ce25SScott Long static struct cam_ed * 127652c9ce25SScott Long ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 127752c9ce25SScott Long { 127852c9ce25SScott Long struct cam_path path; 127930a4094fSAlexander Motin struct ata_quirk_entry *quirk; 128052c9ce25SScott Long struct cam_ed *device; 128152c9ce25SScott Long struct cam_ed *cur_device; 128252c9ce25SScott Long 128352c9ce25SScott Long device = xpt_alloc_device(bus, target, lun_id); 128452c9ce25SScott Long if (device == NULL) 128552c9ce25SScott Long return (NULL); 128652c9ce25SScott Long 128752c9ce25SScott Long /* 128852c9ce25SScott Long * Take the default quirk entry until we have inquiry 128952c9ce25SScott Long * data and can determine a better quirk to use. 129052c9ce25SScott Long */ 129130a4094fSAlexander Motin quirk = &ata_quirk_table[ata_quirk_table_size - 1]; 129252c9ce25SScott Long device->quirk = (void *)quirk; 129330a4094fSAlexander Motin device->mintags = 0; 129430a4094fSAlexander Motin device->maxtags = 0; 129552c9ce25SScott Long bzero(&device->inq_data, sizeof(device->inq_data)); 129652c9ce25SScott Long device->inq_flags = 0; 129752c9ce25SScott Long device->queue_flags = 0; 129852c9ce25SScott Long device->serial_num = NULL; 129952c9ce25SScott Long device->serial_num_len = 0; 130052c9ce25SScott Long 130152c9ce25SScott Long /* 130252c9ce25SScott Long * XXX should be limited by number of CCBs this bus can 130352c9ce25SScott Long * do. 130452c9ce25SScott Long */ 130552c9ce25SScott Long bus->sim->max_ccbs += device->ccbq.devq_openings; 130652c9ce25SScott Long /* Insertion sort into our target's device list */ 130752c9ce25SScott Long cur_device = TAILQ_FIRST(&target->ed_entries); 130852c9ce25SScott Long while (cur_device != NULL && cur_device->lun_id < lun_id) 130952c9ce25SScott Long cur_device = TAILQ_NEXT(cur_device, links); 131052c9ce25SScott Long if (cur_device != NULL) { 131152c9ce25SScott Long TAILQ_INSERT_BEFORE(cur_device, device, links); 131252c9ce25SScott Long } else { 131352c9ce25SScott Long TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 131452c9ce25SScott Long } 131552c9ce25SScott Long target->generation++; 131652c9ce25SScott Long if (lun_id != CAM_LUN_WILDCARD) { 131752c9ce25SScott Long xpt_compile_path(&path, 131852c9ce25SScott Long NULL, 131952c9ce25SScott Long bus->path_id, 132052c9ce25SScott Long target->target_id, 132152c9ce25SScott Long lun_id); 132252c9ce25SScott Long ata_device_transport(&path); 132352c9ce25SScott Long xpt_release_path(&path); 132452c9ce25SScott Long } 132552c9ce25SScott Long 132652c9ce25SScott Long return (device); 132752c9ce25SScott Long } 132852c9ce25SScott Long 132952c9ce25SScott Long static void 133052c9ce25SScott Long ata_device_transport(struct cam_path *path) 133152c9ce25SScott Long { 133252c9ce25SScott Long struct ccb_pathinq cpi; 13334b997c49SAlexander Motin struct ccb_trans_settings cts; 13344b997c49SAlexander Motin struct scsi_inquiry_data *inq_buf = NULL; 13354b997c49SAlexander Motin struct ata_params *ident_buf = NULL; 133652c9ce25SScott Long 133752c9ce25SScott Long /* Get transport information from the SIM */ 133883c5d981SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE); 133952c9ce25SScott Long cpi.ccb_h.func_code = XPT_PATH_INQ; 134052c9ce25SScott Long xpt_action((union ccb *)&cpi); 134152c9ce25SScott Long 134252c9ce25SScott Long path->device->transport = cpi.transport; 13434b997c49SAlexander Motin if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) 13444b997c49SAlexander Motin inq_buf = &path->device->inq_data; 13454b997c49SAlexander Motin if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0) 13464b997c49SAlexander Motin ident_buf = &path->device->ident_data; 13474b997c49SAlexander Motin if (path->device->protocol == PROTO_ATA) { 13484b997c49SAlexander Motin path->device->protocol_version = ident_buf ? 13494b997c49SAlexander Motin ata_version(ident_buf->version_major) : cpi.protocol_version; 13504b997c49SAlexander Motin } else if (path->device->protocol == PROTO_SCSI) { 13514b997c49SAlexander Motin path->device->protocol_version = inq_buf ? 13524b997c49SAlexander Motin SID_ANSI_REV(inq_buf) : cpi.protocol_version; 135352c9ce25SScott Long } 13544b997c49SAlexander Motin path->device->transport_version = ident_buf ? 13554b997c49SAlexander Motin ata_version(ident_buf->version_major) : cpi.transport_version; 135652c9ce25SScott Long 135752c9ce25SScott Long /* Tell the controller what we think */ 135883c5d981SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE); 135952c9ce25SScott Long cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 136052c9ce25SScott Long cts.type = CTS_TYPE_CURRENT_SETTINGS; 136152c9ce25SScott Long cts.transport = path->device->transport; 136252c9ce25SScott Long cts.transport_version = path->device->transport_version; 136352c9ce25SScott Long cts.protocol = path->device->protocol; 136452c9ce25SScott Long cts.protocol_version = path->device->protocol_version; 136552c9ce25SScott Long cts.proto_specific.valid = 0; 13664cca1530SAlexander Motin if (ident_buf) { 13674cca1530SAlexander Motin if (path->device->transport == XPORT_ATA) { 13684cca1530SAlexander Motin cts.xport_specific.ata.atapi = 13694cca1530SAlexander Motin ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 13704cca1530SAlexander Motin ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 13714cca1530SAlexander Motin cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI; 13724cca1530SAlexander Motin } else { 13734cca1530SAlexander Motin cts.xport_specific.sata.atapi = 13744cca1530SAlexander Motin ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 : 13754cca1530SAlexander Motin ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0; 13764cca1530SAlexander Motin cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI; 13774cca1530SAlexander Motin } 13784cca1530SAlexander Motin } else 137952c9ce25SScott Long cts.xport_specific.valid = 0; 138052c9ce25SScott Long xpt_action((union ccb *)&cts); 138152c9ce25SScott Long } 138252c9ce25SScott Long 138352c9ce25SScott Long static void 138452c9ce25SScott Long ata_action(union ccb *start_ccb) 138552c9ce25SScott Long { 138652c9ce25SScott Long 138752c9ce25SScott Long switch (start_ccb->ccb_h.func_code) { 138852c9ce25SScott Long case XPT_SET_TRAN_SETTINGS: 138952c9ce25SScott Long { 139030a4094fSAlexander Motin ata_set_transfer_settings(&start_ccb->cts, 139152c9ce25SScott Long start_ccb->ccb_h.path->device, 139252c9ce25SScott Long /*async_update*/FALSE); 139352c9ce25SScott Long break; 139452c9ce25SScott Long } 139552c9ce25SScott Long case XPT_SCAN_BUS: 139652c9ce25SScott Long ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb); 139752c9ce25SScott Long break; 139852c9ce25SScott Long case XPT_SCAN_LUN: 139952c9ce25SScott Long ata_scan_lun(start_ccb->ccb_h.path->periph, 140052c9ce25SScott Long start_ccb->ccb_h.path, start_ccb->crcn.flags, 140152c9ce25SScott Long start_ccb); 140252c9ce25SScott Long break; 140352c9ce25SScott Long case XPT_GET_TRAN_SETTINGS: 140452c9ce25SScott Long { 140552c9ce25SScott Long struct cam_sim *sim; 140652c9ce25SScott Long 140752c9ce25SScott Long sim = start_ccb->ccb_h.path->bus->sim; 140852c9ce25SScott Long (*(sim->sim_action))(sim, start_ccb); 140952c9ce25SScott Long break; 141052c9ce25SScott Long } 14114cca1530SAlexander Motin case XPT_SCSI_IO: 14124cca1530SAlexander Motin { 14134cca1530SAlexander Motin struct cam_ed *device; 14144cca1530SAlexander Motin u_int maxlen = 0; 14154cca1530SAlexander Motin 14164cca1530SAlexander Motin device = start_ccb->ccb_h.path->device; 14174cca1530SAlexander Motin if (device->protocol == PROTO_SCSI && 14184cca1530SAlexander Motin (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) { 14194cca1530SAlexander Motin uint16_t p = 14204cca1530SAlexander Motin device->ident_data.config & ATA_PROTO_MASK; 14214cca1530SAlexander Motin 14224cca1530SAlexander Motin maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 : 14234cca1530SAlexander Motin (p == ATA_PROTO_ATAPI_12) ? 12 : 0; 14244cca1530SAlexander Motin } 14254cca1530SAlexander Motin if (start_ccb->csio.cdb_len > maxlen) { 14264cca1530SAlexander Motin start_ccb->ccb_h.status = CAM_REQ_INVALID; 14274cca1530SAlexander Motin xpt_done(start_ccb); 14284cca1530SAlexander Motin break; 14294cca1530SAlexander Motin } 14304cca1530SAlexander Motin /* FALLTHROUGH */ 14314cca1530SAlexander Motin } 143252c9ce25SScott Long default: 143352c9ce25SScott Long xpt_action_default(start_ccb); 143452c9ce25SScott Long break; 143552c9ce25SScott Long } 143652c9ce25SScott Long } 143752c9ce25SScott Long 143852c9ce25SScott Long static void 143930a4094fSAlexander Motin ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device, 144052c9ce25SScott Long int async_update) 144152c9ce25SScott Long { 144252c9ce25SScott Long struct ccb_pathinq cpi; 144352c9ce25SScott Long struct ccb_trans_settings cur_cts; 144452c9ce25SScott Long struct ccb_trans_settings_scsi *scsi; 144552c9ce25SScott Long struct ccb_trans_settings_scsi *cur_scsi; 144652c9ce25SScott Long struct cam_sim *sim; 144752c9ce25SScott Long struct scsi_inquiry_data *inq_data; 144852c9ce25SScott Long 144952c9ce25SScott Long if (device == NULL) { 145052c9ce25SScott Long cts->ccb_h.status = CAM_PATH_INVALID; 145152c9ce25SScott Long xpt_done((union ccb *)cts); 145252c9ce25SScott Long return; 145352c9ce25SScott Long } 145452c9ce25SScott Long 145552c9ce25SScott Long if (cts->protocol == PROTO_UNKNOWN 145652c9ce25SScott Long || cts->protocol == PROTO_UNSPECIFIED) { 145752c9ce25SScott Long cts->protocol = device->protocol; 145852c9ce25SScott Long cts->protocol_version = device->protocol_version; 145952c9ce25SScott Long } 146052c9ce25SScott Long 146152c9ce25SScott Long if (cts->protocol_version == PROTO_VERSION_UNKNOWN 146252c9ce25SScott Long || cts->protocol_version == PROTO_VERSION_UNSPECIFIED) 146352c9ce25SScott Long cts->protocol_version = device->protocol_version; 146452c9ce25SScott Long 146552c9ce25SScott Long if (cts->protocol != device->protocol) { 146652c9ce25SScott Long xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n", 146752c9ce25SScott Long cts->protocol, device->protocol); 146852c9ce25SScott Long cts->protocol = device->protocol; 146952c9ce25SScott Long } 147052c9ce25SScott Long 147152c9ce25SScott Long if (cts->protocol_version > device->protocol_version) { 147252c9ce25SScott Long if (bootverbose) { 147352c9ce25SScott Long xpt_print(cts->ccb_h.path, "Down reving Protocol " 147452c9ce25SScott Long "Version from %d to %d?\n", cts->protocol_version, 147552c9ce25SScott Long device->protocol_version); 147652c9ce25SScott Long } 147752c9ce25SScott Long cts->protocol_version = device->protocol_version; 147852c9ce25SScott Long } 147952c9ce25SScott Long 148052c9ce25SScott Long if (cts->transport == XPORT_UNKNOWN 148152c9ce25SScott Long || cts->transport == XPORT_UNSPECIFIED) { 148252c9ce25SScott Long cts->transport = device->transport; 148352c9ce25SScott Long cts->transport_version = device->transport_version; 148452c9ce25SScott Long } 148552c9ce25SScott Long 148652c9ce25SScott Long if (cts->transport_version == XPORT_VERSION_UNKNOWN 148752c9ce25SScott Long || cts->transport_version == XPORT_VERSION_UNSPECIFIED) 148852c9ce25SScott Long cts->transport_version = device->transport_version; 148952c9ce25SScott Long 149052c9ce25SScott Long if (cts->transport != device->transport) { 149152c9ce25SScott Long xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n", 149252c9ce25SScott Long cts->transport, device->transport); 149352c9ce25SScott Long cts->transport = device->transport; 149452c9ce25SScott Long } 149552c9ce25SScott Long 149652c9ce25SScott Long if (cts->transport_version > device->transport_version) { 149752c9ce25SScott Long if (bootverbose) { 149852c9ce25SScott Long xpt_print(cts->ccb_h.path, "Down reving Transport " 149952c9ce25SScott Long "Version from %d to %d?\n", cts->transport_version, 150052c9ce25SScott Long device->transport_version); 150152c9ce25SScott Long } 150252c9ce25SScott Long cts->transport_version = device->transport_version; 150352c9ce25SScott Long } 150452c9ce25SScott Long 150552c9ce25SScott Long sim = cts->ccb_h.path->bus->sim; 150652c9ce25SScott Long 150752c9ce25SScott Long /* 150852c9ce25SScott Long * Nothing more of interest to do unless 150952c9ce25SScott Long * this is a device connected via the 151052c9ce25SScott Long * SCSI protocol. 151152c9ce25SScott Long */ 151252c9ce25SScott Long if (cts->protocol != PROTO_SCSI) { 151352c9ce25SScott Long if (async_update == FALSE) 151452c9ce25SScott Long (*(sim->sim_action))(sim, (union ccb *)cts); 151552c9ce25SScott Long return; 151652c9ce25SScott Long } 151752c9ce25SScott Long 151852c9ce25SScott Long inq_data = &device->inq_data; 151952c9ce25SScott Long scsi = &cts->proto_specific.scsi; 152083c5d981SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE); 152152c9ce25SScott Long cpi.ccb_h.func_code = XPT_PATH_INQ; 152252c9ce25SScott Long xpt_action((union ccb *)&cpi); 152352c9ce25SScott Long 152452c9ce25SScott Long /* SCSI specific sanity checking */ 152552c9ce25SScott Long if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0 152652c9ce25SScott Long || (INQ_DATA_TQ_ENABLED(inq_data)) == 0 152752c9ce25SScott Long || (device->queue_flags & SCP_QUEUE_DQUE) != 0 152852c9ce25SScott Long || (device->mintags == 0)) { 152952c9ce25SScott Long /* 153052c9ce25SScott Long * Can't tag on hardware that doesn't support tags, 153152c9ce25SScott Long * doesn't have it enabled, or has broken tag support. 153252c9ce25SScott Long */ 153352c9ce25SScott Long scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 153452c9ce25SScott Long } 153552c9ce25SScott Long 153652c9ce25SScott Long if (async_update == FALSE) { 153752c9ce25SScott Long /* 153852c9ce25SScott Long * Perform sanity checking against what the 153952c9ce25SScott Long * controller and device can do. 154052c9ce25SScott Long */ 154183c5d981SAlexander Motin xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE); 154252c9ce25SScott Long cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 154352c9ce25SScott Long cur_cts.type = cts->type; 154452c9ce25SScott Long xpt_action((union ccb *)&cur_cts); 154552c9ce25SScott Long if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 154652c9ce25SScott Long return; 154752c9ce25SScott Long } 154852c9ce25SScott Long cur_scsi = &cur_cts.proto_specific.scsi; 154952c9ce25SScott Long if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) { 155052c9ce25SScott Long scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 155152c9ce25SScott Long scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB; 155252c9ce25SScott Long } 155352c9ce25SScott Long if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0) 155452c9ce25SScott Long scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 155552c9ce25SScott Long } 155652c9ce25SScott Long 155752c9ce25SScott Long if (cts->type == CTS_TYPE_CURRENT_SETTINGS 155852c9ce25SScott Long && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 155952c9ce25SScott Long int device_tagenb; 156052c9ce25SScott Long 156152c9ce25SScott Long /* 156252c9ce25SScott Long * If we are transitioning from tags to no-tags or 156352c9ce25SScott Long * vice-versa, we need to carefully freeze and restart 156452c9ce25SScott Long * the queue so that we don't overlap tagged and non-tagged 156552c9ce25SScott Long * commands. We also temporarily stop tags if there is 156652c9ce25SScott Long * a change in transfer negotiation settings to allow 156752c9ce25SScott Long * "tag-less" negotiation. 156852c9ce25SScott Long */ 156952c9ce25SScott Long if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 157052c9ce25SScott Long || (device->inq_flags & SID_CmdQue) != 0) 157152c9ce25SScott Long device_tagenb = TRUE; 157252c9ce25SScott Long else 157352c9ce25SScott Long device_tagenb = FALSE; 157452c9ce25SScott Long 157552c9ce25SScott Long if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0 157652c9ce25SScott Long && device_tagenb == FALSE) 157752c9ce25SScott Long || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0 157852c9ce25SScott Long && device_tagenb == TRUE)) { 157952c9ce25SScott Long 158052c9ce25SScott Long if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) { 158152c9ce25SScott Long /* 158252c9ce25SScott Long * Delay change to use tags until after a 158352c9ce25SScott Long * few commands have gone to this device so 158452c9ce25SScott Long * the controller has time to perform transfer 158552c9ce25SScott Long * negotiations without tagged messages getting 158652c9ce25SScott Long * in the way. 158752c9ce25SScott Long */ 158852c9ce25SScott Long device->tag_delay_count = CAM_TAG_DELAY_COUNT; 158952c9ce25SScott Long device->flags |= CAM_DEV_TAG_AFTER_COUNT; 159052c9ce25SScott Long } else { 159130a4094fSAlexander Motin xpt_stop_tags(cts->ccb_h.path); 159252c9ce25SScott Long } 159352c9ce25SScott Long } 159452c9ce25SScott Long } 159552c9ce25SScott Long if (async_update == FALSE) 159652c9ce25SScott Long (*(sim->sim_action))(sim, (union ccb *)cts); 159752c9ce25SScott Long } 159852c9ce25SScott Long 159952c9ce25SScott Long /* 160052c9ce25SScott Long * Handle any per-device event notifications that require action by the XPT. 160152c9ce25SScott Long */ 160252c9ce25SScott Long static void 160352c9ce25SScott Long ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target, 160452c9ce25SScott Long struct cam_ed *device, void *async_arg) 160552c9ce25SScott Long { 160652c9ce25SScott Long cam_status status; 160752c9ce25SScott Long struct cam_path newpath; 160852c9ce25SScott Long 160952c9ce25SScott Long /* 161052c9ce25SScott Long * We only need to handle events for real devices. 161152c9ce25SScott Long */ 161252c9ce25SScott Long if (target->target_id == CAM_TARGET_WILDCARD 161352c9ce25SScott Long || device->lun_id == CAM_LUN_WILDCARD) 161452c9ce25SScott Long return; 161552c9ce25SScott Long 161652c9ce25SScott Long /* 161752c9ce25SScott Long * We need our own path with wildcards expanded to 161852c9ce25SScott Long * handle certain types of events. 161952c9ce25SScott Long */ 162052c9ce25SScott Long if ((async_code == AC_SENT_BDR) 162152c9ce25SScott Long || (async_code == AC_BUS_RESET) 162252c9ce25SScott Long || (async_code == AC_INQ_CHANGED)) 162352c9ce25SScott Long status = xpt_compile_path(&newpath, NULL, 162452c9ce25SScott Long bus->path_id, 162552c9ce25SScott Long target->target_id, 162652c9ce25SScott Long device->lun_id); 162752c9ce25SScott Long else 162852c9ce25SScott Long status = CAM_REQ_CMP_ERR; 162952c9ce25SScott Long 163052c9ce25SScott Long if (status == CAM_REQ_CMP) { 163152c9ce25SScott Long if (async_code == AC_INQ_CHANGED) { 163252c9ce25SScott Long /* 163352c9ce25SScott Long * We've sent a start unit command, or 163452c9ce25SScott Long * something similar to a device that 163552c9ce25SScott Long * may have caused its inquiry data to 163652c9ce25SScott Long * change. So we re-scan the device to 163752c9ce25SScott Long * refresh the inquiry data for it. 163852c9ce25SScott Long */ 163952c9ce25SScott Long ata_scan_lun(newpath.periph, &newpath, 164052c9ce25SScott Long CAM_EXPECT_INQ_CHANGE, NULL); 164183c5d981SAlexander Motin } else { 164283c5d981SAlexander Motin /* We need to reinitialize device after reset. */ 164383c5d981SAlexander Motin ata_scan_lun(newpath.periph, &newpath, 164483c5d981SAlexander Motin 0, NULL); 164552c9ce25SScott Long } 164652c9ce25SScott Long xpt_release_path(&newpath); 1647f98d7a47SAlexander Motin } else if (async_code == AC_LOST_DEVICE && 1648f98d7a47SAlexander Motin (device->flags & CAM_DEV_UNCONFIGURED) == 0) { 164952c9ce25SScott Long device->flags |= CAM_DEV_UNCONFIGURED; 1650f98d7a47SAlexander Motin xpt_release_device(device); 165152c9ce25SScott Long } else if (async_code == AC_TRANSFER_NEG) { 165252c9ce25SScott Long struct ccb_trans_settings *settings; 165352c9ce25SScott Long 165452c9ce25SScott Long settings = (struct ccb_trans_settings *)async_arg; 165530a4094fSAlexander Motin ata_set_transfer_settings(settings, device, 165652c9ce25SScott Long /*async_update*/TRUE); 165752c9ce25SScott Long } 165852c9ce25SScott Long } 165952c9ce25SScott Long 166057079b17SAlexander Motin static void 166157079b17SAlexander Motin ata_announce_periph(struct cam_periph *periph) 166257079b17SAlexander Motin { 166357079b17SAlexander Motin struct ccb_pathinq cpi; 166457079b17SAlexander Motin struct ccb_trans_settings cts; 166557079b17SAlexander Motin struct cam_path *path = periph->path; 166657079b17SAlexander Motin u_int speed; 166757079b17SAlexander Motin u_int mb; 166857079b17SAlexander Motin 166957079b17SAlexander Motin mtx_assert(periph->sim->mtx, MA_OWNED); 167057079b17SAlexander Motin 167157079b17SAlexander Motin xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL); 167257079b17SAlexander Motin cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 167357079b17SAlexander Motin cts.type = CTS_TYPE_CURRENT_SETTINGS; 167457079b17SAlexander Motin xpt_action((union ccb*)&cts); 167557079b17SAlexander Motin if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 167657079b17SAlexander Motin return; 167757079b17SAlexander Motin /* Ask the SIM for its base transfer speed */ 167857079b17SAlexander Motin xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 167957079b17SAlexander Motin cpi.ccb_h.func_code = XPT_PATH_INQ; 168057079b17SAlexander Motin xpt_action((union ccb *)&cpi); 168157079b17SAlexander Motin /* Report connection speed */ 168257079b17SAlexander Motin speed = cpi.base_transfer_speed; 168357079b17SAlexander Motin if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 168457079b17SAlexander Motin struct ccb_trans_settings_ata *ata = 168557079b17SAlexander Motin &cts.xport_specific.ata; 168657079b17SAlexander Motin 168757079b17SAlexander Motin if (ata->valid & CTS_ATA_VALID_MODE) 168857079b17SAlexander Motin speed = ata_mode2speed(ata->mode); 168957079b17SAlexander Motin } 169057079b17SAlexander Motin if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 169157079b17SAlexander Motin struct ccb_trans_settings_sata *sata = 169257079b17SAlexander Motin &cts.xport_specific.sata; 169357079b17SAlexander Motin 169457079b17SAlexander Motin if (sata->valid & CTS_SATA_VALID_REVISION) 169557079b17SAlexander Motin speed = ata_revision2speed(sata->revision); 169657079b17SAlexander Motin } 169757079b17SAlexander Motin mb = speed / 1000; 169857079b17SAlexander Motin if (mb > 0) 169957079b17SAlexander Motin printf("%s%d: %d.%03dMB/s transfers", 170057079b17SAlexander Motin periph->periph_name, periph->unit_number, 170157079b17SAlexander Motin mb, speed % 1000); 170257079b17SAlexander Motin else 170357079b17SAlexander Motin printf("%s%d: %dKB/s transfers", periph->periph_name, 170457079b17SAlexander Motin periph->unit_number, speed); 170557079b17SAlexander Motin /* Report additional information about connection */ 170657079b17SAlexander Motin if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) { 170757079b17SAlexander Motin struct ccb_trans_settings_ata *ata = 170857079b17SAlexander Motin &cts.xport_specific.ata; 170957079b17SAlexander Motin 171057079b17SAlexander Motin printf(" ("); 171157079b17SAlexander Motin if (ata->valid & CTS_ATA_VALID_MODE) 171257079b17SAlexander Motin printf("%s, ", ata_mode2string(ata->mode)); 171357079b17SAlexander Motin if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0) 171457079b17SAlexander Motin printf("ATAPI %dbytes, ", ata->atapi); 171557079b17SAlexander Motin if (ata->valid & CTS_ATA_VALID_BYTECOUNT) 171657079b17SAlexander Motin printf("PIO %dbytes", ata->bytecount); 171757079b17SAlexander Motin printf(")"); 171857079b17SAlexander Motin } 171957079b17SAlexander Motin if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) { 172057079b17SAlexander Motin struct ccb_trans_settings_sata *sata = 172157079b17SAlexander Motin &cts.xport_specific.sata; 172257079b17SAlexander Motin 172357079b17SAlexander Motin printf(" ("); 172457079b17SAlexander Motin if (sata->valid & CTS_SATA_VALID_REVISION) 172557079b17SAlexander Motin printf("SATA %d.x, ", sata->revision); 172657079b17SAlexander Motin else 172757079b17SAlexander Motin printf("SATA, "); 172857079b17SAlexander Motin if (sata->valid & CTS_SATA_VALID_MODE) 172957079b17SAlexander Motin printf("%s, ", ata_mode2string(sata->mode)); 173057079b17SAlexander Motin if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0) 173157079b17SAlexander Motin printf("ATAPI %dbytes, ", sata->atapi); 173257079b17SAlexander Motin if (sata->valid & CTS_SATA_VALID_BYTECOUNT) 173357079b17SAlexander Motin printf("PIO %dbytes", sata->bytecount); 173457079b17SAlexander Motin printf(")"); 173557079b17SAlexander Motin } 173657079b17SAlexander Motin printf("\n"); 173757079b17SAlexander Motin } 173857079b17SAlexander Motin 1739