summaryrefslogtreecommitdiff
path: root/NeXT/inlines.h
blob: 88e155a05014e4eaf40891a096632442bedfed2a (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
/*
 * Netbufs don't come with nifty queuing functions
 * like mbufs. We therefore make our own quques by
 * squirreling away an extra pointer before the data
 * in a netbuf. As we can't guarantee that this will
 * be aligned to anything in particular I use bcopy to
 * read and write it. bcopy can use 32 bit if it really
 * feels like it...
 * PCF
 *

#if defined(m68k)
#import "spl.h"
#else
#import <kernserv/machine/spl.h>
#endif
#include <kernserv/kern_server_types.h>
#include <kernserv/kalloc.h>
#include "nbq.h"

/*
 * There is no driver kit for the Moto release.
 */
#ifndef IOLog
#define IOLog printf
#define	IOLogDbg		if (sc->sc_flags & SC_DEBUG) printf
#else
#define	IOLogDbg		if (sc->sc_flags & SC_DEBUG) IOLog
#endif

extern kern_server_t instance;

/*
 * Careful about using this function.  Some places
 * in the code drop packets based on this count
 * but they never free them.
 */

static inline int
nbq_full(struct nb_queue* nbq)
{
    int rv;
    rv = (nbq->len >= nbq->max);
    return rv;
}

static inline int
nbq_empty(struct nb_queue* nbq)
{
    int rv;
    rv = (!nbq->head);
    return rv;
}

static inline int
nbq_low(struct nb_queue* nbq)
{
    int rv;
    rv = (nbq->len <= nbq->low);
    return rv;
}

static inline int
nbq_high(struct nb_queue* nbq)
{
    int rv;
    rv = (nbq->len >= nbq->high);
    return rv;
}

static inline NETBUF_T
nbq_peek(struct nb_queue* nbq)
{
    int s;
    NETBUF_T nb;

    s = splimp();
    nb = nbq->head;
    splx(s);
    return nb;
}

static inline NETBUF_T
nbq_dequeue(struct nb_queue* nbq)
{
  int s;
  NETBUF_T nb;

  if (!nbq->head)
      return NULL;

  s = splimp();
  nb = nbq->head;
  NB_GET_NEXT(nb,&nbq->head);
  if (!nbq->head)
	nbq->tail = NULL;
  --nbq->len;
  splx(s);

  return nb;
}

/*
 * One simple note about nbq_enqueue: it will enqueue packets even if
 * it is full, so the caller is responsible for checking this first...
 *
 * We return 1 if we added, else we return 0
 * if there was a problem. We leave it up to the caller
 * to detect an error return value (0) and print
 * an appropriate message/update stats.  However, in the spirit of
 * keeping the code as close to the netbsd version as is possible,
 * WE WILL FREE a packet that can't be enqueued.  This should be the
 * responsibility of the caller but that is currently not the case.
 *
 * Actually, now I'm using the hidden pointer arrangement then theres
 * no circumstances under which this can return 0, oh well...
 * PCF
 */

static inline int
nbq_enqueue(struct nb_queue* nbq, NETBUF_T nb)
{
  int s;

  NB_SET_NEXT(nb,NULL);
  s = splimp();
  if (nbq->tail)
    NB_SET_NEXT(nbq->tail,nb);
  else
    nbq->head = nb;
  nbq->tail = nb;
  ++nbq->len;
  splx(s);
  return 1;
}

static inline void
nbq_flush(struct nb_queue *nbq)
{
    NETBUF_T nb,temp;
    int s;

    s  = splimp();
    nb = nbq->head;
    while(nb) {
	temp=nb;
	NB_GET_NEXT(nb,&nb);
	NB_FREE(temp);
    }

    nbq->head = nbq->tail = NULL;
    nbq->len = 0;
    nbq->dropped = 0;
    splx(s);
}

/*
 * Must not be called at interrupt priority
 */

static inline void
nbq_init(struct nb_queue *nbq, struct qparms *qp)
{
  nbq->name = qp->q_name;
  nbq->head = nbq->tail = NULL;
  nbq->low = qp->q_low;
  nbq->high = qp->q_high;
  nbq->max = qp->q_max;
  nbq->len = 0;
  nbq->dropped = 0;
}

static inline void
nbq_free(struct nb_queue *nbq)
{
  nbq_flush(nbq);
}

static inline void
nbq_drop(struct nb_queue *nbq)
{
    ++nbq->dropped;
}

/*
 * Not very pretty, but it makes for less "diffs"...
 */
#define mtod(m,type)	((type) NB_MAP(m))

typedef void (*pfv)(void *);

/* used by both ppp_tty.c and if_ppp.c */
static inline kern_return_t
pppsched(pfv func, struct ppp_softc *sc)
{
    extern kern_server_t instance;
    kern_return_t result;

    if ((result = kern_serv_callout(&instance, func, (void *)sc)) != KERN_SUCCESS)
      IOLog("kern_serv_callout failed: ret = %x\n", result);

    return result;
}

#undef	u

static inline thread_t
current_thread(void)
{
	extern thread_t active_threads[];

	return active_threads[0];
}

extern struct proc *proc_from_thread(thread_t);
extern struct uthread *uthread_from_thread(thread_t);

#define	curproc		(proc_from_thread(current_thread()))