Home | History | Annotate | Line # | Download | only in boot
wdc.c revision 1.8.2.2
      1  1.8.2.2  tsutsui /*	$NetBSD: wdc.c,v 1.8.2.2 2007/08/03 13:15:58 tsutsui Exp $	*/
      2  1.8.2.2  tsutsui 
      3  1.8.2.2  tsutsui /*-
      4  1.8.2.2  tsutsui  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.8.2.2  tsutsui  * All rights reserved.
      6  1.8.2.2  tsutsui  *
      7  1.8.2.2  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.2.2  tsutsui  * by Manuel Bouyer.
      9  1.8.2.2  tsutsui  *
     10  1.8.2.2  tsutsui  * Redistribution and use in source and binary forms, with or without
     11  1.8.2.2  tsutsui  * modification, are permitted provided that the following conditions
     12  1.8.2.2  tsutsui  * are met:
     13  1.8.2.2  tsutsui  * 1. Redistributions of source code must retain the above copyright
     14  1.8.2.2  tsutsui  *    notice, this list of conditions and the following disclaimer.
     15  1.8.2.2  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.2.2  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.2.2  tsutsui  *    documentation and/or other materials provided with the distribution.
     18  1.8.2.2  tsutsui  * 3. All advertising materials mentioning features or use of this software
     19  1.8.2.2  tsutsui  *    must display the following acknowledgement:
     20  1.8.2.2  tsutsui  *        This product includes software developed by the NetBSD
     21  1.8.2.2  tsutsui  *        Foundation, Inc. and its contributors.
     22  1.8.2.2  tsutsui  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.8.2.2  tsutsui  *    contributors may be used to endorse or promote products derived
     24  1.8.2.2  tsutsui  *    from this software without specific prior written permission.
     25  1.8.2.2  tsutsui  *
     26  1.8.2.2  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.8.2.2  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.8.2.2  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.8.2.2  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.8.2.2  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.8.2.2  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.8.2.2  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.8.2.2  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.8.2.2  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.8.2.2  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.8.2.2  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     37  1.8.2.2  tsutsui  */
     38  1.8.2.2  tsutsui 
     39  1.8.2.2  tsutsui #include <sys/types.h>
     40  1.8.2.2  tsutsui #include <sys/disklabel.h>
     41  1.8.2.2  tsutsui #include <sys/bootblock.h>
     42  1.8.2.2  tsutsui 
     43  1.8.2.2  tsutsui #include <lib/libsa/stand.h>
     44  1.8.2.2  tsutsui #include <machine/param.h>
     45  1.8.2.2  tsutsui 
     46  1.8.2.2  tsutsui #include "boot.h"
     47  1.8.2.2  tsutsui #include "wdvar.h"
     48  1.8.2.2  tsutsui 
     49  1.8.2.2  tsutsui #define WDCDELAY	100
     50  1.8.2.2  tsutsui #define WDCNDELAY_RST	31000 * 10
     51  1.8.2.2  tsutsui 
     52  1.8.2.2  tsutsui static int  wdcprobe(struct wdc_channel *chp);
     53  1.8.2.2  tsutsui static int  wdc_wait_for_ready(struct wdc_channel *chp);
     54  1.8.2.2  tsutsui static int  wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c);
     55  1.8.2.2  tsutsui static int  __wdcwait_reset(struct wdc_channel *chp, int drv_mask);
     56  1.8.2.2  tsutsui 
     57  1.8.2.2  tsutsui /*
     58  1.8.2.2  tsutsui  * Reset the controller.
     59  1.8.2.2  tsutsui  */
     60  1.8.2.2  tsutsui static int
     61  1.8.2.2  tsutsui __wdcwait_reset(struct wdc_channel *chp, int drv_mask)
     62  1.8.2.2  tsutsui {
     63  1.8.2.2  tsutsui 	int timeout;
     64  1.8.2.2  tsutsui 	uint8_t st0, st1;
     65  1.8.2.2  tsutsui 
     66  1.8.2.2  tsutsui 	/* wait for BSY to deassert */
     67  1.8.2.2  tsutsui 	for (timeout = 0; timeout < WDCNDELAY_RST; timeout++) {
     68  1.8.2.2  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM); /* master */
     69  1.8.2.2  tsutsui 		delay(10);
     70  1.8.2.2  tsutsui 		st0 = WDC_READ_REG(chp, wd_status);
     71  1.8.2.2  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10); /* slave */
     72  1.8.2.2  tsutsui 		delay(10);
     73  1.8.2.2  tsutsui 		st1 = WDC_READ_REG(chp, wd_status);
     74  1.8.2.2  tsutsui 
     75  1.8.2.2  tsutsui 		if ((drv_mask & 0x01) == 0) {
     76  1.8.2.2  tsutsui 			/* no master */
     77  1.8.2.2  tsutsui 			if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
     78  1.8.2.2  tsutsui 				/* No master, slave is ready, it's done */
     79  1.8.2.2  tsutsui 				goto end;
     80  1.8.2.2  tsutsui 			}
     81  1.8.2.2  tsutsui 		} else if ((drv_mask & 0x02) == 0) {
     82  1.8.2.2  tsutsui 			/* no slave */
     83  1.8.2.2  tsutsui 			if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
     84  1.8.2.2  tsutsui 				/* No slave, master is ready, it's done */
     85  1.8.2.2  tsutsui 				goto end;
     86  1.8.2.2  tsutsui 			}
     87  1.8.2.2  tsutsui 		} else {
     88  1.8.2.2  tsutsui 			/* Wait for both master and slave to be ready */
     89  1.8.2.2  tsutsui 			if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
     90  1.8.2.2  tsutsui 				goto end;
     91  1.8.2.2  tsutsui 			}
     92  1.8.2.2  tsutsui 		}
     93  1.8.2.2  tsutsui 
     94  1.8.2.2  tsutsui 		delay(WDCDELAY);
     95  1.8.2.2  tsutsui 	}
     96  1.8.2.2  tsutsui 
     97  1.8.2.2  tsutsui 	/* Reset timed out. Maybe it's because drv_mask was not right */
     98  1.8.2.2  tsutsui 	if (st0 & WDCS_BSY)
     99  1.8.2.2  tsutsui 		drv_mask &= ~0x01;
    100  1.8.2.2  tsutsui 	if (st1 & WDCS_BSY)
    101  1.8.2.2  tsutsui 		drv_mask &= ~0x02;
    102  1.8.2.2  tsutsui 
    103  1.8.2.2  tsutsui  end:
    104  1.8.2.2  tsutsui 	return drv_mask;
    105  1.8.2.2  tsutsui }
    106  1.8.2.2  tsutsui 
    107  1.8.2.2  tsutsui /* Test to see controller with at last one attached drive is there.
    108  1.8.2.2  tsutsui  * Returns a bit for each possible drive found (0x01 for drive 0,
    109  1.8.2.2  tsutsui  * 0x02 for drive 1).
    110  1.8.2.2  tsutsui  * Logic:
    111  1.8.2.2  tsutsui  * - If a status register is at 0xff, assume there is no drive here
    112  1.8.2.2  tsutsui  *   (ISA has pull-up resistors).  Similarly if the status register has
    113  1.8.2.2  tsutsui  *   the value we last wrote to the bus (for IDE interfaces without pullups).
    114  1.8.2.2  tsutsui  *   If no drive at all -> return.
    115  1.8.2.2  tsutsui  * - reset the controller, wait for it to complete (may take up to 31s !).
    116  1.8.2.2  tsutsui  *   If timeout -> return.
    117  1.8.2.2  tsutsui  */
    118  1.8.2.2  tsutsui static int
    119  1.8.2.2  tsutsui wdcprobe(struct wdc_channel *chp)
    120  1.8.2.2  tsutsui {
    121  1.8.2.2  tsutsui 	uint8_t st0, st1, sc, sn, cl, ch;
    122  1.8.2.2  tsutsui 	uint8_t ret_value = 0x03;
    123  1.8.2.2  tsutsui 	uint8_t drive;
    124  1.8.2.2  tsutsui 	int found;
    125  1.8.2.2  tsutsui 
    126  1.8.2.2  tsutsui 	/*
    127  1.8.2.2  tsutsui 	 * Sanity check to see if the wdc channel responds at all.
    128  1.8.2.2  tsutsui 	 */
    129  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    130  1.8.2.2  tsutsui 	delay(10);
    131  1.8.2.2  tsutsui 	st0 = WDC_READ_REG(chp, wd_status);
    132  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10);
    133  1.8.2.2  tsutsui 	delay(10);
    134  1.8.2.2  tsutsui 	st1 = WDC_READ_REG(chp, wd_status);
    135  1.8.2.2  tsutsui 
    136  1.8.2.2  tsutsui 	if (st0 == 0xff || st0 == WDSD_IBM)
    137  1.8.2.2  tsutsui 		ret_value &= ~0x01;
    138  1.8.2.2  tsutsui 	if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
    139  1.8.2.2  tsutsui 		ret_value &= ~0x02;
    140  1.8.2.2  tsutsui 	if (ret_value == 0)
    141  1.8.2.2  tsutsui 		return ENXIO;
    142  1.8.2.2  tsutsui 
    143  1.8.2.2  tsutsui 	/* assert SRST, wait for reset to complete */
    144  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    145  1.8.2.2  tsutsui 	delay(10);
    146  1.8.2.2  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_RST | WDCTL_IDS);
    147  1.8.2.2  tsutsui 	delay(1000);
    148  1.8.2.2  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_IDS);
    149  1.8.2.2  tsutsui 	delay(1000);
    150  1.8.2.2  tsutsui 	(void) WDC_READ_REG(chp, wd_error);
    151  1.8.2.2  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_4BIT);
    152  1.8.2.2  tsutsui 	delay(10);
    153  1.8.2.2  tsutsui 
    154  1.8.2.2  tsutsui 	ret_value = __wdcwait_reset(chp, ret_value);
    155  1.8.2.2  tsutsui 
    156  1.8.2.2  tsutsui 	/* if reset failed, there's nothing here */
    157  1.8.2.2  tsutsui 	if (ret_value == 0)
    158  1.8.2.2  tsutsui 		return ENXIO;
    159  1.8.2.2  tsutsui 
    160  1.8.2.2  tsutsui 	/*
    161  1.8.2.2  tsutsui 	 * Test presence of drives. First test register signatures looking for
    162  1.8.2.2  tsutsui 	 * ATAPI devices. If it's not an ATAPI and reset said there may be
    163  1.8.2.2  tsutsui 	 * something here assume it's ATA or OLD. Ghost will be killed later in
    164  1.8.2.2  tsutsui 	 * attach routine.
    165  1.8.2.2  tsutsui 	 */
    166  1.8.2.2  tsutsui 	found = 0;
    167  1.8.2.2  tsutsui 	for (drive = 0; drive < 2; drive++) {
    168  1.8.2.2  tsutsui 		if ((ret_value & (0x01 << drive)) == 0)
    169  1.8.2.2  tsutsui 			continue;
    170  1.8.2.2  tsutsui 		return 0;
    171  1.8.2.2  tsutsui 	}
    172  1.8.2.2  tsutsui 	return ENXIO;
    173  1.8.2.2  tsutsui }
    174  1.8.2.2  tsutsui 
    175  1.8.2.2  tsutsui /*
    176  1.8.2.2  tsutsui  * Initialize the device.
    177  1.8.2.2  tsutsui  */
    178  1.8.2.2  tsutsui int
    179  1.8.2.2  tsutsui wdc_init(struct wd_softc *sc, u_int *unit)
    180  1.8.2.2  tsutsui {
    181  1.8.2.2  tsutsui 
    182  1.8.2.2  tsutsui 	if (pciide_init(&sc->sc_channel, unit) != 0)
    183  1.8.2.2  tsutsui 		return ENXIO;
    184  1.8.2.2  tsutsui 	if (wdcprobe(&sc->sc_channel) != 0)
    185  1.8.2.2  tsutsui 		return ENXIO;
    186  1.8.2.2  tsutsui 	return 0;
    187  1.8.2.2  tsutsui }
    188  1.8.2.2  tsutsui 
    189  1.8.2.2  tsutsui /*
    190  1.8.2.2  tsutsui  * Wait until the device is ready.
    191  1.8.2.2  tsutsui  */
    192  1.8.2.2  tsutsui int
    193  1.8.2.2  tsutsui wdc_wait_for_ready(struct wdc_channel *chp)
    194  1.8.2.2  tsutsui {
    195  1.8.2.2  tsutsui 	u_int timeout;
    196  1.8.2.2  tsutsui 
    197  1.8.2.2  tsutsui 	for (timeout = WDC_TIMEOUT; timeout > 0; --timeout) {
    198  1.8.2.2  tsutsui 		if ((WDC_READ_REG(chp, wd_status) & (WDCS_BSY | WDCS_DRDY))
    199  1.8.2.2  tsutsui 				== WDCS_DRDY)
    200  1.8.2.2  tsutsui 			return 0;
    201  1.8.2.2  tsutsui 	}
    202  1.8.2.2  tsutsui 	return ENXIO;
    203  1.8.2.2  tsutsui }
    204  1.8.2.2  tsutsui 
    205  1.8.2.2  tsutsui /*
    206  1.8.2.2  tsutsui  * Read one block off the device.
    207  1.8.2.2  tsutsui  */
    208  1.8.2.2  tsutsui int
    209  1.8.2.2  tsutsui wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c)
    210  1.8.2.2  tsutsui {
    211  1.8.2.2  tsutsui 	int i;
    212  1.8.2.2  tsutsui 	struct wdc_channel *chp = &sc->sc_channel;
    213  1.8.2.2  tsutsui 	uint16_t *ptr = (uint16_t *)wd_c->data;
    214  1.8.2.2  tsutsui 
    215  1.8.2.2  tsutsui 	if (ptr == NULL)
    216  1.8.2.2  tsutsui 		return 0;
    217  1.8.2.2  tsutsui 
    218  1.8.2.2  tsutsui 	for (i = wd_c->bcount; i > 0; i -= sizeof(uint16_t))
    219  1.8.2.2  tsutsui 		*ptr++ = WDC_READ_DATA(chp);
    220  1.8.2.2  tsutsui 
    221  1.8.2.2  tsutsui 	return 0;
    222  1.8.2.2  tsutsui }
    223  1.8.2.2  tsutsui 
    224  1.8.2.2  tsutsui /*
    225  1.8.2.2  tsutsui  * Send a command to the device (CHS and LBA addressing).
    226  1.8.2.2  tsutsui  */
    227  1.8.2.2  tsutsui int
    228  1.8.2.2  tsutsui wdccommand(struct wd_softc *sc, struct wdc_command *wd_c)
    229  1.8.2.2  tsutsui {
    230  1.8.2.2  tsutsui 	uint8_t err;
    231  1.8.2.2  tsutsui 	struct wdc_channel *chp = &sc->sc_channel;
    232  1.8.2.2  tsutsui 
    233  1.8.2.2  tsutsui #if 0
    234  1.8.2.2  tsutsui 	DPRINTF(("wdccommand(%d, %d, %d, %d, %d, %d, %d)\n",
    235  1.8.2.2  tsutsui 	    wd_c->drive, wd_c->r_command, wd_c->r_cyl,
    236  1.8.2.2  tsutsui 	    wd_c->r_head, wd_c->r_sector, wd_c->bcount,
    237  1.8.2.2  tsutsui 	    wd_c->r_precomp));
    238  1.8.2.2  tsutsui #endif
    239  1.8.2.2  tsutsui 
    240  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_precomp, wd_c->r_precomp);
    241  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    242  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sector, wd_c->r_sector);
    243  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_lo, wd_c->r_cyl);
    244  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_hi, wd_c->r_cyl >> 8);
    245  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sdh,
    246  1.8.2.2  tsutsui 	    WDSD_IBM | (wd_c->drive << 4) | wd_c->r_head);
    247  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    248  1.8.2.2  tsutsui 
    249  1.8.2.2  tsutsui 	if (wdc_wait_for_ready(chp) != 0)
    250  1.8.2.2  tsutsui 		return ENXIO;
    251  1.8.2.2  tsutsui 
    252  1.8.2.2  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    253  1.8.2.2  tsutsui 		printf("wd%d: error %x\n", chp->compatchan,
    254  1.8.2.2  tsutsui 		    WDC_READ_REG(chp, wd_error));
    255  1.8.2.2  tsutsui 		return ENXIO;
    256  1.8.2.2  tsutsui 	}
    257  1.8.2.2  tsutsui 
    258  1.8.2.2  tsutsui 	return 0;
    259  1.8.2.2  tsutsui }
    260  1.8.2.2  tsutsui 
    261  1.8.2.2  tsutsui /*
    262  1.8.2.2  tsutsui  * Send a command to the device (LBA48 addressing).
    263  1.8.2.2  tsutsui  */
    264  1.8.2.2  tsutsui int
    265  1.8.2.2  tsutsui wdccommandext(struct wd_softc *wd, struct wdc_command *wd_c)
    266  1.8.2.2  tsutsui {
    267  1.8.2.2  tsutsui 	uint8_t err;
    268  1.8.2.2  tsutsui 	struct wdc_channel *chp = &wd->sc_channel;
    269  1.8.2.2  tsutsui 
    270  1.8.2.2  tsutsui 	/* Select drive, head, and addressing mode. */
    271  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, (wd_c->drive << 4) | WDSD_LBA);
    272  1.8.2.2  tsutsui 
    273  1.8.2.2  tsutsui 	/* previous */
    274  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    275  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count >> 8);
    276  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 40);
    277  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 32);
    278  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno >> 24);
    279  1.8.2.2  tsutsui 
    280  1.8.2.2  tsutsui 	/* current */
    281  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    282  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    283  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 16);
    284  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 8);
    285  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno);
    286  1.8.2.2  tsutsui 
    287  1.8.2.2  tsutsui 	/* Send command. */
    288  1.8.2.2  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    289  1.8.2.2  tsutsui 
    290  1.8.2.2  tsutsui 	if (wdc_wait_for_ready(chp) != 0)
    291  1.8.2.2  tsutsui 		return ENXIO;
    292  1.8.2.2  tsutsui 
    293  1.8.2.2  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    294  1.8.2.2  tsutsui 		printf("wd%d: error %x\n", chp->compatchan,
    295  1.8.2.2  tsutsui 		    WDC_READ_REG(chp, wd_error));
    296  1.8.2.2  tsutsui 		return ENXIO;
    297  1.8.2.2  tsutsui 	}
    298  1.8.2.2  tsutsui 
    299  1.8.2.2  tsutsui 	return 0;
    300  1.8.2.2  tsutsui }
    301  1.8.2.2  tsutsui 
    302  1.8.2.2  tsutsui /*
    303  1.8.2.2  tsutsui  * Issue 'device identify' command.
    304  1.8.2.2  tsutsui  */
    305  1.8.2.2  tsutsui int
    306  1.8.2.2  tsutsui wdc_exec_identify(struct wd_softc *wd, void *data)
    307  1.8.2.2  tsutsui {
    308  1.8.2.2  tsutsui 	int error;
    309  1.8.2.2  tsutsui 	struct wdc_command wd_c;
    310  1.8.2.2  tsutsui 
    311  1.8.2.2  tsutsui 	memset(&wd_c, 0, sizeof(wd_c));
    312  1.8.2.2  tsutsui 
    313  1.8.2.2  tsutsui 	wd_c.drive = wd->sc_unit;
    314  1.8.2.2  tsutsui 	wd_c.r_command = WDCC_IDENTIFY;
    315  1.8.2.2  tsutsui 	wd_c.bcount = DEV_BSIZE;
    316  1.8.2.2  tsutsui 	wd_c.data = data;
    317  1.8.2.2  tsutsui 
    318  1.8.2.2  tsutsui 	if ((error = wdccommand(wd, &wd_c)) != 0)
    319  1.8.2.2  tsutsui 		return error;
    320  1.8.2.2  tsutsui 
    321  1.8.2.2  tsutsui 	return wdc_read_block(wd, &wd_c);
    322  1.8.2.2  tsutsui }
    323  1.8.2.2  tsutsui 
    324  1.8.2.2  tsutsui /*
    325  1.8.2.2  tsutsui  * Issue 'read' command.
    326  1.8.2.2  tsutsui  */
    327  1.8.2.2  tsutsui int
    328  1.8.2.2  tsutsui wdc_exec_read(struct wd_softc *wd, uint8_t cmd, daddr_t blkno, void *data)
    329  1.8.2.2  tsutsui {
    330  1.8.2.2  tsutsui 	int error;
    331  1.8.2.2  tsutsui 	struct wdc_command wd_c;
    332  1.8.2.2  tsutsui 
    333  1.8.2.2  tsutsui 	memset(&wd_c, 0, sizeof(wd_c));
    334  1.8.2.2  tsutsui 
    335  1.8.2.2  tsutsui 	if (wd->sc_flags & WDF_LBA48) {
    336  1.8.2.2  tsutsui 		/* LBA48 */
    337  1.8.2.2  tsutsui 		wd_c.r_blkno = blkno;
    338  1.8.2.2  tsutsui 	} else if (wd->sc_flags & WDF_LBA) {
    339  1.8.2.2  tsutsui 		/* LBA */
    340  1.8.2.2  tsutsui 		wd_c.r_sector = (blkno >> 0) & 0xff;
    341  1.8.2.2  tsutsui 		wd_c.r_cyl = (blkno >> 8) & 0xffff;
    342  1.8.2.2  tsutsui 		wd_c.r_head = (blkno >> 24) & 0x0f;
    343  1.8.2.2  tsutsui 		wd_c.r_head |= WDSD_LBA;
    344  1.8.2.2  tsutsui 	} else {
    345  1.8.2.2  tsutsui 		/* LHS */
    346  1.8.2.2  tsutsui 		wd_c.r_sector = blkno % wd->sc_label.d_nsectors;
    347  1.8.2.2  tsutsui 		wd_c.r_sector++;    /* Sectors begin with 1, not 0. */
    348  1.8.2.2  tsutsui 		blkno /= wd->sc_label.d_nsectors;
    349  1.8.2.2  tsutsui 		wd_c.r_head = blkno % wd->sc_label.d_ntracks;
    350  1.8.2.2  tsutsui 		blkno /= wd->sc_label.d_ntracks;
    351  1.8.2.2  tsutsui 		wd_c.r_cyl = blkno;
    352  1.8.2.2  tsutsui 		wd_c.r_head |= WDSD_CHS;
    353  1.8.2.2  tsutsui 	}
    354  1.8.2.2  tsutsui 
    355  1.8.2.2  tsutsui 	wd_c.data = data;
    356  1.8.2.2  tsutsui 	wd_c.r_count = 1;
    357  1.8.2.2  tsutsui 	wd_c.drive = wd->sc_unit;
    358  1.8.2.2  tsutsui 	wd_c.r_command = cmd;
    359  1.8.2.2  tsutsui 	wd_c.bcount = wd->sc_label.d_secsize;
    360  1.8.2.2  tsutsui 
    361  1.8.2.2  tsutsui 	if (wd->sc_flags & WDF_LBA48)
    362  1.8.2.2  tsutsui 		error = wdccommandext(wd, &wd_c);
    363  1.8.2.2  tsutsui 	else
    364  1.8.2.2  tsutsui 		error = wdccommand(wd, &wd_c);
    365  1.8.2.2  tsutsui 
    366  1.8.2.2  tsutsui 	if (error != 0)
    367  1.8.2.2  tsutsui 		return error;
    368  1.8.2.2  tsutsui 
    369  1.8.2.2  tsutsui 	return wdc_read_block(wd, &wd_c);
    370  1.8.2.2  tsutsui }
    371