4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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
37 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
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.
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.
51 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
54 * Implement each SATA port as its own SCSI bus on CAM. This way we can
55 * implement future port multiplier features as individual devices on the
58 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
59 * was written natively for DragonFly.
64 static void sili_xpt_action(struct cam_sim *sim, union ccb *ccb);
65 static void sili_xpt_poll(struct cam_sim *sim);
66 static void sili_xpt_scsi_disk_io(struct sili_port *ap,
67 struct ata_port *at, union ccb *ccb);
68 static void sili_xpt_scsi_atapi_io(struct sili_port *ap,
69 struct ata_port *at, union ccb *ccb);
70 static void sili_xpt_page_inquiry(struct sili_port *ap,
71 struct ata_port *at, union ccb *ccb);
73 static void sili_ata_complete_disk_rw(struct ata_xfer *xa);
74 static void sili_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
75 static void sili_atapi_complete_cmd(struct ata_xfer *xa);
76 static void sili_ata_dummy_sense(struct scsi_sense_data *sense_data);
77 static void sili_ata_atapi_sense(struct ata_fis_d2h *rfis,
78 struct scsi_sense_data *sense_data);
80 static int sili_cam_probe_disk(struct sili_port *ap, struct ata_port *at);
81 static int sili_cam_probe_atapi(struct sili_port *ap, struct ata_port *at);
82 static void sili_ata_dummy_done(struct ata_xfer *xa);
83 static void ata_fix_identify(struct ata_identify *id);
84 static int sili_set_xfer(struct sili_port *ap, struct ata_port *atx);
85 static void sili_cam_rescan(struct sili_port *ap);
86 static void sili_strip_string(const char **basep, int *lenp);
89 sili_cam_attach(struct sili_port *ap)
91 struct cam_devq *devq;
97 * We want at least one ccb to be available for error processing
98 * so don't let CAM use more then ncmds - 1.
100 unit = device_get_unit(ap->ap_sc->sc_dev);
101 if (ap->ap_sc->sc_ncmds > 1)
102 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
104 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
110 * Give the devq enough room to run with 32 max_dev_transactions,
111 * but set the overall max tags to 1 until NCQ is negotiated.
113 sim = cam_sim_alloc(sili_xpt_action, sili_xpt_poll, "sili",
114 (void *)ap, unit, &ap->ap_sim_lock,
116 cam_simq_release(devq);
121 sili_os_unlock_port(ap);
122 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
123 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
124 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
125 sili_os_lock_port(ap);
126 if (error != CAM_SUCCESS) {
130 ap->ap_flags |= AP_F_BUS_REGISTERED;
132 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
133 error = sili_cam_probe(ap, NULL);
140 ap->ap_flags |= AP_F_CAM_ATTACHED;
146 * The state of the port has changed.
148 * If atx is NULL the physical port has changed state.
149 * If atx is non-NULL a particular target behind a PM has changed state.
151 * If found is -1 the target state must be queued to a non-interrupt context.
152 * (only works with at == NULL).
154 * If found is 0 the target was removed.
155 * If found is 1 the target was inserted.
158 sili_cam_changed(struct sili_port *ap, struct ata_port *atx, int found)
160 struct cam_path *tmppath;
164 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
166 if (ap->ap_sim == NULL)
168 if (found == CAM_TARGET_WILDCARD) {
169 status = xpt_create_path(&tmppath, NULL,
170 cam_sim_path(ap->ap_sim),
171 target, CAM_LUN_WILDCARD);
172 if (status != CAM_REQ_CMP)
176 status = xpt_create_path(&tmppath, NULL,
177 cam_sim_path(ap->ap_sim),
180 if (status != CAM_REQ_CMP)
187 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
189 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
192 xpt_free_path(tmppath);
196 sili_cam_detach(struct sili_port *ap)
198 int error __debugvar;
200 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
202 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
204 xpt_freeze_simq(ap->ap_sim, 1);
206 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
207 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
208 KKASSERT(error == CAM_REQ_CMP);
209 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
212 cam_sim_free(ap->ap_sim);
215 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
216 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
220 * Once the SILI port has been attached we need to probe for a device or
221 * devices on the port and setup various options.
223 * If at is NULL we are probing the direct-attached device on the port,
224 * which may or may not be a port multiplier.
227 sili_cam_probe(struct sili_port *ap, struct ata_port *atx)
232 u_int64_t capacity_bytes;
239 const char *model_id;
240 const char *firmware_id;
241 const char *serial_id;
250 * Delayed CAM attachment for initial probe, sim may be NULL
252 if (ap->ap_sim == NULL)
256 * A NULL atx indicates a probe of the directly connected device.
257 * A non-NULL atx indicates a device connected via a port multiplier.
258 * We need to preserve atx for calls to sili_ata_get_xfer().
260 * at is always non-NULL. For directly connected devices we supply
261 * an (at) pointing to target 0.
264 at = ap->ap_ata; /* direct attached - device 0 */
265 if (ap->ap_type == ATA_PORT_T_PM) {
266 kprintf("%s: Found Port Multiplier\n",
270 at->at_type = ap->ap_type;
273 if (atx->at_type == ATA_PORT_T_PM) {
274 kprintf("%s: Bogus device, reducing port count to %d\n",
275 ATANAME(ap, atx), atx->at_target);
276 if (ap->ap_pmcount > atx->at_target)
277 ap->ap_pmcount = atx->at_target;
281 if (ap->ap_type == ATA_PORT_T_NONE)
283 if (at->at_type == ATA_PORT_T_NONE)
287 * Issue identify, saving the result
289 xa = sili_ata_get_xfer(ap, atx);
290 xa->complete = sili_ata_dummy_done;
291 xa->data = &at->at_identify;
292 xa->datalen = sizeof(at->at_identify);
293 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
294 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
296 switch(at->at_type) {
297 case ATA_PORT_T_DISK:
298 xa->fis->command = ATA_C_IDENTIFY;
301 case ATA_PORT_T_ATAPI:
302 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
303 xa->flags |= ATA_F_AUTOSENSE;
307 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
308 type = "UNKNOWN(ATAPI?)";
311 xa->fis->features = 0;
315 if (sili_ata_cmd(xa) != ATA_S_COMPLETE) {
316 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
317 ATANAME(ap, atx), type);
318 sili_ata_put_xfer(xa);
321 sili_ata_put_xfer(xa);
323 ata_fix_identify(&at->at_identify);
326 * Read capacity using SATA probe info.
328 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
329 /* LBA48 feature set supported */
331 for (i = 3; i >= 0; --i) {
334 le16toh(at->at_identify.addrsecxt[i]);
337 capacity = le16toh(at->at_identify.addrsec[1]);
339 capacity += le16toh(at->at_identify.addrsec[0]);
341 at->at_capacity = capacity;
343 ap->ap_probe = ATA_PROBE_GOOD;
345 capacity_bytes = capacity * 512;
348 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
349 * number of slots and limit the number of CAM ccb's to one less
350 * so we always have a slot available for recovery.
352 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
353 * not support it, and in that case the driver can handle extra
356 * NCQ is currently used only with direct-attached disks. It is
357 * not used with port multipliers or direct-attached ATAPI devices.
359 * Remember at least one extra CCB needs to be reserved for the
362 if ((ap->ap_sc->sc_flags & SILI_F_NCQ) &&
363 at->at_type == ATA_PORT_T_DISK &&
364 (le16toh(at->at_identify.satacap) & (1 << 8))) {
365 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
366 devncqdepth = at->at_ncqdepth;
367 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
368 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
369 if (at->at_ncqdepth > 1) {
370 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
371 xa = sili_ata_get_xfer(ap, atx);
372 if (xa->tag < at->at_ncqdepth) {
373 xa->state = ATA_S_COMPLETE;
374 sili_ata_put_xfer(xa);
377 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
378 cam_sim_set_max_tags(ap->ap_sim,
379 at->at_ncqdepth - 1);
387 * Make the model string a bit more presentable
389 for (model_len = 40; model_len; --model_len) {
390 if (at->at_identify.model[model_len-1] == ' ')
392 if (at->at_identify.model[model_len-1] == 0)
397 model_len = sizeof(at->at_identify.model);
398 model_id = at->at_identify.model;
399 sili_strip_string(&model_id, &model_len);
401 firmware_len = sizeof(at->at_identify.firmware);
402 firmware_id = at->at_identify.firmware;
403 sili_strip_string(&firmware_id, &firmware_len);
405 serial_len = sizeof(at->at_identify.serial);
406 serial_id = at->at_identify.serial;
407 sili_strip_string(&serial_id, &serial_len);
410 * Generate informatiive strings.
412 * NOTE: We do not automatically set write caching, lookahead,
413 * or the security state for ATAPI devices.
415 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
416 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
418 else if (at->at_type == ATA_PORT_T_ATAPI)
426 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
427 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
429 else if (at->at_type == ATA_PORT_T_ATAPI)
437 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
438 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
440 else if (at->at_type == ATA_PORT_T_ATAPI)
442 else if (SiliNoFeatures & (1 << ap->ap_num))
443 scstr = "<disabled>";
450 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
451 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
452 "capacity=%lld.%02dMB\n",
456 model_len, model_len, model_id,
457 firmware_len, firmware_len, firmware_id,
458 serial_len, serial_len, serial_id,
461 devncqdepth, ap->ap_sc->sc_ncmds,
462 at->at_identify.satacap,
463 at->at_identify.satafsup,
464 (at->at_ncqdepth > 1 ? "YES" : "NO"),
465 (long long)capacity_bytes / (1024 * 1024),
466 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
468 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
470 at->at_identify.features85,
471 at->at_identify.features86,
472 at->at_identify.features87,
479 * Additional type-specific probing
481 switch(at->at_type) {
482 case ATA_PORT_T_DISK:
483 error = sili_cam_probe_disk(ap, atx);
485 case ATA_PORT_T_ATAPI:
486 error = sili_cam_probe_atapi(ap, atx);
494 at->at_probe = ATA_PROBE_FAILED;
496 ap->ap_probe = at->at_probe;
498 at->at_probe = ATA_PROBE_GOOD;
500 ap->ap_probe = at->at_probe;
506 * DISK-specific probe after initial ident
509 sili_cam_probe_disk(struct sili_port *ap, struct ata_port *atx)
514 at = atx ? atx : ap->ap_ata;
517 * Set dummy xfer mode
519 sili_set_xfer(ap, atx);
522 * Enable write cache if supported
524 * NOTE: "WD My Book" external disk devices have a very poor
525 * daughter board between the the ESATA and the HD. Sending
526 * any ATA_C_SET_FEATURES commands will break the hardware port
527 * with a fatal protocol error. However, this device also
528 * indicates that WRITECACHE is already on and READAHEAD is
529 * not supported so we avoid the issue.
531 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
532 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
533 xa = sili_ata_get_xfer(ap, atx);
534 xa->complete = sili_ata_dummy_done;
535 xa->fis->command = ATA_C_SET_FEATURES;
536 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
537 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
538 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
540 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
543 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
544 at->at_features |= ATA_PORT_F_WCACHE;
546 kprintf("%s: Unable to enable write-caching\n",
548 sili_ata_put_xfer(xa);
552 * Enable readahead if supported
554 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
555 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
556 xa = sili_ata_get_xfer(ap, atx);
557 xa->complete = sili_ata_dummy_done;
558 xa->fis->command = ATA_C_SET_FEATURES;
559 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
560 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
562 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
565 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
566 at->at_features |= ATA_PORT_F_RAHEAD;
568 kprintf("%s: Unable to enable read-ahead\n",
570 sili_ata_put_xfer(xa);
574 * FREEZE LOCK the device so malicious users can't lock it on us.
575 * As there is no harm in issuing this to devices that don't
576 * support the security feature set we just send it, and don't bother
577 * checking if the device sends a command abort to tell us it doesn't
580 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
581 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
582 (SiliNoFeatures & (1 << ap->ap_num)) == 0) {
583 xa = sili_ata_get_xfer(ap, atx);
584 xa->complete = sili_ata_dummy_done;
585 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
586 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
587 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
590 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
591 at->at_features |= ATA_PORT_F_FRZLCK;
593 kprintf("%s: Unable to set security freeze\n",
595 sili_ata_put_xfer(xa);
602 * ATAPI-specific probe after initial ident
605 sili_cam_probe_atapi(struct sili_port *ap, struct ata_port *atx)
607 sili_set_xfer(ap, atx);
612 * Setting the transfer mode is irrelevant for the SATA transport
613 * but some (atapi) devices seem to need it anyway. In addition
614 * if we are running through a SATA->PATA converter for some reason
615 * beyond my comprehension we might have to set the mode.
617 * We only support DMA modes for SATA attached devices, so don't bother
621 sili_set_xfer(struct sili_port *ap, struct ata_port *atx)
628 at = atx ? atx : ap->ap_ata;
631 * Figure out the supported UDMA mode. Ignore other legacy modes.
633 mask = le16toh(at->at_identify.ultradma);
634 if ((mask & 0xFF) == 0 || mask == 0xFFFF)
638 while ((mask & 0x8000) == 0) {
644 * SATA atapi devices often still report a dma mode, even though
645 * it is irrelevant for SATA transport. It is also possible that
646 * we are running through a SATA->PATA converter and seeing the
649 * In this case the device may require a (dummy) SETXFER to be
650 * sent before it will work properly.
652 xa = sili_ata_get_xfer(ap, atx);
653 xa->complete = sili_ata_dummy_done;
654 xa->fis->command = ATA_C_SET_FEATURES;
655 xa->fis->features = ATA_SF_SETXFER;
656 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
657 xa->fis->sector_count = mode;
658 xa->flags = ATA_F_PIO | ATA_F_POLL;
661 if (sili_ata_cmd(xa) != ATA_S_COMPLETE) {
662 kprintf("%s: Unable to set dummy xfer mode \n",
664 } else if (bootverbose) {
665 kprintf("%s: Set dummy xfer mode to %02x\n",
666 ATANAME(ap, atx), mode);
668 sili_ata_put_xfer(xa);
673 * Fix byte ordering so buffers can be accessed as
677 ata_fix_identify(struct ata_identify *id)
682 swap = (u_int16_t *)id->serial;
683 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
684 swap[i] = bswap16(swap[i]);
686 swap = (u_int16_t *)id->firmware;
687 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
688 swap[i] = bswap16(swap[i]);
690 swap = (u_int16_t *)id->model;
691 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
692 swap[i] = bswap16(swap[i]);
696 * Dummy done callback for xa.
699 sili_ata_dummy_done(struct ata_xfer *xa)
704 * Use an engineering request to initiate a target scan for devices
705 * behind a port multiplier.
707 * An asynchronous bus scan is used to avoid reentrancy issues.
710 sili_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
712 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
714 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
715 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
716 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
717 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
720 ap->ap_flags |= AP_F_SCAN_COMPLETED;
721 wakeup(&ap->ap_flags);
727 sili_cam_rescan(struct sili_port *ap)
729 struct cam_path *path;
734 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
735 ap->ap_flags |= AP_F_SCAN_REQUESTED;
738 ap->ap_flags |= AP_F_SCAN_RUNNING;
739 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
740 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
743 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
744 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
745 if (status != CAM_REQ_CMP)
748 ccb = xpt_alloc_ccb();
749 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
750 ccb->ccb_h.func_code = XPT_ENG_EXEC;
751 ccb->ccb_h.cbfcnp = sili_cam_rescan_callback;
752 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
753 ccb->crcn.flags = CAM_FLAG_NONE;
754 xpt_action_async(ccb);
758 sili_xpt_rescan(struct sili_port *ap)
760 struct cam_path *path;
764 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
765 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
766 if (status != CAM_REQ_CMP)
769 ccb = xpt_alloc_ccb();
770 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
771 ccb->ccb_h.func_code = XPT_SCAN_BUS;
772 ccb->ccb_h.cbfcnp = sili_cam_rescan_callback;
773 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
774 ccb->crcn.flags = CAM_FLAG_NONE;
775 xpt_action_async(ccb);
779 * Action function - dispatch command
783 sili_xpt_action(struct cam_sim *sim, union ccb *ccb)
785 struct sili_port *ap;
786 struct ata_port *at, *atx;
787 struct ccb_hdr *ccbh;
790 ap = cam_sim_softc(sim);
793 KKASSERT(ap != NULL);
797 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
798 * otherwise the bus rescan will not remove the dead devices when
801 * For non-wildcards we have one target (0) and one lun (0),
802 * unless we have a port multiplier.
804 * A wildcard target indicates only the general bus is being
807 * Calculate at and atx. at is always non-NULL. atx is only
808 * NULL for direct-attached devices. It will be non-NULL for
809 * devices behind a port multiplier.
811 * XXX What do we do with a LUN wildcard?
813 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
814 ccbh->func_code != XPT_PATH_INQ) {
815 if (ap->ap_type == ATA_PORT_T_NONE) {
816 ccbh->status = CAM_DEV_NOT_THERE;
820 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
821 ccbh->status = CAM_DEV_NOT_THERE;
825 at += ccbh->target_id;
826 if (ap->ap_type == ATA_PORT_T_PM)
829 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
830 ccbh->status = CAM_DEV_NOT_THERE;
837 * Switch on the meta XPT command
839 switch(ccbh->func_code) {
842 * This routine is called after a port multiplier has been
845 ccbh->status = CAM_REQ_CMP;
846 sili_os_lock_port(ap);
847 sili_port_state_machine(ap, 0);
848 sili_os_unlock_port(ap);
854 * This command always succeeds, otherwise the bus scan
855 * will not detach dead devices.
857 ccb->cpi.version_num = 1;
858 ccb->cpi.hba_inquiry = 0;
859 ccb->cpi.target_sprt = 0;
860 ccb->cpi.hba_misc = PIM_SEQSCAN;
861 ccb->cpi.hba_eng_cnt = 0;
862 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
863 ccb->cpi.max_target = SILI_MAX_PMPORTS - 1;
864 ccb->cpi.max_lun = 0;
865 ccb->cpi.async_flags = 0;
866 ccb->cpi.hpath_id = 0;
867 ccb->cpi.initiator_id = SILI_MAX_PMPORTS - 1;
868 ccb->cpi.unit_number = cam_sim_unit(sim);
869 ccb->cpi.bus_id = cam_sim_bus(sim);
870 ccb->cpi.base_transfer_speed = 150000;
871 ccb->cpi.transport = XPORT_SATA;
872 ccb->cpi.transport_version = 1;
873 ccb->cpi.protocol = PROTO_SCSI;
874 ccb->cpi.protocol_version = SCSI_REV_2;
875 ccb->cpi.maxio = SILI_MAXPHYS;
877 ccbh->status = CAM_REQ_CMP;
878 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
879 sili_os_lock_port(ap);
880 sili_port_state_machine(ap, 0);
881 sili_os_unlock_port(ap);
883 switch(sili_pread(ap, SILI_PREG_SSTS) &
884 SILI_PREG_SSTS_SPD) {
885 case SILI_PREG_SSTS_SPD_GEN1:
886 ccb->cpi.base_transfer_speed = 150000;
888 case SILI_PREG_SSTS_SPD_GEN2:
889 ccb->cpi.base_transfer_speed = 300000;
893 ccb->cpi.base_transfer_speed = 1000;
897 if (ap->ap_type == ATA_PORT_T_NONE)
898 ccbh->status = CAM_DEV_NOT_THERE;
904 sili_os_lock_port(ap);
905 if (ap->ap_type == ATA_PORT_T_NONE) {
906 ccbh->status = CAM_DEV_NOT_THERE;
908 sili_port_reset(ap, atx, 0);
909 ccbh->status = CAM_REQ_CMP;
911 sili_os_unlock_port(ap);
915 sili_os_lock_port(ap);
916 sili_port_reset(ap, NULL, 1);
917 sili_os_unlock_port(ap);
918 ccbh->status = CAM_REQ_CMP;
921 case XPT_SET_TRAN_SETTINGS:
922 ccbh->status = CAM_FUNC_NOTAVAIL;
925 case XPT_GET_TRAN_SETTINGS:
926 ccb->cts.protocol = PROTO_SCSI;
927 ccb->cts.protocol_version = SCSI_REV_2;
928 ccb->cts.transport = XPORT_SATA;
929 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
930 ccb->cts.proto_specific.valid = 0;
931 ccb->cts.xport_specific.valid = 0;
932 ccbh->status = CAM_REQ_CMP;
935 case XPT_CALC_GEOMETRY:
936 cam_calc_geometry(&ccb->ccg, 1);
941 * Our parallel startup code might have only probed through
942 * to the IDENT, so do the last step if necessary.
944 if (at->at_probe == ATA_PROBE_NEED_IDENT)
945 sili_cam_probe(ap, atx);
946 if (at->at_probe != ATA_PROBE_GOOD) {
947 ccbh->status = CAM_DEV_NOT_THERE;
951 switch(at->at_type) {
952 case ATA_PORT_T_DISK:
953 sili_xpt_scsi_disk_io(ap, atx, ccb);
955 case ATA_PORT_T_ATAPI:
956 sili_xpt_scsi_atapi_io(ap, atx, ccb);
959 ccbh->status = CAM_REQ_INVALID;
965 ccbh->status = CAM_REQ_INVALID;
974 * Generally this function gets called heavily when interrupts might be
975 * non-operational, during a halt/reboot or panic.
979 sili_xpt_poll(struct cam_sim *sim)
981 struct sili_port *ap;
983 ap = cam_sim_softc(sim);
985 sili_os_lock_port(ap);
986 sili_port_intr(ap, 1);
987 sili_os_unlock_port(ap);
992 * Convert the SCSI command in ccb to an ata_xfer command in xa
993 * for ATA_PORT_T_DISK operations. Set the completion function
994 * to convert the response back, then dispatch to the OpenBSD SILI
997 * SILI DISK commands only support a limited command set, and we
998 * fake additional commands to make it play nice with the CAM subsystem.
1002 sili_xpt_scsi_disk_io(struct sili_port *ap, struct ata_port *atx,
1005 struct ccb_hdr *ccbh;
1006 struct ccb_scsiio *csio;
1007 struct ata_xfer *xa;
1008 struct ata_port *at;
1009 struct ata_fis_h2d *fis;
1010 struct ata_pass_12 *atp12;
1011 struct ata_pass_16 *atp16;
1013 union scsi_data *rdata;
1019 ccbh = &ccb->csio.ccb_h;
1021 at = atx ? atx : &ap->ap_ata[0];
1024 * XXX not passing NULL at for direct attach!
1026 xa = sili_ata_get_xfer(ap, atx);
1027 rdata = (void *)csio->data_ptr;
1028 rdata_len = csio->dxfer_len;
1031 * Build the FIS or process the csio to completion.
1033 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1034 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1036 switch(cdb->generic.opcode) {
1039 * Auto-sense everything, so explicit sense requests
1042 ccbh->status = CAM_SCSI_STATUS_ERROR;
1046 * Inquiry supported features
1048 * [opcode, byte2, page_code, length, control]
1050 if (cdb->inquiry.byte2 & SI_EVPD) {
1051 sili_xpt_page_inquiry(ap, at, ccb);
1053 bzero(rdata, rdata_len);
1054 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1055 ccbh->status = CAM_CCB_LEN_ERR;
1058 if (rdata_len > sizeof(rdata->inquiry_data))
1059 rdata_len = sizeof(rdata->inquiry_data);
1060 rdata->inquiry_data.device = T_DIRECT;
1061 rdata->inquiry_data.version = SCSI_REV_SPC2;
1062 rdata->inquiry_data.response_format = 2;
1063 rdata->inquiry_data.additional_length = 32;
1064 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1065 bcopy(at->at_identify.model,
1066 rdata->inquiry_data.product,
1067 sizeof(rdata->inquiry_data.product));
1068 bcopy(at->at_identify.firmware,
1069 rdata->inquiry_data.revision,
1070 sizeof(rdata->inquiry_data.revision));
1071 ccbh->status = CAM_REQ_CMP;
1074 case READ_CAPACITY_16:
1075 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1076 ccbh->status = CAM_REQ_INVALID;
1079 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1080 ccbh->status = CAM_CCB_LEN_ERR;
1085 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1086 ccbh->status = CAM_CCB_LEN_ERR;
1090 capacity = at->at_capacity;
1092 bzero(rdata, rdata_len);
1093 if (cdb->generic.opcode == READ_CAPACITY) {
1094 rdata_len = sizeof(rdata->read_capacity_data);
1095 if (capacity > 0xFFFFFFFFU) {
1097 * Set capacity to 0 so maxsector winds up
1098 * being 0xffffffff in CAM in order to trigger
1103 bzero(&rdata->read_capacity_data, rdata_len);
1104 scsi_ulto4b((u_int32_t)capacity - 1,
1105 rdata->read_capacity_data.addr);
1106 scsi_ulto4b(512, rdata->read_capacity_data.length);
1108 rdata_len = sizeof(rdata->read_capacity_data_16);
1109 bzero(&rdata->read_capacity_data_16, rdata_len);
1110 scsi_u64to8b(capacity - 1,
1111 rdata->read_capacity_data_16.addr);
1112 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1114 ccbh->status = CAM_REQ_CMP;
1116 case SYNCHRONIZE_CACHE:
1118 * Synchronize cache. Specification says this can take
1119 * greater then 30 seconds so give it at least 45.
1122 fis->flags = ATA_H2D_FLAGS_CMD;
1123 fis->command = ATA_C_FLUSH_CACHE;
1125 if (xa->timeout < 45000)
1126 xa->timeout = 45000;
1128 xa->flags = ATA_F_READ;
1129 xa->complete = sili_ata_complete_disk_synchronize_cache;
1131 case TEST_UNIT_READY:
1132 case START_STOP_UNIT:
1135 * Just silently return success
1137 ccbh->status = CAM_REQ_CMP;
1141 atp12 = &cdb->ata_pass_12;
1144 * Figure out the flags to be used, depending on the
1145 * direction of the CAM request.
1147 switch (ccbh->flags & CAM_DIR_MASK) {
1149 xa->flags = ATA_F_READ;
1152 xa->flags = ATA_F_WRITE;
1157 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1158 xa->data = csio->data_ptr;
1159 xa->datalen = csio->dxfer_len;
1160 xa->complete = sili_ata_complete_disk_rw;
1161 xa->timeout = ccbh->timeout;
1164 * Populate the fis from the information we received through CAM
1167 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */
1168 fis->features = atp12->features;
1169 fis->sector_count = atp12->sector_count;
1170 fis->lba_low = atp12->lba_low;
1171 fis->lba_mid = atp12->lba_mid;
1172 fis->lba_high = atp12->lba_high;
1173 fis->device = atp12->device; /* maybe always 0? */
1174 fis->command = atp12->command;
1175 fis->control = atp12->control;
1178 * Mark as in progress so it is sent to the device.
1180 ccbh->status = CAM_REQ_INPROG;
1183 atp16 = &cdb->ata_pass_16;
1186 * Figure out the flags to be used, depending on the direction of the
1189 switch (ccbh->flags & CAM_DIR_MASK) {
1191 xa->flags = ATA_F_READ;
1194 xa->flags = ATA_F_WRITE;
1199 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1200 xa->data = csio->data_ptr;
1201 xa->datalen = csio->dxfer_len;
1202 xa->complete = sili_ata_complete_disk_rw;
1203 xa->timeout = ccbh->timeout;
1206 * Populate the fis from the information we received through CAM
1209 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */
1210 fis->features = atp16->features;
1211 fis->features_exp = atp16->features_ext;
1212 fis->sector_count = atp16->sector_count;
1213 fis->sector_count_exp = atp16->sector_count_ext;
1214 fis->lba_low = atp16->lba_low;
1215 fis->lba_low_exp = atp16->lba_low_ext;
1216 fis->lba_mid = atp16->lba_mid;
1217 fis->lba_mid_exp = atp16->lba_mid_ext;
1218 fis->lba_high = atp16->lba_high;
1219 fis->lba_mid_exp = atp16->lba_mid_ext;
1220 fis->device = atp16->device; /* maybe always 0? */
1221 fis->command = atp16->command;
1224 * Mark as in progress so it is sent to the device.
1226 ccbh->status = CAM_REQ_INPROG;
1229 switch(cdb->generic.opcode) {
1231 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1232 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1233 xa->flags = ATA_F_READ;
1236 lba = scsi_4btoul(cdb->rw_10.addr);
1237 count = scsi_2btoul(cdb->rw_10.length);
1238 xa->flags = ATA_F_READ;
1241 lba = scsi_4btoul(cdb->rw_12.addr);
1242 count = scsi_4btoul(cdb->rw_12.length);
1243 xa->flags = ATA_F_READ;
1246 lba = scsi_8btou64(cdb->rw_16.addr);
1247 count = scsi_4btoul(cdb->rw_16.length);
1248 xa->flags = ATA_F_READ;
1251 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1252 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1253 xa->flags = ATA_F_WRITE;
1256 lba = scsi_4btoul(cdb->rw_10.addr);
1257 count = scsi_2btoul(cdb->rw_10.length);
1258 xa->flags = ATA_F_WRITE;
1261 lba = scsi_4btoul(cdb->rw_12.addr);
1262 count = scsi_4btoul(cdb->rw_12.length);
1263 xa->flags = ATA_F_WRITE;
1266 lba = scsi_8btou64(cdb->rw_16.addr);
1267 count = scsi_4btoul(cdb->rw_16.length);
1268 xa->flags = ATA_F_WRITE;
1271 ccbh->status = CAM_REQ_INVALID;
1274 if (ccbh->status != CAM_REQ_INPROG)
1278 fis->flags = ATA_H2D_FLAGS_CMD;
1279 fis->lba_low = (u_int8_t)lba;
1280 fis->lba_mid = (u_int8_t)(lba >> 8);
1281 fis->lba_high = (u_int8_t)(lba >> 16);
1282 fis->device = ATA_H2D_DEVICE_LBA;
1285 * NCQ only for direct-attached disks, do not currently
1286 * try to use NCQ with port multipliers.
1288 * XXX fixme SII chip can do NCQ w/ port multipliers.
1290 if (at->at_ncqdepth > 1 &&
1291 at->at_type == ATA_PORT_T_DISK &&
1292 (ap->ap_sc->sc_flags & SILI_F_NCQ) &&
1293 (ccbh->flags & CAM_POLLED) == 0) {
1295 * Use NCQ - always uses 48 bit addressing
1297 xa->flags |= ATA_F_NCQ;
1298 fis->command = (xa->flags & ATA_F_WRITE) ?
1299 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1300 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1301 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1302 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1303 fis->sector_count = xa->tag << 3;
1304 fis->features = (u_int8_t)count;
1305 fis->features_exp = (u_int8_t)(count >> 8);
1306 } else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1310 fis->command = (xa->flags & ATA_F_WRITE) ?
1311 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1312 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1313 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1314 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1315 fis->sector_count = (u_int8_t)count;
1316 fis->sector_count_exp = (u_int8_t)(count >> 8);
1321 * NOTE: 256 sectors is supported, stored as 0.
1323 fis->command = (xa->flags & ATA_F_WRITE) ?
1324 ATA_C_WRITEDMA : ATA_C_READDMA;
1325 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1326 fis->sector_count = (u_int8_t)count;
1329 xa->data = csio->data_ptr;
1330 xa->datalen = csio->dxfer_len;
1331 xa->complete = sili_ata_complete_disk_rw;
1332 xa->timeout = ccbh->timeout; /* milliseconds */
1333 if (ccbh->flags & CAM_POLLED)
1334 xa->flags |= ATA_F_POLL;
1339 * If the request is still in progress the xa and FIS have
1340 * been set up (except for the PM target), and must be dispatched.
1341 * Otherwise the request was completed.
1343 if (ccbh->status == CAM_REQ_INPROG) {
1344 KKASSERT(xa->complete != NULL);
1345 xa->atascsi_private = ccb;
1346 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1347 sili_os_lock_port(ap);
1348 xa->fis->flags |= at->at_target;
1350 sili_os_unlock_port(ap);
1352 sili_ata_put_xfer(xa);
1358 * Convert the SCSI command in ccb to an ata_xfer command in xa
1359 * for ATA_PORT_T_ATAPI operations. Set the completion function
1360 * to convert the response back, then dispatch to the OpenBSD SILI
1365 sili_xpt_scsi_atapi_io(struct sili_port *ap, struct ata_port *atx,
1368 struct ccb_hdr *ccbh;
1369 struct ccb_scsiio *csio;
1370 struct ata_xfer *xa;
1371 struct ata_fis_h2d *fis;
1375 struct ata_port *at;
1377 ccbh = &ccb->csio.ccb_h;
1379 at = atx ? atx : &ap->ap_ata[0];
1381 switch (ccbh->flags & CAM_DIR_MASK) {
1383 flags = ATA_F_PACKET | ATA_F_READ;
1386 flags = ATA_F_PACKET | ATA_F_WRITE;
1389 flags = ATA_F_PACKET;
1392 ccbh->status = CAM_REQ_INVALID;
1399 * Special handling to get the rfis back into host memory while
1400 * still allowing the chip to run commands in parallel to
1401 * ATAPI devices behind a PM.
1403 flags |= ATA_F_AUTOSENSE;
1406 * The command has to fit in the packet command buffer.
1408 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1409 ccbh->status = CAM_CCB_LEN_ERR;
1415 * Initialize the XA and FIS. It is unclear how much of
1416 * this has to mimic the equivalent ATA command.
1418 * XXX not passing NULL at for direct attach!
1420 xa = sili_ata_get_xfer(ap, atx);
1423 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1424 fis->command = ATA_C_PACKET;
1425 fis->device = ATA_H2D_DEVICE_LBA;
1426 fis->sector_count = xa->tag << 3;
1427 if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1428 if (flags & ATA_F_WRITE) {
1429 fis->features = ATA_H2D_FEATURES_DMA |
1430 ATA_H2D_FEATURES_DIR_WRITE;
1432 fis->features = ATA_H2D_FEATURES_DMA |
1433 ATA_H2D_FEATURES_DIR_READ;
1439 fis->control = ATA_FIS_CONTROL_4BIT;
1442 xa->data = csio->data_ptr;
1443 xa->datalen = csio->dxfer_len;
1444 xa->timeout = ccbh->timeout; /* milliseconds */
1446 if (ccbh->flags & CAM_POLLED)
1447 xa->flags |= ATA_F_POLL;
1450 * Copy the cdb to the packetcmd buffer in the FIS using a
1451 * convenient pointer in the xa.
1453 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1454 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1455 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1458 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1459 cdbs->generic.opcode,
1460 csio->cdb_len, csio->dxfer_len);
1464 * Some ATAPI commands do not actually follow the SCSI standard.
1466 cdbd = (void *)xa->packetcmd;
1468 switch(cdbd->generic.opcode) {
1471 * Force SENSE requests to the ATAPI sense length.
1473 * It is unclear if this is needed or not.
1475 if (cdbd->sense.length == SSD_FULL_SIZE) {
1476 kprintf("%s: Shortening sense request\n",
1478 cdbd->sense.length = offsetof(struct scsi_sense_data,
1484 * Some ATAPI devices can't handle long inquiry lengths,
1485 * don't ask me why. Truncate the inquiry length.
1487 if (cdbd->inquiry.page_code == 0 &&
1488 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1489 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1495 * Convert *_6 to *_10 commands. Most ATAPI devices
1496 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1498 cdbd->rw_10.opcode |= 0x20;
1499 cdbd->rw_10.byte2 = 0;
1500 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1501 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1502 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1503 cdbd->rw_10.addr[3] = 0;
1504 cdbd->rw_10.reserved = 0;
1505 cdbd->rw_10.length[0] = 0;
1506 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1507 cdbd->rw_10.control = cdbs->rw_6.control;
1516 xa->complete = sili_atapi_complete_cmd;
1517 xa->atascsi_private = ccb;
1518 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1519 sili_os_lock_port(ap);
1521 sili_os_unlock_port(ap);
1525 * Simulate page inquiries for disk attachments.
1529 sili_xpt_page_inquiry(struct sili_port *ap, struct ata_port *at, union ccb *ccb)
1532 struct scsi_vpd_supported_page_list list;
1533 struct scsi_vpd_unit_serial_number serno;
1534 struct scsi_vpd_unit_devid devid;
1542 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1544 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1545 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1547 switch(cdb->inquiry.page_code) {
1548 case SVPD_SUPPORTED_PAGE_LIST:
1550 page->list.device = T_DIRECT;
1551 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1552 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1553 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1554 page->list.list[i++] = SVPD_UNIT_DEVID;
1555 page->list.length = i;
1556 len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1558 case SVPD_UNIT_SERIAL_NUMBER:
1560 j = sizeof(at->at_identify.serial);
1561 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1563 while (j > i && at->at_identify.serial[j-1] == ' ')
1565 page->serno.device = T_DIRECT;
1566 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1567 page->serno.length = j - i;
1568 bcopy(at->at_identify.serial + i,
1569 page->serno.serial_num, j - i);
1570 len = offsetof(struct scsi_vpd_unit_serial_number,
1573 case SVPD_UNIT_DEVID:
1574 /* fall through for now */
1576 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1580 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1581 if (len <= ccb->csio.dxfer_len) {
1582 ccb->ccb_h.status = CAM_REQ_CMP;
1583 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1584 bcopy(page, ccb->csio.data_ptr, len);
1585 ccb->csio.resid = ccb->csio.dxfer_len - len;
1587 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1590 kfree(page, M_DEVBUF);
1594 * Completion function for ATA_PORT_T_DISK cache synchronization.
1598 sili_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1600 union ccb *ccb = xa->atascsi_private;
1601 struct ccb_hdr *ccbh = &ccb->ccb_h;
1602 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1605 case ATA_S_COMPLETE:
1606 ccbh->status = CAM_REQ_CMP;
1607 ccb->csio.scsi_status = SCSI_STATUS_OK;
1610 kprintf("%s: synchronize_cache: error\n",
1611 ATANAME(ap, xa->at));
1612 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1613 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1614 sili_ata_dummy_sense(&ccb->csio.sense_data);
1617 kprintf("%s: synchronize_cache: timeout\n",
1618 ATANAME(ap, xa->at));
1619 ccbh->status = CAM_CMD_TIMEOUT;
1622 kprintf("%s: synchronize_cache: unknown state %d\n",
1623 ATANAME(ap, xa->at), xa->state);
1624 ccbh->status = CAM_REQ_CMP_ERR;
1627 sili_ata_put_xfer(xa);
1628 /*sili_os_unlock_port(ap);*/
1630 /*sili_os_lock_port(ap);*/
1634 * Completion function for ATA_PORT_T_DISK I/O
1638 sili_ata_complete_disk_rw(struct ata_xfer *xa)
1640 union ccb *ccb = xa->atascsi_private;
1641 struct ccb_hdr *ccbh = &ccb->ccb_h;
1642 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1645 case ATA_S_COMPLETE:
1646 ccbh->status = CAM_REQ_CMP;
1647 ccb->csio.scsi_status = SCSI_STATUS_OK;
1650 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1651 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1652 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1653 sili_ata_dummy_sense(&ccb->csio.sense_data);
1656 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1657 ccbh->status = CAM_CMD_TIMEOUT;
1658 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1659 sili_ata_dummy_sense(&ccb->csio.sense_data);
1662 kprintf("%s: disk_rw: unknown state %d\n",
1663 ATANAME(ap, xa->at), xa->state);
1664 ccbh->status = CAM_REQ_CMP_ERR;
1667 ccb->csio.resid = xa->resid;
1668 sili_ata_put_xfer(xa);
1669 /*sili_os_unlock_port(ap);*/
1671 /*sili_os_lock_port(ap);*/
1675 * Completion function for ATA_PORT_T_ATAPI I/O
1677 * Sense data is returned in the rfis.
1681 sili_atapi_complete_cmd(struct ata_xfer *xa)
1683 union ccb *ccb = xa->atascsi_private;
1684 struct ccb_hdr *ccbh = &ccb->ccb_h;
1685 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1688 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1689 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1692 case ATA_S_COMPLETE:
1693 ccbh->status = CAM_REQ_CMP;
1694 ccb->csio.scsi_status = SCSI_STATUS_OK;
1697 ccbh->status = CAM_SCSI_STATUS_ERROR;
1698 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1699 sili_ata_atapi_sense(xa->rfis, &ccb->csio.sense_data);
1702 kprintf("%s: cmd %d: timeout\n",
1703 PORTNAME(ap), cdb->generic.opcode);
1704 ccbh->status = CAM_CMD_TIMEOUT;
1705 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1706 sili_ata_dummy_sense(&ccb->csio.sense_data);
1709 kprintf("%s: cmd %d: unknown state %d\n",
1710 PORTNAME(ap), cdb->generic.opcode, xa->state);
1711 ccbh->status = CAM_REQ_CMP_ERR;
1714 ccb->csio.resid = xa->resid;
1715 sili_ata_put_xfer(xa);
1716 /*sili_os_unlock_port(ap);*/
1718 /*sili_os_lock_port(ap);*/
1722 * Construct dummy sense data for errors on DISKs
1726 sili_ata_dummy_sense(struct scsi_sense_data *sense_data)
1728 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1729 sense_data->segment = 0;
1730 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1731 sense_data->info[0] = 0;
1732 sense_data->info[1] = 0;
1733 sense_data->info[2] = 0;
1734 sense_data->info[3] = 0;
1735 sense_data->extra_len = 0;
1739 * Construct atapi sense data for errors on ATAPI
1741 * The ATAPI sense data is stored in the passed rfis and must be converted
1742 * to SCSI sense data.
1746 sili_ata_atapi_sense(struct ata_fis_d2h *rfis,
1747 struct scsi_sense_data *sense_data)
1749 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1750 sense_data->segment = 0;
1751 sense_data->flags = (rfis->error & 0xF0) >> 4;
1752 if (rfis->error & 0x04)
1753 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1754 if (rfis->error & 0x02)
1755 sense_data->flags |= SSD_EOM;
1756 if (rfis->error & 0x01)
1757 sense_data->flags |= SSD_ILI;
1758 sense_data->info[0] = 0;
1759 sense_data->info[1] = 0;
1760 sense_data->info[2] = 0;
1761 sense_data->info[3] = 0;
1762 sense_data->extra_len = 0;
1767 sili_strip_string(const char **basep, int *lenp)
1769 const char *base = *basep;
1772 while (len && (*base == 0 || *base == ' ')) {
1776 while (len && (base[len-1] == 0 || base[len-1] == ' '))