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 $
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
59 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
60 * was written natively for DragonFly.
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);
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);
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);
90 sili_cam_attach(struct sili_port *ap)
92 struct cam_devq *devq;
98 * We want at least one ccb to be available for error processing
99 * so don't let CAM use more then ncmds - 1.
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);
105 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
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.
114 sim = cam_sim_alloc(sili_xpt_action, sili_xpt_poll, "sili",
115 (void *)ap, unit, &ap->ap_sim_lock,
117 cam_simq_release(devq);
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) {
131 ap->ap_flags |= AP_F_BUS_REGISTERED;
133 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
134 error = sili_cam_probe(ap, NULL);
141 ap->ap_flags |= AP_F_CAM_ATTACHED;
147 * The state of the port has changed.
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.
152 * If found is -1 the target state must be queued to a non-interrupt context.
153 * (only works with at == NULL).
155 * If found is 0 the target was removed.
156 * If found is 1 the target was inserted.
159 sili_cam_changed(struct sili_port *ap, struct ata_port *atx, int found)
161 struct cam_path *tmppath;
165 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
167 if (ap->ap_sim == NULL)
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)
177 status = xpt_create_path(&tmppath, NULL,
178 cam_sim_path(ap->ap_sim),
181 if (status != CAM_REQ_CMP)
188 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
190 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
193 xpt_free_path(tmppath);
197 sili_cam_detach(struct sili_port *ap)
201 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
203 lockmgr(&ap->ap_sim_lock, LK_EXCLUSIVE);
205 xpt_freeze_simq(ap->ap_sim, 1);
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;
213 cam_sim_free(ap->ap_sim);
216 lockmgr(&ap->ap_sim_lock, LK_RELEASE);
217 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
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.
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.
228 sili_cam_probe(struct sili_port *ap, struct ata_port *atx)
233 u_int64_t capacity_bytes;
240 const char *model_id;
241 const char *firmware_id;
242 const char *serial_id;
251 * Delayed CAM attachment for initial probe, sim may be NULL
253 if (ap->ap_sim == NULL)
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().
261 * at is always non-NULL. For directly connected devices we supply
262 * an (at) pointing to target 0.
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",
271 at->at_type = ap->ap_type;
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;
282 if (ap->ap_type == ATA_PORT_T_NONE)
284 if (at->at_type == ATA_PORT_T_NONE)
288 * Issue identify, saving the result
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;
297 switch(at->at_type) {
298 case ATA_PORT_T_DISK:
299 xa->fis->command = ATA_C_IDENTIFY;
302 case ATA_PORT_T_ATAPI:
303 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
304 xa->flags |= ATA_F_AUTOSENSE;
308 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
309 type = "UNKNOWN(ATAPI?)";
312 xa->fis->features = 0;
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);
322 sili_ata_put_xfer(xa);
324 ata_fix_identify(&at->at_identify);
327 * Read capacity using SATA probe info.
329 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
330 /* LBA48 feature set supported */
332 for (i = 3; i >= 0; --i) {
335 le16toh(at->at_identify.addrsecxt[i]);
338 capacity = le16toh(at->at_identify.addrsec[1]);
340 capacity += le16toh(at->at_identify.addrsec[0]);
342 at->at_capacity = capacity;
344 ap->ap_probe = ATA_PROBE_GOOD;
346 capacity_bytes = capacity * 512;
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.
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
357 * NCQ is currently used only with direct-attached disks. It is
358 * not used with port multipliers or direct-attached ATAPI devices.
360 * Remember at least one extra CCB needs to be reserved for the
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);
378 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
379 cam_sim_set_max_tags(ap->ap_sim,
380 at->at_ncqdepth - 1);
388 * Make the model string a bit more presentable
390 for (model_len = 40; model_len; --model_len) {
391 if (at->at_identify.model[model_len-1] == ' ')
393 if (at->at_identify.model[model_len-1] == 0)
398 model_len = sizeof(at->at_identify.model);
399 model_id = at->at_identify.model;
400 sili_strip_string(&model_id, &model_len);
402 firmware_len = sizeof(at->at_identify.firmware);
403 firmware_id = at->at_identify.firmware;
404 sili_strip_string(&firmware_id, &firmware_len);
406 serial_len = sizeof(at->at_identify.serial);
407 serial_id = at->at_identify.serial;
408 sili_strip_string(&serial_id, &serial_len);
411 * Generate informatiive strings.
413 * NOTE: We do not automatically set write caching, lookahead,
414 * or the security state for ATAPI devices.
416 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
417 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
419 else if (at->at_type == ATA_PORT_T_ATAPI)
427 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
428 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
430 else if (at->at_type == ATA_PORT_T_ATAPI)
438 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
439 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
441 else if (at->at_type == ATA_PORT_T_ATAPI)
443 else if (SiliNoFeatures & (1 << ap->ap_num))
444 scstr = "<disabled>";
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",
457 model_len, model_len, model_id,
458 firmware_len, firmware_len, firmware_id,
459 serial_len, serial_len, serial_id,
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)
469 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
471 at->at_identify.features85,
472 at->at_identify.features86,
473 at->at_identify.features87,
480 * Additional type-specific probing
482 switch(at->at_type) {
483 case ATA_PORT_T_DISK:
484 error = sili_cam_probe_disk(ap, atx);
486 case ATA_PORT_T_ATAPI:
487 error = sili_cam_probe_atapi(ap, atx);
495 at->at_probe = ATA_PROBE_FAILED;
497 ap->ap_probe = at->at_probe;
499 at->at_probe = ATA_PROBE_GOOD;
501 ap->ap_probe = at->at_probe;
507 * DISK-specific probe after initial ident
510 sili_cam_probe_disk(struct sili_port *ap, struct ata_port *atx)
515 at = atx ? atx : ap->ap_ata;
518 * Set dummy xfer mode
520 sili_set_xfer(ap, atx);
523 * Enable write cache if supported
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.
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;
541 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
544 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
545 at->at_features |= ATA_PORT_F_WCACHE;
547 kprintf("%s: Unable to enable write-caching\n",
549 sili_ata_put_xfer(xa);
553 * Enable readahead if supported
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;
563 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
566 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
567 at->at_features |= ATA_PORT_F_RAHEAD;
569 kprintf("%s: Unable to enable read-ahead\n",
571 sili_ata_put_xfer(xa);
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
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;
591 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
592 at->at_features |= ATA_PORT_F_FRZLCK;
594 kprintf("%s: Unable to set security freeze\n",
596 sili_ata_put_xfer(xa);
603 * ATAPI-specific probe after initial ident
606 sili_cam_probe_atapi(struct sili_port *ap, struct ata_port *atx)
608 sili_set_xfer(ap, atx);
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.
618 * We only support DMA modes for SATA attached devices, so don't bother
622 sili_set_xfer(struct sili_port *ap, struct ata_port *atx)
629 at = atx ? atx : ap->ap_ata;
632 * Figure out the supported UDMA mode. Ignore other legacy modes.
634 mask = le16toh(at->at_identify.ultradma);
635 if ((mask & 0xFF) == 0 || mask == 0xFFFF)
639 while ((mask & 0x8000) == 0) {
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
650 * In this case the device may require a (dummy) SETXFER to be
651 * sent before it will work properly.
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;
662 if (sili_ata_cmd(xa) != ATA_S_COMPLETE) {
663 kprintf("%s: Unable to set dummy xfer mode \n",
665 } else if (bootverbose) {
666 kprintf("%s: Set dummy xfer mode to %02x\n",
667 ATANAME(ap, atx), mode);
669 sili_ata_put_xfer(xa);
674 * Fix byte ordering so buffers can be accessed as
678 ata_fix_identify(struct ata_identify *id)
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]);
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]);
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]);
697 * Dummy done callback for xa.
700 sili_ata_dummy_done(struct ata_xfer *xa)
705 * Use an engineering request to initiate a target scan for devices
706 * behind a port multiplier.
708 * An asynchronous bus scan is used to avoid reentrancy issues.
711 sili_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
713 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
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;
721 ap->ap_flags |= AP_F_SCAN_COMPLETED;
722 wakeup(&ap->ap_flags);
728 sili_cam_rescan(struct sili_port *ap)
730 struct cam_path *path;
735 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
736 ap->ap_flags |= AP_F_SCAN_REQUESTED;
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;
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)
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);
759 sili_xpt_rescan(struct sili_port *ap)
761 struct cam_path *path;
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)
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);
780 * Action function - dispatch command
784 sili_xpt_action(struct cam_sim *sim, union ccb *ccb)
786 struct sili_port *ap;
787 struct ata_port *at, *atx;
788 struct ccb_hdr *ccbh;
792 ap = cam_sim_softc(sim);
795 KKASSERT(ap != NULL);
797 unit = cam_sim_unit(sim);
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
804 * For non-wildcards we have one target (0) and one lun (0),
805 * unless we have a port multiplier.
807 * A wildcard target indicates only the general bus is being
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.
814 * XXX What do we do with a LUN wildcard?
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;
823 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
824 ccbh->status = CAM_DEV_NOT_THERE;
828 at += ccbh->target_id;
829 if (ap->ap_type == ATA_PORT_T_PM)
832 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
833 ccbh->status = CAM_DEV_NOT_THERE;
840 * Switch on the meta XPT command
842 switch(ccbh->func_code) {
845 * This routine is called after a port multiplier has been
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);
857 * This command always succeeds, otherwise the bus scan
858 * will not detach dead devices.
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;
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);
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;
890 case SILI_PREG_SSTS_SPD_GEN2:
891 ccb->cpi.base_transfer_speed = 300000;
895 ccb->cpi.base_transfer_speed = 1000;
899 if (ap->ap_type == ATA_PORT_T_NONE)
900 ccbh->status = CAM_DEV_NOT_THERE;
906 sili_os_lock_port(ap);
907 if (ap->ap_type == ATA_PORT_T_NONE) {
908 ccbh->status = CAM_DEV_NOT_THERE;
910 sili_port_reset(ap, atx, 0);
911 ccbh->status = CAM_REQ_CMP;
913 sili_os_unlock_port(ap);
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;
923 case XPT_SET_TRAN_SETTINGS:
924 ccbh->status = CAM_FUNC_NOTAVAIL;
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;
937 case XPT_CALC_GEOMETRY:
938 cam_calc_geometry(&ccb->ccg, 1);
943 * Our parallel startup code might have only probed through
944 * to the IDENT, so do the last step if necessary.
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;
953 switch(at->at_type) {
954 case ATA_PORT_T_DISK:
955 sili_xpt_scsi_disk_io(ap, atx, ccb);
957 case ATA_PORT_T_ATAPI:
958 sili_xpt_scsi_atapi_io(ap, atx, ccb);
961 ccbh->status = CAM_REQ_INVALID;
967 ccbh->status = CAM_REQ_INVALID;
976 * Generally this function gets called heavily when interrupts might be
977 * non-operational, during a halt/reboot or panic.
981 sili_xpt_poll(struct cam_sim *sim)
983 struct sili_port *ap;
985 ap = cam_sim_softc(sim);
987 sili_os_lock_port(ap);
988 sili_port_intr(ap, 1);
989 sili_os_unlock_port(ap);
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
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.
1004 sili_xpt_scsi_disk_io(struct sili_port *ap, struct ata_port *atx,
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;
1015 union scsi_data *rdata;
1021 ccbh = &ccb->csio.ccb_h;
1023 at = atx ? atx : &ap->ap_ata[0];
1026 * XXX not passing NULL at for direct attach!
1028 xa = sili_ata_get_xfer(ap, atx);
1029 rdata = (void *)csio->data_ptr;
1030 rdata_len = csio->dxfer_len;
1033 * Build the FIS or process the csio to completion.
1035 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1036 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1038 switch(cdb->generic.opcode) {
1041 * Auto-sense everything, so explicit sense requests
1044 ccbh->status = CAM_SCSI_STATUS_ERROR;
1048 * Inquiry supported features
1050 * [opcode, byte2, page_code, length, control]
1052 if (cdb->inquiry.byte2 & SI_EVPD) {
1053 sili_xpt_page_inquiry(ap, at, ccb);
1055 bzero(rdata, rdata_len);
1056 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1057 ccbh->status = CAM_CCB_LEN_ERR;
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;
1076 case READ_CAPACITY_16:
1077 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1078 ccbh->status = CAM_REQ_INVALID;
1081 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1082 ccbh->status = CAM_CCB_LEN_ERR;
1087 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1088 ccbh->status = CAM_CCB_LEN_ERR;
1092 capacity = at->at_capacity;
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);
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);
1110 ccbh->status = CAM_REQ_CMP;
1112 case SYNCHRONIZE_CACHE:
1114 * Synchronize cache. Specification says this can take
1115 * greater then 30 seconds so give it at least 45.
1118 fis->flags = ATA_H2D_FLAGS_CMD;
1119 fis->command = ATA_C_FLUSH_CACHE;
1121 if (xa->timeout < 45000)
1122 xa->timeout = 45000;
1124 xa->flags = ATA_F_READ;
1125 xa->complete = sili_ata_complete_disk_synchronize_cache;
1127 case TEST_UNIT_READY:
1128 case START_STOP_UNIT:
1131 * Just silently return success
1133 ccbh->status = CAM_REQ_CMP;
1137 atp12 = &cdb->ata_pass_12;
1140 * Figure out the flags to be used, depending on the
1141 * direction of the CAM request.
1143 switch (ccbh->flags & CAM_DIR_MASK) {
1145 xa->flags = ATA_F_READ;
1148 xa->flags = ATA_F_WRITE;
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;
1160 * Populate the fis from the information we received through CAM
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;
1174 * Mark as in progress so it is sent to the device.
1176 ccbh->status = CAM_REQ_INPROG;
1179 atp16 = &cdb->ata_pass_16;
1182 * Figure out the flags to be used, depending on the direction of the
1185 switch (ccbh->flags & CAM_DIR_MASK) {
1187 xa->flags = ATA_F_READ;
1190 xa->flags = ATA_F_WRITE;
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;
1202 * Populate the fis from the information we received through CAM
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;
1220 * Mark as in progress so it is sent to the device.
1222 ccbh->status = CAM_REQ_INPROG;
1225 switch(cdb->generic.opcode) {
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;
1232 lba = scsi_4btoul(cdb->rw_10.addr);
1233 count = scsi_2btoul(cdb->rw_10.length);
1234 xa->flags = ATA_F_READ;
1237 lba = scsi_4btoul(cdb->rw_12.addr);
1238 count = scsi_4btoul(cdb->rw_12.length);
1239 xa->flags = ATA_F_READ;
1242 lba = scsi_8btou64(cdb->rw_16.addr);
1243 count = scsi_4btoul(cdb->rw_16.length);
1244 xa->flags = ATA_F_READ;
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;
1252 lba = scsi_4btoul(cdb->rw_10.addr);
1253 count = scsi_2btoul(cdb->rw_10.length);
1254 xa->flags = ATA_F_WRITE;
1257 lba = scsi_4btoul(cdb->rw_12.addr);
1258 count = scsi_4btoul(cdb->rw_12.length);
1259 xa->flags = ATA_F_WRITE;
1262 lba = scsi_8btou64(cdb->rw_16.addr);
1263 count = scsi_4btoul(cdb->rw_16.length);
1264 xa->flags = ATA_F_WRITE;
1267 ccbh->status = CAM_REQ_INVALID;
1270 if (ccbh->status != CAM_REQ_INPROG)
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;
1281 * NCQ only for direct-attached disks, do not currently
1282 * try to use NCQ with port multipliers.
1284 * XXX fixme SII chip can do NCQ w/ port multipliers.
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) {
1291 * Use NCQ - always uses 48 bit addressing
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) {
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);
1317 * NOTE: 256 sectors is supported, stored as 0.
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;
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;
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.
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;
1346 sili_os_unlock_port(ap);
1348 sili_ata_put_xfer(xa);
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
1361 sili_xpt_scsi_atapi_io(struct sili_port *ap, struct ata_port *atx,
1364 struct ccb_hdr *ccbh;
1365 struct ccb_scsiio *csio;
1366 struct ata_xfer *xa;
1367 struct ata_fis_h2d *fis;
1371 struct ata_port *at;
1373 ccbh = &ccb->csio.ccb_h;
1375 at = atx ? atx : &ap->ap_ata[0];
1377 switch (ccbh->flags & CAM_DIR_MASK) {
1379 flags = ATA_F_PACKET | ATA_F_READ;
1382 flags = ATA_F_PACKET | ATA_F_WRITE;
1385 flags = ATA_F_PACKET;
1388 ccbh->status = CAM_REQ_INVALID;
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.
1399 flags |= ATA_F_AUTOSENSE;
1402 * The command has to fit in the packet command buffer.
1404 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1405 ccbh->status = CAM_CCB_LEN_ERR;
1411 * Initialize the XA and FIS. It is unclear how much of
1412 * this has to mimic the equivalent ATA command.
1414 * XXX not passing NULL at for direct attach!
1416 xa = sili_ata_get_xfer(ap, atx);
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;
1428 fis->features = ATA_H2D_FEATURES_DMA |
1429 ATA_H2D_FEATURES_DIR_READ;
1435 fis->control = ATA_FIS_CONTROL_4BIT;
1438 xa->data = csio->data_ptr;
1439 xa->datalen = csio->dxfer_len;
1440 xa->timeout = ccbh->timeout; /* milliseconds */
1442 if (ccbh->flags & CAM_POLLED)
1443 xa->flags |= ATA_F_POLL;
1446 * Copy the cdb to the packetcmd buffer in the FIS using a
1447 * convenient pointer in the xa.
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);
1454 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1455 cdbs->generic.opcode,
1456 csio->cdb_len, csio->dxfer_len);
1460 * Some ATAPI commands do not actually follow the SCSI standard.
1462 cdbd = (void *)xa->packetcmd;
1464 switch(cdbd->generic.opcode) {
1467 * Force SENSE requests to the ATAPI sense length.
1469 * It is unclear if this is needed or not.
1471 if (cdbd->sense.length == SSD_FULL_SIZE) {
1472 kprintf("%s: Shortening sense request\n",
1474 cdbd->sense.length = offsetof(struct scsi_sense_data,
1480 * Some ATAPI devices can't handle long inquiry lengths,
1481 * don't ask me why. Truncate the inquiry length.
1483 if (cdbd->inquiry.page_code == 0 &&
1484 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1485 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1491 * Convert *_6 to *_10 commands. Most ATAPI devices
1492 * cannot handle the SCSI READ_6 and WRITE_6 commands.
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;
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);
1517 sili_os_unlock_port(ap);
1521 * Simulate page inquiries for disk attachments.
1525 sili_xpt_page_inquiry(struct sili_port *ap, struct ata_port *at, union ccb *ccb)
1528 struct scsi_vpd_supported_page_list list;
1529 struct scsi_vpd_unit_serial_number serno;
1530 struct scsi_vpd_unit_devid devid;
1538 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1540 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1541 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1543 switch(cdb->inquiry.page_code) {
1544 case SVPD_SUPPORTED_PAGE_LIST:
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]);
1554 case SVPD_UNIT_SERIAL_NUMBER:
1556 j = sizeof(at->at_identify.serial);
1557 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1559 while (j > i && at->at_identify.serial[j-1] == ' ')
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,
1569 case SVPD_UNIT_DEVID:
1570 /* fall through for now */
1572 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
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;
1583 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1586 kfree(page, M_DEVBUF);
1590 * Completion function for ATA_PORT_T_DISK cache synchronization.
1594 sili_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
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;
1601 case ATA_S_COMPLETE:
1602 ccbh->status = CAM_REQ_CMP;
1603 ccb->csio.scsi_status = SCSI_STATUS_OK;
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);
1613 kprintf("%s: synchronize_cache: timeout\n",
1614 ATANAME(ap, xa->at));
1615 ccbh->status = CAM_CMD_TIMEOUT;
1618 kprintf("%s: synchronize_cache: unknown state %d\n",
1619 ATANAME(ap, xa->at), xa->state);
1620 ccbh->status = CAM_REQ_CMP_ERR;
1623 sili_ata_put_xfer(xa);
1624 sili_os_unlock_port(ap);
1626 sili_os_lock_port(ap);
1630 * Completion function for ATA_PORT_T_DISK I/O
1634 sili_ata_complete_disk_rw(struct ata_xfer *xa)
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;
1641 case ATA_S_COMPLETE:
1642 ccbh->status = CAM_REQ_CMP;
1643 ccb->csio.scsi_status = SCSI_STATUS_OK;
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);
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);
1658 kprintf("%s: disk_rw: unknown state %d\n",
1659 ATANAME(ap, xa->at), xa->state);
1660 ccbh->status = CAM_REQ_CMP_ERR;
1663 ccb->csio.resid = xa->resid;
1664 sili_ata_put_xfer(xa);
1665 sili_os_unlock_port(ap);
1667 sili_os_lock_port(ap);
1671 * Completion function for ATA_PORT_T_ATAPI I/O
1673 * Sense data is returned in the rfis.
1677 sili_atapi_complete_cmd(struct ata_xfer *xa)
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;
1684 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1685 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1688 case ATA_S_COMPLETE:
1689 ccbh->status = CAM_REQ_CMP;
1690 ccb->csio.scsi_status = SCSI_STATUS_OK;
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);
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);
1705 kprintf("%s: cmd %d: unknown state %d\n",
1706 PORTNAME(ap), cdb->generic.opcode, xa->state);
1707 ccbh->status = CAM_REQ_CMP_ERR;
1710 ccb->csio.resid = xa->resid;
1711 sili_ata_put_xfer(xa);
1712 sili_os_unlock_port(ap);
1714 sili_os_lock_port(ap);
1718 * Construct dummy sense data for errors on DISKs
1722 sili_ata_dummy_sense(struct scsi_sense_data *sense_data)
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;
1735 * Construct atapi sense data for errors on ATAPI
1737 * The ATAPI sense data is stored in the passed rfis and must be converted
1738 * to SCSI sense data.
1742 sili_ata_atapi_sense(struct ata_fis_d2h *rfis,
1743 struct scsi_sense_data *sense_data)
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;
1763 sili_strip_string(const char **basep, int *lenp)
1765 const char *base = *basep;
1768 while (len && (*base == 0 || *base == ' ')) {
1772 while (len && (base[len-1] == 0 || base[len-1] == ' '))