summaryrefslogtreecommitdiffstats
path: root/sys/contrib/dev/fla/fla.c
blob: 3f3aaa06017c9939c2e019b82c9ac7dd28dc1bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * $FreeBSD$ 
 *
 */

#include "fla.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <sys/disklabel.h>
#include <sys/diskslice.h>
#include <sys/devicestat.h>
#include <sys/module.h>
#include <sys/conf.h>
#include <machine/bus.h>
#include <machine/clock.h>
#include <machine/resource.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_param.h>

#include <sys/bus.h>
#include <isa/isareg.h>
#include <isa/isavar.h>

#ifdef SMP
#include <machine/smp.h>
#endif

#include <contrib/dev/fla/msysosak.h>

MALLOC_DEFINE(M_FLA, "fla driver", "fla driver storage");

static int fla_debug = 0;
SYSCTL_INT(_debug, OID_AUTO, fladebug, CTLFLAG_RW, &fla_debug, 0, "");

#define CDEV_MAJOR	102
#define BDEV_MAJOR	28

static d_strategy_t flastrategy;
static d_open_t flaopen;
static d_close_t flaclose;
static d_ioctl_t flaioctl;
static d_psize_t flapsize;

static struct cdevsw fla_cdevsw = {
        /* open */      flaopen,
        /* close */     flaclose,
        /* read */      physread,
        /* write */     physwrite,
        /* ioctl */     flaioctl,
        /* stop */      nostop,
        /* reset */     noreset,
        /* devtotty */  nodevtotty,
        /* poll */      nopoll,
        /* mmap */      nommap,
        /* strategy */  flastrategy,
        /* name */      "fla",
        /* parms */     noparms,
        /* maj */       CDEV_MAJOR,
        /* dump */      nodump,
        /* psize */     flapsize,
        /* flags */     D_DISK | D_CANFREE,
        /* maxio */     0,
        /* bmaj */      BDEV_MAJOR
};

void *
doc2k_malloc(int bytes) 
{
	return malloc(bytes, M_FLA, M_WAITOK);
}

void
doc2k_free(void *ptr)
{
	free(ptr, M_FLA);
}

void
doc2k_delay(unsigned msec)
{
	DELAY(1000 * msec);
}

void
doc2k_memcpy(void *dst, const void *src, unsigned len)
{
	bcopy(src, dst, len);
}

int
doc2k_memcmp(const void *dst, const void *src, unsigned len)
{
	return (bcmp(src, dst, len));
}

void
doc2k_memset(void *dst, int c, unsigned len)
{
	u_char *p = dst;
	while (len--)
		*p++ = c;
}

static struct fla_s {
	unsigned nsect;
	struct doc2k_stat ds;
	struct diskslices *dk_slices;
	struct buf_queue_head buf_queue;
	struct devstat stats;
	int busy;
} softc[NFLA];

static int
flaopen(dev_t dev, int flag, int fmt, struct proc *p)
{
	int unit;
	struct fla_s *sc;
	int error;
	struct disklabel dk_dd;

	if (fla_debug)
		printf("flaopen(%x %x %x %p)\n",
			dev2udev(dev), flag, fmt, p);
	unit = dkunit(dev);
	if (unit >= NFLA)
		return (ENXIO);
	sc = &softc[unit];
	if (!sc->nsect)
		return (ENXIO);
	bzero(&dk_dd, sizeof(dk_dd));
	error = doc2k_size(unit, &dk_dd.d_secperunit,
	    &dk_dd.d_ncylinders, &dk_dd.d_ntracks, &dk_dd.d_nsectors);
	dk_dd.d_secsize = DEV_BSIZE;
	dk_dd.d_secpercyl = dk_dd.d_ntracks * dk_dd.d_nsectors;

	error = dsopen("fla", dev, fmt, 0, &sc->dk_slices, &dk_dd);
	if (error)
		return (error);
	return (0);
}

static int
flaclose(dev_t dev, int flags, int fmt, struct proc *p)
{
	int unit;
	struct fla_s *sc;

	if (fla_debug)
		printf("flaclose(%x %x %x %p)\n",
			dev2udev(dev), flags, fmt, p);
	unit = dkunit(dev);
	sc = &softc[unit];
	dsclose(dev, fmt, sc->dk_slices);
	return (0);
}

static int
flaioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
{
	int unit, error;
	struct fla_s *sc;

	if (fla_debug)
		printf("flaioctl(%x %lx %p %x %p)\n",
			dev2udev(dev), cmd, addr, flags, p);
	unit = dkunit(dev);
	sc = &softc[unit];
	error = dsioctl("fla", dev, cmd, addr, flags, &sc->dk_slices);
	if (error == ENOIOCTL)
		error = ENOTTY;
	return (error);
}

static void
flastrategy(struct buf *bp)
{
	int unit, error;
	int s;
	struct fla_s *sc;
	enum doc2k_work what;
	devstat_trans_flags dop;

	if (fla_debug > 1)
		printf("flastrategy(%p) %x %lx, %d, %ld, %p)\n",
			    bp, dev2udev(bp->b_dev), bp->b_flags, bp->b_blkno, 
			    bp->b_bcount / DEV_BSIZE, bp->b_data);
	unit = dkunit(bp->b_dev);
	sc = &softc[unit];

	if (dscheck(bp, sc->dk_slices) <= 0) {
		biodone(bp);
		return;
	}

	s = splbio();

	bufqdisksort(&sc->buf_queue, bp);

	if (sc->busy) {
		splx(s);
		return;
	}

	sc->busy++;
	
	while (1) {
		bp = bufq_first(&sc->buf_queue);
		if (bp)
			bufq_remove(&sc->buf_queue, bp);
		splx(s);
		if (!bp)
			break;

		devstat_start_transaction(&sc->stats);
		bp->b_resid = bp->b_bcount;
		unit = dkunit(bp->b_dev);

		if (bp->b_flags & B_FREEBUF)
			what = DOC2K_ERASE, dop = DEVSTAT_NO_DATA;
		else if (bp->b_flags & B_READ)
			what = DOC2K_READ, dop = DEVSTAT_READ;
		else 
			what = DOC2K_WRITE, dop = DEVSTAT_WRITE;
#ifdef SMP
		rel_mplock();			
#endif
		error = doc2k_rwe( unit, what, bp->b_pblkno,
		    bp->b_bcount / DEV_BSIZE, bp->b_data);
#ifdef SMP
		get_mplock();			
#endif

		if (fla_debug > 1 || error) {
			printf("fla%d: %d = rwe(%p, %d, %d, %d, %ld, %p)\n",
			    unit, error, bp, unit, what, bp->b_pblkno, 
			    bp->b_bcount / DEV_BSIZE, bp->b_data);
		}
		if (error) {
			bp->b_error = EIO;
			bp->b_flags |= B_ERROR;
		} else {
			bp->b_resid = 0;
		}
		biodone(bp);
		devstat_end_transaction(&sc->stats, bp->b_bcount,
		    DEVSTAT_TAG_NONE, dop);


		s = splbio();
	}
	sc->busy = 0;
	return;
}

static int
flapsize(dev_t dev)
{
	int unit;
	struct fla_s *sc;

	if (fla_debug)
		printf("flapsize(%x)\n", dev2udev(dev));
	unit = dkunit(dev);
	sc = &softc[unit];
	if (!sc->nsect)
		return 0;

	return (dssize(dev, &sc->dk_slices));
}

static int
flarealprobe(int unit)
{
	struct fla_s *sc;
	int i;

	sc = &softc[unit];

	/* This is slightly ugly */
	i = doc2k_probe(unit, KERNBASE + 0xc0000, KERNBASE + 0xe0000);
	if (i)
		return (ENXIO);

	i = doc2k_info(unit, &sc->ds);
	if (i)
		return (ENXIO);

	return (0);
}


static int
flarealattach(int unit)
{
	int i, j, k, l, m, error;
	struct fla_s *sc;

	sc = &softc[unit];

	error = doc2k_open(unit);
	if (error) {
		printf("doc2k_open(%d) -> err %d\n", unit, error);
		return (EIO);
	}

	error = doc2k_size(unit, &sc->nsect, &i, &j, &k );
	if (error) {
		printf("doc2k_size(%d) -> err %d\n", unit, error);
		return (EIO);
	}

	printf("fla%d: <%s %s>\n", unit, sc->ds.product, sc->ds.model);
	
	m = 1024L * 1024L / DEV_BSIZE;
	l = (sc->nsect * 10 + m/2) / m;
	printf("fla%d: %d.%01dMB (%u sectors),"
	    " %d cyls, %d heads, %d S/T, 512 B/S\n",
	    unit, l / 10, l % 10, sc->nsect, i, j, k);

	if (bootverbose)
		printf("fla%d: JEDEC=0x%x unitsize=%ld mediasize=%ld"
		       " chipsize=%ld interleave=%d window=%lx\n", 
		    unit, sc->ds.type, sc->ds.unitSize, sc->ds.mediaSize, 
		    sc->ds.chipSize, sc->ds.interleaving, sc->ds.window);

	bufq_init(&sc->buf_queue);

	return (0);
}

static int
flaprobe (device_t dev)
{
	int unit;
	struct fla_s *sc;
	device_t bus;
	int i;

	unit = device_get_unit(dev);
	sc = &softc[unit];
	i = flarealprobe(unit);
	if (i)
		return (i);

	bus = device_get_parent(dev);
	ISA_SET_RESOURCE(bus, dev, SYS_RES_MEMORY, 0, 
		sc->ds.window - KERNBASE, 8192);

	return (0);
}


static int
flaattach (device_t dev)
{
	int unit, i;

	unit = device_get_unit(dev);
	i = flarealattach(unit);
	devstat_add_entry(&softc[unit].stats, "fla", unit, DEV_BSIZE,
		DEVSTAT_NO_ORDERED_TAGS, 
		DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 0x190);
	return (i);
}

static device_method_t fla_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		flaprobe),
	DEVMETHOD(device_attach,	flaattach),
	{0, 0}
};

static driver_t fladriver = {
	"fla",
	fla_methods,
	sizeof(struct fla_s),
};

static devclass_t	fla_devclass;

DEV_DRIVER_MODULE(fla, isa, fladriver, fla_devclass, fla_cdevsw, 0, 0);
OpenPOWER on IntegriCloud