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