sd.c revision 1.116.2.4 1 1.116.2.4 thorpej /* $NetBSD: sd.c,v 1.116.2.4 1997/10/14 10:25:30 thorpej Exp $ */
2 1.116.2.2 thorpej
3 1.116.2.2 thorpej /*
4 1.116.2.2 thorpej * Copyright (c) 1994, 1995, 1997 Charles M. Hannum. All rights reserved.
5 1.116.2.2 thorpej *
6 1.116.2.2 thorpej * Redistribution and use in source and binary forms, with or without
7 1.116.2.2 thorpej * modification, are permitted provided that the following conditions
8 1.116.2.2 thorpej * are met:
9 1.116.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.116.2.2 thorpej * notice, this list of conditions and the following disclaimer.
11 1.116.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.116.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.116.2.2 thorpej * documentation and/or other materials provided with the distribution.
14 1.116.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
15 1.116.2.2 thorpej * must display the following acknowledgement:
16 1.116.2.2 thorpej * This product includes software developed by Charles M. Hannum.
17 1.116.2.2 thorpej * 4. The name of the author may not be used to endorse or promote products
18 1.116.2.2 thorpej * derived from this software without specific prior written permission.
19 1.116.2.2 thorpej *
20 1.116.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.116.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.116.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.116.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.116.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.116.2.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.116.2.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.116.2.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.116.2.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.116.2.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.116.2.2 thorpej */
31 1.116.2.2 thorpej
32 1.116.2.2 thorpej /*
33 1.116.2.2 thorpej * Originally written by Julian Elischer (julian (at) dialix.oz.au)
34 1.116.2.2 thorpej * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 1.116.2.2 thorpej *
36 1.116.2.2 thorpej * TRW Financial Systems, in accordance with their agreement with Carnegie
37 1.116.2.2 thorpej * Mellon University, makes this software available to CMU to distribute
38 1.116.2.2 thorpej * or use in any manner that they see fit as long as this message is kept with
39 1.116.2.2 thorpej * the software. For this reason TFS also grants any other persons or
40 1.116.2.2 thorpej * organisations permission to use or modify this software.
41 1.116.2.2 thorpej *
42 1.116.2.2 thorpej * TFS supplies this software to be publicly redistributed
43 1.116.2.2 thorpej * on the understanding that TFS is not responsible for the correct
44 1.116.2.2 thorpej * functioning of this software in any circumstances.
45 1.116.2.2 thorpej *
46 1.116.2.2 thorpej * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
47 1.116.2.2 thorpej */
48 1.116.2.2 thorpej
49 1.116.2.4 thorpej #include "rnd.h"
50 1.116.2.4 thorpej
51 1.116.2.2 thorpej #include <sys/types.h>
52 1.116.2.2 thorpej #include <sys/param.h>
53 1.116.2.2 thorpej #include <sys/systm.h>
54 1.116.2.2 thorpej #include <sys/kernel.h>
55 1.116.2.2 thorpej #include <sys/file.h>
56 1.116.2.2 thorpej #include <sys/stat.h>
57 1.116.2.2 thorpej #include <sys/ioctl.h>
58 1.116.2.2 thorpej #include <sys/buf.h>
59 1.116.2.2 thorpej #include <sys/uio.h>
60 1.116.2.2 thorpej #include <sys/malloc.h>
61 1.116.2.2 thorpej #include <sys/errno.h>
62 1.116.2.2 thorpej #include <sys/device.h>
63 1.116.2.2 thorpej #include <sys/disklabel.h>
64 1.116.2.2 thorpej #include <sys/disk.h>
65 1.116.2.2 thorpej #include <sys/proc.h>
66 1.116.2.2 thorpej #include <sys/conf.h>
67 1.116.2.4 thorpej #if NRND > 0
68 1.116.2.4 thorpej #include <sys/rnd.h>
69 1.116.2.4 thorpej #endif
70 1.116.2.2 thorpej
71 1.116.2.2 thorpej #include <dev/scsipi/scsi_all.h>
72 1.116.2.2 thorpej #include <dev/scsipi/scsipi_all.h>
73 1.116.2.2 thorpej #include <dev/scsipi/scsi_all.h>
74 1.116.2.2 thorpej #include <dev/scsipi/scsi_disk.h>
75 1.116.2.2 thorpej #include <dev/scsipi/scsipi_disk.h>
76 1.116.2.2 thorpej #include <dev/scsipi/scsiconf.h>
77 1.116.2.2 thorpej
78 1.116.2.3 thorpej #ifndef SDOUTSTANDING
79 1.116.2.2 thorpej #define SDOUTSTANDING 4
80 1.116.2.3 thorpej #endif
81 1.116.2.2 thorpej #define SDRETRIES 4
82 1.116.2.2 thorpej
83 1.116.2.2 thorpej #define SDUNIT(dev) DISKUNIT(dev)
84 1.116.2.2 thorpej #define SDPART(dev) DISKPART(dev)
85 1.116.2.2 thorpej #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
86 1.116.2.2 thorpej
87 1.116.2.2 thorpej #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
88 1.116.2.2 thorpej
89 1.116.2.2 thorpej struct sd_softc {
90 1.116.2.2 thorpej struct device sc_dev;
91 1.116.2.2 thorpej struct disk sc_dk;
92 1.116.2.2 thorpej
93 1.116.2.2 thorpej int flags;
94 1.116.2.2 thorpej #define SDF_LOCKED 0x01
95 1.116.2.2 thorpej #define SDF_WANTED 0x02
96 1.116.2.2 thorpej #define SDF_WLABEL 0x04 /* label is writable */
97 1.116.2.2 thorpej #define SDF_LABELLING 0x08 /* writing label */
98 1.116.2.2 thorpej #define SDF_ANCIENT 0x10 /* disk is ancient; for minphys */
99 1.116.2.2 thorpej struct scsipi_link *sc_link; /* contains our targ, lun, etc. */
100 1.116.2.2 thorpej struct disk_parms {
101 1.116.2.2 thorpej u_char heads; /* number of heads */
102 1.116.2.2 thorpej u_short cyls; /* number of cylinders */
103 1.116.2.2 thorpej u_char sectors; /* number of sectors/track */
104 1.116.2.2 thorpej int blksize; /* number of bytes/sector */
105 1.116.2.2 thorpej u_long disksize; /* total number sectors */
106 1.116.2.2 thorpej } params;
107 1.116.2.2 thorpej struct buf buf_queue;
108 1.116.2.2 thorpej u_int8_t type;
109 1.116.2.4 thorpej #if NRND > 0
110 1.116.2.4 thorpej rndsource_element_t rnd_source;
111 1.116.2.4 thorpej #endif
112 1.116.2.2 thorpej };
113 1.116.2.2 thorpej
114 1.116.2.2 thorpej struct scsi_mode_sense_data {
115 1.116.2.2 thorpej struct scsi_mode_header header;
116 1.116.2.2 thorpej struct scsi_blk_desc blk_desc;
117 1.116.2.2 thorpej union scsi_disk_pages pages;
118 1.116.2.2 thorpej };
119 1.116.2.2 thorpej
120 1.116.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
121 1.116.2.2 thorpej int sdmatch __P((struct device *, void *, void *));
122 1.116.2.2 thorpej #else
123 1.116.2.2 thorpej int sdmatch __P((struct device *, struct cfdata *, void *));
124 1.116.2.2 thorpej #endif
125 1.116.2.2 thorpej void sdattach __P((struct device *, struct device *, void *));
126 1.116.2.2 thorpej int sdlock __P((struct sd_softc *));
127 1.116.2.2 thorpej void sdunlock __P((struct sd_softc *));
128 1.116.2.2 thorpej void sdminphys __P((struct buf *));
129 1.116.2.4 thorpej void sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *));
130 1.116.2.2 thorpej void sdgetdisklabel __P((struct sd_softc *));
131 1.116.2.2 thorpej void sdstart __P((void *));
132 1.116.2.2 thorpej void sddone __P((struct scsipi_xfer *));
133 1.116.2.2 thorpej int sd_reassign_blocks __P((struct sd_softc *, u_long));
134 1.116.2.2 thorpej int sd_get_optparms __P((struct sd_softc *, int, struct disk_parms *));
135 1.116.2.2 thorpej int sd_get_parms __P((struct sd_softc *, int));
136 1.116.2.2 thorpej static int sd_mode_sense __P((struct sd_softc *, struct scsi_mode_sense_data *,
137 1.116.2.2 thorpej int, int));
138 1.116.2.2 thorpej
139 1.116.2.2 thorpej struct cfattach sd_ca = {
140 1.116.2.2 thorpej sizeof(struct sd_softc), sdmatch, sdattach
141 1.116.2.2 thorpej };
142 1.116.2.2 thorpej
143 1.116.2.2 thorpej struct cfdriver sd_cd = {
144 1.116.2.2 thorpej NULL, "sd", DV_DISK
145 1.116.2.2 thorpej };
146 1.116.2.2 thorpej
147 1.116.2.2 thorpej struct dkdriver sddkdriver = { sdstrategy };
148 1.116.2.2 thorpej
149 1.116.2.2 thorpej struct scsipi_device sd_switch = {
150 1.116.2.2 thorpej NULL, /* Use default error handler */
151 1.116.2.2 thorpej sdstart, /* have a queue, served by this */
152 1.116.2.2 thorpej NULL, /* have no async handler */
153 1.116.2.2 thorpej sddone, /* deal with stats at interrupt time */
154 1.116.2.2 thorpej };
155 1.116.2.2 thorpej
156 1.116.2.2 thorpej struct scsipi_inquiry_pattern sd_patterns[] = {
157 1.116.2.2 thorpej {T_DIRECT, T_FIXED,
158 1.116.2.2 thorpej "", "", ""},
159 1.116.2.2 thorpej {T_DIRECT, T_REMOV,
160 1.116.2.2 thorpej "", "", ""},
161 1.116.2.2 thorpej {T_OPTICAL, T_FIXED,
162 1.116.2.2 thorpej "", "", ""},
163 1.116.2.2 thorpej {T_OPTICAL, T_REMOV,
164 1.116.2.2 thorpej "", "", ""},
165 1.116.2.2 thorpej };
166 1.116.2.2 thorpej
167 1.116.2.2 thorpej int
168 1.116.2.2 thorpej sdmatch(parent, match, aux)
169 1.116.2.2 thorpej struct device *parent;
170 1.116.2.2 thorpej #ifdef __BROKEN_INDIRECT_CONFIG
171 1.116.2.2 thorpej void *match;
172 1.116.2.2 thorpej #else
173 1.116.2.2 thorpej struct cfdata *match;
174 1.116.2.2 thorpej #endif
175 1.116.2.2 thorpej void *aux;
176 1.116.2.2 thorpej {
177 1.116.2.2 thorpej struct scsipibus_attach_args *sa = aux;
178 1.116.2.2 thorpej int priority;
179 1.116.2.2 thorpej
180 1.116.2.2 thorpej (void)scsipi_inqmatch(&sa->sa_inqbuf,
181 1.116.2.4 thorpej (caddr_t)sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
182 1.116.2.2 thorpej sizeof(sd_patterns[0]), &priority);
183 1.116.2.2 thorpej return (priority);
184 1.116.2.2 thorpej }
185 1.116.2.2 thorpej
186 1.116.2.2 thorpej /*
187 1.116.2.2 thorpej * The routine called by the low level scsi routine when it discovers
188 1.116.2.2 thorpej * a device suitable for this driver.
189 1.116.2.2 thorpej */
190 1.116.2.2 thorpej void
191 1.116.2.2 thorpej sdattach(parent, self, aux)
192 1.116.2.2 thorpej struct device *parent, *self;
193 1.116.2.2 thorpej void *aux;
194 1.116.2.2 thorpej {
195 1.116.2.2 thorpej int error;
196 1.116.2.2 thorpej struct sd_softc *sd = (void *)self;
197 1.116.2.2 thorpej struct disk_parms *dp = &sd->params;
198 1.116.2.2 thorpej struct scsipibus_attach_args *sa = aux;
199 1.116.2.2 thorpej struct scsipi_link *sc_link = sa->sa_sc_link;
200 1.116.2.2 thorpej
201 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
202 1.116.2.2 thorpej
203 1.116.2.2 thorpej /*
204 1.116.2.2 thorpej * Store information needed to contact our base driver
205 1.116.2.2 thorpej */
206 1.116.2.2 thorpej sd->sc_link = sc_link;
207 1.116.2.2 thorpej sd->type = (sa->sa_inqbuf.type & SID_TYPE);
208 1.116.2.2 thorpej sc_link->device = &sd_switch;
209 1.116.2.2 thorpej sc_link->device_softc = sd;
210 1.116.2.2 thorpej if (sc_link->openings > SDOUTSTANDING)
211 1.116.2.2 thorpej sc_link->openings = SDOUTSTANDING;
212 1.116.2.2 thorpej
213 1.116.2.2 thorpej /*
214 1.116.2.2 thorpej * Initialize and attach the disk structure.
215 1.116.2.2 thorpej */
216 1.116.2.2 thorpej sd->sc_dk.dk_driver = &sddkdriver;
217 1.116.2.2 thorpej sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
218 1.116.2.2 thorpej disk_attach(&sd->sc_dk);
219 1.116.2.2 thorpej
220 1.116.2.2 thorpej #if !defined(i386)
221 1.116.2.2 thorpej dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */
222 1.116.2.2 thorpej #endif
223 1.116.2.2 thorpej
224 1.116.2.2 thorpej /*
225 1.116.2.2 thorpej * Note if this device is ancient. This is used in sdminphys().
226 1.116.2.2 thorpej */
227 1.116.2.2 thorpej if ((sa->scsipi_info.scsi_version & SID_ANSII) == 0)
228 1.116.2.2 thorpej sd->flags |= SDF_ANCIENT;
229 1.116.2.2 thorpej
230 1.116.2.2 thorpej /*
231 1.116.2.2 thorpej * Use the subdriver to request information regarding
232 1.116.2.2 thorpej * the drive. We cannot use interrupts yet, so the
233 1.116.2.2 thorpej * request must specify this.
234 1.116.2.2 thorpej */
235 1.116.2.2 thorpej printf("\n");
236 1.116.2.2 thorpej printf("%s: ", sd->sc_dev.dv_xname);
237 1.116.2.2 thorpej
238 1.116.2.2 thorpej if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
239 1.116.2.2 thorpej error = scsipi_start(sd->sc_link, SSS_START,
240 1.116.2.4 thorpej SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST |
241 1.116.2.4 thorpej SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
242 1.116.2.2 thorpej } else
243 1.116.2.2 thorpej error = 0;
244 1.116.2.2 thorpej
245 1.116.2.2 thorpej if (error || sd_get_parms(sd, SCSI_AUTOCONF) != 0)
246 1.116.2.2 thorpej printf("drive offline\n");
247 1.116.2.2 thorpej else
248 1.116.2.2 thorpej printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sect x %ld sectors\n",
249 1.116.2.2 thorpej dp->disksize / (1048576 / dp->blksize), dp->cyls,
250 1.116.2.2 thorpej dp->heads, dp->sectors, dp->blksize, dp->disksize);
251 1.116.2.4 thorpej
252 1.116.2.4 thorpej #if NRND > 0
253 1.116.2.4 thorpej /*
254 1.116.2.4 thorpej * attach the device into the random source list
255 1.116.2.4 thorpej */
256 1.116.2.4 thorpej rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname, RND_TYPE_DISK);
257 1.116.2.4 thorpej #endif
258 1.116.2.2 thorpej }
259 1.116.2.2 thorpej
260 1.116.2.2 thorpej /*
261 1.116.2.2 thorpej * Wait interruptibly for an exclusive lock.
262 1.116.2.2 thorpej *
263 1.116.2.2 thorpej * XXX
264 1.116.2.2 thorpej * Several drivers do this; it should be abstracted and made MP-safe.
265 1.116.2.2 thorpej */
266 1.116.2.2 thorpej int
267 1.116.2.2 thorpej sdlock(sd)
268 1.116.2.2 thorpej struct sd_softc *sd;
269 1.116.2.2 thorpej {
270 1.116.2.2 thorpej int error;
271 1.116.2.2 thorpej
272 1.116.2.2 thorpej while ((sd->flags & SDF_LOCKED) != 0) {
273 1.116.2.2 thorpej sd->flags |= SDF_WANTED;
274 1.116.2.2 thorpej if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
275 1.116.2.4 thorpej return (error);
276 1.116.2.2 thorpej }
277 1.116.2.2 thorpej sd->flags |= SDF_LOCKED;
278 1.116.2.4 thorpej return (0);
279 1.116.2.2 thorpej }
280 1.116.2.2 thorpej
281 1.116.2.2 thorpej /*
282 1.116.2.2 thorpej * Unlock and wake up any waiters.
283 1.116.2.2 thorpej */
284 1.116.2.2 thorpej void
285 1.116.2.2 thorpej sdunlock(sd)
286 1.116.2.2 thorpej struct sd_softc *sd;
287 1.116.2.2 thorpej {
288 1.116.2.2 thorpej
289 1.116.2.2 thorpej sd->flags &= ~SDF_LOCKED;
290 1.116.2.2 thorpej if ((sd->flags & SDF_WANTED) != 0) {
291 1.116.2.2 thorpej sd->flags &= ~SDF_WANTED;
292 1.116.2.2 thorpej wakeup(sd);
293 1.116.2.2 thorpej }
294 1.116.2.2 thorpej }
295 1.116.2.2 thorpej
296 1.116.2.2 thorpej /*
297 1.116.2.2 thorpej * open the device. Make sure the partition info is a up-to-date as can be.
298 1.116.2.2 thorpej */
299 1.116.2.2 thorpej int
300 1.116.2.2 thorpej sdopen(dev, flag, fmt, p)
301 1.116.2.2 thorpej dev_t dev;
302 1.116.2.2 thorpej int flag, fmt;
303 1.116.2.2 thorpej struct proc *p;
304 1.116.2.2 thorpej {
305 1.116.2.2 thorpej struct sd_softc *sd;
306 1.116.2.2 thorpej struct scsipi_link *sc_link;
307 1.116.2.2 thorpej int unit, part;
308 1.116.2.2 thorpej int error;
309 1.116.2.2 thorpej
310 1.116.2.2 thorpej unit = SDUNIT(dev);
311 1.116.2.2 thorpej if (unit >= sd_cd.cd_ndevs)
312 1.116.2.4 thorpej return (ENXIO);
313 1.116.2.2 thorpej sd = sd_cd.cd_devs[unit];
314 1.116.2.2 thorpej if (sd == NULL)
315 1.116.2.4 thorpej return (ENXIO);
316 1.116.2.2 thorpej
317 1.116.2.2 thorpej sc_link = sd->sc_link;
318 1.116.2.2 thorpej
319 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB1,
320 1.116.2.2 thorpej ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
321 1.116.2.2 thorpej sd_cd.cd_ndevs, SDPART(dev)));
322 1.116.2.2 thorpej
323 1.116.2.2 thorpej if ((error = sdlock(sd)) != 0)
324 1.116.2.4 thorpej return (error);
325 1.116.2.2 thorpej
326 1.116.2.2 thorpej if (sd->sc_dk.dk_openmask != 0) {
327 1.116.2.2 thorpej /*
328 1.116.2.2 thorpej * If any partition is open, but the disk has been invalidated,
329 1.116.2.2 thorpej * disallow further opens.
330 1.116.2.2 thorpej */
331 1.116.2.2 thorpej if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
332 1.116.2.2 thorpej error = EIO;
333 1.116.2.2 thorpej goto bad3;
334 1.116.2.2 thorpej }
335 1.116.2.2 thorpej } else {
336 1.116.2.2 thorpej /* Check that it is still responding and ok. */
337 1.116.2.2 thorpej error = scsipi_test_unit_ready(sc_link,
338 1.116.2.4 thorpej SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
339 1.116.2.4 thorpej SCSI_IGNORE_NOT_READY);
340 1.116.2.2 thorpej if (error)
341 1.116.2.2 thorpej goto bad3;
342 1.116.2.2 thorpej
343 1.116.2.2 thorpej /* Start the pack spinning if necessary. */
344 1.116.2.2 thorpej if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
345 1.116.2.2 thorpej error = scsipi_start(sc_link, SSS_START,
346 1.116.2.4 thorpej SCSI_IGNORE_ILLEGAL_REQUEST |
347 1.116.2.4 thorpej SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
348 1.116.2.2 thorpej if (error)
349 1.116.2.2 thorpej goto bad3;
350 1.116.2.2 thorpej }
351 1.116.2.2 thorpej
352 1.116.2.2 thorpej sc_link->flags |= SDEV_OPEN;
353 1.116.2.2 thorpej
354 1.116.2.2 thorpej /* Lock the pack in. */
355 1.116.2.2 thorpej error = scsipi_prevent(sc_link, PR_PREVENT,
356 1.116.2.4 thorpej SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
357 1.116.2.2 thorpej if (error)
358 1.116.2.2 thorpej goto bad;
359 1.116.2.2 thorpej
360 1.116.2.2 thorpej if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
361 1.116.2.2 thorpej sc_link->flags |= SDEV_MEDIA_LOADED;
362 1.116.2.2 thorpej
363 1.116.2.2 thorpej /* Load the physical device parameters. */
364 1.116.2.2 thorpej if (sd_get_parms(sd, 0) != 0) {
365 1.116.2.2 thorpej error = ENXIO;
366 1.116.2.2 thorpej goto bad2;
367 1.116.2.2 thorpej }
368 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
369 1.116.2.2 thorpej
370 1.116.2.2 thorpej /* Load the partition info if not already loaded. */
371 1.116.2.2 thorpej sdgetdisklabel(sd);
372 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
373 1.116.2.2 thorpej }
374 1.116.2.2 thorpej }
375 1.116.2.2 thorpej
376 1.116.2.2 thorpej part = SDPART(dev);
377 1.116.2.2 thorpej
378 1.116.2.2 thorpej /* Check that the partition exists. */
379 1.116.2.2 thorpej if (part != RAW_PART &&
380 1.116.2.2 thorpej (part >= sd->sc_dk.dk_label->d_npartitions ||
381 1.116.2.2 thorpej sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
382 1.116.2.2 thorpej error = ENXIO;
383 1.116.2.2 thorpej goto bad;
384 1.116.2.2 thorpej }
385 1.116.2.2 thorpej
386 1.116.2.2 thorpej /* Insure only one open at a time. */
387 1.116.2.2 thorpej switch (fmt) {
388 1.116.2.2 thorpej case S_IFCHR:
389 1.116.2.2 thorpej sd->sc_dk.dk_copenmask |= (1 << part);
390 1.116.2.2 thorpej break;
391 1.116.2.2 thorpej case S_IFBLK:
392 1.116.2.2 thorpej sd->sc_dk.dk_bopenmask |= (1 << part);
393 1.116.2.2 thorpej break;
394 1.116.2.2 thorpej }
395 1.116.2.4 thorpej sd->sc_dk.dk_openmask =
396 1.116.2.4 thorpej sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
397 1.116.2.2 thorpej
398 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
399 1.116.2.2 thorpej sdunlock(sd);
400 1.116.2.4 thorpej return (0);
401 1.116.2.2 thorpej
402 1.116.2.2 thorpej bad2:
403 1.116.2.2 thorpej sc_link->flags &= ~SDEV_MEDIA_LOADED;
404 1.116.2.2 thorpej
405 1.116.2.2 thorpej bad:
406 1.116.2.2 thorpej if (sd->sc_dk.dk_openmask == 0) {
407 1.116.2.2 thorpej scsipi_prevent(sc_link, PR_ALLOW,
408 1.116.2.2 thorpej SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
409 1.116.2.2 thorpej sc_link->flags &= ~SDEV_OPEN;
410 1.116.2.2 thorpej }
411 1.116.2.2 thorpej
412 1.116.2.2 thorpej bad3:
413 1.116.2.2 thorpej sdunlock(sd);
414 1.116.2.4 thorpej return (error);
415 1.116.2.2 thorpej }
416 1.116.2.2 thorpej
417 1.116.2.2 thorpej /*
418 1.116.2.2 thorpej * close the device.. only called if we are the LAST occurence of an open
419 1.116.2.2 thorpej * device. Convenient now but usually a pain.
420 1.116.2.2 thorpej */
421 1.116.2.2 thorpej int
422 1.116.2.2 thorpej sdclose(dev, flag, fmt, p)
423 1.116.2.2 thorpej dev_t dev;
424 1.116.2.2 thorpej int flag, fmt;
425 1.116.2.2 thorpej struct proc *p;
426 1.116.2.2 thorpej {
427 1.116.2.2 thorpej struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
428 1.116.2.2 thorpej int part = SDPART(dev);
429 1.116.2.2 thorpej int error;
430 1.116.2.2 thorpej
431 1.116.2.2 thorpej if ((error = sdlock(sd)) != 0)
432 1.116.2.4 thorpej return (error);
433 1.116.2.2 thorpej
434 1.116.2.2 thorpej switch (fmt) {
435 1.116.2.2 thorpej case S_IFCHR:
436 1.116.2.2 thorpej sd->sc_dk.dk_copenmask &= ~(1 << part);
437 1.116.2.2 thorpej break;
438 1.116.2.2 thorpej case S_IFBLK:
439 1.116.2.2 thorpej sd->sc_dk.dk_bopenmask &= ~(1 << part);
440 1.116.2.2 thorpej break;
441 1.116.2.2 thorpej }
442 1.116.2.4 thorpej sd->sc_dk.dk_openmask =
443 1.116.2.4 thorpej sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
444 1.116.2.2 thorpej
445 1.116.2.2 thorpej if (sd->sc_dk.dk_openmask == 0) {
446 1.116.2.2 thorpej /* XXXX Must wait for I/O to complete! */
447 1.116.2.2 thorpej
448 1.116.2.2 thorpej scsipi_prevent(sd->sc_link, PR_ALLOW,
449 1.116.2.2 thorpej SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
450 1.116.2.2 thorpej sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED);
451 1.116.2.2 thorpej }
452 1.116.2.2 thorpej
453 1.116.2.2 thorpej sdunlock(sd);
454 1.116.2.4 thorpej return (0);
455 1.116.2.2 thorpej }
456 1.116.2.2 thorpej
457 1.116.2.2 thorpej /*
458 1.116.2.2 thorpej * Actually translate the requested transfer into one the physical driver
459 1.116.2.2 thorpej * can understand. The transfer is described by a buf and will include
460 1.116.2.2 thorpej * only one physical transfer.
461 1.116.2.2 thorpej */
462 1.116.2.2 thorpej void
463 1.116.2.2 thorpej sdstrategy(bp)
464 1.116.2.2 thorpej struct buf *bp;
465 1.116.2.2 thorpej {
466 1.116.2.2 thorpej struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
467 1.116.2.2 thorpej int s;
468 1.116.2.2 thorpej
469 1.116.2.2 thorpej SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
470 1.116.2.2 thorpej SC_DEBUG(sd->sc_link, SDEV_DB1,
471 1.116.2.2 thorpej ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
472 1.116.2.2 thorpej /*
473 1.116.2.2 thorpej * The transfer must be a whole number of blocks.
474 1.116.2.2 thorpej */
475 1.116.2.2 thorpej if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) {
476 1.116.2.2 thorpej bp->b_error = EINVAL;
477 1.116.2.2 thorpej goto bad;
478 1.116.2.2 thorpej }
479 1.116.2.2 thorpej /*
480 1.116.2.2 thorpej * If the device has been made invalid, error out
481 1.116.2.2 thorpej */
482 1.116.2.2 thorpej if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
483 1.116.2.2 thorpej bp->b_error = EIO;
484 1.116.2.2 thorpej goto bad;
485 1.116.2.2 thorpej }
486 1.116.2.2 thorpej /*
487 1.116.2.2 thorpej * If it's a null transfer, return immediatly
488 1.116.2.2 thorpej */
489 1.116.2.2 thorpej if (bp->b_bcount == 0)
490 1.116.2.2 thorpej goto done;
491 1.116.2.2 thorpej
492 1.116.2.2 thorpej /*
493 1.116.2.2 thorpej * Do bounds checking, adjust transfer. if error, process.
494 1.116.2.2 thorpej * If end of partition, just return.
495 1.116.2.2 thorpej */
496 1.116.2.2 thorpej if (SDPART(bp->b_dev) != RAW_PART &&
497 1.116.2.2 thorpej bounds_check_with_label(bp, sd->sc_dk.dk_label,
498 1.116.2.2 thorpej (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
499 1.116.2.2 thorpej goto done;
500 1.116.2.2 thorpej
501 1.116.2.2 thorpej s = splbio();
502 1.116.2.2 thorpej
503 1.116.2.2 thorpej /*
504 1.116.2.2 thorpej * Place it in the queue of disk activities for this disk
505 1.116.2.2 thorpej */
506 1.116.2.2 thorpej disksort(&sd->buf_queue, bp);
507 1.116.2.2 thorpej
508 1.116.2.2 thorpej /*
509 1.116.2.2 thorpej * Tell the device to get going on the transfer if it's
510 1.116.2.2 thorpej * not doing anything, otherwise just wait for completion
511 1.116.2.2 thorpej */
512 1.116.2.2 thorpej sdstart(sd);
513 1.116.2.2 thorpej
514 1.116.2.2 thorpej splx(s);
515 1.116.2.2 thorpej return;
516 1.116.2.2 thorpej
517 1.116.2.2 thorpej bad:
518 1.116.2.2 thorpej bp->b_flags |= B_ERROR;
519 1.116.2.2 thorpej done:
520 1.116.2.2 thorpej /*
521 1.116.2.2 thorpej * Correctly set the buf to indicate a completed xfer
522 1.116.2.2 thorpej */
523 1.116.2.2 thorpej bp->b_resid = bp->b_bcount;
524 1.116.2.2 thorpej biodone(bp);
525 1.116.2.2 thorpej }
526 1.116.2.2 thorpej
527 1.116.2.2 thorpej /*
528 1.116.2.2 thorpej * sdstart looks to see if there is a buf waiting for the device
529 1.116.2.2 thorpej * and that the device is not already busy. If both are true,
530 1.116.2.2 thorpej * It dequeues the buf and creates a scsi command to perform the
531 1.116.2.2 thorpej * transfer in the buf. The transfer request will call scsipi_done
532 1.116.2.2 thorpej * on completion, which will in turn call this routine again
533 1.116.2.2 thorpej * so that the next queued transfer is performed.
534 1.116.2.2 thorpej * The bufs are queued by the strategy routine (sdstrategy)
535 1.116.2.2 thorpej *
536 1.116.2.2 thorpej * This routine is also called after other non-queued requests
537 1.116.2.2 thorpej * have been made of the scsi driver, to ensure that the queue
538 1.116.2.2 thorpej * continues to be drained.
539 1.116.2.2 thorpej *
540 1.116.2.2 thorpej * must be called at the correct (highish) spl level
541 1.116.2.2 thorpej * sdstart() is called at splbio from sdstrategy and scsipi_done
542 1.116.2.2 thorpej */
543 1.116.2.2 thorpej void
544 1.116.2.2 thorpej sdstart(v)
545 1.116.2.2 thorpej register void *v;
546 1.116.2.2 thorpej {
547 1.116.2.2 thorpej register struct sd_softc *sd = v;
548 1.116.2.2 thorpej register struct scsipi_link *sc_link = sd->sc_link;
549 1.116.2.4 thorpej struct disklabel *lp = sd->sc_dk.dk_label;
550 1.116.2.2 thorpej struct buf *bp = 0;
551 1.116.2.2 thorpej struct buf *dp;
552 1.116.2.2 thorpej struct scsipi_rw_big cmd_big;
553 1.116.2.2 thorpej struct scsi_rw cmd_small;
554 1.116.2.2 thorpej struct scsipi_generic *cmdp;
555 1.116.2.2 thorpej int blkno, nblks, cmdlen, error;
556 1.116.2.2 thorpej struct partition *p;
557 1.116.2.2 thorpej
558 1.116.2.2 thorpej SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
559 1.116.2.2 thorpej /*
560 1.116.2.2 thorpej * Check if the device has room for another command
561 1.116.2.2 thorpej */
562 1.116.2.2 thorpej while (sc_link->openings > 0) {
563 1.116.2.2 thorpej /*
564 1.116.2.2 thorpej * there is excess capacity, but a special waits
565 1.116.2.2 thorpej * It'll need the adapter as soon as we clear out of the
566 1.116.2.2 thorpej * way and let it run (user level wait).
567 1.116.2.2 thorpej */
568 1.116.2.2 thorpej if (sc_link->flags & SDEV_WAITING) {
569 1.116.2.2 thorpej sc_link->flags &= ~SDEV_WAITING;
570 1.116.2.2 thorpej wakeup((caddr_t)sc_link);
571 1.116.2.2 thorpej return;
572 1.116.2.2 thorpej }
573 1.116.2.2 thorpej
574 1.116.2.2 thorpej /*
575 1.116.2.2 thorpej * See if there is a buf with work for us to do..
576 1.116.2.2 thorpej */
577 1.116.2.2 thorpej dp = &sd->buf_queue;
578 1.116.2.2 thorpej if ((bp = dp->b_actf) == NULL) /* yes, an assign */
579 1.116.2.2 thorpej return;
580 1.116.2.2 thorpej dp->b_actf = bp->b_actf;
581 1.116.2.2 thorpej
582 1.116.2.2 thorpej /*
583 1.116.2.2 thorpej * If the device has become invalid, abort all the
584 1.116.2.2 thorpej * reads and writes until all files have been closed and
585 1.116.2.2 thorpej * re-opened
586 1.116.2.2 thorpej */
587 1.116.2.2 thorpej if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
588 1.116.2.2 thorpej bp->b_error = EIO;
589 1.116.2.2 thorpej bp->b_flags |= B_ERROR;
590 1.116.2.2 thorpej bp->b_resid = bp->b_bcount;
591 1.116.2.2 thorpej biodone(bp);
592 1.116.2.2 thorpej continue;
593 1.116.2.2 thorpej }
594 1.116.2.2 thorpej
595 1.116.2.2 thorpej /*
596 1.116.2.2 thorpej * We have a buf, now we should make a command
597 1.116.2.2 thorpej *
598 1.116.2.2 thorpej * First, translate the block to absolute and put it in terms
599 1.116.2.2 thorpej * of the logical blocksize of the device.
600 1.116.2.2 thorpej */
601 1.116.2.4 thorpej blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
602 1.116.2.2 thorpej if (SDPART(bp->b_dev) != RAW_PART) {
603 1.116.2.4 thorpej p = &lp->d_partitions[SDPART(bp->b_dev)];
604 1.116.2.4 thorpej blkno += p->p_offset;
605 1.116.2.2 thorpej }
606 1.116.2.4 thorpej nblks = howmany(bp->b_bcount, lp->d_secsize);
607 1.116.2.2 thorpej
608 1.116.2.2 thorpej /*
609 1.116.2.2 thorpej * Fill out the scsi command. If the transfer will
610 1.116.2.2 thorpej * fit in a "small" cdb, use it.
611 1.116.2.2 thorpej */
612 1.116.2.2 thorpej if (((blkno & 0x1fffff) == blkno) &&
613 1.116.2.2 thorpej ((nblks & 0xff) == nblks)) {
614 1.116.2.2 thorpej /*
615 1.116.2.2 thorpej * We can fit in a small cdb.
616 1.116.2.2 thorpej */
617 1.116.2.2 thorpej bzero(&cmd_small, sizeof(cmd_small));
618 1.116.2.2 thorpej cmd_small.opcode = (bp->b_flags & B_READ) ?
619 1.116.2.2 thorpej SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
620 1.116.2.2 thorpej _lto3b(blkno, cmd_small.addr);
621 1.116.2.2 thorpej cmd_small.length = nblks & 0xff;
622 1.116.2.2 thorpej cmdlen = sizeof(cmd_small);
623 1.116.2.2 thorpej cmdp = (struct scsipi_generic *)&cmd_small;
624 1.116.2.2 thorpej } else {
625 1.116.2.2 thorpej /*
626 1.116.2.2 thorpej * Need a large cdb.
627 1.116.2.2 thorpej */
628 1.116.2.2 thorpej bzero(&cmd_big, sizeof(cmd_big));
629 1.116.2.2 thorpej cmd_big.opcode = (bp->b_flags & B_READ) ?
630 1.116.2.2 thorpej READ_BIG : WRITE_BIG;
631 1.116.2.2 thorpej _lto4b(blkno, cmd_big.addr);
632 1.116.2.2 thorpej _lto2b(nblks, cmd_big.length);
633 1.116.2.2 thorpej cmdlen = sizeof(cmd_big);
634 1.116.2.2 thorpej cmdp = (struct scsipi_generic *)&cmd_big;
635 1.116.2.2 thorpej }
636 1.116.2.2 thorpej
637 1.116.2.2 thorpej /* Instrumentation. */
638 1.116.2.2 thorpej disk_busy(&sd->sc_dk);
639 1.116.2.2 thorpej
640 1.116.2.2 thorpej /*
641 1.116.2.2 thorpej * Call the routine that chats with the adapter.
642 1.116.2.2 thorpej * Note: we cannot sleep as we may be an interrupt
643 1.116.2.2 thorpej */
644 1.116.2.4 thorpej error = (*sc_link->scsipi_cmd)(sc_link, cmdp, cmdlen,
645 1.116.2.2 thorpej (u_char *)bp->b_data, bp->b_bcount,
646 1.116.2.2 thorpej SDRETRIES, 60000, bp, SCSI_NOSLEEP |
647 1.116.2.2 thorpej ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT));
648 1.116.2.2 thorpej if (error)
649 1.116.2.2 thorpej printf("%s: not queued, error %d\n",
650 1.116.2.2 thorpej sd->sc_dev.dv_xname, error);
651 1.116.2.2 thorpej }
652 1.116.2.2 thorpej }
653 1.116.2.2 thorpej
654 1.116.2.2 thorpej void
655 1.116.2.2 thorpej sddone(xs)
656 1.116.2.2 thorpej struct scsipi_xfer *xs;
657 1.116.2.2 thorpej {
658 1.116.2.2 thorpej struct sd_softc *sd = xs->sc_link->device_softc;
659 1.116.2.2 thorpej
660 1.116.2.4 thorpej if (xs->bp != NULL) {
661 1.116.2.2 thorpej disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
662 1.116.2.4 thorpej #if NRND > 0
663 1.116.2.4 thorpej rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno);
664 1.116.2.4 thorpej #endif
665 1.116.2.4 thorpej }
666 1.116.2.2 thorpej }
667 1.116.2.2 thorpej
668 1.116.2.2 thorpej void
669 1.116.2.2 thorpej sdminphys(bp)
670 1.116.2.2 thorpej struct buf *bp;
671 1.116.2.2 thorpej {
672 1.116.2.2 thorpej struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
673 1.116.2.2 thorpej long max;
674 1.116.2.2 thorpej
675 1.116.2.2 thorpej /*
676 1.116.2.2 thorpej * If the device is ancient, we want to make sure that
677 1.116.2.2 thorpej * the transfer fits into a 6-byte cdb.
678 1.116.2.2 thorpej *
679 1.116.2.2 thorpej * XXX Note that the SCSI-I spec says that 256-block transfers
680 1.116.2.2 thorpej * are allowed in a 6-byte read/write, and are specified
681 1.116.2.2 thorpej * by settng the "length" to 0. However, we're conservative
682 1.116.2.2 thorpej * here, allowing only 255-block transfers in case an
683 1.116.2.2 thorpej * ancient device gets confused by length == 0. A length of 0
684 1.116.2.2 thorpej * in a 10-byte read/write actually means 0 blocks.
685 1.116.2.2 thorpej */
686 1.116.2.2 thorpej if (sd->flags & SDF_ANCIENT) {
687 1.116.2.2 thorpej max = sd->sc_dk.dk_label->d_secsize * 0xff;
688 1.116.2.2 thorpej
689 1.116.2.2 thorpej if (bp->b_bcount > max)
690 1.116.2.2 thorpej bp->b_bcount = max;
691 1.116.2.2 thorpej }
692 1.116.2.2 thorpej
693 1.116.2.2 thorpej (*sd->sc_link->adapter->scsipi_minphys)(bp);
694 1.116.2.2 thorpej }
695 1.116.2.2 thorpej
696 1.116.2.2 thorpej int
697 1.116.2.2 thorpej sdread(dev, uio, ioflag)
698 1.116.2.2 thorpej dev_t dev;
699 1.116.2.2 thorpej struct uio *uio;
700 1.116.2.2 thorpej int ioflag;
701 1.116.2.2 thorpej {
702 1.116.2.2 thorpej
703 1.116.2.2 thorpej return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
704 1.116.2.2 thorpej }
705 1.116.2.2 thorpej
706 1.116.2.2 thorpej int
707 1.116.2.2 thorpej sdwrite(dev, uio, ioflag)
708 1.116.2.2 thorpej dev_t dev;
709 1.116.2.2 thorpej struct uio *uio;
710 1.116.2.2 thorpej int ioflag;
711 1.116.2.2 thorpej {
712 1.116.2.2 thorpej
713 1.116.2.2 thorpej return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
714 1.116.2.2 thorpej }
715 1.116.2.2 thorpej
716 1.116.2.2 thorpej /*
717 1.116.2.2 thorpej * Perform special action on behalf of the user
718 1.116.2.2 thorpej * Knows about the internals of this device
719 1.116.2.2 thorpej */
720 1.116.2.2 thorpej int
721 1.116.2.2 thorpej sdioctl(dev, cmd, addr, flag, p)
722 1.116.2.2 thorpej dev_t dev;
723 1.116.2.2 thorpej u_long cmd;
724 1.116.2.2 thorpej caddr_t addr;
725 1.116.2.2 thorpej int flag;
726 1.116.2.2 thorpej struct proc *p;
727 1.116.2.2 thorpej {
728 1.116.2.2 thorpej struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
729 1.116.2.2 thorpej int error;
730 1.116.2.2 thorpej
731 1.116.2.2 thorpej SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
732 1.116.2.2 thorpej
733 1.116.2.2 thorpej /*
734 1.116.2.2 thorpej * If the device is not valid.. abandon ship
735 1.116.2.2 thorpej */
736 1.116.2.2 thorpej if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
737 1.116.2.4 thorpej return (EIO);
738 1.116.2.2 thorpej
739 1.116.2.2 thorpej switch (cmd) {
740 1.116.2.2 thorpej case DIOCGDINFO:
741 1.116.2.2 thorpej *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
742 1.116.2.4 thorpej return (0);
743 1.116.2.2 thorpej
744 1.116.2.2 thorpej case DIOCGPART:
745 1.116.2.2 thorpej ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
746 1.116.2.2 thorpej ((struct partinfo *)addr)->part =
747 1.116.2.2 thorpej &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
748 1.116.2.4 thorpej return (0);
749 1.116.2.2 thorpej
750 1.116.2.2 thorpej case DIOCWDINFO:
751 1.116.2.2 thorpej case DIOCSDINFO:
752 1.116.2.2 thorpej if ((flag & FWRITE) == 0)
753 1.116.2.4 thorpej return (EBADF);
754 1.116.2.2 thorpej
755 1.116.2.2 thorpej if ((error = sdlock(sd)) != 0)
756 1.116.2.4 thorpej return (error);
757 1.116.2.2 thorpej sd->flags |= SDF_LABELLING;
758 1.116.2.2 thorpej
759 1.116.2.2 thorpej error = setdisklabel(sd->sc_dk.dk_label,
760 1.116.2.2 thorpej (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
761 1.116.2.2 thorpej sd->sc_dk.dk_cpulabel);
762 1.116.2.2 thorpej if (error == 0) {
763 1.116.2.2 thorpej if (cmd == DIOCWDINFO)
764 1.116.2.2 thorpej error = writedisklabel(SDLABELDEV(dev),
765 1.116.2.2 thorpej sdstrategy, sd->sc_dk.dk_label,
766 1.116.2.2 thorpej sd->sc_dk.dk_cpulabel);
767 1.116.2.2 thorpej }
768 1.116.2.2 thorpej
769 1.116.2.2 thorpej sd->flags &= ~SDF_LABELLING;
770 1.116.2.2 thorpej sdunlock(sd);
771 1.116.2.4 thorpej return (error);
772 1.116.2.2 thorpej
773 1.116.2.2 thorpej case DIOCWLABEL:
774 1.116.2.2 thorpej if ((flag & FWRITE) == 0)
775 1.116.2.4 thorpej return (EBADF);
776 1.116.2.2 thorpej if (*(int *)addr)
777 1.116.2.2 thorpej sd->flags |= SDF_WLABEL;
778 1.116.2.2 thorpej else
779 1.116.2.2 thorpej sd->flags &= ~SDF_WLABEL;
780 1.116.2.4 thorpej return (0);
781 1.116.2.2 thorpej
782 1.116.2.2 thorpej case DIOCLOCK:
783 1.116.2.4 thorpej return (scsipi_prevent(sd->sc_link,
784 1.116.2.4 thorpej (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
785 1.116.2.2 thorpej
786 1.116.2.2 thorpej case DIOCEJECT:
787 1.116.2.2 thorpej return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
788 1.116.2.2 thorpej scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
789 1.116.2.2 thorpej
790 1.116.2.4 thorpej case DIOCGDEFLABEL:
791 1.116.2.4 thorpej sdgetdefaultlabel(sd, (struct disklabel *)addr);
792 1.116.2.4 thorpej return (0);
793 1.116.2.4 thorpej
794 1.116.2.2 thorpej default:
795 1.116.2.2 thorpej if (SDPART(dev) != RAW_PART)
796 1.116.2.4 thorpej return (ENOTTY);
797 1.116.2.4 thorpej return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p));
798 1.116.2.2 thorpej }
799 1.116.2.2 thorpej
800 1.116.2.2 thorpej #ifdef DIAGNOSTIC
801 1.116.2.2 thorpej panic("sdioctl: impossible");
802 1.116.2.2 thorpej #endif
803 1.116.2.2 thorpej }
804 1.116.2.2 thorpej
805 1.116.2.2 thorpej void
806 1.116.2.4 thorpej sdgetdefaultlabel(sd, lp)
807 1.116.2.2 thorpej struct sd_softc *sd;
808 1.116.2.4 thorpej struct disklabel *lp;
809 1.116.2.2 thorpej {
810 1.116.2.2 thorpej
811 1.116.2.2 thorpej bzero(lp, sizeof(struct disklabel));
812 1.116.2.2 thorpej
813 1.116.2.2 thorpej lp->d_secsize = sd->params.blksize;
814 1.116.2.2 thorpej lp->d_ntracks = sd->params.heads;
815 1.116.2.2 thorpej lp->d_nsectors = sd->params.sectors;
816 1.116.2.2 thorpej lp->d_ncylinders = sd->params.cyls;
817 1.116.2.2 thorpej lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
818 1.116.2.2 thorpej
819 1.116.2.2 thorpej if (sd->type == T_OPTICAL)
820 1.116.2.2 thorpej strncpy(lp->d_typename, "SCSI optical", 16);
821 1.116.2.2 thorpej else
822 1.116.2.2 thorpej strncpy(lp->d_typename, "SCSI disk", 16);
823 1.116.2.2 thorpej lp->d_type = DTYPE_SCSI;
824 1.116.2.2 thorpej strncpy(lp->d_packname, "fictitious", 16);
825 1.116.2.2 thorpej lp->d_secperunit = sd->params.disksize;
826 1.116.2.2 thorpej lp->d_rpm = 3600;
827 1.116.2.2 thorpej lp->d_interleave = 1;
828 1.116.2.2 thorpej lp->d_flags = 0;
829 1.116.2.2 thorpej
830 1.116.2.2 thorpej lp->d_partitions[RAW_PART].p_offset = 0;
831 1.116.2.2 thorpej lp->d_partitions[RAW_PART].p_size =
832 1.116.2.2 thorpej lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
833 1.116.2.2 thorpej lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
834 1.116.2.2 thorpej lp->d_npartitions = RAW_PART + 1;
835 1.116.2.2 thorpej
836 1.116.2.2 thorpej lp->d_magic = DISKMAGIC;
837 1.116.2.2 thorpej lp->d_magic2 = DISKMAGIC;
838 1.116.2.2 thorpej lp->d_checksum = dkcksum(lp);
839 1.116.2.4 thorpej }
840 1.116.2.4 thorpej
841 1.116.2.4 thorpej
842 1.116.2.4 thorpej /*
843 1.116.2.4 thorpej * Load the label information on the named device
844 1.116.2.4 thorpej */
845 1.116.2.4 thorpej void
846 1.116.2.4 thorpej sdgetdisklabel(sd)
847 1.116.2.4 thorpej struct sd_softc *sd;
848 1.116.2.4 thorpej {
849 1.116.2.4 thorpej struct disklabel *lp = sd->sc_dk.dk_label;
850 1.116.2.4 thorpej char *errstring;
851 1.116.2.4 thorpej
852 1.116.2.4 thorpej bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
853 1.116.2.4 thorpej
854 1.116.2.4 thorpej sdgetdefaultlabel(sd, lp);
855 1.116.2.4 thorpej
856 1.116.2.4 thorpej if (lp->d_secpercyl == 0) {
857 1.116.2.4 thorpej lp->d_secpercyl = 100;
858 1.116.2.4 thorpej /* as long as it's not 0 - readdisklabel divides by it (?) */
859 1.116.2.4 thorpej }
860 1.116.2.2 thorpej
861 1.116.2.2 thorpej /*
862 1.116.2.2 thorpej * Call the generic disklabel extraction routine
863 1.116.2.2 thorpej */
864 1.116.2.2 thorpej errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
865 1.116.2.4 thorpej sdstrategy, lp, sd->sc_dk.dk_cpulabel);
866 1.116.2.2 thorpej if (errstring) {
867 1.116.2.2 thorpej printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
868 1.116.2.2 thorpej return;
869 1.116.2.2 thorpej }
870 1.116.2.2 thorpej }
871 1.116.2.2 thorpej
872 1.116.2.2 thorpej /*
873 1.116.2.2 thorpej * Tell the device to map out a defective block
874 1.116.2.2 thorpej */
875 1.116.2.2 thorpej int
876 1.116.2.2 thorpej sd_reassign_blocks(sd, blkno)
877 1.116.2.2 thorpej struct sd_softc *sd;
878 1.116.2.2 thorpej u_long blkno;
879 1.116.2.2 thorpej {
880 1.116.2.2 thorpej struct scsi_reassign_blocks scsipi_cmd;
881 1.116.2.2 thorpej struct scsi_reassign_blocks_data rbdata;
882 1.116.2.2 thorpej
883 1.116.2.2 thorpej bzero(&scsipi_cmd, sizeof(scsipi_cmd));
884 1.116.2.2 thorpej bzero(&rbdata, sizeof(rbdata));
885 1.116.2.2 thorpej scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
886 1.116.2.2 thorpej
887 1.116.2.2 thorpej _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
888 1.116.2.2 thorpej _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
889 1.116.2.2 thorpej
890 1.116.2.4 thorpej return ((*sd->sc_link->scsipi_cmd)(sd->sc_link,
891 1.116.2.4 thorpej (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
892 1.116.2.4 thorpej (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
893 1.116.2.4 thorpej SCSI_DATA_OUT));
894 1.116.2.2 thorpej }
895 1.116.2.2 thorpej
896 1.116.2.2 thorpej
897 1.116.2.2 thorpej
898 1.116.2.2 thorpej static int
899 1.116.2.2 thorpej sd_mode_sense(sd, scsipi_sense, page, flags)
900 1.116.2.2 thorpej struct sd_softc *sd;
901 1.116.2.2 thorpej struct scsi_mode_sense_data *scsipi_sense;
902 1.116.2.2 thorpej int page, flags;
903 1.116.2.2 thorpej {
904 1.116.2.2 thorpej struct scsi_mode_sense scsipi_cmd;
905 1.116.2.2 thorpej
906 1.116.2.2 thorpej /*
907 1.116.2.2 thorpej * Make sure the sense buffer is clean before we do
908 1.116.2.2 thorpej * the mode sense, so that checks for bogus values of
909 1.116.2.2 thorpej * 0 will work in case the mode sense fails.
910 1.116.2.2 thorpej */
911 1.116.2.2 thorpej bzero(scsipi_sense, sizeof(*scsipi_sense));
912 1.116.2.2 thorpej
913 1.116.2.2 thorpej bzero(&scsipi_cmd, sizeof(scsipi_cmd));
914 1.116.2.2 thorpej scsipi_cmd.opcode = SCSI_MODE_SENSE;
915 1.116.2.2 thorpej scsipi_cmd.page = page;
916 1.116.2.2 thorpej scsipi_cmd.length = 0x20;
917 1.116.2.2 thorpej /*
918 1.116.2.2 thorpej * If the command worked, use the results to fill out
919 1.116.2.2 thorpej * the parameter structure
920 1.116.2.2 thorpej */
921 1.116.2.4 thorpej return ((*sd->sc_link->scsipi_cmd)(sd->sc_link,
922 1.116.2.4 thorpej (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
923 1.116.2.4 thorpej (u_char *)scsipi_sense, sizeof(*scsipi_sense),
924 1.116.2.4 thorpej SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN | SCSI_SILENT));
925 1.116.2.2 thorpej }
926 1.116.2.2 thorpej
927 1.116.2.2 thorpej int
928 1.116.2.2 thorpej sd_get_optparms(sd, flags, dp)
929 1.116.2.2 thorpej struct sd_softc *sd;
930 1.116.2.2 thorpej int flags;
931 1.116.2.2 thorpej struct disk_parms *dp;
932 1.116.2.2 thorpej {
933 1.116.2.2 thorpej struct scsi_mode_sense scsipi_cmd;
934 1.116.2.2 thorpej struct scsi_mode_sense_data {
935 1.116.2.2 thorpej struct scsi_mode_header header;
936 1.116.2.2 thorpej struct scsi_blk_desc blk_desc;
937 1.116.2.2 thorpej union scsi_disk_pages pages;
938 1.116.2.2 thorpej } scsipi_sense;
939 1.116.2.2 thorpej u_long sectors;
940 1.116.2.2 thorpej int error;
941 1.116.2.2 thorpej
942 1.116.2.2 thorpej dp->blksize = 512;
943 1.116.2.2 thorpej if ((sectors = scsipi_size(sd->sc_link, flags)) == 0)
944 1.116.2.4 thorpej return (1);
945 1.116.2.2 thorpej
946 1.116.2.2 thorpej /* XXX
947 1.116.2.2 thorpej * It is better to get the following params from the
948 1.116.2.2 thorpej * mode sense page 6 only (optical device parameter page).
949 1.116.2.2 thorpej * However, there are stupid optical devices which does NOT
950 1.116.2.2 thorpej * support the page 6. Ghaa....
951 1.116.2.2 thorpej */
952 1.116.2.2 thorpej bzero(&scsipi_cmd, sizeof(scsipi_cmd));
953 1.116.2.2 thorpej scsipi_cmd.opcode = SCSI_MODE_SENSE;
954 1.116.2.2 thorpej scsipi_cmd.page = 0x3f; /* all pages */
955 1.116.2.2 thorpej scsipi_cmd.length = sizeof(struct scsi_mode_header) +
956 1.116.2.2 thorpej sizeof(struct scsi_blk_desc);
957 1.116.2.2 thorpej
958 1.116.2.4 thorpej if ((error = (*sd->sc_link->scsipi_cmd)(sd->sc_link,
959 1.116.2.2 thorpej (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
960 1.116.2.2 thorpej (u_char *)&scsipi_sense, sizeof(scsipi_sense), SDRETRIES,
961 1.116.2.2 thorpej 6000, NULL, flags | SCSI_DATA_IN)) != 0)
962 1.116.2.4 thorpej return (error);
963 1.116.2.2 thorpej
964 1.116.2.2 thorpej dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
965 1.116.2.2 thorpej if (dp->blksize == 0)
966 1.116.2.2 thorpej dp->blksize = 512;
967 1.116.2.2 thorpej
968 1.116.2.2 thorpej /*
969 1.116.2.2 thorpej * Create a pseudo-geometry.
970 1.116.2.2 thorpej */
971 1.116.2.2 thorpej dp->heads = 64;
972 1.116.2.2 thorpej dp->sectors = 32;
973 1.116.2.2 thorpej dp->cyls = sectors / (dp->heads * dp->sectors);
974 1.116.2.2 thorpej dp->disksize = sectors;
975 1.116.2.2 thorpej
976 1.116.2.4 thorpej return (0);
977 1.116.2.2 thorpej }
978 1.116.2.2 thorpej
979 1.116.2.2 thorpej /*
980 1.116.2.2 thorpej * Get the scsi driver to send a full inquiry to the * device and use the
981 1.116.2.2 thorpej * results to fill out the disk parameter structure.
982 1.116.2.2 thorpej */
983 1.116.2.2 thorpej int
984 1.116.2.2 thorpej sd_get_parms(sd, flags)
985 1.116.2.2 thorpej struct sd_softc *sd;
986 1.116.2.2 thorpej int flags;
987 1.116.2.2 thorpej {
988 1.116.2.2 thorpej struct disk_parms *dp = &sd->params;
989 1.116.2.2 thorpej struct scsi_mode_sense_data scsipi_sense;
990 1.116.2.2 thorpej u_long sectors;
991 1.116.2.2 thorpej int page;
992 1.116.2.2 thorpej int error;
993 1.116.2.2 thorpej
994 1.116.2.2 thorpej if (sd->type == T_OPTICAL) {
995 1.116.2.2 thorpej if ((error = sd_get_optparms(sd, flags, dp)) != 0)
996 1.116.2.2 thorpej sd->sc_link->flags &= ~SDEV_MEDIA_LOADED;
997 1.116.2.4 thorpej return (error);
998 1.116.2.2 thorpej }
999 1.116.2.2 thorpej
1000 1.116.2.2 thorpej if ((error = sd_mode_sense(sd, &scsipi_sense, page = 4, flags)) == 0) {
1001 1.116.2.2 thorpej SC_DEBUG(sd->sc_link, SDEV_DB3,
1002 1.116.2.2 thorpej ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
1003 1.116.2.2 thorpej _3btol(scsipi_sense.pages.rigid_geometry.ncyl),
1004 1.116.2.2 thorpej scsipi_sense.pages.rigid_geometry.nheads,
1005 1.116.2.2 thorpej _2btol(scsipi_sense.pages.rigid_geometry.st_cyl_wp),
1006 1.116.2.2 thorpej _2btol(scsipi_sense.pages.rigid_geometry.st_cyl_rwc),
1007 1.116.2.2 thorpej _2btol(scsipi_sense.pages.rigid_geometry.land_zone)));
1008 1.116.2.2 thorpej
1009 1.116.2.2 thorpej /*
1010 1.116.2.2 thorpej * KLUDGE!! (for zone recorded disks)
1011 1.116.2.2 thorpej * give a number of sectors so that sec * trks * cyls
1012 1.116.2.2 thorpej * is <= disk_size
1013 1.116.2.2 thorpej * can lead to wasted space! THINK ABOUT THIS !
1014 1.116.2.2 thorpej */
1015 1.116.2.2 thorpej dp->heads = scsipi_sense.pages.rigid_geometry.nheads;
1016 1.116.2.2 thorpej dp->cyls = _3btol(scsipi_sense.pages.rigid_geometry.ncyl);
1017 1.116.2.2 thorpej dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
1018 1.116.2.2 thorpej
1019 1.116.2.2 thorpej if (dp->heads == 0 || dp->cyls == 0)
1020 1.116.2.2 thorpej goto fake_it;
1021 1.116.2.2 thorpej
1022 1.116.2.2 thorpej if (dp->blksize == 0)
1023 1.116.2.2 thorpej dp->blksize = 512;
1024 1.116.2.2 thorpej
1025 1.116.2.2 thorpej sectors = scsipi_size(sd->sc_link, flags);
1026 1.116.2.2 thorpej dp->disksize = sectors;
1027 1.116.2.2 thorpej sectors /= (dp->heads * dp->cyls);
1028 1.116.2.2 thorpej dp->sectors = sectors; /* XXX dubious on SCSI */
1029 1.116.2.2 thorpej
1030 1.116.2.4 thorpej return (0);
1031 1.116.2.2 thorpej }
1032 1.116.2.2 thorpej
1033 1.116.2.2 thorpej if ((error = sd_mode_sense(sd, &scsipi_sense, page = 5, flags)) == 0) {
1034 1.116.2.2 thorpej dp->heads = scsipi_sense.pages.flex_geometry.nheads;
1035 1.116.2.2 thorpej dp->cyls = _2btol(scsipi_sense.pages.flex_geometry.ncyl);
1036 1.116.2.2 thorpej dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
1037 1.116.2.2 thorpej dp->sectors = scsipi_sense.pages.flex_geometry.ph_sec_tr;
1038 1.116.2.2 thorpej dp->disksize = dp->heads * dp->cyls * dp->sectors;
1039 1.116.2.2 thorpej if (dp->disksize == 0)
1040 1.116.2.2 thorpej goto fake_it;
1041 1.116.2.2 thorpej
1042 1.116.2.2 thorpej if (dp->blksize == 0)
1043 1.116.2.2 thorpej dp->blksize = 512;
1044 1.116.2.2 thorpej
1045 1.116.2.4 thorpej return (0);
1046 1.116.2.2 thorpej }
1047 1.116.2.2 thorpej
1048 1.116.2.2 thorpej fake_it:
1049 1.116.2.2 thorpej if ((sd->sc_link->quirks & SDEV_NOMODESENSE) == 0) {
1050 1.116.2.2 thorpej if (error == 0)
1051 1.116.2.2 thorpej printf("%s: mode sense (%d) returned nonsense",
1052 1.116.2.2 thorpej sd->sc_dev.dv_xname, page);
1053 1.116.2.2 thorpej else
1054 1.116.2.2 thorpej printf("%s: could not mode sense (4/5)",
1055 1.116.2.2 thorpej sd->sc_dev.dv_xname);
1056 1.116.2.2 thorpej printf("; using fictitious geometry\n");
1057 1.116.2.2 thorpej }
1058 1.116.2.2 thorpej /*
1059 1.116.2.2 thorpej * use adaptec standard fictitious geometry
1060 1.116.2.2 thorpej * this depends on which controller (e.g. 1542C is
1061 1.116.2.2 thorpej * different. but we have to put SOMETHING here..)
1062 1.116.2.2 thorpej */
1063 1.116.2.2 thorpej sectors = scsipi_size(sd->sc_link, flags);
1064 1.116.2.2 thorpej dp->heads = 64;
1065 1.116.2.2 thorpej dp->sectors = 32;
1066 1.116.2.2 thorpej dp->cyls = sectors / (64 * 32);
1067 1.116.2.2 thorpej dp->blksize = 512;
1068 1.116.2.2 thorpej dp->disksize = sectors;
1069 1.116.2.4 thorpej return (0);
1070 1.116.2.2 thorpej }
1071 1.116.2.2 thorpej
1072 1.116.2.2 thorpej int
1073 1.116.2.2 thorpej sdsize(dev)
1074 1.116.2.2 thorpej dev_t dev;
1075 1.116.2.2 thorpej {
1076 1.116.2.2 thorpej struct sd_softc *sd;
1077 1.116.2.2 thorpej int part, unit, omask;
1078 1.116.2.2 thorpej int size;
1079 1.116.2.2 thorpej
1080 1.116.2.2 thorpej unit = SDUNIT(dev);
1081 1.116.2.2 thorpej if (unit >= sd_cd.cd_ndevs)
1082 1.116.2.2 thorpej return (-1);
1083 1.116.2.2 thorpej sd = sd_cd.cd_devs[unit];
1084 1.116.2.2 thorpej if (sd == NULL)
1085 1.116.2.2 thorpej return (-1);
1086 1.116.2.2 thorpej
1087 1.116.2.2 thorpej part = SDPART(dev);
1088 1.116.2.2 thorpej omask = sd->sc_dk.dk_openmask & (1 << part);
1089 1.116.2.2 thorpej
1090 1.116.2.2 thorpej if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1091 1.116.2.2 thorpej return (-1);
1092 1.116.2.2 thorpej if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1093 1.116.2.2 thorpej size = -1;
1094 1.116.2.2 thorpej else
1095 1.116.2.2 thorpej size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1096 1.116.2.2 thorpej (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1097 1.116.2.2 thorpej if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1098 1.116.2.2 thorpej return (-1);
1099 1.116.2.2 thorpej return (size);
1100 1.116.2.2 thorpej }
1101 1.116.2.2 thorpej
1102 1.116.2.2 thorpej #ifndef __BDEVSW_DUMP_OLD_TYPE
1103 1.116.2.2 thorpej /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1104 1.116.2.2 thorpej static struct scsipi_xfer sx;
1105 1.116.2.2 thorpej static int sddoingadump;
1106 1.116.2.2 thorpej
1107 1.116.2.2 thorpej /*
1108 1.116.2.2 thorpej * dump all of physical memory into the partition specified, starting
1109 1.116.2.2 thorpej * at offset 'dumplo' into the partition.
1110 1.116.2.2 thorpej */
1111 1.116.2.2 thorpej int
1112 1.116.2.2 thorpej sddump(dev, blkno, va, size)
1113 1.116.2.2 thorpej dev_t dev;
1114 1.116.2.2 thorpej daddr_t blkno;
1115 1.116.2.2 thorpej caddr_t va;
1116 1.116.2.2 thorpej size_t size;
1117 1.116.2.2 thorpej {
1118 1.116.2.2 thorpej struct sd_softc *sd; /* disk unit to do the I/O */
1119 1.116.2.2 thorpej struct disklabel *lp; /* disk's disklabel */
1120 1.116.2.2 thorpej int unit, part;
1121 1.116.2.2 thorpej int sectorsize; /* size of a disk sector */
1122 1.116.2.2 thorpej int nsects; /* number of sectors in partition */
1123 1.116.2.2 thorpej int sectoff; /* sector offset of partition */
1124 1.116.2.2 thorpej int totwrt; /* total number of sectors left to write */
1125 1.116.2.2 thorpej int nwrt; /* current number of sectors to write */
1126 1.116.2.2 thorpej struct scsipi_rw_big cmd; /* write command */
1127 1.116.2.2 thorpej struct scsipi_xfer *xs; /* ... convenience */
1128 1.116.2.2 thorpej int retval;
1129 1.116.2.2 thorpej
1130 1.116.2.2 thorpej /* Check if recursive dump; if so, punt. */
1131 1.116.2.2 thorpej if (sddoingadump)
1132 1.116.2.4 thorpej return (EFAULT);
1133 1.116.2.2 thorpej
1134 1.116.2.2 thorpej /* Mark as active early. */
1135 1.116.2.2 thorpej sddoingadump = 1;
1136 1.116.2.2 thorpej
1137 1.116.2.2 thorpej unit = SDUNIT(dev); /* Decompose unit & partition. */
1138 1.116.2.2 thorpej part = SDPART(dev);
1139 1.116.2.2 thorpej
1140 1.116.2.2 thorpej /* Check for acceptable drive number. */
1141 1.116.2.2 thorpej if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1142 1.116.2.4 thorpej return (ENXIO);
1143 1.116.2.2 thorpej
1144 1.116.2.2 thorpej /*
1145 1.116.2.2 thorpej * XXX Can't do this check, since the media might have been
1146 1.116.2.2 thorpej * XXX marked `invalid' by successful unmounting of all
1147 1.116.2.2 thorpej * XXX filesystems.
1148 1.116.2.2 thorpej */
1149 1.116.2.2 thorpej #if 0
1150 1.116.2.2 thorpej /* Make sure it was initialized. */
1151 1.116.2.2 thorpej if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1152 1.116.2.4 thorpej return (ENXIO);
1153 1.116.2.2 thorpej #endif
1154 1.116.2.2 thorpej
1155 1.116.2.2 thorpej /* Convert to disk sectors. Request must be a multiple of size. */
1156 1.116.2.2 thorpej lp = sd->sc_dk.dk_label;
1157 1.116.2.2 thorpej sectorsize = lp->d_secsize;
1158 1.116.2.2 thorpej if ((size % sectorsize) != 0)
1159 1.116.2.4 thorpej return (EFAULT);
1160 1.116.2.2 thorpej totwrt = size / sectorsize;
1161 1.116.2.2 thorpej blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1162 1.116.2.2 thorpej
1163 1.116.2.2 thorpej nsects = lp->d_partitions[part].p_size;
1164 1.116.2.2 thorpej sectoff = lp->d_partitions[part].p_offset;
1165 1.116.2.2 thorpej
1166 1.116.2.2 thorpej /* Check transfer bounds against partition size. */
1167 1.116.2.2 thorpej if ((blkno < 0) || ((blkno + totwrt) > nsects))
1168 1.116.2.4 thorpej return (EINVAL);
1169 1.116.2.2 thorpej
1170 1.116.2.2 thorpej /* Offset block number to start of partition. */
1171 1.116.2.2 thorpej blkno += sectoff;
1172 1.116.2.2 thorpej
1173 1.116.2.2 thorpej xs = &sx;
1174 1.116.2.2 thorpej
1175 1.116.2.2 thorpej while (totwrt > 0) {
1176 1.116.2.2 thorpej nwrt = totwrt; /* XXX */
1177 1.116.2.2 thorpej #ifndef SD_DUMP_NOT_TRUSTED
1178 1.116.2.2 thorpej /*
1179 1.116.2.2 thorpej * Fill out the scsi command
1180 1.116.2.2 thorpej */
1181 1.116.2.2 thorpej bzero(&cmd, sizeof(cmd));
1182 1.116.2.2 thorpej cmd.opcode = WRITE_BIG;
1183 1.116.2.2 thorpej _lto4b(blkno, cmd.addr);
1184 1.116.2.2 thorpej _lto2b(nwrt, cmd.length);
1185 1.116.2.2 thorpej /*
1186 1.116.2.2 thorpej * Fill out the scsipi_xfer structure
1187 1.116.2.2 thorpej * Note: we cannot sleep as we may be an interrupt
1188 1.116.2.2 thorpej * don't use sc_link->scsipi_cmd() as it may want
1189 1.116.2.2 thorpej * to wait for an xs.
1190 1.116.2.2 thorpej */
1191 1.116.2.2 thorpej bzero(xs, sizeof(sx));
1192 1.116.2.2 thorpej xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1193 1.116.2.2 thorpej xs->sc_link = sd->sc_link;
1194 1.116.2.2 thorpej xs->retries = SDRETRIES;
1195 1.116.2.2 thorpej xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1196 1.116.2.2 thorpej xs->cmd = (struct scsipi_generic *)&cmd;
1197 1.116.2.2 thorpej xs->cmdlen = sizeof(cmd);
1198 1.116.2.2 thorpej xs->resid = nwrt * sectorsize;
1199 1.116.2.2 thorpej xs->error = XS_NOERROR;
1200 1.116.2.2 thorpej xs->bp = 0;
1201 1.116.2.2 thorpej xs->data = va;
1202 1.116.2.2 thorpej xs->datalen = nwrt * sectorsize;
1203 1.116.2.2 thorpej
1204 1.116.2.2 thorpej /*
1205 1.116.2.2 thorpej * Pass all this info to the scsi driver.
1206 1.116.2.2 thorpej */
1207 1.116.2.4 thorpej retval = (*sd->sc_link->adapter->scsipi_cmd)(xs);
1208 1.116.2.2 thorpej if (retval != COMPLETE)
1209 1.116.2.4 thorpej return (ENXIO);
1210 1.116.2.2 thorpej #else /* SD_DUMP_NOT_TRUSTED */
1211 1.116.2.2 thorpej /* Let's just talk about this first... */
1212 1.116.2.2 thorpej printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1213 1.116.2.2 thorpej delay(500 * 1000); /* half a second */
1214 1.116.2.2 thorpej #endif /* SD_DUMP_NOT_TRUSTED */
1215 1.116.2.2 thorpej
1216 1.116.2.2 thorpej /* update block count */
1217 1.116.2.2 thorpej totwrt -= nwrt;
1218 1.116.2.2 thorpej blkno += nwrt;
1219 1.116.2.2 thorpej va += sectorsize * nwrt;
1220 1.116.2.2 thorpej }
1221 1.116.2.2 thorpej sddoingadump = 0;
1222 1.116.2.4 thorpej return (0);
1223 1.116.2.2 thorpej }
1224 1.116.2.2 thorpej #else /* __BDEVSW_DUMP_NEW_TYPE */
1225 1.116.2.2 thorpej int
1226 1.116.2.2 thorpej sddump(dev, blkno, va, size)
1227 1.116.2.2 thorpej dev_t dev;
1228 1.116.2.2 thorpej daddr_t blkno;
1229 1.116.2.2 thorpej caddr_t va;
1230 1.116.2.2 thorpej size_t size;
1231 1.116.2.2 thorpej {
1232 1.116.2.2 thorpej
1233 1.116.2.2 thorpej /* Not implemented. */
1234 1.116.2.4 thorpej return (ENXIO);
1235 1.116.2.2 thorpej }
1236 1.116.2.2 thorpej #endif /* __BDEVSW_DUMP_NEW_TYPE */
1237