xref: /freebsd/sys/dev/ioat/ioat.c (revision df1928aa33de9ffb7e181afa3b7b904171fa1757)
1e974f91cSConrad Meyer /*-
2e974f91cSConrad Meyer  * Copyright (C) 2012 Intel Corporation
3e974f91cSConrad Meyer  * All rights reserved.
4e974f91cSConrad Meyer  *
5e974f91cSConrad Meyer  * Redistribution and use in source and binary forms, with or without
6e974f91cSConrad Meyer  * modification, are permitted provided that the following conditions
7e974f91cSConrad Meyer  * are met:
8e974f91cSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
9e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
10e974f91cSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
11e974f91cSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
12e974f91cSConrad Meyer  *    documentation and/or other materials provided with the distribution.
13e974f91cSConrad Meyer  *
14e974f91cSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e974f91cSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e974f91cSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e974f91cSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e974f91cSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e974f91cSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e974f91cSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e974f91cSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e974f91cSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e974f91cSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e974f91cSConrad Meyer  * SUCH DAMAGE.
25e974f91cSConrad Meyer  */
26e974f91cSConrad Meyer 
27e974f91cSConrad Meyer #include <sys/cdefs.h>
28e974f91cSConrad Meyer __FBSDID("$FreeBSD$");
29e974f91cSConrad Meyer 
30e974f91cSConrad Meyer #include <sys/param.h>
31e974f91cSConrad Meyer #include <sys/systm.h>
32e974f91cSConrad Meyer #include <sys/bus.h>
33e974f91cSConrad Meyer #include <sys/conf.h>
34e974f91cSConrad Meyer #include <sys/ioccom.h>
35e974f91cSConrad Meyer #include <sys/kernel.h>
36e974f91cSConrad Meyer #include <sys/lock.h>
37e974f91cSConrad Meyer #include <sys/malloc.h>
38e974f91cSConrad Meyer #include <sys/module.h>
39e974f91cSConrad Meyer #include <sys/mutex.h>
40e974f91cSConrad Meyer #include <sys/rman.h>
41faefad9cSConrad Meyer #include <sys/sbuf.h>
42e974f91cSConrad Meyer #include <sys/sysctl.h>
43374b05e1SConrad Meyer #include <sys/taskqueue.h>
44e974f91cSConrad Meyer #include <sys/time.h>
45e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
46e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
47e974f91cSConrad Meyer #include <machine/bus.h>
48e974f91cSConrad Meyer #include <machine/resource.h>
49e974f91cSConrad Meyer #include <machine/stdarg.h>
50e974f91cSConrad Meyer 
51e974f91cSConrad Meyer #include "ioat.h"
52e974f91cSConrad Meyer #include "ioat_hw.h"
53e974f91cSConrad Meyer #include "ioat_internal.h"
54e974f91cSConrad Meyer 
55519e8baaSConrad Meyer #ifndef	BUS_SPACE_MAXADDR_40BIT
56519e8baaSConrad Meyer #define	BUS_SPACE_MAXADDR_40BIT	0xFFFFFFFFFFULL
57519e8baaSConrad Meyer #endif
58fe720f5aSConrad Meyer #define	IOAT_INTR_TIMO	(hz / 10)
59466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
60fe720f5aSConrad Meyer 
61e974f91cSConrad Meyer static int ioat_probe(device_t device);
62e974f91cSConrad Meyer static int ioat_attach(device_t device);
63e974f91cSConrad Meyer static int ioat_detach(device_t device);
644253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
654253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
66e974f91cSConrad Meyer static int ioat3_attach(device_t device);
67cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
68e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
69e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
70e974f91cSConrad Meyer     int error);
71e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
720d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
73faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
74e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
75e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
76e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
77bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
78bf8553eaSConrad Meyer     struct ioat_descriptor **);
79e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat,
80e974f91cSConrad Meyer     struct ioat_descriptor *desc);
81bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *,
82bf8553eaSConrad Meyer     int mflags);
83bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
84e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat,
85e974f91cSConrad Meyer     uint32_t index);
86bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *,
87bf8553eaSConrad Meyer     uint32_t size, boolean_t need_dscr, int mflags);
88bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder,
89bf8553eaSConrad Meyer     struct ioat_descriptor **);
90bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder,
91bf8553eaSConrad Meyer     struct ioat_descriptor **);
92faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
93e974f91cSConrad Meyer static void ioat_timer_callback(void *arg);
94e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
95e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
96e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
97e974f91cSConrad Meyer     int error);
98e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
99374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int);
100e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
101f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
102466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
103466b3540SConrad Meyer     enum ioat_ref_kind);
104466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
105faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
106faefad9cSConrad Meyer     enum ioat_ref_kind, boolean_t);
107466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
108466b3540SConrad Meyer     enum ioat_ref_kind);
109faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
110faefad9cSConrad Meyer     enum ioat_ref_kind);
1115f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
112e974f91cSConrad Meyer 
1131c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1141c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1151c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1161c25420eSConrad Meyer 	}								\
1171c25420eSConrad Meyer } while (0)
1181c25420eSConrad Meyer 
119e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
120e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
121e974f91cSConrad Meyer 
122e974f91cSConrad Meyer static int g_force_legacy_interrupts;
123e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
124e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
125e974f91cSConrad Meyer 
1261c25420eSConrad Meyer int g_ioat_debug_level = 0;
127e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
128e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
129e974f91cSConrad Meyer 
130e974f91cSConrad Meyer /*
131e974f91cSConrad Meyer  * OS <-> Driver interface structures
132e974f91cSConrad Meyer  */
133e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
134e974f91cSConrad Meyer 	/* Device interface */
135e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
136e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
137e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
138aa853cb4SEnji Cooper 	DEVMETHOD_END
139e974f91cSConrad Meyer };
140e974f91cSConrad Meyer 
141e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
142e974f91cSConrad Meyer 	"ioat",
143e974f91cSConrad Meyer 	ioat_pci_methods,
144e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
145e974f91cSConrad Meyer };
146e974f91cSConrad Meyer 
147e974f91cSConrad Meyer static devclass_t ioat_devclass;
148e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
149c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
150e974f91cSConrad Meyer 
151e974f91cSConrad Meyer /*
152e974f91cSConrad Meyer  * Private data structures
153e974f91cSConrad Meyer  */
154e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
155*df1928aaSConrad Meyer static unsigned ioat_channel_index = 0;
156*df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
157e974f91cSConrad Meyer     "Number of IOAT channels attached");
158e974f91cSConrad Meyer 
159e974f91cSConrad Meyer static struct _pcsid
160e974f91cSConrad Meyer {
161e974f91cSConrad Meyer 	u_int32_t   type;
162e974f91cSConrad Meyer 	const char  *desc;
163e974f91cSConrad Meyer } pci_ids[] = {
164e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
165e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
166e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
167e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
168e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
169e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
170e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
171e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
172e974f91cSConrad Meyer 
173e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
174e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
175e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
176e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
177e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
178e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
179e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
180e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
181e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
182e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
183e974f91cSConrad Meyer 
184e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
185e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
186e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
187e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
188e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
189e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
190e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
191e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
192e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
193e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
194e974f91cSConrad Meyer 
195e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
196e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
197e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
198e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
199e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
200e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
201e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
202e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
203e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
204e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
205e974f91cSConrad Meyer 
206e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
207e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
208e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
209e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
210e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
211e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
212e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
213e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
214e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
215e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
216e974f91cSConrad Meyer 
217e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
218e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
219e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
220e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
221e974f91cSConrad Meyer 
222e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
223e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
224e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
225e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
226e974f91cSConrad Meyer 
2275afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2285afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2295afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2305afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2315afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2325afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2335afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2345afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2355afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2365afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
2375afc2508SConrad Meyer 
238e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
239e974f91cSConrad Meyer };
240e974f91cSConrad Meyer 
241e974f91cSConrad Meyer /*
242e974f91cSConrad Meyer  * OS <-> Driver linkage functions
243e974f91cSConrad Meyer  */
244e974f91cSConrad Meyer static int
245e974f91cSConrad Meyer ioat_probe(device_t device)
246e974f91cSConrad Meyer {
247e974f91cSConrad Meyer 	struct _pcsid *ep;
248e974f91cSConrad Meyer 	u_int32_t type;
249e974f91cSConrad Meyer 
250e974f91cSConrad Meyer 	type = pci_get_devid(device);
251e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
252e974f91cSConrad Meyer 		if (ep->type == type) {
253e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
254e974f91cSConrad Meyer 			return (0);
255e974f91cSConrad Meyer 		}
256e974f91cSConrad Meyer 	}
257e974f91cSConrad Meyer 	return (ENXIO);
258e974f91cSConrad Meyer }
259e974f91cSConrad Meyer 
260e974f91cSConrad Meyer static int
261e974f91cSConrad Meyer ioat_attach(device_t device)
262e974f91cSConrad Meyer {
263e974f91cSConrad Meyer 	struct ioat_softc *ioat;
264e974f91cSConrad Meyer 	int error;
265e974f91cSConrad Meyer 
266e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
267e974f91cSConrad Meyer 	ioat->device = device;
268e974f91cSConrad Meyer 
269e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
270e974f91cSConrad Meyer 	if (error != 0)
271e974f91cSConrad Meyer 		goto err;
272e974f91cSConrad Meyer 
273e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
274e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
275e974f91cSConrad Meyer 		error = ENODEV;
276e974f91cSConrad Meyer 		goto err;
277e974f91cSConrad Meyer 	}
278e974f91cSConrad Meyer 
279e974f91cSConrad Meyer 	error = ioat3_attach(device);
280e974f91cSConrad Meyer 	if (error != 0)
281e974f91cSConrad Meyer 		goto err;
282e974f91cSConrad Meyer 
283e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
284e974f91cSConrad Meyer 	if (error != 0)
285e974f91cSConrad Meyer 		goto err;
286e974f91cSConrad Meyer 
287466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
288466b3540SConrad Meyer 	if (error != 0)
289466b3540SConrad Meyer 		goto err;
290466b3540SConrad Meyer 
291cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2927afbb263SConrad Meyer 	if (error != 0)
293466b3540SConrad Meyer 		goto err;
2947afbb263SConrad Meyer 
2957afbb263SConrad Meyer 	ioat_process_events(ioat);
2967afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2977afbb263SConrad Meyer 
2985f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
299e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
3007afbb263SConrad Meyer 	ioat_test_attach();
301e974f91cSConrad Meyer 
302e974f91cSConrad Meyer err:
303e974f91cSConrad Meyer 	if (error != 0)
304e974f91cSConrad Meyer 		ioat_detach(device);
305e974f91cSConrad Meyer 	return (error);
306e974f91cSConrad Meyer }
307e974f91cSConrad Meyer 
308e974f91cSConrad Meyer static int
309e974f91cSConrad Meyer ioat_detach(device_t device)
310e974f91cSConrad Meyer {
311e974f91cSConrad Meyer 	struct ioat_softc *ioat;
312e974f91cSConrad Meyer 
313e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3147afbb263SConrad Meyer 
3157afbb263SConrad Meyer 	ioat_test_detach();
316374b05e1SConrad Meyer 	taskqueue_drain(taskqueue_thread, &ioat->reset_task);
3175f77bd3eSConrad Meyer 
3185f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3195f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3200ff814e8SConrad Meyer 	ioat->destroying = TRUE;
3210ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
3220ff814e8SConrad Meyer 
3235f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3245f77bd3eSConrad Meyer 
3255f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3265f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
327fe720f5aSConrad Meyer 
328fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
329e974f91cSConrad Meyer 	callout_drain(&ioat->timer);
330e974f91cSConrad Meyer 
331e974f91cSConrad Meyer 	pci_disable_busmaster(device);
332e974f91cSConrad Meyer 
333e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
334e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
335e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
336e974f91cSConrad Meyer 
337bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
338bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
339e974f91cSConrad Meyer 
340e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
341e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
342e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
343e974f91cSConrad Meyer 		    ioat->comp_update_map);
344e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
345e974f91cSConrad Meyer 	}
346e974f91cSConrad Meyer 
347e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
348e974f91cSConrad Meyer 
3494253ea50SConrad Meyer 	return (0);
3504253ea50SConrad Meyer }
3514253ea50SConrad Meyer 
3524253ea50SConrad Meyer static int
3534253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3544253ea50SConrad Meyer {
3554253ea50SConrad Meyer 
356e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3574253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
358e974f91cSConrad Meyer 
359e974f91cSConrad Meyer 	if (ioat->res != NULL)
3604253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
361e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
362e974f91cSConrad Meyer 
3634253ea50SConrad Meyer 	pci_release_msi(ioat->device);
364e974f91cSConrad Meyer 	return (0);
365e974f91cSConrad Meyer }
366e974f91cSConrad Meyer 
367e974f91cSConrad Meyer static int
368cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
369e974f91cSConrad Meyer {
370e974f91cSConrad Meyer 	uint64_t status;
371e974f91cSConrad Meyer 	uint32_t chanerr;
372e974f91cSConrad Meyer 	int i;
373e974f91cSConrad Meyer 
374e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
375e974f91cSConrad Meyer 	ioat_null(&ioat->dmaengine, NULL, NULL, 0);
376e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
377e974f91cSConrad Meyer 
378e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
379e974f91cSConrad Meyer 		DELAY(1);
380e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
381e974f91cSConrad Meyer 		if (is_ioat_idle(status))
382e974f91cSConrad Meyer 			return (0);
383e974f91cSConrad Meyer 	}
384e974f91cSConrad Meyer 
385e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
386e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
38759acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
38859acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
389e974f91cSConrad Meyer 	return (ENXIO);
390e974f91cSConrad Meyer }
391e974f91cSConrad Meyer 
392e974f91cSConrad Meyer /*
393e974f91cSConrad Meyer  * Initialize Hardware
394e974f91cSConrad Meyer  */
395e974f91cSConrad Meyer static int
396e974f91cSConrad Meyer ioat3_attach(device_t device)
397e974f91cSConrad Meyer {
398e974f91cSConrad Meyer 	struct ioat_softc *ioat;
399e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
400e974f91cSConrad Meyer 	struct ioat_descriptor *next;
401e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
402e974f91cSConrad Meyer 	int i, num_descriptors;
403e974f91cSConrad Meyer 	int error;
404e974f91cSConrad Meyer 	uint8_t xfercap;
405e974f91cSConrad Meyer 
406e974f91cSConrad Meyer 	error = 0;
407e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
4081693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
4091693d27bSConrad Meyer 
4101693d27bSConrad Meyer 	ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities,
4111693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
412e974f91cSConrad Meyer 
413e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
414e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
415e974f91cSConrad Meyer 
4165ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
4175ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
4185ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
4195ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
4205ca9fc2aSConrad Meyer 
421e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
422e974f91cSConrad Meyer 
423e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
424faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4257afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
426374b05e1SConrad Meyer 	TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat);
427e974f91cSConrad Meyer 
428faefad9cSConrad Meyer 	/* Establish lock order for Witness */
429faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
430faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
431faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
432faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
433faefad9cSConrad Meyer 
434e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
435e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
436e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
437e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
438e974f91cSConrad Meyer 
439e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
440e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
441e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
442e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
443e974f91cSConrad Meyer 
444e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
445e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
446e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
447e974f91cSConrad Meyer 		return (ENOMEM);
448e974f91cSConrad Meyer 
449e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
450e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
451e974f91cSConrad Meyer 	    0);
452e974f91cSConrad Meyer 	if (error != 0)
453e974f91cSConrad Meyer 		return (error);
454e974f91cSConrad Meyer 
455e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
456e974f91cSConrad Meyer 
457e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
458e974f91cSConrad Meyer 
459e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
460519e8baaSConrad Meyer 	    BUS_SPACE_MAXADDR_40BIT, BUS_SPACE_MAXADDR, NULL, NULL,
461e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
462e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
463e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
464e974f91cSConrad Meyer 
465e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
466bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
467e974f91cSConrad Meyer 
468e974f91cSConrad Meyer 	ring = ioat->ring;
469e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
470bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
471e974f91cSConrad Meyer 		if (ring[i] == NULL)
472e974f91cSConrad Meyer 			return (ENOMEM);
473e974f91cSConrad Meyer 
474e974f91cSConrad Meyer 		ring[i]->id = i;
475e974f91cSConrad Meyer 	}
476e974f91cSConrad Meyer 
477e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
478e974f91cSConrad Meyer 		next = ring[i + 1];
479e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
480e974f91cSConrad Meyer 
481e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
482e974f91cSConrad Meyer 	}
483e974f91cSConrad Meyer 
484e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
485e974f91cSConrad Meyer 
486bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
487e974f91cSConrad Meyer 	ioat->tail = 0;
488e974f91cSConrad Meyer 	ioat->last_seen = 0;
489e974f91cSConrad Meyer 	return (0);
490e974f91cSConrad Meyer }
491e974f91cSConrad Meyer 
492e974f91cSConrad Meyer static int
493e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
494e974f91cSConrad Meyer {
495e974f91cSConrad Meyer 
496e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
497e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
498e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
499e974f91cSConrad Meyer 
500e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
501e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
502e974f91cSConrad Meyer 		return (ENODEV);
503e974f91cSConrad Meyer 	}
504e974f91cSConrad Meyer 
505e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
506e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
507e974f91cSConrad Meyer 	return (0);
508e974f91cSConrad Meyer }
509e974f91cSConrad Meyer 
510e974f91cSConrad Meyer static void
511e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
512e974f91cSConrad Meyer {
513e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
514e974f91cSConrad Meyer 
515cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
516e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
517e974f91cSConrad Meyer }
518e974f91cSConrad Meyer 
519e974f91cSConrad Meyer static void
520e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
521e974f91cSConrad Meyer {
522e974f91cSConrad Meyer 	bus_addr_t *baddr;
523e974f91cSConrad Meyer 
524cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
525e974f91cSConrad Meyer 	baddr = arg;
526e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
527e974f91cSConrad Meyer }
528e974f91cSConrad Meyer 
529e974f91cSConrad Meyer /*
530e974f91cSConrad Meyer  * Interrupt setup and handlers
531e974f91cSConrad Meyer  */
532e974f91cSConrad Meyer static int
5334253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
534e974f91cSConrad Meyer {
535e974f91cSConrad Meyer 	uint32_t num_vectors;
536e974f91cSConrad Meyer 	int error;
537e974f91cSConrad Meyer 	boolean_t use_msix;
538e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
539e974f91cSConrad Meyer 
540e974f91cSConrad Meyer 	use_msix = FALSE;
541e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
542e974f91cSConrad Meyer 
543e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
544e974f91cSConrad Meyer 		num_vectors = 1;
545e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
546e974f91cSConrad Meyer 		if (num_vectors == 1)
547e974f91cSConrad Meyer 			use_msix = TRUE;
548e974f91cSConrad Meyer 	}
549e974f91cSConrad Meyer 
550e974f91cSConrad Meyer 	if (use_msix) {
551e974f91cSConrad Meyer 		ioat->rid = 1;
552e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
553e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
554e974f91cSConrad Meyer 	} else {
555e974f91cSConrad Meyer 		ioat->rid = 0;
556e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
557e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
558e974f91cSConrad Meyer 	}
559e974f91cSConrad Meyer 	if (ioat->res == NULL) {
560e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
561e974f91cSConrad Meyer 		return (ENOMEM);
562e974f91cSConrad Meyer 	}
563e974f91cSConrad Meyer 
564e974f91cSConrad Meyer 	ioat->tag = NULL;
565e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
566e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
567e974f91cSConrad Meyer 	if (error != 0) {
568e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
569e974f91cSConrad Meyer 		return (error);
570e974f91cSConrad Meyer 	}
571e974f91cSConrad Meyer 
572e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
573e974f91cSConrad Meyer 	return (0);
574e974f91cSConrad Meyer }
575e974f91cSConrad Meyer 
5764253ea50SConrad Meyer static boolean_t
5770d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5784253ea50SConrad Meyer {
5794253ea50SConrad Meyer 	u_int32_t pciid;
5804253ea50SConrad Meyer 
5814253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5824253ea50SConrad Meyer 	switch (pciid) {
5830d1a05d9SConrad Meyer 		/* BWD: */
5840d1a05d9SConrad Meyer 	case 0x0c508086:
5850d1a05d9SConrad Meyer 	case 0x0c518086:
5860d1a05d9SConrad Meyer 	case 0x0c528086:
5870d1a05d9SConrad Meyer 	case 0x0c538086:
5880d1a05d9SConrad Meyer 		/* BDXDE: */
5894253ea50SConrad Meyer 	case 0x6f508086:
5904253ea50SConrad Meyer 	case 0x6f518086:
5914253ea50SConrad Meyer 	case 0x6f528086:
5924253ea50SConrad Meyer 	case 0x6f538086:
5934253ea50SConrad Meyer 		return (TRUE);
5944253ea50SConrad Meyer 	}
5954253ea50SConrad Meyer 
5964253ea50SConrad Meyer 	return (FALSE);
5974253ea50SConrad Meyer }
5984253ea50SConrad Meyer 
599e974f91cSConrad Meyer static void
600e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
601e974f91cSConrad Meyer {
602e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
603e974f91cSConrad Meyer 
60401fbbc88SConrad Meyer 	ioat->stats.interrupts++;
605e974f91cSConrad Meyer 	ioat_process_events(ioat);
606e974f91cSConrad Meyer }
607e974f91cSConrad Meyer 
608faefad9cSConrad Meyer static int
609faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
610faefad9cSConrad Meyer {
611faefad9cSConrad Meyer 
612faefad9cSConrad Meyer 	if (chanerr == 0)
613faefad9cSConrad Meyer 		return (0);
614faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
615faefad9cSConrad Meyer 		return (EFAULT);
616faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
617faefad9cSConrad Meyer 		return (EIO);
618faefad9cSConrad Meyer 	/* This one is probably our fault: */
619faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
620faefad9cSConrad Meyer 		return (EIO);
621faefad9cSConrad Meyer 	return (EIO);
622faefad9cSConrad Meyer }
623faefad9cSConrad Meyer 
624e974f91cSConrad Meyer static void
625e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
626e974f91cSConrad Meyer {
627e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
628e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
629e974f91cSConrad Meyer 	uint64_t comp_update, status;
630faefad9cSConrad Meyer 	uint32_t completed, chanerr;
631faefad9cSConrad Meyer 	int error;
632e974f91cSConrad Meyer 
633e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
634e974f91cSConrad Meyer 
635e974f91cSConrad Meyer 	completed = 0;
636e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
637e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
638e974f91cSConrad Meyer 
63943fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
640e974f91cSConrad Meyer 
641007a7030SConrad Meyer 	if (status == ioat->last_seen) {
642007a7030SConrad Meyer 		/*
643007a7030SConrad Meyer 		 * If we landed in process_events and nothing has been
644007a7030SConrad Meyer 		 * completed, check for a timeout due to channel halt.
645007a7030SConrad Meyer 		 */
646007a7030SConrad Meyer 		comp_update = ioat_get_chansts(ioat);
6474becebdfSConrad Meyer 		goto out;
648007a7030SConrad Meyer 	}
649e974f91cSConrad Meyer 
650e974f91cSConrad Meyer 	while (1) {
651e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
652e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
65343fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
654e974f91cSConrad Meyer 
655faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
656faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
657e974f91cSConrad Meyer 
658466b3540SConrad Meyer 		completed++;
659e974f91cSConrad Meyer 		ioat->tail++;
660e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
661e974f91cSConrad Meyer 			break;
662e974f91cSConrad Meyer 	}
663e974f91cSConrad Meyer 
664e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
665e974f91cSConrad Meyer 
666e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
667e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
668fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
669fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
670e974f91cSConrad Meyer 	}
671e974f91cSConrad Meyer 
67201fbbc88SConrad Meyer 	ioat->stats.descriptors_processed += completed;
67301fbbc88SConrad Meyer 
6744becebdfSConrad Meyer out:
675e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
676e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
677466b3540SConrad Meyer 
678007a7030SConrad Meyer 	if (completed != 0) {
679466b3540SConrad Meyer 		ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
680bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
681007a7030SConrad Meyer 	}
682faefad9cSConrad Meyer 
683d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
684faefad9cSConrad Meyer 		return;
685faefad9cSConrad Meyer 
68601fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
68701fbbc88SConrad Meyer 
688faefad9cSConrad Meyer 	/*
689faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
690faefad9cSConrad Meyer 	 * work with error status and restart the engine.
691faefad9cSConrad Meyer 	 */
692faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
693faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
694faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
695faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
696faefad9cSConrad Meyer 
697faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
698faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
69901fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
700faefad9cSConrad Meyer 
701faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
702faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
703faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
704faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
705faefad9cSConrad Meyer 
706faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
707faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
708faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
709faefad9cSConrad Meyer 
710faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
711faefad9cSConrad Meyer 		ioat->tail++;
71201fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
71301fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
714faefad9cSConrad Meyer 	}
715faefad9cSConrad Meyer 
716faefad9cSConrad Meyer 	/* Clear error status */
717faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
718faefad9cSConrad Meyer 
719faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
720faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
721faefad9cSConrad Meyer 
722faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
723374b05e1SConrad Meyer 	error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task);
724374b05e1SConrad Meyer 	KASSERT(error == 0,
725374b05e1SConrad Meyer 	    ("%s: taskqueue_enqueue failed: %d", __func__, error));
726374b05e1SConrad Meyer }
727374b05e1SConrad Meyer 
728374b05e1SConrad Meyer static void
729374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused)
730374b05e1SConrad Meyer {
731374b05e1SConrad Meyer 	struct ioat_softc *ioat;
732374b05e1SConrad Meyer 	int error;
733374b05e1SConrad Meyer 
734374b05e1SConrad Meyer 	ioat = ctx;
735374b05e1SConrad Meyer 	ioat_log_message(1, "%s: Resetting channel\n", __func__);
736374b05e1SConrad Meyer 
737faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
738faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
739374b05e1SConrad Meyer 	(void)error;
740e974f91cSConrad Meyer }
741e974f91cSConrad Meyer 
742e974f91cSConrad Meyer /*
743e974f91cSConrad Meyer  * User API functions
744e974f91cSConrad Meyer  */
745e974f91cSConrad Meyer bus_dmaengine_t
7460ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags)
747e974f91cSConrad Meyer {
7480ff814e8SConrad Meyer 	struct ioat_softc *ioat;
7490ff814e8SConrad Meyer 
7500ff814e8SConrad Meyer 	KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0,
7510ff814e8SConrad Meyer 	    ("invalid flags: 0x%08x", flags));
7520ff814e8SConrad Meyer 	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
7530ff814e8SConrad Meyer 	    ("invalid wait | nowait"));
754e974f91cSConrad Meyer 
755466b3540SConrad Meyer 	if (index >= ioat_channel_index)
756e974f91cSConrad Meyer 		return (NULL);
7575f77bd3eSConrad Meyer 
7580ff814e8SConrad Meyer 	ioat = ioat_channel[index];
7590ff814e8SConrad Meyer 	if (ioat == NULL || ioat->destroying)
7605f77bd3eSConrad Meyer 		return (NULL);
7615f77bd3eSConrad Meyer 
7620ff814e8SConrad Meyer 	if (ioat->quiescing) {
7630ff814e8SConrad Meyer 		if ((flags & M_NOWAIT) != 0)
7640ff814e8SConrad Meyer 			return (NULL);
7650ff814e8SConrad Meyer 
7660ff814e8SConrad Meyer 		mtx_lock(IOAT_REFLK);
7670ff814e8SConrad Meyer 		while (ioat->quiescing && !ioat->destroying)
7680ff814e8SConrad Meyer 			msleep(&ioat->quiescing, IOAT_REFLK, 0, "getdma", 0);
7690ff814e8SConrad Meyer 		mtx_unlock(IOAT_REFLK);
7700ff814e8SConrad Meyer 
7710ff814e8SConrad Meyer 		if (ioat->destroying)
7720ff814e8SConrad Meyer 			return (NULL);
7730ff814e8SConrad Meyer 	}
7740ff814e8SConrad Meyer 
7750ff814e8SConrad Meyer 	/*
7760ff814e8SConrad Meyer 	 * There's a race here between the quiescing check and HW reset or
7770ff814e8SConrad Meyer 	 * module destroy.
7780ff814e8SConrad Meyer 	 */
7790ff814e8SConrad Meyer 	return (&ioat_get(ioat, IOAT_DMAENGINE_REF)->dmaengine);
780466b3540SConrad Meyer }
781466b3540SConrad Meyer 
782466b3540SConrad Meyer void
783466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
784466b3540SConrad Meyer {
785466b3540SConrad Meyer 	struct ioat_softc *ioat;
786466b3540SConrad Meyer 
787466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
788466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
789e974f91cSConrad Meyer }
790e974f91cSConrad Meyer 
7915ca9fc2aSConrad Meyer int
79231bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
79331bf2875SConrad Meyer {
79431bf2875SConrad Meyer 	struct ioat_softc *ioat;
79531bf2875SConrad Meyer 
79631bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
79731bf2875SConrad Meyer 	return (ioat->version);
79831bf2875SConrad Meyer }
79931bf2875SConrad Meyer 
800bd81fe68SConrad Meyer size_t
801bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
802bd81fe68SConrad Meyer {
803bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
804bd81fe68SConrad Meyer 
805bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
806bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
807bd81fe68SConrad Meyer }
808bd81fe68SConrad Meyer 
80931bf2875SConrad Meyer int
8105ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
8115ca9fc2aSConrad Meyer {
8125ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
8135ca9fc2aSConrad Meyer 
8145ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8155ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
8165ca9fc2aSConrad Meyer 		return (ENODEV);
8175ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
8185ca9fc2aSConrad Meyer 		return (ERANGE);
8195ca9fc2aSConrad Meyer 
8205ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
8215ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
8225ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
8235ca9fc2aSConrad Meyer 	return (0);
8245ca9fc2aSConrad Meyer }
8255ca9fc2aSConrad Meyer 
8265ca9fc2aSConrad Meyer uint16_t
8275ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
8285ca9fc2aSConrad Meyer {
8295ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
8305ca9fc2aSConrad Meyer 
8315ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8325ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
8335ca9fc2aSConrad Meyer }
8345ca9fc2aSConrad Meyer 
835e974f91cSConrad Meyer void
836e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
837e974f91cSConrad Meyer {
838e974f91cSConrad Meyer 	struct ioat_softc *ioat;
839e974f91cSConrad Meyer 
840e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
841e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
84243fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
843e974f91cSConrad Meyer }
844e974f91cSConrad Meyer 
8451502e363SConrad Meyer int
8461502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
8471502e363SConrad Meyer {
8481502e363SConrad Meyer 	struct ioat_softc *ioat;
8491502e363SConrad Meyer 	int error;
8501502e363SConrad Meyer 
8511502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8521502e363SConrad Meyer 	ioat_acquire(dmaengine);
8531502e363SConrad Meyer 
8541502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
8551502e363SConrad Meyer 	if (error != 0)
8561502e363SConrad Meyer 		ioat_release(dmaengine);
8571502e363SConrad Meyer 	return (error);
8581502e363SConrad Meyer }
8591502e363SConrad Meyer 
860e974f91cSConrad Meyer void
861e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
862e974f91cSConrad Meyer {
863e974f91cSConrad Meyer 	struct ioat_softc *ioat;
864e974f91cSConrad Meyer 
865e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
86643fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
867bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
868e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
869e974f91cSConrad Meyer }
870e974f91cSConrad Meyer 
8719e3bbf26SConrad Meyer static struct ioat_descriptor *
8729e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
8739e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
8749e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
8759e3bbf26SConrad Meyer     uint32_t flags)
876e974f91cSConrad Meyer {
8779e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
878e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
879bf8553eaSConrad Meyer 	int mflags;
880e974f91cSConrad Meyer 
8819e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
8829e3bbf26SConrad Meyer 
883bec7ff79SConrad Meyer 	KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0,
884bec7ff79SConrad Meyer 	    ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS));
885bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
886bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
887bf8553eaSConrad Meyer 	else
888bf8553eaSConrad Meyer 		mflags = M_WAITOK;
889e974f91cSConrad Meyer 
8909e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
8919e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
8929e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
8939e3bbf26SConrad Meyer 		return (NULL);
8949e3bbf26SConrad Meyer 	}
895e974f91cSConrad Meyer 
896bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
897e974f91cSConrad Meyer 		return (NULL);
898e974f91cSConrad Meyer 
899e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
9009e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
901e974f91cSConrad Meyer 
902e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
9039e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
9049e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
905e974f91cSConrad Meyer 
906e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
9079e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
9086ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
9096ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
910e974f91cSConrad Meyer 
9119e3bbf26SConrad Meyer 	hw_desc->size = size;
9129e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
9139e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
914e974f91cSConrad Meyer 
915e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
916e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
9179e3bbf26SConrad Meyer 	return (desc);
9189e3bbf26SConrad Meyer }
919e974f91cSConrad Meyer 
9209e3bbf26SConrad Meyer struct bus_dmadesc *
9219e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
9229e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
9239e3bbf26SConrad Meyer {
9249e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9259e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
9269e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
9279e3bbf26SConrad Meyer 
9289e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9299e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9309e3bbf26SConrad Meyer 
9319e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
9329e3bbf26SConrad Meyer 	    callback_arg, flags);
9339e3bbf26SConrad Meyer 	if (desc == NULL)
9349e3bbf26SConrad Meyer 		return (NULL);
9359e3bbf26SConrad Meyer 
9369e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
9379e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
938e974f91cSConrad Meyer 	ioat_submit_single(ioat);
939e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
940e974f91cSConrad Meyer }
941e974f91cSConrad Meyer 
942e974f91cSConrad Meyer struct bus_dmadesc *
943e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
944e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
945e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
946e974f91cSConrad Meyer {
947e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9489e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
949e974f91cSConrad Meyer 	struct ioat_softc *ioat;
950e974f91cSConrad Meyer 
9519e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
952e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
953e974f91cSConrad Meyer 
9549e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
9559e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9569e3bbf26SConrad Meyer 		    __func__);
957e974f91cSConrad Meyer 		return (NULL);
958e974f91cSConrad Meyer 	}
959e974f91cSConrad Meyer 
9609e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
9619e3bbf26SConrad Meyer 	    callback_arg, flags);
9629e3bbf26SConrad Meyer 	if (desc == NULL)
963e974f91cSConrad Meyer 		return (NULL);
964e974f91cSConrad Meyer 
965e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
966e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
967e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
968e974f91cSConrad Meyer 
969e974f91cSConrad Meyer 	ioat_submit_single(ioat);
970e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
971e974f91cSConrad Meyer }
972e974f91cSConrad Meyer 
9732a4fd6b1SConrad Meyer struct bus_dmadesc *
9749950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
9759950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
9769950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
9779950fde0SConrad Meyer {
9789950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9799950fde0SConrad Meyer 	struct ioat_descriptor *desc;
9809950fde0SConrad Meyer 	struct ioat_softc *ioat;
9819950fde0SConrad Meyer 
9829950fde0SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9839950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9849950fde0SConrad Meyer 
9859950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
9869950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9879950fde0SConrad Meyer 		    __func__);
9889950fde0SConrad Meyer 		return (NULL);
9899950fde0SConrad Meyer 	}
9909950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
9919950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
9929950fde0SConrad Meyer 		    __func__);
9939950fde0SConrad Meyer 		return (NULL);
9949950fde0SConrad Meyer 	}
9959950fde0SConrad Meyer 
9969950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
9979950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
9989950fde0SConrad Meyer 	if (desc == NULL)
9999950fde0SConrad Meyer 		return (NULL);
10009950fde0SConrad Meyer 
10019950fde0SConrad Meyer 	hw_desc = desc->u.dma;
10029950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
10039950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
10049950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
10059950fde0SConrad Meyer 	}
10069950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
10079950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
10089950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
10099950fde0SConrad Meyer 	}
10109950fde0SConrad Meyer 
10119950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
10129950fde0SConrad Meyer 		dump_descriptor(hw_desc);
10139950fde0SConrad Meyer 
10149950fde0SConrad Meyer 	ioat_submit_single(ioat);
10159950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
10169950fde0SConrad Meyer }
10179950fde0SConrad Meyer 
10189950fde0SConrad Meyer struct bus_dmadesc *
1019bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src,
1020bec7ff79SConrad Meyer     bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr,
1021bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1022bec7ff79SConrad Meyer {
1023bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1024bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1025bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1026bec7ff79SConrad Meyer 	uint32_t teststore;
1027bec7ff79SConrad Meyer 	uint8_t op;
1028bec7ff79SConrad Meyer 
1029bec7ff79SConrad Meyer 	CTR0(KTR_IOAT, __func__);
1030bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1031bec7ff79SConrad Meyer 
1032bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) {
1033bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks MOVECRC capability\n",
1034bec7ff79SConrad Meyer 		    __func__);
1035bec7ff79SConrad Meyer 		return (NULL);
1036bec7ff79SConrad Meyer 	}
1037bec7ff79SConrad Meyer 	if (((src | dst) & (0xffffffull << 40)) != 0) {
1038bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n",
1039bec7ff79SConrad Meyer 		    __func__);
1040bec7ff79SConrad Meyer 		return (NULL);
1041bec7ff79SConrad Meyer 	}
1042bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1043bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1044bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1045bec7ff79SConrad Meyer 		return (NULL);
1046bec7ff79SConrad Meyer 	}
1047bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1048bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1049bec7ff79SConrad Meyer 		    __func__);
1050bec7ff79SConrad Meyer 		return (NULL);
1051bec7ff79SConrad Meyer 	}
1052bec7ff79SConrad Meyer 
1053bec7ff79SConrad Meyer 	switch (teststore) {
1054bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1055bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_STORE;
1056bec7ff79SConrad Meyer 		break;
1057bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1058bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC_TEST;
1059bec7ff79SConrad Meyer 		break;
1060bec7ff79SConrad Meyer 	default:
1061bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1062bec7ff79SConrad Meyer 		op = IOAT_OP_MOVECRC;
1063bec7ff79SConrad Meyer 		break;
1064bec7ff79SConrad Meyer 	}
1065bec7ff79SConrad Meyer 
1066bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1067bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1068bec7ff79SConrad Meyer 		ioat_log_message(0,
1069bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1070bec7ff79SConrad Meyer 		return (NULL);
1071bec7ff79SConrad Meyer 	}
1072bec7ff79SConrad Meyer 
1073bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn,
1074bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1075bec7ff79SConrad Meyer 	if (desc == NULL)
1076bec7ff79SConrad Meyer 		return (NULL);
1077bec7ff79SConrad Meyer 
1078bec7ff79SConrad Meyer 	hw_desc = desc->u.crc32;
1079bec7ff79SConrad Meyer 
1080bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1081bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1082bec7ff79SConrad Meyer 	else
1083bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1084bec7ff79SConrad Meyer 
1085bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1086bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1087bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1088bec7ff79SConrad Meyer 	}
1089bec7ff79SConrad Meyer 
1090bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1091bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1092bec7ff79SConrad Meyer 
1093bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1094bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1095bec7ff79SConrad Meyer }
1096bec7ff79SConrad Meyer 
1097bec7ff79SConrad Meyer struct bus_dmadesc *
1098bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len,
1099bec7ff79SConrad Meyer     uint32_t *initialseed, bus_addr_t crcptr,
1100bec7ff79SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
1101bec7ff79SConrad Meyer {
1102bec7ff79SConrad Meyer 	struct ioat_crc32_hw_descriptor *hw_desc;
1103bec7ff79SConrad Meyer 	struct ioat_descriptor *desc;
1104bec7ff79SConrad Meyer 	struct ioat_softc *ioat;
1105bec7ff79SConrad Meyer 	uint32_t teststore;
1106bec7ff79SConrad Meyer 	uint8_t op;
1107bec7ff79SConrad Meyer 
1108bec7ff79SConrad Meyer 	CTR0(KTR_IOAT, __func__);
1109bec7ff79SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
1110bec7ff79SConrad Meyer 
1111bec7ff79SConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) {
1112bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: Device lacks CRC capability\n",
1113bec7ff79SConrad Meyer 		    __func__);
1114bec7ff79SConrad Meyer 		return (NULL);
1115bec7ff79SConrad Meyer 	}
1116bec7ff79SConrad Meyer 	if ((src & (0xffffffull << 40)) != 0) {
1117bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: High 24 bits of src invalid\n",
1118bec7ff79SConrad Meyer 		    __func__);
1119bec7ff79SConrad Meyer 		return (NULL);
1120bec7ff79SConrad Meyer 	}
1121bec7ff79SConrad Meyer 	teststore = (flags & _DMA_CRC_TESTSTORE);
1122bec7ff79SConrad Meyer 	if (teststore == _DMA_CRC_TESTSTORE) {
1123bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__);
1124bec7ff79SConrad Meyer 		return (NULL);
1125bec7ff79SConrad Meyer 	}
1126bec7ff79SConrad Meyer 	if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) {
1127bec7ff79SConrad Meyer 		ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n",
1128bec7ff79SConrad Meyer 		    __func__);
1129bec7ff79SConrad Meyer 		return (NULL);
1130bec7ff79SConrad Meyer 	}
1131bec7ff79SConrad Meyer 
1132bec7ff79SConrad Meyer 	switch (teststore) {
1133bec7ff79SConrad Meyer 	case DMA_CRC_STORE:
1134bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_STORE;
1135bec7ff79SConrad Meyer 		break;
1136bec7ff79SConrad Meyer 	case DMA_CRC_TEST:
1137bec7ff79SConrad Meyer 		op = IOAT_OP_CRC_TEST;
1138bec7ff79SConrad Meyer 		break;
1139bec7ff79SConrad Meyer 	default:
1140bec7ff79SConrad Meyer 		KASSERT(teststore == 0, ("bogus"));
1141bec7ff79SConrad Meyer 		op = IOAT_OP_CRC;
1142bec7ff79SConrad Meyer 		break;
1143bec7ff79SConrad Meyer 	}
1144bec7ff79SConrad Meyer 
1145bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0 &&
1146bec7ff79SConrad Meyer 	    (crcptr & (0xffffffull << 40)) != 0) {
1147bec7ff79SConrad Meyer 		ioat_log_message(0,
1148bec7ff79SConrad Meyer 		    "%s: High 24 bits of crcptr invalid\n", __func__);
1149bec7ff79SConrad Meyer 		return (NULL);
1150bec7ff79SConrad Meyer 	}
1151bec7ff79SConrad Meyer 
1152bec7ff79SConrad Meyer 	desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn,
1153bec7ff79SConrad Meyer 	    callback_arg, flags & ~_DMA_CRC_FLAGS);
1154bec7ff79SConrad Meyer 	if (desc == NULL)
1155bec7ff79SConrad Meyer 		return (NULL);
1156bec7ff79SConrad Meyer 
1157bec7ff79SConrad Meyer 	hw_desc = desc->u.crc32;
1158bec7ff79SConrad Meyer 
1159bec7ff79SConrad Meyer 	if ((flags & DMA_CRC_INLINE) == 0)
1160bec7ff79SConrad Meyer 		hw_desc->crc_address = crcptr;
1161bec7ff79SConrad Meyer 	else
1162bec7ff79SConrad Meyer 		hw_desc->u.control.crc_location = 1;
1163bec7ff79SConrad Meyer 
1164bec7ff79SConrad Meyer 	if (initialseed != NULL) {
1165bec7ff79SConrad Meyer 		hw_desc->u.control.use_seed = 1;
1166bec7ff79SConrad Meyer 		hw_desc->seed = *initialseed;
1167bec7ff79SConrad Meyer 	}
1168bec7ff79SConrad Meyer 
1169bec7ff79SConrad Meyer 	if (g_ioat_debug_level >= 3)
1170bec7ff79SConrad Meyer 		dump_descriptor(hw_desc);
1171bec7ff79SConrad Meyer 
1172bec7ff79SConrad Meyer 	ioat_submit_single(ioat);
1173bec7ff79SConrad Meyer 	return (&desc->bus_dmadesc);
1174bec7ff79SConrad Meyer }
1175bec7ff79SConrad Meyer 
1176bec7ff79SConrad Meyer struct bus_dmadesc *
11772a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
11782a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
11792a4fd6b1SConrad Meyer     uint32_t flags)
11802a4fd6b1SConrad Meyer {
11812a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
11822a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
11832a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
11842a4fd6b1SConrad Meyer 
11852a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
11862a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
11872a4fd6b1SConrad Meyer 
11881693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
11891693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
11901693d27bSConrad Meyer 		    __func__);
11911693d27bSConrad Meyer 		return (NULL);
11921693d27bSConrad Meyer 	}
11931693d27bSConrad Meyer 
11942a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
11952a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
11962a4fd6b1SConrad Meyer 		    __func__);
11972a4fd6b1SConrad Meyer 		return (NULL);
11982a4fd6b1SConrad Meyer 	}
11992a4fd6b1SConrad Meyer 
12002a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
12012a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
12022a4fd6b1SConrad Meyer 	if (desc == NULL)
12032a4fd6b1SConrad Meyer 		return (NULL);
12042a4fd6b1SConrad Meyer 
12052a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
12062a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
12072a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
12082a4fd6b1SConrad Meyer 
12092a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
12102a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
12112a4fd6b1SConrad Meyer }
12122a4fd6b1SConrad Meyer 
1213e974f91cSConrad Meyer /*
1214e974f91cSConrad Meyer  * Ring Management
1215e974f91cSConrad Meyer  */
1216e974f91cSConrad Meyer static inline uint32_t
1217e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1218e974f91cSConrad Meyer {
1219e974f91cSConrad Meyer 
1220e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1221e974f91cSConrad Meyer }
1222e974f91cSConrad Meyer 
1223e974f91cSConrad Meyer static inline uint32_t
1224e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1225e974f91cSConrad Meyer {
1226e974f91cSConrad Meyer 
1227e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1228e974f91cSConrad Meyer }
1229e974f91cSConrad Meyer 
1230e974f91cSConrad Meyer static struct ioat_descriptor *
1231bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
1232e974f91cSConrad Meyer {
12339e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1234e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1235bf8553eaSConrad Meyer 	int error, busdmaflag;
1236e974f91cSConrad Meyer 
1237f46011aeSConrad Meyer 	error = ENOMEM;
1238f46011aeSConrad Meyer 	hw_desc = NULL;
1239f46011aeSConrad Meyer 
1240bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
1241bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
1242bf8553eaSConrad Meyer 	else
1243bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
1244bf8553eaSConrad Meyer 
1245bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
1246e974f91cSConrad Meyer 	if (desc == NULL)
1247f46011aeSConrad Meyer 		goto out;
1248e974f91cSConrad Meyer 
1249f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
1250bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
1251f46011aeSConrad Meyer 	if (hw_desc == NULL)
1252f46011aeSConrad Meyer 		goto out;
1253e974f91cSConrad Meyer 
1254faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
12559e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
1256f46011aeSConrad Meyer 
1257f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
1258f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
1259bf8553eaSConrad Meyer 	    busdmaflag);
1260f46011aeSConrad Meyer 	if (error)
1261f46011aeSConrad Meyer 		goto out;
1262f46011aeSConrad Meyer 
1263f46011aeSConrad Meyer out:
1264f46011aeSConrad Meyer 	if (error) {
1265f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
1266f46011aeSConrad Meyer 		return (NULL);
1267f46011aeSConrad Meyer 	}
1268e974f91cSConrad Meyer 	return (desc);
1269e974f91cSConrad Meyer }
1270e974f91cSConrad Meyer 
1271e974f91cSConrad Meyer static void
1272e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
1273e974f91cSConrad Meyer {
1274e974f91cSConrad Meyer 
1275e974f91cSConrad Meyer 	if (desc == NULL)
1276e974f91cSConrad Meyer 		return;
1277e974f91cSConrad Meyer 
12789e3bbf26SConrad Meyer 	if (desc->u.generic)
12799e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
1280e974f91cSConrad Meyer 		    ioat->hw_desc_map);
1281e974f91cSConrad Meyer 	free(desc, M_IOAT);
1282e974f91cSConrad Meyer }
1283e974f91cSConrad Meyer 
1284bf8553eaSConrad Meyer /*
1285bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1286bf8553eaSConrad Meyer  * for 'num_descs'.
1287bf8553eaSConrad Meyer  *
1288bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1289bf8553eaSConrad Meyer  *
1290bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1291bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1292bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1293bf8553eaSConrad Meyer  *
1294bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1295bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1296bf8553eaSConrad Meyer  *
1297bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1298bf8553eaSConrad Meyer  * assured enough room is available.)
1299bf8553eaSConrad Meyer  */
1300e974f91cSConrad Meyer static int
1301bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1302e974f91cSConrad Meyer {
1303bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
1304bf8553eaSConrad Meyer 	uint32_t order;
1305bf8553eaSConrad Meyer 	int error;
1306e974f91cSConrad Meyer 
1307bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1308bf8553eaSConrad Meyer 	error = 0;
1309e974f91cSConrad Meyer 
1310bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
1311bf8553eaSConrad Meyer 		error = EINVAL;
1312bf8553eaSConrad Meyer 		goto out;
1313e974f91cSConrad Meyer 	}
13145f77bd3eSConrad Meyer 	if (ioat->quiescing) {
13155f77bd3eSConrad Meyer 		error = ENXIO;
13165f77bd3eSConrad Meyer 		goto out;
13175f77bd3eSConrad Meyer 	}
1318bf8553eaSConrad Meyer 
1319bf8553eaSConrad Meyer 	for (;;) {
1320bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1321bf8553eaSConrad Meyer 			goto out;
1322bf8553eaSConrad Meyer 
1323bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1324bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
1325bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
1326bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
1327bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
1328bf8553eaSConrad Meyer 				continue;
1329bf8553eaSConrad Meyer 			}
1330bf8553eaSConrad Meyer 
1331bf8553eaSConrad Meyer 			error = EAGAIN;
1332bf8553eaSConrad Meyer 			break;
1333bf8553eaSConrad Meyer 		}
1334bf8553eaSConrad Meyer 
1335bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1336bf8553eaSConrad Meyer 		for (;;) {
1337bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1338bf8553eaSConrad Meyer 
1339bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
1340bf8553eaSConrad Meyer 			    TRUE, mflags);
1341bf8553eaSConrad Meyer 
1342bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1343bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1344bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1345bf8553eaSConrad Meyer 
1346bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1347bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1348bf8553eaSConrad Meyer 				    ("allocation failed"));
1349bf8553eaSConrad Meyer 				error = EAGAIN;
1350bf8553eaSConrad Meyer 				break;
1351bf8553eaSConrad Meyer 			}
1352bf8553eaSConrad Meyer 
1353bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1354bf8553eaSConrad Meyer 			if (error == 0)
1355bf8553eaSConrad Meyer 				break;
1356bf8553eaSConrad Meyer 		}
1357bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1358bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1359bf8553eaSConrad Meyer 		if (error)
1360bf8553eaSConrad Meyer 			break;
1361bf8553eaSConrad Meyer 	}
1362bf8553eaSConrad Meyer 
1363bf8553eaSConrad Meyer out:
1364bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1365bf8553eaSConrad Meyer 	return (error);
1366bf8553eaSConrad Meyer }
1367bf8553eaSConrad Meyer 
1368bf8553eaSConrad Meyer static struct ioat_descriptor **
1369bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1370bf8553eaSConrad Meyer     int mflags)
1371bf8553eaSConrad Meyer {
1372bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1373bf8553eaSConrad Meyer 	uint32_t i;
1374bf8553eaSConrad Meyer 	int error;
1375bf8553eaSConrad Meyer 
1376bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1377bf8553eaSConrad Meyer 
1378bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1379bf8553eaSConrad Meyer 	if (ring == NULL)
1380bf8553eaSConrad Meyer 		return (NULL);
1381bf8553eaSConrad Meyer 
1382bf8553eaSConrad Meyer 	if (need_dscr) {
1383bf8553eaSConrad Meyer 		error = ENOMEM;
1384bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1385bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1386bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1387bf8553eaSConrad Meyer 				goto out;
1388bf8553eaSConrad Meyer 			ring[i]->id = i;
1389bf8553eaSConrad Meyer 		}
1390bf8553eaSConrad Meyer 	}
1391bf8553eaSConrad Meyer 	error = 0;
1392bf8553eaSConrad Meyer 
1393bf8553eaSConrad Meyer out:
1394bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1395bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1396bf8553eaSConrad Meyer 		ring = NULL;
1397bf8553eaSConrad Meyer 	}
1398bf8553eaSConrad Meyer 	return (ring);
1399bf8553eaSConrad Meyer }
1400bf8553eaSConrad Meyer 
1401bf8553eaSConrad Meyer static void
1402bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1403bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1404bf8553eaSConrad Meyer {
1405bf8553eaSConrad Meyer 	uint32_t i;
1406bf8553eaSConrad Meyer 
1407bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1408bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1409bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1410bf8553eaSConrad Meyer 	}
1411bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1412e974f91cSConrad Meyer }
1413e974f91cSConrad Meyer 
1414e974f91cSConrad Meyer static struct ioat_descriptor *
1415e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1416e974f91cSConrad Meyer {
1417e974f91cSConrad Meyer 
1418e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1419e974f91cSConrad Meyer }
1420e974f91cSConrad Meyer 
1421bf8553eaSConrad Meyer static int
1422bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1423bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1424e974f91cSConrad Meyer {
1425bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1426e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1427bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1428bf8553eaSConrad Meyer 	int error;
1429e974f91cSConrad Meyer 
1430bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1431e974f91cSConrad Meyer 
1432bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1433bf8553eaSConrad Meyer 
1434bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1435bf8553eaSConrad Meyer 		error = EINVAL;
1436bf8553eaSConrad Meyer 		goto out;
1437bf8553eaSConrad Meyer 	}
1438bf8553eaSConrad Meyer 
1439bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1440bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1441bf8553eaSConrad Meyer 
1442bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1443bf8553eaSConrad Meyer 
1444bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1445bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1446bf8553eaSConrad Meyer 
1447bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1448bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1449bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1450e974f91cSConrad Meyer 
1451e974f91cSConrad Meyer 	/*
1452bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1453bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1454e974f91cSConrad Meyer 	 */
1455bf8553eaSConrad Meyer 	if (head < tail) {
1456bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1457bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1458e974f91cSConrad Meyer 
1459bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1460bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1461e974f91cSConrad Meyer 
1462bf8553eaSConrad Meyer 			newring[i] = tmp;
1463bf8553eaSConrad Meyer 			newring[i]->id = i;
1464bf8553eaSConrad Meyer 		}
1465bf8553eaSConrad Meyer 		head += oldsize;
1466e974f91cSConrad Meyer 	}
1467e974f91cSConrad Meyer 
1468bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1469e974f91cSConrad Meyer 
1470bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1471bf8553eaSConrad Meyer 	if (head < oldsize) {
1472bf8553eaSConrad Meyer 		i = oldsize - 1;
1473bf8553eaSConrad Meyer 		end = newsize;
1474e974f91cSConrad Meyer 	} else {
1475bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1476bf8553eaSConrad Meyer 		i = head;
1477bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1478bf8553eaSConrad Meyer 	}
1479bf8553eaSConrad Meyer 
1480e974f91cSConrad Meyer 	/*
1481bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1482bf8553eaSConrad Meyer 	 * section (tail -> head).
1483e974f91cSConrad Meyer 	 */
1484bf8553eaSConrad Meyer 	for (; i < end; i++) {
1485bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1486bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1487e974f91cSConrad Meyer 
1488bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1489bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1490e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1491e974f91cSConrad Meyer 	}
1492e974f91cSConrad Meyer 
1493e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1494bf8553eaSConrad Meyer 	ioat->ring = newring;
1495bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1496bf8553eaSConrad Meyer 	ioat->tail = tail;
1497bf8553eaSConrad Meyer 	ioat->head = head;
1498bf8553eaSConrad Meyer 	error = 0;
1499e974f91cSConrad Meyer 
1500bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1501bf8553eaSConrad Meyer out:
1502bf8553eaSConrad Meyer 	if (error)
1503bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1504bf8553eaSConrad Meyer 	return (error);
1505bf8553eaSConrad Meyer }
1506bf8553eaSConrad Meyer 
1507bf8553eaSConrad Meyer static int
1508bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1509bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1510bf8553eaSConrad Meyer {
1511bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1512bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1513bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1514bf8553eaSConrad Meyer 	int error;
1515bf8553eaSConrad Meyer 
1516bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1517bf8553eaSConrad Meyer 
1518bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1519bf8553eaSConrad Meyer 
1520bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1521bf8553eaSConrad Meyer 		error = EINVAL;
1522bf8553eaSConrad Meyer 		goto out_unlocked;
1523bf8553eaSConrad Meyer 	}
1524bf8553eaSConrad Meyer 
1525bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1526bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1527bf8553eaSConrad Meyer 
1528bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1529bf8553eaSConrad Meyer 
1530bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1531bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1532bf8553eaSConrad Meyer 		error = ENOMEM;
1533bf8553eaSConrad Meyer 		goto out;
1534bf8553eaSConrad Meyer 	}
1535bf8553eaSConrad Meyer 
1536bf8553eaSConrad Meyer 	/*
1537bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1538bf8553eaSConrad Meyer 	 * descriptors.
1539bf8553eaSConrad Meyer 	 */
1540bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1541bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1542bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1543bf8553eaSConrad Meyer 
1544bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1545bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1546bf8553eaSConrad Meyer 	}
1547bf8553eaSConrad Meyer 
1548bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1549bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1550bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1551bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1552bf8553eaSConrad Meyer 	}
1553bf8553eaSConrad Meyer 
1554bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1555bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1556bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1557bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1558bf8553eaSConrad Meyer 
1559bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1560bf8553eaSConrad Meyer 	ioat->ring = newring;
1561bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1562bf8553eaSConrad Meyer 	error = 0;
1563bf8553eaSConrad Meyer 
1564bf8553eaSConrad Meyer out:
1565bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1566bf8553eaSConrad Meyer out_unlocked:
1567bf8553eaSConrad Meyer 	if (error)
1568bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1569bf8553eaSConrad Meyer 	return (error);
1570e974f91cSConrad Meyer }
1571e974f91cSConrad Meyer 
1572e974f91cSConrad Meyer static void
15738f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1574e974f91cSConrad Meyer {
1575e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
15768f274637SConrad Meyer 
157759acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
157859acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
15798f274637SConrad Meyer 	if (chanerr == 0)
15808f274637SConrad Meyer 		return;
15818f274637SConrad Meyer 
1582faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1583faefad9cSConrad Meyer 
15848f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
15858f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
15868f274637SConrad Meyer 
15878f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
15888f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
15898f274637SConrad Meyer }
15908f274637SConrad Meyer 
15918f274637SConrad Meyer static void
15928f274637SConrad Meyer ioat_timer_callback(void *arg)
15938f274637SConrad Meyer {
1594bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1595e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1596faefad9cSConrad Meyer 	uint32_t order;
1597e974f91cSConrad Meyer 
1598e974f91cSConrad Meyer 	ioat = arg;
1599fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1600e974f91cSConrad Meyer 
1601e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1602e974f91cSConrad Meyer 		ioat_process_events(ioat);
1603faefad9cSConrad Meyer 		return;
1604faefad9cSConrad Meyer 	}
1605faefad9cSConrad Meyer 
1606faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1607e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1608bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1609bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1610bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1611bf8553eaSConrad Meyer 		goto out;
1612bf8553eaSConrad Meyer 	}
1613bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1614e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1615e974f91cSConrad Meyer 
1616bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1617bf8553eaSConrad Meyer 	    M_NOWAIT);
1618bf8553eaSConrad Meyer 
1619bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1620bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1621bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1622bf8553eaSConrad Meyer 
1623bf8553eaSConrad Meyer 	if (newring != NULL)
1624bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1625bf8553eaSConrad Meyer 
1626bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1627bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1628bf8553eaSConrad Meyer 
1629bf8553eaSConrad Meyer out:
1630e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1631bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1632e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1633e974f91cSConrad Meyer }
1634e974f91cSConrad Meyer 
1635e974f91cSConrad Meyer /*
1636e974f91cSConrad Meyer  * Support Functions
1637e974f91cSConrad Meyer  */
1638e974f91cSConrad Meyer static void
1639e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1640e974f91cSConrad Meyer {
1641e974f91cSConrad Meyer 
1642466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1643e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1644bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1645e974f91cSConrad Meyer 
1646e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1647e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1648fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1649fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1650e974f91cSConrad Meyer 	}
165101fbbc88SConrad Meyer 
165201fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1653e974f91cSConrad Meyer }
1654e974f91cSConrad Meyer 
1655e974f91cSConrad Meyer static int
1656e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1657e974f91cSConrad Meyer {
1658e974f91cSConrad Meyer 	uint64_t status;
1659e974f91cSConrad Meyer 	uint32_t chanerr;
1660cea5b880SConrad Meyer 	unsigned timeout;
16615f77bd3eSConrad Meyer 	int error;
16625f77bd3eSConrad Meyer 
16635f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
16645f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
16655f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
16665f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1667e974f91cSConrad Meyer 
1668e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1669e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1670e974f91cSConrad Meyer 		ioat_suspend(ioat);
1671e974f91cSConrad Meyer 
1672e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1673e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1674e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1675e974f91cSConrad Meyer 		DELAY(1000);
1676e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1677e974f91cSConrad Meyer 	}
16785f77bd3eSConrad Meyer 	if (timeout == 20) {
16795f77bd3eSConrad Meyer 		error = ETIMEDOUT;
16805f77bd3eSConrad Meyer 		goto out;
16815f77bd3eSConrad Meyer 	}
1682e974f91cSConrad Meyer 
1683cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1684cea5b880SConrad Meyer 
1685e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1686e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1687e974f91cSConrad Meyer 
1688e974f91cSConrad Meyer 	/*
1689e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1690e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1691e974f91cSConrad Meyer 	 */
1692e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1693e974f91cSConrad Meyer 	    4);
1694e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1695e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1696e974f91cSConrad Meyer 
16970d1a05d9SConrad Meyer 	/*
16980d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
16990d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
17000d1a05d9SConrad Meyer 	 */
1701f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1702f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
17030d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1704f7157235SConrad Meyer 	}
17050d1a05d9SConrad Meyer 
1706e974f91cSConrad Meyer 	ioat_reset(ioat);
1707e974f91cSConrad Meyer 
1708e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1709e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1710e974f91cSConrad Meyer 		DELAY(1000);
17115f77bd3eSConrad Meyer 	if (timeout == 20) {
17125f77bd3eSConrad Meyer 		error = ETIMEDOUT;
17135f77bd3eSConrad Meyer 		goto out;
17145f77bd3eSConrad Meyer 	}
1715e974f91cSConrad Meyer 
1716f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1717f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
17180d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1719f7157235SConrad Meyer 	}
17204253ea50SConrad Meyer 
1721cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1722cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1723cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1724cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1725cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1726cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
17275f77bd3eSConrad Meyer 		error = 0;
17285f77bd3eSConrad Meyer 		goto out;
1729e974f91cSConrad Meyer 	}
1730e974f91cSConrad Meyer 
1731cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
17325f77bd3eSConrad Meyer 	if (chanerr != 0) {
1733faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1734faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1735faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
17365f77bd3eSConrad Meyer 		error = EIO;
17375f77bd3eSConrad Meyer 		goto out;
17385f77bd3eSConrad Meyer 	}
1739cea5b880SConrad Meyer 
1740cea5b880SConrad Meyer 	/*
1741cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1742cea5b880SConrad Meyer 	 * device back to active.
1743cea5b880SConrad Meyer 	 *
1744cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1745cea5b880SConrad Meyer 	 * at zero as well.
1746cea5b880SConrad Meyer 	 */
1747bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1748cea5b880SConrad Meyer 	ioat->last_seen = 0;
1749cea5b880SConrad Meyer 
1750cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1751cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1752cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
17535f77bd3eSConrad Meyer 	error = 0;
17545f77bd3eSConrad Meyer 
17555f77bd3eSConrad Meyer out:
17565f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
17575f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
17580ff814e8SConrad Meyer 	wakeup(&ioat->quiescing);
17595f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
17605f77bd3eSConrad Meyer 
17615f77bd3eSConrad Meyer 	if (error == 0)
17625f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
17635f77bd3eSConrad Meyer 
17645f77bd3eSConrad Meyer 	return (error);
1765cea5b880SConrad Meyer }
1766cea5b880SConrad Meyer 
1767f7157235SConrad Meyer static int
1768faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1769faefad9cSConrad Meyer {
1770faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1771faefad9cSConrad Meyer 	struct sbuf sb;
1772faefad9cSConrad Meyer 	uint64_t status;
1773faefad9cSConrad Meyer 	int error;
1774faefad9cSConrad Meyer 
1775faefad9cSConrad Meyer 	ioat = arg1;
1776faefad9cSConrad Meyer 
1777faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1778faefad9cSConrad Meyer 
1779faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1780faefad9cSConrad Meyer 	switch (status) {
1781faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1782faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1783faefad9cSConrad Meyer 		break;
1784faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1785faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1786faefad9cSConrad Meyer 		break;
1787faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1788faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1789faefad9cSConrad Meyer 		break;
1790faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1791faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1792faefad9cSConrad Meyer 		break;
1793faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1794faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1795faefad9cSConrad Meyer 		break;
1796faefad9cSConrad Meyer 	default:
1797faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1798faefad9cSConrad Meyer 		break;
1799faefad9cSConrad Meyer 	}
1800faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1801faefad9cSConrad Meyer 	sbuf_delete(&sb);
1802faefad9cSConrad Meyer 
1803faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1804faefad9cSConrad Meyer 		return (error);
1805faefad9cSConrad Meyer 	return (EINVAL);
1806faefad9cSConrad Meyer }
1807faefad9cSConrad Meyer 
1808faefad9cSConrad Meyer static int
180901fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
181001fbbc88SConrad Meyer {
181101fbbc88SConrad Meyer 	struct ioat_softc *ioat;
181201fbbc88SConrad Meyer 	struct sbuf sb;
181301fbbc88SConrad Meyer #define	PRECISION	"1"
181401fbbc88SConrad Meyer 	const uintmax_t factor = 10;
181501fbbc88SConrad Meyer 	uintmax_t rate;
181601fbbc88SConrad Meyer 	int error;
181701fbbc88SConrad Meyer 
181801fbbc88SConrad Meyer 	ioat = arg1;
181901fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
182001fbbc88SConrad Meyer 
182101fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
182201fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
182301fbbc88SConrad Meyer 		goto out;
182401fbbc88SConrad Meyer 	}
182501fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
182601fbbc88SConrad Meyer 	    ioat->stats.interrupts;
182701fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
182801fbbc88SConrad Meyer 	    rate % factor);
182901fbbc88SConrad Meyer #undef	PRECISION
183001fbbc88SConrad Meyer out:
183101fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
183201fbbc88SConrad Meyer 	sbuf_delete(&sb);
183301fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
183401fbbc88SConrad Meyer 		return (error);
183501fbbc88SConrad Meyer 	return (EINVAL);
183601fbbc88SConrad Meyer }
183701fbbc88SConrad Meyer 
183801fbbc88SConrad Meyer static int
1839faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1840faefad9cSConrad Meyer {
1841faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1842faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1843faefad9cSConrad Meyer 	int error, arg;
1844faefad9cSConrad Meyer 
1845faefad9cSConrad Meyer 	ioat = arg1;
1846faefad9cSConrad Meyer 
1847faefad9cSConrad Meyer 	arg = 0;
1848faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1849faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1850faefad9cSConrad Meyer 		return (error);
1851faefad9cSConrad Meyer 
1852faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1853faefad9cSConrad Meyer 	if (error != 0)
1854faefad9cSConrad Meyer 		return (error);
1855faefad9cSConrad Meyer 
1856faefad9cSConrad Meyer 	if (arg != 0) {
1857faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1858faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1859faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1860faefad9cSConrad Meyer 		    0);
1861faefad9cSConrad Meyer 		if (desc == NULL)
1862faefad9cSConrad Meyer 			error = ENOMEM;
1863faefad9cSConrad Meyer 		else
1864faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1865faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1866faefad9cSConrad Meyer 	}
1867faefad9cSConrad Meyer 	return (error);
1868faefad9cSConrad Meyer }
1869faefad9cSConrad Meyer 
1870faefad9cSConrad Meyer static int
1871f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1872f7157235SConrad Meyer {
1873f7157235SConrad Meyer 	struct ioat_softc *ioat;
1874f7157235SConrad Meyer 	int error, arg;
1875f7157235SConrad Meyer 
1876f7157235SConrad Meyer 	ioat = arg1;
1877f7157235SConrad Meyer 
1878f7157235SConrad Meyer 	arg = 0;
1879f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1880f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1881f7157235SConrad Meyer 		return (error);
1882f7157235SConrad Meyer 
1883f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1884f7157235SConrad Meyer 	if (error != 0)
1885f7157235SConrad Meyer 		return (error);
1886f7157235SConrad Meyer 
1887f7157235SConrad Meyer 	if (arg != 0)
1888f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1889f7157235SConrad Meyer 
1890f7157235SConrad Meyer 	return (error);
1891f7157235SConrad Meyer }
1892f7157235SConrad Meyer 
1893e974f91cSConrad Meyer static void
1894e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1895e974f91cSConrad Meyer {
1896e974f91cSConrad Meyer 	int i, j;
1897e974f91cSConrad Meyer 
1898e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1899e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1900e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1901e974f91cSConrad Meyer 		printf("\n");
1902e974f91cSConrad Meyer 	}
1903e974f91cSConrad Meyer }
1904e974f91cSConrad Meyer 
1905e974f91cSConrad Meyer static void
1906e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1907e974f91cSConrad Meyer {
190801fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1909f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
191001fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1911e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1912e974f91cSConrad Meyer 
1913e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1914f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1915f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1916f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1917e974f91cSConrad Meyer 
191865e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
191965e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
192065e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
192165e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
19225ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
19235ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
19245ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
19255ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
19265ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
192765e4f8adSConrad Meyer 
192801fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
192901fbbc88SConrad Meyer 	    "IOAT channel internal state");
193001fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
193101fbbc88SConrad Meyer 
193201fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1933bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
193401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
193501fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
193601fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
193701fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
193801fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1939bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1940f7157235SConrad Meyer 
194101fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
194265e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
194365e4f8adSConrad Meyer 
194401fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
194565e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
194601fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
194701fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
194801fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
194965e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
195001fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
195165e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
195265e4f8adSConrad Meyer 
195301fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1954faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1955faefad9cSConrad Meyer 	    "String of the channel status");
195601fbbc88SConrad Meyer 
19575ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
19585ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
19595ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
19605ca9fc2aSConrad Meyer 
196101fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
196201fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
196301fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
196401fbbc88SConrad Meyer 
196501fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
196601fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
196701fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
196801fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error",
196901fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
197001fbbc88SConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
197101fbbc88SConrad Meyer 
197201fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
197301fbbc88SConrad Meyer 	    "IOAT channel statistics");
197401fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
197501fbbc88SConrad Meyer 
197601fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
197701fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
197801fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
197901fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
198001fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
198101fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
198201fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
198301fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
198401fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
198501fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
198601fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
198701fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
198801fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
198901fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
199001fbbc88SConrad Meyer 	    "Number of times the channel has halted");
199101fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
199201fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
199301fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
199401fbbc88SConrad Meyer 
199501fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
199601fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
199701fbbc88SConrad Meyer 	    "Descriptors per interrupt");
1998e974f91cSConrad Meyer }
1999466b3540SConrad Meyer 
2000466b3540SConrad Meyer static inline struct ioat_softc *
2001466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
2002466b3540SConrad Meyer {
2003466b3540SConrad Meyer 	uint32_t old;
2004466b3540SConrad Meyer 
2005466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
2006466b3540SConrad Meyer 
2007466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
2008466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
2009466b3540SConrad Meyer 
2010466b3540SConrad Meyer #ifdef INVARIANTS
2011466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
2012466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
2013466b3540SConrad Meyer #endif
2014466b3540SConrad Meyer 
2015466b3540SConrad Meyer 	return (ioat);
2016466b3540SConrad Meyer }
2017466b3540SConrad Meyer 
2018466b3540SConrad Meyer static inline void
2019466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
2020466b3540SConrad Meyer {
2021faefad9cSConrad Meyer 
2022faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
2023faefad9cSConrad Meyer }
2024faefad9cSConrad Meyer 
2025faefad9cSConrad Meyer static inline void
2026faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
2027faefad9cSConrad Meyer {
2028faefad9cSConrad Meyer 
2029faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
2030faefad9cSConrad Meyer }
2031faefad9cSConrad Meyer 
2032faefad9cSConrad Meyer static inline void
2033faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
2034faefad9cSConrad Meyer     boolean_t locked)
2035faefad9cSConrad Meyer {
2036466b3540SConrad Meyer 	uint32_t old;
2037466b3540SConrad Meyer 
2038466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
2039466b3540SConrad Meyer 
2040466b3540SConrad Meyer 	if (n == 0)
2041466b3540SConrad Meyer 		return;
2042466b3540SConrad Meyer 
2043466b3540SConrad Meyer #ifdef INVARIANTS
2044466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
2045466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
2046466b3540SConrad Meyer #endif
2047466b3540SConrad Meyer 
2048466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
2049466b3540SConrad Meyer 	for (;;) {
2050466b3540SConrad Meyer 		old = ioat->refcnt;
2051466b3540SConrad Meyer 		if (old <= n)
2052466b3540SConrad Meyer 			break;
2053466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
2054466b3540SConrad Meyer 			return;
2055466b3540SConrad Meyer 	}
2056466b3540SConrad Meyer 
2057faefad9cSConrad Meyer 	if (locked)
2058faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
2059faefad9cSConrad Meyer 	else
2060466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
2061faefad9cSConrad Meyer 
2062466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
2063466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
2064466b3540SConrad Meyer 
2065466b3540SConrad Meyer 	if (old == n)
2066466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
2067faefad9cSConrad Meyer 	if (!locked)
2068466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
2069466b3540SConrad Meyer }
2070466b3540SConrad Meyer 
2071466b3540SConrad Meyer static inline void
2072466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
2073466b3540SConrad Meyer {
2074466b3540SConrad Meyer 
2075466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
2076466b3540SConrad Meyer }
2077466b3540SConrad Meyer 
2078466b3540SConrad Meyer static void
20795f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
2080466b3540SConrad Meyer {
2081466b3540SConrad Meyer 
20825f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
2083466b3540SConrad Meyer 	while (ioat->refcnt > 0)
2084466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
2085466b3540SConrad Meyer }
2086