xref: /freebsd/sys/dev/ioat/ioat.c (revision faefad9c125a9478dd46ccadd0b20a2c825de803)
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>
41*faefad9cSConrad 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);
69*faefad9cSConrad 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 **);
88*faefad9cSConrad 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);
100*faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t,
101*faefad9cSConrad 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);
104*faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t,
105*faefad9cSConrad 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);
144e974f91cSConrad Meyer 
145e974f91cSConrad Meyer /*
146e974f91cSConrad Meyer  * Private data structures
147e974f91cSConrad Meyer  */
148e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS];
149e974f91cSConrad Meyer static int ioat_channel_index = 0;
150e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0,
151e974f91cSConrad Meyer     "Number of IOAT channels attached");
152e974f91cSConrad Meyer 
153e974f91cSConrad Meyer static struct _pcsid
154e974f91cSConrad Meyer {
155e974f91cSConrad Meyer 	u_int32_t   type;
156e974f91cSConrad Meyer 	const char  *desc;
157e974f91cSConrad Meyer } pci_ids[] = {
158e974f91cSConrad Meyer 	{ 0x34308086, "TBG IOAT Ch0" },
159e974f91cSConrad Meyer 	{ 0x34318086, "TBG IOAT Ch1" },
160e974f91cSConrad Meyer 	{ 0x34328086, "TBG IOAT Ch2" },
161e974f91cSConrad Meyer 	{ 0x34338086, "TBG IOAT Ch3" },
162e974f91cSConrad Meyer 	{ 0x34298086, "TBG IOAT Ch4" },
163e974f91cSConrad Meyer 	{ 0x342a8086, "TBG IOAT Ch5" },
164e974f91cSConrad Meyer 	{ 0x342b8086, "TBG IOAT Ch6" },
165e974f91cSConrad Meyer 	{ 0x342c8086, "TBG IOAT Ch7" },
166e974f91cSConrad Meyer 
167e974f91cSConrad Meyer 	{ 0x37108086, "JSF IOAT Ch0" },
168e974f91cSConrad Meyer 	{ 0x37118086, "JSF IOAT Ch1" },
169e974f91cSConrad Meyer 	{ 0x37128086, "JSF IOAT Ch2" },
170e974f91cSConrad Meyer 	{ 0x37138086, "JSF IOAT Ch3" },
171e974f91cSConrad Meyer 	{ 0x37148086, "JSF IOAT Ch4" },
172e974f91cSConrad Meyer 	{ 0x37158086, "JSF IOAT Ch5" },
173e974f91cSConrad Meyer 	{ 0x37168086, "JSF IOAT Ch6" },
174e974f91cSConrad Meyer 	{ 0x37178086, "JSF IOAT Ch7" },
175e974f91cSConrad Meyer 	{ 0x37188086, "JSF IOAT Ch0 (RAID)" },
176e974f91cSConrad Meyer 	{ 0x37198086, "JSF IOAT Ch1 (RAID)" },
177e974f91cSConrad Meyer 
178e974f91cSConrad Meyer 	{ 0x3c208086, "SNB IOAT Ch0" },
179e974f91cSConrad Meyer 	{ 0x3c218086, "SNB IOAT Ch1" },
180e974f91cSConrad Meyer 	{ 0x3c228086, "SNB IOAT Ch2" },
181e974f91cSConrad Meyer 	{ 0x3c238086, "SNB IOAT Ch3" },
182e974f91cSConrad Meyer 	{ 0x3c248086, "SNB IOAT Ch4" },
183e974f91cSConrad Meyer 	{ 0x3c258086, "SNB IOAT Ch5" },
184e974f91cSConrad Meyer 	{ 0x3c268086, "SNB IOAT Ch6" },
185e974f91cSConrad Meyer 	{ 0x3c278086, "SNB IOAT Ch7" },
186e974f91cSConrad Meyer 	{ 0x3c2e8086, "SNB IOAT Ch0 (RAID)" },
187e974f91cSConrad Meyer 	{ 0x3c2f8086, "SNB IOAT Ch1 (RAID)" },
188e974f91cSConrad Meyer 
189e974f91cSConrad Meyer 	{ 0x0e208086, "IVB IOAT Ch0" },
190e974f91cSConrad Meyer 	{ 0x0e218086, "IVB IOAT Ch1" },
191e974f91cSConrad Meyer 	{ 0x0e228086, "IVB IOAT Ch2" },
192e974f91cSConrad Meyer 	{ 0x0e238086, "IVB IOAT Ch3" },
193e974f91cSConrad Meyer 	{ 0x0e248086, "IVB IOAT Ch4" },
194e974f91cSConrad Meyer 	{ 0x0e258086, "IVB IOAT Ch5" },
195e974f91cSConrad Meyer 	{ 0x0e268086, "IVB IOAT Ch6" },
196e974f91cSConrad Meyer 	{ 0x0e278086, "IVB IOAT Ch7" },
197e974f91cSConrad Meyer 	{ 0x0e2e8086, "IVB IOAT Ch0 (RAID)" },
198e974f91cSConrad Meyer 	{ 0x0e2f8086, "IVB IOAT Ch1 (RAID)" },
199e974f91cSConrad Meyer 
200e974f91cSConrad Meyer 	{ 0x2f208086, "HSW IOAT Ch0" },
201e974f91cSConrad Meyer 	{ 0x2f218086, "HSW IOAT Ch1" },
202e974f91cSConrad Meyer 	{ 0x2f228086, "HSW IOAT Ch2" },
203e974f91cSConrad Meyer 	{ 0x2f238086, "HSW IOAT Ch3" },
204e974f91cSConrad Meyer 	{ 0x2f248086, "HSW IOAT Ch4" },
205e974f91cSConrad Meyer 	{ 0x2f258086, "HSW IOAT Ch5" },
206e974f91cSConrad Meyer 	{ 0x2f268086, "HSW IOAT Ch6" },
207e974f91cSConrad Meyer 	{ 0x2f278086, "HSW IOAT Ch7" },
208e974f91cSConrad Meyer 	{ 0x2f2e8086, "HSW IOAT Ch0 (RAID)" },
209e974f91cSConrad Meyer 	{ 0x2f2f8086, "HSW IOAT Ch1 (RAID)" },
210e974f91cSConrad Meyer 
211e974f91cSConrad Meyer 	{ 0x0c508086, "BWD IOAT Ch0" },
212e974f91cSConrad Meyer 	{ 0x0c518086, "BWD IOAT Ch1" },
213e974f91cSConrad Meyer 	{ 0x0c528086, "BWD IOAT Ch2" },
214e974f91cSConrad Meyer 	{ 0x0c538086, "BWD IOAT Ch3" },
215e974f91cSConrad Meyer 
216e974f91cSConrad Meyer 	{ 0x6f508086, "BDXDE IOAT Ch0" },
217e974f91cSConrad Meyer 	{ 0x6f518086, "BDXDE IOAT Ch1" },
218e974f91cSConrad Meyer 	{ 0x6f528086, "BDXDE IOAT Ch2" },
219e974f91cSConrad Meyer 	{ 0x6f538086, "BDXDE IOAT Ch3" },
220e974f91cSConrad Meyer 
221e974f91cSConrad Meyer 	{ 0x00000000, NULL           }
222e974f91cSConrad Meyer };
223e974f91cSConrad Meyer 
224e974f91cSConrad Meyer /*
225e974f91cSConrad Meyer  * OS <-> Driver linkage functions
226e974f91cSConrad Meyer  */
227e974f91cSConrad Meyer static int
228e974f91cSConrad Meyer ioat_probe(device_t device)
229e974f91cSConrad Meyer {
230e974f91cSConrad Meyer 	struct _pcsid *ep;
231e974f91cSConrad Meyer 	u_int32_t type;
232e974f91cSConrad Meyer 
233e974f91cSConrad Meyer 	type = pci_get_devid(device);
234e974f91cSConrad Meyer 	for (ep = pci_ids; ep->type; ep++) {
235e974f91cSConrad Meyer 		if (ep->type == type) {
236e974f91cSConrad Meyer 			device_set_desc(device, ep->desc);
237e974f91cSConrad Meyer 			return (0);
238e974f91cSConrad Meyer 		}
239e974f91cSConrad Meyer 	}
240e974f91cSConrad Meyer 	return (ENXIO);
241e974f91cSConrad Meyer }
242e974f91cSConrad Meyer 
243e974f91cSConrad Meyer static int
244e974f91cSConrad Meyer ioat_attach(device_t device)
245e974f91cSConrad Meyer {
246e974f91cSConrad Meyer 	struct ioat_softc *ioat;
247e974f91cSConrad Meyer 	int error;
248e974f91cSConrad Meyer 
249e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
250e974f91cSConrad Meyer 	ioat->device = device;
251e974f91cSConrad Meyer 
252e974f91cSConrad Meyer 	error = ioat_map_pci_bar(ioat);
253e974f91cSConrad Meyer 	if (error != 0)
254e974f91cSConrad Meyer 		goto err;
255e974f91cSConrad Meyer 
256e974f91cSConrad Meyer 	ioat->version = ioat_read_cbver(ioat);
257e974f91cSConrad Meyer 	if (ioat->version < IOAT_VER_3_0) {
258e974f91cSConrad Meyer 		error = ENODEV;
259e974f91cSConrad Meyer 		goto err;
260e974f91cSConrad Meyer 	}
261e974f91cSConrad Meyer 
262e974f91cSConrad Meyer 	error = ioat3_attach(device);
263e974f91cSConrad Meyer 	if (error != 0)
264e974f91cSConrad Meyer 		goto err;
265e974f91cSConrad Meyer 
266e974f91cSConrad Meyer 	error = pci_enable_busmaster(device);
267e974f91cSConrad Meyer 	if (error != 0)
268e974f91cSConrad Meyer 		goto err;
269e974f91cSConrad Meyer 
270466b3540SConrad Meyer 	error = ioat_setup_intr(ioat);
271466b3540SConrad Meyer 	if (error != 0)
272466b3540SConrad Meyer 		goto err;
273466b3540SConrad Meyer 
274cea5b880SConrad Meyer 	error = ioat_reset_hw(ioat);
2757afbb263SConrad Meyer 	if (error != 0)
276466b3540SConrad Meyer 		goto err;
2777afbb263SConrad Meyer 
2787afbb263SConrad Meyer 	ioat_process_events(ioat);
2797afbb263SConrad Meyer 	ioat_setup_sysctl(device);
2807afbb263SConrad Meyer 
2815f77bd3eSConrad Meyer 	ioat->chan_idx = ioat_channel_index;
282e974f91cSConrad Meyer 	ioat_channel[ioat_channel_index++] = ioat;
2837afbb263SConrad Meyer 	ioat_test_attach();
284e974f91cSConrad Meyer 
285e974f91cSConrad Meyer err:
286e974f91cSConrad Meyer 	if (error != 0)
287e974f91cSConrad Meyer 		ioat_detach(device);
288e974f91cSConrad Meyer 	return (error);
289e974f91cSConrad Meyer }
290e974f91cSConrad Meyer 
291e974f91cSConrad Meyer static int
292e974f91cSConrad Meyer ioat_detach(device_t device)
293e974f91cSConrad Meyer {
294e974f91cSConrad Meyer 	struct ioat_softc *ioat;
295e974f91cSConrad Meyer 
296e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
2977afbb263SConrad Meyer 
2987afbb263SConrad Meyer 	ioat_test_detach();
2995f77bd3eSConrad Meyer 
3005f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
3015f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
3025f77bd3eSConrad Meyer 	ioat_channel[ioat->chan_idx] = NULL;
3035f77bd3eSConrad Meyer 
3045f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
3055f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
306fe720f5aSConrad Meyer 
307fe720f5aSConrad Meyer 	ioat_teardown_intr(ioat);
308e974f91cSConrad Meyer 	callout_drain(&ioat->timer);
309e974f91cSConrad Meyer 
310e974f91cSConrad Meyer 	pci_disable_busmaster(device);
311e974f91cSConrad Meyer 
312e974f91cSConrad Meyer 	if (ioat->pci_resource != NULL)
313e974f91cSConrad Meyer 		bus_release_resource(device, SYS_RES_MEMORY,
314e974f91cSConrad Meyer 		    ioat->pci_resource_id, ioat->pci_resource);
315e974f91cSConrad Meyer 
316bf8553eaSConrad Meyer 	if (ioat->ring != NULL)
317bf8553eaSConrad Meyer 		ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring);
318e974f91cSConrad Meyer 
319e974f91cSConrad Meyer 	if (ioat->comp_update != NULL) {
320e974f91cSConrad Meyer 		bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map);
321e974f91cSConrad Meyer 		bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update,
322e974f91cSConrad Meyer 		    ioat->comp_update_map);
323e974f91cSConrad Meyer 		bus_dma_tag_destroy(ioat->comp_update_tag);
324e974f91cSConrad Meyer 	}
325e974f91cSConrad Meyer 
326e974f91cSConrad Meyer 	bus_dma_tag_destroy(ioat->hw_desc_tag);
327e974f91cSConrad Meyer 
3284253ea50SConrad Meyer 	return (0);
3294253ea50SConrad Meyer }
3304253ea50SConrad Meyer 
3314253ea50SConrad Meyer static int
3324253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat)
3334253ea50SConrad Meyer {
3344253ea50SConrad Meyer 
335e974f91cSConrad Meyer 	if (ioat->tag != NULL)
3364253ea50SConrad Meyer 		bus_teardown_intr(ioat->device, ioat->res, ioat->tag);
337e974f91cSConrad Meyer 
338e974f91cSConrad Meyer 	if (ioat->res != NULL)
3394253ea50SConrad Meyer 		bus_release_resource(ioat->device, SYS_RES_IRQ,
340e974f91cSConrad Meyer 		    rman_get_rid(ioat->res), ioat->res);
341e974f91cSConrad Meyer 
3424253ea50SConrad Meyer 	pci_release_msi(ioat->device);
343e974f91cSConrad Meyer 	return (0);
344e974f91cSConrad Meyer }
345e974f91cSConrad Meyer 
346e974f91cSConrad Meyer static int
347cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat)
348e974f91cSConrad Meyer {
349e974f91cSConrad Meyer 	uint64_t status;
350e974f91cSConrad Meyer 	uint32_t chanerr;
351e974f91cSConrad Meyer 	int i;
352e974f91cSConrad Meyer 
353e974f91cSConrad Meyer 	ioat_acquire(&ioat->dmaengine);
354e974f91cSConrad Meyer 	ioat_null(&ioat->dmaengine, NULL, NULL, 0);
355e974f91cSConrad Meyer 	ioat_release(&ioat->dmaengine);
356e974f91cSConrad Meyer 
357e974f91cSConrad Meyer 	for (i = 0; i < 100; i++) {
358e974f91cSConrad Meyer 		DELAY(1);
359e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
360e974f91cSConrad Meyer 		if (is_ioat_idle(status))
361e974f91cSConrad Meyer 			return (0);
362e974f91cSConrad Meyer 	}
363e974f91cSConrad Meyer 
364e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
365e974f91cSConrad Meyer 	ioat_log_message(0, "could not start channel: "
36659acd4baSConrad Meyer 	    "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr,
36759acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
368e974f91cSConrad Meyer 	return (ENXIO);
369e974f91cSConrad Meyer }
370e974f91cSConrad Meyer 
371e974f91cSConrad Meyer /*
372e974f91cSConrad Meyer  * Initialize Hardware
373e974f91cSConrad Meyer  */
374e974f91cSConrad Meyer static int
375e974f91cSConrad Meyer ioat3_attach(device_t device)
376e974f91cSConrad Meyer {
377e974f91cSConrad Meyer 	struct ioat_softc *ioat;
378e974f91cSConrad Meyer 	struct ioat_descriptor **ring;
379e974f91cSConrad Meyer 	struct ioat_descriptor *next;
380e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *dma_hw_desc;
381e974f91cSConrad Meyer 	int i, num_descriptors;
382e974f91cSConrad Meyer 	int error;
383e974f91cSConrad Meyer 	uint8_t xfercap;
384e974f91cSConrad Meyer 
385e974f91cSConrad Meyer 	error = 0;
386e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
3871693d27bSConrad Meyer 	ioat->capabilities = ioat_read_dmacapability(ioat);
3881693d27bSConrad Meyer 
3891693d27bSConrad Meyer 	ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities,
3901693d27bSConrad Meyer 	    IOAT_DMACAP_STR);
391e974f91cSConrad Meyer 
392e974f91cSConrad Meyer 	xfercap = ioat_read_xfercap(ioat);
393e974f91cSConrad Meyer 	ioat->max_xfer_size = 1 << xfercap;
394e974f91cSConrad Meyer 
395e974f91cSConrad Meyer 	/* TODO: need to check DCA here if we ever do XOR/PQ */
396e974f91cSConrad Meyer 
397e974f91cSConrad Meyer 	mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF);
398*faefad9cSConrad Meyer 	mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF);
3997afbb263SConrad Meyer 	callout_init(&ioat->timer, 1);
400e974f91cSConrad Meyer 
401*faefad9cSConrad Meyer 	/* Establish lock order for Witness */
402*faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
403*faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
404*faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
405*faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
406*faefad9cSConrad Meyer 
407e974f91cSConrad Meyer 	ioat->is_resize_pending = FALSE;
408e974f91cSConrad Meyer 	ioat->is_completion_pending = FALSE;
409e974f91cSConrad Meyer 	ioat->is_reset_pending = FALSE;
410e974f91cSConrad Meyer 	ioat->is_channel_running = FALSE;
411e974f91cSConrad Meyer 
412e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0,
413e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
414e974f91cSConrad Meyer 	    sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL,
415e974f91cSConrad Meyer 	    &ioat->comp_update_tag);
416e974f91cSConrad Meyer 
417e974f91cSConrad Meyer 	error = bus_dmamem_alloc(ioat->comp_update_tag,
418e974f91cSConrad Meyer 	    (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map);
419e974f91cSConrad Meyer 	if (ioat->comp_update == NULL)
420e974f91cSConrad Meyer 		return (ENOMEM);
421e974f91cSConrad Meyer 
422e974f91cSConrad Meyer 	error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map,
423e974f91cSConrad Meyer 	    ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat,
424e974f91cSConrad Meyer 	    0);
425e974f91cSConrad Meyer 	if (error != 0)
426e974f91cSConrad Meyer 		return (error);
427e974f91cSConrad Meyer 
428e974f91cSConrad Meyer 	ioat->ring_size_order = IOAT_MIN_ORDER;
429e974f91cSConrad Meyer 
430e974f91cSConrad Meyer 	num_descriptors = 1 << ioat->ring_size_order;
431e974f91cSConrad Meyer 
432e974f91cSConrad Meyer 	bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0,
433e974f91cSConrad Meyer 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
434e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 1,
435e974f91cSConrad Meyer 	    sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL,
436e974f91cSConrad Meyer 	    &ioat->hw_desc_tag);
437e974f91cSConrad Meyer 
438e974f91cSConrad Meyer 	ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT,
439bf8553eaSConrad Meyer 	    M_ZERO | M_WAITOK);
440e974f91cSConrad Meyer 	if (ioat->ring == NULL)
441e974f91cSConrad Meyer 		return (ENOMEM);
442e974f91cSConrad Meyer 
443e974f91cSConrad Meyer 	ring = ioat->ring;
444e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors; i++) {
445bf8553eaSConrad Meyer 		ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK);
446e974f91cSConrad Meyer 		if (ring[i] == NULL)
447e974f91cSConrad Meyer 			return (ENOMEM);
448e974f91cSConrad Meyer 
449e974f91cSConrad Meyer 		ring[i]->id = i;
450e974f91cSConrad Meyer 	}
451e974f91cSConrad Meyer 
452e974f91cSConrad Meyer 	for (i = 0; i < num_descriptors - 1; i++) {
453e974f91cSConrad Meyer 		next = ring[i + 1];
454e974f91cSConrad Meyer 		dma_hw_desc = ring[i]->u.dma;
455e974f91cSConrad Meyer 
456e974f91cSConrad Meyer 		dma_hw_desc->next = next->hw_desc_bus_addr;
457e974f91cSConrad Meyer 	}
458e974f91cSConrad Meyer 
459e974f91cSConrad Meyer 	ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr;
460e974f91cSConrad Meyer 
461bf8553eaSConrad Meyer 	ioat->head = ioat->hw_head = 0;
462e974f91cSConrad Meyer 	ioat->tail = 0;
463e974f91cSConrad Meyer 	ioat->last_seen = 0;
464e974f91cSConrad Meyer 	return (0);
465e974f91cSConrad Meyer }
466e974f91cSConrad Meyer 
467e974f91cSConrad Meyer static int
468e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat)
469e974f91cSConrad Meyer {
470e974f91cSConrad Meyer 
471e974f91cSConrad Meyer 	ioat->pci_resource_id = PCIR_BAR(0);
472e88e14b9SConrad Meyer 	ioat->pci_resource = bus_alloc_resource_any(ioat->device,
473e88e14b9SConrad Meyer 	    SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE);
474e974f91cSConrad Meyer 
475e974f91cSConrad Meyer 	if (ioat->pci_resource == NULL) {
476e974f91cSConrad Meyer 		ioat_log_message(0, "unable to allocate pci resource\n");
477e974f91cSConrad Meyer 		return (ENODEV);
478e974f91cSConrad Meyer 	}
479e974f91cSConrad Meyer 
480e974f91cSConrad Meyer 	ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource);
481e974f91cSConrad Meyer 	ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource);
482e974f91cSConrad Meyer 	return (0);
483e974f91cSConrad Meyer }
484e974f91cSConrad Meyer 
485e974f91cSConrad Meyer static void
486e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error)
487e974f91cSConrad Meyer {
488e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
489e974f91cSConrad Meyer 
490cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
491e974f91cSConrad Meyer 	ioat->comp_update_bus_addr = seg[0].ds_addr;
492e974f91cSConrad Meyer }
493e974f91cSConrad Meyer 
494e974f91cSConrad Meyer static void
495e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
496e974f91cSConrad Meyer {
497e974f91cSConrad Meyer 	bus_addr_t *baddr;
498e974f91cSConrad Meyer 
499cea5b880SConrad Meyer 	KASSERT(error == 0, ("%s: error:%d", __func__, error));
500e974f91cSConrad Meyer 	baddr = arg;
501e974f91cSConrad Meyer 	*baddr = segs->ds_addr;
502e974f91cSConrad Meyer }
503e974f91cSConrad Meyer 
504e974f91cSConrad Meyer /*
505e974f91cSConrad Meyer  * Interrupt setup and handlers
506e974f91cSConrad Meyer  */
507e974f91cSConrad Meyer static int
5084253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat)
509e974f91cSConrad Meyer {
510e974f91cSConrad Meyer 	uint32_t num_vectors;
511e974f91cSConrad Meyer 	int error;
512e974f91cSConrad Meyer 	boolean_t use_msix;
513e974f91cSConrad Meyer 	boolean_t force_legacy_interrupts;
514e974f91cSConrad Meyer 
515e974f91cSConrad Meyer 	use_msix = FALSE;
516e974f91cSConrad Meyer 	force_legacy_interrupts = FALSE;
517e974f91cSConrad Meyer 
518e974f91cSConrad Meyer 	if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) {
519e974f91cSConrad Meyer 		num_vectors = 1;
520e974f91cSConrad Meyer 		pci_alloc_msix(ioat->device, &num_vectors);
521e974f91cSConrad Meyer 		if (num_vectors == 1)
522e974f91cSConrad Meyer 			use_msix = TRUE;
523e974f91cSConrad Meyer 	}
524e974f91cSConrad Meyer 
525e974f91cSConrad Meyer 	if (use_msix) {
526e974f91cSConrad Meyer 		ioat->rid = 1;
527e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
528e974f91cSConrad Meyer 		    &ioat->rid, RF_ACTIVE);
529e974f91cSConrad Meyer 	} else {
530e974f91cSConrad Meyer 		ioat->rid = 0;
531e974f91cSConrad Meyer 		ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ,
532e974f91cSConrad Meyer 		    &ioat->rid, RF_SHAREABLE | RF_ACTIVE);
533e974f91cSConrad Meyer 	}
534e974f91cSConrad Meyer 	if (ioat->res == NULL) {
535e974f91cSConrad Meyer 		ioat_log_message(0, "bus_alloc_resource failed\n");
536e974f91cSConrad Meyer 		return (ENOMEM);
537e974f91cSConrad Meyer 	}
538e974f91cSConrad Meyer 
539e974f91cSConrad Meyer 	ioat->tag = NULL;
540e974f91cSConrad Meyer 	error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE |
541e974f91cSConrad Meyer 	    INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag);
542e974f91cSConrad Meyer 	if (error != 0) {
543e974f91cSConrad Meyer 		ioat_log_message(0, "bus_setup_intr failed\n");
544e974f91cSConrad Meyer 		return (error);
545e974f91cSConrad Meyer 	}
546e974f91cSConrad Meyer 
547e974f91cSConrad Meyer 	ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN);
548e974f91cSConrad Meyer 	return (0);
549e974f91cSConrad Meyer }
550e974f91cSConrad Meyer 
5514253ea50SConrad Meyer static boolean_t
5520d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat)
5534253ea50SConrad Meyer {
5544253ea50SConrad Meyer 	u_int32_t pciid;
5554253ea50SConrad Meyer 
5564253ea50SConrad Meyer 	pciid = pci_get_devid(ioat->device);
5574253ea50SConrad Meyer 	switch (pciid) {
5580d1a05d9SConrad Meyer 		/* BWD: */
5590d1a05d9SConrad Meyer 	case 0x0c508086:
5600d1a05d9SConrad Meyer 	case 0x0c518086:
5610d1a05d9SConrad Meyer 	case 0x0c528086:
5620d1a05d9SConrad Meyer 	case 0x0c538086:
5630d1a05d9SConrad Meyer 		/* BDXDE: */
5644253ea50SConrad Meyer 	case 0x6f508086:
5654253ea50SConrad Meyer 	case 0x6f518086:
5664253ea50SConrad Meyer 	case 0x6f528086:
5674253ea50SConrad Meyer 	case 0x6f538086:
5684253ea50SConrad Meyer 		return (TRUE);
5694253ea50SConrad Meyer 	}
5704253ea50SConrad Meyer 
5714253ea50SConrad Meyer 	return (FALSE);
5724253ea50SConrad Meyer }
5734253ea50SConrad Meyer 
574e974f91cSConrad Meyer static void
575e974f91cSConrad Meyer ioat_interrupt_handler(void *arg)
576e974f91cSConrad Meyer {
577e974f91cSConrad Meyer 	struct ioat_softc *ioat = arg;
578e974f91cSConrad Meyer 
579e974f91cSConrad Meyer 	ioat_process_events(ioat);
580e974f91cSConrad Meyer }
581e974f91cSConrad Meyer 
582*faefad9cSConrad Meyer static int
583*faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr)
584*faefad9cSConrad Meyer {
585*faefad9cSConrad Meyer 
586*faefad9cSConrad Meyer 	if (chanerr == 0)
587*faefad9cSConrad Meyer 		return (0);
588*faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0)
589*faefad9cSConrad Meyer 		return (EFAULT);
590*faefad9cSConrad Meyer 	if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0)
591*faefad9cSConrad Meyer 		return (EIO);
592*faefad9cSConrad Meyer 	/* This one is probably our fault: */
593*faefad9cSConrad Meyer 	if ((chanerr & IOAT_CHANERR_NDADDERR) != 0)
594*faefad9cSConrad Meyer 		return (EIO);
595*faefad9cSConrad Meyer 	return (EIO);
596*faefad9cSConrad Meyer }
597*faefad9cSConrad Meyer 
598e974f91cSConrad Meyer static void
599e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat)
600e974f91cSConrad Meyer {
601e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
602e974f91cSConrad Meyer 	struct bus_dmadesc *dmadesc;
603e974f91cSConrad Meyer 	uint64_t comp_update, status;
604*faefad9cSConrad Meyer 	uint32_t completed, chanerr;
605*faefad9cSConrad Meyer 	int error;
606e974f91cSConrad Meyer 
607e974f91cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
608e974f91cSConrad Meyer 
609e974f91cSConrad Meyer 	completed = 0;
610e974f91cSConrad Meyer 	comp_update = *ioat->comp_update;
611e974f91cSConrad Meyer 	status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK;
612e974f91cSConrad Meyer 
61343fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
614e974f91cSConrad Meyer 
6154becebdfSConrad Meyer 	if (status == ioat->last_seen)
6164becebdfSConrad Meyer 		goto out;
617e974f91cSConrad Meyer 
618e974f91cSConrad Meyer 	while (1) {
619e974f91cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
620e974f91cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
62143fc1847SConrad Meyer 		CTR1(KTR_IOAT, "completing desc %d", ioat->tail);
622e974f91cSConrad Meyer 
623*faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
624*faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg, 0);
625e974f91cSConrad Meyer 
626466b3540SConrad Meyer 		completed++;
627e974f91cSConrad Meyer 		ioat->tail++;
628e974f91cSConrad Meyer 		if (desc->hw_desc_bus_addr == status)
629e974f91cSConrad Meyer 			break;
630e974f91cSConrad Meyer 	}
631e974f91cSConrad Meyer 
632e974f91cSConrad Meyer 	ioat->last_seen = desc->hw_desc_bus_addr;
633e974f91cSConrad Meyer 
634e974f91cSConrad Meyer 	if (ioat->head == ioat->tail) {
635e974f91cSConrad Meyer 		ioat->is_completion_pending = FALSE;
636fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
637fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
638e974f91cSConrad Meyer 	}
639e974f91cSConrad Meyer 
6404becebdfSConrad Meyer out:
641e974f91cSConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
642e974f91cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
643466b3540SConrad Meyer 
644466b3540SConrad Meyer 	ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF);
645bf8553eaSConrad Meyer 	wakeup(&ioat->tail);
646*faefad9cSConrad Meyer 
647*faefad9cSConrad Meyer 	if (!is_ioat_halted(comp_update))
648*faefad9cSConrad Meyer 		return;
649*faefad9cSConrad Meyer 
650*faefad9cSConrad Meyer 	/*
651*faefad9cSConrad Meyer 	 * Fatal programming error on this DMA channel.  Flush any outstanding
652*faefad9cSConrad Meyer 	 * work with error status and restart the engine.
653*faefad9cSConrad Meyer 	 */
654*faefad9cSConrad Meyer 	ioat_log_message(0, "Channel halted due to fatal programming error\n");
655*faefad9cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
656*faefad9cSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
657*faefad9cSConrad Meyer 	ioat->quiescing = TRUE;
658*faefad9cSConrad Meyer 
659*faefad9cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
660*faefad9cSConrad Meyer 	ioat_halted_debug(ioat, chanerr);
661*faefad9cSConrad Meyer 
662*faefad9cSConrad Meyer 	while (ioat_get_active(ioat) > 0) {
663*faefad9cSConrad Meyer 		desc = ioat_get_ring_entry(ioat, ioat->tail);
664*faefad9cSConrad Meyer 		dmadesc = &desc->bus_dmadesc;
665*faefad9cSConrad Meyer 		CTR1(KTR_IOAT, "completing err desc %d", ioat->tail);
666*faefad9cSConrad Meyer 
667*faefad9cSConrad Meyer 		if (dmadesc->callback_fn != NULL)
668*faefad9cSConrad Meyer 			dmadesc->callback_fn(dmadesc->callback_arg,
669*faefad9cSConrad Meyer 			    chanerr_to_errno(chanerr));
670*faefad9cSConrad Meyer 
671*faefad9cSConrad Meyer 		ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF);
672*faefad9cSConrad Meyer 		ioat->tail++;
673*faefad9cSConrad Meyer 	}
674*faefad9cSConrad Meyer 
675*faefad9cSConrad Meyer 	/* Clear error status */
676*faefad9cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
677*faefad9cSConrad Meyer 
678*faefad9cSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
679*faefad9cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
680*faefad9cSConrad Meyer 
681*faefad9cSConrad Meyer 	ioat_log_message(0, "Resetting channel to recover from error\n");
682*faefad9cSConrad Meyer 	error = ioat_reset_hw(ioat);
683*faefad9cSConrad Meyer 	KASSERT(error == 0, ("%s: reset failed: %d", __func__, error));
684e974f91cSConrad Meyer }
685e974f91cSConrad Meyer 
686e974f91cSConrad Meyer /*
687e974f91cSConrad Meyer  * User API functions
688e974f91cSConrad Meyer  */
689e974f91cSConrad Meyer bus_dmaengine_t
690e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index)
691e974f91cSConrad Meyer {
6925f77bd3eSConrad Meyer 	struct ioat_softc *sc;
693e974f91cSConrad Meyer 
694466b3540SConrad Meyer 	if (index >= ioat_channel_index)
695e974f91cSConrad Meyer 		return (NULL);
6965f77bd3eSConrad Meyer 
6975f77bd3eSConrad Meyer 	sc = ioat_channel[index];
6985f77bd3eSConrad Meyer 	if (sc == NULL || sc->quiescing)
6995f77bd3eSConrad Meyer 		return (NULL);
7005f77bd3eSConrad Meyer 
7015f77bd3eSConrad Meyer 	return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine);
702466b3540SConrad Meyer }
703466b3540SConrad Meyer 
704466b3540SConrad Meyer void
705466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine)
706466b3540SConrad Meyer {
707466b3540SConrad Meyer 	struct ioat_softc *ioat;
708466b3540SConrad Meyer 
709466b3540SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
710466b3540SConrad Meyer 	ioat_put(ioat, IOAT_DMAENGINE_REF);
711e974f91cSConrad Meyer }
712e974f91cSConrad Meyer 
713e974f91cSConrad Meyer void
714e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine)
715e974f91cSConrad Meyer {
716e974f91cSConrad Meyer 	struct ioat_softc *ioat;
717e974f91cSConrad Meyer 
718e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
719e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
72043fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
721e974f91cSConrad Meyer }
722e974f91cSConrad Meyer 
723e974f91cSConrad Meyer void
724e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine)
725e974f91cSConrad Meyer {
726e974f91cSConrad Meyer 	struct ioat_softc *ioat;
727e974f91cSConrad Meyer 
728e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
72943fc1847SConrad Meyer 	CTR0(KTR_IOAT, __func__);
730bf8553eaSConrad Meyer 	ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head);
731e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
732e974f91cSConrad Meyer }
733e974f91cSConrad Meyer 
7349e3bbf26SConrad Meyer static struct ioat_descriptor *
7359e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op,
7369e3bbf26SConrad Meyer     uint32_t size, uint64_t src, uint64_t dst,
7379e3bbf26SConrad Meyer     bus_dmaengine_callback_t callback_fn, void *callback_arg,
7389e3bbf26SConrad Meyer     uint32_t flags)
739e974f91cSConrad Meyer {
7409e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
741e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
742bf8553eaSConrad Meyer 	int mflags;
743e974f91cSConrad Meyer 
7449e3bbf26SConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
7459e3bbf26SConrad Meyer 
746e974f91cSConrad Meyer 	KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x",
747e974f91cSConrad Meyer 		flags & ~DMA_ALL_FLAGS));
748bf8553eaSConrad Meyer 	if ((flags & DMA_NO_WAIT) != 0)
749bf8553eaSConrad Meyer 		mflags = M_NOWAIT;
750bf8553eaSConrad Meyer 	else
751bf8553eaSConrad Meyer 		mflags = M_WAITOK;
752e974f91cSConrad Meyer 
7539e3bbf26SConrad Meyer 	if (size > ioat->max_xfer_size) {
7549e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n",
7559e3bbf26SConrad Meyer 		    __func__, ioat->max_xfer_size, (unsigned)size);
7569e3bbf26SConrad Meyer 		return (NULL);
7579e3bbf26SConrad Meyer 	}
758e974f91cSConrad Meyer 
759bf8553eaSConrad Meyer 	if (ioat_reserve_space(ioat, 1, mflags) != 0)
760e974f91cSConrad Meyer 		return (NULL);
761e974f91cSConrad Meyer 
762e974f91cSConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->head);
7639e3bbf26SConrad Meyer 	hw_desc = desc->u.generic;
764e974f91cSConrad Meyer 
765e974f91cSConrad Meyer 	hw_desc->u.control_raw = 0;
7669e3bbf26SConrad Meyer 	hw_desc->u.control_generic.op = op;
7679e3bbf26SConrad Meyer 	hw_desc->u.control_generic.completion_update = 1;
768e974f91cSConrad Meyer 
769e974f91cSConrad Meyer 	if ((flags & DMA_INT_EN) != 0)
7709e3bbf26SConrad Meyer 		hw_desc->u.control_generic.int_enable = 1;
771e974f91cSConrad Meyer 
7729e3bbf26SConrad Meyer 	hw_desc->size = size;
7739e3bbf26SConrad Meyer 	hw_desc->src_addr = src;
7749e3bbf26SConrad Meyer 	hw_desc->dest_addr = dst;
775e974f91cSConrad Meyer 
776e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_fn = callback_fn;
777e974f91cSConrad Meyer 	desc->bus_dmadesc.callback_arg = callback_arg;
7789e3bbf26SConrad Meyer 	return (desc);
7799e3bbf26SConrad Meyer }
780e974f91cSConrad Meyer 
7819e3bbf26SConrad Meyer struct bus_dmadesc *
7829e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn,
7839e3bbf26SConrad Meyer     void *callback_arg, uint32_t flags)
7849e3bbf26SConrad Meyer {
7859e3bbf26SConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
7869e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
7879e3bbf26SConrad Meyer 	struct ioat_softc *ioat;
7889e3bbf26SConrad Meyer 
7899e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
7909e3bbf26SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
7919e3bbf26SConrad Meyer 
7929e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn,
7939e3bbf26SConrad Meyer 	    callback_arg, flags);
7949e3bbf26SConrad Meyer 	if (desc == NULL)
7959e3bbf26SConrad Meyer 		return (NULL);
7969e3bbf26SConrad Meyer 
7979e3bbf26SConrad Meyer 	hw_desc = desc->u.dma;
7989e3bbf26SConrad Meyer 	hw_desc->u.control.null = 1;
799e974f91cSConrad Meyer 	ioat_submit_single(ioat);
800e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
801e974f91cSConrad Meyer }
802e974f91cSConrad Meyer 
803e974f91cSConrad Meyer struct bus_dmadesc *
804e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst,
805e974f91cSConrad Meyer     bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn,
806e974f91cSConrad Meyer     void *callback_arg, uint32_t flags)
807e974f91cSConrad Meyer {
808e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw_desc;
8099e3bbf26SConrad Meyer 	struct ioat_descriptor *desc;
810e974f91cSConrad Meyer 	struct ioat_softc *ioat;
811e974f91cSConrad Meyer 
8129e3bbf26SConrad Meyer 	CTR0(KTR_IOAT, __func__);
813e974f91cSConrad Meyer 	ioat = to_ioat_softc(dmaengine);
814e974f91cSConrad Meyer 
8159e3bbf26SConrad Meyer 	if (((src | dst) & (0xffffull << 48)) != 0) {
8169e3bbf26SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n",
8179e3bbf26SConrad Meyer 		    __func__);
818e974f91cSConrad Meyer 		return (NULL);
819e974f91cSConrad Meyer 	}
820e974f91cSConrad Meyer 
8219e3bbf26SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn,
8229e3bbf26SConrad Meyer 	    callback_arg, flags);
8239e3bbf26SConrad Meyer 	if (desc == NULL)
824e974f91cSConrad Meyer 		return (NULL);
825e974f91cSConrad Meyer 
826e974f91cSConrad Meyer 	hw_desc = desc->u.dma;
827e974f91cSConrad Meyer 	if (g_ioat_debug_level >= 3)
828e974f91cSConrad Meyer 		dump_descriptor(hw_desc);
829e974f91cSConrad Meyer 
830e974f91cSConrad Meyer 	ioat_submit_single(ioat);
831e974f91cSConrad Meyer 	return (&desc->bus_dmadesc);
832e974f91cSConrad Meyer }
833e974f91cSConrad Meyer 
8342a4fd6b1SConrad Meyer struct bus_dmadesc *
8352a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern,
8362a4fd6b1SConrad Meyer     bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg,
8372a4fd6b1SConrad Meyer     uint32_t flags)
8382a4fd6b1SConrad Meyer {
8392a4fd6b1SConrad Meyer 	struct ioat_fill_hw_descriptor *hw_desc;
8402a4fd6b1SConrad Meyer 	struct ioat_descriptor *desc;
8412a4fd6b1SConrad Meyer 	struct ioat_softc *ioat;
8422a4fd6b1SConrad Meyer 
8432a4fd6b1SConrad Meyer 	CTR0(KTR_IOAT, __func__);
8442a4fd6b1SConrad Meyer 	ioat = to_ioat_softc(dmaengine);
8452a4fd6b1SConrad Meyer 
8461693d27bSConrad Meyer 	if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) {
8471693d27bSConrad Meyer 		ioat_log_message(0, "%s: Device lacks BFILL capability\n",
8481693d27bSConrad Meyer 		    __func__);
8491693d27bSConrad Meyer 		return (NULL);
8501693d27bSConrad Meyer 	}
8511693d27bSConrad Meyer 
8522a4fd6b1SConrad Meyer 	if ((dst & (0xffffull << 48)) != 0) {
8532a4fd6b1SConrad Meyer 		ioat_log_message(0, "%s: High 16 bits of dst invalid\n",
8542a4fd6b1SConrad Meyer 		    __func__);
8552a4fd6b1SConrad Meyer 		return (NULL);
8562a4fd6b1SConrad Meyer 	}
8572a4fd6b1SConrad Meyer 
8582a4fd6b1SConrad Meyer 	desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst,
8592a4fd6b1SConrad Meyer 	    callback_fn, callback_arg, flags);
8602a4fd6b1SConrad Meyer 	if (desc == NULL)
8612a4fd6b1SConrad Meyer 		return (NULL);
8622a4fd6b1SConrad Meyer 
8632a4fd6b1SConrad Meyer 	hw_desc = desc->u.fill;
8642a4fd6b1SConrad Meyer 	if (g_ioat_debug_level >= 3)
8652a4fd6b1SConrad Meyer 		dump_descriptor(hw_desc);
8662a4fd6b1SConrad Meyer 
8672a4fd6b1SConrad Meyer 	ioat_submit_single(ioat);
8682a4fd6b1SConrad Meyer 	return (&desc->bus_dmadesc);
8692a4fd6b1SConrad Meyer }
8702a4fd6b1SConrad Meyer 
871e974f91cSConrad Meyer /*
872e974f91cSConrad Meyer  * Ring Management
873e974f91cSConrad Meyer  */
874e974f91cSConrad Meyer static inline uint32_t
875e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat)
876e974f91cSConrad Meyer {
877e974f91cSConrad Meyer 
878e974f91cSConrad Meyer 	return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1));
879e974f91cSConrad Meyer }
880e974f91cSConrad Meyer 
881e974f91cSConrad Meyer static inline uint32_t
882e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat)
883e974f91cSConrad Meyer {
884e974f91cSConrad Meyer 
885e974f91cSConrad Meyer 	return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1);
886e974f91cSConrad Meyer }
887e974f91cSConrad Meyer 
888e974f91cSConrad Meyer static struct ioat_descriptor *
889bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags)
890e974f91cSConrad Meyer {
8919e3bbf26SConrad Meyer 	struct ioat_generic_hw_descriptor *hw_desc;
892e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
893bf8553eaSConrad Meyer 	int error, busdmaflag;
894e974f91cSConrad Meyer 
895f46011aeSConrad Meyer 	error = ENOMEM;
896f46011aeSConrad Meyer 	hw_desc = NULL;
897f46011aeSConrad Meyer 
898bf8553eaSConrad Meyer 	if ((mflags & M_WAITOK) != 0)
899bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_WAITOK;
900bf8553eaSConrad Meyer 	else
901bf8553eaSConrad Meyer 		busdmaflag = BUS_DMA_NOWAIT;
902bf8553eaSConrad Meyer 
903bf8553eaSConrad Meyer 	desc = malloc(sizeof(*desc), M_IOAT, mflags);
904e974f91cSConrad Meyer 	if (desc == NULL)
905f46011aeSConrad Meyer 		goto out;
906e974f91cSConrad Meyer 
907f46011aeSConrad Meyer 	bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc,
908bf8553eaSConrad Meyer 	    BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map);
909f46011aeSConrad Meyer 	if (hw_desc == NULL)
910f46011aeSConrad Meyer 		goto out;
911e974f91cSConrad Meyer 
912*faefad9cSConrad Meyer 	memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc));
9139e3bbf26SConrad Meyer 	desc->u.generic = hw_desc;
914f46011aeSConrad Meyer 
915f46011aeSConrad Meyer 	error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc,
916f46011aeSConrad Meyer 	    sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr,
917bf8553eaSConrad Meyer 	    busdmaflag);
918f46011aeSConrad Meyer 	if (error)
919f46011aeSConrad Meyer 		goto out;
920f46011aeSConrad Meyer 
921f46011aeSConrad Meyer out:
922f46011aeSConrad Meyer 	if (error) {
923f46011aeSConrad Meyer 		ioat_free_ring_entry(ioat, desc);
924f46011aeSConrad Meyer 		return (NULL);
925f46011aeSConrad Meyer 	}
926e974f91cSConrad Meyer 	return (desc);
927e974f91cSConrad Meyer }
928e974f91cSConrad Meyer 
929e974f91cSConrad Meyer static void
930e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc)
931e974f91cSConrad Meyer {
932e974f91cSConrad Meyer 
933e974f91cSConrad Meyer 	if (desc == NULL)
934e974f91cSConrad Meyer 		return;
935e974f91cSConrad Meyer 
9369e3bbf26SConrad Meyer 	if (desc->u.generic)
9379e3bbf26SConrad Meyer 		bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic,
938e974f91cSConrad Meyer 		    ioat->hw_desc_map);
939e974f91cSConrad Meyer 	free(desc, M_IOAT);
940e974f91cSConrad Meyer }
941e974f91cSConrad Meyer 
942bf8553eaSConrad Meyer /*
943bf8553eaSConrad Meyer  * Reserves space in this IOAT descriptor ring by ensuring enough slots remain
944bf8553eaSConrad Meyer  * for 'num_descs'.
945bf8553eaSConrad Meyer  *
946bf8553eaSConrad Meyer  * If mflags contains M_WAITOK, blocks until enough space is available.
947bf8553eaSConrad Meyer  *
948bf8553eaSConrad Meyer  * Returns zero on success, or an errno on error.  If num_descs is beyond the
949bf8553eaSConrad Meyer  * maximum ring size, returns EINVAl; if allocation would block and mflags
950bf8553eaSConrad Meyer  * contains M_NOWAIT, returns EAGAIN.
951bf8553eaSConrad Meyer  *
952bf8553eaSConrad Meyer  * Must be called with the submit_lock held; returns with the lock held.  The
953bf8553eaSConrad Meyer  * lock may be dropped to allocate the ring.
954bf8553eaSConrad Meyer  *
955bf8553eaSConrad Meyer  * (The submit_lock is needed to add any entries to the ring, so callers are
956bf8553eaSConrad Meyer  * assured enough room is available.)
957bf8553eaSConrad Meyer  */
958e974f91cSConrad Meyer static int
959bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags)
960e974f91cSConrad Meyer {
961bf8553eaSConrad Meyer 	struct ioat_descriptor **new_ring;
962bf8553eaSConrad Meyer 	uint32_t order;
963bf8553eaSConrad Meyer 	int error;
964e974f91cSConrad Meyer 
965bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
966bf8553eaSConrad Meyer 	error = 0;
967e974f91cSConrad Meyer 
968bf8553eaSConrad Meyer 	if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) {
969bf8553eaSConrad Meyer 		error = EINVAL;
970bf8553eaSConrad Meyer 		goto out;
971e974f91cSConrad Meyer 	}
9725f77bd3eSConrad Meyer 	if (ioat->quiescing) {
9735f77bd3eSConrad Meyer 		error = ENXIO;
9745f77bd3eSConrad Meyer 		goto out;
9755f77bd3eSConrad Meyer 	}
976bf8553eaSConrad Meyer 
977bf8553eaSConrad Meyer 	for (;;) {
978bf8553eaSConrad Meyer 		if (ioat_get_ring_space(ioat) >= num_descs)
979bf8553eaSConrad Meyer 			goto out;
980bf8553eaSConrad Meyer 
981bf8553eaSConrad Meyer 		order = ioat->ring_size_order;
982bf8553eaSConrad Meyer 		if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) {
983bf8553eaSConrad Meyer 			if ((mflags & M_WAITOK) != 0) {
984bf8553eaSConrad Meyer 				msleep(&ioat->tail, &ioat->submit_lock, 0,
985bf8553eaSConrad Meyer 				    "ioat_rsz", 0);
986bf8553eaSConrad Meyer 				continue;
987bf8553eaSConrad Meyer 			}
988bf8553eaSConrad Meyer 
989bf8553eaSConrad Meyer 			error = EAGAIN;
990bf8553eaSConrad Meyer 			break;
991bf8553eaSConrad Meyer 		}
992bf8553eaSConrad Meyer 
993bf8553eaSConrad Meyer 		ioat->is_resize_pending = TRUE;
994bf8553eaSConrad Meyer 		for (;;) {
995bf8553eaSConrad Meyer 			mtx_unlock(&ioat->submit_lock);
996bf8553eaSConrad Meyer 
997bf8553eaSConrad Meyer 			new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1),
998bf8553eaSConrad Meyer 			    TRUE, mflags);
999bf8553eaSConrad Meyer 
1000bf8553eaSConrad Meyer 			mtx_lock(&ioat->submit_lock);
1001bf8553eaSConrad Meyer 			KASSERT(ioat->ring_size_order == order,
1002bf8553eaSConrad Meyer 			    ("is_resize_pending should protect order"));
1003bf8553eaSConrad Meyer 
1004bf8553eaSConrad Meyer 			if (new_ring == NULL) {
1005bf8553eaSConrad Meyer 				KASSERT((mflags & M_WAITOK) == 0,
1006bf8553eaSConrad Meyer 				    ("allocation failed"));
1007bf8553eaSConrad Meyer 				error = EAGAIN;
1008bf8553eaSConrad Meyer 				break;
1009bf8553eaSConrad Meyer 			}
1010bf8553eaSConrad Meyer 
1011bf8553eaSConrad Meyer 			error = ring_grow(ioat, order, new_ring);
1012bf8553eaSConrad Meyer 			if (error == 0)
1013bf8553eaSConrad Meyer 				break;
1014bf8553eaSConrad Meyer 		}
1015bf8553eaSConrad Meyer 		ioat->is_resize_pending = FALSE;
1016bf8553eaSConrad Meyer 		wakeup(&ioat->tail);
1017bf8553eaSConrad Meyer 		if (error)
1018bf8553eaSConrad Meyer 			break;
1019bf8553eaSConrad Meyer 	}
1020bf8553eaSConrad Meyer 
1021bf8553eaSConrad Meyer out:
1022bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1023bf8553eaSConrad Meyer 	return (error);
1024bf8553eaSConrad Meyer }
1025bf8553eaSConrad Meyer 
1026bf8553eaSConrad Meyer static struct ioat_descriptor **
1027bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr,
1028bf8553eaSConrad Meyer     int mflags)
1029bf8553eaSConrad Meyer {
1030bf8553eaSConrad Meyer 	struct ioat_descriptor **ring;
1031bf8553eaSConrad Meyer 	uint32_t i;
1032bf8553eaSConrad Meyer 	int error;
1033bf8553eaSConrad Meyer 
1034bf8553eaSConrad Meyer 	KASSERT(size > 0 && powerof2(size), ("bogus size"));
1035bf8553eaSConrad Meyer 
1036bf8553eaSConrad Meyer 	ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags);
1037bf8553eaSConrad Meyer 	if (ring == NULL)
1038bf8553eaSConrad Meyer 		return (NULL);
1039bf8553eaSConrad Meyer 
1040bf8553eaSConrad Meyer 	if (need_dscr) {
1041bf8553eaSConrad Meyer 		error = ENOMEM;
1042bf8553eaSConrad Meyer 		for (i = size / 2; i < size; i++) {
1043bf8553eaSConrad Meyer 			ring[i] = ioat_alloc_ring_entry(ioat, mflags);
1044bf8553eaSConrad Meyer 			if (ring[i] == NULL)
1045bf8553eaSConrad Meyer 				goto out;
1046bf8553eaSConrad Meyer 			ring[i]->id = i;
1047bf8553eaSConrad Meyer 		}
1048bf8553eaSConrad Meyer 	}
1049bf8553eaSConrad Meyer 	error = 0;
1050bf8553eaSConrad Meyer 
1051bf8553eaSConrad Meyer out:
1052bf8553eaSConrad Meyer 	if (error != 0 && ring != NULL) {
1053bf8553eaSConrad Meyer 		ioat_free_ring(ioat, size, ring);
1054bf8553eaSConrad Meyer 		ring = NULL;
1055bf8553eaSConrad Meyer 	}
1056bf8553eaSConrad Meyer 	return (ring);
1057bf8553eaSConrad Meyer }
1058bf8553eaSConrad Meyer 
1059bf8553eaSConrad Meyer static void
1060bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size,
1061bf8553eaSConrad Meyer     struct ioat_descriptor **ring)
1062bf8553eaSConrad Meyer {
1063bf8553eaSConrad Meyer 	uint32_t i;
1064bf8553eaSConrad Meyer 
1065bf8553eaSConrad Meyer 	for (i = 0; i < size; i++) {
1066bf8553eaSConrad Meyer 		if (ring[i] != NULL)
1067bf8553eaSConrad Meyer 			ioat_free_ring_entry(ioat, ring[i]);
1068bf8553eaSConrad Meyer 	}
1069bf8553eaSConrad Meyer 	free(ring, M_IOAT);
1070e974f91cSConrad Meyer }
1071e974f91cSConrad Meyer 
1072e974f91cSConrad Meyer static struct ioat_descriptor *
1073e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index)
1074e974f91cSConrad Meyer {
1075e974f91cSConrad Meyer 
1076e974f91cSConrad Meyer 	return (ioat->ring[index % (1 << ioat->ring_size_order)]);
1077e974f91cSConrad Meyer }
1078e974f91cSConrad Meyer 
1079bf8553eaSConrad Meyer static int
1080bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder,
1081bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1082e974f91cSConrad Meyer {
1083bf8553eaSConrad Meyer 	struct ioat_descriptor *tmp, *next;
1084e974f91cSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1085bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, head, tail, i, end;
1086bf8553eaSConrad Meyer 	int error;
1087e974f91cSConrad Meyer 
1088bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1089e974f91cSConrad Meyer 
1090bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1091bf8553eaSConrad Meyer 
1092bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) {
1093bf8553eaSConrad Meyer 		error = EINVAL;
1094bf8553eaSConrad Meyer 		goto out;
1095bf8553eaSConrad Meyer 	}
1096bf8553eaSConrad Meyer 
1097bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1098bf8553eaSConrad Meyer 	newsize = (1 << (oldorder + 1));
1099bf8553eaSConrad Meyer 
1100bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1101bf8553eaSConrad Meyer 
1102bf8553eaSConrad Meyer 	head = ioat->head & (oldsize - 1);
1103bf8553eaSConrad Meyer 	tail = ioat->tail & (oldsize - 1);
1104bf8553eaSConrad Meyer 
1105bf8553eaSConrad Meyer 	/* Copy old descriptors to new ring */
1106bf8553eaSConrad Meyer 	for (i = 0; i < oldsize; i++)
1107bf8553eaSConrad Meyer 		newring[i] = ioat->ring[i];
1108e974f91cSConrad Meyer 
1109e974f91cSConrad Meyer 	/*
1110bf8553eaSConrad Meyer 	 * If head has wrapped but tail hasn't, we must swap some descriptors
1111bf8553eaSConrad Meyer 	 * around so that tail can increment directly to head.
1112e974f91cSConrad Meyer 	 */
1113bf8553eaSConrad Meyer 	if (head < tail) {
1114bf8553eaSConrad Meyer 		for (i = 0; i <= head; i++) {
1115bf8553eaSConrad Meyer 			tmp = newring[oldsize + i];
1116e974f91cSConrad Meyer 
1117bf8553eaSConrad Meyer 			newring[oldsize + i] = newring[i];
1118bf8553eaSConrad Meyer 			newring[oldsize + i]->id = oldsize + i;
1119e974f91cSConrad Meyer 
1120bf8553eaSConrad Meyer 			newring[i] = tmp;
1121bf8553eaSConrad Meyer 			newring[i]->id = i;
1122bf8553eaSConrad Meyer 		}
1123bf8553eaSConrad Meyer 		head += oldsize;
1124e974f91cSConrad Meyer 	}
1125e974f91cSConrad Meyer 
1126bf8553eaSConrad Meyer 	KASSERT(head >= tail, ("invariants"));
1127e974f91cSConrad Meyer 
1128bf8553eaSConrad Meyer 	/* Head didn't wrap; we only need to link in oldsize..newsize */
1129bf8553eaSConrad Meyer 	if (head < oldsize) {
1130bf8553eaSConrad Meyer 		i = oldsize - 1;
1131bf8553eaSConrad Meyer 		end = newsize;
1132e974f91cSConrad Meyer 	} else {
1133bf8553eaSConrad Meyer 		/* Head did wrap; link newhead..newsize and 0..oldhead */
1134bf8553eaSConrad Meyer 		i = head;
1135bf8553eaSConrad Meyer 		end = newsize + (head - oldsize) + 1;
1136bf8553eaSConrad Meyer 	}
1137bf8553eaSConrad Meyer 
1138e974f91cSConrad Meyer 	/*
1139bf8553eaSConrad Meyer 	 * Fix up hardware ring, being careful not to trample the active
1140bf8553eaSConrad Meyer 	 * section (tail -> head).
1141e974f91cSConrad Meyer 	 */
1142bf8553eaSConrad Meyer 	for (; i < end; i++) {
1143bf8553eaSConrad Meyer 		KASSERT((i & (newsize - 1)) < tail ||
1144bf8553eaSConrad Meyer 		    (i & (newsize - 1)) >= head, ("trampling snake"));
1145e974f91cSConrad Meyer 
1146bf8553eaSConrad Meyer 		next = newring[(i + 1) & (newsize - 1)];
1147bf8553eaSConrad Meyer 		hw = newring[i & (newsize - 1)]->u.dma;
1148e974f91cSConrad Meyer 		hw->next = next->hw_desc_bus_addr;
1149e974f91cSConrad Meyer 	}
1150e974f91cSConrad Meyer 
1151e974f91cSConrad Meyer 	free(ioat->ring, M_IOAT);
1152bf8553eaSConrad Meyer 	ioat->ring = newring;
1153bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder + 1;
1154bf8553eaSConrad Meyer 	ioat->tail = tail;
1155bf8553eaSConrad Meyer 	ioat->head = head;
1156bf8553eaSConrad Meyer 	error = 0;
1157e974f91cSConrad Meyer 
1158bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1159bf8553eaSConrad Meyer out:
1160bf8553eaSConrad Meyer 	if (error)
1161bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder + 1)), newring);
1162bf8553eaSConrad Meyer 	return (error);
1163bf8553eaSConrad Meyer }
1164bf8553eaSConrad Meyer 
1165bf8553eaSConrad Meyer static int
1166bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder,
1167bf8553eaSConrad Meyer     struct ioat_descriptor **newring)
1168bf8553eaSConrad Meyer {
1169bf8553eaSConrad Meyer 	struct ioat_dma_hw_descriptor *hw;
1170bf8553eaSConrad Meyer 	struct ioat_descriptor *ent, *next;
1171bf8553eaSConrad Meyer 	uint32_t oldsize, newsize, current_idx, new_idx, i;
1172bf8553eaSConrad Meyer 	int error;
1173bf8553eaSConrad Meyer 
1174bf8553eaSConrad Meyer 	CTR0(KTR_IOAT, __func__);
1175bf8553eaSConrad Meyer 
1176bf8553eaSConrad Meyer 	mtx_assert(&ioat->submit_lock, MA_OWNED);
1177bf8553eaSConrad Meyer 
1178bf8553eaSConrad Meyer 	if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) {
1179bf8553eaSConrad Meyer 		error = EINVAL;
1180bf8553eaSConrad Meyer 		goto out_unlocked;
1181bf8553eaSConrad Meyer 	}
1182bf8553eaSConrad Meyer 
1183bf8553eaSConrad Meyer 	oldsize = (1 << oldorder);
1184bf8553eaSConrad Meyer 	newsize = (1 << (oldorder - 1));
1185bf8553eaSConrad Meyer 
1186bf8553eaSConrad Meyer 	mtx_lock(&ioat->cleanup_lock);
1187bf8553eaSConrad Meyer 
1188bf8553eaSConrad Meyer 	/* Can't shrink below current active set! */
1189bf8553eaSConrad Meyer 	if (ioat_get_active(ioat) >= newsize) {
1190bf8553eaSConrad Meyer 		error = ENOMEM;
1191bf8553eaSConrad Meyer 		goto out;
1192bf8553eaSConrad Meyer 	}
1193bf8553eaSConrad Meyer 
1194bf8553eaSConrad Meyer 	/*
1195bf8553eaSConrad Meyer 	 * Copy current descriptors to the new ring, dropping the removed
1196bf8553eaSConrad Meyer 	 * descriptors.
1197bf8553eaSConrad Meyer 	 */
1198bf8553eaSConrad Meyer 	for (i = 0; i < newsize; i++) {
1199bf8553eaSConrad Meyer 		current_idx = (ioat->tail + i) & (oldsize - 1);
1200bf8553eaSConrad Meyer 		new_idx = (ioat->tail + i) & (newsize - 1);
1201bf8553eaSConrad Meyer 
1202bf8553eaSConrad Meyer 		newring[new_idx] = ioat->ring[current_idx];
1203bf8553eaSConrad Meyer 		newring[new_idx]->id = new_idx;
1204bf8553eaSConrad Meyer 	}
1205bf8553eaSConrad Meyer 
1206bf8553eaSConrad Meyer 	/* Free deleted descriptors */
1207bf8553eaSConrad Meyer 	for (i = newsize; i < oldsize; i++) {
1208bf8553eaSConrad Meyer 		ent = ioat_get_ring_entry(ioat, ioat->tail + i);
1209bf8553eaSConrad Meyer 		ioat_free_ring_entry(ioat, ent);
1210bf8553eaSConrad Meyer 	}
1211bf8553eaSConrad Meyer 
1212bf8553eaSConrad Meyer 	/* Fix up hardware ring. */
1213bf8553eaSConrad Meyer 	hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma;
1214bf8553eaSConrad Meyer 	next = newring[(ioat->tail + newsize) & (newsize - 1)];
1215bf8553eaSConrad Meyer 	hw->next = next->hw_desc_bus_addr;
1216bf8553eaSConrad Meyer 
1217bf8553eaSConrad Meyer 	free(ioat->ring, M_IOAT);
1218bf8553eaSConrad Meyer 	ioat->ring = newring;
1219bf8553eaSConrad Meyer 	ioat->ring_size_order = oldorder - 1;
1220bf8553eaSConrad Meyer 	error = 0;
1221bf8553eaSConrad Meyer 
1222bf8553eaSConrad Meyer out:
1223bf8553eaSConrad Meyer 	mtx_unlock(&ioat->cleanup_lock);
1224bf8553eaSConrad Meyer out_unlocked:
1225bf8553eaSConrad Meyer 	if (error)
1226bf8553eaSConrad Meyer 		ioat_free_ring(ioat, (1 << (oldorder - 1)), newring);
1227bf8553eaSConrad Meyer 	return (error);
1228e974f91cSConrad Meyer }
1229e974f91cSConrad Meyer 
1230e974f91cSConrad Meyer static void
12318f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr)
1232e974f91cSConrad Meyer {
1233e974f91cSConrad Meyer 	struct ioat_descriptor *desc;
12348f274637SConrad Meyer 
123559acd4baSConrad Meyer 	ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr,
123659acd4baSConrad Meyer 	    IOAT_CHANERR_STR);
12378f274637SConrad Meyer 	if (chanerr == 0)
12388f274637SConrad Meyer 		return;
12398f274637SConrad Meyer 
1240*faefad9cSConrad Meyer 	mtx_assert(&ioat->cleanup_lock, MA_OWNED);
1241*faefad9cSConrad Meyer 
12428f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 0);
12438f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
12448f274637SConrad Meyer 
12458f274637SConrad Meyer 	desc = ioat_get_ring_entry(ioat, ioat->tail + 1);
12468f274637SConrad Meyer 	dump_descriptor(desc->u.raw);
12478f274637SConrad Meyer }
12488f274637SConrad Meyer 
12498f274637SConrad Meyer static void
12508f274637SConrad Meyer ioat_timer_callback(void *arg)
12518f274637SConrad Meyer {
1252bf8553eaSConrad Meyer 	struct ioat_descriptor **newring;
1253e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1254*faefad9cSConrad Meyer 	uint32_t order;
1255e974f91cSConrad Meyer 
1256e974f91cSConrad Meyer 	ioat = arg;
1257fe720f5aSConrad Meyer 	ioat_log_message(1, "%s\n", __func__);
1258e974f91cSConrad Meyer 
1259e974f91cSConrad Meyer 	if (ioat->is_completion_pending) {
1260e974f91cSConrad Meyer 		ioat_process_events(ioat);
1261*faefad9cSConrad Meyer 		return;
1262*faefad9cSConrad Meyer 	}
1263*faefad9cSConrad Meyer 
1264*faefad9cSConrad Meyer 	/* Slowly scale the ring down if idle. */
1265e974f91cSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1266bf8553eaSConrad Meyer 	order = ioat->ring_size_order;
1267bf8553eaSConrad Meyer 	if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) {
1268bf8553eaSConrad Meyer 		mtx_unlock(&ioat->submit_lock);
1269bf8553eaSConrad Meyer 		goto out;
1270bf8553eaSConrad Meyer 	}
1271bf8553eaSConrad Meyer 	ioat->is_resize_pending = TRUE;
1272e974f91cSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1273e974f91cSConrad Meyer 
1274bf8553eaSConrad Meyer 	newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE,
1275bf8553eaSConrad Meyer 	    M_NOWAIT);
1276bf8553eaSConrad Meyer 
1277bf8553eaSConrad Meyer 	mtx_lock(&ioat->submit_lock);
1278bf8553eaSConrad Meyer 	KASSERT(ioat->ring_size_order == order,
1279bf8553eaSConrad Meyer 	    ("resize_pending protects order"));
1280bf8553eaSConrad Meyer 
1281bf8553eaSConrad Meyer 	if (newring != NULL)
1282bf8553eaSConrad Meyer 		ring_shrink(ioat, order, newring);
1283bf8553eaSConrad Meyer 
1284bf8553eaSConrad Meyer 	ioat->is_resize_pending = FALSE;
1285bf8553eaSConrad Meyer 	mtx_unlock(&ioat->submit_lock);
1286bf8553eaSConrad Meyer 
1287bf8553eaSConrad Meyer out:
1288e974f91cSConrad Meyer 	if (ioat->ring_size_order > IOAT_MIN_ORDER)
1289bf8553eaSConrad Meyer 		callout_reset(&ioat->timer, 10 * hz,
1290e974f91cSConrad Meyer 		    ioat_timer_callback, ioat);
1291e974f91cSConrad Meyer }
1292e974f91cSConrad Meyer 
1293e974f91cSConrad Meyer /*
1294e974f91cSConrad Meyer  * Support Functions
1295e974f91cSConrad Meyer  */
1296e974f91cSConrad Meyer static void
1297e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat)
1298e974f91cSConrad Meyer {
1299e974f91cSConrad Meyer 
1300466b3540SConrad Meyer 	ioat_get(ioat, IOAT_ACTIVE_DESCR_REF);
1301e974f91cSConrad Meyer 	atomic_add_rel_int(&ioat->head, 1);
1302bf8553eaSConrad Meyer 	atomic_add_rel_int(&ioat->hw_head, 1);
1303e974f91cSConrad Meyer 
1304e974f91cSConrad Meyer 	if (!ioat->is_completion_pending) {
1305e974f91cSConrad Meyer 		ioat->is_completion_pending = TRUE;
1306fe720f5aSConrad Meyer 		callout_reset(&ioat->timer, IOAT_INTR_TIMO,
1307fe720f5aSConrad Meyer 		    ioat_timer_callback, ioat);
1308e974f91cSConrad Meyer 	}
1309e974f91cSConrad Meyer }
1310e974f91cSConrad Meyer 
1311e974f91cSConrad Meyer static int
1312e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat)
1313e974f91cSConrad Meyer {
1314e974f91cSConrad Meyer 	uint64_t status;
1315e974f91cSConrad Meyer 	uint32_t chanerr;
1316cea5b880SConrad Meyer 	unsigned timeout;
13175f77bd3eSConrad Meyer 	int error;
13185f77bd3eSConrad Meyer 
13195f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
13205f77bd3eSConrad Meyer 	ioat->quiescing = TRUE;
13215f77bd3eSConrad Meyer 	ioat_drain_locked(ioat);
13225f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
1323e974f91cSConrad Meyer 
1324e974f91cSConrad Meyer 	status = ioat_get_chansts(ioat);
1325e974f91cSConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status))
1326e974f91cSConrad Meyer 		ioat_suspend(ioat);
1327e974f91cSConrad Meyer 
1328e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1329e974f91cSConrad Meyer 	for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) &&
1330e974f91cSConrad Meyer 	    timeout < 20; timeout++) {
1331e974f91cSConrad Meyer 		DELAY(1000);
1332e974f91cSConrad Meyer 		status = ioat_get_chansts(ioat);
1333e974f91cSConrad Meyer 	}
13345f77bd3eSConrad Meyer 	if (timeout == 20) {
13355f77bd3eSConrad Meyer 		error = ETIMEDOUT;
13365f77bd3eSConrad Meyer 		goto out;
13375f77bd3eSConrad Meyer 	}
1338e974f91cSConrad Meyer 
1339cea5b880SConrad Meyer 	KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce"));
1340cea5b880SConrad Meyer 
1341e974f91cSConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
1342e974f91cSConrad Meyer 	ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr);
1343e974f91cSConrad Meyer 
1344e974f91cSConrad Meyer 	/*
1345e974f91cSConrad Meyer 	 * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors
1346e974f91cSConrad Meyer 	 *  that can cause stability issues for IOAT v3.
1347e974f91cSConrad Meyer 	 */
1348e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07,
1349e974f91cSConrad Meyer 	    4);
1350e974f91cSConrad Meyer 	chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4);
1351e974f91cSConrad Meyer 	pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4);
1352e974f91cSConrad Meyer 
13530d1a05d9SConrad Meyer 	/*
13540d1a05d9SConrad Meyer 	 * BDXDE and BWD models reset MSI-X registers on device reset.
13550d1a05d9SConrad Meyer 	 * Save/restore their contents manually.
13560d1a05d9SConrad Meyer 	 */
1357f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1358f7157235SConrad Meyer 		ioat_log_message(1, "device resets MSI-X registers; saving\n");
13590d1a05d9SConrad Meyer 		pci_save_state(ioat->device);
1360f7157235SConrad Meyer 	}
13610d1a05d9SConrad Meyer 
1362e974f91cSConrad Meyer 	ioat_reset(ioat);
1363e974f91cSConrad Meyer 
1364e974f91cSConrad Meyer 	/* Wait at most 20 ms */
1365e974f91cSConrad Meyer 	for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++)
1366e974f91cSConrad Meyer 		DELAY(1000);
13675f77bd3eSConrad Meyer 	if (timeout == 20) {
13685f77bd3eSConrad Meyer 		error = ETIMEDOUT;
13695f77bd3eSConrad Meyer 		goto out;
13705f77bd3eSConrad Meyer 	}
1371e974f91cSConrad Meyer 
1372f7157235SConrad Meyer 	if (ioat_model_resets_msix(ioat)) {
1373f7157235SConrad Meyer 		ioat_log_message(1, "device resets registers; restored\n");
13740d1a05d9SConrad Meyer 		pci_restore_state(ioat->device);
1375f7157235SConrad Meyer 	}
13764253ea50SConrad Meyer 
1377cea5b880SConrad Meyer 	/* Reset attempts to return the hardware to "halted." */
1378cea5b880SConrad Meyer 	status = ioat_get_chansts(ioat);
1379cea5b880SConrad Meyer 	if (is_ioat_active(status) || is_ioat_idle(status)) {
1380cea5b880SConrad Meyer 		/* So this really shouldn't happen... */
1381cea5b880SConrad Meyer 		ioat_log_message(0, "Device is active after a reset?\n");
1382cea5b880SConrad Meyer 		ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
13835f77bd3eSConrad Meyer 		error = 0;
13845f77bd3eSConrad Meyer 		goto out;
1385e974f91cSConrad Meyer 	}
1386e974f91cSConrad Meyer 
1387cea5b880SConrad Meyer 	chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET);
13885f77bd3eSConrad Meyer 	if (chanerr != 0) {
1389*faefad9cSConrad Meyer 		mtx_lock(&ioat->cleanup_lock);
1390*faefad9cSConrad Meyer 		ioat_halted_debug(ioat, chanerr);
1391*faefad9cSConrad Meyer 		mtx_unlock(&ioat->cleanup_lock);
13925f77bd3eSConrad Meyer 		error = EIO;
13935f77bd3eSConrad Meyer 		goto out;
13945f77bd3eSConrad Meyer 	}
1395cea5b880SConrad Meyer 
1396cea5b880SConrad Meyer 	/*
1397cea5b880SConrad Meyer 	 * Bring device back online after reset.  Writing CHAINADDR brings the
1398cea5b880SConrad Meyer 	 * device back to active.
1399cea5b880SConrad Meyer 	 *
1400cea5b880SConrad Meyer 	 * The internal ring counter resets to zero, so we have to start over
1401cea5b880SConrad Meyer 	 * at zero as well.
1402cea5b880SConrad Meyer 	 */
1403bf8553eaSConrad Meyer 	ioat->tail = ioat->head = ioat->hw_head = 0;
1404cea5b880SConrad Meyer 	ioat->last_seen = 0;
1405cea5b880SConrad Meyer 
1406cea5b880SConrad Meyer 	ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN);
1407cea5b880SConrad Meyer 	ioat_write_chancmp(ioat, ioat->comp_update_bus_addr);
1408cea5b880SConrad Meyer 	ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr);
14095f77bd3eSConrad Meyer 	error = 0;
14105f77bd3eSConrad Meyer 
14115f77bd3eSConrad Meyer out:
14125f77bd3eSConrad Meyer 	mtx_lock(IOAT_REFLK);
14135f77bd3eSConrad Meyer 	ioat->quiescing = FALSE;
14145f77bd3eSConrad Meyer 	mtx_unlock(IOAT_REFLK);
14155f77bd3eSConrad Meyer 
14165f77bd3eSConrad Meyer 	if (error == 0)
14175f77bd3eSConrad Meyer 		error = ioat_start_channel(ioat);
14185f77bd3eSConrad Meyer 
14195f77bd3eSConrad Meyer 	return (error);
1420cea5b880SConrad Meyer }
1421cea5b880SConrad Meyer 
1422f7157235SConrad Meyer static int
1423*faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS)
1424*faefad9cSConrad Meyer {
1425*faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1426*faefad9cSConrad Meyer 	struct sbuf sb;
1427*faefad9cSConrad Meyer 	uint64_t status;
1428*faefad9cSConrad Meyer 	int error;
1429*faefad9cSConrad Meyer 
1430*faefad9cSConrad Meyer 	ioat = arg1;
1431*faefad9cSConrad Meyer 
1432*faefad9cSConrad Meyer 	status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS;
1433*faefad9cSConrad Meyer 
1434*faefad9cSConrad Meyer 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
1435*faefad9cSConrad Meyer 	switch (status) {
1436*faefad9cSConrad Meyer 	case IOAT_CHANSTS_ACTIVE:
1437*faefad9cSConrad Meyer 		sbuf_printf(&sb, "ACTIVE");
1438*faefad9cSConrad Meyer 		break;
1439*faefad9cSConrad Meyer 	case IOAT_CHANSTS_IDLE:
1440*faefad9cSConrad Meyer 		sbuf_printf(&sb, "IDLE");
1441*faefad9cSConrad Meyer 		break;
1442*faefad9cSConrad Meyer 	case IOAT_CHANSTS_SUSPENDED:
1443*faefad9cSConrad Meyer 		sbuf_printf(&sb, "SUSPENDED");
1444*faefad9cSConrad Meyer 		break;
1445*faefad9cSConrad Meyer 	case IOAT_CHANSTS_HALTED:
1446*faefad9cSConrad Meyer 		sbuf_printf(&sb, "HALTED");
1447*faefad9cSConrad Meyer 		break;
1448*faefad9cSConrad Meyer 	case IOAT_CHANSTS_ARMED:
1449*faefad9cSConrad Meyer 		sbuf_printf(&sb, "ARMED");
1450*faefad9cSConrad Meyer 		break;
1451*faefad9cSConrad Meyer 	default:
1452*faefad9cSConrad Meyer 		sbuf_printf(&sb, "UNKNOWN");
1453*faefad9cSConrad Meyer 		break;
1454*faefad9cSConrad Meyer 	}
1455*faefad9cSConrad Meyer 	error = sbuf_finish(&sb);
1456*faefad9cSConrad Meyer 	sbuf_delete(&sb);
1457*faefad9cSConrad Meyer 
1458*faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1459*faefad9cSConrad Meyer 		return (error);
1460*faefad9cSConrad Meyer 	return (EINVAL);
1461*faefad9cSConrad Meyer }
1462*faefad9cSConrad Meyer 
1463*faefad9cSConrad Meyer static int
1464*faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS)
1465*faefad9cSConrad Meyer {
1466*faefad9cSConrad Meyer 	struct ioat_descriptor *desc;
1467*faefad9cSConrad Meyer 	struct ioat_softc *ioat;
1468*faefad9cSConrad Meyer 	int error, arg;
1469*faefad9cSConrad Meyer 
1470*faefad9cSConrad Meyer 	ioat = arg1;
1471*faefad9cSConrad Meyer 
1472*faefad9cSConrad Meyer 	arg = 0;
1473*faefad9cSConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1474*faefad9cSConrad Meyer 	if (error != 0 || req->newptr == NULL)
1475*faefad9cSConrad Meyer 		return (error);
1476*faefad9cSConrad Meyer 
1477*faefad9cSConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1478*faefad9cSConrad Meyer 	if (error != 0)
1479*faefad9cSConrad Meyer 		return (error);
1480*faefad9cSConrad Meyer 
1481*faefad9cSConrad Meyer 	if (arg != 0) {
1482*faefad9cSConrad Meyer 		ioat_acquire(&ioat->dmaengine);
1483*faefad9cSConrad Meyer 		desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1,
1484*faefad9cSConrad Meyer 		    0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL,
1485*faefad9cSConrad Meyer 		    0);
1486*faefad9cSConrad Meyer 		if (desc == NULL)
1487*faefad9cSConrad Meyer 			error = ENOMEM;
1488*faefad9cSConrad Meyer 		else
1489*faefad9cSConrad Meyer 			ioat_submit_single(ioat);
1490*faefad9cSConrad Meyer 		ioat_release(&ioat->dmaengine);
1491*faefad9cSConrad Meyer 	}
1492*faefad9cSConrad Meyer 	return (error);
1493*faefad9cSConrad Meyer }
1494*faefad9cSConrad Meyer 
1495*faefad9cSConrad Meyer static int
1496f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS)
1497f7157235SConrad Meyer {
1498f7157235SConrad Meyer 	struct ioat_softc *ioat;
1499f7157235SConrad Meyer 	int error, arg;
1500f7157235SConrad Meyer 
1501f7157235SConrad Meyer 	ioat = arg1;
1502f7157235SConrad Meyer 
1503f7157235SConrad Meyer 	arg = 0;
1504f7157235SConrad Meyer 	error = SYSCTL_OUT(req, &arg, sizeof(arg));
1505f7157235SConrad Meyer 	if (error != 0 || req->newptr == NULL)
1506f7157235SConrad Meyer 		return (error);
1507f7157235SConrad Meyer 
1508f7157235SConrad Meyer 	error = SYSCTL_IN(req, &arg, sizeof(arg));
1509f7157235SConrad Meyer 	if (error != 0)
1510f7157235SConrad Meyer 		return (error);
1511f7157235SConrad Meyer 
1512f7157235SConrad Meyer 	if (arg != 0)
1513f7157235SConrad Meyer 		error = ioat_reset_hw(ioat);
1514f7157235SConrad Meyer 
1515f7157235SConrad Meyer 	return (error);
1516f7157235SConrad Meyer }
1517f7157235SConrad Meyer 
1518e974f91cSConrad Meyer static void
1519e974f91cSConrad Meyer dump_descriptor(void *hw_desc)
1520e974f91cSConrad Meyer {
1521e974f91cSConrad Meyer 	int i, j;
1522e974f91cSConrad Meyer 
1523e974f91cSConrad Meyer 	for (i = 0; i < 2; i++) {
1524e974f91cSConrad Meyer 		for (j = 0; j < 8; j++)
1525e974f91cSConrad Meyer 			printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]);
1526e974f91cSConrad Meyer 		printf("\n");
1527e974f91cSConrad Meyer 	}
1528e974f91cSConrad Meyer }
1529e974f91cSConrad Meyer 
1530e974f91cSConrad Meyer static void
1531e974f91cSConrad Meyer ioat_setup_sysctl(device_t device)
1532e974f91cSConrad Meyer {
1533f7157235SConrad Meyer 	struct sysctl_oid_list *par;
1534f7157235SConrad Meyer 	struct sysctl_ctx_list *ctx;
1535f7157235SConrad Meyer 	struct sysctl_oid *tree;
1536e974f91cSConrad Meyer 	struct ioat_softc *ioat;
1537e974f91cSConrad Meyer 
1538e974f91cSConrad Meyer 	ioat = DEVICE2SOFTC(device);
1539f7157235SConrad Meyer 	ctx = device_get_sysctl_ctx(device);
1540f7157235SConrad Meyer 	tree = device_get_sysctl_tree(device);
1541f7157235SConrad Meyer 	par = SYSCTL_CHILDREN(tree);
1542e974f91cSConrad Meyer 
154365e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD,
154465e4f8adSConrad Meyer 	    &ioat->version, 0, "HW version (0xMM form)");
154565e4f8adSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD,
154665e4f8adSConrad Meyer 	    &ioat->max_xfer_size, 0, "HW maximum transfer size");
154765e4f8adSConrad Meyer 
1548f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "ring_size_order", CTLFLAG_RD,
1549bf8553eaSConrad Meyer 	    &ioat->ring_size_order, 0, "SW descriptor ring size order");
1550f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 0,
1551bf8553eaSConrad Meyer 	    "SW descriptor head pointer index");
1552f7157235SConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 0,
1553bf8553eaSConrad Meyer 	    "SW descriptor tail pointer index");
1554bf8553eaSConrad Meyer 	SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "hw_head", CTLFLAG_RD,
1555bf8553eaSConrad Meyer 	    &ioat->hw_head, 0, "HW DMACOUNT");
1556f7157235SConrad Meyer 
155765e4f8adSConrad Meyer 	SYSCTL_ADD_UQUAD(ctx, par, OID_AUTO, "last_completion", CTLFLAG_RD,
155865e4f8adSConrad Meyer 	    ioat->comp_update, "HW addr of last completion");
155965e4f8adSConrad Meyer 
156065e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_resize_pending", CTLFLAG_RD,
156165e4f8adSConrad Meyer 	    &ioat->is_resize_pending, 0, "resize pending");
156265e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_completion_pending", CTLFLAG_RD,
156365e4f8adSConrad Meyer 	    &ioat->is_completion_pending, 0, "completion pending");
156465e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_reset_pending", CTLFLAG_RD,
156565e4f8adSConrad Meyer 	    &ioat->is_reset_pending, 0, "reset pending");
156665e4f8adSConrad Meyer 	SYSCTL_ADD_INT(ctx, par, OID_AUTO, "is_channel_running", CTLFLAG_RD,
156765e4f8adSConrad Meyer 	    &ioat->is_channel_running, 0, "channel running");
156865e4f8adSConrad Meyer 
1569f7157235SConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "force_hw_reset",
1570f7157235SConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I",
1571f7157235SConrad Meyer 	    "Set to non-zero to reset the hardware");
1572*faefad9cSConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "force_hw_error",
1573*faefad9cSConrad Meyer 	    CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I",
1574*faefad9cSConrad Meyer 	    "Set to non-zero to inject a recoverable hardware error");
1575*faefad9cSConrad Meyer 	SYSCTL_ADD_PROC(ctx, par, OID_AUTO, "chansts",
1576*faefad9cSConrad Meyer 	    CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A",
1577*faefad9cSConrad Meyer 	    "String of the channel status");
1578e974f91cSConrad Meyer }
1579466b3540SConrad Meyer 
1580466b3540SConrad Meyer static inline struct ioat_softc *
1581466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1582466b3540SConrad Meyer {
1583466b3540SConrad Meyer 	uint32_t old;
1584466b3540SConrad Meyer 
1585466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1586466b3540SConrad Meyer 
1587466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, 1);
1588466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt overflow"));
1589466b3540SConrad Meyer 
1590466b3540SConrad Meyer #ifdef INVARIANTS
1591466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], 1);
1592466b3540SConrad Meyer 	KASSERT(old < UINT32_MAX, ("refcnt kind overflow"));
1593466b3540SConrad Meyer #endif
1594466b3540SConrad Meyer 
1595466b3540SConrad Meyer 	return (ioat);
1596466b3540SConrad Meyer }
1597466b3540SConrad Meyer 
1598466b3540SConrad Meyer static inline void
1599466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1600466b3540SConrad Meyer {
1601*faefad9cSConrad Meyer 
1602*faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, FALSE);
1603*faefad9cSConrad Meyer }
1604*faefad9cSConrad Meyer 
1605*faefad9cSConrad Meyer static inline void
1606*faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind)
1607*faefad9cSConrad Meyer {
1608*faefad9cSConrad Meyer 
1609*faefad9cSConrad Meyer 	_ioat_putn(ioat, n, kind, TRUE);
1610*faefad9cSConrad Meyer }
1611*faefad9cSConrad Meyer 
1612*faefad9cSConrad Meyer static inline void
1613*faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind,
1614*faefad9cSConrad Meyer     boolean_t locked)
1615*faefad9cSConrad Meyer {
1616466b3540SConrad Meyer 	uint32_t old;
1617466b3540SConrad Meyer 
1618466b3540SConrad Meyer 	KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus"));
1619466b3540SConrad Meyer 
1620466b3540SConrad Meyer 	if (n == 0)
1621466b3540SConrad Meyer 		return;
1622466b3540SConrad Meyer 
1623466b3540SConrad Meyer #ifdef INVARIANTS
1624466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refkinds[kind], -n);
1625466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt kind underflow"));
1626466b3540SConrad Meyer #endif
1627466b3540SConrad Meyer 
1628466b3540SConrad Meyer 	/* Skip acquiring the lock if resulting refcnt > 0. */
1629466b3540SConrad Meyer 	for (;;) {
1630466b3540SConrad Meyer 		old = ioat->refcnt;
1631466b3540SConrad Meyer 		if (old <= n)
1632466b3540SConrad Meyer 			break;
1633466b3540SConrad Meyer 		if (atomic_cmpset_32(&ioat->refcnt, old, old - n))
1634466b3540SConrad Meyer 			return;
1635466b3540SConrad Meyer 	}
1636466b3540SConrad Meyer 
1637*faefad9cSConrad Meyer 	if (locked)
1638*faefad9cSConrad Meyer 		mtx_assert(IOAT_REFLK, MA_OWNED);
1639*faefad9cSConrad Meyer 	else
1640466b3540SConrad Meyer 		mtx_lock(IOAT_REFLK);
1641*faefad9cSConrad Meyer 
1642466b3540SConrad Meyer 	old = atomic_fetchadd_32(&ioat->refcnt, -n);
1643466b3540SConrad Meyer 	KASSERT(old >= n, ("refcnt error"));
1644466b3540SConrad Meyer 
1645466b3540SConrad Meyer 	if (old == n)
1646466b3540SConrad Meyer 		wakeup(IOAT_REFLK);
1647*faefad9cSConrad Meyer 	if (!locked)
1648466b3540SConrad Meyer 		mtx_unlock(IOAT_REFLK);
1649466b3540SConrad Meyer }
1650466b3540SConrad Meyer 
1651466b3540SConrad Meyer static inline void
1652466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind)
1653466b3540SConrad Meyer {
1654466b3540SConrad Meyer 
1655466b3540SConrad Meyer 	ioat_putn(ioat, 1, kind);
1656466b3540SConrad Meyer }
1657466b3540SConrad Meyer 
1658466b3540SConrad Meyer static void
16595f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat)
1660466b3540SConrad Meyer {
1661466b3540SConrad Meyer 
16625f77bd3eSConrad Meyer 	mtx_assert(IOAT_REFLK, MA_OWNED);
1663466b3540SConrad Meyer 	while (ioat->refcnt > 0)
1664466b3540SConrad Meyer 		msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0);
1665466b3540SConrad Meyer }
1666