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