xref: /dflybsd-src/sys/dev/disk/ahci/ahci_cam.c (revision d15a49f2d55e581da5b9f1a62329f85b726c015e)
1258223a3SMatthew Dillon /*
2258223a3SMatthew Dillon  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3258223a3SMatthew Dillon  *
4258223a3SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5258223a3SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6258223a3SMatthew Dillon  *
7258223a3SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8258223a3SMatthew Dillon  * modification, are permitted provided that the following conditions
9258223a3SMatthew Dillon  * are met:
10258223a3SMatthew Dillon  *
11258223a3SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13258223a3SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14258223a3SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15258223a3SMatthew Dillon  *    the documentation and/or other materials provided with the
16258223a3SMatthew Dillon  *    distribution.
17258223a3SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18258223a3SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19258223a3SMatthew Dillon  *    from this software without specific, prior written permission.
20258223a3SMatthew Dillon  *
21258223a3SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22258223a3SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23258223a3SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24258223a3SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25258223a3SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26258223a3SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27258223a3SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28258223a3SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29258223a3SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30258223a3SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31258223a3SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32258223a3SMatthew Dillon  * SUCH DAMAGE.
33258223a3SMatthew Dillon  *
34258223a3SMatthew Dillon  *
35258223a3SMatthew Dillon  * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
36258223a3SMatthew Dillon  *
37258223a3SMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
38258223a3SMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
39258223a3SMatthew Dillon  * copyright notice and this permission notice appear in all copies.
40258223a3SMatthew Dillon  *
41258223a3SMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42258223a3SMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43258223a3SMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44258223a3SMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45258223a3SMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46258223a3SMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47258223a3SMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48258223a3SMatthew Dillon  *
49258223a3SMatthew Dillon  * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
50258223a3SMatthew Dillon  * $DragonFly$
51258223a3SMatthew Dillon  */
52258223a3SMatthew Dillon /*
53258223a3SMatthew Dillon  * Implement each SATA port as its own SCSI bus on CAM.  This way we can
54258223a3SMatthew Dillon  * implement future port multiplier features as individual devices on the
55258223a3SMatthew Dillon  * bus.
56258223a3SMatthew Dillon  *
57258223a3SMatthew Dillon  * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
58258223a3SMatthew Dillon  * was written natively for DragonFly.
59258223a3SMatthew Dillon  */
60258223a3SMatthew Dillon 
61258223a3SMatthew Dillon #include "ahci.h"
62258223a3SMatthew Dillon 
63258223a3SMatthew Dillon static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
64258223a3SMatthew Dillon static void ahci_xpt_poll(struct cam_sim *sim);
651980eff3SMatthew Dillon static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
661980eff3SMatthew Dillon 			struct ata_port *at, union ccb *ccb);
671980eff3SMatthew Dillon static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
681980eff3SMatthew Dillon 			struct ata_port *at, union ccb *ccb);
696e0003adSMatthew Dillon static void ahci_xpt_page_inquiry(struct ahci_port *ap,
706e0003adSMatthew Dillon 			struct ata_port *at, union ccb *ccb);
71258223a3SMatthew Dillon 
72258223a3SMatthew Dillon static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
73258223a3SMatthew Dillon static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
74b4189e5eSMatthew Dillon static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
75b4189e5eSMatthew Dillon static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
767d4fcf34SMatthew Dillon static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
777d4fcf34SMatthew Dillon 		     struct scsi_sense_data *sense_data);
78258223a3SMatthew Dillon 
791980eff3SMatthew Dillon static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
801980eff3SMatthew Dillon static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
8169b3741eSMatthew Dillon static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx);
82b4189e5eSMatthew Dillon static void ahci_ata_dummy_done(struct ata_xfer *xa);
83258223a3SMatthew Dillon static void ata_fix_identify(struct ata_identify *id);
84258223a3SMatthew Dillon static void ahci_cam_rescan(struct ahci_port *ap);
856e0003adSMatthew Dillon static void ahci_strip_string(const char **basep, int *lenp);
86258223a3SMatthew Dillon 
87258223a3SMatthew Dillon int
88258223a3SMatthew Dillon ahci_cam_attach(struct ahci_port *ap)
89258223a3SMatthew Dillon {
90258223a3SMatthew Dillon 	struct cam_devq *devq;
91258223a3SMatthew Dillon 	struct cam_sim *sim;
92258223a3SMatthew Dillon 	int error;
93258223a3SMatthew Dillon 	int unit;
94258223a3SMatthew Dillon 
95cec85a37SMatthew Dillon 	/*
96cec85a37SMatthew Dillon 	 * We want at least one ccb to be available for error processing
97cec85a37SMatthew Dillon 	 * so don't let CAM use more then ncmds - 1.
98cec85a37SMatthew Dillon 	 */
99258223a3SMatthew Dillon 	unit = device_get_unit(ap->ap_sc->sc_dev);
100cec85a37SMatthew Dillon 	if (ap->ap_sc->sc_ncmds > 1)
101cec85a37SMatthew Dillon 		devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
102cec85a37SMatthew Dillon 	else
103258223a3SMatthew Dillon 		devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
104258223a3SMatthew Dillon 	if (devq == NULL) {
105258223a3SMatthew Dillon 		return (ENOMEM);
106258223a3SMatthew Dillon 	}
107258223a3SMatthew Dillon 	sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
108258223a3SMatthew Dillon 			   (void *)ap, unit, &sim_mplock, 1, 1, devq);
109258223a3SMatthew Dillon 	cam_simq_release(devq);
110258223a3SMatthew Dillon 	if (sim == NULL) {
111258223a3SMatthew Dillon 		return (ENOMEM);
112258223a3SMatthew Dillon 	}
113258223a3SMatthew Dillon 	ap->ap_sim = sim;
114831bc9e3SMatthew Dillon 	ahci_os_unlock_port(ap);
115258223a3SMatthew Dillon 	error = xpt_bus_register(ap->ap_sim, ap->ap_num);
116831bc9e3SMatthew Dillon 	ahci_os_lock_port(ap);
117258223a3SMatthew Dillon 	if (error != CAM_SUCCESS) {
118258223a3SMatthew Dillon 		ahci_cam_detach(ap);
119258223a3SMatthew Dillon 		return (EINVAL);
120258223a3SMatthew Dillon 	}
121258223a3SMatthew Dillon 	ap->ap_flags |= AP_F_BUS_REGISTERED;
122258223a3SMatthew Dillon 
123c408a8b3SMatthew Dillon 	if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
1241980eff3SMatthew Dillon 		error = ahci_cam_probe(ap, NULL);
125c408a8b3SMatthew Dillon 	else
126c408a8b3SMatthew Dillon 		error = 0;
127258223a3SMatthew Dillon 	if (error) {
128258223a3SMatthew Dillon 		ahci_cam_detach(ap);
129258223a3SMatthew Dillon 		return (EIO);
130258223a3SMatthew Dillon 	}
131258223a3SMatthew Dillon 	ap->ap_flags |= AP_F_CAM_ATTACHED;
132258223a3SMatthew Dillon 
133258223a3SMatthew Dillon 	return(0);
134258223a3SMatthew Dillon }
135258223a3SMatthew Dillon 
1361980eff3SMatthew Dillon /*
1373209f581SMatthew Dillon  * The state of the port has changed.
1383209f581SMatthew Dillon  *
1393209f581SMatthew Dillon  * If at is NULL the physical port has changed state.
1403209f581SMatthew Dillon  * If at is non-NULL a particular target behind a PM has changed state.
1413209f581SMatthew Dillon  *
1423209f581SMatthew Dillon  * If found is -1 the target state must be queued to a non-interrupt context.
1433209f581SMatthew Dillon  * (only works with at == NULL).
1443209f581SMatthew Dillon  *
1453209f581SMatthew Dillon  * If found is 0 the target was removed.
1463209f581SMatthew Dillon  * If found is 1 the target was inserted.
1471980eff3SMatthew Dillon  */
148258223a3SMatthew Dillon void
1493209f581SMatthew Dillon ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
150258223a3SMatthew Dillon {
151fd8bd957SMatthew Dillon 	struct cam_path *tmppath;
1523209f581SMatthew Dillon 	int status;
1533209f581SMatthew Dillon 	int target;
1543209f581SMatthew Dillon 
1553209f581SMatthew Dillon 	target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
156fd8bd957SMatthew Dillon 
157258223a3SMatthew Dillon 	if (ap->ap_sim == NULL)
158258223a3SMatthew Dillon 		return;
1593209f581SMatthew Dillon 	if (found == CAM_TARGET_WILDCARD) {
1603209f581SMatthew Dillon 		status = xpt_create_path(&tmppath, NULL,
1613209f581SMatthew Dillon 					 cam_sim_path(ap->ap_sim),
1623209f581SMatthew Dillon 					 target, CAM_LUN_WILDCARD);
1633209f581SMatthew Dillon 		if (status != CAM_REQ_CMP)
164fd8bd957SMatthew Dillon 			return;
165258223a3SMatthew Dillon 		ahci_cam_rescan(ap);
166fd8bd957SMatthew Dillon 	} else {
1673209f581SMatthew Dillon 		status = xpt_create_path(&tmppath, NULL,
1683209f581SMatthew Dillon 					 cam_sim_path(ap->ap_sim),
1693209f581SMatthew Dillon 					 target,
1703209f581SMatthew Dillon 					 CAM_LUN_WILDCARD);
1713209f581SMatthew Dillon 		if (status != CAM_REQ_CMP)
1723209f581SMatthew Dillon 			return;
1733209f581SMatthew Dillon #if 0
1743209f581SMatthew Dillon 		/*
1753209f581SMatthew Dillon 		 * This confuses CAM
1763209f581SMatthew Dillon 		 */
1773209f581SMatthew Dillon 		if (found)
1783209f581SMatthew Dillon 			xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
1793209f581SMatthew Dillon 		else
180fd8bd957SMatthew Dillon 			xpt_async(AC_LOST_DEVICE, tmppath, NULL);
1813209f581SMatthew Dillon #endif
182fd8bd957SMatthew Dillon 	}
183fd8bd957SMatthew Dillon 	xpt_free_path(tmppath);
184258223a3SMatthew Dillon }
185258223a3SMatthew Dillon 
186258223a3SMatthew Dillon void
187258223a3SMatthew Dillon ahci_cam_detach(struct ahci_port *ap)
188258223a3SMatthew Dillon {
189258223a3SMatthew Dillon 	int error;
190258223a3SMatthew Dillon 
191258223a3SMatthew Dillon 	if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
192258223a3SMatthew Dillon 		return;
193258223a3SMatthew Dillon 	get_mplock();
194258223a3SMatthew Dillon 	if (ap->ap_sim) {
195258223a3SMatthew Dillon 		xpt_freeze_simq(ap->ap_sim, 1);
196258223a3SMatthew Dillon 	}
197258223a3SMatthew Dillon 	if (ap->ap_flags & AP_F_BUS_REGISTERED) {
198258223a3SMatthew Dillon 		error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
199258223a3SMatthew Dillon 		KKASSERT(error == CAM_REQ_CMP);
200258223a3SMatthew Dillon 		ap->ap_flags &= ~AP_F_BUS_REGISTERED;
201258223a3SMatthew Dillon 	}
202258223a3SMatthew Dillon 	if (ap->ap_sim) {
203258223a3SMatthew Dillon 		cam_sim_free(ap->ap_sim);
204258223a3SMatthew Dillon 		ap->ap_sim = NULL;
205258223a3SMatthew Dillon 	}
206258223a3SMatthew Dillon 	rel_mplock();
207258223a3SMatthew Dillon 	ap->ap_flags &= ~AP_F_CAM_ATTACHED;
208258223a3SMatthew Dillon }
209258223a3SMatthew Dillon 
210258223a3SMatthew Dillon /*
2111980eff3SMatthew Dillon  * Once the AHCI port has been attached we need to probe for a device or
212258223a3SMatthew Dillon  * devices on the port and setup various options.
2131980eff3SMatthew Dillon  *
2141980eff3SMatthew Dillon  * If at is NULL we are probing the direct-attached device on the port,
2151980eff3SMatthew Dillon  * which may or may not be a port multiplier.
216258223a3SMatthew Dillon  */
2173209f581SMatthew Dillon int
2181980eff3SMatthew Dillon ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
219258223a3SMatthew Dillon {
2201980eff3SMatthew Dillon 	struct ata_port	*at;
221258223a3SMatthew Dillon 	struct ata_xfer	*xa;
222258223a3SMatthew Dillon 	u_int64_t	capacity;
223258223a3SMatthew Dillon 	u_int64_t	capacity_bytes;
224258223a3SMatthew Dillon 	int		model_len;
2256e0003adSMatthew Dillon 	int		firmware_len;
2266e0003adSMatthew Dillon 	int		serial_len;
227fd8bd957SMatthew Dillon 	int		error;
228258223a3SMatthew Dillon 	int		devncqdepth;
229258223a3SMatthew Dillon 	int		i;
2306e0003adSMatthew Dillon 	const char	*model_id;
2316e0003adSMatthew Dillon 	const char	*firmware_id;
2326e0003adSMatthew Dillon 	const char	*serial_id;
233669fbbf7SMatthew Dillon 	const char	*wcstr;
234669fbbf7SMatthew Dillon 	const char	*rastr;
235fd8bd957SMatthew Dillon 	const char	*scstr;
236fd8bd957SMatthew Dillon 	const char	*type;
237fd8bd957SMatthew Dillon 
2383209f581SMatthew Dillon 	error = EIO;
2391980eff3SMatthew Dillon 
2401980eff3SMatthew Dillon 	/*
241f4553de1SMatthew Dillon 	 * Delayed CAM attachment for initial probe, sim may be NULL
242f4553de1SMatthew Dillon 	 */
243f4553de1SMatthew Dillon 	if (ap->ap_sim == NULL)
244f4553de1SMatthew Dillon 		return(0);
245f4553de1SMatthew Dillon 
246f4553de1SMatthew Dillon 	/*
2471980eff3SMatthew Dillon 	 * A NULL atx indicates a probe of the directly connected device.
2481980eff3SMatthew Dillon 	 * A non-NULL atx indicates a device connected via a port multiplier.
2491980eff3SMatthew Dillon 	 * We need to preserve atx for calls to ahci_ata_get_xfer().
2501980eff3SMatthew Dillon 	 *
2511980eff3SMatthew Dillon 	 * at is always non-NULL.  For directly connected devices we supply
2521980eff3SMatthew Dillon 	 * an (at) pointing to target 0.
2531980eff3SMatthew Dillon 	 */
2541980eff3SMatthew Dillon 	if (atx == NULL) {
255b012a2caSMatthew Dillon 		at = ap->ap_ata[0];	/* direct attached - device 0 */
2561980eff3SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM) {
257831bc9e3SMatthew Dillon 			kprintf("%s: Found Port Multiplier\n",
258831bc9e3SMatthew Dillon 				ATANAME(ap, atx));
2591980eff3SMatthew Dillon 			return (0);
2601980eff3SMatthew Dillon 		}
2611980eff3SMatthew Dillon 		at->at_type = ap->ap_type;
2621980eff3SMatthew Dillon 	} else {
2633209f581SMatthew Dillon 		at = atx;
2641980eff3SMatthew Dillon 		if (atx->at_type == ATA_PORT_T_PM) {
2651980eff3SMatthew Dillon 			kprintf("%s: Bogus device, reducing port count to %d\n",
2661980eff3SMatthew Dillon 				ATANAME(ap, atx), atx->at_target);
2671980eff3SMatthew Dillon 			if (ap->ap_pmcount > atx->at_target)
2681980eff3SMatthew Dillon 				ap->ap_pmcount = atx->at_target;
2693209f581SMatthew Dillon 			goto err;
2701980eff3SMatthew Dillon 		}
2711980eff3SMatthew Dillon 	}
2723209f581SMatthew Dillon 	if (ap->ap_type == ATA_PORT_T_NONE)
2733209f581SMatthew Dillon 		goto err;
2741980eff3SMatthew Dillon 	if (at->at_type == ATA_PORT_T_NONE)
2753209f581SMatthew Dillon 		goto err;
276258223a3SMatthew Dillon 
277258223a3SMatthew Dillon 	/*
278258223a3SMatthew Dillon 	 * Issue identify, saving the result
279258223a3SMatthew Dillon 	 */
2801980eff3SMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
281258223a3SMatthew Dillon 	xa->complete = ahci_ata_dummy_done;
2821980eff3SMatthew Dillon 	xa->data = &at->at_identify;
2831980eff3SMatthew Dillon 	xa->datalen = sizeof(at->at_identify);
28412feb904SMatthew Dillon 	xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
2851980eff3SMatthew Dillon 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
2861980eff3SMatthew Dillon 
2871980eff3SMatthew Dillon 	switch(at->at_type) {
2881980eff3SMatthew Dillon 	case ATA_PORT_T_DISK:
289258223a3SMatthew Dillon 		xa->fis->command = ATA_C_IDENTIFY;
290fd8bd957SMatthew Dillon 		type = "DISK";
2911980eff3SMatthew Dillon 		break;
2921980eff3SMatthew Dillon 	case ATA_PORT_T_ATAPI:
2931980eff3SMatthew Dillon 		xa->fis->command = ATA_C_ATAPI_IDENTIFY;
29412feb904SMatthew Dillon 		xa->flags |= ATA_F_AUTOSENSE;
2951980eff3SMatthew Dillon 		type = "ATAPI";
2961980eff3SMatthew Dillon 		break;
2971980eff3SMatthew Dillon 	default:
2981980eff3SMatthew Dillon 		xa->fis->command = ATA_C_ATAPI_IDENTIFY;
2991980eff3SMatthew Dillon 		type = "UNKNOWN(ATAPI?)";
3001980eff3SMatthew Dillon 		break;
301fd8bd957SMatthew Dillon 	}
302258223a3SMatthew Dillon 	xa->fis->features = 0;
303258223a3SMatthew Dillon 	xa->fis->device = 0;
3043209f581SMatthew Dillon 	xa->timeout = 1000;
305258223a3SMatthew Dillon 
306831bc9e3SMatthew Dillon 	if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
307fd8bd957SMatthew Dillon 		kprintf("%s: Detected %s device but unable to IDENTIFY\n",
3081980eff3SMatthew Dillon 			ATANAME(ap, atx), type);
309258223a3SMatthew Dillon 		ahci_ata_put_xfer(xa);
3103209f581SMatthew Dillon 		goto err;
311258223a3SMatthew Dillon 	}
312258223a3SMatthew Dillon 	ahci_ata_put_xfer(xa);
313258223a3SMatthew Dillon 
3141980eff3SMatthew Dillon 	ata_fix_identify(&at->at_identify);
315258223a3SMatthew Dillon 
316258223a3SMatthew Dillon 	/*
317258223a3SMatthew Dillon 	 * Read capacity using SATA probe info.
318258223a3SMatthew Dillon 	 */
3191980eff3SMatthew Dillon 	if (le16toh(at->at_identify.cmdset83) & 0x0400) {
320258223a3SMatthew Dillon 		/* LBA48 feature set supported */
321258223a3SMatthew Dillon 		capacity = 0;
322258223a3SMatthew Dillon 		for (i = 3; i >= 0; --i) {
323258223a3SMatthew Dillon 			capacity <<= 16;
324258223a3SMatthew Dillon 			capacity +=
3251980eff3SMatthew Dillon 			    le16toh(at->at_identify.addrsecxt[i]);
326258223a3SMatthew Dillon 		}
327258223a3SMatthew Dillon 	} else {
3281980eff3SMatthew Dillon 		capacity = le16toh(at->at_identify.addrsec[1]);
329258223a3SMatthew Dillon 		capacity <<= 16;
3301980eff3SMatthew Dillon 		capacity += le16toh(at->at_identify.addrsec[0]);
331258223a3SMatthew Dillon 	}
33212feb904SMatthew Dillon 	if (capacity == 0)
33312feb904SMatthew Dillon 		capacity = 1024 * 1024 / 512;
3341980eff3SMatthew Dillon 	at->at_capacity = capacity;
3351980eff3SMatthew Dillon 	if (atx == NULL)
3361980eff3SMatthew Dillon 		ap->ap_probe = ATA_PROBE_GOOD;
337258223a3SMatthew Dillon 
338258223a3SMatthew Dillon 	capacity_bytes = capacity * 512;
339258223a3SMatthew Dillon 
340258223a3SMatthew Dillon 	/*
341258223a3SMatthew Dillon 	 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
342258223a3SMatthew Dillon 	 * number of slots and limit the number of CAM ccb's to one less
343258223a3SMatthew Dillon 	 * so we always have a slot available for recovery.
344258223a3SMatthew Dillon 	 *
345258223a3SMatthew Dillon 	 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
346258223a3SMatthew Dillon 	 * not support it, and in that case the driver can handle extra
347258223a3SMatthew Dillon 	 * ccb's.
348cec85a37SMatthew Dillon 	 *
3491980eff3SMatthew Dillon 	 * NCQ is currently used only with direct-attached disks.  It is
3501980eff3SMatthew Dillon 	 * not used with port multipliers or direct-attached ATAPI devices.
3511980eff3SMatthew Dillon 	 *
352cec85a37SMatthew Dillon 	 * Remember at least one extra CCB needs to be reserved for the
353cec85a37SMatthew Dillon 	 * error ccb.
354258223a3SMatthew Dillon 	 */
355258223a3SMatthew Dillon 	if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
3561980eff3SMatthew Dillon 	    ap->ap_type == ATA_PORT_T_DISK &&
3571980eff3SMatthew Dillon 	    (le16toh(at->at_identify.satacap) & (1 << 8))) {
3581980eff3SMatthew Dillon 		at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
3591980eff3SMatthew Dillon 		devncqdepth = at->at_ncqdepth;
3601980eff3SMatthew Dillon 		if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
3611980eff3SMatthew Dillon 			at->at_ncqdepth = ap->ap_sc->sc_ncmds;
3621980eff3SMatthew Dillon 		if (at->at_ncqdepth > 1) {
363258223a3SMatthew Dillon 			for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
3641980eff3SMatthew Dillon 				xa = ahci_ata_get_xfer(ap, atx);
3651980eff3SMatthew Dillon 				if (xa->tag < at->at_ncqdepth) {
366258223a3SMatthew Dillon 					xa->state = ATA_S_COMPLETE;
367258223a3SMatthew Dillon 					ahci_ata_put_xfer(xa);
368258223a3SMatthew Dillon 				}
369258223a3SMatthew Dillon 			}
3701980eff3SMatthew Dillon 			if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
371258223a3SMatthew Dillon 				cam_devq_resize(ap->ap_sim->devq,
3721980eff3SMatthew Dillon 						at->at_ncqdepth - 1);
373258223a3SMatthew Dillon 			}
374cec85a37SMatthew Dillon 		}
375258223a3SMatthew Dillon 	} else {
376258223a3SMatthew Dillon 		devncqdepth = 0;
377258223a3SMatthew Dillon 	}
378258223a3SMatthew Dillon 
3796e0003adSMatthew Dillon 	model_len = sizeof(at->at_identify.model);
3806e0003adSMatthew Dillon 	model_id = at->at_identify.model;
3816e0003adSMatthew Dillon 	ahci_strip_string(&model_id, &model_len);
3826e0003adSMatthew Dillon 
3836e0003adSMatthew Dillon 	firmware_len = sizeof(at->at_identify.firmware);
3846e0003adSMatthew Dillon 	firmware_id = at->at_identify.firmware;
3856e0003adSMatthew Dillon 	ahci_strip_string(&firmware_id, &firmware_len);
3866e0003adSMatthew Dillon 
3876e0003adSMatthew Dillon 	serial_len = sizeof(at->at_identify.serial);
3886e0003adSMatthew Dillon 	serial_id = at->at_identify.serial;
3896e0003adSMatthew Dillon 	ahci_strip_string(&serial_id, &serial_len);
390669fbbf7SMatthew Dillon 
391fd8bd957SMatthew Dillon 	/*
392fd8bd957SMatthew Dillon 	 * Generate informatiive strings.
393fd8bd957SMatthew Dillon 	 *
394fd8bd957SMatthew Dillon 	 * NOTE: We do not automatically set write caching, lookahead,
395fd8bd957SMatthew Dillon 	 *	 or the security state for ATAPI devices.
396fd8bd957SMatthew Dillon 	 */
3971980eff3SMatthew Dillon 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
3981980eff3SMatthew Dillon 		if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
399669fbbf7SMatthew Dillon 			wcstr = "enabled";
4001980eff3SMatthew Dillon 		else if (at->at_type == ATA_PORT_T_ATAPI)
401fd8bd957SMatthew Dillon 			wcstr = "disabled";
402669fbbf7SMatthew Dillon 		else
403669fbbf7SMatthew Dillon 			wcstr = "enabling";
404669fbbf7SMatthew Dillon 	} else {
405669fbbf7SMatthew Dillon 		    wcstr = "notsupp";
406669fbbf7SMatthew Dillon 	}
407669fbbf7SMatthew Dillon 
4081980eff3SMatthew Dillon 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
4091980eff3SMatthew Dillon 		if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
410669fbbf7SMatthew Dillon 			rastr = "enabled";
4111980eff3SMatthew Dillon 		else if (at->at_type == ATA_PORT_T_ATAPI)
412fd8bd957SMatthew Dillon 			rastr = "disabled";
413669fbbf7SMatthew Dillon 		else
414669fbbf7SMatthew Dillon 			rastr = "enabling";
415669fbbf7SMatthew Dillon 	} else {
416669fbbf7SMatthew Dillon 		    rastr = "notsupp";
417669fbbf7SMatthew Dillon 	}
418669fbbf7SMatthew Dillon 
4191980eff3SMatthew Dillon 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
4201980eff3SMatthew Dillon 		if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
421fd8bd957SMatthew Dillon 			scstr = "frozen";
4221980eff3SMatthew Dillon 		else if (at->at_type == ATA_PORT_T_ATAPI)
423fd8bd957SMatthew Dillon 			scstr = "unfrozen";
424afa796d2SMatthew Dillon 		else if (AhciNoFeatures & (1 << ap->ap_num))
425afa796d2SMatthew Dillon 			scstr = "<disabled>";
426fd8bd957SMatthew Dillon 		else
427fd8bd957SMatthew Dillon 			scstr = "freezing";
428fd8bd957SMatthew Dillon 	} else {
429fd8bd957SMatthew Dillon 		    scstr = "notsupp";
430fd8bd957SMatthew Dillon 	}
431fd8bd957SMatthew Dillon 
4326e0003adSMatthew Dillon 	kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
433074579dfSMatthew Dillon 		"%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
434074579dfSMatthew Dillon 		"capacity=%lld.%02dMB\n",
435074579dfSMatthew Dillon 
4361980eff3SMatthew Dillon 		ATANAME(ap, atx),
437fd8bd957SMatthew Dillon 		type,
4386e0003adSMatthew Dillon 		model_len, model_len, model_id,
4396e0003adSMatthew Dillon 		firmware_len, firmware_len, firmware_id,
4406e0003adSMatthew Dillon 		serial_len, serial_len, serial_id,
441258223a3SMatthew Dillon 
4421980eff3SMatthew Dillon 		ATANAME(ap, atx),
443258223a3SMatthew Dillon 		devncqdepth, ap->ap_sc->sc_ncmds,
4441980eff3SMatthew Dillon 		at->at_identify.satacap,
4451980eff3SMatthew Dillon 		at->at_identify.satafsup,
446074579dfSMatthew Dillon 		(at->at_ncqdepth > 1 ? "YES" : "NO"),
447258223a3SMatthew Dillon 		(long long)capacity_bytes / (1024 * 1024),
448074579dfSMatthew Dillon 		(int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
449074579dfSMatthew Dillon 	);
450074579dfSMatthew Dillon 	kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
4511980eff3SMatthew Dillon 		ATANAME(ap, atx),
4521980eff3SMatthew Dillon 		at->at_identify.features85,
4531980eff3SMatthew Dillon 		at->at_identify.features86,
4541980eff3SMatthew Dillon 		at->at_identify.features87,
455669fbbf7SMatthew Dillon 		wcstr,
456fd8bd957SMatthew Dillon 		rastr,
457fd8bd957SMatthew Dillon 		scstr
458258223a3SMatthew Dillon 	);
459258223a3SMatthew Dillon 
460258223a3SMatthew Dillon 	/*
461fd8bd957SMatthew Dillon 	 * Additional type-specific probing
462fd8bd957SMatthew Dillon 	 */
4631980eff3SMatthew Dillon 	switch(at->at_type) {
464fd8bd957SMatthew Dillon 	case ATA_PORT_T_DISK:
4651980eff3SMatthew Dillon 		error = ahci_cam_probe_disk(ap, atx);
4661980eff3SMatthew Dillon 		break;
4671980eff3SMatthew Dillon 	case ATA_PORT_T_ATAPI:
4681980eff3SMatthew Dillon 		error = ahci_cam_probe_atapi(ap, atx);
469fd8bd957SMatthew Dillon 		break;
470fd8bd957SMatthew Dillon 	default:
4711980eff3SMatthew Dillon 		error = EIO;
472fd8bd957SMatthew Dillon 		break;
473fd8bd957SMatthew Dillon 	}
4743209f581SMatthew Dillon err:
4753209f581SMatthew Dillon 	if (error) {
4763209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_FAILED;
4773209f581SMatthew Dillon 		if (atx == NULL)
4783209f581SMatthew Dillon 			ap->ap_probe = at->at_probe;
4793209f581SMatthew Dillon 	} else {
4803209f581SMatthew Dillon 		at->at_probe = ATA_PROBE_GOOD;
4813209f581SMatthew Dillon 		if (atx == NULL)
4823209f581SMatthew Dillon 			ap->ap_probe = at->at_probe;
4833209f581SMatthew Dillon 	}
4843209f581SMatthew Dillon 	return (error);
485fd8bd957SMatthew Dillon }
486fd8bd957SMatthew Dillon 
487fd8bd957SMatthew Dillon /*
488fd8bd957SMatthew Dillon  * DISK-specific probe after initial ident
489fd8bd957SMatthew Dillon  */
490fd8bd957SMatthew Dillon static int
4911980eff3SMatthew Dillon ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
492fd8bd957SMatthew Dillon {
4931980eff3SMatthew Dillon 	struct ata_port *at;
494fd8bd957SMatthew Dillon 	struct ata_xfer	*xa;
495fd8bd957SMatthew Dillon 
496b012a2caSMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
4971980eff3SMatthew Dillon 
498fd8bd957SMatthew Dillon 	/*
49969b3741eSMatthew Dillon 	 * Set dummy xfer mode
50069b3741eSMatthew Dillon 	 */
50169b3741eSMatthew Dillon 	ahci_set_xfer(ap, atx);
50269b3741eSMatthew Dillon 
50369b3741eSMatthew Dillon 	/*
504258223a3SMatthew Dillon 	 * Enable write cache if supported
505fd8bd957SMatthew Dillon 	 *
506fd8bd957SMatthew Dillon 	 * NOTE: "WD My Book" external disk devices have a very poor
507fd8bd957SMatthew Dillon 	 *	 daughter board between the the ESATA and the HD.  Sending
508fd8bd957SMatthew Dillon 	 *	 any ATA_C_SET_FEATURES commands will break the hardware port
509fd8bd957SMatthew Dillon 	 *	 with a fatal protocol error.  However, this device also
510fd8bd957SMatthew Dillon 	 *	 indicates that WRITECACHE is already on and READAHEAD is
511fd8bd957SMatthew Dillon 	 *	 not supported so we avoid the issue.
512258223a3SMatthew Dillon 	 */
5131980eff3SMatthew Dillon 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
5141980eff3SMatthew Dillon 	    (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
5151980eff3SMatthew Dillon 		xa = ahci_ata_get_xfer(ap, atx);
516258223a3SMatthew Dillon 		xa->complete = ahci_ata_dummy_done;
517258223a3SMatthew Dillon 		xa->fis->command = ATA_C_SET_FEATURES;
518b012a2caSMatthew Dillon 		xa->fis->features = ATA_SF_WRITECACHE_EN;
519b012a2caSMatthew Dillon 		/* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */
5201980eff3SMatthew Dillon 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
521669fbbf7SMatthew Dillon 		xa->fis->device = 0;
522b012a2caSMatthew Dillon 		xa->flags = ATA_F_PIO | ATA_F_POLL;
5233209f581SMatthew Dillon 		xa->timeout = 1000;
524669fbbf7SMatthew Dillon 		xa->datalen = 0;
525831bc9e3SMatthew Dillon 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
5261980eff3SMatthew Dillon 			at->at_features |= ATA_PORT_F_WCACHE;
527afa796d2SMatthew Dillon 		else
528afa796d2SMatthew Dillon 			kprintf("%s: Unable to enable write-caching\n",
529afa796d2SMatthew Dillon 				ATANAME(ap, atx));
530258223a3SMatthew Dillon 		ahci_ata_put_xfer(xa);
531258223a3SMatthew Dillon 	}
532258223a3SMatthew Dillon 
533258223a3SMatthew Dillon 	/*
534258223a3SMatthew Dillon 	 * Enable readahead if supported
535258223a3SMatthew Dillon 	 */
5361980eff3SMatthew Dillon 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
5371980eff3SMatthew Dillon 	    (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
5381980eff3SMatthew Dillon 		xa = ahci_ata_get_xfer(ap, atx);
539258223a3SMatthew Dillon 		xa->complete = ahci_ata_dummy_done;
540258223a3SMatthew Dillon 		xa->fis->command = ATA_C_SET_FEATURES;
541258223a3SMatthew Dillon 		xa->fis->features = ATA_SF_LOOKAHEAD_EN;
5421980eff3SMatthew Dillon 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
543669fbbf7SMatthew Dillon 		xa->fis->device = 0;
544b012a2caSMatthew Dillon 		xa->flags = ATA_F_PIO | ATA_F_POLL;
5453209f581SMatthew Dillon 		xa->timeout = 1000;
546669fbbf7SMatthew Dillon 		xa->datalen = 0;
547831bc9e3SMatthew Dillon 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
5481980eff3SMatthew Dillon 			at->at_features |= ATA_PORT_F_RAHEAD;
549afa796d2SMatthew Dillon 		else
550afa796d2SMatthew Dillon 			kprintf("%s: Unable to enable read-ahead\n",
551afa796d2SMatthew Dillon 				ATANAME(ap, atx));
552258223a3SMatthew Dillon 		ahci_ata_put_xfer(xa);
553258223a3SMatthew Dillon 	}
554258223a3SMatthew Dillon 
555258223a3SMatthew Dillon 	/*
556258223a3SMatthew Dillon 	 * FREEZE LOCK the device so malicious users can't lock it on us.
557258223a3SMatthew Dillon 	 * As there is no harm in issuing this to devices that don't
558258223a3SMatthew Dillon 	 * support the security feature set we just send it, and don't bother
559258223a3SMatthew Dillon 	 * checking if the device sends a command abort to tell us it doesn't
560258223a3SMatthew Dillon 	 * support it
561258223a3SMatthew Dillon 	 */
5621980eff3SMatthew Dillon 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
563afa796d2SMatthew Dillon 	    (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
564afa796d2SMatthew Dillon 	    (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
5651980eff3SMatthew Dillon 		xa = ahci_ata_get_xfer(ap, atx);
566258223a3SMatthew Dillon 		xa->complete = ahci_ata_dummy_done;
567258223a3SMatthew Dillon 		xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
5681980eff3SMatthew Dillon 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
569b012a2caSMatthew Dillon 		xa->flags = ATA_F_PIO | ATA_F_POLL;
5703209f581SMatthew Dillon 		xa->timeout = 1000;
571669fbbf7SMatthew Dillon 		xa->datalen = 0;
572831bc9e3SMatthew Dillon 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
5731980eff3SMatthew Dillon 			at->at_features |= ATA_PORT_F_FRZLCK;
574afa796d2SMatthew Dillon 		else
575afa796d2SMatthew Dillon 			kprintf("%s: Unable to set security freeze\n",
576afa796d2SMatthew Dillon 				ATANAME(ap, atx));
577258223a3SMatthew Dillon 		ahci_ata_put_xfer(xa);
578669fbbf7SMatthew Dillon 	}
579258223a3SMatthew Dillon 
580b4189e5eSMatthew Dillon 	return (0);
581b4189e5eSMatthew Dillon }
582b4189e5eSMatthew Dillon 
583fd8bd957SMatthew Dillon /*
584fd8bd957SMatthew Dillon  * ATAPI-specific probe after initial ident
585fd8bd957SMatthew Dillon  */
586b4189e5eSMatthew Dillon static int
5871980eff3SMatthew Dillon ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
588b4189e5eSMatthew Dillon {
58969b3741eSMatthew Dillon 	ahci_set_xfer(ap, atx);
59069b3741eSMatthew Dillon 	return(0);
59169b3741eSMatthew Dillon }
59269b3741eSMatthew Dillon 
59369b3741eSMatthew Dillon /*
59469b3741eSMatthew Dillon  * Setting the transfer mode is irrelevant for the SATA transport
59569b3741eSMatthew Dillon  * but some (atapi) devices seem to need it anyway.  In addition
59669b3741eSMatthew Dillon  * if we are running through a SATA->PATA converter for some reason
59769b3741eSMatthew Dillon  * beyond my comprehension we might have to set the mode.
598*d15a49f2SMatthew Dillon  *
599*d15a49f2SMatthew Dillon  * We only support DMA modes for SATA attached devices, so don't bother
600*d15a49f2SMatthew Dillon  * with legacy modes.
60169b3741eSMatthew Dillon  */
60269b3741eSMatthew Dillon static int
60369b3741eSMatthew Dillon ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx)
60469b3741eSMatthew Dillon {
60569b3741eSMatthew Dillon 	struct ata_port *at;
60669b3741eSMatthew Dillon 	struct ata_xfer	*xa;
60769b3741eSMatthew Dillon 	u_int16_t mode;
608*d15a49f2SMatthew Dillon 	u_int16_t mask;
60969b3741eSMatthew Dillon 
61069b3741eSMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
61169b3741eSMatthew Dillon 
61269b3741eSMatthew Dillon 	/*
613*d15a49f2SMatthew Dillon 	 * Figure out the supported UDMA mode.  Ignore other legacy modes.
61469b3741eSMatthew Dillon 	 */
615*d15a49f2SMatthew Dillon 	mask = le16toh(at->at_identify.ultradma);
616*d15a49f2SMatthew Dillon 	if ((mask & 0xFF) == 0 || mask == 0xFFFF)
61769b3741eSMatthew Dillon 		return(0);
618*d15a49f2SMatthew Dillon 	mask &= 0xFF;
619*d15a49f2SMatthew Dillon 	mode = 0x4F;
620*d15a49f2SMatthew Dillon 	while ((mask & 0x8000) == 0) {
621*d15a49f2SMatthew Dillon 		mask <<= 1;
622*d15a49f2SMatthew Dillon 		--mode;
623*d15a49f2SMatthew Dillon 	}
62469b3741eSMatthew Dillon 
62569b3741eSMatthew Dillon 	/*
62669b3741eSMatthew Dillon 	 * SATA atapi devices often still report a dma mode, even though
62769b3741eSMatthew Dillon 	 * it is irrelevant for SATA transport.  It is also possible that
62869b3741eSMatthew Dillon 	 * we are running through a SATA->PATA converter and seeing the
62969b3741eSMatthew Dillon 	 * PATA dma mode.
63069b3741eSMatthew Dillon 	 *
63169b3741eSMatthew Dillon 	 * In this case the device may require a (dummy) SETXFER to be
63269b3741eSMatthew Dillon 	 * sent before it will work properly.
63369b3741eSMatthew Dillon 	 */
63469b3741eSMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
63569b3741eSMatthew Dillon 	xa->complete = ahci_ata_dummy_done;
63669b3741eSMatthew Dillon 	xa->fis->command = ATA_C_SET_FEATURES;
63769b3741eSMatthew Dillon 	xa->fis->features = ATA_SF_SETXFER;
63869b3741eSMatthew Dillon 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
639*d15a49f2SMatthew Dillon 	xa->fis->sector_count = mode;
64069b3741eSMatthew Dillon 	xa->flags = ATA_F_PIO | ATA_F_POLL;
64169b3741eSMatthew Dillon 	xa->timeout = 1000;
64269b3741eSMatthew Dillon 	xa->datalen = 0;
64369b3741eSMatthew Dillon 	if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
64469b3741eSMatthew Dillon 		kprintf("%s: Unable to set dummy xfer mode \n",
64569b3741eSMatthew Dillon 			ATANAME(ap, atx));
64669b3741eSMatthew Dillon 	} else if (bootverbose) {
64769b3741eSMatthew Dillon 		kprintf("%s: Set dummy xfer mode to %02x\n",
648*d15a49f2SMatthew Dillon 			ATANAME(ap, atx), mode);
64969b3741eSMatthew Dillon 	}
65069b3741eSMatthew Dillon 	ahci_ata_put_xfer(xa);
651fd8bd957SMatthew Dillon 	return(0);
652fd8bd957SMatthew Dillon }
653fd8bd957SMatthew Dillon 
654b4189e5eSMatthew Dillon /*
655b4189e5eSMatthew Dillon  * Fix byte ordering so buffers can be accessed as
656b4189e5eSMatthew Dillon  * strings.
657b4189e5eSMatthew Dillon  */
658258223a3SMatthew Dillon static void
659258223a3SMatthew Dillon ata_fix_identify(struct ata_identify *id)
660258223a3SMatthew Dillon {
661258223a3SMatthew Dillon 	u_int16_t	*swap;
662258223a3SMatthew Dillon 	int		i;
663258223a3SMatthew Dillon 
664258223a3SMatthew Dillon 	swap = (u_int16_t *)id->serial;
665258223a3SMatthew Dillon 	for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
666258223a3SMatthew Dillon 		swap[i] = bswap16(swap[i]);
667258223a3SMatthew Dillon 
668258223a3SMatthew Dillon 	swap = (u_int16_t *)id->firmware;
669258223a3SMatthew Dillon 	for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
670258223a3SMatthew Dillon 		swap[i] = bswap16(swap[i]);
671258223a3SMatthew Dillon 
672258223a3SMatthew Dillon 	swap = (u_int16_t *)id->model;
673258223a3SMatthew Dillon 	for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
674258223a3SMatthew Dillon 		swap[i] = bswap16(swap[i]);
675258223a3SMatthew Dillon }
676258223a3SMatthew Dillon 
677258223a3SMatthew Dillon /*
678b4189e5eSMatthew Dillon  * Dummy done callback for xa.
679b4189e5eSMatthew Dillon  */
680b4189e5eSMatthew Dillon static void
681b4189e5eSMatthew Dillon ahci_ata_dummy_done(struct ata_xfer *xa)
682b4189e5eSMatthew Dillon {
683b4189e5eSMatthew Dillon }
684b4189e5eSMatthew Dillon 
685b4189e5eSMatthew Dillon /*
6863209f581SMatthew Dillon  * Use an engineering request to initiate a target scan for devices
6873209f581SMatthew Dillon  * behind a port multiplier.
688fd8bd957SMatthew Dillon  *
6893209f581SMatthew Dillon  * An asynchronous bus scan is used to avoid reentrancy issues.
690258223a3SMatthew Dillon  */
691258223a3SMatthew Dillon static void
692258223a3SMatthew Dillon ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
693258223a3SMatthew Dillon {
6943209f581SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
6953209f581SMatthew Dillon 
696f4553de1SMatthew Dillon 	if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
6973209f581SMatthew Dillon 		ap->ap_flags &= ~AP_F_SCAN_RUNNING;
6983209f581SMatthew Dillon 		if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
6993209f581SMatthew Dillon 			ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
7003209f581SMatthew Dillon 			ahci_cam_rescan(ap);
7013209f581SMatthew Dillon 		}
702f4553de1SMatthew Dillon 		ap->ap_flags |= AP_F_SCAN_COMPLETED;
703f4553de1SMatthew Dillon 		wakeup(&ap->ap_flags);
704f4553de1SMatthew Dillon 	}
705f4553de1SMatthew Dillon 	xpt_free_ccb(ccb);
706258223a3SMatthew Dillon }
707258223a3SMatthew Dillon 
708258223a3SMatthew Dillon static void
709258223a3SMatthew Dillon ahci_cam_rescan(struct ahci_port *ap)
710258223a3SMatthew Dillon {
711258223a3SMatthew Dillon 	struct cam_path *path;
712258223a3SMatthew Dillon 	union ccb *ccb;
713258223a3SMatthew Dillon 	int status;
7143209f581SMatthew Dillon 	int i;
7153209f581SMatthew Dillon 
7163209f581SMatthew Dillon 	if (ap->ap_flags & AP_F_SCAN_RUNNING) {
7173209f581SMatthew Dillon 		ap->ap_flags |= AP_F_SCAN_REQUESTED;
7183209f581SMatthew Dillon 		return;
7193209f581SMatthew Dillon 	}
7203209f581SMatthew Dillon 	ap->ap_flags |= AP_F_SCAN_RUNNING;
7213209f581SMatthew Dillon 	for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
722b012a2caSMatthew Dillon 		ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN;
7233209f581SMatthew Dillon 	}
724258223a3SMatthew Dillon 
725258223a3SMatthew Dillon 	status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
726258223a3SMatthew Dillon 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
727258223a3SMatthew Dillon 	if (status != CAM_REQ_CMP)
728258223a3SMatthew Dillon 		return;
729258223a3SMatthew Dillon 
730f4553de1SMatthew Dillon 	ccb = xpt_alloc_ccb();
731258223a3SMatthew Dillon 	xpt_setup_ccb(&ccb->ccb_h, path, 5);	/* 5 = low priority */
7322de5e9baSMatthew Dillon 	ccb->ccb_h.func_code = XPT_ENG_EXEC;
733258223a3SMatthew Dillon 	ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
7343209f581SMatthew Dillon 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
735258223a3SMatthew Dillon 	ccb->crcn.flags = CAM_FLAG_NONE;
7362de5e9baSMatthew Dillon 	xpt_action_async(ccb);
737258223a3SMatthew Dillon }
738258223a3SMatthew Dillon 
7393209f581SMatthew Dillon static void
7403209f581SMatthew Dillon ahci_xpt_rescan(struct ahci_port *ap)
7413209f581SMatthew Dillon {
7423209f581SMatthew Dillon 	struct cam_path *path;
7433209f581SMatthew Dillon 	union ccb *ccb;
7443209f581SMatthew Dillon 	int status;
7453209f581SMatthew Dillon 
7463209f581SMatthew Dillon 	status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
7473209f581SMatthew Dillon 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
7483209f581SMatthew Dillon 	if (status != CAM_REQ_CMP)
7493209f581SMatthew Dillon 		return;
750f4553de1SMatthew Dillon 
751f4553de1SMatthew Dillon 	ccb = xpt_alloc_ccb();
7523209f581SMatthew Dillon 	xpt_setup_ccb(&ccb->ccb_h, path, 5);	/* 5 = low priority */
7532de5e9baSMatthew Dillon 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
7543209f581SMatthew Dillon 	ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
7553209f581SMatthew Dillon 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
7563209f581SMatthew Dillon 	ccb->crcn.flags = CAM_FLAG_NONE;
757baef7501SMatthew Dillon 	xpt_action_async(ccb);
7583209f581SMatthew Dillon }
7593209f581SMatthew Dillon 
760258223a3SMatthew Dillon /*
761258223a3SMatthew Dillon  * Action function - dispatch command
762258223a3SMatthew Dillon  */
763258223a3SMatthew Dillon static
764258223a3SMatthew Dillon void
765258223a3SMatthew Dillon ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
766258223a3SMatthew Dillon {
767258223a3SMatthew Dillon 	struct ahci_port *ap;
7681980eff3SMatthew Dillon 	struct ata_port	 *at, *atx;
769258223a3SMatthew Dillon 	struct ccb_hdr *ccbh;
770258223a3SMatthew Dillon 	int unit;
771258223a3SMatthew Dillon 
772258223a3SMatthew Dillon 	/* XXX lock */
773258223a3SMatthew Dillon 	ap = cam_sim_softc(sim);
7741980eff3SMatthew Dillon 	atx = NULL;
775258223a3SMatthew Dillon 	KKASSERT(ap != NULL);
776258223a3SMatthew Dillon 	ccbh = &ccb->ccb_h;
777258223a3SMatthew Dillon 	unit = cam_sim_unit(sim);
778258223a3SMatthew Dillon 
779258223a3SMatthew Dillon 	/*
7803209f581SMatthew Dillon 	 * Early failure checks.  These checks do not apply to XPT_PATH_INQ,
7813209f581SMatthew Dillon 	 * otherwise the bus rescan will not remove the dead devices when
7823209f581SMatthew Dillon 	 * unplugging a PM.
7833209f581SMatthew Dillon 	 *
7841980eff3SMatthew Dillon 	 * For non-wildcards we have one target (0) and one lun (0),
7851980eff3SMatthew Dillon 	 * unless we have a port multiplier.
7861980eff3SMatthew Dillon 	 *
7871980eff3SMatthew Dillon 	 * A wildcard target indicates only the general bus is being
7881980eff3SMatthew Dillon 	 * probed.
7891980eff3SMatthew Dillon 	 *
7901980eff3SMatthew Dillon 	 * Calculate at and atx.  at is always non-NULL.  atx is only
7911980eff3SMatthew Dillon 	 * non-NULL for direct-attached devices.  It will be NULL for
7921980eff3SMatthew Dillon 	 * devices behind a port multiplier.
793258223a3SMatthew Dillon 	 *
794258223a3SMatthew Dillon 	 * XXX What do we do with a LUN wildcard?
795258223a3SMatthew Dillon 	 */
7963209f581SMatthew Dillon 	if (ccbh->target_id != CAM_TARGET_WILDCARD &&
7973209f581SMatthew Dillon 	    ccbh->func_code != XPT_PATH_INQ) {
7981980eff3SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_NONE) {
7993209f581SMatthew Dillon 			ccbh->status = CAM_DEV_NOT_THERE;
800258223a3SMatthew Dillon 			xpt_done(ccb);
801258223a3SMatthew Dillon 			return;
802258223a3SMatthew Dillon 		}
8031980eff3SMatthew Dillon 		if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
804258223a3SMatthew Dillon 			ccbh->status = CAM_DEV_NOT_THERE;
805258223a3SMatthew Dillon 			xpt_done(ccb);
806258223a3SMatthew Dillon 			return;
807258223a3SMatthew Dillon 		}
808b012a2caSMatthew Dillon 		at = ap->ap_ata[ccbh->target_id];
8091980eff3SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_PM)
8101980eff3SMatthew Dillon 			atx = at;
8111980eff3SMatthew Dillon 
812258223a3SMatthew Dillon 		if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
813258223a3SMatthew Dillon 			ccbh->status = CAM_DEV_NOT_THERE;
814258223a3SMatthew Dillon 			xpt_done(ccb);
815258223a3SMatthew Dillon 			return;
816258223a3SMatthew Dillon 		}
817b012a2caSMatthew Dillon 	} else {
818b012a2caSMatthew Dillon 		at = ap->ap_ata[0];
819258223a3SMatthew Dillon 	}
820258223a3SMatthew Dillon 
821258223a3SMatthew Dillon 	/*
822258223a3SMatthew Dillon 	 * Switch on the meta XPT command
823258223a3SMatthew Dillon 	 */
824258223a3SMatthew Dillon 	switch(ccbh->func_code) {
8253209f581SMatthew Dillon 	case XPT_ENG_EXEC:
8263209f581SMatthew Dillon 		/*
8273209f581SMatthew Dillon 		 * This routine is called after a port multiplier has been
8283209f581SMatthew Dillon 		 * probed.
8293209f581SMatthew Dillon 		 */
8303209f581SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
831f4553de1SMatthew Dillon 		ahci_os_lock_port(ap);
832831bc9e3SMatthew Dillon 		ahci_port_state_machine(ap, 0);
833f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
8343209f581SMatthew Dillon 		xpt_done(ccb);
8353209f581SMatthew Dillon 		ahci_xpt_rescan(ap);
8363209f581SMatthew Dillon 		break;
837258223a3SMatthew Dillon 	case XPT_PATH_INQ:
8383209f581SMatthew Dillon 		/*
8393209f581SMatthew Dillon 		 * This command always succeeds, otherwise the bus scan
8403209f581SMatthew Dillon 		 * will not detach dead devices.
8413209f581SMatthew Dillon 		 */
842258223a3SMatthew Dillon 		ccb->cpi.version_num = 1;
843258223a3SMatthew Dillon 		ccb->cpi.hba_inquiry = 0;
844258223a3SMatthew Dillon 		ccb->cpi.target_sprt = 0;
8451980eff3SMatthew Dillon 		ccb->cpi.hba_misc = PIM_SEQSCAN;
846258223a3SMatthew Dillon 		ccb->cpi.hba_eng_cnt = 0;
847258223a3SMatthew Dillon 		bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
84876497a9cSMatthew Dillon 		ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
849258223a3SMatthew Dillon 		ccb->cpi.max_lun = 0;
850258223a3SMatthew Dillon 		ccb->cpi.async_flags = 0;
851258223a3SMatthew Dillon 		ccb->cpi.hpath_id = 0;
8521980eff3SMatthew Dillon 		ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
853258223a3SMatthew Dillon 		ccb->cpi.unit_number = cam_sim_unit(sim);
854258223a3SMatthew Dillon 		ccb->cpi.bus_id = cam_sim_bus(sim);
855258223a3SMatthew Dillon 		ccb->cpi.base_transfer_speed = 150000;
8562cc2e845SMatthew Dillon 		ccb->cpi.transport = XPORT_SATA;
857258223a3SMatthew Dillon 		ccb->cpi.transport_version = 1;
858258223a3SMatthew Dillon 		ccb->cpi.protocol = PROTO_SCSI;
859258223a3SMatthew Dillon 		ccb->cpi.protocol_version = SCSI_REV_2;
860258223a3SMatthew Dillon 
8613209f581SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
862831bc9e3SMatthew Dillon 		if (ccbh->target_id == CAM_TARGET_WILDCARD) {
863831bc9e3SMatthew Dillon 			ahci_os_lock_port(ap);
864831bc9e3SMatthew Dillon 			ahci_port_state_machine(ap, 0);
865831bc9e3SMatthew Dillon 			ahci_os_unlock_port(ap);
866831bc9e3SMatthew Dillon 		} else {
867258223a3SMatthew Dillon 			switch(ahci_pread(ap, AHCI_PREG_SSTS) &
868258223a3SMatthew Dillon 			       AHCI_PREG_SSTS_SPD) {
869258223a3SMatthew Dillon 			case AHCI_PREG_SSTS_SPD_GEN1:
870258223a3SMatthew Dillon 				ccb->cpi.base_transfer_speed = 150000;
871258223a3SMatthew Dillon 				break;
872258223a3SMatthew Dillon 			case AHCI_PREG_SSTS_SPD_GEN2:
873258223a3SMatthew Dillon 				ccb->cpi.base_transfer_speed = 300000;
874258223a3SMatthew Dillon 				break;
875258223a3SMatthew Dillon 			default:
876258223a3SMatthew Dillon 				/* unknown */
877258223a3SMatthew Dillon 				ccb->cpi.base_transfer_speed = 1000;
878258223a3SMatthew Dillon 				break;
879258223a3SMatthew Dillon 			}
8803209f581SMatthew Dillon #if 0
8811980eff3SMatthew Dillon 			if (ap->ap_type == ATA_PORT_T_NONE)
8821980eff3SMatthew Dillon 				ccbh->status = CAM_DEV_NOT_THERE;
8833209f581SMatthew Dillon #endif
8841980eff3SMatthew Dillon 		}
885258223a3SMatthew Dillon 		xpt_done(ccb);
886258223a3SMatthew Dillon 		break;
887258223a3SMatthew Dillon 	case XPT_RESET_DEV:
888f4553de1SMatthew Dillon 		ahci_os_lock_port(ap);
8891980eff3SMatthew Dillon 		if (ap->ap_type == ATA_PORT_T_NONE) {
8903209f581SMatthew Dillon 			ccbh->status = CAM_DEV_NOT_THERE;
8911980eff3SMatthew Dillon 		} else {
8921980eff3SMatthew Dillon 			ahci_port_reset(ap, atx, 0);
893fd8bd957SMatthew Dillon 			ccbh->status = CAM_REQ_CMP;
8941980eff3SMatthew Dillon 		}
895f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
896258223a3SMatthew Dillon 		xpt_done(ccb);
897258223a3SMatthew Dillon 		break;
898258223a3SMatthew Dillon 	case XPT_RESET_BUS:
899f4553de1SMatthew Dillon 		ahci_os_lock_port(ap);
9001980eff3SMatthew Dillon 		ahci_port_reset(ap, NULL, 1);
901f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
902fd8bd957SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
903258223a3SMatthew Dillon 		xpt_done(ccb);
904258223a3SMatthew Dillon 		break;
905258223a3SMatthew Dillon 	case XPT_SET_TRAN_SETTINGS:
906258223a3SMatthew Dillon 		ccbh->status = CAM_FUNC_NOTAVAIL;
907258223a3SMatthew Dillon 		xpt_done(ccb);
908258223a3SMatthew Dillon 		break;
909258223a3SMatthew Dillon 	case XPT_GET_TRAN_SETTINGS:
910258223a3SMatthew Dillon 		ccb->cts.protocol = PROTO_SCSI;
911258223a3SMatthew Dillon 		ccb->cts.protocol_version = SCSI_REV_2;
9122cc2e845SMatthew Dillon 		ccb->cts.transport = XPORT_SATA;
913258223a3SMatthew Dillon 		ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
914258223a3SMatthew Dillon 		ccb->cts.proto_specific.valid = 0;
915258223a3SMatthew Dillon 		ccb->cts.xport_specific.valid = 0;
916258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
917258223a3SMatthew Dillon 		xpt_done(ccb);
918258223a3SMatthew Dillon 		break;
919258223a3SMatthew Dillon 	case XPT_CALC_GEOMETRY:
920258223a3SMatthew Dillon 		cam_calc_geometry(&ccb->ccg, 1);
921258223a3SMatthew Dillon 		xpt_done(ccb);
922258223a3SMatthew Dillon 		break;
923258223a3SMatthew Dillon 	case XPT_SCSI_IO:
924f4553de1SMatthew Dillon 		/*
925f4553de1SMatthew Dillon 		 * Our parallel startup code might have only probed through
926f4553de1SMatthew Dillon 		 * to the IDENT, so do the last step if necessary.
927f4553de1SMatthew Dillon 		 */
928f4553de1SMatthew Dillon 		if (at->at_probe == ATA_PROBE_NEED_IDENT)
929f4553de1SMatthew Dillon 			ahci_cam_probe(ap, atx);
930f4553de1SMatthew Dillon 		if (at->at_probe != ATA_PROBE_GOOD) {
931f4553de1SMatthew Dillon 			ccbh->status = CAM_DEV_NOT_THERE;
932f4553de1SMatthew Dillon 			xpt_done(ccb);
933f4553de1SMatthew Dillon 			break;
934f4553de1SMatthew Dillon 		}
9351980eff3SMatthew Dillon 		switch(at->at_type) {
936258223a3SMatthew Dillon 		case ATA_PORT_T_DISK:
9371980eff3SMatthew Dillon 			ahci_xpt_scsi_disk_io(ap, atx, ccb);
938258223a3SMatthew Dillon 			break;
939258223a3SMatthew Dillon 		case ATA_PORT_T_ATAPI:
9401980eff3SMatthew Dillon 			ahci_xpt_scsi_atapi_io(ap, atx, ccb);
941258223a3SMatthew Dillon 			break;
942258223a3SMatthew Dillon 		default:
943258223a3SMatthew Dillon 			ccbh->status = CAM_REQ_INVALID;
944258223a3SMatthew Dillon 			xpt_done(ccb);
945258223a3SMatthew Dillon 			break;
946258223a3SMatthew Dillon 		}
947258223a3SMatthew Dillon 		break;
948258223a3SMatthew Dillon 	default:
949258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_INVALID;
950258223a3SMatthew Dillon 		xpt_done(ccb);
951258223a3SMatthew Dillon 		break;
952258223a3SMatthew Dillon 	}
953258223a3SMatthew Dillon }
954258223a3SMatthew Dillon 
955258223a3SMatthew Dillon /*
956de68d532SMatthew Dillon  * Poll function.
957de68d532SMatthew Dillon  *
958de68d532SMatthew Dillon  * Generally this function gets called heavily when interrupts might be
959de68d532SMatthew Dillon  * non-operational, during a halt/reboot or panic.
960258223a3SMatthew Dillon  */
961258223a3SMatthew Dillon static
962258223a3SMatthew Dillon void
963258223a3SMatthew Dillon ahci_xpt_poll(struct cam_sim *sim)
964258223a3SMatthew Dillon {
965de68d532SMatthew Dillon 	struct ahci_port *ap;
966258223a3SMatthew Dillon 
967de68d532SMatthew Dillon 	ap = cam_sim_softc(sim);
968de68d532SMatthew Dillon 	crit_enter();
969f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
970f4553de1SMatthew Dillon 	ahci_port_intr(ap, 1);
971f4553de1SMatthew Dillon 	ahci_os_unlock_port(ap);
972de68d532SMatthew Dillon 	crit_exit();
973258223a3SMatthew Dillon }
974258223a3SMatthew Dillon 
975258223a3SMatthew Dillon /*
976b4189e5eSMatthew Dillon  * Convert the SCSI command in ccb to an ata_xfer command in xa
977b4189e5eSMatthew Dillon  * for ATA_PORT_T_DISK operations.  Set the completion function
978b4189e5eSMatthew Dillon  * to convert the response back, then dispatch to the OpenBSD AHCI
979b4189e5eSMatthew Dillon  * layer.
980258223a3SMatthew Dillon  *
981b4189e5eSMatthew Dillon  * AHCI DISK commands only support a limited command set, and we
982b4189e5eSMatthew Dillon  * fake additional commands to make it play nice with the CAM subsystem.
983258223a3SMatthew Dillon  */
984258223a3SMatthew Dillon static
985258223a3SMatthew Dillon void
9861980eff3SMatthew Dillon ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
9871980eff3SMatthew Dillon 		      union ccb *ccb)
988258223a3SMatthew Dillon {
989258223a3SMatthew Dillon 	struct ccb_hdr *ccbh;
990258223a3SMatthew Dillon 	struct ccb_scsiio *csio;
991258223a3SMatthew Dillon 	struct ata_xfer *xa;
9921980eff3SMatthew Dillon 	struct ata_port	*at;
993258223a3SMatthew Dillon 	struct ata_fis_h2d *fis;
994bedbe7f8SAlex Hornung 	struct ata_pass_12 *atp12;
995bedbe7f8SAlex Hornung 	struct ata_pass_16 *atp16;
996258223a3SMatthew Dillon 	scsi_cdb_t cdb;
997258223a3SMatthew Dillon 	union scsi_data *rdata;
998258223a3SMatthew Dillon 	int rdata_len;
999258223a3SMatthew Dillon 	u_int64_t capacity;
1000258223a3SMatthew Dillon 	u_int64_t lba;
1001258223a3SMatthew Dillon 	u_int32_t count;
1002258223a3SMatthew Dillon 
1003258223a3SMatthew Dillon 	ccbh = &ccb->csio.ccb_h;
1004258223a3SMatthew Dillon 	csio = &ccb->csio;
1005b012a2caSMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
10061980eff3SMatthew Dillon 
10071980eff3SMatthew Dillon 	/*
10081980eff3SMatthew Dillon 	 * XXX not passing NULL at for direct attach!
10091980eff3SMatthew Dillon 	 */
10101980eff3SMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
1011258223a3SMatthew Dillon 	rdata = (void *)csio->data_ptr;
1012258223a3SMatthew Dillon 	rdata_len = csio->dxfer_len;
1013258223a3SMatthew Dillon 
1014258223a3SMatthew Dillon 	/*
1015258223a3SMatthew Dillon 	 * Build the FIS or process the csio to completion.
1016258223a3SMatthew Dillon 	 */
1017258223a3SMatthew Dillon 	cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1018258223a3SMatthew Dillon 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1019258223a3SMatthew Dillon 
1020258223a3SMatthew Dillon 	switch(cdb->generic.opcode) {
1021258223a3SMatthew Dillon 	case REQUEST_SENSE:
1022258223a3SMatthew Dillon 		/*
1023258223a3SMatthew Dillon 		 * Auto-sense everything, so explicit sense requests
1024258223a3SMatthew Dillon 		 * return no-sense.
1025258223a3SMatthew Dillon 		 */
1026258223a3SMatthew Dillon 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1027258223a3SMatthew Dillon 		break;
1028258223a3SMatthew Dillon 	case INQUIRY:
1029258223a3SMatthew Dillon 		/*
1030258223a3SMatthew Dillon 		 * Inquiry supported features
1031258223a3SMatthew Dillon 		 *
1032258223a3SMatthew Dillon 		 * [opcode, byte2, page_code, length, control]
1033258223a3SMatthew Dillon 		 */
1034258223a3SMatthew Dillon 		if (cdb->inquiry.byte2 & SI_EVPD) {
10356e0003adSMatthew Dillon 			ahci_xpt_page_inquiry(ap, at, ccb);
1036258223a3SMatthew Dillon 		} else {
1037258223a3SMatthew Dillon 			bzero(rdata, rdata_len);
1038258223a3SMatthew Dillon 			if (rdata_len < SHORT_INQUIRY_LENGTH) {
1039258223a3SMatthew Dillon 				ccbh->status = CAM_CCB_LEN_ERR;
1040258223a3SMatthew Dillon 				break;
1041258223a3SMatthew Dillon 			}
1042258223a3SMatthew Dillon 			if (rdata_len > sizeof(rdata->inquiry_data))
1043258223a3SMatthew Dillon 				rdata_len = sizeof(rdata->inquiry_data);
1044258223a3SMatthew Dillon 			rdata->inquiry_data.device = T_DIRECT;
1045258223a3SMatthew Dillon 			rdata->inquiry_data.version = SCSI_REV_SPC2;
1046258223a3SMatthew Dillon 			rdata->inquiry_data.response_format = 2;
1047258223a3SMatthew Dillon 			rdata->inquiry_data.additional_length = 32;
1048258223a3SMatthew Dillon 			bcopy("SATA    ", rdata->inquiry_data.vendor, 8);
10491980eff3SMatthew Dillon 			bcopy(at->at_identify.model,
1050258223a3SMatthew Dillon 			      rdata->inquiry_data.product,
1051258223a3SMatthew Dillon 			      sizeof(rdata->inquiry_data.product));
10521980eff3SMatthew Dillon 			bcopy(at->at_identify.firmware,
1053258223a3SMatthew Dillon 			      rdata->inquiry_data.revision,
1054258223a3SMatthew Dillon 			      sizeof(rdata->inquiry_data.revision));
1055258223a3SMatthew Dillon 			ccbh->status = CAM_REQ_CMP;
1056258223a3SMatthew Dillon 		}
1057258223a3SMatthew Dillon 		break;
1058258223a3SMatthew Dillon 	case READ_CAPACITY_16:
1059258223a3SMatthew Dillon 		if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1060258223a3SMatthew Dillon 			ccbh->status = CAM_REQ_INVALID;
1061258223a3SMatthew Dillon 			break;
1062258223a3SMatthew Dillon 		}
1063258223a3SMatthew Dillon 		if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1064258223a3SMatthew Dillon 			ccbh->status = CAM_CCB_LEN_ERR;
1065258223a3SMatthew Dillon 			break;
1066258223a3SMatthew Dillon 		}
1067258223a3SMatthew Dillon 		/* fall through */
1068258223a3SMatthew Dillon 	case READ_CAPACITY:
1069258223a3SMatthew Dillon 		if (rdata_len < sizeof(rdata->read_capacity_data)) {
1070258223a3SMatthew Dillon 			ccbh->status = CAM_CCB_LEN_ERR;
1071258223a3SMatthew Dillon 			break;
1072258223a3SMatthew Dillon 		}
1073258223a3SMatthew Dillon 
10741980eff3SMatthew Dillon 		capacity = at->at_capacity;
1075258223a3SMatthew Dillon 
1076258223a3SMatthew Dillon 		bzero(rdata, rdata_len);
1077258223a3SMatthew Dillon 		if (cdb->generic.opcode == READ_CAPACITY) {
1078258223a3SMatthew Dillon 			rdata_len = sizeof(rdata->read_capacity_data);
1079258223a3SMatthew Dillon 			if (capacity > 0xFFFFFFFFU)
1080258223a3SMatthew Dillon 				capacity = 0xFFFFFFFFU;
1081258223a3SMatthew Dillon 			bzero(&rdata->read_capacity_data, rdata_len);
1082258223a3SMatthew Dillon 			scsi_ulto4b((u_int32_t)capacity - 1,
1083258223a3SMatthew Dillon 				    rdata->read_capacity_data.addr);
1084258223a3SMatthew Dillon 			scsi_ulto4b(512, rdata->read_capacity_data.length);
1085258223a3SMatthew Dillon 		} else {
1086258223a3SMatthew Dillon 			rdata_len = sizeof(rdata->read_capacity_data_16);
1087258223a3SMatthew Dillon 			bzero(&rdata->read_capacity_data_16, rdata_len);
1088258223a3SMatthew Dillon 			scsi_u64to8b(capacity - 1,
1089258223a3SMatthew Dillon 				     rdata->read_capacity_data_16.addr);
1090258223a3SMatthew Dillon 			scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1091258223a3SMatthew Dillon 		}
1092258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
1093258223a3SMatthew Dillon 		break;
1094258223a3SMatthew Dillon 	case SYNCHRONIZE_CACHE:
1095258223a3SMatthew Dillon 		/*
1096258223a3SMatthew Dillon 		 * Synchronize cache.  Specification says this can take
1097258223a3SMatthew Dillon 		 * greater then 30 seconds so give it at least 45.
1098258223a3SMatthew Dillon 		 */
1099258223a3SMatthew Dillon 		fis = xa->fis;
1100258223a3SMatthew Dillon 		fis->flags = ATA_H2D_FLAGS_CMD;
1101258223a3SMatthew Dillon 		fis->command = ATA_C_FLUSH_CACHE;
1102258223a3SMatthew Dillon 		fis->device = 0;
11033209f581SMatthew Dillon 		if (xa->timeout < 45000)
11043209f581SMatthew Dillon 			xa->timeout = 45000;
11051980eff3SMatthew Dillon 		xa->datalen = 0;
1106b012a2caSMatthew Dillon 		xa->flags = 0;
11071980eff3SMatthew Dillon 		xa->complete = ahci_ata_complete_disk_synchronize_cache;
1108258223a3SMatthew Dillon 		break;
1109258223a3SMatthew Dillon 	case TEST_UNIT_READY:
1110258223a3SMatthew Dillon 	case START_STOP_UNIT:
1111258223a3SMatthew Dillon 	case PREVENT_ALLOW:
1112258223a3SMatthew Dillon 		/*
1113258223a3SMatthew Dillon 		 * Just silently return success
1114258223a3SMatthew Dillon 		 */
1115258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
1116258223a3SMatthew Dillon 		rdata_len = 0;
1117258223a3SMatthew Dillon 		break;
1118258223a3SMatthew Dillon 	case ATA_PASS_12:
1119bedbe7f8SAlex Hornung 		atp12 = &cdb->ata_pass_12;
1120bedbe7f8SAlex Hornung 		fis = xa->fis;
1121258223a3SMatthew Dillon 		/*
1122bedbe7f8SAlex Hornung 		 * Figure out the flags to be used, depending on the direction of the
1123bedbe7f8SAlex Hornung 		 * CAM request.
1124258223a3SMatthew Dillon 		 */
1125bedbe7f8SAlex Hornung 		switch (ccbh->flags & CAM_DIR_MASK) {
1126bedbe7f8SAlex Hornung 		case CAM_DIR_IN:
1127bedbe7f8SAlex Hornung 			xa->flags = ATA_F_READ;
1128bedbe7f8SAlex Hornung 			break;
1129bedbe7f8SAlex Hornung 		case CAM_DIR_OUT:
1130bedbe7f8SAlex Hornung 			xa->flags = ATA_F_WRITE;
1131bedbe7f8SAlex Hornung 			break;
1132bedbe7f8SAlex Hornung 		default:
1133bedbe7f8SAlex Hornung 			xa->flags = 0;
1134bedbe7f8SAlex Hornung 		}
1135bedbe7f8SAlex Hornung 		xa->flags |= ATA_F_POLL;
1136bedbe7f8SAlex Hornung 		xa->data = csio->data_ptr;
1137bedbe7f8SAlex Hornung 		xa->datalen = csio->dxfer_len;
1138bedbe7f8SAlex Hornung 		xa->complete = ahci_ata_complete_disk_rw;
1139bedbe7f8SAlex Hornung 		xa->timeout = ccbh->timeout;
1140bedbe7f8SAlex Hornung 
1141bedbe7f8SAlex Hornung 		/*
1142bedbe7f8SAlex Hornung 		 * Populate the fis from the information we received through CAM
1143bedbe7f8SAlex Hornung 		 * ATA passthrough.
1144bedbe7f8SAlex Hornung 		 */
1145bedbe7f8SAlex Hornung 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp12->flags ? */
1146bedbe7f8SAlex Hornung 		fis->features = atp12->features;
1147bedbe7f8SAlex Hornung 		fis->sector_count = atp12->sector_count;
1148bedbe7f8SAlex Hornung 		fis->lba_low = atp12->lba_low;
1149bedbe7f8SAlex Hornung 		fis->lba_mid = atp12->lba_mid;
1150bedbe7f8SAlex Hornung 		fis->lba_high = atp12->lba_high;
1151bedbe7f8SAlex Hornung 		fis->device = atp12->device;	/* maybe always 0? */
1152bedbe7f8SAlex Hornung 		fis->command = atp12->command;
1153bedbe7f8SAlex Hornung 		fis->control = atp12->control;
1154bedbe7f8SAlex Hornung 
1155bedbe7f8SAlex Hornung 		/*
1156bedbe7f8SAlex Hornung 		 * Mark as in progress so it is sent to the device.
1157bedbe7f8SAlex Hornung 		 */
1158bedbe7f8SAlex Hornung 		ccbh->status = CAM_REQ_INPROG;
1159bedbe7f8SAlex Hornung 		break;
1160bedbe7f8SAlex Hornung 	case ATA_PASS_16:
1161bedbe7f8SAlex Hornung 		atp16 = &cdb->ata_pass_16;
1162bedbe7f8SAlex Hornung 		fis = xa->fis;
1163bedbe7f8SAlex Hornung 		/*
1164bedbe7f8SAlex Hornung 		 * Figure out the flags to be used, depending on the direction of the
1165bedbe7f8SAlex Hornung 		 * CAM request.
1166bedbe7f8SAlex Hornung 		 */
1167bedbe7f8SAlex Hornung 		switch (ccbh->flags & CAM_DIR_MASK) {
1168bedbe7f8SAlex Hornung 		case CAM_DIR_IN:
1169bedbe7f8SAlex Hornung 			xa->flags = ATA_F_READ;
1170bedbe7f8SAlex Hornung 			break;
1171bedbe7f8SAlex Hornung 		case CAM_DIR_OUT:
1172bedbe7f8SAlex Hornung 			xa->flags = ATA_F_WRITE;
1173bedbe7f8SAlex Hornung 			break;
1174bedbe7f8SAlex Hornung 		default:
1175bedbe7f8SAlex Hornung 			xa->flags = 0;
1176bedbe7f8SAlex Hornung 		}
1177bedbe7f8SAlex Hornung 		xa->flags |= ATA_F_POLL;
1178bedbe7f8SAlex Hornung 		xa->data = csio->data_ptr;
1179bedbe7f8SAlex Hornung 		xa->datalen = csio->dxfer_len;
1180bedbe7f8SAlex Hornung 		xa->complete = ahci_ata_complete_disk_rw;
1181bedbe7f8SAlex Hornung 		xa->timeout = ccbh->timeout;
1182bedbe7f8SAlex Hornung 
1183bedbe7f8SAlex Hornung 		/*
1184bedbe7f8SAlex Hornung 		 * Populate the fis from the information we received through CAM
1185bedbe7f8SAlex Hornung 		 * ATA passthrough.
1186bedbe7f8SAlex Hornung 		 */
1187bedbe7f8SAlex Hornung 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp16->flags ? */
1188bedbe7f8SAlex Hornung 		fis->features = atp16->features;
1189bedbe7f8SAlex Hornung 		fis->features_exp = atp16->features_ext;
1190bedbe7f8SAlex Hornung 		fis->sector_count = atp16->sector_count;
1191bedbe7f8SAlex Hornung 		fis->sector_count_exp = atp16->sector_count_ext;
1192bedbe7f8SAlex Hornung 		fis->lba_low = atp16->lba_low;
1193bedbe7f8SAlex Hornung 		fis->lba_low_exp = atp16->lba_low_ext;
1194bedbe7f8SAlex Hornung 		fis->lba_mid = atp16->lba_mid;
1195bedbe7f8SAlex Hornung 		fis->lba_mid_exp = atp16->lba_mid_ext;
1196bedbe7f8SAlex Hornung 		fis->lba_high = atp16->lba_high;
1197bedbe7f8SAlex Hornung 		fis->lba_mid_exp = atp16->lba_mid_ext;
1198bedbe7f8SAlex Hornung 		fis->device = atp16->device;	/* maybe always 0? */
1199bedbe7f8SAlex Hornung 		fis->command = atp16->command;
1200bedbe7f8SAlex Hornung 
1201bedbe7f8SAlex Hornung 		/*
1202bedbe7f8SAlex Hornung 		 * Mark as in progress so it is sent to the device.
1203bedbe7f8SAlex Hornung 		 */
1204bedbe7f8SAlex Hornung 		ccbh->status = CAM_REQ_INPROG;
1205258223a3SMatthew Dillon 		break;
1206258223a3SMatthew Dillon 	default:
1207258223a3SMatthew Dillon 		switch(cdb->generic.opcode) {
1208258223a3SMatthew Dillon 		case READ_6:
1209258223a3SMatthew Dillon 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1210258223a3SMatthew Dillon 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1211258223a3SMatthew Dillon 			xa->flags = ATA_F_READ;
1212258223a3SMatthew Dillon 			break;
1213258223a3SMatthew Dillon 		case READ_10:
1214258223a3SMatthew Dillon 			lba = scsi_4btoul(cdb->rw_10.addr);
1215258223a3SMatthew Dillon 			count = scsi_2btoul(cdb->rw_10.length);
1216258223a3SMatthew Dillon 			xa->flags = ATA_F_READ;
1217258223a3SMatthew Dillon 			break;
1218258223a3SMatthew Dillon 		case READ_12:
1219258223a3SMatthew Dillon 			lba = scsi_4btoul(cdb->rw_12.addr);
1220258223a3SMatthew Dillon 			count = scsi_4btoul(cdb->rw_12.length);
1221258223a3SMatthew Dillon 			xa->flags = ATA_F_READ;
1222258223a3SMatthew Dillon 			break;
1223258223a3SMatthew Dillon 		case READ_16:
1224258223a3SMatthew Dillon 			lba = scsi_8btou64(cdb->rw_16.addr);
1225258223a3SMatthew Dillon 			count = scsi_4btoul(cdb->rw_16.length);
1226258223a3SMatthew Dillon 			xa->flags = ATA_F_READ;
1227258223a3SMatthew Dillon 			break;
1228258223a3SMatthew Dillon 		case WRITE_6:
1229258223a3SMatthew Dillon 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1230258223a3SMatthew Dillon 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1231258223a3SMatthew Dillon 			xa->flags = ATA_F_WRITE;
1232258223a3SMatthew Dillon 			break;
1233258223a3SMatthew Dillon 		case WRITE_10:
1234258223a3SMatthew Dillon 			lba = scsi_4btoul(cdb->rw_10.addr);
1235258223a3SMatthew Dillon 			count = scsi_2btoul(cdb->rw_10.length);
1236258223a3SMatthew Dillon 			xa->flags = ATA_F_WRITE;
1237258223a3SMatthew Dillon 			break;
1238258223a3SMatthew Dillon 		case WRITE_12:
1239258223a3SMatthew Dillon 			lba = scsi_4btoul(cdb->rw_12.addr);
1240258223a3SMatthew Dillon 			count = scsi_4btoul(cdb->rw_12.length);
1241258223a3SMatthew Dillon 			xa->flags = ATA_F_WRITE;
1242258223a3SMatthew Dillon 			break;
1243258223a3SMatthew Dillon 		case WRITE_16:
1244258223a3SMatthew Dillon 			lba = scsi_8btou64(cdb->rw_16.addr);
1245258223a3SMatthew Dillon 			count = scsi_4btoul(cdb->rw_16.length);
1246258223a3SMatthew Dillon 			xa->flags = ATA_F_WRITE;
1247258223a3SMatthew Dillon 			break;
1248258223a3SMatthew Dillon 		default:
1249258223a3SMatthew Dillon 			ccbh->status = CAM_REQ_INVALID;
1250258223a3SMatthew Dillon 			break;
1251258223a3SMatthew Dillon 		}
1252258223a3SMatthew Dillon 		if (ccbh->status != CAM_REQ_INPROG)
1253258223a3SMatthew Dillon 			break;
1254258223a3SMatthew Dillon 
1255258223a3SMatthew Dillon 		fis = xa->fis;
1256258223a3SMatthew Dillon 		fis->flags = ATA_H2D_FLAGS_CMD;
1257258223a3SMatthew Dillon 		fis->lba_low = (u_int8_t)lba;
1258258223a3SMatthew Dillon 		fis->lba_mid = (u_int8_t)(lba >> 8);
1259258223a3SMatthew Dillon 		fis->lba_high = (u_int8_t)(lba >> 16);
1260258223a3SMatthew Dillon 		fis->device = ATA_H2D_DEVICE_LBA;
1261258223a3SMatthew Dillon 
12621980eff3SMatthew Dillon 		/*
12631980eff3SMatthew Dillon 		 * NCQ only for direct-attached disks, do not currently
12641980eff3SMatthew Dillon 		 * try to use NCQ with port multipliers.
12651980eff3SMatthew Dillon 		 */
12661980eff3SMatthew Dillon 		if (at->at_ncqdepth > 1 &&
12671980eff3SMatthew Dillon 		    ap->ap_type == ATA_PORT_T_DISK &&
1268258223a3SMatthew Dillon 		    (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1269258223a3SMatthew Dillon 		    (ccbh->flags & CAM_POLLED) == 0) {
1270258223a3SMatthew Dillon 			/*
1271258223a3SMatthew Dillon 			 * Use NCQ - always uses 48 bit addressing
1272258223a3SMatthew Dillon 			 */
1273258223a3SMatthew Dillon 			xa->flags |= ATA_F_NCQ;
1274258223a3SMatthew Dillon 			fis->command = (xa->flags & ATA_F_WRITE) ?
1275258223a3SMatthew Dillon 					ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1276258223a3SMatthew Dillon 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1277258223a3SMatthew Dillon 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1278258223a3SMatthew Dillon 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1279258223a3SMatthew Dillon 			fis->sector_count = xa->tag << 3;
1280258223a3SMatthew Dillon 			fis->features = (u_int8_t)count;
1281258223a3SMatthew Dillon 			fis->features_exp = (u_int8_t)(count >> 8);
1282b2772aeeSMatthew Dillon 		} else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1283258223a3SMatthew Dillon 			/*
1284258223a3SMatthew Dillon 			 * Use LBA48
1285258223a3SMatthew Dillon 			 */
1286258223a3SMatthew Dillon 			fis->command = (xa->flags & ATA_F_WRITE) ?
1287258223a3SMatthew Dillon 					ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1288258223a3SMatthew Dillon 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1289258223a3SMatthew Dillon 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1290258223a3SMatthew Dillon 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1291258223a3SMatthew Dillon 			fis->sector_count = (u_int8_t)count;
1292258223a3SMatthew Dillon 			fis->sector_count_exp = (u_int8_t)(count >> 8);
1293258223a3SMatthew Dillon 		} else {
1294258223a3SMatthew Dillon 			/*
1295258223a3SMatthew Dillon 			 * Use LBA
1296258223a3SMatthew Dillon 			 *
1297258223a3SMatthew Dillon 			 * NOTE: 256 sectors is supported, stored as 0.
1298258223a3SMatthew Dillon 			 */
1299258223a3SMatthew Dillon 			fis->command = (xa->flags & ATA_F_WRITE) ?
1300258223a3SMatthew Dillon 					ATA_C_WRITEDMA : ATA_C_READDMA;
1301258223a3SMatthew Dillon 			fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1302258223a3SMatthew Dillon 			fis->sector_count = (u_int8_t)count;
1303258223a3SMatthew Dillon 		}
1304258223a3SMatthew Dillon 
1305258223a3SMatthew Dillon 		xa->data = csio->data_ptr;
1306258223a3SMatthew Dillon 		xa->datalen = csio->dxfer_len;
1307258223a3SMatthew Dillon 		xa->complete = ahci_ata_complete_disk_rw;
13083209f581SMatthew Dillon 		xa->timeout = ccbh->timeout;	/* milliseconds */
130912feb904SMatthew Dillon #if 0
131012feb904SMatthew Dillon 		if (xa->timeout > 10000)	/* XXX - debug */
131112feb904SMatthew Dillon 			xa->timeout = 10000;
131212feb904SMatthew Dillon #endif
1313258223a3SMatthew Dillon 		if (ccbh->flags & CAM_POLLED)
1314258223a3SMatthew Dillon 			xa->flags |= ATA_F_POLL;
1315258223a3SMatthew Dillon 		break;
1316258223a3SMatthew Dillon 	}
1317258223a3SMatthew Dillon 
1318258223a3SMatthew Dillon 	/*
1319258223a3SMatthew Dillon 	 * If the request is still in progress the xa and FIS have
13206e0003adSMatthew Dillon 	 * been set up (except for the PM target), and must be dispatched.
13216e0003adSMatthew Dillon 	 * Otherwise the request was completed.
1322258223a3SMatthew Dillon 	 */
1323258223a3SMatthew Dillon 	if (ccbh->status == CAM_REQ_INPROG) {
1324258223a3SMatthew Dillon 		KKASSERT(xa->complete != NULL);
1325258223a3SMatthew Dillon 		xa->atascsi_private = ccb;
1326258223a3SMatthew Dillon 		ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1327f4553de1SMatthew Dillon 		ahci_os_lock_port(ap);
13286e0003adSMatthew Dillon 		xa->fis->flags |= at->at_target;
1329258223a3SMatthew Dillon 		ahci_ata_cmd(xa);
1330f4553de1SMatthew Dillon 		ahci_os_unlock_port(ap);
1331258223a3SMatthew Dillon 	} else {
1332258223a3SMatthew Dillon 		ahci_ata_put_xfer(xa);
1333258223a3SMatthew Dillon 		xpt_done(ccb);
1334258223a3SMatthew Dillon 	}
1335258223a3SMatthew Dillon }
1336258223a3SMatthew Dillon 
1337b4189e5eSMatthew Dillon /*
1338b4189e5eSMatthew Dillon  * Convert the SCSI command in ccb to an ata_xfer command in xa
1339b4189e5eSMatthew Dillon  * for ATA_PORT_T_ATAPI operations.  Set the completion function
1340b4189e5eSMatthew Dillon  * to convert the response back, then dispatch to the OpenBSD AHCI
1341b4189e5eSMatthew Dillon  * layer.
1342b4189e5eSMatthew Dillon  */
1343258223a3SMatthew Dillon static
1344258223a3SMatthew Dillon void
13451980eff3SMatthew Dillon ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
13461980eff3SMatthew Dillon 			union ccb *ccb)
1347258223a3SMatthew Dillon {
1348258223a3SMatthew Dillon 	struct ccb_hdr *ccbh;
1349258223a3SMatthew Dillon 	struct ccb_scsiio *csio;
1350258223a3SMatthew Dillon 	struct ata_xfer *xa;
1351258223a3SMatthew Dillon 	struct ata_fis_h2d *fis;
1352b4189e5eSMatthew Dillon 	scsi_cdb_t cdbs;
1353b4189e5eSMatthew Dillon 	scsi_cdb_t cdbd;
1354b4189e5eSMatthew Dillon 	int flags;
13551980eff3SMatthew Dillon 	struct ata_port	*at;
1356258223a3SMatthew Dillon 
1357258223a3SMatthew Dillon 	ccbh = &ccb->csio.ccb_h;
1358258223a3SMatthew Dillon 	csio = &ccb->csio;
1359b012a2caSMatthew Dillon 	at = atx ? atx : ap->ap_ata[0];
1360b4189e5eSMatthew Dillon 
1361b4189e5eSMatthew Dillon 	switch (ccbh->flags & CAM_DIR_MASK) {
1362b4189e5eSMatthew Dillon 	case CAM_DIR_IN:
1363b4189e5eSMatthew Dillon 		flags = ATA_F_PACKET | ATA_F_READ;
1364b4189e5eSMatthew Dillon 		break;
1365b4189e5eSMatthew Dillon 	case CAM_DIR_OUT:
1366b4189e5eSMatthew Dillon 		flags = ATA_F_PACKET | ATA_F_WRITE;
1367b4189e5eSMatthew Dillon 		break;
1368b4189e5eSMatthew Dillon 	case CAM_DIR_NONE:
1369b4189e5eSMatthew Dillon 		flags = ATA_F_PACKET;
1370b4189e5eSMatthew Dillon 		break;
1371b4189e5eSMatthew Dillon 	default:
1372b4189e5eSMatthew Dillon 		ccbh->status = CAM_REQ_INVALID;
1373b4189e5eSMatthew Dillon 		xpt_done(ccb);
1374b4189e5eSMatthew Dillon 		return;
1375b4189e5eSMatthew Dillon 		/* NOT REACHED */
1376b4189e5eSMatthew Dillon 	}
1377b4189e5eSMatthew Dillon 
1378b4189e5eSMatthew Dillon 	/*
137912feb904SMatthew Dillon 	 * Special handling to get the rfis back into host memory while
1380192ee1d0SMatthew Dillon 	 * still allowing the chip to run commands in parallel to
138112feb904SMatthew Dillon 	 * ATAPI devices behind a PM.
138212feb904SMatthew Dillon 	 */
138312feb904SMatthew Dillon 	flags |= ATA_F_AUTOSENSE;
138412feb904SMatthew Dillon 
138512feb904SMatthew Dillon 	/*
1386b4189e5eSMatthew Dillon 	 * The command has to fit in the packet command buffer.
1387b4189e5eSMatthew Dillon 	 */
1388b4189e5eSMatthew Dillon 	if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1389b4189e5eSMatthew Dillon 		ccbh->status = CAM_CCB_LEN_ERR;
1390b4189e5eSMatthew Dillon 		xpt_done(ccb);
1391b4189e5eSMatthew Dillon 		return;
1392b4189e5eSMatthew Dillon 	}
1393b4189e5eSMatthew Dillon 
1394b4189e5eSMatthew Dillon 	/*
1395192ee1d0SMatthew Dillon 	 * Initialize the XA and FIS.  It is unclear how much of
1396192ee1d0SMatthew Dillon 	 * this has to mimic the equivalent ATA command.
13971980eff3SMatthew Dillon 	 *
13981980eff3SMatthew Dillon 	 * XXX not passing NULL at for direct attach!
1399b4189e5eSMatthew Dillon 	 */
14001980eff3SMatthew Dillon 	xa = ahci_ata_get_xfer(ap, atx);
1401258223a3SMatthew Dillon 	fis = xa->fis;
1402258223a3SMatthew Dillon 
14031980eff3SMatthew Dillon 	fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
14041980eff3SMatthew Dillon 	fis->command = ATA_C_PACKET;
1405192ee1d0SMatthew Dillon 	fis->device = ATA_H2D_DEVICE_LBA;
14061980eff3SMatthew Dillon 	fis->sector_count = xa->tag << 3;
1407192ee1d0SMatthew Dillon 	if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1408192ee1d0SMatthew Dillon 		if (flags & ATA_F_WRITE) {
14091980eff3SMatthew Dillon 			fis->features = ATA_H2D_FEATURES_DMA |
1410192ee1d0SMatthew Dillon 				       ATA_H2D_FEATURES_DIR_WRITE;
1411192ee1d0SMatthew Dillon 		} else {
1412192ee1d0SMatthew Dillon 			fis->features = ATA_H2D_FEATURES_DMA |
1413192ee1d0SMatthew Dillon 				       ATA_H2D_FEATURES_DIR_READ;
1414192ee1d0SMatthew Dillon 		}
1415192ee1d0SMatthew Dillon 	} else {
1416192ee1d0SMatthew Dillon 		fis->lba_mid = 0;
1417192ee1d0SMatthew Dillon 		fis->lba_high = 0;
1418192ee1d0SMatthew Dillon 	}
1419192ee1d0SMatthew Dillon 	fis->control = ATA_FIS_CONTROL_4BIT;
14201980eff3SMatthew Dillon 
1421b4189e5eSMatthew Dillon 	xa->flags = flags;
1422b4189e5eSMatthew Dillon 	xa->data = csio->data_ptr;
1423b4189e5eSMatthew Dillon 	xa->datalen = csio->dxfer_len;
14243209f581SMatthew Dillon 	xa->timeout = ccbh->timeout;	/* milliseconds */
14251980eff3SMatthew Dillon 
1426b4189e5eSMatthew Dillon 	if (ccbh->flags & CAM_POLLED)
1427b4189e5eSMatthew Dillon 		xa->flags |= ATA_F_POLL;
1428258223a3SMatthew Dillon 
1429258223a3SMatthew Dillon 	/*
1430b4189e5eSMatthew Dillon 	 * Copy the cdb to the packetcmd buffer in the FIS using a
1431b4189e5eSMatthew Dillon 	 * convenient pointer in the xa.
1432a7d90c87SMatthew Dillon 	 *
1433a7d90c87SMatthew Dillon 	 * Zero-out any trailing bytes in case the ATAPI device cares.
1434258223a3SMatthew Dillon 	 */
1435b4189e5eSMatthew Dillon 	cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1436b4189e5eSMatthew Dillon 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1437b4189e5eSMatthew Dillon 	bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1438a7d90c87SMatthew Dillon 	if (csio->cdb_len < 16)
1439a7d90c87SMatthew Dillon 		bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len);
1440b4189e5eSMatthew Dillon 
1441669fbbf7SMatthew Dillon #if 0
1442b4189e5eSMatthew Dillon 	kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1443b4189e5eSMatthew Dillon 		cdbs->generic.opcode,
1444b4189e5eSMatthew Dillon 		csio->cdb_len, csio->dxfer_len);
1445669fbbf7SMatthew Dillon #endif
1446b4189e5eSMatthew Dillon 
1447b4189e5eSMatthew Dillon 	/*
1448b4189e5eSMatthew Dillon 	 * Some ATAPI commands do not actually follow the SCSI standard.
1449b4189e5eSMatthew Dillon 	 */
1450b4189e5eSMatthew Dillon 	cdbd = (void *)xa->packetcmd;
1451b4189e5eSMatthew Dillon 
1452b4189e5eSMatthew Dillon 	switch(cdbd->generic.opcode) {
1453192ee1d0SMatthew Dillon 	case REQUEST_SENSE:
1454192ee1d0SMatthew Dillon 		/*
1455192ee1d0SMatthew Dillon 		 * Force SENSE requests to the ATAPI sense length.
1456192ee1d0SMatthew Dillon 		 *
1457192ee1d0SMatthew Dillon 		 * It is unclear if this is needed or not.
1458192ee1d0SMatthew Dillon 		 */
1459192ee1d0SMatthew Dillon 		if (cdbd->sense.length == SSD_FULL_SIZE) {
1460b012a2caSMatthew Dillon 			if (bootverbose) {
1461192ee1d0SMatthew Dillon 				kprintf("%s: Shortening sense request\n",
1462192ee1d0SMatthew Dillon 					PORTNAME(ap));
1463b012a2caSMatthew Dillon 			}
1464192ee1d0SMatthew Dillon 			cdbd->sense.length = offsetof(struct scsi_sense_data,
1465192ee1d0SMatthew Dillon 						      extra_bytes[0]);
1466192ee1d0SMatthew Dillon 		}
1467192ee1d0SMatthew Dillon 		break;
1468258223a3SMatthew Dillon 	case INQUIRY:
1469b4189e5eSMatthew Dillon 		/*
1470b4189e5eSMatthew Dillon 		 * Some ATAPI devices can't handle long inquiry lengths,
1471b4189e5eSMatthew Dillon 		 * don't ask me why.  Truncate the inquiry length.
1472b4189e5eSMatthew Dillon 		 */
1473b4189e5eSMatthew Dillon 		if (cdbd->inquiry.page_code == 0 &&
1474b4189e5eSMatthew Dillon 		    cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1475b4189e5eSMatthew Dillon 			cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1476b4189e5eSMatthew Dillon 		}
1477b4189e5eSMatthew Dillon 		break;
1478258223a3SMatthew Dillon 	case READ_6:
1479258223a3SMatthew Dillon 	case WRITE_6:
1480b4189e5eSMatthew Dillon 		/*
1481b4189e5eSMatthew Dillon 		 * Convert *_6 to *_10 commands.  Most ATAPI devices
1482b4189e5eSMatthew Dillon 		 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1483b4189e5eSMatthew Dillon 		 */
1484b4189e5eSMatthew Dillon 		cdbd->rw_10.opcode |= 0x20;
1485b4189e5eSMatthew Dillon 		cdbd->rw_10.byte2 = 0;
1486b4189e5eSMatthew Dillon 		cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1487b4189e5eSMatthew Dillon 		cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1488b4189e5eSMatthew Dillon 		cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1489b4189e5eSMatthew Dillon 		cdbd->rw_10.addr[3] = 0;
1490b4189e5eSMatthew Dillon 		cdbd->rw_10.reserved = 0;
1491b4189e5eSMatthew Dillon 		cdbd->rw_10.length[0] = 0;
1492b4189e5eSMatthew Dillon 		cdbd->rw_10.length[1] = cdbs->rw_6.length;
1493b4189e5eSMatthew Dillon 		cdbd->rw_10.control = cdbs->rw_6.control;
1494b4189e5eSMatthew Dillon 		break;
1495258223a3SMatthew Dillon 	default:
1496258223a3SMatthew Dillon 		break;
1497258223a3SMatthew Dillon 	}
1498258223a3SMatthew Dillon 
1499b4189e5eSMatthew Dillon 	/*
1500b4189e5eSMatthew Dillon 	 * And dispatch
1501b4189e5eSMatthew Dillon 	 */
1502b4189e5eSMatthew Dillon 	xa->complete = ahci_atapi_complete_cmd;
1503258223a3SMatthew Dillon 	xa->atascsi_private = ccb;
1504258223a3SMatthew Dillon 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
150576497a9cSMatthew Dillon 	ahci_os_lock_port(ap);
1506258223a3SMatthew Dillon 	ahci_ata_cmd(xa);
150776497a9cSMatthew Dillon 	ahci_os_unlock_port(ap);
1508258223a3SMatthew Dillon }
1509258223a3SMatthew Dillon 
1510b4189e5eSMatthew Dillon /*
15116e0003adSMatthew Dillon  * Simulate page inquiries for disk attachments.
15126e0003adSMatthew Dillon  */
15136e0003adSMatthew Dillon static
15146e0003adSMatthew Dillon void
15156e0003adSMatthew Dillon ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
15166e0003adSMatthew Dillon {
15176e0003adSMatthew Dillon 	union {
15186e0003adSMatthew Dillon 		struct scsi_vpd_supported_page_list	list;
15196e0003adSMatthew Dillon 		struct scsi_vpd_unit_serial_number	serno;
15206e0003adSMatthew Dillon 		struct scsi_vpd_unit_devid		devid;
15216e0003adSMatthew Dillon 		char					buf[256];
15226e0003adSMatthew Dillon 	} *page;
15236e0003adSMatthew Dillon 	scsi_cdb_t cdb;
15246e0003adSMatthew Dillon 	int i;
15256e0003adSMatthew Dillon 	int j;
15266e0003adSMatthew Dillon 	int len;
15276e0003adSMatthew Dillon 
15286e0003adSMatthew Dillon 	page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
15296e0003adSMatthew Dillon 
15306e0003adSMatthew Dillon 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
15316e0003adSMatthew Dillon 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
15326e0003adSMatthew Dillon 
15336e0003adSMatthew Dillon 	switch(cdb->inquiry.page_code) {
15346e0003adSMatthew Dillon 	case SVPD_SUPPORTED_PAGE_LIST:
15356e0003adSMatthew Dillon 		i = 0;
15366e0003adSMatthew Dillon 		page->list.device = T_DIRECT;
15376e0003adSMatthew Dillon 		page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
15386e0003adSMatthew Dillon 		page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
15396e0003adSMatthew Dillon 		page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
15406e0003adSMatthew Dillon 		page->list.list[i++] = SVPD_UNIT_DEVID;
15416e0003adSMatthew Dillon 		page->list.length = i;
15426e0003adSMatthew Dillon 		len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
15436e0003adSMatthew Dillon 		break;
15446e0003adSMatthew Dillon 	case SVPD_UNIT_SERIAL_NUMBER:
15456e0003adSMatthew Dillon 		i = 0;
15466e0003adSMatthew Dillon 		j = sizeof(at->at_identify.serial);
15476e0003adSMatthew Dillon 		for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
15486e0003adSMatthew Dillon 			;
15496e0003adSMatthew Dillon 		while (j > i && at->at_identify.serial[j-1] == ' ')
15506e0003adSMatthew Dillon 			--j;
15516e0003adSMatthew Dillon 		page->serno.device = T_DIRECT;
15526e0003adSMatthew Dillon 		page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
15536e0003adSMatthew Dillon 		page->serno.length = j - i;
15546e0003adSMatthew Dillon 		bcopy(at->at_identify.serial + i,
15556e0003adSMatthew Dillon 		      page->serno.serial_num, j - i);
15566e0003adSMatthew Dillon 		len = offsetof(struct scsi_vpd_unit_serial_number,
15576e0003adSMatthew Dillon 			       serial_num[j-i]);
15586e0003adSMatthew Dillon 		break;
15596e0003adSMatthew Dillon 	case SVPD_UNIT_DEVID:
15606e0003adSMatthew Dillon 		/* fall through for now */
15616e0003adSMatthew Dillon 	default:
15626e0003adSMatthew Dillon 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
15636e0003adSMatthew Dillon 		len = 0;
15646e0003adSMatthew Dillon 		break;
15656e0003adSMatthew Dillon 	}
15666e0003adSMatthew Dillon 	if (ccb->ccb_h.status == CAM_REQ_INPROG) {
15676e0003adSMatthew Dillon 		if (len <= ccb->csio.dxfer_len) {
15686e0003adSMatthew Dillon 			ccb->ccb_h.status = CAM_REQ_CMP;
15696e0003adSMatthew Dillon 			bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
15706e0003adSMatthew Dillon 			bcopy(page, ccb->csio.data_ptr, len);
15716e0003adSMatthew Dillon 			ccb->csio.resid = ccb->csio.dxfer_len - len;
15726e0003adSMatthew Dillon 		} else {
15736e0003adSMatthew Dillon 			ccb->ccb_h.status = CAM_CCB_LEN_ERR;
15746e0003adSMatthew Dillon 		}
15756e0003adSMatthew Dillon 	}
15766e0003adSMatthew Dillon 	kfree(page, M_DEVBUF);
15776e0003adSMatthew Dillon }
15786e0003adSMatthew Dillon 
15796e0003adSMatthew Dillon /*
1580b4189e5eSMatthew Dillon  * Completion function for ATA_PORT_T_DISK cache synchronization.
1581b4189e5eSMatthew Dillon  */
1582258223a3SMatthew Dillon static
1583258223a3SMatthew Dillon void
1584258223a3SMatthew Dillon ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1585258223a3SMatthew Dillon {
1586258223a3SMatthew Dillon 	union ccb *ccb = xa->atascsi_private;
1587258223a3SMatthew Dillon 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1588258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1589258223a3SMatthew Dillon 
1590258223a3SMatthew Dillon 	switch(xa->state) {
1591258223a3SMatthew Dillon 	case ATA_S_COMPLETE:
1592258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
1593b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1594258223a3SMatthew Dillon 		break;
1595258223a3SMatthew Dillon 	case ATA_S_ERROR:
15961980eff3SMatthew Dillon 		kprintf("%s: synchronize_cache: error\n",
15971980eff3SMatthew Dillon 			ATANAME(ap, xa->at));
1598b4189e5eSMatthew Dillon 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1599b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1600b4189e5eSMatthew Dillon 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1601258223a3SMatthew Dillon 		break;
1602258223a3SMatthew Dillon 	case ATA_S_TIMEOUT:
16031980eff3SMatthew Dillon 		kprintf("%s: synchronize_cache: timeout\n",
16041980eff3SMatthew Dillon 			ATANAME(ap, xa->at));
1605258223a3SMatthew Dillon 		ccbh->status = CAM_CMD_TIMEOUT;
1606258223a3SMatthew Dillon 		break;
1607258223a3SMatthew Dillon 	default:
1608258223a3SMatthew Dillon 		kprintf("%s: synchronize_cache: unknown state %d\n",
16091980eff3SMatthew Dillon 			ATANAME(ap, xa->at), xa->state);
1610258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP_ERR;
1611258223a3SMatthew Dillon 		break;
1612258223a3SMatthew Dillon 	}
1613258223a3SMatthew Dillon 	ahci_ata_put_xfer(xa);
1614f4553de1SMatthew Dillon 	ahci_os_unlock_port(ap);
1615258223a3SMatthew Dillon 	xpt_done(ccb);
1616f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
1617258223a3SMatthew Dillon }
1618258223a3SMatthew Dillon 
1619b4189e5eSMatthew Dillon /*
1620b4189e5eSMatthew Dillon  * Completion function for ATA_PORT_T_DISK I/O
1621b4189e5eSMatthew Dillon  */
1622258223a3SMatthew Dillon static
1623258223a3SMatthew Dillon void
1624258223a3SMatthew Dillon ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1625258223a3SMatthew Dillon {
1626258223a3SMatthew Dillon 	union ccb *ccb = xa->atascsi_private;
1627258223a3SMatthew Dillon 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1628258223a3SMatthew Dillon 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1629258223a3SMatthew Dillon 
1630258223a3SMatthew Dillon 	switch(xa->state) {
1631258223a3SMatthew Dillon 	case ATA_S_COMPLETE:
1632258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
1633b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1634258223a3SMatthew Dillon 		break;
1635258223a3SMatthew Dillon 	case ATA_S_ERROR:
16361980eff3SMatthew Dillon 		kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1637b4189e5eSMatthew Dillon 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1638b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1639b4189e5eSMatthew Dillon 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1640258223a3SMatthew Dillon 		break;
1641258223a3SMatthew Dillon 	case ATA_S_TIMEOUT:
16421980eff3SMatthew Dillon 		kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1643258223a3SMatthew Dillon 		ccbh->status = CAM_CMD_TIMEOUT;
16444c339a5fSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
16454c339a5fSMatthew Dillon 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1646258223a3SMatthew Dillon 		break;
1647258223a3SMatthew Dillon 	default:
1648258223a3SMatthew Dillon 		kprintf("%s: disk_rw: unknown state %d\n",
16491980eff3SMatthew Dillon 			ATANAME(ap, xa->at), xa->state);
1650258223a3SMatthew Dillon 		ccbh->status = CAM_REQ_CMP_ERR;
1651258223a3SMatthew Dillon 		break;
1652258223a3SMatthew Dillon 	}
1653258223a3SMatthew Dillon 	ccb->csio.resid = xa->resid;
1654258223a3SMatthew Dillon 	ahci_ata_put_xfer(xa);
1655f4553de1SMatthew Dillon 	ahci_os_unlock_port(ap);
1656258223a3SMatthew Dillon 	xpt_done(ccb);
1657f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
1658258223a3SMatthew Dillon }
1659b4189e5eSMatthew Dillon 
16607d4fcf34SMatthew Dillon /*
16617d4fcf34SMatthew Dillon  * Completion function for ATA_PORT_T_ATAPI I/O
16627d4fcf34SMatthew Dillon  *
16637d4fcf34SMatthew Dillon  * Sense data is returned in the rfis.
16647d4fcf34SMatthew Dillon  */
1665b4189e5eSMatthew Dillon static
1666b4189e5eSMatthew Dillon void
1667b4189e5eSMatthew Dillon ahci_atapi_complete_cmd(struct ata_xfer *xa)
1668b4189e5eSMatthew Dillon {
1669b4189e5eSMatthew Dillon 	union ccb *ccb = xa->atascsi_private;
1670b4189e5eSMatthew Dillon 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1671b4189e5eSMatthew Dillon 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1672b4189e5eSMatthew Dillon 	scsi_cdb_t cdb;
1673b4189e5eSMatthew Dillon 
1674b4189e5eSMatthew Dillon 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1675b4189e5eSMatthew Dillon 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1676b4189e5eSMatthew Dillon 
1677b4189e5eSMatthew Dillon 	switch(xa->state) {
1678b4189e5eSMatthew Dillon 	case ATA_S_COMPLETE:
1679b4189e5eSMatthew Dillon 		ccbh->status = CAM_REQ_CMP;
1680b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1681b4189e5eSMatthew Dillon 		break;
1682b4189e5eSMatthew Dillon 	case ATA_S_ERROR:
1683b4189e5eSMatthew Dillon 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1684b4189e5eSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
16857d4fcf34SMatthew Dillon 		ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1686b4189e5eSMatthew Dillon 		break;
1687b4189e5eSMatthew Dillon 	case ATA_S_TIMEOUT:
1688b4189e5eSMatthew Dillon 		kprintf("%s: cmd %d: timeout\n",
1689b4189e5eSMatthew Dillon 			PORTNAME(ap), cdb->generic.opcode);
1690b4189e5eSMatthew Dillon 		ccbh->status = CAM_CMD_TIMEOUT;
16914c339a5fSMatthew Dillon 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
16924c339a5fSMatthew Dillon 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1693b4189e5eSMatthew Dillon 		break;
1694b4189e5eSMatthew Dillon 	default:
1695b4189e5eSMatthew Dillon 		kprintf("%s: cmd %d: unknown state %d\n",
1696b4189e5eSMatthew Dillon 			PORTNAME(ap), cdb->generic.opcode, xa->state);
1697b4189e5eSMatthew Dillon 		ccbh->status = CAM_REQ_CMP_ERR;
1698b4189e5eSMatthew Dillon 		break;
1699b4189e5eSMatthew Dillon 	}
1700b4189e5eSMatthew Dillon 	ccb->csio.resid = xa->resid;
1701b4189e5eSMatthew Dillon 	ahci_ata_put_xfer(xa);
1702f4553de1SMatthew Dillon 	ahci_os_unlock_port(ap);
1703b4189e5eSMatthew Dillon 	xpt_done(ccb);
1704f4553de1SMatthew Dillon 	ahci_os_lock_port(ap);
1705b4189e5eSMatthew Dillon }
1706b4189e5eSMatthew Dillon 
17077d4fcf34SMatthew Dillon /*
17087d4fcf34SMatthew Dillon  * Construct dummy sense data for errors on DISKs
17097d4fcf34SMatthew Dillon  */
1710b4189e5eSMatthew Dillon static
1711b4189e5eSMatthew Dillon void
1712b4189e5eSMatthew Dillon ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1713b4189e5eSMatthew Dillon {
1714b4189e5eSMatthew Dillon 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1715b4189e5eSMatthew Dillon 	sense_data->segment = 0;
1716b4189e5eSMatthew Dillon 	sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1717b4189e5eSMatthew Dillon 	sense_data->info[0] = 0;
1718b4189e5eSMatthew Dillon 	sense_data->info[1] = 0;
1719b4189e5eSMatthew Dillon 	sense_data->info[2] = 0;
1720b4189e5eSMatthew Dillon 	sense_data->info[3] = 0;
1721b4189e5eSMatthew Dillon 	sense_data->extra_len = 0;
1722b4189e5eSMatthew Dillon }
17237d4fcf34SMatthew Dillon 
17247d4fcf34SMatthew Dillon /*
17257d4fcf34SMatthew Dillon  * Construct atapi sense data for errors on ATAPI
17267d4fcf34SMatthew Dillon  *
17277d4fcf34SMatthew Dillon  * The ATAPI sense data is stored in the passed rfis and must be converted
17287d4fcf34SMatthew Dillon  * to SCSI sense data.
17297d4fcf34SMatthew Dillon  */
17307d4fcf34SMatthew Dillon static
17317d4fcf34SMatthew Dillon void
17327d4fcf34SMatthew Dillon ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
17337d4fcf34SMatthew Dillon 		     struct scsi_sense_data *sense_data)
17347d4fcf34SMatthew Dillon {
17357d4fcf34SMatthew Dillon 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
17367d4fcf34SMatthew Dillon 	sense_data->segment = 0;
17377d4fcf34SMatthew Dillon 	sense_data->flags = (rfis->error & 0xF0) >> 4;
17387d4fcf34SMatthew Dillon 	if (rfis->error & 0x04)
17397d4fcf34SMatthew Dillon 		sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
17407d4fcf34SMatthew Dillon 	if (rfis->error & 0x02)
17417d4fcf34SMatthew Dillon 		sense_data->flags |= SSD_EOM;
17427d4fcf34SMatthew Dillon 	if (rfis->error & 0x01)
17437d4fcf34SMatthew Dillon 		sense_data->flags |= SSD_ILI;
17447d4fcf34SMatthew Dillon 	sense_data->info[0] = 0;
17457d4fcf34SMatthew Dillon 	sense_data->info[1] = 0;
17467d4fcf34SMatthew Dillon 	sense_data->info[2] = 0;
17477d4fcf34SMatthew Dillon 	sense_data->info[3] = 0;
17487d4fcf34SMatthew Dillon 	sense_data->extra_len = 0;
17497d4fcf34SMatthew Dillon }
17506e0003adSMatthew Dillon 
17516e0003adSMatthew Dillon static
17526e0003adSMatthew Dillon void
17536e0003adSMatthew Dillon ahci_strip_string(const char **basep, int *lenp)
17546e0003adSMatthew Dillon {
17556e0003adSMatthew Dillon 	const char *base = *basep;
17566e0003adSMatthew Dillon 	int len = *lenp;
17576e0003adSMatthew Dillon 
17586e0003adSMatthew Dillon 	while (len && (*base == 0 || *base == ' ')) {
17596e0003adSMatthew Dillon 		--len;
17606e0003adSMatthew Dillon 		++base;
17616e0003adSMatthew Dillon 	}
17626e0003adSMatthew Dillon 	while (len && (base[len-1] == 0 || base[len-1] == ' '))
17636e0003adSMatthew Dillon 		--len;
17646e0003adSMatthew Dillon 	*basep = base;
17656e0003adSMatthew Dillon 	*lenp = len;
17666e0003adSMatthew Dillon }
1767