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 static void sili_xpt_action(struct cam_sim
*sim
, union ccb
*ccb
);
64 static void sili_xpt_poll(struct cam_sim
*sim
);
65 static void sili_xpt_scsi_disk_io(struct sili_port
*ap
,
66 struct ata_port
*at
, union ccb
*ccb
);
67 static void sili_xpt_scsi_atapi_io(struct sili_port
*ap
,
68 struct ata_port
*at
, union ccb
*ccb
);
69 static void sili_xpt_page_inquiry(struct sili_port
*ap
,
70 struct ata_port
*at
, union ccb
*ccb
);
72 static void sili_ata_complete_disk_rw(struct ata_xfer
*xa
);
73 static void sili_ata_complete_disk_synchronize_cache(struct ata_xfer
*xa
);
74 static void sili_atapi_complete_cmd(struct ata_xfer
*xa
);
75 static void sili_ata_dummy_sense(struct scsi_sense_data
*sense_data
);
76 static void sili_ata_atapi_sense(struct ata_fis_d2h
*rfis
,
77 struct scsi_sense_data
*sense_data
);
79 static int sili_cam_probe_disk(struct sili_port
*ap
, struct ata_port
*at
);
80 static int sili_cam_probe_atapi(struct sili_port
*ap
, struct ata_port
*at
);
81 static void sili_ata_dummy_done(struct ata_xfer
*xa
);
82 static void ata_fix_identify(struct ata_identify
*id
);
83 static void sili_cam_rescan(struct sili_port
*ap
);
84 static void sili_strip_string(const char **basep
, int *lenp
);
87 sili_cam_attach(struct sili_port
*ap
)
89 struct cam_devq
*devq
;
95 * We want at least one ccb to be available for error processing
96 * so don't let CAM use more then ncmds - 1.
98 unit
= device_get_unit(ap
->ap_sc
->sc_dev
);
99 if (ap
->ap_sc
->sc_ncmds
> 1)
100 devq
= cam_simq_alloc(ap
->ap_sc
->sc_ncmds
- 1);
102 devq
= cam_simq_alloc(ap
->ap_sc
->sc_ncmds
);
106 sim
= cam_sim_alloc(sili_xpt_action
, sili_xpt_poll
, "sili",
107 (void *)ap
, unit
, &sim_mplock
, 1, 1, devq
);
108 cam_simq_release(devq
);
113 sili_os_unlock_port(ap
);
114 error
= xpt_bus_register(ap
->ap_sim
, ap
->ap_num
);
115 sili_os_lock_port(ap
);
116 if (error
!= CAM_SUCCESS
) {
120 ap
->ap_flags
|= AP_F_BUS_REGISTERED
;
122 if (ap
->ap_probe
== ATA_PROBE_NEED_IDENT
)
123 error
= sili_cam_probe(ap
, NULL
);
130 ap
->ap_flags
|= AP_F_CAM_ATTACHED
;
136 * The state of the port has changed.
138 * If at is NULL the physical port has changed state.
139 * If at is non-NULL a particular target behind a PM has changed state.
141 * If found is -1 the target state must be queued to a non-interrupt context.
142 * (only works with at == NULL).
144 * If found is 0 the target was removed.
145 * If found is 1 the target was inserted.
148 sili_cam_changed(struct sili_port
*ap
, struct ata_port
*atx
, int found
)
150 struct cam_path
*tmppath
;
154 target
= atx
? atx
->at_target
: CAM_TARGET_WILDCARD
;
156 if (ap
->ap_sim
== NULL
)
158 if (found
== CAM_TARGET_WILDCARD
) {
159 status
= xpt_create_path(&tmppath
, NULL
,
160 cam_sim_path(ap
->ap_sim
),
161 target
, CAM_LUN_WILDCARD
);
162 if (status
!= CAM_REQ_CMP
)
166 status
= xpt_create_path(&tmppath
, NULL
,
167 cam_sim_path(ap
->ap_sim
),
170 if (status
!= CAM_REQ_CMP
)
177 xpt_async(AC_FOUND_DEVICE
, tmppath
, NULL
);
179 xpt_async(AC_LOST_DEVICE
, tmppath
, NULL
);
182 xpt_free_path(tmppath
);
186 sili_cam_detach(struct sili_port
*ap
)
190 if ((ap
->ap_flags
& AP_F_CAM_ATTACHED
) == 0)
194 xpt_freeze_simq(ap
->ap_sim
, 1);
196 if (ap
->ap_flags
& AP_F_BUS_REGISTERED
) {
197 error
= xpt_bus_deregister(cam_sim_path(ap
->ap_sim
));
198 KKASSERT(error
== CAM_REQ_CMP
);
199 ap
->ap_flags
&= ~AP_F_BUS_REGISTERED
;
202 cam_sim_free(ap
->ap_sim
);
206 ap
->ap_flags
&= ~AP_F_CAM_ATTACHED
;
210 * Once the SILI port has been attached we need to probe for a device or
211 * devices on the port and setup various options.
213 * If at is NULL we are probing the direct-attached device on the port,
214 * which may or may not be a port multiplier.
217 sili_cam_probe(struct sili_port
*ap
, struct ata_port
*atx
)
222 u_int64_t capacity_bytes
;
229 const char *model_id
;
230 const char *firmware_id
;
231 const char *serial_id
;
240 * Delayed CAM attachment for initial probe, sim may be NULL
242 if (ap
->ap_sim
== NULL
)
246 * A NULL atx indicates a probe of the directly connected device.
247 * A non-NULL atx indicates a device connected via a port multiplier.
248 * We need to preserve atx for calls to sili_ata_get_xfer().
250 * at is always non-NULL. For directly connected devices we supply
251 * an (at) pointing to target 0.
254 at
= ap
->ap_ata
; /* direct attached - device 0 */
255 if (ap
->ap_type
== ATA_PORT_T_PM
) {
256 kprintf("%s: Found Port Multiplier\n",
260 at
->at_type
= ap
->ap_type
;
263 if (atx
->at_type
== ATA_PORT_T_PM
) {
264 kprintf("%s: Bogus device, reducing port count to %d\n",
265 ATANAME(ap
, atx
), atx
->at_target
);
266 if (ap
->ap_pmcount
> atx
->at_target
)
267 ap
->ap_pmcount
= atx
->at_target
;
271 if (ap
->ap_type
== ATA_PORT_T_NONE
)
273 if (at
->at_type
== ATA_PORT_T_NONE
)
277 * Issue identify, saving the result
279 xa
= sili_ata_get_xfer(ap
, atx
);
280 xa
->complete
= sili_ata_dummy_done
;
281 xa
->data
= &at
->at_identify
;
282 xa
->datalen
= sizeof(at
->at_identify
);
283 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
284 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
286 switch(at
->at_type
) {
287 case ATA_PORT_T_DISK
:
288 xa
->fis
->command
= ATA_C_IDENTIFY
;
291 case ATA_PORT_T_ATAPI
:
292 xa
->fis
->command
= ATA_C_ATAPI_IDENTIFY
;
293 xa
->flags
|= ATA_F_AUTOSENSE
;
297 xa
->fis
->command
= ATA_C_ATAPI_IDENTIFY
;
298 type
= "UNKNOWN(ATAPI?)";
301 xa
->fis
->features
= 0;
305 if (sili_ata_cmd(xa
) != ATA_S_COMPLETE
) {
306 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
307 ATANAME(ap
, atx
), type
);
308 sili_ata_put_xfer(xa
);
311 sili_ata_put_xfer(xa
);
313 ata_fix_identify(&at
->at_identify
);
316 * Read capacity using SATA probe info.
318 if (le16toh(at
->at_identify
.cmdset83
) & 0x0400) {
319 /* LBA48 feature set supported */
321 for (i
= 3; i
>= 0; --i
) {
324 le16toh(at
->at_identify
.addrsecxt
[i
]);
327 capacity
= le16toh(at
->at_identify
.addrsec
[1]);
329 capacity
+= le16toh(at
->at_identify
.addrsec
[0]);
331 at
->at_capacity
= capacity
;
333 ap
->ap_probe
= ATA_PROBE_GOOD
;
335 capacity_bytes
= capacity
* 512;
338 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
339 * number of slots and limit the number of CAM ccb's to one less
340 * so we always have a slot available for recovery.
342 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
343 * not support it, and in that case the driver can handle extra
346 * NCQ is currently used only with direct-attached disks. It is
347 * not used with port multipliers or direct-attached ATAPI devices.
349 * Remember at least one extra CCB needs to be reserved for the
352 if ((ap
->ap_sc
->sc_flags
& SILI_F_NCQ
) &&
353 at
->at_type
== ATA_PORT_T_DISK
&&
354 (le16toh(at
->at_identify
.satacap
) & (1 << 8))) {
355 at
->at_ncqdepth
= (le16toh(at
->at_identify
.qdepth
) & 0x1F) + 1;
356 devncqdepth
= at
->at_ncqdepth
;
357 if (at
->at_ncqdepth
> ap
->ap_sc
->sc_ncmds
)
358 at
->at_ncqdepth
= ap
->ap_sc
->sc_ncmds
;
359 if (at
->at_ncqdepth
> 1) {
360 for (i
= 0; i
< ap
->ap_sc
->sc_ncmds
; ++i
) {
361 xa
= sili_ata_get_xfer(ap
, atx
);
362 if (xa
->tag
< at
->at_ncqdepth
) {
363 xa
->state
= ATA_S_COMPLETE
;
364 sili_ata_put_xfer(xa
);
367 if (at
->at_ncqdepth
>= ap
->ap_sc
->sc_ncmds
) {
368 cam_devq_resize(ap
->ap_sim
->devq
,
369 at
->at_ncqdepth
- 1);
377 * Make the model string a bit more presentable
379 for (model_len
= 40; model_len
; --model_len
) {
380 if (at
->at_identify
.model
[model_len
-1] == ' ')
382 if (at
->at_identify
.model
[model_len
-1] == 0)
387 model_len
= sizeof(at
->at_identify
.model
);
388 model_id
= at
->at_identify
.model
;
389 sili_strip_string(&model_id
, &model_len
);
391 firmware_len
= sizeof(at
->at_identify
.firmware
);
392 firmware_id
= at
->at_identify
.firmware
;
393 sili_strip_string(&firmware_id
, &firmware_len
);
395 serial_len
= sizeof(at
->at_identify
.serial
);
396 serial_id
= at
->at_identify
.serial
;
397 sili_strip_string(&serial_id
, &serial_len
);
400 * Generate informatiive strings.
402 * NOTE: We do not automatically set write caching, lookahead,
403 * or the security state for ATAPI devices.
405 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_WRITECACHE
) {
406 if (at
->at_identify
.features85
& ATA_IDENTIFY_WRITECACHE
)
408 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
416 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_LOOKAHEAD
) {
417 if (at
->at_identify
.features85
& ATA_IDENTIFY_LOOKAHEAD
)
419 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
427 if (at
->at_identify
.cmdset82
& ATA_IDENTIFY_SECURITY
) {
428 if (at
->at_identify
.securestatus
& ATA_SECURE_FROZEN
)
430 else if (at
->at_type
== ATA_PORT_T_ATAPI
)
432 else if (SiliNoFeatures
& (1 << ap
->ap_num
))
433 scstr
= "<disabled>";
440 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
441 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
442 "capacity=%lld.%02dMB\n",
446 model_len
, model_len
, model_id
,
447 firmware_len
, firmware_len
, firmware_id
,
448 serial_len
, serial_len
, serial_id
,
451 devncqdepth
, ap
->ap_sc
->sc_ncmds
,
452 at
->at_identify
.satacap
,
453 at
->at_identify
.satafsup
,
454 (at
->at_ncqdepth
> 1 ? "YES" : "NO"),
455 (long long)capacity_bytes
/ (1024 * 1024),
456 (int)(capacity_bytes
% (1024 * 1024)) * 100 / (1024 * 1024)
458 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
460 at
->at_identify
.features85
,
461 at
->at_identify
.features86
,
462 at
->at_identify
.features87
,
469 * Additional type-specific probing
471 switch(at
->at_type
) {
472 case ATA_PORT_T_DISK
:
473 error
= sili_cam_probe_disk(ap
, atx
);
475 case ATA_PORT_T_ATAPI
:
476 error
= sili_cam_probe_atapi(ap
, atx
);
484 at
->at_probe
= ATA_PROBE_FAILED
;
486 ap
->ap_probe
= at
->at_probe
;
488 at
->at_probe
= ATA_PROBE_GOOD
;
490 ap
->ap_probe
= at
->at_probe
;
496 * DISK-specific probe after initial ident
499 sili_cam_probe_disk(struct sili_port
*ap
, struct ata_port
*atx
)
504 at
= atx
? atx
: ap
->ap_ata
;
507 * Enable write cache if supported
509 * NOTE: "WD My Book" external disk devices have a very poor
510 * daughter board between the the ESATA and the HD. Sending
511 * any ATA_C_SET_FEATURES commands will break the hardware port
512 * with a fatal protocol error. However, this device also
513 * indicates that WRITECACHE is already on and READAHEAD is
514 * not supported so we avoid the issue.
516 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_WRITECACHE
) &&
517 (at
->at_identify
.features85
& ATA_IDENTIFY_WRITECACHE
) == 0) {
518 xa
= sili_ata_get_xfer(ap
, atx
);
519 xa
->complete
= sili_ata_dummy_done
;
520 xa
->fis
->command
= ATA_C_SET_FEATURES
;
521 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
522 xa
->fis
->features
= ATA_SF_LOOKAHEAD_EN
;
523 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
525 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
528 if (sili_ata_cmd(xa
) == ATA_S_COMPLETE
)
529 at
->at_features
|= ATA_PORT_F_WCACHE
;
531 kprintf("%s: Unable to enable write-caching\n",
533 sili_ata_put_xfer(xa
);
537 * Enable readahead if supported
539 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_LOOKAHEAD
) &&
540 (at
->at_identify
.features85
& ATA_IDENTIFY_LOOKAHEAD
) == 0) {
541 xa
= sili_ata_get_xfer(ap
, atx
);
542 xa
->complete
= sili_ata_dummy_done
;
543 xa
->fis
->command
= ATA_C_SET_FEATURES
;
544 xa
->fis
->features
= ATA_SF_LOOKAHEAD_EN
;
545 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
547 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
550 if (sili_ata_cmd(xa
) == ATA_S_COMPLETE
)
551 at
->at_features
|= ATA_PORT_F_RAHEAD
;
553 kprintf("%s: Unable to enable read-ahead\n",
555 sili_ata_put_xfer(xa
);
559 * FREEZE LOCK the device so malicious users can't lock it on us.
560 * As there is no harm in issuing this to devices that don't
561 * support the security feature set we just send it, and don't bother
562 * checking if the device sends a command abort to tell us it doesn't
565 if ((at
->at_identify
.cmdset82
& ATA_IDENTIFY_SECURITY
) &&
566 (at
->at_identify
.securestatus
& ATA_SECURE_FROZEN
) == 0 &&
567 (SiliNoFeatures
& (1 << ap
->ap_num
)) == 0) {
568 xa
= sili_ata_get_xfer(ap
, atx
);
569 xa
->complete
= sili_ata_dummy_done
;
570 xa
->fis
->command
= ATA_C_SEC_FREEZE_LOCK
;
571 xa
->fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
572 xa
->flags
= ATA_F_READ
| ATA_F_PIO
| ATA_F_POLL
;
575 if (sili_ata_cmd(xa
) == ATA_S_COMPLETE
)
576 at
->at_features
|= ATA_PORT_F_FRZLCK
;
578 kprintf("%s: Unable to set security freeze\n",
580 sili_ata_put_xfer(xa
);
587 * ATAPI-specific probe after initial ident
590 sili_cam_probe_atapi(struct sili_port
*ap
, struct ata_port
*atx
)
596 * Fix byte ordering so buffers can be accessed as
600 ata_fix_identify(struct ata_identify
*id
)
605 swap
= (u_int16_t
*)id
->serial
;
606 for (i
= 0; i
< sizeof(id
->serial
) / sizeof(u_int16_t
); i
++)
607 swap
[i
] = bswap16(swap
[i
]);
609 swap
= (u_int16_t
*)id
->firmware
;
610 for (i
= 0; i
< sizeof(id
->firmware
) / sizeof(u_int16_t
); i
++)
611 swap
[i
] = bswap16(swap
[i
]);
613 swap
= (u_int16_t
*)id
->model
;
614 for (i
= 0; i
< sizeof(id
->model
) / sizeof(u_int16_t
); i
++)
615 swap
[i
] = bswap16(swap
[i
]);
619 * Dummy done callback for xa.
622 sili_ata_dummy_done(struct ata_xfer
*xa
)
627 * Use an engineering request to initiate a target scan for devices
628 * behind a port multiplier.
630 * An asynchronous bus scan is used to avoid reentrancy issues.
633 sili_cam_rescan_callback(struct cam_periph
*periph
, union ccb
*ccb
)
635 struct sili_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
637 if (ccb
->ccb_h
.func_code
== XPT_SCAN_BUS
) {
638 ap
->ap_flags
&= ~AP_F_SCAN_RUNNING
;
639 if (ap
->ap_flags
& AP_F_SCAN_REQUESTED
) {
640 ap
->ap_flags
&= ~AP_F_SCAN_REQUESTED
;
643 ap
->ap_flags
|= AP_F_SCAN_COMPLETED
;
644 wakeup(&ap
->ap_flags
);
650 sili_cam_rescan(struct sili_port
*ap
)
652 struct cam_path
*path
;
657 if (ap
->ap_flags
& AP_F_SCAN_RUNNING
) {
658 ap
->ap_flags
|= AP_F_SCAN_REQUESTED
;
661 ap
->ap_flags
|= AP_F_SCAN_RUNNING
;
662 for (i
= 0; i
< SILI_MAX_PMPORTS
; ++i
) {
663 ap
->ap_ata
[i
].at_features
|= ATA_PORT_F_RESCAN
;
666 status
= xpt_create_path(&path
, xpt_periph
, cam_sim_path(ap
->ap_sim
),
667 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
668 if (status
!= CAM_REQ_CMP
)
671 ccb
= xpt_alloc_ccb();
672 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5); /* 5 = low priority */
673 ccb
->ccb_h
.func_code
= XPT_ENG_EXEC
;
674 ccb
->ccb_h
.cbfcnp
= sili_cam_rescan_callback
;
675 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
676 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
677 xpt_action_async(ccb
);
681 sili_xpt_rescan(struct sili_port
*ap
)
683 struct cam_path
*path
;
687 status
= xpt_create_path(&path
, xpt_periph
, cam_sim_path(ap
->ap_sim
),
688 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
689 if (status
!= CAM_REQ_CMP
)
692 ccb
= xpt_alloc_ccb();
693 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5); /* 5 = low priority */
694 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
695 ccb
->ccb_h
.cbfcnp
= sili_cam_rescan_callback
;
696 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
697 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
698 xpt_action_async(ccb
);
702 * Action function - dispatch command
706 sili_xpt_action(struct cam_sim
*sim
, union ccb
*ccb
)
708 struct sili_port
*ap
;
709 struct ata_port
*at
, *atx
;
710 struct ccb_hdr
*ccbh
;
714 ap
= cam_sim_softc(sim
);
717 KKASSERT(ap
!= NULL
);
719 unit
= cam_sim_unit(sim
);
722 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
723 * otherwise the bus rescan will not remove the dead devices when
726 * For non-wildcards we have one target (0) and one lun (0),
727 * unless we have a port multiplier.
729 * A wildcard target indicates only the general bus is being
732 * Calculate at and atx. at is always non-NULL. atx is only
733 * non-NULL for direct-attached devices. It will be NULL for
734 * devices behind a port multiplier.
736 * XXX What do we do with a LUN wildcard?
738 if (ccbh
->target_id
!= CAM_TARGET_WILDCARD
&&
739 ccbh
->func_code
!= XPT_PATH_INQ
) {
740 if (ap
->ap_type
== ATA_PORT_T_NONE
) {
741 ccbh
->status
= CAM_DEV_NOT_THERE
;
745 if (ccbh
->target_id
< 0 || ccbh
->target_id
>= ap
->ap_pmcount
) {
746 ccbh
->status
= CAM_DEV_NOT_THERE
;
750 at
+= ccbh
->target_id
;
751 if (ap
->ap_type
== ATA_PORT_T_PM
)
754 if (ccbh
->target_lun
!= CAM_LUN_WILDCARD
&& ccbh
->target_lun
) {
755 ccbh
->status
= CAM_DEV_NOT_THERE
;
762 * Switch on the meta XPT command
764 switch(ccbh
->func_code
) {
767 * This routine is called after a port multiplier has been
770 ccbh
->status
= CAM_REQ_CMP
;
771 sili_os_lock_port(ap
);
772 sili_port_state_machine(ap
, 0);
773 sili_os_unlock_port(ap
);
779 * This command always succeeds, otherwise the bus scan
780 * will not detach dead devices.
782 ccb
->cpi
.version_num
= 1;
783 ccb
->cpi
.hba_inquiry
= 0;
784 ccb
->cpi
.target_sprt
= 0;
785 ccb
->cpi
.hba_misc
= PIM_SEQSCAN
;
786 ccb
->cpi
.hba_eng_cnt
= 0;
787 bzero(ccb
->cpi
.vuhba_flags
, sizeof(ccb
->cpi
.vuhba_flags
));
788 ccb
->cpi
.max_target
= SILI_MAX_PMPORTS
- 1;
789 ccb
->cpi
.max_lun
= 0;
790 ccb
->cpi
.async_flags
= 0;
791 ccb
->cpi
.hpath_id
= 0;
792 ccb
->cpi
.initiator_id
= SILI_MAX_PMPORTS
- 1;
793 ccb
->cpi
.unit_number
= cam_sim_unit(sim
);
794 ccb
->cpi
.bus_id
= cam_sim_bus(sim
);
795 ccb
->cpi
.base_transfer_speed
= 150000;
796 ccb
->cpi
.transport
= XPORT_SATA
;
797 ccb
->cpi
.transport_version
= 1;
798 ccb
->cpi
.protocol
= PROTO_SCSI
;
799 ccb
->cpi
.protocol_version
= SCSI_REV_2
;
801 ccbh
->status
= CAM_REQ_CMP
;
802 if (ccbh
->target_id
== CAM_TARGET_WILDCARD
) {
803 sili_os_lock_port(ap
);
804 sili_port_state_machine(ap
, 0);
805 sili_os_unlock_port(ap
);
807 switch(sili_pread(ap
, SILI_PREG_SSTS
) &
808 SILI_PREG_SSTS_SPD
) {
809 case SILI_PREG_SSTS_SPD_GEN1
:
810 ccb
->cpi
.base_transfer_speed
= 150000;
812 case SILI_PREG_SSTS_SPD_GEN2
:
813 ccb
->cpi
.base_transfer_speed
= 300000;
817 ccb
->cpi
.base_transfer_speed
= 1000;
821 if (ap
->ap_type
== ATA_PORT_T_NONE
)
822 ccbh
->status
= CAM_DEV_NOT_THERE
;
828 sili_os_lock_port(ap
);
829 if (ap
->ap_type
== ATA_PORT_T_NONE
) {
830 ccbh
->status
= CAM_DEV_NOT_THERE
;
832 sili_port_reset(ap
, atx
, 0);
833 ccbh
->status
= CAM_REQ_CMP
;
835 sili_os_unlock_port(ap
);
839 sili_os_lock_port(ap
);
840 sili_port_reset(ap
, NULL
, 1);
841 sili_os_unlock_port(ap
);
842 ccbh
->status
= CAM_REQ_CMP
;
845 case XPT_SET_TRAN_SETTINGS
:
846 ccbh
->status
= CAM_FUNC_NOTAVAIL
;
849 case XPT_GET_TRAN_SETTINGS
:
850 ccb
->cts
.protocol
= PROTO_SCSI
;
851 ccb
->cts
.protocol_version
= SCSI_REV_2
;
852 ccb
->cts
.transport
= XPORT_SATA
;
853 ccb
->cts
.transport_version
= XPORT_VERSION_UNSPECIFIED
;
854 ccb
->cts
.proto_specific
.valid
= 0;
855 ccb
->cts
.xport_specific
.valid
= 0;
856 ccbh
->status
= CAM_REQ_CMP
;
859 case XPT_CALC_GEOMETRY
:
860 cam_calc_geometry(&ccb
->ccg
, 1);
865 * Our parallel startup code might have only probed through
866 * to the IDENT, so do the last step if necessary.
868 if (at
->at_probe
== ATA_PROBE_NEED_IDENT
)
869 sili_cam_probe(ap
, atx
);
870 if (at
->at_probe
!= ATA_PROBE_GOOD
) {
871 ccbh
->status
= CAM_DEV_NOT_THERE
;
875 switch(at
->at_type
) {
876 case ATA_PORT_T_DISK
:
877 sili_xpt_scsi_disk_io(ap
, atx
, ccb
);
879 case ATA_PORT_T_ATAPI
:
880 sili_xpt_scsi_atapi_io(ap
, atx
, ccb
);
883 ccbh
->status
= CAM_REQ_INVALID
;
889 ccbh
->status
= CAM_REQ_INVALID
;
898 * Generally this function gets called heavily when interrupts might be
899 * non-operational, during a halt/reboot or panic.
903 sili_xpt_poll(struct cam_sim
*sim
)
905 struct sili_port
*ap
;
907 ap
= cam_sim_softc(sim
);
909 sili_os_lock_port(ap
);
910 sili_port_intr(ap
, 1);
911 sili_os_unlock_port(ap
);
916 * Convert the SCSI command in ccb to an ata_xfer command in xa
917 * for ATA_PORT_T_DISK operations. Set the completion function
918 * to convert the response back, then dispatch to the OpenBSD SILI
921 * SILI DISK commands only support a limited command set, and we
922 * fake additional commands to make it play nice with the CAM subsystem.
926 sili_xpt_scsi_disk_io(struct sili_port
*ap
, struct ata_port
*atx
,
929 struct ccb_hdr
*ccbh
;
930 struct ccb_scsiio
*csio
;
933 struct ata_fis_h2d
*fis
;
934 struct ata_pass_12
*atp12
;
935 struct ata_pass_16
*atp16
;
937 union scsi_data
*rdata
;
943 ccbh
= &ccb
->csio
.ccb_h
;
945 at
= atx
? atx
: &ap
->ap_ata
[0];
948 * XXX not passing NULL at for direct attach!
950 xa
= sili_ata_get_xfer(ap
, atx
);
951 rdata
= (void *)csio
->data_ptr
;
952 rdata_len
= csio
->dxfer_len
;
955 * Build the FIS or process the csio to completion.
957 cdb
= (void *)((ccbh
->flags
& CAM_CDB_POINTER
) ?
958 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
);
960 switch(cdb
->generic
.opcode
) {
963 * Auto-sense everything, so explicit sense requests
966 ccbh
->status
= CAM_SCSI_STATUS_ERROR
;
970 * Inquiry supported features
972 * [opcode, byte2, page_code, length, control]
974 if (cdb
->inquiry
.byte2
& SI_EVPD
) {
975 sili_xpt_page_inquiry(ap
, at
, ccb
);
977 bzero(rdata
, rdata_len
);
978 if (rdata_len
< SHORT_INQUIRY_LENGTH
) {
979 ccbh
->status
= CAM_CCB_LEN_ERR
;
982 if (rdata_len
> sizeof(rdata
->inquiry_data
))
983 rdata_len
= sizeof(rdata
->inquiry_data
);
984 rdata
->inquiry_data
.device
= T_DIRECT
;
985 rdata
->inquiry_data
.version
= SCSI_REV_SPC2
;
986 rdata
->inquiry_data
.response_format
= 2;
987 rdata
->inquiry_data
.additional_length
= 32;
988 bcopy("SATA ", rdata
->inquiry_data
.vendor
, 8);
989 bcopy(at
->at_identify
.model
,
990 rdata
->inquiry_data
.product
,
991 sizeof(rdata
->inquiry_data
.product
));
992 bcopy(at
->at_identify
.firmware
,
993 rdata
->inquiry_data
.revision
,
994 sizeof(rdata
->inquiry_data
.revision
));
995 ccbh
->status
= CAM_REQ_CMP
;
998 case READ_CAPACITY_16
:
999 if (cdb
->read_capacity_16
.service_action
!= SRC16_SERVICE_ACTION
) {
1000 ccbh
->status
= CAM_REQ_INVALID
;
1003 if (rdata_len
< sizeof(rdata
->read_capacity_data_16
)) {
1004 ccbh
->status
= CAM_CCB_LEN_ERR
;
1009 if (rdata_len
< sizeof(rdata
->read_capacity_data
)) {
1010 ccbh
->status
= CAM_CCB_LEN_ERR
;
1014 capacity
= at
->at_capacity
;
1016 bzero(rdata
, rdata_len
);
1017 if (cdb
->generic
.opcode
== READ_CAPACITY
) {
1018 rdata_len
= sizeof(rdata
->read_capacity_data
);
1019 if (capacity
> 0xFFFFFFFFU
)
1020 capacity
= 0xFFFFFFFFU
;
1021 bzero(&rdata
->read_capacity_data
, rdata_len
);
1022 scsi_ulto4b((u_int32_t
)capacity
- 1,
1023 rdata
->read_capacity_data
.addr
);
1024 scsi_ulto4b(512, rdata
->read_capacity_data
.length
);
1026 rdata_len
= sizeof(rdata
->read_capacity_data_16
);
1027 bzero(&rdata
->read_capacity_data_16
, rdata_len
);
1028 scsi_u64to8b(capacity
- 1,
1029 rdata
->read_capacity_data_16
.addr
);
1030 scsi_ulto4b(512, rdata
->read_capacity_data_16
.length
);
1032 ccbh
->status
= CAM_REQ_CMP
;
1034 case SYNCHRONIZE_CACHE
:
1036 * Synchronize cache. Specification says this can take
1037 * greater then 30 seconds so give it at least 45.
1040 fis
->flags
= ATA_H2D_FLAGS_CMD
;
1041 fis
->command
= ATA_C_FLUSH_CACHE
;
1043 if (xa
->timeout
< 45000)
1044 xa
->timeout
= 45000;
1046 xa
->flags
= ATA_F_READ
;
1047 xa
->complete
= sili_ata_complete_disk_synchronize_cache
;
1049 case TEST_UNIT_READY
:
1050 case START_STOP_UNIT
:
1053 * Just silently return success
1055 ccbh
->status
= CAM_REQ_CMP
;
1059 atp12
= &cdb
->ata_pass_12
;
1062 * Figure out the flags to be used, depending on the
1063 * direction of the CAM request.
1065 switch (ccbh
->flags
& CAM_DIR_MASK
) {
1067 xa
->flags
= ATA_F_READ
;
1070 xa
->flags
= ATA_F_WRITE
;
1075 xa
->flags
|= ATA_F_POLL
;
1076 xa
->data
= csio
->data_ptr
;
1077 xa
->datalen
= csio
->dxfer_len
;
1078 xa
->complete
= sili_ata_complete_disk_rw
;
1079 xa
->timeout
= ccbh
->timeout
;
1082 * Populate the fis from the information we received through CAM
1085 fis
->flags
= ATA_H2D_FLAGS_CMD
; /* maybe also atp12->flags ? */
1086 fis
->features
= atp12
->features
;
1087 fis
->sector_count
= atp12
->sector_count
;
1088 fis
->lba_low
= atp12
->lba_low
;
1089 fis
->lba_mid
= atp12
->lba_mid
;
1090 fis
->lba_high
= atp12
->lba_high
;
1091 fis
->device
= atp12
->device
; /* maybe always 0? */
1092 fis
->command
= atp12
->command
;
1093 fis
->control
= atp12
->control
;
1096 * Mark as in progress so it is sent to the device.
1098 ccbh
->status
= CAM_REQ_INPROG
;
1101 atp16
= &cdb
->ata_pass_16
;
1104 * Figure out the flags to be used, depending on the direction of the
1107 switch (ccbh
->flags
& CAM_DIR_MASK
) {
1109 xa
->flags
= ATA_F_READ
;
1112 xa
->flags
= ATA_F_WRITE
;
1117 xa
->flags
|= ATA_F_POLL
;
1118 xa
->data
= csio
->data_ptr
;
1119 xa
->datalen
= csio
->dxfer_len
;
1120 xa
->complete
= sili_ata_complete_disk_rw
;
1121 xa
->timeout
= ccbh
->timeout
;
1124 * Populate the fis from the information we received through CAM
1127 fis
->flags
= ATA_H2D_FLAGS_CMD
; /* maybe also atp16->flags ? */
1128 fis
->features
= atp16
->features
;
1129 fis
->features_exp
= atp16
->features_ext
;
1130 fis
->sector_count
= atp16
->sector_count
;
1131 fis
->sector_count_exp
= atp16
->sector_count_ext
;
1132 fis
->lba_low
= atp16
->lba_low
;
1133 fis
->lba_low_exp
= atp16
->lba_low_ext
;
1134 fis
->lba_mid
= atp16
->lba_mid
;
1135 fis
->lba_mid_exp
= atp16
->lba_mid_ext
;
1136 fis
->lba_high
= atp16
->lba_high
;
1137 fis
->lba_mid_exp
= atp16
->lba_mid_ext
;
1138 fis
->device
= atp16
->device
; /* maybe always 0? */
1139 fis
->command
= atp16
->command
;
1142 * Mark as in progress so it is sent to the device.
1144 ccbh
->status
= CAM_REQ_INPROG
;
1147 switch(cdb
->generic
.opcode
) {
1149 lba
= scsi_3btoul(cdb
->rw_6
.addr
) & 0x1FFFFF;
1150 count
= cdb
->rw_6
.length
? cdb
->rw_6
.length
: 0x100;
1151 xa
->flags
= ATA_F_READ
;
1154 lba
= scsi_4btoul(cdb
->rw_10
.addr
);
1155 count
= scsi_2btoul(cdb
->rw_10
.length
);
1156 xa
->flags
= ATA_F_READ
;
1159 lba
= scsi_4btoul(cdb
->rw_12
.addr
);
1160 count
= scsi_4btoul(cdb
->rw_12
.length
);
1161 xa
->flags
= ATA_F_READ
;
1164 lba
= scsi_8btou64(cdb
->rw_16
.addr
);
1165 count
= scsi_4btoul(cdb
->rw_16
.length
);
1166 xa
->flags
= ATA_F_READ
;
1169 lba
= scsi_3btoul(cdb
->rw_6
.addr
) & 0x1FFFFF;
1170 count
= cdb
->rw_6
.length
? cdb
->rw_6
.length
: 0x100;
1171 xa
->flags
= ATA_F_WRITE
;
1174 lba
= scsi_4btoul(cdb
->rw_10
.addr
);
1175 count
= scsi_2btoul(cdb
->rw_10
.length
);
1176 xa
->flags
= ATA_F_WRITE
;
1179 lba
= scsi_4btoul(cdb
->rw_12
.addr
);
1180 count
= scsi_4btoul(cdb
->rw_12
.length
);
1181 xa
->flags
= ATA_F_WRITE
;
1184 lba
= scsi_8btou64(cdb
->rw_16
.addr
);
1185 count
= scsi_4btoul(cdb
->rw_16
.length
);
1186 xa
->flags
= ATA_F_WRITE
;
1189 ccbh
->status
= CAM_REQ_INVALID
;
1192 if (ccbh
->status
!= CAM_REQ_INPROG
)
1196 fis
->flags
= ATA_H2D_FLAGS_CMD
;
1197 fis
->lba_low
= (u_int8_t
)lba
;
1198 fis
->lba_mid
= (u_int8_t
)(lba
>> 8);
1199 fis
->lba_high
= (u_int8_t
)(lba
>> 16);
1200 fis
->device
= ATA_H2D_DEVICE_LBA
;
1203 * NCQ only for direct-attached disks, do not currently
1204 * try to use NCQ with port multipliers.
1206 * XXX fixme SII chip can do NCQ w/ port multipliers.
1208 if (at
->at_ncqdepth
> 1 &&
1209 at
->at_type
== ATA_PORT_T_DISK
&&
1210 (ap
->ap_sc
->sc_flags
& SILI_F_NCQ
) &&
1211 (ccbh
->flags
& CAM_POLLED
) == 0) {
1213 * Use NCQ - always uses 48 bit addressing
1215 xa
->flags
|= ATA_F_NCQ
;
1216 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1217 ATA_C_WRITE_FPDMA
: ATA_C_READ_FPDMA
;
1218 fis
->lba_low_exp
= (u_int8_t
)(lba
>> 24);
1219 fis
->lba_mid_exp
= (u_int8_t
)(lba
>> 32);
1220 fis
->lba_high_exp
= (u_int8_t
)(lba
>> 40);
1221 fis
->sector_count
= xa
->tag
<< 3;
1222 fis
->features
= (u_int8_t
)count
;
1223 fis
->features_exp
= (u_int8_t
)(count
>> 8);
1224 } else if (count
> 0x100 || lba
> 0x0FFFFFFFU
) {
1228 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1229 ATA_C_WRITEDMA_EXT
: ATA_C_READDMA_EXT
;
1230 fis
->lba_low_exp
= (u_int8_t
)(lba
>> 24);
1231 fis
->lba_mid_exp
= (u_int8_t
)(lba
>> 32);
1232 fis
->lba_high_exp
= (u_int8_t
)(lba
>> 40);
1233 fis
->sector_count
= (u_int8_t
)count
;
1234 fis
->sector_count_exp
= (u_int8_t
)(count
>> 8);
1239 * NOTE: 256 sectors is supported, stored as 0.
1241 fis
->command
= (xa
->flags
& ATA_F_WRITE
) ?
1242 ATA_C_WRITEDMA
: ATA_C_READDMA
;
1243 fis
->device
|= (u_int8_t
)(lba
>> 24) & 0x0F;
1244 fis
->sector_count
= (u_int8_t
)count
;
1247 xa
->data
= csio
->data_ptr
;
1248 xa
->datalen
= csio
->dxfer_len
;
1249 xa
->complete
= sili_ata_complete_disk_rw
;
1250 xa
->timeout
= ccbh
->timeout
; /* milliseconds */
1251 if (ccbh
->flags
& CAM_POLLED
)
1252 xa
->flags
|= ATA_F_POLL
;
1257 * If the request is still in progress the xa and FIS have
1258 * been set up (except for the PM target), and must be dispatched.
1259 * Otherwise the request was completed.
1261 if (ccbh
->status
== CAM_REQ_INPROG
) {
1262 KKASSERT(xa
->complete
!= NULL
);
1263 xa
->atascsi_private
= ccb
;
1264 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
1265 sili_os_lock_port(ap
);
1266 xa
->fis
->flags
|= at
->at_target
;
1268 sili_os_unlock_port(ap
);
1270 sili_ata_put_xfer(xa
);
1276 * Convert the SCSI command in ccb to an ata_xfer command in xa
1277 * for ATA_PORT_T_ATAPI operations. Set the completion function
1278 * to convert the response back, then dispatch to the OpenBSD SILI
1283 sili_xpt_scsi_atapi_io(struct sili_port
*ap
, struct ata_port
*atx
,
1286 struct ccb_hdr
*ccbh
;
1287 struct ccb_scsiio
*csio
;
1288 struct ata_xfer
*xa
;
1289 struct ata_fis_h2d
*fis
;
1293 struct ata_port
*at
;
1295 ccbh
= &ccb
->csio
.ccb_h
;
1297 at
= atx
? atx
: &ap
->ap_ata
[0];
1299 switch (ccbh
->flags
& CAM_DIR_MASK
) {
1301 flags
= ATA_F_PACKET
| ATA_F_READ
;
1304 flags
= ATA_F_PACKET
| ATA_F_WRITE
;
1307 flags
= ATA_F_PACKET
;
1310 ccbh
->status
= CAM_REQ_INVALID
;
1317 * Special handling to get the rfis back into host memory while
1318 * still allowing the chip to run commands in parallel to
1319 * ATAPI devices behind a PM.
1321 flags
|= ATA_F_AUTOSENSE
;
1324 * The command has to fit in the packet command buffer.
1326 if (csio
->cdb_len
< 6 || csio
->cdb_len
> 16) {
1327 ccbh
->status
= CAM_CCB_LEN_ERR
;
1333 * Initialize the XA and FIS. It is unclear how much of
1334 * this has to mimic the equivalent ATA command.
1336 * XXX not passing NULL at for direct attach!
1338 xa
= sili_ata_get_xfer(ap
, atx
);
1341 fis
->flags
= ATA_H2D_FLAGS_CMD
| at
->at_target
;
1342 fis
->command
= ATA_C_PACKET
;
1343 fis
->device
= ATA_H2D_DEVICE_LBA
;
1344 fis
->sector_count
= xa
->tag
<< 3;
1345 if (flags
& (ATA_F_READ
| ATA_F_WRITE
)) {
1346 if (flags
& ATA_F_WRITE
) {
1347 fis
->features
= ATA_H2D_FEATURES_DMA
|
1348 ATA_H2D_FEATURES_DIR_WRITE
;
1350 fis
->features
= ATA_H2D_FEATURES_DMA
|
1351 ATA_H2D_FEATURES_DIR_READ
;
1357 fis
->control
= ATA_FIS_CONTROL_4BIT
;
1360 xa
->data
= csio
->data_ptr
;
1361 xa
->datalen
= csio
->dxfer_len
;
1362 xa
->timeout
= ccbh
->timeout
; /* milliseconds */
1364 if (ccbh
->flags
& CAM_POLLED
)
1365 xa
->flags
|= ATA_F_POLL
;
1368 * Copy the cdb to the packetcmd buffer in the FIS using a
1369 * convenient pointer in the xa.
1371 cdbs
= (void *)((ccbh
->flags
& CAM_CDB_POINTER
) ?
1372 csio
->cdb_io
.cdb_ptr
: csio
->cdb_io
.cdb_bytes
);
1373 bcopy(cdbs
, xa
->packetcmd
, csio
->cdb_len
);
1376 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1377 cdbs
->generic
.opcode
,
1378 csio
->cdb_len
, csio
->dxfer_len
);
1382 * Some ATAPI commands do not actually follow the SCSI standard.
1384 cdbd
= (void *)xa
->packetcmd
;
1386 switch(cdbd
->generic
.opcode
) {
1389 * Force SENSE requests to the ATAPI sense length.
1391 * It is unclear if this is needed or not.
1393 if (cdbd
->sense
.length
== SSD_FULL_SIZE
) {
1394 kprintf("%s: Shortening sense request\n",
1396 cdbd
->sense
.length
= offsetof(struct scsi_sense_data
,
1402 * Some ATAPI devices can't handle long inquiry lengths,
1403 * don't ask me why. Truncate the inquiry length.
1405 if (cdbd
->inquiry
.page_code
== 0 &&
1406 cdbd
->inquiry
.length
> SHORT_INQUIRY_LENGTH
) {
1407 cdbd
->inquiry
.length
= SHORT_INQUIRY_LENGTH
;
1413 * Convert *_6 to *_10 commands. Most ATAPI devices
1414 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1416 cdbd
->rw_10
.opcode
|= 0x20;
1417 cdbd
->rw_10
.byte2
= 0;
1418 cdbd
->rw_10
.addr
[0] = cdbs
->rw_6
.addr
[0] & 0x1F;
1419 cdbd
->rw_10
.addr
[1] = cdbs
->rw_6
.addr
[1];
1420 cdbd
->rw_10
.addr
[2] = cdbs
->rw_6
.addr
[2];
1421 cdbd
->rw_10
.addr
[3] = 0;
1422 cdbd
->rw_10
.reserved
= 0;
1423 cdbd
->rw_10
.length
[0] = 0;
1424 cdbd
->rw_10
.length
[1] = cdbs
->rw_6
.length
;
1425 cdbd
->rw_10
.control
= cdbs
->rw_6
.control
;
1434 xa
->complete
= sili_atapi_complete_cmd
;
1435 xa
->atascsi_private
= ccb
;
1436 ccb
->ccb_h
.sim_priv
.entries
[0].ptr
= ap
;
1437 sili_os_lock_port(ap
);
1439 sili_os_unlock_port(ap
);
1443 * Simulate page inquiries for disk attachments.
1447 sili_xpt_page_inquiry(struct sili_port
*ap
, struct ata_port
*at
, union ccb
*ccb
)
1450 struct scsi_vpd_supported_page_list list
;
1451 struct scsi_vpd_unit_serial_number serno
;
1452 struct scsi_vpd_unit_devid devid
;
1460 page
= kmalloc(sizeof(*page
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
1462 cdb
= (void *)((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) ?
1463 ccb
->csio
.cdb_io
.cdb_ptr
: ccb
->csio
.cdb_io
.cdb_bytes
);
1465 switch(cdb
->inquiry
.page_code
) {
1466 case SVPD_SUPPORTED_PAGE_LIST
:
1468 page
->list
.device
= T_DIRECT
;
1469 page
->list
.page_code
= SVPD_SUPPORTED_PAGE_LIST
;
1470 page
->list
.list
[i
++] = SVPD_SUPPORTED_PAGE_LIST
;
1471 page
->list
.list
[i
++] = SVPD_UNIT_SERIAL_NUMBER
;
1472 page
->list
.list
[i
++] = SVPD_UNIT_DEVID
;
1473 page
->list
.length
= i
;
1474 len
= offsetof(struct scsi_vpd_supported_page_list
, list
[3]);
1476 case SVPD_UNIT_SERIAL_NUMBER
:
1478 j
= sizeof(at
->at_identify
.serial
);
1479 for (i
= 0; i
< j
&& at
->at_identify
.serial
[i
] == ' '; ++i
)
1481 while (j
> i
&& at
->at_identify
.serial
[j
-1] == ' ')
1483 page
->serno
.device
= T_DIRECT
;
1484 page
->serno
.page_code
= SVPD_UNIT_SERIAL_NUMBER
;
1485 page
->serno
.length
= j
- i
;
1486 bcopy(at
->at_identify
.serial
+ i
,
1487 page
->serno
.serial_num
, j
- i
);
1488 len
= offsetof(struct scsi_vpd_unit_serial_number
,
1491 case SVPD_UNIT_DEVID
:
1492 /* fall through for now */
1494 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
1498 if (ccb
->ccb_h
.status
== CAM_REQ_INPROG
) {
1499 if (len
<= ccb
->csio
.dxfer_len
) {
1500 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
1501 bzero(ccb
->csio
.data_ptr
, ccb
->csio
.dxfer_len
);
1502 bcopy(page
, ccb
->csio
.data_ptr
, len
);
1503 ccb
->csio
.resid
= ccb
->csio
.dxfer_len
- len
;
1505 ccb
->ccb_h
.status
= CAM_CCB_LEN_ERR
;
1508 kfree(page
, M_DEVBUF
);
1512 * Completion function for ATA_PORT_T_DISK cache synchronization.
1516 sili_ata_complete_disk_synchronize_cache(struct ata_xfer
*xa
)
1518 union ccb
*ccb
= xa
->atascsi_private
;
1519 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1520 struct sili_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1523 case ATA_S_COMPLETE
:
1524 ccbh
->status
= CAM_REQ_CMP
;
1525 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1528 kprintf("%s: synchronize_cache: error\n",
1529 ATANAME(ap
, xa
->at
));
1530 ccbh
->status
= CAM_SCSI_STATUS_ERROR
| CAM_AUTOSNS_VALID
;
1531 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1532 sili_ata_dummy_sense(&ccb
->csio
.sense_data
);
1535 kprintf("%s: synchronize_cache: timeout\n",
1536 ATANAME(ap
, xa
->at
));
1537 ccbh
->status
= CAM_CMD_TIMEOUT
;
1540 kprintf("%s: synchronize_cache: unknown state %d\n",
1541 ATANAME(ap
, xa
->at
), xa
->state
);
1542 ccbh
->status
= CAM_REQ_CMP_ERR
;
1545 sili_ata_put_xfer(xa
);
1546 sili_os_unlock_port(ap
);
1548 sili_os_lock_port(ap
);
1552 * Completion function for ATA_PORT_T_DISK I/O
1556 sili_ata_complete_disk_rw(struct ata_xfer
*xa
)
1558 union ccb
*ccb
= xa
->atascsi_private
;
1559 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1560 struct sili_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1563 case ATA_S_COMPLETE
:
1564 ccbh
->status
= CAM_REQ_CMP
;
1565 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1568 kprintf("%s: disk_rw: error\n", ATANAME(ap
, xa
->at
));
1569 ccbh
->status
= CAM_SCSI_STATUS_ERROR
| CAM_AUTOSNS_VALID
;
1570 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1571 sili_ata_dummy_sense(&ccb
->csio
.sense_data
);
1574 kprintf("%s: disk_rw: timeout\n", ATANAME(ap
, xa
->at
));
1575 ccbh
->status
= CAM_CMD_TIMEOUT
;
1576 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1577 sili_ata_dummy_sense(&ccb
->csio
.sense_data
);
1580 kprintf("%s: disk_rw: unknown state %d\n",
1581 ATANAME(ap
, xa
->at
), xa
->state
);
1582 ccbh
->status
= CAM_REQ_CMP_ERR
;
1585 ccb
->csio
.resid
= xa
->resid
;
1586 sili_ata_put_xfer(xa
);
1587 sili_os_unlock_port(ap
);
1589 sili_os_lock_port(ap
);
1593 * Completion function for ATA_PORT_T_ATAPI I/O
1595 * Sense data is returned in the rfis.
1599 sili_atapi_complete_cmd(struct ata_xfer
*xa
)
1601 union ccb
*ccb
= xa
->atascsi_private
;
1602 struct ccb_hdr
*ccbh
= &ccb
->ccb_h
;
1603 struct sili_port
*ap
= ccb
->ccb_h
.sim_priv
.entries
[0].ptr
;
1606 cdb
= (void *)((ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) ?
1607 ccb
->csio
.cdb_io
.cdb_ptr
: ccb
->csio
.cdb_io
.cdb_bytes
);
1610 case ATA_S_COMPLETE
:
1611 ccbh
->status
= CAM_REQ_CMP
;
1612 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
1615 ccbh
->status
= CAM_SCSI_STATUS_ERROR
;
1616 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1617 sili_ata_atapi_sense(xa
->rfis
, &ccb
->csio
.sense_data
);
1620 kprintf("%s: cmd %d: timeout\n",
1621 PORTNAME(ap
), cdb
->generic
.opcode
);
1622 ccbh
->status
= CAM_CMD_TIMEOUT
;
1623 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
1624 sili_ata_dummy_sense(&ccb
->csio
.sense_data
);
1627 kprintf("%s: cmd %d: unknown state %d\n",
1628 PORTNAME(ap
), cdb
->generic
.opcode
, xa
->state
);
1629 ccbh
->status
= CAM_REQ_CMP_ERR
;
1632 ccb
->csio
.resid
= xa
->resid
;
1633 sili_ata_put_xfer(xa
);
1634 sili_os_unlock_port(ap
);
1636 sili_os_lock_port(ap
);
1640 * Construct dummy sense data for errors on DISKs
1644 sili_ata_dummy_sense(struct scsi_sense_data
*sense_data
)
1646 sense_data
->error_code
= SSD_ERRCODE_VALID
| SSD_CURRENT_ERROR
;
1647 sense_data
->segment
= 0;
1648 sense_data
->flags
= SSD_KEY_MEDIUM_ERROR
;
1649 sense_data
->info
[0] = 0;
1650 sense_data
->info
[1] = 0;
1651 sense_data
->info
[2] = 0;
1652 sense_data
->info
[3] = 0;
1653 sense_data
->extra_len
= 0;
1657 * Construct atapi sense data for errors on ATAPI
1659 * The ATAPI sense data is stored in the passed rfis and must be converted
1660 * to SCSI sense data.
1664 sili_ata_atapi_sense(struct ata_fis_d2h
*rfis
,
1665 struct scsi_sense_data
*sense_data
)
1667 sense_data
->error_code
= SSD_ERRCODE_VALID
| SSD_CURRENT_ERROR
;
1668 sense_data
->segment
= 0;
1669 sense_data
->flags
= (rfis
->error
& 0xF0) >> 4;
1670 if (rfis
->error
& 0x04)
1671 sense_data
->flags
|= SSD_KEY_ILLEGAL_REQUEST
;
1672 if (rfis
->error
& 0x02)
1673 sense_data
->flags
|= SSD_EOM
;
1674 if (rfis
->error
& 0x01)
1675 sense_data
->flags
|= SSD_ILI
;
1676 sense_data
->info
[0] = 0;
1677 sense_data
->info
[1] = 0;
1678 sense_data
->info
[2] = 0;
1679 sense_data
->info
[3] = 0;
1680 sense_data
->extra_len
= 0;
1685 sili_strip_string(const char **basep
, int *lenp
)
1687 const char *base
= *basep
;
1690 while (len
&& (*base
== 0 || *base
== ' ')) {
1694 while (len
&& (base
[len
-1] == 0 || base
[len
-1] == ' '))