if_ni.c revision 1.4
1/* $NetBSD: if_ni.c,v 1.4 2000/06/04 06:17:00 matt Exp $ */ 2/* 3 * Copyright (c) 2000 Ludd, University of Lule}, Sweden. All rights reserved. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed at Ludd, University of 16 * Lule}, Sweden and its contributors. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* 33 * Driver for DEBNA/DEBNT/DEBNK ethernet cards. 34 * Things that is still to do: 35 * Collect statistics. 36 */ 37 38#include "opt_inet.h" 39#include "bpfilter.h" 40 41#include <sys/param.h> 42#include <sys/mbuf.h> 43#include <sys/socket.h> 44#include <sys/device.h> 45#include <sys/systm.h> 46#include <sys/sockio.h> 47 48#include <net/if.h> 49#include <net/if_ether.h> 50#include <net/if_dl.h> 51 52#include <netinet/in.h> 53#include <netinet/if_inarp.h> 54 55#if NBPFILTER > 0 56#include <net/bpf.h> 57#include <net/bpfdesc.h> 58#endif 59 60#include <machine/bus.h> 61#ifdef __vax__ 62#include <machine/mtpr.h> 63#include <machine/pte.h> 64#endif 65 66#include <dev/bi/bireg.h> 67#include <dev/bi/bivar.h> 68 69#include "ioconf.h" 70#include "locators.h" 71 72/* 73 * Tunable buffer parameters. Good idea to have them as power of 8; then 74 * they will fit into a logical VAX page. 75 */ 76#define NMSGBUF 8 /* Message queue entries */ 77#define NTXBUF 16 /* Transmit queue entries */ 78#define NTXFRAGS 8 /* Number of transmit buffer fragments */ 79#define NRXBUF 24 /* Receive queue entries */ 80#define NBDESCS (NTXBUF * NTXFRAGS + NRXBUF) 81#define NQUEUES 3 /* RX + TX + MSG */ 82#define PKTHDR 18 /* Length of (control) packet header */ 83#define RXADD 18 /* Additional length of receive datagram */ 84#define TXADD (10+NTXFRAGS*8) /* "" transmit "" */ 85#define MSGADD 134 /* "" message "" */ 86 87#include <dev/bi/if_nireg.h> /* XXX include earlier */ 88 89/* 90 * Macros for (most cases of) insqti/remqhi. 91 * Retry NRETRIES times to do the operation, if it still fails assume 92 * a lost lock and panic. 93 */ 94#define NRETRIES 100 95#define INSQTI(e, h) ({ \ 96 int ret, i; \ 97 for (i = 0; i < NRETRIES; i++) { \ 98 if ((ret = insqti(e, h)) != ILCK_FAILED) \ 99 break; \ 100 } \ 101 if (i == NRETRIES) \ 102 panic("ni: insqti failed at %d", __LINE__); \ 103 ret; \ 104}) 105#define REMQHI(h) ({ \ 106 int i;void *ret; \ 107 for (i = 0; i < NRETRIES; i++) { \ 108 if ((ret = remqhi(h)) != (void *)ILCK_FAILED) \ 109 break; \ 110 } \ 111 if (i == NRETRIES) \ 112 panic("ni: remqhi failed at %d", __LINE__); \ 113 ret; \ 114}) 115 116 117#define nipqb (&sc->sc_gvppqb->nc_pqb) 118#define gvp sc->sc_gvppqb 119#define fqb sc->sc_fqb 120#define bbd sc->sc_bbd 121 122struct ni_softc { 123 struct device sc_dev; /* Configuration common part */ 124 struct evcnt sc_intrcnt; /* Interrupt coounting */ 125 struct ethercom sc_ec; /* Ethernet common part */ 126#define sc_if sc_ec.ec_if /* network-visible interface */ 127 bus_space_tag_t sc_iot; 128 bus_addr_t sc_ioh; 129 bus_dma_tag_t sc_dmat; 130 struct ni_gvppqb *sc_gvppqb; /* Port queue block */ 131 struct ni_gvppqb *sc_pgvppqb; /* Phys address of PQB */ 132 struct ni_fqb *sc_fqb; /* Free Queue block */ 133 struct ni_bbd *sc_bbd; /* Buffer descriptors */ 134 u_int8_t sc_enaddr[ETHER_ADDR_LEN]; 135}; 136 137static int nimatch __P((struct device *, struct cfdata *, void *)); 138static void niattach __P((struct device *, struct device *, void *)); 139static void niinit __P((struct ni_softc *)); 140static void nistart __P((struct ifnet *)); 141static void niintr __P((void *)); 142static int niioctl __P((struct ifnet *, u_long, caddr_t)); 143static int ni_add_rxbuf(struct ni_softc *, struct ni_dg *, int); 144static void ni_setup __P((struct ni_softc *)); 145static void nitimeout __P((struct ifnet *)); 146static void ni_shutdown(void *); 147static void ni_getpgs(struct ni_softc *sc, int size, caddr_t *v, paddr_t *p); 148static int failtest(struct ni_softc *, int, int, int, char *); 149 150volatile int endwait, retry; /* Used during autoconfig */ 151 152struct cfattach ni_ca = { 153 sizeof(struct ni_softc), nimatch, niattach 154}; 155 156#define NI_WREG(csr, val) \ 157 bus_space_write_4(sc->sc_iot, sc->sc_ioh, csr, val) 158#define NI_RREG(csr) \ 159 bus_space_read_4(sc->sc_iot, sc->sc_ioh, csr) 160 161#define WAITREG(csr,val) while (NI_RREG(csr) & val); 162/* 163 * Check for present device. 164 */ 165int 166nimatch(parent, cf, aux) 167 struct device *parent; 168 struct cfdata *cf; 169 void *aux; 170{ 171 struct bi_attach_args *ba = aux; 172 u_short type; 173 174 type = bus_space_read_2(ba->ba_iot, ba->ba_ioh, BIREG_DTYPE); 175 if (type != BIDT_DEBNA && type != BIDT_DEBNT && type != BIDT_DEBNK) 176 return 0; 177 178 if (cf->cf_loc[BICF_NODE] != BICF_NODE_DEFAULT && 179 cf->cf_loc[BICF_NODE] != ba->ba_nodenr) 180 return 0; 181 182 return 1; 183} 184 185/* 186 * Allocate a bunch of descriptor-safe memory. 187 * We need to get the structures from the beginning of its own pages. 188 */ 189static void 190ni_getpgs(struct ni_softc *sc, int size, caddr_t *v, paddr_t *p) 191{ 192 bus_dma_segment_t seg; 193 int nsegs, error; 194 195 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1, 196 &nsegs, BUS_DMA_NOWAIT)) != 0) 197 panic(" unable to allocate memory: error %d", error); 198 199 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, v, 200 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) 201 panic(" unable to map memory: error %d", error); 202 203 if (p) 204 *p = seg.ds_addr; 205 bzero(*v, size); 206} 207 208static int 209failtest(struct ni_softc *sc, int reg, int mask, int test, char *str) 210{ 211 int i = 100; 212 213 do { 214 DELAY(100000); 215 } while (((NI_RREG(reg) & mask) != test) && --i); 216 217 if (i == 0) { 218 printf("%s: %s\n", sc->sc_dev.dv_xname, str); 219 return 1; 220 } 221 return 0; 222} 223 224 225/* 226 * Interface exists: make available by filling in network interface 227 * record. System will initialize the interface when it is ready 228 * to accept packets. 229 */ 230void 231niattach(parent, self, aux) 232 struct device *parent, *self; 233 void *aux; 234{ 235 struct bi_attach_args *ba = aux; 236 struct ni_softc *sc = (struct ni_softc *)self; 237 struct ifnet *ifp = (struct ifnet *)&sc->sc_if; 238 struct ni_msg *msg; 239 struct ni_ptdb *ptdb; 240 caddr_t va; 241 int i, j, s, res; 242 u_short type; 243 244 type = bus_space_read_2(ba->ba_iot, ba->ba_ioh, BIREG_DTYPE); 245 printf(": DEBN%c\n", type == BIDT_DEBNA ? 'A' : type == BIDT_DEBNT ? 246 'T' : 'K'); 247 sc->sc_iot = ba->ba_iot; 248 sc->sc_ioh = ba->ba_ioh; 249 sc->sc_dmat = ba->ba_dmat; 250 251 bi_intr_establish(ba->ba_icookie, ba->ba_ivec, 252 niintr, sc, &sc->sc_intrcnt); 253 evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt); 254 255 ni_getpgs(sc, sizeof(struct ni_gvppqb), (caddr_t *)&sc->sc_gvppqb, 256 (paddr_t *)&sc->sc_pgvppqb); 257 ni_getpgs(sc, sizeof(struct ni_fqb), (caddr_t *)&sc->sc_fqb, 0); 258 ni_getpgs(sc, NBDESCS * sizeof(struct ni_bbd), 259 (caddr_t *)&sc->sc_bbd, 0); 260 /* 261 * Zero the newly allocated memory. 262 */ 263 264 nipqb->np_veclvl = (ba->ba_ivec << 2) + 2; 265 nipqb->np_node = ba->ba_intcpu; 266 nipqb->np_vpqb = (u_int32_t)gvp; 267#ifdef __vax__ 268 nipqb->np_spt = nipqb->np_gpt = mfpr(PR_SBR); 269 nipqb->np_sptlen = nipqb->np_gptlen = mfpr(PR_SLR); 270#else 271#error Must fix support for non-vax. 272#endif 273 nipqb->np_bvplvl = 1; 274 nipqb->np_vfqb = (u_int32_t)fqb; 275 nipqb->np_vbdt = (u_int32_t)bbd; 276 nipqb->np_nbdr = NBDESCS; 277 278 /* Free queue block */ 279 nipqb->np_freeq = NQUEUES; 280 fqb->nf_mlen = PKTHDR+MSGADD; 281 fqb->nf_dlen = PKTHDR+TXADD; 282 fqb->nf_rlen = PKTHDR+RXADD; 283 284 strcpy(ifp->if_xname, sc->sc_dev.dv_xname); 285 ifp->if_softc = sc; 286 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 287 ifp->if_start = nistart; 288 ifp->if_ioctl = niioctl; 289 ifp->if_watchdog = nitimeout; 290 291 /* 292 * Start init sequence. 293 */ 294 295 /* Reset the node */ 296 NI_WREG(BIREG_VAXBICSR, NI_RREG(BIREG_VAXBICSR) | BICSR_NRST); 297 DELAY(500000); 298 i = 20; 299 while ((NI_RREG(BIREG_VAXBICSR) & BICSR_BROKE) && --i) 300 DELAY(500000); 301 if (i == 0) { 302 printf("%s: BROKE bit set after reset\n", sc->sc_dev.dv_xname); 303 return; 304 } 305 306 /* Check state */ 307 if (failtest(sc, NI_PSR, PSR_STATE, PSR_UNDEF, "not undefined state")) 308 return; 309 310 /* Clear owner bits */ 311 NI_WREG(NI_PSR, NI_RREG(NI_PSR) & ~PSR_OWN); 312 NI_WREG(NI_PCR, NI_RREG(NI_PCR) & ~PCR_OWN); 313 314 /* kick off init */ 315 NI_WREG(NI_PCR, (u_int32_t)sc->sc_pgvppqb | PCR_INIT | PCR_OWN); 316 while (NI_RREG(NI_PCR) & PCR_OWN) 317 DELAY(100000); 318 319 /* Check state */ 320 if (failtest(sc, NI_PSR, PSR_INITED, PSR_INITED, "failed initialize")) 321 return; 322 323 NI_WREG(NI_PSR, NI_RREG(NI_PSR) & ~PSR_OWN); 324 325 WAITREG(NI_PCR, PCR_OWN); 326 NI_WREG(NI_PCR, PCR_OWN|PCR_ENABLE); 327 WAITREG(NI_PCR, PCR_OWN); 328 WAITREG(NI_PSR, PSR_OWN); 329 330 /* Check state */ 331 if (failtest(sc, NI_PSR, PSR_STATE, PSR_ENABLED, "failed enable")) 332 return; 333 334 NI_WREG(NI_PSR, NI_RREG(NI_PSR) & ~PSR_OWN); 335 336 /* 337 * The message queue packets must be located on the beginning 338 * of a page. A VAX page is 512 bytes, but it clusters 8 pages. 339 * This knowledge is used here when allocating pages. 340 * !!! How should this be done on MIPS and Alpha??? !!! 341 */ 342#if NBPG < 4096 343#error pagesize too small 344#endif 345 s = splimp(); 346 /* Set up message free queue */ 347 ni_getpgs(sc, NMSGBUF * 512, &va, 0); 348 for (i = 0; i < NMSGBUF; i++) { 349 struct ni_msg *msg; 350 351 msg = (void *)(va + i * 512); 352 353 res = INSQTI(msg, &fqb->nf_mforw); 354 } 355 WAITREG(NI_PCR, PCR_OWN); 356 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_MFREEQ|PCR_OWN); 357 WAITREG(NI_PCR, PCR_OWN); 358 359 /* Set up xmit queue */ 360 ni_getpgs(sc, NTXBUF * 512, &va, 0); 361 for (i = 0; i < NTXBUF; i++) { 362 struct ni_dg *data; 363 364 data = (void *)(va + i * 512); 365 data->nd_status = 0; 366 data->nd_len = TXADD; 367 data->nd_ptdbidx = 1; 368 data->nd_opcode = BVP_DGRAM; 369 for (j = 0; j < NTXFRAGS; j++) { 370 data->bufs[j]._offset = 0; 371 data->bufs[j]._key = 1; 372 bbd[i * NTXFRAGS + j].nb_key = 1; 373 bbd[i * NTXFRAGS + j].nb_status = 0; 374 data->bufs[j]._index = i * NTXFRAGS + j; 375 } 376 res = INSQTI(data, &fqb->nf_dforw); 377 } 378 WAITREG(NI_PCR, PCR_OWN); 379 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_DFREEQ|PCR_OWN); 380 WAITREG(NI_PCR, PCR_OWN); 381 382 /* recv buffers */ 383 ni_getpgs(sc, NRXBUF * 512, &va, 0); 384 for (i = 0; i < NRXBUF; i++) { 385 struct ni_dg *data; 386 int idx; 387 388 data = (void *)(va + i * 512); 389 data->nd_len = RXADD; 390 data->nd_opcode = BVP_DGRAMRX; 391 data->nd_ptdbidx = 2; 392 data->bufs[0]._key = 1; 393 394 idx = NTXBUF * NTXFRAGS + i; 395 if (ni_add_rxbuf(sc, data, idx)) 396 panic("niattach: ni_add_rxbuf: out of mbufs"); 397 398 res = INSQTI(data, &fqb->nf_rforw); 399 } 400 WAITREG(NI_PCR, PCR_OWN); 401 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_RFREEQ|PCR_OWN); 402 WAITREG(NI_PCR, PCR_OWN); 403 404 splx(s); 405 406 /* Set initial parameters */ 407 msg = REMQHI(&fqb->nf_mforw); 408 409 msg->nm_opcode = BVP_MSG; 410 msg->nm_status = 0; 411 msg->nm_len = sizeof(struct ni_param) + 6; 412 msg->nm_opcode2 = NI_WPARAM; 413 ((struct ni_param *)&msg->nm_text[0])->np_flags = NP_PAD; 414 415 endwait = retry = 0; 416 res = INSQTI(msg, &gvp->nc_forw0); 417 418retry: WAITREG(NI_PCR, PCR_OWN); 419 NI_WREG(NI_PCR, PCR_CMDQNE|PCR_CMDQ0|PCR_OWN); 420 WAITREG(NI_PCR, PCR_OWN); 421 i = 1000; 422 while (endwait == 0 && --i) 423 DELAY(10000); 424 425 if (endwait == 0) { 426 if (++retry < 3) 427 goto retry; 428 printf("%s: no response to set params\n", sc->sc_dev.dv_xname); 429 return; 430 } 431 432 /* Clear counters */ 433 msg = REMQHI(&fqb->nf_mforw); 434 msg->nm_opcode = BVP_MSG; 435 msg->nm_status = 0; 436 msg->nm_len = sizeof(struct ni_param) + 6; 437 msg->nm_opcode2 = NI_RCCNTR; 438 439 res = INSQTI(msg, &gvp->nc_forw0); 440 441 WAITREG(NI_PCR, PCR_OWN); 442 NI_WREG(NI_PCR, PCR_CMDQNE|PCR_CMDQ0|PCR_OWN); 443 WAITREG(NI_PCR, PCR_OWN); 444 445 /* Enable transmit logic */ 446 msg = REMQHI(&fqb->nf_mforw); 447 448 msg->nm_opcode = BVP_MSG; 449 msg->nm_status = 0; 450 msg->nm_len = 18; 451 msg->nm_opcode2 = NI_STPTDB; 452 ptdb = (struct ni_ptdb *)&msg->nm_text[0]; 453 bzero(ptdb, sizeof(struct ni_ptdb)); 454 ptdb->np_index = 1; 455 ptdb->np_fque = 1; 456 457 res = INSQTI(msg, &gvp->nc_forw0); 458 459 WAITREG(NI_PCR, PCR_OWN); 460 NI_WREG(NI_PCR, PCR_CMDQNE|PCR_CMDQ0|PCR_OWN); 461 WAITREG(NI_PCR, PCR_OWN); 462 463 /* Wait for everything to finish */ 464 WAITREG(NI_PSR, PSR_OWN); 465 466 printf("%s: hardware address %s\n", sc->sc_dev.dv_xname, 467 ether_sprintf(sc->sc_enaddr)); 468 469 /* 470 * Attach the interface. 471 */ 472 if_attach(ifp); 473 ether_ifattach(ifp, sc->sc_enaddr); 474 if (shutdownhook_establish(ni_shutdown, sc) == 0) 475 printf("%s: WARNING: unable to establish shutdown hook\n", 476 sc->sc_dev.dv_xname); 477 478#if NBPFILTER > 0 479 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); 480#endif 481} 482 483/* 484 * Initialization of interface. 485 */ 486void 487niinit(sc) 488 struct ni_softc *sc; 489{ 490 struct ifnet *ifp = (struct ifnet *)&sc->sc_if; 491 492 /* 493 * Set flags (so ni_setup() do the right thing). 494 */ 495 ifp->if_flags |= IFF_RUNNING; 496 ifp->if_flags &= ~IFF_OACTIVE; 497 498 /* 499 * Send setup messages so that the rx/tx locic starts. 500 */ 501 ni_setup(sc); 502 503} 504 505/* 506 * Start output on interface. 507 */ 508void 509nistart(ifp) 510 struct ifnet *ifp; 511{ 512 struct ni_softc *sc = ifp->if_softc; 513 struct ni_dg *data; 514 struct ni_bbd *bdp; 515 struct mbuf *m, *m0; 516 int i, cnt, res, mlen; 517 518 if (ifp->if_flags & IFF_OACTIVE) 519 return; 520#ifdef DEBUG 521 if (ifp->if_flags & IFF_DEBUG) 522 printf("%s: nistart\n", sc->sc_dev.dv_xname); 523#endif 524 525 while (fqb->nf_dforw) { 526 IF_DEQUEUE(&sc->sc_if.if_snd, m); 527 if (m == 0) 528 break; 529 530 data = REMQHI(&fqb->nf_dforw); 531 if ((int)data == Q_EMPTY) { 532 IF_PREPEND(&sc->sc_if.if_snd, m); 533 ifp->if_flags |= IFF_OACTIVE; 534 break; 535 } 536 537 /* 538 * Count number of mbufs in chain. 539 * Always do DMA directly from mbufs, therefore the transmit 540 * ring is really big. 541 */ 542 for (m0 = m, cnt = 0; m0; m0 = m0->m_next) 543 if (m0->m_len) 544 cnt++; 545 if (cnt > NTXFRAGS) 546 panic("nistart"); /* XXX */ 547 548#if NBPFILTER > 0 549 if (ifp->if_bpf) 550 bpf_mtap(ifp->if_bpf, m); 551#endif 552 bdp = &bbd[(data->bufs[0]._index & 0x7fff)]; 553 for (m0 = m, i = 0, mlen = 0; m0; m0 = m0->m_next) { 554 if (m0->m_len == 0) 555 continue; 556 bdp->nb_status = (mtod(m0, u_int32_t) & NIBD_OFFSET) | 557 NIBD_VALID; 558 bdp->nb_pte = (u_int32_t)kvtopte(mtod(m0, void *)); 559 bdp->nb_len = m0->m_len; 560 data->bufs[i]._offset = 0; 561 data->bufs[i]._len = bdp->nb_len; 562 data->bufs[i]._index |= NIDG_CHAIN; 563 mlen += bdp->nb_len; 564 bdp++; 565 i++; 566 } 567 data->nd_opcode = BVP_DGRAM; 568 data->nd_pad3 = 1; 569 data->nd_ptdbidx = 1; 570 data->nd_len = 10 + i * 8; 571 data->bufs[i - 1]._index &= ~NIDG_CHAIN; 572 if (mlen < 64) 573 data->bufs[i - 1]._len = bdp[-1].nb_len += (64 - mlen); 574 data->nd_cmdref = (u_int32_t)m; 575#ifdef DEBUG 576 if (ifp->if_flags & IFF_DEBUG) 577 printf("%s: sending %d bytes (%d segments)\n", 578 sc->sc_dev.dv_xname, mlen, i); 579#endif 580 581 res = INSQTI(data, &gvp->nc_forw0); 582 if (res == Q_EMPTY) { 583 WAITREG(NI_PCR, PCR_OWN); 584 NI_WREG(NI_PCR, PCR_CMDQNE|PCR_CMDQ0|PCR_OWN); 585 } 586 } 587} 588 589void 590niintr(void *arg) 591{ 592 struct ether_header *eh; 593 struct ni_softc *sc = arg; 594 struct ni_dg *data; 595 struct ni_msg *msg; 596 struct ifnet *ifp = &sc->sc_if; 597 struct ni_bbd *bd; 598 struct mbuf *m; 599 int idx, res; 600 601 if ((NI_RREG(NI_PSR) & PSR_STATE) != PSR_ENABLED) 602 return; 603 604 if ((NI_RREG(NI_PSR) & PSR_ERR)) 605 printf("%s: PSR %x\n", sc->sc_dev.dv_xname, NI_RREG(NI_PSR)); 606 607 /* Got any response packets? */ 608 while ((NI_RREG(NI_PSR) & PSR_RSQ) && (data = REMQHI(&gvp->nc_forwr))) { 609 610 switch (data->nd_opcode) { 611 case BVP_DGRAMRX: /* Receive datagram */ 612 idx = data->bufs[0]._index; 613 bd = &bbd[idx]; 614 m = (void *)data->nd_cmdref; 615 m->m_pkthdr.len = m->m_len = data->bufs[0]._len; 616 m->m_pkthdr.rcvif = ifp; 617 if (ni_add_rxbuf(sc, data, idx)) { 618 bd->nb_len = (m->m_ext.ext_size - 2); 619 bd->nb_pte = 620 (long)kvtopte(m->m_ext.ext_buf); 621 bd->nb_status = 2 | NIBD_VALID; 622 bd->nb_key = 1; 623 } 624 data->nd_len = RXADD; 625 data->nd_status = 0; 626 res = INSQTI(data, &fqb->nf_rforw); 627 if (res == Q_EMPTY) { 628 WAITREG(NI_PCR, PCR_OWN); 629 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_RFREEQ|PCR_OWN); 630 } 631 if (m == (void *)data->nd_cmdref) 632 break; /* Out of mbufs */ 633 634#if NBPFILTER > 0 635 eh = mtod(m, struct ether_header *); 636 if (ifp->if_bpf) { 637 bpf_mtap(ifp->if_bpf, m); 638 if ((ifp->if_flags & IFF_PROMISC) != 0 639 && bcmp(LLADDR(ifp->if_sadl), 640 eh->ether_dhost, 641 ETHER_ADDR_LEN) != 0 && 642 ((eh->ether_dhost[0] & 1) == 0)) { 643 m_freem(m); 644 continue; 645 } 646 } 647#endif 648 (*ifp->if_input)(ifp, m); 649 break; 650 651 case BVP_DGRAM: 652 m = (struct mbuf *)data->nd_cmdref; 653 ifp->if_flags &= ~IFF_OACTIVE; 654 m_freem(m); 655 res = INSQTI(data, &fqb->nf_dforw); 656 if (res == Q_EMPTY) { 657 WAITREG(NI_PCR, PCR_OWN); 658 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_DFREEQ|PCR_OWN); 659 } 660 break; 661 662 case BVP_MSGRX: 663 msg = (struct ni_msg *)data; 664 switch (msg->nm_opcode2) { 665 case NI_WPARAM: 666 bcopy(((struct ni_param *)&msg->nm_text[0])->np_dpa, sc->sc_enaddr, ETHER_ADDR_LEN); 667 endwait = 1; 668 break; 669 670 case NI_RCCNTR: 671 case NI_CLPTDB: 672 case NI_STPTDB: 673 break; 674 675 default: 676 printf("Unkn resp %d\n", 677 msg->nm_opcode2); 678 break; 679 } 680 res = INSQTI(data, &fqb->nf_mforw); 681 if (res == Q_EMPTY) { 682 WAITREG(NI_PCR, PCR_OWN); 683 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_MFREEQ|PCR_OWN); 684 } 685 break; 686 687 default: 688 printf("Unknown opcode %d\n", data->nd_opcode); 689 res = INSQTI(data, &fqb->nf_mforw); 690 if (res == Q_EMPTY) { 691 WAITREG(NI_PCR, PCR_OWN); 692 NI_WREG(NI_PCR, PCR_FREEQNE|PCR_MFREEQ|PCR_OWN); 693 } 694 } 695 } 696 697 /* Try to kick on the start routine again */ 698 nistart(ifp); 699 700 NI_WREG(NI_PSR, NI_RREG(NI_PSR) & ~(PSR_OWN|PSR_RSQ)); 701} 702 703/* 704 * Process an ioctl request. 705 */ 706int 707niioctl(ifp, cmd, data) 708 register struct ifnet *ifp; 709 u_long cmd; 710 caddr_t data; 711{ 712 struct ni_softc *sc = ifp->if_softc; 713 struct ifreq *ifr = (struct ifreq *)data; 714 struct ifaddr *ifa = (struct ifaddr *)data; 715 int s = splnet(), error = 0; 716 717 switch (cmd) { 718 719 case SIOCSIFADDR: 720 ifp->if_flags |= IFF_UP; 721 switch(ifa->ifa_addr->sa_family) { 722#ifdef INET 723 case AF_INET: 724 niinit(sc); 725 arp_ifinit(ifp, ifa); 726 break; 727#endif 728 } 729 break; 730 731 case SIOCSIFFLAGS: 732 if ((ifp->if_flags & IFF_UP) == 0 && 733 (ifp->if_flags & IFF_RUNNING) != 0) { 734 /* 735 * If interface is marked down and it is running, 736 * stop it. 737 */ 738 ifp->if_flags &= ~IFF_RUNNING; 739 ni_setup(sc); 740 } else if ((ifp->if_flags & IFF_UP) != 0 && 741 (ifp->if_flags & IFF_RUNNING) == 0) { 742 /* 743 * If interface it marked up and it is stopped, then 744 * start it. 745 */ 746 niinit(sc); 747 } else if ((ifp->if_flags & IFF_UP) != 0) { 748 /* 749 * Send a new setup packet to match any new changes. 750 * (Like IFF_PROMISC etc) 751 */ 752 ni_setup(sc); 753 } 754 break; 755 756 case SIOCADDMULTI: 757 case SIOCDELMULTI: 758 /* 759 * Update our multicast list. 760 */ 761 error = (cmd == SIOCADDMULTI) ? 762 ether_addmulti(ifr, &sc->sc_ec): 763 ether_delmulti(ifr, &sc->sc_ec); 764 765 if (error == ENETRESET) { 766 /* 767 * Multicast list has changed; set the hardware filter 768 * accordingly. 769 */ 770 ni_setup(sc); 771 error = 0; 772 } 773 break; 774 775 default: 776 error = EINVAL; 777 778 } 779 splx(s); 780 return (error); 781} 782 783/* 784 * Add a receive buffer to the indicated descriptor. 785 */ 786int 787ni_add_rxbuf(struct ni_softc *sc, struct ni_dg *data, int idx) 788{ 789 struct ni_bbd *bd = &bbd[idx]; 790 struct mbuf *m; 791 792 MGETHDR(m, M_DONTWAIT, MT_DATA); 793 if (m == NULL) 794 return (ENOBUFS); 795 796 MCLGET(m, M_DONTWAIT); 797 if ((m->m_flags & M_EXT) == 0) { 798 m_freem(m); 799 return (ENOBUFS); 800 } 801 802 m->m_data += 2; 803 bd->nb_len = (m->m_ext.ext_size - 2); 804 bd->nb_pte = (long)kvtopte(m->m_ext.ext_buf); 805 bd->nb_status = 2 | NIBD_VALID; 806 bd->nb_key = 1; 807 808 data->bufs[0]._offset = 0; 809 data->bufs[0]._len = bd->nb_len; 810 data->bufs[0]._index = idx; 811 data->nd_cmdref = (long)m; 812 813 return (0); 814} 815 816/* 817 * Create setup packet and put in queue for sending. 818 */ 819void 820ni_setup(struct ni_softc *sc) 821{ 822 struct ifnet *ifp = &sc->sc_if; 823 struct ni_msg *msg; 824 struct ni_ptdb *ptdb; 825 struct ether_multi *enm; 826 struct ether_multistep step; 827 int i, res; 828 829 msg = REMQHI(&fqb->nf_mforw); 830 if ((int)msg == Q_EMPTY) 831 return; /* What to do? */ 832 833 ptdb = (struct ni_ptdb *)&msg->nm_text[0]; 834 bzero(ptdb, sizeof(struct ni_ptdb)); 835 836 msg->nm_opcode = BVP_MSG; 837 msg->nm_len = 18; 838 ptdb->np_index = 2; /* definition type index */ 839 ptdb->np_fque = 2; /* Free queue */ 840 if (ifp->if_flags & IFF_RUNNING) { 841 msg->nm_opcode2 = NI_STPTDB; 842 ptdb->np_type = ETHERTYPE_IP; 843 ptdb->np_flags = PTDB_UNKN|PTDB_BDC; 844 if (ifp->if_flags & IFF_PROMISC) 845 ptdb->np_flags |= PTDB_PROMISC; 846 memset(ptdb->np_mcast[0], 0xff, ETHER_ADDR_LEN); /* Broadcast */ 847 ptdb->np_adrlen = 1; 848 msg->nm_len += 8; 849 ifp->if_flags &= ~IFF_ALLMULTI; 850 if ((ifp->if_flags & IFF_PROMISC) == 0) { 851 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm); 852 i = 1; 853 while (enm != NULL) { 854 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) { 855 ifp->if_flags |= IFF_ALLMULTI; 856 ptdb->np_flags |= PTDB_AMC; 857 break; 858 } 859 msg->nm_len += 8; 860 ptdb->np_adrlen++; 861 bcopy(enm->enm_addrlo, ptdb->np_mcast[i++], 862 ETHER_ADDR_LEN); 863 ETHER_NEXT_MULTI(step, enm); 864 } 865 } 866 } else 867 msg->nm_opcode2 = NI_CLPTDB; 868 869 res = INSQTI(msg, &gvp->nc_forw0); 870 if (res == Q_EMPTY) { 871 WAITREG(NI_PCR, PCR_OWN); 872 NI_WREG(NI_PCR, PCR_CMDQNE|PCR_CMDQ0|PCR_OWN); 873 } 874} 875 876/* 877 * Check for dead transmit logic. Not uncommon. 878 */ 879void 880nitimeout(ifp) 881 struct ifnet *ifp; 882{ 883#if 0 884 struct ni_softc *sc = ifp->if_softc; 885 886 if (sc->sc_inq == 0) 887 return; 888 889 printf("%s: xmit logic died, resetting...\n", sc->sc_dev.dv_xname); 890 /* 891 * Do a reset of interface, to get it going again. 892 * Will it work by just restart the transmit logic? 893 */ 894 niinit(sc); 895#endif 896} 897 898/* 899 * Shutdown hook. Make sure the interface is stopped at reboot. 900 */ 901void 902ni_shutdown(arg) 903 void *arg; 904{ 905 struct ni_softc *sc = arg; 906 907 WAITREG(NI_PCR, PCR_OWN); 908 NI_WREG(NI_PCR, PCR_OWN|PCR_SHUTDOWN); 909 WAITREG(NI_PCR, PCR_OWN); 910 WAITREG(NI_PSR, PSR_OWN); 911 912} 913 914