2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
53 * Implement each SATA port as its own SCSI bus on CAM. This way we can
54 * implement future port multiplier features as individual devices on the
57 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
58 * was written natively for DragonFly.
63 const char *ScsiTypeArray
[32] = {
98 static void ahci_xpt_action(struct cam_sim
*sim
, union ccb
*ccb
);
99 static void ahci_xpt_poll(struct cam_sim
*sim
);
100 static void ahci_xpt_scsi_disk_io(struct ahci_port
*ap
,
101 struct ata_port
*at
, union ccb
*ccb
);
102 static void ahci_xpt_scsi_atapi_io(struct ahci_port
*ap
,
103 struct ata_port
*at
, union ccb
*ccb
);
104 static void ahci_xpt_page_inquiry(struct ahci_port
*ap
,
105 struct ata_port
*at
, union ccb
*ccb
);
107 static void ahci_ata_complete_disk_rw(struct ata_xfer
*xa
);
108 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer
*xa
);
109 static void ahci_atapi_complete_cmd(struct ata_xfer
*xa
);
110 static void ahci_ata_dummy_sense(struct scsi_sense_data
*sense_data
);
111 static void ahci_ata_atapi_sense(struct ata_fis_d2h
*rfis
,
112 struct scsi_sense_data
*sense_data
);
114 static int ahci_cam_probe_disk(struct ahci_port
*ap
, struct ata_port
*at
);
115 static int ahci_cam_probe_atapi(struct ahci_port
*ap
, struct ata_port
*at
);
116 static void ahci_ata_dummy_done(struct ata_xfer
*xa
);
117 static void ata_fix_identify(struct ata_identify
*id
);
118 static void ahci_cam_rescan(struct ahci_port
*ap
);
119 static void ahci_strip_string(const char **basep
, int *lenp
);
122 ahci_cam_attach(struct ahci_port
*ap
)
124 struct cam_devq
*devq
;
130 * We want at least one ccb to be available for error processing
131 * so don't let CAM use more then ncmds - 1.
133 unit
= device_get_unit(ap
->ap_sc
->sc_dev
);
134 if (ap
->ap_sc
->sc_ncmds
> 1)
135 devq
= cam_simq_alloc(ap
->ap_sc
->sc_ncmds
- 1);
137 devq
= cam_simq_alloc(ap
->ap_sc
->sc_ncmds
);
141 sim
= cam_sim_alloc(ahci_xpt_action
, ahci_xpt_poll
, "ahci",
142 (void *)ap
, unit
, &sim_mplock
, 1, 1, devq
);
143 cam_simq_release(devq
);
148 ahci_os_unlock_port(ap
);
149 error
= xpt_bus_register(ap
->ap_sim
, ap
->ap_num
);
150 ahci_os_lock_port(ap
);
151 if (error
!= CAM_SUCCESS
) {
155 ap
->ap_flags
|= AP_F_BUS_REGISTERED
;
157 if (ap
->ap_probe
== ATA_PROBE_NEED_IDENT
)
158 error
= ahci_cam_probe(ap
, NULL
);
165 ap
->ap_flags
|= AP_F_CAM_ATTACHED
;
171 * The state of the port has changed.
173 * If at is NULL the physical port has changed state.
174 * If at is non-NULL a particular target behind a PM has changed state.
176 * If found is -1 the target state must be queued to a non-interrupt context.
177 * (only works with at == NULL).
179 * If found is 0 the target was removed.
180 * If found is 1 the target was inserted.
183 ahci_cam_changed(struct ahci_port
*ap
, struct ata_port
*atx
, int found
)
185 struct cam_path
*tmppath
;
189 target
= atx
? atx
->at_target
: CAM_TARGET_WILDCARD
;
191 if (ap
->ap_sim
== NULL
)
193 if (found
== CAM_TARGET_WILDCARD
) {
194 status
= xpt_create_path(&tmppath
, NULL
,
195 cam_sim_path(ap
->ap_sim
),
196 target
, CAM_LUN_WILDCARD
);
197 if (status
!= CAM_REQ_CMP
)
201 status
= xpt_create_path(&tmppath
, NULL
,
202 cam_sim_path(ap
->ap_sim
),
205 if (status
!= CAM_REQ_CMP
)
212 xpt_async(AC_FOUND_DEVICE
, tmppath
, NULL
);
214 xpt_async(AC_LOST_DEVICE
, tmppath
, NULL
);
217 xpt_free_path(tmppath
);
221 ahci_cam_detach(struct ahci_port
*ap
)
225 if ((ap
->ap_flags
& AP_F_CAM_ATTACHED
) == 0)
229 xpt_freeze_simq(ap
->ap_sim
, 1);
231 if (ap
->ap_flags
& AP_F_BUS_REGISTERED
) {
232 error
= xpt_bus_deregister(cam_sim_path(ap
->ap_sim
));
233 KKASSERT(error
== CAM_REQ_CMP
);
234 ap
->ap_flags
&= ~AP_F_BUS_REGISTERED
;
237 cam_sim_free(ap
->ap_sim
);
241 ap
->ap_flags
&= ~AP_F_CAM_ATTACHED
;
245 * Once the AHCI port has been attached we need to probe for a device or
246 * devices on the port and setup various options.
248 * If at is NULL we are probing the direct-attached device on the port,
249 * which may or may not be a port multiplier.
252 ahci_cam_probe(struct ahci_port
*ap
, struct ata_port
*atx
)
257 u_int64_t capacity_bytes
;
264 const char *model_id
;
265 const char *firmware_id
;
266 const char *serial_id
;
275 * Delayed CAM attachment for initial probe, sim may be NULL
277 if (ap
->ap_sim
== NULL
)
281 * A NULL atx indicates a probe of the directly connected device.
282 * A non-NULL atx indicates a device connected via a port multiplier.
283 * We need to preserve atx for calls to ahci_ata_get_xfer().
285 * at is always non-NULL. For directly connected devices we supply
286 * an (at) pointing to target 0.
289 at
= ap
->ap_ata
; /* direct attached - device 0 */
290 if (ap
->ap_type
== ATA_PORT_T_PM
) {
291 kprintf("%s: Found Port Multiplier\n",
295 at
->at_type
= ap
->ap_type
;
298 if (atx
->at_type
== ATA_PORT_T_PM
) {
299 kprintf("%s: Bogus device, reducing port count to %d\n",
300 ATANAME(ap
, atx
), atx
->at_target
);
301 if (ap
->ap_pmcount
> atx
->at_target
)
302 ap
->ap_pmcount
= atx
->at_target
;
306 if (ap
->ap_type
== ATA_PORT_T_NONE
)
308 if (at
->at_type
== ATA_PORT_T_NONE
)
312 * Issue identify, saving the result
314 xa
= ahci_ata_get_xfer(ap
, atx
);
315 xa
->complete
= ahci_ata_dummy_done
;
316 xa
->data
= &at
->at_identify
;
317 xa
->datalen
= sizeof(at
->at_identify
);
318 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
319 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
321 switch(at
->at_type
) {
322 case ATA_PORT_T_DISK
:
323 xa
->fis
->command
= ATA_C_IDENTIFY
;
326 case ATA_PORT_T_ATAPI
:
327 xa
->fis
->command
= ATA_C_ATAPI_IDENTIFY
;
328 xa
->flags
|= ATA_F_AUTOSENSE
;
332 xa
->fis
->command
= ATA_C_ATAPI_IDENTIFY
;
333 type
= "UNKNOWN(ATAPI?)";
336 xa
->fis
->features
= 0;
340 if (ahci_ata_cmd(xa
) != ATA_S_COMPLETE
) {
341 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
342 ATANAME(ap
, atx
), type
);
343 ahci_ata_put_xfer(xa
);
346 ahci_ata_put_xfer(xa
);
348 ata_fix_identify(&at
->at_identify
);
351 * Read capacity using SATA probe info.
353 if (le16toh(at
->at_identify
.cmdset83
) & 0x0400) {
354 /* LBA48 feature set supported */
356 for (i
= 3; i
>= 0; --i
) {
359 le16toh(at
->at_identify
.addrsecxt
[i
]);
362 capacity
= le16toh(at
->at_identify
.addrsec
[1]);
364 capacity
+= le16toh(at
->at_identify
.addrsec
[0]);
367 capacity
= 1024 * 1024 / 512;
368 at
->at_capacity
= capacity
;
370 ap
->ap_probe
= ATA_PROBE_GOOD
;
372 capacity_bytes
= capacity
* 512;
375 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
376 * number of slots and limit the number of CAM ccb's to one less
377 * so we always have a slot available for recovery.
379 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
380 * not support it, and in that case the driver can handle extra
383 * NCQ is currently used only with direct-attached disks. It is
384 * not used with port multipliers or direct-attached ATAPI devices.
386 * Remember at least one extra CCB needs to be reserved for the
389 if ((ap
->ap_sc
->sc_cap
& AHCI_REG_CAP_SNCQ
) &&
390 ap
->ap_type
== ATA_PORT_T_DISK
&&
391 (le16toh(at
->at_identify
.satacap
) & (1 << 8))) {
392 at
->at_ncqdepth
= (le16toh(at
->at_identify
.qdepth
) & 0x1F) + 1;
393 devncqdepth
= at
->at_ncqdepth
;
394 if (at
->at_ncqdepth
> ap
->ap_sc
->sc_ncmds
)
395 at
->at_ncqdepth
= ap
->ap_sc
->sc_ncmds
;
396 if (at
->at_ncqdepth
> 1) {
397 for (i
= 0; i
< ap
->ap_sc
->sc_ncmds
; ++i
) {
398 xa
= ahci_ata_get_xfer(ap
, atx
);
399 if (xa
->tag
< at
->at_ncqdepth
) {
400 xa
->state
= ATA_S_COMPLETE
;
401 ahci_ata_put_xfer(xa
);
404 if (at
->at_ncqdepth
>= ap
->ap_sc
->sc_ncmds
) {
405 cam_devq_resize(ap
->ap_sim
->devq
,
406 at
->at_ncqdepth
- 1);
413 model_len
= sizeof(at
->at_identify
.model
);
414 model_id
= at
->at_identify
.model
;
415 ahci_strip_string(&model_id
, &model_len
);
417 firmware_len
= sizeof(at
->at_identify
.firmware
);
418 firmware_id
= at
->at_identify
.firmware
;
419 ahci_strip_string(&firmware_id
, &firmware_len
);
421 serial_len
= sizeof(at
->at_identify
.serial
);
422 serial_id
= at
->at_identify
.serial
;
423 ahci_strip_string(&serial_id
, &serial_len
);
426 * Generate informatiive strings.
428 * NOTE: We do not automatically set write caching, lookahead,
429 * or the security state for ATAPI devices.
431 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_WRITECACHE
) {
432 if (at
->at_identify
.features85
& ATA_IDENTIFY_WRITECACHE
)
434 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
442 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_LOOKAHEAD
) {
443 if (at
->at_identify
.features85
& ATA_IDENTIFY_LOOKAHEAD
)
445 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
453 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_SECURITY
) {
454 if (at
->at_identify
.securestatus
& ATA_SECURE_FROZEN
)
456 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
458 else if (AhciNoFeatures
& (1 << ap
->ap_num
))
459 scstr
= "<disabled>";
466 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
467 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
468 "capacity=%lld.%02dMB\n",
472 model_len
, model_len
, model_id
,
473 firmware_len
, firmware_len
, firmware_id
,
474 serial_len
, serial_len
, serial_id
,
477 devncqdepth
, ap
->ap_sc
->sc_ncmds
,
478 at
->at_identify
.satacap
,
479 at
->at_identify
.satafsup
,
480 (at
->at_ncqdepth
> 1 ? "YES" : "NO"),
481 (long long)capacity_bytes
/ (1024 * 1024),
482 (int)(capacity_bytes
% (1024 * 1024)) * 100 / (1024 * 1024)
484 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
486 at
->at_identify
.features85
,
487 at
->at_identify
.features86
,
488 at
->at_identify
.features87
,
495 * Additional type-specific probing
497 switch(at
->at_type
) {
498 case ATA_PORT_T_DISK
:
499 error
= ahci_cam_probe_disk(ap
, atx
);
501 case ATA_PORT_T_ATAPI
:
502 error
= ahci_cam_probe_atapi(ap
, atx
);
510 at
->at_probe
= ATA_PROBE_FAILED
;
512 ap
->ap_probe
= at
->at_probe
;
514 at
->at_probe
= ATA_PROBE_GOOD
;
516 ap
->ap_probe
= at
->at_probe
;
522 * DISK-specific probe after initial ident
525 ahci_cam_probe_disk(struct ahci_port
*ap
, struct ata_port
*atx
)
530 at
= atx
? atx
: ap
->ap_ata
;
533 * Enable write cache if supported
535 * NOTE: "WD My Book" external disk devices have a very poor
536 * daughter board between the the ESATA and the HD. Sending
537 * any ATA_C_SET_FEATURES commands will break the hardware port
538 * with a fatal protocol error. However, this device also
539 * indicates that WRITECACHE is already on and READAHEAD is
540 * not supported so we avoid the issue.
542 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_WRITECACHE
) &&
543 (at
->at_identify
.features85
& ATA_IDENTIFY_WRITECACHE
) == 0) {
544 xa
= ahci_ata_get_xfer(ap
, atx
);
545 xa
->complete
= ahci_ata_dummy_done
;
546 xa
->fis
->command
= ATA_C_SET_FEATURES
;
547 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
548 xa
->fis
->features
= ATA_SF_LOOKAHEAD_EN
;
549 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
551 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
554 if (ahci_ata_cmd(xa
) == ATA_S_COMPLETE
)
555 at
->at_features
|= ATA_PORT_F_WCACHE
;
557 kprintf("%s: Unable to enable write-caching\n",
559 ahci_ata_put_xfer(xa
);
563 * Enable readahead if supported
565 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_LOOKAHEAD
) &&
566 (at
->at_identify
.features85
& ATA_IDENTIFY_LOOKAHEAD
) == 0) {
567 xa
= ahci_ata_get_xfer(ap
, atx
);
568 xa
->complete
= ahci_ata_dummy_done
;
569 xa
->fis
->command
= ATA_C_SET_FEATURES
;
570 xa
->fis
->features
= ATA_SF_LOOKAHEAD_EN
;
571 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
573 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
576 if (ahci_ata_cmd(xa
) == ATA_S_COMPLETE
)
577 at
->at_features
|= ATA_PORT_F_RAHEAD
;
579 kprintf("%s: Unable to enable read-ahead\n",
581 ahci_ata_put_xfer(xa
);
585 * FREEZE LOCK the device so malicious users can't lock it on us.
586 * As there is no harm in issuing this to devices that don't
587 * support the security feature set we just send it, and don't bother
588 * checking if the device sends a command abort to tell us it doesn't
591 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_SECURITY
) &&
592 (at
->at_identify
.securestatus
& ATA_SECURE_FROZEN
) == 0 &&
593 (AhciNoFeatures
& (1 << ap
->ap_num
)) == 0) {
594 xa
= ahci_ata_get_xfer(ap
, atx
);
595 xa
->complete
= ahci_ata_dummy_done
;
596 xa
->fis
->command
= ATA_C_SEC_FREEZE_LOCK
;
597 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
598 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
601 if (ahci_ata_cmd(xa
) == ATA_S_COMPLETE
)
602 at
->at_features
|= ATA_PORT_F_FRZLCK
;
604 kprintf("%s: Unable to set security freeze\n",
606 ahci_ata_put_xfer(xa
);
613 * ATAPI-specific probe after initial ident
616 ahci_cam_probe_atapi(struct ahci_port
*ap
, struct ata_port
*atx
)
622 * Fix byte ordering so buffers can be accessed as
626 ata_fix_identify(struct ata_identify
*id
)
631 swap
= (u_int16_t
*)id
->serial
;
632 for (i
= 0; i
< sizeof(id
->serial
) / sizeof(u_int16_t
); i
++)
633 swap
[i
] = bswap16(swap
[i
]);
635 swap
= (u_int16_t
*)id
->firmware
;
636 for (i
= 0; i
< sizeof(id
->firmware
) / sizeof(u_int16_t
); i
++)
637 swap
[i
] = bswap16(swap
[i
]);
639 swap
= (u_int16_t
*)id
->model
;
640 for (i
= 0; i
< sizeof(id
->model
) / sizeof(u_int16_t
); i
++)
641 swap
[i
] = bswap16(swap
[i
]);
645 * Dummy done callback for xa.
648 ahci_ata_dummy_done(struct ata_xfer
*xa
)
653 * Use an engineering request to initiate a target scan for devices
654 * behind a port multiplier.
656 * An asynchronous bus scan is used to avoid reentrancy issues.
659 ahci_cam_rescan_callback(struct cam_periph
*periph
, union ccb
*ccb
)
661 struct ahci_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
663 if (ccb
->ccb_h
.func_code
== XPT_SCAN_BUS
) {
664 ap
->ap_flags
&= ~AP_F_SCAN_RUNNING
;
665 if (ap
->ap_flags
& AP_F_SCAN_REQUESTED
) {
666 ap
->ap_flags
&= ~AP_F_SCAN_REQUESTED
;
669 ap
->ap_flags
|= AP_F_SCAN_COMPLETED
;
670 wakeup(&ap
->ap_flags
);
676 ahci_cam_rescan(struct ahci_port
*ap
)
678 struct cam_path
*path
;
683 if (ap
->ap_flags
& AP_F_SCAN_RUNNING
) {
684 ap
->ap_flags
|= AP_F_SCAN_REQUESTED
;
687 ap
->ap_flags
|= AP_F_SCAN_RUNNING
;
688 for (i
= 0; i
< AHCI_MAX_PMPORTS
; ++i
) {
689 ap
->ap_ata
[i
].at_features
|= ATA_PORT_F_RESCAN
;
692 status
= xpt_create_path(&path
, xpt_periph
, cam_sim_path(ap
->ap_sim
),
693 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
694 if (status
!= CAM_REQ_CMP
)
697 ccb
= xpt_alloc_ccb();
698 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5); /* 5 = low priority */
699 ccb
->ccb_h
.func_code
= XPT_ENG_EXEC
;
700 ccb
->ccb_h
.cbfcnp
= ahci_cam_rescan_callback
;
701 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
702 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
703 xpt_action_async(ccb
);
707 ahci_xpt_rescan(struct ahci_port
*ap
)
709 struct cam_path
*path
;
713 status
= xpt_create_path(&path
, xpt_periph
, cam_sim_path(ap
->ap_sim
),
714 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
715 if (status
!= CAM_REQ_CMP
)
718 ccb
= xpt_alloc_ccb();
719 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5); /* 5 = low priority */
720 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
721 ccb
->ccb_h
.cbfcnp
= ahci_cam_rescan_callback
;
722 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
723 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
724 xpt_action_async(ccb
);
728 * Action function - dispatch command
732 ahci_xpt_action(struct cam_sim
*sim
, union ccb
*ccb
)
734 struct ahci_port
*ap
;
735 struct ata_port
*at
, *atx
;
736 struct ccb_hdr
*ccbh
;
740 ap
= cam_sim_softc(sim
);
743 KKASSERT(ap
!= NULL
);
745 unit
= cam_sim_unit(sim
);
748 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
749 * otherwise the bus rescan will not remove the dead devices when
752 * For non-wildcards we have one target (0) and one lun (0),
753 * unless we have a port multiplier.
755 * A wildcard target indicates only the general bus is being
758 * Calculate at and atx. at is always non-NULL. atx is only
759 * non-NULL for direct-attached devices. It will be NULL for
760 * devices behind a port multiplier.
762 * XXX What do we do with a LUN wildcard?
764 if (ccbh
->target_id
!= CAM_TARGET_WILDCARD
&&
765 ccbh
->func_code
!= XPT_PATH_INQ
) {
766 if (ap
->ap_type
== ATA_PORT_T_NONE
) {
767 ccbh
->status
= CAM_DEV_NOT_THERE
;
771 if (ccbh
->target_id
< 0 || ccbh
->target_id
>= ap
->ap_pmcount
) {
772 ccbh
->status
= CAM_DEV_NOT_THERE
;
776 at
+= ccbh
->target_id
;
777 if (ap
->ap_type
== ATA_PORT_T_PM
)
780 if (ccbh
->target_lun
!= CAM_LUN_WILDCARD
&& ccbh
->target_lun
) {
781 ccbh
->status
= CAM_DEV_NOT_THERE
;
788 * Switch on the meta XPT command
790 switch(ccbh
->func_code
) {
793 * This routine is called after a port multiplier has been
796 ccbh
->status
= CAM_REQ_CMP
;
797 ahci_os_lock_port(ap
);
798 ahci_port_state_machine(ap
, 0);
799 ahci_os_unlock_port(ap
);
805 * This command always succeeds, otherwise the bus scan
806 * will not detach dead devices.
808 ccb
->cpi
.version_num
= 1;
809 ccb
->cpi
.hba_inquiry
= 0;
810 ccb
->cpi
.target_sprt
= 0;
811 ccb
->cpi
.hba_misc
= PIM_SEQSCAN
;
812 ccb
->cpi
.hba_eng_cnt
= 0;
813 bzero(ccb
->cpi
.vuhba_flags
, sizeof(ccb
->cpi
.vuhba_flags
));
814 ccb
->cpi
.max_target
= AHCI_MAX_PMPORTS
- 1;
815 ccb
->cpi
.max_lun
= 0;
816 ccb
->cpi
.async_flags
= 0;
817 ccb
->cpi
.hpath_id
= 0;
818 ccb
->cpi
.initiator_id
= AHCI_MAX_PMPORTS
- 1;
819 ccb
->cpi
.unit_number
= cam_sim_unit(sim
);
820 ccb
->cpi
.bus_id
= cam_sim_bus(sim
);
821 ccb
->cpi
.base_transfer_speed
= 150000;
822 ccb
->cpi
.transport
= XPORT_SATA
;
823 ccb
->cpi
.transport_version
= 1;
824 ccb
->cpi
.protocol
= PROTO_SCSI
;
825 ccb
->cpi
.protocol_version
= SCSI_REV_2
;
827 ccbh
->status
= CAM_REQ_CMP
;
828 if (ccbh
->target_id
== CAM_TARGET_WILDCARD
) {
829 ahci_os_lock_port(ap
);
830 ahci_port_state_machine(ap
, 0);
831 ahci_os_unlock_port(ap
);
833 switch(ahci_pread(ap
, AHCI_PREG_SSTS
) &
834 AHCI_PREG_SSTS_SPD
) {
835 case AHCI_PREG_SSTS_SPD_GEN1
:
836 ccb
->cpi
.base_transfer_speed
= 150000;
838 case AHCI_PREG_SSTS_SPD_GEN2
:
839 ccb
->cpi
.base_transfer_speed
= 300000;
843 ccb
->cpi
.base_transfer_speed
= 1000;
847 if (ap
->ap_type
== ATA_PORT_T_NONE
)
848 ccbh
->status
= CAM_DEV_NOT_THERE
;
854 ahci_os_lock_port(ap
);
855 if (ap
->ap_type
== ATA_PORT_T_NONE
) {
856 ccbh
->status
= CAM_DEV_NOT_THERE
;
858 ahci_port_reset(ap
, atx
, 0);
859 ccbh
->status
= CAM_REQ_CMP
;
861 ahci_os_unlock_port(ap
);
865 ahci_os_lock_port(ap
);
866 ahci_port_reset(ap
, NULL
, 1);
867 ahci_os_unlock_port(ap
);
868 ccbh
->status
= CAM_REQ_CMP
;
871 case XPT_SET_TRAN_SETTINGS
:
872 ccbh
->status
= CAM_FUNC_NOTAVAIL
;
875 case XPT_GET_TRAN_SETTINGS
:
876 ccb
->cts
.protocol
= PROTO_SCSI
;
877 ccb
->cts
.protocol_version
= SCSI_REV_2
;
878 ccb
->cts
.transport
= XPORT_SATA
;
879 ccb
->cts
.transport_version
= XPORT_VERSION_UNSPECIFIED
;
880 ccb
->cts
.proto_specific
.valid
= 0;
881 ccb
->cts
.xport_specific
.valid
= 0;
882 ccbh
->status
= CAM_REQ_CMP
;
885 case XPT_CALC_GEOMETRY
:
886 cam_calc_geometry(&ccb
->ccg
, 1);
891 * Our parallel startup code might have only probed through
892 * to the IDENT, so do the last step if necessary.
894 if (at
->at_probe
== ATA_PROBE_NEED_IDENT
)
895 ahci_cam_probe(ap
, atx
);
896 if (at
->at_probe
!= ATA_PROBE_GOOD
) {
897 ccbh
->status
= CAM_DEV_NOT_THERE
;
901 switch(at
->at_type
) {
902 case ATA_PORT_T_DISK
:
903 ahci_xpt_scsi_disk_io(ap
, atx
, ccb
);
905 case ATA_PORT_T_ATAPI
:
906 ahci_xpt_scsi_atapi_io(ap
, atx
, ccb
);
909 ccbh
->status
= CAM_REQ_INVALID
;
915 ccbh
->status
= CAM_REQ_INVALID
;
924 * Generally this function gets called heavily when interrupts might be
925 * non-operational, during a halt/reboot or panic.
929 ahci_xpt_poll(struct cam_sim
*sim
)
931 struct ahci_port
*ap
;
933 ap
= cam_sim_softc(sim
);
935 ahci_os_lock_port(ap
);
936 ahci_port_intr(ap
, 1);
937 ahci_os_unlock_port(ap
);
942 * Convert the SCSI command in ccb to an ata_xfer command in xa
943 * for ATA_PORT_T_DISK operations. Set the completion function
944 * to convert the response back, then dispatch to the OpenBSD AHCI
947 * AHCI DISK commands only support a limited command set, and we
948 * fake additional commands to make it play nice with the CAM subsystem.
952 ahci_xpt_scsi_disk_io(struct ahci_port
*ap
, struct ata_port
*atx
,
955 struct ccb_hdr
*ccbh
;
956 struct ccb_scsiio
*csio
;
959 struct ata_fis_h2d
*fis
;
961 union scsi_data
*rdata
;
967 ccbh
= &ccb
->csio
.ccb_h
;
969 at
= atx
? atx
: &ap
->ap_ata
[0];
972 * XXX not passing NULL at for direct attach!
974 xa
= ahci_ata_get_xfer(ap
, atx
);
975 rdata
= (void *)csio
->data_ptr
;
976 rdata_len
= csio
->dxfer_len
;
979 * Build the FIS or process the csio to completion.
981 cdb
= (void *)((ccbh
->flags
& CAM_CDB_POINTER
) ?
982 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
);
984 switch(cdb
->generic
.opcode
) {
987 * Auto-sense everything, so explicit sense requests
990 ccbh
->status
= CAM_SCSI_STATUS_ERROR
;
994 * Inquiry supported features
996 * [opcode, byte2, page_code, length, control]
998 if (cdb
->inquiry
.byte2
& SI_EVPD
) {
999 ahci_xpt_page_inquiry(ap
, at
, ccb
);
1001 bzero(rdata
, rdata_len
);
1002 if (rdata_len
< SHORT_INQUIRY_LENGTH
) {
1003 ccbh
->status
= CAM_CCB_LEN_ERR
;
1006 if (rdata_len
> sizeof(rdata
->inquiry_data
))
1007 rdata_len
= sizeof(rdata
->inquiry_data
);
1008 rdata
->inquiry_data
.device
= T_DIRECT
;
1009 rdata
->inquiry_data
.version
= SCSI_REV_SPC2
;
1010 rdata
->inquiry_data
.response_format
= 2;
1011 rdata
->inquiry_data
.additional_length
= 32;
1012 bcopy("SATA ", rdata
->inquiry_data
.vendor
, 8);
1013 bcopy(at
->at_identify
.model
,
1014 rdata
->inquiry_data
.product
,
1015 sizeof(rdata
->inquiry_data
.product
));
1016 bcopy(at
->at_identify
.firmware
,
1017 rdata
->inquiry_data
.revision
,
1018 sizeof(rdata
->inquiry_data
.revision
));
1019 ccbh
->status
= CAM_REQ_CMP
;
1022 case READ_CAPACITY_16
:
1023 if (cdb
->read_capacity_16
.service_action
!= SRC16_SERVICE_ACTION
) {
1024 ccbh
->status
= CAM_REQ_INVALID
;
1027 if (rdata_len
< sizeof(rdata
->read_capacity_data_16
)) {
1028 ccbh
->status
= CAM_CCB_LEN_ERR
;
1033 if (rdata_len
< sizeof(rdata
->read_capacity_data
)) {
1034 ccbh
->status
= CAM_CCB_LEN_ERR
;
1038 capacity
= at
->at_capacity
;
1040 bzero(rdata
, rdata_len
);
1041 if (cdb
->generic
.opcode
== READ_CAPACITY
) {
1042 rdata_len
= sizeof(rdata
->read_capacity_data
);
1043 if (capacity
> 0xFFFFFFFFU
)
1044 capacity
= 0xFFFFFFFFU
;
1045 bzero(&rdata
->read_capacity_data
, rdata_len
);
1046 scsi_ulto4b((u_int32_t
)capacity
- 1,
1047 rdata
->read_capacity_data
.addr
);
1048 scsi_ulto4b(512, rdata
->read_capacity_data
.length
);
1050 rdata_len
= sizeof(rdata
->read_capacity_data_16
);
1051 bzero(&rdata
->read_capacity_data_16
, rdata_len
);
1052 scsi_u64to8b(capacity
- 1,
1053 rdata
->read_capacity_data_16
.addr
);
1054 scsi_ulto4b(512, rdata
->read_capacity_data_16
.length
);
1056 ccbh
->status
= CAM_REQ_CMP
;
1058 case SYNCHRONIZE_CACHE
:
1060 * Synchronize cache. Specification says this can take
1061 * greater then 30 seconds so give it at least 45.
1064 fis
->flags
= ATA_H2D_FLAGS_CMD
;
1065 fis
->command
= ATA_C_FLUSH_CACHE
;
1067 if (xa
->timeout
< 45000)
1068 xa
->timeout
= 45000;
1070 xa
->flags
= ATA_F_READ
;
1071 xa
->complete
= ahci_ata_complete_disk_synchronize_cache
;
1073 case TEST_UNIT_READY
:
1074 case START_STOP_UNIT
:
1077 * Just silently return success
1079 ccbh
->status
= CAM_REQ_CMP
;
1085 * XXX implement pass-through
1087 ccbh
->status
= CAM_FUNC_NOTAVAIL
;
1090 switch(cdb
->generic
.opcode
) {
1092 lba
= scsi_3btoul(cdb
->rw_6
.addr
) & 0x1FFFFF;
1093 count
= cdb
->rw_6
.length
? cdb
->rw_6
.length
: 0x100;
1094 xa
->flags
= ATA_F_READ
;
1097 lba
= scsi_4btoul(cdb
->rw_10
.addr
);
1098 count
= scsi_2btoul(cdb
->rw_10
.length
);
1099 xa
->flags
= ATA_F_READ
;
1102 lba
= scsi_4btoul(cdb
->rw_12
.addr
);
1103 count
= scsi_4btoul(cdb
->rw_12
.length
);
1104 xa
->flags
= ATA_F_READ
;
1107 lba
= scsi_8btou64(cdb
->rw_16
.addr
);
1108 count
= scsi_4btoul(cdb
->rw_16
.length
);
1109 xa
->flags
= ATA_F_READ
;
1112 lba
= scsi_3btoul(cdb
->rw_6
.addr
) & 0x1FFFFF;
1113 count
= cdb
->rw_6
.length
? cdb
->rw_6
.length
: 0x100;
1114 xa
->flags
= ATA_F_WRITE
;
1117 lba
= scsi_4btoul(cdb
->rw_10
.addr
);
1118 count
= scsi_2btoul(cdb
->rw_10
.length
);
1119 xa
->flags
= ATA_F_WRITE
;
1122 lba
= scsi_4btoul(cdb
->rw_12
.addr
);
1123 count
= scsi_4btoul(cdb
->rw_12
.length
);
1124 xa
->flags
= ATA_F_WRITE
;
1127 lba
= scsi_8btou64(cdb
->rw_16
.addr
);
1128 count
= scsi_4btoul(cdb
->rw_16
.length
);
1129 xa
->flags
= ATA_F_WRITE
;
1132 ccbh
->status
= CAM_REQ_INVALID
;
1135 if (ccbh
->status
!= CAM_REQ_INPROG
)
1139 fis
->flags
= ATA_H2D_FLAGS_CMD
;
1140 fis
->lba_low
= (u_int8_t
)lba
;
1141 fis
->lba_mid
= (u_int8_t
)(lba
>> 8);
1142 fis
->lba_high
= (u_int8_t
)(lba
>> 16);
1143 fis
->device
= ATA_H2D_DEVICE_LBA
;
1146 * NCQ only for direct-attached disks, do not currently
1147 * try to use NCQ with port multipliers.
1149 if (at
->at_ncqdepth
> 1 &&
1150 ap
->ap_type
== ATA_PORT_T_DISK
&&
1151 (ap
->ap_sc
->sc_cap
& AHCI_REG_CAP_SNCQ
) &&
1152 (ccbh
->flags
& CAM_POLLED
) == 0) {
1154 * Use NCQ - always uses 48 bit addressing
1156 xa
->flags
|= ATA_F_NCQ
;
1157 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1158 ATA_C_WRITE_FPDMA
: ATA_C_READ_FPDMA
;
1159 fis
->lba_low_exp
= (u_int8_t
)(lba
>> 24);
1160 fis
->lba_mid_exp
= (u_int8_t
)(lba
>> 32);
1161 fis
->lba_high_exp
= (u_int8_t
)(lba
>> 40);
1162 fis
->sector_count
= xa
->tag
<< 3;
1163 fis
->features
= (u_int8_t
)count
;
1164 fis
->features_exp
= (u_int8_t
)(count
>> 8);
1165 } else if (count
> 0x100 || lba
> 0xFFFFFFFFU
) {
1169 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1170 ATA_C_WRITEDMA_EXT
: ATA_C_READDMA_EXT
;
1171 fis
->lba_low_exp
= (u_int8_t
)(lba
>> 24);
1172 fis
->lba_mid_exp
= (u_int8_t
)(lba
>> 32);
1173 fis
->lba_high_exp
= (u_int8_t
)(lba
>> 40);
1174 fis
->sector_count
= (u_int8_t
)count
;
1175 fis
->sector_count_exp
= (u_int8_t
)(count
>> 8);
1180 * NOTE: 256 sectors is supported, stored as 0.
1182 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1183 ATA_C_WRITEDMA
: ATA_C_READDMA
;
1184 fis
->device
|= (u_int8_t
)(lba
>> 24) & 0x0F;
1185 fis
->sector_count
= (u_int8_t
)count
;
1188 xa
->data
= csio
->data_ptr
;
1189 xa
->datalen
= csio
->dxfer_len
;
1190 xa
->complete
= ahci_ata_complete_disk_rw
;
1191 xa
->timeout
= ccbh
->timeout
; /* milliseconds */
1193 if (xa
->timeout
> 10000) /* XXX - debug */
1194 xa
->timeout
= 10000;
1196 if (ccbh
->flags
& CAM_POLLED
)
1197 xa
->flags
|= ATA_F_POLL
;
1202 * If the request is still in progress the xa and FIS have
1203 * been set up (except for the PM target), and must be dispatched.
1204 * Otherwise the request was completed.
1206 if (ccbh
->status
== CAM_REQ_INPROG
) {
1207 KKASSERT(xa
->complete
!= NULL
);
1208 xa
->atascsi_private
= ccb
;
1209 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
1210 ahci_os_lock_port(ap
);
1211 xa
->fis
->flags
|= at
->at_target
;
1213 ahci_os_unlock_port(ap
);
1215 ahci_ata_put_xfer(xa
);
1221 * Convert the SCSI command in ccb to an ata_xfer command in xa
1222 * for ATA_PORT_T_ATAPI operations. Set the completion function
1223 * to convert the response back, then dispatch to the OpenBSD AHCI
1228 ahci_xpt_scsi_atapi_io(struct ahci_port
*ap
, struct ata_port
*atx
,
1231 struct ccb_hdr
*ccbh
;
1232 struct ccb_scsiio
*csio
;
1233 struct ata_xfer
*xa
;
1234 struct ata_fis_h2d
*fis
;
1238 struct ata_port
*at
;
1240 ccbh
= &ccb
->csio
.ccb_h
;
1242 at
= atx
? atx
: &ap
->ap_ata
[0];
1244 switch (ccbh
->flags
& CAM_DIR_MASK
) {
1246 flags
= ATA_F_PACKET
| ATA_F_READ
;
1249 flags
= ATA_F_PACKET
| ATA_F_WRITE
;
1252 flags
= ATA_F_PACKET
;
1255 ccbh
->status
= CAM_REQ_INVALID
;
1262 * Special handling to get the rfis back into host memory while
1263 * still allowing the Sili chip to run commands in parallel to
1264 * ATAPI devices behind a PM.
1266 flags
|= ATA_F_AUTOSENSE
;
1269 * The command has to fit in the packet command buffer.
1271 if (csio
->cdb_len
< 6 || csio
->cdb_len
> 16) {
1272 ccbh
->status
= CAM_CCB_LEN_ERR
;
1278 * Initialize the XA and FIS.
1280 * XXX not passing NULL at for direct attach!
1282 xa
= ahci_ata_get_xfer(ap
, atx
);
1285 fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
1286 fis
->command
= ATA_C_PACKET
;
1288 fis
->sector_count
= xa
->tag
<< 3;
1289 fis
->features
= ATA_H2D_FEATURES_DMA
|
1290 ((flags
& ATA_F_WRITE
) ?
1291 ATA_H2D_FEATURES_DIR_WRITE
: ATA_H2D_FEATURES_DIR_READ
);
1292 fis
->lba_mid
= 0x00;
1293 fis
->lba_high
= 0x20;
1296 xa
->data
= csio
->data_ptr
;
1297 xa
->datalen
= csio
->dxfer_len
;
1298 xa
->timeout
= ccbh
->timeout
; /* milliseconds */
1300 if (ccbh
->flags
& CAM_POLLED
)
1301 xa
->flags
|= ATA_F_POLL
;
1304 * Copy the cdb to the packetcmd buffer in the FIS using a
1305 * convenient pointer in the xa.
1307 cdbs
= (void *)((ccbh
->flags
& CAM_CDB_POINTER
) ?
1308 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
);
1309 bcopy(cdbs
, xa
->packetcmd
, csio
->cdb_len
);
1312 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1313 cdbs
->generic
.opcode
,
1314 csio
->cdb_len
, csio
->dxfer_len
);
1318 * Some ATAPI commands do not actually follow the SCSI standard.
1320 cdbd
= (void *)xa
->packetcmd
;
1322 switch(cdbd
->generic
.opcode
) {
1325 * Some ATAPI devices can't handle long inquiry lengths,
1326 * don't ask me why. Truncate the inquiry length.
1328 if (cdbd
->inquiry
.page_code
== 0 &&
1329 cdbd
->inquiry
.length
> SHORT_INQUIRY_LENGTH
) {
1330 cdbd
->inquiry
.length
= SHORT_INQUIRY_LENGTH
;
1336 * Convert *_6 to *_10 commands. Most ATAPI devices
1337 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1339 cdbd
->rw_10
.opcode
|= 0x20;
1340 cdbd
->rw_10
.byte2
= 0;
1341 cdbd
->rw_10
.addr
[0] = cdbs
->rw_6
.addr
[0] & 0x1F;
1342 cdbd
->rw_10
.addr
[1] = cdbs
->rw_6
.addr
[1];
1343 cdbd
->rw_10
.addr
[2] = cdbs
->rw_6
.addr
[2];
1344 cdbd
->rw_10
.addr
[3] = 0;
1345 cdbd
->rw_10
.reserved
= 0;
1346 cdbd
->rw_10
.length
[0] = 0;
1347 cdbd
->rw_10
.length
[1] = cdbs
->rw_6
.length
;
1348 cdbd
->rw_10
.control
= cdbs
->rw_6
.control
;
1357 xa
->complete
= ahci_atapi_complete_cmd
;
1358 xa
->atascsi_private
= ccb
;
1359 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
1360 ahci_os_lock_port(ap
);
1362 ahci_os_unlock_port(ap
);
1366 * Simulate page inquiries for disk attachments.
1370 ahci_xpt_page_inquiry(struct ahci_port
*ap
, struct ata_port
*at
, union ccb
*ccb
)
1373 struct scsi_vpd_supported_page_list list
;
1374 struct scsi_vpd_unit_serial_number serno
;
1375 struct scsi_vpd_unit_devid devid
;
1383 page
= kmalloc(sizeof(*page
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
1385 cdb
= (void *)((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) ?
1386 ccb
->csio
.cdb_io
.cdb_ptr
: ccb
->csio
.cdb_io
.cdb_bytes
);
1388 switch(cdb
->inquiry
.page_code
) {
1389 case SVPD_SUPPORTED_PAGE_LIST
:
1391 page
->list
.device
= T_DIRECT
;
1392 page
->list
.page_code
= SVPD_SUPPORTED_PAGE_LIST
;
1393 page
->list
.list
[i
++] = SVPD_SUPPORTED_PAGE_LIST
;
1394 page
->list
.list
[i
++] = SVPD_UNIT_SERIAL_NUMBER
;
1395 page
->list
.list
[i
++] = SVPD_UNIT_DEVID
;
1396 page
->list
.length
= i
;
1397 len
= offsetof(struct scsi_vpd_supported_page_list
, list
[3]);
1399 case SVPD_UNIT_SERIAL_NUMBER
:
1401 j
= sizeof(at
->at_identify
.serial
);
1402 for (i
= 0; i
< j
&& at
->at_identify
.serial
[i
] == ' '; ++i
)
1404 while (j
> i
&& at
->at_identify
.serial
[j
-1] == ' ')
1406 page
->serno
.device
= T_DIRECT
;
1407 page
->serno
.page_code
= SVPD_UNIT_SERIAL_NUMBER
;
1408 page
->serno
.length
= j
- i
;
1409 bcopy(at
->at_identify
.serial
+ i
,
1410 page
->serno
.serial_num
, j
- i
);
1411 len
= offsetof(struct scsi_vpd_unit_serial_number
,
1414 case SVPD_UNIT_DEVID
:
1415 /* fall through for now */
1417 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
1421 if (ccb
->ccb_h
.status
== CAM_REQ_INPROG
) {
1422 if (len
<= ccb
->csio
.dxfer_len
) {
1423 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
1424 bzero(ccb
->csio
.data_ptr
, ccb
->csio
.dxfer_len
);
1425 bcopy(page
, ccb
->csio
.data_ptr
, len
);
1426 ccb
->csio
.resid
= ccb
->csio
.dxfer_len
- len
;
1428 ccb
->ccb_h
.status
= CAM_CCB_LEN_ERR
;
1431 kfree(page
, M_DEVBUF
);
1435 * Completion function for ATA_PORT_T_DISK cache synchronization.
1439 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer
*xa
)
1441 union ccb
*ccb
= xa
->atascsi_private
;
1442 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1443 struct ahci_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1446 case ATA_S_COMPLETE
:
1447 ccbh
->status
= CAM_REQ_CMP
;
1448 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1451 kprintf("%s: synchronize_cache: error\n",
1452 ATANAME(ap
, xa
->at
));
1453 ccbh
->status
= CAM_SCSI_STATUS_ERROR
| CAM_AUTOSNS_VALID
;
1454 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1455 ahci_ata_dummy_sense(&ccb
->csio
.sense_data
);
1458 kprintf("%s: synchronize_cache: timeout\n",
1459 ATANAME(ap
, xa
->at
));
1460 ccbh
->status
= CAM_CMD_TIMEOUT
;
1463 kprintf("%s: synchronize_cache: unknown state %d\n",
1464 ATANAME(ap
, xa
->at
), xa
->state
);
1465 ccbh
->status
= CAM_REQ_CMP_ERR
;
1468 ahci_ata_put_xfer(xa
);
1469 ahci_os_unlock_port(ap
);
1471 ahci_os_lock_port(ap
);
1475 * Completion function for ATA_PORT_T_DISK I/O
1479 ahci_ata_complete_disk_rw(struct ata_xfer
*xa
)
1481 union ccb
*ccb
= xa
->atascsi_private
;
1482 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1483 struct ahci_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1486 case ATA_S_COMPLETE
:
1487 ccbh
->status
= CAM_REQ_CMP
;
1488 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1491 kprintf("%s: disk_rw: error\n", ATANAME(ap
, xa
->at
));
1492 ccbh
->status
= CAM_SCSI_STATUS_ERROR
| CAM_AUTOSNS_VALID
;
1493 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1494 ahci_ata_dummy_sense(&ccb
->csio
.sense_data
);
1497 kprintf("%s: disk_rw: timeout\n", ATANAME(ap
, xa
->at
));
1498 ccbh
->status
= CAM_CMD_TIMEOUT
;
1499 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1500 ahci_ata_dummy_sense(&ccb
->csio
.sense_data
);
1503 kprintf("%s: disk_rw: unknown state %d\n",
1504 ATANAME(ap
, xa
->at
), xa
->state
);
1505 ccbh
->status
= CAM_REQ_CMP_ERR
;
1508 ccb
->csio
.resid
= xa
->resid
;
1509 ahci_ata_put_xfer(xa
);
1510 ahci_os_unlock_port(ap
);
1512 ahci_os_lock_port(ap
);
1516 * Completion function for ATA_PORT_T_ATAPI I/O
1518 * Sense data is returned in the rfis.
1522 ahci_atapi_complete_cmd(struct ata_xfer
*xa
)
1524 union ccb
*ccb
= xa
->atascsi_private
;
1525 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1526 struct ahci_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1529 cdb
= (void *)((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) ?
1530 ccb
->csio
.cdb_io
.cdb_ptr
: ccb
->csio
.cdb_io
.cdb_bytes
);
1533 case ATA_S_COMPLETE
:
1534 ccbh
->status
= CAM_REQ_CMP
;
1535 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1538 ccbh
->status
= CAM_SCSI_STATUS_ERROR
;
1539 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1540 ahci_ata_atapi_sense(&xa
->rfis
, &ccb
->csio
.sense_data
);
1543 kprintf("%s: cmd %d: timeout\n",
1544 PORTNAME(ap
), cdb
->generic
.opcode
);
1545 ccbh
->status
= CAM_CMD_TIMEOUT
;
1546 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1547 ahci_ata_dummy_sense(&ccb
->csio
.sense_data
);
1550 kprintf("%s: cmd %d: unknown state %d\n",
1551 PORTNAME(ap
), cdb
->generic
.opcode
, xa
->state
);
1552 ccbh
->status
= CAM_REQ_CMP_ERR
;
1555 ccb
->csio
.resid
= xa
->resid
;
1556 ahci_ata_put_xfer(xa
);
1557 ahci_os_unlock_port(ap
);
1559 ahci_os_lock_port(ap
);
1563 * Construct dummy sense data for errors on DISKs
1567 ahci_ata_dummy_sense(struct scsi_sense_data
*sense_data
)
1569 sense_data
->error_code
= SSD_ERRCODE_VALID
| SSD_CURRENT_ERROR
;
1570 sense_data
->segment
= 0;
1571 sense_data
->flags
= SSD_KEY_MEDIUM_ERROR
;
1572 sense_data
->info
[0] = 0;
1573 sense_data
->info
[1] = 0;
1574 sense_data
->info
[2] = 0;
1575 sense_data
->info
[3] = 0;
1576 sense_data
->extra_len
= 0;
1580 * Construct atapi sense data for errors on ATAPI
1582 * The ATAPI sense data is stored in the passed rfis and must be converted
1583 * to SCSI sense data.
1587 ahci_ata_atapi_sense(struct ata_fis_d2h
*rfis
,
1588 struct scsi_sense_data
*sense_data
)
1590 sense_data
->error_code
= SSD_ERRCODE_VALID
| SSD_CURRENT_ERROR
;
1591 sense_data
->segment
= 0;
1592 sense_data
->flags
= (rfis
->error
& 0xF0) >> 4;
1593 if (rfis
->error
& 0x04)
1594 sense_data
->flags
|= SSD_KEY_ILLEGAL_REQUEST
;
1595 if (rfis
->error
& 0x02)
1596 sense_data
->flags
|= SSD_EOM
;
1597 if (rfis
->error
& 0x01)
1598 sense_data
->flags
|= SSD_ILI
;
1599 sense_data
->info
[0] = 0;
1600 sense_data
->info
[1] = 0;
1601 sense_data
->info
[2] = 0;
1602 sense_data
->info
[3] = 0;
1603 sense_data
->extra_len
= 0;
1608 ahci_strip_string(const char **basep
, int *lenp
)
1610 const char *base
= *basep
;
1613 while (len
&& (*base
== 0 || *base
== ' ')) {
1617 while (len
&& (base
[len
-1] == 0 || base
[len
-1] == ' '))