satafis_subr.c revision 1.2.2.2 1 1.2.2.2 yamt /* $NetBSD: satafis_subr.c,v 1.2.2.2 2009/06/20 07:20:20 yamt Exp $ */
2 1.2.2.2 yamt
3 1.2.2.2 yamt /*-
4 1.2.2.2 yamt * Copyright (c) 2009 Jonathan A. Kollasch.
5 1.2.2.2 yamt * All rights reserved.
6 1.2.2.2 yamt *
7 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 yamt * modification, are permitted provided that the following conditions
9 1.2.2.2 yamt * are met:
10 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
15 1.2.2.2 yamt *
16 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.2.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.2.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.2.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.2.2.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.2.2.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.2.2.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.2.2.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.2.2.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.2.2.2 yamt */
27 1.2.2.2 yamt
28 1.2.2.2 yamt /*
29 1.2.2.2 yamt * Copyright (c) 2006 Manuel Bouyer.
30 1.2.2.2 yamt *
31 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
32 1.2.2.2 yamt * modification, are permitted provided that the following conditions
33 1.2.2.2 yamt * are met:
34 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
35 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
36 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
37 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
38 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
39 1.2.2.2 yamt * 3. All advertising materials mentioning features or use of this software
40 1.2.2.2 yamt * must display the following acknowledgement:
41 1.2.2.2 yamt * This product includes software developed by Manuel Bouyer.
42 1.2.2.2 yamt * 4. The name of the author may not be used to endorse or promote products
43 1.2.2.2 yamt * derived from this software without specific prior written permission.
44 1.2.2.2 yamt *
45 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 1.2.2.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 1.2.2.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 1.2.2.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 1.2.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 1.2.2.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 1.2.2.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 1.2.2.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 1.2.2.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 1.2.2.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 1.2.2.2 yamt *
56 1.2.2.2 yamt */
57 1.2.2.2 yamt
58 1.2.2.2 yamt #include <sys/cdefs.h>
59 1.2.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: satafis_subr.c,v 1.2.2.2 2009/06/20 07:20:20 yamt Exp $");
60 1.2.2.2 yamt
61 1.2.2.2 yamt #include <sys/param.h>
62 1.2.2.2 yamt #include <sys/systm.h>
63 1.2.2.2 yamt
64 1.2.2.2 yamt #include <sys/disklabel.h>
65 1.2.2.2 yamt
66 1.2.2.2 yamt #include <dev/ata/atareg.h>
67 1.2.2.2 yamt #include <dev/ata/atavar.h>
68 1.2.2.2 yamt
69 1.2.2.2 yamt #include <dev/ata/satafisreg.h>
70 1.2.2.2 yamt #include <dev/ata/satafisvar.h>
71 1.2.2.2 yamt
72 1.2.2.2 yamt #include <dev/ic/wdcreg.h> /* for WDCTL_4BIT */
73 1.2.2.2 yamt
74 1.2.2.2 yamt #include "atapibus.h"
75 1.2.2.2 yamt
76 1.2.2.2 yamt void
77 1.2.2.2 yamt satafis_rhd_construct_cmd(struct ata_command *ata_c, uint8_t *fis)
78 1.2.2.2 yamt {
79 1.2.2.2 yamt memset(fis, 0, RHD_FISLEN);
80 1.2.2.2 yamt
81 1.2.2.2 yamt fis[fis_type] = RHD_FISTYPE;
82 1.2.2.2 yamt fis[rhd_cdpmp] = 0x80; /* xxx magic */
83 1.2.2.2 yamt fis[rhd_command] = ata_c->r_command;
84 1.2.2.2 yamt fis[rhd_features] = ata_c->r_features;
85 1.2.2.2 yamt fis[rhd_sector] = ata_c->r_sector;
86 1.2.2.2 yamt fis[rhd_cyl_lo] = ata_c->r_cyl & 0xff;
87 1.2.2.2 yamt fis[rhd_cyl_hi] = (ata_c->r_cyl >> 8) & 0xff;
88 1.2.2.2 yamt fis[rhd_dh] = ata_c->r_head & 0x0f;
89 1.2.2.2 yamt fis[rhd_seccnt] = ata_c->r_count;
90 1.2.2.2 yamt fis[rhd_control] = WDCTL_4BIT;
91 1.2.2.2 yamt
92 1.2.2.2 yamt return;
93 1.2.2.2 yamt }
94 1.2.2.2 yamt
95 1.2.2.2 yamt void
96 1.2.2.2 yamt satafis_rhd_construct_bio(struct ata_xfer *xfer, uint8_t *fis)
97 1.2.2.2 yamt {
98 1.2.2.2 yamt struct ata_bio *ata_bio = xfer->c_cmd;
99 1.2.2.2 yamt int nblks;
100 1.2.2.2 yamt
101 1.2.2.2 yamt nblks = xfer->c_bcount / ata_bio->lp->d_secsize;
102 1.2.2.2 yamt
103 1.2.2.2 yamt memset(fis, 0, RHD_FISLEN);
104 1.2.2.2 yamt
105 1.2.2.2 yamt fis[fis_type] = RHD_FISTYPE;
106 1.2.2.2 yamt fis[rhd_cdpmp] = 0x80; /* xxx magic */
107 1.2.2.2 yamt if (ata_bio->flags & ATA_LBA48) {
108 1.2.2.2 yamt fis[rhd_command] = (ata_bio->flags & ATA_READ) ?
109 1.2.2.2 yamt WDCC_READDMA_EXT : WDCC_WRITEDMA_EXT;
110 1.2.2.2 yamt } else {
111 1.2.2.2 yamt fis[rhd_command] =
112 1.2.2.2 yamt (ata_bio->flags & ATA_READ) ? WDCC_READDMA : WDCC_WRITEDMA;
113 1.2.2.2 yamt }
114 1.2.2.2 yamt fis[rhd_sector] = ata_bio->blkno & 0xff;
115 1.2.2.2 yamt fis[rhd_cyl_lo] = (ata_bio->blkno >> 8) & 0xff;
116 1.2.2.2 yamt fis[rhd_cyl_hi] = (ata_bio->blkno >> 16) & 0xff;
117 1.2.2.2 yamt if (ata_bio->flags & ATA_LBA48) {
118 1.2.2.2 yamt fis[rhd_dh] = WDSD_LBA;
119 1.2.2.2 yamt fis[rhd_sector_exp] = (ata_bio->blkno >> 24) & 0xff;
120 1.2.2.2 yamt fis[rhd_cyl_lo_exp] = (ata_bio->blkno >> 32) & 0xff;
121 1.2.2.2 yamt fis[rhd_cyl_hi_exp] = (ata_bio->blkno >> 40) & 0xff;
122 1.2.2.2 yamt } else {
123 1.2.2.2 yamt fis[rhd_dh] = ((ata_bio->blkno >> 24) & 0x0f) | WDSD_LBA;
124 1.2.2.2 yamt }
125 1.2.2.2 yamt fis[rhd_seccnt] = nblks & 0xff;
126 1.2.2.2 yamt fis[rhd_seccnt_exp] = (ata_bio->flags & ATA_LBA48) ?
127 1.2.2.2 yamt ((nblks >> 8) & 0xff) : 0;
128 1.2.2.2 yamt fis[rhd_control] = WDCTL_4BIT;
129 1.2.2.2 yamt return;
130 1.2.2.2 yamt }
131 1.2.2.2 yamt
132 1.2.2.2 yamt #if NATAPIBUS > 0
133 1.2.2.2 yamt void
134 1.2.2.2 yamt satafis_rhd_construct_atapi(struct ata_xfer *xfer, uint8_t *fis)
135 1.2.2.2 yamt {
136 1.2.2.2 yamt memset(fis, 0, RHD_FISLEN);
137 1.2.2.2 yamt
138 1.2.2.2 yamt fis[fis_type] = RHD_FISTYPE;
139 1.2.2.2 yamt fis[rhd_cdpmp] = 0x80; /* xxx magic */
140 1.2.2.2 yamt fis[rhd_command] = ATAPI_PKT_CMD;
141 1.2.2.2 yamt fis[rhd_features] = (xfer->c_flags & C_DMA) ?
142 1.2.2.2 yamt ATAPI_PKT_CMD_FTRE_DMA : 0;
143 1.2.2.2 yamt fis[rhd_dh] = WDSD_IBM; /* XXX or WDSD_LBA? */
144 1.2.2.2 yamt fis[rhd_control] = WDCTL_4BIT;
145 1.2.2.2 yamt
146 1.2.2.2 yamt return;
147 1.2.2.2 yamt }
148 1.2.2.2 yamt #endif /* NATAPIBUS */
149 1.2.2.2 yamt
150 1.2.2.2 yamt void
151 1.2.2.2 yamt satafis_sdb_parse(struct ata_channel *chp, uint8_t *fis)
152 1.2.2.2 yamt {
153 1.2.2.2 yamt KASSERT(fis[fis_type] != SDB_FISTYPE);
154 1.2.2.2 yamt chp->ch_status = fis[sdb_status];
155 1.2.2.2 yamt chp->ch_error = fis[sdb_error];
156 1.2.2.2 yamt
157 1.2.2.2 yamt return;
158 1.2.2.2 yamt }
159