xref: /freebsd/sys/dev/ioat/ioat.c (revision 1502e36346e5a499b67f0de80338f65b9877841a)
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 
6334becebdfSConrad Meyer 	if (status == ioat->last_seen)
6344becebdfSConrad Meyer 		goto out;
635e974f91cSConrad Meyer 
636e974f91cSConrad Meyer 	while (1) {
637e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
638e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
63943fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
640e974f91cSConrad Meyer 
641faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
642faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
643e974f91cSConrad Meyer 
644466b3540SConrad Meyer 		completed++;
645e974f91cSConrad Meyer 		ioat->tail++;
646e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
647e974f91cSConrad Meyer 			break;
648e974f91cSConrad Meyer 	}
649e974f91cSConrad Meyer 
650e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
651e974f91cSConrad Meyer 
652e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
653e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
654fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
655fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
656e974f91cSConrad Meyer 	}
657e974f91cSConrad Meyer 
65801fbbc88SConrad Meyer 	ioat->stats.descriptors_processed += completed;
65901fbbc88SConrad Meyer 
6604becebdfSConrad Meyer out:
661e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
662e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
663466b3540SConrad Meyer 
664466b3540SConrad Meyer 	ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
665bf8553eaSConrad Meyer 	wakeup(&ioat->tail);
666faefad9cSConrad Meyer 
667faefad9cSConrad Meyer 	if (!is_ioat_halted(comp_update))
668faefad9cSConrad Meyer 		return;
669faefad9cSConrad Meyer 
67001fbbc88SConrad Meyer 	ioat->stats.channel_halts++;
67101fbbc88SConrad Meyer 
672faefad9cSConrad Meyer 	/*
673faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
674faefad9cSConrad Meyer 	 * work with error status and restart the engine.
675faefad9cSConrad Meyer 	 */
676faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
677faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
678faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
679faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
680faefad9cSConrad Meyer 
681faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
682faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
68301fbbc88SConrad Meyer 	ioat->stats.last_halt_chanerr = chanerr;
684faefad9cSConrad Meyer 
685faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
686faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
687faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
688faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
689faefad9cSConrad Meyer 
690faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
691faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
692faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
693faefad9cSConrad Meyer 
694faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
695faefad9cSConrad Meyer 		ioat->tail++;
69601fbbc88SConrad Meyer 		ioat->stats.descriptors_processed++;
69701fbbc88SConrad Meyer 		ioat->stats.descriptors_error++;
698faefad9cSConrad Meyer 	}
699faefad9cSConrad Meyer 
700faefad9cSConrad Meyer 	/* Clear error status */
701faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
702faefad9cSConrad Meyer 
703faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
704faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
705faefad9cSConrad Meyer 
706faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
707faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
708faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
709e974f91cSConrad Meyer }
710e974f91cSConrad Meyer 
711e974f91cSConrad Meyer /*
712e974f91cSConrad Meyer  * User API functions
713e974f91cSConrad Meyer  */
714e974f91cSConrad Meyer bus_dmaengine_t
715e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
716e974f91cSConrad Meyer {
7175f77bd3eSConrad Meyer 	struct ioat_softc *sc;
718e974f91cSConrad Meyer 
719466b3540SConrad Meyer 	if (index >= ioat_channel_index)
720e974f91cSConrad Meyer 		return (NULL);
7215f77bd3eSConrad Meyer 
7225f77bd3eSConrad Meyer 	sc = ioat_channel[index];
7235f77bd3eSConrad Meyer 	if (sc == NULL || sc->quiescing)
7245f77bd3eSConrad Meyer 		return (NULL);
7255f77bd3eSConrad Meyer 
7265f77bd3eSConrad Meyer 	return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine);
727466b3540SConrad Meyer }
728466b3540SConrad Meyer 
729466b3540SConrad Meyer void
730466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
731466b3540SConrad Meyer {
732466b3540SConrad Meyer 	struct ioat_softc *ioat;
733466b3540SConrad Meyer 
734466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
735466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
736e974f91cSConrad Meyer }
737e974f91cSConrad Meyer 
7385ca9fc2aSConrad Meyer int
73931bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine)
74031bf2875SConrad Meyer {
74131bf2875SConrad Meyer 	struct ioat_softc *ioat;
74231bf2875SConrad Meyer 
74331bf2875SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
74431bf2875SConrad Meyer 	return (ioat->version);
74531bf2875SConrad Meyer }
74631bf2875SConrad Meyer 
747bd81fe68SConrad Meyer size_t
748bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine)
749bd81fe68SConrad Meyer {
750bd81fe68SConrad Meyer 	struct ioat_softc *ioat;
751bd81fe68SConrad Meyer 
752bd81fe68SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
753bd81fe68SConrad Meyer 	return (ioat->max_xfer_size);
754bd81fe68SConrad Meyer }
755bd81fe68SConrad Meyer 
75631bf2875SConrad Meyer int
7575ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay)
7585ca9fc2aSConrad Meyer {
7595ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
7605ca9fc2aSConrad Meyer 
7615ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7625ca9fc2aSConrad Meyer 	if (!ioat->intrdelay_supported)
7635ca9fc2aSConrad Meyer 		return (ENODEV);
7645ca9fc2aSConrad Meyer 	if (delay > ioat->intrdelay_max)
7655ca9fc2aSConrad Meyer 		return (ERANGE);
7665ca9fc2aSConrad Meyer 
7675ca9fc2aSConrad Meyer 	ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay);
7685ca9fc2aSConrad Meyer 	ioat->cached_intrdelay =
7695ca9fc2aSConrad Meyer 	    ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK;
7705ca9fc2aSConrad Meyer 	return (0);
7715ca9fc2aSConrad Meyer }
7725ca9fc2aSConrad Meyer 
7735ca9fc2aSConrad Meyer uint16_t
7745ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine)
7755ca9fc2aSConrad Meyer {
7765ca9fc2aSConrad Meyer 	struct ioat_softc *ioat;
7775ca9fc2aSConrad Meyer 
7785ca9fc2aSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7795ca9fc2aSConrad Meyer 	return (ioat->intrdelay_max);
7805ca9fc2aSConrad Meyer }
7815ca9fc2aSConrad Meyer 
782e974f91cSConrad Meyer void
783e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
784e974f91cSConrad Meyer {
785e974f91cSConrad Meyer 	struct ioat_softc *ioat;
786e974f91cSConrad Meyer 
787e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
788e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
78943fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
790e974f91cSConrad Meyer }
791e974f91cSConrad Meyer 
792*1502e363SConrad Meyer int
793*1502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags)
794*1502e363SConrad Meyer {
795*1502e363SConrad Meyer 	struct ioat_softc *ioat;
796*1502e363SConrad Meyer 	int error;
797*1502e363SConrad Meyer 
798*1502e363SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
799*1502e363SConrad Meyer 	ioat_acquire(dmaengine);
800*1502e363SConrad Meyer 
801*1502e363SConrad Meyer 	error = ioat_reserve_space(ioat, n, mflags);
802*1502e363SConrad Meyer 	if (error != 0)
803*1502e363SConrad Meyer 		ioat_release(dmaengine);
804*1502e363SConrad Meyer 	return (error);
805*1502e363SConrad Meyer }
806*1502e363SConrad Meyer 
807e974f91cSConrad Meyer void
808e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
809e974f91cSConrad Meyer {
810e974f91cSConrad Meyer 	struct ioat_softc *ioat;
811e974f91cSConrad Meyer 
812e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
81343fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
814bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
815e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
816e974f91cSConrad Meyer }
817e974f91cSConrad Meyer 
8189e3bbf26SConrad Meyer static struct ioat_descriptor *
8199e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
8209e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
8219e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
8229e3bbf26SConrad Meyer     uint32_t flags)
823e974f91cSConrad Meyer {
8249e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
825e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
826bf8553eaSConrad Meyer 	int mflags;
827e974f91cSConrad Meyer 
8289e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
8299e3bbf26SConrad Meyer 
830e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
831e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
832bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
833bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
834bf8553eaSConrad Meyer 	else
835bf8553eaSConrad Meyer 		mflags = M_WAITOK;
836e974f91cSConrad Meyer 
8379e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
8389e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
8399e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
8409e3bbf26SConrad Meyer 		return (NULL);
8419e3bbf26SConrad Meyer 	}
842e974f91cSConrad Meyer 
843bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
844e974f91cSConrad Meyer 		return (NULL);
845e974f91cSConrad Meyer 
846e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
8479e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
848e974f91cSConrad Meyer 
849e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
8509e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
8519e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
852e974f91cSConrad Meyer 
853e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
8549e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
855e974f91cSConrad Meyer 
8569e3bbf26SConrad Meyer 	hw_desc->size = size;
8579e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
8589e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
859e974f91cSConrad Meyer 
860e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
861e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
8629e3bbf26SConrad Meyer 	return (desc);
8639e3bbf26SConrad Meyer }
864e974f91cSConrad Meyer 
8659e3bbf26SConrad Meyer struct bus_dmadesc *
8669e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
8679e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
8689e3bbf26SConrad Meyer {
8699e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8709e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
8719e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
8729e3bbf26SConrad Meyer 
8739e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8749e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8759e3bbf26SConrad Meyer 
8769e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
8779e3bbf26SConrad Meyer 	    callback_arg, flags);
8789e3bbf26SConrad Meyer 	if (desc == NULL)
8799e3bbf26SConrad Meyer 		return (NULL);
8809e3bbf26SConrad Meyer 
8819e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
8829e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
883e974f91cSConrad Meyer 	ioat_submit_single(ioat);
884e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
885e974f91cSConrad Meyer }
886e974f91cSConrad Meyer 
887e974f91cSConrad Meyer struct bus_dmadesc *
888e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
889e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
890e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
891e974f91cSConrad Meyer {
892e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8939e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
894e974f91cSConrad Meyer 	struct ioat_softc *ioat;
895e974f91cSConrad Meyer 
8969e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
897e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
898e974f91cSConrad Meyer 
8999e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
9009e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9019e3bbf26SConrad Meyer 		    __func__);
902e974f91cSConrad Meyer 		return (NULL);
903e974f91cSConrad Meyer 	}
904e974f91cSConrad Meyer 
9059e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
9069e3bbf26SConrad Meyer 	    callback_arg, flags);
9079e3bbf26SConrad Meyer 	if (desc == NULL)
908e974f91cSConrad Meyer 		return (NULL);
909e974f91cSConrad Meyer 
910e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
911e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
912e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
913e974f91cSConrad Meyer 
914e974f91cSConrad Meyer 	ioat_submit_single(ioat);
915e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
916e974f91cSConrad Meyer }
917e974f91cSConrad Meyer 
9182a4fd6b1SConrad Meyer struct bus_dmadesc *
9199950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1,
9209950fde0SConrad Meyer     bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2,
9219950fde0SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags)
9229950fde0SConrad Meyer {
9239950fde0SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
9249950fde0SConrad Meyer 	struct ioat_descriptor *desc;
9259950fde0SConrad Meyer 	struct ioat_softc *ioat;
9269950fde0SConrad Meyer 
9279950fde0SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9289950fde0SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9299950fde0SConrad Meyer 
9309950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) {
9319950fde0SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
9329950fde0SConrad Meyer 		    __func__);
9339950fde0SConrad Meyer 		return (NULL);
9349950fde0SConrad Meyer 	}
9359950fde0SConrad Meyer 	if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) {
9369950fde0SConrad Meyer 		ioat_log_message(0, "%s: Addresses must be page-aligned\n",
9379950fde0SConrad Meyer 		    __func__);
9389950fde0SConrad Meyer 		return (NULL);
9399950fde0SConrad Meyer 	}
9409950fde0SConrad Meyer 
9419950fde0SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1,
9429950fde0SConrad Meyer 	    callback_fn, callback_arg, flags);
9439950fde0SConrad Meyer 	if (desc == NULL)
9449950fde0SConrad Meyer 		return (NULL);
9459950fde0SConrad Meyer 
9469950fde0SConrad Meyer 	hw_desc = desc->u.dma;
9479950fde0SConrad Meyer 	if (src2 != src1 + PAGE_SIZE) {
9489950fde0SConrad Meyer 		hw_desc->u.control.src_page_break = 1;
9499950fde0SConrad Meyer 		hw_desc->next_src_addr = src2;
9509950fde0SConrad Meyer 	}
9519950fde0SConrad Meyer 	if (dst2 != dst1 + PAGE_SIZE) {
9529950fde0SConrad Meyer 		hw_desc->u.control.dest_page_break = 1;
9539950fde0SConrad Meyer 		hw_desc->next_dest_addr = dst2;
9549950fde0SConrad Meyer 	}
9559950fde0SConrad Meyer 
9569950fde0SConrad Meyer 	if (g_ioat_debug_level >= 3)
9579950fde0SConrad Meyer 		dump_descriptor(hw_desc);
9589950fde0SConrad Meyer 
9599950fde0SConrad Meyer 	ioat_submit_single(ioat);
9609950fde0SConrad Meyer 	return (&desc->bus_dmadesc);
9619950fde0SConrad Meyer }
9629950fde0SConrad Meyer 
9639950fde0SConrad Meyer struct bus_dmadesc *
9642a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
9652a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
9662a4fd6b1SConrad Meyer     uint32_t flags)
9672a4fd6b1SConrad Meyer {
9682a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
9692a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
9702a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
9712a4fd6b1SConrad Meyer 
9722a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
9732a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
9742a4fd6b1SConrad Meyer 
9751693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
9761693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
9771693d27bSConrad Meyer 		    __func__);
9781693d27bSConrad Meyer 		return (NULL);
9791693d27bSConrad Meyer 	}
9801693d27bSConrad Meyer 
9812a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
9822a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
9832a4fd6b1SConrad Meyer 		    __func__);
9842a4fd6b1SConrad Meyer 		return (NULL);
9852a4fd6b1SConrad Meyer 	}
9862a4fd6b1SConrad Meyer 
9872a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
9882a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
9892a4fd6b1SConrad Meyer 	if (desc == NULL)
9902a4fd6b1SConrad Meyer 		return (NULL);
9912a4fd6b1SConrad Meyer 
9922a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
9932a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
9942a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
9952a4fd6b1SConrad Meyer 
9962a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
9972a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
9982a4fd6b1SConrad Meyer }
9992a4fd6b1SConrad Meyer 
1000e974f91cSConrad Meyer /*
1001e974f91cSConrad Meyer  * Ring Management
1002e974f91cSConrad Meyer  */
1003e974f91cSConrad Meyer static inline uint32_t
1004e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
1005e974f91cSConrad Meyer {
1006e974f91cSConrad Meyer 
1007e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
1008e974f91cSConrad Meyer }
1009e974f91cSConrad Meyer 
1010e974f91cSConrad Meyer static inline uint32_t
1011e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
1012e974f91cSConrad Meyer {
1013e974f91cSConrad Meyer 
1014e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
1015e974f91cSConrad Meyer }
1016e974f91cSConrad Meyer 
1017e974f91cSConrad Meyer static struct ioat_descriptor *
1018bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
1019e974f91cSConrad Meyer {
10209e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
1021e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
1022bf8553eaSConrad Meyer 	int error, busdmaflag;
1023e974f91cSConrad Meyer 
1024f46011aeSConrad Meyer 	error = ENOMEM;
1025f46011aeSConrad Meyer 	hw_desc = NULL;
1026f46011aeSConrad Meyer 
1027bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
1028bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
1029bf8553eaSConrad Meyer 	else
1030bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
1031bf8553eaSConrad Meyer 
1032bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
1033e974f91cSConrad Meyer 	if (desc == NULL)
1034f46011aeSConrad Meyer 		goto out;
1035e974f91cSConrad Meyer 
1036f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
1037bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
1038f46011aeSConrad Meyer 	if (hw_desc == NULL)
1039f46011aeSConrad Meyer 		goto out;
1040e974f91cSConrad Meyer 
1041faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
10429e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
1043f46011aeSConrad Meyer 
1044f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
1045f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
1046bf8553eaSConrad Meyer 	    busdmaflag);
1047f46011aeSConrad Meyer 	if (error)
1048f46011aeSConrad Meyer 		goto out;
1049f46011aeSConrad Meyer 
1050f46011aeSConrad Meyer out:
1051f46011aeSConrad Meyer 	if (error) {
1052f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
1053f46011aeSConrad Meyer 		return (NULL);
1054f46011aeSConrad Meyer 	}
1055e974f91cSConrad Meyer 	return (desc);
1056e974f91cSConrad Meyer }
1057e974f91cSConrad Meyer 
1058e974f91cSConrad Meyer static void
1059e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
1060e974f91cSConrad Meyer {
1061e974f91cSConrad Meyer 
1062e974f91cSConrad Meyer 	if (desc == NULL)
1063e974f91cSConrad Meyer 		return;
1064e974f91cSConrad Meyer 
10659e3bbf26SConrad Meyer 	if (desc->u.generic)
10669e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
1067e974f91cSConrad Meyer 		    ioat->hw_desc_map);
1068e974f91cSConrad Meyer 	free(desc, M_IOAT);
1069e974f91cSConrad Meyer }
1070e974f91cSConrad Meyer 
1071bf8553eaSConrad Meyer /*
1072bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
1073bf8553eaSConrad Meyer  * for 'num_descs'.
1074bf8553eaSConrad Meyer  *
1075bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
1076bf8553eaSConrad Meyer  *
1077bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
1078bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
1079bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
1080bf8553eaSConrad Meyer  *
1081bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
1082bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
1083bf8553eaSConrad Meyer  *
1084bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
1085bf8553eaSConrad Meyer  * assured enough room is available.)
1086bf8553eaSConrad Meyer  */
1087e974f91cSConrad Meyer static int
1088bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
1089e974f91cSConrad Meyer {
1090bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
1091bf8553eaSConrad Meyer 	uint32_t order;
1092bf8553eaSConrad Meyer 	int error;
1093e974f91cSConrad Meyer 
1094bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1095bf8553eaSConrad Meyer 	error = 0;
1096e974f91cSConrad Meyer 
1097bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
1098bf8553eaSConrad Meyer 		error = EINVAL;
1099bf8553eaSConrad Meyer 		goto out;
1100e974f91cSConrad Meyer 	}
11015f77bd3eSConrad Meyer 	if (ioat->quiescing) {
11025f77bd3eSConrad Meyer 		error = ENXIO;
11035f77bd3eSConrad Meyer 		goto out;
11045f77bd3eSConrad Meyer 	}
1105bf8553eaSConrad Meyer 
1106bf8553eaSConrad Meyer 	for (;;) {
1107bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
1108bf8553eaSConrad Meyer 			goto out;
1109bf8553eaSConrad Meyer 
1110bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
1111bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
1112bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
1113bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
1114bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
1115bf8553eaSConrad Meyer 				continue;
1116bf8553eaSConrad Meyer 			}
1117bf8553eaSConrad Meyer 
1118bf8553eaSConrad Meyer 			error = EAGAIN;
1119bf8553eaSConrad Meyer 			break;
1120bf8553eaSConrad Meyer 		}
1121bf8553eaSConrad Meyer 
1122bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
1123bf8553eaSConrad Meyer 		for (;;) {
1124bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
1125bf8553eaSConrad Meyer 
1126bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
1127bf8553eaSConrad Meyer 			    TRUE, mflags);
1128bf8553eaSConrad Meyer 
1129bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1130bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1131bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1132bf8553eaSConrad Meyer 
1133bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1134bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1135bf8553eaSConrad Meyer 				    ("allocation failed"));
1136bf8553eaSConrad Meyer 				error = EAGAIN;
1137bf8553eaSConrad Meyer 				break;
1138bf8553eaSConrad Meyer 			}
1139bf8553eaSConrad Meyer 
1140bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1141bf8553eaSConrad Meyer 			if (error == 0)
1142bf8553eaSConrad Meyer 				break;
1143bf8553eaSConrad Meyer 		}
1144bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1145bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1146bf8553eaSConrad Meyer 		if (error)
1147bf8553eaSConrad Meyer 			break;
1148bf8553eaSConrad Meyer 	}
1149bf8553eaSConrad Meyer 
1150bf8553eaSConrad Meyer out:
1151bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1152bf8553eaSConrad Meyer 	return (error);
1153bf8553eaSConrad Meyer }
1154bf8553eaSConrad Meyer 
1155bf8553eaSConrad Meyer static struct ioat_descriptor **
1156bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1157bf8553eaSConrad Meyer     int mflags)
1158bf8553eaSConrad Meyer {
1159bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1160bf8553eaSConrad Meyer 	uint32_t i;
1161bf8553eaSConrad Meyer 	int error;
1162bf8553eaSConrad Meyer 
1163bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1164bf8553eaSConrad Meyer 
1165bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1166bf8553eaSConrad Meyer 	if (ring == NULL)
1167bf8553eaSConrad Meyer 		return (NULL);
1168bf8553eaSConrad Meyer 
1169bf8553eaSConrad Meyer 	if (need_dscr) {
1170bf8553eaSConrad Meyer 		error = ENOMEM;
1171bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1172bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1173bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1174bf8553eaSConrad Meyer 				goto out;
1175bf8553eaSConrad Meyer 			ring[i]->id = i;
1176bf8553eaSConrad Meyer 		}
1177bf8553eaSConrad Meyer 	}
1178bf8553eaSConrad Meyer 	error = 0;
1179bf8553eaSConrad Meyer 
1180bf8553eaSConrad Meyer out:
1181bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1182bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1183bf8553eaSConrad Meyer 		ring = NULL;
1184bf8553eaSConrad Meyer 	}
1185bf8553eaSConrad Meyer 	return (ring);
1186bf8553eaSConrad Meyer }
1187bf8553eaSConrad Meyer 
1188bf8553eaSConrad Meyer static void
1189bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1190bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1191bf8553eaSConrad Meyer {
1192bf8553eaSConrad Meyer 	uint32_t i;
1193bf8553eaSConrad Meyer 
1194bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1195bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1196bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1197bf8553eaSConrad Meyer 	}
1198bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1199e974f91cSConrad Meyer }
1200e974f91cSConrad Meyer 
1201e974f91cSConrad Meyer static struct ioat_descriptor *
1202e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1203e974f91cSConrad Meyer {
1204e974f91cSConrad Meyer 
1205e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1206e974f91cSConrad Meyer }
1207e974f91cSConrad Meyer 
1208bf8553eaSConrad Meyer static int
1209bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1210bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1211e974f91cSConrad Meyer {
1212bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1213e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1214bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1215bf8553eaSConrad Meyer 	int error;
1216e974f91cSConrad Meyer 
1217bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1218e974f91cSConrad Meyer 
1219bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1220bf8553eaSConrad Meyer 
1221bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1222bf8553eaSConrad Meyer 		error = EINVAL;
1223bf8553eaSConrad Meyer 		goto out;
1224bf8553eaSConrad Meyer 	}
1225bf8553eaSConrad Meyer 
1226bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1227bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1228bf8553eaSConrad Meyer 
1229bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1230bf8553eaSConrad Meyer 
1231bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1232bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1233bf8553eaSConrad Meyer 
1234bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1235bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1236bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1237e974f91cSConrad Meyer 
1238e974f91cSConrad Meyer 	/*
1239bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1240bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1241e974f91cSConrad Meyer 	 */
1242bf8553eaSConrad Meyer 	if (head < tail) {
1243bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1244bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1245e974f91cSConrad Meyer 
1246bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1247bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1248e974f91cSConrad Meyer 
1249bf8553eaSConrad Meyer 			newring[i] = tmp;
1250bf8553eaSConrad Meyer 			newring[i]->id = i;
1251bf8553eaSConrad Meyer 		}
1252bf8553eaSConrad Meyer 		head += oldsize;
1253e974f91cSConrad Meyer 	}
1254e974f91cSConrad Meyer 
1255bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1256e974f91cSConrad Meyer 
1257bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1258bf8553eaSConrad Meyer 	if (head < oldsize) {
1259bf8553eaSConrad Meyer 		i = oldsize - 1;
1260bf8553eaSConrad Meyer 		end = newsize;
1261e974f91cSConrad Meyer 	} else {
1262bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1263bf8553eaSConrad Meyer 		i = head;
1264bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1265bf8553eaSConrad Meyer 	}
1266bf8553eaSConrad Meyer 
1267e974f91cSConrad Meyer 	/*
1268bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1269bf8553eaSConrad Meyer 	 * section (tail -> head).
1270e974f91cSConrad Meyer 	 */
1271bf8553eaSConrad Meyer 	for (; i < end; i++) {
1272bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1273bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1274e974f91cSConrad Meyer 
1275bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1276bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1277e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1278e974f91cSConrad Meyer 	}
1279e974f91cSConrad Meyer 
1280e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1281bf8553eaSConrad Meyer 	ioat->ring = newring;
1282bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1283bf8553eaSConrad Meyer 	ioat->tail = tail;
1284bf8553eaSConrad Meyer 	ioat->head = head;
1285bf8553eaSConrad Meyer 	error = 0;
1286e974f91cSConrad Meyer 
1287bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1288bf8553eaSConrad Meyer out:
1289bf8553eaSConrad Meyer 	if (error)
1290bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1291bf8553eaSConrad Meyer 	return (error);
1292bf8553eaSConrad Meyer }
1293bf8553eaSConrad Meyer 
1294bf8553eaSConrad Meyer static int
1295bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1296bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1297bf8553eaSConrad Meyer {
1298bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1299bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1300bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1301bf8553eaSConrad Meyer 	int error;
1302bf8553eaSConrad Meyer 
1303bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1304bf8553eaSConrad Meyer 
1305bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1306bf8553eaSConrad Meyer 
1307bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1308bf8553eaSConrad Meyer 		error = EINVAL;
1309bf8553eaSConrad Meyer 		goto out_unlocked;
1310bf8553eaSConrad Meyer 	}
1311bf8553eaSConrad Meyer 
1312bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1313bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1314bf8553eaSConrad Meyer 
1315bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1316bf8553eaSConrad Meyer 
1317bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1318bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1319bf8553eaSConrad Meyer 		error = ENOMEM;
1320bf8553eaSConrad Meyer 		goto out;
1321bf8553eaSConrad Meyer 	}
1322bf8553eaSConrad Meyer 
1323bf8553eaSConrad Meyer 	/*
1324bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1325bf8553eaSConrad Meyer 	 * descriptors.
1326bf8553eaSConrad Meyer 	 */
1327bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1328bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1329bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1330bf8553eaSConrad Meyer 
1331bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1332bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1333bf8553eaSConrad Meyer 	}
1334bf8553eaSConrad Meyer 
1335bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1336bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1337bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1338bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1339bf8553eaSConrad Meyer 	}
1340bf8553eaSConrad Meyer 
1341bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1342bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1343bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1344bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1345bf8553eaSConrad Meyer 
1346bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1347bf8553eaSConrad Meyer 	ioat->ring = newring;
1348bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1349bf8553eaSConrad Meyer 	error = 0;
1350bf8553eaSConrad Meyer 
1351bf8553eaSConrad Meyer out:
1352bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1353bf8553eaSConrad Meyer out_unlocked:
1354bf8553eaSConrad Meyer 	if (error)
1355bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1356bf8553eaSConrad Meyer 	return (error);
1357e974f91cSConrad Meyer }
1358e974f91cSConrad Meyer 
1359e974f91cSConrad Meyer static void
13608f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1361e974f91cSConrad Meyer {
1362e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
13638f274637SConrad Meyer 
136459acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
136559acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
13668f274637SConrad Meyer 	if (chanerr == 0)
13678f274637SConrad Meyer 		return;
13688f274637SConrad Meyer 
1369faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1370faefad9cSConrad Meyer 
13718f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
13728f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13738f274637SConrad Meyer 
13748f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
13758f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
13768f274637SConrad Meyer }
13778f274637SConrad Meyer 
13788f274637SConrad Meyer static void
13798f274637SConrad Meyer ioat_timer_callback(void *arg)
13808f274637SConrad Meyer {
1381bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1382e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1383faefad9cSConrad Meyer 	uint32_t order;
1384e974f91cSConrad Meyer 
1385e974f91cSConrad Meyer 	ioat = arg;
1386fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1387e974f91cSConrad Meyer 
1388e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1389e974f91cSConrad Meyer 		ioat_process_events(ioat);
1390faefad9cSConrad Meyer 		return;
1391faefad9cSConrad Meyer 	}
1392faefad9cSConrad Meyer 
1393faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1394e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1395bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1396bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1397bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1398bf8553eaSConrad Meyer 		goto out;
1399bf8553eaSConrad Meyer 	}
1400bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1401e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1402e974f91cSConrad Meyer 
1403bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1404bf8553eaSConrad Meyer 	    M_NOWAIT);
1405bf8553eaSConrad Meyer 
1406bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1407bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1408bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1409bf8553eaSConrad Meyer 
1410bf8553eaSConrad Meyer 	if (newring != NULL)
1411bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1412bf8553eaSConrad Meyer 
1413bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1414bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1415bf8553eaSConrad Meyer 
1416bf8553eaSConrad Meyer out:
1417e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1418bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1419e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1420e974f91cSConrad Meyer }
1421e974f91cSConrad Meyer 
1422e974f91cSConrad Meyer /*
1423e974f91cSConrad Meyer  * Support Functions
1424e974f91cSConrad Meyer  */
1425e974f91cSConrad Meyer static void
1426e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1427e974f91cSConrad Meyer {
1428e974f91cSConrad Meyer 
1429466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1430e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1431bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1432e974f91cSConrad Meyer 
1433e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1434e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1435fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1436fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1437e974f91cSConrad Meyer 	}
143801fbbc88SConrad Meyer 
143901fbbc88SConrad Meyer 	ioat->stats.descriptors_submitted++;
1440e974f91cSConrad Meyer }
1441e974f91cSConrad Meyer 
1442e974f91cSConrad Meyer static int
1443e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1444e974f91cSConrad Meyer {
1445e974f91cSConrad Meyer 	uint64_t status;
1446e974f91cSConrad Meyer 	uint32_t chanerr;
1447cea5b880SConrad Meyer 	unsigned timeout;
14485f77bd3eSConrad Meyer 	int error;
14495f77bd3eSConrad Meyer 
14505f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
14515f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
14525f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
14535f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1454e974f91cSConrad Meyer 
1455e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1456e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1457e974f91cSConrad Meyer 		ioat_suspend(ioat);
1458e974f91cSConrad Meyer 
1459e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1460e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1461e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1462e974f91cSConrad Meyer 		DELAY(1000);
1463e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1464e974f91cSConrad Meyer 	}
14655f77bd3eSConrad Meyer 	if (timeout == 20) {
14665f77bd3eSConrad Meyer 		error = ETIMEDOUT;
14675f77bd3eSConrad Meyer 		goto out;
14685f77bd3eSConrad Meyer 	}
1469e974f91cSConrad Meyer 
1470cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1471cea5b880SConrad Meyer 
1472e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1473e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1474e974f91cSConrad Meyer 
1475e974f91cSConrad Meyer 	/*
1476e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1477e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1478e974f91cSConrad Meyer 	 */
1479e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1480e974f91cSConrad Meyer 	    4);
1481e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1482e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1483e974f91cSConrad Meyer 
14840d1a05d9SConrad Meyer 	/*
14850d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
14860d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
14870d1a05d9SConrad Meyer 	 */
1488f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1489f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
14900d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1491f7157235SConrad Meyer 	}
14920d1a05d9SConrad Meyer 
1493e974f91cSConrad Meyer 	ioat_reset(ioat);
1494e974f91cSConrad Meyer 
1495e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1496e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1497e974f91cSConrad Meyer 		DELAY(1000);
14985f77bd3eSConrad Meyer 	if (timeout == 20) {
14995f77bd3eSConrad Meyer 		error = ETIMEDOUT;
15005f77bd3eSConrad Meyer 		goto out;
15015f77bd3eSConrad Meyer 	}
1502e974f91cSConrad Meyer 
1503f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1504f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
15050d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1506f7157235SConrad Meyer 	}
15074253ea50SConrad Meyer 
1508cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1509cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1510cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1511cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1512cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1513cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
15145f77bd3eSConrad Meyer 		error = 0;
15155f77bd3eSConrad Meyer 		goto out;
1516e974f91cSConrad Meyer 	}
1517e974f91cSConrad Meyer 
1518cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
15195f77bd3eSConrad Meyer 	if (chanerr != 0) {
1520faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1521faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1522faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
15235f77bd3eSConrad Meyer 		error = EIO;
15245f77bd3eSConrad Meyer 		goto out;
15255f77bd3eSConrad Meyer 	}
1526cea5b880SConrad Meyer 
1527cea5b880SConrad Meyer 	/*
1528cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1529cea5b880SConrad Meyer 	 * device back to active.
1530cea5b880SConrad Meyer 	 *
1531cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1532cea5b880SConrad Meyer 	 * at zero as well.
1533cea5b880SConrad Meyer 	 */
1534bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1535cea5b880SConrad Meyer 	ioat->last_seen = 0;
1536cea5b880SConrad Meyer 
1537cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1538cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1539cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
15405f77bd3eSConrad Meyer 	error = 0;
15415f77bd3eSConrad Meyer 
15425f77bd3eSConrad Meyer out:
15435f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
15445f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
15455f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
15465f77bd3eSConrad Meyer 
15475f77bd3eSConrad Meyer 	if (error == 0)
15485f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
15495f77bd3eSConrad Meyer 
15505f77bd3eSConrad Meyer 	return (error);
1551cea5b880SConrad Meyer }
1552cea5b880SConrad Meyer 
1553f7157235SConrad Meyer static int
1554faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1555faefad9cSConrad Meyer {
1556faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1557faefad9cSConrad Meyer 	struct sbuf sb;
1558faefad9cSConrad Meyer 	uint64_t status;
1559faefad9cSConrad Meyer 	int error;
1560faefad9cSConrad Meyer 
1561faefad9cSConrad Meyer 	ioat = arg1;
1562faefad9cSConrad Meyer 
1563faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1564faefad9cSConrad Meyer 
1565faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1566faefad9cSConrad Meyer 	switch (status) {
1567faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1568faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1569faefad9cSConrad Meyer 		break;
1570faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1571faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1572faefad9cSConrad Meyer 		break;
1573faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1574faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1575faefad9cSConrad Meyer 		break;
1576faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1577faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1578faefad9cSConrad Meyer 		break;
1579faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1580faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1581faefad9cSConrad Meyer 		break;
1582faefad9cSConrad Meyer 	default:
1583faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1584faefad9cSConrad Meyer 		break;
1585faefad9cSConrad Meyer 	}
1586faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1587faefad9cSConrad Meyer 	sbuf_delete(&sb);
1588faefad9cSConrad Meyer 
1589faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1590faefad9cSConrad Meyer 		return (error);
1591faefad9cSConrad Meyer 	return (EINVAL);
1592faefad9cSConrad Meyer }
1593faefad9cSConrad Meyer 
1594faefad9cSConrad Meyer static int
159501fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS)
159601fbbc88SConrad Meyer {
159701fbbc88SConrad Meyer 	struct ioat_softc *ioat;
159801fbbc88SConrad Meyer 	struct sbuf sb;
159901fbbc88SConrad Meyer #define	PRECISION	"1"
160001fbbc88SConrad Meyer 	const uintmax_t factor = 10;
160101fbbc88SConrad Meyer 	uintmax_t rate;
160201fbbc88SConrad Meyer 	int error;
160301fbbc88SConrad Meyer 
160401fbbc88SConrad Meyer 	ioat = arg1;
160501fbbc88SConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 16, req);
160601fbbc88SConrad Meyer 
160701fbbc88SConrad Meyer 	if (ioat->stats.interrupts == 0) {
160801fbbc88SConrad Meyer 		sbuf_printf(&sb, "NaN");
160901fbbc88SConrad Meyer 		goto out;
161001fbbc88SConrad Meyer 	}
161101fbbc88SConrad Meyer 	rate = ioat->stats.descriptors_processed * factor /
161201fbbc88SConrad Meyer 	    ioat->stats.interrupts;
161301fbbc88SConrad Meyer 	sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor,
161401fbbc88SConrad Meyer 	    rate % factor);
161501fbbc88SConrad Meyer #undef	PRECISION
161601fbbc88SConrad Meyer out:
161701fbbc88SConrad Meyer 	error = sbuf_finish(&sb);
161801fbbc88SConrad Meyer 	sbuf_delete(&sb);
161901fbbc88SConrad Meyer 	if (error != 0 || req->newptr == NULL)
162001fbbc88SConrad Meyer 		return (error);
162101fbbc88SConrad Meyer 	return (EINVAL);
162201fbbc88SConrad Meyer }
162301fbbc88SConrad Meyer 
162401fbbc88SConrad Meyer static int
1625faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1626faefad9cSConrad Meyer {
1627faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1628faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1629faefad9cSConrad Meyer 	int error, arg;
1630faefad9cSConrad Meyer 
1631faefad9cSConrad Meyer 	ioat = arg1;
1632faefad9cSConrad Meyer 
1633faefad9cSConrad Meyer 	arg = 0;
1634faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1635faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1636faefad9cSConrad Meyer 		return (error);
1637faefad9cSConrad Meyer 
1638faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1639faefad9cSConrad Meyer 	if (error != 0)
1640faefad9cSConrad Meyer 		return (error);
1641faefad9cSConrad Meyer 
1642faefad9cSConrad Meyer 	if (arg != 0) {
1643faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1644faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1645faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1646faefad9cSConrad Meyer 		    0);
1647faefad9cSConrad Meyer 		if (desc == NULL)
1648faefad9cSConrad Meyer 			error = ENOMEM;
1649faefad9cSConrad Meyer 		else
1650faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1651faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1652faefad9cSConrad Meyer 	}
1653faefad9cSConrad Meyer 	return (error);
1654faefad9cSConrad Meyer }
1655faefad9cSConrad Meyer 
1656faefad9cSConrad Meyer static int
1657f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1658f7157235SConrad Meyer {
1659f7157235SConrad Meyer 	struct ioat_softc *ioat;
1660f7157235SConrad Meyer 	int error, arg;
1661f7157235SConrad Meyer 
1662f7157235SConrad Meyer 	ioat = arg1;
1663f7157235SConrad Meyer 
1664f7157235SConrad Meyer 	arg = 0;
1665f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1666f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1667f7157235SConrad Meyer 		return (error);
1668f7157235SConrad Meyer 
1669f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1670f7157235SConrad Meyer 	if (error != 0)
1671f7157235SConrad Meyer 		return (error);
1672f7157235SConrad Meyer 
1673f7157235SConrad Meyer 	if (arg != 0)
1674f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1675f7157235SConrad Meyer 
1676f7157235SConrad Meyer 	return (error);
1677f7157235SConrad Meyer }
1678f7157235SConrad Meyer 
1679e974f91cSConrad Meyer static void
1680e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1681e974f91cSConrad Meyer {
1682e974f91cSConrad Meyer 	int i, j;
1683e974f91cSConrad Meyer 
1684e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1685e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1686e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1687e974f91cSConrad Meyer 		printf("\n");
1688e974f91cSConrad Meyer 	}
1689e974f91cSConrad Meyer }
1690e974f91cSConrad Meyer 
1691e974f91cSConrad Meyer static void
1692e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1693e974f91cSConrad Meyer {
169401fbbc88SConrad Meyer 	struct sysctl_oid_list *par, *statpar, *state, *hammer;
1695f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
169601fbbc88SConrad Meyer 	struct sysctl_oid *tree, *tmp;
1697e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1698e974f91cSConrad Meyer 
1699e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1700f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1701f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1702f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1703e974f91cSConrad Meyer 
170465e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
170565e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
170665e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
170765e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
17085ca9fc2aSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD,
17095ca9fc2aSConrad Meyer 	    &ioat->intrdelay_supported, 0, "Is INTRDELAY supported");
17105ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD,
17115ca9fc2aSConrad Meyer 	    &ioat->intrdelay_max, 0,
17125ca9fc2aSConrad Meyer 	    "Maximum configurable INTRDELAY on this channel (microseconds)");
171365e4f8adSConrad Meyer 
171401fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL,
171501fbbc88SConrad Meyer 	    "IOAT channel internal state");
171601fbbc88SConrad Meyer 	state = SYSCTL_CHILDREN(tmp);
171701fbbc88SConrad Meyer 
171801fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1719bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
172001fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head,
172101fbbc88SConrad Meyer 	    0, "SW descriptor head pointer index");
172201fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail,
172301fbbc88SConrad Meyer 	    0, "SW descriptor tail pointer index");
172401fbbc88SConrad Meyer 	SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD,
1725bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1726f7157235SConrad Meyer 
172701fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD,
172865e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
172965e4f8adSConrad Meyer 
173001fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
173165e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
173201fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending",
173301fbbc88SConrad Meyer 	    CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending");
173401fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
173565e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
173601fbbc88SConrad Meyer 	SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD,
173765e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
173865e4f8adSConrad Meyer 
173901fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts",
1740faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1741faefad9cSConrad Meyer 	    "String of the channel status");
174201fbbc88SConrad Meyer 
17435ca9fc2aSConrad Meyer 	SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD,
17445ca9fc2aSConrad Meyer 	    &ioat->cached_intrdelay, 0,
17455ca9fc2aSConrad Meyer 	    "Current INTRDELAY on this channel (cached, microseconds)");
17465ca9fc2aSConrad Meyer 
174701fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL,
174801fbbc88SConrad Meyer 	    "Big hammers (mostly for testing)");
174901fbbc88SConrad Meyer 	hammer = SYSCTL_CHILDREN(tmp);
175001fbbc88SConrad Meyer 
175101fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset",
175201fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
175301fbbc88SConrad Meyer 	    "Set to non-zero to reset the hardware");
175401fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error",
175501fbbc88SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
175601fbbc88SConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
175701fbbc88SConrad Meyer 
175801fbbc88SConrad Meyer 	tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL,
175901fbbc88SConrad Meyer 	    "IOAT channel statistics");
176001fbbc88SConrad Meyer 	statpar = SYSCTL_CHILDREN(tmp);
176101fbbc88SConrad Meyer 
176201fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW,
176301fbbc88SConrad Meyer 	    &ioat->stats.interrupts,
176401fbbc88SConrad Meyer 	    "Number of interrupts processed on this channel");
176501fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW,
176601fbbc88SConrad Meyer 	    &ioat->stats.descriptors_processed,
176701fbbc88SConrad Meyer 	    "Number of descriptors processed on this channel");
176801fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW,
176901fbbc88SConrad Meyer 	    &ioat->stats.descriptors_submitted,
177001fbbc88SConrad Meyer 	    "Number of descriptors submitted to this channel");
177101fbbc88SConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW,
177201fbbc88SConrad Meyer 	    &ioat->stats.descriptors_error,
177301fbbc88SConrad Meyer 	    "Number of descriptors failed by channel errors");
177401fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW,
177501fbbc88SConrad Meyer 	    &ioat->stats.channel_halts, 0,
177601fbbc88SConrad Meyer 	    "Number of times the channel has halted");
177701fbbc88SConrad Meyer 	SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW,
177801fbbc88SConrad Meyer 	    &ioat->stats.last_halt_chanerr, 0,
177901fbbc88SConrad Meyer 	    "The raw CHANERR when the channel was last halted");
178001fbbc88SConrad Meyer 
178101fbbc88SConrad Meyer 	SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt",
178201fbbc88SConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A",
178301fbbc88SConrad Meyer 	    "Descriptors per interrupt");
1784e974f91cSConrad Meyer }
1785466b3540SConrad Meyer 
1786466b3540SConrad Meyer static inline struct ioat_softc *
1787466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1788466b3540SConrad Meyer {
1789466b3540SConrad Meyer 	uint32_t old;
1790466b3540SConrad Meyer 
1791466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1792466b3540SConrad Meyer 
1793466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1794466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1795466b3540SConrad Meyer 
1796466b3540SConrad Meyer #ifdef INVARIANTS
1797466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1798466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1799466b3540SConrad Meyer #endif
1800466b3540SConrad Meyer 
1801466b3540SConrad Meyer 	return (ioat);
1802466b3540SConrad Meyer }
1803466b3540SConrad Meyer 
1804466b3540SConrad Meyer static inline void
1805466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1806466b3540SConrad Meyer {
1807faefad9cSConrad Meyer 
1808faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1809faefad9cSConrad Meyer }
1810faefad9cSConrad Meyer 
1811faefad9cSConrad Meyer static inline void
1812faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1813faefad9cSConrad Meyer {
1814faefad9cSConrad Meyer 
1815faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1816faefad9cSConrad Meyer }
1817faefad9cSConrad Meyer 
1818faefad9cSConrad Meyer static inline void
1819faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1820faefad9cSConrad Meyer     boolean_t locked)
1821faefad9cSConrad Meyer {
1822466b3540SConrad Meyer 	uint32_t old;
1823466b3540SConrad Meyer 
1824466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1825466b3540SConrad Meyer 
1826466b3540SConrad Meyer 	if (n == 0)
1827466b3540SConrad Meyer 		return;
1828466b3540SConrad Meyer 
1829466b3540SConrad Meyer #ifdef INVARIANTS
1830466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1831466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1832466b3540SConrad Meyer #endif
1833466b3540SConrad Meyer 
1834466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1835466b3540SConrad Meyer 	for (;;) {
1836466b3540SConrad Meyer 		old = ioat->refcnt;
1837466b3540SConrad Meyer 		if (old <= n)
1838466b3540SConrad Meyer 			break;
1839466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1840466b3540SConrad Meyer 			return;
1841466b3540SConrad Meyer 	}
1842466b3540SConrad Meyer 
1843faefad9cSConrad Meyer 	if (locked)
1844faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1845faefad9cSConrad Meyer 	else
1846466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1847faefad9cSConrad Meyer 
1848466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1849466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1850466b3540SConrad Meyer 
1851466b3540SConrad Meyer 	if (old == n)
1852466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1853faefad9cSConrad Meyer 	if (!locked)
1854466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1855466b3540SConrad Meyer }
1856466b3540SConrad Meyer 
1857466b3540SConrad Meyer static inline void
1858466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1859466b3540SConrad Meyer {
1860466b3540SConrad Meyer 
1861466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1862466b3540SConrad Meyer }
1863466b3540SConrad Meyer 
1864466b3540SConrad Meyer static void
18655f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1866466b3540SConrad Meyer {
1867466b3540SConrad Meyer 
18685f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1869466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1870466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1871466b3540SConrad Meyer }
1872