xref: /freebsd/sys/dev/ioat/ioat.c (revision 01fbbc8886fe6b66c19efde29f96ac5f30ef0965)
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 
407e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
408e974f91cSConrad Meyer 
409e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
410faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
4117afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
412e974f91cSConrad Meyer 
413faefad9cSConrad Meyer 	/* Establish lock order for Witness */
414faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
415faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
416faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
417faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
418faefad9cSConrad Meyer 
419e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
420e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
421e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
422e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
423e974f91cSConrad Meyer 
424e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
425e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
426e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
427e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
428e974f91cSConrad Meyer 
429e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
430e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
431e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
432e974f91cSConrad Meyer 		return (ENOMEM);
433e974f91cSConrad Meyer 
434e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
435e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
436e974f91cSConrad Meyer 	    0);
437e974f91cSConrad Meyer 	if (error != 0)
438e974f91cSConrad Meyer 		return (error);
439e974f91cSConrad Meyer 
440e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
441e974f91cSConrad Meyer 
442e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
443e974f91cSConrad Meyer 
444e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
445e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
446e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
447e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
448e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
449e974f91cSConrad Meyer 
450e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
451bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
452e974f91cSConrad Meyer 	if (ioat->ring == NULL)
453e974f91cSConrad Meyer 		return (ENOMEM);
454e974f91cSConrad Meyer 
455e974f91cSConrad Meyer 	ring = ioat->ring;
456e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
457bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
458e974f91cSConrad Meyer 		if (ring[i] == NULL)
459e974f91cSConrad Meyer 			return (ENOMEM);
460e974f91cSConrad Meyer 
461e974f91cSConrad Meyer 		ring[i]->id = i;
462e974f91cSConrad Meyer 	}
463e974f91cSConrad Meyer 
464e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
465e974f91cSConrad Meyer 		next = ring[i + 1];
466e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
467e974f91cSConrad Meyer 
468e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
469e974f91cSConrad Meyer 	}
470e974f91cSConrad Meyer 
471e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
472e974f91cSConrad Meyer 
473bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
474e974f91cSConrad Meyer 	ioat->tail = 0;
475e974f91cSConrad Meyer 	ioat->last_seen = 0;
476e974f91cSConrad Meyer 	return (0);
477e974f91cSConrad Meyer }
478e974f91cSConrad Meyer 
479e974f91cSConrad Meyer static int
480e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
481e974f91cSConrad Meyer {
482e974f91cSConrad Meyer 
483e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
484e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
485e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
486e974f91cSConrad Meyer 
487e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
488e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
489e974f91cSConrad Meyer 		return (ENODEV);
490e974f91cSConrad Meyer 	}
491e974f91cSConrad Meyer 
492e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
493e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
494e974f91cSConrad Meyer 	return (0);
495e974f91cSConrad Meyer }
496e974f91cSConrad Meyer 
497e974f91cSConrad Meyer static void
498e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
499e974f91cSConrad Meyer {
500e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
501e974f91cSConrad Meyer 
502cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
503e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
504e974f91cSConrad Meyer }
505e974f91cSConrad Meyer 
506e974f91cSConrad Meyer static void
507e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
508e974f91cSConrad Meyer {
509e974f91cSConrad Meyer 	bus_addr_t *baddr;
510e974f91cSConrad Meyer 
511cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
512e974f91cSConrad Meyer 	baddr = arg;
513e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
514e974f91cSConrad Meyer }
515e974f91cSConrad Meyer 
516e974f91cSConrad Meyer /*
517e974f91cSConrad Meyer  * Interrupt setup and handlers
518e974f91cSConrad Meyer  */
519e974f91cSConrad Meyer static int
5204253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
521e974f91cSConrad Meyer {
522e974f91cSConrad Meyer 	uint32_t num_vectors;
523e974f91cSConrad Meyer 	int error;
524e974f91cSConrad Meyer 	boolean_t use_msix;
525e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
526e974f91cSConrad Meyer 
527e974f91cSConrad Meyer 	use_msix = FALSE;
528e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
529e974f91cSConrad Meyer 
530e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
531e974f91cSConrad Meyer 		num_vectors = 1;
532e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
533e974f91cSConrad Meyer 		if (num_vectors == 1)
534e974f91cSConrad Meyer 			use_msix = TRUE;
535e974f91cSConrad Meyer 	}
536e974f91cSConrad Meyer 
537e974f91cSConrad Meyer 	if (use_msix) {
538e974f91cSConrad Meyer 		ioat->rid = 1;
539e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
540e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
541e974f91cSConrad Meyer 	} else {
542e974f91cSConrad Meyer 		ioat->rid = 0;
543e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
544e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
545e974f91cSConrad Meyer 	}
546e974f91cSConrad Meyer 	if (ioat->res == NULL) {
547e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
548e974f91cSConrad Meyer 		return (ENOMEM);
549e974f91cSConrad Meyer 	}
550e974f91cSConrad Meyer 
551e974f91cSConrad Meyer 	ioat->tag = NULL;
552e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
553e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
554e974f91cSConrad Meyer 	if (error != 0) {
555e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
556e974f91cSConrad Meyer 		return (error);
557e974f91cSConrad Meyer 	}
558e974f91cSConrad Meyer 
559e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
560e974f91cSConrad Meyer 	return (0);
561e974f91cSConrad Meyer }
562e974f91cSConrad Meyer 
5634253ea50SConrad Meyer static boolean_t
5640d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5654253ea50SConrad Meyer {
5664253ea50SConrad Meyer 	u_int32_t pciid;
5674253ea50SConrad Meyer 
5684253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5694253ea50SConrad Meyer 	switch (pciid) {
5700d1a05d9SConrad Meyer 		/* BWD: */
5710d1a05d9SConrad Meyer 	case 0x0c508086:
5720d1a05d9SConrad Meyer 	case 0x0c518086:
5730d1a05d9SConrad Meyer 	case 0x0c528086:
5740d1a05d9SConrad Meyer 	case 0x0c538086:
5750d1a05d9SConrad Meyer 		/* BDXDE: */
5764253ea50SConrad Meyer 	case 0x6f508086:
5774253ea50SConrad Meyer 	case 0x6f518086:
5784253ea50SConrad Meyer 	case 0x6f528086:
5794253ea50SConrad Meyer 	case 0x6f538086:
5804253ea50SConrad Meyer 		return (TRUE);
5814253ea50SConrad Meyer 	}
5824253ea50SConrad Meyer 
5834253ea50SConrad Meyer 	return (FALSE);
5844253ea50SConrad Meyer }
5854253ea50SConrad Meyer 
586e974f91cSConrad Meyer static void
587e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
588e974f91cSConrad Meyer {
589e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
590e974f91cSConrad Meyer 
591*01fbbc88SConrad Meyer 	ioat->stats.interrupts++;
592e974f91cSConrad Meyer 	ioat_process_events(ioat);
593e974f91cSConrad Meyer }
594e974f91cSConrad Meyer 
595faefad9cSConrad Meyer static int
596faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
597faefad9cSConrad Meyer {
598faefad9cSConrad Meyer 
599faefad9cSConrad Meyer 	if (chanerr == 0)
600faefad9cSConrad Meyer 		return (0);
601faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
602faefad9cSConrad Meyer 		return (EFAULT);
603faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
604faefad9cSConrad Meyer 		return (EIO);
605faefad9cSConrad Meyer 	/* This one is probably our fault: */
606faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
607faefad9cSConrad Meyer 		return (EIO);
608faefad9cSConrad Meyer 	return (EIO);
609faefad9cSConrad Meyer }
610faefad9cSConrad Meyer 
611e974f91cSConrad Meyer static void
612e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
613e974f91cSConrad Meyer {
614e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
615e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
616e974f91cSConrad Meyer 	uint64_t comp_update, status;
617faefad9cSConrad Meyer 	uint32_t completed, chanerr;
618faefad9cSConrad Meyer 	int error;
619e974f91cSConrad Meyer 
620e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
621e974f91cSConrad Meyer 
622e974f91cSConrad Meyer 	completed = 0;
623e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
624e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
625e974f91cSConrad Meyer 
62643fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
627e974f91cSConrad Meyer 
6284becebdfSConrad Meyer 	if (status == ioat->last_seen)
6294becebdfSConrad Meyer 		goto out;
630e974f91cSConrad Meyer 
631e974f91cSConrad Meyer 	while (1) {
632e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
633e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
63443fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
635e974f91cSConrad Meyer 
636faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
637faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
638e974f91cSConrad Meyer 
639466b3540SConrad Meyer 		completed++;
640e974f91cSConrad Meyer 		ioat->tail++;
641e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
642e974f91cSConrad Meyer 			break;
643e974f91cSConrad Meyer 	}
644e974f91cSConrad Meyer 
645e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
646e974f91cSConrad Meyer 
647e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
648e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
649fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
650fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
651e974f91cSConrad Meyer 	}
652e974f91cSConrad Meyer 
653*01fbbc88SConrad Meyer 	ioat->stats.descriptors_processed += completed;
654*01fbbc88SConrad Meyer 
6554becebdfSConrad Meyer out:
656e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
657e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
658466b3540SConrad Meyer 
659466b3540SConrad Meyer 	ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
660bf8553eaSConrad Meyer 	wakeup(&ioat->tail);
661faefad9cSConrad Meyer 
662faefad9cSConrad Meyer 	if (!is_ioat_halted(comp_update))
663faefad9cSConrad Meyer 		return;
664faefad9cSConrad Meyer 
665*01fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
666*01fbbc88SConrad Meyer 
667faefad9cSConrad Meyer 	/*
668faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
669faefad9cSConrad Meyer 	 * work with error status and restart the engine.
670faefad9cSConrad Meyer 	 */
671faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
672faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
673faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
674faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
675faefad9cSConrad Meyer 
676faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
677faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
678*01fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
679faefad9cSConrad Meyer 
680faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
681faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
682faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
683faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
684faefad9cSConrad Meyer 
685faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
686faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
687faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
688faefad9cSConrad Meyer 
689faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
690faefad9cSConrad Meyer 		ioat->tail++;
691*01fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
692*01fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
693faefad9cSConrad Meyer 	}
694faefad9cSConrad Meyer 
695faefad9cSConrad Meyer 	/* Clear error status */
696faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
697faefad9cSConrad Meyer 
698faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
699faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
700faefad9cSConrad Meyer 
701faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
702faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
703faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
704e974f91cSConrad Meyer }
705e974f91cSConrad Meyer 
706e974f91cSConrad Meyer /*
707e974f91cSConrad Meyer  * User API functions
708e974f91cSConrad Meyer  */
709e974f91cSConrad Meyer bus_dmaengine_t
710e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
711e974f91cSConrad Meyer {
7125f77bd3eSConrad Meyer 	struct ioat_softc *sc;
713e974f91cSConrad Meyer 
714466b3540SConrad Meyer 	if (index >= ioat_channel_index)
715e974f91cSConrad Meyer 		return (NULL);
7165f77bd3eSConrad Meyer 
7175f77bd3eSConrad Meyer 	sc = ioat_channel[index];
7185f77bd3eSConrad Meyer 	if (sc == NULL || sc->quiescing)
7195f77bd3eSConrad Meyer 		return (NULL);
7205f77bd3eSConrad Meyer 
7215f77bd3eSConrad Meyer 	return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine);
722466b3540SConrad Meyer }
723466b3540SConrad Meyer 
724466b3540SConrad Meyer void
725466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
726466b3540SConrad Meyer {
727466b3540SConrad Meyer 	struct ioat_softc *ioat;
728466b3540SConrad Meyer 
729466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
730466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
731e974f91cSConrad Meyer }
732e974f91cSConrad Meyer 
733e974f91cSConrad Meyer void
734e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
735e974f91cSConrad Meyer {
736e974f91cSConrad Meyer 	struct ioat_softc *ioat;
737e974f91cSConrad Meyer 
738e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
739e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
74043fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
741e974f91cSConrad Meyer }
742e974f91cSConrad Meyer 
743e974f91cSConrad Meyer void
744e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
745e974f91cSConrad Meyer {
746e974f91cSConrad Meyer 	struct ioat_softc *ioat;
747e974f91cSConrad Meyer 
748e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
74943fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
750bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
751e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
752e974f91cSConrad Meyer }
753e974f91cSConrad Meyer 
7549e3bbf26SConrad Meyer static struct ioat_descriptor *
7559e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
7569e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
7579e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
7589e3bbf26SConrad Meyer     uint32_t flags)
759e974f91cSConrad Meyer {
7609e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
761e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
762bf8553eaSConrad Meyer 	int mflags;
763e974f91cSConrad Meyer 
7649e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
7659e3bbf26SConrad Meyer 
766e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
767e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
768bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
769bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
770bf8553eaSConrad Meyer 	else
771bf8553eaSConrad Meyer 		mflags = M_WAITOK;
772e974f91cSConrad Meyer 
7739e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
7749e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
7759e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
7769e3bbf26SConrad Meyer 		return (NULL);
7779e3bbf26SConrad Meyer 	}
778e974f91cSConrad Meyer 
779bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
780e974f91cSConrad Meyer 		return (NULL);
781e974f91cSConrad Meyer 
782e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
7839e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
784e974f91cSConrad Meyer 
785e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
7869e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
7879e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
788e974f91cSConrad Meyer 
789e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
7909e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
791e974f91cSConrad Meyer 
7929e3bbf26SConrad Meyer 	hw_desc->size = size;
7939e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
7949e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
795e974f91cSConrad Meyer 
796e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
797e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
7989e3bbf26SConrad Meyer 	return (desc);
7999e3bbf26SConrad Meyer }
800e974f91cSConrad Meyer 
8019e3bbf26SConrad Meyer struct bus_dmadesc *
8029e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
8039e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
8049e3bbf26SConrad Meyer {
8059e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8069e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
8079e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
8089e3bbf26SConrad Meyer 
8099e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8109e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8119e3bbf26SConrad Meyer 
8129e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
8139e3bbf26SConrad Meyer 	    callback_arg, flags);
8149e3bbf26SConrad Meyer 	if (desc == NULL)
8159e3bbf26SConrad Meyer 		return (NULL);
8169e3bbf26SConrad Meyer 
8179e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
8189e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
819e974f91cSConrad Meyer 	ioat_submit_single(ioat);
820e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
821e974f91cSConrad Meyer }
822e974f91cSConrad Meyer 
823e974f91cSConrad Meyer struct bus_dmadesc *
824e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
825e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
826e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
827e974f91cSConrad Meyer {
828e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8299e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
830e974f91cSConrad Meyer 	struct ioat_softc *ioat;
831e974f91cSConrad Meyer 
8329e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
833e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
834e974f91cSConrad Meyer 
8359e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
8369e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
8379e3bbf26SConrad Meyer 		    __func__);
838e974f91cSConrad Meyer 		return (NULL);
839e974f91cSConrad Meyer 	}
840e974f91cSConrad Meyer 
8419e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
8429e3bbf26SConrad Meyer 	    callback_arg, flags);
8439e3bbf26SConrad Meyer 	if (desc == NULL)
844e974f91cSConrad Meyer 		return (NULL);
845e974f91cSConrad Meyer 
846e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
847e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
848e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
849e974f91cSConrad Meyer 
850e974f91cSConrad Meyer 	ioat_submit_single(ioat);
851e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
852e974f91cSConrad Meyer }
853e974f91cSConrad Meyer 
8542a4fd6b1SConrad Meyer struct bus_dmadesc *
8559950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
8569950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
8579950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
8589950fde0SConrad Meyer {
8599950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8609950fde0SConrad Meyer 	struct ioat_descriptor *desc;
8619950fde0SConrad Meyer 	struct ioat_softc *ioat;
8629950fde0SConrad Meyer 
8639950fde0SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8649950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8659950fde0SConrad Meyer 
8669950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
8679950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
8689950fde0SConrad Meyer 		    __func__);
8699950fde0SConrad Meyer 		return (NULL);
8709950fde0SConrad Meyer 	}
8719950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
8729950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
8739950fde0SConrad Meyer 		    __func__);
8749950fde0SConrad Meyer 		return (NULL);
8759950fde0SConrad Meyer 	}
8769950fde0SConrad Meyer 
8779950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
8789950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
8799950fde0SConrad Meyer 	if (desc == NULL)
8809950fde0SConrad Meyer 		return (NULL);
8819950fde0SConrad Meyer 
8829950fde0SConrad Meyer 	hw_desc = desc->u.dma;
8839950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
8849950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
8859950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
8869950fde0SConrad Meyer 	}
8879950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
8889950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
8899950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
8909950fde0SConrad Meyer 	}
8919950fde0SConrad Meyer 
8929950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
8939950fde0SConrad Meyer 		dump_descriptor(hw_desc);
8949950fde0SConrad Meyer 
8959950fde0SConrad Meyer 	ioat_submit_single(ioat);
8969950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
8979950fde0SConrad Meyer }
8989950fde0SConrad Meyer 
8999950fde0SConrad Meyer struct bus_dmadesc *
9002a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
9012a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
9022a4fd6b1SConrad Meyer     uint32_t flags)
9032a4fd6b1SConrad Meyer {
9042a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
9052a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
9062a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
9072a4fd6b1SConrad Meyer 
9082a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9092a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9102a4fd6b1SConrad Meyer 
9111693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
9121693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
9131693d27bSConrad Meyer 		    __func__);
9141693d27bSConrad Meyer 		return (NULL);
9151693d27bSConrad Meyer 	}
9161693d27bSConrad Meyer 
9172a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
9182a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
9192a4fd6b1SConrad Meyer 		    __func__);
9202a4fd6b1SConrad Meyer 		return (NULL);
9212a4fd6b1SConrad Meyer 	}
9222a4fd6b1SConrad Meyer 
9232a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
9242a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
9252a4fd6b1SConrad Meyer 	if (desc == NULL)
9262a4fd6b1SConrad Meyer 		return (NULL);
9272a4fd6b1SConrad Meyer 
9282a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
9292a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
9302a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
9312a4fd6b1SConrad Meyer 
9322a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
9332a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
9342a4fd6b1SConrad Meyer }
9352a4fd6b1SConrad Meyer 
936e974f91cSConrad Meyer /*
937e974f91cSConrad Meyer  * Ring Management
938e974f91cSConrad Meyer  */
939e974f91cSConrad Meyer static inline uint32_t
940e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
941e974f91cSConrad Meyer {
942e974f91cSConrad Meyer 
943e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
944e974f91cSConrad Meyer }
945e974f91cSConrad Meyer 
946e974f91cSConrad Meyer static inline uint32_t
947e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
948e974f91cSConrad Meyer {
949e974f91cSConrad Meyer 
950e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
951e974f91cSConrad Meyer }
952e974f91cSConrad Meyer 
953e974f91cSConrad Meyer static struct ioat_descriptor *
954bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
955e974f91cSConrad Meyer {
9569e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
957e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
958bf8553eaSConrad Meyer 	int error, busdmaflag;
959e974f91cSConrad Meyer 
960f46011aeSConrad Meyer 	error = ENOMEM;
961f46011aeSConrad Meyer 	hw_desc = NULL;
962f46011aeSConrad Meyer 
963bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
964bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
965bf8553eaSConrad Meyer 	else
966bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
967bf8553eaSConrad Meyer 
968bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
969e974f91cSConrad Meyer 	if (desc == NULL)
970f46011aeSConrad Meyer 		goto out;
971e974f91cSConrad Meyer 
972f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
973bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
974f46011aeSConrad Meyer 	if (hw_desc == NULL)
975f46011aeSConrad Meyer 		goto out;
976e974f91cSConrad Meyer 
977faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
9789e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
979f46011aeSConrad Meyer 
980f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
981f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
982bf8553eaSConrad Meyer 	    busdmaflag);
983f46011aeSConrad Meyer 	if (error)
984f46011aeSConrad Meyer 		goto out;
985f46011aeSConrad Meyer 
986f46011aeSConrad Meyer out:
987f46011aeSConrad Meyer 	if (error) {
988f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
989f46011aeSConrad Meyer 		return (NULL);
990f46011aeSConrad Meyer 	}
991e974f91cSConrad Meyer 	return (desc);
992e974f91cSConrad Meyer }
993e974f91cSConrad Meyer 
994e974f91cSConrad Meyer static void
995e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
996e974f91cSConrad Meyer {
997e974f91cSConrad Meyer 
998e974f91cSConrad Meyer 	if (desc == NULL)
999e974f91cSConrad Meyer 		return;
1000e974f91cSConrad Meyer 
10019e3bbf26SConrad Meyer 	if (desc->u.generic)
10029e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
1003e974f91cSConrad Meyer 		    ioat->hw_desc_map);
1004e974f91cSConrad Meyer 	free(desc, M_IOAT);
1005e974f91cSConrad Meyer }
1006e974f91cSConrad Meyer 
1007bf8553eaSConrad Meyer /*
1008bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1009bf8553eaSConrad Meyer  * for 'num_descs'.
1010bf8553eaSConrad Meyer  *
1011bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1012bf8553eaSConrad Meyer  *
1013bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1014bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1015bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1016bf8553eaSConrad Meyer  *
1017bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1018bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1019bf8553eaSConrad Meyer  *
1020bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1021bf8553eaSConrad Meyer  * assured enough room is available.)
1022bf8553eaSConrad Meyer  */
1023e974f91cSConrad Meyer static int
1024bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1025e974f91cSConrad Meyer {
1026bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
1027bf8553eaSConrad Meyer 	uint32_t order;
1028bf8553eaSConrad Meyer 	int error;
1029e974f91cSConrad Meyer 
1030bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1031bf8553eaSConrad Meyer 	error = 0;
1032e974f91cSConrad Meyer 
1033bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
1034bf8553eaSConrad Meyer 		error = EINVAL;
1035bf8553eaSConrad Meyer 		goto out;
1036e974f91cSConrad Meyer 	}
10375f77bd3eSConrad Meyer 	if (ioat->quiescing) {
10385f77bd3eSConrad Meyer 		error = ENXIO;
10395f77bd3eSConrad Meyer 		goto out;
10405f77bd3eSConrad Meyer 	}
1041bf8553eaSConrad Meyer 
1042bf8553eaSConrad Meyer 	for (;;) {
1043bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1044bf8553eaSConrad Meyer 			goto out;
1045bf8553eaSConrad Meyer 
1046bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1047bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
1048bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
1049bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
1050bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
1051bf8553eaSConrad Meyer 				continue;
1052bf8553eaSConrad Meyer 			}
1053bf8553eaSConrad Meyer 
1054bf8553eaSConrad Meyer 			error = EAGAIN;
1055bf8553eaSConrad Meyer 			break;
1056bf8553eaSConrad Meyer 		}
1057bf8553eaSConrad Meyer 
1058bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1059bf8553eaSConrad Meyer 		for (;;) {
1060bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1061bf8553eaSConrad Meyer 
1062bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
1063bf8553eaSConrad Meyer 			    TRUE, mflags);
1064bf8553eaSConrad Meyer 
1065bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1066bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1067bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1068bf8553eaSConrad Meyer 
1069bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1070bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1071bf8553eaSConrad Meyer 				    ("allocation failed"));
1072bf8553eaSConrad Meyer 				error = EAGAIN;
1073bf8553eaSConrad Meyer 				break;
1074bf8553eaSConrad Meyer 			}
1075bf8553eaSConrad Meyer 
1076bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1077bf8553eaSConrad Meyer 			if (error == 0)
1078bf8553eaSConrad Meyer 				break;
1079bf8553eaSConrad Meyer 		}
1080bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1081bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1082bf8553eaSConrad Meyer 		if (error)
1083bf8553eaSConrad Meyer 			break;
1084bf8553eaSConrad Meyer 	}
1085bf8553eaSConrad Meyer 
1086bf8553eaSConrad Meyer out:
1087bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1088bf8553eaSConrad Meyer 	return (error);
1089bf8553eaSConrad Meyer }
1090bf8553eaSConrad Meyer 
1091bf8553eaSConrad Meyer static struct ioat_descriptor **
1092bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1093bf8553eaSConrad Meyer     int mflags)
1094bf8553eaSConrad Meyer {
1095bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1096bf8553eaSConrad Meyer 	uint32_t i;
1097bf8553eaSConrad Meyer 	int error;
1098bf8553eaSConrad Meyer 
1099bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1100bf8553eaSConrad Meyer 
1101bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1102bf8553eaSConrad Meyer 	if (ring == NULL)
1103bf8553eaSConrad Meyer 		return (NULL);
1104bf8553eaSConrad Meyer 
1105bf8553eaSConrad Meyer 	if (need_dscr) {
1106bf8553eaSConrad Meyer 		error = ENOMEM;
1107bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1108bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1109bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1110bf8553eaSConrad Meyer 				goto out;
1111bf8553eaSConrad Meyer 			ring[i]->id = i;
1112bf8553eaSConrad Meyer 		}
1113bf8553eaSConrad Meyer 	}
1114bf8553eaSConrad Meyer 	error = 0;
1115bf8553eaSConrad Meyer 
1116bf8553eaSConrad Meyer out:
1117bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1118bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1119bf8553eaSConrad Meyer 		ring = NULL;
1120bf8553eaSConrad Meyer 	}
1121bf8553eaSConrad Meyer 	return (ring);
1122bf8553eaSConrad Meyer }
1123bf8553eaSConrad Meyer 
1124bf8553eaSConrad Meyer static void
1125bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1126bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1127bf8553eaSConrad Meyer {
1128bf8553eaSConrad Meyer 	uint32_t i;
1129bf8553eaSConrad Meyer 
1130bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1131bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1132bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1133bf8553eaSConrad Meyer 	}
1134bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1135e974f91cSConrad Meyer }
1136e974f91cSConrad Meyer 
1137e974f91cSConrad Meyer static struct ioat_descriptor *
1138e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1139e974f91cSConrad Meyer {
1140e974f91cSConrad Meyer 
1141e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1142e974f91cSConrad Meyer }
1143e974f91cSConrad Meyer 
1144bf8553eaSConrad Meyer static int
1145bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1146bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1147e974f91cSConrad Meyer {
1148bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1149e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1150bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1151bf8553eaSConrad Meyer 	int error;
1152e974f91cSConrad Meyer 
1153bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1154e974f91cSConrad Meyer 
1155bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1156bf8553eaSConrad Meyer 
1157bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1158bf8553eaSConrad Meyer 		error = EINVAL;
1159bf8553eaSConrad Meyer 		goto out;
1160bf8553eaSConrad Meyer 	}
1161bf8553eaSConrad Meyer 
1162bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1163bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1164bf8553eaSConrad Meyer 
1165bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1166bf8553eaSConrad Meyer 
1167bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1168bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1169bf8553eaSConrad Meyer 
1170bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1171bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1172bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1173e974f91cSConrad Meyer 
1174e974f91cSConrad Meyer 	/*
1175bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1176bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1177e974f91cSConrad Meyer 	 */
1178bf8553eaSConrad Meyer 	if (head < tail) {
1179bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1180bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1181e974f91cSConrad Meyer 
1182bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1183bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1184e974f91cSConrad Meyer 
1185bf8553eaSConrad Meyer 			newring[i] = tmp;
1186bf8553eaSConrad Meyer 			newring[i]->id = i;
1187bf8553eaSConrad Meyer 		}
1188bf8553eaSConrad Meyer 		head += oldsize;
1189e974f91cSConrad Meyer 	}
1190e974f91cSConrad Meyer 
1191bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1192e974f91cSConrad Meyer 
1193bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1194bf8553eaSConrad Meyer 	if (head < oldsize) {
1195bf8553eaSConrad Meyer 		i = oldsize - 1;
1196bf8553eaSConrad Meyer 		end = newsize;
1197e974f91cSConrad Meyer 	} else {
1198bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1199bf8553eaSConrad Meyer 		i = head;
1200bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1201bf8553eaSConrad Meyer 	}
1202bf8553eaSConrad Meyer 
1203e974f91cSConrad Meyer 	/*
1204bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1205bf8553eaSConrad Meyer 	 * section (tail -> head).
1206e974f91cSConrad Meyer 	 */
1207bf8553eaSConrad Meyer 	for (; i < end; i++) {
1208bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1209bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1210e974f91cSConrad Meyer 
1211bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1212bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1213e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1214e974f91cSConrad Meyer 	}
1215e974f91cSConrad Meyer 
1216e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1217bf8553eaSConrad Meyer 	ioat->ring = newring;
1218bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1219bf8553eaSConrad Meyer 	ioat->tail = tail;
1220bf8553eaSConrad Meyer 	ioat->head = head;
1221bf8553eaSConrad Meyer 	error = 0;
1222e974f91cSConrad Meyer 
1223bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1224bf8553eaSConrad Meyer out:
1225bf8553eaSConrad Meyer 	if (error)
1226bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1227bf8553eaSConrad Meyer 	return (error);
1228bf8553eaSConrad Meyer }
1229bf8553eaSConrad Meyer 
1230bf8553eaSConrad Meyer static int
1231bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1232bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1233bf8553eaSConrad Meyer {
1234bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1235bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1236bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1237bf8553eaSConrad Meyer 	int error;
1238bf8553eaSConrad Meyer 
1239bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1240bf8553eaSConrad Meyer 
1241bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1242bf8553eaSConrad Meyer 
1243bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1244bf8553eaSConrad Meyer 		error = EINVAL;
1245bf8553eaSConrad Meyer 		goto out_unlocked;
1246bf8553eaSConrad Meyer 	}
1247bf8553eaSConrad Meyer 
1248bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1249bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1250bf8553eaSConrad Meyer 
1251bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1252bf8553eaSConrad Meyer 
1253bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1254bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1255bf8553eaSConrad Meyer 		error = ENOMEM;
1256bf8553eaSConrad Meyer 		goto out;
1257bf8553eaSConrad Meyer 	}
1258bf8553eaSConrad Meyer 
1259bf8553eaSConrad Meyer 	/*
1260bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1261bf8553eaSConrad Meyer 	 * descriptors.
1262bf8553eaSConrad Meyer 	 */
1263bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1264bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1265bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1266bf8553eaSConrad Meyer 
1267bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1268bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1269bf8553eaSConrad Meyer 	}
1270bf8553eaSConrad Meyer 
1271bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1272bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1273bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1274bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1275bf8553eaSConrad Meyer 	}
1276bf8553eaSConrad Meyer 
1277bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1278bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1279bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1280bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1281bf8553eaSConrad Meyer 
1282bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1283bf8553eaSConrad Meyer 	ioat->ring = newring;
1284bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1285bf8553eaSConrad Meyer 	error = 0;
1286bf8553eaSConrad Meyer 
1287bf8553eaSConrad Meyer out:
1288bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1289bf8553eaSConrad Meyer out_unlocked:
1290bf8553eaSConrad Meyer 	if (error)
1291bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1292bf8553eaSConrad Meyer 	return (error);
1293e974f91cSConrad Meyer }
1294e974f91cSConrad Meyer 
1295e974f91cSConrad Meyer static void
12968f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1297e974f91cSConrad Meyer {
1298e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
12998f274637SConrad Meyer 
130059acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
130159acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
13028f274637SConrad Meyer 	if (chanerr == 0)
13038f274637SConrad Meyer 		return;
13048f274637SConrad Meyer 
1305faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1306faefad9cSConrad Meyer 
13078f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
13088f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13098f274637SConrad Meyer 
13108f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
13118f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13128f274637SConrad Meyer }
13138f274637SConrad Meyer 
13148f274637SConrad Meyer static void
13158f274637SConrad Meyer ioat_timer_callback(void *arg)
13168f274637SConrad Meyer {
1317bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1318e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1319faefad9cSConrad Meyer 	uint32_t order;
1320e974f91cSConrad Meyer 
1321e974f91cSConrad Meyer 	ioat = arg;
1322fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1323e974f91cSConrad Meyer 
1324e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1325e974f91cSConrad Meyer 		ioat_process_events(ioat);
1326faefad9cSConrad Meyer 		return;
1327faefad9cSConrad Meyer 	}
1328faefad9cSConrad Meyer 
1329faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1330e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1331bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1332bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1333bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1334bf8553eaSConrad Meyer 		goto out;
1335bf8553eaSConrad Meyer 	}
1336bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1337e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1338e974f91cSConrad Meyer 
1339bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1340bf8553eaSConrad Meyer 	    M_NOWAIT);
1341bf8553eaSConrad Meyer 
1342bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1343bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1344bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1345bf8553eaSConrad Meyer 
1346bf8553eaSConrad Meyer 	if (newring != NULL)
1347bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1348bf8553eaSConrad Meyer 
1349bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1350bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1351bf8553eaSConrad Meyer 
1352bf8553eaSConrad Meyer out:
1353e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1354bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1355e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1356e974f91cSConrad Meyer }
1357e974f91cSConrad Meyer 
1358e974f91cSConrad Meyer /*
1359e974f91cSConrad Meyer  * Support Functions
1360e974f91cSConrad Meyer  */
1361e974f91cSConrad Meyer static void
1362e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1363e974f91cSConrad Meyer {
1364e974f91cSConrad Meyer 
1365466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1366e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1367bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1368e974f91cSConrad Meyer 
1369e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1370e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1371fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1372fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1373e974f91cSConrad Meyer 	}
1374*01fbbc88SConrad Meyer 
1375*01fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1376e974f91cSConrad Meyer }
1377e974f91cSConrad Meyer 
1378e974f91cSConrad Meyer static int
1379e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1380e974f91cSConrad Meyer {
1381e974f91cSConrad Meyer 	uint64_t status;
1382e974f91cSConrad Meyer 	uint32_t chanerr;
1383cea5b880SConrad Meyer 	unsigned timeout;
13845f77bd3eSConrad Meyer 	int error;
13855f77bd3eSConrad Meyer 
13865f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
13875f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
13885f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
13895f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1390e974f91cSConrad Meyer 
1391e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1392e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1393e974f91cSConrad Meyer 		ioat_suspend(ioat);
1394e974f91cSConrad Meyer 
1395e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1396e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1397e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1398e974f91cSConrad Meyer 		DELAY(1000);
1399e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1400e974f91cSConrad Meyer 	}
14015f77bd3eSConrad Meyer 	if (timeout == 20) {
14025f77bd3eSConrad Meyer 		error = ETIMEDOUT;
14035f77bd3eSConrad Meyer 		goto out;
14045f77bd3eSConrad Meyer 	}
1405e974f91cSConrad Meyer 
1406cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1407cea5b880SConrad Meyer 
1408e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1409e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1410e974f91cSConrad Meyer 
1411e974f91cSConrad Meyer 	/*
1412e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1413e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1414e974f91cSConrad Meyer 	 */
1415e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1416e974f91cSConrad Meyer 	    4);
1417e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1418e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1419e974f91cSConrad Meyer 
14200d1a05d9SConrad Meyer 	/*
14210d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
14220d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
14230d1a05d9SConrad Meyer 	 */
1424f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1425f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
14260d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1427f7157235SConrad Meyer 	}
14280d1a05d9SConrad Meyer 
1429e974f91cSConrad Meyer 	ioat_reset(ioat);
1430e974f91cSConrad Meyer 
1431e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1432e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1433e974f91cSConrad Meyer 		DELAY(1000);
14345f77bd3eSConrad Meyer 	if (timeout == 20) {
14355f77bd3eSConrad Meyer 		error = ETIMEDOUT;
14365f77bd3eSConrad Meyer 		goto out;
14375f77bd3eSConrad Meyer 	}
1438e974f91cSConrad Meyer 
1439f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1440f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
14410d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1442f7157235SConrad Meyer 	}
14434253ea50SConrad Meyer 
1444cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1445cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1446cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1447cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1448cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1449cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
14505f77bd3eSConrad Meyer 		error = 0;
14515f77bd3eSConrad Meyer 		goto out;
1452e974f91cSConrad Meyer 	}
1453e974f91cSConrad Meyer 
1454cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
14555f77bd3eSConrad Meyer 	if (chanerr != 0) {
1456faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1457faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1458faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
14595f77bd3eSConrad Meyer 		error = EIO;
14605f77bd3eSConrad Meyer 		goto out;
14615f77bd3eSConrad Meyer 	}
1462cea5b880SConrad Meyer 
1463cea5b880SConrad Meyer 	/*
1464cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1465cea5b880SConrad Meyer 	 * device back to active.
1466cea5b880SConrad Meyer 	 *
1467cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1468cea5b880SConrad Meyer 	 * at zero as well.
1469cea5b880SConrad Meyer 	 */
1470bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1471cea5b880SConrad Meyer 	ioat->last_seen = 0;
1472cea5b880SConrad Meyer 
1473cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1474cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1475cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
14765f77bd3eSConrad Meyer 	error = 0;
14775f77bd3eSConrad Meyer 
14785f77bd3eSConrad Meyer out:
14795f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
14805f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
14815f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
14825f77bd3eSConrad Meyer 
14835f77bd3eSConrad Meyer 	if (error == 0)
14845f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
14855f77bd3eSConrad Meyer 
14865f77bd3eSConrad Meyer 	return (error);
1487cea5b880SConrad Meyer }
1488cea5b880SConrad Meyer 
1489f7157235SConrad Meyer static int
1490faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1491faefad9cSConrad Meyer {
1492faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1493faefad9cSConrad Meyer 	struct sbuf sb;
1494faefad9cSConrad Meyer 	uint64_t status;
1495faefad9cSConrad Meyer 	int error;
1496faefad9cSConrad Meyer 
1497faefad9cSConrad Meyer 	ioat = arg1;
1498faefad9cSConrad Meyer 
1499faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1500faefad9cSConrad Meyer 
1501faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1502faefad9cSConrad Meyer 	switch (status) {
1503faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1504faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1505faefad9cSConrad Meyer 		break;
1506faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1507faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1508faefad9cSConrad Meyer 		break;
1509faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1510faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1511faefad9cSConrad Meyer 		break;
1512faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1513faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1514faefad9cSConrad Meyer 		break;
1515faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1516faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1517faefad9cSConrad Meyer 		break;
1518faefad9cSConrad Meyer 	default:
1519faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1520faefad9cSConrad Meyer 		break;
1521faefad9cSConrad Meyer 	}
1522faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1523faefad9cSConrad Meyer 	sbuf_delete(&sb);
1524faefad9cSConrad Meyer 
1525faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1526faefad9cSConrad Meyer 		return (error);
1527faefad9cSConrad Meyer 	return (EINVAL);
1528faefad9cSConrad Meyer }
1529faefad9cSConrad Meyer 
1530faefad9cSConrad Meyer static int
1531*01fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
1532*01fbbc88SConrad Meyer {
1533*01fbbc88SConrad Meyer 	struct ioat_softc *ioat;
1534*01fbbc88SConrad Meyer 	struct sbuf sb;
1535*01fbbc88SConrad Meyer #define	PRECISION	"1"
1536*01fbbc88SConrad Meyer 	const uintmax_t factor = 10;
1537*01fbbc88SConrad Meyer 	uintmax_t rate;
1538*01fbbc88SConrad Meyer 	int error;
1539*01fbbc88SConrad Meyer 
1540*01fbbc88SConrad Meyer 	ioat = arg1;
1541*01fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
1542*01fbbc88SConrad Meyer 
1543*01fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
1544*01fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
1545*01fbbc88SConrad Meyer 		goto out;
1546*01fbbc88SConrad Meyer 	}
1547*01fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
1548*01fbbc88SConrad Meyer 	    ioat->stats.interrupts;
1549*01fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
1550*01fbbc88SConrad Meyer 	    rate % factor);
1551*01fbbc88SConrad Meyer #undef	PRECISION
1552*01fbbc88SConrad Meyer out:
1553*01fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
1554*01fbbc88SConrad Meyer 	sbuf_delete(&sb);
1555*01fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1556*01fbbc88SConrad Meyer 		return (error);
1557*01fbbc88SConrad Meyer 	return (EINVAL);
1558*01fbbc88SConrad Meyer }
1559*01fbbc88SConrad Meyer 
1560*01fbbc88SConrad Meyer static int
1561faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1562faefad9cSConrad Meyer {
1563faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1564faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1565faefad9cSConrad Meyer 	int error, arg;
1566faefad9cSConrad Meyer 
1567faefad9cSConrad Meyer 	ioat = arg1;
1568faefad9cSConrad Meyer 
1569faefad9cSConrad Meyer 	arg = 0;
1570faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1571faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1572faefad9cSConrad Meyer 		return (error);
1573faefad9cSConrad Meyer 
1574faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1575faefad9cSConrad Meyer 	if (error != 0)
1576faefad9cSConrad Meyer 		return (error);
1577faefad9cSConrad Meyer 
1578faefad9cSConrad Meyer 	if (arg != 0) {
1579faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1580faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1581faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1582faefad9cSConrad Meyer 		    0);
1583faefad9cSConrad Meyer 		if (desc == NULL)
1584faefad9cSConrad Meyer 			error = ENOMEM;
1585faefad9cSConrad Meyer 		else
1586faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1587faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1588faefad9cSConrad Meyer 	}
1589faefad9cSConrad Meyer 	return (error);
1590faefad9cSConrad Meyer }
1591faefad9cSConrad Meyer 
1592faefad9cSConrad Meyer static int
1593f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1594f7157235SConrad Meyer {
1595f7157235SConrad Meyer 	struct ioat_softc *ioat;
1596f7157235SConrad Meyer 	int error, arg;
1597f7157235SConrad Meyer 
1598f7157235SConrad Meyer 	ioat = arg1;
1599f7157235SConrad Meyer 
1600f7157235SConrad Meyer 	arg = 0;
1601f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1602f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1603f7157235SConrad Meyer 		return (error);
1604f7157235SConrad Meyer 
1605f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1606f7157235SConrad Meyer 	if (error != 0)
1607f7157235SConrad Meyer 		return (error);
1608f7157235SConrad Meyer 
1609f7157235SConrad Meyer 	if (arg != 0)
1610f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1611f7157235SConrad Meyer 
1612f7157235SConrad Meyer 	return (error);
1613f7157235SConrad Meyer }
1614f7157235SConrad Meyer 
1615e974f91cSConrad Meyer static void
1616e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1617e974f91cSConrad Meyer {
1618e974f91cSConrad Meyer 	int i, j;
1619e974f91cSConrad Meyer 
1620e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1621e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1622e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1623e974f91cSConrad Meyer 		printf("\n");
1624e974f91cSConrad Meyer 	}
1625e974f91cSConrad Meyer }
1626e974f91cSConrad Meyer 
1627e974f91cSConrad Meyer static void
1628e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1629e974f91cSConrad Meyer {
1630*01fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1631f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
1632*01fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1633e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1634e974f91cSConrad Meyer 
1635e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1636f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1637f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1638f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1639e974f91cSConrad Meyer 
164065e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
164165e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
164265e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
164365e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
164465e4f8adSConrad Meyer 
1645*01fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
1646*01fbbc88SConrad Meyer 	    "IOAT channel internal state");
1647*01fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
1648*01fbbc88SConrad Meyer 
1649*01fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1650bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
1651*01fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
1652*01fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
1653*01fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
1654*01fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
1655*01fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1656bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1657f7157235SConrad Meyer 
1658*01fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
165965e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
166065e4f8adSConrad Meyer 
1661*01fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
166265e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
1663*01fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
1664*01fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
1665*01fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
166665e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
1667*01fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
166865e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
166965e4f8adSConrad Meyer 
1670*01fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1671faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1672faefad9cSConrad Meyer 	    "String of the channel status");
1673*01fbbc88SConrad Meyer 
1674*01fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
1675*01fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
1676*01fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
1677*01fbbc88SConrad Meyer 
1678*01fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
1679*01fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1680*01fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
1681*01fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error",
1682*01fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
1683*01fbbc88SConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
1684*01fbbc88SConrad Meyer 
1685*01fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
1686*01fbbc88SConrad Meyer 	    "IOAT channel statistics");
1687*01fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
1688*01fbbc88SConrad Meyer 
1689*01fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
1690*01fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
1691*01fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
1692*01fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
1693*01fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
1694*01fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
1695*01fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
1696*01fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
1697*01fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
1698*01fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
1699*01fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
1700*01fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
1701*01fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
1702*01fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
1703*01fbbc88SConrad Meyer 	    "Number of times the channel has halted");
1704*01fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
1705*01fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
1706*01fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
1707*01fbbc88SConrad Meyer 
1708*01fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
1709*01fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
1710*01fbbc88SConrad Meyer 	    "Descriptors per interrupt");
1711e974f91cSConrad Meyer }
1712466b3540SConrad Meyer 
1713466b3540SConrad Meyer static inline struct ioat_softc *
1714466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1715466b3540SConrad Meyer {
1716466b3540SConrad Meyer 	uint32_t old;
1717466b3540SConrad Meyer 
1718466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1719466b3540SConrad Meyer 
1720466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1721466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1722466b3540SConrad Meyer 
1723466b3540SConrad Meyer #ifdef INVARIANTS
1724466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1725466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1726466b3540SConrad Meyer #endif
1727466b3540SConrad Meyer 
1728466b3540SConrad Meyer 	return (ioat);
1729466b3540SConrad Meyer }
1730466b3540SConrad Meyer 
1731466b3540SConrad Meyer static inline void
1732466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1733466b3540SConrad Meyer {
1734faefad9cSConrad Meyer 
1735faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1736faefad9cSConrad Meyer }
1737faefad9cSConrad Meyer 
1738faefad9cSConrad Meyer static inline void
1739faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1740faefad9cSConrad Meyer {
1741faefad9cSConrad Meyer 
1742faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1743faefad9cSConrad Meyer }
1744faefad9cSConrad Meyer 
1745faefad9cSConrad Meyer static inline void
1746faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1747faefad9cSConrad Meyer     boolean_t locked)
1748faefad9cSConrad Meyer {
1749466b3540SConrad Meyer 	uint32_t old;
1750466b3540SConrad Meyer 
1751466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1752466b3540SConrad Meyer 
1753466b3540SConrad Meyer 	if (n == 0)
1754466b3540SConrad Meyer 		return;
1755466b3540SConrad Meyer 
1756466b3540SConrad Meyer #ifdef INVARIANTS
1757466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1758466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1759466b3540SConrad Meyer #endif
1760466b3540SConrad Meyer 
1761466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1762466b3540SConrad Meyer 	for (;;) {
1763466b3540SConrad Meyer 		old = ioat->refcnt;
1764466b3540SConrad Meyer 		if (old <= n)
1765466b3540SConrad Meyer 			break;
1766466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1767466b3540SConrad Meyer 			return;
1768466b3540SConrad Meyer 	}
1769466b3540SConrad Meyer 
1770faefad9cSConrad Meyer 	if (locked)
1771faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1772faefad9cSConrad Meyer 	else
1773466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1774faefad9cSConrad Meyer 
1775466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1776466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1777466b3540SConrad Meyer 
1778466b3540SConrad Meyer 	if (old == n)
1779466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1780faefad9cSConrad Meyer 	if (!locked)
1781466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1782466b3540SConrad Meyer }
1783466b3540SConrad Meyer 
1784466b3540SConrad Meyer static inline void
1785466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1786466b3540SConrad Meyer {
1787466b3540SConrad Meyer 
1788466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1789466b3540SConrad Meyer }
1790466b3540SConrad Meyer 
1791466b3540SConrad Meyer static void
17925f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1793466b3540SConrad Meyer {
1794466b3540SConrad Meyer 
17955f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1796466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1797466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1798466b3540SConrad Meyer }
1799