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