biosdisk_ll.c revision 1.4 1 /* $NetBSD: biosdisk_ll.c,v 1.4 1998/07/23 09:59:44 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
6 * Copyright (c) 1996
7 * Perry E. Metzger. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgements:
19 * This product includes software developed for the NetBSD Project
20 * by Matthias Drochner.
21 * This product includes software developed for the NetBSD Project
22 * by Perry E. Metzger.
23 * 4. The names of the authors may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * shared by bootsector startup (bootsectmain) and biosdisk.c
40 * needs lowlevel parts from bios_disk.S
41 */
42
43 #include <lib/libsa/stand.h>
44
45 #include "biosdisk_ll.h"
46 #include "diskbuf.h"
47
48 extern int get_diskinfo __P((int));
49 extern int biosread __P((int, int, int, int, int, char *));
50
51 #define SPT(di) ((di)&0xff)
52 #define HEADS(di) ((((di)>>8)&0xff)+1)
53
54 #ifndef BIOSDISK_RETRIES
55 #define BIOSDISK_RETRIES 5
56 #endif
57
58 int
59 set_geometry(d)
60 struct biosdisk_ll *d;
61 {
62 int diskinfo;
63
64 diskinfo = get_diskinfo(d->dev);
65
66 d->spc = (d->spt = SPT(diskinfo)) * HEADS(diskinfo);
67
68 /*
69 * get_diskinfo assumes floppy if BIOS call fails. Check at least
70 * "valid" geometry.
71 */
72 return (!d->spc || !d->spt);
73 }
74
75 /*
76 * Global shared "diskbuf" is used as read ahead buffer. For reading from
77 * floppies, the bootstrap has to be loaded on a 64K boundary to ensure that
78 * this buffer doesn't cross a 64K DMA boundary.
79 */
80 #define RA_SECTORS (DISKBUFSIZE / BIOSDISK_SECSIZE)
81 static int ra_dev;
82 static int ra_end;
83 static int ra_first;
84
85 int
86 readsects(d, dblk, num, buf, cold) /* reads ahead if (!cold) */
87 struct biosdisk_ll *d;
88 int dblk, num;
89 char *buf;
90 int cold; /* don't use data segment or bss, don't call
91 * library functions */
92 {
93 while (num) {
94 int nsec;
95
96 /* check for usable data in read-ahead buffer */
97 if (cold || diskbuf_user != &ra_dev || d->dev != ra_dev
98 || dblk < ra_first || dblk >= ra_end) {
99
100 /* no, read from disk */
101 int cyl, head, sec;
102 char *trbuf;
103 int retries = BIOSDISK_RETRIES;
104
105 cyl = dblk / d->spc;
106 head = (dblk % d->spc) / d->spt;
107 sec = dblk % d->spt;
108 nsec = d->spt - sec;
109
110 if (cold) {
111 /* transfer directly to buffer */
112 trbuf = buf;
113 if (nsec > num)
114 nsec = num;
115 } else {
116 /* fill read-ahead buffer */
117 trbuf = diskbuf;
118 if (nsec > RA_SECTORS)
119 nsec = RA_SECTORS;
120
121 ra_dev = d->dev;
122 ra_first = dblk;
123 ra_end = dblk + nsec;
124 diskbuf_user = &ra_dev;
125 }
126
127 while (biosread(d->dev, cyl, head, sec, nsec, trbuf)) {
128 #ifdef DISK_DEBUG
129 if (!cold)
130 printf("read error C:%d H:%d S:%d-%d\n",
131 cyl, head, sec, sec + nsec - 1);
132 #endif
133 if (retries-- > 0)
134 continue;
135 if (!cold)
136 diskbuf_user = 0; /* mark invalid */
137 return (-1); /* XXX cannot output here if
138 * (cold) */
139 }
140 } else /* can take blocks from end of read-ahead
141 * buffer */
142 nsec = ra_end - dblk;
143
144 if (!cold) {
145 /* copy data from read-ahead to user buffer */
146 if (nsec > num)
147 nsec = num;
148 bcopy(diskbuf + (dblk - ra_first) * BIOSDISK_SECSIZE,
149 buf, nsec * BIOSDISK_SECSIZE);
150 }
151 buf += nsec * BIOSDISK_SECSIZE;
152 num -= nsec;
153 dblk += nsec;
154 }
155
156 return (0);
157 }
158