From 3ad82a03a474ea58539066268f2e267030c8e7d9 Mon Sep 17 00:00:00 2001 From: David Gwynne Date: Wed, 20 Aug 2014 01:00:16 +0000 Subject: replace the custom jumbo allocator with MCLGETI. putting this in the tree to make it easier for people to test. --- sys/dev/pci/if_lge.c | 171 +------------------------------------------- sys/dev/pci/if_lgereg.h | 12 +--- sys/dev/pci/if_nge.c | 186 +----------------------------------------------- sys/dev/pci/if_ngereg.h | 17 +---- 4 files changed, 8 insertions(+), 378 deletions(-) (limited to 'sys') diff --git a/sys/dev/pci/if_lge.c b/sys/dev/pci/if_lge.c index ea655e0eacc..164e367f9e6 100644 --- a/sys/dev/pci/if_lge.c +++ b/sys/dev/pci/if_lge.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_lge.c,v 1.62 2014/07/22 13:12:11 mpi Exp $ */ +/* $OpenBSD: if_lge.c,v 1.63 2014/08/20 01:00:15 dlg Exp $ */ /* * Copyright (c) 2001 Wind River Systems * Copyright (c) 1997, 1998, 1999, 2000, 2001 @@ -123,10 +123,6 @@ struct cfdriver lge_cd = { NULL, "lge", DV_IFNET }; -int lge_alloc_jumbo_mem(struct lge_softc *); -void *lge_jalloc(struct lge_softc *); -void lge_jfree(caddr_t, u_int, void *); - int lge_newbuf(struct lge_softc *, struct lge_rx_desc *, struct mbuf *); int lge_encap(struct lge_softc *, struct mbuf *, u_int32_t *); @@ -499,14 +495,6 @@ lge_attach(struct device *parent, struct device *self, void *aux) DPRINTFN(5, ("bzero\n")); sc->lge_ldata = (struct lge_list_data *)kva; - /* Try to allocate memory for jumbo buffers. */ - DPRINTFN(5, ("lge_alloc_jumbo_mem\n")); - if (lge_alloc_jumbo_mem(sc)) { - printf("%s: jumbo buffer allocation failed\n", - sc->sc_dv.dv_xname); - goto fail_5; - } - ifp = &sc->arpcom.ac_if; ifp->if_softc = sc; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; @@ -642,22 +630,9 @@ lge_newbuf(struct lge_softc *sc, struct lge_rx_desc *c, struct mbuf *m) struct mbuf *m_new = NULL; if (m == NULL) { - caddr_t buf = NULL; - - MGETHDR(m_new, M_DONTWAIT, MT_DATA); + m_new = MCLGETI(NULL, LGE_JLEN, NULL, M_DONTWAIT); if (m_new == NULL) return (ENOBUFS); - - /* Allocate the jumbo buffer */ - buf = lge_jalloc(sc); - if (buf == NULL) { - m_freem(m_new); - return (ENOBUFS); - } - - /* Attach the buffer to the mbuf */ - m_new->m_len = m_new->m_pkthdr.len = LGE_JLEN; - MEXTADD(m_new, buf, LGE_JLEN, 0, lge_jfree, sc); } else { /* * We're re-using a previously allocated mbuf; @@ -665,9 +640,9 @@ lge_newbuf(struct lge_softc *sc, struct lge_rx_desc *c, struct mbuf *m) * default values. */ m_new = m; - m_new->m_len = m_new->m_pkthdr.len = LGE_JLEN; m_new->m_data = m_new->m_ext.ext_buf; } + m_new->m_len = m_new->m_pkthdr.len = LGE_JLEN; /* * Adjust alignment so packet payload begins on a @@ -700,146 +675,6 @@ lge_newbuf(struct lge_softc *sc, struct lge_rx_desc *c, struct mbuf *m) return (0); } -int -lge_alloc_jumbo_mem(struct lge_softc *sc) -{ - caddr_t ptr, kva; - bus_dma_segment_t seg; - bus_dmamap_t dmamap; - int i, rseg, state, error; - struct lge_jpool_entry *entry; - - state = error = 0; - - /* Grab a big chunk o' storage. */ - if (bus_dmamem_alloc(sc->sc_dmatag, LGE_JMEM, PAGE_SIZE, 0, - &seg, 1, &rseg, BUS_DMA_NOWAIT)) { - printf("%s: can't alloc rx buffers\n", sc->sc_dv.dv_xname); - return (ENOBUFS); - } - - state = 1; - if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, LGE_JMEM, &kva, - BUS_DMA_NOWAIT)) { - printf("%s: can't map dma buffers (%zd bytes)\n", - sc->sc_dv.dv_xname, LGE_JMEM); - error = ENOBUFS; - goto out; - } - - state = 2; - if (bus_dmamap_create(sc->sc_dmatag, LGE_JMEM, 1, - LGE_JMEM, 0, BUS_DMA_NOWAIT, &dmamap)) { - printf("%s: can't create dma map\n", sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - - state = 3; - if (bus_dmamap_load(sc->sc_dmatag, dmamap, kva, LGE_JMEM, - NULL, BUS_DMA_NOWAIT)) { - printf("%s: can't load dma map\n", sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - - state = 4; - sc->lge_cdata.lge_jumbo_buf = (caddr_t)kva; - DPRINTFN(1,("lge_jumbo_buf = 0x%08X\n", sc->lge_cdata.lge_jumbo_buf)); - DPRINTFN(1,("LGE_JLEN = 0x%08X\n", LGE_JLEN)); - - LIST_INIT(&sc->lge_jfree_listhead); - LIST_INIT(&sc->lge_jinuse_listhead); - - /* - * Now divide it up into 9K pieces and save the addresses - * in an array. - */ - ptr = sc->lge_cdata.lge_jumbo_buf; - for (i = 0; i < LGE_JSLOTS; i++) { - sc->lge_cdata.lge_jslots[i] = ptr; - ptr += LGE_JLEN; - entry = malloc(sizeof(struct lge_jpool_entry), - M_DEVBUF, M_NOWAIT); - if (entry == NULL) { - sc->lge_cdata.lge_jumbo_buf = NULL; - printf("%s: no memory for jumbo buffer queue!\n", - sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - entry->slot = i; - LIST_INSERT_HEAD(&sc->lge_jfree_listhead, - entry, jpool_entries); - } -out: - if (error != 0) { - switch (state) { - case 4: - bus_dmamap_unload(sc->sc_dmatag, dmamap); - case 3: - bus_dmamap_destroy(sc->sc_dmatag, dmamap); - case 2: - bus_dmamem_unmap(sc->sc_dmatag, kva, LGE_JMEM); - case 1: - bus_dmamem_free(sc->sc_dmatag, &seg, rseg); - break; - default: - break; - } - } - - return (error); -} - -/* - * Allocate a jumbo buffer. - */ -void * -lge_jalloc(struct lge_softc *sc) -{ - struct lge_jpool_entry *entry; - - entry = LIST_FIRST(&sc->lge_jfree_listhead); - - if (entry == NULL) - return (NULL); - - LIST_REMOVE(entry, jpool_entries); - LIST_INSERT_HEAD(&sc->lge_jinuse_listhead, entry, jpool_entries); - return (sc->lge_cdata.lge_jslots[entry->slot]); -} - -/* - * Release a jumbo buffer. - */ -void -lge_jfree(caddr_t buf, u_int size, void *arg) -{ - struct lge_softc *sc; - int i; - struct lge_jpool_entry *entry; - - /* Extract the softc struct pointer. */ - sc = (struct lge_softc *)arg; - - if (sc == NULL) - panic("lge_jfree: can't find softc pointer!"); - - /* calculate the slot this buffer belongs to */ - i = ((vaddr_t)buf - (vaddr_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN; - - if ((i < 0) || (i >= LGE_JSLOTS)) - panic("lge_jfree: asked to free buffer that we don't manage!"); - - entry = LIST_FIRST(&sc->lge_jinuse_listhead); - if (entry == NULL) - panic("lge_jfree: buffer not in use!"); - entry->slot = i; - LIST_REMOVE(entry, jpool_entries); - LIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry, jpool_entries); -} - /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. diff --git a/sys/dev/pci/if_lgereg.h b/sys/dev/pci/if_lgereg.h index 103a1ca0aef..e31f9aafc54 100644 --- a/sys/dev/pci/if_lgereg.h +++ b/sys/dev/pci/if_lgereg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if_lgereg.h,v 1.7 2012/10/18 21:44:21 deraadt Exp $ */ +/* $OpenBSD: if_lgereg.h,v 1.8 2014/08/20 01:00:15 dlg Exp $ */ /* * Copyright (c) 2001 Wind River Systems * Copyright (c) 1997, 1998, 1999, 2000, 2001 @@ -497,19 +497,11 @@ struct lge_mii_frame { #define LGE_RESID (LGE_JPAGESZ - (LGE_JLEN * LGE_JSLOTS) % LGE_JPAGESZ) #define LGE_JMEM ((LGE_JLEN * LGE_JSLOTS) + LGE_RESID) -struct lge_jpool_entry { - int slot; - LIST_ENTRY(lge_jpool_entry) jpool_entries; -}; - struct lge_ring_data { int lge_rx_prod; int lge_rx_cons; int lge_tx_prod; int lge_tx_cons; - /* Stick the jumbo mem management stuff here too. */ - caddr_t lge_jslots[LGE_JSLOTS]; - void *lge_jumbo_buf; }; struct lge_softc { @@ -527,8 +519,6 @@ struct lge_softc { struct lge_list_data *lge_ldata; struct lge_ring_data lge_cdata; struct timeout lge_timeout; - LIST_HEAD(__lge_jfreehead, lge_jpool_entry) lge_jfree_listhead; - LIST_HEAD(__lge_jinusehead, lge_jpool_entry) lge_jinuse_listhead; }; /* diff --git a/sys/dev/pci/if_nge.c b/sys/dev/pci/if_nge.c index e5cb1730f90..b027aa4e48c 100644 --- a/sys/dev/pci/if_nge.c +++ b/sys/dev/pci/if_nge.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_nge.c,v 1.79 2014/07/22 13:12:11 mpi Exp $ */ +/* $OpenBSD: if_nge.c,v 1.80 2014/08/20 00:59:56 dlg Exp $ */ /* * Copyright (c) 2001 Wind River Systems * Copyright (c) 1997, 1998, 1999, 2000, 2001 @@ -135,10 +135,6 @@ int nge_probe(struct device *, void *, void *); void nge_attach(struct device *, struct device *, void *); -int nge_alloc_jumbo_mem(struct nge_softc *); -void *nge_jalloc(struct nge_softc *); -void nge_jfree(caddr_t, u_int, void *); - int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *); int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *); @@ -797,14 +793,6 @@ nge_attach(struct device *parent, struct device *self, void *aux) DPRINTFN(5, ("%s: bzero\n", sc->sc_dv.dv_xname)); sc->nge_ldata = (struct nge_list_data *)kva; - /* Try to allocate memory for jumbo buffers. */ - DPRINTFN(5, ("%s: nge_alloc_jumbo_mem\n", sc->sc_dv.dv_xname)); - if (nge_alloc_jumbo_mem(sc)) { - printf("%s: jumbo buffer allocation failed\n", - sc->sc_dv.dv_xname); - goto fail_5; - } - ifp = &sc->arpcom.ac_if; ifp->if_softc = sc; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; @@ -983,22 +971,9 @@ nge_newbuf(struct nge_softc *sc, struct nge_desc *c, struct mbuf *m) struct mbuf *m_new = NULL; if (m == NULL) { - caddr_t buf = NULL; - - MGETHDR(m_new, M_DONTWAIT, MT_DATA); + m_new = MCLGETI(NULL, NGE_MCLBYTES, NULL, M_DONTWAIT); if (m_new == NULL) return (ENOBUFS); - - /* Allocate the jumbo buffer */ - buf = nge_jalloc(sc); - if (buf == NULL) { - m_freem(m_new); - return (ENOBUFS); - } - - /* Attach the buffer to the mbuf */ - m_new->m_len = m_new->m_pkthdr.len = NGE_MCLBYTES; - MEXTADD(m_new, buf, NGE_MCLBYTES, 0, nge_jfree, sc); } else { /* * We're re-using a previously allocated mbuf; @@ -1006,10 +981,10 @@ nge_newbuf(struct nge_softc *sc, struct nge_desc *c, struct mbuf *m) * default values. */ m_new = m; - m_new->m_len = m_new->m_pkthdr.len = NGE_MCLBYTES; m_new->m_data = m_new->m_ext.ext_buf; } + m_new->m_len = m_new->m_pkthdr.len = NGE_MCLBYTES; m_adj(m_new, sizeof(u_int64_t)); c->nge_mbuf = m_new; @@ -1022,161 +997,6 @@ nge_newbuf(struct nge_softc *sc, struct nge_desc *c, struct mbuf *m) return(0); } -int -nge_alloc_jumbo_mem(struct nge_softc *sc) -{ - caddr_t ptr, kva; - bus_dma_segment_t seg; - bus_dmamap_t dmamap; - int i, rseg, state, error; - struct nge_jpool_entry *entry; - - state = error = 0; - - if (bus_dmamem_alloc(sc->sc_dmatag, NGE_JMEM, PAGE_SIZE, 0, - &seg, 1, &rseg, BUS_DMA_NOWAIT)) { - printf("%s: can't alloc rx buffers\n", sc->sc_dv.dv_xname); - return (ENOBUFS); - } - - state = 1; - if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, NGE_JMEM, &kva, - BUS_DMA_NOWAIT)) { - printf("%s: can't map dma buffers (%zd bytes)\n", - sc->sc_dv.dv_xname, NGE_JMEM); - error = ENOBUFS; - goto out; - } - - state = 2; - if (bus_dmamap_create(sc->sc_dmatag, NGE_JMEM, 1, - NGE_JMEM, 0, BUS_DMA_NOWAIT, &dmamap)) { - printf("%s: can't create dma map\n", sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - - state = 3; - if (bus_dmamap_load(sc->sc_dmatag, dmamap, kva, NGE_JMEM, - NULL, BUS_DMA_NOWAIT)) { - printf("%s: can't load dma map\n", sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - - state = 4; - sc->nge_cdata.nge_jumbo_buf = (caddr_t)kva; - DPRINTFN(1,("%s: nge_jumbo_buf=%#x, NGE_MCLBYTES=%#x\n", - sc->sc_dv.dv_xname , sc->nge_cdata.nge_jumbo_buf, - NGE_MCLBYTES)); - - LIST_INIT(&sc->nge_jfree_listhead); - LIST_INIT(&sc->nge_jinuse_listhead); - - /* - * Now divide it up into 9K pieces and save the addresses - * in an array. Note that we play an evil trick here by using - * the first few bytes in the buffer to hold the address - * of the softc structure for this interface. This is because - * nge_jfree() needs it, but it is called by the mbuf management - * code which will not pass it to us explicitly. - */ - ptr = sc->nge_cdata.nge_jumbo_buf; - for (i = 0; i < NGE_JSLOTS; i++) { - sc->nge_cdata.nge_jslots[i].nge_buf = ptr; - sc->nge_cdata.nge_jslots[i].nge_inuse = 0; - ptr += NGE_MCLBYTES; - entry = malloc(sizeof(struct nge_jpool_entry), - M_DEVBUF, M_NOWAIT); - if (entry == NULL) { - sc->nge_cdata.nge_jumbo_buf = NULL; - printf("%s: no memory for jumbo buffer queue!\n", - sc->sc_dv.dv_xname); - error = ENOBUFS; - goto out; - } - entry->slot = i; - LIST_INSERT_HEAD(&sc->nge_jfree_listhead, entry, - jpool_entries); - } -out: - if (error != 0) { - switch (state) { - case 4: - bus_dmamap_unload(sc->sc_dmatag, dmamap); - case 3: - bus_dmamap_destroy(sc->sc_dmatag, dmamap); - case 2: - bus_dmamem_unmap(sc->sc_dmatag, kva, NGE_JMEM); - case 1: - bus_dmamem_free(sc->sc_dmatag, &seg, rseg); - break; - default: - break; - } - } - - return (error); -} - -/* - * Allocate a jumbo buffer. - */ -void * -nge_jalloc(struct nge_softc *sc) -{ - struct nge_jpool_entry *entry; - - entry = LIST_FIRST(&sc->nge_jfree_listhead); - - if (entry == NULL) - return (NULL); - - LIST_REMOVE(entry, jpool_entries); - LIST_INSERT_HEAD(&sc->nge_jinuse_listhead, entry, jpool_entries); - sc->nge_cdata.nge_jslots[entry->slot].nge_inuse = 1; - return(sc->nge_cdata.nge_jslots[entry->slot].nge_buf); -} - -/* - * Release a jumbo buffer. - */ -void -nge_jfree(caddr_t buf, u_int size, void *arg) -{ - struct nge_softc *sc; - int i; - struct nge_jpool_entry *entry; - - /* Extract the softc struct pointer. */ - sc = (struct nge_softc *)arg; - - if (sc == NULL) - panic("nge_jfree: can't find softc pointer!"); - - /* calculate the slot this buffer belongs to */ - - i = ((vaddr_t)buf - (vaddr_t)sc->nge_cdata.nge_jumbo_buf) - / NGE_MCLBYTES; - - if ((i < 0) || (i >= NGE_JSLOTS)) - panic("nge_jfree: asked to free buffer that we don't manage!"); - else if (sc->nge_cdata.nge_jslots[i].nge_inuse == 0) - panic("nge_jfree: buffer already free!"); - else { - sc->nge_cdata.nge_jslots[i].nge_inuse--; - if(sc->nge_cdata.nge_jslots[i].nge_inuse == 0) { - entry = LIST_FIRST(&sc->nge_jinuse_listhead); - if (entry == NULL) - panic("nge_jfree: buffer not in use!"); - entry->slot = i; - LIST_REMOVE(entry, jpool_entries); - LIST_INSERT_HEAD(&sc->nge_jfree_listhead, - entry, jpool_entries); - } - } -} - /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. diff --git a/sys/dev/pci/if_ngereg.h b/sys/dev/pci/if_ngereg.h index c9f9db59298..7b92f0ec5b2 100644 --- a/sys/dev/pci/if_ngereg.h +++ b/sys/dev/pci/if_ngereg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: if_ngereg.h,v 1.9 2012/10/18 21:44:21 deraadt Exp $ */ +/* $OpenBSD: if_ngereg.h,v 1.10 2014/08/20 00:59:56 dlg Exp $ */ /* * Copyright (c) 2001 Wind River Systems * Copyright (c) 1997, 1998, 1999, 2000, 2001 @@ -625,24 +625,11 @@ struct nge_mii_frame { #define NGE_RESID (NGE_JPAGESZ - (NGE_JLEN * NGE_JSLOTS) % NGE_JPAGESZ) #define NGE_JMEM ((NGE_JLEN * NGE_JSLOTS) + NGE_RESID) -struct nge_jslot { - caddr_t nge_buf; - int nge_inuse; -}; - -struct nge_jpool_entry { - int slot; - LIST_ENTRY(nge_jpool_entry) jpool_entries; -}; - struct nge_ring_data { int nge_rx_prod; int nge_tx_prod; int nge_tx_cons; int nge_tx_cnt; - /* Stick the jumbo mem management stuff here too. */ - struct nge_jslot nge_jslots[NGE_JSLOTS]; - void *nge_jumbo_buf; }; struct nge_softc { @@ -662,8 +649,6 @@ struct nge_softc { struct nge_list_data *nge_ldata; struct nge_ring_data nge_cdata; struct timeout nge_timeout; - LIST_HEAD(__nge_jfreehead, nge_jpool_entry) nge_jfree_listhead; - LIST_HEAD(__nge_jinusehead, nge_jpool_entry) nge_jinuse_listhead; u_int8_t nge_tbi; struct ifmedia nge_ifmedia; }; -- cgit v1.2.3