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