xref: /dragonfly/sys/dev/netif/sbsh/if_sbsh.c (revision 8e1c6f81)
1 /**
2  * Granch SBNI16 G.SHDSL Modem driver
3  * Written by Denis I. Timofeev, 2002-2003.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/sbsh/if_sbsh.c,v 1.3.2.1 2003/04/15 18:15:07 fjoe Exp $
27  * $DragonFly: src/sys/dev/netif/sbsh/if_sbsh.c,v 1.28 2008/08/17 04:32:34 sephe Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sockio.h>
33 #include <sys/mbuf.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/proc.h>
37 #include <sys/socket.h>
38 #include <sys/random.h>
39 #include <sys/serialize.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/thread2.h>
43 #include <sys/interrupt.h>
44 
45 #include <net/if.h>
46 #include <net/ifq_var.h>
47 #include <net/if_arp.h>
48 #include <net/ethernet.h>
49 #include <net/if_media.h>
50 
51 #include <net/bpf.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 
56 #include <bus/pci/pcireg.h>
57 #include <bus/pci/pcivar.h>
58 
59 #include "if_sbshreg.h"
60 
61 /* -------------------------------------------------------------------------- */
62 
63 struct sbni16_hw_regs {
64 	u_int8_t  CR, CRB, SR, IMR, CTDR, LTDR, CRDR, LRDR;
65 };
66 
67 struct hw_descr {
68 	u_int32_t  address;
69 	u_int32_t  length;
70 };
71 
72 struct cx28975_cmdarea {
73 	u_int8_t  intr_host;
74 	u_int8_t  intr_8051;
75 	u_int8_t  map_version;
76 
77 	u_int8_t  in_dest;
78 	u_int8_t  in_opcode;
79 	u_int8_t  in_zero;
80 	u_int8_t  in_length;
81 	u_int8_t  in_csum;
82 	u_int8_t  in_data[75];
83 	u_int8_t  in_datasum;
84 
85 	u_int8_t  out_dest;
86 	u_int8_t  out_opcode;
87 	u_int8_t  out_ack;
88 	u_int8_t  out_length;
89 	u_int8_t  out_csum;
90 	u_int8_t  out_data[75];
91 	u_int8_t  out_datasum;
92 };
93 
94 #define XQLEN	8
95 #define RQLEN	8
96 
97 struct sbsh_softc {
98 	struct arpcom	arpcom;		/* ethernet common */
99 
100 	struct resource	*mem_res;
101 	struct resource	*irq_res;
102 	void		*intr_hand;
103 
104 	void		*mem_base;		/* mapped memory address */
105 
106 	volatile struct sbni16_hw_regs	*regs;
107 	volatile struct hw_descr	*tbd;
108 	volatile struct hw_descr	*rbd;
109 	volatile struct cx28975_cmdarea	*cmdp;
110 
111 	/* SBNI16 controller statistics */
112 	struct sbni16_stats {
113 		u_int32_t  sent_pkts, rcvd_pkts;
114 		u_int32_t  crc_errs, ufl_errs, ofl_errs, attempts, last_time;
115 	} in_stats;
116 
117 	/* transmit and reception queues */
118 	struct mbuf	*xq[XQLEN], *rq[RQLEN];
119 	unsigned	head_xq, tail_xq, head_rq, tail_rq;
120 
121 	/* the descriptors mapped onto the first buffers in xq and rq */
122 	unsigned	head_tdesc, head_rdesc;
123 	u_int8_t	state;
124 };
125 
126 struct cx28975_cfg {
127 	u_int8_t   *firmw_image;
128 	u_int32_t  firmw_len;
129 	u_int32_t  lrate: 10;
130 	u_int32_t  master: 1;
131 	u_int32_t  mod: 2;
132 	u_int32_t  crc16: 1;
133 	u_int32_t  fill_7e: 1;
134 	u_int32_t  inv: 1;
135 	u_int32_t  rburst: 1;
136 	u_int32_t  wburst: 1;
137 	u_int32_t  : 14;
138 };
139 
140 /* SHDSL transceiver statistics */
141 struct dsl_stats {
142 	u_int8_t	status_1, status_3;
143 	u_int8_t	attenuat, nmr, tpbo, rpbo;
144 	u_int16_t	losw, segd, crc, sega, losd;
145 };
146 
147 enum State { NOT_LOADED, DOWN, ACTIVATION, ACTIVE };
148 
149 #define	SIOCLOADFIRMW	_IOWR('i', 67, struct ifreq)
150 #define SIOCGETSTATS	_IOWR('i', 68, struct ifreq)
151 #define SIOCCLRSTATS	_IOWR('i', 69, struct ifreq)
152 
153 static int	sbsh_probe(device_t);
154 static int	sbsh_attach(device_t);
155 static int	sbsh_detach(device_t);
156 static int	sbsh_ioctl(struct ifnet	*, u_long, caddr_t, struct ucred *);
157 static void	sbsh_shutdown(device_t);
158 static int	sbsh_suspend(device_t);
159 static int	sbsh_resume(device_t);
160 static void	sbsh_watchdog(struct ifnet *);
161 
162 static void	sbsh_start(struct ifnet *);
163 static void	sbsh_init(void *);
164 static void	sbsh_stop(struct sbsh_softc *);
165 static void	init_card(struct sbsh_softc *);
166 static void	sbsh_intr(void *);
167 static void	resume_tx(struct sbsh_softc *);
168 static void	start_xmit_frames(struct sbsh_softc *);
169 static void	encap_frame(struct sbsh_softc *, struct mbuf *);
170 static struct mbuf *	repack(struct sbsh_softc *, struct mbuf *);
171 static void	free_sent_buffers(struct sbsh_softc *);
172 static void	alloc_rx_buffers(struct sbsh_softc *);
173 static void	indicate_frames(struct sbsh_softc *);
174 static void	drop_queues(struct sbsh_softc *);
175 static void	activate(struct sbsh_softc *);
176 static void	deactivate(struct sbsh_softc *);
177 static void	cx28975_interrupt(struct sbsh_softc *);
178 static int	start_cx28975(struct sbsh_softc *, struct cx28975_cfg);
179 static int	download_firmware(struct sbsh_softc *, u_int8_t *, u_int32_t);
180 static int	issue_cx28975_cmd(struct sbsh_softc *, u_int8_t,
181 					u_int8_t *, u_int8_t);
182 
183 static device_method_t sbsh_methods[] = {
184 	/* Device interface */
185 	DEVMETHOD(device_probe,		sbsh_probe),
186 	DEVMETHOD(device_attach,	sbsh_attach),
187 	DEVMETHOD(device_detach,	sbsh_detach),
188 	DEVMETHOD(device_shutdown,	sbsh_shutdown),
189 	DEVMETHOD(device_suspend,	sbsh_suspend),
190 	DEVMETHOD(device_resume,	sbsh_resume),
191 
192 	{ 0, 0 }
193 };
194 
195 static driver_t sbsh_driver = {
196 	"sbsh",
197 	sbsh_methods,
198 	sizeof(struct sbsh_softc)
199 };
200 
201 static devclass_t sbsh_devclass;
202 
203 DECLARE_DUMMY_MODULE(if_sbsh);
204 DRIVER_MODULE(if_sbsh, pci, sbsh_driver, sbsh_devclass, 0, 0);
205 
206 static int
207 sbsh_probe(device_t dev)
208 {
209 	if (pci_get_vendor(dev) != SBNI16_VENDOR
210 	    || pci_get_device(dev) != SBNI16_DEVICE
211 	    || pci_get_subdevice(dev) != SBNI16_SUBDEV)
212 		return (ENXIO);
213 
214 	device_set_desc(dev, "Granch SBNI16 G.SHDSL Modem");
215 	return (0);
216 }
217 
218 static int
219 sbsh_attach(device_t dev)
220 {
221 	struct sbsh_softc	*sc;
222 	struct ifnet		*ifp;
223 	int			unit, error = 0, rid;
224 
225 	sc = device_get_softc(dev);
226 	unit = device_get_unit(dev);
227 
228 	rid = PCIR_MAPS + 4;
229 	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
230 					0, ~0, 4096, RF_ACTIVE);
231 
232 	if (sc->mem_res == NULL) {
233 		kprintf ("sbsh%d: couldn't map memory\n", unit);
234 		error = ENXIO;
235 		goto fail;
236 	}
237 
238 	rid = 0;
239 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
240 	    RF_SHAREABLE | RF_ACTIVE);
241 
242 	if (sc->irq_res == NULL) {
243 		kprintf("sbsh%d: couldn't map interrupt\n", unit);
244 		error = ENXIO;
245 		goto fail;
246 	}
247 
248 	sc->mem_base = rman_get_virtual(sc->mem_res);
249 	init_card(sc);
250 	/* generate ethernet MAC address */
251 	*(u_int32_t *)sc->arpcom.ac_enaddr = htonl(0x00ff0192);
252 	read_random_unlimited(sc->arpcom.ac_enaddr + 4, 2);
253 
254 	ifp = &sc->arpcom.ac_if;
255 	ifp->if_softc = sc;
256 	if_initname(ifp, "sbsh", unit);
257 	ifp->if_mtu = ETHERMTU;
258 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
259 	ifp->if_ioctl = sbsh_ioctl;
260 	ifp->if_start = sbsh_start;
261 	ifp->if_watchdog = sbsh_watchdog;
262 	ifp->if_init = sbsh_init;
263 	ifp->if_baudrate = 4600000;
264 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
265 	ifq_set_ready(&ifp->if_snd);
266 
267 	ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
268 
269 	error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
270 				sbsh_intr, sc, &sc->intr_hand,
271 				ifp->if_serializer);
272 	if (error) {
273 		ether_ifdetach(ifp);
274 		kprintf("sbsh%d: couldn't set up irq\n", unit);
275 		goto fail;
276 	}
277 
278 	ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq_res));
279 	KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
280 
281 	return(0);
282 
283 fail:
284 	sbsh_detach(dev);
285 	return (error);
286 }
287 
288 static int
289 sbsh_detach(device_t dev)
290 {
291 	struct sbsh_softc *sc = device_get_softc(dev);
292 	struct ifnet *ifp = &sc->arpcom.ac_if;
293 
294 	if (device_is_attached(dev)) {
295 		lwkt_serialize_enter(ifp->if_serializer);
296 		sbsh_stop(sc);
297 		bus_teardown_intr(dev, sc->irq_res, sc->intr_hand);
298 		lwkt_serialize_exit(ifp->if_serializer);
299 
300 		ether_ifdetach(ifp);
301 	}
302 
303 	if (sc->irq_res)
304 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
305 	if (sc->mem_res) {
306 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_MAPS + 4,
307 				     sc->mem_res);
308 	}
309 
310 	return (0);
311 }
312 
313 
314 static void
315 sbsh_start(struct ifnet *ifp)
316 {
317 	struct sbsh_softc  *sc = ifp->if_softc;
318 
319 	if (sc->state == ACTIVE)
320 		start_xmit_frames(ifp->if_softc);
321 }
322 
323 
324 static void
325 sbsh_init(void *xsc)
326 {
327 	struct sbsh_softc	*sc = xsc;
328 	struct ifnet		*ifp = &sc->arpcom.ac_if;
329 	u_int8_t		t;
330 
331 	if ((ifp->if_flags & IFF_RUNNING) || sc->state == NOT_LOADED) {
332 		return;
333 	}
334 
335 	bzero(&sc->in_stats, sizeof(struct sbni16_stats));
336 	sc->head_xq = sc->tail_xq = sc->head_rq = sc->tail_rq = 0;
337 	sc->head_tdesc = sc->head_rdesc = 0;
338 
339 	sc->regs->IMR = EXT;
340 	t = 2;
341 	issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1);
342 	if (issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1) == 0) {
343 		sc->state = ACTIVATION;
344 
345 		ifp->if_flags |= IFF_RUNNING;
346 		ifp->if_flags &= ~IFF_OACTIVE;
347 	}
348 }
349 
350 
351 static void
352 sbsh_stop(struct sbsh_softc *sc)
353 {
354 	u_int8_t  t;
355 
356 	sc->regs->IMR = EXT;
357 
358 	t = 0;
359 	issue_cx28975_cmd(sc, _DSL_ACTIVATION, &t, 1);
360 	if (sc->state == ACTIVE) {
361 		t = 1;
362 		issue_cx28975_cmd(sc, _DSL_FORCE_DEACTIVATE, &t, 1);
363 		/* FIX! activation manager state */
364 
365 		/* Is it really must be done here? It calls from intr handler */
366 		deactivate(sc);
367 	}
368 
369 	sc->regs->IMR = 0;
370 	sc->state = DOWN;
371 }
372 
373 
374 static void
375 init_card(struct sbsh_softc *sc)
376 {
377 	sc->state = NOT_LOADED;
378 	sc->tbd  = (struct hw_descr *) sc->mem_base;
379 	sc->rbd  = (struct hw_descr *) ((u_int8_t *)sc->mem_base + 0x400);
380 	sc->regs = (struct sbni16_hw_regs *) ((u_int8_t *)sc->mem_base + 0x800);
381 	sc->cmdp = (struct cx28975_cmdarea *) ((u_int8_t *)sc->mem_base + 0xc00);
382 
383 	sc->regs->CR = 0;
384 	sc->regs->SR = 0xff;
385 	sc->regs->IMR = 0;
386 }
387 
388 
389 static int
390 sbsh_ioctl(struct ifnet	*ifp, u_long cmd, caddr_t data, struct ucred *cr)
391 {
392 	struct sbsh_softc	*sc = ifp->if_softc;
393 	struct ifreq		*ifr = (struct ifreq *) data;
394 	struct cx28975_cfg	cfg;
395 	struct dsl_stats	ds;
396 	int			error = 0;
397 	u_int8_t		t;
398 
399 	switch(cmd) {
400 	case SIOCLOADFIRMW:
401 		if ((error = suser_cred(cr, NULL_CRED_OKAY)) != 0)
402 			break;
403 		if (ifp->if_flags & IFF_UP)
404 			error = EBUSY;
405 
406 		bcopy((caddr_t)ifr->ifr_data, (caddr_t)&cfg, sizeof cfg);
407 		if (start_cx28975(sc, cfg) == 0) {
408 			static char  *modstr[] = {
409 				"TCPAM32", "TCPAM16", "TCPAM8", "TCPAM4" };
410 			if_printf(&sc->arpcom.ac_if, "%s, rate %d, %s\n",
411 				cfg.master ? "master" : "slave",
412 				cfg.lrate << 3, modstr[cfg.mod]);
413 		} else {
414 			if_printf(&sc->arpcom.ac_if,
415 				"unable to load firmware\n");
416 			error = EIO;
417 		}
418 		break;
419 
420 	case  SIOCGETSTATS :
421 		if ((error = suser_cred(cr, NULL_CRED_OKAY)) != 0)
422 			break;
423 
424 		t = 0;
425 		if (issue_cx28975_cmd(sc, _DSL_FAR_END_ATTEN, &t, 1))
426 			error = EIO;
427 		ds.attenuat = sc->cmdp->out_data[0];
428 
429 		if (issue_cx28975_cmd(sc, _DSL_NOISE_MARGIN, &t, 1))
430 			error = EIO;
431 		ds.nmr = sc->cmdp->out_data[0];
432 
433 		if (issue_cx28975_cmd(sc, _DSL_POWER_BACK_OFF_RESULT, &t, 1))
434 			error = EIO;
435 		ds.tpbo = sc->cmdp->out_data[0];
436 		ds.rpbo = sc->cmdp->out_data[1];
437 
438 		if (!issue_cx28975_cmd(sc, _DSL_HDSL_PERF_ERR_CTRS, &t, 1)) {
439 			int i;
440 			for (i = 0; i < 10; ++i)
441 				((u_int8_t *) &ds.losw)[i] =
442 					sc->cmdp->out_data[i];
443 		} else
444 			error = EIO;
445 
446 		ds.status_1 = ((volatile u_int8_t *)sc->cmdp)[0x3c0];
447 		ds.status_3 = ((volatile u_int8_t *)sc->cmdp)[0x3c2];
448 
449 		bcopy(&sc->in_stats, ifr->ifr_data, sizeof(struct sbni16_stats));
450 		bcopy(&ds, (char *)ifr->ifr_data + sizeof(struct sbni16_stats),
451 		    sizeof(struct dsl_stats));
452 		break;
453 
454 	case  SIOCCLRSTATS :
455 		if (!(error = suser_cred(cr, NULL_CRED_OKAY))) {
456 			bzero(&sc->in_stats, sizeof(struct sbni16_stats));
457 			t = 2;
458 			if (issue_cx28975_cmd(sc, _DSL_CLEAR_ERROR_CTRS, &t, 1))
459 				error = EIO;
460 		}
461 		break;
462 	case SIOCSIFFLAGS:
463 		if (ifp->if_flags & IFF_UP) {
464 			if (!(ifp->if_flags & IFF_RUNNING)) {
465 				if (sc->state == NOT_LOADED) {
466 					if_printf(ifp, "firmware wasn't loaded\n");
467 					error = EBUSY;
468 				} else
469 					sbsh_init(sc);
470 			}
471 		} else {
472 			if (ifp->if_flags & IFF_RUNNING) {
473 				sbsh_stop(sc);
474 				ifp->if_flags &= ~IFF_RUNNING;
475 			}
476 		}
477 		break;
478 
479 	case SIOCADDMULTI:
480 	case SIOCDELMULTI:
481 		error = 0;
482 		break;
483 	default:
484 		error = ether_ioctl(ifp, cmd, data);
485 		break;
486 	}
487 	return (error);
488 }
489 
490 
491 static void
492 sbsh_shutdown(device_t dev)
493 {
494 	struct sbsh_softc	*sc = device_get_softc(dev);
495 
496 	lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
497 	sbsh_stop(sc);
498 	lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
499 }
500 
501 static int
502 sbsh_suspend(device_t dev)
503 {
504 	struct sbsh_softc *sc = device_get_softc(dev);
505 
506 	lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
507 	sbsh_stop(sc);
508 	lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
509 
510 	return (0);
511 }
512 
513 static int
514 sbsh_resume(device_t dev)
515 {
516 	struct sbsh_softc *sc = device_get_softc(dev);
517 	struct ifnet *ifp = &sc->arpcom.ac_if;
518 
519 	if (ifp->if_flags & IFF_UP)
520 		sbsh_init(sc);
521 
522 	return (0);
523 }
524 
525 
526 static void
527 sbsh_watchdog(struct ifnet *ifp)
528 {
529 	struct sbsh_softc	*sc = ifp->if_softc;
530 
531 	if_printf(ifp, "transmit timeout\n");
532 
533 	if (sc->regs->SR & TXS) {
534 		sc->regs->SR = TXS;
535 		if_printf(ifp, "interrupt posted but not delivered\n");
536 	}
537 	free_sent_buffers(sc);
538 }
539 
540 /* -------------------------------------------------------------------------- */
541 
542 static void
543 sbsh_intr(void *arg)
544 {
545 	struct sbsh_softc  *sc = (struct sbsh_softc *)arg;
546 	u_int8_t  status = sc->regs->SR;
547 
548 	if (status == 0)
549 		return;
550 
551 	if (status & EXT) {
552 		cx28975_interrupt(sc);
553 		sc->regs->SR = EXT;
554 	}
555 
556 	if (status & UFL) {
557 		resume_tx(sc);
558 		sc->regs->SR = UFL;
559 		++sc->in_stats.ufl_errs;
560 		++sc->arpcom.ac_if.if_oerrors;
561 	}
562 
563 	if (status & RXS) {
564 		sc->regs->SR = RXS;
565 		indicate_frames(sc);
566 		alloc_rx_buffers(sc);
567 	}
568 
569 	if (status & TXS) {
570 		sc->regs->SR = TXS;
571 		free_sent_buffers(sc);
572 	}
573 
574 	if (status & CRC) {
575 		++sc->in_stats.crc_errs;
576 		++sc->arpcom.ac_if.if_ierrors;
577 		sc->regs->SR = CRC;
578 	}
579 
580 	if (status & OFL) {
581 		++sc->in_stats.ofl_errs;
582 		++sc->arpcom.ac_if.if_ierrors;
583 		sc->regs->SR = OFL;
584 	}
585 }
586 
587 /*
588  * Look for a first descriptor of a next packet, and write it's number
589  * into CTDR. Then enable the transmitter.
590  */
591 static void
592 resume_tx(struct sbsh_softc *sc)
593 {
594 	u_int32_t	cur_tbd = sc->regs->CTDR;
595 
596 	while (cur_tbd != sc->regs->LTDR
597 		&& (sc->tbd[cur_tbd++].length & LAST_FRAG) == 0)
598 		;
599 	sc->regs->CTDR = cur_tbd;
600 	sc->regs->CR |= TXEN;
601 }
602 
603 static void
604 start_xmit_frames(struct sbsh_softc *sc)
605 {
606 	struct ifnet	*ifp = &sc->arpcom.ac_if;
607 	struct mbuf	*m;
608 
609 	/*
610 	 * Check if we have any free descriptor(s) and free space in
611 	 * our transmit queue.
612 	 */
613 	while (sc->tail_xq != ((sc->head_xq - 1) & (XQLEN - 1))
614 	    && sc->regs->LTDR != ((sc->head_tdesc - 1) & 0x7f)) {
615 
616 		m = ifq_dequeue(&ifp->if_snd, NULL);
617 		if (m == NULL)
618 			break;
619 		if (m->m_pkthdr.len) {
620 			BPF_MTAP(ifp, m);
621 			encap_frame(sc, m);
622 		} else
623 			m_freem(m);
624 	}
625 
626 	if (sc->regs->CTDR != sc->regs->LTDR)
627 		ifp->if_flags |= IFF_OACTIVE;
628 	else
629 		ifp->if_flags &= ~IFF_OACTIVE;
630 }
631 
632 
633 static void
634 encap_frame(struct sbsh_softc *sc, struct mbuf *m_head)
635 {
636 	struct mbuf	*m;
637 	u_int32_t	cur_tbd;
638 	int  done;
639 
640 look_for_nonzero:
641 	for (m = m_head; !m->m_len; m = m->m_next)
642 		;
643 
644 	cur_tbd = sc->regs->LTDR & 0x7f;
645 	done = 0;
646 	do {
647 		if (m->m_len < 5 || cur_tbd == ((sc->head_tdesc - 1) & 0x7f)) {
648 			if ((m_head = repack(sc, m_head)) != NULL)
649 				goto look_for_nonzero;
650 			else
651 				return;
652 		}
653 
654 		sc->tbd[cur_tbd].address = vtophys(mtod(m, vm_offset_t));
655 		sc->tbd[cur_tbd].length  = m->m_len;
656 
657 		do {
658 			m = m->m_next;
659 		} while (m && !m->m_len);
660 
661 		if (!m) {	/* last fragment has been reached */
662 			sc->tbd[cur_tbd].length |= LAST_FRAG;
663 			done = 1;
664 		}
665 
666 		++cur_tbd;
667 		cur_tbd &= 0x7f;
668 	} while (!done);
669 
670 	sc->xq[sc->tail_xq++] = m_head;
671 	sc->tail_xq &= (XQLEN - 1);
672 
673 	sc->regs->LTDR = cur_tbd;
674 	++sc->in_stats.sent_pkts;
675 	++sc->arpcom.ac_if.if_opackets;
676 }
677 
678 static struct mbuf *
679 repack(struct sbsh_softc *sc, struct mbuf *m)
680 {
681 	struct mbuf  *m_new;
682 
683 	MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
684 	if (!m_new) {
685 		if_printf (&sc->arpcom.ac_if,
686 			   "unable to get mbuf.\n");
687 		return (NULL);
688 	}
689 
690 	if (m->m_pkthdr.len > MHLEN) {
691 		MCLGET(m_new, MB_DONTWAIT);
692 		if (!(m_new->m_flags & M_EXT)) {
693 			m_freem(m_new);
694 			if_printf (&sc->arpcom.ac_if,
695 				   "unable to get mbuf cluster.\n");
696 			return (NULL);
697 		}
698 	}
699 
700 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
701 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
702 	m_freem(m);
703 	return (m_new);
704 }
705 
706 static void
707 free_sent_buffers(struct sbsh_softc *sc)
708 {
709 	u_int32_t  cur_tbd;
710 
711 	cur_tbd = sc->regs->CTDR;
712 
713 	while (sc->head_tdesc != cur_tbd) {
714 		/*
715 		 * Be careful! one element in xq may correspond to
716 		 * multiple descriptors.
717 		 */
718 		if (sc->tbd[sc->head_tdesc].length & LAST_FRAG) {
719 			m_freem(sc->xq[sc->head_xq++]);
720 			sc->head_xq &= (XQLEN - 1);
721 		}
722 
723 		sc->tbd[sc->head_tdesc].length = 0;
724 		sc->head_tdesc = (sc->head_tdesc + 1) & 0x7f;
725 	}
726 
727 	start_xmit_frames(sc);
728 }
729 
730 /*
731  * DON'T use free_sent_buffers to drop the queue!
732  */
733 static void
734 alloc_rx_buffers(struct sbsh_softc *sc)
735 {
736 	unsigned	cur_rbd = sc->regs->LRDR & 0x7f;
737 	struct mbuf	*m;
738 
739 	while (sc->tail_rq != ((sc->head_rq - 1) & (RQLEN - 1))) {
740 		MGETHDR(m, MB_DONTWAIT, MT_DATA);
741 		if (!m) {
742 			if_printf (&sc->arpcom.ac_if,
743 				   "unable to get mbuf.\n");
744 			return;
745 		}
746 
747 		if (SBNI16_MAX_FRAME > MHLEN) {
748 			MCLGET(m, MB_DONTWAIT);
749 			if (!(m->m_flags & M_EXT)) {
750 				m_freem(m);
751 				if_printf (&sc->arpcom.ac_if,
752 					   "unable to get mbuf cluster.\n");
753 				return;
754 			}
755 			m->m_pkthdr.len = m->m_len = MCLBYTES;
756 		}
757 
758 		m_adj(m, 2);	/* align ip on longword boundaries */
759 
760 		sc->rq[sc->tail_rq++] = m;
761 		sc->tail_rq &= (RQLEN - 1);
762 
763 		sc->rbd[cur_rbd].address = vtophys(mtod(m, vm_offset_t));
764 		sc->rbd[cur_rbd].length  = 0;
765 		sc->regs->LRDR = cur_rbd = (cur_rbd + 1) & 0x7f;
766 	}
767 }
768 
769 static void
770 indicate_frames(struct sbsh_softc *sc)
771 {
772 	struct ifnet *ifp = &sc->arpcom.ac_if;
773 	unsigned  cur_rbd = sc->regs->CRDR & 0x7f;
774 
775 	while (sc->head_rdesc != cur_rbd) {
776 		struct mbuf  *m = sc->rq[sc->head_rq++];
777 		sc->head_rq &= (RQLEN - 1);
778 
779 		m->m_pkthdr.len = m->m_len =
780 				sc->rbd[sc->head_rdesc].length & 0x7ff;
781 		m->m_pkthdr.rcvif = ifp;
782 
783 		ifp->if_input(ifp, m);
784 		++sc->in_stats.rcvd_pkts;
785 		++ifp->if_ipackets;
786 
787 		sc->head_rdesc = (sc->head_rdesc + 1) & 0x7f;
788 	}
789 }
790 
791 static void
792 drop_queues(struct sbsh_softc *sc)
793 {
794 	while (sc->head_rq != sc->tail_rq) {
795 		m_freem(sc->rq[sc->head_rq++]);
796 		sc->head_rq &= (RQLEN - 1);
797 	}
798 
799 	while (sc->head_xq != sc->tail_xq) {
800 		m_freem(sc->xq[sc->head_xq++]);
801 		sc->head_xq &= (XQLEN - 1);
802 	}
803 }
804 
805 /* -------------------------------------------------------------------------- */
806 
807 static void
808 activate(struct sbsh_softc *sc)
809 {
810 	struct timeval	tv;
811 
812 	sc->regs->SR   = 0xff;		/* clear it! */
813 	sc->regs->CTDR = sc->regs->LTDR = sc->regs->CRDR = sc->regs->LRDR = 0;
814 
815 	sc->head_tdesc = sc->head_rdesc = 0;
816 	alloc_rx_buffers(sc);
817 
818 	sc->regs->CRB &= ~RXDE;
819 	sc->regs->IMR = EXT | RXS | TXS | CRC | OFL | UFL;
820 	sc->regs->CR |= TXEN | RXEN;
821 
822 	sc->state = ACTIVE;
823 	++sc->in_stats.attempts;
824 	microtime(&tv);
825 	sc->in_stats.last_time = tv.tv_sec;
826 	start_xmit_frames(sc);
827 }
828 
829 static void
830 deactivate(struct sbsh_softc *sc)
831 {
832 	sc->regs->CR &= ~(RXEN | TXEN);
833 	sc->regs->CRB |= RXDE;
834 	sc->regs->IMR  = EXT;
835 	sc->regs->CTDR = sc->regs->LTDR;
836 	sc->regs->CRDR = sc->regs->LRDR;
837 	sc->state = ACTIVATION;
838 
839 	drop_queues(sc);
840 }
841 
842 /* -------------------------------------------------------------------------- */
843 
844 static void
845 cx28975_interrupt(struct sbsh_softc *sc)
846 {
847 	volatile struct cx28975_cmdarea  *p = sc->cmdp;
848 	u_int8_t  t;
849 
850 	if (p->intr_host != 0xfe)
851 		return;
852 
853 	if (p->out_ack & 0x80) {
854 		if (*((volatile u_int8_t *)p + 0x3c7) & 2) {
855 			if (sc->state != ACTIVE
856 			    && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) == 0x40) {
857 				activate(sc);
858 				if_printf(&sc->arpcom.ac_if, "connected to peer\n");
859 			} else if (sc->state == ACTIVE
860 				 && (*((volatile u_int8_t *)p + 0x3c0) & 0xc0) != 0x40) {
861 				deactivate(sc);
862 				if_printf(&sc->arpcom.ac_if, "carrier lost\n");
863 			}
864 		}
865 
866 		p->intr_host = 0;
867 		t = p->intr_host;
868 		p->out_ack = 0;
869 	} else {
870 		wakeup(sc);
871 
872 		p->intr_host = 0;
873 		t = p->intr_host;
874 	}
875 }
876 
877 /* -------------------------------------------------------------------------- */
878 
879 static int
880 start_cx28975(struct sbsh_softc *sc, struct cx28975_cfg cfg)
881 {
882 	static char  thresh[] = { +8, -4, -16, -40 };
883 
884 	volatile struct cx28975_cmdarea  *p = sc->cmdp;
885 	u_int8_t  t, parm[12];
886 
887 	p->intr_host = 0;
888 	t = p->intr_host;
889 
890 	/* reset chip set */
891 	sc->regs->IMR = EXT;
892 	sc->regs->CR  = 0;
893 	sc->regs->SR  = 0xff;
894 	DELAY(2);
895 	sc->regs->CR = XRST;
896 	if (cfg.crc16)
897 		sc->regs->CR |= CMOD;
898 	if (cfg.fill_7e)
899 		sc->regs->CR |= FMOD;
900 	if (cfg.inv)
901 		sc->regs->CR |= PMOD;
902 
903 	sc->regs->CRB |= RODD | RXDE;
904 	if (cfg.rburst)
905 		sc->regs->CRB |= RDBE;
906 	if (cfg.wburst)
907 		sc->regs->CRB |= WTBE;
908 
909 	tsleep(sc, 0, "sbsh", 0);
910 	if ((p->out_ack & 0x1f) != _ACK_BOOT_WAKE_UP)
911 		return (-1);
912 
913 	if (download_firmware(sc, cfg.firmw_image, cfg.firmw_len))
914 		return (-1);
915 
916 	tsleep(sc, 0, "sbsh", 0);
917 	if ((p->out_ack & 0x1f) != _ACK_OPER_WAKE_UP)
918 		return (-1);
919 
920 	t = cfg.master ? 1 : 9;
921 	if (issue_cx28975_cmd(sc, _DSL_SYSTEM_ENABLE, &t, 1))
922 		return (-1);
923 
924 	t = 0x63;
925 	if (issue_cx28975_cmd(sc, _DSL_SYSTEM_CONFIG, &t, 1))
926 		return (-1);
927 
928 	*(u_int16_t *)parm = cfg.lrate >> 3;
929 	parm[2] = parm[3] = parm[0];
930 	parm[5] = cfg.lrate & 7;
931 	parm[4] = parm[7] = 1;
932 	parm[6] = 0;
933 	if (issue_cx28975_cmd(sc, _DSL_MULTI_RATE_CONFIG, parm, 8))
934 		return (-1);
935 
936 	parm[0] = 0x02 | (cfg.mod << 4);
937 	parm[1] = 0;
938 	if (issue_cx28975_cmd(sc, _DSL_TRAINING_MODE, parm, 2))
939 		return (-1);
940 
941 	bzero(parm, 12);
942 	parm[0] = 0x04;		/* pre-activation: G.hs */
943 	parm[4] = 0x04;		/* no remote configuration */
944 	parm[7] = 0x01;		/* annex A (default) */
945 	parm[8] = 0xff;		/* i-bit mask (all bits) */
946 	if (issue_cx28975_cmd(sc, _DSL_PREACTIVATION_CFG, parm, 12))
947 		return (-1);
948 
949 	parm[0] = 0x03;		/* dying gasp time - 3 frames */
950 	parm[1] = thresh[cfg.mod];
951 	parm[2] = 0xff;		/* attenuation */
952 	parm[3] = 0x04;		/* line probe NMR (+2 dB) */
953 	parm[4] = 0x00;		/* reserved */
954 	parm[5] = 0x00;
955 	if (issue_cx28975_cmd(sc, _DSL_THRESHOLDS, parm, 6))
956 		return (-1);
957 
958 	t = cfg.master ? 0x23 : 0x21;
959 	if (issue_cx28975_cmd(sc, _DSL_FR_PCM_CONFIG, &t, 1))
960 		return (-1);
961 
962 	t = 0x02;
963 	if (issue_cx28975_cmd(sc, _DSL_INTR_HOST_MASK, &t, 1))
964 		return (-1);
965 
966 	sc->state = DOWN;
967 	return (0);
968 }
969 
970 static int
971 download_firmware(struct sbsh_softc *sc, u_int8_t *img, u_int32_t img_len)
972 {
973 	u_int32_t	t;
974 	int		i;
975 	u_int8_t	cksum = 0;
976 
977 	for (i = 0; i < img_len; ++i)
978 		cksum += img[i];
979 
980 	t = img_len;
981 	if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_START, (u_int8_t *) &t, 4))
982 		return (-1);
983 
984 	for (i = 0; img_len >= 75; i += 75, img_len -= 75) {
985 		if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, 75))
986 			return (-1);
987 	}
988 
989 	if (img_len
990 	    &&  issue_cx28975_cmd(sc, _DSL_DOWNLOAD_DATA, img + i, img_len))
991 		return (-1);
992 
993 	t = (cksum ^ 0xff) + 1;
994 	if (issue_cx28975_cmd(sc, _DSL_DOWNLOAD_END, (u_int8_t *) &t, 1))
995 		return (-1);
996 
997 	return (0);
998 }
999 
1000 static int
1001 issue_cx28975_cmd(struct sbsh_softc *sc, u_int8_t cmd,
1002 			u_int8_t *data, u_int8_t size)
1003 {
1004 	volatile struct cx28975_cmdarea  *p = sc->cmdp;
1005 	volatile u_int8_t  *databuf = p->in_data;
1006 	int  i;
1007 
1008 	u_int8_t  cksum = 0;
1009 
1010 	p->in_dest	= 0xf0;
1011 	p->in_opcode	= cmd;
1012 	p->in_zero	= 0;
1013 	p->in_length	= --size;
1014 	p->in_csum	= 0xf0 ^ cmd ^ size ^ 0xaa;
1015 
1016 	for (i = 0; i <= size; ++i) {
1017 		cksum ^= *data;
1018 		*databuf++ = *data++;	/* only 1 byte per cycle! */
1019 	}
1020 
1021 	p->in_datasum	= cksum ^ 0xaa;
1022 	p->out_ack	= _ACK_NOT_COMPLETE;
1023 	p->intr_8051	= 0xfe;
1024 
1025 	if (tsleep(sc, 0, "sbsh", hz << 3))
1026 		return (-1);
1027 
1028 	while (p->out_ack == _ACK_NOT_COMPLETE)
1029 		;					/* FIXME ! */
1030 
1031 	if ((p->out_ack & 0x1f) == _ACK_PASS) {
1032 		p->out_ack = 0;
1033 		return (0);
1034 	} else {
1035 		p->out_ack = 0;
1036 		return (-1);
1037 	}
1038 }
1039