1 /* $NetBSD: atawd.c,v 1.8 2009/01/12 08:06:54 tsutsui Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * 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 the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/disklabel.h>
36 #include <sys/bootblock.h>
37 #include <dev/raidframe/raidframevar.h>
38 #include <dev/ic/wdcreg.h>
39 #include <dev/ata/atareg.h>
41 #include <lib/libsa/stand.h>
42 #include <lib/libkern/libkern.h>
43 #include <machine/stdarg.h>
48 #define DPRINTF(x) printf x;
54 int (*match
)(unsigned, void *);
55 void *(*init
)(unsigned, void *);
60 #define ATAC_DECL(xxx) \
61 int xxx ## _match(unsigned, void *); \
62 void * xxx ## _init(unsigned, void *)
66 static struct atacdv vatacdv
[] = {
67 { pciide_match
, pciide_init
, 01 },
69 static int natacdv
= sizeof(vatacdv
)/sizeof(vatacdv
[0]);
75 static int wd_get_params(struct wd_softc
*);
76 static int wdgetdisklabel(struct wd_softc
*);
78 int atac_init(unsigned);
79 int atac_probe(void *);
80 static int atac_wait_for_ready(struct atac_channel
*);
81 static int atac_exec_identify(struct wd_softc
*, void *);
82 static int atac_read_block(struct wd_softc
*, struct atac_command
*);
83 static int atacommand(struct wd_softc
*, struct atac_command
*);
84 static int atac_exec_read(struct wd_softc
*, int, daddr_t
, void *);
86 #define WDC_TIMEOUT 2000000
89 wdopen(struct open_file
*f
, ...)
98 unit
= va_arg(ap
, u_int
);
99 part
= va_arg(ap
, u_int
);
106 if (part
>= lp
->d_npartitions
)
108 pp
= &lp
->d_partitions
[part
];
109 if (pp
->p_size
== 0 || pp
->p_fstype
== FS_UNUSED
)
117 wdclose(struct open_file
*f
)
125 wdstrategy(void *f
, int rw
, daddr_t dblk
, size_t size
, void *p
, size_t *rsize
)
130 struct partition
*pp
;
141 pp
= &wd
->sc_label
.d_partitions
[wd
->sc_part
];
143 nsect
= howmany(size
, wd
->sc_label
.d_secsize
);
144 blkno
= dblk
+ pp
->p_offset
;
145 if (pp
->p_fstype
== FS_RAID
)
146 blkno
+= RF_PROTECTED_SECTORS
;
148 for (i
= 0; i
< nsect
; i
++, blkno
++) {
151 if ((error
= atac_exec_read(wd
, WDCC_READ
, blkno
, buf
)) != 0)
154 buf
+= wd
->sc_label
.d_secsize
;
162 parsefstype(void *data
)
164 struct wd_softc
*wd
= data
;
165 struct disklabel
*lp
= &wd
->sc_label
;
166 struct partition
*pp
= &lp
->d_partitions
[wd
->sc_part
];
172 atac_init(unsigned tag
)
177 for (n
= 0; n
< natacdv
; n
++) {
179 if ((*dv
->match
)(tag
, NULL
) > 0)
185 atac
->priv
= (*dv
->init
)(tag
, (void *)dv
->chvalid
);
190 atac_probe(void *atac
)
192 struct atac_softc
*l
= atac
;
194 int i
, error
, chvalid
;
197 chvalid
= l
->chvalid
;
198 for (i
= 0; chvalid
!= 0; i
+= 1) {
199 if (chvalid
& (01 << i
)) {
200 chvalid
&= ~(01 << i
);
202 error
= diskprobe(atac
);
206 wd
= alloc(sizeof(struct wd_softc
));
207 memset(wd
, 0, sizeof(struct wd_softc
));
209 wd
->sc_channel
= &l
->channel
[i
];
210 disk
[ndisk
] = (void *)wd
;
211 error
= wd_get_params(wd
);
214 error
= wdgetdisklabel(wd
);
224 wd_get_params(struct wd_softc
*wd
)
227 uint8_t *buf
= wd
->sc_buf
;
229 if ((error
= atac_exec_identify(wd
, buf
)) != 0)
232 wd
->sc_params
= *(struct ataparams
*)buf
;
234 /* 48-bit LBA addressing */
235 if ((wd
->sc_params
.atap_cmd2_en
& ATA_CMD2_LBA48
) != 0) {
236 DPRINTF(("Drive supports LBA48.\n"));
237 wd
->sc_flags
|= WDF_LBA48
;
240 /* Prior to ATA-4, LBA was optional. */
241 if ((wd
->sc_params
.atap_capabilities1
& WDC_CAP_LBA
) != 0) {
242 DPRINTF(("Drive supports LBA.\n"));
243 wd
->sc_flags
|= WDF_LBA
;
250 wdgetdisklabel(struct wd_softc
*wd
)
255 struct mbr_partition
*dp
, *bsdp
;
256 struct disklabel
*lp
;
257 uint8_t *buf
= wd
->sc_buf
;
260 memset(lp
, 0, sizeof(struct disklabel
));
263 if (wdstrategy(wd
, F_READ
, MBR_BBSECTOR
, DEV_BSIZE
, buf
, &rsize
))
266 dp
= (struct mbr_partition
*)(buf
+ MBR_PART_OFFSET
);
268 for (i
= 0; i
< MBR_PART_COUNT
; i
++, dp
++) {
269 if (dp
->mbrp_type
== MBR_PTYPE_NETBSD
) {
275 /* generate fake disklabel */
276 lp
->d_secsize
= DEV_BSIZE
;
277 lp
->d_ntracks
= wd
->sc_params
.atap_heads
;
278 lp
->d_nsectors
= wd
->sc_params
.atap_sectors
;
279 lp
->d_ncylinders
= wd
->sc_params
.atap_cylinders
;
280 lp
->d_secpercyl
= lp
->d_ntracks
* lp
->d_nsectors
;
281 if (strcmp((char *)wd
->sc_params
.atap_model
, "ST506") == 0)
282 lp
->d_type
= DTYPE_ST506
;
284 lp
->d_type
= DTYPE_ESDI
;
285 strncpy(lp
->d_typename
, (char *)wd
->sc_params
.atap_model
, 16);
286 strncpy(lp
->d_packname
, "fictitious", 16);
287 if (wd
->sc_capacity
> UINT32_MAX
)
288 lp
->d_secperunit
= UINT32_MAX
;
290 lp
->d_secperunit
= wd
->sc_capacity
;
292 lp
->d_interleave
= 1;
294 lp
->d_partitions
[RAW_PART
].p_offset
= 0;
295 lp
->d_partitions
[RAW_PART
].p_size
=
296 lp
->d_secperunit
* (lp
->d_secsize
/ DEV_BSIZE
);
297 lp
->d_partitions
[RAW_PART
].p_fstype
= FS_UNUSED
;
298 lp
->d_magic
= DISKMAGIC
;
299 lp
->d_magic2
= DISKMAGIC
;
300 lp
->d_checksum
= dkcksum(lp
);
302 dp
= (struct mbr_partition
*)(buf
+ MBR_PART_OFFSET
);
304 for (i
= 0; i
< MBR_PART_COUNT
; i
++, dp
++) {
305 if (dp
->mbrp_type
== MBR_PTYPE_UNUSED
)
307 lp
->d_partitions
[n
].p_offset
= bswap32(dp
->mbrp_start
);
308 lp
->d_partitions
[n
].p_size
= bswap32(dp
->mbrp_size
);
309 switch (dp
->mbrp_type
) {
310 case MBR_PTYPE_FAT12
:
311 case MBR_PTYPE_FAT16S
:
312 case MBR_PTYPE_FAT16B
:
313 case MBR_PTYPE_FAT32
:
314 case MBR_PTYPE_FAT32L
:
315 case MBR_PTYPE_FAT16L
:
316 lp
->d_partitions
[n
].p_fstype
= FS_MSDOS
;
318 case MBR_PTYPE_LNXEXT2
:
319 lp
->d_partitions
[n
].p_fstype
= FS_EX2FS
;
322 lp
->d_partitions
[n
].p_fstype
= FS_OTHER
;
327 lp
->d_npartitions
= n
;
330 sector
= bswap32(bsdp
->mbrp_start
);
331 if (wdstrategy(wd
, F_READ
, sector
+ LABELSECTOR
, DEV_BSIZE
,
334 msg
= getdisklabel((char *)buf
+ LABELOFFSET
, &wd
->sc_label
);
336 printf("wd%d: getdisklabel: %s\n", wd
->sc_unit
, msg
);
339 DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
340 "d_ntracks %d, d_secpercyl %d\n",
341 wd
->sc_label
.d_secsize
,
342 wd
->sc_label
.d_nsectors
,
343 wd
->sc_label
.d_ncylinders
,
344 wd
->sc_label
.d_ntracks
,
345 wd
->sc_label
.d_secpercyl
));
351 atac_wait_for_ready(struct atac_channel
*chp
)
355 for (timeout
= WDC_TIMEOUT
; timeout
> 0; --timeout
) {
356 if ((WDC_READ_CMD(chp
, wd_status
) & (WDCS_BSY
| WDCS_DRDY
))
364 atac_read_block(struct wd_softc
*wd
, struct atac_command
*cmd
)
367 struct atac_channel
*chp
= wd
->sc_channel
;
368 uint16_t *ptr
= (uint16_t *)cmd
->data
;
372 for (i
= cmd
->bcount
; i
> 0; i
-= sizeof(uint16_t))
373 *ptr
++ = WDC_READ_DATA(chp
);
378 atacommand(struct wd_softc
*wd
, struct atac_command
*cmd
)
380 struct atac_channel
*chp
= wd
->sc_channel
;
382 if (wd
->sc_flags
& WDF_LBA48
) {
383 WDC_WRITE_CMD(chp
, wd_sdh
, (cmd
->drive
<< 4) | WDSD_LBA
);
386 WDC_WRITE_CMD(chp
, wd_features
, 0);
387 WDC_WRITE_CMD(chp
, wd_seccnt
, cmd
->r_count
>> 8);
388 WDC_WRITE_CMD(chp
, wd_lba_hi
, cmd
->r_blkno
>> 40);
389 WDC_WRITE_CMD(chp
, wd_lba_mi
, cmd
->r_blkno
>> 32);
390 WDC_WRITE_CMD(chp
, wd_lba_lo
, cmd
->r_blkno
>> 24);
393 WDC_WRITE_CMD(chp
, wd_features
, 0);
394 WDC_WRITE_CMD(chp
, wd_seccnt
, cmd
->r_count
);
395 WDC_WRITE_CMD(chp
, wd_lba_hi
, cmd
->r_blkno
>> 16);
396 WDC_WRITE_CMD(chp
, wd_lba_mi
, cmd
->r_blkno
>> 8);
397 WDC_WRITE_CMD(chp
, wd_lba_lo
, cmd
->r_blkno
);
400 WDC_WRITE_CMD(chp
, wd_command
, cmd
->r_command
);
402 if (atac_wait_for_ready(chp
) != 0)
405 if (WDC_READ_CMD(chp
, wd_status
) & WDCS_ERR
) {
406 printf("wd%d: error %x\n", chp
->compatchan
,
407 WDC_READ_CMD(chp
, wd_error
));
412 WDC_WRITE_CMD(chp
, wd_precomp
, cmd
->r_precomp
);
413 WDC_WRITE_CMD(chp
, wd_seccnt
, cmd
->r_count
);
414 WDC_WRITE_CMD(chp
, wd_sector
, cmd
->r_sector
);
415 WDC_WRITE_CMD(chp
, wd_cyl_lo
, cmd
->r_cyl
);
416 WDC_WRITE_CMD(chp
, wd_cyl_hi
, cmd
->r_cyl
>> 8);
417 WDC_WRITE_CMD(chp
, wd_sdh
,
418 WDSD_IBM
| (cmd
->drive
<< 4) | cmd
->r_head
);
419 WDC_WRITE_CMD(chp
, wd_command
, cmd
->r_command
);
422 if (atac_wait_for_ready(chp
) != 0)
425 if (WDC_READ_CMD(chp
, wd_status
) & WDCS_ERR
) {
426 printf("wd%d: error %x\n", chp
->compatchan
,
427 WDC_READ_CMD(chp
, wd_error
));
434 atac_exec_identify(struct wd_softc
*wd
, void *data
)
437 struct atac_command
*cmd
;
439 cmd
= &wd
->sc_command
;
440 memset(cmd
, 0, sizeof(struct atac_command
));
442 cmd
->r_command
= WDCC_IDENTIFY
;
445 cmd
->drive
= wd
->sc_unit
;
446 cmd
->bcount
= wd
->sc_label
.d_secsize
;
448 if ((error
= atacommand(wd
, cmd
)) != 0)
450 return atac_read_block(wd
, cmd
);
454 atac_exec_read(struct wd_softc
*wd
, int exe
, daddr_t blkno
, void *data
)
457 struct atac_command
*cmd
;
459 cmd
= &wd
->sc_command
;
460 memset(cmd
, 0, sizeof(struct atac_command
));
462 if (wd
->sc_flags
& WDF_LBA48
)
463 cmd
->r_blkno
= blkno
;
464 else if (wd
->sc_flags
& WDF_LBA
) {
465 cmd
->r_sector
= blkno
& 0xff;
466 cmd
->r_cyl
= (blkno
>> 8) & 0xffff;
467 cmd
->r_head
= (blkno
>> 24) & 0x0f;
468 cmd
->r_head
|= WDSD_LBA
;
471 cmd
->r_sector
= 1 + (blkno
% wd
->sc_label
.d_nsectors
);
472 blkno
/= wd
->sc_label
.d_nsectors
;
473 cmd
->r_head
= blkno
% wd
->sc_label
.d_ntracks
;
474 blkno
/= wd
->sc_label
.d_ntracks
;
476 cmd
->r_head
|= WDSD_CHS
;
478 cmd
->r_command
= exe
;
481 cmd
->drive
= wd
->sc_unit
;
482 cmd
->bcount
= wd
->sc_label
.d_secsize
;
484 if ((error
= atacommand(wd
, cmd
)) != 0)
486 return atac_read_block(wd
, cmd
);