xref: /freebsd/sys/dev/ioat/ioat.c (revision d2c55e5ad032e6063025a7c88f35680858f5c6ad)
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>
43e974f91cSConrad Meyer #include <sys/time.h>
44e974f91cSConrad Meyer #include <dev/pci/pcireg.h>
45e974f91cSConrad Meyer #include <dev/pci/pcivar.h>
46e974f91cSConrad Meyer #include <machine/bus.h>
47e974f91cSConrad Meyer #include <machine/resource.h>
48e974f91cSConrad Meyer #include <machine/stdarg.h>
49e974f91cSConrad Meyer 
50e974f91cSConrad Meyer #include "ioat.h"
51e974f91cSConrad Meyer #include "ioat_hw.h"
52e974f91cSConrad Meyer #include "ioat_internal.h"
53e974f91cSConrad Meyer 
54fe720f5aSConrad Meyer #define	IOAT_INTR_TIMO	(hz / 10)
55466b3540SConrad Meyer #define	IOAT_REFLK	(&ioat->submit_lock)
56fe720f5aSConrad Meyer 
57e974f91cSConrad Meyer static int ioat_probe(device_t device);
58e974f91cSConrad Meyer static int ioat_attach(device_t device);
59e974f91cSConrad Meyer static int ioat_detach(device_t device);
604253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat);
614253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat);
62e974f91cSConrad Meyer static int ioat3_attach(device_t device);
63cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat);
64e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat);
65e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
66e974f91cSConrad Meyer     int error);
67e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg);
680d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat);
69faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t);
70e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat);
71e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat);
72e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat);
73bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size,
74bf8553eaSConrad Meyer     struct ioat_descriptor **);
75e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat,
76e974f91cSConrad Meyer     struct ioat_descriptor *desc);
77bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *,
78bf8553eaSConrad Meyer     int mflags);
79bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags);
80e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat,
81e974f91cSConrad Meyer     uint32_t index);
82bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *,
83bf8553eaSConrad Meyer     uint32_t size, boolean_t need_dscr, int mflags);
84bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder,
85bf8553eaSConrad Meyer     struct ioat_descriptor **);
86bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder,
87bf8553eaSConrad Meyer     struct ioat_descriptor **);
88faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t);
89e974f91cSConrad Meyer static void ioat_timer_callback(void *arg);
90e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc);
91e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat);
92e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg,
93e974f91cSConrad Meyer     int error);
94e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat);
95e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device);
96f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS);
97466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *,
98466b3540SConrad Meyer     enum ioat_ref_kind);
99466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind);
100faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
101faefad9cSConrad Meyer     enum ioat_ref_kind, boolean_t);
102466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t,
103466b3540SConrad Meyer     enum ioat_ref_kind);
104faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
105faefad9cSConrad Meyer     enum ioat_ref_kind);
1065f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *);
107e974f91cSConrad Meyer 
1081c25420eSConrad Meyer #define	ioat_log_message(v, ...) do {					\
1091c25420eSConrad Meyer 	if ((v) <= g_ioat_debug_level) {				\
1101c25420eSConrad Meyer 		device_printf(ioat->device, __VA_ARGS__);		\
1111c25420eSConrad Meyer 	}								\
1121c25420eSConrad Meyer } while (0)
1131c25420eSConrad Meyer 
114e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations");
115e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node");
116e974f91cSConrad Meyer 
117e974f91cSConrad Meyer static int g_force_legacy_interrupts;
118e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN,
119e974f91cSConrad Meyer     &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled");
120e974f91cSConrad Meyer 
1211c25420eSConrad Meyer int g_ioat_debug_level = 0;
122e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level,
123e974f91cSConrad Meyer     0, "Set log level (0-3) for ioat(4). Higher is more verbose.");
124e974f91cSConrad Meyer 
125e974f91cSConrad Meyer /*
126e974f91cSConrad Meyer  * OS <-> Driver interface structures
127e974f91cSConrad Meyer  */
128e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = {
129e974f91cSConrad Meyer 	/* Device interface */
130e974f91cSConrad Meyer 	DEVMETHOD(device_probe,     ioat_probe),
131e974f91cSConrad Meyer 	DEVMETHOD(device_attach,    ioat_attach),
132e974f91cSConrad Meyer 	DEVMETHOD(device_detach,    ioat_detach),
133e974f91cSConrad Meyer 	{ 0, 0 }
134e974f91cSConrad Meyer };
135e974f91cSConrad Meyer 
136e974f91cSConrad Meyer static driver_t ioat_pci_driver = {
137e974f91cSConrad Meyer 	"ioat",
138e974f91cSConrad Meyer 	ioat_pci_methods,
139e974f91cSConrad Meyer 	sizeof(struct ioat_softc),
140e974f91cSConrad Meyer };
141e974f91cSConrad Meyer 
142e974f91cSConrad Meyer static devclass_t ioat_devclass;
143e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0);
144c2b69205SConrad Meyer MODULE_VERSION(ioat, 1);
145e974f91cSConrad Meyer 
146e974f91cSConrad Meyer /*
147e974f91cSConrad Meyer  * Private data structures
148e974f91cSConrad Meyer  */
149e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
150e974f91cSConrad Meyer static int ioat_channel_index = 0;
151e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
152e974f91cSConrad Meyer     "Number of IOAT channels attached");
153e974f91cSConrad Meyer 
154e974f91cSConrad Meyer static struct _pcsid
155e974f91cSConrad Meyer {
156e974f91cSConrad Meyer 	u_int32_t   type;
157e974f91cSConrad Meyer 	const char  *desc;
158e974f91cSConrad Meyer } pci_ids[] = {
159e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
160e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
161e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
162e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
163e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
164e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
165e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
166e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
167e974f91cSConrad Meyer 
168e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
169e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
170e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
171e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
172e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
173e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
174e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
175e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
176e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
177e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
178e974f91cSConrad Meyer 
179e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
180e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
181e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
182e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
183e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
184e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
185e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
186e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
187e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
188e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
189e974f91cSConrad Meyer 
190e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
191e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
192e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
193e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
194e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
195e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
196e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
197e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
198e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
199e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
200e974f91cSConrad Meyer 
201e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
202e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
203e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
204e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
205e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
206e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
207e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
208e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
209e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
210e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
211e974f91cSConrad Meyer 
212e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
213e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
214e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
215e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
216e974f91cSConrad Meyer 
217e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
218e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
219e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
220e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
221e974f91cSConrad Meyer 
2225afc2508SConrad Meyer 	{ 0x6f208086, "BDX IOAT Ch0" },
2235afc2508SConrad Meyer 	{ 0x6f218086, "BDX IOAT Ch1" },
2245afc2508SConrad Meyer 	{ 0x6f228086, "BDX IOAT Ch2" },
2255afc2508SConrad Meyer 	{ 0x6f238086, "BDX IOAT Ch3" },
2265afc2508SConrad Meyer 	{ 0x6f248086, "BDX IOAT Ch4" },
2275afc2508SConrad Meyer 	{ 0x6f258086, "BDX IOAT Ch5" },
2285afc2508SConrad Meyer 	{ 0x6f268086, "BDX IOAT Ch6" },
2295afc2508SConrad Meyer 	{ 0x6f278086, "BDX IOAT Ch7" },
2305afc2508SConrad Meyer 	{ 0x6f2e8086, "BDX IOAT Ch0 (RAID)" },
2315afc2508SConrad Meyer 	{ 0x6f2f8086, "BDX IOAT Ch1 (RAID)" },
2325afc2508SConrad Meyer 
233e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
234e974f91cSConrad Meyer };
235e974f91cSConrad Meyer 
236e974f91cSConrad Meyer /*
237e974f91cSConrad Meyer  * OS <-> Driver linkage functions
238e974f91cSConrad Meyer  */
239e974f91cSConrad Meyer static int
240e974f91cSConrad Meyer ioat_probe(device_t device)
241e974f91cSConrad Meyer {
242e974f91cSConrad Meyer 	struct _pcsid *ep;
243e974f91cSConrad Meyer 	u_int32_t type;
244e974f91cSConrad Meyer 
245e974f91cSConrad Meyer 	type = pci_get_devid(device);
246e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
247e974f91cSConrad Meyer 		if (ep->type == type) {
248e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
249e974f91cSConrad Meyer 			return (0);
250e974f91cSConrad Meyer 		}
251e974f91cSConrad Meyer 	}
252e974f91cSConrad Meyer 	return (ENXIO);
253e974f91cSConrad Meyer }
254e974f91cSConrad Meyer 
255e974f91cSConrad Meyer static int
256e974f91cSConrad Meyer ioat_attach(device_t device)
257e974f91cSConrad Meyer {
258e974f91cSConrad Meyer 	struct ioat_softc *ioat;
259e974f91cSConrad Meyer 	int error;
260e974f91cSConrad Meyer 
261e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
262e974f91cSConrad Meyer 	ioat->device = device;
263e974f91cSConrad Meyer 
264e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
265e974f91cSConrad Meyer 	if (error != 0)
266e974f91cSConrad Meyer 		goto err;
267e974f91cSConrad Meyer 
268e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
269e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
270e974f91cSConrad Meyer 		error = ENODEV;
271e974f91cSConrad Meyer 		goto err;
272e974f91cSConrad Meyer 	}
273e974f91cSConrad Meyer 
274e974f91cSConrad Meyer 	error = ioat3_attach(device);
275e974f91cSConrad Meyer 	if (error != 0)
276e974f91cSConrad Meyer 		goto err;
277e974f91cSConrad Meyer 
278e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
279e974f91cSConrad Meyer 	if (error != 0)
280e974f91cSConrad Meyer 		goto err;
281e974f91cSConrad Meyer 
282466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
283466b3540SConrad Meyer 	if (error != 0)
284466b3540SConrad Meyer 		goto err;
285466b3540SConrad Meyer 
286cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2877afbb263SConrad Meyer 	if (error != 0)
288466b3540SConrad Meyer 		goto err;
2897afbb263SConrad Meyer 
2907afbb263SConrad Meyer 	ioat_process_events(ioat);
2917afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2927afbb263SConrad Meyer 
2935f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
294e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
2957afbb263SConrad Meyer 	ioat_test_attach();
296e974f91cSConrad Meyer 
297e974f91cSConrad Meyer err:
298e974f91cSConrad Meyer 	if (error != 0)
299e974f91cSConrad Meyer 		ioat_detach(device);
300e974f91cSConrad Meyer 	return (error);
301e974f91cSConrad Meyer }
302e974f91cSConrad Meyer 
303e974f91cSConrad Meyer static int
304e974f91cSConrad Meyer ioat_detach(device_t device)
305e974f91cSConrad Meyer {
306e974f91cSConrad Meyer 	struct ioat_softc *ioat;
307e974f91cSConrad Meyer 
308e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3097afbb263SConrad Meyer 
3107afbb263SConrad Meyer 	ioat_test_detach();
3115f77bd3eSConrad Meyer 
3125f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3135f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3145f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3155f77bd3eSConrad Meyer 
3165f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3175f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
318fe720f5aSConrad Meyer 
319fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
320e974f91cSConrad Meyer 	callout_drain(&ioat->timer);
321e974f91cSConrad Meyer 
322e974f91cSConrad Meyer 	pci_disable_busmaster(device);
323e974f91cSConrad Meyer 
324e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
325e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
326e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
327e974f91cSConrad Meyer 
328bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
329bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
330e974f91cSConrad Meyer 
331e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
332e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
333e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
334e974f91cSConrad Meyer 		    ioat->comp_update_map);
335e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
336e974f91cSConrad Meyer 	}
337e974f91cSConrad Meyer 
338e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
339e974f91cSConrad Meyer 
3404253ea50SConrad Meyer 	return (0);
3414253ea50SConrad Meyer }
3424253ea50SConrad Meyer 
3434253ea50SConrad Meyer static int
3444253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3454253ea50SConrad Meyer {
3464253ea50SConrad Meyer 
347e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3484253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
349e974f91cSConrad Meyer 
350e974f91cSConrad Meyer 	if (ioat->res != NULL)
3514253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
352e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
353e974f91cSConrad Meyer 
3544253ea50SConrad Meyer 	pci_release_msi(ioat->device);
355e974f91cSConrad Meyer 	return (0);
356e974f91cSConrad Meyer }
357e974f91cSConrad Meyer 
358e974f91cSConrad Meyer static int
359cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
360e974f91cSConrad Meyer {
361e974f91cSConrad Meyer 	uint64_t status;
362e974f91cSConrad Meyer 	uint32_t chanerr;
363e974f91cSConrad Meyer 	int i;
364e974f91cSConrad Meyer 
365e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
366e974f91cSConrad Meyer 	ioat_null(&ioat->dmaengine, NULL, NULL, 0);
367e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
368e974f91cSConrad Meyer 
369e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
370e974f91cSConrad Meyer 		DELAY(1);
371e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
372e974f91cSConrad Meyer 		if (is_ioat_idle(status))
373e974f91cSConrad Meyer 			return (0);
374e974f91cSConrad Meyer 	}
375e974f91cSConrad Meyer 
376e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
377e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
37859acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
37959acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
380e974f91cSConrad Meyer 	return (ENXIO);
381e974f91cSConrad Meyer }
382e974f91cSConrad Meyer 
383e974f91cSConrad Meyer /*
384e974f91cSConrad Meyer  * Initialize Hardware
385e974f91cSConrad Meyer  */
386e974f91cSConrad Meyer static int
387e974f91cSConrad Meyer ioat3_attach(device_t device)
388e974f91cSConrad Meyer {
389e974f91cSConrad Meyer 	struct ioat_softc *ioat;
390e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
391e974f91cSConrad Meyer 	struct ioat_descriptor *next;
392e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
393e974f91cSConrad Meyer 	int i, num_descriptors;
394e974f91cSConrad Meyer 	int error;
395e974f91cSConrad Meyer 	uint8_t xfercap;
396e974f91cSConrad Meyer 
397e974f91cSConrad Meyer 	error = 0;
398e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3991693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
4001693d27bSConrad Meyer 
4011693d27bSConrad Meyer 	ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities,
4021693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
403e974f91cSConrad Meyer 
404e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
405e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
406e974f91cSConrad Meyer 
4075ca9fc2aSConrad Meyer 	ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) &
4085ca9fc2aSConrad Meyer 	    IOAT_INTRDELAY_SUPPORTED) != 0;
4095ca9fc2aSConrad Meyer 	if (ioat->intrdelay_supported)
4105ca9fc2aSConrad Meyer 		ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK;
4115ca9fc2aSConrad Meyer 
412e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
413e974f91cSConrad Meyer 
414e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
415faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4167afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
417e974f91cSConrad Meyer 
418faefad9cSConrad Meyer 	/* Establish lock order for Witness */
419faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
420faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
421faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
422faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
423faefad9cSConrad Meyer 
424e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
425e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
426e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
427e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
428e974f91cSConrad Meyer 
429e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
430e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
431e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
432e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
433e974f91cSConrad Meyer 
434e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
435e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
436e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
437e974f91cSConrad Meyer 		return (ENOMEM);
438e974f91cSConrad Meyer 
439e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
440e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
441e974f91cSConrad Meyer 	    0);
442e974f91cSConrad Meyer 	if (error != 0)
443e974f91cSConrad Meyer 		return (error);
444e974f91cSConrad Meyer 
445e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
446e974f91cSConrad Meyer 
447e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
448e974f91cSConrad Meyer 
449e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
450e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
451e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
452e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
453e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
454e974f91cSConrad Meyer 
455e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
456bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
457e974f91cSConrad Meyer 	if (ioat->ring == NULL)
458e974f91cSConrad Meyer 		return (ENOMEM);
459e974f91cSConrad Meyer 
460e974f91cSConrad Meyer 	ring = ioat->ring;
461e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
462bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
463e974f91cSConrad Meyer 		if (ring[i] == NULL)
464e974f91cSConrad Meyer 			return (ENOMEM);
465e974f91cSConrad Meyer 
466e974f91cSConrad Meyer 		ring[i]->id = i;
467e974f91cSConrad Meyer 	}
468e974f91cSConrad Meyer 
469e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
470e974f91cSConrad Meyer 		next = ring[i + 1];
471e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
472e974f91cSConrad Meyer 
473e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
474e974f91cSConrad Meyer 	}
475e974f91cSConrad Meyer 
476e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
477e974f91cSConrad Meyer 
478bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
479e974f91cSConrad Meyer 	ioat->tail = 0;
480e974f91cSConrad Meyer 	ioat->last_seen = 0;
481e974f91cSConrad Meyer 	return (0);
482e974f91cSConrad Meyer }
483e974f91cSConrad Meyer 
484e974f91cSConrad Meyer static int
485e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
486e974f91cSConrad Meyer {
487e974f91cSConrad Meyer 
488e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
489e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
490e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
491e974f91cSConrad Meyer 
492e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
493e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
494e974f91cSConrad Meyer 		return (ENODEV);
495e974f91cSConrad Meyer 	}
496e974f91cSConrad Meyer 
497e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
498e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
499e974f91cSConrad Meyer 	return (0);
500e974f91cSConrad Meyer }
501e974f91cSConrad Meyer 
502e974f91cSConrad Meyer static void
503e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
504e974f91cSConrad Meyer {
505e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
506e974f91cSConrad Meyer 
507cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
508e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
509e974f91cSConrad Meyer }
510e974f91cSConrad Meyer 
511e974f91cSConrad Meyer static void
512e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
513e974f91cSConrad Meyer {
514e974f91cSConrad Meyer 	bus_addr_t *baddr;
515e974f91cSConrad Meyer 
516cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
517e974f91cSConrad Meyer 	baddr = arg;
518e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
519e974f91cSConrad Meyer }
520e974f91cSConrad Meyer 
521e974f91cSConrad Meyer /*
522e974f91cSConrad Meyer  * Interrupt setup and handlers
523e974f91cSConrad Meyer  */
524e974f91cSConrad Meyer static int
5254253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
526e974f91cSConrad Meyer {
527e974f91cSConrad Meyer 	uint32_t num_vectors;
528e974f91cSConrad Meyer 	int error;
529e974f91cSConrad Meyer 	boolean_t use_msix;
530e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
531e974f91cSConrad Meyer 
532e974f91cSConrad Meyer 	use_msix = FALSE;
533e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
534e974f91cSConrad Meyer 
535e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
536e974f91cSConrad Meyer 		num_vectors = 1;
537e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
538e974f91cSConrad Meyer 		if (num_vectors == 1)
539e974f91cSConrad Meyer 			use_msix = TRUE;
540e974f91cSConrad Meyer 	}
541e974f91cSConrad Meyer 
542e974f91cSConrad Meyer 	if (use_msix) {
543e974f91cSConrad Meyer 		ioat->rid = 1;
544e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
545e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
546e974f91cSConrad Meyer 	} else {
547e974f91cSConrad Meyer 		ioat->rid = 0;
548e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
549e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
550e974f91cSConrad Meyer 	}
551e974f91cSConrad Meyer 	if (ioat->res == NULL) {
552e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
553e974f91cSConrad Meyer 		return (ENOMEM);
554e974f91cSConrad Meyer 	}
555e974f91cSConrad Meyer 
556e974f91cSConrad Meyer 	ioat->tag = NULL;
557e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
558e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
559e974f91cSConrad Meyer 	if (error != 0) {
560e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
561e974f91cSConrad Meyer 		return (error);
562e974f91cSConrad Meyer 	}
563e974f91cSConrad Meyer 
564e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
565e974f91cSConrad Meyer 	return (0);
566e974f91cSConrad Meyer }
567e974f91cSConrad Meyer 
5684253ea50SConrad Meyer static boolean_t
5690d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5704253ea50SConrad Meyer {
5714253ea50SConrad Meyer 	u_int32_t pciid;
5724253ea50SConrad Meyer 
5734253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5744253ea50SConrad Meyer 	switch (pciid) {
5750d1a05d9SConrad Meyer 		/* BWD: */
5760d1a05d9SConrad Meyer 	case 0x0c508086:
5770d1a05d9SConrad Meyer 	case 0x0c518086:
5780d1a05d9SConrad Meyer 	case 0x0c528086:
5790d1a05d9SConrad Meyer 	case 0x0c538086:
5800d1a05d9SConrad Meyer 		/* BDXDE: */
5814253ea50SConrad Meyer 	case 0x6f508086:
5824253ea50SConrad Meyer 	case 0x6f518086:
5834253ea50SConrad Meyer 	case 0x6f528086:
5844253ea50SConrad Meyer 	case 0x6f538086:
5854253ea50SConrad Meyer 		return (TRUE);
5864253ea50SConrad Meyer 	}
5874253ea50SConrad Meyer 
5884253ea50SConrad Meyer 	return (FALSE);
5894253ea50SConrad Meyer }
5904253ea50SConrad Meyer 
591e974f91cSConrad Meyer static void
592e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
593e974f91cSConrad Meyer {
594e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
595e974f91cSConrad Meyer 
59601fbbc88SConrad Meyer 	ioat->stats.interrupts++;
597e974f91cSConrad Meyer 	ioat_process_events(ioat);
598e974f91cSConrad Meyer }
599e974f91cSConrad Meyer 
600faefad9cSConrad Meyer static int
601faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
602faefad9cSConrad Meyer {
603faefad9cSConrad Meyer 
604faefad9cSConrad Meyer 	if (chanerr == 0)
605faefad9cSConrad Meyer 		return (0);
606faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
607faefad9cSConrad Meyer 		return (EFAULT);
608faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
609faefad9cSConrad Meyer 		return (EIO);
610faefad9cSConrad Meyer 	/* This one is probably our fault: */
611faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
612faefad9cSConrad Meyer 		return (EIO);
613faefad9cSConrad Meyer 	return (EIO);
614faefad9cSConrad Meyer }
615faefad9cSConrad Meyer 
616e974f91cSConrad Meyer static void
617e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
618e974f91cSConrad Meyer {
619e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
620e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
621e974f91cSConrad Meyer 	uint64_t comp_update, status;
622faefad9cSConrad Meyer 	uint32_t completed, chanerr;
623faefad9cSConrad Meyer 	int error;
624e974f91cSConrad Meyer 
625e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
626e974f91cSConrad Meyer 
627e974f91cSConrad Meyer 	completed = 0;
628e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
629e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
630e974f91cSConrad Meyer 
63143fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
632e974f91cSConrad Meyer 
633007a7030SConrad Meyer 	if (status == ioat->last_seen) {
634007a7030SConrad Meyer 		/*
635007a7030SConrad Meyer 		 * If we landed in process_events and nothing has been
636007a7030SConrad Meyer 		 * completed, check for a timeout due to channel halt.
637007a7030SConrad Meyer 		 */
638007a7030SConrad Meyer 		comp_update = ioat_get_chansts(ioat);
6394becebdfSConrad Meyer 		goto out;
640007a7030SConrad Meyer 	}
641e974f91cSConrad Meyer 
642e974f91cSConrad Meyer 	while (1) {
643e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
644e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
64543fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
646e974f91cSConrad Meyer 
647faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
648faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
649e974f91cSConrad Meyer 
650466b3540SConrad Meyer 		completed++;
651e974f91cSConrad Meyer 		ioat->tail++;
652e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
653e974f91cSConrad Meyer 			break;
654e974f91cSConrad Meyer 	}
655e974f91cSConrad Meyer 
656e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
657e974f91cSConrad Meyer 
658e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
659e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
660fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
661fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
662e974f91cSConrad Meyer 	}
663e974f91cSConrad Meyer 
66401fbbc88SConrad Meyer 	ioat->stats.descriptors_processed += completed;
66501fbbc88SConrad Meyer 
6664becebdfSConrad Meyer out:
667e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
668e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
669466b3540SConrad Meyer 
670007a7030SConrad Meyer 	if (completed != 0) {
671466b3540SConrad Meyer 		ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
672bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
673007a7030SConrad Meyer 	}
674faefad9cSConrad Meyer 
675*d2c55e5aSConrad Meyer 	if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update))
676faefad9cSConrad Meyer 		return;
677faefad9cSConrad Meyer 
67801fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
67901fbbc88SConrad Meyer 
680faefad9cSConrad Meyer 	/*
681faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
682faefad9cSConrad Meyer 	 * work with error status and restart the engine.
683faefad9cSConrad Meyer 	 */
684faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
685faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
686faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
687faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
688faefad9cSConrad Meyer 
689faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
690faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
69101fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
692faefad9cSConrad Meyer 
693faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
694faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
695faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
696faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
697faefad9cSConrad Meyer 
698faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
699faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
700faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
701faefad9cSConrad Meyer 
702faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
703faefad9cSConrad Meyer 		ioat->tail++;
70401fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
70501fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
706faefad9cSConrad Meyer 	}
707faefad9cSConrad Meyer 
708faefad9cSConrad Meyer 	/* Clear error status */
709faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
710faefad9cSConrad Meyer 
711faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
712faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
713faefad9cSConrad Meyer 
714faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
715faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
716faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
717e974f91cSConrad Meyer }
718e974f91cSConrad Meyer 
719e974f91cSConrad Meyer /*
720e974f91cSConrad Meyer  * User API functions
721e974f91cSConrad Meyer  */
722e974f91cSConrad Meyer bus_dmaengine_t
723e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
724e974f91cSConrad Meyer {
7255f77bd3eSConrad Meyer 	struct ioat_softc *sc;
726e974f91cSConrad Meyer 
727466b3540SConrad Meyer 	if (index >= ioat_channel_index)
728e974f91cSConrad Meyer 		return (NULL);
7295f77bd3eSConrad Meyer 
7305f77bd3eSConrad Meyer 	sc = ioat_channel[index];
7315f77bd3eSConrad Meyer 	if (sc == NULL || sc->quiescing)
7325f77bd3eSConrad Meyer 		return (NULL);
7335f77bd3eSConrad Meyer 
7345f77bd3eSConrad Meyer 	return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine);
735466b3540SConrad Meyer }
736466b3540SConrad Meyer 
737466b3540SConrad Meyer void
738466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
739466b3540SConrad Meyer {
740466b3540SConrad Meyer 	struct ioat_softc *ioat;
741466b3540SConrad Meyer 
742466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
743466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
744e974f91cSConrad Meyer }
745e974f91cSConrad Meyer 
7465ca9fc2aSConrad Meyer int
74731bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
74831bf2875SConrad Meyer {
74931bf2875SConrad Meyer 	struct ioat_softc *ioat;
75031bf2875SConrad Meyer 
75131bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
75231bf2875SConrad Meyer 	return (ioat->version);
75331bf2875SConrad Meyer }
75431bf2875SConrad Meyer 
755bd81fe68SConrad Meyer size_t
756bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
757bd81fe68SConrad Meyer {
758bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
759bd81fe68SConrad Meyer 
760bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
761bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
762bd81fe68SConrad Meyer }
763bd81fe68SConrad Meyer 
76431bf2875SConrad Meyer int
7655ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
7665ca9fc2aSConrad Meyer {
7675ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
7685ca9fc2aSConrad Meyer 
7695ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7705ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
7715ca9fc2aSConrad Meyer 		return (ENODEV);
7725ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
7735ca9fc2aSConrad Meyer 		return (ERANGE);
7745ca9fc2aSConrad Meyer 
7755ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
7765ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
7775ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
7785ca9fc2aSConrad Meyer 	return (0);
7795ca9fc2aSConrad Meyer }
7805ca9fc2aSConrad Meyer 
7815ca9fc2aSConrad Meyer uint16_t
7825ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
7835ca9fc2aSConrad Meyer {
7845ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
7855ca9fc2aSConrad Meyer 
7865ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7875ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
7885ca9fc2aSConrad Meyer }
7895ca9fc2aSConrad Meyer 
790e974f91cSConrad Meyer void
791e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
792e974f91cSConrad Meyer {
793e974f91cSConrad Meyer 	struct ioat_softc *ioat;
794e974f91cSConrad Meyer 
795e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
796e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
79743fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
798e974f91cSConrad Meyer }
799e974f91cSConrad Meyer 
8001502e363SConrad Meyer int
8011502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
8021502e363SConrad Meyer {
8031502e363SConrad Meyer 	struct ioat_softc *ioat;
8041502e363SConrad Meyer 	int error;
8051502e363SConrad Meyer 
8061502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8071502e363SConrad Meyer 	ioat_acquire(dmaengine);
8081502e363SConrad Meyer 
8091502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
8101502e363SConrad Meyer 	if (error != 0)
8111502e363SConrad Meyer 		ioat_release(dmaengine);
8121502e363SConrad Meyer 	return (error);
8131502e363SConrad Meyer }
8141502e363SConrad Meyer 
815e974f91cSConrad Meyer void
816e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
817e974f91cSConrad Meyer {
818e974f91cSConrad Meyer 	struct ioat_softc *ioat;
819e974f91cSConrad Meyer 
820e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
82143fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
822bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
823e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
824e974f91cSConrad Meyer }
825e974f91cSConrad Meyer 
8269e3bbf26SConrad Meyer static struct ioat_descriptor *
8279e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
8289e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
8299e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
8309e3bbf26SConrad Meyer     uint32_t flags)
831e974f91cSConrad Meyer {
8329e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
833e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
834bf8553eaSConrad Meyer 	int mflags;
835e974f91cSConrad Meyer 
8369e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
8379e3bbf26SConrad Meyer 
838e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
839e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
840bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
841bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
842bf8553eaSConrad Meyer 	else
843bf8553eaSConrad Meyer 		mflags = M_WAITOK;
844e974f91cSConrad Meyer 
8459e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
8469e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
8479e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
8489e3bbf26SConrad Meyer 		return (NULL);
8499e3bbf26SConrad Meyer 	}
850e974f91cSConrad Meyer 
851bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
852e974f91cSConrad Meyer 		return (NULL);
853e974f91cSConrad Meyer 
854e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
8559e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
856e974f91cSConrad Meyer 
857e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
8589e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
8599e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
860e974f91cSConrad Meyer 
861e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
8629e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
8636ca07079SConrad Meyer 	if ((flags & DMA_FENCE) != 0)
8646ca07079SConrad Meyer 		hw_desc->u.control_generic.fence = 1;
865e974f91cSConrad Meyer 
8669e3bbf26SConrad Meyer 	hw_desc->size = size;
8679e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
8689e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
869e974f91cSConrad Meyer 
870e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
871e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
8729e3bbf26SConrad Meyer 	return (desc);
8739e3bbf26SConrad Meyer }
874e974f91cSConrad Meyer 
8759e3bbf26SConrad Meyer struct bus_dmadesc *
8769e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
8779e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
8789e3bbf26SConrad Meyer {
8799e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8809e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
8819e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
8829e3bbf26SConrad Meyer 
8839e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8849e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8859e3bbf26SConrad Meyer 
8869e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
8879e3bbf26SConrad Meyer 	    callback_arg, flags);
8889e3bbf26SConrad Meyer 	if (desc == NULL)
8899e3bbf26SConrad Meyer 		return (NULL);
8909e3bbf26SConrad Meyer 
8919e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
8929e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
893e974f91cSConrad Meyer 	ioat_submit_single(ioat);
894e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
895e974f91cSConrad Meyer }
896e974f91cSConrad Meyer 
897e974f91cSConrad Meyer struct bus_dmadesc *
898e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
899e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
900e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
901e974f91cSConrad Meyer {
902e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9039e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
904e974f91cSConrad Meyer 	struct ioat_softc *ioat;
905e974f91cSConrad Meyer 
9069e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
907e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
908e974f91cSConrad Meyer 
9099e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
9109e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9119e3bbf26SConrad Meyer 		    __func__);
912e974f91cSConrad Meyer 		return (NULL);
913e974f91cSConrad Meyer 	}
914e974f91cSConrad Meyer 
9159e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
9169e3bbf26SConrad Meyer 	    callback_arg, flags);
9179e3bbf26SConrad Meyer 	if (desc == NULL)
918e974f91cSConrad Meyer 		return (NULL);
919e974f91cSConrad Meyer 
920e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
921e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
922e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
923e974f91cSConrad Meyer 
924e974f91cSConrad Meyer 	ioat_submit_single(ioat);
925e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
926e974f91cSConrad Meyer }
927e974f91cSConrad Meyer 
9282a4fd6b1SConrad Meyer struct bus_dmadesc *
9299950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
9309950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
9319950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
9329950fde0SConrad Meyer {
9339950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9349950fde0SConrad Meyer 	struct ioat_descriptor *desc;
9359950fde0SConrad Meyer 	struct ioat_softc *ioat;
9369950fde0SConrad Meyer 
9379950fde0SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9389950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9399950fde0SConrad Meyer 
9409950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
9419950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9429950fde0SConrad Meyer 		    __func__);
9439950fde0SConrad Meyer 		return (NULL);
9449950fde0SConrad Meyer 	}
9459950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
9469950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
9479950fde0SConrad Meyer 		    __func__);
9489950fde0SConrad Meyer 		return (NULL);
9499950fde0SConrad Meyer 	}
9509950fde0SConrad Meyer 
9519950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
9529950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
9539950fde0SConrad Meyer 	if (desc == NULL)
9549950fde0SConrad Meyer 		return (NULL);
9559950fde0SConrad Meyer 
9569950fde0SConrad Meyer 	hw_desc = desc->u.dma;
9579950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
9589950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
9599950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
9609950fde0SConrad Meyer 	}
9619950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
9629950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
9639950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
9649950fde0SConrad Meyer 	}
9659950fde0SConrad Meyer 
9669950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
9679950fde0SConrad Meyer 		dump_descriptor(hw_desc);
9689950fde0SConrad Meyer 
9699950fde0SConrad Meyer 	ioat_submit_single(ioat);
9709950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
9719950fde0SConrad Meyer }
9729950fde0SConrad Meyer 
9739950fde0SConrad Meyer struct bus_dmadesc *
9742a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
9752a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
9762a4fd6b1SConrad Meyer     uint32_t flags)
9772a4fd6b1SConrad Meyer {
9782a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
9792a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
9802a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
9812a4fd6b1SConrad Meyer 
9822a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9832a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9842a4fd6b1SConrad Meyer 
9851693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
9861693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
9871693d27bSConrad Meyer 		    __func__);
9881693d27bSConrad Meyer 		return (NULL);
9891693d27bSConrad Meyer 	}
9901693d27bSConrad Meyer 
9912a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
9922a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
9932a4fd6b1SConrad Meyer 		    __func__);
9942a4fd6b1SConrad Meyer 		return (NULL);
9952a4fd6b1SConrad Meyer 	}
9962a4fd6b1SConrad Meyer 
9972a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
9982a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
9992a4fd6b1SConrad Meyer 	if (desc == NULL)
10002a4fd6b1SConrad Meyer 		return (NULL);
10012a4fd6b1SConrad Meyer 
10022a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
10032a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
10042a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
10052a4fd6b1SConrad Meyer 
10062a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
10072a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
10082a4fd6b1SConrad Meyer }
10092a4fd6b1SConrad Meyer 
1010e974f91cSConrad Meyer /*
1011e974f91cSConrad Meyer  * Ring Management
1012e974f91cSConrad Meyer  */
1013e974f91cSConrad Meyer static inline uint32_t
1014e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1015e974f91cSConrad Meyer {
1016e974f91cSConrad Meyer 
1017e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1018e974f91cSConrad Meyer }
1019e974f91cSConrad Meyer 
1020e974f91cSConrad Meyer static inline uint32_t
1021e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1022e974f91cSConrad Meyer {
1023e974f91cSConrad Meyer 
1024e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1025e974f91cSConrad Meyer }
1026e974f91cSConrad Meyer 
1027e974f91cSConrad Meyer static struct ioat_descriptor *
1028bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
1029e974f91cSConrad Meyer {
10309e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1031e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1032bf8553eaSConrad Meyer 	int error, busdmaflag;
1033e974f91cSConrad Meyer 
1034f46011aeSConrad Meyer 	error = ENOMEM;
1035f46011aeSConrad Meyer 	hw_desc = NULL;
1036f46011aeSConrad Meyer 
1037bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
1038bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
1039bf8553eaSConrad Meyer 	else
1040bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
1041bf8553eaSConrad Meyer 
1042bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
1043e974f91cSConrad Meyer 	if (desc == NULL)
1044f46011aeSConrad Meyer 		goto out;
1045e974f91cSConrad Meyer 
1046f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
1047bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
1048f46011aeSConrad Meyer 	if (hw_desc == NULL)
1049f46011aeSConrad Meyer 		goto out;
1050e974f91cSConrad Meyer 
1051faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
10529e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
1053f46011aeSConrad Meyer 
1054f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
1055f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
1056bf8553eaSConrad Meyer 	    busdmaflag);
1057f46011aeSConrad Meyer 	if (error)
1058f46011aeSConrad Meyer 		goto out;
1059f46011aeSConrad Meyer 
1060f46011aeSConrad Meyer out:
1061f46011aeSConrad Meyer 	if (error) {
1062f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
1063f46011aeSConrad Meyer 		return (NULL);
1064f46011aeSConrad Meyer 	}
1065e974f91cSConrad Meyer 	return (desc);
1066e974f91cSConrad Meyer }
1067e974f91cSConrad Meyer 
1068e974f91cSConrad Meyer static void
1069e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
1070e974f91cSConrad Meyer {
1071e974f91cSConrad Meyer 
1072e974f91cSConrad Meyer 	if (desc == NULL)
1073e974f91cSConrad Meyer 		return;
1074e974f91cSConrad Meyer 
10759e3bbf26SConrad Meyer 	if (desc->u.generic)
10769e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
1077e974f91cSConrad Meyer 		    ioat->hw_desc_map);
1078e974f91cSConrad Meyer 	free(desc, M_IOAT);
1079e974f91cSConrad Meyer }
1080e974f91cSConrad Meyer 
1081bf8553eaSConrad Meyer /*
1082bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1083bf8553eaSConrad Meyer  * for 'num_descs'.
1084bf8553eaSConrad Meyer  *
1085bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1086bf8553eaSConrad Meyer  *
1087bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1088bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1089bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1090bf8553eaSConrad Meyer  *
1091bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1092bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1093bf8553eaSConrad Meyer  *
1094bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1095bf8553eaSConrad Meyer  * assured enough room is available.)
1096bf8553eaSConrad Meyer  */
1097e974f91cSConrad Meyer static int
1098bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1099e974f91cSConrad Meyer {
1100bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
1101bf8553eaSConrad Meyer 	uint32_t order;
1102bf8553eaSConrad Meyer 	int error;
1103e974f91cSConrad Meyer 
1104bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1105bf8553eaSConrad Meyer 	error = 0;
1106e974f91cSConrad Meyer 
1107bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
1108bf8553eaSConrad Meyer 		error = EINVAL;
1109bf8553eaSConrad Meyer 		goto out;
1110e974f91cSConrad Meyer 	}
11115f77bd3eSConrad Meyer 	if (ioat->quiescing) {
11125f77bd3eSConrad Meyer 		error = ENXIO;
11135f77bd3eSConrad Meyer 		goto out;
11145f77bd3eSConrad Meyer 	}
1115bf8553eaSConrad Meyer 
1116bf8553eaSConrad Meyer 	for (;;) {
1117bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1118bf8553eaSConrad Meyer 			goto out;
1119bf8553eaSConrad Meyer 
1120bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1121bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
1122bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
1123bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
1124bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
1125bf8553eaSConrad Meyer 				continue;
1126bf8553eaSConrad Meyer 			}
1127bf8553eaSConrad Meyer 
1128bf8553eaSConrad Meyer 			error = EAGAIN;
1129bf8553eaSConrad Meyer 			break;
1130bf8553eaSConrad Meyer 		}
1131bf8553eaSConrad Meyer 
1132bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1133bf8553eaSConrad Meyer 		for (;;) {
1134bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1135bf8553eaSConrad Meyer 
1136bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
1137bf8553eaSConrad Meyer 			    TRUE, mflags);
1138bf8553eaSConrad Meyer 
1139bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1140bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1141bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1142bf8553eaSConrad Meyer 
1143bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1144bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1145bf8553eaSConrad Meyer 				    ("allocation failed"));
1146bf8553eaSConrad Meyer 				error = EAGAIN;
1147bf8553eaSConrad Meyer 				break;
1148bf8553eaSConrad Meyer 			}
1149bf8553eaSConrad Meyer 
1150bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1151bf8553eaSConrad Meyer 			if (error == 0)
1152bf8553eaSConrad Meyer 				break;
1153bf8553eaSConrad Meyer 		}
1154bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1155bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1156bf8553eaSConrad Meyer 		if (error)
1157bf8553eaSConrad Meyer 			break;
1158bf8553eaSConrad Meyer 	}
1159bf8553eaSConrad Meyer 
1160bf8553eaSConrad Meyer out:
1161bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1162bf8553eaSConrad Meyer 	return (error);
1163bf8553eaSConrad Meyer }
1164bf8553eaSConrad Meyer 
1165bf8553eaSConrad Meyer static struct ioat_descriptor **
1166bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1167bf8553eaSConrad Meyer     int mflags)
1168bf8553eaSConrad Meyer {
1169bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1170bf8553eaSConrad Meyer 	uint32_t i;
1171bf8553eaSConrad Meyer 	int error;
1172bf8553eaSConrad Meyer 
1173bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1174bf8553eaSConrad Meyer 
1175bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1176bf8553eaSConrad Meyer 	if (ring == NULL)
1177bf8553eaSConrad Meyer 		return (NULL);
1178bf8553eaSConrad Meyer 
1179bf8553eaSConrad Meyer 	if (need_dscr) {
1180bf8553eaSConrad Meyer 		error = ENOMEM;
1181bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1182bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1183bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1184bf8553eaSConrad Meyer 				goto out;
1185bf8553eaSConrad Meyer 			ring[i]->id = i;
1186bf8553eaSConrad Meyer 		}
1187bf8553eaSConrad Meyer 	}
1188bf8553eaSConrad Meyer 	error = 0;
1189bf8553eaSConrad Meyer 
1190bf8553eaSConrad Meyer out:
1191bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1192bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1193bf8553eaSConrad Meyer 		ring = NULL;
1194bf8553eaSConrad Meyer 	}
1195bf8553eaSConrad Meyer 	return (ring);
1196bf8553eaSConrad Meyer }
1197bf8553eaSConrad Meyer 
1198bf8553eaSConrad Meyer static void
1199bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1200bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1201bf8553eaSConrad Meyer {
1202bf8553eaSConrad Meyer 	uint32_t i;
1203bf8553eaSConrad Meyer 
1204bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1205bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1206bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1207bf8553eaSConrad Meyer 	}
1208bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1209e974f91cSConrad Meyer }
1210e974f91cSConrad Meyer 
1211e974f91cSConrad Meyer static struct ioat_descriptor *
1212e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1213e974f91cSConrad Meyer {
1214e974f91cSConrad Meyer 
1215e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1216e974f91cSConrad Meyer }
1217e974f91cSConrad Meyer 
1218bf8553eaSConrad Meyer static int
1219bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1220bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1221e974f91cSConrad Meyer {
1222bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1223e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1224bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1225bf8553eaSConrad Meyer 	int error;
1226e974f91cSConrad Meyer 
1227bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1228e974f91cSConrad Meyer 
1229bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1230bf8553eaSConrad Meyer 
1231bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1232bf8553eaSConrad Meyer 		error = EINVAL;
1233bf8553eaSConrad Meyer 		goto out;
1234bf8553eaSConrad Meyer 	}
1235bf8553eaSConrad Meyer 
1236bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1237bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1238bf8553eaSConrad Meyer 
1239bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1240bf8553eaSConrad Meyer 
1241bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1242bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1243bf8553eaSConrad Meyer 
1244bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1245bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1246bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1247e974f91cSConrad Meyer 
1248e974f91cSConrad Meyer 	/*
1249bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1250bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1251e974f91cSConrad Meyer 	 */
1252bf8553eaSConrad Meyer 	if (head < tail) {
1253bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1254bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1255e974f91cSConrad Meyer 
1256bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1257bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1258e974f91cSConrad Meyer 
1259bf8553eaSConrad Meyer 			newring[i] = tmp;
1260bf8553eaSConrad Meyer 			newring[i]->id = i;
1261bf8553eaSConrad Meyer 		}
1262bf8553eaSConrad Meyer 		head += oldsize;
1263e974f91cSConrad Meyer 	}
1264e974f91cSConrad Meyer 
1265bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1266e974f91cSConrad Meyer 
1267bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1268bf8553eaSConrad Meyer 	if (head < oldsize) {
1269bf8553eaSConrad Meyer 		i = oldsize - 1;
1270bf8553eaSConrad Meyer 		end = newsize;
1271e974f91cSConrad Meyer 	} else {
1272bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1273bf8553eaSConrad Meyer 		i = head;
1274bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1275bf8553eaSConrad Meyer 	}
1276bf8553eaSConrad Meyer 
1277e974f91cSConrad Meyer 	/*
1278bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1279bf8553eaSConrad Meyer 	 * section (tail -> head).
1280e974f91cSConrad Meyer 	 */
1281bf8553eaSConrad Meyer 	for (; i < end; i++) {
1282bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1283bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1284e974f91cSConrad Meyer 
1285bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1286bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1287e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1288e974f91cSConrad Meyer 	}
1289e974f91cSConrad Meyer 
1290e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1291bf8553eaSConrad Meyer 	ioat->ring = newring;
1292bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1293bf8553eaSConrad Meyer 	ioat->tail = tail;
1294bf8553eaSConrad Meyer 	ioat->head = head;
1295bf8553eaSConrad Meyer 	error = 0;
1296e974f91cSConrad Meyer 
1297bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1298bf8553eaSConrad Meyer out:
1299bf8553eaSConrad Meyer 	if (error)
1300bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1301bf8553eaSConrad Meyer 	return (error);
1302bf8553eaSConrad Meyer }
1303bf8553eaSConrad Meyer 
1304bf8553eaSConrad Meyer static int
1305bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1306bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1307bf8553eaSConrad Meyer {
1308bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1309bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1310bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1311bf8553eaSConrad Meyer 	int error;
1312bf8553eaSConrad Meyer 
1313bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1314bf8553eaSConrad Meyer 
1315bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1316bf8553eaSConrad Meyer 
1317bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1318bf8553eaSConrad Meyer 		error = EINVAL;
1319bf8553eaSConrad Meyer 		goto out_unlocked;
1320bf8553eaSConrad Meyer 	}
1321bf8553eaSConrad Meyer 
1322bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1323bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1324bf8553eaSConrad Meyer 
1325bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1326bf8553eaSConrad Meyer 
1327bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1328bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1329bf8553eaSConrad Meyer 		error = ENOMEM;
1330bf8553eaSConrad Meyer 		goto out;
1331bf8553eaSConrad Meyer 	}
1332bf8553eaSConrad Meyer 
1333bf8553eaSConrad Meyer 	/*
1334bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1335bf8553eaSConrad Meyer 	 * descriptors.
1336bf8553eaSConrad Meyer 	 */
1337bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1338bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1339bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1340bf8553eaSConrad Meyer 
1341bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1342bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1343bf8553eaSConrad Meyer 	}
1344bf8553eaSConrad Meyer 
1345bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1346bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1347bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1348bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1349bf8553eaSConrad Meyer 	}
1350bf8553eaSConrad Meyer 
1351bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1352bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1353bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1354bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1355bf8553eaSConrad Meyer 
1356bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1357bf8553eaSConrad Meyer 	ioat->ring = newring;
1358bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1359bf8553eaSConrad Meyer 	error = 0;
1360bf8553eaSConrad Meyer 
1361bf8553eaSConrad Meyer out:
1362bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1363bf8553eaSConrad Meyer out_unlocked:
1364bf8553eaSConrad Meyer 	if (error)
1365bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1366bf8553eaSConrad Meyer 	return (error);
1367e974f91cSConrad Meyer }
1368e974f91cSConrad Meyer 
1369e974f91cSConrad Meyer static void
13708f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1371e974f91cSConrad Meyer {
1372e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
13738f274637SConrad Meyer 
137459acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
137559acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
13768f274637SConrad Meyer 	if (chanerr == 0)
13778f274637SConrad Meyer 		return;
13788f274637SConrad Meyer 
1379faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1380faefad9cSConrad Meyer 
13818f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
13828f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13838f274637SConrad Meyer 
13848f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
13858f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13868f274637SConrad Meyer }
13878f274637SConrad Meyer 
13888f274637SConrad Meyer static void
13898f274637SConrad Meyer ioat_timer_callback(void *arg)
13908f274637SConrad Meyer {
1391bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1392e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1393faefad9cSConrad Meyer 	uint32_t order;
1394e974f91cSConrad Meyer 
1395e974f91cSConrad Meyer 	ioat = arg;
1396fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1397e974f91cSConrad Meyer 
1398e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1399e974f91cSConrad Meyer 		ioat_process_events(ioat);
1400faefad9cSConrad Meyer 		return;
1401faefad9cSConrad Meyer 	}
1402faefad9cSConrad Meyer 
1403faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1404e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1405bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1406bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1407bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1408bf8553eaSConrad Meyer 		goto out;
1409bf8553eaSConrad Meyer 	}
1410bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1411e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1412e974f91cSConrad Meyer 
1413bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1414bf8553eaSConrad Meyer 	    M_NOWAIT);
1415bf8553eaSConrad Meyer 
1416bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1417bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1418bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1419bf8553eaSConrad Meyer 
1420bf8553eaSConrad Meyer 	if (newring != NULL)
1421bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1422bf8553eaSConrad Meyer 
1423bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1424bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1425bf8553eaSConrad Meyer 
1426bf8553eaSConrad Meyer out:
1427e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1428bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1429e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1430e974f91cSConrad Meyer }
1431e974f91cSConrad Meyer 
1432e974f91cSConrad Meyer /*
1433e974f91cSConrad Meyer  * Support Functions
1434e974f91cSConrad Meyer  */
1435e974f91cSConrad Meyer static void
1436e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1437e974f91cSConrad Meyer {
1438e974f91cSConrad Meyer 
1439466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1440e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1441bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1442e974f91cSConrad Meyer 
1443e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1444e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1445fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1446fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1447e974f91cSConrad Meyer 	}
144801fbbc88SConrad Meyer 
144901fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1450e974f91cSConrad Meyer }
1451e974f91cSConrad Meyer 
1452e974f91cSConrad Meyer static int
1453e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1454e974f91cSConrad Meyer {
1455e974f91cSConrad Meyer 	uint64_t status;
1456e974f91cSConrad Meyer 	uint32_t chanerr;
1457cea5b880SConrad Meyer 	unsigned timeout;
14585f77bd3eSConrad Meyer 	int error;
14595f77bd3eSConrad Meyer 
14605f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
14615f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
14625f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
14635f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1464e974f91cSConrad Meyer 
1465e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1466e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1467e974f91cSConrad Meyer 		ioat_suspend(ioat);
1468e974f91cSConrad Meyer 
1469e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1470e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1471e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1472e974f91cSConrad Meyer 		DELAY(1000);
1473e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1474e974f91cSConrad Meyer 	}
14755f77bd3eSConrad Meyer 	if (timeout == 20) {
14765f77bd3eSConrad Meyer 		error = ETIMEDOUT;
14775f77bd3eSConrad Meyer 		goto out;
14785f77bd3eSConrad Meyer 	}
1479e974f91cSConrad Meyer 
1480cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1481cea5b880SConrad Meyer 
1482e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1483e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1484e974f91cSConrad Meyer 
1485e974f91cSConrad Meyer 	/*
1486e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1487e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1488e974f91cSConrad Meyer 	 */
1489e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1490e974f91cSConrad Meyer 	    4);
1491e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1492e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1493e974f91cSConrad Meyer 
14940d1a05d9SConrad Meyer 	/*
14950d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
14960d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
14970d1a05d9SConrad Meyer 	 */
1498f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1499f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
15000d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1501f7157235SConrad Meyer 	}
15020d1a05d9SConrad Meyer 
1503e974f91cSConrad Meyer 	ioat_reset(ioat);
1504e974f91cSConrad Meyer 
1505e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1506e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1507e974f91cSConrad Meyer 		DELAY(1000);
15085f77bd3eSConrad Meyer 	if (timeout == 20) {
15095f77bd3eSConrad Meyer 		error = ETIMEDOUT;
15105f77bd3eSConrad Meyer 		goto out;
15115f77bd3eSConrad Meyer 	}
1512e974f91cSConrad Meyer 
1513f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1514f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
15150d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1516f7157235SConrad Meyer 	}
15174253ea50SConrad Meyer 
1518cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1519cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1520cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1521cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1522cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1523cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
15245f77bd3eSConrad Meyer 		error = 0;
15255f77bd3eSConrad Meyer 		goto out;
1526e974f91cSConrad Meyer 	}
1527e974f91cSConrad Meyer 
1528cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
15295f77bd3eSConrad Meyer 	if (chanerr != 0) {
1530faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1531faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1532faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
15335f77bd3eSConrad Meyer 		error = EIO;
15345f77bd3eSConrad Meyer 		goto out;
15355f77bd3eSConrad Meyer 	}
1536cea5b880SConrad Meyer 
1537cea5b880SConrad Meyer 	/*
1538cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1539cea5b880SConrad Meyer 	 * device back to active.
1540cea5b880SConrad Meyer 	 *
1541cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1542cea5b880SConrad Meyer 	 * at zero as well.
1543cea5b880SConrad Meyer 	 */
1544bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1545cea5b880SConrad Meyer 	ioat->last_seen = 0;
1546cea5b880SConrad Meyer 
1547cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1548cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1549cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
15505f77bd3eSConrad Meyer 	error = 0;
15515f77bd3eSConrad Meyer 
15525f77bd3eSConrad Meyer out:
15535f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
15545f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
15555f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
15565f77bd3eSConrad Meyer 
15575f77bd3eSConrad Meyer 	if (error == 0)
15585f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
15595f77bd3eSConrad Meyer 
15605f77bd3eSConrad Meyer 	return (error);
1561cea5b880SConrad Meyer }
1562cea5b880SConrad Meyer 
1563f7157235SConrad Meyer static int
1564faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1565faefad9cSConrad Meyer {
1566faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1567faefad9cSConrad Meyer 	struct sbuf sb;
1568faefad9cSConrad Meyer 	uint64_t status;
1569faefad9cSConrad Meyer 	int error;
1570faefad9cSConrad Meyer 
1571faefad9cSConrad Meyer 	ioat = arg1;
1572faefad9cSConrad Meyer 
1573faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1574faefad9cSConrad Meyer 
1575faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1576faefad9cSConrad Meyer 	switch (status) {
1577faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1578faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1579faefad9cSConrad Meyer 		break;
1580faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1581faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1582faefad9cSConrad Meyer 		break;
1583faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1584faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1585faefad9cSConrad Meyer 		break;
1586faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1587faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1588faefad9cSConrad Meyer 		break;
1589faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1590faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1591faefad9cSConrad Meyer 		break;
1592faefad9cSConrad Meyer 	default:
1593faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1594faefad9cSConrad Meyer 		break;
1595faefad9cSConrad Meyer 	}
1596faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1597faefad9cSConrad Meyer 	sbuf_delete(&sb);
1598faefad9cSConrad Meyer 
1599faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1600faefad9cSConrad Meyer 		return (error);
1601faefad9cSConrad Meyer 	return (EINVAL);
1602faefad9cSConrad Meyer }
1603faefad9cSConrad Meyer 
1604faefad9cSConrad Meyer static int
160501fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
160601fbbc88SConrad Meyer {
160701fbbc88SConrad Meyer 	struct ioat_softc *ioat;
160801fbbc88SConrad Meyer 	struct sbuf sb;
160901fbbc88SConrad Meyer #define	PRECISION	"1"
161001fbbc88SConrad Meyer 	const uintmax_t factor = 10;
161101fbbc88SConrad Meyer 	uintmax_t rate;
161201fbbc88SConrad Meyer 	int error;
161301fbbc88SConrad Meyer 
161401fbbc88SConrad Meyer 	ioat = arg1;
161501fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
161601fbbc88SConrad Meyer 
161701fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
161801fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
161901fbbc88SConrad Meyer 		goto out;
162001fbbc88SConrad Meyer 	}
162101fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
162201fbbc88SConrad Meyer 	    ioat->stats.interrupts;
162301fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
162401fbbc88SConrad Meyer 	    rate % factor);
162501fbbc88SConrad Meyer #undef	PRECISION
162601fbbc88SConrad Meyer out:
162701fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
162801fbbc88SConrad Meyer 	sbuf_delete(&sb);
162901fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
163001fbbc88SConrad Meyer 		return (error);
163101fbbc88SConrad Meyer 	return (EINVAL);
163201fbbc88SConrad Meyer }
163301fbbc88SConrad Meyer 
163401fbbc88SConrad Meyer static int
1635faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1636faefad9cSConrad Meyer {
1637faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1638faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1639faefad9cSConrad Meyer 	int error, arg;
1640faefad9cSConrad Meyer 
1641faefad9cSConrad Meyer 	ioat = arg1;
1642faefad9cSConrad Meyer 
1643faefad9cSConrad Meyer 	arg = 0;
1644faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1645faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1646faefad9cSConrad Meyer 		return (error);
1647faefad9cSConrad Meyer 
1648faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1649faefad9cSConrad Meyer 	if (error != 0)
1650faefad9cSConrad Meyer 		return (error);
1651faefad9cSConrad Meyer 
1652faefad9cSConrad Meyer 	if (arg != 0) {
1653faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1654faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1655faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1656faefad9cSConrad Meyer 		    0);
1657faefad9cSConrad Meyer 		if (desc == NULL)
1658faefad9cSConrad Meyer 			error = ENOMEM;
1659faefad9cSConrad Meyer 		else
1660faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1661faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1662faefad9cSConrad Meyer 	}
1663faefad9cSConrad Meyer 	return (error);
1664faefad9cSConrad Meyer }
1665faefad9cSConrad Meyer 
1666faefad9cSConrad Meyer static int
1667f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1668f7157235SConrad Meyer {
1669f7157235SConrad Meyer 	struct ioat_softc *ioat;
1670f7157235SConrad Meyer 	int error, arg;
1671f7157235SConrad Meyer 
1672f7157235SConrad Meyer 	ioat = arg1;
1673f7157235SConrad Meyer 
1674f7157235SConrad Meyer 	arg = 0;
1675f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1676f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1677f7157235SConrad Meyer 		return (error);
1678f7157235SConrad Meyer 
1679f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1680f7157235SConrad Meyer 	if (error != 0)
1681f7157235SConrad Meyer 		return (error);
1682f7157235SConrad Meyer 
1683f7157235SConrad Meyer 	if (arg != 0)
1684f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1685f7157235SConrad Meyer 
1686f7157235SConrad Meyer 	return (error);
1687f7157235SConrad Meyer }
1688f7157235SConrad Meyer 
1689e974f91cSConrad Meyer static void
1690e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1691e974f91cSConrad Meyer {
1692e974f91cSConrad Meyer 	int i, j;
1693e974f91cSConrad Meyer 
1694e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1695e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1696e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1697e974f91cSConrad Meyer 		printf("\n");
1698e974f91cSConrad Meyer 	}
1699e974f91cSConrad Meyer }
1700e974f91cSConrad Meyer 
1701e974f91cSConrad Meyer static void
1702e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1703e974f91cSConrad Meyer {
170401fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1705f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
170601fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1707e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1708e974f91cSConrad Meyer 
1709e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1710f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1711f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1712f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1713e974f91cSConrad Meyer 
171465e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
171565e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
171665e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
171765e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
17185ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
17195ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
17205ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
17215ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
17225ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
172365e4f8adSConrad Meyer 
172401fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
172501fbbc88SConrad Meyer 	    "IOAT channel internal state");
172601fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
172701fbbc88SConrad Meyer 
172801fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1729bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
173001fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
173101fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
173201fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
173301fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
173401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1735bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1736f7157235SConrad Meyer 
173701fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
173865e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
173965e4f8adSConrad Meyer 
174001fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
174165e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
174201fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
174301fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
174401fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
174565e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
174601fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
174765e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
174865e4f8adSConrad Meyer 
174901fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1750faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1751faefad9cSConrad Meyer 	    "String of the channel status");
175201fbbc88SConrad Meyer 
17535ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
17545ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
17555ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
17565ca9fc2aSConrad Meyer 
175701fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
175801fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
175901fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
176001fbbc88SConrad Meyer 
176101fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
176201fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
176301fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
176401fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error",
176501fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
176601fbbc88SConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
176701fbbc88SConrad Meyer 
176801fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
176901fbbc88SConrad Meyer 	    "IOAT channel statistics");
177001fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
177101fbbc88SConrad Meyer 
177201fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
177301fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
177401fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
177501fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
177601fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
177701fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
177801fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
177901fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
178001fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
178101fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
178201fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
178301fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
178401fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
178501fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
178601fbbc88SConrad Meyer 	    "Number of times the channel has halted");
178701fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
178801fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
178901fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
179001fbbc88SConrad Meyer 
179101fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
179201fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
179301fbbc88SConrad Meyer 	    "Descriptors per interrupt");
1794e974f91cSConrad Meyer }
1795466b3540SConrad Meyer 
1796466b3540SConrad Meyer static inline struct ioat_softc *
1797466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1798466b3540SConrad Meyer {
1799466b3540SConrad Meyer 	uint32_t old;
1800466b3540SConrad Meyer 
1801466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1802466b3540SConrad Meyer 
1803466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1804466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1805466b3540SConrad Meyer 
1806466b3540SConrad Meyer #ifdef INVARIANTS
1807466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1808466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1809466b3540SConrad Meyer #endif
1810466b3540SConrad Meyer 
1811466b3540SConrad Meyer 	return (ioat);
1812466b3540SConrad Meyer }
1813466b3540SConrad Meyer 
1814466b3540SConrad Meyer static inline void
1815466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1816466b3540SConrad Meyer {
1817faefad9cSConrad Meyer 
1818faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1819faefad9cSConrad Meyer }
1820faefad9cSConrad Meyer 
1821faefad9cSConrad Meyer static inline void
1822faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1823faefad9cSConrad Meyer {
1824faefad9cSConrad Meyer 
1825faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1826faefad9cSConrad Meyer }
1827faefad9cSConrad Meyer 
1828faefad9cSConrad Meyer static inline void
1829faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1830faefad9cSConrad Meyer     boolean_t locked)
1831faefad9cSConrad Meyer {
1832466b3540SConrad Meyer 	uint32_t old;
1833466b3540SConrad Meyer 
1834466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1835466b3540SConrad Meyer 
1836466b3540SConrad Meyer 	if (n == 0)
1837466b3540SConrad Meyer 		return;
1838466b3540SConrad Meyer 
1839466b3540SConrad Meyer #ifdef INVARIANTS
1840466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1841466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1842466b3540SConrad Meyer #endif
1843466b3540SConrad Meyer 
1844466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1845466b3540SConrad Meyer 	for (;;) {
1846466b3540SConrad Meyer 		old = ioat->refcnt;
1847466b3540SConrad Meyer 		if (old <= n)
1848466b3540SConrad Meyer 			break;
1849466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1850466b3540SConrad Meyer 			return;
1851466b3540SConrad Meyer 	}
1852466b3540SConrad Meyer 
1853faefad9cSConrad Meyer 	if (locked)
1854faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1855faefad9cSConrad Meyer 	else
1856466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1857faefad9cSConrad Meyer 
1858466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1859466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1860466b3540SConrad Meyer 
1861466b3540SConrad Meyer 	if (old == n)
1862466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1863faefad9cSConrad Meyer 	if (!locked)
1864466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1865466b3540SConrad Meyer }
1866466b3540SConrad Meyer 
1867466b3540SConrad Meyer static inline void
1868466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1869466b3540SConrad Meyer {
1870466b3540SConrad Meyer 
1871466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1872466b3540SConrad Meyer }
1873466b3540SConrad Meyer 
1874466b3540SConrad Meyer static void
18755f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1876466b3540SConrad Meyer {
1877466b3540SConrad Meyer 
18785f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1879466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1880466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1881466b3540SConrad Meyer }
1882