sd.c revision 1.18.2.3 1 1.18.2.2 mycroft /*
2 1.18.2.2 mycroft * Written by Julian Elischer (julian (at) dialix.oz.au)
3 1.8 deraadt * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 1.1 cgd *
5 1.1 cgd * TRW Financial Systems, in accordance with their agreement with Carnegie
6 1.1 cgd * Mellon University, makes this software available to CMU to distribute
7 1.1 cgd * or use in any manner that they see fit as long as this message is kept with
8 1.1 cgd * the software. For this reason TFS also grants any other persons or
9 1.1 cgd * organisations permission to use or modify this software.
10 1.1 cgd *
11 1.1 cgd * TFS supplies this software to be publicly redistributed
12 1.1 cgd * on the understanding that TFS is not responsible for the correct
13 1.1 cgd * functioning of this software in any circumstances.
14 1.9 cgd *
15 1.18.2.2 mycroft * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
16 1.18.2.2 mycroft *
17 1.18.2.3 mycroft * $Id: sd.c,v 1.18.2.3 1993/11/24 09:45:14 mycroft Exp $
18 1.1 cgd */
19 1.1 cgd
20 1.18.2.2 mycroft #define NSD 8 /* XXX kluge */
21 1.3 deraadt
22 1.18.2.2 mycroft #include <sys/types.h>
23 1.18.2.2 mycroft #include <sys/param.h>
24 1.18.2.2 mycroft #include <sys/dkbad.h>
25 1.18.2.2 mycroft #include <sys/systm.h>
26 1.18.2.2 mycroft #include <sys/conf.h>
27 1.18.2.2 mycroft #include <sys/file.h>
28 1.18.2.2 mycroft #include <sys/stat.h>
29 1.18.2.2 mycroft #include <sys/ioctl.h>
30 1.18.2.2 mycroft #include <sys/buf.h>
31 1.18.2.2 mycroft #include <sys/uio.h>
32 1.18.2.2 mycroft #include <sys/malloc.h>
33 1.18.2.2 mycroft #include <sys/errno.h>
34 1.18.2.2 mycroft #include <sys/device.h>
35 1.18.2.2 mycroft #include <sys/disklabel.h>
36 1.18.2.3 mycroft #include <sys/disk.h>
37 1.18.2.2 mycroft
38 1.18.2.2 mycroft #include <scsi/scsi_all.h>
39 1.18.2.2 mycroft #include <scsi/scsi_disk.h>
40 1.18.2.2 mycroft #include <scsi/scsiconf.h>
41 1.18.2.2 mycroft
42 1.18.2.2 mycroft #ifdef DDB
43 1.18.2.2 mycroft int Debugger();
44 1.18.2.2 mycroft #else /* DDB */
45 1.18.2.2 mycroft #define Debugger()
46 1.18.2.2 mycroft #endif /* DDB */
47 1.1 cgd
48 1.3 deraadt #define SECSIZE 512
49 1.1 cgd #define SDOUTSTANDING 2
50 1.1 cgd #define SDQSIZE 4
51 1.1 cgd #define SD_RETRIES 4
52 1.1 cgd
53 1.18.2.3 mycroft #define MAKESDDEV(maj, unit, part) (makedev(maj,(unit<<3)|part))
54 1.18.2.3 mycroft #define SDPART(z) (minor(z) & 7)
55 1.18.2.3 mycroft #define SDUNIT(z) (minor(z) >> 3)
56 1.1 cgd #define RAW_PART 3
57 1.1 cgd
58 1.18.2.2 mycroft int sdgetdisklabel __P((unsigned char unit));
59 1.18.2.2 mycroft int sd_get_parms __P((int unit, int flags));
60 1.18.2.2 mycroft void dstrategy();
61 1.18.2.2 mycroft void sdstart();
62 1.18.2.2 mycroft
63 1.18.2.2 mycroft struct scsi_device sd_switch =
64 1.18.2.2 mycroft {
65 1.18.2.2 mycroft NULL, /* Use default error handler */
66 1.18.2.2 mycroft sdstart, /* have a queue, served by this */
67 1.18.2.2 mycroft NULL, /* have no async handler */
68 1.18.2.2 mycroft NULL, /* Use default 'done' routine */
69 1.18.2.2 mycroft "sd",
70 1.18.2.3 mycroft 0
71 1.18.2.2 mycroft };
72 1.18.2.2 mycroft
73 1.18.2.2 mycroft int
74 1.18.2.2 mycroft sdnull()
75 1.18.2.2 mycroft {
76 1.18.2.2 mycroft printf("sdnull called ");
77 1.18.2.2 mycroft return 0;
78 1.18.2.2 mycroft }
79 1.18.2.2 mycroft void
80 1.18.2.2 mycroft sdnullattach()
81 1.18.2.2 mycroft {
82 1.18.2.2 mycroft printf("sdnullattach called ");
83 1.18.2.2 mycroft }
84 1.18.2.2 mycroft
85 1.18.2.2 mycroft struct cfdriver sdcd =
86 1.18.2.2 mycroft { NULL, "sd", sdnull, sdnullattach, DV_DISK, 0 };
87 1.18.2.2 mycroft
88 1.18.2.2 mycroft struct sd_data {
89 1.18.2.3 mycroft struct dkdevice sc_dk;
90 1.18.2.3 mycroft
91 1.18.2.2 mycroft u_int32 flags;
92 1.18.2.2 mycroft #define SDINIT 0x04 /* device has been init'd */
93 1.18.2.2 mycroft #define SDHAVELABEL 0x10 /* have read the label */
94 1.18.2.2 mycroft #define SDDOSPART 0x20 /* Have read the DOS partition table */
95 1.18.2.2 mycroft #define SDWRITEPROT 0x40 /* Device in readonly mode (S/W) */
96 1.18.2.2 mycroft struct scsi_link *sc_link; /* contains our targ, lun etc. */
97 1.18.2.2 mycroft u_int32 ad_info; /* info about the adapter */
98 1.18.2.2 mycroft u_int32 cmdscount; /* cmds allowed outstanding by board */
99 1.18.2.2 mycroft boolean wlabel; /* label is writable */
100 1.18.2.2 mycroft struct disk_parms {
101 1.18.2.2 mycroft u_char heads; /* Number of heads */
102 1.18.2.2 mycroft u_int16 cyls; /* Number of cylinders */
103 1.18.2.2 mycroft u_char sectors; /*dubious *//* Number of sectors/track */
104 1.18.2.2 mycroft u_int16 secsiz; /* Number of bytes/sector */
105 1.18.2.2 mycroft u_int32 disksize; /* total number sectors */
106 1.18.2.2 mycroft } params;
107 1.18.2.2 mycroft u_int32 partflags[MAXPARTITIONS]; /* per partition flags */
108 1.18.2.2 mycroft #define SDOPEN 0x01
109 1.18.2.2 mycroft u_int32 openparts; /* one bit for each open partition */
110 1.18.2.2 mycroft u_int32 sd_start_of_unix; /* unix vs dos partitions */
111 1.18.2.2 mycroft struct buf buf_queue;
112 1.18.2.2 mycroft u_int32 xfer_block_wait;
113 1.18.2.2 mycroft } *sd_data[NSD];
114 1.18.2.2 mycroft
115 1.18.2.2 mycroft static u_int32 next_sd_unit = 0;
116 1.1 cgd
117 1.3 deraadt /*
118 1.3 deraadt * The routine called by the low level scsi routine when it discovers
119 1.18.2.2 mycroft * a device suitable for this driver.
120 1.3 deraadt */
121 1.18.2.2 mycroft int
122 1.18.2.2 mycroft sdattach(sc_link)
123 1.18.2.2 mycroft struct scsi_link *sc_link;
124 1.1 cgd {
125 1.18.2.2 mycroft u_int32 unit;
126 1.3 deraadt struct sd_data *sd;
127 1.18.2.2 mycroft struct disk_parms *dp;
128 1.3 deraadt
129 1.18.2.2 mycroft unit = next_sd_unit++;
130 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
131 1.18.2.2 mycroft /*
132 1.18.2.2 mycroft * Check we have the resources for another drive
133 1.18.2.2 mycroft */
134 1.18.2.2 mycroft if (unit >= NSD) {
135 1.18.2.2 mycroft printf("Too many scsi disks..(%d > %d) reconfigure kernel\n",
136 1.18.2.2 mycroft (unit + 1), NSD);
137 1.8 deraadt return 0;
138 1.18.2.2 mycroft }
139 1.18.2.2 mycroft if (sd_data[unit]) {
140 1.18.2.2 mycroft printf("sd%d: unit already has storage allocated!\n", unit);
141 1.18.2.2 mycroft return 0;
142 1.18.2.2 mycroft }
143 1.18.2.2 mycroft sd = sd_data[unit] = malloc(sizeof(struct sd_data), M_DEVBUF, M_NOWAIT);
144 1.18.2.2 mycroft if (!sd) {
145 1.18.2.2 mycroft printf("malloc failed in sd.c\n");
146 1.18.2.3 mycroft return 0;
147 1.18.2.2 mycroft }
148 1.18.2.2 mycroft bzero(sd, sizeof(struct sd_data));
149 1.3 deraadt
150 1.3 deraadt dp = &(sd->params);
151 1.18.2.2 mycroft /*
152 1.18.2.2 mycroft * Store information needed to contact our base driver
153 1.18.2.2 mycroft */
154 1.18.2.2 mycroft sd->sc_link = sc_link;
155 1.18.2.2 mycroft sc_link->device = &sd_switch;
156 1.18.2.2 mycroft sc_link->dev_unit = unit;
157 1.18.2.2 mycroft
158 1.18.2.2 mycroft if (sd->sc_link->adapter->adapter_info) {
159 1.18.2.2 mycroft sd->ad_info = ((*(sd->sc_link->adapter->adapter_info)) (sc_link->adapter_softc));
160 1.18.2.2 mycroft sd->cmdscount = sd->ad_info & AD_INF_MAX_CMDS;
161 1.18.2.2 mycroft if (sd->cmdscount > SDOUTSTANDING) {
162 1.1 cgd sd->cmdscount = SDOUTSTANDING;
163 1.18.2.2 mycroft }
164 1.3 deraadt } else {
165 1.1 cgd sd->ad_info = 1;
166 1.18.2.2 mycroft sd->cmdscount = 1;
167 1.18.2.2 mycroft }
168 1.18.2.2 mycroft sc_link->opennings = sd->cmdscount;
169 1.3 deraadt /*
170 1.3 deraadt * Use the subdriver to request information regarding
171 1.3 deraadt * the drive. We cannot use interrupts yet, so the
172 1.3 deraadt * request must specify this.
173 1.3 deraadt */
174 1.18.2.2 mycroft sd_get_parms(unit, SCSI_NOSLEEP | SCSI_NOMASK);
175 1.18.2.3 mycroft printf("sd%d: %dMB (%d total sec), %d cyl, %d head, %d sec, %d bytes/sec\n",
176 1.18.2.2 mycroft unit,
177 1.18.2.2 mycroft dp->disksize / ((1024L * 1024L) / dp->secsiz),
178 1.18.2.2 mycroft dp->disksize,
179 1.18.2.2 mycroft dp->cyls,
180 1.18.2.2 mycroft dp->heads,
181 1.18.2.2 mycroft dp->sectors,
182 1.18.2.2 mycroft dp->secsiz);
183 1.1 cgd sd->flags |= SDINIT;
184 1.18.2.2 mycroft return 0;
185 1.1 cgd }
186 1.1 cgd
187 1.3 deraadt /*
188 1.18.2.2 mycroft * open the device. Make sure the partition info is a up-to-date as can be.
189 1.3 deraadt */
190 1.3 deraadt int
191 1.18.2.2 mycroft sdopen(dev)
192 1.18.2.2 mycroft int dev; /* XXX should be dev_t, but avoid promotion problems for now */
193 1.1 cgd {
194 1.18.2.2 mycroft int errcode = 0;
195 1.18.2.2 mycroft u_int32 unit, part;
196 1.3 deraadt struct sd_data *sd;
197 1.18.2.2 mycroft struct scsi_link *sc_link;
198 1.1 cgd
199 1.18.2.3 mycroft unit = SDUNIT(dev);
200 1.18.2.3 mycroft part = SDPART(dev);
201 1.18.2.3 mycroft
202 1.18.2.3 mycroft if (unit >= NSD)
203 1.18.2.3 mycroft return ENXIO;
204 1.18.2.2 mycroft sd = sd_data[unit];
205 1.18.2.2 mycroft /*
206 1.18.2.2 mycroft * Make sure the disk has been initialised
207 1.18.2.2 mycroft * At some point in the future, get the scsi driver
208 1.18.2.2 mycroft * to look for a new device if we are not initted
209 1.18.2.2 mycroft */
210 1.18.2.3 mycroft if ((!sd) || (!(sd->flags & SDINIT)))
211 1.18.2.3 mycroft return ENXIO;
212 1.18.2.3 mycroft
213 1.18.2.2 mycroft sc_link = sd->sc_link;
214 1.3 deraadt
215 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB1,
216 1.18.2.2 mycroft ("sdopen: dev=0x%x (unit %d (of %d),partition %d)\n"
217 1.18.2.2 mycroft ,dev, unit, NSD, part));
218 1.3 deraadt
219 1.3 deraadt /*
220 1.18.2.2 mycroft * "unit attention" errors should occur here if the
221 1.18.2.2 mycroft * drive has been restarted or the pack changed.
222 1.18.2.2 mycroft * just ingnore the result, it's a decoy instruction
223 1.18.2.2 mycroft * The error code will act on the error though
224 1.18.2.2 mycroft * and invalidate any media information we had.
225 1.3 deraadt */
226 1.18.2.2 mycroft scsi_test_unit_ready(sc_link, 0);
227 1.3 deraadt
228 1.3 deraadt /*
229 1.18.2.2 mycroft * If it's been invalidated, then forget the label
230 1.3 deraadt */
231 1.18.2.2 mycroft sc_link->flags |= SDEV_OPEN; /* unit attn becomes an err now */
232 1.18.2.2 mycroft if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
233 1.18.2.2 mycroft sd->flags &= ~SDHAVELABEL;
234 1.3 deraadt
235 1.18.2.2 mycroft /*
236 1.18.2.2 mycroft * If somebody still has it open, then forbid re-entry.
237 1.18.2.2 mycroft */
238 1.18.2.2 mycroft if (sd->openparts) {
239 1.18.2.2 mycroft errcode = ENXIO;
240 1.18.2.2 mycroft goto bad;
241 1.18.2.2 mycroft }
242 1.18.2.2 mycroft }
243 1.3 deraadt /*
244 1.18.2.2 mycroft * In case it is a funny one, tell it to start
245 1.18.2.2 mycroft * not needed for most hard drives (ignore failure)
246 1.3 deraadt */
247 1.18.2.2 mycroft scsi_start_unit(sc_link, SCSI_ERR_OK | SCSI_SILENT);
248 1.3 deraadt
249 1.3 deraadt /*
250 1.18.2.2 mycroft * Check that it is still responding and ok.
251 1.18.2.2 mycroft */
252 1.18.2.2 mycroft if (scsi_test_unit_ready(sc_link, 0)) {
253 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("device not reponding\n"));
254 1.18.2.2 mycroft errcode = ENXIO;
255 1.18.2.2 mycroft goto bad;
256 1.11 davidb }
257 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("device ok\n"));
258 1.3 deraadt
259 1.3 deraadt /*
260 1.3 deraadt * Load the physical device parameters
261 1.3 deraadt */
262 1.18.2.2 mycroft sd_get_parms(unit, 0); /* sets SDEV_MEDIA_LOADED */
263 1.18.2.2 mycroft if (sd->params.secsiz != SECSIZE) { /* XXX One day... */
264 1.3 deraadt printf("sd%d: Can't deal with %d bytes logical blocks\n",
265 1.18.2.2 mycroft unit, sd->params.secsiz);
266 1.18.2.2 mycroft Debugger();
267 1.18.2.2 mycroft errcode = ENXIO;
268 1.18.2.2 mycroft goto bad;
269 1.1 cgd }
270 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
271 1.18.2.2 mycroft
272 1.18.2.2 mycroft /* Lock the pack in. */
273 1.18.2.2 mycroft scsi_prevent(sc_link, PR_PREVENT, SCSI_ERR_OK | SCSI_SILENT);
274 1.3 deraadt
275 1.3 deraadt /*
276 1.18.2.2 mycroft * Load the partition info if not already loaded.
277 1.3 deraadt */
278 1.18.2.3 mycroft if ((errcode = sdgetdisklabel(unit)) && (part != RAW_PART))
279 1.18.2.2 mycroft goto bad;
280 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
281 1.3 deraadt /*
282 1.3 deraadt * Check the partition is legal
283 1.3 deraadt */
284 1.18.2.2 mycroft if (part >= MAXPARTITIONS) {
285 1.18.2.2 mycroft errcode = ENXIO;
286 1.18.2.2 mycroft goto bad;
287 1.1 cgd }
288 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("partition ok"));
289 1.3 deraadt
290 1.3 deraadt /*
291 1.3 deraadt * Check that the partition exists
292 1.3 deraadt */
293 1.18.2.3 mycroft if ((sd->sc_dk.dk_label.d_partitions[part].p_size == 0)
294 1.18.2.2 mycroft && (part != RAW_PART)) {
295 1.18.2.2 mycroft errcode = ENXIO;
296 1.18.2.2 mycroft goto bad;
297 1.1 cgd }
298 1.1 cgd sd->partflags[part] |= SDOPEN;
299 1.1 cgd sd->openparts |= (1 << part);
300 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("open\n"));
301 1.3 deraadt return 0;
302 1.1 cgd
303 1.18.2.2 mycroft bad:
304 1.18.2.2 mycroft if (!(sd->openparts)) {
305 1.18.2.2 mycroft scsi_prevent(sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
306 1.18.2.2 mycroft sc_link->flags &= ~SDEV_OPEN;
307 1.1 cgd }
308 1.18.2.2 mycroft return errcode;
309 1.1 cgd }
310 1.1 cgd
311 1.3 deraadt /*
312 1.18.2.2 mycroft * close the device.. only called if we are the LAST occurence of an open
313 1.18.2.2 mycroft * device. Convenient now but usually a pain.
314 1.3 deraadt */
315 1.18.2.2 mycroft int
316 1.18.2.2 mycroft sdclose(dev)
317 1.18.2.2 mycroft dev_t dev;
318 1.1 cgd {
319 1.18.2.2 mycroft unsigned char unit, part;
320 1.18.2.2 mycroft struct sd_data *sd;
321 1.18.2.2 mycroft
322 1.18.2.3 mycroft unit = SDUNIT(dev);
323 1.18.2.3 mycroft part = SDPART(dev);
324 1.18.2.2 mycroft sd = sd_data[unit];
325 1.18.2.2 mycroft sd->partflags[part] &= ~SDOPEN;
326 1.18.2.2 mycroft sd->openparts &= ~(1 << part);
327 1.18.2.2 mycroft scsi_prevent(sd->sc_link, PR_ALLOW, SCSI_SILENT | SCSI_ERR_OK);
328 1.18.2.2 mycroft if (!(sd->openparts))
329 1.18.2.2 mycroft sd->sc_link->flags &= ~SDEV_OPEN;
330 1.18.2.2 mycroft return 0;
331 1.1 cgd }
332 1.1 cgd
333 1.3 deraadt /*
334 1.3 deraadt * trim the size of the transfer if needed, called by physio
335 1.3 deraadt * basically the smaller of our max and the scsi driver's
336 1.3 deraadt * minphys (note we have no max)
337 1.18.2.2 mycroft *
338 1.18.2.2 mycroft * Trim buffer length if buffer-size is bigger than page size
339 1.3 deraadt */
340 1.18.2.2 mycroft void
341 1.18.2.2 mycroft sdminphys(bp)
342 1.18.2.2 mycroft struct buf *bp;
343 1.3 deraadt {
344 1.18.2.3 mycroft (*(sd_data[SDUNIT(bp->b_dev)]->sc_link->adapter->scsi_minphys)) (bp);
345 1.3 deraadt }
346 1.1 cgd
347 1.3 deraadt /*
348 1.18.2.2 mycroft * Actually translate the requested transfer into one the physical driver
349 1.18.2.2 mycroft * can understand. The transfer is described by a buf and will include
350 1.3 deraadt * only one physical transfer.
351 1.3 deraadt */
352 1.18.2.2 mycroft void
353 1.18.2.2 mycroft sdstrategy(bp)
354 1.18.2.2 mycroft struct buf *bp;
355 1.1 cgd {
356 1.18.2.2 mycroft struct buf *dp;
357 1.18.2.2 mycroft u_int32 opri;
358 1.3 deraadt struct sd_data *sd;
359 1.18.2.2 mycroft u_int32 unit;
360 1.1 cgd
361 1.18.2.3 mycroft unit = SDUNIT((bp->b_dev));
362 1.3 deraadt sd = sd_data[unit];
363 1.18.2.2 mycroft SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
364 1.18.2.2 mycroft SC_DEBUG(sd->sc_link, SDEV_DB1,
365 1.18.2.2 mycroft (" %d bytes @ blk%d\n", bp->b_bcount, bp->b_blkno));
366 1.1 cgd sdminphys(bp);
367 1.18.2.2 mycroft /*
368 1.18.2.2 mycroft * If the device has been made invalid, error out
369 1.18.2.2 mycroft */
370 1.18.2.2 mycroft if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED)) {
371 1.18.2.2 mycroft sd->flags &= ~SDHAVELABEL;
372 1.1 cgd bp->b_error = EIO;
373 1.1 cgd goto bad;
374 1.1 cgd }
375 1.18.2.2 mycroft /*
376 1.18.2.2 mycroft * "soft" write protect check
377 1.18.2.2 mycroft */
378 1.1 cgd if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
379 1.1 cgd bp->b_error = EROFS;
380 1.1 cgd goto bad;
381 1.1 cgd }
382 1.18.2.2 mycroft /*
383 1.18.2.2 mycroft * If it's a null transfer, return immediatly
384 1.18.2.2 mycroft */
385 1.18.2.2 mycroft if (bp->b_bcount == 0) {
386 1.1 cgd goto done;
387 1.18.2.2 mycroft }
388 1.3 deraadt /*
389 1.3 deraadt * Decide which unit and partition we are talking about
390 1.3 deraadt * only raw is ok if no label
391 1.3 deraadt */
392 1.18.2.3 mycroft if (SDPART(bp->b_dev) != RAW_PART) {
393 1.3 deraadt if (!(sd->flags & SDHAVELABEL)) {
394 1.1 cgd bp->b_error = EIO;
395 1.1 cgd goto bad;
396 1.1 cgd }
397 1.1 cgd /*
398 1.1 cgd * do bounds checking, adjust transfer. if error, process.
399 1.1 cgd * if end of partition, just return
400 1.1 cgd */
401 1.18.2.3 mycroft if (bounds_check_with_label(bp, &sd->sc_dk.dk_label, sd->wlabel) <= 0)
402 1.1 cgd goto done;
403 1.1 cgd /* otherwise, process transfer request */
404 1.1 cgd }
405 1.18.2.2 mycroft opri = splbio();
406 1.18.2.2 mycroft dp = &sd->buf_queue;
407 1.1 cgd
408 1.18.2.2 mycroft /*
409 1.18.2.2 mycroft * Place it in the queue of disk activities for this disk
410 1.18.2.2 mycroft */
411 1.1 cgd disksort(dp, bp);
412 1.1 cgd
413 1.3 deraadt /*
414 1.3 deraadt * Tell the device to get going on the transfer if it's
415 1.3 deraadt * not doing anything, otherwise just wait for completion
416 1.3 deraadt */
417 1.1 cgd sdstart(unit);
418 1.1 cgd
419 1.1 cgd splx(opri);
420 1.1 cgd bad:
421 1.1 cgd bp->b_flags |= B_ERROR;
422 1.1 cgd done:
423 1.18.2.2 mycroft
424 1.18.2.2 mycroft /*
425 1.18.2.2 mycroft * Correctly set the buf to indicate a completed xfer
426 1.18.2.2 mycroft */
427 1.18.2.2 mycroft bp->b_resid = bp->b_bcount;
428 1.1 cgd biodone(bp);
429 1.1 cgd }
430 1.1 cgd
431 1.3 deraadt /*
432 1.3 deraadt * sdstart looks to see if there is a buf waiting for the device
433 1.3 deraadt * and that the device is not already busy. If both are true,
434 1.18.2.2 mycroft * It dequeues the buf and creates a scsi command to perform the
435 1.18.2.2 mycroft * transfer in the buf. The transfer request will call scsi_done
436 1.3 deraadt * on completion, which will in turn call this routine again
437 1.3 deraadt * so that the next queued transfer is performed.
438 1.3 deraadt * The bufs are queued by the strategy routine (sdstrategy)
439 1.18.2.2 mycroft *
440 1.3 deraadt * This routine is also called after other non-queued requests
441 1.3 deraadt * have been made of the scsi driver, to ensure that the queue
442 1.3 deraadt * continues to be drained.
443 1.18.2.2 mycroft *
444 1.3 deraadt * must be called at the correct (highish) spl level
445 1.18.2.2 mycroft * sdstart() is called at splbio from sdstrategy and scsi_done
446 1.3 deraadt */
447 1.18.2.2 mycroft void
448 1.18.2.2 mycroft sdstart(unit)
449 1.18.2.2 mycroft u_int32 unit;
450 1.18.2.2 mycroft {
451 1.18.2.2 mycroft register struct sd_data *sd = sd_data[unit];
452 1.18.2.2 mycroft register struct scsi_link *sc_link = sd->sc_link;
453 1.18.2.2 mycroft struct buf *bp = 0;
454 1.18.2.2 mycroft struct buf *dp;
455 1.3 deraadt struct scsi_rw_big cmd;
456 1.18.2.2 mycroft u_int32 blkno, nblk;
457 1.3 deraadt struct partition *p;
458 1.3 deraadt
459 1.18.2.2 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
460 1.3 deraadt /*
461 1.18.2.2 mycroft * Check if the device has room for another command
462 1.3 deraadt */
463 1.18.2.2 mycroft while (sc_link->opennings) {
464 1.1 cgd
465 1.18.2.2 mycroft /*
466 1.18.2.2 mycroft * there is excess capacity, but a special waits
467 1.18.2.2 mycroft * It'll need the adapter as soon as we clear out of the
468 1.18.2.2 mycroft * way and let it run (user level wait).
469 1.18.2.2 mycroft */
470 1.18.2.2 mycroft if (sc_link->flags & SDEV_WAITING) {
471 1.18.2.2 mycroft return;
472 1.18.2.2 mycroft }
473 1.18.2.2 mycroft /*
474 1.18.2.2 mycroft * See if there is a buf with work for us to do..
475 1.18.2.2 mycroft */
476 1.18.2.2 mycroft dp = &sd->buf_queue;
477 1.18.2.2 mycroft if ((bp = dp->b_actf) == NULL) { /* yes, an assign */
478 1.18.2.2 mycroft return;
479 1.18.2.2 mycroft }
480 1.1 cgd dp->b_actf = bp->av_forw;
481 1.1 cgd
482 1.18.2.2 mycroft /*
483 1.18.2.2 mycroft * If the device has become invalid, abort all the
484 1.18.2.2 mycroft * reads and writes until all files have been closed and
485 1.18.2.2 mycroft * re-openned
486 1.18.2.2 mycroft */
487 1.18.2.2 mycroft if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
488 1.18.2.2 mycroft sd->flags &= ~SDHAVELABEL;
489 1.18.2.2 mycroft goto bad;
490 1.18.2.2 mycroft }
491 1.18.2.2 mycroft /*
492 1.18.2.2 mycroft * We have a buf, now we know we are going to go through
493 1.18.2.2 mycroft * With this thing..
494 1.18.2.2 mycroft *
495 1.18.2.2 mycroft * First, translate the block to absolute
496 1.18.2.2 mycroft */
497 1.18.2.3 mycroft p = sd->sc_dk.dk_label.d_partitions + SDPART(bp->b_dev);
498 1.18.2.2 mycroft blkno = bp->b_blkno + p->p_offset;
499 1.18.2.2 mycroft nblk = (bp->b_bcount + 511) >> 9;
500 1.1 cgd
501 1.18.2.2 mycroft /*
502 1.18.2.2 mycroft * Fill out the scsi command
503 1.18.2.2 mycroft */
504 1.18.2.2 mycroft bzero(&cmd, sizeof(cmd));
505 1.18.2.2 mycroft cmd.op_code = (bp->b_flags & B_READ)
506 1.18.2.2 mycroft ? READ_BIG : WRITE_BIG;
507 1.18.2.2 mycroft cmd.addr_3 = (blkno & 0xff000000) >> 24;
508 1.18.2.2 mycroft cmd.addr_2 = (blkno & 0xff0000) >> 16;
509 1.18.2.2 mycroft cmd.addr_1 = (blkno & 0xff00) >> 8;
510 1.18.2.2 mycroft cmd.addr_0 = blkno & 0xff;
511 1.18.2.2 mycroft cmd.length2 = (nblk & 0xff00) >> 8;
512 1.18.2.2 mycroft cmd.length1 = (nblk & 0xff);
513 1.18.2.2 mycroft /*
514 1.18.2.2 mycroft * Call the routine that chats with the adapter.
515 1.18.2.2 mycroft * Note: we cannot sleep as we may be an interrupt
516 1.18.2.2 mycroft */
517 1.18.2.2 mycroft if (scsi_scsi_cmd(sc_link,
518 1.18.2.2 mycroft (struct scsi_generic *) &cmd,
519 1.18.2.2 mycroft sizeof(cmd),
520 1.18.2.2 mycroft (u_char *) bp->b_un.b_addr,
521 1.18.2.2 mycroft bp->b_bcount,
522 1.18.2.2 mycroft SD_RETRIES,
523 1.18.2.2 mycroft 10000,
524 1.18.2.2 mycroft bp,
525 1.18.2.2 mycroft SCSI_NOSLEEP | ((bp->b_flags & B_READ) ?
526 1.18.2.2 mycroft SCSI_DATA_IN : SCSI_DATA_OUT))
527 1.18.2.2 mycroft == SUCCESSFULLY_QUEUED) {
528 1.18.2.2 mycroft } else {
529 1.18.2.2 mycroft bad:
530 1.18.2.2 mycroft printf("sd%d: oops not queued", unit);
531 1.1 cgd bp->b_error = EIO;
532 1.18.2.2 mycroft bp->b_flags |= B_ERROR;
533 1.18.2.2 mycroft biodone(bp);
534 1.3 deraadt }
535 1.18.2.2 mycroft }
536 1.1 cgd }
537 1.3 deraadt
538 1.3 deraadt /*
539 1.3 deraadt * Perform special action on behalf of the user
540 1.3 deraadt * Knows about the internals of this device
541 1.3 deraadt */
542 1.18.2.2 mycroft int
543 1.1 cgd sdioctl(dev_t dev, int cmd, caddr_t addr, int flag)
544 1.1 cgd {
545 1.18.2.2 mycroft /* struct sd_cmd_buf *args; */
546 1.18.2.2 mycroft int error = 0;
547 1.3 deraadt unsigned char unit, part;
548 1.18.2.2 mycroft register struct sd_data *sd;
549 1.1 cgd
550 1.18.2.2 mycroft /*
551 1.18.2.2 mycroft * Find the device that the user is talking about
552 1.18.2.2 mycroft */
553 1.18.2.3 mycroft unit = SDUNIT(dev);
554 1.18.2.3 mycroft part = SDPART(dev);
555 1.4 deraadt sd = sd_data[unit];
556 1.18.2.2 mycroft SC_DEBUG(sd->sc_link, SDEV_DB1, ("sdioctl (0x%x)", cmd));
557 1.3 deraadt
558 1.18.2.2 mycroft /*
559 1.18.2.2 mycroft * If the device is not valid.. abandon ship
560 1.18.2.2 mycroft */
561 1.18.2.2 mycroft if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED))
562 1.18.2.3 mycroft return EIO;
563 1.18.2.2 mycroft switch (cmd) {
564 1.3 deraadt
565 1.1 cgd case DIOCSBAD:
566 1.3 deraadt error = EINVAL;
567 1.1 cgd break;
568 1.18.2.2 mycroft
569 1.1 cgd case DIOCGDINFO:
570 1.18.2.3 mycroft *(struct disklabel *) addr = sd->sc_dk.dk_label;
571 1.1 cgd break;
572 1.18.2.2 mycroft
573 1.3 deraadt case DIOCGPART:
574 1.18.2.3 mycroft ((struct partinfo *) addr)->disklab = &sd->sc_dk.dk_label;
575 1.18.2.2 mycroft ((struct partinfo *) addr)->part =
576 1.18.2.3 mycroft &sd->sc_dk.dk_label.d_partitions[SDPART(dev)];
577 1.3 deraadt break;
578 1.18.2.2 mycroft
579 1.3 deraadt case DIOCSDINFO:
580 1.3 deraadt if ((flag & FWRITE) == 0)
581 1.3 deraadt error = EBADF;
582 1.18.2.2 mycroft else
583 1.18.2.3 mycroft error = setdisklabel(&sd->sc_dk.dk_label,
584 1.18.2.2 mycroft (struct disklabel *)addr,
585 1.18.2.2 mycroft /*(sd->flags & DKFL_BSDLABEL) ? sd->openparts : */ 0,
586 1.18.2.3 mycroft &sd->sc_dk.dk_cpulabel);
587 1.18.2.3 mycroft if (error == 0)
588 1.1 cgd sd->flags |= SDHAVELABEL;
589 1.3 deraadt break;
590 1.18.2.2 mycroft
591 1.3 deraadt case DIOCWLABEL:
592 1.1 cgd sd->flags &= ~SDWRITEPROT;
593 1.3 deraadt if ((flag & FWRITE) == 0)
594 1.3 deraadt error = EBADF;
595 1.3 deraadt else
596 1.18.2.2 mycroft sd->wlabel = *(boolean *) addr;
597 1.3 deraadt break;
598 1.18.2.2 mycroft
599 1.3 deraadt case DIOCWDINFO:
600 1.1 cgd sd->flags &= ~SDWRITEPROT;
601 1.3 deraadt if ((flag & FWRITE) == 0)
602 1.3 deraadt error = EBADF;
603 1.3 deraadt else {
604 1.18.2.3 mycroft error = setdisklabel(&sd->sc_dk.dk_label,
605 1.3 deraadt (struct disklabel *)addr,
606 1.18.2.2 mycroft /*(sd->flags & SDHAVELABEL) ? sd->openparts : */ 0,
607 1.18.2.3 mycroft &sd->sc_dk.dk_cpulabel);
608 1.18.2.2 mycroft if (!error) {
609 1.18.2.2 mycroft boolean wlab;
610 1.1 cgd
611 1.18.2.2 mycroft /* ok - write will succeed */
612 1.18.2.2 mycroft sd->flags |= SDHAVELABEL;
613 1.1 cgd
614 1.3 deraadt /* simulate opening partition 0 so write succeeds */
615 1.18.2.2 mycroft sd->openparts |= (1 << 0); /* XXX */
616 1.3 deraadt wlab = sd->wlabel;
617 1.3 deraadt sd->wlabel = 1;
618 1.3 deraadt error = writedisklabel(dev, sdstrategy,
619 1.18.2.3 mycroft &sd->sc_dk.dk_label,
620 1.18.2.3 mycroft &sd->sc_dk.dk_cpulabel);
621 1.3 deraadt sd->wlabel = wlab;
622 1.3 deraadt }
623 1.18.2.2 mycroft }
624 1.3 deraadt break;
625 1.18.2.2 mycroft
626 1.1 cgd default:
627 1.18.2.2 mycroft if (part == RAW_PART)
628 1.18.2.2 mycroft error = scsi_do_ioctl(sd->sc_link, cmd, addr, flag);
629 1.18.2.2 mycroft else
630 1.18.2.2 mycroft error = ENOTTY;
631 1.1 cgd break;
632 1.1 cgd }
633 1.3 deraadt return error;
634 1.1 cgd }
635 1.1 cgd
636 1.3 deraadt /*
637 1.3 deraadt * Load the label information on the named device
638 1.3 deraadt */
639 1.18.2.2 mycroft int
640 1.18.2.2 mycroft sdgetdisklabel(unsigned char unit)
641 1.1 cgd {
642 1.18.2.2 mycroft char *errstring;
643 1.3 deraadt struct sd_data *sd = sd_data[unit];
644 1.18.2.2 mycroft
645 1.18.2.2 mycroft /*
646 1.18.2.2 mycroft * If the inflo is already loaded, use it
647 1.18.2.2 mycroft */
648 1.18.2.2 mycroft if (sd->flags & SDHAVELABEL)
649 1.18.2.3 mycroft return 0;
650 1.3 deraadt
651 1.18.2.3 mycroft bzero(&sd->sc_dk.dk_label, sizeof(struct disklabel));
652 1.3 deraadt /*
653 1.18.2.2 mycroft * make partition 3 the whole disk in case of failure then get pdinfo
654 1.18.2.2 mycroft * for historical reasons, make part a same as raw part
655 1.3 deraadt */
656 1.18.2.3 mycroft sd->sc_dk.dk_label.d_partitions[0].p_offset = 0;
657 1.18.2.3 mycroft sd->sc_dk.dk_label.d_partitions[0].p_size = sd->params.disksize;
658 1.18.2.3 mycroft sd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
659 1.18.2.3 mycroft sd->sc_dk.dk_label.d_partitions[RAW_PART].p_size = sd->params.disksize;
660 1.18.2.3 mycroft sd->sc_dk.dk_label.d_npartitions = MAXPARTITIONS;
661 1.18.2.3 mycroft sd->sc_dk.dk_label.d_secsize = SECSIZE; /* as long as it's not 0 */
662 1.18.2.3 mycroft sd->sc_dk.dk_label.d_ntracks = sd->params.heads;
663 1.18.2.3 mycroft sd->sc_dk.dk_label.d_nsectors = sd->params.sectors;
664 1.18.2.3 mycroft sd->sc_dk.dk_label.d_ncylinders = sd->params.cyls;
665 1.18.2.3 mycroft sd->sc_dk.dk_label.d_secpercyl = sd->params.heads * sd->params.sectors;
666 1.18.2.3 mycroft if (sd->sc_dk.dk_label.d_secpercyl == 0) {
667 1.18.2.3 mycroft sd->sc_dk.dk_label.d_secpercyl = 100;
668 1.18.2.2 mycroft /* as long as it's not 0 - readdisklabel divides by it (?) */
669 1.1 cgd }
670 1.18.2.2 mycroft /*
671 1.18.2.2 mycroft * Call the generic disklabel extraction routine
672 1.18.2.2 mycroft */
673 1.18.2.3 mycroft if (errstring = readdisklabel(MAKESDDEV(0, unit, RAW_PART),
674 1.18.2.2 mycroft sdstrategy,
675 1.18.2.3 mycroft &sd->sc_dk.dk_label,
676 1.18.2.3 mycroft &sd->sc_dk.dk_cpulabel)) {
677 1.18.2.2 mycroft printf("sd%d: %s\n", unit, errstring);
678 1.3 deraadt return ENXIO;
679 1.1 cgd }
680 1.3 deraadt sd->flags |= SDHAVELABEL; /* WE HAVE IT ALL NOW */
681 1.18.2.2 mycroft return 0;
682 1.1 cgd }
683 1.1 cgd
684 1.3 deraadt /*
685 1.3 deraadt * Find out from the device what it's capacity is
686 1.3 deraadt */
687 1.18.2.2 mycroft u_int32
688 1.18.2.2 mycroft sd_size(unit, flags)
689 1.18.2.2 mycroft int unit, flags;
690 1.1 cgd {
691 1.1 cgd struct scsi_read_cap_data rdcap;
692 1.1 cgd struct scsi_read_capacity scsi_cmd;
693 1.18.2.2 mycroft u_int32 size;
694 1.1 cgd
695 1.3 deraadt /*
696 1.3 deraadt * make up a scsi command and ask the scsi driver to do
697 1.3 deraadt * it for you.
698 1.3 deraadt */
699 1.1 cgd bzero(&scsi_cmd, sizeof(scsi_cmd));
700 1.1 cgd scsi_cmd.op_code = READ_CAPACITY;
701 1.1 cgd
702 1.3 deraadt /*
703 1.3 deraadt * If the command works, interpret the result as a 4 byte
704 1.3 deraadt * number of blocks
705 1.3 deraadt */
706 1.18.2.2 mycroft if (scsi_scsi_cmd(sd_data[unit]->sc_link,
707 1.18.2.2 mycroft (struct scsi_generic *) &scsi_cmd,
708 1.18.2.2 mycroft sizeof(scsi_cmd),
709 1.18.2.2 mycroft (u_char *) & rdcap,
710 1.18.2.2 mycroft sizeof(rdcap),
711 1.18.2.2 mycroft SD_RETRIES,
712 1.18.2.2 mycroft 2000,
713 1.18.2.2 mycroft NULL,
714 1.18.2.2 mycroft flags | SCSI_DATA_IN) != 0) {
715 1.18.2.2 mycroft printf("sd%d: could not get size\n", unit);
716 1.18.2.3 mycroft return 0;
717 1.1 cgd } else {
718 1.18.2.2 mycroft size = rdcap.addr_0 + 1;
719 1.1 cgd size += rdcap.addr_1 << 8;
720 1.1 cgd size += rdcap.addr_2 << 16;
721 1.1 cgd size += rdcap.addr_3 << 24;
722 1.1 cgd }
723 1.18.2.3 mycroft return size;
724 1.3 deraadt }
725 1.3 deraadt
726 1.3 deraadt /*
727 1.3 deraadt * Tell the device to map out a defective block
728 1.3 deraadt */
729 1.3 deraadt int
730 1.18.2.2 mycroft sd_reassign_blocks(unit, block)
731 1.18.2.2 mycroft int unit, block;
732 1.1 cgd {
733 1.3 deraadt struct scsi_reassign_blocks scsi_cmd;
734 1.18.2.2 mycroft struct scsi_reassign_blocks_data rbdata;
735 1.1 cgd
736 1.1 cgd bzero(&scsi_cmd, sizeof(scsi_cmd));
737 1.1 cgd bzero(&rbdata, sizeof(rbdata));
738 1.1 cgd scsi_cmd.op_code = REASSIGN_BLOCKS;
739 1.1 cgd
740 1.1 cgd rbdata.length_msb = 0;
741 1.1 cgd rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
742 1.1 cgd rbdata.defect_descriptor[0].dlbaddr_3 = ((block >> 24) & 0xff);
743 1.1 cgd rbdata.defect_descriptor[0].dlbaddr_2 = ((block >> 16) & 0xff);
744 1.18.2.2 mycroft rbdata.defect_descriptor[0].dlbaddr_1 = ((block >> 8) & 0xff);
745 1.18.2.2 mycroft rbdata.defect_descriptor[0].dlbaddr_0 = ((block) & 0xff);
746 1.1 cgd
747 1.18.2.3 mycroft return scsi_scsi_cmd(sd_data[unit]->sc_link,
748 1.18.2.2 mycroft (struct scsi_generic *) &scsi_cmd,
749 1.18.2.2 mycroft sizeof(scsi_cmd),
750 1.18.2.2 mycroft (u_char *) & rbdata,
751 1.18.2.2 mycroft sizeof(rbdata),
752 1.18.2.2 mycroft SD_RETRIES,
753 1.18.2.2 mycroft 5000,
754 1.18.2.2 mycroft NULL,
755 1.18.2.3 mycroft SCSI_DATA_OUT);
756 1.1 cgd }
757 1.1 cgd #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
758 1.1 cgd
759 1.3 deraadt /*
760 1.3 deraadt * Get the scsi driver to send a full inquiry to the
761 1.18.2.2 mycroft * device and use the results to fill out the disk
762 1.3 deraadt * parameter structure.
763 1.3 deraadt */
764 1.18.2.2 mycroft int
765 1.18.2.2 mycroft sd_get_parms(unit, flags)
766 1.18.2.2 mycroft int unit, flags;
767 1.1 cgd {
768 1.3 deraadt struct sd_data *sd = sd_data[unit];
769 1.1 cgd struct disk_parms *disk_parms = &sd->params;
770 1.18.2.2 mycroft struct scsi_mode_sense scsi_cmd;
771 1.3 deraadt struct scsi_mode_sense_data {
772 1.18.2.2 mycroft struct scsi_mode_header header;
773 1.18.2.2 mycroft struct blk_desc blk_desc;
774 1.18.2.2 mycroft union disk_pages pages;
775 1.3 deraadt } scsi_sense;
776 1.18.2.2 mycroft u_int32 sectors;
777 1.1 cgd
778 1.18.2.2 mycroft /*
779 1.18.2.2 mycroft * First check if we have it all loaded
780 1.18.2.2 mycroft */
781 1.18.2.2 mycroft if (sd->flags & SDEV_MEDIA_LOADED)
782 1.3 deraadt return 0;
783 1.3 deraadt
784 1.18.2.2 mycroft /*
785 1.18.2.2 mycroft * do a "mode sense page 4"
786 1.18.2.2 mycroft */
787 1.1 cgd bzero(&scsi_cmd, sizeof(scsi_cmd));
788 1.1 cgd scsi_cmd.op_code = MODE_SENSE;
789 1.18.2.2 mycroft scsi_cmd.page = 4;
790 1.1 cgd scsi_cmd.length = 0x20;
791 1.3 deraadt /*
792 1.3 deraadt * If the command worked, use the results to fill out
793 1.3 deraadt * the parameter structure
794 1.3 deraadt */
795 1.18.2.2 mycroft if (scsi_scsi_cmd(sd->sc_link,
796 1.18.2.2 mycroft (struct scsi_generic *) &scsi_cmd,
797 1.18.2.2 mycroft sizeof(scsi_cmd),
798 1.18.2.2 mycroft (u_char *) & scsi_sense,
799 1.18.2.2 mycroft sizeof(scsi_sense),
800 1.18.2.2 mycroft SD_RETRIES,
801 1.18.2.2 mycroft 2000,
802 1.18.2.2 mycroft NULL,
803 1.18.2.2 mycroft flags | SCSI_DATA_IN) != 0) {
804 1.18.2.2 mycroft
805 1.18.2.2 mycroft printf("sd%d could not mode sense (4).", unit);
806 1.18.2.2 mycroft printf(" Using ficticious geometry\n");
807 1.18.2.2 mycroft /*
808 1.18.2.2 mycroft * use adaptec standard ficticious geometry
809 1.18.2.2 mycroft * this depends on which controller (e.g. 1542C is
810 1.18.2.2 mycroft * different. but we have to put SOMETHING here..)
811 1.18.2.2 mycroft */
812 1.1 cgd sectors = sd_size(unit, flags);
813 1.1 cgd disk_parms->heads = 64;
814 1.1 cgd disk_parms->sectors = 32;
815 1.18.2.2 mycroft disk_parms->cyls = sectors / (64 * 32);
816 1.1 cgd disk_parms->secsiz = SECSIZE;
817 1.18.2.2 mycroft disk_parms->disksize = sectors;
818 1.3 deraadt } else {
819 1.18.2.2 mycroft
820 1.18.2.2 mycroft SC_DEBUG(sd->sc_link, SDEV_DB3,
821 1.18.2.2 mycroft ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
822 1.18.2.2 mycroft _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2),
823 1.1 cgd scsi_sense.pages.rigid_geometry.nheads,
824 1.1 cgd b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
825 1.1 cgd b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
826 1.18.2.2 mycroft b2tol(scsi_sense.pages.rigid_geometry.land_zone)));
827 1.1 cgd
828 1.3 deraadt /*
829 1.3 deraadt * KLUDGE!!(for zone recorded disks)
830 1.3 deraadt * give a number of sectors so that sec * trks * cyls
831 1.18.2.2 mycroft * is <= disk_size
832 1.18.2.2 mycroft * can lead to wasted space! THINK ABOUT THIS !
833 1.3 deraadt */
834 1.1 cgd disk_parms->heads = scsi_sense.pages.rigid_geometry.nheads;
835 1.18.2.2 mycroft disk_parms->cyls = _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2);
836 1.18.2.2 mycroft disk_parms->secsiz = _3btol(scsi_sense.blk_desc.blklen);
837 1.1 cgd
838 1.1 cgd sectors = sd_size(unit, flags);
839 1.18.2.2 mycroft disk_parms->disksize = sectors;
840 1.18.2.2 mycroft sectors /= (disk_parms->heads * disk_parms->cyls);
841 1.18.2.2 mycroft disk_parms->sectors = sectors; /* dubious on SCSI *//*XXX */
842 1.1 cgd }
843 1.18.2.2 mycroft sd->sc_link->flags |= SDEV_MEDIA_LOADED;
844 1.3 deraadt return 0;
845 1.1 cgd }
846 1.1 cgd
847 1.3 deraadt int
848 1.18.2.2 mycroft sdsize(dev_t dev)
849 1.1 cgd {
850 1.18.2.3 mycroft int unit = SDUNIT(dev), part = SDPART(dev), val;
851 1.3 deraadt struct sd_data *sd;
852 1.1 cgd
853 1.18.2.2 mycroft if (unit >= NSD)
854 1.18.2.2 mycroft return -1;
855 1.18.2.2 mycroft
856 1.3 deraadt sd = sd_data[unit];
857 1.18.2.2 mycroft if (!sd)
858 1.18.2.2 mycroft return -1;
859 1.18.2.2 mycroft if ((sd->flags & SDINIT) == 0)
860 1.18.2.2 mycroft return -1;
861 1.18.2.2 mycroft if (sd == 0 || (sd->flags & SDHAVELABEL) == 0) {
862 1.18.2.2 mycroft val = sdopen(MAKESDDEV(major(dev), unit, RAW_PART), FREAD, S_IFBLK, 0);
863 1.18.2.2 mycroft if (val != 0)
864 1.18.2.2 mycroft return -1;
865 1.18.2.2 mycroft }
866 1.18.2.2 mycroft if (sd->flags & SDWRITEPROT)
867 1.18.2.2 mycroft return -1;
868 1.18.2.2 mycroft
869 1.18.2.3 mycroft return (int)sd->sc_dk.dk_label.d_partitions[part].p_size;
870 1.3 deraadt }
871 1.3 deraadt
872 1.3 deraadt
873 1.18.2.2 mycroft #define SCSIDUMP 1
874 1.18.2.2 mycroft #undef SCSIDUMP
875 1.18.2.2 mycroft #define NOT_TRUSTED 1
876 1.3 deraadt
877 1.18.2.2 mycroft #ifdef SCSIDUMP
878 1.18.2.2 mycroft #include <vm/vm.h>
879 1.18.2.2 mycroft
880 1.18.2.2 mycroft static struct scsi_xfer sx;
881 1.18.2.2 mycroft #define MAXTRANSFER 8 /* 1 page at a time */
882 1.3 deraadt
883 1.3 deraadt /*
884 1.18.2.2 mycroft * dump all of physical memory into the partition specified, starting
885 1.18.2.2 mycroft * at offset 'dumplo' into the partition.
886 1.3 deraadt */
887 1.3 deraadt int
888 1.18.2.2 mycroft sddump(dev_t dev)
889 1.18.2.2 mycroft { /* dump core after a system crash */
890 1.18.2.2 mycroft register struct sd_data *sd; /* disk unit to do the IO */
891 1.18.2.2 mycroft int32 num; /* number of sectors to write */
892 1.18.2.2 mycroft u_int32 unit, part;
893 1.18.2.2 mycroft int32 blkoff, blknum, blkcnt = MAXTRANSFER;
894 1.18.2.2 mycroft int32 nblocks;
895 1.18.2.2 mycroft char *addr;
896 1.18.2.2 mycroft struct scsi_rw_big cmd;
897 1.18.2.2 mycroft extern int Maxmem;
898 1.18.2.2 mycroft static int sddoingadump = 0;
899 1.18.2.2 mycroft #define MAPTO CADDR1
900 1.18.2.2 mycroft extern caddr_t MAPTO; /* map the page we are about to write, here */
901 1.18.2.2 mycroft struct scsi_xfer *xs = &sx;
902 1.18.2.2 mycroft int retval;
903 1.18.2.2 mycroft int c;
904 1.18.2.2 mycroft
905 1.18.2.2 mycroft addr = (char *) 0; /* starting address */
906 1.18.2.2 mycroft
907 1.18.2.2 mycroft /* toss any characters present prior to dump */
908 1.18.2.2 mycroft while ((c = sgetc(1)) && (c != 0x100)); /*syscons and pccons differ */
909 1.18.2.2 mycroft
910 1.18.2.2 mycroft /* size of memory to dump */
911 1.18.2.2 mycroft num = Maxmem;
912 1.18.2.3 mycroft unit = SDUNIT(dev); /* eventually support floppies? */
913 1.18.2.3 mycroft part = SDPART(dev); /* file system */
914 1.18.2.2 mycroft /* check for acceptable drive number */
915 1.1 cgd if (unit >= NSD)
916 1.18.2.3 mycroft return ENXIO;
917 1.3 deraadt
918 1.3 deraadt sd = sd_data[unit];
919 1.18.2.2 mycroft if (!sd)
920 1.18.2.3 mycroft return ENXIO;
921 1.18.2.2 mycroft /* was it ever initialized etc. ? */
922 1.18.2.2 mycroft if (!(sd->flags & SDINIT))
923 1.18.2.3 mycroft return ENXIO;
924 1.18.2.2 mycroft if (sd->sc_link->flags & SDEV_MEDIA_LOADED != SDEV_MEDIA_LOADED)
925 1.18.2.3 mycroft return ENXIO;
926 1.18.2.2 mycroft if (sd->flags & SDWRITEPROT)
927 1.18.2.3 mycroft return ENXIO;
928 1.18.2.2 mycroft
929 1.18.2.2 mycroft /* Convert to disk sectors */
930 1.18.2.3 mycroft num = (u_int32) num * NBPG / sd->sc_dk.dk_label.d_secsize;
931 1.18.2.2 mycroft
932 1.18.2.2 mycroft /* check if controller active */
933 1.18.2.2 mycroft if (sddoingadump)
934 1.18.2.3 mycroft return EFAULT;
935 1.18.2.2 mycroft
936 1.18.2.3 mycroft nblocks = sd->sc_dk.dk_label.d_partitions[part].p_size;
937 1.18.2.3 mycroft blkoff = sd->sc_dk.dk_label.d_partitions[part].p_offset;
938 1.18.2.2 mycroft
939 1.18.2.2 mycroft /* check transfer bounds against partition size */
940 1.18.2.2 mycroft if ((dumplo < 0) || ((dumplo + num) > nblocks))
941 1.18.2.3 mycroft return EINVAL;
942 1.18.2.2 mycroft
943 1.18.2.2 mycroft sddoingadump = 1;
944 1.18.2.2 mycroft
945 1.18.2.2 mycroft blknum = dumplo + blkoff;
946 1.18.2.2 mycroft /* blkcnt = initialise_me; */
947 1.18.2.2 mycroft while (num > 0) {
948 1.18.2.2 mycroft pmap_enter(kernel_pmap,
949 1.18.2.2 mycroft MAPTO,
950 1.18.2.2 mycroft trunc_page(addr),
951 1.18.2.2 mycroft VM_PROT_READ,
952 1.18.2.2 mycroft TRUE);
953 1.18.2.2 mycroft #ifndef NOT_TRUSTED
954 1.18.2.2 mycroft /*
955 1.18.2.2 mycroft * Fill out the scsi command
956 1.18.2.2 mycroft */
957 1.18.2.2 mycroft bzero(&cmd, sizeof(cmd));
958 1.18.2.2 mycroft cmd.op_code = WRITE_BIG;
959 1.18.2.2 mycroft cmd.addr_3 = (blknum & 0xff000000) >> 24;
960 1.18.2.2 mycroft cmd.addr_2 = (blknum & 0xff0000) >> 16;
961 1.18.2.2 mycroft cmd.addr_1 = (blknum & 0xff00) >> 8;
962 1.18.2.2 mycroft cmd.addr_0 = blknum & 0xff;
963 1.18.2.2 mycroft cmd.length2 = (blkcnt & 0xff00) >> 8;
964 1.18.2.2 mycroft cmd.length1 = (blkcnt & 0xff);
965 1.18.2.2 mycroft /*
966 1.18.2.2 mycroft * Fill out the scsi_xfer structure
967 1.18.2.2 mycroft * Note: we cannot sleep as we may be an interrupt
968 1.18.2.2 mycroft * don't use scsi_scsi_cmd() as it may want
969 1.18.2.2 mycroft * to wait for an xs.
970 1.18.2.2 mycroft */
971 1.18.2.2 mycroft bzero(xs, sizeof(sx));
972 1.18.2.2 mycroft xs->flags |= SCSI_NOMASK | SCSI_NOSLEEP | INUSE;
973 1.18.2.2 mycroft xs->sc_link = sd->sc_link;
974 1.18.2.2 mycroft xs->retries = SD_RETRIES;
975 1.18.2.2 mycroft xs->timeout = 10000; /* 10000 millisecs for a disk ! */
976 1.18.2.2 mycroft xs->cmd = (struct scsi_generic *) &cmd;
977 1.18.2.2 mycroft xs->cmdlen = sizeof(cmd);
978 1.18.2.2 mycroft xs->resid = blkcnt * 512;
979 1.18.2.2 mycroft xs->error = XS_NOERROR;
980 1.18.2.2 mycroft xs->bp = 0;
981 1.18.2.2 mycroft xs->data = (u_char *) MAPTO;
982 1.18.2.2 mycroft xs->datalen = blkcnt * 512;
983 1.1 cgd
984 1.18.2.2 mycroft /*
985 1.18.2.2 mycroft * Pass all this info to the scsi driver.
986 1.18.2.2 mycroft */
987 1.18.2.2 mycroft retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
988 1.18.2.2 mycroft switch (retval) {
989 1.18.2.2 mycroft case SUCCESSFULLY_QUEUED:
990 1.18.2.2 mycroft case HAD_ERROR:
991 1.18.2.3 mycroft return ENXIO; /* we said not to sleep! */
992 1.18.2.2 mycroft case COMPLETE:
993 1.18.2.2 mycroft break;
994 1.18.2.2 mycroft default:
995 1.18.2.3 mycroft return ENXIO; /* we said not to sleep! */
996 1.18.2.2 mycroft }
997 1.18.2.2 mycroft #else /* NOT_TRUSTED */
998 1.18.2.2 mycroft /* lets just talk about this first... */
999 1.18.2.2 mycroft printf("sd%d: dump addr 0x%x, blk %d\n", unit, addr, blknum);
1000 1.18.2.2 mycroft #endif /* NOT_TRUSTED */
1001 1.18.2.2 mycroft
1002 1.18.2.2 mycroft if ((unsigned) addr % (1024 * 1024) == 0)
1003 1.18.2.2 mycroft printf("%d ", num / 2048);
1004 1.18.2.2 mycroft /* update block count */
1005 1.18.2.2 mycroft num -= blkcnt;
1006 1.18.2.2 mycroft blknum += blkcnt;
1007 1.18.2.2 mycroft (int) addr += 512 * blkcnt;
1008 1.18.2.2 mycroft
1009 1.18.2.2 mycroft /* operator aborting dump? */
1010 1.18.2.2 mycroft if ((c = sgetc(1)) && (c != 0x100))
1011 1.18.2.3 mycroft return EINTR;
1012 1.18.2.2 mycroft }
1013 1.18.2.3 mycroft return 0;
1014 1.1 cgd }
1015 1.18.2.2 mycroft #else /* SCSIDUMP */
1016 1.18.2.2 mycroft int
1017 1.1 cgd sddump()
1018 1.1 cgd {
1019 1.18.2.2 mycroft printf("\nsddump() -- not implemented\n");
1020 1.18.2.2 mycroft DELAY(60000000); /* 60 seconds */
1021 1.3 deraadt return -1;
1022 1.1 cgd }
1023 1.18.2.2 mycroft #endif /* SCSIDUMP */
1024