xref: /dragonfly/sys/dev/disk/ahci/ahci_cam.c (revision e96fb831)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *
37  * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
38  *
39  * Permission to use, copy, modify, and distribute this software for any
40  * purpose with or without fee is hereby granted, provided that the above
41  * copyright notice and this permission notice appear in all copies.
42  *
43  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
44  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
46  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
47  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
48  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
49  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
50  *
51  * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
52  * $DragonFly$
53  */
54 /*
55  * Implement each SATA port as its own SCSI bus on CAM.  This way we can
56  * implement future port multiplier features as individual devices on the
57  * bus.
58  *
59  * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
60  * was written natively for DragonFly.
61  */
62 
63 #include "ahci.h"
64 
65 static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
66 static void ahci_xpt_poll(struct cam_sim *sim);
67 static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
68 			struct ata_port *at, union ccb *ccb);
69 static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
70 			struct ata_port *at, union ccb *ccb);
71 static void ahci_xpt_page_inquiry(struct ahci_port *ap,
72 			struct ata_port *at, union ccb *ccb);
73 
74 static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
75 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
76 static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
77 static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
78 static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
79 		     struct scsi_sense_data *sense_data);
80 
81 static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
82 static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
83 static int ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx);
84 static void ahci_ata_dummy_done(struct ata_xfer *xa);
85 static void ata_fix_identify(struct ata_identify *id);
86 static void ahci_cam_rescan(struct ahci_port *ap);
87 static void ahci_strip_string(const char **basep, int *lenp);
88 
89 int
90 ahci_cam_attach(struct ahci_port *ap)
91 {
92 	struct cam_devq *devq;
93 	struct cam_sim *sim;
94 	int error;
95 	int unit;
96 
97 	/*
98 	 * We want at least one ccb to be available for error processing
99 	 * so don't let CAM use more then ncmds - 1.
100 	 */
101 	unit = device_get_unit(ap->ap_sc->sc_dev);
102 	if (ap->ap_sc->sc_ncmds > 1)
103 		devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
104 	else
105 		devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
106 	if (devq == NULL) {
107 		return (ENOMEM);
108 	}
109 
110 	/*
111 	 * Give the devq enough room to run with 32 max_dev_transactions,
112 	 * but set the overall max tags to 1 until NCQ is negotiated.
113 	 */
114 	sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
115 			   (void *)ap, unit, &ap->ap_sim_lock,
116 			   32, 1, devq);
117 	cam_simq_release(devq);
118 	if (sim == NULL) {
119 		return (ENOMEM);
120 	}
121 	ap->ap_sim = sim;
122 	ahci_os_unlock_port(ap);
123 	lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
124 	error = xpt_bus_register(ap->ap_sim, ap->ap_num);
125 	lockmgr(&ap->ap_sim_lock, LK_RELEASE);
126 	ahci_os_lock_port(ap);
127 	if (error != CAM_SUCCESS) {
128 		ahci_cam_detach(ap);
129 		return (EINVAL);
130 	}
131 	ap->ap_flags |= AP_F_BUS_REGISTERED;
132 
133 	if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
134 		error = ahci_cam_probe(ap, NULL);
135 	else
136 		error = 0;
137 	if (error) {
138 		ahci_cam_detach(ap);
139 		return (EIO);
140 	}
141 	ap->ap_flags |= AP_F_CAM_ATTACHED;
142 
143 	return(0);
144 }
145 
146 /*
147  * The state of the port has changed.
148  *
149  * If at is NULL the physical port has changed state.
150  * If at is non-NULL a particular target behind a PM has changed state.
151  *
152  * If found is -1 the target state must be queued to a non-interrupt context.
153  * (only works with at == NULL).
154  *
155  * If found is 0 the target was removed.
156  * If found is 1 the target was inserted.
157  */
158 void
159 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
160 {
161 	struct cam_path *tmppath;
162 	int status;
163 	int target;
164 
165 	target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
166 
167 	if (ap->ap_sim == NULL)
168 		return;
169 	if (found == CAM_TARGET_WILDCARD) {
170 		status = xpt_create_path(&tmppath, NULL,
171 					 cam_sim_path(ap->ap_sim),
172 					 target, CAM_LUN_WILDCARD);
173 		if (status != CAM_REQ_CMP)
174 			return;
175 		ahci_cam_rescan(ap);
176 	} else {
177 		status = xpt_create_path(&tmppath, NULL,
178 					 cam_sim_path(ap->ap_sim),
179 					 target,
180 					 CAM_LUN_WILDCARD);
181 		if (status != CAM_REQ_CMP)
182 			return;
183 #if 0
184 		/*
185 		 * This confuses CAM
186 		 */
187 		if (found)
188 			xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
189 		else
190 			xpt_async(AC_LOST_DEVICE, tmppath, NULL);
191 #endif
192 	}
193 	xpt_free_path(tmppath);
194 }
195 
196 void
197 ahci_cam_detach(struct ahci_port *ap)
198 {
199 	int error;
200 
201 	if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
202 		return;
203 	lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
204 	if (ap->ap_sim) {
205 		xpt_freeze_simq(ap->ap_sim, 1);
206 	}
207 	if (ap->ap_flags & AP_F_BUS_REGISTERED) {
208 		error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
209 		KKASSERT(error == CAM_REQ_CMP);
210 		ap->ap_flags &= ~AP_F_BUS_REGISTERED;
211 	}
212 	if (ap->ap_sim) {
213 		cam_sim_free(ap->ap_sim);
214 		ap->ap_sim = NULL;
215 	}
216 	lockmgr(&ap->ap_sim_lock, LK_RELEASE);
217 	ap->ap_flags &= ~AP_F_CAM_ATTACHED;
218 }
219 
220 /*
221  * Once the AHCI port has been attached we need to probe for a device or
222  * devices on the port and setup various options.
223  *
224  * If at is NULL we are probing the direct-attached device on the port,
225  * which may or may not be a port multiplier.
226  */
227 int
228 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
229 {
230 	struct ata_port	*at;
231 	struct ata_xfer	*xa;
232 	u_int64_t	capacity;
233 	u_int64_t	capacity_bytes;
234 	int		model_len;
235 	int		firmware_len;
236 	int		serial_len;
237 	int		error;
238 	int		devncqdepth;
239 	int		i;
240 	const char	*model_id;
241 	const char	*firmware_id;
242 	const char	*serial_id;
243 	const char	*wcstr;
244 	const char	*rastr;
245 	const char	*scstr;
246 	const char	*type;
247 
248 	error = EIO;
249 
250 	/*
251 	 * Delayed CAM attachment for initial probe, sim may be NULL
252 	 */
253 	if (ap->ap_sim == NULL)
254 		return(0);
255 
256 	/*
257 	 * A NULL atx indicates a probe of the directly connected device.
258 	 * A non-NULL atx indicates a device connected via a port multiplier.
259 	 * We need to preserve atx for calls to ahci_ata_get_xfer().
260 	 *
261 	 * at is always non-NULL.  For directly connected devices we supply
262 	 * an (at) pointing to target 0.
263 	 */
264 	if (atx == NULL) {
265 		at = ap->ap_ata[0];	/* direct attached - device 0 */
266 		if (ap->ap_type == ATA_PORT_T_PM) {
267 			kprintf("%s: Found Port Multiplier\n",
268 				ATANAME(ap, atx));
269 			return (0);
270 		}
271 		at->at_type = ap->ap_type;
272 	} else {
273 		at = atx;
274 		if (atx->at_type == ATA_PORT_T_PM) {
275 			kprintf("%s: Bogus device, reducing port count to %d\n",
276 				ATANAME(ap, atx), atx->at_target);
277 			if (ap->ap_pmcount > atx->at_target)
278 				ap->ap_pmcount = atx->at_target;
279 			goto err;
280 		}
281 	}
282 	if (ap->ap_type == ATA_PORT_T_NONE)
283 		goto err;
284 	if (at->at_type == ATA_PORT_T_NONE)
285 		goto err;
286 
287 	/*
288 	 * Issue identify, saving the result
289 	 */
290 	xa = ahci_ata_get_xfer(ap, atx);
291 	xa->complete = ahci_ata_dummy_done;
292 	xa->data = &at->at_identify;
293 	xa->datalen = sizeof(at->at_identify);
294 	xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
295 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
296 
297 	switch(at->at_type) {
298 	case ATA_PORT_T_DISK:
299 		xa->fis->command = ATA_C_IDENTIFY;
300 		type = "DISK";
301 		break;
302 	case ATA_PORT_T_ATAPI:
303 		xa->fis->command = ATA_C_ATAPI_IDENTIFY;
304 		xa->flags |= ATA_F_AUTOSENSE;
305 		type = "ATAPI";
306 		break;
307 	default:
308 		xa->fis->command = ATA_C_ATAPI_IDENTIFY;
309 		type = "UNKNOWN(ATAPI?)";
310 		break;
311 	}
312 	xa->fis->features = 0;
313 	xa->fis->device = 0;
314 	xa->timeout = 1000;
315 
316 	if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
317 		kprintf("%s: Detected %s device but unable to IDENTIFY\n",
318 			ATANAME(ap, atx), type);
319 		ahci_ata_put_xfer(xa);
320 		goto err;
321 	}
322 	ahci_ata_put_xfer(xa);
323 
324 	ata_fix_identify(&at->at_identify);
325 
326 	/*
327 	 * Read capacity using SATA probe info.
328 	 */
329 	if (le16toh(at->at_identify.cmdset83) & 0x0400) {
330 		/* LBA48 feature set supported */
331 		capacity = 0;
332 		for (i = 3; i >= 0; --i) {
333 			capacity <<= 16;
334 			capacity +=
335 			    le16toh(at->at_identify.addrsecxt[i]);
336 		}
337 	} else {
338 		capacity = le16toh(at->at_identify.addrsec[1]);
339 		capacity <<= 16;
340 		capacity += le16toh(at->at_identify.addrsec[0]);
341 	}
342 	if (capacity == 0)
343 		capacity = 1024 * 1024 / 512;
344 	at->at_capacity = capacity;
345 	if (atx == NULL)
346 		ap->ap_probe = ATA_PROBE_GOOD;
347 
348 	capacity_bytes = capacity * 512;
349 
350 	/*
351 	 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
352 	 * number of slots and limit the number of CAM ccb's to one less
353 	 * so we always have a slot available for recovery.
354 	 *
355 	 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
356 	 * not support it, and in that case the driver can handle extra
357 	 * ccb's.
358 	 *
359 	 * NCQ is currently used only with direct-attached disks.  It is
360 	 * not used with port multipliers or direct-attached ATAPI devices.
361 	 *
362 	 * Remember at least one extra CCB needs to be reserved for the
363 	 * error ccb.
364 	 */
365 	if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
366 	    ap->ap_type == ATA_PORT_T_DISK &&
367 	    (le16toh(at->at_identify.satacap) & (1 << 8))) {
368 		at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
369 		devncqdepth = at->at_ncqdepth;
370 		if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
371 			at->at_ncqdepth = ap->ap_sc->sc_ncmds;
372 		if (at->at_ncqdepth > 1) {
373 			for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
374 				xa = ahci_ata_get_xfer(ap, atx);
375 				if (xa->tag < at->at_ncqdepth) {
376 					xa->state = ATA_S_COMPLETE;
377 					ahci_ata_put_xfer(xa);
378 				}
379 			}
380 			if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
381 				cam_sim_set_max_tags(ap->ap_sim,
382 						     at->at_ncqdepth - 1);
383 			}
384 		}
385 	} else {
386 		devncqdepth = 0;
387 	}
388 
389 	model_len = sizeof(at->at_identify.model);
390 	model_id = at->at_identify.model;
391 	ahci_strip_string(&model_id, &model_len);
392 
393 	firmware_len = sizeof(at->at_identify.firmware);
394 	firmware_id = at->at_identify.firmware;
395 	ahci_strip_string(&firmware_id, &firmware_len);
396 
397 	serial_len = sizeof(at->at_identify.serial);
398 	serial_id = at->at_identify.serial;
399 	ahci_strip_string(&serial_id, &serial_len);
400 
401 	/*
402 	 * Generate informatiive strings.
403 	 *
404 	 * NOTE: We do not automatically set write caching, lookahead,
405 	 *	 or the security state for ATAPI devices.
406 	 */
407 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
408 		if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
409 			wcstr = "enabled";
410 		else if (at->at_type == ATA_PORT_T_ATAPI)
411 			wcstr = "disabled";
412 		else
413 			wcstr = "enabling";
414 	} else {
415 		    wcstr = "notsupp";
416 	}
417 
418 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
419 		if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
420 			rastr = "enabled";
421 		else if (at->at_type == ATA_PORT_T_ATAPI)
422 			rastr = "disabled";
423 		else
424 			rastr = "enabling";
425 	} else {
426 		    rastr = "notsupp";
427 	}
428 
429 	if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
430 		if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
431 			scstr = "frozen";
432 		else if (at->at_type == ATA_PORT_T_ATAPI)
433 			scstr = "unfrozen";
434 		else if (AhciNoFeatures & (1 << ap->ap_num))
435 			scstr = "<disabled>";
436 		else
437 			scstr = "freezing";
438 	} else {
439 		    scstr = "notsupp";
440 	}
441 
442 	kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
443 		"%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
444 		"capacity=%lld.%02dMB\n",
445 
446 		ATANAME(ap, atx),
447 		type,
448 		model_len, model_len, model_id,
449 		firmware_len, firmware_len, firmware_id,
450 		serial_len, serial_len, serial_id,
451 
452 		ATANAME(ap, atx),
453 		devncqdepth, ap->ap_sc->sc_ncmds,
454 		at->at_identify.satacap,
455 		at->at_identify.satafsup,
456 		(at->at_ncqdepth > 1 ? "YES" : "NO"),
457 		(long long)capacity_bytes / (1024 * 1024),
458 		(int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
459 	);
460 	kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
461 		ATANAME(ap, atx),
462 		at->at_identify.features85,
463 		at->at_identify.features86,
464 		at->at_identify.features87,
465 		wcstr,
466 		rastr,
467 		scstr
468 	);
469 
470 	/*
471 	 * Additional type-specific probing
472 	 */
473 	switch(at->at_type) {
474 	case ATA_PORT_T_DISK:
475 		error = ahci_cam_probe_disk(ap, atx);
476 		break;
477 	case ATA_PORT_T_ATAPI:
478 		error = ahci_cam_probe_atapi(ap, atx);
479 		break;
480 	default:
481 		error = EIO;
482 		break;
483 	}
484 err:
485 	if (error) {
486 		at->at_probe = ATA_PROBE_FAILED;
487 		if (atx == NULL)
488 			ap->ap_probe = at->at_probe;
489 	} else {
490 		at->at_probe = ATA_PROBE_GOOD;
491 		if (atx == NULL)
492 			ap->ap_probe = at->at_probe;
493 	}
494 	return (error);
495 }
496 
497 /*
498  * DISK-specific probe after initial ident
499  */
500 static int
501 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
502 {
503 	struct ata_port *at;
504 	struct ata_xfer	*xa;
505 
506 	at = atx ? atx : ap->ap_ata[0];
507 
508 	/*
509 	 * Set dummy xfer mode
510 	 */
511 	ahci_set_xfer(ap, atx);
512 
513 	/*
514 	 * Enable write cache if supported
515 	 *
516 	 * NOTE: "WD My Book" external disk devices have a very poor
517 	 *	 daughter board between the the ESATA and the HD.  Sending
518 	 *	 any ATA_C_SET_FEATURES commands will break the hardware port
519 	 *	 with a fatal protocol error.  However, this device also
520 	 *	 indicates that WRITECACHE is already on and READAHEAD is
521 	 *	 not supported so we avoid the issue.
522 	 */
523 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
524 	    (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
525 		xa = ahci_ata_get_xfer(ap, atx);
526 		xa->complete = ahci_ata_dummy_done;
527 		xa->fis->command = ATA_C_SET_FEATURES;
528 		xa->fis->features = ATA_SF_WRITECACHE_EN;
529 		/* xa->fis->features = ATA_SF_LOOKAHEAD_EN; */
530 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
531 		xa->fis->device = 0;
532 		xa->flags = ATA_F_PIO | ATA_F_POLL;
533 		xa->timeout = 1000;
534 		xa->datalen = 0;
535 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
536 			at->at_features |= ATA_PORT_F_WCACHE;
537 		else
538 			kprintf("%s: Unable to enable write-caching\n",
539 				ATANAME(ap, atx));
540 		ahci_ata_put_xfer(xa);
541 	}
542 
543 	/*
544 	 * Enable readahead if supported
545 	 */
546 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
547 	    (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
548 		xa = ahci_ata_get_xfer(ap, atx);
549 		xa->complete = ahci_ata_dummy_done;
550 		xa->fis->command = ATA_C_SET_FEATURES;
551 		xa->fis->features = ATA_SF_LOOKAHEAD_EN;
552 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
553 		xa->fis->device = 0;
554 		xa->flags = ATA_F_PIO | ATA_F_POLL;
555 		xa->timeout = 1000;
556 		xa->datalen = 0;
557 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
558 			at->at_features |= ATA_PORT_F_RAHEAD;
559 		else
560 			kprintf("%s: Unable to enable read-ahead\n",
561 				ATANAME(ap, atx));
562 		ahci_ata_put_xfer(xa);
563 	}
564 
565 	/*
566 	 * FREEZE LOCK the device so malicious users can't lock it on us.
567 	 * As there is no harm in issuing this to devices that don't
568 	 * support the security feature set we just send it, and don't bother
569 	 * checking if the device sends a command abort to tell us it doesn't
570 	 * support it
571 	 */
572 	if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
573 	    (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
574 	    (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
575 		xa = ahci_ata_get_xfer(ap, atx);
576 		xa->complete = ahci_ata_dummy_done;
577 		xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
578 		xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
579 		xa->flags = ATA_F_PIO | ATA_F_POLL;
580 		xa->timeout = 1000;
581 		xa->datalen = 0;
582 		if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
583 			at->at_features |= ATA_PORT_F_FRZLCK;
584 		else
585 			kprintf("%s: Unable to set security freeze\n",
586 				ATANAME(ap, atx));
587 		ahci_ata_put_xfer(xa);
588 	}
589 
590 	return (0);
591 }
592 
593 /*
594  * ATAPI-specific probe after initial ident
595  */
596 static int
597 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
598 {
599 	ahci_set_xfer(ap, atx);
600 	return(0);
601 }
602 
603 /*
604  * Setting the transfer mode is irrelevant for the SATA transport
605  * but some (atapi) devices seem to need it anyway.  In addition
606  * if we are running through a SATA->PATA converter for some reason
607  * beyond my comprehension we might have to set the mode.
608  *
609  * We only support DMA modes for SATA attached devices, so don't bother
610  * with legacy modes.
611  */
612 static int
613 ahci_set_xfer(struct ahci_port *ap, struct ata_port *atx)
614 {
615 	struct ata_port *at;
616 	struct ata_xfer	*xa;
617 	u_int16_t mode;
618 	u_int16_t mask;
619 
620 	at = atx ? atx : ap->ap_ata[0];
621 
622 	/*
623 	 * Figure out the supported UDMA mode.  Ignore other legacy modes.
624 	 */
625 	mask = le16toh(at->at_identify.ultradma);
626 	if ((mask & 0xFF) == 0 || mask == 0xFFFF)
627 		return(0);
628 	mask &= 0xFF;
629 	mode = 0x4F;
630 	while ((mask & 0x8000) == 0) {
631 		mask <<= 1;
632 		--mode;
633 	}
634 
635 	/*
636 	 * SATA atapi devices often still report a dma mode, even though
637 	 * it is irrelevant for SATA transport.  It is also possible that
638 	 * we are running through a SATA->PATA converter and seeing the
639 	 * PATA dma mode.
640 	 *
641 	 * In this case the device may require a (dummy) SETXFER to be
642 	 * sent before it will work properly.
643 	 */
644 	xa = ahci_ata_get_xfer(ap, atx);
645 	xa->complete = ahci_ata_dummy_done;
646 	xa->fis->command = ATA_C_SET_FEATURES;
647 	xa->fis->features = ATA_SF_SETXFER;
648 	xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
649 	xa->fis->sector_count = mode;
650 	xa->flags = ATA_F_PIO | ATA_F_POLL;
651 	xa->timeout = 1000;
652 	xa->datalen = 0;
653 	if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
654 		kprintf("%s: Unable to set dummy xfer mode \n",
655 			ATANAME(ap, atx));
656 	} else if (bootverbose) {
657 		kprintf("%s: Set dummy xfer mode to %02x\n",
658 			ATANAME(ap, atx), mode);
659 	}
660 	ahci_ata_put_xfer(xa);
661 	return(0);
662 }
663 
664 /*
665  * Fix byte ordering so buffers can be accessed as
666  * strings.
667  */
668 static void
669 ata_fix_identify(struct ata_identify *id)
670 {
671 	u_int16_t	*swap;
672 	int		i;
673 
674 	swap = (u_int16_t *)id->serial;
675 	for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
676 		swap[i] = bswap16(swap[i]);
677 
678 	swap = (u_int16_t *)id->firmware;
679 	for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
680 		swap[i] = bswap16(swap[i]);
681 
682 	swap = (u_int16_t *)id->model;
683 	for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
684 		swap[i] = bswap16(swap[i]);
685 }
686 
687 /*
688  * Dummy done callback for xa.
689  */
690 static void
691 ahci_ata_dummy_done(struct ata_xfer *xa)
692 {
693 }
694 
695 /*
696  * Use an engineering request to initiate a target scan for devices
697  * behind a port multiplier.
698  *
699  * An asynchronous bus scan is used to avoid reentrancy issues.
700  */
701 static void
702 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
703 {
704 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
705 
706 	if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
707 		ap->ap_flags &= ~AP_F_SCAN_RUNNING;
708 		if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
709 			ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
710 			ahci_cam_rescan(ap);
711 		}
712 		ap->ap_flags |= AP_F_SCAN_COMPLETED;
713 		wakeup(&ap->ap_flags);
714 	}
715 	xpt_free_ccb(ccb);
716 }
717 
718 static void
719 ahci_cam_rescan(struct ahci_port *ap)
720 {
721 	struct cam_path *path;
722 	union ccb *ccb;
723 	int status;
724 	int i;
725 
726 	if (ap->ap_flags & AP_F_SCAN_RUNNING) {
727 		ap->ap_flags |= AP_F_SCAN_REQUESTED;
728 		return;
729 	}
730 	ap->ap_flags |= AP_F_SCAN_RUNNING;
731 	for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
732 		ap->ap_ata[i]->at_features |= ATA_PORT_F_RESCAN;
733 	}
734 
735 	status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
736 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
737 	if (status != CAM_REQ_CMP)
738 		return;
739 
740 	ccb = xpt_alloc_ccb();
741 	xpt_setup_ccb(&ccb->ccb_h, path, 5);	/* 5 = low priority */
742 	ccb->ccb_h.func_code = XPT_ENG_EXEC;
743 	ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
744 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
745 	ccb->crcn.flags = CAM_FLAG_NONE;
746 	xpt_action_async(ccb);
747 }
748 
749 static void
750 ahci_xpt_rescan(struct ahci_port *ap)
751 {
752 	struct cam_path *path;
753 	union ccb *ccb;
754 	int status;
755 
756 	status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
757 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
758 	if (status != CAM_REQ_CMP)
759 		return;
760 
761 	ccb = xpt_alloc_ccb();
762 	xpt_setup_ccb(&ccb->ccb_h, path, 5);	/* 5 = low priority */
763 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
764 	ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
765 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
766 	ccb->crcn.flags = CAM_FLAG_NONE;
767 	xpt_action_async(ccb);
768 }
769 
770 /*
771  * Action function - dispatch command
772  */
773 static
774 void
775 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
776 {
777 	struct ahci_port *ap;
778 	struct ata_port	 *at, *atx;
779 	struct ccb_hdr *ccbh;
780 	int unit;
781 
782 	/* XXX lock */
783 	ap = cam_sim_softc(sim);
784 	atx = NULL;
785 	KKASSERT(ap != NULL);
786 	ccbh = &ccb->ccb_h;
787 	unit = cam_sim_unit(sim);
788 
789 	/*
790 	 * Early failure checks.  These checks do not apply to XPT_PATH_INQ,
791 	 * otherwise the bus rescan will not remove the dead devices when
792 	 * unplugging a PM.
793 	 *
794 	 * For non-wildcards we have one target (0) and one lun (0),
795 	 * unless we have a port multiplier.
796 	 *
797 	 * A wildcard target indicates only the general bus is being
798 	 * probed.
799 	 *
800 	 * Calculate at and atx.  at is always non-NULL.  atx is only
801 	 * non-NULL for direct-attached devices.  It will be NULL for
802 	 * devices behind a port multiplier.
803 	 *
804 	 * XXX What do we do with a LUN wildcard?
805 	 */
806 	if (ccbh->target_id != CAM_TARGET_WILDCARD &&
807 	    ccbh->func_code != XPT_PATH_INQ) {
808 		if (ap->ap_type == ATA_PORT_T_NONE) {
809 			ccbh->status = CAM_DEV_NOT_THERE;
810 			xpt_done(ccb);
811 			return;
812 		}
813 		if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
814 			ccbh->status = CAM_DEV_NOT_THERE;
815 			xpt_done(ccb);
816 			return;
817 		}
818 		at = ap->ap_ata[ccbh->target_id];
819 		if (ap->ap_type == ATA_PORT_T_PM)
820 			atx = at;
821 
822 		if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
823 			ccbh->status = CAM_DEV_NOT_THERE;
824 			xpt_done(ccb);
825 			return;
826 		}
827 	} else {
828 		at = ap->ap_ata[0];
829 	}
830 
831 	/*
832 	 * Switch on the meta XPT command
833 	 */
834 	switch(ccbh->func_code) {
835 	case XPT_ENG_EXEC:
836 		/*
837 		 * This routine is called after a port multiplier has been
838 		 * probed.
839 		 */
840 		ccbh->status = CAM_REQ_CMP;
841 		ahci_os_lock_port(ap);
842 		ahci_port_state_machine(ap, 0);
843 		ahci_os_unlock_port(ap);
844 		xpt_done(ccb);
845 		ahci_xpt_rescan(ap);
846 		break;
847 	case XPT_PATH_INQ:
848 		/*
849 		 * This command always succeeds, otherwise the bus scan
850 		 * will not detach dead devices.
851 		 */
852 		ccb->cpi.version_num = 1;
853 		ccb->cpi.hba_inquiry = 0;
854 		ccb->cpi.target_sprt = 0;
855 		ccb->cpi.hba_misc = PIM_SEQSCAN;
856 		ccb->cpi.hba_eng_cnt = 0;
857 		bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
858 		ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
859 		ccb->cpi.max_lun = 0;
860 		ccb->cpi.async_flags = 0;
861 		ccb->cpi.hpath_id = 0;
862 		ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
863 		ccb->cpi.unit_number = cam_sim_unit(sim);
864 		ccb->cpi.bus_id = cam_sim_bus(sim);
865 		ccb->cpi.base_transfer_speed = 150000;
866 		ccb->cpi.transport = XPORT_SATA;
867 		ccb->cpi.transport_version = 1;
868 		ccb->cpi.protocol = PROTO_SCSI;
869 		ccb->cpi.protocol_version = SCSI_REV_2;
870 
871 		ccbh->status = CAM_REQ_CMP;
872 		if (ccbh->target_id == CAM_TARGET_WILDCARD) {
873 			ahci_os_lock_port(ap);
874 			ahci_port_state_machine(ap, 0);
875 			ahci_os_unlock_port(ap);
876 		} else {
877 			switch(ahci_pread(ap, AHCI_PREG_SSTS) &
878 			       AHCI_PREG_SSTS_SPD) {
879 			case AHCI_PREG_SSTS_SPD_GEN1:
880 				ccb->cpi.base_transfer_speed = 150000;
881 				break;
882 			case AHCI_PREG_SSTS_SPD_GEN2:
883 				ccb->cpi.base_transfer_speed = 300000;
884 				break;
885 			case AHCI_PREG_SSTS_SPD_GEN3:
886 				ccb->cpi.base_transfer_speed = 600000;
887 				break;
888 			default:
889 				/* unknown */
890 				ccb->cpi.base_transfer_speed = 1000;
891 				break;
892 			}
893 #if 0
894 			if (ap->ap_type == ATA_PORT_T_NONE)
895 				ccbh->status = CAM_DEV_NOT_THERE;
896 #endif
897 		}
898 		xpt_done(ccb);
899 		break;
900 	case XPT_RESET_DEV:
901 		ahci_os_lock_port(ap);
902 		if (ap->ap_type == ATA_PORT_T_NONE) {
903 			ccbh->status = CAM_DEV_NOT_THERE;
904 		} else {
905 			ahci_port_reset(ap, atx, 0);
906 			ccbh->status = CAM_REQ_CMP;
907 		}
908 		ahci_os_unlock_port(ap);
909 		xpt_done(ccb);
910 		break;
911 	case XPT_RESET_BUS:
912 		ahci_os_lock_port(ap);
913 		ahci_port_reset(ap, NULL, 1);
914 		ahci_os_unlock_port(ap);
915 		ccbh->status = CAM_REQ_CMP;
916 		xpt_done(ccb);
917 		break;
918 	case XPT_SET_TRAN_SETTINGS:
919 		ccbh->status = CAM_FUNC_NOTAVAIL;
920 		xpt_done(ccb);
921 		break;
922 	case XPT_GET_TRAN_SETTINGS:
923 		ccb->cts.protocol = PROTO_SCSI;
924 		ccb->cts.protocol_version = SCSI_REV_2;
925 		ccb->cts.transport = XPORT_SATA;
926 		ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
927 		ccb->cts.proto_specific.valid = 0;
928 		ccb->cts.xport_specific.valid = 0;
929 		ccbh->status = CAM_REQ_CMP;
930 		xpt_done(ccb);
931 		break;
932 	case XPT_CALC_GEOMETRY:
933 		cam_calc_geometry(&ccb->ccg, 1);
934 		xpt_done(ccb);
935 		break;
936 	case XPT_SCSI_IO:
937 		/*
938 		 * Our parallel startup code might have only probed through
939 		 * to the IDENT, so do the last step if necessary.
940 		 */
941 		if (at->at_probe == ATA_PROBE_NEED_IDENT)
942 			ahci_cam_probe(ap, atx);
943 		if (at->at_probe != ATA_PROBE_GOOD) {
944 			ccbh->status = CAM_DEV_NOT_THERE;
945 			xpt_done(ccb);
946 			break;
947 		}
948 		switch(at->at_type) {
949 		case ATA_PORT_T_DISK:
950 			ahci_xpt_scsi_disk_io(ap, atx, ccb);
951 			break;
952 		case ATA_PORT_T_ATAPI:
953 			ahci_xpt_scsi_atapi_io(ap, atx, ccb);
954 			break;
955 		default:
956 			ccbh->status = CAM_REQ_INVALID;
957 			xpt_done(ccb);
958 			break;
959 		}
960 		break;
961 	case XPT_TRIM:
962 	{
963 		scsi_cdb_t cdb;
964 		struct ccb_scsiio *csio;
965 		csio = &ccb->csio;
966 		cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
967 		    csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
968 		cdb->generic.opcode = TRIM;
969 		ahci_xpt_scsi_disk_io(ap, atx, ccb);
970 		break;
971 	}
972 	default:
973 		ccbh->status = CAM_REQ_INVALID;
974 		xpt_done(ccb);
975 		break;
976 	}
977 }
978 
979 /*
980  * Poll function.
981  *
982  * Generally this function gets called heavily when interrupts might be
983  * non-operational, during a halt/reboot or panic.
984  */
985 static
986 void
987 ahci_xpt_poll(struct cam_sim *sim)
988 {
989 	struct ahci_port *ap;
990 
991 	ap = cam_sim_softc(sim);
992 	crit_enter();
993 	ahci_os_lock_port(ap);
994 	ahci_port_intr(ap, 1);
995 	ahci_os_unlock_port(ap);
996 	crit_exit();
997 }
998 
999 /*
1000  * Convert the SCSI command in ccb to an ata_xfer command in xa
1001  * for ATA_PORT_T_DISK operations.  Set the completion function
1002  * to convert the response back, then dispatch to the OpenBSD AHCI
1003  * layer.
1004  *
1005  * AHCI DISK commands only support a limited command set, and we
1006  * fake additional commands to make it play nice with the CAM subsystem.
1007  */
1008 static
1009 void
1010 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
1011 		      union ccb *ccb)
1012 {
1013 	struct ccb_hdr *ccbh;
1014 	struct ccb_scsiio *csio;
1015 	struct ata_xfer *xa;
1016 	struct ata_port	*at;
1017 	struct ata_fis_h2d *fis;
1018 	struct ata_pass_12 *atp12;
1019 	struct ata_pass_16 *atp16;
1020 	scsi_cdb_t cdb;
1021 	union scsi_data *rdata;
1022 	int rdata_len;
1023 	u_int64_t capacity;
1024 	u_int64_t lba;
1025 	u_int32_t count;
1026 
1027 	ccbh = &ccb->csio.ccb_h;
1028 	csio = &ccb->csio;
1029 	at = atx ? atx : ap->ap_ata[0];
1030 
1031 	/*
1032 	 * XXX not passing NULL at for direct attach!
1033 	 */
1034 	xa = ahci_ata_get_xfer(ap, atx);
1035 	rdata = (void *)csio->data_ptr;
1036 	rdata_len = csio->dxfer_len;
1037 
1038 	/*
1039 	 * Build the FIS or process the csio to completion.
1040 	 */
1041 	cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1042 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1043 
1044 	switch(cdb->generic.opcode) {
1045 	case REQUEST_SENSE:
1046 		/*
1047 		 * Auto-sense everything, so explicit sense requests
1048 		 * return no-sense.
1049 		 */
1050 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1051 		break;
1052 	case INQUIRY:
1053 		/*
1054 		 * Inquiry supported features
1055 		 *
1056 		 * [opcode, byte2, page_code, length, control]
1057 		 */
1058 		if (cdb->inquiry.byte2 & SI_EVPD) {
1059 			ahci_xpt_page_inquiry(ap, at, ccb);
1060 		} else {
1061 			bzero(rdata, rdata_len);
1062 			if (rdata_len < SHORT_INQUIRY_LENGTH) {
1063 				ccbh->status = CAM_CCB_LEN_ERR;
1064 				break;
1065 			}
1066 			if (rdata_len > sizeof(rdata->inquiry_data))
1067 				rdata_len = sizeof(rdata->inquiry_data);
1068 			rdata->inquiry_data.device = T_DIRECT;
1069 			rdata->inquiry_data.version = SCSI_REV_SPC2;
1070 			rdata->inquiry_data.response_format = 2;
1071 			rdata->inquiry_data.additional_length = 32;
1072 			bcopy("SATA    ", rdata->inquiry_data.vendor, 8);
1073 			bcopy(at->at_identify.model,
1074 			      rdata->inquiry_data.product,
1075 			      sizeof(rdata->inquiry_data.product));
1076 			bcopy(at->at_identify.firmware,
1077 			      rdata->inquiry_data.revision,
1078 			      sizeof(rdata->inquiry_data.revision));
1079 			ccbh->status = CAM_REQ_CMP;
1080 		}
1081 
1082 		/*
1083 		 * Use the vendor specific area to set the TRIM status
1084 		 * for scsi_da
1085 		 */
1086 		if (at->at_identify.support_dsm) {
1087 			rdata->inquiry_data.vendor_specific1[0] =
1088 			    at->at_identify.support_dsm &ATA_SUPPORT_DSM_TRIM;
1089 			rdata->inquiry_data.vendor_specific1[1] =
1090 			    at->at_identify.max_dsm_blocks;
1091 		}
1092 		break;
1093 	case READ_CAPACITY_16:
1094 		if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1095 			ccbh->status = CAM_REQ_INVALID;
1096 			break;
1097 		}
1098 		if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1099 			ccbh->status = CAM_CCB_LEN_ERR;
1100 			break;
1101 		}
1102 		/* fall through */
1103 	case READ_CAPACITY:
1104 		if (rdata_len < sizeof(rdata->read_capacity_data)) {
1105 			ccbh->status = CAM_CCB_LEN_ERR;
1106 			break;
1107 		}
1108 
1109 		capacity = at->at_capacity;
1110 
1111 		bzero(rdata, rdata_len);
1112 		if (cdb->generic.opcode == READ_CAPACITY) {
1113 			rdata_len = sizeof(rdata->read_capacity_data);
1114 			if (capacity > 0xFFFFFFFFU)
1115 				capacity = 0xFFFFFFFFU;
1116 			bzero(&rdata->read_capacity_data, rdata_len);
1117 			scsi_ulto4b((u_int32_t)capacity - 1,
1118 				    rdata->read_capacity_data.addr);
1119 			scsi_ulto4b(512, rdata->read_capacity_data.length);
1120 		} else {
1121 			rdata_len = sizeof(rdata->read_capacity_data_16);
1122 			bzero(&rdata->read_capacity_data_16, rdata_len);
1123 			scsi_u64to8b(capacity - 1,
1124 				     rdata->read_capacity_data_16.addr);
1125 			scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1126 		}
1127 		ccbh->status = CAM_REQ_CMP;
1128 		break;
1129 	case SYNCHRONIZE_CACHE:
1130 		/*
1131 		 * Synchronize cache.  Specification says this can take
1132 		 * greater then 30 seconds so give it at least 45.
1133 		 */
1134 		fis = xa->fis;
1135 		fis->flags = ATA_H2D_FLAGS_CMD;
1136 		fis->command = ATA_C_FLUSH_CACHE;
1137 		fis->device = 0;
1138 		if (xa->timeout < 45000)
1139 			xa->timeout = 45000;
1140 		xa->datalen = 0;
1141 		xa->flags = 0;
1142 		xa->complete = ahci_ata_complete_disk_synchronize_cache;
1143 		break;
1144 	case TRIM:
1145 		fis = xa->fis;
1146 		fis->command = ATA_C_DATA_SET_MANAGEMENT;
1147 		fis->features = (u_int8_t)ATA_SF_DSM_TRIM;
1148 		fis->features_exp = (u_int8_t)(ATA_SF_DSM_TRIM>> 8);
1149 
1150 		xa->flags = ATA_F_WRITE;
1151 		fis->flags = ATA_H2D_FLAGS_CMD;
1152 
1153 		xa->data = csio->data_ptr;
1154 		xa->datalen = csio->dxfer_len;
1155 		xa->timeout = ccbh->timeout*50;	/* milliseconds */
1156 
1157 		fis->sector_count =(u_int8_t)(xa->datalen/512);
1158 		fis->sector_count_exp =(u_int8_t)((xa->datalen/512)>>8);
1159 
1160 		lba = 0;
1161 		fis->lba_low = (u_int8_t)lba;
1162 		fis->lba_mid = (u_int8_t)(lba >> 8);
1163 		fis->lba_high = (u_int8_t)(lba >> 16);
1164 		fis->lba_low_exp = (u_int8_t)(lba >> 24);
1165 		fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1166 		fis->lba_high_exp = (u_int8_t)(lba >> 40);
1167 
1168 		fis->device = ATA_H2D_DEVICE_LBA;
1169 		xa->data = csio->data_ptr;
1170 
1171 		xa->complete = ahci_ata_complete_disk_rw;
1172 		ccbh->status = CAM_REQ_INPROG;
1173 		break;
1174 	case TEST_UNIT_READY:
1175 	case START_STOP_UNIT:
1176 	case PREVENT_ALLOW:
1177 		/*
1178 		 * Just silently return success
1179 		 */
1180 		ccbh->status = CAM_REQ_CMP;
1181 		rdata_len = 0;
1182 		break;
1183 	case ATA_PASS_12:
1184 		atp12 = &cdb->ata_pass_12;
1185 		fis = xa->fis;
1186 		/*
1187 		 * Figure out the flags to be used, depending on the direction of the
1188 		 * CAM request.
1189 		 */
1190 		switch (ccbh->flags & CAM_DIR_MASK) {
1191 		case CAM_DIR_IN:
1192 			xa->flags = ATA_F_READ;
1193 			break;
1194 		case CAM_DIR_OUT:
1195 			xa->flags = ATA_F_WRITE;
1196 			break;
1197 		default:
1198 			xa->flags = 0;
1199 		}
1200 		xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1201 		xa->data = csio->data_ptr;
1202 		xa->datalen = csio->dxfer_len;
1203 		xa->complete = ahci_ata_complete_disk_rw;
1204 		xa->timeout = ccbh->timeout;
1205 
1206 		/*
1207 		 * Populate the fis from the information we received through CAM
1208 		 * ATA passthrough.
1209 		 */
1210 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp12->flags ? */
1211 		fis->features = atp12->features;
1212 		fis->sector_count = atp12->sector_count;
1213 		fis->lba_low = atp12->lba_low;
1214 		fis->lba_mid = atp12->lba_mid;
1215 		fis->lba_high = atp12->lba_high;
1216 		fis->device = atp12->device;	/* maybe always 0? */
1217 		fis->command = atp12->command;
1218 		fis->control = atp12->control;
1219 
1220 		/*
1221 		 * Mark as in progress so it is sent to the device.
1222 		 */
1223 		ccbh->status = CAM_REQ_INPROG;
1224 		break;
1225 	case ATA_PASS_16:
1226 		atp16 = &cdb->ata_pass_16;
1227 		fis = xa->fis;
1228 		/*
1229 		 * Figure out the flags to be used, depending on the direction of the
1230 		 * CAM request.
1231 		 */
1232 		switch (ccbh->flags & CAM_DIR_MASK) {
1233 		case CAM_DIR_IN:
1234 			xa->flags = ATA_F_READ;
1235 			break;
1236 		case CAM_DIR_OUT:
1237 			xa->flags = ATA_F_WRITE;
1238 			break;
1239 		default:
1240 			xa->flags = 0;
1241 		}
1242 		xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1243 		xa->data = csio->data_ptr;
1244 		xa->datalen = csio->dxfer_len;
1245 		xa->complete = ahci_ata_complete_disk_rw;
1246 		xa->timeout = ccbh->timeout;
1247 
1248 		/*
1249 		 * Populate the fis from the information we received through CAM
1250 		 * ATA passthrough.
1251 		 */
1252 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp16->flags ? */
1253 		fis->features = atp16->features;
1254 		fis->features_exp = atp16->features_ext;
1255 		fis->sector_count = atp16->sector_count;
1256 		fis->sector_count_exp = atp16->sector_count_ext;
1257 		fis->lba_low = atp16->lba_low;
1258 		fis->lba_low_exp = atp16->lba_low_ext;
1259 		fis->lba_mid = atp16->lba_mid;
1260 		fis->lba_mid_exp = atp16->lba_mid_ext;
1261 		fis->lba_high = atp16->lba_high;
1262 		fis->lba_mid_exp = atp16->lba_mid_ext;
1263 		fis->device = atp16->device;	/* maybe always 0? */
1264 		fis->command = atp16->command;
1265 
1266 		/*
1267 		 * Mark as in progress so it is sent to the device.
1268 		 */
1269 		ccbh->status = CAM_REQ_INPROG;
1270 		break;
1271 	default:
1272 		switch(cdb->generic.opcode) {
1273 		case READ_6:
1274 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1275 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1276 			xa->flags = ATA_F_READ;
1277 			break;
1278 		case READ_10:
1279 			lba = scsi_4btoul(cdb->rw_10.addr);
1280 			count = scsi_2btoul(cdb->rw_10.length);
1281 			xa->flags = ATA_F_READ;
1282 			break;
1283 		case READ_12:
1284 			lba = scsi_4btoul(cdb->rw_12.addr);
1285 			count = scsi_4btoul(cdb->rw_12.length);
1286 			xa->flags = ATA_F_READ;
1287 			break;
1288 		case READ_16:
1289 			lba = scsi_8btou64(cdb->rw_16.addr);
1290 			count = scsi_4btoul(cdb->rw_16.length);
1291 			xa->flags = ATA_F_READ;
1292 			break;
1293 		case WRITE_6:
1294 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1295 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1296 			xa->flags = ATA_F_WRITE;
1297 			break;
1298 		case WRITE_10:
1299 			lba = scsi_4btoul(cdb->rw_10.addr);
1300 			count = scsi_2btoul(cdb->rw_10.length);
1301 			xa->flags = ATA_F_WRITE;
1302 			break;
1303 		case WRITE_12:
1304 			lba = scsi_4btoul(cdb->rw_12.addr);
1305 			count = scsi_4btoul(cdb->rw_12.length);
1306 			xa->flags = ATA_F_WRITE;
1307 			break;
1308 		case WRITE_16:
1309 			lba = scsi_8btou64(cdb->rw_16.addr);
1310 			count = scsi_4btoul(cdb->rw_16.length);
1311 			xa->flags = ATA_F_WRITE;
1312 			break;
1313 		default:
1314 			ccbh->status = CAM_REQ_INVALID;
1315 			break;
1316 		}
1317 		if (ccbh->status != CAM_REQ_INPROG)
1318 			break;
1319 
1320 		fis = xa->fis;
1321 		fis->flags = ATA_H2D_FLAGS_CMD;
1322 		fis->lba_low = (u_int8_t)lba;
1323 		fis->lba_mid = (u_int8_t)(lba >> 8);
1324 		fis->lba_high = (u_int8_t)(lba >> 16);
1325 		fis->device = ATA_H2D_DEVICE_LBA;
1326 
1327 		/*
1328 		 * NCQ only for direct-attached disks, do not currently
1329 		 * try to use NCQ with port multipliers.
1330 		 */
1331 		if (at->at_ncqdepth > 1 &&
1332 		    ap->ap_type == ATA_PORT_T_DISK &&
1333 		    (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1334 		    (ccbh->flags & CAM_POLLED) == 0) {
1335 			/*
1336 			 * Use NCQ - always uses 48 bit addressing
1337 			 */
1338 			xa->flags |= ATA_F_NCQ;
1339 			fis->command = (xa->flags & ATA_F_WRITE) ?
1340 					ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1341 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1342 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1343 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1344 			fis->sector_count = xa->tag << 3;
1345 			fis->features = (u_int8_t)count;
1346 			fis->features_exp = (u_int8_t)(count >> 8);
1347 		} else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1348 			/*
1349 			 * Use LBA48
1350 			 */
1351 			fis->command = (xa->flags & ATA_F_WRITE) ?
1352 					ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1353 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1354 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1355 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1356 			fis->sector_count = (u_int8_t)count;
1357 			fis->sector_count_exp = (u_int8_t)(count >> 8);
1358 		} else {
1359 			/*
1360 			 * Use LBA
1361 			 *
1362 			 * NOTE: 256 sectors is supported, stored as 0.
1363 			 */
1364 			fis->command = (xa->flags & ATA_F_WRITE) ?
1365 					ATA_C_WRITEDMA : ATA_C_READDMA;
1366 			fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1367 			fis->sector_count = (u_int8_t)count;
1368 		}
1369 
1370 		xa->data = csio->data_ptr;
1371 		xa->datalen = csio->dxfer_len;
1372 		xa->complete = ahci_ata_complete_disk_rw;
1373 		xa->timeout = ccbh->timeout;	/* milliseconds */
1374 #if 0
1375 		if (xa->timeout > 10000)	/* XXX - debug */
1376 			xa->timeout = 10000;
1377 #endif
1378 		if (ccbh->flags & CAM_POLLED)
1379 			xa->flags |= ATA_F_POLL;
1380 		break;
1381 	}
1382 
1383 	/*
1384 	 * If the request is still in progress the xa and FIS have
1385 	 * been set up (except for the PM target), and must be dispatched.
1386 	 * Otherwise the request was completed.
1387 	 */
1388 	if (ccbh->status == CAM_REQ_INPROG) {
1389 		KKASSERT(xa->complete != NULL);
1390 		xa->atascsi_private = ccb;
1391 		ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1392 		ahci_os_lock_port(ap);
1393 		xa->fis->flags |= at->at_target;
1394 		ahci_ata_cmd(xa);
1395 		ahci_os_unlock_port(ap);
1396 	} else {
1397 		ahci_ata_put_xfer(xa);
1398 		xpt_done(ccb);
1399 	}
1400 }
1401 
1402 /*
1403  * Convert the SCSI command in ccb to an ata_xfer command in xa
1404  * for ATA_PORT_T_ATAPI operations.  Set the completion function
1405  * to convert the response back, then dispatch to the OpenBSD AHCI
1406  * layer.
1407  */
1408 static
1409 void
1410 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1411 			union ccb *ccb)
1412 {
1413 	struct ccb_hdr *ccbh;
1414 	struct ccb_scsiio *csio;
1415 	struct ata_xfer *xa;
1416 	struct ata_fis_h2d *fis;
1417 	scsi_cdb_t cdbs;
1418 	scsi_cdb_t cdbd;
1419 	int flags;
1420 	struct ata_port	*at;
1421 
1422 	ccbh = &ccb->csio.ccb_h;
1423 	csio = &ccb->csio;
1424 	at = atx ? atx : ap->ap_ata[0];
1425 
1426 	switch (ccbh->flags & CAM_DIR_MASK) {
1427 	case CAM_DIR_IN:
1428 		flags = ATA_F_PACKET | ATA_F_READ;
1429 		break;
1430 	case CAM_DIR_OUT:
1431 		flags = ATA_F_PACKET | ATA_F_WRITE;
1432 		break;
1433 	case CAM_DIR_NONE:
1434 		flags = ATA_F_PACKET;
1435 		break;
1436 	default:
1437 		ccbh->status = CAM_REQ_INVALID;
1438 		xpt_done(ccb);
1439 		return;
1440 		/* NOT REACHED */
1441 	}
1442 
1443 	/*
1444 	 * Special handling to get the rfis back into host memory while
1445 	 * still allowing the chip to run commands in parallel to
1446 	 * ATAPI devices behind a PM.
1447 	 */
1448 	flags |= ATA_F_AUTOSENSE;
1449 
1450 	/*
1451 	 * The command has to fit in the packet command buffer.
1452 	 */
1453 	if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1454 		ccbh->status = CAM_CCB_LEN_ERR;
1455 		xpt_done(ccb);
1456 		return;
1457 	}
1458 
1459 	/*
1460 	 * Initialize the XA and FIS.  It is unclear how much of
1461 	 * this has to mimic the equivalent ATA command.
1462 	 *
1463 	 * XXX not passing NULL at for direct attach!
1464 	 */
1465 	xa = ahci_ata_get_xfer(ap, atx);
1466 	fis = xa->fis;
1467 
1468 	fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1469 	fis->command = ATA_C_PACKET;
1470 	fis->device = ATA_H2D_DEVICE_LBA;
1471 	fis->sector_count = xa->tag << 3;
1472 	if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1473 		if (flags & ATA_F_WRITE) {
1474 			fis->features = ATA_H2D_FEATURES_DMA |
1475 				       ATA_H2D_FEATURES_DIR_WRITE;
1476 		} else {
1477 			fis->features = ATA_H2D_FEATURES_DMA |
1478 				       ATA_H2D_FEATURES_DIR_READ;
1479 		}
1480 	} else {
1481 		fis->lba_mid = 0;
1482 		fis->lba_high = 0;
1483 	}
1484 	fis->control = ATA_FIS_CONTROL_4BIT;
1485 
1486 	xa->flags = flags;
1487 	xa->data = csio->data_ptr;
1488 	xa->datalen = csio->dxfer_len;
1489 	xa->timeout = ccbh->timeout;	/* milliseconds */
1490 
1491 	if (ccbh->flags & CAM_POLLED)
1492 		xa->flags |= ATA_F_POLL;
1493 
1494 	/*
1495 	 * Copy the cdb to the packetcmd buffer in the FIS using a
1496 	 * convenient pointer in the xa.
1497 	 *
1498 	 * Zero-out any trailing bytes in case the ATAPI device cares.
1499 	 */
1500 	cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1501 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1502 	bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1503 	if (csio->cdb_len < 16)
1504 		bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len);
1505 
1506 #if 0
1507 	kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1508 		cdbs->generic.opcode,
1509 		csio->cdb_len, csio->dxfer_len);
1510 #endif
1511 
1512 	/*
1513 	 * Some ATAPI commands do not actually follow the SCSI standard.
1514 	 */
1515 	cdbd = (void *)xa->packetcmd;
1516 
1517 	switch(cdbd->generic.opcode) {
1518 	case REQUEST_SENSE:
1519 		/*
1520 		 * Force SENSE requests to the ATAPI sense length.
1521 		 *
1522 		 * It is unclear if this is needed or not.
1523 		 */
1524 		if (cdbd->sense.length == SSD_FULL_SIZE) {
1525 			if (bootverbose) {
1526 				kprintf("%s: Shortening sense request\n",
1527 					PORTNAME(ap));
1528 			}
1529 			cdbd->sense.length = offsetof(struct scsi_sense_data,
1530 						      extra_bytes[0]);
1531 		}
1532 		break;
1533 	case INQUIRY:
1534 		/*
1535 		 * Some ATAPI devices can't handle long inquiry lengths,
1536 		 * don't ask me why.  Truncate the inquiry length.
1537 		 */
1538 		if (cdbd->inquiry.page_code == 0 &&
1539 		    cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1540 			cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1541 		}
1542 		break;
1543 	case READ_6:
1544 	case WRITE_6:
1545 		/*
1546 		 * Convert *_6 to *_10 commands.  Most ATAPI devices
1547 		 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1548 		 */
1549 		cdbd->rw_10.opcode |= 0x20;
1550 		cdbd->rw_10.byte2 = 0;
1551 		cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1552 		cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1553 		cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1554 		cdbd->rw_10.addr[3] = 0;
1555 		cdbd->rw_10.reserved = 0;
1556 		cdbd->rw_10.length[0] = 0;
1557 		cdbd->rw_10.length[1] = cdbs->rw_6.length;
1558 		cdbd->rw_10.control = cdbs->rw_6.control;
1559 		break;
1560 	default:
1561 		break;
1562 	}
1563 
1564 	/*
1565 	 * And dispatch
1566 	 */
1567 	xa->complete = ahci_atapi_complete_cmd;
1568 	xa->atascsi_private = ccb;
1569 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1570 	ahci_os_lock_port(ap);
1571 	ahci_ata_cmd(xa);
1572 	ahci_os_unlock_port(ap);
1573 }
1574 
1575 /*
1576  * Simulate page inquiries for disk attachments.
1577  */
1578 static
1579 void
1580 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
1581 {
1582 	union {
1583 		struct scsi_vpd_supported_page_list	list;
1584 		struct scsi_vpd_unit_serial_number	serno;
1585 		struct scsi_vpd_unit_devid		devid;
1586 		char					buf[256];
1587 	} *page;
1588 	scsi_cdb_t cdb;
1589 	int i;
1590 	int j;
1591 	int len;
1592 
1593 	page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1594 
1595 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1596 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1597 
1598 	switch(cdb->inquiry.page_code) {
1599 	case SVPD_SUPPORTED_PAGE_LIST:
1600 		i = 0;
1601 		page->list.device = T_DIRECT;
1602 		page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1603 		page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1604 		page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1605 		page->list.list[i++] = SVPD_UNIT_DEVID;
1606 		page->list.length = i;
1607 		len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1608 		break;
1609 	case SVPD_UNIT_SERIAL_NUMBER:
1610 		i = 0;
1611 		j = sizeof(at->at_identify.serial);
1612 		for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1613 			;
1614 		while (j > i && at->at_identify.serial[j-1] == ' ')
1615 			--j;
1616 		page->serno.device = T_DIRECT;
1617 		page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1618 		page->serno.length = j - i;
1619 		bcopy(at->at_identify.serial + i,
1620 		      page->serno.serial_num, j - i);
1621 		len = offsetof(struct scsi_vpd_unit_serial_number,
1622 			       serial_num[j-i]);
1623 		break;
1624 	case SVPD_UNIT_DEVID:
1625 		/* fall through for now */
1626 	default:
1627 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1628 		len = 0;
1629 		break;
1630 	}
1631 	if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1632 		if (len <= ccb->csio.dxfer_len) {
1633 			ccb->ccb_h.status = CAM_REQ_CMP;
1634 			bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1635 			bcopy(page, ccb->csio.data_ptr, len);
1636 			ccb->csio.resid = ccb->csio.dxfer_len - len;
1637 		} else {
1638 			ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1639 		}
1640 	}
1641 	kfree(page, M_DEVBUF);
1642 }
1643 
1644 /*
1645  * Completion function for ATA_PORT_T_DISK cache synchronization.
1646  */
1647 static
1648 void
1649 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1650 {
1651 	union ccb *ccb = xa->atascsi_private;
1652 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1653 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1654 
1655 	switch(xa->state) {
1656 	case ATA_S_COMPLETE:
1657 		ccbh->status = CAM_REQ_CMP;
1658 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1659 		break;
1660 	case ATA_S_ERROR:
1661 		kprintf("%s: synchronize_cache: error\n",
1662 			ATANAME(ap, xa->at));
1663 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1664 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1665 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1666 		break;
1667 	case ATA_S_TIMEOUT:
1668 		kprintf("%s: synchronize_cache: timeout\n",
1669 			ATANAME(ap, xa->at));
1670 		ccbh->status = CAM_CMD_TIMEOUT;
1671 		break;
1672 	default:
1673 		kprintf("%s: synchronize_cache: unknown state %d\n",
1674 			ATANAME(ap, xa->at), xa->state);
1675 		panic("%s: Unknown state", ATANAME(ap, xa->at));
1676 		ccbh->status = CAM_REQ_CMP_ERR;
1677 		break;
1678 	}
1679 	ahci_ata_put_xfer(xa);
1680 	ahci_os_unlock_port(ap);
1681 	xpt_done(ccb);
1682 	ahci_os_lock_port(ap);
1683 }
1684 
1685 /*
1686  * Completion function for ATA_PORT_T_DISK I/O
1687  */
1688 static
1689 void
1690 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1691 {
1692 	union ccb *ccb = xa->atascsi_private;
1693 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1694 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1695 
1696 	switch(xa->state) {
1697 	case ATA_S_COMPLETE:
1698 		ccbh->status = CAM_REQ_CMP;
1699 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1700 		break;
1701 	case ATA_S_ERROR:
1702 		kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1703 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1704 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1705 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1706 		break;
1707 	case ATA_S_TIMEOUT:
1708 		kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1709 		ccbh->status = CAM_CMD_TIMEOUT;
1710 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1711 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1712 		break;
1713 	default:
1714 		kprintf("%s: disk_rw: unknown state %d\n",
1715 			ATANAME(ap, xa->at), xa->state);
1716 		panic("%s: Unknown state", ATANAME(ap, xa->at));
1717 		ccbh->status = CAM_REQ_CMP_ERR;
1718 		break;
1719 	}
1720 	ccb->csio.resid = xa->resid;
1721 	ahci_ata_put_xfer(xa);
1722 	ahci_os_unlock_port(ap);
1723 	xpt_done(ccb);
1724 	ahci_os_lock_port(ap);
1725 }
1726 
1727 /*
1728  * Completion function for ATA_PORT_T_ATAPI I/O
1729  *
1730  * Sense data is returned in the rfis.
1731  */
1732 static
1733 void
1734 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1735 {
1736 	union ccb *ccb = xa->atascsi_private;
1737 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1738 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1739 	scsi_cdb_t cdb;
1740 
1741 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1742 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1743 
1744 	switch(xa->state) {
1745 	case ATA_S_COMPLETE:
1746 		ccbh->status = CAM_REQ_CMP;
1747 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1748 		break;
1749 	case ATA_S_ERROR:
1750 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1751 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1752 		ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1753 		break;
1754 	case ATA_S_TIMEOUT:
1755 		kprintf("%s: cmd %d: timeout\n",
1756 			PORTNAME(ap), cdb->generic.opcode);
1757 		ccbh->status = CAM_CMD_TIMEOUT;
1758 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1759 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1760 		break;
1761 	default:
1762 		kprintf("%s: cmd %d: unknown state %d\n",
1763 			PORTNAME(ap), cdb->generic.opcode, xa->state);
1764 		panic("%s: Unknown state", PORTNAME(ap));
1765 		ccbh->status = CAM_REQ_CMP_ERR;
1766 		break;
1767 	}
1768 	ccb->csio.resid = xa->resid;
1769 	ahci_ata_put_xfer(xa);
1770 	ahci_os_unlock_port(ap);
1771 	xpt_done(ccb);
1772 	ahci_os_lock_port(ap);
1773 }
1774 
1775 /*
1776  * Construct dummy sense data for errors on DISKs
1777  */
1778 static
1779 void
1780 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1781 {
1782 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1783 	sense_data->segment = 0;
1784 	sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1785 	sense_data->info[0] = 0;
1786 	sense_data->info[1] = 0;
1787 	sense_data->info[2] = 0;
1788 	sense_data->info[3] = 0;
1789 	sense_data->extra_len = 0;
1790 }
1791 
1792 /*
1793  * Construct atapi sense data for errors on ATAPI
1794  *
1795  * The ATAPI sense data is stored in the passed rfis and must be converted
1796  * to SCSI sense data.
1797  */
1798 static
1799 void
1800 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1801 		     struct scsi_sense_data *sense_data)
1802 {
1803 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1804 	sense_data->segment = 0;
1805 	sense_data->flags = (rfis->error & 0xF0) >> 4;
1806 	if (rfis->error & 0x04)
1807 		sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1808 	if (rfis->error & 0x02)
1809 		sense_data->flags |= SSD_EOM;
1810 	if (rfis->error & 0x01)
1811 		sense_data->flags |= SSD_ILI;
1812 	sense_data->info[0] = 0;
1813 	sense_data->info[1] = 0;
1814 	sense_data->info[2] = 0;
1815 	sense_data->info[3] = 0;
1816 	sense_data->extra_len = 0;
1817 }
1818 
1819 static
1820 void
1821 ahci_strip_string(const char **basep, int *lenp)
1822 {
1823 	const char *base = *basep;
1824 	int len = *lenp;
1825 
1826 	while (len && (*base == 0 || *base == ' ')) {
1827 		--len;
1828 		++base;
1829 	}
1830 	while (len && (base[len-1] == 0 || base[len-1] == ' '))
1831 		--len;
1832 	*basep = base;
1833 	*lenp = len;
1834 }
1835