xref: /dragonfly/sys/dev/disk/ahci/ahci_cam.c (revision 92fc8b5c)
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 	default:
962 		ccbh->status = CAM_REQ_INVALID;
963 		xpt_done(ccb);
964 		break;
965 	}
966 }
967 
968 /*
969  * Poll function.
970  *
971  * Generally this function gets called heavily when interrupts might be
972  * non-operational, during a halt/reboot or panic.
973  */
974 static
975 void
976 ahci_xpt_poll(struct cam_sim *sim)
977 {
978 	struct ahci_port *ap;
979 
980 	ap = cam_sim_softc(sim);
981 	crit_enter();
982 	ahci_os_lock_port(ap);
983 	ahci_port_intr(ap, 1);
984 	ahci_os_unlock_port(ap);
985 	crit_exit();
986 }
987 
988 /*
989  * Convert the SCSI command in ccb to an ata_xfer command in xa
990  * for ATA_PORT_T_DISK operations.  Set the completion function
991  * to convert the response back, then dispatch to the OpenBSD AHCI
992  * layer.
993  *
994  * AHCI DISK commands only support a limited command set, and we
995  * fake additional commands to make it play nice with the CAM subsystem.
996  */
997 static
998 void
999 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
1000 		      union ccb *ccb)
1001 {
1002 	struct ccb_hdr *ccbh;
1003 	struct ccb_scsiio *csio;
1004 	struct ata_xfer *xa;
1005 	struct ata_port	*at;
1006 	struct ata_fis_h2d *fis;
1007 	struct ata_pass_12 *atp12;
1008 	struct ata_pass_16 *atp16;
1009 	scsi_cdb_t cdb;
1010 	union scsi_data *rdata;
1011 	int rdata_len;
1012 	u_int64_t capacity;
1013 	u_int64_t lba;
1014 	u_int32_t count;
1015 
1016 	ccbh = &ccb->csio.ccb_h;
1017 	csio = &ccb->csio;
1018 	at = atx ? atx : ap->ap_ata[0];
1019 
1020 	/*
1021 	 * XXX not passing NULL at for direct attach!
1022 	 */
1023 	xa = ahci_ata_get_xfer(ap, atx);
1024 	rdata = (void *)csio->data_ptr;
1025 	rdata_len = csio->dxfer_len;
1026 
1027 	/*
1028 	 * Build the FIS or process the csio to completion.
1029 	 */
1030 	cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1031 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1032 
1033 	switch(cdb->generic.opcode) {
1034 	case REQUEST_SENSE:
1035 		/*
1036 		 * Auto-sense everything, so explicit sense requests
1037 		 * return no-sense.
1038 		 */
1039 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1040 		break;
1041 	case INQUIRY:
1042 		/*
1043 		 * Inquiry supported features
1044 		 *
1045 		 * [opcode, byte2, page_code, length, control]
1046 		 */
1047 		if (cdb->inquiry.byte2 & SI_EVPD) {
1048 			ahci_xpt_page_inquiry(ap, at, ccb);
1049 		} else {
1050 			bzero(rdata, rdata_len);
1051 			if (rdata_len < SHORT_INQUIRY_LENGTH) {
1052 				ccbh->status = CAM_CCB_LEN_ERR;
1053 				break;
1054 			}
1055 			if (rdata_len > sizeof(rdata->inquiry_data))
1056 				rdata_len = sizeof(rdata->inquiry_data);
1057 			rdata->inquiry_data.device = T_DIRECT;
1058 			rdata->inquiry_data.version = SCSI_REV_SPC2;
1059 			rdata->inquiry_data.response_format = 2;
1060 			rdata->inquiry_data.additional_length = 32;
1061 			bcopy("SATA    ", rdata->inquiry_data.vendor, 8);
1062 			bcopy(at->at_identify.model,
1063 			      rdata->inquiry_data.product,
1064 			      sizeof(rdata->inquiry_data.product));
1065 			bcopy(at->at_identify.firmware,
1066 			      rdata->inquiry_data.revision,
1067 			      sizeof(rdata->inquiry_data.revision));
1068 			ccbh->status = CAM_REQ_CMP;
1069 		}
1070 		break;
1071 	case READ_CAPACITY_16:
1072 		if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1073 			ccbh->status = CAM_REQ_INVALID;
1074 			break;
1075 		}
1076 		if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1077 			ccbh->status = CAM_CCB_LEN_ERR;
1078 			break;
1079 		}
1080 		/* fall through */
1081 	case READ_CAPACITY:
1082 		if (rdata_len < sizeof(rdata->read_capacity_data)) {
1083 			ccbh->status = CAM_CCB_LEN_ERR;
1084 			break;
1085 		}
1086 
1087 		capacity = at->at_capacity;
1088 
1089 		bzero(rdata, rdata_len);
1090 		if (cdb->generic.opcode == READ_CAPACITY) {
1091 			rdata_len = sizeof(rdata->read_capacity_data);
1092 			if (capacity > 0xFFFFFFFFU)
1093 				capacity = 0xFFFFFFFFU;
1094 			bzero(&rdata->read_capacity_data, rdata_len);
1095 			scsi_ulto4b((u_int32_t)capacity - 1,
1096 				    rdata->read_capacity_data.addr);
1097 			scsi_ulto4b(512, rdata->read_capacity_data.length);
1098 		} else {
1099 			rdata_len = sizeof(rdata->read_capacity_data_16);
1100 			bzero(&rdata->read_capacity_data_16, rdata_len);
1101 			scsi_u64to8b(capacity - 1,
1102 				     rdata->read_capacity_data_16.addr);
1103 			scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1104 		}
1105 		ccbh->status = CAM_REQ_CMP;
1106 		break;
1107 	case SYNCHRONIZE_CACHE:
1108 		/*
1109 		 * Synchronize cache.  Specification says this can take
1110 		 * greater then 30 seconds so give it at least 45.
1111 		 */
1112 		fis = xa->fis;
1113 		fis->flags = ATA_H2D_FLAGS_CMD;
1114 		fis->command = ATA_C_FLUSH_CACHE;
1115 		fis->device = 0;
1116 		if (xa->timeout < 45000)
1117 			xa->timeout = 45000;
1118 		xa->datalen = 0;
1119 		xa->flags = 0;
1120 		xa->complete = ahci_ata_complete_disk_synchronize_cache;
1121 		break;
1122 	case TEST_UNIT_READY:
1123 	case START_STOP_UNIT:
1124 	case PREVENT_ALLOW:
1125 		/*
1126 		 * Just silently return success
1127 		 */
1128 		ccbh->status = CAM_REQ_CMP;
1129 		rdata_len = 0;
1130 		break;
1131 	case ATA_PASS_12:
1132 		atp12 = &cdb->ata_pass_12;
1133 		fis = xa->fis;
1134 		/*
1135 		 * Figure out the flags to be used, depending on the direction of the
1136 		 * CAM request.
1137 		 */
1138 		switch (ccbh->flags & CAM_DIR_MASK) {
1139 		case CAM_DIR_IN:
1140 			xa->flags = ATA_F_READ;
1141 			break;
1142 		case CAM_DIR_OUT:
1143 			xa->flags = ATA_F_WRITE;
1144 			break;
1145 		default:
1146 			xa->flags = 0;
1147 		}
1148 		xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1149 		xa->data = csio->data_ptr;
1150 		xa->datalen = csio->dxfer_len;
1151 		xa->complete = ahci_ata_complete_disk_rw;
1152 		xa->timeout = ccbh->timeout;
1153 
1154 		/*
1155 		 * Populate the fis from the information we received through CAM
1156 		 * ATA passthrough.
1157 		 */
1158 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp12->flags ? */
1159 		fis->features = atp12->features;
1160 		fis->sector_count = atp12->sector_count;
1161 		fis->lba_low = atp12->lba_low;
1162 		fis->lba_mid = atp12->lba_mid;
1163 		fis->lba_high = atp12->lba_high;
1164 		fis->device = atp12->device;	/* maybe always 0? */
1165 		fis->command = atp12->command;
1166 		fis->control = atp12->control;
1167 
1168 		/*
1169 		 * Mark as in progress so it is sent to the device.
1170 		 */
1171 		ccbh->status = CAM_REQ_INPROG;
1172 		break;
1173 	case ATA_PASS_16:
1174 		atp16 = &cdb->ata_pass_16;
1175 		fis = xa->fis;
1176 		/*
1177 		 * Figure out the flags to be used, depending on the direction of the
1178 		 * CAM request.
1179 		 */
1180 		switch (ccbh->flags & CAM_DIR_MASK) {
1181 		case CAM_DIR_IN:
1182 			xa->flags = ATA_F_READ;
1183 			break;
1184 		case CAM_DIR_OUT:
1185 			xa->flags = ATA_F_WRITE;
1186 			break;
1187 		default:
1188 			xa->flags = 0;
1189 		}
1190 		xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1191 		xa->data = csio->data_ptr;
1192 		xa->datalen = csio->dxfer_len;
1193 		xa->complete = ahci_ata_complete_disk_rw;
1194 		xa->timeout = ccbh->timeout;
1195 
1196 		/*
1197 		 * Populate the fis from the information we received through CAM
1198 		 * ATA passthrough.
1199 		 */
1200 		fis->flags = ATA_H2D_FLAGS_CMD;	/* maybe also atp16->flags ? */
1201 		fis->features = atp16->features;
1202 		fis->features_exp = atp16->features_ext;
1203 		fis->sector_count = atp16->sector_count;
1204 		fis->sector_count_exp = atp16->sector_count_ext;
1205 		fis->lba_low = atp16->lba_low;
1206 		fis->lba_low_exp = atp16->lba_low_ext;
1207 		fis->lba_mid = atp16->lba_mid;
1208 		fis->lba_mid_exp = atp16->lba_mid_ext;
1209 		fis->lba_high = atp16->lba_high;
1210 		fis->lba_mid_exp = atp16->lba_mid_ext;
1211 		fis->device = atp16->device;	/* maybe always 0? */
1212 		fis->command = atp16->command;
1213 
1214 		/*
1215 		 * Mark as in progress so it is sent to the device.
1216 		 */
1217 		ccbh->status = CAM_REQ_INPROG;
1218 		break;
1219 	default:
1220 		switch(cdb->generic.opcode) {
1221 		case READ_6:
1222 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1223 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1224 			xa->flags = ATA_F_READ;
1225 			break;
1226 		case READ_10:
1227 			lba = scsi_4btoul(cdb->rw_10.addr);
1228 			count = scsi_2btoul(cdb->rw_10.length);
1229 			xa->flags = ATA_F_READ;
1230 			break;
1231 		case READ_12:
1232 			lba = scsi_4btoul(cdb->rw_12.addr);
1233 			count = scsi_4btoul(cdb->rw_12.length);
1234 			xa->flags = ATA_F_READ;
1235 			break;
1236 		case READ_16:
1237 			lba = scsi_8btou64(cdb->rw_16.addr);
1238 			count = scsi_4btoul(cdb->rw_16.length);
1239 			xa->flags = ATA_F_READ;
1240 			break;
1241 		case WRITE_6:
1242 			lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1243 			count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1244 			xa->flags = ATA_F_WRITE;
1245 			break;
1246 		case WRITE_10:
1247 			lba = scsi_4btoul(cdb->rw_10.addr);
1248 			count = scsi_2btoul(cdb->rw_10.length);
1249 			xa->flags = ATA_F_WRITE;
1250 			break;
1251 		case WRITE_12:
1252 			lba = scsi_4btoul(cdb->rw_12.addr);
1253 			count = scsi_4btoul(cdb->rw_12.length);
1254 			xa->flags = ATA_F_WRITE;
1255 			break;
1256 		case WRITE_16:
1257 			lba = scsi_8btou64(cdb->rw_16.addr);
1258 			count = scsi_4btoul(cdb->rw_16.length);
1259 			xa->flags = ATA_F_WRITE;
1260 			break;
1261 		default:
1262 			ccbh->status = CAM_REQ_INVALID;
1263 			break;
1264 		}
1265 		if (ccbh->status != CAM_REQ_INPROG)
1266 			break;
1267 
1268 		fis = xa->fis;
1269 		fis->flags = ATA_H2D_FLAGS_CMD;
1270 		fis->lba_low = (u_int8_t)lba;
1271 		fis->lba_mid = (u_int8_t)(lba >> 8);
1272 		fis->lba_high = (u_int8_t)(lba >> 16);
1273 		fis->device = ATA_H2D_DEVICE_LBA;
1274 
1275 		/*
1276 		 * NCQ only for direct-attached disks, do not currently
1277 		 * try to use NCQ with port multipliers.
1278 		 */
1279 		if (at->at_ncqdepth > 1 &&
1280 		    ap->ap_type == ATA_PORT_T_DISK &&
1281 		    (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1282 		    (ccbh->flags & CAM_POLLED) == 0) {
1283 			/*
1284 			 * Use NCQ - always uses 48 bit addressing
1285 			 */
1286 			xa->flags |= ATA_F_NCQ;
1287 			fis->command = (xa->flags & ATA_F_WRITE) ?
1288 					ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1289 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1290 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1291 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1292 			fis->sector_count = xa->tag << 3;
1293 			fis->features = (u_int8_t)count;
1294 			fis->features_exp = (u_int8_t)(count >> 8);
1295 		} else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1296 			/*
1297 			 * Use LBA48
1298 			 */
1299 			fis->command = (xa->flags & ATA_F_WRITE) ?
1300 					ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1301 			fis->lba_low_exp = (u_int8_t)(lba >> 24);
1302 			fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1303 			fis->lba_high_exp = (u_int8_t)(lba >> 40);
1304 			fis->sector_count = (u_int8_t)count;
1305 			fis->sector_count_exp = (u_int8_t)(count >> 8);
1306 		} else {
1307 			/*
1308 			 * Use LBA
1309 			 *
1310 			 * NOTE: 256 sectors is supported, stored as 0.
1311 			 */
1312 			fis->command = (xa->flags & ATA_F_WRITE) ?
1313 					ATA_C_WRITEDMA : ATA_C_READDMA;
1314 			fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1315 			fis->sector_count = (u_int8_t)count;
1316 		}
1317 
1318 		xa->data = csio->data_ptr;
1319 		xa->datalen = csio->dxfer_len;
1320 		xa->complete = ahci_ata_complete_disk_rw;
1321 		xa->timeout = ccbh->timeout;	/* milliseconds */
1322 #if 0
1323 		if (xa->timeout > 10000)	/* XXX - debug */
1324 			xa->timeout = 10000;
1325 #endif
1326 		if (ccbh->flags & CAM_POLLED)
1327 			xa->flags |= ATA_F_POLL;
1328 		break;
1329 	}
1330 
1331 	/*
1332 	 * If the request is still in progress the xa and FIS have
1333 	 * been set up (except for the PM target), and must be dispatched.
1334 	 * Otherwise the request was completed.
1335 	 */
1336 	if (ccbh->status == CAM_REQ_INPROG) {
1337 		KKASSERT(xa->complete != NULL);
1338 		xa->atascsi_private = ccb;
1339 		ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1340 		ahci_os_lock_port(ap);
1341 		xa->fis->flags |= at->at_target;
1342 		ahci_ata_cmd(xa);
1343 		ahci_os_unlock_port(ap);
1344 	} else {
1345 		ahci_ata_put_xfer(xa);
1346 		xpt_done(ccb);
1347 	}
1348 }
1349 
1350 /*
1351  * Convert the SCSI command in ccb to an ata_xfer command in xa
1352  * for ATA_PORT_T_ATAPI operations.  Set the completion function
1353  * to convert the response back, then dispatch to the OpenBSD AHCI
1354  * layer.
1355  */
1356 static
1357 void
1358 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1359 			union ccb *ccb)
1360 {
1361 	struct ccb_hdr *ccbh;
1362 	struct ccb_scsiio *csio;
1363 	struct ata_xfer *xa;
1364 	struct ata_fis_h2d *fis;
1365 	scsi_cdb_t cdbs;
1366 	scsi_cdb_t cdbd;
1367 	int flags;
1368 	struct ata_port	*at;
1369 
1370 	ccbh = &ccb->csio.ccb_h;
1371 	csio = &ccb->csio;
1372 	at = atx ? atx : ap->ap_ata[0];
1373 
1374 	switch (ccbh->flags & CAM_DIR_MASK) {
1375 	case CAM_DIR_IN:
1376 		flags = ATA_F_PACKET | ATA_F_READ;
1377 		break;
1378 	case CAM_DIR_OUT:
1379 		flags = ATA_F_PACKET | ATA_F_WRITE;
1380 		break;
1381 	case CAM_DIR_NONE:
1382 		flags = ATA_F_PACKET;
1383 		break;
1384 	default:
1385 		ccbh->status = CAM_REQ_INVALID;
1386 		xpt_done(ccb);
1387 		return;
1388 		/* NOT REACHED */
1389 	}
1390 
1391 	/*
1392 	 * Special handling to get the rfis back into host memory while
1393 	 * still allowing the chip to run commands in parallel to
1394 	 * ATAPI devices behind a PM.
1395 	 */
1396 	flags |= ATA_F_AUTOSENSE;
1397 
1398 	/*
1399 	 * The command has to fit in the packet command buffer.
1400 	 */
1401 	if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1402 		ccbh->status = CAM_CCB_LEN_ERR;
1403 		xpt_done(ccb);
1404 		return;
1405 	}
1406 
1407 	/*
1408 	 * Initialize the XA and FIS.  It is unclear how much of
1409 	 * this has to mimic the equivalent ATA command.
1410 	 *
1411 	 * XXX not passing NULL at for direct attach!
1412 	 */
1413 	xa = ahci_ata_get_xfer(ap, atx);
1414 	fis = xa->fis;
1415 
1416 	fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1417 	fis->command = ATA_C_PACKET;
1418 	fis->device = ATA_H2D_DEVICE_LBA;
1419 	fis->sector_count = xa->tag << 3;
1420 	if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1421 		if (flags & ATA_F_WRITE) {
1422 			fis->features = ATA_H2D_FEATURES_DMA |
1423 				       ATA_H2D_FEATURES_DIR_WRITE;
1424 		} else {
1425 			fis->features = ATA_H2D_FEATURES_DMA |
1426 				       ATA_H2D_FEATURES_DIR_READ;
1427 		}
1428 	} else {
1429 		fis->lba_mid = 0;
1430 		fis->lba_high = 0;
1431 	}
1432 	fis->control = ATA_FIS_CONTROL_4BIT;
1433 
1434 	xa->flags = flags;
1435 	xa->data = csio->data_ptr;
1436 	xa->datalen = csio->dxfer_len;
1437 	xa->timeout = ccbh->timeout;	/* milliseconds */
1438 
1439 	if (ccbh->flags & CAM_POLLED)
1440 		xa->flags |= ATA_F_POLL;
1441 
1442 	/*
1443 	 * Copy the cdb to the packetcmd buffer in the FIS using a
1444 	 * convenient pointer in the xa.
1445 	 *
1446 	 * Zero-out any trailing bytes in case the ATAPI device cares.
1447 	 */
1448 	cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1449 			csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1450 	bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1451 	if (csio->cdb_len < 16)
1452 		bzero(xa->packetcmd + csio->cdb_len, 16 - csio->cdb_len);
1453 
1454 #if 0
1455 	kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1456 		cdbs->generic.opcode,
1457 		csio->cdb_len, csio->dxfer_len);
1458 #endif
1459 
1460 	/*
1461 	 * Some ATAPI commands do not actually follow the SCSI standard.
1462 	 */
1463 	cdbd = (void *)xa->packetcmd;
1464 
1465 	switch(cdbd->generic.opcode) {
1466 	case REQUEST_SENSE:
1467 		/*
1468 		 * Force SENSE requests to the ATAPI sense length.
1469 		 *
1470 		 * It is unclear if this is needed or not.
1471 		 */
1472 		if (cdbd->sense.length == SSD_FULL_SIZE) {
1473 			if (bootverbose) {
1474 				kprintf("%s: Shortening sense request\n",
1475 					PORTNAME(ap));
1476 			}
1477 			cdbd->sense.length = offsetof(struct scsi_sense_data,
1478 						      extra_bytes[0]);
1479 		}
1480 		break;
1481 	case INQUIRY:
1482 		/*
1483 		 * Some ATAPI devices can't handle long inquiry lengths,
1484 		 * don't ask me why.  Truncate the inquiry length.
1485 		 */
1486 		if (cdbd->inquiry.page_code == 0 &&
1487 		    cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1488 			cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1489 		}
1490 		break;
1491 	case READ_6:
1492 	case WRITE_6:
1493 		/*
1494 		 * Convert *_6 to *_10 commands.  Most ATAPI devices
1495 		 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1496 		 */
1497 		cdbd->rw_10.opcode |= 0x20;
1498 		cdbd->rw_10.byte2 = 0;
1499 		cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1500 		cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1501 		cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1502 		cdbd->rw_10.addr[3] = 0;
1503 		cdbd->rw_10.reserved = 0;
1504 		cdbd->rw_10.length[0] = 0;
1505 		cdbd->rw_10.length[1] = cdbs->rw_6.length;
1506 		cdbd->rw_10.control = cdbs->rw_6.control;
1507 		break;
1508 	default:
1509 		break;
1510 	}
1511 
1512 	/*
1513 	 * And dispatch
1514 	 */
1515 	xa->complete = ahci_atapi_complete_cmd;
1516 	xa->atascsi_private = ccb;
1517 	ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1518 	ahci_os_lock_port(ap);
1519 	ahci_ata_cmd(xa);
1520 	ahci_os_unlock_port(ap);
1521 }
1522 
1523 /*
1524  * Simulate page inquiries for disk attachments.
1525  */
1526 static
1527 void
1528 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
1529 {
1530 	union {
1531 		struct scsi_vpd_supported_page_list	list;
1532 		struct scsi_vpd_unit_serial_number	serno;
1533 		struct scsi_vpd_unit_devid		devid;
1534 		char					buf[256];
1535 	} *page;
1536 	scsi_cdb_t cdb;
1537 	int i;
1538 	int j;
1539 	int len;
1540 
1541 	page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1542 
1543 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1544 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1545 
1546 	switch(cdb->inquiry.page_code) {
1547 	case SVPD_SUPPORTED_PAGE_LIST:
1548 		i = 0;
1549 		page->list.device = T_DIRECT;
1550 		page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1551 		page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1552 		page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1553 		page->list.list[i++] = SVPD_UNIT_DEVID;
1554 		page->list.length = i;
1555 		len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1556 		break;
1557 	case SVPD_UNIT_SERIAL_NUMBER:
1558 		i = 0;
1559 		j = sizeof(at->at_identify.serial);
1560 		for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1561 			;
1562 		while (j > i && at->at_identify.serial[j-1] == ' ')
1563 			--j;
1564 		page->serno.device = T_DIRECT;
1565 		page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1566 		page->serno.length = j - i;
1567 		bcopy(at->at_identify.serial + i,
1568 		      page->serno.serial_num, j - i);
1569 		len = offsetof(struct scsi_vpd_unit_serial_number,
1570 			       serial_num[j-i]);
1571 		break;
1572 	case SVPD_UNIT_DEVID:
1573 		/* fall through for now */
1574 	default:
1575 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1576 		len = 0;
1577 		break;
1578 	}
1579 	if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1580 		if (len <= ccb->csio.dxfer_len) {
1581 			ccb->ccb_h.status = CAM_REQ_CMP;
1582 			bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1583 			bcopy(page, ccb->csio.data_ptr, len);
1584 			ccb->csio.resid = ccb->csio.dxfer_len - len;
1585 		} else {
1586 			ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1587 		}
1588 	}
1589 	kfree(page, M_DEVBUF);
1590 }
1591 
1592 /*
1593  * Completion function for ATA_PORT_T_DISK cache synchronization.
1594  */
1595 static
1596 void
1597 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1598 {
1599 	union ccb *ccb = xa->atascsi_private;
1600 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1601 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1602 
1603 	switch(xa->state) {
1604 	case ATA_S_COMPLETE:
1605 		ccbh->status = CAM_REQ_CMP;
1606 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1607 		break;
1608 	case ATA_S_ERROR:
1609 		kprintf("%s: synchronize_cache: error\n",
1610 			ATANAME(ap, xa->at));
1611 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1612 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1613 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1614 		break;
1615 	case ATA_S_TIMEOUT:
1616 		kprintf("%s: synchronize_cache: timeout\n",
1617 			ATANAME(ap, xa->at));
1618 		ccbh->status = CAM_CMD_TIMEOUT;
1619 		break;
1620 	default:
1621 		kprintf("%s: synchronize_cache: unknown state %d\n",
1622 			ATANAME(ap, xa->at), xa->state);
1623 		ccbh->status = CAM_REQ_CMP_ERR;
1624 		break;
1625 	}
1626 	ahci_ata_put_xfer(xa);
1627 	ahci_os_unlock_port(ap);
1628 	xpt_done(ccb);
1629 	ahci_os_lock_port(ap);
1630 }
1631 
1632 /*
1633  * Completion function for ATA_PORT_T_DISK I/O
1634  */
1635 static
1636 void
1637 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1638 {
1639 	union ccb *ccb = xa->atascsi_private;
1640 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1641 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1642 
1643 	switch(xa->state) {
1644 	case ATA_S_COMPLETE:
1645 		ccbh->status = CAM_REQ_CMP;
1646 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1647 		break;
1648 	case ATA_S_ERROR:
1649 		kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1650 		ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1651 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1652 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1653 		break;
1654 	case ATA_S_TIMEOUT:
1655 		kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1656 		ccbh->status = CAM_CMD_TIMEOUT;
1657 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1658 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1659 		break;
1660 	default:
1661 		kprintf("%s: disk_rw: unknown state %d\n",
1662 			ATANAME(ap, xa->at), xa->state);
1663 		ccbh->status = CAM_REQ_CMP_ERR;
1664 		break;
1665 	}
1666 	ccb->csio.resid = xa->resid;
1667 	ahci_ata_put_xfer(xa);
1668 	ahci_os_unlock_port(ap);
1669 	xpt_done(ccb);
1670 	ahci_os_lock_port(ap);
1671 }
1672 
1673 /*
1674  * Completion function for ATA_PORT_T_ATAPI I/O
1675  *
1676  * Sense data is returned in the rfis.
1677  */
1678 static
1679 void
1680 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1681 {
1682 	union ccb *ccb = xa->atascsi_private;
1683 	struct ccb_hdr *ccbh = &ccb->ccb_h;
1684 	struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1685 	scsi_cdb_t cdb;
1686 
1687 	cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1688 			ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1689 
1690 	switch(xa->state) {
1691 	case ATA_S_COMPLETE:
1692 		ccbh->status = CAM_REQ_CMP;
1693 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1694 		break;
1695 	case ATA_S_ERROR:
1696 		ccbh->status = CAM_SCSI_STATUS_ERROR;
1697 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1698 		ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1699 		break;
1700 	case ATA_S_TIMEOUT:
1701 		kprintf("%s: cmd %d: timeout\n",
1702 			PORTNAME(ap), cdb->generic.opcode);
1703 		ccbh->status = CAM_CMD_TIMEOUT;
1704 		ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1705 		ahci_ata_dummy_sense(&ccb->csio.sense_data);
1706 		break;
1707 	default:
1708 		kprintf("%s: cmd %d: unknown state %d\n",
1709 			PORTNAME(ap), cdb->generic.opcode, xa->state);
1710 		ccbh->status = CAM_REQ_CMP_ERR;
1711 		break;
1712 	}
1713 	ccb->csio.resid = xa->resid;
1714 	ahci_ata_put_xfer(xa);
1715 	ahci_os_unlock_port(ap);
1716 	xpt_done(ccb);
1717 	ahci_os_lock_port(ap);
1718 }
1719 
1720 /*
1721  * Construct dummy sense data for errors on DISKs
1722  */
1723 static
1724 void
1725 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1726 {
1727 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1728 	sense_data->segment = 0;
1729 	sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1730 	sense_data->info[0] = 0;
1731 	sense_data->info[1] = 0;
1732 	sense_data->info[2] = 0;
1733 	sense_data->info[3] = 0;
1734 	sense_data->extra_len = 0;
1735 }
1736 
1737 /*
1738  * Construct atapi sense data for errors on ATAPI
1739  *
1740  * The ATAPI sense data is stored in the passed rfis and must be converted
1741  * to SCSI sense data.
1742  */
1743 static
1744 void
1745 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1746 		     struct scsi_sense_data *sense_data)
1747 {
1748 	sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1749 	sense_data->segment = 0;
1750 	sense_data->flags = (rfis->error & 0xF0) >> 4;
1751 	if (rfis->error & 0x04)
1752 		sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1753 	if (rfis->error & 0x02)
1754 		sense_data->flags |= SSD_EOM;
1755 	if (rfis->error & 0x01)
1756 		sense_data->flags |= SSD_ILI;
1757 	sense_data->info[0] = 0;
1758 	sense_data->info[1] = 0;
1759 	sense_data->info[2] = 0;
1760 	sense_data->info[3] = 0;
1761 	sense_data->extra_len = 0;
1762 }
1763 
1764 static
1765 void
1766 ahci_strip_string(const char **basep, int *lenp)
1767 {
1768 	const char *base = *basep;
1769 	int len = *lenp;
1770 
1771 	while (len && (*base == 0 || *base == ' ')) {
1772 		--len;
1773 		++base;
1774 	}
1775 	while (len && (base[len-1] == 0 || base[len-1] == ' '))
1776 		--len;
1777 	*basep = base;
1778 	*lenp = len;
1779 }
1780