~kris/9p

9hist

ref: d58cfd17b00a5f5d9d8df46dccb720f469165593 9hist/power/devhotrod.c -rw-r--r-- 7.7 KiB
d58cfd17 — David du Colombier Plan 9 from Bell Labs 1992-04-29 34 years ago
                                                                                
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#define	BITBOTCH	256	/* remove with BIT3 */
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"devtab.h"
#include	"fcall.h"

#include	"io.h"
#include	"../../fs/cyc/comm.h"


typedef struct Commrod	Commrod;

enum
{
	Vmevec=		0xd2,		/* vme vector for interrupts */
	Intlevel=	5,		/* level to interrupt on */
	Qdir=		0,		/* Qid's */
	Qhotrod=	1,
	Nhotrod=	1,
};

Dirtab hotroddir[]={
	"hotrod",	{Qhotrod},	0,	0600,
};

#define	NHOTRODDIR	(sizeof hotroddir/sizeof(Dirtab))

struct Commrod
{
	Lock;
	QLock		buflock;
	Lock		busy;
	Comm		*addr;		/* address of the device */
	int		vec;		/* vme interrupt vector */
	int		wi;		/* where to write next cmd */
	int		ri;		/* where to read next reply */
	uchar		buf[MAXFDATA+MAXMSG+BITBOTCH];
};

Commrod hotrod[Nhotrod];

void	hotrodintr(int);

#define	HOTROD	VMEA24SUP(Comm, 0x10000);

/*
 * Commands
 */

Hotmsg**
hotsend(Commrod *h, Hotmsg *m)
{
	Hotmsg **mp;
int i;

	lock(h);
	mp = (Hotmsg**)&h->addr->reqstq[h->wi];
	*mp = (Hotmsg*)MP2VME(m);
	h->wi++;
	if(h->wi >= NRQ)
		h->wi = 0;
	unlock(h);
	return mp;
}

void
hotwait(Hotmsg **mp)
{
	ulong l;

	l = 0;
	while(*mp){
		delay(0);	/* just a subroutine call; stay off VME */
		if(++l > 1000*1000){
			l = 0;
			print("hotsend blocked\n");
		}
	}
	return;
}

/*
 *  reset all hotrod boards
 */
void
hotrodreset(void)
{
	int i;
	Commrod *hp;

	for(hp=hotrod,i=0; i<Nhotrod; i++,hp++){
		hp->addr = HOTROD+i;
		/*
		 * Write queue is at end of hotrod memory
		 */
		hp->vec = Vmevec+i;
		setvmevec(hp->vec, hotrodintr);
	}	
}

void
hotrodinit(void)
{
}

/*
 *  enable the device for interrupts, spec is the device number
 */
Chan*
hotrodattach(char *spec)
{
	Commrod *hp;
	int i;
	Chan *c;

	i = strtoul(spec, 0, 0);
	if(i >= Nhotrod)
		error(Ebadarg);

	c = devattach('H', spec);
	c->dev = i;
	c->qid.path = CHDIR;
	c->qid.vers = 0;
	return c;
}

Chan*
hotrodclone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

int	 
hotrodwalk(Chan *c, char *name)
{
	return devwalk(c, name, hotroddir, NHOTRODDIR, devgen);
}

void	 
hotrodstat(Chan *c, char *dp)
{
	devstat(c, dp, hotroddir, NHOTRODDIR, devgen);
}

Chan*
hotrodopen(Chan *c, int omode)
{
	Commrod *hp;
	Hotmsg *mp, **hmp;

	if(c->qid.path == CHDIR){
		if(omode != OREAD)
			error(Eperm);
	}else if(c->qid.path == Qhotrod){
		hp = &hotrod[c->dev];
		if(!canlock(&hp->busy))
			error(Einuse);
		/*
		 * Clear communications region
		 */
		memset(hp->addr->reqstq, 0, NRQ*sizeof(ulong));
		hp->addr->reqstp = 0;
		memset(hp->addr->replyq, 0, NRQ*sizeof(ulong));
		hp->addr->replyp = 0;

		/*
		 * Issue reset
		 */
		hp->wi = 0;
		hp->ri = 0;
		mp = &u->khot;
		mp->cmd = Ureset;
		hmp = hotsend(hp, &((User*)(u->p->upage->pa|KZERO))->khot);
		hotwait(hmp);
		delay(100);
		print("reset\n");
	}
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

void	 
hotrodcreate(Chan *c, char *name, int omode, ulong perm)
{
	USED(c, name, omode, perm);
	error(Eperm);
}

void	 
hotrodclose(Chan *c)
{
	Commrod *hp;
	Hotmsg **hmp;

	hp = &hotrod[c->dev];
	if(c->qid.path != CHDIR){
		u->khot.cmd = Ureboot;
		hmp = hotsend(hp, &((User*)(u->p->upage->pa|KZERO))->khot);
		hotwait(hmp);
		unlock(&hp->busy);
	}
}

ulong
hotsum(ulong *p, int n, ulong doit)
{
	ulong sum;

	if(!doit)
		return 0;
	sum = 0;
	n = (n+sizeof(ulong)-1)/sizeof(ulong);
	while(--n >= 0)
		sum += *p++;
	return sum;
}

int
hotmsgintr(Hotmsg *hm)
{
	return hm->intr;
}

/*
 * Read and write use physical addresses if they can, which they usually can.
 * Most I/O is from devmnt, which has local buffers.  Therefore just check
 * that buf is in KSEG0 and is at an even address.
 */

long	 
hotrodread(Chan *c, void *buf, long n, ulong offset)
{
	Commrod *hp;
	Hotmsg *mp, **hmp;
	ulong l, m, isflush;

	hp = &hotrod[c->dev];
	switch(c->qid.path & ~CHDIR){
	case Qdir:
		return devdirread(c, buf, n, hotroddir, NHOTRODDIR, devgen);

	case Qhotrod:
		if(n > sizeof hp->buf){
			print("hotrod bufsize %d %d\n", n, sizeof hp->buf);
			error(Egreg);
		}
		if((((ulong)buf)&(KSEGM|3)) == KSEG0){
			/*
			 *  use supplied buffer, no need to lock for reply
			 */
			isflush = 0;
			mp = &((User*)(u->p->upage->pa|KZERO))->khot;
			if(mp->abort){	/* use reserved flush msg */
				mp = &((User*)(u->p->upage->pa|KZERO))->fhot;
				isflush = 1;
			}
			mp->param[2] = 0;	/* reply checksum */
			mp->param[3] = 0;	/* reply count */
			mp->cmd = Uread;
			mp->param[0] = MP2VME(buf);
			mp->param[1] = n;
			mp->abort = isflush;
			mp->intr = 0;
			hmp = hotsend(hp, mp);
			if(isflush){		/* busy loop */
				l = 100*1000*1000;
				do
					m = mp->param[3];
				while(m==0 && --l>0);
			}else{
				if(waserror()){
					if(*hmp && *hmp==mp)
						hotwait(hmp);
					mp->abort = 1;
					nexterror();
				}
				sleep(&mp->r, hotmsgintr, mp);
				m = mp->param[3];
			}
			if(m==0 || m>n){
				print("devhotrod: count %ld %ld\n", m, n);
				error(Egreg);
			}
			if(mp->param[2] != hotsum(buf, m, mp->param[2])){
				print("hotrod cksum err is %lux sb %lux\n",
					hotsum(buf, m, 1), mp->param[2]);
				error(Eio);
			}
			mp->abort = 0;
			if(isflush)
				u->khot.abort = 0;	/* flushed message's done too */
			else
				poperror();
		}else{
			/*
			 * use hotrod buffer. lock the buffer until the reply
			 */
			mp = &((User*)(u->p->upage->pa|KZERO))->uhot;
			mp->param[2] = 0;	/* reply checksum */
			mp->param[3] = 0;	/* reply count */
			qlock(&hp->buflock);
			mp->cmd = Uread;
			mp->param[0] = MP2VME(hp->buf);
			mp->param[1] = n;
			mp->abort = 1;
			mp->intr = 0;
			hmp = hotsend(hp, mp);
			hotwait(hmp);
			l = 100*1000*1000;
			do
				m = mp->param[3];
			while(m==0 && --l>0);
			if(m==0 || m>n){
				print("devhotrod: count %ld %ld\n", m, n);
				qunlock(&hp->buflock);
				error(Egreg);
			}
			if(mp->param[2] != hotsum((ulong*)hp->buf, m, mp->param[2])){
				print("hotrod cksum err is %lux sb %lux\n",
					hotsum((ulong*)hp->buf, m, 1), mp->param[2]);
				qunlock(&hp->buflock);
				error(Eio);
			}
			memmove(buf, hp->buf, m);
			mp->abort = 0;
			qunlock(&hp->buflock);
		}
		return m;
	}
	print("hotrod read unk\n");
	error(Egreg);
	return 0;
}

long	 
hotrodwrite(Chan *c, void *buf, long n, ulong offset)
{
	Commrod *hp;
	Hotmsg *mp, **hmp;

	hp = &hotrod[c->dev];
	switch(c->qid.path & ~CHDIR){
	case Qdir:
		return devdirread(c, buf, n, hotroddir, NHOTRODDIR, devgen);

	case Qhotrod:
		if(n > sizeof hp->buf){
			print("hotrod write bufsize\n");
			error(Egreg);
		}
		if((((ulong)buf)&(KSEGM|3)) == KSEG0){
			/*
			 * use supplied buffer, no need to lock for reply
			 */
			mp = &((User*)(u->p->upage->pa|KZERO))->khot;
			mp->wlen = n;
			if(mp->abort)	/* use reserved flush msg */
				mp = &((User*)(u->p->upage->pa|KZERO))->fhot;
			mp->cmd = Uwrite;
			mp->param[0] = MP2VME(buf);
			mp->param[1] = n;
			mp->param[2] = 0;
			hmp = hotsend(hp, mp);
			hotwait(hmp);
		}else{
			/*
			 * use hotrod buffer.  lock the buffer until the reply
			 */
			mp = &((User*)(u->p->upage->pa|KZERO))->uhot;
			mp->wlen = n;
			qlock(&hp->buflock);
			memmove(hp->buf, buf, n);
			mp->cmd = Uwrite;
			mp->param[0] = MP2VME(hp->buf);
			mp->param[1] = n;
			mp->param[2] = 0;
			hmp = hotsend(hp, mp);
			hotwait(hmp);
			qunlock(&hp->buflock);
		}
		return n;
	}
	print("hotrod write unk\n");
	error(Egreg);
	return 0;
}

void	 
hotrodremove(Chan *c)
{
	USED(c);
	error(Eperm);
}

void	 
hotrodwstat(Chan *c, char *dp)
{
	USED(c, dp);
	error(Eperm);
}

void
hotrodintr(int vec)
{
	Commrod *h;
	Hotmsg *hm;
	ulong l;

	h = &hotrod[vec - Vmevec];
	if(h<hotrod || h>&hotrod[Nhotrod]){
		print("bad hotrod vec\n");
		return;
	}
	while(l = h->addr->replyq[h->ri]){	/* assign = */
		hm = (Hotmsg*)(VME2MP(l));
		h->addr->replyq[h->ri] = 0;
		h->ri++;
		if(h->ri >= NRQ)
			h->ri = 0;
		hm->intr = 1;
		if(!hm->abort)
			wakeup(&hm->r);
	}
}