xref: /freebsd/sys/dev/ioat/ioat.c (revision 1693d27b71fe4cff20e3b4dad55dcb27c9c7d11a)
1e974f91cSConrad Meyer /*-
2e974f91cSConrad Meyer  * Copyright (C) 2012 Intel Corporation
3e974f91cSConrad Meyer  * All rights reserved.
4e974f91cSConrad Meyer  *
5e974f91cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
6e974f91cSConrad Meyer  * modification, are permitted provided that the following conditions
7e974f91cSConrad Meyer  * are met:
8e974f91cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
9e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
10e974f91cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
11e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
12e974f91cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
13e974f91cSConrad Meyer  *
14e974f91cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e974f91cSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e974f91cSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e974f91cSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e974f91cSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e974f91cSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e974f91cSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e974f91cSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e974f91cSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e974f91cSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e974f91cSConrad Meyer  * SUCH DAMAGE.
25e974f91cSConrad Meyer  */
26e974f91cSConrad Meyer 
27e974f91cSConrad Meyer #include <sys/cdefs.h>
28e974f91cSConrad Meyer __FBSDID("$FreeBSD$");
29e974f91cSConrad Meyer 
30e974f91cSConrad Meyer #include <sys/param.h>
31e974f91cSConrad Meyer #include <sys/systm.h>
32e974f91cSConrad Meyer #include <sys/bus.h>
33e974f91cSConrad Meyer #include <sys/conf.h>
34e974f91cSConrad Meyer #include <sys/ioccom.h>
35e974f91cSConrad Meyer #include <sys/kernel.h>
36e974f91cSConrad Meyer #include <sys/lock.h>
37e974f91cSConrad Meyer #include <sys/malloc.h>
38e974f91cSConrad Meyer #include <sys/module.h>
39e974f91cSConrad Meyer #include <sys/mutex.h>
40e974f91cSConrad Meyer #include <sys/rman.h>
41e974f91cSConrad Meyer #include <sys/sysctl.h>
42e974f91cSConrad Meyer #include <sys/time.h>
43e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
44e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
45e974f91cSConrad Meyer #include <machine/bus.h>
46e974f91cSConrad Meyer #include <machine/resource.h>
47e974f91cSConrad Meyer #include <machine/stdarg.h>
48e974f91cSConrad Meyer 
49e974f91cSConrad Meyer #include "ioat.h"
50e974f91cSConrad Meyer #include "ioat_hw.h"
51e974f91cSConrad Meyer #include "ioat_internal.h"
52e974f91cSConrad Meyer 
53fe720f5aSConrad Meyer #define	IOAT_INTR_TIMO	(hz / 10)
54466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
55fe720f5aSConrad Meyer 
56e974f91cSConrad Meyer static int ioat_probe(device_t device);
57e974f91cSConrad Meyer static int ioat_attach(device_t device);
58e974f91cSConrad Meyer static int ioat_detach(device_t device);
594253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
604253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
61e974f91cSConrad Meyer static int ioat3_attach(device_t device);
62cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
63e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
64e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
65e974f91cSConrad Meyer     int error);
66e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
670d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
68e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
69e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
70e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
71bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
72bf8553eaSConrad Meyer     struct ioat_descriptor **);
73e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat,
74e974f91cSConrad Meyer     struct ioat_descriptor *desc);
75bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *,
76bf8553eaSConrad Meyer     int mflags);
77bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
78e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat,
79e974f91cSConrad Meyer     uint32_t index);
80bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *,
81bf8553eaSConrad Meyer     uint32_t size, boolean_t need_dscr, int mflags);
82bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder,
83bf8553eaSConrad Meyer     struct ioat_descriptor **);
84bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder,
85bf8553eaSConrad Meyer     struct ioat_descriptor **);
86e974f91cSConrad Meyer static void ioat_timer_callback(void *arg);
87e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
88e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
89e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
90e974f91cSConrad Meyer     int error);
91e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
92e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
93f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
94466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
95466b3540SConrad Meyer     enum ioat_ref_kind);
96466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
97466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
98466b3540SConrad Meyer     enum ioat_ref_kind);
99466b3540SConrad Meyer static void ioat_drain(struct ioat_softc *);
100e974f91cSConrad Meyer 
1011c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1021c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1031c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1041c25420eSConrad Meyer 	}								\
1051c25420eSConrad Meyer } while (0)
1061c25420eSConrad Meyer 
107e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
108e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
109e974f91cSConrad Meyer 
110e974f91cSConrad Meyer static int g_force_legacy_interrupts;
111e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
112e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
113e974f91cSConrad Meyer 
1141c25420eSConrad Meyer int g_ioat_debug_level = 0;
115e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
116e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
117e974f91cSConrad Meyer 
118e974f91cSConrad Meyer /*
119e974f91cSConrad Meyer  * OS <-> Driver interface structures
120e974f91cSConrad Meyer  */
121e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
122e974f91cSConrad Meyer 	/* Device interface */
123e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
124e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
125e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
126e974f91cSConrad Meyer 	{ 0, 0 }
127e974f91cSConrad Meyer };
128e974f91cSConrad Meyer 
129e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
130e974f91cSConrad Meyer 	"ioat",
131e974f91cSConrad Meyer 	ioat_pci_methods,
132e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
133e974f91cSConrad Meyer };
134e974f91cSConrad Meyer 
135e974f91cSConrad Meyer static devclass_t ioat_devclass;
136e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
137e974f91cSConrad Meyer 
138e974f91cSConrad Meyer /*
139e974f91cSConrad Meyer  * Private data structures
140e974f91cSConrad Meyer  */
141e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
142e974f91cSConrad Meyer static int ioat_channel_index = 0;
143e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
144e974f91cSConrad Meyer     "Number of IOAT channels attached");
145e974f91cSConrad Meyer 
146e974f91cSConrad Meyer static struct _pcsid
147e974f91cSConrad Meyer {
148e974f91cSConrad Meyer 	u_int32_t   type;
149e974f91cSConrad Meyer 	const char  *desc;
150e974f91cSConrad Meyer } pci_ids[] = {
151e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
152e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
153e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
154e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
155e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
156e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
157e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
158e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
159e974f91cSConrad Meyer 
160e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
161e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
162e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
163e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
164e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
165e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
166e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
167e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
168e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
169e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
170e974f91cSConrad Meyer 
171e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
172e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
173e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
174e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
175e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
176e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
177e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
178e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
179e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
180e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
181e974f91cSConrad Meyer 
182e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
183e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
184e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
185e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
186e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
187e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
188e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
189e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
190e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
191e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
192e974f91cSConrad Meyer 
193e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
194e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
195e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
196e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
197e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
198e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
199e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
200e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
201e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
202e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
203e974f91cSConrad Meyer 
204e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
205e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
206e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
207e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
208e974f91cSConrad Meyer 
209e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
210e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
211e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
212e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
213e974f91cSConrad Meyer 
214e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
215e974f91cSConrad Meyer };
216e974f91cSConrad Meyer 
217e974f91cSConrad Meyer /*
218e974f91cSConrad Meyer  * OS <-> Driver linkage functions
219e974f91cSConrad Meyer  */
220e974f91cSConrad Meyer static int
221e974f91cSConrad Meyer ioat_probe(device_t device)
222e974f91cSConrad Meyer {
223e974f91cSConrad Meyer 	struct _pcsid *ep;
224e974f91cSConrad Meyer 	u_int32_t type;
225e974f91cSConrad Meyer 
226e974f91cSConrad Meyer 	type = pci_get_devid(device);
227e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
228e974f91cSConrad Meyer 		if (ep->type == type) {
229e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
230e974f91cSConrad Meyer 			return (0);
231e974f91cSConrad Meyer 		}
232e974f91cSConrad Meyer 	}
233e974f91cSConrad Meyer 	return (ENXIO);
234e974f91cSConrad Meyer }
235e974f91cSConrad Meyer 
236e974f91cSConrad Meyer static int
237e974f91cSConrad Meyer ioat_attach(device_t device)
238e974f91cSConrad Meyer {
239e974f91cSConrad Meyer 	struct ioat_softc *ioat;
240e974f91cSConrad Meyer 	int error;
241e974f91cSConrad Meyer 
242e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
243e974f91cSConrad Meyer 	ioat->device = device;
244e974f91cSConrad Meyer 
245e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
246e974f91cSConrad Meyer 	if (error != 0)
247e974f91cSConrad Meyer 		goto err;
248e974f91cSConrad Meyer 
249e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
250e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
251e974f91cSConrad Meyer 		error = ENODEV;
252e974f91cSConrad Meyer 		goto err;
253e974f91cSConrad Meyer 	}
254e974f91cSConrad Meyer 
255e974f91cSConrad Meyer 	error = ioat3_attach(device);
256e974f91cSConrad Meyer 	if (error != 0)
257e974f91cSConrad Meyer 		goto err;
258e974f91cSConrad Meyer 
259e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
260e974f91cSConrad Meyer 	if (error != 0)
261e974f91cSConrad Meyer 		goto err;
262e974f91cSConrad Meyer 
263466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
264466b3540SConrad Meyer 	if (error != 0)
265466b3540SConrad Meyer 		goto err;
266466b3540SConrad Meyer 
267cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2687afbb263SConrad Meyer 	if (error != 0)
269466b3540SConrad Meyer 		goto err;
2707afbb263SConrad Meyer 
2717afbb263SConrad Meyer 	ioat_process_events(ioat);
2727afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2737afbb263SConrad Meyer 
274e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
2757afbb263SConrad Meyer 	ioat_test_attach();
276e974f91cSConrad Meyer 
277e974f91cSConrad Meyer err:
278e974f91cSConrad Meyer 	if (error != 0)
279e974f91cSConrad Meyer 		ioat_detach(device);
280e974f91cSConrad Meyer 	return (error);
281e974f91cSConrad Meyer }
282e974f91cSConrad Meyer 
283e974f91cSConrad Meyer static int
284e974f91cSConrad Meyer ioat_detach(device_t device)
285e974f91cSConrad Meyer {
286e974f91cSConrad Meyer 	struct ioat_softc *ioat;
287e974f91cSConrad Meyer 
288e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
2897afbb263SConrad Meyer 
2907afbb263SConrad Meyer 	ioat_test_detach();
291466b3540SConrad Meyer 	ioat_drain(ioat);
292fe720f5aSConrad Meyer 
293fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
294e974f91cSConrad Meyer 	callout_drain(&ioat->timer);
295e974f91cSConrad Meyer 
296e974f91cSConrad Meyer 	pci_disable_busmaster(device);
297e974f91cSConrad Meyer 
298e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
299e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
300e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
301e974f91cSConrad Meyer 
302bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
303bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
304e974f91cSConrad Meyer 
305e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
306e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
307e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
308e974f91cSConrad Meyer 		    ioat->comp_update_map);
309e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
310e974f91cSConrad Meyer 	}
311e974f91cSConrad Meyer 
312e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
313e974f91cSConrad Meyer 
3144253ea50SConrad Meyer 	return (0);
3154253ea50SConrad Meyer }
3164253ea50SConrad Meyer 
3174253ea50SConrad Meyer static int
3184253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3194253ea50SConrad Meyer {
3204253ea50SConrad Meyer 
321e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3224253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
323e974f91cSConrad Meyer 
324e974f91cSConrad Meyer 	if (ioat->res != NULL)
3254253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
326e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
327e974f91cSConrad Meyer 
3284253ea50SConrad Meyer 	pci_release_msi(ioat->device);
329e974f91cSConrad Meyer 	return (0);
330e974f91cSConrad Meyer }
331e974f91cSConrad Meyer 
332e974f91cSConrad Meyer static int
333cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
334e974f91cSConrad Meyer {
335e974f91cSConrad Meyer 	uint64_t status;
336e974f91cSConrad Meyer 	uint32_t chanerr;
337e974f91cSConrad Meyer 	int i;
338e974f91cSConrad Meyer 
339e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
340e974f91cSConrad Meyer 	ioat_null(&ioat->dmaengine, NULL, NULL, 0);
341e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
342e974f91cSConrad Meyer 
343e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
344e974f91cSConrad Meyer 		DELAY(1);
345e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
346e974f91cSConrad Meyer 		if (is_ioat_idle(status))
347e974f91cSConrad Meyer 			return (0);
348e974f91cSConrad Meyer 	}
349e974f91cSConrad Meyer 
350e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
351e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
35259acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
35359acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
354e974f91cSConrad Meyer 	return (ENXIO);
355e974f91cSConrad Meyer }
356e974f91cSConrad Meyer 
357e974f91cSConrad Meyer /*
358e974f91cSConrad Meyer  * Initialize Hardware
359e974f91cSConrad Meyer  */
360e974f91cSConrad Meyer static int
361e974f91cSConrad Meyer ioat3_attach(device_t device)
362e974f91cSConrad Meyer {
363e974f91cSConrad Meyer 	struct ioat_softc *ioat;
364e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
365e974f91cSConrad Meyer 	struct ioat_descriptor *next;
366e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
367e974f91cSConrad Meyer 	int i, num_descriptors;
368e974f91cSConrad Meyer 	int error;
369e974f91cSConrad Meyer 	uint8_t xfercap;
370e974f91cSConrad Meyer 
371e974f91cSConrad Meyer 	error = 0;
372e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
373*1693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
374*1693d27bSConrad Meyer 
375*1693d27bSConrad Meyer 	ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities,
376*1693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
377e974f91cSConrad Meyer 
378e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
379e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
380e974f91cSConrad Meyer 
381e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
382e974f91cSConrad Meyer 
383e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
384e974f91cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_process_events", NULL, MTX_DEF);
3857afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
386e974f91cSConrad Meyer 
387e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
388e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
389e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
390e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
391e974f91cSConrad Meyer 
392e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
393e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
394e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
395e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
396e974f91cSConrad Meyer 
397e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
398e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
399e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
400e974f91cSConrad Meyer 		return (ENOMEM);
401e974f91cSConrad Meyer 
402e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
403e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
404e974f91cSConrad Meyer 	    0);
405e974f91cSConrad Meyer 	if (error != 0)
406e974f91cSConrad Meyer 		return (error);
407e974f91cSConrad Meyer 
408e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
409e974f91cSConrad Meyer 
410e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
411e974f91cSConrad Meyer 
412e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
413e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
414e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
415e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
416e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
417e974f91cSConrad Meyer 
418e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
419bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
420e974f91cSConrad Meyer 	if (ioat->ring == NULL)
421e974f91cSConrad Meyer 		return (ENOMEM);
422e974f91cSConrad Meyer 
423e974f91cSConrad Meyer 	ring = ioat->ring;
424e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
425bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
426e974f91cSConrad Meyer 		if (ring[i] == NULL)
427e974f91cSConrad Meyer 			return (ENOMEM);
428e974f91cSConrad Meyer 
429e974f91cSConrad Meyer 		ring[i]->id = i;
430e974f91cSConrad Meyer 	}
431e974f91cSConrad Meyer 
432e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
433e974f91cSConrad Meyer 		next = ring[i + 1];
434e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
435e974f91cSConrad Meyer 
436e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
437e974f91cSConrad Meyer 	}
438e974f91cSConrad Meyer 
439e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
440e974f91cSConrad Meyer 
441bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
442e974f91cSConrad Meyer 	ioat->tail = 0;
443e974f91cSConrad Meyer 	ioat->last_seen = 0;
444e974f91cSConrad Meyer 	return (0);
445e974f91cSConrad Meyer }
446e974f91cSConrad Meyer 
447e974f91cSConrad Meyer static int
448e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
449e974f91cSConrad Meyer {
450e974f91cSConrad Meyer 
451e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
452e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
453e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
454e974f91cSConrad Meyer 
455e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
456e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
457e974f91cSConrad Meyer 		return (ENODEV);
458e974f91cSConrad Meyer 	}
459e974f91cSConrad Meyer 
460e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
461e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
462e974f91cSConrad Meyer 	return (0);
463e974f91cSConrad Meyer }
464e974f91cSConrad Meyer 
465e974f91cSConrad Meyer static void
466e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
467e974f91cSConrad Meyer {
468e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
469e974f91cSConrad Meyer 
470cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
471e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
472e974f91cSConrad Meyer }
473e974f91cSConrad Meyer 
474e974f91cSConrad Meyer static void
475e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
476e974f91cSConrad Meyer {
477e974f91cSConrad Meyer 	bus_addr_t *baddr;
478e974f91cSConrad Meyer 
479cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
480e974f91cSConrad Meyer 	baddr = arg;
481e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
482e974f91cSConrad Meyer }
483e974f91cSConrad Meyer 
484e974f91cSConrad Meyer /*
485e974f91cSConrad Meyer  * Interrupt setup and handlers
486e974f91cSConrad Meyer  */
487e974f91cSConrad Meyer static int
4884253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
489e974f91cSConrad Meyer {
490e974f91cSConrad Meyer 	uint32_t num_vectors;
491e974f91cSConrad Meyer 	int error;
492e974f91cSConrad Meyer 	boolean_t use_msix;
493e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
494e974f91cSConrad Meyer 
495e974f91cSConrad Meyer 	use_msix = FALSE;
496e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
497e974f91cSConrad Meyer 
498e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
499e974f91cSConrad Meyer 		num_vectors = 1;
500e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
501e974f91cSConrad Meyer 		if (num_vectors == 1)
502e974f91cSConrad Meyer 			use_msix = TRUE;
503e974f91cSConrad Meyer 	}
504e974f91cSConrad Meyer 
505e974f91cSConrad Meyer 	if (use_msix) {
506e974f91cSConrad Meyer 		ioat->rid = 1;
507e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
508e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
509e974f91cSConrad Meyer 	} else {
510e974f91cSConrad Meyer 		ioat->rid = 0;
511e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
512e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
513e974f91cSConrad Meyer 	}
514e974f91cSConrad Meyer 	if (ioat->res == NULL) {
515e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
516e974f91cSConrad Meyer 		return (ENOMEM);
517e974f91cSConrad Meyer 	}
518e974f91cSConrad Meyer 
519e974f91cSConrad Meyer 	ioat->tag = NULL;
520e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
521e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
522e974f91cSConrad Meyer 	if (error != 0) {
523e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
524e974f91cSConrad Meyer 		return (error);
525e974f91cSConrad Meyer 	}
526e974f91cSConrad Meyer 
527e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
528e974f91cSConrad Meyer 	return (0);
529e974f91cSConrad Meyer }
530e974f91cSConrad Meyer 
5314253ea50SConrad Meyer static boolean_t
5320d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5334253ea50SConrad Meyer {
5344253ea50SConrad Meyer 	u_int32_t pciid;
5354253ea50SConrad Meyer 
5364253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5374253ea50SConrad Meyer 	switch (pciid) {
5380d1a05d9SConrad Meyer 		/* BWD: */
5390d1a05d9SConrad Meyer 	case 0x0c508086:
5400d1a05d9SConrad Meyer 	case 0x0c518086:
5410d1a05d9SConrad Meyer 	case 0x0c528086:
5420d1a05d9SConrad Meyer 	case 0x0c538086:
5430d1a05d9SConrad Meyer 		/* BDXDE: */
5444253ea50SConrad Meyer 	case 0x6f508086:
5454253ea50SConrad Meyer 	case 0x6f518086:
5464253ea50SConrad Meyer 	case 0x6f528086:
5474253ea50SConrad Meyer 	case 0x6f538086:
5484253ea50SConrad Meyer 		return (TRUE);
5494253ea50SConrad Meyer 	}
5504253ea50SConrad Meyer 
5514253ea50SConrad Meyer 	return (FALSE);
5524253ea50SConrad Meyer }
5534253ea50SConrad Meyer 
554e974f91cSConrad Meyer static void
555e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
556e974f91cSConrad Meyer {
557e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
558e974f91cSConrad Meyer 
559e974f91cSConrad Meyer 	ioat_process_events(ioat);
560e974f91cSConrad Meyer }
561e974f91cSConrad Meyer 
562e974f91cSConrad Meyer static void
563e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
564e974f91cSConrad Meyer {
565e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
566e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
567e974f91cSConrad Meyer 	uint64_t comp_update, status;
568e974f91cSConrad Meyer 	uint32_t completed;
569e974f91cSConrad Meyer 
570e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
571e974f91cSConrad Meyer 
572e974f91cSConrad Meyer 	completed = 0;
573e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
574e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
575e974f91cSConrad Meyer 
57643fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
577e974f91cSConrad Meyer 
5784becebdfSConrad Meyer 	if (status == ioat->last_seen)
5794becebdfSConrad Meyer 		goto out;
580e974f91cSConrad Meyer 
581e974f91cSConrad Meyer 	while (1) {
582e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
583e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
58443fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
585e974f91cSConrad Meyer 
586e974f91cSConrad Meyer 		if (dmadesc->callback_fn)
587e974f91cSConrad Meyer 			(*dmadesc->callback_fn)(dmadesc->callback_arg);
588e974f91cSConrad Meyer 
589466b3540SConrad Meyer 		completed++;
590e974f91cSConrad Meyer 		ioat->tail++;
591e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
592e974f91cSConrad Meyer 			break;
593e974f91cSConrad Meyer 	}
594e974f91cSConrad Meyer 
595e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
596e974f91cSConrad Meyer 
597e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
598e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
599fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
600fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
601e974f91cSConrad Meyer 	}
602e974f91cSConrad Meyer 
6034becebdfSConrad Meyer out:
604e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
605e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
606466b3540SConrad Meyer 
607466b3540SConrad Meyer 	ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
608bf8553eaSConrad Meyer 	wakeup(&ioat->tail);
609e974f91cSConrad Meyer }
610e974f91cSConrad Meyer 
611e974f91cSConrad Meyer /*
612e974f91cSConrad Meyer  * User API functions
613e974f91cSConrad Meyer  */
614e974f91cSConrad Meyer bus_dmaengine_t
615e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
616e974f91cSConrad Meyer {
617e974f91cSConrad Meyer 
618466b3540SConrad Meyer 	if (index >= ioat_channel_index)
619e974f91cSConrad Meyer 		return (NULL);
620466b3540SConrad Meyer 	return (&ioat_get(ioat_channel[index], IOAT_DMAENGINE_REF)->dmaengine);
621466b3540SConrad Meyer }
622466b3540SConrad Meyer 
623466b3540SConrad Meyer void
624466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
625466b3540SConrad Meyer {
626466b3540SConrad Meyer 	struct ioat_softc *ioat;
627466b3540SConrad Meyer 
628466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
629466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
630e974f91cSConrad Meyer }
631e974f91cSConrad Meyer 
632e974f91cSConrad Meyer void
633e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
634e974f91cSConrad Meyer {
635e974f91cSConrad Meyer 	struct ioat_softc *ioat;
636e974f91cSConrad Meyer 
637e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
638e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
63943fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
640e974f91cSConrad Meyer }
641e974f91cSConrad Meyer 
642e974f91cSConrad Meyer void
643e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
644e974f91cSConrad Meyer {
645e974f91cSConrad Meyer 	struct ioat_softc *ioat;
646e974f91cSConrad Meyer 
647e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
64843fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
649bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
650e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
651e974f91cSConrad Meyer }
652e974f91cSConrad Meyer 
6539e3bbf26SConrad Meyer static struct ioat_descriptor *
6549e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
6559e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
6569e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
6579e3bbf26SConrad Meyer     uint32_t flags)
658e974f91cSConrad Meyer {
6599e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
660e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
661bf8553eaSConrad Meyer 	int mflags;
662e974f91cSConrad Meyer 
6639e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
6649e3bbf26SConrad Meyer 
665e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
666e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
667bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
668bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
669bf8553eaSConrad Meyer 	else
670bf8553eaSConrad Meyer 		mflags = M_WAITOK;
671e974f91cSConrad Meyer 
6729e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
6739e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
6749e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
6759e3bbf26SConrad Meyer 		return (NULL);
6769e3bbf26SConrad Meyer 	}
677e974f91cSConrad Meyer 
678bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
679e974f91cSConrad Meyer 		return (NULL);
680e974f91cSConrad Meyer 
681e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
6829e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
683e974f91cSConrad Meyer 
684e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
6859e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
6869e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
687e974f91cSConrad Meyer 
688e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
6899e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
690e974f91cSConrad Meyer 
6919e3bbf26SConrad Meyer 	hw_desc->size = size;
6929e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
6939e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
694e974f91cSConrad Meyer 
695e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
696e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
6979e3bbf26SConrad Meyer 	return (desc);
6989e3bbf26SConrad Meyer }
699e974f91cSConrad Meyer 
7009e3bbf26SConrad Meyer struct bus_dmadesc *
7019e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
7029e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
7039e3bbf26SConrad Meyer {
7049e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
7059e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
7069e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
7079e3bbf26SConrad Meyer 
7089e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
7099e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7109e3bbf26SConrad Meyer 
7119e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
7129e3bbf26SConrad Meyer 	    callback_arg, flags);
7139e3bbf26SConrad Meyer 	if (desc == NULL)
7149e3bbf26SConrad Meyer 		return (NULL);
7159e3bbf26SConrad Meyer 
7169e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
7179e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
718e974f91cSConrad Meyer 	ioat_submit_single(ioat);
719e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
720e974f91cSConrad Meyer }
721e974f91cSConrad Meyer 
722e974f91cSConrad Meyer struct bus_dmadesc *
723e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
724e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
725e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
726e974f91cSConrad Meyer {
727e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
7289e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
729e974f91cSConrad Meyer 	struct ioat_softc *ioat;
730e974f91cSConrad Meyer 
7319e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
732e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
733e974f91cSConrad Meyer 
7349e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
7359e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
7369e3bbf26SConrad Meyer 		    __func__);
737e974f91cSConrad Meyer 		return (NULL);
738e974f91cSConrad Meyer 	}
739e974f91cSConrad Meyer 
7409e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
7419e3bbf26SConrad Meyer 	    callback_arg, flags);
7429e3bbf26SConrad Meyer 	if (desc == NULL)
743e974f91cSConrad Meyer 		return (NULL);
744e974f91cSConrad Meyer 
745e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
746e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
747e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
748e974f91cSConrad Meyer 
749e974f91cSConrad Meyer 	ioat_submit_single(ioat);
750e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
751e974f91cSConrad Meyer }
752e974f91cSConrad Meyer 
7532a4fd6b1SConrad Meyer struct bus_dmadesc *
7542a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
7552a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
7562a4fd6b1SConrad Meyer     uint32_t flags)
7572a4fd6b1SConrad Meyer {
7582a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
7592a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
7602a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
7612a4fd6b1SConrad Meyer 
7622a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
7632a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7642a4fd6b1SConrad Meyer 
765*1693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
766*1693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
767*1693d27bSConrad Meyer 		    __func__);
768*1693d27bSConrad Meyer 		return (NULL);
769*1693d27bSConrad Meyer 	}
770*1693d27bSConrad Meyer 
7712a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
7722a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
7732a4fd6b1SConrad Meyer 		    __func__);
7742a4fd6b1SConrad Meyer 		return (NULL);
7752a4fd6b1SConrad Meyer 	}
7762a4fd6b1SConrad Meyer 
7772a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
7782a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
7792a4fd6b1SConrad Meyer 	if (desc == NULL)
7802a4fd6b1SConrad Meyer 		return (NULL);
7812a4fd6b1SConrad Meyer 
7822a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
7832a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
7842a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
7852a4fd6b1SConrad Meyer 
7862a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
7872a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
7882a4fd6b1SConrad Meyer }
7892a4fd6b1SConrad Meyer 
790e974f91cSConrad Meyer /*
791e974f91cSConrad Meyer  * Ring Management
792e974f91cSConrad Meyer  */
793e974f91cSConrad Meyer static inline uint32_t
794e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
795e974f91cSConrad Meyer {
796e974f91cSConrad Meyer 
797e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
798e974f91cSConrad Meyer }
799e974f91cSConrad Meyer 
800e974f91cSConrad Meyer static inline uint32_t
801e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
802e974f91cSConrad Meyer {
803e974f91cSConrad Meyer 
804e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
805e974f91cSConrad Meyer }
806e974f91cSConrad Meyer 
807e974f91cSConrad Meyer static struct ioat_descriptor *
808bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
809e974f91cSConrad Meyer {
8109e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
811e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
812bf8553eaSConrad Meyer 	int error, busdmaflag;
813e974f91cSConrad Meyer 
814f46011aeSConrad Meyer 	error = ENOMEM;
815f46011aeSConrad Meyer 	hw_desc = NULL;
816f46011aeSConrad Meyer 
817bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
818bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
819bf8553eaSConrad Meyer 	else
820bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
821bf8553eaSConrad Meyer 
822bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
823e974f91cSConrad Meyer 	if (desc == NULL)
824f46011aeSConrad Meyer 		goto out;
825e974f91cSConrad Meyer 
826f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
827bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
828f46011aeSConrad Meyer 	if (hw_desc == NULL)
829f46011aeSConrad Meyer 		goto out;
830e974f91cSConrad Meyer 
8319e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
832f46011aeSConrad Meyer 
833f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
834f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
835bf8553eaSConrad Meyer 	    busdmaflag);
836f46011aeSConrad Meyer 	if (error)
837f46011aeSConrad Meyer 		goto out;
838f46011aeSConrad Meyer 
839f46011aeSConrad Meyer out:
840f46011aeSConrad Meyer 	if (error) {
841f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
842f46011aeSConrad Meyer 		return (NULL);
843f46011aeSConrad Meyer 	}
844e974f91cSConrad Meyer 	return (desc);
845e974f91cSConrad Meyer }
846e974f91cSConrad Meyer 
847e974f91cSConrad Meyer static void
848e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
849e974f91cSConrad Meyer {
850e974f91cSConrad Meyer 
851e974f91cSConrad Meyer 	if (desc == NULL)
852e974f91cSConrad Meyer 		return;
853e974f91cSConrad Meyer 
8549e3bbf26SConrad Meyer 	if (desc->u.generic)
8559e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
856e974f91cSConrad Meyer 		    ioat->hw_desc_map);
857e974f91cSConrad Meyer 	free(desc, M_IOAT);
858e974f91cSConrad Meyer }
859e974f91cSConrad Meyer 
860bf8553eaSConrad Meyer /*
861bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
862bf8553eaSConrad Meyer  * for 'num_descs'.
863bf8553eaSConrad Meyer  *
864bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
865bf8553eaSConrad Meyer  *
866bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
867bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
868bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
869bf8553eaSConrad Meyer  *
870bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
871bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
872bf8553eaSConrad Meyer  *
873bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
874bf8553eaSConrad Meyer  * assured enough room is available.)
875bf8553eaSConrad Meyer  */
876e974f91cSConrad Meyer static int
877bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
878e974f91cSConrad Meyer {
879bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
880bf8553eaSConrad Meyer 	uint32_t order;
881bf8553eaSConrad Meyer 	int error;
882e974f91cSConrad Meyer 
883bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
884bf8553eaSConrad Meyer 	error = 0;
885e974f91cSConrad Meyer 
886bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
887bf8553eaSConrad Meyer 		error = EINVAL;
888bf8553eaSConrad Meyer 		goto out;
889e974f91cSConrad Meyer 	}
890bf8553eaSConrad Meyer 
891bf8553eaSConrad Meyer 	for (;;) {
892bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
893bf8553eaSConrad Meyer 			goto out;
894bf8553eaSConrad Meyer 
895bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
896bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
897bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
898bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
899bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
900bf8553eaSConrad Meyer 				continue;
901bf8553eaSConrad Meyer 			}
902bf8553eaSConrad Meyer 
903bf8553eaSConrad Meyer 			error = EAGAIN;
904bf8553eaSConrad Meyer 			break;
905bf8553eaSConrad Meyer 		}
906bf8553eaSConrad Meyer 
907bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
908bf8553eaSConrad Meyer 		for (;;) {
909bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
910bf8553eaSConrad Meyer 
911bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
912bf8553eaSConrad Meyer 			    TRUE, mflags);
913bf8553eaSConrad Meyer 
914bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
915bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
916bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
917bf8553eaSConrad Meyer 
918bf8553eaSConrad Meyer 			if (new_ring == NULL) {
919bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
920bf8553eaSConrad Meyer 				    ("allocation failed"));
921bf8553eaSConrad Meyer 				error = EAGAIN;
922bf8553eaSConrad Meyer 				break;
923bf8553eaSConrad Meyer 			}
924bf8553eaSConrad Meyer 
925bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
926bf8553eaSConrad Meyer 			if (error == 0)
927bf8553eaSConrad Meyer 				break;
928bf8553eaSConrad Meyer 		}
929bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
930bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
931bf8553eaSConrad Meyer 		if (error)
932bf8553eaSConrad Meyer 			break;
933bf8553eaSConrad Meyer 	}
934bf8553eaSConrad Meyer 
935bf8553eaSConrad Meyer out:
936bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
937bf8553eaSConrad Meyer 	return (error);
938bf8553eaSConrad Meyer }
939bf8553eaSConrad Meyer 
940bf8553eaSConrad Meyer static struct ioat_descriptor **
941bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
942bf8553eaSConrad Meyer     int mflags)
943bf8553eaSConrad Meyer {
944bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
945bf8553eaSConrad Meyer 	uint32_t i;
946bf8553eaSConrad Meyer 	int error;
947bf8553eaSConrad Meyer 
948bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
949bf8553eaSConrad Meyer 
950bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
951bf8553eaSConrad Meyer 	if (ring == NULL)
952bf8553eaSConrad Meyer 		return (NULL);
953bf8553eaSConrad Meyer 
954bf8553eaSConrad Meyer 	if (need_dscr) {
955bf8553eaSConrad Meyer 		error = ENOMEM;
956bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
957bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
958bf8553eaSConrad Meyer 			if (ring[i] == NULL)
959bf8553eaSConrad Meyer 				goto out;
960bf8553eaSConrad Meyer 			ring[i]->id = i;
961bf8553eaSConrad Meyer 		}
962bf8553eaSConrad Meyer 	}
963bf8553eaSConrad Meyer 	error = 0;
964bf8553eaSConrad Meyer 
965bf8553eaSConrad Meyer out:
966bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
967bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
968bf8553eaSConrad Meyer 		ring = NULL;
969bf8553eaSConrad Meyer 	}
970bf8553eaSConrad Meyer 	return (ring);
971bf8553eaSConrad Meyer }
972bf8553eaSConrad Meyer 
973bf8553eaSConrad Meyer static void
974bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
975bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
976bf8553eaSConrad Meyer {
977bf8553eaSConrad Meyer 	uint32_t i;
978bf8553eaSConrad Meyer 
979bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
980bf8553eaSConrad Meyer 		if (ring[i] != NULL)
981bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
982bf8553eaSConrad Meyer 	}
983bf8553eaSConrad Meyer 	free(ring, M_IOAT);
984e974f91cSConrad Meyer }
985e974f91cSConrad Meyer 
986e974f91cSConrad Meyer static struct ioat_descriptor *
987e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
988e974f91cSConrad Meyer {
989e974f91cSConrad Meyer 
990e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
991e974f91cSConrad Meyer }
992e974f91cSConrad Meyer 
993bf8553eaSConrad Meyer static int
994bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
995bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
996e974f91cSConrad Meyer {
997bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
998e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
999bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1000bf8553eaSConrad Meyer 	int error;
1001e974f91cSConrad Meyer 
1002bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1003e974f91cSConrad Meyer 
1004bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1005bf8553eaSConrad Meyer 
1006bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1007bf8553eaSConrad Meyer 		error = EINVAL;
1008bf8553eaSConrad Meyer 		goto out;
1009bf8553eaSConrad Meyer 	}
1010bf8553eaSConrad Meyer 
1011bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1012bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1013bf8553eaSConrad Meyer 
1014bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1015bf8553eaSConrad Meyer 
1016bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1017bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1018bf8553eaSConrad Meyer 
1019bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1020bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1021bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1022e974f91cSConrad Meyer 
1023e974f91cSConrad Meyer 	/*
1024bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1025bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1026e974f91cSConrad Meyer 	 */
1027bf8553eaSConrad Meyer 	if (head < tail) {
1028bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1029bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1030e974f91cSConrad Meyer 
1031bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1032bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1033e974f91cSConrad Meyer 
1034bf8553eaSConrad Meyer 			newring[i] = tmp;
1035bf8553eaSConrad Meyer 			newring[i]->id = i;
1036bf8553eaSConrad Meyer 		}
1037bf8553eaSConrad Meyer 		head += oldsize;
1038e974f91cSConrad Meyer 	}
1039e974f91cSConrad Meyer 
1040bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1041e974f91cSConrad Meyer 
1042bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1043bf8553eaSConrad Meyer 	if (head < oldsize) {
1044bf8553eaSConrad Meyer 		i = oldsize - 1;
1045bf8553eaSConrad Meyer 		end = newsize;
1046e974f91cSConrad Meyer 	} else {
1047bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1048bf8553eaSConrad Meyer 		i = head;
1049bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1050bf8553eaSConrad Meyer 	}
1051bf8553eaSConrad Meyer 
1052e974f91cSConrad Meyer 	/*
1053bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1054bf8553eaSConrad Meyer 	 * section (tail -> head).
1055e974f91cSConrad Meyer 	 */
1056bf8553eaSConrad Meyer 	for (; i < end; i++) {
1057bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1058bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1059e974f91cSConrad Meyer 
1060bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1061bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1062e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1063e974f91cSConrad Meyer 	}
1064e974f91cSConrad Meyer 
1065e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1066bf8553eaSConrad Meyer 	ioat->ring = newring;
1067bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1068bf8553eaSConrad Meyer 	ioat->tail = tail;
1069bf8553eaSConrad Meyer 	ioat->head = head;
1070bf8553eaSConrad Meyer 	error = 0;
1071e974f91cSConrad Meyer 
1072bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1073bf8553eaSConrad Meyer out:
1074bf8553eaSConrad Meyer 	if (error)
1075bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1076bf8553eaSConrad Meyer 	return (error);
1077bf8553eaSConrad Meyer }
1078bf8553eaSConrad Meyer 
1079bf8553eaSConrad Meyer static int
1080bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1081bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1082bf8553eaSConrad Meyer {
1083bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1084bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1085bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1086bf8553eaSConrad Meyer 	int error;
1087bf8553eaSConrad Meyer 
1088bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1089bf8553eaSConrad Meyer 
1090bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1091bf8553eaSConrad Meyer 
1092bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1093bf8553eaSConrad Meyer 		error = EINVAL;
1094bf8553eaSConrad Meyer 		goto out_unlocked;
1095bf8553eaSConrad Meyer 	}
1096bf8553eaSConrad Meyer 
1097bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1098bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1099bf8553eaSConrad Meyer 
1100bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1101bf8553eaSConrad Meyer 
1102bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1103bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1104bf8553eaSConrad Meyer 		error = ENOMEM;
1105bf8553eaSConrad Meyer 		goto out;
1106bf8553eaSConrad Meyer 	}
1107bf8553eaSConrad Meyer 
1108bf8553eaSConrad Meyer 	/*
1109bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1110bf8553eaSConrad Meyer 	 * descriptors.
1111bf8553eaSConrad Meyer 	 */
1112bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1113bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1114bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1115bf8553eaSConrad Meyer 
1116bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1117bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1118bf8553eaSConrad Meyer 	}
1119bf8553eaSConrad Meyer 
1120bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1121bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1122bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1123bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1124bf8553eaSConrad Meyer 	}
1125bf8553eaSConrad Meyer 
1126bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1127bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1128bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1129bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1130bf8553eaSConrad Meyer 
1131bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1132bf8553eaSConrad Meyer 	ioat->ring = newring;
1133bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1134bf8553eaSConrad Meyer 	error = 0;
1135bf8553eaSConrad Meyer 
1136bf8553eaSConrad Meyer out:
1137bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1138bf8553eaSConrad Meyer out_unlocked:
1139bf8553eaSConrad Meyer 	if (error)
1140bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1141bf8553eaSConrad Meyer 	return (error);
1142e974f91cSConrad Meyer }
1143e974f91cSConrad Meyer 
1144e974f91cSConrad Meyer static void
11458f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1146e974f91cSConrad Meyer {
1147e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
11488f274637SConrad Meyer 
114959acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
115059acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
11518f274637SConrad Meyer 	if (chanerr == 0)
11528f274637SConrad Meyer 		return;
11538f274637SConrad Meyer 
1154bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
11558f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
11568f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
11578f274637SConrad Meyer 
11588f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
11598f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
1160bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
11618f274637SConrad Meyer }
11628f274637SConrad Meyer 
11638f274637SConrad Meyer static void
11648f274637SConrad Meyer ioat_timer_callback(void *arg)
11658f274637SConrad Meyer {
1166bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1167e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1168e974f91cSConrad Meyer 	uint64_t status;
1169bf8553eaSConrad Meyer 	uint32_t chanerr, order;
1170e974f91cSConrad Meyer 
1171e974f91cSConrad Meyer 	ioat = arg;
1172fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1173e974f91cSConrad Meyer 
1174e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1175e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1176e974f91cSConrad Meyer 
1177e974f91cSConrad Meyer 		/*
1178e974f91cSConrad Meyer 		 * When halted due to errors, check for channel programming
1179e974f91cSConrad Meyer 		 * errors before advancing the completion state.
1180e974f91cSConrad Meyer 		 */
1181e974f91cSConrad Meyer 		if (is_ioat_halted(status)) {
1182e974f91cSConrad Meyer 			chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
11838f274637SConrad Meyer 			ioat_halted_debug(ioat, chanerr);
1184e974f91cSConrad Meyer 		}
1185e974f91cSConrad Meyer 		ioat_process_events(ioat);
1186e974f91cSConrad Meyer 	} else {
1187e974f91cSConrad Meyer 		mtx_lock(&ioat->submit_lock);
1188bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1189bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1190bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1191bf8553eaSConrad Meyer 			goto out;
1192bf8553eaSConrad Meyer 		}
1193bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1194e974f91cSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1195e974f91cSConrad Meyer 
1196bf8553eaSConrad Meyer 		newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1197bf8553eaSConrad Meyer 		    M_NOWAIT);
1198bf8553eaSConrad Meyer 
1199bf8553eaSConrad Meyer 		mtx_lock(&ioat->submit_lock);
1200bf8553eaSConrad Meyer 		KASSERT(ioat->ring_size_order == order,
1201bf8553eaSConrad Meyer 		    ("resize_pending protects order"));
1202bf8553eaSConrad Meyer 
1203bf8553eaSConrad Meyer 		if (newring != NULL)
1204bf8553eaSConrad Meyer 			ring_shrink(ioat, order, newring);
1205bf8553eaSConrad Meyer 
1206bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1207bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1208bf8553eaSConrad Meyer 
1209bf8553eaSConrad Meyer out:
1210bf8553eaSConrad Meyer 		/* Slowly scale the ring down if idle. */
1211e974f91cSConrad Meyer 		if (ioat->ring_size_order > IOAT_MIN_ORDER)
1212bf8553eaSConrad Meyer 			callout_reset(&ioat->timer, 10 * hz,
1213e974f91cSConrad Meyer 			    ioat_timer_callback, ioat);
1214e974f91cSConrad Meyer 	}
1215e974f91cSConrad Meyer }
1216e974f91cSConrad Meyer 
1217e974f91cSConrad Meyer /*
1218e974f91cSConrad Meyer  * Support Functions
1219e974f91cSConrad Meyer  */
1220e974f91cSConrad Meyer static void
1221e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1222e974f91cSConrad Meyer {
1223e974f91cSConrad Meyer 
1224466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1225e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1226bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1227e974f91cSConrad Meyer 
1228e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1229e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1230fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1231fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1232e974f91cSConrad Meyer 	}
1233e974f91cSConrad Meyer }
1234e974f91cSConrad Meyer 
1235e974f91cSConrad Meyer static int
1236e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1237e974f91cSConrad Meyer {
1238e974f91cSConrad Meyer 	uint64_t status;
1239e974f91cSConrad Meyer 	uint32_t chanerr;
1240cea5b880SConrad Meyer 	unsigned timeout;
1241e974f91cSConrad Meyer 
1242e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1243e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1244e974f91cSConrad Meyer 		ioat_suspend(ioat);
1245e974f91cSConrad Meyer 
1246e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1247e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1248e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1249e974f91cSConrad Meyer 		DELAY(1000);
1250e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1251e974f91cSConrad Meyer 	}
1252e974f91cSConrad Meyer 	if (timeout == 20)
1253e974f91cSConrad Meyer 		return (ETIMEDOUT);
1254e974f91cSConrad Meyer 
1255cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1256cea5b880SConrad Meyer 
1257e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1258e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1259e974f91cSConrad Meyer 
1260e974f91cSConrad Meyer 	/*
1261e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1262e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1263e974f91cSConrad Meyer 	 */
1264e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1265e974f91cSConrad Meyer 	    4);
1266e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1267e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1268e974f91cSConrad Meyer 
12690d1a05d9SConrad Meyer 	/*
12700d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
12710d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
12720d1a05d9SConrad Meyer 	 */
1273f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1274f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
12750d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1276f7157235SConrad Meyer 	}
12770d1a05d9SConrad Meyer 
1278e974f91cSConrad Meyer 	ioat_reset(ioat);
1279e974f91cSConrad Meyer 
1280e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1281e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1282e974f91cSConrad Meyer 		DELAY(1000);
1283e974f91cSConrad Meyer 	if (timeout == 20)
1284e974f91cSConrad Meyer 		return (ETIMEDOUT);
1285e974f91cSConrad Meyer 
1286f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1287f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
12880d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1289f7157235SConrad Meyer 	}
12904253ea50SConrad Meyer 
1291cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1292cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1293cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1294cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1295cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1296cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1297e974f91cSConrad Meyer 		return (0);
1298e974f91cSConrad Meyer 	}
1299e974f91cSConrad Meyer 
1300cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1301cea5b880SConrad Meyer 	ioat_halted_debug(ioat, chanerr);
1302cea5b880SConrad Meyer 	if (chanerr != 0)
1303cea5b880SConrad Meyer 		return (EIO);
1304cea5b880SConrad Meyer 
1305cea5b880SConrad Meyer 	/*
1306cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1307cea5b880SConrad Meyer 	 * device back to active.
1308cea5b880SConrad Meyer 	 *
1309cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1310cea5b880SConrad Meyer 	 * at zero as well.
1311cea5b880SConrad Meyer 	 */
1312bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1313cea5b880SConrad Meyer 	ioat->last_seen = 0;
1314cea5b880SConrad Meyer 
1315cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1316cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1317cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
1318cea5b880SConrad Meyer 	return (ioat_start_channel(ioat));
1319cea5b880SConrad Meyer }
1320cea5b880SConrad Meyer 
1321f7157235SConrad Meyer static int
1322f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1323f7157235SConrad Meyer {
1324f7157235SConrad Meyer 	struct ioat_softc *ioat;
1325f7157235SConrad Meyer 	int error, arg;
1326f7157235SConrad Meyer 
1327f7157235SConrad Meyer 	ioat = arg1;
1328f7157235SConrad Meyer 
1329f7157235SConrad Meyer 	arg = 0;
1330f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1331f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1332f7157235SConrad Meyer 		return (error);
1333f7157235SConrad Meyer 
1334f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1335f7157235SConrad Meyer 	if (error != 0)
1336f7157235SConrad Meyer 		return (error);
1337f7157235SConrad Meyer 
1338f7157235SConrad Meyer 	if (arg != 0)
1339f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1340f7157235SConrad Meyer 
1341f7157235SConrad Meyer 	return (error);
1342f7157235SConrad Meyer }
1343f7157235SConrad Meyer 
1344e974f91cSConrad Meyer static void
1345e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1346e974f91cSConrad Meyer {
1347e974f91cSConrad Meyer 	int i, j;
1348e974f91cSConrad Meyer 
1349e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1350e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1351e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1352e974f91cSConrad Meyer 		printf("\n");
1353e974f91cSConrad Meyer 	}
1354e974f91cSConrad Meyer }
1355e974f91cSConrad Meyer 
1356e974f91cSConrad Meyer static void
1357e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1358e974f91cSConrad Meyer {
1359f7157235SConrad Meyer 	struct sysctl_oid_list *par;
1360f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
1361f7157235SConrad Meyer 	struct sysctl_oid *tree;
1362e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1363e974f91cSConrad Meyer 
1364e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1365f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1366f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1367f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1368e974f91cSConrad Meyer 
136965e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
137065e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
137165e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
137265e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
137365e4f8adSConrad Meyer 
1374f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1375bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
1376f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 0,
1377bf8553eaSConrad Meyer 	    "SW descriptor head pointer index");
1378f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 0,
1379bf8553eaSConrad Meyer 	    "SW descriptor tail pointer index");
1380bf8553eaSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "hw_head", CTLFLAG_RD,
1381bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1382f7157235SConrad Meyer 
138365e4f8adSConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, par, OID_AUTO, "last_completion", CTLFLAG_RD,
138465e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
138565e4f8adSConrad Meyer 
138665e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
138765e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
138865e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_completion_pending", CTLFLAG_RD,
138965e4f8adSConrad Meyer 	    &ioat->is_completion_pending, 0, "completion pending");
139065e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
139165e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
139265e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_channel_running", CTLFLAG_RD,
139365e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
139465e4f8adSConrad Meyer 
1395f7157235SConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "force_hw_reset",
1396f7157235SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1397f7157235SConrad Meyer 	    "Set to non-zero to reset the hardware");
1398e974f91cSConrad Meyer }
1399466b3540SConrad Meyer 
1400466b3540SConrad Meyer static inline struct ioat_softc *
1401466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1402466b3540SConrad Meyer {
1403466b3540SConrad Meyer 	uint32_t old;
1404466b3540SConrad Meyer 
1405466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1406466b3540SConrad Meyer 
1407466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1408466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1409466b3540SConrad Meyer 
1410466b3540SConrad Meyer #ifdef INVARIANTS
1411466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1412466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1413466b3540SConrad Meyer #endif
1414466b3540SConrad Meyer 
1415466b3540SConrad Meyer 	return (ioat);
1416466b3540SConrad Meyer }
1417466b3540SConrad Meyer 
1418466b3540SConrad Meyer static inline void
1419466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1420466b3540SConrad Meyer {
1421466b3540SConrad Meyer 	uint32_t old;
1422466b3540SConrad Meyer 
1423466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1424466b3540SConrad Meyer 
1425466b3540SConrad Meyer 	if (n == 0)
1426466b3540SConrad Meyer 		return;
1427466b3540SConrad Meyer 
1428466b3540SConrad Meyer #ifdef INVARIANTS
1429466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1430466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1431466b3540SConrad Meyer #endif
1432466b3540SConrad Meyer 
1433466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1434466b3540SConrad Meyer 	for (;;) {
1435466b3540SConrad Meyer 		old = ioat->refcnt;
1436466b3540SConrad Meyer 		if (old <= n)
1437466b3540SConrad Meyer 			break;
1438466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1439466b3540SConrad Meyer 			return;
1440466b3540SConrad Meyer 	}
1441466b3540SConrad Meyer 
1442466b3540SConrad Meyer 	mtx_lock(IOAT_REFLK);
1443466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1444466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1445466b3540SConrad Meyer 
1446466b3540SConrad Meyer 	if (old == n)
1447466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1448466b3540SConrad Meyer 	mtx_unlock(IOAT_REFLK);
1449466b3540SConrad Meyer }
1450466b3540SConrad Meyer 
1451466b3540SConrad Meyer static inline void
1452466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1453466b3540SConrad Meyer {
1454466b3540SConrad Meyer 
1455466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1456466b3540SConrad Meyer }
1457466b3540SConrad Meyer 
1458466b3540SConrad Meyer static void
1459466b3540SConrad Meyer ioat_drain(struct ioat_softc *ioat)
1460466b3540SConrad Meyer {
1461466b3540SConrad Meyer 
1462466b3540SConrad Meyer 	mtx_lock(IOAT_REFLK);
1463466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1464466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1465466b3540SConrad Meyer 	mtx_unlock(IOAT_REFLK);
1466466b3540SConrad Meyer }
1467