+/*
+ * Given a NIC memory source address and a host memory destination
+ * address, copy 'amount' from NIC to host using Programmed I/O.
+ * The 'amount' is rounded up to a word - okay as long as mbufs
+ * are word sized.
+ * This routine is currently Novell-specific.
+ */
+void
+ed_pio_readmem(sc,src,dst,amount)
+ struct ed_softc *sc;
+ unsigned short src;
+ unsigned char *dst;
+ unsigned short amount;
+{
+ unsigned short tmp_amount;
+
+ /* select page 0 registers */
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
+
+ /* round up to a word */
+ tmp_amount = amount;
+ if (amount & 1) ++amount;
+
+ /* set up DMA byte count */
+ outb(sc->nic_addr + ED_P0_RBCR0, amount);
+ outb(sc->nic_addr + ED_P0_RBCR1, amount>>8);
+
+ /* set up source address in NIC mem */
+ outb(sc->nic_addr + ED_P0_RSAR0, src);
+ outb(sc->nic_addr + ED_P0_RSAR1, src>>8);
+
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD0 | ED_CR_STA);
+
+ if (sc->isa16bit) {
+ insw(sc->asic_addr + ED_NOVELL_DATA, dst, amount/2);
+ } else
+ insb(sc->asic_addr + ED_NOVELL_DATA, dst, amount);
+
+}
+
+/*
+ * Stripped down routine for writing a linear buffer to NIC memory.
+ * Only used in the probe routine to test the memory. 'len' must
+ * be even.
+ */
+void
+ed_pio_writemem(sc,src,dst,len)
+ struct ed_softc *sc;
+ char *src;
+ unsigned short dst;
+ unsigned short len;
+{
+ int maxwait=100; /* about 120us */
+
+ /* select page 0 registers */
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
+
+ /* reset remote DMA complete flag */
+ outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
+
+ /* set up DMA byte count */
+ outb(sc->nic_addr + ED_P0_RBCR0, len);
+ outb(sc->nic_addr + ED_P0_RBCR1, len>>8);
+
+ /* set up destination address in NIC mem */
+ outb(sc->nic_addr + ED_P0_RSAR0, dst);
+ outb(sc->nic_addr + ED_P0_RSAR1, dst>>8);
+
+ /* set remote DMA write */
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
+
+ if (sc->isa16bit)
+ outsw(sc->asic_addr + ED_NOVELL_DATA, src, len/2);
+ else
+ outsb(sc->asic_addr + ED_NOVELL_DATA, src, len);
+ /*
+ * Wait for remote DMA complete. This is necessary because on the
+ * transmit side, data is handled internally by the NIC in bursts
+ * and we can't start another remote DMA until this one completes.
+ * Not waiting causes really bad things to happen - like the NIC
+ * irrecoverably jamming the ISA bus.
+ */
+ while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
+}
+
+/*
+ * Write an mbuf chain to the destination NIC memory address using
+ * programmed I/O.
+ */
+u_short
+ed_pio_write_mbufs(sc,m,dst)
+ struct ed_softc *sc;
+ struct mbuf *m;
+ unsigned short dst;
+{
+ unsigned short len, mb_offset;
+ struct mbuf *mp;
+ unsigned char residual[2];
+ int maxwait=100; /* about 120us */
+
+ /* First, count up the total number of bytes to copy */
+ for (len = 0, mp = m; mp; mp = mp->m_next)
+ len += mp->m_len;
+
+ /* select page 0 registers */
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD2|ED_CR_STA);
+
+ /* reset remote DMA complete flag */
+ outb(sc->nic_addr + ED_P0_ISR, ED_ISR_RDC);
+
+ /* set up DMA byte count */
+ outb(sc->nic_addr + ED_P0_RBCR0, len);
+ outb(sc->nic_addr + ED_P0_RBCR1, len>>8);
+
+ /* set up destination address in NIC mem */
+ outb(sc->nic_addr + ED_P0_RSAR0, dst);
+ outb(sc->nic_addr + ED_P0_RSAR1, dst>>8);
+
+ /* set remote DMA write */
+ outb(sc->nic_addr + ED_P0_CR, ED_CR_RD1 | ED_CR_STA);
+
+ mb_offset = 0;
+ /*
+ * Transfer the mbuf chain to the NIC memory.
+ * The following code isn't too pretty. The problem is that we can only
+ * transfer words to the board, and if an mbuf has an odd number
+ * of bytes in it, this is a problem. It's not a simple matter of
+ * just removing a byte from the next mbuf (adjusting data++ and
+ * len--) because this will hose-over the mbuf chain which might
+ * be needed later for BPF. Instead, we maintain an offset
+ * (mb_offset) which let's us skip over the first byte in the
+ * following mbuf.
+ */
+ while (m) {
+ if (m->m_len - mb_offset) {
+ if (sc->isa16bit) {
+ if ((m->m_len - mb_offset) > 1)
+ outsw(sc->asic_addr + ED_NOVELL_DATA,
+ mtod(m, caddr_t) + mb_offset,
+ (m->m_len - mb_offset) / 2);
+
+ /*
+ * if odd number of bytes, get the odd byte from
+ * the next mbuf with data
+ */
+ if ((m->m_len - mb_offset) & 1) {
+ /* first the last byte in current mbuf */
+ residual[0] = *(mtod(m, caddr_t) +
+ m->m_len - 1);
+
+ /* advance past any empty mbufs */
+ while (m->m_next && (m->m_next->m_len == 0))
+ m = m->m_next;
+
+ if (m->m_next) {
+ /* remove first byte in next mbuf */
+ residual[1] = *(mtod(m->m_next, caddr_t));
+ mb_offset = 1;
+ }
+
+ outw(sc->asic_addr + ED_NOVELL_DATA,
+ *((unsigned short *) residual));
+ } else
+ mb_offset = 0;
+ } else
+ outsb(sc->asic_addr + ED_NOVELL_DATA, m->m_data, m->m_len);
+
+ }
+ m = m->m_next;
+ }
+
+ /*
+ * Wait for remote DMA complete. This is necessary because on the
+ * transmit side, data is handled internally by the NIC in bursts
+ * and we can't start another remote DMA until this one completes.
+ * Not waiting causes really bad things to happen - like the NIC
+ * irrecoverably jamming the ISA bus.
+ */
+ while (((inb(sc->nic_addr + ED_P0_ISR) & ED_ISR_RDC) != ED_ISR_RDC) && --maxwait);
+
+ if (!maxwait) {
+ log(LOG_WARNING, "ed%d: remote transmit DMA failed to complete\n",
+ sc->arpcom.ac_if.if_unit);
+ ed_reset(sc->arpcom.ac_if.if_unit, 0);
+ }
+
+ return(len);
+}
+