/sys/dev/en/midway.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 3364 lines · 2167 code · 444 blank · 753 comment · 438 complexity · 5a129879e1fd7ab72c5b7edf4a6f7794 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* $NetBSD: midway.c,v 1.30 1997/09/29 17:40:38 chuck Exp $ */
  2. /* (sync'd to midway.c 1.68) */
  3. /*-
  4. * Copyright (c) 1996 Charles D. Cranor and Washington University.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by Charles D. Cranor and
  18. * Washington University.
  19. * 4. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. /*
  36. *
  37. * m i d w a y . c e n i 1 5 5 d r i v e r
  38. *
  39. * author: Chuck Cranor <chuck@ccrc.wustl.edu>
  40. * started: spring, 1996 (written from scratch).
  41. *
  42. * notes from the author:
  43. * Extra special thanks go to Werner Almesberger, EPFL LRC. Werner's
  44. * ENI driver was especially useful in figuring out how this card works.
  45. * I would also like to thank Werner for promptly answering email and being
  46. * generally helpful.
  47. */
  48. #define EN_DIAG
  49. #define EN_DDBHOOK 1 /* compile in ddb functions */
  50. /*
  51. * Note on EN_ENIDMAFIX: the byte aligner on the ENI version of the card
  52. * appears to be broken. it works just fine if there is no load... however
  53. * when the card is loaded the data get corrupted. to see this, one only
  54. * has to use "telnet" over ATM. do the following command in "telnet":
  55. * cat /usr/share/misc/termcap
  56. * "telnet" seems to generate lots of 1023 byte mbufs (which make great
  57. * use of the byte aligner). watch "netstat -s" for checksum errors.
  58. *
  59. * I further tested this by adding a function that compared the transmit
  60. * data on the card's SRAM with the data in the mbuf chain _after_ the
  61. * "transmit DMA complete" interrupt. using the "telnet" test I got data
  62. * mismatches where the byte-aligned data should have been. using ddb
  63. * and en_dumpmem() I verified that the DTQs fed into the card were
  64. * absolutely correct. thus, we are forced to concluded that the ENI
  65. * hardware is buggy. note that the Adaptec version of the card works
  66. * just fine with byte DMA.
  67. *
  68. * bottom line: we set EN_ENIDMAFIX to 1 to avoid byte DMAs on the ENI
  69. * card.
  70. */
  71. #if defined(DIAGNOSTIC) && !defined(EN_DIAG)
  72. #define EN_DIAG /* link in with master DIAG option */
  73. #endif
  74. #define EN_COUNT(X) (X)++
  75. #ifdef EN_DEBUG
  76. #undef EN_DDBHOOK
  77. #define EN_DDBHOOK 1
  78. /*
  79. * This macro removes almost all the EN_DEBUG conditionals in the code that make
  80. * to code a good deal less readable.
  81. */
  82. #define DBG(SC, FL, PRINT) do { \
  83. if ((SC)->debug & DBG_##FL) { \
  84. device_printf((SC)->dev, "%s: "#FL": ", __func__); \
  85. printf PRINT; \
  86. printf("\n"); \
  87. } \
  88. } while (0)
  89. enum {
  90. DBG_INIT = 0x0001, /* debug attach/detach */
  91. DBG_TX = 0x0002, /* debug transmitting */
  92. DBG_SERV = 0x0004, /* debug service interrupts */
  93. DBG_IOCTL = 0x0008, /* debug ioctls */
  94. DBG_VC = 0x0010, /* debug VC handling */
  95. DBG_INTR = 0x0020, /* debug interrupts */
  96. DBG_DMA = 0x0040, /* debug DMA probing */
  97. DBG_IPACKETS = 0x0080, /* print input packets */
  98. DBG_REG = 0x0100, /* print all register access */
  99. DBG_LOCK = 0x0200, /* debug locking */
  100. };
  101. #else /* EN_DEBUG */
  102. #define DBG(SC, FL, PRINT) do { } while (0)
  103. #endif /* EN_DEBUG */
  104. #include "opt_inet.h"
  105. #include "opt_natm.h"
  106. #include "opt_ddb.h"
  107. #ifdef DDB
  108. #undef EN_DDBHOOK
  109. #define EN_DDBHOOK 1
  110. #endif
  111. #include <sys/param.h>
  112. #include <sys/systm.h>
  113. #include <sys/queue.h>
  114. #include <sys/sockio.h>
  115. #include <sys/socket.h>
  116. #include <sys/mbuf.h>
  117. #include <sys/endian.h>
  118. #include <sys/stdint.h>
  119. #include <sys/lock.h>
  120. #include <sys/mutex.h>
  121. #include <sys/condvar.h>
  122. #include <vm/uma.h>
  123. #include <net/if.h>
  124. #include <net/if_media.h>
  125. #include <net/if_atm.h>
  126. #if defined(NATM) || defined(INET) || defined(INET6)
  127. #include <netinet/in.h>
  128. #if defined(INET) || defined(INET6)
  129. #include <netinet/if_atm.h>
  130. #endif
  131. #endif
  132. #ifdef NATM
  133. #include <netnatm/natm.h>
  134. #endif
  135. #include <sys/bus.h>
  136. #include <machine/bus.h>
  137. #include <sys/rman.h>
  138. #include <sys/module.h>
  139. #include <sys/sysctl.h>
  140. #include <sys/malloc.h>
  141. #include <machine/resource.h>
  142. #include <dev/utopia/utopia.h>
  143. #include <dev/en/midwayreg.h>
  144. #include <dev/en/midwayvar.h>
  145. #include <net/bpf.h>
  146. /*
  147. * params
  148. */
  149. #ifndef EN_TXHIWAT
  150. #define EN_TXHIWAT (64 * 1024) /* max 64 KB waiting to be DMAd out */
  151. #endif
  152. SYSCTL_DECL(_hw_atm);
  153. /*
  154. * dma tables
  155. *
  156. * The plan is indexed by the number of words to transfer.
  157. * The maximum index is 15 for 60 words.
  158. */
  159. struct en_dmatab {
  160. uint8_t bcode; /* code */
  161. uint8_t divshift; /* byte divisor */
  162. };
  163. static const struct en_dmatab en_dmaplan[] = {
  164. { 0, 0 }, /* 0 */ { MIDDMA_WORD, 2}, /* 1 */
  165. { MIDDMA_2WORD, 3}, /* 2 */ { MIDDMA_WORD, 2}, /* 3 */
  166. { MIDDMA_4WORD, 4}, /* 4 */ { MIDDMA_WORD, 2}, /* 5 */
  167. { MIDDMA_2WORD, 3}, /* 6 */ { MIDDMA_WORD, 2}, /* 7 */
  168. { MIDDMA_8WORD, 5}, /* 8 */ { MIDDMA_WORD, 2}, /* 9 */
  169. { MIDDMA_2WORD, 3}, /* 10 */ { MIDDMA_WORD, 2}, /* 11 */
  170. { MIDDMA_4WORD, 4}, /* 12 */ { MIDDMA_WORD, 2}, /* 13 */
  171. { MIDDMA_2WORD, 3}, /* 14 */ { MIDDMA_WORD, 2}, /* 15 */
  172. { MIDDMA_16WORD,6}, /* 16 */
  173. };
  174. /*
  175. * prototypes
  176. */
  177. #ifdef EN_DDBHOOK
  178. int en_dump(int unit, int level);
  179. int en_dumpmem(int,int,int);
  180. #endif
  181. static void en_close_finish(struct en_softc *sc, struct en_vcc *vc);
  182. #define EN_LOCK(SC) do { \
  183. DBG(SC, LOCK, ("ENLOCK %d\n", __LINE__)); \
  184. mtx_lock(&sc->en_mtx); \
  185. } while (0)
  186. #define EN_UNLOCK(SC) do { \
  187. DBG(SC, LOCK, ("ENUNLOCK %d\n", __LINE__)); \
  188. mtx_unlock(&sc->en_mtx); \
  189. } while (0)
  190. #define EN_CHECKLOCK(sc) mtx_assert(&sc->en_mtx, MA_OWNED)
  191. /*
  192. * While a transmit mbuf is waiting to get transmit DMA resources we
  193. * need to keep some information with it. We don't want to allocate
  194. * additional memory for this so we stuff it into free fields in the
  195. * mbuf packet header. Neither the checksum fields nor the rcvif field are used
  196. * so use these.
  197. */
  198. #define TX_AAL5 0x1 /* transmit AAL5 PDU */
  199. #define TX_HAS_TBD 0x2 /* TBD did fit into mbuf */
  200. #define TX_HAS_PAD 0x4 /* padding did fit into mbuf */
  201. #define TX_HAS_PDU 0x8 /* PDU trailer did fit into mbuf */
  202. #define MBUF_SET_TX(M, VCI, FLAGS, DATALEN, PAD, MAP) do { \
  203. (M)->m_pkthdr.csum_data = (VCI) | ((FLAGS) << MID_VCI_BITS); \
  204. (M)->m_pkthdr.csum_flags = ((DATALEN) & 0xffff) | \
  205. ((PAD & 0x3f) << 16); \
  206. (M)->m_pkthdr.rcvif = (void *)(MAP); \
  207. } while (0)
  208. #define MBUF_GET_TX(M, VCI, FLAGS, DATALEN, PAD, MAP) do { \
  209. (VCI) = (M)->m_pkthdr.csum_data & ((1 << MID_VCI_BITS) - 1); \
  210. (FLAGS) = ((M)->m_pkthdr.csum_data >> MID_VCI_BITS) & 0xf; \
  211. (DATALEN) = (M)->m_pkthdr.csum_flags & 0xffff; \
  212. (PAD) = ((M)->m_pkthdr.csum_flags >> 16) & 0x3f; \
  213. (MAP) = (void *)((M)->m_pkthdr.rcvif); \
  214. } while (0)
  215. #define EN_WRAPADD(START, STOP, CUR, VAL) do { \
  216. (CUR) = (CUR) + (VAL); \
  217. if ((CUR) >= (STOP)) \
  218. (CUR) = (START) + ((CUR) - (STOP)); \
  219. } while (0)
  220. #define WORD_IDX(START, X) (((X) - (START)) / sizeof(uint32_t))
  221. #define SETQ_END(SC, VAL) ((SC)->is_adaptec ? \
  222. ((VAL) | (MID_DMA_END >> 4)) : \
  223. ((VAL) | (MID_DMA_END)))
  224. /*
  225. * The dtq and drq members are set for each END entry in the corresponding
  226. * card queue entry. It is used to find out, when a buffer has been
  227. * finished DMAing and can be freed.
  228. *
  229. * We store sc->dtq and sc->drq data in the following format...
  230. * the 0x80000 ensures we != 0
  231. */
  232. #define EN_DQ_MK(SLOT, LEN) (((SLOT) << 20) | (LEN) | (0x80000))
  233. #define EN_DQ_SLOT(X) ((X) >> 20)
  234. #define EN_DQ_LEN(X) ((X) & 0x3ffff)
  235. /*
  236. * Variables
  237. */
  238. static uma_zone_t en_vcc_zone;
  239. /***********************************************************************/
  240. /*
  241. * en_read{x}: read a word from the card. These are the only functions
  242. * that read from the card.
  243. */
  244. static __inline uint32_t
  245. en_readx(struct en_softc *sc, uint32_t r)
  246. {
  247. uint32_t v;
  248. #ifdef EN_DIAG
  249. if (r > MID_MAXOFF || (r % 4))
  250. panic("en_read out of range, r=0x%x", r);
  251. #endif
  252. v = bus_space_read_4(sc->en_memt, sc->en_base, r);
  253. return (v);
  254. }
  255. static __inline uint32_t
  256. en_read(struct en_softc *sc, uint32_t r)
  257. {
  258. uint32_t v;
  259. #ifdef EN_DIAG
  260. if (r > MID_MAXOFF || (r % 4))
  261. panic("en_read out of range, r=0x%x", r);
  262. #endif
  263. v = bus_space_read_4(sc->en_memt, sc->en_base, r);
  264. DBG(sc, REG, ("en_read(%#x) -> %08x", r, v));
  265. return (v);
  266. }
  267. /*
  268. * en_write: write a word to the card. This is the only function that
  269. * writes to the card.
  270. */
  271. static __inline void
  272. en_write(struct en_softc *sc, uint32_t r, uint32_t v)
  273. {
  274. #ifdef EN_DIAG
  275. if (r > MID_MAXOFF || (r % 4))
  276. panic("en_write out of range, r=0x%x", r);
  277. #endif
  278. DBG(sc, REG, ("en_write(%#x) <- %08x", r, v));
  279. bus_space_write_4(sc->en_memt, sc->en_base, r, v);
  280. }
  281. /*
  282. * en_k2sz: convert KBytes to a size parameter (a log2)
  283. */
  284. static __inline int
  285. en_k2sz(int k)
  286. {
  287. switch(k) {
  288. case 1: return (0);
  289. case 2: return (1);
  290. case 4: return (2);
  291. case 8: return (3);
  292. case 16: return (4);
  293. case 32: return (5);
  294. case 64: return (6);
  295. case 128: return (7);
  296. default:
  297. panic("en_k2sz");
  298. }
  299. return (0);
  300. }
  301. #define en_log2(X) en_k2sz(X)
  302. /*
  303. * en_b2sz: convert a DMA burst code to its byte size
  304. */
  305. static __inline int
  306. en_b2sz(int b)
  307. {
  308. switch (b) {
  309. case MIDDMA_WORD: return (1*4);
  310. case MIDDMA_2WMAYBE:
  311. case MIDDMA_2WORD: return (2*4);
  312. case MIDDMA_4WMAYBE:
  313. case MIDDMA_4WORD: return (4*4);
  314. case MIDDMA_8WMAYBE:
  315. case MIDDMA_8WORD: return (8*4);
  316. case MIDDMA_16WMAYBE:
  317. case MIDDMA_16WORD: return (16*4);
  318. default:
  319. panic("en_b2sz");
  320. }
  321. return (0);
  322. }
  323. /*
  324. * en_sz2b: convert a burst size (bytes) to DMA burst code
  325. */
  326. static __inline int
  327. en_sz2b(int sz)
  328. {
  329. switch (sz) {
  330. case 1*4: return (MIDDMA_WORD);
  331. case 2*4: return (MIDDMA_2WORD);
  332. case 4*4: return (MIDDMA_4WORD);
  333. case 8*4: return (MIDDMA_8WORD);
  334. case 16*4: return (MIDDMA_16WORD);
  335. default:
  336. panic("en_sz2b");
  337. }
  338. return(0);
  339. }
  340. #ifdef EN_DEBUG
  341. /*
  342. * Dump a packet
  343. */
  344. static void
  345. en_dump_packet(struct en_softc *sc, struct mbuf *m)
  346. {
  347. int plen = m->m_pkthdr.len;
  348. u_int pos = 0;
  349. u_int totlen = 0;
  350. int len;
  351. u_char *ptr;
  352. device_printf(sc->dev, "packet len=%d", plen);
  353. while (m != NULL) {
  354. totlen += m->m_len;
  355. ptr = mtod(m, u_char *);
  356. for (len = 0; len < m->m_len; len++, pos++, ptr++) {
  357. if (pos % 16 == 8)
  358. printf(" ");
  359. if (pos % 16 == 0)
  360. printf("\n");
  361. printf(" %02x", *ptr);
  362. }
  363. m = m->m_next;
  364. }
  365. printf("\n");
  366. if (totlen != plen)
  367. printf("sum of m_len=%u\n", totlen);
  368. }
  369. #endif
  370. /*********************************************************************/
  371. /*
  372. * DMA maps
  373. */
  374. /*
  375. * Map constructor for a MAP.
  376. *
  377. * This is called each time when a map is allocated
  378. * from the pool and about to be returned to the user. Here we actually
  379. * allocate the map if there isn't one. The problem is that we may fail
  380. * to allocate the DMA map yet have no means to signal this error. Therefor
  381. * when allocating a map, the call must check that there is a map. An
  382. * additional problem is, that i386 maps will be NULL, yet are ok and must
  383. * be freed so let's use a flag to signal allocation.
  384. *
  385. * Caveat: we have no way to know that we are called from an interrupt context
  386. * here. We rely on the fact, that bus_dmamap_create uses M_NOWAIT in all
  387. * its allocations.
  388. *
  389. * LOCK: any, not needed
  390. */
  391. static int
  392. en_map_ctor(void *mem, int size, void *arg, int flags)
  393. {
  394. struct en_softc *sc = arg;
  395. struct en_map *map = mem;
  396. int err;
  397. err = bus_dmamap_create(sc->txtag, 0, &map->map);
  398. if (err != 0) {
  399. device_printf(sc->dev, "cannot create DMA map %d\n", err);
  400. return (err);
  401. }
  402. map->flags = ENMAP_ALLOC;
  403. map->sc = sc;
  404. return (0);
  405. }
  406. /*
  407. * Map destructor.
  408. *
  409. * Called when a map is disposed into the zone. If the map is loaded, unload
  410. * it.
  411. *
  412. * LOCK: any, not needed
  413. */
  414. static void
  415. en_map_dtor(void *mem, int size, void *arg)
  416. {
  417. struct en_map *map = mem;
  418. if (map->flags & ENMAP_LOADED) {
  419. bus_dmamap_unload(map->sc->txtag, map->map);
  420. map->flags &= ~ENMAP_LOADED;
  421. }
  422. }
  423. /*
  424. * Map finializer.
  425. *
  426. * This is called each time a map is returned from the zone to the system.
  427. * Get rid of the dmamap here.
  428. *
  429. * LOCK: any, not needed
  430. */
  431. static void
  432. en_map_fini(void *mem, int size)
  433. {
  434. struct en_map *map = mem;
  435. bus_dmamap_destroy(map->sc->txtag, map->map);
  436. }
  437. /*********************************************************************/
  438. /*
  439. * Transmission
  440. */
  441. /*
  442. * Argument structure to load a transmit DMA map
  443. */
  444. struct txarg {
  445. struct en_softc *sc;
  446. struct mbuf *m;
  447. u_int vci;
  448. u_int chan; /* transmit channel */
  449. u_int datalen; /* length of user data */
  450. u_int flags;
  451. u_int wait; /* return: out of resources */
  452. };
  453. /*
  454. * TX DMA map loader helper. This function is the callback when the map
  455. * is loaded. It should fill the DMA segment descriptors into the hardware.
  456. *
  457. * LOCK: locked, needed
  458. */
  459. static void
  460. en_txdma_load(void *uarg, bus_dma_segment_t *segs, int nseg, bus_size_t mapsize,
  461. int error)
  462. {
  463. struct txarg *tx = uarg;
  464. struct en_softc *sc = tx->sc;
  465. struct en_txslot *slot = &sc->txslot[tx->chan];
  466. uint32_t cur; /* on-card buffer position (bytes offset) */
  467. uint32_t dtq; /* on-card queue position (byte offset) */
  468. uint32_t last_dtq; /* last DTQ we have written */
  469. uint32_t tmp;
  470. u_int free; /* free queue entries on card */
  471. u_int needalign, cnt;
  472. bus_size_t rest; /* remaining bytes in current segment */
  473. bus_addr_t addr;
  474. bus_dma_segment_t *s;
  475. uint32_t count, bcode;
  476. int i;
  477. if (error != 0)
  478. return;
  479. cur = slot->cur;
  480. dtq = sc->dtq_us;
  481. free = sc->dtq_free;
  482. last_dtq = 0; /* make gcc happy */
  483. /*
  484. * Local macro to add an entry to the transmit DMA area. If there
  485. * are no entries left, return. Save the byte offset of the entry
  486. * in last_dtq for later use.
  487. */
  488. #define PUT_DTQ_ENTRY(ENI, BCODE, COUNT, ADDR) \
  489. if (free == 0) { \
  490. EN_COUNT(sc->stats.txdtqout); \
  491. tx->wait = 1; \
  492. return; \
  493. } \
  494. last_dtq = dtq; \
  495. en_write(sc, dtq + 0, (ENI || !sc->is_adaptec) ? \
  496. MID_MK_TXQ_ENI(COUNT, tx->chan, 0, BCODE) : \
  497. MID_MK_TXQ_ADP(COUNT, tx->chan, 0, BCODE)); \
  498. en_write(sc, dtq + 4, ADDR); \
  499. \
  500. EN_WRAPADD(MID_DTQOFF, MID_DTQEND, dtq, 8); \
  501. free--;
  502. /*
  503. * Local macro to generate a DMA entry to DMA cnt bytes. Updates
  504. * the current buffer byte offset accordingly.
  505. */
  506. #define DO_DTQ(TYPE) do { \
  507. rest -= cnt; \
  508. EN_WRAPADD(slot->start, slot->stop, cur, cnt); \
  509. DBG(sc, TX, ("tx%d: "TYPE" %u bytes, %ju left, cur %#x", \
  510. tx->chan, cnt, (uintmax_t)rest, cur)); \
  511. \
  512. PUT_DTQ_ENTRY(1, bcode, count, addr); \
  513. \
  514. addr += cnt; \
  515. } while (0)
  516. if (!(tx->flags & TX_HAS_TBD)) {
  517. /*
  518. * Prepend the TBD - it did not fit into the first mbuf
  519. */
  520. tmp = MID_TBD_MK1((tx->flags & TX_AAL5) ?
  521. MID_TBD_AAL5 : MID_TBD_NOAAL5,
  522. sc->vccs[tx->vci]->txspeed,
  523. tx->m->m_pkthdr.len / MID_ATMDATASZ);
  524. en_write(sc, cur, tmp);
  525. EN_WRAPADD(slot->start, slot->stop, cur, 4);
  526. tmp = MID_TBD_MK2(tx->vci, 0, 0);
  527. en_write(sc, cur, tmp);
  528. EN_WRAPADD(slot->start, slot->stop, cur, 4);
  529. /* update DMA address */
  530. PUT_DTQ_ENTRY(0, MIDDMA_JK, WORD_IDX(slot->start, cur), 0);
  531. }
  532. for (i = 0, s = segs; i < nseg; i++, s++) {
  533. rest = s->ds_len;
  534. addr = s->ds_addr;
  535. if (sc->is_adaptec) {
  536. /* adaptec card - simple */
  537. /* advance the on-card buffer pointer */
  538. EN_WRAPADD(slot->start, slot->stop, cur, rest);
  539. DBG(sc, TX, ("tx%d: adp %ju bytes %#jx (cur now 0x%x)",
  540. tx->chan, (uintmax_t)rest, (uintmax_t)addr, cur));
  541. PUT_DTQ_ENTRY(0, 0, rest, addr);
  542. continue;
  543. }
  544. /*
  545. * do we need to do a DMA op to align to the maximum
  546. * burst? Note, that we are alway 32-bit aligned.
  547. */
  548. if (sc->alburst &&
  549. (needalign = (addr & sc->bestburstmask)) != 0) {
  550. /* compute number of bytes, words and code */
  551. cnt = sc->bestburstlen - needalign;
  552. if (cnt > rest)
  553. cnt = rest;
  554. count = cnt / sizeof(uint32_t);
  555. if (sc->noalbursts) {
  556. bcode = MIDDMA_WORD;
  557. } else {
  558. bcode = en_dmaplan[count].bcode;
  559. count = cnt >> en_dmaplan[count].divshift;
  560. }
  561. DO_DTQ("al_dma");
  562. }
  563. /* do we need to do a max-sized burst? */
  564. if (rest >= sc->bestburstlen) {
  565. count = rest >> sc->bestburstshift;
  566. cnt = count << sc->bestburstshift;
  567. bcode = sc->bestburstcode;
  568. DO_DTQ("best_dma");
  569. }
  570. /* do we need to do a cleanup burst? */
  571. if (rest != 0) {
  572. cnt = rest;
  573. count = rest / sizeof(uint32_t);
  574. if (sc->noalbursts) {
  575. bcode = MIDDMA_WORD;
  576. } else {
  577. bcode = en_dmaplan[count].bcode;
  578. count = cnt >> en_dmaplan[count].divshift;
  579. }
  580. DO_DTQ("clean_dma");
  581. }
  582. }
  583. KASSERT (tx->flags & TX_HAS_PAD, ("PDU not padded"));
  584. if ((tx->flags & TX_AAL5) && !(tx->flags & TX_HAS_PDU)) {
  585. /*
  586. * Append the AAL5 PDU trailer
  587. */
  588. tmp = MID_PDU_MK1(0, 0, tx->datalen);
  589. en_write(sc, cur, tmp);
  590. EN_WRAPADD(slot->start, slot->stop, cur, 4);
  591. en_write(sc, cur, 0);
  592. EN_WRAPADD(slot->start, slot->stop, cur, 4);
  593. /* update DMA address */
  594. PUT_DTQ_ENTRY(0, MIDDMA_JK, WORD_IDX(slot->start, cur), 0);
  595. }
  596. /* record the end for the interrupt routine */
  597. sc->dtq[MID_DTQ_A2REG(last_dtq)] =
  598. EN_DQ_MK(tx->chan, tx->m->m_pkthdr.len);
  599. /* set the end flag in the last descriptor */
  600. en_write(sc, last_dtq + 0, SETQ_END(sc, en_read(sc, last_dtq + 0)));
  601. #undef PUT_DTQ_ENTRY
  602. #undef DO_DTQ
  603. /* commit */
  604. slot->cur = cur;
  605. sc->dtq_free = free;
  606. sc->dtq_us = dtq;
  607. /* tell card */
  608. en_write(sc, MID_DMA_WRTX, MID_DTQ_A2REG(sc->dtq_us));
  609. }
  610. /*
  611. * en_txdma: start transmit DMA on the given channel, if possible
  612. *
  613. * This is called from two places: when we got new packets from the upper
  614. * layer or when we found that buffer space has freed up during interrupt
  615. * processing.
  616. *
  617. * LOCK: locked, needed
  618. */
  619. static void
  620. en_txdma(struct en_softc *sc, struct en_txslot *slot)
  621. {
  622. struct en_map *map;
  623. struct mbuf *lastm;
  624. struct txarg tx;
  625. u_int pad;
  626. int error;
  627. DBG(sc, TX, ("tx%td: starting ...", slot - sc->txslot));
  628. again:
  629. bzero(&tx, sizeof(tx));
  630. tx.chan = slot - sc->txslot;
  631. tx.sc = sc;
  632. /*
  633. * get an mbuf waiting for DMA
  634. */
  635. _IF_DEQUEUE(&slot->q, tx.m);
  636. if (tx.m == NULL) {
  637. DBG(sc, TX, ("tx%td: ...done!", slot - sc->txslot));
  638. return;
  639. }
  640. MBUF_GET_TX(tx.m, tx.vci, tx.flags, tx.datalen, pad, map);
  641. /*
  642. * note: don't use the entire buffer space. if WRTX becomes equal
  643. * to RDTX, the transmitter stops assuming the buffer is empty! --kjc
  644. */
  645. if (tx.m->m_pkthdr.len >= slot->bfree) {
  646. EN_COUNT(sc->stats.txoutspace);
  647. DBG(sc, TX, ("tx%td: out of transmit space", slot - sc->txslot));
  648. goto waitres;
  649. }
  650. lastm = NULL;
  651. if (!(tx.flags & TX_HAS_PAD)) {
  652. if (pad != 0) {
  653. /* Append the padding buffer */
  654. (void)m_length(tx.m, &lastm);
  655. lastm->m_next = sc->padbuf;
  656. sc->padbuf->m_len = pad;
  657. }
  658. tx.flags |= TX_HAS_PAD;
  659. }
  660. /*
  661. * Try to load that map
  662. */
  663. error = bus_dmamap_load_mbuf(sc->txtag, map->map, tx.m,
  664. en_txdma_load, &tx, BUS_DMA_NOWAIT);
  665. if (lastm != NULL)
  666. lastm->m_next = NULL;
  667. if (error != 0) {
  668. device_printf(sc->dev, "loading TX map failed %d\n",
  669. error);
  670. goto dequeue_drop;
  671. }
  672. map->flags |= ENMAP_LOADED;
  673. if (tx.wait) {
  674. /* probably not enough space */
  675. bus_dmamap_unload(map->sc->txtag, map->map);
  676. map->flags &= ~ENMAP_LOADED;
  677. sc->need_dtqs = 1;
  678. DBG(sc, TX, ("tx%td: out of transmit DTQs", slot - sc->txslot));
  679. goto waitres;
  680. }
  681. EN_COUNT(sc->stats.launch);
  682. sc->ifp->if_opackets++;
  683. sc->vccs[tx.vci]->opackets++;
  684. sc->vccs[tx.vci]->obytes += tx.datalen;
  685. #ifdef ENABLE_BPF
  686. if (bpf_peers_present(sc->ifp->if_bpf)) {
  687. /*
  688. * adjust the top of the mbuf to skip the TBD if present
  689. * before passing the packet to bpf.
  690. * Also remove padding and the PDU trailer. Assume both of
  691. * them to be in the same mbuf. pktlen, m_len and m_data
  692. * are not needed anymore so we can change them.
  693. */
  694. if (tx.flags & TX_HAS_TBD) {
  695. tx.m->m_data += MID_TBD_SIZE;
  696. tx.m->m_len -= MID_TBD_SIZE;
  697. }
  698. tx.m->m_pkthdr.len = m_length(tx.m, &lastm);
  699. if (tx.m->m_pkthdr.len > tx.datalen) {
  700. lastm->m_len -= tx.m->m_pkthdr.len - tx.datalen;
  701. tx.m->m_pkthdr.len = tx.datalen;
  702. }
  703. bpf_mtap(sc->ifp->if_bpf, tx.m);
  704. }
  705. #endif
  706. /*
  707. * do some housekeeping and get the next packet
  708. */
  709. slot->bfree -= tx.m->m_pkthdr.len;
  710. _IF_ENQUEUE(&slot->indma, tx.m);
  711. goto again;
  712. /*
  713. * error handling. This is jumped to when we just want to drop
  714. * the packet. Must be unlocked here.
  715. */
  716. dequeue_drop:
  717. if (map != NULL)
  718. uma_zfree(sc->map_zone, map);
  719. slot->mbsize -= tx.m->m_pkthdr.len;
  720. m_freem(tx.m);
  721. goto again;
  722. waitres:
  723. _IF_PREPEND(&slot->q, tx.m);
  724. }
  725. /*
  726. * Create a copy of a single mbuf. It can have either internal or
  727. * external data, it may have a packet header. External data is really
  728. * copied, so the new buffer is writeable.
  729. *
  730. * LOCK: any, not needed
  731. */
  732. static struct mbuf *
  733. copy_mbuf(struct mbuf *m)
  734. {
  735. struct mbuf *new;
  736. MGET(new, M_WAIT, MT_DATA);
  737. if (m->m_flags & M_PKTHDR) {
  738. M_MOVE_PKTHDR(new, m);
  739. if (m->m_len > MHLEN)
  740. MCLGET(new, M_WAIT);
  741. } else {
  742. if (m->m_len > MLEN)
  743. MCLGET(new, M_WAIT);
  744. }
  745. bcopy(m->m_data, new->m_data, m->m_len);
  746. new->m_len = m->m_len;
  747. new->m_flags &= ~M_RDONLY;
  748. return (new);
  749. }
  750. /*
  751. * This function is called when we have an ENI adapter. It fixes the
  752. * mbuf chain, so that all addresses and lengths are 4 byte aligned.
  753. * The overall length is already padded to multiple of cells plus the
  754. * TBD so this must always succeed. The routine can fail, when it
  755. * needs to copy an mbuf (this may happen if an mbuf is readonly).
  756. *
  757. * We assume here, that aligning the virtual addresses to 4 bytes also
  758. * aligns the physical addresses.
  759. *
  760. * LOCK: locked, needed
  761. */
  762. static struct mbuf *
  763. en_fix_mchain(struct en_softc *sc, struct mbuf *m0, u_int *pad)
  764. {
  765. struct mbuf **prev = &m0;
  766. struct mbuf *m = m0;
  767. struct mbuf *new;
  768. u_char *d;
  769. int off;
  770. while (m != NULL) {
  771. d = mtod(m, u_char *);
  772. if ((off = (uintptr_t)d % sizeof(uint32_t)) != 0) {
  773. EN_COUNT(sc->stats.mfixaddr);
  774. if (M_WRITABLE(m)) {
  775. bcopy(d, d - off, m->m_len);
  776. m->m_data -= off;
  777. } else {
  778. if ((new = copy_mbuf(m)) == NULL) {
  779. EN_COUNT(sc->stats.mfixfail);
  780. m_freem(m0);
  781. return (NULL);
  782. }
  783. new->m_next = m_free(m);
  784. *prev = m = new;
  785. }
  786. }
  787. if ((off = m->m_len % sizeof(uint32_t)) != 0) {
  788. EN_COUNT(sc->stats.mfixlen);
  789. if (!M_WRITABLE(m)) {
  790. if ((new = copy_mbuf(m)) == NULL) {
  791. EN_COUNT(sc->stats.mfixfail);
  792. m_freem(m0);
  793. return (NULL);
  794. }
  795. new->m_next = m_free(m);
  796. *prev = m = new;
  797. }
  798. d = mtod(m, u_char *) + m->m_len;
  799. off = 4 - off;
  800. while (off) {
  801. while (m->m_next && m->m_next->m_len == 0)
  802. m->m_next = m_free(m->m_next);
  803. if (m->m_next == NULL) {
  804. *d++ = 0;
  805. KASSERT(*pad > 0, ("no padding space"));
  806. (*pad)--;
  807. } else {
  808. *d++ = *mtod(m->m_next, u_char *);
  809. m->m_next->m_len--;
  810. m->m_next->m_data++;
  811. }
  812. m->m_len++;
  813. off--;
  814. }
  815. }
  816. prev = &m->m_next;
  817. m = m->m_next;
  818. }
  819. return (m0);
  820. }
  821. /*
  822. * en_start: start transmitting the next packet that needs to go out
  823. * if there is one. We take off all packets from the interface's queue and
  824. * put them into the channels queue.
  825. *
  826. * Here we also prepend the transmit packet descriptor and append the padding
  827. * and (for aal5) the PDU trailer. This is different from the original driver:
  828. * we assume, that allocating one or two additional mbufs is actually cheaper
  829. * than all this algorithmic fiddling we would need otherwise.
  830. *
  831. * While the packet is on the channels wait queue we use the csum_* fields
  832. * in the packet header to hold the original datalen, the AAL5 flag and the
  833. * VCI. The packet length field in the header holds the needed buffer space.
  834. * This may actually be more than the length of the current mbuf chain (when
  835. * one or more of TBD, padding and PDU do not fit).
  836. *
  837. * LOCK: unlocked, needed
  838. */
  839. static void
  840. en_start(struct ifnet *ifp)
  841. {
  842. struct en_softc *sc = (struct en_softc *)ifp->if_softc;
  843. struct mbuf *m, *lastm;
  844. struct atm_pseudohdr *ap;
  845. u_int pad; /* 0-bytes to pad at PDU end */
  846. u_int datalen; /* length of user data */
  847. u_int vci; /* the VCI we are transmitting on */
  848. u_int flags;
  849. uint32_t tbd[2];
  850. uint32_t pdu[2];
  851. struct en_vcc *vc;
  852. struct en_map *map;
  853. struct en_txslot *tx;
  854. while (1) {
  855. IF_DEQUEUE(&ifp->if_snd, m);
  856. if (m == NULL)
  857. return;
  858. flags = 0;
  859. ap = mtod(m, struct atm_pseudohdr *);
  860. vci = ATM_PH_VCI(ap);
  861. if (ATM_PH_VPI(ap) != 0 || vci >= MID_N_VC ||
  862. (vc = sc->vccs[vci]) == NULL ||
  863. (vc->vflags & VCC_CLOSE_RX)) {
  864. DBG(sc, TX, ("output vpi=%u, vci=%u -- drop",
  865. ATM_PH_VPI(ap), vci));
  866. m_freem(m);
  867. continue;
  868. }
  869. if (vc->vcc.aal == ATMIO_AAL_5)
  870. flags |= TX_AAL5;
  871. m_adj(m, sizeof(struct atm_pseudohdr));
  872. /*
  873. * (re-)calculate size of packet (in bytes)
  874. */
  875. m->m_pkthdr.len = datalen = m_length(m, &lastm);
  876. /*
  877. * computing how much padding we need on the end of the mbuf,
  878. * then see if we can put the TBD at the front of the mbuf
  879. * where the link header goes (well behaved protocols will
  880. * reserve room for us). Last, check if room for PDU tail.
  881. */
  882. if (flags & TX_AAL5)
  883. m->m_pkthdr.len += MID_PDU_SIZE;
  884. m->m_pkthdr.len = roundup(m->m_pkthdr.len, MID_ATMDATASZ);
  885. pad = m->m_pkthdr.len - datalen;
  886. if (flags & TX_AAL5)
  887. pad -= MID_PDU_SIZE;
  888. m->m_pkthdr.len += MID_TBD_SIZE;
  889. DBG(sc, TX, ("txvci%d: buflen=%u datalen=%u lead=%d trail=%d",
  890. vci, m->m_pkthdr.len, datalen, (int)M_LEADINGSPACE(m),
  891. (int)M_TRAILINGSPACE(lastm)));
  892. /*
  893. * From here on we need access to sc
  894. */
  895. EN_LOCK(sc);
  896. /*
  897. * Allocate a map. We do this here rather then in en_txdma,
  898. * because en_txdma is also called from the interrupt handler
  899. * and we are going to have a locking problem then. We must
  900. * use NOWAIT here, because the ip_output path holds various
  901. * locks.
  902. */
  903. map = uma_zalloc_arg(sc->map_zone, sc, M_NOWAIT);
  904. if (map == NULL) {
  905. /* drop that packet */
  906. EN_COUNT(sc->stats.txnomap);
  907. EN_UNLOCK(sc);
  908. m_freem(m);
  909. continue;
  910. }
  911. if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
  912. EN_UNLOCK(sc);
  913. uma_zfree(sc->map_zone, map);
  914. m_freem(m);
  915. continue;
  916. }
  917. /*
  918. * Look, whether we can prepend the TBD (8 byte)
  919. */
  920. if (M_WRITABLE(m) && M_LEADINGSPACE(m) >= MID_TBD_SIZE) {
  921. tbd[0] = htobe32(MID_TBD_MK1((flags & TX_AAL5) ?
  922. MID_TBD_AAL5 : MID_TBD_NOAAL5,
  923. vc->txspeed, m->m_pkthdr.len / MID_ATMDATASZ));
  924. tbd[1] = htobe32(MID_TBD_MK2(vci, 0, 0));
  925. m->m_data -= MID_TBD_SIZE;
  926. bcopy(tbd, m->m_data, MID_TBD_SIZE);
  927. m->m_len += MID_TBD_SIZE;
  928. flags |= TX_HAS_TBD;
  929. }
  930. /*
  931. * Check whether the padding fits (must be writeable -
  932. * we pad with zero).
  933. */
  934. if (M_WRITABLE(lastm) && M_TRAILINGSPACE(lastm) >= pad) {
  935. bzero(lastm->m_data + lastm->m_len, pad);
  936. lastm->m_len += pad;
  937. flags |= TX_HAS_PAD;
  938. if ((flags & TX_AAL5) &&
  939. M_TRAILINGSPACE(lastm) > MID_PDU_SIZE) {
  940. pdu[0] = htobe32(MID_PDU_MK1(0, 0, datalen));
  941. pdu[1] = 0;
  942. bcopy(pdu, lastm->m_data + lastm->m_len,
  943. MID_PDU_SIZE);
  944. lastm->m_len += MID_PDU_SIZE;
  945. flags |= TX_HAS_PDU;
  946. }
  947. }
  948. if (!sc->is_adaptec &&
  949. (m = en_fix_mchain(sc, m, &pad)) == NULL) {
  950. EN_UNLOCK(sc);
  951. uma_zfree(sc->map_zone, map);
  952. continue;
  953. }
  954. /*
  955. * get assigned channel (will be zero unless txspeed is set)
  956. */
  957. tx = vc->txslot;
  958. if (m->m_pkthdr.len > EN_TXSZ * 1024) {
  959. DBG(sc, TX, ("tx%td: packet larger than xmit buffer "
  960. "(%d > %d)\n", tx - sc->txslot, m->m_pkthdr.len,
  961. EN_TXSZ * 1024));
  962. EN_UNLOCK(sc);
  963. m_freem(m);
  964. uma_zfree(sc->map_zone, map);
  965. continue;
  966. }
  967. if (tx->mbsize > EN_TXHIWAT) {
  968. EN_COUNT(sc->stats.txmbovr);
  969. DBG(sc, TX, ("tx%td: buffer space shortage",
  970. tx - sc->txslot));
  971. EN_UNLOCK(sc);
  972. m_freem(m);
  973. uma_zfree(sc->map_zone, map);
  974. continue;
  975. }
  976. /* commit */
  977. tx->mbsize += m->m_pkthdr.len;
  978. DBG(sc, TX, ("tx%td: VCI=%d, speed=0x%x, buflen=%d, mbsize=%d",
  979. tx - sc->txslot, vci, sc->vccs[vci]->txspeed,
  980. m->m_pkthdr.len, tx->mbsize));
  981. MBUF_SET_TX(m, vci, flags, datalen, pad, map);
  982. _IF_ENQUEUE(&tx->q, m);
  983. en_txdma(sc, tx);
  984. EN_UNLOCK(sc);
  985. }
  986. }
  987. /*********************************************************************/
  988. /*
  989. * VCs
  990. */
  991. /*
  992. * en_loadvc: load a vc tab entry from a slot
  993. *
  994. * LOCK: locked, needed
  995. */
  996. static void
  997. en_loadvc(struct en_softc *sc, struct en_vcc *vc)
  998. {
  999. uint32_t reg = en_read(sc, MID_VC(vc->vcc.vci));
  1000. reg = MIDV_SETMODE(reg, MIDV_TRASH);
  1001. en_write(sc, MID_VC(vc->vcc.vci), reg);
  1002. DELAY(27);
  1003. /* no need to set CRC */
  1004. /* read pointer = 0, desc. start = 0 */
  1005. en_write(sc, MID_DST_RP(vc->vcc.vci), 0);
  1006. /* write pointer = 0 */
  1007. en_write(sc, MID_WP_ST_CNT(vc->vcc.vci), 0);
  1008. /* set mode, size, loc */
  1009. en_write(sc, MID_VC(vc->vcc.vci), vc->rxslot->mode);
  1010. vc->rxslot->cur = vc->rxslot->start;
  1011. DBG(sc, VC, ("rx%td: assigned to VCI %d", vc->rxslot - sc->rxslot,
  1012. vc->vcc.vci));
  1013. }
  1014. /*
  1015. * Open the given vcc.
  1016. *
  1017. * LOCK: unlocked, needed
  1018. */
  1019. static int
  1020. en_open_vcc(struct en_softc *sc, struct atmio_openvcc *op)
  1021. {
  1022. uint32_t oldmode, newmode;
  1023. struct en_rxslot *slot;
  1024. struct en_vcc *vc;
  1025. int error = 0;
  1026. DBG(sc, IOCTL, ("enable vpi=%d, vci=%d, flags=%#x",
  1027. op->param.vpi, op->param.vci, op->param.flags));
  1028. if (op->param.vpi != 0 || op->param.vci >= MID_N_VC)
  1029. return (EINVAL);
  1030. vc = uma_zalloc(en_vcc_zone, M_NOWAIT | M_ZERO);
  1031. if (vc == NULL)
  1032. return (ENOMEM);
  1033. EN_LOCK(sc);
  1034. if (sc->vccs[op->param.vci] != NULL) {
  1035. error = EBUSY;
  1036. goto done;
  1037. }
  1038. /* find a free receive slot */
  1039. for (slot = sc->rxslot; slot < &sc->rxslot[sc->en_nrx]; slot++)
  1040. if (slot->vcc == NULL)
  1041. break;
  1042. if (slot == &sc->rxslot[sc->en_nrx]) {
  1043. error = ENOSPC;
  1044. goto done;
  1045. }
  1046. vc->rxslot = slot;
  1047. vc->rxhand = op->rxhand;
  1048. vc->vcc = op->param;
  1049. oldmode = slot->mode;
  1050. newmode = (op->param.aal == ATMIO_AAL_5) ? MIDV_AAL5 : MIDV_NOAAL;
  1051. slot->mode = MIDV_SETMODE(oldmode, newmode);
  1052. slot->vcc = vc;
  1053. KASSERT (_IF_QLEN(&slot->indma) == 0 && _IF_QLEN(&slot->q) == 0,
  1054. ("en_rxctl: left over mbufs on enable slot=%td",
  1055. vc->rxslot - sc->rxslot));
  1056. vc->txspeed = 0;
  1057. vc->txslot = sc->txslot;
  1058. vc->txslot->nref++; /* bump reference count */
  1059. en_loadvc(sc, vc); /* does debug printf for us */
  1060. /* don't free below */
  1061. sc->vccs[vc->vcc.vci] = vc;
  1062. vc = NULL;
  1063. sc->vccs_open++;
  1064. done:
  1065. if (vc != NULL)
  1066. uma_zfree(en_vcc_zone, vc);
  1067. EN_UNLOCK(sc);
  1068. return (error);
  1069. }
  1070. /*
  1071. * Close finished
  1072. */
  1073. static void
  1074. en_close_finish(struct en_softc *sc, struct en_vcc *vc)
  1075. {
  1076. if (vc->rxslot != NULL)
  1077. vc->rxslot->vcc = NULL;
  1078. DBG(sc, VC, ("vci: %u free (%p)", vc->vcc.vci, vc));
  1079. sc->vccs[vc->vcc.vci] = NULL;
  1080. uma_zfree(en_vcc_zone, vc);
  1081. sc->vccs_open--;
  1082. }
  1083. /*
  1084. * LOCK: unlocked, needed
  1085. */
  1086. static int
  1087. en_close_vcc(struct en_softc *sc, struct atmio_closevcc *cl)
  1088. {
  1089. uint32_t oldmode, newmode;
  1090. struct en_vcc *vc;
  1091. int error = 0;
  1092. DBG(sc, IOCTL, ("disable vpi=%d, vci=%d", cl->vpi, cl->vci));
  1093. if (cl->vpi != 0 || cl->vci >= MID_N_VC)
  1094. return (EINVAL);
  1095. EN_LOCK(sc);
  1096. if ((vc = sc->vccs[cl->vci]) == NULL) {
  1097. error = ENOTCONN;
  1098. goto done;
  1099. }
  1100. /*
  1101. * turn off VCI
  1102. */
  1103. if (vc->rxslot == NULL) {
  1104. error = ENOTCONN;
  1105. goto done;
  1106. }
  1107. if (vc->vflags & VCC_DRAIN) {
  1108. error = EINVAL;
  1109. goto done;
  1110. }
  1111. oldmode = en_read(sc, MID_VC(cl->vci));
  1112. newmode = MIDV_SETMODE(oldmode, MIDV_TRASH) & ~MIDV_INSERVICE;
  1113. en_write(sc, MID_VC(cl->vci), (newmode | (oldmode & MIDV_INSERVICE)));
  1114. /* halt in tracks, be careful to preserve inservice bit */
  1115. DELAY(27);
  1116. vc->rxslot->mode = newmode;
  1117. vc->txslot->nref--;
  1118. /* if stuff is still going on we are going to have to drain it out */
  1119. if (_IF_QLEN(&vc->rxslot->indma) == 0 &&
  1120. _IF_QLEN(&vc->rxslot->q) == 0 &&
  1121. (vc->vflags & VCC_SWSL) == 0) {
  1122. en_close_finish(sc, vc);
  1123. goto done;
  1124. }
  1125. vc->vflags |= VCC_DRAIN;
  1126. DBG(sc, IOCTL, ("VCI %u now draining", cl->vci));
  1127. if (vc->vcc.flags & ATMIO_FLAG_ASYNC)
  1128. goto done;
  1129. vc->vflags |= VCC_CLOSE_RX;
  1130. while ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) &&
  1131. (vc->vflags & VCC_DRAIN))
  1132. cv_wait(&sc->cv_close, &sc->en_mtx);
  1133. en_close_finish(sc, vc);
  1134. if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  1135. error = EIO;
  1136. goto done;
  1137. }
  1138. done:
  1139. EN_UNLOCK(sc);
  1140. return (error);
  1141. }
  1142. /*********************************************************************/
  1143. /*
  1144. * starting/stopping the card
  1145. */
  1146. /*
  1147. * en_reset_ul: reset the board, throw away work in progress.
  1148. * must en_init to recover.
  1149. *
  1150. * LOCK: locked, needed
  1151. */
  1152. static void
  1153. en_reset_ul(struct en_softc *sc)
  1154. {
  1155. struct en_map *map;
  1156. struct mbuf *m;
  1157. struct en_rxslot *rx;
  1158. int lcv;
  1159. device_printf(sc->dev, "reset\n");
  1160. sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  1161. if (sc->en_busreset)
  1162. sc->en_busreset(sc);
  1163. en_write(sc, MID_RESID, 0x0); /* reset hardware */
  1164. /*
  1165. * recv: dump any mbufs we are dma'ing into, if DRAINing, then a reset
  1166. * will free us! Don't release the rxslot from the channel.
  1167. */
  1168. for (lcv = 0 ; lcv < MID_N_VC ; lcv++) {
  1169. if (sc->vccs[lcv] == NULL)
  1170. continue;
  1171. rx = sc->vccs[lcv]->rxslot;
  1172. for (;;) {
  1173. _IF_DEQUEUE(&rx->indma, m);
  1174. if (m == NULL)
  1175. break;
  1176. map = (void *)m->m_pkthdr.rcvif;
  1177. uma_zfree(sc->map_zone, map);
  1178. m_freem(m);
  1179. }
  1180. for (;;) {
  1181. _IF_DEQUEUE(&rx->q, m);
  1182. if (m == NULL)
  1183. break;
  1184. m_freem(m);
  1185. }
  1186. sc->vccs[lcv]->vflags = 0;
  1187. }
  1188. /*
  1189. * xmit: dump everything
  1190. */
  1191. for (lcv = 0 ; lcv < EN_NTX ; lcv++) {
  1192. for (;;) {
  1193. _IF_DEQUEUE(&sc->txslot[lcv].indma, m);
  1194. if (m == NULL)
  1195. break;
  1196. map = (void *)m->m_pkthdr.rcvif;
  1197. uma_zfree(sc->map_zone, map);
  1198. m_freem(m);
  1199. }
  1200. for (;;) {
  1201. _IF_DEQUEUE(&sc->txslot[lcv].q, m);
  1202. if (m == NULL)
  1203. break;
  1204. map = (void *)m->m_pkthdr.rcvif;
  1205. uma_zfree(sc->map_zone, map);
  1206. m_freem(m);
  1207. }
  1208. sc->txslot[lcv].mbsize = 0;
  1209. }
  1210. /*
  1211. * Unstop all waiters
  1212. */
  1213. cv_broadcast(&sc->cv_close);
  1214. }
  1215. /*
  1216. * en_reset: reset the board, throw away work in progress.
  1217. * must en_init to recover.
  1218. *
  1219. * LOCK: unlocked, needed
  1220. *
  1221. * Use en_reset_ul if you alreay have the lock
  1222. */
  1223. void
  1224. en_reset(struct en_softc *sc)
  1225. {
  1226. EN_LOCK(sc);
  1227. en_reset_ul(sc);
  1228. EN_UNLOCK(sc);
  1229. }
  1230. /*
  1231. * en_init: init board and sync the card with the data in the softc.
  1232. *
  1233. * LOCK: locked, needed
  1234. */
  1235. static void
  1236. en_init(struct en_softc *sc)
  1237. {
  1238. int vc, slot;
  1239. uint32_t loc;
  1240. if ((sc->ifp->if_flags & IFF_UP) == 0) {
  1241. DBG(sc, INIT, ("going down"));
  1242. en_reset(sc); /* to be safe */
  1243. return;
  1244. }
  1245. DBG(sc, INIT, ("going up"));
  1246. sc->ifp->if_drv_flags |= IFF_DRV_RUNNING; /* enable */
  1247. if (sc->en_busreset)
  1248. sc->en_busreset(sc);
  1249. en_write(sc, MID_RESID, 0x0); /* reset */
  1250. /* zero memory */
  1251. bus_space_set_region_4(sc->en_memt, sc->en_base,
  1252. MID_RAMOFF, 0, sc->en_obmemsz / 4);
  1253. /*
  1254. * init obmem data structures: vc tab, dma q's, slist.
  1255. *
  1256. * note that we set drq_free/dtq_free to one less than the total number
  1257. * of DTQ/DRQs present. we do this because the card uses the condition
  1258. * (drq_chip == drq_us) to mean "list is empty"... but if you allow the
  1259. * circular list to be completely full then (drq_chip == drq_us) [i.e.
  1260. * the drq_us pointer will wrap all the way around]. by restricting
  1261. * the number of active requests to (N - 1) we prevent the list from
  1262. * becoming completely full. note that the card will sometimes give
  1263. * us an interrupt for a DTQ/DRQ we have already processes... this helps
  1264. * keep that interrupt from messing us up.
  1265. */
  1266. bzero(&sc->drq, sizeof(sc->drq));
  1267. sc->drq_free = MID_DRQ_N - 1;
  1268. sc->drq_chip = MID_DRQ_REG2A(en_read(sc, MID_DMA_RDRX));
  1269. en_write(sc, MID_DMA_WRRX, MID_DRQ_A2REG(sc->drq_chip));
  1270. sc->drq_us = sc->drq_chip;
  1271. bzero(&sc->dtq, sizeof(sc->dtq));
  1272. sc->dtq_free = MID_DTQ_N - 1;
  1273. sc->dtq_chip = MID_DTQ_REG2A(en_read(sc, MID_DMA_RDTX));
  1274. en_write(sc, MID_DMA_WRTX, MID_DRQ_A2REG(sc->dtq_chip));
  1275. sc->dtq_us = sc->dtq_chip;
  1276. sc->hwslistp = MID_SL_REG2A(en_read(sc, MID_SERV_WRITE));
  1277. sc->swsl_size = sc->swsl_head = sc->swsl_tail = 0;
  1278. DBG(sc, INIT, ("drq free/chip: %d/0x%x, dtq free/chip: %d/0x%x, "
  1279. "hwslist: 0x%x", sc->drq_free, sc->drq_chip, sc->dtq_free,
  1280. sc->dtq_chip, sc->hwslistp));
  1281. for (slot = 0 ; slot < EN_NTX ; slot++) {
  1282. sc->txslot[slot].bfree = EN_TXSZ * 1024;
  1283. en_write(sc, MIDX_READPTR(slot), 0);
  1284. en_write(sc, MIDX_DESCSTART(slot), 0);
  1285. loc = sc->txslot[slot].cur = sc->txslot[slot].start;
  1286. loc = loc - MID_RAMOFF;
  1287. /* mask, cvt to words */
  1288. loc = (loc & ~((EN_TXSZ * 1024) - 1)) >> 2;
  1289. /* top 11 bits */
  1290. loc = loc >> MIDV_LOCTOPSHFT;
  1291. en_write(sc, MIDX_PLACE(slot), MIDX_MKPLACE(en_k2sz(EN_TXSZ),
  1292. loc));
  1293. DBG(sc, INIT, ("tx%d: place 0x%x", slot,
  1294. (u_int)en_read(sc, MIDX_PLACE(slot))));
  1295. }
  1296. for (vc = 0; vc < MID_N_VC; vc++)
  1297. if (sc->vccs[vc] != NULL)
  1298. en_loadvc(sc, sc->vccs[vc]);
  1299. /*
  1300. * enable!
  1301. */
  1302. en_write(sc, MID_INTENA, MID_INT_TX | MID_INT_DMA_OVR | MID_INT_IDENT |
  1303. MID_INT_LERR | MID_INT_DMA_ERR | MID_INT_DMA_RX | MID_INT_DMA_TX |
  1304. MID_INT_SERVICE | MID_INT_SUNI | MID_INT_STATS);
  1305. en_write(sc, MID_MAST_CSR, MID_SETIPL(sc->ipl) | MID_MCSR_ENDMA |
  1306. MID_MCSR_ENTX | MID_MCSR_ENRX);
  1307. }
  1308. /*********************************************************************/
  1309. /*
  1310. * Ioctls
  1311. */
  1312. /*
  1313. * en_ioctl: handle ioctl requests
  1314. *
  1315. * NOTE: if you add an ioctl to set txspeed, you should choose a new
  1316. * TX channel/slot. Choose the one with the lowest sc->txslot[slot].nref
  1317. * value, subtract one from sc->txslot[0].nref, add one to the
  1318. * sc->txslot[slot].nref, set sc->txvc2slot[vci] = slot, and then set
  1319. * txspeed[vci].
  1320. *
  1321. * LOCK: unlocked, needed
  1322. */
  1323. static int
  1324. en_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  1325. {
  1326. struct en_softc *sc = (struct en_softc *)ifp->if_softc;
  1327. #if defined(INET) || defined(INET6)
  1328. struct ifaddr *ifa = (struct ifaddr *)data;
  1329. #endif
  1330. struct ifreq *ifr = (struct ifreq *)data;
  1331. struct atmio_vcctable *vtab;
  1332. int error = 0;
  1333. switch (cmd) {
  1334. case SIOCSIFADDR:
  1335. EN_LOCK(sc);
  1336. ifp->if_flags |= IFF_UP;
  1337. #if defined(INET) || defined(INET6)
  1338. if (ifa->ifa_addr->sa_family == AF_INET
  1339. || ifa->ifa_addr->sa_family == AF_INET6) {
  1340. if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  1341. en_reset_ul(sc);
  1342. en_init(sc);
  1343. }
  1344. ifa->ifa_rtrequest = atm_rtrequest; /* ??? */
  1345. EN_UNLOCK(sc);
  1346. break;
  1347. }
  1348. #endif /* INET */
  1349. if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  1350. en_reset_ul(sc);
  1351. en_init(sc);
  1352. }
  1353. EN_UNLOCK(sc);
  1354. break;
  1355. case SIOCSIFFLAGS:
  1356. EN_LOCK(sc);
  1357. if (ifp->if_flags & IFF_UP) {
  1358. if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
  1359. en_init(sc);
  1360. } else {
  1361. if (ifp->if_drv_flags & IFF_DRV_RUNNING)
  1362. en_reset_ul(sc);
  1363. }
  1364. EN_UNLOCK(sc);
  1365. break;
  1366. case SIOCSIFMTU:
  1367. /*
  1368. * Set the interface MTU.
  1369. */
  1370. if (ifr->ifr_mtu > ATMMTU) {
  1371. error = EINVAL;
  1372. break;
  1373. }
  1374. ifp->if_mtu = ifr->ifr_mtu;
  1375. break;
  1376. case SIOCSIFMEDIA:
  1377. case SIOCGIFMEDIA:
  1378. error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
  1379. break;
  1380. case SIOCATMOPENVCC: /* kernel internal use */
  1381. error = en_open_vcc(sc, (struct atmio_openvcc *)data);
  1382. break;
  1383. case SIOCATMCLOSEVCC: /* kernel internal use */
  1384. error = en_close_vcc(sc, (struct atmio_closevcc *)data);
  1385. break;
  1386. case SIOCATMGETVCCS: /* internal netgraph use */
  1387. vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
  1388. MID_N_VC, sc->vccs_open, &sc->en_mtx, 0);
  1389. if (vtab == NULL) {
  1390. error = ENOMEM;
  1391. break;
  1392. }
  1393. *(void **)data = vtab;
  1394. break;
  1395. case SIOCATMGVCCS: /* return vcc table */
  1396. vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
  1397. MID_N_VC, sc->vccs_open, &sc->en_mtx, 1);
  1398. error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
  1399. vtab->count * sizeof(vtab->vccs[0]));
  1400. free(vtab, M_DEVBUF);
  1401. break;
  1402. default:
  1403. error = EINVAL;
  1404. break;
  1405. }
  1406. return (error);
  1407. }
  1408. /*********************************************************************/
  1409. /*
  1410. * Sysctl's
  1411. */
  1412. /*
  1413. * Sysctl handler for internal statistics
  1414. *
  1415. * LOCK: unlocked, needed
  1416. */
  1417. static int
  1418. en_sysctl_istats(SYSCTL_HANDLER_ARGS)
  1419. {
  1420. struct en_softc *sc = arg1;
  1421. uint32_t *ret;
  1422. int error;
  1423. ret = malloc(sizeof(sc->stats), M_TEMP, M_WAITOK);
  1424. EN_LOCK(sc);
  1425. bcopy(&sc->stats, ret, sizeof(sc->stats));
  1426. EN_UNLOCK(sc);
  1427. error = SYSCTL_OUT(req, ret, sizeof(sc->stats));
  1428. free(ret, M_TEMP);
  1429. return (error);
  1430. }
  1431. /*********************************************************************/
  1432. /*
  1433. * Interrupts
  1434. */
  1435. /*
  1436. * Transmit interrupt handler
  1437. *
  1438. * check for tx complete, if detected then this means that some space
  1439. * has come free on the card. we must account for it and arrange to
  1440. * kick the channel to life (in case it is stalled waiting on the card).
  1441. *
  1442. * LOCK: locked, needed
  1443. */
  1444. static uint32_t
  1445. en_intr_tx(struct en_softc *sc, uint32_t reg)
  1446. {
  1447. uint32_t kick;
  1448. uint32_t mask;
  1449. uint32_t val;
  1450. int chan;
  1451. kick = 0; /* bitmask of channels to kick */
  1452. for (mask = 1, chan = 0; chan < EN_NTX; chan++, mask *= 2) {
  1453. if (!(reg & MID_TXCHAN(chan)))
  1454. continue;
  1455. kick = kick | mask;
  1456. /* current read pointer */
  1457. val = en_read(sc, MIDX_READPTR(chan));
  1458. /* as offset */
  1459. val = (val * sizeof(uint32_t)) + sc->txslot[chan].start;
  1460. if (val > sc->txslot[chan].cur)
  1461. sc->txslot[chan].bfree = val - sc->txslot[chan].cur;
  1462. else
  1463. sc->txslot[chan].bfree = (val + (EN_TXSZ * 1024)) -
  1464. sc->txslot[chan].cur;
  1465. DBG(sc, INTR, ("tx%d: transmit done. %d bytes now free in "
  1466. "buffer", chan, sc->txslot[chan].bfree));
  1467. }
  1468. return (kick);
  1469. }
  1470. /*
  1471. * TX DMA interrupt
  1472. *
  1473. * check for TX DMA complete, if detected then this means
  1474. * that some DTQs are now free. it also means some indma
  1475. * mbufs can be freed. if we needed DTQs, kick all channels.
  1476. *
  1477. * LOCK: locked, needed
  1478. */
  1479. static uint32_t
  1480. en_intr_tx_dma(struct en_softc *sc)
  1481. {
  1482. uint32_t kick = 0;
  1483. uint32_t val;
  1484. uint32_t idx;
  1485. uint32_t slot;
  1486. uint32_t dtq;
  1487. struct en_map *map;
  1488. struct mbuf *m;
  1489. val = en_read(sc, MID_DMA_RDTX); /* chip's current location */
  1490. idx = MID_DTQ_A2REG(sc->dtq_chip); /* where we last saw chip */
  1491. if (sc->need_dtqs) {
  1492. kick = MID_NTX_CH - 1; /* assume power of 2, kick all! */
  1493. sc->need_dtqs = 0; /* recalculated in "kick" loop below */
  1494. DBG(sc, INTR, ("cleared need DTQ condition"));
  1495. }
  1496. while (idx != val) {
  1497. sc->dtq_free++;
  1498. if ((dtq = sc->dtq[idx]) != 0) {
  1499. /* don't forget to zero it out when done */
  1500. sc->dtq[idx] = 0;
  1501. slot = EN_DQ_SLOT(dtq);
  1502. _IF_DEQUEUE(&sc->txslot[slot].indma, m);
  1503. if (m == NULL)
  1504. panic("enintr: dtqsync");
  1505. map = (void *)m->m_pkthdr.rcvif;
  1506. uma_zfree(sc->map_zone, map);
  1507. m_freem(m);
  1508. sc->txslot[slot].mbsize -= EN_DQ_LEN(dtq);
  1509. DBG(sc, INTR, ("tx%d: free %d dma bytes, mbsize now "
  1510. "%d", slot, EN_DQ_LEN(dtq),
  1511. sc->txslot[slot].mbsize));
  1512. }
  1513. EN_WRAPADD(0, MID_DTQ_N, idx, 1);
  1514. }
  1515. sc->dtq_chip = MID_DTQ_REG2A(val); /* sync softc */
  1516. return (kick);
  1517. }
  1518. /*
  1519. * Service interrupt
  1520. *
  1521. * LOCK: locked, needed
  1522. */
  1523. static int
  1524. en_intr_service(struct en_softc *sc)
  1525. {
  1526. uint32_t chip;
  1527. uint32_t vci;
  1528. int need_softserv = 0;
  1529. struct en_vcc *vc;
  1530. chip = MID_SL_REG2A(en_read(sc, MID_SERV_WRITE));
  1531. while (sc->hwslistp != chip) {
  1532. /* fetch and remove it from hardware service list */
  1533. vci = en_read(sc, sc->hwslistp);
  1534. EN_WRAPADD(MID_SLOFF, MID_SLEND, sc->hwslistp, 4);
  1535. if ((vc = sc->vccs[vci]) == NULL ||
  1536. (vc->vcc.flags & ATMIO_FLAG_NORX)) {
  1537. DBG(sc, INTR, ("unexpected rx interrupt VCI %d", vci));
  1538. en_write(sc, MID_VC(vci), MIDV_TRASH); /* rx off */
  1539. continue;
  1540. }
  1541. /* remove from hwsl */
  1542. en_write(sc, MID_VC(vci), vc->rxslot->mode);
  1543. EN_COUNT(sc->stats.hwpull);
  1544. DBG(sc, INTR, ("pulled VCI %d off hwslist", vci));
  1545. /* add it to the software service list (if needed) */
  1546. if ((vc->vflags & VCC_SWSL) == 0) {
  1547. EN_COUNT(sc->stats.swadd);
  1548. need_softserv = 1;
  1549. vc->vflags |= VCC_SWSL;
  1550. sc->swslist[sc->swsl_tail] = vci;
  1551. EN_WRAPADD(0, MID_SL_N, sc->swsl_tail, 1);
  1552. sc->swsl_size++;
  1553. DBG(sc, INTR, ("added VCI %d to swslist", vci));
  1554. }
  1555. }
  1556. return (need_softserv);
  1557. }
  1558. /*
  1559. * Handle a receive DMA completion
  1560. */
  1561. static void
  1562. en_rx_drain(struct en_softc *sc, u_int drq)
  1563. {
  1564. struct en_rxslot *slot;
  1565. struct en_vcc *vc;
  1566. struct mbuf *m;
  1567. struct atm_pseudohdr ah;
  1568. slot = &sc->rxslot[EN_DQ_SLOT(drq)];
  1569. m = NULL; /* assume "JK" trash DMA */
  1570. if (EN_DQ_LEN(drq) != 0) {
  1571. _IF_DEQUEUE(&slot->indma, m);
  1572. KASSERT(m != NULL, ("drqsync: %s: lost mbuf in slot %td!",
  1573. sc->ifp->if_xname, slot - sc->rxslot));
  1574. uma_zfree(sc->map_zone, (struct en_map *)m->m_pkthdr.rcvif);
  1575. }
  1576. if ((vc = slot->vcc) == NULL) {
  1577. /* ups */
  1578. if (m != NULL)
  1579. m_freem(m);
  1580. return;
  1581. }
  1582. /* do something with this mbuf */
  1583. if (vc->vflags & VCC_DRAIN) {
  1584. /* drain? */
  1585. if (m != NULL)
  1586. m_freem(m);
  1587. if (_IF_QLEN(&slot->indma) == 0 && _IF_QLEN(&slot->q) == 0 &&
  1588. (en_read(sc, MID_VC(vc->vcc.vci)) & MIDV_INSERVICE) == 0 &&
  1589. (vc->vflags & VCC_SWSL) == 0) {
  1590. vc->vflags &= ~VCC_CLOSE_RX;
  1591. if (vc->vcc.flags & ATMIO_FLAG_ASYNC)
  1592. en_close_finish(sc, vc);
  1593. else
  1594. cv_signal(&sc->cv_close);
  1595. }
  1596. return;
  1597. }
  1598. if (m != NULL) {
  1599. ATM_PH_FLAGS(&ah) = vc->vcc.flags;
  1600. ATM_PH_VPI(&ah) = 0;
  1601. ATM_PH_SETVCI(&ah, vc->vcc.vci);
  1602. DBG(sc, INTR, ("rx%td: rxvci%d: atm_input, mbuf %p, len %d, "
  1603. "hand %p", slot - sc->rxslot, vc->vcc.vci, m,
  1604. EN_DQ_LEN(drq), vc->rxhand));
  1605. m->m_pkthdr.rcvif = sc->ifp;
  1606. sc->ifp->if_ipackets++;
  1607. vc->ipackets++;
  1608. vc->ibytes += m->m_pkthdr.len;
  1609. #ifdef EN_DEBUG
  1610. if (sc->debug & DBG_IPACKETS)
  1611. en_dump_packet(sc, m);
  1612. #endif
  1613. #ifdef ENABLE_BPF
  1614. BPF_MTAP(sc->ifp, m);
  1615. #endif
  1616. EN_UNLOCK(sc);
  1617. atm_input(sc->ifp, &ah, m, vc->rxhand);
  1618. EN_LOCK(sc);
  1619. }
  1620. }
  1621. /*
  1622. * check for RX DMA complete, and pass the data "upstairs"
  1623. *
  1624. * LOCK: locked, needed
  1625. */
  1626. static int
  1627. en_intr_rx_dma(struct en_softc *sc)
  1628. {
  1629. uint32_t val;
  1630. uint32_t idx;
  1631. uint32_t drq;
  1632. val = en_read(sc, MID_DMA_RDRX); /* chip's current location */
  1633. idx = MID_DRQ_A2REG(sc->drq_chip); /* where we last saw chip */
  1634. while (idx != val) {
  1635. sc->drq_free++;
  1636. if ((drq = sc->drq[idx]) != 0) {
  1637. /* don't forget to zero it out when done */
  1638. sc->drq[idx] = 0;
  1639. en_rx_drain(sc, drq);
  1640. }
  1641. EN_WRAPADD(0, MID_DRQ_N, idx, 1);
  1642. }
  1643. sc->drq_chip = MID_DRQ_REG2A(val); /* sync softc */
  1644. if (sc->need_drqs) {
  1645. /* true if we had a DRQ shortage */
  1646. sc->need_drqs = 0;
  1647. DBG(sc, INTR, ("cleared need DRQ condition"));
  1648. return (1);
  1649. } else
  1650. return (0);
  1651. }
  1652. /*
  1653. * en_mget: get an mbuf chain that can hold totlen bytes and return it
  1654. * (for recv). For the actual allocation totlen is rounded up to a multiple
  1655. * of 4. We also ensure, that each mbuf has a multiple of 4 bytes.
  1656. *
  1657. * After this call the sum of all the m_len's in the chain will be totlen.
  1658. * This is called at interrupt time, so we can't wait here.
  1659. *
  1660. * LOCK: any, not needed
  1661. */
  1662. static struct mbuf *
  1663. en_mget(struct en_softc *sc, u_int pktlen)
  1664. {
  1665. struct mbuf *m, *tmp;
  1666. u_int totlen, pad;
  1667. totlen = roundup(pktlen, sizeof(uint32_t));
  1668. pad = totlen - pktlen;
  1669. /*
  1670. * First get an mbuf with header. Keep space for a couple of
  1671. * words at the begin.
  1672. */
  1673. /* called from interrupt context */
  1674. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1675. if (m == NULL)
  1676. return (NULL);
  1677. m->m_pkthdr.rcvif = NULL;
  1678. m->m_pkthdr.len = pktlen;
  1679. m->m_len = EN_RX1BUF;
  1680. MH_ALIGN(m, EN_RX1BUF);
  1681. if (m->m_len >= totlen) {
  1682. m->m_len = totlen;
  1683. } else {
  1684. totlen -= m->m_len;
  1685. /* called from interrupt context */
  1686. tmp = m_getm(m, totlen, M_DONTWAIT, MT_DATA);
  1687. if (tmp == NULL) {
  1688. m_free(m);
  1689. return (NULL);
  1690. }
  1691. tmp = m->m_next;
  1692. /* m_getm could do this for us */
  1693. while (tmp != NULL) {
  1694. tmp->m_len = min(MCLBYTES, totlen);
  1695. totlen -= tmp->m_len;
  1696. tmp = tmp->m_next;
  1697. }
  1698. }
  1699. return (m);
  1700. }
  1701. /*
  1702. * Argument for RX DMAMAP loader.
  1703. */
  1704. struct rxarg {
  1705. struct en_softc *sc;
  1706. struct mbuf *m;
  1707. u_int pre_skip; /* number of bytes to skip at begin */
  1708. u_int post_skip; /* number of bytes to skip at end */
  1709. struct en_vcc *vc; /* vc we are receiving on */
  1710. int wait; /* wait for DRQ entries */
  1711. };
  1712. /*
  1713. * Copy the segment table to the buffer for later use. And compute the
  1714. * number of dma queue entries we need.
  1715. *
  1716. * LOCK: locked, needed
  1717. */
  1718. static void
  1719. en_rxdma_load(void *uarg, bus_dma_segment_t *segs, int nseg,
  1720. bus_size_t mapsize, int error)
  1721. {
  1722. struct rxarg *rx = uarg;
  1723. struct en_softc *sc = rx->sc;
  1724. struct en_rxslot *slot = rx->vc->rxslot;
  1725. u_int free; /* number of free DRQ entries */
  1726. uint32_t cur; /* current buffer offset */
  1727. uint32_t drq; /* DRQ entry pointer */
  1728. uint32_t last_drq; /* where we have written last */
  1729. u_int needalign, cnt, count, bcode;
  1730. bus_addr_t addr;
  1731. bus_size_t rest;
  1732. int i;
  1733. if (error != 0)
  1734. return;
  1735. if (nseg > EN_MAX_DMASEG)
  1736. panic("too many DMA segments");
  1737. rx->wait = 0;
  1738. free = sc->drq_free;
  1739. drq = sc->drq_us;
  1740. cur = slot->cur;
  1741. last_drq = 0;
  1742. /*
  1743. * Local macro to add an entry to the receive DMA area. If there
  1744. * are no entries left, return. Save the byte offset of the entry
  1745. * in last_drq for later use.
  1746. */
  1747. #define PUT