4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies.
43 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
44 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
46 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
47 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
48 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
49 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
55 * Implement each SATA port as its own SCSI bus on CAM. This way we can
56 * implement future port multiplier features as individual devices on the
59 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
60 * was written natively for DragonFly.
65 static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
66 static void ahci_xpt_poll(struct cam_sim *sim);
67 static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
68 struct ata_port *at, union ccb *ccb);
69 static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
70 struct ata_port *at, union ccb *ccb);
71 static void ahci_xpt_page_inquiry(struct ahci_port *ap,
72 struct ata_port *at, union ccb *ccb);
74 static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
75 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
76 static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
77 static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
78 static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
79 struct scsi_sense_data *sense_data);
81 static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
82 static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
83 static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx);
84 static void ahci_ata_dummy_done(struct ata_xfer *xa);
85 static void ata_fix_identify(struct ata_identify *id);
86 static void ahci_cam_rescan(struct ahci_port *ap);
87 static void ahci_strip_string(const char **basep, int *lenp);
90 ahci_cam_attach(struct ahci_port *ap)
92 struct cam_devq *devq;
98 * We want at least one ccb to be available for error processing
99 * so don't let CAM use more then ncmds - 1.
101 unit = device_get_unit(ap->ap_sc->sc_dev);
102 if (ap->ap_sc->sc_ncmds > 1)
103 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
105 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
111 * Give the devq enough room to run with 32 max_dev_transactions,
112 * but set the overall max tags to 1 until NCQ is negotiated.
114 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
115 (void *)ap, unit, &ap->ap_sim_lock,
117 cam_simq_release(devq);
122 ahci_os_unlock_port(ap);
123 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
124 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
125 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
126 ahci_os_lock_port(ap);
127 if (error != CAM_SUCCESS) {
131 ap->ap_flags |= AP_F_BUS_REGISTERED;
133 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
134 error = ahci_cam_probe(ap, NULL);
141 ap->ap_flags |= AP_F_CAM_ATTACHED;
147 * The state of the port has changed.
149 * If at is NULL the physical port has changed state.
150 * If at is non-NULL a particular target behind a PM has changed state.
152 * If found is -1 the target state must be queued to a non-interrupt context.
153 * (only works with at == NULL).
155 * If found is 0 the target was removed.
156 * If found is 1 the target was inserted.
159 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
161 struct cam_path *tmppath;
165 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
167 if (ap->ap_sim == NULL)
169 if (found == CAM_TARGET_WILDCARD) {
170 status = xpt_create_path(&tmppath, NULL,
171 cam_sim_path(ap->ap_sim),
172 target, CAM_LUN_WILDCARD);
173 if (status != CAM_REQ_CMP)
177 status = xpt_create_path(&tmppath, NULL,
178 cam_sim_path(ap->ap_sim),
181 if (status != CAM_REQ_CMP)
188 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
190 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
193 xpt_free_path(tmppath);
197 ahci_cam_detach(struct ahci_port *ap)
201 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
203 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
205 xpt_freeze_simq(ap->ap_sim, 1);
207 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
208 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
209 KKASSERT(error == CAM_REQ_CMP);
210 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
213 cam_sim_free(ap->ap_sim);
216 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
217 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
221 * Once the AHCI port has been attached we need to probe for a device or
222 * devices on the port and setup various options.
224 * If at is NULL we are probing the direct-attached device on the port,
225 * which may or may not be a port multiplier.
228 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
233 u_int64_t capacity_bytes;
240 const char *model_id;
241 const char *firmware_id;
242 const char *serial_id;
251 * Delayed CAM attachment for initial probe, sim may be NULL
253 if (ap->ap_sim == NULL)
257 * A NULL atx indicates a probe of the directly connected device.
258 * A non-NULL atx indicates a device connected via a port multiplier.
259 * We need to preserve atx for calls to ahci_ata_get_xfer().
261 * at is always non-NULL. For directly connected devices we supply
262 * an (at) pointing to target 0.
265 at = ap->ap_ata[0]; /* direct attached - device 0 */
266 if (ap->ap_type == ATA_PORT_T_PM) {
267 kprintf("%s: Found Port Multiplier\n",
271 at->at_type = ap->ap_type;
274 if (atx->at_type == ATA_PORT_T_PM) {
275 kprintf("%s: Bogus device, reducing port count to %d\n",
276 ATANAME(ap, atx), atx->at_target);
277 if (ap->ap_pmcount > atx->at_target)
278 ap->ap_pmcount = atx->at_target;
282 if (ap->ap_type == ATA_PORT_T_NONE)
284 if (at->at_type == ATA_PORT_T_NONE)
288 * Issue identify, saving the result
290 xa = ahci_ata_get_xfer(ap, atx);
291 xa->complete = ahci_ata_dummy_done;
292 xa->data = &at->at_identify;
293 xa->datalen = sizeof(at->at_identify);
294 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
295 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
297 switch(at->at_type) {
298 case ATA_PORT_T_DISK:
299 xa->fis->command = ATA_C_IDENTIFY;
302 case ATA_PORT_T_ATAPI:
303 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
304 xa->flags |= ATA_F_AUTOSENSE;
308 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
309 type = "UNKNOWN(ATAPI?)";
312 xa->fis->features = 0;
316 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
317 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
318 ATANAME(ap, atx), type);
319 ahci_ata_put_xfer(xa);
322 ahci_ata_put_xfer(xa);
324 ata_fix_identify(&at->at_identify);
327 * Read capacity using SATA probe info.
329 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
330 /* LBA48 feature set supported */
332 for (i = 3; i >= 0; --i) {
335 le16toh(at->at_identify.addrsecxt[i]);
338 capacity = le16toh(at->at_identify.addrsec[1]);
340 capacity += le16toh(at->at_identify.addrsec[0]);
343 capacity = 1024 * 1024 / 512;
344 at->at_capacity = capacity;
346 ap->ap_probe = ATA_PROBE_GOOD;
348 capacity_bytes = capacity * 512;
351 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
352 * number of slots and limit the number of CAM ccb's to one less
353 * so we always have a slot available for recovery.
355 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
356 * not support it, and in that case the driver can handle extra
359 * NCQ is currently used only with direct-attached disks. It is
360 * not used with port multipliers or direct-attached ATAPI devices.
362 * Remember at least one extra CCB needs to be reserved for the
365 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
366 ap->ap_type == ATA_PORT_T_DISK &&
367 (le16toh(at->at_identify.satacap) & (1 << 8))) {
368 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
369 devncqdepth = at->at_ncqdepth;
370 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
371 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
372 if (at->at_ncqdepth > 1) {
373 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
374 xa = ahci_ata_get_xfer(ap, atx);
375 if (xa->tag < at->at_ncqdepth) {
376 xa->state = ATA_S_COMPLETE;
377 ahci_ata_put_xfer(xa);
380 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
381 cam_sim_set_max_tags(ap->ap_sim,
382 at->at_ncqdepth - 1);
389 model_len = sizeof(at->at_identify.model);
390 model_id = at->at_identify.model;
391 ahci_strip_string(&model_id, &model_len);
393 firmware_len = sizeof(at->at_identify.firmware);
394 firmware_id = at->at_identify.firmware;
395 ahci_strip_string(&firmware_id, &firmware_len);
397 serial_len = sizeof(at->at_identify.serial);
398 serial_id = at->at_identify.serial;
399 ahci_strip_string(&serial_id, &serial_len);
402 * Generate informatiive strings.
404 * NOTE: We do not automatically set write caching, lookahead,
405 * or the security state for ATAPI devices.
407 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
408 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
410 else if (at->at_type == ATA_PORT_T_ATAPI)
418 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
419 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
421 else if (at->at_type == ATA_PORT_T_ATAPI)
429 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
430 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
432 else if (at->at_type == ATA_PORT_T_ATAPI)
434 else if (AhciNoFeatures & (1 << ap->ap_num))
435 scstr = "<disabled>";
442 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
443 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
444 "capacity=%lld.%02dMB\n",
448 model_len, model_len, model_id,
449 firmware_len, firmware_len, firmware_id,
450 serial_len, serial_len, serial_id,
453 devncqdepth, ap->ap_sc->sc_ncmds,
454 at->at_identify.satacap,
455 at->at_identify.satafsup,
456 (at->at_ncqdepth > 1 ? "YES" : "NO"),
457 (long long)capacity_bytes / (1024 * 1024),
458 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
460 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
462 at->at_identify.features85,
463 at->at_identify.features86,
464 at->at_identify.features87,
471 * Additional type-specific probing
473 switch(at->at_type) {
474 case ATA_PORT_T_DISK:
475 error = ahci_cam_probe_disk(ap, atx);
477 case ATA_PORT_T_ATAPI:
478 error = ahci_cam_probe_atapi(ap, atx);
486 at->at_probe = ATA_PROBE_FAILED;
488 ap->ap_probe = at->at_probe;
490 at->at_probe = ATA_PROBE_GOOD;
492 ap->ap_probe = at->at_probe;
498 * DISK-specific probe after initial ident
501 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
506 at = atx ? atx : ap->ap_ata[0];
509 * Set dummy xfer mode
511 ahci_set_xfer(ap, atx);
514 * Enable write cache if supported
516 * NOTE: "WD My Book" external disk devices have a very poor
517 * daughter board between the the ESATA and the HD. Sending
518 * any ATA_C_SET_FEATURES commands will break the hardware port
519 * with a fatal protocol error. However, this device also
520 * indicates that WRITECACHE is already on and READAHEAD is
521 * not supported so we avoid the issue.
523 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
524 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
525 xa = ahci_ata_get_xfer(ap, atx);
526 xa->complete = ahci_ata_dummy_done;
527 xa->fis->command = ATA_C_SET_FEATURES;
528 xa->fis->features = ATA_SF_WRITECACHE_EN;
529 /* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */
530 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
532 xa->flags = ATA_F_PIO | ATA_F_POLL;
535 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
536 at->at_features |= ATA_PORT_F_WCACHE;
538 kprintf("%s: Unable to enable write-caching\n",
540 ahci_ata_put_xfer(xa);
544 * Enable readahead if supported
546 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
547 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
548 xa = ahci_ata_get_xfer(ap, atx);
549 xa->complete = ahci_ata_dummy_done;
550 xa->fis->command = ATA_C_SET_FEATURES;
551 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
552 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
554 xa->flags = ATA_F_PIO | ATA_F_POLL;
557 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
558 at->at_features |= ATA_PORT_F_RAHEAD;
560 kprintf("%s: Unable to enable read-ahead\n",
562 ahci_ata_put_xfer(xa);
566 * FREEZE LOCK the device so malicious users can't lock it on us.
567 * As there is no harm in issuing this to devices that don't
568 * support the security feature set we just send it, and don't bother
569 * checking if the device sends a command abort to tell us it doesn't
572 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
573 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
574 (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
575 xa = ahci_ata_get_xfer(ap, atx);
576 xa->complete = ahci_ata_dummy_done;
577 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
578 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
579 xa->flags = ATA_F_PIO | ATA_F_POLL;
582 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
583 at->at_features |= ATA_PORT_F_FRZLCK;
585 kprintf("%s: Unable to set security freeze\n",
587 ahci_ata_put_xfer(xa);
594 * ATAPI-specific probe after initial ident
597 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
599 ahci_set_xfer(ap, atx);
604 * Setting the transfer mode is irrelevant for the SATA transport
605 * but some (atapi) devices seem to need it anyway. In addition
606 * if we are running through a SATA->PATA converter for some reason
607 * beyond my comprehension we might have to set the mode.
609 * We only support DMA modes for SATA attached devices, so don't bother
613 ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx)
620 at = atx ? atx : ap->ap_ata[0];
623 * Figure out the supported UDMA mode. Ignore other legacy modes.
625 mask = le16toh(at->at_identify.ultradma);
626 if ((mask & 0xFF) == 0 || mask == 0xFFFF)
630 while ((mask & 0x8000) == 0) {
636 * SATA atapi devices often still report a dma mode, even though
637 * it is irrelevant for SATA transport. It is also possible that
638 * we are running through a SATA->PATA converter and seeing the
641 * In this case the device may require a (dummy) SETXFER to be
642 * sent before it will work properly.
644 xa = ahci_ata_get_xfer(ap, atx);
645 xa->complete = ahci_ata_dummy_done;
646 xa->fis->command = ATA_C_SET_FEATURES;
647 xa->fis->features = ATA_SF_SETXFER;
648 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
649 xa->fis->sector_count = mode;
650 xa->flags = ATA_F_PIO | ATA_F_POLL;
653 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
654 kprintf("%s: Unable to set dummy xfer mode \n",
656 } else if (bootverbose) {
657 kprintf("%s: Set dummy xfer mode to %02x\n",
658 ATANAME(ap, atx), mode);
660 ahci_ata_put_xfer(xa);
665 * Fix byte ordering so buffers can be accessed as
669 ata_fix_identify(struct ata_identify *id)
674 swap = (u_int16_t *)id->serial;
675 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
676 swap[i] = bswap16(swap[i]);
678 swap = (u_int16_t *)id->firmware;
679 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
680 swap[i] = bswap16(swap[i]);
682 swap = (u_int16_t *)id->model;
683 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
684 swap[i] = bswap16(swap[i]);
688 * Dummy done callback for xa.
691 ahci_ata_dummy_done(struct ata_xfer *xa)
696 * Use an engineering request to initiate a target scan for devices
697 * behind a port multiplier.
699 * An asynchronous bus scan is used to avoid reentrancy issues.
702 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
704 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
706 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
707 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
708 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
709 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
712 ap->ap_flags |= AP_F_SCAN_COMPLETED;
713 wakeup(&ap->ap_flags);
719 ahci_cam_rescan(struct ahci_port *ap)
721 struct cam_path *path;
726 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
727 ap->ap_flags |= AP_F_SCAN_REQUESTED;
730 ap->ap_flags |= AP_F_SCAN_RUNNING;
731 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
732 ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN;
735 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
736 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
737 if (status != CAM_REQ_CMP)
740 ccb = xpt_alloc_ccb();
741 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
742 ccb->ccb_h.func_code = XPT_ENG_EXEC;
743 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
744 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
745 ccb->crcn.flags = CAM_FLAG_NONE;
746 xpt_action_async(ccb);
750 ahci_xpt_rescan(struct ahci_port *ap)
752 struct cam_path *path;
756 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
757 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
758 if (status != CAM_REQ_CMP)
761 ccb = xpt_alloc_ccb();
762 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
763 ccb->ccb_h.func_code = XPT_SCAN_BUS;
764 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
765 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
766 ccb->crcn.flags = CAM_FLAG_NONE;
767 xpt_action_async(ccb);
771 * Action function - dispatch command
775 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
777 struct ahci_port *ap;
778 struct ata_port *at, *atx;
779 struct ccb_hdr *ccbh;
783 ap = cam_sim_softc(sim);
785 KKASSERT(ap != NULL);
787 unit = cam_sim_unit(sim);
790 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
791 * otherwise the bus rescan will not remove the dead devices when
794 * For non-wildcards we have one target (0) and one lun (0),
795 * unless we have a port multiplier.
797 * A wildcard target indicates only the general bus is being
800 * Calculate at and atx. at is always non-NULL. atx is only
801 * non-NULL for direct-attached devices. It will be NULL for
802 * devices behind a port multiplier.
804 * XXX What do we do with a LUN wildcard?
806 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
807 ccbh->func_code != XPT_PATH_INQ) {
808 if (ap->ap_type == ATA_PORT_T_NONE) {
809 ccbh->status = CAM_DEV_NOT_THERE;
813 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
814 ccbh->status = CAM_DEV_NOT_THERE;
818 at = ap->ap_ata[ccbh->target_id];
819 if (ap->ap_type == ATA_PORT_T_PM)
822 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
823 ccbh->status = CAM_DEV_NOT_THERE;
832 * Switch on the meta XPT command
834 switch(ccbh->func_code) {
837 * This routine is called after a port multiplier has been
840 ccbh->status = CAM_REQ_CMP;
841 ahci_os_lock_port(ap);
842 ahci_port_state_machine(ap, 0);
843 ahci_os_unlock_port(ap);
849 * This command always succeeds, otherwise the bus scan
850 * will not detach dead devices.
852 ccb->cpi.version_num = 1;
853 ccb->cpi.hba_inquiry = 0;
854 ccb->cpi.target_sprt = 0;
855 ccb->cpi.hba_misc = PIM_SEQSCAN;
856 ccb->cpi.hba_eng_cnt = 0;
857 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
858 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
859 ccb->cpi.max_lun = 0;
860 ccb->cpi.async_flags = 0;
861 ccb->cpi.hpath_id = 0;
862 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
863 ccb->cpi.unit_number = cam_sim_unit(sim);
864 ccb->cpi.bus_id = cam_sim_bus(sim);
865 ccb->cpi.base_transfer_speed = 150000;
866 ccb->cpi.transport = XPORT_SATA;
867 ccb->cpi.transport_version = 1;
868 ccb->cpi.protocol = PROTO_SCSI;
869 ccb->cpi.protocol_version = SCSI_REV_2;
871 ccbh->status = CAM_REQ_CMP;
872 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
873 ahci_os_lock_port(ap);
874 ahci_port_state_machine(ap, 0);
875 ahci_os_unlock_port(ap);
877 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
878 AHCI_PREG_SSTS_SPD) {
879 case AHCI_PREG_SSTS_SPD_GEN1:
880 ccb->cpi.base_transfer_speed = 150000;
882 case AHCI_PREG_SSTS_SPD_GEN2:
883 ccb->cpi.base_transfer_speed = 300000;
885 case AHCI_PREG_SSTS_SPD_GEN3:
886 ccb->cpi.base_transfer_speed = 600000;
890 ccb->cpi.base_transfer_speed = 1000;
894 if (ap->ap_type == ATA_PORT_T_NONE)
895 ccbh->status = CAM_DEV_NOT_THERE;
901 ahci_os_lock_port(ap);
902 if (ap->ap_type == ATA_PORT_T_NONE) {
903 ccbh->status = CAM_DEV_NOT_THERE;
905 ahci_port_reset(ap, atx, 0);
906 ccbh->status = CAM_REQ_CMP;
908 ahci_os_unlock_port(ap);
912 ahci_os_lock_port(ap);
913 ahci_port_reset(ap, NULL, 1);
914 ahci_os_unlock_port(ap);
915 ccbh->status = CAM_REQ_CMP;
918 case XPT_SET_TRAN_SETTINGS:
919 ccbh->status = CAM_FUNC_NOTAVAIL;
922 case XPT_GET_TRAN_SETTINGS:
923 ccb->cts.protocol = PROTO_SCSI;
924 ccb->cts.protocol_version = SCSI_REV_2;
925 ccb->cts.transport = XPORT_SATA;
926 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
927 ccb->cts.proto_specific.valid = 0;
928 ccb->cts.xport_specific.valid = 0;
929 ccbh->status = CAM_REQ_CMP;
932 case XPT_CALC_GEOMETRY:
933 cam_calc_geometry(&ccb->ccg, 1);
938 * Our parallel startup code might have only probed through
939 * to the IDENT, so do the last step if necessary.
941 if (at->at_probe == ATA_PROBE_NEED_IDENT)
942 ahci_cam_probe(ap, atx);
943 if (at->at_probe != ATA_PROBE_GOOD) {
944 ccbh->status = CAM_DEV_NOT_THERE;
948 switch(at->at_type) {
949 case ATA_PORT_T_DISK:
950 ahci_xpt_scsi_disk_io(ap, atx, ccb);
952 case ATA_PORT_T_ATAPI:
953 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
956 ccbh->status = CAM_REQ_INVALID;
962 ccbh->status = CAM_REQ_INVALID;
971 * Generally this function gets called heavily when interrupts might be
972 * non-operational, during a halt/reboot or panic.
976 ahci_xpt_poll(struct cam_sim *sim)
978 struct ahci_port *ap;
980 ap = cam_sim_softc(sim);
982 ahci_os_lock_port(ap);
983 ahci_port_intr(ap, 1);
984 ahci_os_unlock_port(ap);
989 * Convert the SCSI command in ccb to an ata_xfer command in xa
990 * for ATA_PORT_T_DISK operations. Set the completion function
991 * to convert the response back, then dispatch to the OpenBSD AHCI
994 * AHCI DISK commands only support a limited command set, and we
995 * fake additional commands to make it play nice with the CAM subsystem.
999 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
1002 struct ccb_hdr *ccbh;
1003 struct ccb_scsiio *csio;
1004 struct ata_xfer *xa;
1005 struct ata_port *at;
1006 struct ata_fis_h2d *fis;
1007 struct ata_pass_12 *atp12;
1008 struct ata_pass_16 *atp16;
1010 union scsi_data *rdata;
1016 ccbh = &ccb->csio.ccb_h;
1018 at = atx ? atx : ap->ap_ata[0];
1021 * XXX not passing NULL at for direct attach!
1023 xa = ahci_ata_get_xfer(ap, atx);
1024 rdata = (void *)csio->data_ptr;
1025 rdata_len = csio->dxfer_len;
1028 * Build the FIS or process the csio to completion.
1030 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1031 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1033 switch(cdb->generic.opcode) {
1036 * Auto-sense everything, so explicit sense requests
1039 ccbh->status = CAM_SCSI_STATUS_ERROR;
1043 * Inquiry supported features
1045 * [opcode, byte2, page_code, length, control]
1047 if (cdb->inquiry.byte2 & SI_EVPD) {
1048 ahci_xpt_page_inquiry(ap, at, ccb);
1050 bzero(rdata, rdata_len);
1051 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1052 ccbh->status = CAM_CCB_LEN_ERR;
1055 if (rdata_len > sizeof(rdata->inquiry_data))
1056 rdata_len = sizeof(rdata->inquiry_data);
1057 rdata->inquiry_data.device = T_DIRECT;
1058 rdata->inquiry_data.version = SCSI_REV_SPC2;
1059 rdata->inquiry_data.response_format = 2;
1060 rdata->inquiry_data.additional_length = 32;
1061 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1062 bcopy(at->at_identify.model,
1063 rdata->inquiry_data.product,
1064 sizeof(rdata->inquiry_data.product));
1065 bcopy(at->at_identify.firmware,
1066 rdata->inquiry_data.revision,
1067 sizeof(rdata->inquiry_data.revision));
1068 ccbh->status = CAM_REQ_CMP;
1071 case READ_CAPACITY_16:
1072 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1073 ccbh->status = CAM_REQ_INVALID;
1076 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1077 ccbh->status = CAM_CCB_LEN_ERR;
1082 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1083 ccbh->status = CAM_CCB_LEN_ERR;
1087 capacity = at->at_capacity;
1089 bzero(rdata, rdata_len);
1090 if (cdb->generic.opcode == READ_CAPACITY) {
1091 rdata_len = sizeof(rdata->read_capacity_data);
1092 if (capacity > 0xFFFFFFFFU)
1093 capacity = 0xFFFFFFFFU;
1094 bzero(&rdata->read_capacity_data, rdata_len);
1095 scsi_ulto4b((u_int32_t)capacity - 1,
1096 rdata->read_capacity_data.addr);
1097 scsi_ulto4b(512, rdata->read_capacity_data.length);
1099 rdata_len = sizeof(rdata->read_capacity_data_16);
1100 bzero(&rdata->read_capacity_data_16, rdata_len);
1101 scsi_u64to8b(capacity - 1,
1102 rdata->read_capacity_data_16.addr);
1103 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1105 ccbh->status = CAM_REQ_CMP;
1107 case SYNCHRONIZE_CACHE:
1109 * Synchronize cache. Specification says this can take
1110 * greater then 30 seconds so give it at least 45.
1113 fis->flags = ATA_H2D_FLAGS_CMD;
1114 fis->command = ATA_C_FLUSH_CACHE;
1116 if (xa->timeout < 45000)
1117 xa->timeout = 45000;
1120 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1122 case TEST_UNIT_READY:
1123 case START_STOP_UNIT:
1126 * Just silently return success
1128 ccbh->status = CAM_REQ_CMP;
1132 atp12 = &cdb->ata_pass_12;
1135 * Figure out the flags to be used, depending on the direction of the
1138 switch (ccbh->flags & CAM_DIR_MASK) {
1140 xa->flags = ATA_F_READ;
1143 xa->flags = ATA_F_WRITE;
1148 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1149 xa->data = csio->data_ptr;
1150 xa->datalen = csio->dxfer_len;
1151 xa->complete = ahci_ata_complete_disk_rw;
1152 xa->timeout = ccbh->timeout;
1155 * Populate the fis from the information we received through CAM
1158 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */
1159 fis->features = atp12->features;
1160 fis->sector_count = atp12->sector_count;
1161 fis->lba_low = atp12->lba_low;
1162 fis->lba_mid = atp12->lba_mid;
1163 fis->lba_high = atp12->lba_high;
1164 fis->device = atp12->device; /* maybe always 0? */
1165 fis->command = atp12->command;
1166 fis->control = atp12->control;
1169 * Mark as in progress so it is sent to the device.
1171 ccbh->status = CAM_REQ_INPROG;
1174 atp16 = &cdb->ata_pass_16;
1177 * Figure out the flags to be used, depending on the direction of the
1180 switch (ccbh->flags & CAM_DIR_MASK) {
1182 xa->flags = ATA_F_READ;
1185 xa->flags = ATA_F_WRITE;
1190 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1191 xa->data = csio->data_ptr;
1192 xa->datalen = csio->dxfer_len;
1193 xa->complete = ahci_ata_complete_disk_rw;
1194 xa->timeout = ccbh->timeout;
1197 * Populate the fis from the information we received through CAM
1200 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */
1201 fis->features = atp16->features;
1202 fis->features_exp = atp16->features_ext;
1203 fis->sector_count = atp16->sector_count;
1204 fis->sector_count_exp = atp16->sector_count_ext;
1205 fis->lba_low = atp16->lba_low;
1206 fis->lba_low_exp = atp16->lba_low_ext;
1207 fis->lba_mid = atp16->lba_mid;
1208 fis->lba_mid_exp = atp16->lba_mid_ext;
1209 fis->lba_high = atp16->lba_high;
1210 fis->lba_mid_exp = atp16->lba_mid_ext;
1211 fis->device = atp16->device; /* maybe always 0? */
1212 fis->command = atp16->command;
1215 * Mark as in progress so it is sent to the device.
1217 ccbh->status = CAM_REQ_INPROG;
1220 switch(cdb->generic.opcode) {
1222 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1223 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1224 xa->flags = ATA_F_READ;
1227 lba = scsi_4btoul(cdb->rw_10.addr);
1228 count = scsi_2btoul(cdb->rw_10.length);
1229 xa->flags = ATA_F_READ;
1232 lba = scsi_4btoul(cdb->rw_12.addr);
1233 count = scsi_4btoul(cdb->rw_12.length);
1234 xa->flags = ATA_F_READ;
1237 lba = scsi_8btou64(cdb->rw_16.addr);
1238 count = scsi_4btoul(cdb->rw_16.length);
1239 xa->flags = ATA_F_READ;
1242 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1243 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1244 xa->flags = ATA_F_WRITE;
1247 lba = scsi_4btoul(cdb->rw_10.addr);
1248 count = scsi_2btoul(cdb->rw_10.length);
1249 xa->flags = ATA_F_WRITE;
1252 lba = scsi_4btoul(cdb->rw_12.addr);
1253 count = scsi_4btoul(cdb->rw_12.length);
1254 xa->flags = ATA_F_WRITE;
1257 lba = scsi_8btou64(cdb->rw_16.addr);
1258 count = scsi_4btoul(cdb->rw_16.length);
1259 xa->flags = ATA_F_WRITE;
1262 ccbh->status = CAM_REQ_INVALID;
1265 if (ccbh->status != CAM_REQ_INPROG)
1269 fis->flags = ATA_H2D_FLAGS_CMD;
1270 fis->lba_low = (u_int8_t)lba;
1271 fis->lba_mid = (u_int8_t)(lba >> 8);
1272 fis->lba_high = (u_int8_t)(lba >> 16);
1273 fis->device = ATA_H2D_DEVICE_LBA;
1276 * NCQ only for direct-attached disks, do not currently
1277 * try to use NCQ with port multipliers.
1279 if (at->at_ncqdepth > 1 &&
1280 ap->ap_type == ATA_PORT_T_DISK &&
1281 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1282 (ccbh->flags & CAM_POLLED) == 0) {
1284 * Use NCQ - always uses 48 bit addressing
1286 xa->flags |= ATA_F_NCQ;
1287 fis->command = (xa->flags & ATA_F_WRITE) ?
1288 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1289 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1290 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1291 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1292 fis->sector_count = xa->tag << 3;
1293 fis->features = (u_int8_t)count;
1294 fis->features_exp = (u_int8_t)(count >> 8);
1295 } else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1299 fis->command = (xa->flags & ATA_F_WRITE) ?
1300 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1301 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1302 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1303 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1304 fis->sector_count = (u_int8_t)count;
1305 fis->sector_count_exp = (u_int8_t)(count >> 8);
1310 * NOTE: 256 sectors is supported, stored as 0.
1312 fis->command = (xa->flags & ATA_F_WRITE) ?
1313 ATA_C_WRITEDMA : ATA_C_READDMA;
1314 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1315 fis->sector_count = (u_int8_t)count;
1318 xa->data = csio->data_ptr;
1319 xa->datalen = csio->dxfer_len;
1320 xa->complete = ahci_ata_complete_disk_rw;
1321 xa->timeout = ccbh->timeout; /* milliseconds */
1323 if (xa->timeout > 10000) /* XXX - debug */
1324 xa->timeout = 10000;
1326 if (ccbh->flags & CAM_POLLED)
1327 xa->flags |= ATA_F_POLL;
1332 * If the request is still in progress the xa and FIS have
1333 * been set up (except for the PM target), and must be dispatched.
1334 * Otherwise the request was completed.
1336 if (ccbh->status == CAM_REQ_INPROG) {
1337 KKASSERT(xa->complete != NULL);
1338 xa->atascsi_private = ccb;
1339 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1340 ahci_os_lock_port(ap);
1341 xa->fis->flags |= at->at_target;
1343 ahci_os_unlock_port(ap);
1345 ahci_ata_put_xfer(xa);
1351 * Convert the SCSI command in ccb to an ata_xfer command in xa
1352 * for ATA_PORT_T_ATAPI operations. Set the completion function
1353 * to convert the response back, then dispatch to the OpenBSD AHCI
1358 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1361 struct ccb_hdr *ccbh;
1362 struct ccb_scsiio *csio;
1363 struct ata_xfer *xa;
1364 struct ata_fis_h2d *fis;
1368 struct ata_port *at;
1370 ccbh = &ccb->csio.ccb_h;
1372 at = atx ? atx : ap->ap_ata[0];
1374 switch (ccbh->flags & CAM_DIR_MASK) {
1376 flags = ATA_F_PACKET | ATA_F_READ;
1379 flags = ATA_F_PACKET | ATA_F_WRITE;
1382 flags = ATA_F_PACKET;
1385 ccbh->status = CAM_REQ_INVALID;
1392 * Special handling to get the rfis back into host memory while
1393 * still allowing the chip to run commands in parallel to
1394 * ATAPI devices behind a PM.
1396 flags |= ATA_F_AUTOSENSE;
1399 * The command has to fit in the packet command buffer.
1401 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1402 ccbh->status = CAM_CCB_LEN_ERR;
1408 * Initialize the XA and FIS. It is unclear how much of
1409 * this has to mimic the equivalent ATA command.
1411 * XXX not passing NULL at for direct attach!
1413 xa = ahci_ata_get_xfer(ap, atx);
1416 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1417 fis->command = ATA_C_PACKET;
1418 fis->device = ATA_H2D_DEVICE_LBA;
1419 fis->sector_count = xa->tag << 3;
1420 if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1421 if (flags & ATA_F_WRITE) {
1422 fis->features = ATA_H2D_FEATURES_DMA |
1423 ATA_H2D_FEATURES_DIR_WRITE;
1425 fis->features = ATA_H2D_FEATURES_DMA |
1426 ATA_H2D_FEATURES_DIR_READ;
1432 fis->control = ATA_FIS_CONTROL_4BIT;
1435 xa->data = csio->data_ptr;
1436 xa->datalen = csio->dxfer_len;
1437 xa->timeout = ccbh->timeout; /* milliseconds */
1439 if (ccbh->flags & CAM_POLLED)
1440 xa->flags |= ATA_F_POLL;
1443 * Copy the cdb to the packetcmd buffer in the FIS using a
1444 * convenient pointer in the xa.
1446 * Zero-out any trailing bytes in case the ATAPI device cares.
1448 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1449 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1450 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1451 if (csio->cdb_len < 16)
1452 bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len);
1455 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1456 cdbs->generic.opcode,
1457 csio->cdb_len, csio->dxfer_len);
1461 * Some ATAPI commands do not actually follow the SCSI standard.
1463 cdbd = (void *)xa->packetcmd;
1465 switch(cdbd->generic.opcode) {
1468 * Force SENSE requests to the ATAPI sense length.
1470 * It is unclear if this is needed or not.
1472 if (cdbd->sense.length == SSD_FULL_SIZE) {
1474 kprintf("%s: Shortening sense request\n",
1477 cdbd->sense.length = offsetof(struct scsi_sense_data,
1483 * Some ATAPI devices can't handle long inquiry lengths,
1484 * don't ask me why. Truncate the inquiry length.
1486 if (cdbd->inquiry.page_code == 0 &&
1487 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1488 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1494 * Convert *_6 to *_10 commands. Most ATAPI devices
1495 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1497 cdbd->rw_10.opcode |= 0x20;
1498 cdbd->rw_10.byte2 = 0;
1499 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1500 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1501 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1502 cdbd->rw_10.addr[3] = 0;
1503 cdbd->rw_10.reserved = 0;
1504 cdbd->rw_10.length[0] = 0;
1505 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1506 cdbd->rw_10.control = cdbs->rw_6.control;
1515 xa->complete = ahci_atapi_complete_cmd;
1516 xa->atascsi_private = ccb;
1517 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1518 ahci_os_lock_port(ap);
1520 ahci_os_unlock_port(ap);
1524 * Simulate page inquiries for disk attachments.
1528 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
1531 struct scsi_vpd_supported_page_list list;
1532 struct scsi_vpd_unit_serial_number serno;
1533 struct scsi_vpd_unit_devid devid;
1541 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1543 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1544 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1546 switch(cdb->inquiry.page_code) {
1547 case SVPD_SUPPORTED_PAGE_LIST:
1549 page->list.device = T_DIRECT;
1550 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1551 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1552 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1553 page->list.list[i++] = SVPD_UNIT_DEVID;
1554 page->list.length = i;
1555 len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1557 case SVPD_UNIT_SERIAL_NUMBER:
1559 j = sizeof(at->at_identify.serial);
1560 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1562 while (j > i && at->at_identify.serial[j-1] == ' ')
1564 page->serno.device = T_DIRECT;
1565 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1566 page->serno.length = j - i;
1567 bcopy(at->at_identify.serial + i,
1568 page->serno.serial_num, j - i);
1569 len = offsetof(struct scsi_vpd_unit_serial_number,
1572 case SVPD_UNIT_DEVID:
1573 /* fall through for now */
1575 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1579 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1580 if (len <= ccb->csio.dxfer_len) {
1581 ccb->ccb_h.status = CAM_REQ_CMP;
1582 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1583 bcopy(page, ccb->csio.data_ptr, len);
1584 ccb->csio.resid = ccb->csio.dxfer_len - len;
1586 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1589 kfree(page, M_DEVBUF);
1593 * Completion function for ATA_PORT_T_DISK cache synchronization.
1597 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1599 union ccb *ccb = xa->atascsi_private;
1600 struct ccb_hdr *ccbh = &ccb->ccb_h;
1601 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1604 case ATA_S_COMPLETE:
1605 ccbh->status = CAM_REQ_CMP;
1606 ccb->csio.scsi_status = SCSI_STATUS_OK;
1609 kprintf("%s: synchronize_cache: error\n",
1610 ATANAME(ap, xa->at));
1611 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1612 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1613 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1616 kprintf("%s: synchronize_cache: timeout\n",
1617 ATANAME(ap, xa->at));
1618 ccbh->status = CAM_CMD_TIMEOUT;
1621 kprintf("%s: synchronize_cache: unknown state %d\n",
1622 ATANAME(ap, xa->at), xa->state);
1623 ccbh->status = CAM_REQ_CMP_ERR;
1626 ahci_ata_put_xfer(xa);
1627 ahci_os_unlock_port(ap);
1629 ahci_os_lock_port(ap);
1633 * Completion function for ATA_PORT_T_DISK I/O
1637 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1639 union ccb *ccb = xa->atascsi_private;
1640 struct ccb_hdr *ccbh = &ccb->ccb_h;
1641 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1644 case ATA_S_COMPLETE:
1645 ccbh->status = CAM_REQ_CMP;
1646 ccb->csio.scsi_status = SCSI_STATUS_OK;
1649 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1650 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1651 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1652 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1655 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1656 ccbh->status = CAM_CMD_TIMEOUT;
1657 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1658 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1661 kprintf("%s: disk_rw: unknown state %d\n",
1662 ATANAME(ap, xa->at), xa->state);
1663 ccbh->status = CAM_REQ_CMP_ERR;
1666 ccb->csio.resid = xa->resid;
1667 ahci_ata_put_xfer(xa);
1668 ahci_os_unlock_port(ap);
1670 ahci_os_lock_port(ap);
1674 * Completion function for ATA_PORT_T_ATAPI I/O
1676 * Sense data is returned in the rfis.
1680 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1682 union ccb *ccb = xa->atascsi_private;
1683 struct ccb_hdr *ccbh = &ccb->ccb_h;
1684 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1687 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1688 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1691 case ATA_S_COMPLETE:
1692 ccbh->status = CAM_REQ_CMP;
1693 ccb->csio.scsi_status = SCSI_STATUS_OK;
1696 ccbh->status = CAM_SCSI_STATUS_ERROR;
1697 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1698 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1701 kprintf("%s: cmd %d: timeout\n",
1702 PORTNAME(ap), cdb->generic.opcode);
1703 ccbh->status = CAM_CMD_TIMEOUT;
1704 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1705 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1708 kprintf("%s: cmd %d: unknown state %d\n",
1709 PORTNAME(ap), cdb->generic.opcode, xa->state);
1710 ccbh->status = CAM_REQ_CMP_ERR;
1713 ccb->csio.resid = xa->resid;
1714 ahci_ata_put_xfer(xa);
1715 ahci_os_unlock_port(ap);
1717 ahci_os_lock_port(ap);
1721 * Construct dummy sense data for errors on DISKs
1725 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1727 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1728 sense_data->segment = 0;
1729 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1730 sense_data->info[0] = 0;
1731 sense_data->info[1] = 0;
1732 sense_data->info[2] = 0;
1733 sense_data->info[3] = 0;
1734 sense_data->extra_len = 0;
1738 * Construct atapi sense data for errors on ATAPI
1740 * The ATAPI sense data is stored in the passed rfis and must be converted
1741 * to SCSI sense data.
1745 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1746 struct scsi_sense_data *sense_data)
1748 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1749 sense_data->segment = 0;
1750 sense_data->flags = (rfis->error & 0xF0) >> 4;
1751 if (rfis->error & 0x04)
1752 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1753 if (rfis->error & 0x02)
1754 sense_data->flags |= SSD_EOM;
1755 if (rfis->error & 0x01)
1756 sense_data->flags |= SSD_ILI;
1757 sense_data->info[0] = 0;
1758 sense_data->info[1] = 0;
1759 sense_data->info[2] = 0;
1760 sense_data->info[3] = 0;
1761 sense_data->extra_len = 0;
1766 ahci_strip_string(const char **basep, int *lenp)
1768 const char *base = *basep;
1771 while (len && (*base == 0 || *base == ' ')) {
1775 while (len && (base[len-1] == 0 || base[len-1] == ' '))