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