summaryrefslogtreecommitdiff
path: root/sys/dev/pci/if_art.c
blob: 9a50ad6e7251f183feea7c1eef39982de7d7b02f (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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*	$OpenBSD: if_art.c,v 1.18 2010/06/29 07:12:31 matthew Exp $ */

/*
 * Copyright (c) 2004,2005  Internet Business Solutions AG, Zurich, Switzerland
 * Written by: Claudio Jeker <jeker@accoom.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/types.h>

#include <sys/device.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/syslog.h>
#include <sys/systm.h>

#include <machine/bus.h>

#include <net/if.h>
#include <net/if_media.h>
#include <net/if_sppp.h>

#include <dev/pci/musyccvar.h>
#include <dev/pci/if_art.h>

#define ART_E1_MASK 0xffffffff
#define ART_T1_MASK 0x01fffffe

int	art_match(struct device *, void *, void *);
void	art_softc_attach(struct device *, struct device *, void *);

int	art_ioctl(struct ifnet *, u_long, caddr_t);
int	art_ifm_change(struct ifnet *);
void	art_ifm_status(struct ifnet *, struct ifmediareq *);
int	art_ifm_options(struct ifnet *, struct channel_softc *, u_int);
void	art_onesec(void *);
void	art_linkstate(void *);
u_int32_t art_mask_tsmap(u_int, u_int32_t);

struct cfattach art_ca = {
	sizeof(struct art_softc), art_match, art_softc_attach
};

struct cfdriver art_cd = {
	NULL, "art", DV_IFNET
};

int
art_match(struct device *parent, void *match, void *aux)
{
	struct musycc_attach_args	*ma = aux;

	if (ma->ma_type == MUSYCC_FRAMER_BT8370)
		return (1);
	return (0);
}

/*
 * used for the one second timer
 */
extern int hz;

void
art_softc_attach(struct device *parent, struct device *self, void *aux)
{
	struct art_softc		*sc = (struct art_softc *)self;
	struct musycc_softc		*psc = (struct musycc_softc *)parent;
	struct musycc_attach_args	*ma = aux;

	printf(" \"%s\"", ma->ma_product);

	if (ebus_attach_device(&sc->art_ebus, psc, ma->ma_base,
	    ma->ma_size) != 0) {
		printf(": can't map framer\n");
		return;
	}

	/* set basic values */
	sc->art_port = ma->ma_port;
	sc->art_slot = ma->ma_slot;
	sc->art_gnum = ma->ma_gnum;
	sc->art_type = ma->ma_flags & 0x03;

	sc->art_channel = musycc_channel_create(self->dv_xname, 1);
	if (sc->art_channel == NULL) {
		printf(": could not alloc channel descriptor\n");
		return;
	}

	if (musycc_channel_attach(psc, sc->art_channel, self, sc->art_gnum) ==
	    -1) {
		printf(": unable to attach to hdlc controller\n");
		return;
	}

	ifmedia_init(&sc->art_ifm, 0, art_ifm_change, art_ifm_status);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1, 0, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1_AMI, 0, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1, 0, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704, 0, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704_CRC4, 0, 0), 0, NULL);

	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1, IFM_TDM_MASTER, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1_AMI, IFM_TDM_MASTER, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1, IFM_TDM_MASTER, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704, IFM_TDM_MASTER, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704_CRC4, IFM_TDM_MASTER, 0),
	    0, NULL);

	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1, IFM_TDM_PPP, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1, IFM_TDM_PPP, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1_AMI, IFM_TDM_PPP, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704, IFM_TDM_PPP, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704_CRC4, IFM_TDM_PPP, 0), 0,
	    NULL);

	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1, IFM_TDM_PPP | IFM_TDM_MASTER, 0),
	    0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1, IFM_TDM_PPP | IFM_TDM_MASTER, 0),
	    0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_T1_AMI, IFM_TDM_PPP | IFM_TDM_MASTER,
	    0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704, IFM_TDM_PPP |
	    IFM_TDM_MASTER, 0), 0, NULL);
	ifmedia_add(&sc->art_ifm,
	    IFM_MAKEWORD(IFM_TDM, IFM_TDM_E1_G704_CRC4, IFM_TDM_PPP |
	    IFM_TDM_MASTER, 0), 0, NULL);

	printf("\n");

	if (bt8370_reset(sc) != 0)
		return;

	/* Initialize timeout for statistics update. */
	timeout_set(&sc->art_onesec, art_onesec, sc);

	ifmedia_set(&sc->art_ifm, IFM_TDM|IFM_TDM_E1_G704_CRC4);
	sc->art_media = sc->art_ifm.ifm_media;

	bt8370_set_frame_mode(sc, sc->art_type, IFM_TDM_E1_G704_CRC4, 0);
	musycc_attach_sppp(sc->art_channel, art_ioctl);

	/* Set linkstate hook to track link state changes done by sppp. */
	sc->art_linkstatehook = hook_establish(
	    sc->art_channel->cc_ifp->if_linkstatehooks, 0, art_linkstate, sc);

	/* Schedule the timeout one second from now. */
	timeout_add_sec(&sc->art_onesec, 1);
}

/* interface ioctl */
int
art_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
	struct ifreq		*ifr = (struct ifreq*) data;
	struct channel_softc	*cc = ifp->if_softc;
	struct art_softc	*ac = (struct art_softc *)cc->cc_parent;
	u_int32_t		 tsmap;
	int			 s, rv = 0;

	s = splnet();
	switch (command) {
	case SIOCSIFADDR:
		if ((rv = musycc_init_channel(cc, ac->art_slot)))
			break;
		rv = sppp_ioctl(ifp, command, data);
		break;
	case SIOCSIFTIMESLOT:
		if ((rv = suser(curproc, 0)) != 0)
			break;
		rv = copyin(ifr->ifr_data, &tsmap, sizeof(tsmap));
		if (rv)
			break;
		if (art_mask_tsmap(IFM_SUBTYPE(ac->art_media), tsmap) !=
		    tsmap) {
			rv = EINVAL;
			break;
		}
		if (ac->art_type == ART_SBI_SINGLE &&
		    (IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1 ||
		    IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1_AMI))
			/*
			 * need to adjust timeslot mask for T1 on single port
			 * cards. There timeslot 0-23 are usable not 1-24
			 */
			tsmap >>= 1;

		cc->cc_tslots = tsmap;
		rv = musycc_init_channel(cc, ac->art_slot);
		break;
	case SIOCGIFTIMESLOT:
		tsmap = cc->cc_tslots;
		if (ac->art_type == ART_SBI_SINGLE &&
		    (IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1 ||
		    IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1_AMI))
			tsmap <<= 1;
		rv = copyout(&tsmap, ifr->ifr_data, sizeof(tsmap));
		break;
	case SIOCSIFFLAGS:
		/*
		 * If interface is marked up and not running, then start it.
		 * If it is marked down and running, stop it.
		 */
		if (ifr->ifr_flags & IFF_UP && cc->cc_state != CHAN_RUNNING) {
			if ((rv = musycc_init_channel(cc, ac->art_slot)))
				break;
		} else if ((ifr->ifr_flags & IFF_UP) == 0 &&
		    cc->cc_state == CHAN_RUNNING)
			musycc_stop_channel(cc);
		rv = sppp_ioctl(ifp, command, data);
		break;
	case SIOCSIFMEDIA:
	case SIOCGIFMEDIA:
		if (ac != NULL)
			rv = ifmedia_ioctl(ifp, ifr, &ac->art_ifm, command);
		else
			rv = EINVAL;
		break;
	default:
		rv = sppp_ioctl(ifp, command, data);
		break;
	}
	splx(s);
	return (rv);
}

int
art_ifm_change(struct ifnet *ifp)
{
	struct channel_softc	*cc = ifp->if_softc;
	struct art_softc	*ac = (struct art_softc *)cc->cc_parent;
	struct ifmedia		*ifm = &ac->art_ifm;
	u_int64_t		baudrate;
	int			rv, s;

	ACCOOM_PRINTF(2, ("%s: art_ifm_change %08x\n", ifp->if_xname,
	    ifm->ifm_media));

	if (IFM_TYPE(ifm->ifm_media) != IFM_TDM)
		return (EINVAL);

	/* OPTIONS (controller mode hdlc, ppp, eoe) */
	if ((rv = art_ifm_options(ifp, cc, IFM_OPTIONS(ifm->ifm_media))) != 0)
		return (rv);

	/* SUBTYPE (framing mode T1/E1) + MODE (clocking master/slave) */
	if (IFM_SUBTYPE(ifm->ifm_media) != IFM_SUBTYPE(ac->art_media) ||
	    IFM_MODE(ifm->ifm_media) != IFM_MODE(ac->art_media)) {
		ACCOOM_PRINTF(0, ("%s: art_ifm_change type %d mode %x\n",
		    ifp->if_xname, IFM_SUBTYPE(ifm->ifm_media),
		    IFM_MODE(ifm->ifm_media)));

		bt8370_set_frame_mode(ac, ac->art_type,
		    IFM_SUBTYPE(ifm->ifm_media), IFM_MODE(ifm->ifm_media));

		if (IFM_SUBTYPE(ifm->ifm_media) != IFM_SUBTYPE(ac->art_media)) {
			/* adjust timeslot map on media change */
			cc->cc_tslots = art_mask_tsmap(
			    IFM_SUBTYPE(ifm->ifm_media), cc->cc_tslots);

			if (ac->art_type == ART_SBI_SINGLE &&
			    (IFM_SUBTYPE(ifm->ifm_media) == IFM_TDM_T1 ||
			     IFM_SUBTYPE(ifm->ifm_media) == IFM_TDM_T1_AMI) &&
			    (IFM_SUBTYPE(ac->art_media) != IFM_TDM_T1 &&
			     IFM_SUBTYPE(ac->art_media) != IFM_TDM_T1_AMI))
				/*
				 * need to adjust timeslot mask for T1 on
				 * single port cards. There timeslot 0-23 are
				 * usable not 1-24
				 */
				cc->cc_tslots >>= 1;
			else if (ac->art_type == ART_SBI_SINGLE &&
			    (IFM_SUBTYPE(ifm->ifm_media) != IFM_TDM_T1 &&
			     IFM_SUBTYPE(ifm->ifm_media) != IFM_TDM_T1_AMI) &&
			    (IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1 ||
			     IFM_SUBTYPE(ac->art_media) == IFM_TDM_T1_AMI))
				/* undo the last adjustment */
				cc->cc_tslots <<= 1;
		}

		/* re-init the card */
		if ((rv = musycc_init_channel(cc, ac->art_slot)))
			return (rv);
	}

	baudrate = ifmedia_baudrate(ac->art_media);
	if (baudrate != ifp->if_baudrate) {
		ifp->if_baudrate = baudrate;
		s = splsoftnet();
		if_link_state_change(ifp);
		splx(s);
	}

	ac->art_media = ifm->ifm_media;

	return (0);
}

void
art_ifm_status(struct ifnet *ifp, struct ifmediareq *ifmreq)
{
	struct art_softc	*ac;

	ac = (struct art_softc *)
	    ((struct channel_softc *)ifp->if_softc)->cc_parent;
	ifmreq->ifm_status = IFM_AVALID;
	if (LINK_STATE_IS_UP(ifp->if_link_state))
		ifmreq->ifm_status |= IFM_ACTIVE;
	ifmreq->ifm_active = ac->art_media;

	return;
}

int
art_ifm_options(struct ifnet *ifp, struct channel_softc *cc, u_int options)
{
	struct art_softc	*ac = (struct art_softc *)cc->cc_parent;
	u_int			 flags = cc->cc_ppp.pp_flags;
	int			 rv;

	if (options == IFM_TDM_PPP) {
		flags &= ~PP_CISCO;
		flags |= PP_KEEPALIVE;
	} else if (options == 0) {
		flags |= PP_CISCO;
		flags |= PP_KEEPALIVE;
	} else {
		ACCOOM_PRINTF(0, ("%s: Unsupported ifmedia options\n",
		    ifp->if_xname));
		return (EINVAL);
	}
	if (flags != cc->cc_ppp.pp_flags) {
		musycc_stop_channel(cc);
		cc->cc_ppp.pp_flags = flags;
		if ((rv = musycc_init_channel(cc, ac->art_slot)))
			return (rv);
		return (sppp_ioctl(ifp, SIOCSIFFLAGS, NULL));
	}
	return (0);
}

void
art_onesec(void *arg)
{
	struct art_softc	*ac = arg;
	struct ifnet		*ifp = ac->art_channel->cc_ifp;
	struct sppp		*ppp = &ac->art_channel->cc_ppp;
	int			 s, rv, link_state;

	rv = bt8370_link_status(ac);
	switch (rv) {
	case 1:
		link_state = LINK_STATE_UP;
		/* set green led */
		ebus_set_led(ac->art_channel, 1, MUSYCC_LED_GREEN);
		break;
	case 0:
		link_state = LINK_STATE_DOWN;
		/* set green led and red led as well */
		ebus_set_led(ac->art_channel, 1,
		    MUSYCC_LED_GREEN | MUSYCC_LED_RED);
		break;
	default:
		link_state = LINK_STATE_DOWN;
		/* turn green led off */
		ebus_set_led(ac->art_channel, 0, MUSYCC_LED_GREEN);
		break;
	}

	if (link_state != ifp->if_link_state) {
		s = splsoftnet();
		if (LINK_STATE_IS_UP(link_state))
			ppp->pp_up(ppp);
		else
			ppp->pp_down(ppp);
		splx(s);
	}

	/*
	 * run musycc onesec job
	 */
	musycc_tick(ac->art_channel);

	/*
	 * Schedule another timeout one second from now.
	 */
	timeout_add_sec(&ac->art_onesec, 1);
}

void
art_linkstate(void *arg)
{
	struct art_softc	*ac = arg;
	struct ifnet		*ifp = ac->art_channel->cc_ifp;

	if (LINK_STATE_IS_UP(ifp->if_link_state))
		/* turn red led off */
		ebus_set_led(ac->art_channel, 0, MUSYCC_LED_RED);
	else
		/* turn red led on */
		ebus_set_led(ac->art_channel, 1, MUSYCC_LED_RED);
}

u_int32_t
art_mask_tsmap(u_int mode, u_int32_t tsmap)
{
	switch (mode) {
	case IFM_TDM_E1:
	case IFM_TDM_E1_G704:
	case IFM_TDM_E1_G704_CRC4:
		return (tsmap & ART_E1_MASK);
	case IFM_TDM_T1_AMI:
	case IFM_TDM_T1:
		return (tsmap & ART_T1_MASK);
	default:
		return (tsmap);
	}
}