1 /**************************************************************************
3 * Copyright (c) 2007, Kip Macy kmacy@freebsd.org
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. The name of Kip Macy nor the names of other
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/cxgb/sys/mvec.h,v 1.5 2007/05/25 16:42:25 kmacy Exp $
30 ***************************************************************************/
37 #define mtomv(m) ((struct mbuf_vec *)((m)->m_pktdat))
39 #define M_IOVEC 0x100000 /* mbuf immediate data area is used for cluster ptrs */
40 #define MBUF_IOV_TYPE_MASK ((1<<3)-1)
41 #define mbuf_vec_set_type(mv, i, type) \
42 (mv)->mv_vec[(i)].mi_flags = (((mv)->mv_vec[(i)].mi_flags \
43 & ~MBUF_IOV_TYPE_MASK) | type)
45 #define mbuf_vec_get_type(mv, i) \
46 ((mv)->mv_vec[(i)].mi_flags & MBUF_IOV_TYPE_MASK)
50 uint16_t mi_flags
; /* per-cluster flags */
51 uint16_t mi_len
; /* length of cluster */
52 uint32_t mi_offset
; /* data offsets into cluster */
53 uint8_t *mi_base
; /* pointers to cluster */
54 volatile uint32_t *mi_refcnt
; /* refcnt for cluster*/
56 void *mi_args
; /* for sf_buf */
60 #define MAX_MBUF_IOV ((MHLEN-8)/sizeof(struct mbuf_iovec))
62 uint16_t mv_first
; /* first valid cluster */
63 uint16_t mv_count
; /* # of clusters */
64 uint32_t mv_flags
; /* flags for iovec */
65 struct mbuf_iovec mv_vec
[MAX_MBUF_IOV
];
68 int _m_explode(struct mbuf
*);
69 int _m_collapse(struct mbuf
*, int maxbufs
, struct mbuf
**);
70 void mb_free_vec(struct mbuf
*m
);
73 m_iovinit(struct mbuf
*m
)
75 struct mbuf_vec
*mv
= mtomv(m
);
77 mv
->mv_first
= mv
->mv_count
= 0;
78 m
->m_pkthdr
.len
= m
->m_len
= 0;
79 m
->m_flags
|= M_IOVEC
;
83 m_iovappend(struct mbuf
*m
, uint8_t *cl
, int size
, int len
, int offset
)
85 struct mbuf_vec
*mv
= mtomv(m
);
86 struct mbuf_iovec
*iov
;
87 int idx
= mv
->mv_first
+ mv
->mv_count
;
90 KASSERT(idx
<= MAX_MBUF_IOV
, ("tried to append too many clusters to mbuf iovec"));
92 if ((m
->m_flags
& M_EXT
) != 0)
93 panic("invalid flags in %s", __func__
);
95 if (mv
->mv_count
== 0)
96 m
->m_data
= cl
+ offset
;
98 iov
= &mv
->mv_vec
[idx
];
100 iov
->mi_flags
= m_gettype(size
);
104 iov
->mi_offset
= offset
;
105 m
->m_pkthdr
.len
+= len
;
111 m_explode(struct mbuf
*m
)
113 if ((m
->m_flags
& M_IOVEC
) == 0)
116 return _m_explode(m
);
120 m_collapse(struct mbuf
*m
, int maxbufs
, struct mbuf
**mnew
)
122 #if (!defined(__sparc64__) && !defined(__sun4v__))
123 if (m
->m_next
== NULL
)
129 return _m_collapse(m
, maxbufs
, mnew
);
132 static inline struct mbuf
*
133 m_free_vec(struct mbuf
*m
)
136 struct mbuf
*n
= m
->m_next
;
138 if (m
->m_flags
& M_IOVEC
)
140 else if (m
->m_flags
& M_EXT
)
143 uma_zfree(zone_mbuf
, m
);
146 struct mbuf
*n
= NULL
;
154 m_freem_vec(struct mbuf
*m
)
161 static inline uma_zone_t
162 m_getzonefromtype(int type
)
173 #if MJUMPAGESIZE != MCLBYTES
184 #ifndef PACKET_ZONE_DISABLED
190 panic("%s: invalid cluster type %d", __func__
, type
);
196 #if (!defined(__sparc64__) && !defined(__sun4v__))
198 bus_dmamap_load_mvec_sg(bus_dma_tag_t dmat
, bus_dmamap_t map
, struct mbuf
*m0
,
199 bus_dma_segment_t
*segs
, int *nsegs
, int flags
);
202 #define bus_dmamap_load_mvec_sg bus_dmamap_load_mbuf_sg