ixgbe_netbsd.c revision 1.15.6.1 1 1.15.6.1 thorpej /* $NetBSD: ixgbe_netbsd.c,v 1.15.6.1 2021/05/13 00:47:31 thorpej Exp $ */
2 1.1 dyoung /*
3 1.1 dyoung * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 dyoung * All rights reserved.
5 1.1 dyoung *
6 1.1 dyoung * This code is derived from software contributed to The NetBSD Foundation
7 1.1 dyoung * by Coyote Point Systems, Inc.
8 1.1 dyoung *
9 1.1 dyoung * Redistribution and use in source and binary forms, with or without
10 1.1 dyoung * modification, are permitted provided that the following conditions
11 1.1 dyoung * are met:
12 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
13 1.1 dyoung * notice, this list of conditions and the following disclaimer.
14 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
16 1.1 dyoung * documentation and/or other materials provided with the distribution.
17 1.1 dyoung *
18 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 dyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 dyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 dyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 dyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 dyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 dyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 dyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 dyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 dyoung * POSSIBILITY OF SUCH DAMAGE.
29 1.1 dyoung */
30 1.15.6.1 thorpej
31 1.15.6.1 thorpej #include <sys/cdefs.h>
32 1.15.6.1 thorpej __KERNEL_RCSID(0, "$NetBSD: ixgbe_netbsd.c,v 1.15.6.1 2021/05/13 00:47:31 thorpej Exp $");
33 1.15.6.1 thorpej
34 1.1 dyoung #include <sys/param.h>
35 1.1 dyoung
36 1.1 dyoung #include <sys/atomic.h>
37 1.1 dyoung #include <sys/bus.h>
38 1.1 dyoung #include <sys/condvar.h>
39 1.1 dyoung #include <sys/cpu.h>
40 1.1 dyoung #include <sys/kmem.h>
41 1.1 dyoung #include <sys/mbuf.h>
42 1.1 dyoung #include <sys/mutex.h>
43 1.1 dyoung #include <sys/queue.h>
44 1.1 dyoung #include <sys/workqueue.h>
45 1.4 msaitoh #include <dev/pci/pcivar.h>
46 1.1 dyoung
47 1.7 msaitoh #include "ixgbe.h"
48 1.1 dyoung
49 1.1 dyoung void
50 1.1 dyoung ixgbe_dma_tag_destroy(ixgbe_dma_tag_t *dt)
51 1.1 dyoung {
52 1.1 dyoung kmem_free(dt, sizeof(*dt));
53 1.1 dyoung }
54 1.1 dyoung
55 1.1 dyoung int
56 1.1 dyoung ixgbe_dma_tag_create(bus_dma_tag_t dmat, bus_size_t alignment,
57 1.1 dyoung bus_size_t boundary, bus_size_t maxsize, int nsegments,
58 1.1 dyoung bus_size_t maxsegsize, int flags, ixgbe_dma_tag_t **dtp)
59 1.1 dyoung {
60 1.1 dyoung ixgbe_dma_tag_t *dt;
61 1.1 dyoung
62 1.1 dyoung *dtp = NULL;
63 1.1 dyoung
64 1.6 chs dt = kmem_zalloc(sizeof(*dt), KM_SLEEP);
65 1.1 dyoung dt->dt_dmat = dmat;
66 1.1 dyoung dt->dt_alignment = alignment;
67 1.1 dyoung dt->dt_boundary = boundary;
68 1.1 dyoung dt->dt_maxsize = maxsize;
69 1.1 dyoung dt->dt_nsegments = nsegments;
70 1.1 dyoung dt->dt_maxsegsize = maxsegsize;
71 1.1 dyoung dt->dt_flags = flags;
72 1.1 dyoung *dtp = dt;
73 1.1 dyoung
74 1.1 dyoung return 0;
75 1.1 dyoung }
76 1.1 dyoung
77 1.1 dyoung void
78 1.1 dyoung ixgbe_dmamap_destroy(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
79 1.1 dyoung {
80 1.1 dyoung bus_dmamap_destroy(dt->dt_dmat, dmam);
81 1.1 dyoung }
82 1.1 dyoung
83 1.1 dyoung void
84 1.1 dyoung ixgbe_dmamap_sync(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam, int ops)
85 1.1 dyoung {
86 1.14 msaitoh bus_dmamap_sync(dt->dt_dmat, dmam, 0, dt->dt_maxsize, ops);
87 1.1 dyoung }
88 1.1 dyoung
89 1.1 dyoung void
90 1.1 dyoung ixgbe_dmamap_unload(ixgbe_dma_tag_t *dt, bus_dmamap_t dmam)
91 1.1 dyoung {
92 1.1 dyoung bus_dmamap_unload(dt->dt_dmat, dmam);
93 1.1 dyoung }
94 1.1 dyoung
95 1.1 dyoung int
96 1.1 dyoung ixgbe_dmamap_create(ixgbe_dma_tag_t *dt, int flags, bus_dmamap_t *dmamp)
97 1.1 dyoung {
98 1.1 dyoung return bus_dmamap_create(dt->dt_dmat, dt->dt_maxsize, dt->dt_nsegments,
99 1.1 dyoung dt->dt_maxsegsize, dt->dt_boundary, flags, dmamp);
100 1.1 dyoung }
101 1.1 dyoung
102 1.1 dyoung static void
103 1.1 dyoung ixgbe_putext(ixgbe_extmem_t *em)
104 1.1 dyoung {
105 1.1 dyoung ixgbe_extmem_head_t *eh = em->em_head;
106 1.1 dyoung
107 1.1 dyoung mutex_enter(&eh->eh_mtx);
108 1.1 dyoung
109 1.1 dyoung TAILQ_INSERT_HEAD(&eh->eh_freelist, em, em_link);
110 1.1 dyoung
111 1.1 dyoung mutex_exit(&eh->eh_mtx);
112 1.1 dyoung
113 1.1 dyoung return;
114 1.1 dyoung }
115 1.1 dyoung
116 1.1 dyoung static ixgbe_extmem_t *
117 1.1 dyoung ixgbe_getext(ixgbe_extmem_head_t *eh, size_t size)
118 1.1 dyoung {
119 1.1 dyoung ixgbe_extmem_t *em;
120 1.1 dyoung
121 1.1 dyoung mutex_enter(&eh->eh_mtx);
122 1.1 dyoung
123 1.1 dyoung TAILQ_FOREACH(em, &eh->eh_freelist, em_link) {
124 1.1 dyoung if (em->em_size >= size)
125 1.1 dyoung break;
126 1.1 dyoung }
127 1.1 dyoung
128 1.1 dyoung if (em != NULL)
129 1.1 dyoung TAILQ_REMOVE(&eh->eh_freelist, em, em_link);
130 1.1 dyoung
131 1.1 dyoung mutex_exit(&eh->eh_mtx);
132 1.1 dyoung
133 1.1 dyoung return em;
134 1.1 dyoung }
135 1.1 dyoung
136 1.1 dyoung static ixgbe_extmem_t *
137 1.1 dyoung ixgbe_newext(ixgbe_extmem_head_t *eh, bus_dma_tag_t dmat, size_t size)
138 1.1 dyoung {
139 1.1 dyoung ixgbe_extmem_t *em;
140 1.1 dyoung int nseg, rc;
141 1.1 dyoung
142 1.3 msaitoh em = kmem_zalloc(sizeof(*em), KM_SLEEP);
143 1.1 dyoung
144 1.1 dyoung rc = bus_dmamem_alloc(dmat, size, PAGE_SIZE, 0, &em->em_seg, 1, &nseg,
145 1.3 msaitoh BUS_DMA_WAITOK);
146 1.1 dyoung
147 1.1 dyoung if (rc != 0)
148 1.1 dyoung goto post_zalloc_err;
149 1.1 dyoung
150 1.1 dyoung rc = bus_dmamem_map(dmat, &em->em_seg, 1, size, &em->em_vaddr,
151 1.3 msaitoh BUS_DMA_WAITOK);
152 1.1 dyoung
153 1.1 dyoung if (rc != 0)
154 1.1 dyoung goto post_dmamem_err;
155 1.1 dyoung
156 1.1 dyoung em->em_dmat = dmat;
157 1.1 dyoung em->em_size = size;
158 1.1 dyoung em->em_head = eh;
159 1.1 dyoung
160 1.1 dyoung return em;
161 1.1 dyoung post_dmamem_err:
162 1.1 dyoung bus_dmamem_free(dmat, &em->em_seg, 1);
163 1.1 dyoung post_zalloc_err:
164 1.1 dyoung kmem_free(em, sizeof(*em));
165 1.1 dyoung return NULL;
166 1.1 dyoung }
167 1.1 dyoung
168 1.11 msaitoh static void
169 1.11 msaitoh ixgbe_jcl_freeall(struct adapter *adapter, struct rx_ring *rxr)
170 1.11 msaitoh {
171 1.11 msaitoh ixgbe_extmem_head_t *eh = &rxr->jcl_head;
172 1.11 msaitoh ixgbe_extmem_t *em;
173 1.11 msaitoh bus_dma_tag_t dmat = rxr->ptag->dt_dmat;
174 1.11 msaitoh
175 1.11 msaitoh while ((em = ixgbe_getext(eh, 0)) != NULL) {
176 1.11 msaitoh KASSERT(em->em_vaddr != NULL);
177 1.11 msaitoh bus_dmamem_unmap(dmat, em->em_vaddr, em->em_size);
178 1.11 msaitoh bus_dmamem_free(dmat, &em->em_seg, 1);
179 1.11 msaitoh memset(em, 0, sizeof(*em));
180 1.11 msaitoh kmem_free(em, sizeof(*em));
181 1.11 msaitoh }
182 1.11 msaitoh }
183 1.11 msaitoh
184 1.1 dyoung void
185 1.8 msaitoh ixgbe_jcl_reinit(struct adapter *adapter, bus_dma_tag_t dmat,
186 1.8 msaitoh struct rx_ring *rxr, int nbuf, size_t size)
187 1.1 dyoung {
188 1.8 msaitoh ixgbe_extmem_head_t *eh = &rxr->jcl_head;
189 1.7 msaitoh ixgbe_extmem_t *em;
190 1.1 dyoung int i;
191 1.1 dyoung
192 1.1 dyoung if (!eh->eh_initialized) {
193 1.1 dyoung TAILQ_INIT(&eh->eh_freelist);
194 1.1 dyoung mutex_init(&eh->eh_mtx, MUTEX_DEFAULT, IPL_NET);
195 1.1 dyoung eh->eh_initialized = true;
196 1.1 dyoung }
197 1.1 dyoung
198 1.7 msaitoh /*
199 1.7 msaitoh * Check previous parameters. If it's not required to reinit, just
200 1.7 msaitoh * return.
201 1.7 msaitoh *
202 1.7 msaitoh * Note that the num_rx_desc is currently fixed value. It's never
203 1.7 msaitoh * changed after device is attached.
204 1.7 msaitoh */
205 1.8 msaitoh if ((rxr->last_rx_mbuf_sz == rxr->mbuf_sz)
206 1.8 msaitoh && (rxr->last_num_rx_desc == adapter->num_rx_desc))
207 1.7 msaitoh return;
208 1.7 msaitoh
209 1.7 msaitoh /* Free all dmamem */
210 1.11 msaitoh ixgbe_jcl_freeall(adapter, rxr);
211 1.1 dyoung
212 1.1 dyoung for (i = 0; i < nbuf; i++) {
213 1.1 dyoung if ((em = ixgbe_newext(eh, dmat, size)) == NULL) {
214 1.10 msaitoh device_printf(adapter->dev,
215 1.10 msaitoh "%s: only %d of %d jumbo buffers allocated\n",
216 1.1 dyoung __func__, i, nbuf);
217 1.1 dyoung break;
218 1.1 dyoung }
219 1.1 dyoung ixgbe_putext(em);
220 1.1 dyoung }
221 1.7 msaitoh
222 1.7 msaitoh /* Keep current parameters */
223 1.8 msaitoh rxr->last_rx_mbuf_sz = adapter->rx_mbuf_sz;
224 1.8 msaitoh rxr->last_num_rx_desc = adapter->num_rx_desc;
225 1.1 dyoung }
226 1.1 dyoung
227 1.11 msaitoh void
228 1.11 msaitoh ixgbe_jcl_destroy(struct adapter *adapter, struct rx_ring *rxr)
229 1.11 msaitoh {
230 1.11 msaitoh ixgbe_extmem_head_t *eh = &rxr->jcl_head;
231 1.11 msaitoh
232 1.12 msaitoh if (eh->eh_initialized) {
233 1.12 msaitoh /* Free all dmamem */
234 1.12 msaitoh ixgbe_jcl_freeall(adapter, rxr);
235 1.11 msaitoh
236 1.11 msaitoh mutex_destroy(&eh->eh_mtx);
237 1.11 msaitoh eh->eh_initialized = false;
238 1.11 msaitoh }
239 1.11 msaitoh }
240 1.11 msaitoh
241 1.11 msaitoh
242 1.1 dyoung static void
243 1.1 dyoung ixgbe_jcl_free(struct mbuf *m, void *buf, size_t size, void *arg)
244 1.1 dyoung {
245 1.1 dyoung ixgbe_extmem_t *em = arg;
246 1.1 dyoung
247 1.1 dyoung KASSERT(em->em_size == size);
248 1.1 dyoung
249 1.1 dyoung ixgbe_putext(em);
250 1.1 dyoung /* this is an abstraction violation, but it does not lead to a
251 1.1 dyoung * double-free
252 1.1 dyoung */
253 1.1 dyoung if (__predict_true(m != NULL)) {
254 1.1 dyoung KASSERT(m->m_type != MT_FREE);
255 1.1 dyoung m->m_type = MT_FREE;
256 1.1 dyoung pool_cache_put(mb_cache, m);
257 1.1 dyoung }
258 1.1 dyoung }
259 1.1 dyoung
260 1.1 dyoung /* XXX need to wait for the system to finish with each jumbo mbuf and
261 1.1 dyoung * free it before detaching the driver from the device.
262 1.1 dyoung */
263 1.1 dyoung struct mbuf *
264 1.1 dyoung ixgbe_getjcl(ixgbe_extmem_head_t *eh, int nowait /* M_DONTWAIT */,
265 1.1 dyoung int type /* MT_DATA */, int flags /* M_PKTHDR */, size_t size)
266 1.1 dyoung {
267 1.1 dyoung ixgbe_extmem_t *em;
268 1.1 dyoung struct mbuf *m;
269 1.1 dyoung
270 1.1 dyoung if ((flags & M_PKTHDR) != 0)
271 1.1 dyoung m = m_gethdr(nowait, type);
272 1.1 dyoung else
273 1.1 dyoung m = m_get(nowait, type);
274 1.1 dyoung
275 1.1 dyoung if (m == NULL)
276 1.1 dyoung return NULL;
277 1.1 dyoung
278 1.1 dyoung em = ixgbe_getext(eh, size);
279 1.1 dyoung if (em == NULL) {
280 1.1 dyoung m_freem(m);
281 1.1 dyoung return NULL;
282 1.1 dyoung }
283 1.1 dyoung
284 1.1 dyoung MEXTADD(m, em->em_vaddr, em->em_size, M_DEVBUF, &ixgbe_jcl_free, em);
285 1.1 dyoung
286 1.1 dyoung if ((m->m_flags & M_EXT) == 0) {
287 1.1 dyoung ixgbe_putext(em);
288 1.1 dyoung m_freem(m);
289 1.1 dyoung return NULL;
290 1.1 dyoung }
291 1.1 dyoung
292 1.1 dyoung return m;
293 1.1 dyoung }
294 1.4 msaitoh
295 1.4 msaitoh void
296 1.4 msaitoh ixgbe_pci_enable_busmaster(pci_chipset_tag_t pc, pcitag_t tag)
297 1.4 msaitoh {
298 1.4 msaitoh pcireg_t pci_cmd_word;
299 1.4 msaitoh
300 1.4 msaitoh pci_cmd_word = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
301 1.4 msaitoh if (!(pci_cmd_word & PCI_COMMAND_MASTER_ENABLE)) {
302 1.4 msaitoh pci_cmd_word |= PCI_COMMAND_MASTER_ENABLE;
303 1.4 msaitoh pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, pci_cmd_word);
304 1.4 msaitoh }
305 1.4 msaitoh }
306 1.9 msaitoh
307 1.9 msaitoh u_int
308 1.9 msaitoh atomic_load_acq_uint(volatile u_int *p)
309 1.9 msaitoh {
310 1.13 riastrad return atomic_load_acquire(p);
311 1.9 msaitoh }
312 1.15 msaitoh
313 1.15 msaitoh void
314 1.15 msaitoh ixgbe_delay(unsigned int us)
315 1.15 msaitoh {
316 1.15 msaitoh
317 1.15 msaitoh if (__predict_false(cold))
318 1.15 msaitoh delay(us);
319 1.15 msaitoh else if ((us / 1000) >= hztoms(1)) {
320 1.15 msaitoh /*
321 1.15 msaitoh * Wait at least two clock ticks so we know the time has
322 1.15 msaitoh * passed.
323 1.15 msaitoh */
324 1.15 msaitoh kpause("ixgdly", false, mstohz(us / 1000) + 1, NULL);
325 1.15 msaitoh } else
326 1.15 msaitoh delay(us);
327 1.15 msaitoh }
328