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);
109 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
110 (void *)ap, unit, &ap->ap_sim_lock, 1, 1, devq);
111 cam_simq_release(devq);
116 ahci_os_unlock_port(ap);
117 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
118 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
119 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
120 ahci_os_lock_port(ap);
121 if (error != CAM_SUCCESS) {
125 ap->ap_flags |= AP_F_BUS_REGISTERED;
127 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
128 error = ahci_cam_probe(ap, NULL);
135 ap->ap_flags |= AP_F_CAM_ATTACHED;
141 * The state of the port has changed.
143 * If at is NULL the physical port has changed state.
144 * If at is non-NULL a particular target behind a PM has changed state.
146 * If found is -1 the target state must be queued to a non-interrupt context.
147 * (only works with at == NULL).
149 * If found is 0 the target was removed.
150 * If found is 1 the target was inserted.
153 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
155 struct cam_path *tmppath;
159 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
161 if (ap->ap_sim == NULL)
163 if (found == CAM_TARGET_WILDCARD) {
164 status = xpt_create_path(&tmppath, NULL,
165 cam_sim_path(ap->ap_sim),
166 target, CAM_LUN_WILDCARD);
167 if (status != CAM_REQ_CMP)
171 status = xpt_create_path(&tmppath, NULL,
172 cam_sim_path(ap->ap_sim),
175 if (status != CAM_REQ_CMP)
182 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
184 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
187 xpt_free_path(tmppath);
191 ahci_cam_detach(struct ahci_port *ap)
195 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
197 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
199 xpt_freeze_simq(ap->ap_sim, 1);
201 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
202 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
203 KKASSERT(error == CAM_REQ_CMP);
204 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
207 cam_sim_free(ap->ap_sim);
210 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
211 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
215 * Once the AHCI port has been attached we need to probe for a device or
216 * devices on the port and setup various options.
218 * If at is NULL we are probing the direct-attached device on the port,
219 * which may or may not be a port multiplier.
222 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
227 u_int64_t capacity_bytes;
234 const char *model_id;
235 const char *firmware_id;
236 const char *serial_id;
245 * Delayed CAM attachment for initial probe, sim may be NULL
247 if (ap->ap_sim == NULL)
251 * A NULL atx indicates a probe of the directly connected device.
252 * A non-NULL atx indicates a device connected via a port multiplier.
253 * We need to preserve atx for calls to ahci_ata_get_xfer().
255 * at is always non-NULL. For directly connected devices we supply
256 * an (at) pointing to target 0.
259 at = ap->ap_ata[0]; /* direct attached - device 0 */
260 if (ap->ap_type == ATA_PORT_T_PM) {
261 kprintf("%s: Found Port Multiplier\n",
265 at->at_type = ap->ap_type;
268 if (atx->at_type == ATA_PORT_T_PM) {
269 kprintf("%s: Bogus device, reducing port count to %d\n",
270 ATANAME(ap, atx), atx->at_target);
271 if (ap->ap_pmcount > atx->at_target)
272 ap->ap_pmcount = atx->at_target;
276 if (ap->ap_type == ATA_PORT_T_NONE)
278 if (at->at_type == ATA_PORT_T_NONE)
282 * Issue identify, saving the result
284 xa = ahci_ata_get_xfer(ap, atx);
285 xa->complete = ahci_ata_dummy_done;
286 xa->data = &at->at_identify;
287 xa->datalen = sizeof(at->at_identify);
288 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
289 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
291 switch(at->at_type) {
292 case ATA_PORT_T_DISK:
293 xa->fis->command = ATA_C_IDENTIFY;
296 case ATA_PORT_T_ATAPI:
297 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
298 xa->flags |= ATA_F_AUTOSENSE;
302 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
303 type = "UNKNOWN(ATAPI?)";
306 xa->fis->features = 0;
310 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
311 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
312 ATANAME(ap, atx), type);
313 ahci_ata_put_xfer(xa);
316 ahci_ata_put_xfer(xa);
318 ata_fix_identify(&at->at_identify);
321 * Read capacity using SATA probe info.
323 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
324 /* LBA48 feature set supported */
326 for (i = 3; i >= 0; --i) {
329 le16toh(at->at_identify.addrsecxt[i]);
332 capacity = le16toh(at->at_identify.addrsec[1]);
334 capacity += le16toh(at->at_identify.addrsec[0]);
337 capacity = 1024 * 1024 / 512;
338 at->at_capacity = capacity;
340 ap->ap_probe = ATA_PROBE_GOOD;
342 capacity_bytes = capacity * 512;
345 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
346 * number of slots and limit the number of CAM ccb's to one less
347 * so we always have a slot available for recovery.
349 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
350 * not support it, and in that case the driver can handle extra
353 * NCQ is currently used only with direct-attached disks. It is
354 * not used with port multipliers or direct-attached ATAPI devices.
356 * Remember at least one extra CCB needs to be reserved for the
359 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
360 ap->ap_type == ATA_PORT_T_DISK &&
361 (le16toh(at->at_identify.satacap) & (1 << 8))) {
362 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
363 devncqdepth = at->at_ncqdepth;
364 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
365 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
366 if (at->at_ncqdepth > 1) {
367 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
368 xa = ahci_ata_get_xfer(ap, atx);
369 if (xa->tag < at->at_ncqdepth) {
370 xa->state = ATA_S_COMPLETE;
371 ahci_ata_put_xfer(xa);
374 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
375 cam_devq_resize(ap->ap_sim->devq,
376 at->at_ncqdepth - 1);
383 model_len = sizeof(at->at_identify.model);
384 model_id = at->at_identify.model;
385 ahci_strip_string(&model_id, &model_len);
387 firmware_len = sizeof(at->at_identify.firmware);
388 firmware_id = at->at_identify.firmware;
389 ahci_strip_string(&firmware_id, &firmware_len);
391 serial_len = sizeof(at->at_identify.serial);
392 serial_id = at->at_identify.serial;
393 ahci_strip_string(&serial_id, &serial_len);
396 * Generate informatiive strings.
398 * NOTE: We do not automatically set write caching, lookahead,
399 * or the security state for ATAPI devices.
401 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
402 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
404 else if (at->at_type == ATA_PORT_T_ATAPI)
412 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
413 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
415 else if (at->at_type == ATA_PORT_T_ATAPI)
423 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
424 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
426 else if (at->at_type == ATA_PORT_T_ATAPI)
428 else if (AhciNoFeatures & (1 << ap->ap_num))
429 scstr = "<disabled>";
436 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
437 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
438 "capacity=%lld.%02dMB\n",
442 model_len, model_len, model_id,
443 firmware_len, firmware_len, firmware_id,
444 serial_len, serial_len, serial_id,
447 devncqdepth, ap->ap_sc->sc_ncmds,
448 at->at_identify.satacap,
449 at->at_identify.satafsup,
450 (at->at_ncqdepth > 1 ? "YES" : "NO"),
451 (long long)capacity_bytes / (1024 * 1024),
452 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
454 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
456 at->at_identify.features85,
457 at->at_identify.features86,
458 at->at_identify.features87,
465 * Additional type-specific probing
467 switch(at->at_type) {
468 case ATA_PORT_T_DISK:
469 error = ahci_cam_probe_disk(ap, atx);
471 case ATA_PORT_T_ATAPI:
472 error = ahci_cam_probe_atapi(ap, atx);
480 at->at_probe = ATA_PROBE_FAILED;
482 ap->ap_probe = at->at_probe;
484 at->at_probe = ATA_PROBE_GOOD;
486 ap->ap_probe = at->at_probe;
492 * DISK-specific probe after initial ident
495 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
500 at = atx ? atx : ap->ap_ata[0];
503 * Set dummy xfer mode
505 ahci_set_xfer(ap, atx);
508 * Enable write cache if supported
510 * NOTE: "WD My Book" external disk devices have a very poor
511 * daughter board between the the ESATA and the HD. Sending
512 * any ATA_C_SET_FEATURES commands will break the hardware port
513 * with a fatal protocol error. However, this device also
514 * indicates that WRITECACHE is already on and READAHEAD is
515 * not supported so we avoid the issue.
517 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
518 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
519 xa = ahci_ata_get_xfer(ap, atx);
520 xa->complete = ahci_ata_dummy_done;
521 xa->fis->command = ATA_C_SET_FEATURES;
522 xa->fis->features = ATA_SF_WRITECACHE_EN;
523 /* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */
524 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
526 xa->flags = ATA_F_PIO | ATA_F_POLL;
529 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
530 at->at_features |= ATA_PORT_F_WCACHE;
532 kprintf("%s: Unable to enable write-caching\n",
534 ahci_ata_put_xfer(xa);
538 * Enable readahead if supported
540 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
541 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
542 xa = ahci_ata_get_xfer(ap, atx);
543 xa->complete = ahci_ata_dummy_done;
544 xa->fis->command = ATA_C_SET_FEATURES;
545 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
546 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
548 xa->flags = ATA_F_PIO | ATA_F_POLL;
551 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
552 at->at_features |= ATA_PORT_F_RAHEAD;
554 kprintf("%s: Unable to enable read-ahead\n",
556 ahci_ata_put_xfer(xa);
560 * FREEZE LOCK the device so malicious users can't lock it on us.
561 * As there is no harm in issuing this to devices that don't
562 * support the security feature set we just send it, and don't bother
563 * checking if the device sends a command abort to tell us it doesn't
566 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
567 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
568 (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
569 xa = ahci_ata_get_xfer(ap, atx);
570 xa->complete = ahci_ata_dummy_done;
571 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
572 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
573 xa->flags = ATA_F_PIO | ATA_F_POLL;
576 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
577 at->at_features |= ATA_PORT_F_FRZLCK;
579 kprintf("%s: Unable to set security freeze\n",
581 ahci_ata_put_xfer(xa);
588 * ATAPI-specific probe after initial ident
591 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
593 ahci_set_xfer(ap, atx);
598 * Setting the transfer mode is irrelevant for the SATA transport
599 * but some (atapi) devices seem to need it anyway. In addition
600 * if we are running through a SATA->PATA converter for some reason
601 * beyond my comprehension we might have to set the mode.
603 * We only support DMA modes for SATA attached devices, so don't bother
607 ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx)
614 at = atx ? atx : ap->ap_ata[0];
617 * Figure out the supported UDMA mode. Ignore other legacy modes.
619 mask = le16toh(at->at_identify.ultradma);
620 if ((mask & 0xFF) == 0 || mask == 0xFFFF)
624 while ((mask & 0x8000) == 0) {
630 * SATA atapi devices often still report a dma mode, even though
631 * it is irrelevant for SATA transport. It is also possible that
632 * we are running through a SATA->PATA converter and seeing the
635 * In this case the device may require a (dummy) SETXFER to be
636 * sent before it will work properly.
638 xa = ahci_ata_get_xfer(ap, atx);
639 xa->complete = ahci_ata_dummy_done;
640 xa->fis->command = ATA_C_SET_FEATURES;
641 xa->fis->features = ATA_SF_SETXFER;
642 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
643 xa->fis->sector_count = mode;
644 xa->flags = ATA_F_PIO | ATA_F_POLL;
647 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
648 kprintf("%s: Unable to set dummy xfer mode \n",
650 } else if (bootverbose) {
651 kprintf("%s: Set dummy xfer mode to %02x\n",
652 ATANAME(ap, atx), mode);
654 ahci_ata_put_xfer(xa);
659 * Fix byte ordering so buffers can be accessed as
663 ata_fix_identify(struct ata_identify *id)
668 swap = (u_int16_t *)id->serial;
669 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
670 swap[i] = bswap16(swap[i]);
672 swap = (u_int16_t *)id->firmware;
673 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
674 swap[i] = bswap16(swap[i]);
676 swap = (u_int16_t *)id->model;
677 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
678 swap[i] = bswap16(swap[i]);
682 * Dummy done callback for xa.
685 ahci_ata_dummy_done(struct ata_xfer *xa)
690 * Use an engineering request to initiate a target scan for devices
691 * behind a port multiplier.
693 * An asynchronous bus scan is used to avoid reentrancy issues.
696 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
698 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
700 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
701 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
702 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
703 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
706 ap->ap_flags |= AP_F_SCAN_COMPLETED;
707 wakeup(&ap->ap_flags);
713 ahci_cam_rescan(struct ahci_port *ap)
715 struct cam_path *path;
720 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
721 ap->ap_flags |= AP_F_SCAN_REQUESTED;
724 ap->ap_flags |= AP_F_SCAN_RUNNING;
725 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
726 ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN;
729 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
730 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
731 if (status != CAM_REQ_CMP)
734 ccb = xpt_alloc_ccb();
735 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
736 ccb->ccb_h.func_code = XPT_ENG_EXEC;
737 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
738 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
739 ccb->crcn.flags = CAM_FLAG_NONE;
740 xpt_action_async(ccb);
744 ahci_xpt_rescan(struct ahci_port *ap)
746 struct cam_path *path;
750 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
751 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
752 if (status != CAM_REQ_CMP)
755 ccb = xpt_alloc_ccb();
756 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
757 ccb->ccb_h.func_code = XPT_SCAN_BUS;
758 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
759 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
760 ccb->crcn.flags = CAM_FLAG_NONE;
761 xpt_action_async(ccb);
765 * Action function - dispatch command
769 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
771 struct ahci_port *ap;
772 struct ata_port *at, *atx;
773 struct ccb_hdr *ccbh;
777 ap = cam_sim_softc(sim);
779 KKASSERT(ap != NULL);
781 unit = cam_sim_unit(sim);
784 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
785 * otherwise the bus rescan will not remove the dead devices when
788 * For non-wildcards we have one target (0) and one lun (0),
789 * unless we have a port multiplier.
791 * A wildcard target indicates only the general bus is being
794 * Calculate at and atx. at is always non-NULL. atx is only
795 * non-NULL for direct-attached devices. It will be NULL for
796 * devices behind a port multiplier.
798 * XXX What do we do with a LUN wildcard?
800 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
801 ccbh->func_code != XPT_PATH_INQ) {
802 if (ap->ap_type == ATA_PORT_T_NONE) {
803 ccbh->status = CAM_DEV_NOT_THERE;
807 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
808 ccbh->status = CAM_DEV_NOT_THERE;
812 at = ap->ap_ata[ccbh->target_id];
813 if (ap->ap_type == ATA_PORT_T_PM)
816 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
817 ccbh->status = CAM_DEV_NOT_THERE;
826 * Switch on the meta XPT command
828 switch(ccbh->func_code) {
831 * This routine is called after a port multiplier has been
834 ccbh->status = CAM_REQ_CMP;
835 ahci_os_lock_port(ap);
836 ahci_port_state_machine(ap, 0);
837 ahci_os_unlock_port(ap);
843 * This command always succeeds, otherwise the bus scan
844 * will not detach dead devices.
846 ccb->cpi.version_num = 1;
847 ccb->cpi.hba_inquiry = 0;
848 ccb->cpi.target_sprt = 0;
849 ccb->cpi.hba_misc = PIM_SEQSCAN;
850 ccb->cpi.hba_eng_cnt = 0;
851 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
852 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
853 ccb->cpi.max_lun = 0;
854 ccb->cpi.async_flags = 0;
855 ccb->cpi.hpath_id = 0;
856 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
857 ccb->cpi.unit_number = cam_sim_unit(sim);
858 ccb->cpi.bus_id = cam_sim_bus(sim);
859 ccb->cpi.base_transfer_speed = 150000;
860 ccb->cpi.transport = XPORT_SATA;
861 ccb->cpi.transport_version = 1;
862 ccb->cpi.protocol = PROTO_SCSI;
863 ccb->cpi.protocol_version = SCSI_REV_2;
865 ccbh->status = CAM_REQ_CMP;
866 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
867 ahci_os_lock_port(ap);
868 ahci_port_state_machine(ap, 0);
869 ahci_os_unlock_port(ap);
871 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
872 AHCI_PREG_SSTS_SPD) {
873 case AHCI_PREG_SSTS_SPD_GEN1:
874 ccb->cpi.base_transfer_speed = 150000;
876 case AHCI_PREG_SSTS_SPD_GEN2:
877 ccb->cpi.base_transfer_speed = 300000;
881 ccb->cpi.base_transfer_speed = 1000;
885 if (ap->ap_type == ATA_PORT_T_NONE)
886 ccbh->status = CAM_DEV_NOT_THERE;
892 ahci_os_lock_port(ap);
893 if (ap->ap_type == ATA_PORT_T_NONE) {
894 ccbh->status = CAM_DEV_NOT_THERE;
896 ahci_port_reset(ap, atx, 0);
897 ccbh->status = CAM_REQ_CMP;
899 ahci_os_unlock_port(ap);
903 ahci_os_lock_port(ap);
904 ahci_port_reset(ap, NULL, 1);
905 ahci_os_unlock_port(ap);
906 ccbh->status = CAM_REQ_CMP;
909 case XPT_SET_TRAN_SETTINGS:
910 ccbh->status = CAM_FUNC_NOTAVAIL;
913 case XPT_GET_TRAN_SETTINGS:
914 ccb->cts.protocol = PROTO_SCSI;
915 ccb->cts.protocol_version = SCSI_REV_2;
916 ccb->cts.transport = XPORT_SATA;
917 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
918 ccb->cts.proto_specific.valid = 0;
919 ccb->cts.xport_specific.valid = 0;
920 ccbh->status = CAM_REQ_CMP;
923 case XPT_CALC_GEOMETRY:
924 cam_calc_geometry(&ccb->ccg, 1);
929 * Our parallel startup code might have only probed through
930 * to the IDENT, so do the last step if necessary.
932 if (at->at_probe == ATA_PROBE_NEED_IDENT)
933 ahci_cam_probe(ap, atx);
934 if (at->at_probe != ATA_PROBE_GOOD) {
935 ccbh->status = CAM_DEV_NOT_THERE;
939 switch(at->at_type) {
940 case ATA_PORT_T_DISK:
941 ahci_xpt_scsi_disk_io(ap, atx, ccb);
943 case ATA_PORT_T_ATAPI:
944 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
947 ccbh->status = CAM_REQ_INVALID;
953 ccbh->status = CAM_REQ_INVALID;
962 * Generally this function gets called heavily when interrupts might be
963 * non-operational, during a halt/reboot or panic.
967 ahci_xpt_poll(struct cam_sim *sim)
969 struct ahci_port *ap;
971 ap = cam_sim_softc(sim);
973 ahci_os_lock_port(ap);
974 ahci_port_intr(ap, 1);
975 ahci_os_unlock_port(ap);
980 * Convert the SCSI command in ccb to an ata_xfer command in xa
981 * for ATA_PORT_T_DISK operations. Set the completion function
982 * to convert the response back, then dispatch to the OpenBSD AHCI
985 * AHCI DISK commands only support a limited command set, and we
986 * fake additional commands to make it play nice with the CAM subsystem.
990 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
993 struct ccb_hdr *ccbh;
994 struct ccb_scsiio *csio;
997 struct ata_fis_h2d *fis;
998 struct ata_pass_12 *atp12;
999 struct ata_pass_16 *atp16;
1001 union scsi_data *rdata;
1007 ccbh = &ccb->csio.ccb_h;
1009 at = atx ? atx : ap->ap_ata[0];
1012 * XXX not passing NULL at for direct attach!
1014 xa = ahci_ata_get_xfer(ap, atx);
1015 rdata = (void *)csio->data_ptr;
1016 rdata_len = csio->dxfer_len;
1019 * Build the FIS or process the csio to completion.
1021 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1022 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1024 switch(cdb->generic.opcode) {
1027 * Auto-sense everything, so explicit sense requests
1030 ccbh->status = CAM_SCSI_STATUS_ERROR;
1034 * Inquiry supported features
1036 * [opcode, byte2, page_code, length, control]
1038 if (cdb->inquiry.byte2 & SI_EVPD) {
1039 ahci_xpt_page_inquiry(ap, at, ccb);
1041 bzero(rdata, rdata_len);
1042 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1043 ccbh->status = CAM_CCB_LEN_ERR;
1046 if (rdata_len > sizeof(rdata->inquiry_data))
1047 rdata_len = sizeof(rdata->inquiry_data);
1048 rdata->inquiry_data.device = T_DIRECT;
1049 rdata->inquiry_data.version = SCSI_REV_SPC2;
1050 rdata->inquiry_data.response_format = 2;
1051 rdata->inquiry_data.additional_length = 32;
1052 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1053 bcopy(at->at_identify.model,
1054 rdata->inquiry_data.product,
1055 sizeof(rdata->inquiry_data.product));
1056 bcopy(at->at_identify.firmware,
1057 rdata->inquiry_data.revision,
1058 sizeof(rdata->inquiry_data.revision));
1059 ccbh->status = CAM_REQ_CMP;
1062 case READ_CAPACITY_16:
1063 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1064 ccbh->status = CAM_REQ_INVALID;
1067 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1068 ccbh->status = CAM_CCB_LEN_ERR;
1073 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1074 ccbh->status = CAM_CCB_LEN_ERR;
1078 capacity = at->at_capacity;
1080 bzero(rdata, rdata_len);
1081 if (cdb->generic.opcode == READ_CAPACITY) {
1082 rdata_len = sizeof(rdata->read_capacity_data);
1083 if (capacity > 0xFFFFFFFFU)
1084 capacity = 0xFFFFFFFFU;
1085 bzero(&rdata->read_capacity_data, rdata_len);
1086 scsi_ulto4b((u_int32_t)capacity - 1,
1087 rdata->read_capacity_data.addr);
1088 scsi_ulto4b(512, rdata->read_capacity_data.length);
1090 rdata_len = sizeof(rdata->read_capacity_data_16);
1091 bzero(&rdata->read_capacity_data_16, rdata_len);
1092 scsi_u64to8b(capacity - 1,
1093 rdata->read_capacity_data_16.addr);
1094 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1096 ccbh->status = CAM_REQ_CMP;
1098 case SYNCHRONIZE_CACHE:
1100 * Synchronize cache. Specification says this can take
1101 * greater then 30 seconds so give it at least 45.
1104 fis->flags = ATA_H2D_FLAGS_CMD;
1105 fis->command = ATA_C_FLUSH_CACHE;
1107 if (xa->timeout < 45000)
1108 xa->timeout = 45000;
1111 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1113 case TEST_UNIT_READY:
1114 case START_STOP_UNIT:
1117 * Just silently return success
1119 ccbh->status = CAM_REQ_CMP;
1123 atp12 = &cdb->ata_pass_12;
1126 * Figure out the flags to be used, depending on the direction of the
1129 switch (ccbh->flags & CAM_DIR_MASK) {
1131 xa->flags = ATA_F_READ;
1134 xa->flags = ATA_F_WRITE;
1139 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1140 xa->data = csio->data_ptr;
1141 xa->datalen = csio->dxfer_len;
1142 xa->complete = ahci_ata_complete_disk_rw;
1143 xa->timeout = ccbh->timeout;
1146 * Populate the fis from the information we received through CAM
1149 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */
1150 fis->features = atp12->features;
1151 fis->sector_count = atp12->sector_count;
1152 fis->lba_low = atp12->lba_low;
1153 fis->lba_mid = atp12->lba_mid;
1154 fis->lba_high = atp12->lba_high;
1155 fis->device = atp12->device; /* maybe always 0? */
1156 fis->command = atp12->command;
1157 fis->control = atp12->control;
1160 * Mark as in progress so it is sent to the device.
1162 ccbh->status = CAM_REQ_INPROG;
1165 atp16 = &cdb->ata_pass_16;
1168 * Figure out the flags to be used, depending on the direction of the
1171 switch (ccbh->flags & CAM_DIR_MASK) {
1173 xa->flags = ATA_F_READ;
1176 xa->flags = ATA_F_WRITE;
1181 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1182 xa->data = csio->data_ptr;
1183 xa->datalen = csio->dxfer_len;
1184 xa->complete = ahci_ata_complete_disk_rw;
1185 xa->timeout = ccbh->timeout;
1188 * Populate the fis from the information we received through CAM
1191 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */
1192 fis->features = atp16->features;
1193 fis->features_exp = atp16->features_ext;
1194 fis->sector_count = atp16->sector_count;
1195 fis->sector_count_exp = atp16->sector_count_ext;
1196 fis->lba_low = atp16->lba_low;
1197 fis->lba_low_exp = atp16->lba_low_ext;
1198 fis->lba_mid = atp16->lba_mid;
1199 fis->lba_mid_exp = atp16->lba_mid_ext;
1200 fis->lba_high = atp16->lba_high;
1201 fis->lba_mid_exp = atp16->lba_mid_ext;
1202 fis->device = atp16->device; /* maybe always 0? */
1203 fis->command = atp16->command;
1206 * Mark as in progress so it is sent to the device.
1208 ccbh->status = CAM_REQ_INPROG;
1211 switch(cdb->generic.opcode) {
1213 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1214 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1215 xa->flags = ATA_F_READ;
1218 lba = scsi_4btoul(cdb->rw_10.addr);
1219 count = scsi_2btoul(cdb->rw_10.length);
1220 xa->flags = ATA_F_READ;
1223 lba = scsi_4btoul(cdb->rw_12.addr);
1224 count = scsi_4btoul(cdb->rw_12.length);
1225 xa->flags = ATA_F_READ;
1228 lba = scsi_8btou64(cdb->rw_16.addr);
1229 count = scsi_4btoul(cdb->rw_16.length);
1230 xa->flags = ATA_F_READ;
1233 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1234 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1235 xa->flags = ATA_F_WRITE;
1238 lba = scsi_4btoul(cdb->rw_10.addr);
1239 count = scsi_2btoul(cdb->rw_10.length);
1240 xa->flags = ATA_F_WRITE;
1243 lba = scsi_4btoul(cdb->rw_12.addr);
1244 count = scsi_4btoul(cdb->rw_12.length);
1245 xa->flags = ATA_F_WRITE;
1248 lba = scsi_8btou64(cdb->rw_16.addr);
1249 count = scsi_4btoul(cdb->rw_16.length);
1250 xa->flags = ATA_F_WRITE;
1253 ccbh->status = CAM_REQ_INVALID;
1256 if (ccbh->status != CAM_REQ_INPROG)
1260 fis->flags = ATA_H2D_FLAGS_CMD;
1261 fis->lba_low = (u_int8_t)lba;
1262 fis->lba_mid = (u_int8_t)(lba >> 8);
1263 fis->lba_high = (u_int8_t)(lba >> 16);
1264 fis->device = ATA_H2D_DEVICE_LBA;
1267 * NCQ only for direct-attached disks, do not currently
1268 * try to use NCQ with port multipliers.
1270 if (at->at_ncqdepth > 1 &&
1271 ap->ap_type == ATA_PORT_T_DISK &&
1272 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1273 (ccbh->flags & CAM_POLLED) == 0) {
1275 * Use NCQ - always uses 48 bit addressing
1277 xa->flags |= ATA_F_NCQ;
1278 fis->command = (xa->flags & ATA_F_WRITE) ?
1279 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1280 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1281 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1282 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1283 fis->sector_count = xa->tag << 3;
1284 fis->features = (u_int8_t)count;
1285 fis->features_exp = (u_int8_t)(count >> 8);
1286 } else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1290 fis->command = (xa->flags & ATA_F_WRITE) ?
1291 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1292 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1293 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1294 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1295 fis->sector_count = (u_int8_t)count;
1296 fis->sector_count_exp = (u_int8_t)(count >> 8);
1301 * NOTE: 256 sectors is supported, stored as 0.
1303 fis->command = (xa->flags & ATA_F_WRITE) ?
1304 ATA_C_WRITEDMA : ATA_C_READDMA;
1305 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1306 fis->sector_count = (u_int8_t)count;
1309 xa->data = csio->data_ptr;
1310 xa->datalen = csio->dxfer_len;
1311 xa->complete = ahci_ata_complete_disk_rw;
1312 xa->timeout = ccbh->timeout; /* milliseconds */
1314 if (xa->timeout > 10000) /* XXX - debug */
1315 xa->timeout = 10000;
1317 if (ccbh->flags & CAM_POLLED)
1318 xa->flags |= ATA_F_POLL;
1323 * If the request is still in progress the xa and FIS have
1324 * been set up (except for the PM target), and must be dispatched.
1325 * Otherwise the request was completed.
1327 if (ccbh->status == CAM_REQ_INPROG) {
1328 KKASSERT(xa->complete != NULL);
1329 xa->atascsi_private = ccb;
1330 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1331 ahci_os_lock_port(ap);
1332 xa->fis->flags |= at->at_target;
1334 ahci_os_unlock_port(ap);
1336 ahci_ata_put_xfer(xa);
1342 * Convert the SCSI command in ccb to an ata_xfer command in xa
1343 * for ATA_PORT_T_ATAPI operations. Set the completion function
1344 * to convert the response back, then dispatch to the OpenBSD AHCI
1349 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1352 struct ccb_hdr *ccbh;
1353 struct ccb_scsiio *csio;
1354 struct ata_xfer *xa;
1355 struct ata_fis_h2d *fis;
1359 struct ata_port *at;
1361 ccbh = &ccb->csio.ccb_h;
1363 at = atx ? atx : ap->ap_ata[0];
1365 switch (ccbh->flags & CAM_DIR_MASK) {
1367 flags = ATA_F_PACKET | ATA_F_READ;
1370 flags = ATA_F_PACKET | ATA_F_WRITE;
1373 flags = ATA_F_PACKET;
1376 ccbh->status = CAM_REQ_INVALID;
1383 * Special handling to get the rfis back into host memory while
1384 * still allowing the chip to run commands in parallel to
1385 * ATAPI devices behind a PM.
1387 flags |= ATA_F_AUTOSENSE;
1390 * The command has to fit in the packet command buffer.
1392 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1393 ccbh->status = CAM_CCB_LEN_ERR;
1399 * Initialize the XA and FIS. It is unclear how much of
1400 * this has to mimic the equivalent ATA command.
1402 * XXX not passing NULL at for direct attach!
1404 xa = ahci_ata_get_xfer(ap, atx);
1407 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1408 fis->command = ATA_C_PACKET;
1409 fis->device = ATA_H2D_DEVICE_LBA;
1410 fis->sector_count = xa->tag << 3;
1411 if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1412 if (flags & ATA_F_WRITE) {
1413 fis->features = ATA_H2D_FEATURES_DMA |
1414 ATA_H2D_FEATURES_DIR_WRITE;
1416 fis->features = ATA_H2D_FEATURES_DMA |
1417 ATA_H2D_FEATURES_DIR_READ;
1423 fis->control = ATA_FIS_CONTROL_4BIT;
1426 xa->data = csio->data_ptr;
1427 xa->datalen = csio->dxfer_len;
1428 xa->timeout = ccbh->timeout; /* milliseconds */
1430 if (ccbh->flags & CAM_POLLED)
1431 xa->flags |= ATA_F_POLL;
1434 * Copy the cdb to the packetcmd buffer in the FIS using a
1435 * convenient pointer in the xa.
1437 * Zero-out any trailing bytes in case the ATAPI device cares.
1439 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1440 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1441 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1442 if (csio->cdb_len < 16)
1443 bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len);
1446 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1447 cdbs->generic.opcode,
1448 csio->cdb_len, csio->dxfer_len);
1452 * Some ATAPI commands do not actually follow the SCSI standard.
1454 cdbd = (void *)xa->packetcmd;
1456 switch(cdbd->generic.opcode) {
1459 * Force SENSE requests to the ATAPI sense length.
1461 * It is unclear if this is needed or not.
1463 if (cdbd->sense.length == SSD_FULL_SIZE) {
1465 kprintf("%s: Shortening sense request\n",
1468 cdbd->sense.length = offsetof(struct scsi_sense_data,
1474 * Some ATAPI devices can't handle long inquiry lengths,
1475 * don't ask me why. Truncate the inquiry length.
1477 if (cdbd->inquiry.page_code == 0 &&
1478 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1479 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1485 * Convert *_6 to *_10 commands. Most ATAPI devices
1486 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1488 cdbd->rw_10.opcode |= 0x20;
1489 cdbd->rw_10.byte2 = 0;
1490 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1491 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1492 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1493 cdbd->rw_10.addr[3] = 0;
1494 cdbd->rw_10.reserved = 0;
1495 cdbd->rw_10.length[0] = 0;
1496 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1497 cdbd->rw_10.control = cdbs->rw_6.control;
1506 xa->complete = ahci_atapi_complete_cmd;
1507 xa->atascsi_private = ccb;
1508 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1509 ahci_os_lock_port(ap);
1511 ahci_os_unlock_port(ap);
1515 * Simulate page inquiries for disk attachments.
1519 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
1522 struct scsi_vpd_supported_page_list list;
1523 struct scsi_vpd_unit_serial_number serno;
1524 struct scsi_vpd_unit_devid devid;
1532 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1534 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1535 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1537 switch(cdb->inquiry.page_code) {
1538 case SVPD_SUPPORTED_PAGE_LIST:
1540 page->list.device = T_DIRECT;
1541 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1542 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1543 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1544 page->list.list[i++] = SVPD_UNIT_DEVID;
1545 page->list.length = i;
1546 len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1548 case SVPD_UNIT_SERIAL_NUMBER:
1550 j = sizeof(at->at_identify.serial);
1551 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1553 while (j > i && at->at_identify.serial[j-1] == ' ')
1555 page->serno.device = T_DIRECT;
1556 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1557 page->serno.length = j - i;
1558 bcopy(at->at_identify.serial + i,
1559 page->serno.serial_num, j - i);
1560 len = offsetof(struct scsi_vpd_unit_serial_number,
1563 case SVPD_UNIT_DEVID:
1564 /* fall through for now */
1566 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1570 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1571 if (len <= ccb->csio.dxfer_len) {
1572 ccb->ccb_h.status = CAM_REQ_CMP;
1573 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1574 bcopy(page, ccb->csio.data_ptr, len);
1575 ccb->csio.resid = ccb->csio.dxfer_len - len;
1577 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1580 kfree(page, M_DEVBUF);
1584 * Completion function for ATA_PORT_T_DISK cache synchronization.
1588 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1590 union ccb *ccb = xa->atascsi_private;
1591 struct ccb_hdr *ccbh = &ccb->ccb_h;
1592 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1595 case ATA_S_COMPLETE:
1596 ccbh->status = CAM_REQ_CMP;
1597 ccb->csio.scsi_status = SCSI_STATUS_OK;
1600 kprintf("%s: synchronize_cache: error\n",
1601 ATANAME(ap, xa->at));
1602 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1603 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1604 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1607 kprintf("%s: synchronize_cache: timeout\n",
1608 ATANAME(ap, xa->at));
1609 ccbh->status = CAM_CMD_TIMEOUT;
1612 kprintf("%s: synchronize_cache: unknown state %d\n",
1613 ATANAME(ap, xa->at), xa->state);
1614 ccbh->status = CAM_REQ_CMP_ERR;
1617 ahci_ata_put_xfer(xa);
1618 ahci_os_unlock_port(ap);
1620 ahci_os_lock_port(ap);
1624 * Completion function for ATA_PORT_T_DISK I/O
1628 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1630 union ccb *ccb = xa->atascsi_private;
1631 struct ccb_hdr *ccbh = &ccb->ccb_h;
1632 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1635 case ATA_S_COMPLETE:
1636 ccbh->status = CAM_REQ_CMP;
1637 ccb->csio.scsi_status = SCSI_STATUS_OK;
1640 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1641 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1642 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1643 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1646 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1647 ccbh->status = CAM_CMD_TIMEOUT;
1648 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1649 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1652 kprintf("%s: disk_rw: unknown state %d\n",
1653 ATANAME(ap, xa->at), xa->state);
1654 ccbh->status = CAM_REQ_CMP_ERR;
1657 ccb->csio.resid = xa->resid;
1658 ahci_ata_put_xfer(xa);
1659 ahci_os_unlock_port(ap);
1661 ahci_os_lock_port(ap);
1665 * Completion function for ATA_PORT_T_ATAPI I/O
1667 * Sense data is returned in the rfis.
1671 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1673 union ccb *ccb = xa->atascsi_private;
1674 struct ccb_hdr *ccbh = &ccb->ccb_h;
1675 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1678 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1679 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1682 case ATA_S_COMPLETE:
1683 ccbh->status = CAM_REQ_CMP;
1684 ccb->csio.scsi_status = SCSI_STATUS_OK;
1687 ccbh->status = CAM_SCSI_STATUS_ERROR;
1688 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1689 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1692 kprintf("%s: cmd %d: timeout\n",
1693 PORTNAME(ap), cdb->generic.opcode);
1694 ccbh->status = CAM_CMD_TIMEOUT;
1695 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1696 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1699 kprintf("%s: cmd %d: unknown state %d\n",
1700 PORTNAME(ap), cdb->generic.opcode, xa->state);
1701 ccbh->status = CAM_REQ_CMP_ERR;
1704 ccb->csio.resid = xa->resid;
1705 ahci_ata_put_xfer(xa);
1706 ahci_os_unlock_port(ap);
1708 ahci_os_lock_port(ap);
1712 * Construct dummy sense data for errors on DISKs
1716 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1718 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1719 sense_data->segment = 0;
1720 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1721 sense_data->info[0] = 0;
1722 sense_data->info[1] = 0;
1723 sense_data->info[2] = 0;
1724 sense_data->info[3] = 0;
1725 sense_data->extra_len = 0;
1729 * Construct atapi sense data for errors on ATAPI
1731 * The ATAPI sense data is stored in the passed rfis and must be converted
1732 * to SCSI sense data.
1736 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1737 struct scsi_sense_data *sense_data)
1739 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1740 sense_data->segment = 0;
1741 sense_data->flags = (rfis->error & 0xF0) >> 4;
1742 if (rfis->error & 0x04)
1743 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1744 if (rfis->error & 0x02)
1745 sense_data->flags |= SSD_EOM;
1746 if (rfis->error & 0x01)
1747 sense_data->flags |= SSD_ILI;
1748 sense_data->info[0] = 0;
1749 sense_data->info[1] = 0;
1750 sense_data->info[2] = 0;
1751 sense_data->info[3] = 0;
1752 sense_data->extra_len = 0;
1757 ahci_strip_string(const char **basep, int *lenp)
1759 const char *base = *basep;
1762 while (len && (*base == 0 || *base == ' ')) {
1766 while (len && (base[len-1] == 0 || base[len-1] == ' '))