xref: /freebsd/sys/dev/ioat/ioat.c (revision 1bbc06b85b06630f38959fd9b42f879dfbc52d96)
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 
300bb35debSConrad Meyer #include "opt_ddb.h"
310bb35debSConrad Meyer 
32e974f91cSConrad Meyer #include <sys/param.h>
33e974f91cSConrad Meyer #include <sys/systm.h>
34e974f91cSConrad Meyer #include <sys/bus.h>
35e974f91cSConrad Meyer #include <sys/conf.h>
36e974f91cSConrad Meyer #include <sys/ioccom.h>
37e974f91cSConrad Meyer #include <sys/kernel.h>
38e974f91cSConrad Meyer #include <sys/lock.h>
39e974f91cSConrad Meyer #include <sys/malloc.h>
40e974f91cSConrad Meyer #include <sys/module.h>
41e974f91cSConrad Meyer #include <sys/mutex.h>
42e974f91cSConrad Meyer #include <sys/rman.h>
43faefad9cSConrad Meyer #include <sys/sbuf.h>
44e974f91cSConrad Meyer #include <sys/sysctl.h>
45374b05e1SConrad Meyer #include <sys/taskqueue.h>
46e974f91cSConrad Meyer #include <sys/time.h>
47e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
48e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
49e974f91cSConrad Meyer #include <machine/bus.h>
50e974f91cSConrad Meyer #include <machine/resource.h>
51e974f91cSConrad Meyer #include <machine/stdarg.h>
52e974f91cSConrad Meyer 
530bb35debSConrad Meyer #ifdef DDB
540bb35debSConrad Meyer #include <ddb/ddb.h>
550bb35debSConrad Meyer #endif
560bb35debSConrad Meyer 
57e974f91cSConrad Meyer #include "ioat.h"
58e974f91cSConrad Meyer #include "ioat_hw.h"
59e974f91cSConrad Meyer #include "ioat_internal.h"
60e974f91cSConrad Meyer 
61519e8baaSConrad Meyer #ifndef	BUS_SPACE_MAXADDR_40BIT
62519e8baaSConrad Meyer #define	BUS_SPACE_MAXADDR_40BIT	0xFFFFFFFFFFULL
63519e8baaSConrad Meyer #endif
64466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
655ac77963SConrad Meyer #define	IOAT_SHRINK_PERIOD	(10 * hz)
66fe720f5aSConrad Meyer 
67e974f91cSConrad Meyer static int ioat_probe(device_t device);
68e974f91cSConrad Meyer static int ioat_attach(device_t device);
69e974f91cSConrad Meyer static int ioat_detach(device_t device);
704253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
714253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
72e974f91cSConrad Meyer static int ioat3_attach(device_t device);
73cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
74e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
75e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
76e974f91cSConrad Meyer     int error);
77e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
780d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
79faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
80e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
81e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
82e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
83bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
84bf8553eaSConrad Meyer     struct ioat_descriptor **);
85e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat,
86e974f91cSConrad Meyer     struct ioat_descriptor *desc);
87bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *,
88bf8553eaSConrad Meyer     int mflags);
89bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
90e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat,
91e974f91cSConrad Meyer     uint32_t index);
92bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *,
93bf8553eaSConrad Meyer     uint32_t size, boolean_t need_dscr, int mflags);
94bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder,
95bf8553eaSConrad Meyer     struct ioat_descriptor **);
96bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder,
97bf8553eaSConrad Meyer     struct ioat_descriptor **);
98faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
995ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg);
1005ac77963SConrad Meyer static void ioat_shrink_timer_callback(void *arg);
101e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
102e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
103e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
104e974f91cSConrad Meyer     int error);
105e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
106374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int);
107e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
108f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
109466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
110466b3540SConrad Meyer     enum ioat_ref_kind);
111466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
112faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
113faefad9cSConrad Meyer     enum ioat_ref_kind, boolean_t);
114466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
115466b3540SConrad Meyer     enum ioat_ref_kind);
116faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
117faefad9cSConrad Meyer     enum ioat_ref_kind);
1185f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
119e974f91cSConrad Meyer 
1201c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1211c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1221c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1231c25420eSConrad Meyer 	}								\
1241c25420eSConrad Meyer } while (0)
1251c25420eSConrad Meyer 
126e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
127e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
128e974f91cSConrad Meyer 
129e974f91cSConrad Meyer static int g_force_legacy_interrupts;
130e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
131e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
132e974f91cSConrad Meyer 
1331c25420eSConrad Meyer int g_ioat_debug_level = 0;
134e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
135e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
136e974f91cSConrad Meyer 
137e974f91cSConrad Meyer /*
138e974f91cSConrad Meyer  * OS <-> Driver interface structures
139e974f91cSConrad Meyer  */
140e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
141e974f91cSConrad Meyer 	/* Device interface */
142e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
143e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
144e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
145aa853cb4SEnji Cooper 	DEVMETHOD_END
146e974f91cSConrad Meyer };
147e974f91cSConrad Meyer 
148e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
149e974f91cSConrad Meyer 	"ioat",
150e974f91cSConrad Meyer 	ioat_pci_methods,
151e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
152e974f91cSConrad Meyer };
153e974f91cSConrad Meyer 
154e974f91cSConrad Meyer static devclass_t ioat_devclass;
155e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
156c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
157e974f91cSConrad Meyer 
158e974f91cSConrad Meyer /*
159e974f91cSConrad Meyer  * Private data structures
160e974f91cSConrad Meyer  */
161e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
162df1928aaSConrad Meyer static unsigned ioat_channel_index = 0;
163df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
164e974f91cSConrad Meyer     "Number of IOAT channels attached");
165e974f91cSConrad Meyer 
166e974f91cSConrad Meyer static struct _pcsid
167e974f91cSConrad Meyer {
168e974f91cSConrad Meyer 	u_int32_t   type;
169e974f91cSConrad Meyer 	const char  *desc;
170e974f91cSConrad Meyer } pci_ids[] = {
171e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
172e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
173e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
174e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
175e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
176e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
177e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
178e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
179e974f91cSConrad Meyer 
180e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
181e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
182e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
183e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
184e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
185e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
186e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
187e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
188e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
189e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
190e974f91cSConrad Meyer 
191e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
192e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
193e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
194e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
195e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
196e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
197e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
198e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
199e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
200e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
201e974f91cSConrad Meyer 
202e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
203e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
204e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
205e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
206e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
207e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
208e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
209e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
210e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
211e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
212e974f91cSConrad Meyer 
213e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
214e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
215e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
216e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
217e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
218e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
219e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
220e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
221e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
222e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
223e974f91cSConrad Meyer 
224e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
225e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
226e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
227e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
228e974f91cSConrad Meyer 
229e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
230e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
231e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
232e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
233e974f91cSConrad Meyer 
2345afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2355afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2365afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2375afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2385afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2395afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2405afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2415afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2425afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2435afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
2445afc2508SConrad Meyer 
245e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
246e974f91cSConrad Meyer };
247e974f91cSConrad Meyer 
248e974f91cSConrad Meyer /*
249e974f91cSConrad Meyer  * OS <-> Driver linkage functions
250e974f91cSConrad Meyer  */
251e974f91cSConrad Meyer static int
252e974f91cSConrad Meyer ioat_probe(device_t device)
253e974f91cSConrad Meyer {
254e974f91cSConrad Meyer 	struct _pcsid *ep;
255e974f91cSConrad Meyer 	u_int32_t type;
256e974f91cSConrad Meyer 
257e974f91cSConrad Meyer 	type = pci_get_devid(device);
258e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
259e974f91cSConrad Meyer 		if (ep->type == type) {
260e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
261e974f91cSConrad Meyer 			return (0);
262e974f91cSConrad Meyer 		}
263e974f91cSConrad Meyer 	}
264e974f91cSConrad Meyer 	return (ENXIO);
265e974f91cSConrad Meyer }
266e974f91cSConrad Meyer 
267e974f91cSConrad Meyer static int
268e974f91cSConrad Meyer ioat_attach(device_t device)
269e974f91cSConrad Meyer {
270e974f91cSConrad Meyer 	struct ioat_softc *ioat;
271e974f91cSConrad Meyer 	int error;
272e974f91cSConrad Meyer 
273e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
274e974f91cSConrad Meyer 	ioat->device = device;
275e974f91cSConrad Meyer 
276e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
277e974f91cSConrad Meyer 	if (error != 0)
278e974f91cSConrad Meyer 		goto err;
279e974f91cSConrad Meyer 
280e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
281e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
282e974f91cSConrad Meyer 		error = ENODEV;
283e974f91cSConrad Meyer 		goto err;
284e974f91cSConrad Meyer 	}
285e974f91cSConrad Meyer 
286e974f91cSConrad Meyer 	error = ioat3_attach(device);
287e974f91cSConrad Meyer 	if (error != 0)
288e974f91cSConrad Meyer 		goto err;
289e974f91cSConrad Meyer 
290e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
291e974f91cSConrad Meyer 	if (error != 0)
292e974f91cSConrad Meyer 		goto err;
293e974f91cSConrad Meyer 
294466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
295466b3540SConrad Meyer 	if (error != 0)
296466b3540SConrad Meyer 		goto err;
297466b3540SConrad Meyer 
298cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2997afbb263SConrad Meyer 	if (error != 0)
300466b3540SConrad Meyer 		goto err;
3017afbb263SConrad Meyer 
3027afbb263SConrad Meyer 	ioat_process_events(ioat);
3037afbb263SConrad Meyer 	ioat_setup_sysctl(device);
3047afbb263SConrad Meyer 
3055f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
306e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
3077afbb263SConrad Meyer 	ioat_test_attach();
308e974f91cSConrad Meyer 
309e974f91cSConrad Meyer err:
310e974f91cSConrad Meyer 	if (error != 0)
311e974f91cSConrad Meyer 		ioat_detach(device);
312e974f91cSConrad Meyer 	return (error);
313e974f91cSConrad Meyer }
314e974f91cSConrad Meyer 
315e974f91cSConrad Meyer static int
316e974f91cSConrad Meyer ioat_detach(device_t device)
317e974f91cSConrad Meyer {
318e974f91cSConrad Meyer 	struct ioat_softc *ioat;
319e974f91cSConrad Meyer 
320e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3217afbb263SConrad Meyer 
3227afbb263SConrad Meyer 	ioat_test_detach();
323374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3245f77bd3eSConrad Meyer 
3255f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3265f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3270ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3280ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
32993f7f84aSConrad Meyer 	wakeup(&ioat->resetting);
3300ff814e8SConrad Meyer 
3315f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3325f77bd3eSConrad Meyer 
3335f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3345f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
335fe720f5aSConrad Meyer 
336fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
3375ac77963SConrad Meyer 	callout_drain(&ioat->poll_timer);
3385ac77963SConrad Meyer 	callout_drain(&ioat->shrink_timer);
339e974f91cSConrad Meyer 
340e974f91cSConrad Meyer 	pci_disable_busmaster(device);
341e974f91cSConrad Meyer 
342e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
343e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
344e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
345e974f91cSConrad Meyer 
346bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
347bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
348e974f91cSConrad Meyer 
349e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
350e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
351e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
352e974f91cSConrad Meyer 		    ioat->comp_update_map);
353e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
354e974f91cSConrad Meyer 	}
355e974f91cSConrad Meyer 
356e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
357e974f91cSConrad Meyer 
3584253ea50SConrad Meyer 	return (0);
3594253ea50SConrad Meyer }
3604253ea50SConrad Meyer 
3614253ea50SConrad Meyer static int
3624253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3634253ea50SConrad Meyer {
3644253ea50SConrad Meyer 
365e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3664253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
367e974f91cSConrad Meyer 
368e974f91cSConrad Meyer 	if (ioat->res != NULL)
3694253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
370e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
371e974f91cSConrad Meyer 
3724253ea50SConrad Meyer 	pci_release_msi(ioat->device);
373e974f91cSConrad Meyer 	return (0);
374e974f91cSConrad Meyer }
375e974f91cSConrad Meyer 
376e974f91cSConrad Meyer static int
377cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
378e974f91cSConrad Meyer {
379fe8712f8SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
380fe8712f8SConrad Meyer 	struct ioat_descriptor *desc;
381fe8712f8SConrad Meyer 	struct bus_dmadesc *dmadesc;
382e974f91cSConrad Meyer 	uint64_t status;
383e974f91cSConrad Meyer 	uint32_t chanerr;
384e974f91cSConrad Meyer 	int i;
385e974f91cSConrad Meyer 
386e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
387fe8712f8SConrad Meyer 
388fe8712f8SConrad Meyer 	/* Submit 'NULL' operation manually to avoid quiescing flag */
389fe8712f8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
390fe8712f8SConrad Meyer 	dmadesc = &desc->bus_dmadesc;
391fe8712f8SConrad Meyer 	hw_desc = desc->u.dma;
392fe8712f8SConrad Meyer 
393fe8712f8SConrad Meyer 	dmadesc->callback_fn = NULL;
394fe8712f8SConrad Meyer 	dmadesc->callback_arg = NULL;
395fe8712f8SConrad Meyer 
396fe8712f8SConrad Meyer 	hw_desc->u.control_raw = 0;
397fe8712f8SConrad Meyer 	hw_desc->u.control_generic.op = IOAT_OP_COPY;
398fe8712f8SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
399fe8712f8SConrad Meyer 	hw_desc->size = 8;
400fe8712f8SConrad Meyer 	hw_desc->src_addr = 0;
401fe8712f8SConrad Meyer 	hw_desc->dest_addr = 0;
402fe8712f8SConrad Meyer 	hw_desc->u.control.null = 1;
403fe8712f8SConrad Meyer 
404fe8712f8SConrad Meyer 	ioat_submit_single(ioat);
405e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
406e974f91cSConrad Meyer 
407e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
408e974f91cSConrad Meyer 		DELAY(1);
409e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
410e974f91cSConrad Meyer 		if (is_ioat_idle(status))
411e974f91cSConrad Meyer 			return (0);
412e974f91cSConrad Meyer 	}
413e974f91cSConrad Meyer 
414e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
415e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
41659acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
41759acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
418e974f91cSConrad Meyer 	return (ENXIO);
419e974f91cSConrad Meyer }
420e974f91cSConrad Meyer 
421e974f91cSConrad Meyer /*
422e974f91cSConrad Meyer  * Initialize Hardware
423e974f91cSConrad Meyer  */
424e974f91cSConrad Meyer static int
425e974f91cSConrad Meyer ioat3_attach(device_t device)
426e974f91cSConrad Meyer {
427e974f91cSConrad Meyer 	struct ioat_softc *ioat;
428e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
429e974f91cSConrad Meyer 	struct ioat_descriptor *next;
430e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
431e974f91cSConrad Meyer 	int i, num_descriptors;
432e974f91cSConrad Meyer 	int error;
433e974f91cSConrad Meyer 	uint8_t xfercap;
434e974f91cSConrad Meyer 
435e974f91cSConrad Meyer 	error = 0;
436e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
4371693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
4381693d27bSConrad Meyer 
43948fed014SConrad Meyer 	ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities,
4401693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
441e974f91cSConrad Meyer 
442e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
443e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
444e974f91cSConrad Meyer 
4455ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
4465ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
4475ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
4485ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
4495ca9fc2aSConrad Meyer 
450e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
451e974f91cSConrad Meyer 
452e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
453faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4545ac77963SConrad Meyer 	callout_init(&ioat->poll_timer, 1);
4555ac77963SConrad Meyer 	callout_init(&ioat->shrink_timer, 1);
456374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
457e974f91cSConrad Meyer 
458faefad9cSConrad Meyer 	/* Establish lock order for Witness */
459faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
460faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
461faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
462faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
463faefad9cSConrad Meyer 
464e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
46525ad9585SConrad Meyer 	ioat->is_submitter_processing = FALSE;
466e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
467e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
468e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
469e974f91cSConrad Meyer 
470e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
471e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
472e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
473e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
474e974f91cSConrad Meyer 
475e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
476e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
477e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
478e974f91cSConrad Meyer 		return (ENOMEM);
479e974f91cSConrad Meyer 
480e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
481e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
482e974f91cSConrad Meyer 	    0);
483e974f91cSConrad Meyer 	if (error != 0)
484e974f91cSConrad Meyer 		return (error);
485e974f91cSConrad Meyer 
486e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
487e974f91cSConrad Meyer 
488e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
489e974f91cSConrad Meyer 
490e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
491519e8baaSConrad Meyer 	    BUS_SPACE_MAXADDR_40BIT, BUS_SPACE_MAXADDR, NULL, NULL,
492e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
493e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
494e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
495e974f91cSConrad Meyer 
496e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
497bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
498e974f91cSConrad Meyer 
499e974f91cSConrad Meyer 	ring = ioat->ring;
500e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
501bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
502e974f91cSConrad Meyer 		if (ring[i] == NULL)
503e974f91cSConrad Meyer 			return (ENOMEM);
504e974f91cSConrad Meyer 
505e974f91cSConrad Meyer 		ring[i]->id = i;
506e974f91cSConrad Meyer 	}
507e974f91cSConrad Meyer 
508e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
509e974f91cSConrad Meyer 		next = ring[i + 1];
510e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
511e974f91cSConrad Meyer 
512e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
513e974f91cSConrad Meyer 	}
514e974f91cSConrad Meyer 
515e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
516e974f91cSConrad Meyer 
517bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
518e974f91cSConrad Meyer 	ioat->tail = 0;
519e974f91cSConrad Meyer 	ioat->last_seen = 0;
520fe8712f8SConrad Meyer 	*ioat->comp_update = 0;
521e974f91cSConrad Meyer 	return (0);
522e974f91cSConrad Meyer }
523e974f91cSConrad Meyer 
524e974f91cSConrad Meyer static int
525e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
526e974f91cSConrad Meyer {
527e974f91cSConrad Meyer 
528e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
529e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
530e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
531e974f91cSConrad Meyer 
532e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
533e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
534e974f91cSConrad Meyer 		return (ENODEV);
535e974f91cSConrad Meyer 	}
536e974f91cSConrad Meyer 
537e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
538e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
539e974f91cSConrad Meyer 	return (0);
540e974f91cSConrad Meyer }
541e974f91cSConrad Meyer 
542e974f91cSConrad Meyer static void
543e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
544e974f91cSConrad Meyer {
545e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
546e974f91cSConrad Meyer 
547cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
548e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
549e974f91cSConrad Meyer }
550e974f91cSConrad Meyer 
551e974f91cSConrad Meyer static void
552e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
553e974f91cSConrad Meyer {
554e974f91cSConrad Meyer 	bus_addr_t *baddr;
555e974f91cSConrad Meyer 
556cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
557e974f91cSConrad Meyer 	baddr = arg;
558e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
559e974f91cSConrad Meyer }
560e974f91cSConrad Meyer 
561e974f91cSConrad Meyer /*
562e974f91cSConrad Meyer  * Interrupt setup and handlers
563e974f91cSConrad Meyer  */
564e974f91cSConrad Meyer static int
5654253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
566e974f91cSConrad Meyer {
567e974f91cSConrad Meyer 	uint32_t num_vectors;
568e974f91cSConrad Meyer 	int error;
569e974f91cSConrad Meyer 	boolean_t use_msix;
570e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
571e974f91cSConrad Meyer 
572e974f91cSConrad Meyer 	use_msix = FALSE;
573e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
574e974f91cSConrad Meyer 
575e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
576e974f91cSConrad Meyer 		num_vectors = 1;
577e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
578e974f91cSConrad Meyer 		if (num_vectors == 1)
579e974f91cSConrad Meyer 			use_msix = TRUE;
580e974f91cSConrad Meyer 	}
581e974f91cSConrad Meyer 
582e974f91cSConrad Meyer 	if (use_msix) {
583e974f91cSConrad Meyer 		ioat->rid = 1;
584e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
585e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
586e974f91cSConrad Meyer 	} else {
587e974f91cSConrad Meyer 		ioat->rid = 0;
588e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
589e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
590e974f91cSConrad Meyer 	}
591e974f91cSConrad Meyer 	if (ioat->res == NULL) {
592e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
593e974f91cSConrad Meyer 		return (ENOMEM);
594e974f91cSConrad Meyer 	}
595e974f91cSConrad Meyer 
596e974f91cSConrad Meyer 	ioat->tag = NULL;
597e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
598e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
599e974f91cSConrad Meyer 	if (error != 0) {
600e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
601e974f91cSConrad Meyer 		return (error);
602e974f91cSConrad Meyer 	}
603e974f91cSConrad Meyer 
604e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
605e974f91cSConrad Meyer 	return (0);
606e974f91cSConrad Meyer }
607e974f91cSConrad Meyer 
6084253ea50SConrad Meyer static boolean_t
6090d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
6104253ea50SConrad Meyer {
6114253ea50SConrad Meyer 	u_int32_t pciid;
6124253ea50SConrad Meyer 
6134253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
6144253ea50SConrad Meyer 	switch (pciid) {
6150d1a05d9SConrad Meyer 		/* BWD: */
6160d1a05d9SConrad Meyer 	case 0x0c508086:
6170d1a05d9SConrad Meyer 	case 0x0c518086:
6180d1a05d9SConrad Meyer 	case 0x0c528086:
6190d1a05d9SConrad Meyer 	case 0x0c538086:
6200d1a05d9SConrad Meyer 		/* BDXDE: */
6214253ea50SConrad Meyer 	case 0x6f508086:
6224253ea50SConrad Meyer 	case 0x6f518086:
6234253ea50SConrad Meyer 	case 0x6f528086:
6244253ea50SConrad Meyer 	case 0x6f538086:
6254253ea50SConrad Meyer 		return (TRUE);
6264253ea50SConrad Meyer 	}
6274253ea50SConrad Meyer 
6284253ea50SConrad Meyer 	return (FALSE);
6294253ea50SConrad Meyer }
6304253ea50SConrad Meyer 
631e974f91cSConrad Meyer static void
632e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
633e974f91cSConrad Meyer {
634e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
635e974f91cSConrad Meyer 
63601fbbc88SConrad Meyer 	ioat->stats.interrupts++;
637e974f91cSConrad Meyer 	ioat_process_events(ioat);
638e974f91cSConrad Meyer }
639e974f91cSConrad Meyer 
640faefad9cSConrad Meyer static int
641faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
642faefad9cSConrad Meyer {
643faefad9cSConrad Meyer 
644faefad9cSConrad Meyer 	if (chanerr == 0)
645faefad9cSConrad Meyer 		return (0);
646faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
647faefad9cSConrad Meyer 		return (EFAULT);
648faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
649faefad9cSConrad Meyer 		return (EIO);
650faefad9cSConrad Meyer 	/* This one is probably our fault: */
651faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
652faefad9cSConrad Meyer 		return (EIO);
653faefad9cSConrad Meyer 	return (EIO);
654faefad9cSConrad Meyer }
655faefad9cSConrad Meyer 
656e974f91cSConrad Meyer static void
657e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
658e974f91cSConrad Meyer {
659e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
660e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
661e974f91cSConrad Meyer 	uint64_t comp_update, status;
662faefad9cSConrad Meyer 	uint32_t completed, chanerr;
6635ac77963SConrad Meyer 	boolean_t pending;
664faefad9cSConrad Meyer 	int error;
665e974f91cSConrad Meyer 
666520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
667fe8712f8SConrad Meyer 
668e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
669e974f91cSConrad Meyer 
670fe8712f8SConrad Meyer 	/*
671fe8712f8SConrad Meyer 	 * Don't run while the hardware is being reset.  Reset is responsible
672fe8712f8SConrad Meyer 	 * for blocking new work and draining & completing existing work, so
673fe8712f8SConrad Meyer 	 * there is nothing to do until new work is queued after reset anyway.
674fe8712f8SConrad Meyer 	 */
675fe8712f8SConrad Meyer 	if (ioat->resetting_cleanup) {
676fe8712f8SConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
677fe8712f8SConrad Meyer 		return;
678fe8712f8SConrad Meyer 	}
679fe8712f8SConrad Meyer 
680e974f91cSConrad Meyer 	completed = 0;
6810283c0f5SConrad Meyer 	comp_update = ioat_get_chansts(ioat);
6820283c0f5SConrad Meyer 	CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx",
6830283c0f5SConrad Meyer 	    __func__, ioat->chan_idx, comp_update, ioat->last_seen);
684e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
685e974f91cSConrad Meyer 
686*1bbc06b8SConrad Meyer 	if (status == ioat->last_seen) {
687*1bbc06b8SConrad Meyer 		/*
688*1bbc06b8SConrad Meyer 		 * If we landed in process_events and nothing has been
689*1bbc06b8SConrad Meyer 		 * completed, check for a timeout due to channel halt.
690*1bbc06b8SConrad Meyer 		 */
691*1bbc06b8SConrad Meyer 		goto out;
692*1bbc06b8SConrad Meyer 	}
693*1bbc06b8SConrad Meyer 
694*1bbc06b8SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail - 1);
695*1bbc06b8SConrad Meyer 	while (desc->hw_desc_bus_addr != status && ioat_get_active(ioat) > 0) {
696e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
697e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
698520f6023SConrad Meyer 		CTR4(KTR_IOAT, "channel=%u completing desc %u ok  cb %p(%p)",
699520f6023SConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc->callback_fn,
700520f6023SConrad Meyer 		    dmadesc->callback_arg);
701e974f91cSConrad Meyer 
702faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
703faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
704e974f91cSConrad Meyer 
705466b3540SConrad Meyer 		completed++;
706e974f91cSConrad Meyer 		ioat->tail++;
707e974f91cSConrad Meyer 	}
708e974f91cSConrad Meyer 
7090283c0f5SConrad Meyer 	if (completed != 0) {
710e974f91cSConrad Meyer 		ioat->last_seen = desc->hw_desc_bus_addr;
71101fbbc88SConrad Meyer 		ioat->stats.descriptors_processed += completed;
7120283c0f5SConrad Meyer 	}
71301fbbc88SConrad Meyer 
714*1bbc06b8SConrad Meyer out:
715e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
7165ac77963SConrad Meyer 
7175ac77963SConrad Meyer 	/* Perform a racy check first; only take the locks if it passes. */
7185ac77963SConrad Meyer 	pending = (ioat_get_active(ioat) != 0);
7195ac77963SConrad Meyer 	if (!pending && ioat->is_completion_pending) {
720e974f91cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
7215ac77963SConrad Meyer 		mtx_lock(&ioat->submit_lock);
7225ac77963SConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
7235ac77963SConrad Meyer 
7245ac77963SConrad Meyer 		pending = (ioat_get_active(ioat) != 0);
7255ac77963SConrad Meyer 		if (!pending && ioat->is_completion_pending) {
7265ac77963SConrad Meyer 			ioat->is_completion_pending = FALSE;
7275ac77963SConrad Meyer 			callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD,
7285ac77963SConrad Meyer 			    ioat_shrink_timer_callback, ioat);
7295ac77963SConrad Meyer 			callout_stop(&ioat->poll_timer);
7305ac77963SConrad Meyer 		}
7315ac77963SConrad Meyer 		mtx_unlock(&ioat->submit_lock);
7325ac77963SConrad Meyer 	}
7335ac77963SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
7345ac77963SConrad Meyer 
7355ac77963SConrad Meyer 	if (pending)
7365ac77963SConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
7375ac77963SConrad Meyer 		    ioat);
738466b3540SConrad Meyer 
739007a7030SConrad Meyer 	if (completed != 0) {
740466b3540SConrad Meyer 		ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
741bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
742007a7030SConrad Meyer 	}
743faefad9cSConrad Meyer 
744d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
745faefad9cSConrad Meyer 		return;
746faefad9cSConrad Meyer 
74701fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
74801fbbc88SConrad Meyer 
749faefad9cSConrad Meyer 	/*
750faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
751faefad9cSConrad Meyer 	 * work with error status and restart the engine.
752faefad9cSConrad Meyer 	 */
753faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
754faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
755faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
756faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
757faefad9cSConrad Meyer 
758faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
759faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
76001fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
761faefad9cSConrad Meyer 
762faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
763faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
764faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
765520f6023SConrad Meyer 		CTR4(KTR_IOAT, "channel=%u completing desc %u err cb %p(%p)",
766520f6023SConrad Meyer 		    ioat->chan_idx, ioat->tail, dmadesc->callback_fn,
767520f6023SConrad Meyer 		    dmadesc->callback_arg);
768faefad9cSConrad Meyer 
769faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
770faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
771faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
772faefad9cSConrad Meyer 
773faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
774faefad9cSConrad Meyer 		ioat->tail++;
77501fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
77601fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
777faefad9cSConrad Meyer 	}
778faefad9cSConrad Meyer 
779c114e741SConrad Meyer 	if (ioat->is_completion_pending) {
780c114e741SConrad Meyer 		ioat->is_completion_pending = FALSE;
781c114e741SConrad Meyer 		callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD,
782c114e741SConrad Meyer 		    ioat_shrink_timer_callback, ioat);
783c114e741SConrad Meyer 		callout_stop(&ioat->poll_timer);
784c114e741SConrad Meyer 	}
785c114e741SConrad Meyer 
786faefad9cSConrad Meyer 	/* Clear error status */
787faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
788faefad9cSConrad Meyer 
789faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
790faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
791faefad9cSConrad Meyer 
792faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
793374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
794374b05e1SConrad Meyer 	KASSERT(error == 0,
795374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
796374b05e1SConrad Meyer }
797374b05e1SConrad Meyer 
798374b05e1SConrad Meyer static void
799374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
800374b05e1SConrad Meyer {
801374b05e1SConrad Meyer 	struct ioat_softc *ioat;
802374b05e1SConrad Meyer 	int error;
803374b05e1SConrad Meyer 
804374b05e1SConrad Meyer 	ioat = ctx;
805374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
806374b05e1SConrad Meyer 
807faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
808faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
809374b05e1SConrad Meyer 	(void)error;
810e974f91cSConrad Meyer }
811e974f91cSConrad Meyer 
812e974f91cSConrad Meyer /*
813e974f91cSConrad Meyer  * User API functions
814e974f91cSConrad Meyer  */
815f8f92e91SConrad Meyer unsigned
816f8f92e91SConrad Meyer ioat_get_nchannels(void)
817f8f92e91SConrad Meyer {
818f8f92e91SConrad Meyer 
819f8f92e91SConrad Meyer 	return (ioat_channel_index);
820f8f92e91SConrad Meyer }
821f8f92e91SConrad Meyer 
822e974f91cSConrad Meyer bus_dmaengine_t
8230ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
824e974f91cSConrad Meyer {
8250ff814e8SConrad Meyer 	struct ioat_softc *ioat;
8260ff814e8SConrad Meyer 
8270ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
8280ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
8290ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
8300ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
831e974f91cSConrad Meyer 
832466b3540SConrad Meyer 	if (index >= ioat_channel_index)
833e974f91cSConrad Meyer 		return (NULL);
8345f77bd3eSConrad Meyer 
8350ff814e8SConrad Meyer 	ioat = ioat_channel[index];
8360ff814e8SConrad Meyer 	if (ioat == NULL || ioat->destroying)
8375f77bd3eSConrad Meyer 		return (NULL);
8385f77bd3eSConrad Meyer 
8390ff814e8SConrad Meyer 	if (ioat->quiescing) {
8400ff814e8SConrad Meyer 		if ((flags & M_NOWAIT) != 0)
8410ff814e8SConrad Meyer 			return (NULL);
8420ff814e8SConrad Meyer 
8430ff814e8SConrad Meyer 		mtx_lock(IOAT_REFLK);
8440ff814e8SConrad Meyer 		while (ioat->quiescing && !ioat->destroying)
8450ff814e8SConrad Meyer 			msleep(&ioat->quiescing, IOAT_REFLK, 0, "getdma", 0);
8460ff814e8SConrad Meyer 		mtx_unlock(IOAT_REFLK);
8470ff814e8SConrad Meyer 
8480ff814e8SConrad Meyer 		if (ioat->destroying)
8490ff814e8SConrad Meyer 			return (NULL);
8500ff814e8SConrad Meyer 	}
8510ff814e8SConrad Meyer 
8520ff814e8SConrad Meyer 	/*
8530ff814e8SConrad Meyer 	 * There's a race here between the quiescing check and HW reset or
8540ff814e8SConrad Meyer 	 * module destroy.
8550ff814e8SConrad Meyer 	 */
8560ff814e8SConrad Meyer 	return (&ioat_get(ioat, IOAT_DMAENGINE_REF)->dmaengine);
857466b3540SConrad Meyer }
858466b3540SConrad Meyer 
859466b3540SConrad Meyer void
860466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
861466b3540SConrad Meyer {
862466b3540SConrad Meyer 	struct ioat_softc *ioat;
863466b3540SConrad Meyer 
864466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
865466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
866e974f91cSConrad Meyer }
867e974f91cSConrad Meyer 
8685ca9fc2aSConrad Meyer int
86931bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
87031bf2875SConrad Meyer {
87131bf2875SConrad Meyer 	struct ioat_softc *ioat;
87231bf2875SConrad Meyer 
87331bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
87431bf2875SConrad Meyer 	return (ioat->version);
87531bf2875SConrad Meyer }
87631bf2875SConrad Meyer 
877bd81fe68SConrad Meyer size_t
878bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
879bd81fe68SConrad Meyer {
880bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
881bd81fe68SConrad Meyer 
882bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
883bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
884bd81fe68SConrad Meyer }
885bd81fe68SConrad Meyer 
886f8253f1aSConrad Meyer uint32_t
887f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine)
888f8253f1aSConrad Meyer {
889f8253f1aSConrad Meyer 	struct ioat_softc *ioat;
890f8253f1aSConrad Meyer 
891f8253f1aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
892f8253f1aSConrad Meyer 	return (ioat->capabilities);
893f8253f1aSConrad Meyer }
894f8253f1aSConrad Meyer 
89531bf2875SConrad Meyer int
8965ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
8975ca9fc2aSConrad Meyer {
8985ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
8995ca9fc2aSConrad Meyer 
9005ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9015ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
9025ca9fc2aSConrad Meyer 		return (ENODEV);
9035ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
9045ca9fc2aSConrad Meyer 		return (ERANGE);
9055ca9fc2aSConrad Meyer 
9065ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
9075ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
9085ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
9095ca9fc2aSConrad Meyer 	return (0);
9105ca9fc2aSConrad Meyer }
9115ca9fc2aSConrad Meyer 
9125ca9fc2aSConrad Meyer uint16_t
9135ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
9145ca9fc2aSConrad Meyer {
9155ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
9165ca9fc2aSConrad Meyer 
9175ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9185ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
9195ca9fc2aSConrad Meyer }
9205ca9fc2aSConrad Meyer 
921e974f91cSConrad Meyer void
922e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
923e974f91cSConrad Meyer {
924e974f91cSConrad Meyer 	struct ioat_softc *ioat;
925e974f91cSConrad Meyer 
926e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
927e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
928520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
929e974f91cSConrad Meyer }
930e974f91cSConrad Meyer 
9311502e363SConrad Meyer int
9321502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
9331502e363SConrad Meyer {
9341502e363SConrad Meyer 	struct ioat_softc *ioat;
9351502e363SConrad Meyer 	int error;
9361502e363SConrad Meyer 
9371502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9381502e363SConrad Meyer 	ioat_acquire(dmaengine);
9391502e363SConrad Meyer 
9401502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
9411502e363SConrad Meyer 	if (error != 0)
9421502e363SConrad Meyer 		ioat_release(dmaengine);
9431502e363SConrad Meyer 	return (error);
9441502e363SConrad Meyer }
9451502e363SConrad Meyer 
946e974f91cSConrad Meyer void
947e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
948e974f91cSConrad Meyer {
949e974f91cSConrad Meyer 	struct ioat_softc *ioat;
950e974f91cSConrad Meyer 
951e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
952520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
953bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
954e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
955e974f91cSConrad Meyer }
956e974f91cSConrad Meyer 
9579e3bbf26SConrad Meyer static struct ioat_descriptor *
9589e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
9599e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
9609e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
9619e3bbf26SConrad Meyer     uint32_t flags)
962e974f91cSConrad Meyer {
9639e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
964e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
965bf8553eaSConrad Meyer 	int mflags;
966e974f91cSConrad Meyer 
9679e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
9689e3bbf26SConrad Meyer 
969bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
970bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
971bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
972bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
973bf8553eaSConrad Meyer 	else
974bf8553eaSConrad Meyer 		mflags = M_WAITOK;
975e974f91cSConrad Meyer 
9769e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
9779e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
9789e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
9799e3bbf26SConrad Meyer 		return (NULL);
9809e3bbf26SConrad Meyer 	}
981e974f91cSConrad Meyer 
982bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
983e974f91cSConrad Meyer 		return (NULL);
984e974f91cSConrad Meyer 
985e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
9869e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
987e974f91cSConrad Meyer 
988e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
9899e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
9909e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
991e974f91cSConrad Meyer 
992e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
9939e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
9946ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
9956ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
996e974f91cSConrad Meyer 
9979e3bbf26SConrad Meyer 	hw_desc->size = size;
9989e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
9999e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
1000e974f91cSConrad Meyer 
1001e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
1002e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
10039e3bbf26SConrad Meyer 	return (desc);
10049e3bbf26SConrad Meyer }
1005e974f91cSConrad Meyer 
10069e3bbf26SConrad Meyer struct bus_dmadesc *
10079e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
10089e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
10099e3bbf26SConrad Meyer {
10109e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10119e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
10129e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
10139e3bbf26SConrad Meyer 
10149e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1015520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
10169e3bbf26SConrad Meyer 
10179e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
10189e3bbf26SConrad Meyer 	    callback_arg, flags);
10199e3bbf26SConrad Meyer 	if (desc == NULL)
10209e3bbf26SConrad Meyer 		return (NULL);
10219e3bbf26SConrad Meyer 
10229e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
10239e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
1024e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1025e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1026e974f91cSConrad Meyer }
1027e974f91cSConrad Meyer 
1028e974f91cSConrad Meyer struct bus_dmadesc *
1029e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
1030e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
1031e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
1032e974f91cSConrad Meyer {
1033e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10349e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
1035e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1036e974f91cSConrad Meyer 
1037e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1038520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1039e974f91cSConrad Meyer 
10409e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
10419e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
10429e3bbf26SConrad Meyer 		    __func__);
1043e974f91cSConrad Meyer 		return (NULL);
1044e974f91cSConrad Meyer 	}
1045e974f91cSConrad Meyer 
10469e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
10479e3bbf26SConrad Meyer 	    callback_arg, flags);
10489e3bbf26SConrad Meyer 	if (desc == NULL)
1049e974f91cSConrad Meyer 		return (NULL);
1050e974f91cSConrad Meyer 
1051e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
1052e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
1053e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
1054e974f91cSConrad Meyer 
1055e974f91cSConrad Meyer 	ioat_submit_single(ioat);
1056e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
1057e974f91cSConrad Meyer }
1058e974f91cSConrad Meyer 
10592a4fd6b1SConrad Meyer struct bus_dmadesc *
10609950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
10619950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
10629950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
10639950fde0SConrad Meyer {
10649950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
10659950fde0SConrad Meyer 	struct ioat_descriptor *desc;
10669950fde0SConrad Meyer 	struct ioat_softc *ioat;
10679950fde0SConrad Meyer 
10689950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1069520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
10709950fde0SConrad Meyer 
10719950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
10729950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
10739950fde0SConrad Meyer 		    __func__);
10749950fde0SConrad Meyer 		return (NULL);
10759950fde0SConrad Meyer 	}
10769950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
10779950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
10789950fde0SConrad Meyer 		    __func__);
10799950fde0SConrad Meyer 		return (NULL);
10809950fde0SConrad Meyer 	}
10819950fde0SConrad Meyer 
10829950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
10839950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
10849950fde0SConrad Meyer 	if (desc == NULL)
10859950fde0SConrad Meyer 		return (NULL);
10869950fde0SConrad Meyer 
10879950fde0SConrad Meyer 	hw_desc = desc->u.dma;
10889950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
10899950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
10909950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
10919950fde0SConrad Meyer 	}
10929950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
10939950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
10949950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
10959950fde0SConrad Meyer 	}
10969950fde0SConrad Meyer 
10979950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
10989950fde0SConrad Meyer 		dump_descriptor(hw_desc);
10999950fde0SConrad Meyer 
11009950fde0SConrad Meyer 	ioat_submit_single(ioat);
11019950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
11029950fde0SConrad Meyer }
11039950fde0SConrad Meyer 
11049950fde0SConrad Meyer struct bus_dmadesc *
1105bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1106bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1107bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1108bec7ff79SConrad Meyer {
1109bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1110bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1111bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1112bec7ff79SConrad Meyer 	uint32_t teststore;
1113bec7ff79SConrad Meyer 	uint8_t op;
1114bec7ff79SConrad Meyer 
1115bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1116520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1117bec7ff79SConrad Meyer 
1118bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1119bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1120bec7ff79SConrad Meyer 		    __func__);
1121bec7ff79SConrad Meyer 		return (NULL);
1122bec7ff79SConrad Meyer 	}
1123bec7ff79SConrad Meyer 	if (((src | dst) & (0xffffffull << 40)) != 0) {
1124bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1125bec7ff79SConrad Meyer 		    __func__);
1126bec7ff79SConrad Meyer 		return (NULL);
1127bec7ff79SConrad Meyer 	}
1128bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1129bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1130bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1131bec7ff79SConrad Meyer 		return (NULL);
1132bec7ff79SConrad Meyer 	}
1133bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1134bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1135bec7ff79SConrad Meyer 		    __func__);
1136bec7ff79SConrad Meyer 		return (NULL);
1137bec7ff79SConrad Meyer 	}
1138bec7ff79SConrad Meyer 
1139bec7ff79SConrad Meyer 	switch (teststore) {
1140bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1141bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1142bec7ff79SConrad Meyer 		break;
1143bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1144bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1145bec7ff79SConrad Meyer 		break;
1146bec7ff79SConrad Meyer 	default:
1147bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1148bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1149bec7ff79SConrad Meyer 		break;
1150bec7ff79SConrad Meyer 	}
1151bec7ff79SConrad Meyer 
1152bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1153bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1154bec7ff79SConrad Meyer 		ioat_log_message(0,
1155bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1156bec7ff79SConrad Meyer 		return (NULL);
1157bec7ff79SConrad Meyer 	}
1158bec7ff79SConrad Meyer 
1159bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1160bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1161bec7ff79SConrad Meyer 	if (desc == NULL)
1162bec7ff79SConrad Meyer 		return (NULL);
1163bec7ff79SConrad Meyer 
1164bec7ff79SConrad Meyer 	hw_desc = desc->u.crc32;
1165bec7ff79SConrad Meyer 
1166bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1167bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1168bec7ff79SConrad Meyer 	else
1169bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1170bec7ff79SConrad Meyer 
1171bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1172bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1173bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1174bec7ff79SConrad Meyer 	}
1175bec7ff79SConrad Meyer 
1176bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1177bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1178bec7ff79SConrad Meyer 
1179bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1180bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1181bec7ff79SConrad Meyer }
1182bec7ff79SConrad Meyer 
1183bec7ff79SConrad Meyer struct bus_dmadesc *
1184bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1185bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1186bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1187bec7ff79SConrad Meyer {
1188bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1189bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1190bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1191bec7ff79SConrad Meyer 	uint32_t teststore;
1192bec7ff79SConrad Meyer 	uint8_t op;
1193bec7ff79SConrad Meyer 
1194bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1195520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1196bec7ff79SConrad Meyer 
1197bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1198bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks CRC capability\n",
1199bec7ff79SConrad Meyer 		    __func__);
1200bec7ff79SConrad Meyer 		return (NULL);
1201bec7ff79SConrad Meyer 	}
1202bec7ff79SConrad Meyer 	if ((src & (0xffffffull << 40)) != 0) {
1203bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1204bec7ff79SConrad Meyer 		    __func__);
1205bec7ff79SConrad Meyer 		return (NULL);
1206bec7ff79SConrad Meyer 	}
1207bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1208bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1209bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1210bec7ff79SConrad Meyer 		return (NULL);
1211bec7ff79SConrad Meyer 	}
1212bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1213bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1214bec7ff79SConrad Meyer 		    __func__);
1215bec7ff79SConrad Meyer 		return (NULL);
1216bec7ff79SConrad Meyer 	}
1217bec7ff79SConrad Meyer 
1218bec7ff79SConrad Meyer 	switch (teststore) {
1219bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1220bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1221bec7ff79SConrad Meyer 		break;
1222bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1223bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1224bec7ff79SConrad Meyer 		break;
1225bec7ff79SConrad Meyer 	default:
1226bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1227bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1228bec7ff79SConrad Meyer 		break;
1229bec7ff79SConrad Meyer 	}
1230bec7ff79SConrad Meyer 
1231bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1232bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1233bec7ff79SConrad Meyer 		ioat_log_message(0,
1234bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1235bec7ff79SConrad Meyer 		return (NULL);
1236bec7ff79SConrad Meyer 	}
1237bec7ff79SConrad Meyer 
1238bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1239bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1240bec7ff79SConrad Meyer 	if (desc == NULL)
1241bec7ff79SConrad Meyer 		return (NULL);
1242bec7ff79SConrad Meyer 
1243bec7ff79SConrad Meyer 	hw_desc = desc->u.crc32;
1244bec7ff79SConrad Meyer 
1245bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1246bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1247bec7ff79SConrad Meyer 	else
1248bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1249bec7ff79SConrad Meyer 
1250bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1251bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1252bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1253bec7ff79SConrad Meyer 	}
1254bec7ff79SConrad Meyer 
1255bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1256bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1257bec7ff79SConrad Meyer 
1258bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1259bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1260bec7ff79SConrad Meyer }
1261bec7ff79SConrad Meyer 
1262bec7ff79SConrad Meyer struct bus_dmadesc *
12632a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
12642a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
12652a4fd6b1SConrad Meyer     uint32_t flags)
12662a4fd6b1SConrad Meyer {
12672a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
12682a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
12692a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
12702a4fd6b1SConrad Meyer 
12712a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1272520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
12732a4fd6b1SConrad Meyer 
12741693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
12751693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
12761693d27bSConrad Meyer 		    __func__);
12771693d27bSConrad Meyer 		return (NULL);
12781693d27bSConrad Meyer 	}
12791693d27bSConrad Meyer 
12802a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
12812a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
12822a4fd6b1SConrad Meyer 		    __func__);
12832a4fd6b1SConrad Meyer 		return (NULL);
12842a4fd6b1SConrad Meyer 	}
12852a4fd6b1SConrad Meyer 
12862a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
12872a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
12882a4fd6b1SConrad Meyer 	if (desc == NULL)
12892a4fd6b1SConrad Meyer 		return (NULL);
12902a4fd6b1SConrad Meyer 
12912a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
12922a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
12932a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
12942a4fd6b1SConrad Meyer 
12952a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
12962a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
12972a4fd6b1SConrad Meyer }
12982a4fd6b1SConrad Meyer 
1299e974f91cSConrad Meyer /*
1300e974f91cSConrad Meyer  * Ring Management
1301e974f91cSConrad Meyer  */
1302e974f91cSConrad Meyer static inline uint32_t
1303e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1304e974f91cSConrad Meyer {
1305e974f91cSConrad Meyer 
1306e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1307e974f91cSConrad Meyer }
1308e974f91cSConrad Meyer 
1309e974f91cSConrad Meyer static inline uint32_t
1310e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1311e974f91cSConrad Meyer {
1312e974f91cSConrad Meyer 
1313e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1314e974f91cSConrad Meyer }
1315e974f91cSConrad Meyer 
1316e974f91cSConrad Meyer static struct ioat_descriptor *
1317bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
1318e974f91cSConrad Meyer {
13199e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1320e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1321bf8553eaSConrad Meyer 	int error, busdmaflag;
1322e974f91cSConrad Meyer 
1323f46011aeSConrad Meyer 	error = ENOMEM;
1324f46011aeSConrad Meyer 	hw_desc = NULL;
1325f46011aeSConrad Meyer 
1326bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
1327bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
1328bf8553eaSConrad Meyer 	else
1329bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
1330bf8553eaSConrad Meyer 
1331bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
1332e974f91cSConrad Meyer 	if (desc == NULL)
1333f46011aeSConrad Meyer 		goto out;
1334e974f91cSConrad Meyer 
1335f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
1336bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
1337f46011aeSConrad Meyer 	if (hw_desc == NULL)
1338f46011aeSConrad Meyer 		goto out;
1339e974f91cSConrad Meyer 
1340faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
13419e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
1342f46011aeSConrad Meyer 
1343f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
1344f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
1345bf8553eaSConrad Meyer 	    busdmaflag);
1346f46011aeSConrad Meyer 	if (error)
1347f46011aeSConrad Meyer 		goto out;
1348f46011aeSConrad Meyer 
1349f46011aeSConrad Meyer out:
1350f46011aeSConrad Meyer 	if (error) {
1351f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
1352f46011aeSConrad Meyer 		return (NULL);
1353f46011aeSConrad Meyer 	}
1354e974f91cSConrad Meyer 	return (desc);
1355e974f91cSConrad Meyer }
1356e974f91cSConrad Meyer 
1357e974f91cSConrad Meyer static void
1358e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
1359e974f91cSConrad Meyer {
1360e974f91cSConrad Meyer 
1361e974f91cSConrad Meyer 	if (desc == NULL)
1362e974f91cSConrad Meyer 		return;
1363e974f91cSConrad Meyer 
13649e3bbf26SConrad Meyer 	if (desc->u.generic)
13659e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
1366e974f91cSConrad Meyer 		    ioat->hw_desc_map);
1367e974f91cSConrad Meyer 	free(desc, M_IOAT);
1368e974f91cSConrad Meyer }
1369e974f91cSConrad Meyer 
1370bf8553eaSConrad Meyer /*
1371bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1372bf8553eaSConrad Meyer  * for 'num_descs'.
1373bf8553eaSConrad Meyer  *
1374bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1375bf8553eaSConrad Meyer  *
1376bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1377bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1378bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1379bf8553eaSConrad Meyer  *
1380bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1381bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1382bf8553eaSConrad Meyer  *
1383bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1384bf8553eaSConrad Meyer  * assured enough room is available.)
1385bf8553eaSConrad Meyer  */
1386e974f91cSConrad Meyer static int
1387bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1388e974f91cSConrad Meyer {
1389bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
1390bf8553eaSConrad Meyer 	uint32_t order;
139125ad9585SConrad Meyer 	boolean_t dug;
1392bf8553eaSConrad Meyer 	int error;
1393e974f91cSConrad Meyer 
1394bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1395bf8553eaSConrad Meyer 	error = 0;
139625ad9585SConrad Meyer 	dug = FALSE;
1397e974f91cSConrad Meyer 
139808a6b96aSConrad Meyer 	if (num_descs < 1 || num_descs >= (1 << IOAT_MAX_ORDER)) {
1399bf8553eaSConrad Meyer 		error = EINVAL;
1400bf8553eaSConrad Meyer 		goto out;
1401e974f91cSConrad Meyer 	}
140208a6b96aSConrad Meyer 
140308a6b96aSConrad Meyer 	for (;;) {
14045f77bd3eSConrad Meyer 		if (ioat->quiescing) {
14055f77bd3eSConrad Meyer 			error = ENXIO;
14065f77bd3eSConrad Meyer 			goto out;
14075f77bd3eSConrad Meyer 		}
1408bf8553eaSConrad Meyer 
1409bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1410bf8553eaSConrad Meyer 			goto out;
1411bf8553eaSConrad Meyer 
141225ad9585SConrad Meyer 		if (!dug && !ioat->is_submitter_processing &&
141325ad9585SConrad Meyer 		    (1 << ioat->ring_size_order) > num_descs) {
141425ad9585SConrad Meyer 			ioat->is_submitter_processing = TRUE;
141525ad9585SConrad Meyer 			mtx_unlock(&ioat->submit_lock);
141625ad9585SConrad Meyer 
141725ad9585SConrad Meyer 			ioat_process_events(ioat);
141825ad9585SConrad Meyer 
141925ad9585SConrad Meyer 			mtx_lock(&ioat->submit_lock);
142025ad9585SConrad Meyer 			dug = TRUE;
142125ad9585SConrad Meyer 			KASSERT(ioat->is_submitter_processing == TRUE,
142225ad9585SConrad Meyer 			    ("is_submitter_processing"));
142325ad9585SConrad Meyer 			ioat->is_submitter_processing = FALSE;
142425ad9585SConrad Meyer 			wakeup(&ioat->tail);
142525ad9585SConrad Meyer 			continue;
142625ad9585SConrad Meyer 		}
142725ad9585SConrad Meyer 
1428bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1429bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
1430bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
1431bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
1432bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
1433bf8553eaSConrad Meyer 				continue;
1434bf8553eaSConrad Meyer 			}
1435bf8553eaSConrad Meyer 
1436bf8553eaSConrad Meyer 			error = EAGAIN;
1437bf8553eaSConrad Meyer 			break;
1438bf8553eaSConrad Meyer 		}
1439bf8553eaSConrad Meyer 
1440bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1441bf8553eaSConrad Meyer 		for (;;) {
1442bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1443bf8553eaSConrad Meyer 
1444bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
1445bf8553eaSConrad Meyer 			    TRUE, mflags);
1446bf8553eaSConrad Meyer 
1447bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1448bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1449bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1450bf8553eaSConrad Meyer 
1451bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1452bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1453bf8553eaSConrad Meyer 				    ("allocation failed"));
1454bf8553eaSConrad Meyer 				error = EAGAIN;
1455bf8553eaSConrad Meyer 				break;
1456bf8553eaSConrad Meyer 			}
1457bf8553eaSConrad Meyer 
1458bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1459bf8553eaSConrad Meyer 			if (error == 0)
1460bf8553eaSConrad Meyer 				break;
1461bf8553eaSConrad Meyer 		}
1462bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1463bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1464bf8553eaSConrad Meyer 		if (error)
1465bf8553eaSConrad Meyer 			break;
1466bf8553eaSConrad Meyer 	}
1467bf8553eaSConrad Meyer 
1468bf8553eaSConrad Meyer out:
1469bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
147008a6b96aSConrad Meyer 	KASSERT(!ioat->quiescing || error == ENXIO,
147108a6b96aSConrad Meyer 	    ("reserved during quiesce"));
1472bf8553eaSConrad Meyer 	return (error);
1473bf8553eaSConrad Meyer }
1474bf8553eaSConrad Meyer 
1475bf8553eaSConrad Meyer static struct ioat_descriptor **
1476bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1477bf8553eaSConrad Meyer     int mflags)
1478bf8553eaSConrad Meyer {
1479bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1480bf8553eaSConrad Meyer 	uint32_t i;
1481bf8553eaSConrad Meyer 	int error;
1482bf8553eaSConrad Meyer 
1483bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1484bf8553eaSConrad Meyer 
1485bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1486bf8553eaSConrad Meyer 	if (ring == NULL)
1487bf8553eaSConrad Meyer 		return (NULL);
1488bf8553eaSConrad Meyer 
1489bf8553eaSConrad Meyer 	if (need_dscr) {
1490bf8553eaSConrad Meyer 		error = ENOMEM;
1491bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1492bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1493bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1494bf8553eaSConrad Meyer 				goto out;
1495bf8553eaSConrad Meyer 			ring[i]->id = i;
1496bf8553eaSConrad Meyer 		}
1497bf8553eaSConrad Meyer 	}
1498bf8553eaSConrad Meyer 	error = 0;
1499bf8553eaSConrad Meyer 
1500bf8553eaSConrad Meyer out:
1501bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1502bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1503bf8553eaSConrad Meyer 		ring = NULL;
1504bf8553eaSConrad Meyer 	}
1505bf8553eaSConrad Meyer 	return (ring);
1506bf8553eaSConrad Meyer }
1507bf8553eaSConrad Meyer 
1508bf8553eaSConrad Meyer static void
1509bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1510bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1511bf8553eaSConrad Meyer {
1512bf8553eaSConrad Meyer 	uint32_t i;
1513bf8553eaSConrad Meyer 
1514bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1515bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1516bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1517bf8553eaSConrad Meyer 	}
1518bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1519e974f91cSConrad Meyer }
1520e974f91cSConrad Meyer 
1521e974f91cSConrad Meyer static struct ioat_descriptor *
1522e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1523e974f91cSConrad Meyer {
1524e974f91cSConrad Meyer 
1525e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1526e974f91cSConrad Meyer }
1527e974f91cSConrad Meyer 
1528bf8553eaSConrad Meyer static int
1529bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1530bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1531e974f91cSConrad Meyer {
1532bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1533e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1534bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1535bf8553eaSConrad Meyer 	int error;
1536e974f91cSConrad Meyer 
1537520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1538e974f91cSConrad Meyer 
1539bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1540bf8553eaSConrad Meyer 
1541bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1542bf8553eaSConrad Meyer 		error = EINVAL;
1543bf8553eaSConrad Meyer 		goto out;
1544bf8553eaSConrad Meyer 	}
1545bf8553eaSConrad Meyer 
1546bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1547bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1548bf8553eaSConrad Meyer 
1549bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1550bf8553eaSConrad Meyer 
1551bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1552bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1553bf8553eaSConrad Meyer 
1554bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1555bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1556bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1557e974f91cSConrad Meyer 
1558e974f91cSConrad Meyer 	/*
1559bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1560bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1561e974f91cSConrad Meyer 	 */
1562bf8553eaSConrad Meyer 	if (head < tail) {
1563bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1564bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1565e974f91cSConrad Meyer 
1566bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1567bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1568e974f91cSConrad Meyer 
1569bf8553eaSConrad Meyer 			newring[i] = tmp;
1570bf8553eaSConrad Meyer 			newring[i]->id = i;
1571bf8553eaSConrad Meyer 		}
1572bf8553eaSConrad Meyer 		head += oldsize;
1573e974f91cSConrad Meyer 	}
1574e974f91cSConrad Meyer 
1575bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1576e974f91cSConrad Meyer 
1577bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1578bf8553eaSConrad Meyer 	if (head < oldsize) {
1579bf8553eaSConrad Meyer 		i = oldsize - 1;
1580bf8553eaSConrad Meyer 		end = newsize;
1581e974f91cSConrad Meyer 	} else {
1582bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1583bf8553eaSConrad Meyer 		i = head;
1584bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1585bf8553eaSConrad Meyer 	}
1586bf8553eaSConrad Meyer 
1587e974f91cSConrad Meyer 	/*
1588bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1589bf8553eaSConrad Meyer 	 * section (tail -> head).
1590e974f91cSConrad Meyer 	 */
1591bf8553eaSConrad Meyer 	for (; i < end; i++) {
1592bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1593bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1594e974f91cSConrad Meyer 
1595bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1596bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1597e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1598e974f91cSConrad Meyer 	}
1599e974f91cSConrad Meyer 
16001be25cc9SConrad Meyer #ifdef INVARIANTS
16011be25cc9SConrad Meyer 	for (i = 0; i < newsize; i++) {
16021be25cc9SConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
16031be25cc9SConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
16041be25cc9SConrad Meyer 
16051be25cc9SConrad Meyer 		KASSERT(hw->next == next->hw_desc_bus_addr,
16061be25cc9SConrad Meyer 		    ("mismatch at i:%u (oldsize:%u); next=%p nextaddr=0x%lx"
16071be25cc9SConrad Meyer 		     " (tail:%u)", i, oldsize, next, next->hw_desc_bus_addr,
16081be25cc9SConrad Meyer 		     tail));
16091be25cc9SConrad Meyer 	}
16101be25cc9SConrad Meyer #endif
16111be25cc9SConrad Meyer 
1612e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1613bf8553eaSConrad Meyer 	ioat->ring = newring;
1614bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1615bf8553eaSConrad Meyer 	ioat->tail = tail;
1616bf8553eaSConrad Meyer 	ioat->head = head;
1617bf8553eaSConrad Meyer 	error = 0;
1618e974f91cSConrad Meyer 
1619bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1620bf8553eaSConrad Meyer out:
1621bf8553eaSConrad Meyer 	if (error)
1622bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1623bf8553eaSConrad Meyer 	return (error);
1624bf8553eaSConrad Meyer }
1625bf8553eaSConrad Meyer 
1626bf8553eaSConrad Meyer static int
1627bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1628bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1629bf8553eaSConrad Meyer {
1630bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1631bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1632bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1633bf8553eaSConrad Meyer 	int error;
1634bf8553eaSConrad Meyer 
1635520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
1636bf8553eaSConrad Meyer 
1637bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1638bf8553eaSConrad Meyer 
1639bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1640bf8553eaSConrad Meyer 		error = EINVAL;
1641bf8553eaSConrad Meyer 		goto out_unlocked;
1642bf8553eaSConrad Meyer 	}
1643bf8553eaSConrad Meyer 
1644bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1645bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1646bf8553eaSConrad Meyer 
1647bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1648bf8553eaSConrad Meyer 
1649bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1650bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1651bf8553eaSConrad Meyer 		error = ENOMEM;
1652bf8553eaSConrad Meyer 		goto out;
1653bf8553eaSConrad Meyer 	}
1654bf8553eaSConrad Meyer 
1655bf8553eaSConrad Meyer 	/*
1656bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1657bf8553eaSConrad Meyer 	 * descriptors.
1658bf8553eaSConrad Meyer 	 */
1659bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1660bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1661bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1662bf8553eaSConrad Meyer 
1663bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1664bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1665bf8553eaSConrad Meyer 	}
1666bf8553eaSConrad Meyer 
1667bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1668bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1669bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1670bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1671bf8553eaSConrad Meyer 	}
1672bf8553eaSConrad Meyer 
1673bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1674bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1675bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1676bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1677bf8553eaSConrad Meyer 
16781be25cc9SConrad Meyer #ifdef INVARIANTS
16791be25cc9SConrad Meyer 	for (i = 0; i < newsize; i++) {
16801be25cc9SConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
16811be25cc9SConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
16821be25cc9SConrad Meyer 
16831be25cc9SConrad Meyer 		KASSERT(hw->next == next->hw_desc_bus_addr,
16841be25cc9SConrad Meyer 		    ("mismatch at i:%u (newsize:%u); next=%p nextaddr=0x%lx "
16851be25cc9SConrad Meyer 		     "(tail:%u)", i, newsize, next, next->hw_desc_bus_addr,
16861be25cc9SConrad Meyer 		     ioat->tail));
16871be25cc9SConrad Meyer 	}
16881be25cc9SConrad Meyer #endif
16891be25cc9SConrad Meyer 
1690bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1691bf8553eaSConrad Meyer 	ioat->ring = newring;
1692bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1693bf8553eaSConrad Meyer 	error = 0;
1694bf8553eaSConrad Meyer 
1695bf8553eaSConrad Meyer out:
1696bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1697bf8553eaSConrad Meyer out_unlocked:
1698bf8553eaSConrad Meyer 	if (error)
1699bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1700bf8553eaSConrad Meyer 	return (error);
1701e974f91cSConrad Meyer }
1702e974f91cSConrad Meyer 
1703e974f91cSConrad Meyer static void
17048f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1705e974f91cSConrad Meyer {
1706e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
17078f274637SConrad Meyer 
170859acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
170959acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
17108f274637SConrad Meyer 	if (chanerr == 0)
17118f274637SConrad Meyer 		return;
17128f274637SConrad Meyer 
1713faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1714faefad9cSConrad Meyer 
17158f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
17168f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
17178f274637SConrad Meyer 
17188f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
17198f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
17208f274637SConrad Meyer }
17218f274637SConrad Meyer 
17228f274637SConrad Meyer static void
17235ac77963SConrad Meyer ioat_poll_timer_callback(void *arg)
17245ac77963SConrad Meyer {
17255ac77963SConrad Meyer 	struct ioat_softc *ioat;
17265ac77963SConrad Meyer 
17275ac77963SConrad Meyer 	ioat = arg;
17285ac77963SConrad Meyer 	ioat_log_message(3, "%s\n", __func__);
17295ac77963SConrad Meyer 
17305ac77963SConrad Meyer 	ioat_process_events(ioat);
17315ac77963SConrad Meyer }
17325ac77963SConrad Meyer 
17335ac77963SConrad Meyer static void
17345ac77963SConrad Meyer ioat_shrink_timer_callback(void *arg)
17358f274637SConrad Meyer {
1736bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1737e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1738faefad9cSConrad Meyer 	uint32_t order;
1739e974f91cSConrad Meyer 
1740e974f91cSConrad Meyer 	ioat = arg;
1741fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1742e974f91cSConrad Meyer 
1743faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1744e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1745fe8712f8SConrad Meyer 
1746fe8712f8SConrad Meyer 	/* Don't run while the hardware is being reset. */
1747fe8712f8SConrad Meyer 	if (ioat->resetting) {
1748fe8712f8SConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1749fe8712f8SConrad Meyer 		return;
1750fe8712f8SConrad Meyer 	}
1751fe8712f8SConrad Meyer 
1752bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1753c62db395SConrad Meyer 	if (ioat->is_completion_pending || ioat->is_resize_pending ||
1754c62db395SConrad Meyer 	    order == IOAT_MIN_ORDER) {
1755bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1756bf8553eaSConrad Meyer 		goto out;
1757bf8553eaSConrad Meyer 	}
1758bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1759e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1760e974f91cSConrad Meyer 
1761bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1762bf8553eaSConrad Meyer 	    M_NOWAIT);
1763bf8553eaSConrad Meyer 
1764bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1765bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1766bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1767bf8553eaSConrad Meyer 
1768c62db395SConrad Meyer 	if (newring != NULL && !ioat->is_completion_pending)
1769bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1770c62db395SConrad Meyer 	else if (newring != NULL)
1771c62db395SConrad Meyer 		ioat_free_ring(ioat, (1 << (order - 1)), newring);
1772bf8553eaSConrad Meyer 
1773bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1774bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1775bf8553eaSConrad Meyer 
1776bf8553eaSConrad Meyer out:
1777e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1778db16ab7eSConrad Meyer 		callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD,
17795ac77963SConrad Meyer 		    ioat_shrink_timer_callback, ioat);
1780e974f91cSConrad Meyer }
1781e974f91cSConrad Meyer 
1782e974f91cSConrad Meyer /*
1783e974f91cSConrad Meyer  * Support Functions
1784e974f91cSConrad Meyer  */
1785e974f91cSConrad Meyer static void
1786e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1787e974f91cSConrad Meyer {
1788e974f91cSConrad Meyer 
1789466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1790e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1791bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1792e974f91cSConrad Meyer 
1793e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1794e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
17955ac77963SConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
17965ac77963SConrad Meyer 		    ioat);
17975ac77963SConrad Meyer 		callout_stop(&ioat->shrink_timer);
1798e974f91cSConrad Meyer 	}
179901fbbc88SConrad Meyer 
180001fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1801e974f91cSConrad Meyer }
1802e974f91cSConrad Meyer 
1803e974f91cSConrad Meyer static int
1804e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1805e974f91cSConrad Meyer {
1806e974f91cSConrad Meyer 	uint64_t status;
1807e974f91cSConrad Meyer 	uint32_t chanerr;
1808cea5b880SConrad Meyer 	unsigned timeout;
18095f77bd3eSConrad Meyer 	int error;
18105f77bd3eSConrad Meyer 
1811520f6023SConrad Meyer 	CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx);
18126d41e1edSConrad Meyer 
18135f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
181493f7f84aSConrad Meyer 	while (ioat->resetting && !ioat->destroying)
181593f7f84aSConrad Meyer 		msleep(&ioat->resetting, IOAT_REFLK, 0, "IRH_drain", 0);
181693f7f84aSConrad Meyer 	if (ioat->destroying) {
181793f7f84aSConrad Meyer 		mtx_unlock(IOAT_REFLK);
181893f7f84aSConrad Meyer 		return (ENXIO);
181993f7f84aSConrad Meyer 	}
182093f7f84aSConrad Meyer 	ioat->resetting = TRUE;
182193f7f84aSConrad Meyer 
18225f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
18235f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
18245f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1825e974f91cSConrad Meyer 
1826fe8712f8SConrad Meyer 	/*
1827fe8712f8SConrad Meyer 	 * Suspend ioat_process_events while the hardware and softc are in an
1828fe8712f8SConrad Meyer 	 * indeterminate state.
1829fe8712f8SConrad Meyer 	 */
1830fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1831fe8712f8SConrad Meyer 	ioat->resetting_cleanup = TRUE;
1832fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1833fe8712f8SConrad Meyer 
1834e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1835e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1836e974f91cSConrad Meyer 		ioat_suspend(ioat);
1837e974f91cSConrad Meyer 
1838e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1839e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1840e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1841e974f91cSConrad Meyer 		DELAY(1000);
1842e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1843e974f91cSConrad Meyer 	}
18445f77bd3eSConrad Meyer 	if (timeout == 20) {
18455f77bd3eSConrad Meyer 		error = ETIMEDOUT;
18465f77bd3eSConrad Meyer 		goto out;
18475f77bd3eSConrad Meyer 	}
1848e974f91cSConrad Meyer 
1849cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1850cea5b880SConrad Meyer 
1851e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1852e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1853e974f91cSConrad Meyer 
1854e974f91cSConrad Meyer 	/*
1855e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1856e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1857e974f91cSConrad Meyer 	 */
1858e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1859e974f91cSConrad Meyer 	    4);
1860e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1861e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1862e974f91cSConrad Meyer 
18630d1a05d9SConrad Meyer 	/*
18640d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
18650d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
18660d1a05d9SConrad Meyer 	 */
1867f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1868f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
18690d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1870f7157235SConrad Meyer 	}
18710d1a05d9SConrad Meyer 
1872e974f91cSConrad Meyer 	ioat_reset(ioat);
1873e974f91cSConrad Meyer 
1874e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1875e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1876e974f91cSConrad Meyer 		DELAY(1000);
18775f77bd3eSConrad Meyer 	if (timeout == 20) {
18785f77bd3eSConrad Meyer 		error = ETIMEDOUT;
18795f77bd3eSConrad Meyer 		goto out;
18805f77bd3eSConrad Meyer 	}
1881e974f91cSConrad Meyer 
1882f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1883f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
18840d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1885f7157235SConrad Meyer 	}
18864253ea50SConrad Meyer 
1887cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1888cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1889cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1890cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1891cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1892cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
18935f77bd3eSConrad Meyer 		error = 0;
18945f77bd3eSConrad Meyer 		goto out;
1895e974f91cSConrad Meyer 	}
1896e974f91cSConrad Meyer 
1897cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
18985f77bd3eSConrad Meyer 	if (chanerr != 0) {
1899faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1900faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1901faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
19025f77bd3eSConrad Meyer 		error = EIO;
19035f77bd3eSConrad Meyer 		goto out;
19045f77bd3eSConrad Meyer 	}
1905cea5b880SConrad Meyer 
1906cea5b880SConrad Meyer 	/*
1907cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1908cea5b880SConrad Meyer 	 * device back to active.
1909cea5b880SConrad Meyer 	 *
1910cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1911cea5b880SConrad Meyer 	 * at zero as well.
1912cea5b880SConrad Meyer 	 */
1913bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1914cea5b880SConrad Meyer 	ioat->last_seen = 0;
1915fe8712f8SConrad Meyer 	*ioat->comp_update = 0;
1916c114e741SConrad Meyer 	KASSERT(!ioat->is_completion_pending, ("bogus completion_pending"));
1917cea5b880SConrad Meyer 
1918cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1919cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1920cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
19215f77bd3eSConrad Meyer 	error = 0;
19225f77bd3eSConrad Meyer 
19235f77bd3eSConrad Meyer out:
1924*1bbc06b8SConrad Meyer 	/* Enqueues a null operation and ensures it completes. */
1925*1bbc06b8SConrad Meyer 	if (error == 0)
1926*1bbc06b8SConrad Meyer 		error = ioat_start_channel(ioat);
1927*1bbc06b8SConrad Meyer 
1928fe8712f8SConrad Meyer 	/*
1929fe8712f8SConrad Meyer 	 * Resume completions now that ring state is consistent.
1930fe8712f8SConrad Meyer 	 */
1931fe8712f8SConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1932fe8712f8SConrad Meyer 	ioat->resetting_cleanup = FALSE;
1933fe8712f8SConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
193493f7f84aSConrad Meyer 
1935fe8712f8SConrad Meyer 	/* Unblock submission of new work */
1936fe8712f8SConrad Meyer 	mtx_lock(IOAT_REFLK);
1937fe8712f8SConrad Meyer 	ioat->quiescing = FALSE;
1938fe8712f8SConrad Meyer 	wakeup(&ioat->quiescing);
1939fe8712f8SConrad Meyer 
1940fe8712f8SConrad Meyer 	ioat->resetting = FALSE;
1941fe8712f8SConrad Meyer 	wakeup(&ioat->resetting);
1942*1bbc06b8SConrad Meyer 
1943*1bbc06b8SConrad Meyer 	if (ioat->is_completion_pending)
1944*1bbc06b8SConrad Meyer 		callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback,
1945*1bbc06b8SConrad Meyer 		    ioat);
1946fe8712f8SConrad Meyer 	mtx_unlock(IOAT_REFLK);
1947fe8712f8SConrad Meyer 
19485f77bd3eSConrad Meyer 	return (error);
1949cea5b880SConrad Meyer }
1950cea5b880SConrad Meyer 
1951f7157235SConrad Meyer static int
1952faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1953faefad9cSConrad Meyer {
1954faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1955faefad9cSConrad Meyer 	struct sbuf sb;
1956faefad9cSConrad Meyer 	uint64_t status;
1957faefad9cSConrad Meyer 	int error;
1958faefad9cSConrad Meyer 
1959faefad9cSConrad Meyer 	ioat = arg1;
1960faefad9cSConrad Meyer 
1961faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1962faefad9cSConrad Meyer 
1963faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1964faefad9cSConrad Meyer 	switch (status) {
1965faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1966faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1967faefad9cSConrad Meyer 		break;
1968faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1969faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1970faefad9cSConrad Meyer 		break;
1971faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1972faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1973faefad9cSConrad Meyer 		break;
1974faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1975faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1976faefad9cSConrad Meyer 		break;
1977faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1978faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1979faefad9cSConrad Meyer 		break;
1980faefad9cSConrad Meyer 	default:
1981faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1982faefad9cSConrad Meyer 		break;
1983faefad9cSConrad Meyer 	}
1984faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1985faefad9cSConrad Meyer 	sbuf_delete(&sb);
1986faefad9cSConrad Meyer 
1987faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1988faefad9cSConrad Meyer 		return (error);
1989faefad9cSConrad Meyer 	return (EINVAL);
1990faefad9cSConrad Meyer }
1991faefad9cSConrad Meyer 
1992faefad9cSConrad Meyer static int
199301fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
199401fbbc88SConrad Meyer {
199501fbbc88SConrad Meyer 	struct ioat_softc *ioat;
199601fbbc88SConrad Meyer 	struct sbuf sb;
199701fbbc88SConrad Meyer #define	PRECISION	"1"
199801fbbc88SConrad Meyer 	const uintmax_t factor = 10;
199901fbbc88SConrad Meyer 	uintmax_t rate;
200001fbbc88SConrad Meyer 	int error;
200101fbbc88SConrad Meyer 
200201fbbc88SConrad Meyer 	ioat = arg1;
200301fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
200401fbbc88SConrad Meyer 
200501fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
200601fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
200701fbbc88SConrad Meyer 		goto out;
200801fbbc88SConrad Meyer 	}
200901fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
201001fbbc88SConrad Meyer 	    ioat->stats.interrupts;
201101fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
201201fbbc88SConrad Meyer 	    rate % factor);
201301fbbc88SConrad Meyer #undef	PRECISION
201401fbbc88SConrad Meyer out:
201501fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
201601fbbc88SConrad Meyer 	sbuf_delete(&sb);
201701fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
201801fbbc88SConrad Meyer 		return (error);
201901fbbc88SConrad Meyer 	return (EINVAL);
202001fbbc88SConrad Meyer }
202101fbbc88SConrad Meyer 
202201fbbc88SConrad Meyer static int
2023f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
2024f7157235SConrad Meyer {
2025f7157235SConrad Meyer 	struct ioat_softc *ioat;
2026f7157235SConrad Meyer 	int error, arg;
2027f7157235SConrad Meyer 
2028f7157235SConrad Meyer 	ioat = arg1;
2029f7157235SConrad Meyer 
2030f7157235SConrad Meyer 	arg = 0;
2031f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
2032f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
2033f7157235SConrad Meyer 		return (error);
2034f7157235SConrad Meyer 
2035f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
2036f7157235SConrad Meyer 	if (error != 0)
2037f7157235SConrad Meyer 		return (error);
2038f7157235SConrad Meyer 
2039f7157235SConrad Meyer 	if (arg != 0)
2040f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
2041f7157235SConrad Meyer 
2042f7157235SConrad Meyer 	return (error);
2043f7157235SConrad Meyer }
2044f7157235SConrad Meyer 
2045e974f91cSConrad Meyer static void
2046e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
2047e974f91cSConrad Meyer {
2048e974f91cSConrad Meyer 	int i, j;
2049e974f91cSConrad Meyer 
2050e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
2051e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
2052e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
2053e974f91cSConrad Meyer 		printf("\n");
2054e974f91cSConrad Meyer 	}
2055e974f91cSConrad Meyer }
2056e974f91cSConrad Meyer 
2057e974f91cSConrad Meyer static void
2058e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
2059e974f91cSConrad Meyer {
206001fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
2061f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
206201fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
2063e974f91cSConrad Meyer 	struct ioat_softc *ioat;
2064e974f91cSConrad Meyer 
2065e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
2066f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
2067f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
2068f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
2069e974f91cSConrad Meyer 
207065e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
207165e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
207265e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
207365e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
20745ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
20755ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
20765ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
20775ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
20785ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
207965e4f8adSConrad Meyer 
208001fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
208101fbbc88SConrad Meyer 	    "IOAT channel internal state");
208201fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
208301fbbc88SConrad Meyer 
208401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
2085bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
208601fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
208701fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
208801fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
208901fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
209001fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
2091bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
2092f7157235SConrad Meyer 
209301fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
209465e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
209565e4f8adSConrad Meyer 
209601fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
209765e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
209825ad9585SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing",
209925ad9585SConrad Meyer 	    CTLFLAG_RD, &ioat->is_submitter_processing, 0,
210025ad9585SConrad Meyer 	    "submitter processing");
210101fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
210201fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
210301fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
210465e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
210501fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
210665e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
210765e4f8adSConrad Meyer 
210801fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
2109faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
2110faefad9cSConrad Meyer 	    "String of the channel status");
211101fbbc88SConrad Meyer 
21125ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
21135ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
21145ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
21155ca9fc2aSConrad Meyer 
211601fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
211701fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
211801fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
211901fbbc88SConrad Meyer 
212001fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
212101fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
212201fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
212301fbbc88SConrad Meyer 
212401fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
212501fbbc88SConrad Meyer 	    "IOAT channel statistics");
212601fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
212701fbbc88SConrad Meyer 
212801fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
212901fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
213001fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
213101fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
213201fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
213301fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
213401fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
213501fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
213601fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
213701fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
213801fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
213901fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
214001fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
214101fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
214201fbbc88SConrad Meyer 	    "Number of times the channel has halted");
214301fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
214401fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
214501fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
214601fbbc88SConrad Meyer 
214701fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
214801fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
214901fbbc88SConrad Meyer 	    "Descriptors per interrupt");
2150e974f91cSConrad Meyer }
2151466b3540SConrad Meyer 
2152466b3540SConrad Meyer static inline struct ioat_softc *
2153466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
2154466b3540SConrad Meyer {
2155466b3540SConrad Meyer 	uint32_t old;
2156466b3540SConrad Meyer 
2157466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
2158466b3540SConrad Meyer 
2159466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
2160466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
2161466b3540SConrad Meyer 
2162466b3540SConrad Meyer #ifdef INVARIANTS
2163466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
2164466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
2165466b3540SConrad Meyer #endif
2166466b3540SConrad Meyer 
2167466b3540SConrad Meyer 	return (ioat);
2168466b3540SConrad Meyer }
2169466b3540SConrad Meyer 
2170466b3540SConrad Meyer static inline void
2171466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
2172466b3540SConrad Meyer {
2173faefad9cSConrad Meyer 
2174faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
2175faefad9cSConrad Meyer }
2176faefad9cSConrad Meyer 
2177faefad9cSConrad Meyer static inline void
2178faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
2179faefad9cSConrad Meyer {
2180faefad9cSConrad Meyer 
2181faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
2182faefad9cSConrad Meyer }
2183faefad9cSConrad Meyer 
2184faefad9cSConrad Meyer static inline void
2185faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
2186faefad9cSConrad Meyer     boolean_t locked)
2187faefad9cSConrad Meyer {
2188466b3540SConrad Meyer 	uint32_t old;
2189466b3540SConrad Meyer 
2190466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
2191466b3540SConrad Meyer 
2192466b3540SConrad Meyer 	if (n == 0)
2193466b3540SConrad Meyer 		return;
2194466b3540SConrad Meyer 
2195466b3540SConrad Meyer #ifdef INVARIANTS
2196466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
2197466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
2198466b3540SConrad Meyer #endif
2199466b3540SConrad Meyer 
2200466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
2201466b3540SConrad Meyer 	for (;;) {
2202466b3540SConrad Meyer 		old = ioat->refcnt;
2203466b3540SConrad Meyer 		if (old <= n)
2204466b3540SConrad Meyer 			break;
2205466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
2206466b3540SConrad Meyer 			return;
2207466b3540SConrad Meyer 	}
2208466b3540SConrad Meyer 
2209faefad9cSConrad Meyer 	if (locked)
2210faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
2211faefad9cSConrad Meyer 	else
2212466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
2213faefad9cSConrad Meyer 
2214466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
2215466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
2216466b3540SConrad Meyer 
2217466b3540SConrad Meyer 	if (old == n)
2218466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
2219faefad9cSConrad Meyer 	if (!locked)
2220466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
2221466b3540SConrad Meyer }
2222466b3540SConrad Meyer 
2223466b3540SConrad Meyer static inline void
2224466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
2225466b3540SConrad Meyer {
2226466b3540SConrad Meyer 
2227466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
2228466b3540SConrad Meyer }
2229466b3540SConrad Meyer 
2230466b3540SConrad Meyer static void
22315f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
2232466b3540SConrad Meyer {
2233466b3540SConrad Meyer 
22345f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
2235466b3540SConrad Meyer 	while (ioat->refcnt > 0)
2236466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
2237466b3540SConrad Meyer }
22380bb35debSConrad Meyer 
22390bb35debSConrad Meyer #ifdef DDB
22400bb35debSConrad Meyer #define	_db_show_lock(lo)	LOCK_CLASS(lo)->lc_ddb_show(lo)
22410bb35debSConrad Meyer #define	db_show_lock(lk)	_db_show_lock(&(lk)->lock_object)
22420bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat)
22430bb35debSConrad Meyer {
22440bb35debSConrad Meyer 	struct ioat_softc *sc;
22450bb35debSConrad Meyer 	unsigned idx;
22460bb35debSConrad Meyer 
22470bb35debSConrad Meyer 	if (!have_addr)
22480bb35debSConrad Meyer 		goto usage;
22490bb35debSConrad Meyer 	idx = (unsigned)addr;
225035b7a45eSConrad Meyer 	if (idx >= ioat_channel_index)
22510bb35debSConrad Meyer 		goto usage;
22520bb35debSConrad Meyer 
22530bb35debSConrad Meyer 	sc = ioat_channel[idx];
22540bb35debSConrad Meyer 	db_printf("ioat softc at %p\n", sc);
22550bb35debSConrad Meyer 	if (sc == NULL)
22560bb35debSConrad Meyer 		return;
22570bb35debSConrad Meyer 
22580bb35debSConrad Meyer 	db_printf(" version: %d\n", sc->version);
22590bb35debSConrad Meyer 	db_printf(" chan_idx: %u\n", sc->chan_idx);
22600bb35debSConrad Meyer 	db_printf(" submit_lock: ");
22610bb35debSConrad Meyer 	db_show_lock(&sc->submit_lock);
22620bb35debSConrad Meyer 
22630bb35debSConrad Meyer 	db_printf(" capabilities: %b\n", (int)sc->capabilities,
22640bb35debSConrad Meyer 	    IOAT_DMACAP_STR);
22650bb35debSConrad Meyer 	db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay);
22660bb35debSConrad Meyer 	db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update);
22670bb35debSConrad Meyer 
22685ac77963SConrad Meyer 	db_printf(" poll_timer:\n");
22695ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time);
22705ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->poll_timer.c_arg);
22715ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->poll_timer.c_func);
22725ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->poll_timer.c_lock);
22735ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags);
22745ac77963SConrad Meyer 
22755ac77963SConrad Meyer 	db_printf(" shrink_timer:\n");
22765ac77963SConrad Meyer 	db_printf("  c_time: %ju\n", (uintmax_t)sc->shrink_timer.c_time);
22775ac77963SConrad Meyer 	db_printf("  c_arg: %p\n", sc->shrink_timer.c_arg);
22785ac77963SConrad Meyer 	db_printf("  c_func: %p\n", sc->shrink_timer.c_func);
22795ac77963SConrad Meyer 	db_printf("  c_lock: %p\n", sc->shrink_timer.c_lock);
22805ac77963SConrad Meyer 	db_printf("  c_flags: 0x%x\n", (unsigned)sc->shrink_timer.c_flags);
22810bb35debSConrad Meyer 
22820bb35debSConrad Meyer 	db_printf(" quiescing: %d\n", (int)sc->quiescing);
22830bb35debSConrad Meyer 	db_printf(" destroying: %d\n", (int)sc->destroying);
22840bb35debSConrad Meyer 	db_printf(" is_resize_pending: %d\n", (int)sc->is_resize_pending);
228525ad9585SConrad Meyer 	db_printf(" is_submitter_processing: %d\n",
228625ad9585SConrad Meyer 	    (int)sc->is_submitter_processing);
22870bb35debSConrad Meyer 	db_printf(" is_completion_pending: %d\n", (int)sc->is_completion_pending);
22880bb35debSConrad Meyer 	db_printf(" is_reset_pending: %d\n", (int)sc->is_reset_pending);
22890bb35debSConrad Meyer 	db_printf(" is_channel_running: %d\n", (int)sc->is_channel_running);
22900bb35debSConrad Meyer 	db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported);
229193f7f84aSConrad Meyer 	db_printf(" resetting: %d\n", (int)sc->resetting);
22920bb35debSConrad Meyer 
22930bb35debSConrad Meyer 	db_printf(" head: %u\n", sc->head);
22940bb35debSConrad Meyer 	db_printf(" tail: %u\n", sc->tail);
22950bb35debSConrad Meyer 	db_printf(" hw_head: %u\n", sc->hw_head);
22960bb35debSConrad Meyer 	db_printf(" ring_size_order: %u\n", sc->ring_size_order);
22970bb35debSConrad Meyer 	db_printf(" last_seen: 0x%lx\n", sc->last_seen);
22980bb35debSConrad Meyer 	db_printf(" ring: %p\n", sc->ring);
22990bb35debSConrad Meyer 
230035b7a45eSConrad Meyer 	db_printf("  ring[%u] (tail):\n", sc->tail %
230135b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
230235b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id);
230335b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
230435b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->tail)->hw_desc_bus_addr);
230535b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
230635b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->tail)->u.generic->next);
230735b7a45eSConrad Meyer 
230835b7a45eSConrad Meyer 	db_printf("  ring[%u] (head - 1):\n", (sc->head - 1) %
230935b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
231035b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id);
231135b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
231235b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->head - 1)->hw_desc_bus_addr);
231335b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
231435b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->head - 1)->u.generic->next);
231535b7a45eSConrad Meyer 
231635b7a45eSConrad Meyer 	db_printf("  ring[%u] (head):\n", (sc->head) %
231735b7a45eSConrad Meyer 	    (1 << sc->ring_size_order));
231835b7a45eSConrad Meyer 	db_printf("   id: %u\n", ioat_get_ring_entry(sc, sc->head)->id);
231935b7a45eSConrad Meyer 	db_printf("   addr: 0x%lx\n",
232035b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->head)->hw_desc_bus_addr);
232135b7a45eSConrad Meyer 	db_printf("   next: 0x%lx\n",
232235b7a45eSConrad Meyer 	    ioat_get_ring_entry(sc, sc->head)->u.generic->next);
232335b7a45eSConrad Meyer 
232435b7a45eSConrad Meyer 	for (idx = 0; idx < (1 << sc->ring_size_order); idx++)
232535b7a45eSConrad Meyer 		if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK)
232635b7a45eSConrad Meyer 		    == ioat_get_ring_entry(sc, idx)->hw_desc_bus_addr)
232735b7a45eSConrad Meyer 			db_printf("  ring[%u] == hardware tail\n", idx);
232835b7a45eSConrad Meyer 
23290bb35debSConrad Meyer 	db_printf(" cleanup_lock: ");
23300bb35debSConrad Meyer 	db_show_lock(&sc->cleanup_lock);
23310bb35debSConrad Meyer 
23320bb35debSConrad Meyer 	db_printf(" refcnt: %u\n", sc->refcnt);
23330bb35debSConrad Meyer #ifdef INVARIANTS
23340bb35debSConrad Meyer 	CTASSERT(IOAT_NUM_REF_KINDS == 2);
23350bb35debSConrad Meyer 	db_printf(" refkinds: [ENG=%u, DESCR=%u]\n", sc->refkinds[0],
23360bb35debSConrad Meyer 	    sc->refkinds[1]);
23370bb35debSConrad Meyer #endif
23380bb35debSConrad Meyer 	db_printf(" stats:\n");
23390bb35debSConrad Meyer 	db_printf("  interrupts: %lu\n", sc->stats.interrupts);
23400bb35debSConrad Meyer 	db_printf("  descriptors_processed: %lu\n", sc->stats.descriptors_processed);
23410bb35debSConrad Meyer 	db_printf("  descriptors_error: %lu\n", sc->stats.descriptors_error);
23420bb35debSConrad Meyer 	db_printf("  descriptors_submitted: %lu\n", sc->stats.descriptors_submitted);
23430bb35debSConrad Meyer 
23440bb35debSConrad Meyer 	db_printf("  channel_halts: %u\n", sc->stats.channel_halts);
23450bb35debSConrad Meyer 	db_printf("  last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr);
23460bb35debSConrad Meyer 
23470bb35debSConrad Meyer 	if (db_pager_quit)
23480bb35debSConrad Meyer 		return;
23490bb35debSConrad Meyer 
23500bb35debSConrad Meyer 	db_printf(" hw status:\n");
23510bb35debSConrad Meyer 	db_printf("  status: 0x%lx\n", ioat_get_chansts(sc));
23520bb35debSConrad Meyer 	db_printf("  chanctrl: 0x%x\n",
23530bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET));
23540bb35debSConrad Meyer 	db_printf("  chancmd: 0x%x\n",
23550bb35debSConrad Meyer 	    (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET));
23560bb35debSConrad Meyer 	db_printf("  dmacount: 0x%x\n",
23570bb35debSConrad Meyer 	    (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET));
23580bb35debSConrad Meyer 	db_printf("  chainaddr: 0x%lx\n",
23590bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW));
23600bb35debSConrad Meyer 	db_printf("  chancmp: 0x%lx\n",
23610bb35debSConrad Meyer 	    ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW));
23620bb35debSConrad Meyer 	db_printf("  chanerr: %b\n",
23630bb35debSConrad Meyer 	    (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR);
23640bb35debSConrad Meyer 	return;
23650bb35debSConrad Meyer usage:
23660bb35debSConrad Meyer 	db_printf("usage: show ioat <0-%u>\n", ioat_channel_index);
23670bb35debSConrad Meyer 	return;
23680bb35debSConrad Meyer }
23690bb35debSConrad Meyer #endif /* DDB */
2370