1e974f91cSConrad Meyer /*- 2e974f91cSConrad Meyer * Copyright (C) 2012 Intel Corporation 3e974f91cSConrad Meyer * All rights reserved. 4*2f03a95fSAlexander Motin * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org> 5e974f91cSConrad Meyer * 6e974f91cSConrad Meyer * Redistribution and use in source and binary forms, with or without 7e974f91cSConrad Meyer * modification, are permitted provided that the following conditions 8e974f91cSConrad Meyer * are met: 9e974f91cSConrad Meyer * 1. Redistributions of source code must retain the above copyright 10e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer. 11e974f91cSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 12e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer in the 13e974f91cSConrad Meyer * documentation and/or other materials provided with the distribution. 14e974f91cSConrad Meyer * 15e974f91cSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16e974f91cSConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17e974f91cSConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18e974f91cSConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19e974f91cSConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20e974f91cSConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21e974f91cSConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22e974f91cSConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23e974f91cSConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24e974f91cSConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25e974f91cSConrad Meyer * SUCH DAMAGE. 26e974f91cSConrad Meyer */ 27e974f91cSConrad Meyer 28e974f91cSConrad Meyer #include <sys/cdefs.h> 29e974f91cSConrad Meyer __FBSDID("$FreeBSD$"); 30e974f91cSConrad Meyer 310bb35debSConrad Meyer #include "opt_ddb.h" 320bb35debSConrad Meyer 33e974f91cSConrad Meyer #include <sys/param.h> 34e974f91cSConrad Meyer #include <sys/systm.h> 35e974f91cSConrad Meyer #include <sys/bus.h> 36e974f91cSConrad Meyer #include <sys/conf.h> 3776305bb8SConrad Meyer #include <sys/fail.h> 38e974f91cSConrad Meyer #include <sys/ioccom.h> 39e974f91cSConrad Meyer #include <sys/kernel.h> 40e974f91cSConrad Meyer #include <sys/lock.h> 41e974f91cSConrad Meyer #include <sys/malloc.h> 42e974f91cSConrad Meyer #include <sys/module.h> 43e974f91cSConrad Meyer #include <sys/mutex.h> 44e974f91cSConrad Meyer #include <sys/rman.h> 45faefad9cSConrad Meyer #include <sys/sbuf.h> 46e974f91cSConrad Meyer #include <sys/sysctl.h> 47374b05e1SConrad Meyer #include <sys/taskqueue.h> 48e974f91cSConrad Meyer #include <sys/time.h> 49e974f91cSConrad Meyer #include <dev/pci/pcireg.h> 50e974f91cSConrad Meyer #include <dev/pci/pcivar.h> 51e974f91cSConrad Meyer #include <machine/bus.h> 52e974f91cSConrad Meyer #include <machine/resource.h> 53e974f91cSConrad Meyer #include <machine/stdarg.h> 54e974f91cSConrad Meyer 550bb35debSConrad Meyer #ifdef DDB 560bb35debSConrad Meyer #include <ddb/ddb.h> 570bb35debSConrad Meyer #endif 580bb35debSConrad Meyer 59e974f91cSConrad Meyer #include "ioat.h" 60e974f91cSConrad Meyer #include "ioat_hw.h" 61e974f91cSConrad Meyer #include "ioat_internal.h" 62e974f91cSConrad Meyer 63519e8baaSConrad Meyer #ifndef BUS_SPACE_MAXADDR_40BIT 64519e8baaSConrad Meyer #define BUS_SPACE_MAXADDR_40BIT 0xFFFFFFFFFFULL 65519e8baaSConrad Meyer #endif 66fe720f5aSConrad Meyer 67e974f91cSConrad Meyer static int ioat_probe(device_t device); 68e974f91cSConrad Meyer static int ioat_attach(device_t device); 69e974f91cSConrad Meyer static int ioat_detach(device_t device); 704253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat); 714253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat); 72e974f91cSConrad Meyer static int ioat3_attach(device_t device); 73cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat); 74e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat); 75e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, 76e974f91cSConrad Meyer int error); 77e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg); 780d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat); 79faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t); 80*2f03a95fSAlexander Motin static void ioat_process_events(struct ioat_softc *ioat, boolean_t intr); 81e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat); 82e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat); 83bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size, 848e269d99SConrad Meyer struct ioat_descriptor *); 85bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags); 868e269d99SConrad Meyer static union ioat_hw_descriptor *ioat_get_descriptor(struct ioat_softc *, 878e269d99SConrad Meyer uint32_t index); 888e269d99SConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *, 89e974f91cSConrad Meyer uint32_t index); 90faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t); 915ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg); 92e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc); 93e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat); 94e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, 95e974f91cSConrad Meyer int error); 96e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat); 97374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int); 98e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device); 99f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS); 100*2f03a95fSAlexander Motin static void ioat_get(struct ioat_softc *); 101*2f03a95fSAlexander Motin static void ioat_put(struct ioat_softc *); 1025f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *); 103e974f91cSConrad Meyer 1041c25420eSConrad Meyer #define ioat_log_message(v, ...) do { \ 1051c25420eSConrad Meyer if ((v) <= g_ioat_debug_level) { \ 1061c25420eSConrad Meyer device_printf(ioat->device, __VA_ARGS__); \ 1071c25420eSConrad Meyer } \ 1081c25420eSConrad Meyer } while (0) 1091c25420eSConrad Meyer 110e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations"); 111e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node"); 112e974f91cSConrad Meyer 113e974f91cSConrad Meyer static int g_force_legacy_interrupts; 114e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN, 115e974f91cSConrad Meyer &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled"); 116e974f91cSConrad Meyer 1171c25420eSConrad Meyer int g_ioat_debug_level = 0; 118e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level, 119e974f91cSConrad Meyer 0, "Set log level (0-3) for ioat(4). Higher is more verbose."); 120e974f91cSConrad Meyer 121a0992979SConrad Meyer unsigned g_ioat_ring_order = 13; 122a0992979SConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ioat_ring_order, 123a0992979SConrad Meyer 0, "Set IOAT ring order. (1 << this) == ring size."); 124a0992979SConrad 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), 133aa853cb4SEnji Cooper DEVMETHOD_END 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]; 150df1928aaSConrad Meyer static unsigned ioat_channel_index = 0; 151df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0, 152e974f91cSConrad Meyer "Number of IOAT channels attached"); 153*2f03a95fSAlexander Motin static struct mtx ioat_list_mtx; 154*2f03a95fSAlexander Motin MTX_SYSINIT(ioat_list_mtx, &ioat_list_mtx, "ioat list mtx", MTX_DEF); 155e974f91cSConrad Meyer 156e974f91cSConrad Meyer static struct _pcsid 157e974f91cSConrad Meyer { 158e974f91cSConrad Meyer u_int32_t type; 159e974f91cSConrad Meyer const char *desc; 160e974f91cSConrad Meyer } pci_ids[] = { 161e974f91cSConrad Meyer { 0x34308086, "TBG IOAT Ch0" }, 162e974f91cSConrad Meyer { 0x34318086, "TBG IOAT Ch1" }, 163e974f91cSConrad Meyer { 0x34328086, "TBG IOAT Ch2" }, 164e974f91cSConrad Meyer { 0x34338086, "TBG IOAT Ch3" }, 165e974f91cSConrad Meyer { 0x34298086, "TBG IOAT Ch4" }, 166e974f91cSConrad Meyer { 0x342a8086, "TBG IOAT Ch5" }, 167e974f91cSConrad Meyer { 0x342b8086, "TBG IOAT Ch6" }, 168e974f91cSConrad Meyer { 0x342c8086, "TBG IOAT Ch7" }, 169e974f91cSConrad Meyer 170e974f91cSConrad Meyer { 0x37108086, "JSF IOAT Ch0" }, 171e974f91cSConrad Meyer { 0x37118086, "JSF IOAT Ch1" }, 172e974f91cSConrad Meyer { 0x37128086, "JSF IOAT Ch2" }, 173e974f91cSConrad Meyer { 0x37138086, "JSF IOAT Ch3" }, 174e974f91cSConrad Meyer { 0x37148086, "JSF IOAT Ch4" }, 175e974f91cSConrad Meyer { 0x37158086, "JSF IOAT Ch5" }, 176e974f91cSConrad Meyer { 0x37168086, "JSF IOAT Ch6" }, 177e974f91cSConrad Meyer { 0x37178086, "JSF IOAT Ch7" }, 178e974f91cSConrad Meyer { 0x37188086, "JSF IOAT Ch0 (RAID)" }, 179e974f91cSConrad Meyer { 0x37198086, "JSF IOAT Ch1 (RAID)" }, 180e974f91cSConrad Meyer 181e974f91cSConrad Meyer { 0x3c208086, "SNB IOAT Ch0" }, 182e974f91cSConrad Meyer { 0x3c218086, "SNB IOAT Ch1" }, 183e974f91cSConrad Meyer { 0x3c228086, "SNB IOAT Ch2" }, 184e974f91cSConrad Meyer { 0x3c238086, "SNB IOAT Ch3" }, 185e974f91cSConrad Meyer { 0x3c248086, "SNB IOAT Ch4" }, 186e974f91cSConrad Meyer { 0x3c258086, "SNB IOAT Ch5" }, 187e974f91cSConrad Meyer { 0x3c268086, "SNB IOAT Ch6" }, 188e974f91cSConrad Meyer { 0x3c278086, "SNB IOAT Ch7" }, 189e974f91cSConrad Meyer { 0x3c2e8086, "SNB IOAT Ch0 (RAID)" }, 190e974f91cSConrad Meyer { 0x3c2f8086, "SNB IOAT Ch1 (RAID)" }, 191e974f91cSConrad Meyer 192e974f91cSConrad Meyer { 0x0e208086, "IVB IOAT Ch0" }, 193e974f91cSConrad Meyer { 0x0e218086, "IVB IOAT Ch1" }, 194e974f91cSConrad Meyer { 0x0e228086, "IVB IOAT Ch2" }, 195e974f91cSConrad Meyer { 0x0e238086, "IVB IOAT Ch3" }, 196e974f91cSConrad Meyer { 0x0e248086, "IVB IOAT Ch4" }, 197e974f91cSConrad Meyer { 0x0e258086, "IVB IOAT Ch5" }, 198e974f91cSConrad Meyer { 0x0e268086, "IVB IOAT Ch6" }, 199e974f91cSConrad Meyer { 0x0e278086, "IVB IOAT Ch7" }, 200e974f91cSConrad Meyer { 0x0e2e8086, "IVB IOAT Ch0 (RAID)" }, 201e974f91cSConrad Meyer { 0x0e2f8086, "IVB IOAT Ch1 (RAID)" }, 202e974f91cSConrad Meyer 203e974f91cSConrad Meyer { 0x2f208086, "HSW IOAT Ch0" }, 204e974f91cSConrad Meyer { 0x2f218086, "HSW IOAT Ch1" }, 205e974f91cSConrad Meyer { 0x2f228086, "HSW IOAT Ch2" }, 206e974f91cSConrad Meyer { 0x2f238086, "HSW IOAT Ch3" }, 207e974f91cSConrad Meyer { 0x2f248086, "HSW IOAT Ch4" }, 208e974f91cSConrad Meyer { 0x2f258086, "HSW IOAT Ch5" }, 209e974f91cSConrad Meyer { 0x2f268086, "HSW IOAT Ch6" }, 210e974f91cSConrad Meyer { 0x2f278086, "HSW IOAT Ch7" }, 211e974f91cSConrad Meyer { 0x2f2e8086, "HSW IOAT Ch0 (RAID)" }, 212e974f91cSConrad Meyer { 0x2f2f8086, "HSW IOAT Ch1 (RAID)" }, 213e974f91cSConrad Meyer 214e974f91cSConrad Meyer { 0x0c508086, "BWD IOAT Ch0" }, 215e974f91cSConrad Meyer { 0x0c518086, "BWD IOAT Ch1" }, 216e974f91cSConrad Meyer { 0x0c528086, "BWD IOAT Ch2" }, 217e974f91cSConrad Meyer { 0x0c538086, "BWD IOAT Ch3" }, 218e974f91cSConrad Meyer 219e974f91cSConrad Meyer { 0x6f508086, "BDXDE IOAT Ch0" }, 220e974f91cSConrad Meyer { 0x6f518086, "BDXDE IOAT Ch1" }, 221e974f91cSConrad Meyer { 0x6f528086, "BDXDE IOAT Ch2" }, 222e974f91cSConrad Meyer { 0x6f538086, "BDXDE IOAT Ch3" }, 223e974f91cSConrad Meyer 2245afc2508SConrad Meyer { 0x6f208086, "BDX IOAT Ch0" }, 2255afc2508SConrad Meyer { 0x6f218086, "BDX IOAT Ch1" }, 2265afc2508SConrad Meyer { 0x6f228086, "BDX IOAT Ch2" }, 2275afc2508SConrad Meyer { 0x6f238086, "BDX IOAT Ch3" }, 2285afc2508SConrad Meyer { 0x6f248086, "BDX IOAT Ch4" }, 2295afc2508SConrad Meyer { 0x6f258086, "BDX IOAT Ch5" }, 2305afc2508SConrad Meyer { 0x6f268086, "BDX IOAT Ch6" }, 2315afc2508SConrad Meyer { 0x6f278086, "BDX IOAT Ch7" }, 2325afc2508SConrad Meyer { 0x6f2e8086, "BDX IOAT Ch0 (RAID)" }, 2335afc2508SConrad Meyer { 0x6f2f8086, "BDX IOAT Ch1 (RAID)" }, 23408f16d0cSConrad Meyer 23508f16d0cSConrad Meyer { 0x20218086, "SKX IOAT" }, 236e974f91cSConrad Meyer }; 237e974f91cSConrad Meyer 238d2064cf0SWarner Losh MODULE_PNP_INFO("W32:vendor/device;D:#", pci, ioat, pci_ids, 239329e817fSWarner Losh nitems(pci_ids)); 240a64bf59cSConrad Meyer 241e974f91cSConrad Meyer /* 242e974f91cSConrad Meyer * OS <-> Driver linkage functions 243e974f91cSConrad Meyer */ 244e974f91cSConrad Meyer static int 245e974f91cSConrad Meyer ioat_probe(device_t device) 246e974f91cSConrad Meyer { 247e974f91cSConrad Meyer struct _pcsid *ep; 248e974f91cSConrad Meyer u_int32_t type; 249e974f91cSConrad Meyer 250e974f91cSConrad Meyer type = pci_get_devid(device); 251a64bf59cSConrad Meyer for (ep = pci_ids; ep < &pci_ids[nitems(pci_ids)]; ep++) { 252e974f91cSConrad Meyer if (ep->type == type) { 253e974f91cSConrad Meyer device_set_desc(device, ep->desc); 254e974f91cSConrad Meyer return (0); 255e974f91cSConrad Meyer } 256e974f91cSConrad Meyer } 257e974f91cSConrad Meyer return (ENXIO); 258e974f91cSConrad Meyer } 259e974f91cSConrad Meyer 260e974f91cSConrad Meyer static int 261e974f91cSConrad Meyer ioat_attach(device_t device) 262e974f91cSConrad Meyer { 263e974f91cSConrad Meyer struct ioat_softc *ioat; 264*2f03a95fSAlexander Motin int error, i; 265e974f91cSConrad Meyer 266e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 267e974f91cSConrad Meyer ioat->device = device; 268e974f91cSConrad Meyer 269e974f91cSConrad Meyer error = ioat_map_pci_bar(ioat); 270e974f91cSConrad Meyer if (error != 0) 271e974f91cSConrad Meyer goto err; 272e974f91cSConrad Meyer 273e974f91cSConrad Meyer ioat->version = ioat_read_cbver(ioat); 274e974f91cSConrad Meyer if (ioat->version < IOAT_VER_3_0) { 275e974f91cSConrad Meyer error = ENODEV; 276e974f91cSConrad Meyer goto err; 277e974f91cSConrad Meyer } 278e974f91cSConrad Meyer 279e974f91cSConrad Meyer error = ioat3_attach(device); 280e974f91cSConrad Meyer if (error != 0) 281e974f91cSConrad Meyer goto err; 282e974f91cSConrad Meyer 283e974f91cSConrad Meyer error = pci_enable_busmaster(device); 284e974f91cSConrad Meyer if (error != 0) 285e974f91cSConrad Meyer goto err; 286e974f91cSConrad Meyer 287466b3540SConrad Meyer error = ioat_setup_intr(ioat); 288466b3540SConrad Meyer if (error != 0) 289466b3540SConrad Meyer goto err; 290466b3540SConrad Meyer 291cea5b880SConrad Meyer error = ioat_reset_hw(ioat); 2927afbb263SConrad Meyer if (error != 0) 293466b3540SConrad Meyer goto err; 2947afbb263SConrad Meyer 295*2f03a95fSAlexander Motin ioat_process_events(ioat, FALSE); 2967afbb263SConrad Meyer ioat_setup_sysctl(device); 2977afbb263SConrad Meyer 298*2f03a95fSAlexander Motin mtx_lock(&ioat_list_mtx); 299*2f03a95fSAlexander Motin for (i = 0; i < IOAT_MAX_CHANNELS; i++) { 300*2f03a95fSAlexander Motin if (ioat_channel[i] == NULL) 301*2f03a95fSAlexander Motin break; 302*2f03a95fSAlexander Motin } 303*2f03a95fSAlexander Motin if (i >= IOAT_MAX_CHANNELS) { 304*2f03a95fSAlexander Motin mtx_unlock(&ioat_list_mtx); 305*2f03a95fSAlexander Motin device_printf(device, "Too many I/OAT devices in system\n"); 306*2f03a95fSAlexander Motin error = ENXIO; 307*2f03a95fSAlexander Motin goto err; 308*2f03a95fSAlexander Motin } 309*2f03a95fSAlexander Motin ioat->chan_idx = i; 310*2f03a95fSAlexander Motin ioat_channel[i] = ioat; 311*2f03a95fSAlexander Motin if (i >= ioat_channel_index) 312*2f03a95fSAlexander Motin ioat_channel_index = i + 1; 313*2f03a95fSAlexander Motin mtx_unlock(&ioat_list_mtx); 314*2f03a95fSAlexander Motin 3157afbb263SConrad Meyer ioat_test_attach(); 316e974f91cSConrad Meyer 317e974f91cSConrad Meyer err: 318e974f91cSConrad Meyer if (error != 0) 319e974f91cSConrad Meyer ioat_detach(device); 320e974f91cSConrad Meyer return (error); 321e974f91cSConrad Meyer } 322e974f91cSConrad Meyer 323e974f91cSConrad Meyer static int 324e974f91cSConrad Meyer ioat_detach(device_t device) 325e974f91cSConrad Meyer { 326e974f91cSConrad Meyer struct ioat_softc *ioat; 327e974f91cSConrad Meyer 328e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3297afbb263SConrad Meyer 330*2f03a95fSAlexander Motin mtx_lock(&ioat_list_mtx); 331*2f03a95fSAlexander Motin ioat_channel[ioat->chan_idx] = NULL; 332*2f03a95fSAlexander Motin while (ioat_channel_index > 0 && 333*2f03a95fSAlexander Motin ioat_channel[ioat_channel_index - 1] == NULL) 334*2f03a95fSAlexander Motin ioat_channel_index--; 335*2f03a95fSAlexander Motin mtx_unlock(&ioat_list_mtx); 336*2f03a95fSAlexander Motin 3377afbb263SConrad Meyer ioat_test_detach(); 338374b05e1SConrad Meyer taskqueue_drain(taskqueue_thread, &ioat->reset_task); 3395f77bd3eSConrad Meyer 340*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 3415f77bd3eSConrad Meyer ioat->quiescing = TRUE; 3420ff814e8SConrad Meyer ioat->destroying = TRUE; 3430ff814e8SConrad Meyer wakeup(&ioat->quiescing); 34493f7f84aSConrad Meyer wakeup(&ioat->resetting); 3450ff814e8SConrad Meyer 3465f77bd3eSConrad Meyer ioat_drain_locked(ioat); 347*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 348*2f03a95fSAlexander Motin mtx_lock(&ioat->cleanup_lock); 349*2f03a95fSAlexander Motin while (ioat_get_active(ioat) > 0) 350*2f03a95fSAlexander Motin msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1); 351*2f03a95fSAlexander Motin mtx_unlock(&ioat->cleanup_lock); 352fe720f5aSConrad Meyer 353fe720f5aSConrad Meyer ioat_teardown_intr(ioat); 3545ac77963SConrad Meyer callout_drain(&ioat->poll_timer); 355e974f91cSConrad Meyer 356e974f91cSConrad Meyer pci_disable_busmaster(device); 357e974f91cSConrad Meyer 358e974f91cSConrad Meyer if (ioat->pci_resource != NULL) 359e974f91cSConrad Meyer bus_release_resource(device, SYS_RES_MEMORY, 360e974f91cSConrad Meyer ioat->pci_resource_id, ioat->pci_resource); 361e974f91cSConrad Meyer 362bf8553eaSConrad Meyer if (ioat->ring != NULL) 363bf8553eaSConrad Meyer ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring); 364e974f91cSConrad Meyer 365e974f91cSConrad Meyer if (ioat->comp_update != NULL) { 366e974f91cSConrad Meyer bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map); 367e974f91cSConrad Meyer bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update, 368e974f91cSConrad Meyer ioat->comp_update_map); 369e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->comp_update_tag); 370e974f91cSConrad Meyer } 371e974f91cSConrad Meyer 3728e269d99SConrad Meyer if (ioat->hw_desc_ring != NULL) { 3738e269d99SConrad Meyer bus_dmamap_unload(ioat->hw_desc_tag, ioat->hw_desc_map); 3748e269d99SConrad Meyer bus_dmamem_free(ioat->hw_desc_tag, ioat->hw_desc_ring, 3758e269d99SConrad Meyer ioat->hw_desc_map); 376e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->hw_desc_tag); 3778e269d99SConrad Meyer } 378e974f91cSConrad Meyer 3794253ea50SConrad Meyer return (0); 3804253ea50SConrad Meyer } 3814253ea50SConrad Meyer 3824253ea50SConrad Meyer static int 3834253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat) 3844253ea50SConrad Meyer { 3854253ea50SConrad Meyer 386e974f91cSConrad Meyer if (ioat->tag != NULL) 3874253ea50SConrad Meyer bus_teardown_intr(ioat->device, ioat->res, ioat->tag); 388e974f91cSConrad Meyer 389e974f91cSConrad Meyer if (ioat->res != NULL) 3904253ea50SConrad Meyer bus_release_resource(ioat->device, SYS_RES_IRQ, 391e974f91cSConrad Meyer rman_get_rid(ioat->res), ioat->res); 392e974f91cSConrad Meyer 3934253ea50SConrad Meyer pci_release_msi(ioat->device); 394e974f91cSConrad Meyer return (0); 395e974f91cSConrad Meyer } 396e974f91cSConrad Meyer 397e974f91cSConrad Meyer static int 398cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat) 399e974f91cSConrad Meyer { 400fe8712f8SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 401fe8712f8SConrad Meyer struct ioat_descriptor *desc; 402fe8712f8SConrad Meyer struct bus_dmadesc *dmadesc; 403e974f91cSConrad Meyer uint64_t status; 404e974f91cSConrad Meyer uint32_t chanerr; 405e974f91cSConrad Meyer int i; 406e974f91cSConrad Meyer 407e974f91cSConrad Meyer ioat_acquire(&ioat->dmaengine); 408fe8712f8SConrad Meyer 409fe8712f8SConrad Meyer /* Submit 'NULL' operation manually to avoid quiescing flag */ 410fe8712f8SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 4118e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, ioat->head)->dma; 412fe8712f8SConrad Meyer dmadesc = &desc->bus_dmadesc; 413fe8712f8SConrad Meyer 414fe8712f8SConrad Meyer dmadesc->callback_fn = NULL; 415fe8712f8SConrad Meyer dmadesc->callback_arg = NULL; 416fe8712f8SConrad Meyer 417fe8712f8SConrad Meyer hw_desc->u.control_raw = 0; 418fe8712f8SConrad Meyer hw_desc->u.control_generic.op = IOAT_OP_COPY; 419fe8712f8SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 420fe8712f8SConrad Meyer hw_desc->size = 8; 421fe8712f8SConrad Meyer hw_desc->src_addr = 0; 422fe8712f8SConrad Meyer hw_desc->dest_addr = 0; 423fe8712f8SConrad Meyer hw_desc->u.control.null = 1; 424fe8712f8SConrad Meyer 425fe8712f8SConrad Meyer ioat_submit_single(ioat); 426e974f91cSConrad Meyer ioat_release(&ioat->dmaengine); 427e974f91cSConrad Meyer 428e974f91cSConrad Meyer for (i = 0; i < 100; i++) { 429e974f91cSConrad Meyer DELAY(1); 430e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 431e974f91cSConrad Meyer if (is_ioat_idle(status)) 432e974f91cSConrad Meyer return (0); 433e974f91cSConrad Meyer } 434e974f91cSConrad Meyer 435e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 436e974f91cSConrad Meyer ioat_log_message(0, "could not start channel: " 43759acd4baSConrad Meyer "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr, 43859acd4baSConrad Meyer IOAT_CHANERR_STR); 439e974f91cSConrad Meyer return (ENXIO); 440e974f91cSConrad Meyer } 441e974f91cSConrad Meyer 442e974f91cSConrad Meyer /* 443e974f91cSConrad Meyer * Initialize Hardware 444e974f91cSConrad Meyer */ 445e974f91cSConrad Meyer static int 446e974f91cSConrad Meyer ioat3_attach(device_t device) 447e974f91cSConrad Meyer { 448e974f91cSConrad Meyer struct ioat_softc *ioat; 4498e269d99SConrad Meyer struct ioat_descriptor *ring; 450e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *dma_hw_desc; 4518e269d99SConrad Meyer void *hw_desc; 4528e269d99SConrad Meyer size_t ringsz; 453e974f91cSConrad Meyer int i, num_descriptors; 454e974f91cSConrad Meyer int error; 455e974f91cSConrad Meyer uint8_t xfercap; 456e974f91cSConrad Meyer 457e974f91cSConrad Meyer error = 0; 458e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 4591693d27bSConrad Meyer ioat->capabilities = ioat_read_dmacapability(ioat); 4601693d27bSConrad Meyer 46148fed014SConrad Meyer ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities, 4621693d27bSConrad Meyer IOAT_DMACAP_STR); 463e974f91cSConrad Meyer 464e974f91cSConrad Meyer xfercap = ioat_read_xfercap(ioat); 465e974f91cSConrad Meyer ioat->max_xfer_size = 1 << xfercap; 466e974f91cSConrad Meyer 4675ca9fc2aSConrad Meyer ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & 4685ca9fc2aSConrad Meyer IOAT_INTRDELAY_SUPPORTED) != 0; 4695ca9fc2aSConrad Meyer if (ioat->intrdelay_supported) 4705ca9fc2aSConrad Meyer ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK; 4715ca9fc2aSConrad Meyer 472e974f91cSConrad Meyer /* TODO: need to check DCA here if we ever do XOR/PQ */ 473e974f91cSConrad Meyer 474e974f91cSConrad Meyer mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF); 475faefad9cSConrad Meyer mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF); 4765ac77963SConrad Meyer callout_init(&ioat->poll_timer, 1); 477374b05e1SConrad Meyer TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat); 478e974f91cSConrad Meyer 479faefad9cSConrad Meyer /* Establish lock order for Witness */ 480faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 481*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 482faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 483*2f03a95fSAlexander Motin mtx_unlock(&ioat->cleanup_lock); 484faefad9cSConrad Meyer 48525ad9585SConrad Meyer ioat->is_submitter_processing = FALSE; 486e974f91cSConrad Meyer 487e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0, 488e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 489e974f91cSConrad Meyer sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL, 490e974f91cSConrad Meyer &ioat->comp_update_tag); 491e974f91cSConrad Meyer 492e974f91cSConrad Meyer error = bus_dmamem_alloc(ioat->comp_update_tag, 493e974f91cSConrad Meyer (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map); 494e974f91cSConrad Meyer if (ioat->comp_update == NULL) 495e974f91cSConrad Meyer return (ENOMEM); 496e974f91cSConrad Meyer 497e974f91cSConrad Meyer error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map, 498e974f91cSConrad Meyer ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat, 499e974f91cSConrad Meyer 0); 500e974f91cSConrad Meyer if (error != 0) 501e974f91cSConrad Meyer return (error); 502e974f91cSConrad Meyer 503a0992979SConrad Meyer ioat->ring_size_order = g_ioat_ring_order; 504e974f91cSConrad Meyer num_descriptors = 1 << ioat->ring_size_order; 5058e269d99SConrad Meyer ringsz = sizeof(struct ioat_dma_hw_descriptor) * num_descriptors; 506e974f91cSConrad Meyer 5078e269d99SConrad Meyer error = bus_dma_tag_create(bus_get_dma_tag(ioat->device), 508e2a65c00SConrad Meyer 2 * 1024 * 1024, 0x0, (bus_addr_t)BUS_SPACE_MAXADDR_40BIT, 509e2a65c00SConrad Meyer BUS_SPACE_MAXADDR, NULL, NULL, ringsz, 1, ringsz, 0, NULL, NULL, 510e2a65c00SConrad Meyer &ioat->hw_desc_tag); 5118e269d99SConrad Meyer if (error != 0) 5128e269d99SConrad Meyer return (error); 5138e269d99SConrad Meyer 5148e269d99SConrad Meyer error = bus_dmamem_alloc(ioat->hw_desc_tag, &hw_desc, 5158e269d99SConrad Meyer BUS_DMA_ZERO | BUS_DMA_WAITOK, &ioat->hw_desc_map); 5168e269d99SConrad Meyer if (error != 0) 5178e269d99SConrad Meyer return (error); 5188e269d99SConrad Meyer 5198e269d99SConrad Meyer error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc, 5208e269d99SConrad Meyer ringsz, ioat_dmamap_cb, &ioat->hw_desc_bus_addr, BUS_DMA_WAITOK); 5218e269d99SConrad Meyer if (error) 5228e269d99SConrad Meyer return (error); 5238e269d99SConrad Meyer 5248e269d99SConrad Meyer ioat->hw_desc_ring = hw_desc; 525e974f91cSConrad Meyer 526e974f91cSConrad Meyer ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT, 527bf8553eaSConrad Meyer M_ZERO | M_WAITOK); 528e974f91cSConrad Meyer 529e974f91cSConrad Meyer ring = ioat->ring; 530e974f91cSConrad Meyer for (i = 0; i < num_descriptors; i++) { 5318e269d99SConrad Meyer memset(&ring[i].bus_dmadesc, 0, sizeof(ring[i].bus_dmadesc)); 5328e269d99SConrad Meyer ring[i].id = i; 533e974f91cSConrad Meyer } 534e974f91cSConrad Meyer 5358e269d99SConrad Meyer for (i = 0; i < num_descriptors; i++) { 5368e269d99SConrad Meyer dma_hw_desc = &ioat->hw_desc_ring[i].dma; 5378e269d99SConrad Meyer dma_hw_desc->next = RING_PHYS_ADDR(ioat, i + 1); 538e974f91cSConrad Meyer } 539e974f91cSConrad Meyer 540*2f03a95fSAlexander Motin ioat->head = 0; 541e974f91cSConrad Meyer ioat->tail = 0; 542e974f91cSConrad Meyer ioat->last_seen = 0; 543fe8712f8SConrad Meyer *ioat->comp_update = 0; 544e974f91cSConrad Meyer return (0); 545e974f91cSConrad Meyer } 546e974f91cSConrad Meyer 547e974f91cSConrad Meyer static int 548e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat) 549e974f91cSConrad Meyer { 550e974f91cSConrad Meyer 551e974f91cSConrad Meyer ioat->pci_resource_id = PCIR_BAR(0); 552e88e14b9SConrad Meyer ioat->pci_resource = bus_alloc_resource_any(ioat->device, 553e88e14b9SConrad Meyer SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE); 554e974f91cSConrad Meyer 555e974f91cSConrad Meyer if (ioat->pci_resource == NULL) { 556e974f91cSConrad Meyer ioat_log_message(0, "unable to allocate pci resource\n"); 557e974f91cSConrad Meyer return (ENODEV); 558e974f91cSConrad Meyer } 559e974f91cSConrad Meyer 560e974f91cSConrad Meyer ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource); 561e974f91cSConrad Meyer ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource); 562e974f91cSConrad Meyer return (0); 563e974f91cSConrad Meyer } 564e974f91cSConrad Meyer 565e974f91cSConrad Meyer static void 566e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 567e974f91cSConrad Meyer { 568e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 569e974f91cSConrad Meyer 570cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 571e974f91cSConrad Meyer ioat->comp_update_bus_addr = seg[0].ds_addr; 572e974f91cSConrad Meyer } 573e974f91cSConrad Meyer 574e974f91cSConrad Meyer static void 575e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 576e974f91cSConrad Meyer { 577e974f91cSConrad Meyer bus_addr_t *baddr; 578e974f91cSConrad Meyer 579cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 580e974f91cSConrad Meyer baddr = arg; 581e974f91cSConrad Meyer *baddr = segs->ds_addr; 582e974f91cSConrad Meyer } 583e974f91cSConrad Meyer 584e974f91cSConrad Meyer /* 585e974f91cSConrad Meyer * Interrupt setup and handlers 586e974f91cSConrad Meyer */ 587e974f91cSConrad Meyer static int 5884253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat) 589e974f91cSConrad Meyer { 590e974f91cSConrad Meyer uint32_t num_vectors; 591e974f91cSConrad Meyer int error; 592e974f91cSConrad Meyer boolean_t use_msix; 593e974f91cSConrad Meyer boolean_t force_legacy_interrupts; 594e974f91cSConrad Meyer 595e974f91cSConrad Meyer use_msix = FALSE; 596e974f91cSConrad Meyer force_legacy_interrupts = FALSE; 597e974f91cSConrad Meyer 598e974f91cSConrad Meyer if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) { 599e974f91cSConrad Meyer num_vectors = 1; 600e974f91cSConrad Meyer pci_alloc_msix(ioat->device, &num_vectors); 601e974f91cSConrad Meyer if (num_vectors == 1) 602e974f91cSConrad Meyer use_msix = TRUE; 603e974f91cSConrad Meyer } 604e974f91cSConrad Meyer 605e974f91cSConrad Meyer if (use_msix) { 606e974f91cSConrad Meyer ioat->rid = 1; 607e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 608e974f91cSConrad Meyer &ioat->rid, RF_ACTIVE); 609e974f91cSConrad Meyer } else { 610e974f91cSConrad Meyer ioat->rid = 0; 611e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 612e974f91cSConrad Meyer &ioat->rid, RF_SHAREABLE | RF_ACTIVE); 613e974f91cSConrad Meyer } 614e974f91cSConrad Meyer if (ioat->res == NULL) { 615e974f91cSConrad Meyer ioat_log_message(0, "bus_alloc_resource failed\n"); 616e974f91cSConrad Meyer return (ENOMEM); 617e974f91cSConrad Meyer } 618e974f91cSConrad Meyer 619e974f91cSConrad Meyer ioat->tag = NULL; 620e974f91cSConrad Meyer error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE | 621e974f91cSConrad Meyer INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag); 622e974f91cSConrad Meyer if (error != 0) { 623e974f91cSConrad Meyer ioat_log_message(0, "bus_setup_intr failed\n"); 624e974f91cSConrad Meyer return (error); 625e974f91cSConrad Meyer } 626e974f91cSConrad Meyer 627e974f91cSConrad Meyer ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN); 628e974f91cSConrad Meyer return (0); 629e974f91cSConrad Meyer } 630e974f91cSConrad Meyer 6314253ea50SConrad Meyer static boolean_t 6320d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat) 6334253ea50SConrad Meyer { 6344253ea50SConrad Meyer u_int32_t pciid; 6354253ea50SConrad Meyer 6364253ea50SConrad Meyer pciid = pci_get_devid(ioat->device); 6374253ea50SConrad Meyer switch (pciid) { 6380d1a05d9SConrad Meyer /* BWD: */ 6390d1a05d9SConrad Meyer case 0x0c508086: 6400d1a05d9SConrad Meyer case 0x0c518086: 6410d1a05d9SConrad Meyer case 0x0c528086: 6420d1a05d9SConrad Meyer case 0x0c538086: 6430d1a05d9SConrad Meyer /* BDXDE: */ 6444253ea50SConrad Meyer case 0x6f508086: 6454253ea50SConrad Meyer case 0x6f518086: 6464253ea50SConrad Meyer case 0x6f528086: 6474253ea50SConrad Meyer case 0x6f538086: 6484253ea50SConrad Meyer return (TRUE); 6494253ea50SConrad Meyer } 6504253ea50SConrad Meyer 6514253ea50SConrad Meyer return (FALSE); 6524253ea50SConrad Meyer } 6534253ea50SConrad Meyer 654e974f91cSConrad Meyer static void 655e974f91cSConrad Meyer ioat_interrupt_handler(void *arg) 656e974f91cSConrad Meyer { 657e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 658e974f91cSConrad Meyer 65901fbbc88SConrad Meyer ioat->stats.interrupts++; 660*2f03a95fSAlexander Motin ioat_process_events(ioat, TRUE); 661e974f91cSConrad Meyer } 662e974f91cSConrad Meyer 663faefad9cSConrad Meyer static int 664faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr) 665faefad9cSConrad Meyer { 666faefad9cSConrad Meyer 667faefad9cSConrad Meyer if (chanerr == 0) 668faefad9cSConrad Meyer return (0); 669faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0) 670faefad9cSConrad Meyer return (EFAULT); 671faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0) 672faefad9cSConrad Meyer return (EIO); 673faefad9cSConrad Meyer /* This one is probably our fault: */ 674faefad9cSConrad Meyer if ((chanerr & IOAT_CHANERR_NDADDERR) != 0) 675faefad9cSConrad Meyer return (EIO); 676faefad9cSConrad Meyer return (EIO); 677faefad9cSConrad Meyer } 678faefad9cSConrad Meyer 679e974f91cSConrad Meyer static void 680*2f03a95fSAlexander Motin ioat_process_events(struct ioat_softc *ioat, boolean_t intr) 681e974f91cSConrad Meyer { 682e974f91cSConrad Meyer struct ioat_descriptor *desc; 683e974f91cSConrad Meyer struct bus_dmadesc *dmadesc; 684e974f91cSConrad Meyer uint64_t comp_update, status; 685faefad9cSConrad Meyer uint32_t completed, chanerr; 686faefad9cSConrad Meyer int error; 687e974f91cSConrad Meyer 688e974f91cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 689e974f91cSConrad Meyer 690fe8712f8SConrad Meyer /* 691fe8712f8SConrad Meyer * Don't run while the hardware is being reset. Reset is responsible 692fe8712f8SConrad Meyer * for blocking new work and draining & completing existing work, so 693fe8712f8SConrad Meyer * there is nothing to do until new work is queued after reset anyway. 694fe8712f8SConrad Meyer */ 695fe8712f8SConrad Meyer if (ioat->resetting_cleanup) { 696fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 697fe8712f8SConrad Meyer return; 698fe8712f8SConrad Meyer } 699fe8712f8SConrad Meyer 700e974f91cSConrad Meyer completed = 0; 7010d0f2640SConrad Meyer comp_update = *ioat->comp_update; 702e974f91cSConrad Meyer status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK; 703e974f91cSConrad Meyer 7048e269d99SConrad Meyer if (status < ioat->hw_desc_bus_addr || 7058e269d99SConrad Meyer status >= ioat->hw_desc_bus_addr + (1 << ioat->ring_size_order) * 7068e269d99SConrad Meyer sizeof(struct ioat_generic_hw_descriptor)) 7078e269d99SConrad Meyer panic("Bogus completion address %jx (channel %u)", 7088e269d99SConrad Meyer (uintmax_t)status, ioat->chan_idx); 7098e269d99SConrad Meyer 7101bbc06b8SConrad Meyer if (status == ioat->last_seen) { 7111bbc06b8SConrad Meyer /* 7121bbc06b8SConrad Meyer * If we landed in process_events and nothing has been 7131bbc06b8SConrad Meyer * completed, check for a timeout due to channel halt. 7141bbc06b8SConrad Meyer */ 7151bbc06b8SConrad Meyer goto out; 7161bbc06b8SConrad Meyer } 717dc465059SConrad Meyer CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx", 718dc465059SConrad Meyer __func__, ioat->chan_idx, comp_update, ioat->last_seen); 7191bbc06b8SConrad Meyer 7208e269d99SConrad Meyer while (RING_PHYS_ADDR(ioat, ioat->tail - 1) != status) { 721e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 722e974f91cSConrad Meyer dmadesc = &desc->bus_dmadesc; 72305e4cffbSConrad Meyer CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok cb %p(%p)", 72405e4cffbSConrad Meyer ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn, 725520f6023SConrad Meyer dmadesc->callback_arg); 726e974f91cSConrad Meyer 727faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 728faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 0); 729e974f91cSConrad Meyer 730466b3540SConrad Meyer completed++; 731e974f91cSConrad Meyer ioat->tail++; 732e974f91cSConrad Meyer } 73305e4cffbSConrad Meyer CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__, 73405e4cffbSConrad Meyer ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat)); 735e974f91cSConrad Meyer 7360283c0f5SConrad Meyer if (completed != 0) { 7378e269d99SConrad Meyer ioat->last_seen = RING_PHYS_ADDR(ioat, ioat->tail - 1); 73801fbbc88SConrad Meyer ioat->stats.descriptors_processed += completed; 739*2f03a95fSAlexander Motin wakeup(&ioat->tail); 7400283c0f5SConrad Meyer } 74101fbbc88SConrad Meyer 7421bbc06b8SConrad Meyer out: 743e974f91cSConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 744e974f91cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 745faefad9cSConrad Meyer 746ea9e23edSConrad Meyer /* 747ea9e23edSConrad Meyer * The device doesn't seem to reliably push suspend/halt statuses to 748ea9e23edSConrad Meyer * the channel completion memory address, so poll the device register 749*2f03a95fSAlexander Motin * here. For performance reasons skip it on interrupts, do it only 750*2f03a95fSAlexander Motin * on much more rare polling events. 751ea9e23edSConrad Meyer */ 752*2f03a95fSAlexander Motin if (!intr) 753ea9e23edSConrad Meyer comp_update = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS; 754d2c55e5aSConrad Meyer if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update)) 755faefad9cSConrad Meyer return; 756faefad9cSConrad Meyer 75701fbbc88SConrad Meyer ioat->stats.channel_halts++; 75801fbbc88SConrad Meyer 759faefad9cSConrad Meyer /* 760faefad9cSConrad Meyer * Fatal programming error on this DMA channel. Flush any outstanding 761faefad9cSConrad Meyer * work with error status and restart the engine. 762faefad9cSConrad Meyer */ 763faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 764faefad9cSConrad Meyer ioat->quiescing = TRUE; 765*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 766*2f03a95fSAlexander Motin 7673a370919SConrad Meyer /* 768*2f03a95fSAlexander Motin * This is safe to do here because the submit queue is quiesced. We 769*2f03a95fSAlexander Motin * know that we will drain all outstanding events, so ioat_reset_hw 770*2f03a95fSAlexander Motin * can't deadlock. It is necessary to protect other ioat_process_event 771*2f03a95fSAlexander Motin * threads from racing ioat_reset_hw, reading an indeterminate hw 772*2f03a95fSAlexander Motin * state, and attempting to continue issuing completions. 7733a370919SConrad Meyer */ 774*2f03a95fSAlexander Motin mtx_lock(&ioat->cleanup_lock); 7753a370919SConrad Meyer ioat->resetting_cleanup = TRUE; 776faefad9cSConrad Meyer 777faefad9cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 778985efa77SConrad Meyer if (1 <= g_ioat_debug_level) 779faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 78001fbbc88SConrad Meyer ioat->stats.last_halt_chanerr = chanerr; 781faefad9cSConrad Meyer 782faefad9cSConrad Meyer while (ioat_get_active(ioat) > 0) { 783faefad9cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 784faefad9cSConrad Meyer dmadesc = &desc->bus_dmadesc; 78505e4cffbSConrad Meyer CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)", 78605e4cffbSConrad Meyer ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn, 787520f6023SConrad Meyer dmadesc->callback_arg); 788faefad9cSConrad Meyer 789faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 790faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 791faefad9cSConrad Meyer chanerr_to_errno(chanerr)); 792faefad9cSConrad Meyer 793faefad9cSConrad Meyer ioat->tail++; 79401fbbc88SConrad Meyer ioat->stats.descriptors_processed++; 79501fbbc88SConrad Meyer ioat->stats.descriptors_error++; 796faefad9cSConrad Meyer } 79705e4cffbSConrad Meyer CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__, 79805e4cffbSConrad Meyer ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat)); 799faefad9cSConrad Meyer 800faefad9cSConrad Meyer /* Clear error status */ 801faefad9cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 802faefad9cSConrad Meyer 803faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 804faefad9cSConrad Meyer 805faefad9cSConrad Meyer ioat_log_message(0, "Resetting channel to recover from error\n"); 806374b05e1SConrad Meyer error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task); 807374b05e1SConrad Meyer KASSERT(error == 0, 808374b05e1SConrad Meyer ("%s: taskqueue_enqueue failed: %d", __func__, error)); 809374b05e1SConrad Meyer } 810374b05e1SConrad Meyer 811374b05e1SConrad Meyer static void 812374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused) 813374b05e1SConrad Meyer { 814374b05e1SConrad Meyer struct ioat_softc *ioat; 815374b05e1SConrad Meyer int error; 816374b05e1SConrad Meyer 817374b05e1SConrad Meyer ioat = ctx; 818374b05e1SConrad Meyer ioat_log_message(1, "%s: Resetting channel\n", __func__); 819374b05e1SConrad Meyer 820faefad9cSConrad Meyer error = ioat_reset_hw(ioat); 821faefad9cSConrad Meyer KASSERT(error == 0, ("%s: reset failed: %d", __func__, error)); 822374b05e1SConrad Meyer (void)error; 823e974f91cSConrad Meyer } 824e974f91cSConrad Meyer 825e974f91cSConrad Meyer /* 826e974f91cSConrad Meyer * User API functions 827e974f91cSConrad Meyer */ 828f8f92e91SConrad Meyer unsigned 829f8f92e91SConrad Meyer ioat_get_nchannels(void) 830f8f92e91SConrad Meyer { 831f8f92e91SConrad Meyer 832f8f92e91SConrad Meyer return (ioat_channel_index); 833f8f92e91SConrad Meyer } 834f8f92e91SConrad Meyer 835e974f91cSConrad Meyer bus_dmaengine_t 8360ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags) 837e974f91cSConrad Meyer { 8380ff814e8SConrad Meyer struct ioat_softc *ioat; 8390ff814e8SConrad Meyer 8400ff814e8SConrad Meyer KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0, 8410ff814e8SConrad Meyer ("invalid flags: 0x%08x", flags)); 8420ff814e8SConrad Meyer KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK), 8430ff814e8SConrad Meyer ("invalid wait | nowait")); 844e974f91cSConrad Meyer 845*2f03a95fSAlexander Motin mtx_lock(&ioat_list_mtx); 846*2f03a95fSAlexander Motin if (index >= ioat_channel_index || 847*2f03a95fSAlexander Motin (ioat = ioat_channel[index]) == NULL) { 848*2f03a95fSAlexander Motin mtx_unlock(&ioat_list_mtx); 849e974f91cSConrad Meyer return (NULL); 850*2f03a95fSAlexander Motin } 851*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 852*2f03a95fSAlexander Motin mtx_unlock(&ioat_list_mtx); 8535f77bd3eSConrad Meyer 854*2f03a95fSAlexander Motin if (ioat->destroying) { 855*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 8560ff814e8SConrad Meyer return (NULL); 8570ff814e8SConrad Meyer } 8580ff814e8SConrad Meyer 859*2f03a95fSAlexander Motin ioat_get(ioat); 860*2f03a95fSAlexander Motin if (ioat->quiescing) { 861*2f03a95fSAlexander Motin if ((flags & M_NOWAIT) != 0) { 862*2f03a95fSAlexander Motin ioat_put(ioat); 863*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 864*2f03a95fSAlexander Motin return (NULL); 865*2f03a95fSAlexander Motin } 866*2f03a95fSAlexander Motin 867*2f03a95fSAlexander Motin while (ioat->quiescing && !ioat->destroying) 868*2f03a95fSAlexander Motin msleep(&ioat->quiescing, &ioat->submit_lock, 0, "getdma", 0); 869*2f03a95fSAlexander Motin 870*2f03a95fSAlexander Motin if (ioat->destroying) { 871*2f03a95fSAlexander Motin ioat_put(ioat); 872*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 873*2f03a95fSAlexander Motin return (NULL); 874*2f03a95fSAlexander Motin } 875*2f03a95fSAlexander Motin } 876*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 877*2f03a95fSAlexander Motin return (&ioat->dmaengine); 878466b3540SConrad Meyer } 879466b3540SConrad Meyer 880466b3540SConrad Meyer void 881466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine) 882466b3540SConrad Meyer { 883466b3540SConrad Meyer struct ioat_softc *ioat; 884466b3540SConrad Meyer 885466b3540SConrad Meyer ioat = to_ioat_softc(dmaengine); 886*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 887*2f03a95fSAlexander Motin ioat_put(ioat); 888*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 889e974f91cSConrad Meyer } 890e974f91cSConrad Meyer 8915ca9fc2aSConrad Meyer int 89231bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine) 89331bf2875SConrad Meyer { 89431bf2875SConrad Meyer struct ioat_softc *ioat; 89531bf2875SConrad Meyer 89631bf2875SConrad Meyer ioat = to_ioat_softc(dmaengine); 89731bf2875SConrad Meyer return (ioat->version); 89831bf2875SConrad Meyer } 89931bf2875SConrad Meyer 900bd81fe68SConrad Meyer size_t 901bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine) 902bd81fe68SConrad Meyer { 903bd81fe68SConrad Meyer struct ioat_softc *ioat; 904bd81fe68SConrad Meyer 905bd81fe68SConrad Meyer ioat = to_ioat_softc(dmaengine); 906bd81fe68SConrad Meyer return (ioat->max_xfer_size); 907bd81fe68SConrad Meyer } 908bd81fe68SConrad Meyer 909f8253f1aSConrad Meyer uint32_t 910f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine) 911f8253f1aSConrad Meyer { 912f8253f1aSConrad Meyer struct ioat_softc *ioat; 913f8253f1aSConrad Meyer 914f8253f1aSConrad Meyer ioat = to_ioat_softc(dmaengine); 915f8253f1aSConrad Meyer return (ioat->capabilities); 916f8253f1aSConrad Meyer } 917f8253f1aSConrad Meyer 91831bf2875SConrad Meyer int 9195ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay) 9205ca9fc2aSConrad Meyer { 9215ca9fc2aSConrad Meyer struct ioat_softc *ioat; 9225ca9fc2aSConrad Meyer 9235ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 9245ca9fc2aSConrad Meyer if (!ioat->intrdelay_supported) 9255ca9fc2aSConrad Meyer return (ENODEV); 9265ca9fc2aSConrad Meyer if (delay > ioat->intrdelay_max) 9275ca9fc2aSConrad Meyer return (ERANGE); 9285ca9fc2aSConrad Meyer 9295ca9fc2aSConrad Meyer ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay); 9305ca9fc2aSConrad Meyer ioat->cached_intrdelay = 9315ca9fc2aSConrad Meyer ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK; 9325ca9fc2aSConrad Meyer return (0); 9335ca9fc2aSConrad Meyer } 9345ca9fc2aSConrad Meyer 9355ca9fc2aSConrad Meyer uint16_t 9365ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine) 9375ca9fc2aSConrad Meyer { 9385ca9fc2aSConrad Meyer struct ioat_softc *ioat; 9395ca9fc2aSConrad Meyer 9405ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 9415ca9fc2aSConrad Meyer return (ioat->intrdelay_max); 9425ca9fc2aSConrad Meyer } 9435ca9fc2aSConrad Meyer 944e974f91cSConrad Meyer void 945e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine) 946e974f91cSConrad Meyer { 947e974f91cSConrad Meyer struct ioat_softc *ioat; 948e974f91cSConrad Meyer 949e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 950e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 951520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 95258a639b7SConrad Meyer ioat->acq_head = ioat->head; 953e974f91cSConrad Meyer } 954e974f91cSConrad Meyer 9551502e363SConrad Meyer int 9561502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags) 9571502e363SConrad Meyer { 9581502e363SConrad Meyer struct ioat_softc *ioat; 9591502e363SConrad Meyer int error; 9601502e363SConrad Meyer 9611502e363SConrad Meyer ioat = to_ioat_softc(dmaengine); 9621502e363SConrad Meyer ioat_acquire(dmaengine); 9631502e363SConrad Meyer 9641502e363SConrad Meyer error = ioat_reserve_space(ioat, n, mflags); 9651502e363SConrad Meyer if (error != 0) 9661502e363SConrad Meyer ioat_release(dmaengine); 9671502e363SConrad Meyer return (error); 9681502e363SConrad Meyer } 9691502e363SConrad Meyer 970e974f91cSConrad Meyer void 971e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine) 972e974f91cSConrad Meyer { 973e974f91cSConrad Meyer struct ioat_softc *ioat; 974e974f91cSConrad Meyer 975e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 976*2f03a95fSAlexander Motin CTR3(KTR_IOAT, "%s channel=%u dispatch1 head=%u", __func__, 977*2f03a95fSAlexander Motin ioat->chan_idx, ioat->head); 97876305bb8SConrad Meyer KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */); 979*2f03a95fSAlexander Motin CTR3(KTR_IOAT, "%s channel=%u dispatch2 head=%u", __func__, 980*2f03a95fSAlexander Motin ioat->chan_idx, ioat->head); 98176305bb8SConrad Meyer 98258a639b7SConrad Meyer if (ioat->acq_head != ioat->head) { 98358a639b7SConrad Meyer ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, 984*2f03a95fSAlexander Motin (uint16_t)ioat->head); 985f44abf1fSConrad Meyer 986*2f03a95fSAlexander Motin if (!callout_pending(&ioat->poll_timer)) { 98758a639b7SConrad Meyer callout_reset(&ioat->poll_timer, 1, 98858a639b7SConrad Meyer ioat_poll_timer_callback, ioat); 98958a639b7SConrad Meyer } 990f44abf1fSConrad Meyer } 991e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 992e974f91cSConrad Meyer } 993e974f91cSConrad Meyer 9949e3bbf26SConrad Meyer static struct ioat_descriptor * 9959e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op, 9969e3bbf26SConrad Meyer uint32_t size, uint64_t src, uint64_t dst, 9979e3bbf26SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, 9989e3bbf26SConrad Meyer uint32_t flags) 999e974f91cSConrad Meyer { 10009e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 1001e974f91cSConrad Meyer struct ioat_descriptor *desc; 1002bf8553eaSConrad Meyer int mflags; 1003e974f91cSConrad Meyer 10049e3bbf26SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 10059e3bbf26SConrad Meyer 1006bec7ff79SConrad Meyer KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0, 1007bec7ff79SConrad Meyer ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS)); 1008bf8553eaSConrad Meyer if ((flags & DMA_NO_WAIT) != 0) 1009bf8553eaSConrad Meyer mflags = M_NOWAIT; 1010bf8553eaSConrad Meyer else 1011bf8553eaSConrad Meyer mflags = M_WAITOK; 1012e974f91cSConrad Meyer 10139e3bbf26SConrad Meyer if (size > ioat->max_xfer_size) { 10149e3bbf26SConrad Meyer ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n", 10159e3bbf26SConrad Meyer __func__, ioat->max_xfer_size, (unsigned)size); 10169e3bbf26SConrad Meyer return (NULL); 10179e3bbf26SConrad Meyer } 1018e974f91cSConrad Meyer 1019bf8553eaSConrad Meyer if (ioat_reserve_space(ioat, 1, mflags) != 0) 1020e974f91cSConrad Meyer return (NULL); 1021e974f91cSConrad Meyer 1022e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 10238e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, ioat->head)->generic; 1024e974f91cSConrad Meyer 1025e974f91cSConrad Meyer hw_desc->u.control_raw = 0; 10269e3bbf26SConrad Meyer hw_desc->u.control_generic.op = op; 10279e3bbf26SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 1028e974f91cSConrad Meyer 1029e974f91cSConrad Meyer if ((flags & DMA_INT_EN) != 0) 10309e3bbf26SConrad Meyer hw_desc->u.control_generic.int_enable = 1; 10316ca07079SConrad Meyer if ((flags & DMA_FENCE) != 0) 10326ca07079SConrad Meyer hw_desc->u.control_generic.fence = 1; 1033e974f91cSConrad Meyer 10349e3bbf26SConrad Meyer hw_desc->size = size; 10359e3bbf26SConrad Meyer hw_desc->src_addr = src; 10369e3bbf26SConrad Meyer hw_desc->dest_addr = dst; 1037e974f91cSConrad Meyer 1038e974f91cSConrad Meyer desc->bus_dmadesc.callback_fn = callback_fn; 1039e974f91cSConrad Meyer desc->bus_dmadesc.callback_arg = callback_arg; 10409e3bbf26SConrad Meyer return (desc); 10419e3bbf26SConrad Meyer } 1042e974f91cSConrad Meyer 10439e3bbf26SConrad Meyer struct bus_dmadesc * 10449e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn, 10459e3bbf26SConrad Meyer void *callback_arg, uint32_t flags) 10469e3bbf26SConrad Meyer { 10479e3bbf26SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 10489e3bbf26SConrad Meyer struct ioat_descriptor *desc; 10499e3bbf26SConrad Meyer struct ioat_softc *ioat; 10509e3bbf26SConrad Meyer 10519e3bbf26SConrad Meyer ioat = to_ioat_softc(dmaengine); 1052520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 10539e3bbf26SConrad Meyer 10549e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn, 10559e3bbf26SConrad Meyer callback_arg, flags); 10569e3bbf26SConrad Meyer if (desc == NULL) 10579e3bbf26SConrad Meyer return (NULL); 10589e3bbf26SConrad Meyer 10598e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma; 10609e3bbf26SConrad Meyer hw_desc->u.control.null = 1; 1061e974f91cSConrad Meyer ioat_submit_single(ioat); 1062e974f91cSConrad Meyer return (&desc->bus_dmadesc); 1063e974f91cSConrad Meyer } 1064e974f91cSConrad Meyer 1065e974f91cSConrad Meyer struct bus_dmadesc * 1066e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst, 1067e974f91cSConrad Meyer bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn, 1068e974f91cSConrad Meyer void *callback_arg, uint32_t flags) 1069e974f91cSConrad Meyer { 1070e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 10719e3bbf26SConrad Meyer struct ioat_descriptor *desc; 1072e974f91cSConrad Meyer struct ioat_softc *ioat; 1073e974f91cSConrad Meyer 1074e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 1075e974f91cSConrad Meyer 10769e3bbf26SConrad Meyer if (((src | dst) & (0xffffull << 48)) != 0) { 10779e3bbf26SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 10789e3bbf26SConrad Meyer __func__); 1079e974f91cSConrad Meyer return (NULL); 1080e974f91cSConrad Meyer } 1081e974f91cSConrad Meyer 10829e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn, 10839e3bbf26SConrad Meyer callback_arg, flags); 10849e3bbf26SConrad Meyer if (desc == NULL) 1085e974f91cSConrad Meyer return (NULL); 1086e974f91cSConrad Meyer 10878e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma; 1088e974f91cSConrad Meyer if (g_ioat_debug_level >= 3) 1089e974f91cSConrad Meyer dump_descriptor(hw_desc); 1090e974f91cSConrad Meyer 1091e974f91cSConrad Meyer ioat_submit_single(ioat); 109205e4cffbSConrad Meyer CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx", 109305e4cffbSConrad Meyer __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len); 1094e974f91cSConrad Meyer return (&desc->bus_dmadesc); 1095e974f91cSConrad Meyer } 1096e974f91cSConrad Meyer 10972a4fd6b1SConrad Meyer struct bus_dmadesc * 10989950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1, 10999950fde0SConrad Meyer bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2, 11009950fde0SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 11019950fde0SConrad Meyer { 11029950fde0SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 11039950fde0SConrad Meyer struct ioat_descriptor *desc; 11049950fde0SConrad Meyer struct ioat_softc *ioat; 11059950fde0SConrad Meyer 11069950fde0SConrad Meyer ioat = to_ioat_softc(dmaengine); 1107520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 11089950fde0SConrad Meyer 11099950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) { 11109950fde0SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 11119950fde0SConrad Meyer __func__); 11129950fde0SConrad Meyer return (NULL); 11139950fde0SConrad Meyer } 11149950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) { 11159950fde0SConrad Meyer ioat_log_message(0, "%s: Addresses must be page-aligned\n", 11169950fde0SConrad Meyer __func__); 11179950fde0SConrad Meyer return (NULL); 11189950fde0SConrad Meyer } 11199950fde0SConrad Meyer 11209950fde0SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1, 11219950fde0SConrad Meyer callback_fn, callback_arg, flags); 11229950fde0SConrad Meyer if (desc == NULL) 11239950fde0SConrad Meyer return (NULL); 11249950fde0SConrad Meyer 11258e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->dma; 11269950fde0SConrad Meyer if (src2 != src1 + PAGE_SIZE) { 11279950fde0SConrad Meyer hw_desc->u.control.src_page_break = 1; 11289950fde0SConrad Meyer hw_desc->next_src_addr = src2; 11299950fde0SConrad Meyer } 11309950fde0SConrad Meyer if (dst2 != dst1 + PAGE_SIZE) { 11319950fde0SConrad Meyer hw_desc->u.control.dest_page_break = 1; 11329950fde0SConrad Meyer hw_desc->next_dest_addr = dst2; 11339950fde0SConrad Meyer } 11349950fde0SConrad Meyer 11359950fde0SConrad Meyer if (g_ioat_debug_level >= 3) 11369950fde0SConrad Meyer dump_descriptor(hw_desc); 11379950fde0SConrad Meyer 11389950fde0SConrad Meyer ioat_submit_single(ioat); 11399950fde0SConrad Meyer return (&desc->bus_dmadesc); 11409950fde0SConrad Meyer } 11419950fde0SConrad Meyer 11429950fde0SConrad Meyer struct bus_dmadesc * 1143bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src, 1144bec7ff79SConrad Meyer bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr, 1145bec7ff79SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 1146bec7ff79SConrad Meyer { 1147bec7ff79SConrad Meyer struct ioat_crc32_hw_descriptor *hw_desc; 1148bec7ff79SConrad Meyer struct ioat_descriptor *desc; 1149bec7ff79SConrad Meyer struct ioat_softc *ioat; 1150bec7ff79SConrad Meyer uint32_t teststore; 1151bec7ff79SConrad Meyer uint8_t op; 1152bec7ff79SConrad Meyer 1153bec7ff79SConrad Meyer ioat = to_ioat_softc(dmaengine); 1154520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1155bec7ff79SConrad Meyer 1156bec7ff79SConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) { 1157bec7ff79SConrad Meyer ioat_log_message(0, "%s: Device lacks MOVECRC capability\n", 1158bec7ff79SConrad Meyer __func__); 1159bec7ff79SConrad Meyer return (NULL); 1160bec7ff79SConrad Meyer } 1161bec7ff79SConrad Meyer if (((src | dst) & (0xffffffull << 40)) != 0) { 1162bec7ff79SConrad Meyer ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n", 1163bec7ff79SConrad Meyer __func__); 1164bec7ff79SConrad Meyer return (NULL); 1165bec7ff79SConrad Meyer } 1166bec7ff79SConrad Meyer teststore = (flags & _DMA_CRC_TESTSTORE); 1167bec7ff79SConrad Meyer if (teststore == _DMA_CRC_TESTSTORE) { 1168bec7ff79SConrad Meyer ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__); 1169bec7ff79SConrad Meyer return (NULL); 1170bec7ff79SConrad Meyer } 1171bec7ff79SConrad Meyer if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) { 1172bec7ff79SConrad Meyer ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n", 1173bec7ff79SConrad Meyer __func__); 1174bec7ff79SConrad Meyer return (NULL); 1175bec7ff79SConrad Meyer } 1176bec7ff79SConrad Meyer 1177bec7ff79SConrad Meyer switch (teststore) { 1178bec7ff79SConrad Meyer case DMA_CRC_STORE: 1179bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC_STORE; 1180bec7ff79SConrad Meyer break; 1181bec7ff79SConrad Meyer case DMA_CRC_TEST: 1182bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC_TEST; 1183bec7ff79SConrad Meyer break; 1184bec7ff79SConrad Meyer default: 1185bec7ff79SConrad Meyer KASSERT(teststore == 0, ("bogus")); 1186bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC; 1187bec7ff79SConrad Meyer break; 1188bec7ff79SConrad Meyer } 1189bec7ff79SConrad Meyer 1190bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0 && 1191bec7ff79SConrad Meyer (crcptr & (0xffffffull << 40)) != 0) { 1192bec7ff79SConrad Meyer ioat_log_message(0, 1193bec7ff79SConrad Meyer "%s: High 24 bits of crcptr invalid\n", __func__); 1194bec7ff79SConrad Meyer return (NULL); 1195bec7ff79SConrad Meyer } 1196bec7ff79SConrad Meyer 1197bec7ff79SConrad Meyer desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn, 1198bec7ff79SConrad Meyer callback_arg, flags & ~_DMA_CRC_FLAGS); 1199bec7ff79SConrad Meyer if (desc == NULL) 1200bec7ff79SConrad Meyer return (NULL); 1201bec7ff79SConrad Meyer 12028e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32; 1203bec7ff79SConrad Meyer 1204bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0) 1205bec7ff79SConrad Meyer hw_desc->crc_address = crcptr; 1206bec7ff79SConrad Meyer else 1207bec7ff79SConrad Meyer hw_desc->u.control.crc_location = 1; 1208bec7ff79SConrad Meyer 1209bec7ff79SConrad Meyer if (initialseed != NULL) { 1210bec7ff79SConrad Meyer hw_desc->u.control.use_seed = 1; 1211bec7ff79SConrad Meyer hw_desc->seed = *initialseed; 1212bec7ff79SConrad Meyer } 1213bec7ff79SConrad Meyer 1214bec7ff79SConrad Meyer if (g_ioat_debug_level >= 3) 1215bec7ff79SConrad Meyer dump_descriptor(hw_desc); 1216bec7ff79SConrad Meyer 1217bec7ff79SConrad Meyer ioat_submit_single(ioat); 1218bec7ff79SConrad Meyer return (&desc->bus_dmadesc); 1219bec7ff79SConrad Meyer } 1220bec7ff79SConrad Meyer 1221bec7ff79SConrad Meyer struct bus_dmadesc * 1222bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len, 1223bec7ff79SConrad Meyer uint32_t *initialseed, bus_addr_t crcptr, 1224bec7ff79SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 1225bec7ff79SConrad Meyer { 1226bec7ff79SConrad Meyer struct ioat_crc32_hw_descriptor *hw_desc; 1227bec7ff79SConrad Meyer struct ioat_descriptor *desc; 1228bec7ff79SConrad Meyer struct ioat_softc *ioat; 1229bec7ff79SConrad Meyer uint32_t teststore; 1230bec7ff79SConrad Meyer uint8_t op; 1231bec7ff79SConrad Meyer 1232bec7ff79SConrad Meyer ioat = to_ioat_softc(dmaengine); 1233520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1234bec7ff79SConrad Meyer 1235bec7ff79SConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) { 1236bec7ff79SConrad Meyer ioat_log_message(0, "%s: Device lacks CRC capability\n", 1237bec7ff79SConrad Meyer __func__); 1238bec7ff79SConrad Meyer return (NULL); 1239bec7ff79SConrad Meyer } 1240bec7ff79SConrad Meyer if ((src & (0xffffffull << 40)) != 0) { 1241bec7ff79SConrad Meyer ioat_log_message(0, "%s: High 24 bits of src invalid\n", 1242bec7ff79SConrad Meyer __func__); 1243bec7ff79SConrad Meyer return (NULL); 1244bec7ff79SConrad Meyer } 1245bec7ff79SConrad Meyer teststore = (flags & _DMA_CRC_TESTSTORE); 1246bec7ff79SConrad Meyer if (teststore == _DMA_CRC_TESTSTORE) { 1247bec7ff79SConrad Meyer ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__); 1248bec7ff79SConrad Meyer return (NULL); 1249bec7ff79SConrad Meyer } 1250bec7ff79SConrad Meyer if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) { 1251bec7ff79SConrad Meyer ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n", 1252bec7ff79SConrad Meyer __func__); 1253bec7ff79SConrad Meyer return (NULL); 1254bec7ff79SConrad Meyer } 1255bec7ff79SConrad Meyer 1256bec7ff79SConrad Meyer switch (teststore) { 1257bec7ff79SConrad Meyer case DMA_CRC_STORE: 1258bec7ff79SConrad Meyer op = IOAT_OP_CRC_STORE; 1259bec7ff79SConrad Meyer break; 1260bec7ff79SConrad Meyer case DMA_CRC_TEST: 1261bec7ff79SConrad Meyer op = IOAT_OP_CRC_TEST; 1262bec7ff79SConrad Meyer break; 1263bec7ff79SConrad Meyer default: 1264bec7ff79SConrad Meyer KASSERT(teststore == 0, ("bogus")); 1265bec7ff79SConrad Meyer op = IOAT_OP_CRC; 1266bec7ff79SConrad Meyer break; 1267bec7ff79SConrad Meyer } 1268bec7ff79SConrad Meyer 1269bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0 && 1270bec7ff79SConrad Meyer (crcptr & (0xffffffull << 40)) != 0) { 1271bec7ff79SConrad Meyer ioat_log_message(0, 1272bec7ff79SConrad Meyer "%s: High 24 bits of crcptr invalid\n", __func__); 1273bec7ff79SConrad Meyer return (NULL); 1274bec7ff79SConrad Meyer } 1275bec7ff79SConrad Meyer 1276bec7ff79SConrad Meyer desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn, 1277bec7ff79SConrad Meyer callback_arg, flags & ~_DMA_CRC_FLAGS); 1278bec7ff79SConrad Meyer if (desc == NULL) 1279bec7ff79SConrad Meyer return (NULL); 1280bec7ff79SConrad Meyer 12818e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->crc32; 1282bec7ff79SConrad Meyer 1283bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0) 1284bec7ff79SConrad Meyer hw_desc->crc_address = crcptr; 1285bec7ff79SConrad Meyer else 1286bec7ff79SConrad Meyer hw_desc->u.control.crc_location = 1; 1287bec7ff79SConrad Meyer 1288bec7ff79SConrad Meyer if (initialseed != NULL) { 1289bec7ff79SConrad Meyer hw_desc->u.control.use_seed = 1; 1290bec7ff79SConrad Meyer hw_desc->seed = *initialseed; 1291bec7ff79SConrad Meyer } 1292bec7ff79SConrad Meyer 1293bec7ff79SConrad Meyer if (g_ioat_debug_level >= 3) 1294bec7ff79SConrad Meyer dump_descriptor(hw_desc); 1295bec7ff79SConrad Meyer 1296bec7ff79SConrad Meyer ioat_submit_single(ioat); 1297bec7ff79SConrad Meyer return (&desc->bus_dmadesc); 1298bec7ff79SConrad Meyer } 1299bec7ff79SConrad Meyer 1300bec7ff79SConrad Meyer struct bus_dmadesc * 13012a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern, 13022a4fd6b1SConrad Meyer bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg, 13032a4fd6b1SConrad Meyer uint32_t flags) 13042a4fd6b1SConrad Meyer { 13052a4fd6b1SConrad Meyer struct ioat_fill_hw_descriptor *hw_desc; 13062a4fd6b1SConrad Meyer struct ioat_descriptor *desc; 13072a4fd6b1SConrad Meyer struct ioat_softc *ioat; 13082a4fd6b1SConrad Meyer 13092a4fd6b1SConrad Meyer ioat = to_ioat_softc(dmaengine); 1310520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 13112a4fd6b1SConrad Meyer 13121693d27bSConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) { 13131693d27bSConrad Meyer ioat_log_message(0, "%s: Device lacks BFILL capability\n", 13141693d27bSConrad Meyer __func__); 13151693d27bSConrad Meyer return (NULL); 13161693d27bSConrad Meyer } 13171693d27bSConrad Meyer 13182a4fd6b1SConrad Meyer if ((dst & (0xffffull << 48)) != 0) { 13192a4fd6b1SConrad Meyer ioat_log_message(0, "%s: High 16 bits of dst invalid\n", 13202a4fd6b1SConrad Meyer __func__); 13212a4fd6b1SConrad Meyer return (NULL); 13222a4fd6b1SConrad Meyer } 13232a4fd6b1SConrad Meyer 13242a4fd6b1SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst, 13252a4fd6b1SConrad Meyer callback_fn, callback_arg, flags); 13262a4fd6b1SConrad Meyer if (desc == NULL) 13272a4fd6b1SConrad Meyer return (NULL); 13282a4fd6b1SConrad Meyer 13298e269d99SConrad Meyer hw_desc = &ioat_get_descriptor(ioat, desc->id)->fill; 13302a4fd6b1SConrad Meyer if (g_ioat_debug_level >= 3) 13312a4fd6b1SConrad Meyer dump_descriptor(hw_desc); 13322a4fd6b1SConrad Meyer 13332a4fd6b1SConrad Meyer ioat_submit_single(ioat); 13342a4fd6b1SConrad Meyer return (&desc->bus_dmadesc); 13352a4fd6b1SConrad Meyer } 13362a4fd6b1SConrad Meyer 1337e974f91cSConrad Meyer /* 1338e974f91cSConrad Meyer * Ring Management 1339e974f91cSConrad Meyer */ 1340e974f91cSConrad Meyer static inline uint32_t 1341e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat) 1342e974f91cSConrad Meyer { 1343e974f91cSConrad Meyer 1344e974f91cSConrad Meyer return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1)); 1345e974f91cSConrad Meyer } 1346e974f91cSConrad Meyer 1347e974f91cSConrad Meyer static inline uint32_t 1348e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat) 1349e974f91cSConrad Meyer { 1350e974f91cSConrad Meyer 1351e974f91cSConrad Meyer return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1); 1352e974f91cSConrad Meyer } 1353e974f91cSConrad Meyer 1354bf8553eaSConrad Meyer /* 1355bf8553eaSConrad Meyer * Reserves space in this IOAT descriptor ring by ensuring enough slots remain 1356bf8553eaSConrad Meyer * for 'num_descs'. 1357bf8553eaSConrad Meyer * 1358bf8553eaSConrad Meyer * If mflags contains M_WAITOK, blocks until enough space is available. 1359bf8553eaSConrad Meyer * 1360bf8553eaSConrad Meyer * Returns zero on success, or an errno on error. If num_descs is beyond the 1361bf8553eaSConrad Meyer * maximum ring size, returns EINVAl; if allocation would block and mflags 1362bf8553eaSConrad Meyer * contains M_NOWAIT, returns EAGAIN. 1363bf8553eaSConrad Meyer * 1364bf8553eaSConrad Meyer * Must be called with the submit_lock held; returns with the lock held. The 1365bf8553eaSConrad Meyer * lock may be dropped to allocate the ring. 1366bf8553eaSConrad Meyer * 1367bf8553eaSConrad Meyer * (The submit_lock is needed to add any entries to the ring, so callers are 1368bf8553eaSConrad Meyer * assured enough room is available.) 1369bf8553eaSConrad Meyer */ 1370e974f91cSConrad Meyer static int 1371bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags) 1372e974f91cSConrad Meyer { 137325ad9585SConrad Meyer boolean_t dug; 1374bf8553eaSConrad Meyer int error; 1375e974f91cSConrad Meyer 1376bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1377bf8553eaSConrad Meyer error = 0; 137825ad9585SConrad Meyer dug = FALSE; 1379e974f91cSConrad Meyer 1380a0992979SConrad Meyer if (num_descs < 1 || num_descs >= (1 << ioat->ring_size_order)) { 1381bf8553eaSConrad Meyer error = EINVAL; 1382bf8553eaSConrad Meyer goto out; 1383e974f91cSConrad Meyer } 138408a6b96aSConrad Meyer 138508a6b96aSConrad Meyer for (;;) { 13865f77bd3eSConrad Meyer if (ioat->quiescing) { 13875f77bd3eSConrad Meyer error = ENXIO; 13885f77bd3eSConrad Meyer goto out; 13895f77bd3eSConrad Meyer } 1390bf8553eaSConrad Meyer 1391bf8553eaSConrad Meyer if (ioat_get_ring_space(ioat) >= num_descs) 1392bf8553eaSConrad Meyer goto out; 1393bf8553eaSConrad Meyer 139405e4cffbSConrad Meyer CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__, 139505e4cffbSConrad Meyer ioat->chan_idx, num_descs); 139605e4cffbSConrad Meyer 1397a0992979SConrad Meyer if (!dug && !ioat->is_submitter_processing) { 139825ad9585SConrad Meyer ioat->is_submitter_processing = TRUE; 139925ad9585SConrad Meyer mtx_unlock(&ioat->submit_lock); 140025ad9585SConrad Meyer 140105e4cffbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u attempting to process events", 140205e4cffbSConrad Meyer __func__, ioat->chan_idx); 1403*2f03a95fSAlexander Motin ioat_process_events(ioat, FALSE); 140425ad9585SConrad Meyer 140525ad9585SConrad Meyer mtx_lock(&ioat->submit_lock); 140625ad9585SConrad Meyer dug = TRUE; 140725ad9585SConrad Meyer KASSERT(ioat->is_submitter_processing == TRUE, 140825ad9585SConrad Meyer ("is_submitter_processing")); 140925ad9585SConrad Meyer ioat->is_submitter_processing = FALSE; 141025ad9585SConrad Meyer wakeup(&ioat->tail); 141125ad9585SConrad Meyer continue; 141225ad9585SConrad Meyer } 141325ad9585SConrad Meyer 1414a0992979SConrad Meyer if ((mflags & M_WAITOK) == 0) { 1415a0992979SConrad Meyer error = EAGAIN; 1416a0992979SConrad Meyer break; 1417a0992979SConrad Meyer } 141805e4cffbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u blocking on completions", 141905e4cffbSConrad Meyer __func__, ioat->chan_idx); 1420bf8553eaSConrad Meyer msleep(&ioat->tail, &ioat->submit_lock, 0, 1421a0992979SConrad Meyer "ioat_full", 0); 1422bf8553eaSConrad Meyer continue; 1423bf8553eaSConrad Meyer } 1424bf8553eaSConrad Meyer 1425bf8553eaSConrad Meyer out: 1426bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 142708a6b96aSConrad Meyer KASSERT(!ioat->quiescing || error == ENXIO, 142808a6b96aSConrad Meyer ("reserved during quiesce")); 1429bf8553eaSConrad Meyer return (error); 1430bf8553eaSConrad Meyer } 1431bf8553eaSConrad Meyer 1432bf8553eaSConrad Meyer static void 1433bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size, 14348e269d99SConrad Meyer struct ioat_descriptor *ring) 1435bf8553eaSConrad Meyer { 1436bf8553eaSConrad Meyer 1437bf8553eaSConrad Meyer free(ring, M_IOAT); 1438e974f91cSConrad Meyer } 1439e974f91cSConrad Meyer 1440e974f91cSConrad Meyer static struct ioat_descriptor * 1441e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index) 1442e974f91cSConrad Meyer { 1443e974f91cSConrad Meyer 14448e269d99SConrad Meyer return (&ioat->ring[index % (1 << ioat->ring_size_order)]); 14458e269d99SConrad Meyer } 14468e269d99SConrad Meyer 14478e269d99SConrad Meyer static union ioat_hw_descriptor * 14488e269d99SConrad Meyer ioat_get_descriptor(struct ioat_softc *ioat, uint32_t index) 14498e269d99SConrad Meyer { 14508e269d99SConrad Meyer 14518e269d99SConrad Meyer return (&ioat->hw_desc_ring[index % (1 << ioat->ring_size_order)]); 1452e974f91cSConrad Meyer } 1453e974f91cSConrad Meyer 1454e974f91cSConrad Meyer static void 14558f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr) 1456e974f91cSConrad Meyer { 14578e269d99SConrad Meyer union ioat_hw_descriptor *desc; 14588f274637SConrad Meyer 145959acd4baSConrad Meyer ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr, 146059acd4baSConrad Meyer IOAT_CHANERR_STR); 14618f274637SConrad Meyer if (chanerr == 0) 14628f274637SConrad Meyer return; 14638f274637SConrad Meyer 1464faefad9cSConrad Meyer mtx_assert(&ioat->cleanup_lock, MA_OWNED); 1465faefad9cSConrad Meyer 14668e269d99SConrad Meyer desc = ioat_get_descriptor(ioat, ioat->tail + 0); 14678e269d99SConrad Meyer dump_descriptor(desc); 14688f274637SConrad Meyer 14698e269d99SConrad Meyer desc = ioat_get_descriptor(ioat, ioat->tail + 1); 14708e269d99SConrad Meyer dump_descriptor(desc); 14718f274637SConrad Meyer } 14728f274637SConrad Meyer 14738f274637SConrad Meyer static void 14745ac77963SConrad Meyer ioat_poll_timer_callback(void *arg) 14755ac77963SConrad Meyer { 14765ac77963SConrad Meyer struct ioat_softc *ioat; 14775ac77963SConrad Meyer 14785ac77963SConrad Meyer ioat = arg; 14795ac77963SConrad Meyer ioat_log_message(3, "%s\n", __func__); 14805ac77963SConrad Meyer 1481*2f03a95fSAlexander Motin ioat_process_events(ioat, FALSE); 1482*2f03a95fSAlexander Motin 1483*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 1484*2f03a95fSAlexander Motin if (ioat_get_active(ioat) > 0) 1485*2f03a95fSAlexander Motin callout_schedule(&ioat->poll_timer, 1); 1486*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 14875ac77963SConrad Meyer } 14885ac77963SConrad Meyer 1489e974f91cSConrad Meyer /* 1490e974f91cSConrad Meyer * Support Functions 1491e974f91cSConrad Meyer */ 1492e974f91cSConrad Meyer static void 1493e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat) 1494e974f91cSConrad Meyer { 1495e974f91cSConrad Meyer 14965a8af401SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 14975a8af401SConrad Meyer 1498*2f03a95fSAlexander Motin ioat->head++; 1499*2f03a95fSAlexander Motin CTR4(KTR_IOAT, "%s channel=%u head=%u tail=%u", __func__, 1500*2f03a95fSAlexander Motin ioat->chan_idx, ioat->head, ioat->tail); 1501e974f91cSConrad Meyer 150201fbbc88SConrad Meyer ioat->stats.descriptors_submitted++; 1503e974f91cSConrad Meyer } 1504e974f91cSConrad Meyer 1505e974f91cSConrad Meyer static int 1506e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat) 1507e974f91cSConrad Meyer { 1508e974f91cSConrad Meyer uint64_t status; 1509e974f91cSConrad Meyer uint32_t chanerr; 1510cea5b880SConrad Meyer unsigned timeout; 15115f77bd3eSConrad Meyer int error; 15125f77bd3eSConrad Meyer 1513520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 15146d41e1edSConrad Meyer 1515*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 151693f7f84aSConrad Meyer while (ioat->resetting && !ioat->destroying) 1517*2f03a95fSAlexander Motin msleep(&ioat->resetting, &ioat->submit_lock, 0, "IRH_drain", 0); 151893f7f84aSConrad Meyer if (ioat->destroying) { 1519*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 152093f7f84aSConrad Meyer return (ENXIO); 152193f7f84aSConrad Meyer } 152293f7f84aSConrad Meyer ioat->resetting = TRUE; 15235f77bd3eSConrad Meyer ioat->quiescing = TRUE; 1524*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 1525*2f03a95fSAlexander Motin mtx_lock(&ioat->cleanup_lock); 1526*2f03a95fSAlexander Motin while (ioat_get_active(ioat) > 0) 1527*2f03a95fSAlexander Motin msleep(&ioat->tail, &ioat->cleanup_lock, 0, "ioat_drain", 1); 1528e974f91cSConrad Meyer 1529fe8712f8SConrad Meyer /* 1530fe8712f8SConrad Meyer * Suspend ioat_process_events while the hardware and softc are in an 1531fe8712f8SConrad Meyer * indeterminate state. 1532fe8712f8SConrad Meyer */ 1533fe8712f8SConrad Meyer ioat->resetting_cleanup = TRUE; 1534fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1535fe8712f8SConrad Meyer 1536ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__, 1537ee5642bbSConrad Meyer ioat->chan_idx); 1538ee5642bbSConrad Meyer 1539e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1540e974f91cSConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) 1541e974f91cSConrad Meyer ioat_suspend(ioat); 1542e974f91cSConrad Meyer 1543e974f91cSConrad Meyer /* Wait at most 20 ms */ 1544e974f91cSConrad Meyer for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) && 1545e974f91cSConrad Meyer timeout < 20; timeout++) { 1546e974f91cSConrad Meyer DELAY(1000); 1547e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1548e974f91cSConrad Meyer } 15495f77bd3eSConrad Meyer if (timeout == 20) { 15505f77bd3eSConrad Meyer error = ETIMEDOUT; 15515f77bd3eSConrad Meyer goto out; 15525f77bd3eSConrad Meyer } 1553e974f91cSConrad Meyer 1554cea5b880SConrad Meyer KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce")); 1555cea5b880SConrad Meyer 1556e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 1557e974f91cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 1558e974f91cSConrad Meyer 1559ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__, 1560ee5642bbSConrad Meyer ioat->chan_idx); 1561ee5642bbSConrad Meyer 1562e974f91cSConrad Meyer /* 1563e974f91cSConrad Meyer * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors 1564e974f91cSConrad Meyer * that can cause stability issues for IOAT v3. 1565e974f91cSConrad Meyer */ 1566e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07, 1567e974f91cSConrad Meyer 4); 1568e974f91cSConrad Meyer chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4); 1569e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4); 1570e974f91cSConrad Meyer 15710d1a05d9SConrad Meyer /* 15720d1a05d9SConrad Meyer * BDXDE and BWD models reset MSI-X registers on device reset. 15730d1a05d9SConrad Meyer * Save/restore their contents manually. 15740d1a05d9SConrad Meyer */ 1575f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1576f7157235SConrad Meyer ioat_log_message(1, "device resets MSI-X registers; saving\n"); 15770d1a05d9SConrad Meyer pci_save_state(ioat->device); 1578f7157235SConrad Meyer } 15790d1a05d9SConrad Meyer 1580e974f91cSConrad Meyer ioat_reset(ioat); 1581ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__, 1582ee5642bbSConrad Meyer ioat->chan_idx); 1583e974f91cSConrad Meyer 1584e974f91cSConrad Meyer /* Wait at most 20 ms */ 1585e974f91cSConrad Meyer for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++) 1586e974f91cSConrad Meyer DELAY(1000); 15875f77bd3eSConrad Meyer if (timeout == 20) { 15885f77bd3eSConrad Meyer error = ETIMEDOUT; 15895f77bd3eSConrad Meyer goto out; 15905f77bd3eSConrad Meyer } 1591e974f91cSConrad Meyer 1592f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1593f7157235SConrad Meyer ioat_log_message(1, "device resets registers; restored\n"); 15940d1a05d9SConrad Meyer pci_restore_state(ioat->device); 1595f7157235SConrad Meyer } 15964253ea50SConrad Meyer 1597cea5b880SConrad Meyer /* Reset attempts to return the hardware to "halted." */ 1598cea5b880SConrad Meyer status = ioat_get_chansts(ioat); 1599cea5b880SConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) { 1600cea5b880SConrad Meyer /* So this really shouldn't happen... */ 1601cea5b880SConrad Meyer ioat_log_message(0, "Device is active after a reset?\n"); 1602cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 16035f77bd3eSConrad Meyer error = 0; 16045f77bd3eSConrad Meyer goto out; 1605e974f91cSConrad Meyer } 1606e974f91cSConrad Meyer 1607cea5b880SConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 16085f77bd3eSConrad Meyer if (chanerr != 0) { 1609faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1610faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 1611faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 16125f77bd3eSConrad Meyer error = EIO; 16135f77bd3eSConrad Meyer goto out; 16145f77bd3eSConrad Meyer } 1615cea5b880SConrad Meyer 1616cea5b880SConrad Meyer /* 1617cea5b880SConrad Meyer * Bring device back online after reset. Writing CHAINADDR brings the 1618cea5b880SConrad Meyer * device back to active. 1619cea5b880SConrad Meyer * 1620cea5b880SConrad Meyer * The internal ring counter resets to zero, so we have to start over 1621cea5b880SConrad Meyer * at zero as well. 1622cea5b880SConrad Meyer */ 1623*2f03a95fSAlexander Motin ioat->tail = ioat->head = 0; 1624cea5b880SConrad Meyer ioat->last_seen = 0; 1625fe8712f8SConrad Meyer *ioat->comp_update = 0; 1626cea5b880SConrad Meyer 1627cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 1628cea5b880SConrad Meyer ioat_write_chancmp(ioat, ioat->comp_update_bus_addr); 16298e269d99SConrad Meyer ioat_write_chainaddr(ioat, RING_PHYS_ADDR(ioat, 0)); 16305f77bd3eSConrad Meyer error = 0; 1631ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__, 1632ee5642bbSConrad Meyer ioat->chan_idx); 16335f77bd3eSConrad Meyer 16345f77bd3eSConrad Meyer out: 16351bbc06b8SConrad Meyer /* Enqueues a null operation and ensures it completes. */ 1636ee5642bbSConrad Meyer if (error == 0) { 16371bbc06b8SConrad Meyer error = ioat_start_channel(ioat); 1638ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u started channel", __func__, 1639ee5642bbSConrad Meyer ioat->chan_idx); 1640ee5642bbSConrad Meyer } 16411bbc06b8SConrad Meyer 1642fe8712f8SConrad Meyer /* 1643fe8712f8SConrad Meyer * Resume completions now that ring state is consistent. 1644fe8712f8SConrad Meyer */ 1645fe8712f8SConrad Meyer mtx_lock(&ioat->cleanup_lock); 1646fe8712f8SConrad Meyer ioat->resetting_cleanup = FALSE; 1647fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 164893f7f84aSConrad Meyer 1649fe8712f8SConrad Meyer /* Unblock submission of new work */ 1650*2f03a95fSAlexander Motin mtx_lock(&ioat->submit_lock); 1651fe8712f8SConrad Meyer ioat->quiescing = FALSE; 1652fe8712f8SConrad Meyer wakeup(&ioat->quiescing); 1653fe8712f8SConrad Meyer 1654fe8712f8SConrad Meyer ioat->resetting = FALSE; 1655fe8712f8SConrad Meyer wakeup(&ioat->resetting); 16561bbc06b8SConrad Meyer 1657ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx); 1658*2f03a95fSAlexander Motin mtx_unlock(&ioat->submit_lock); 1659fe8712f8SConrad Meyer 16605f77bd3eSConrad Meyer return (error); 1661cea5b880SConrad Meyer } 1662cea5b880SConrad Meyer 1663f7157235SConrad Meyer static int 1664faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS) 1665faefad9cSConrad Meyer { 1666faefad9cSConrad Meyer struct ioat_softc *ioat; 1667faefad9cSConrad Meyer struct sbuf sb; 1668faefad9cSConrad Meyer uint64_t status; 1669faefad9cSConrad Meyer int error; 1670faefad9cSConrad Meyer 1671faefad9cSConrad Meyer ioat = arg1; 1672faefad9cSConrad Meyer 1673faefad9cSConrad Meyer status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS; 1674faefad9cSConrad Meyer 1675faefad9cSConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 256, req); 1676faefad9cSConrad Meyer switch (status) { 1677faefad9cSConrad Meyer case IOAT_CHANSTS_ACTIVE: 1678faefad9cSConrad Meyer sbuf_printf(&sb, "ACTIVE"); 1679faefad9cSConrad Meyer break; 1680faefad9cSConrad Meyer case IOAT_CHANSTS_IDLE: 1681faefad9cSConrad Meyer sbuf_printf(&sb, "IDLE"); 1682faefad9cSConrad Meyer break; 1683faefad9cSConrad Meyer case IOAT_CHANSTS_SUSPENDED: 1684faefad9cSConrad Meyer sbuf_printf(&sb, "SUSPENDED"); 1685faefad9cSConrad Meyer break; 1686faefad9cSConrad Meyer case IOAT_CHANSTS_HALTED: 1687faefad9cSConrad Meyer sbuf_printf(&sb, "HALTED"); 1688faefad9cSConrad Meyer break; 1689faefad9cSConrad Meyer case IOAT_CHANSTS_ARMED: 1690faefad9cSConrad Meyer sbuf_printf(&sb, "ARMED"); 1691faefad9cSConrad Meyer break; 1692faefad9cSConrad Meyer default: 1693faefad9cSConrad Meyer sbuf_printf(&sb, "UNKNOWN"); 1694faefad9cSConrad Meyer break; 1695faefad9cSConrad Meyer } 1696faefad9cSConrad Meyer error = sbuf_finish(&sb); 1697faefad9cSConrad Meyer sbuf_delete(&sb); 1698faefad9cSConrad Meyer 1699faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 1700faefad9cSConrad Meyer return (error); 1701faefad9cSConrad Meyer return (EINVAL); 1702faefad9cSConrad Meyer } 1703faefad9cSConrad Meyer 1704faefad9cSConrad Meyer static int 170501fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS) 170601fbbc88SConrad Meyer { 170701fbbc88SConrad Meyer struct ioat_softc *ioat; 170801fbbc88SConrad Meyer struct sbuf sb; 170901fbbc88SConrad Meyer #define PRECISION "1" 171001fbbc88SConrad Meyer const uintmax_t factor = 10; 171101fbbc88SConrad Meyer uintmax_t rate; 171201fbbc88SConrad Meyer int error; 171301fbbc88SConrad Meyer 171401fbbc88SConrad Meyer ioat = arg1; 171501fbbc88SConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 16, req); 171601fbbc88SConrad Meyer 171701fbbc88SConrad Meyer if (ioat->stats.interrupts == 0) { 171801fbbc88SConrad Meyer sbuf_printf(&sb, "NaN"); 171901fbbc88SConrad Meyer goto out; 172001fbbc88SConrad Meyer } 172101fbbc88SConrad Meyer rate = ioat->stats.descriptors_processed * factor / 172201fbbc88SConrad Meyer ioat->stats.interrupts; 172301fbbc88SConrad Meyer sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor, 172401fbbc88SConrad Meyer rate % factor); 172501fbbc88SConrad Meyer #undef PRECISION 172601fbbc88SConrad Meyer out: 172701fbbc88SConrad Meyer error = sbuf_finish(&sb); 172801fbbc88SConrad Meyer sbuf_delete(&sb); 172901fbbc88SConrad Meyer if (error != 0 || req->newptr == NULL) 173001fbbc88SConrad Meyer return (error); 173101fbbc88SConrad Meyer return (EINVAL); 173201fbbc88SConrad Meyer } 173301fbbc88SConrad Meyer 173401fbbc88SConrad Meyer static int 1735f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS) 1736f7157235SConrad Meyer { 1737f7157235SConrad Meyer struct ioat_softc *ioat; 1738f7157235SConrad Meyer int error, arg; 1739f7157235SConrad Meyer 1740f7157235SConrad Meyer ioat = arg1; 1741f7157235SConrad Meyer 1742f7157235SConrad Meyer arg = 0; 1743f7157235SConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 1744f7157235SConrad Meyer if (error != 0 || req->newptr == NULL) 1745f7157235SConrad Meyer return (error); 1746f7157235SConrad Meyer 1747f7157235SConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 1748f7157235SConrad Meyer if (error != 0) 1749f7157235SConrad Meyer return (error); 1750f7157235SConrad Meyer 1751f7157235SConrad Meyer if (arg != 0) 1752f7157235SConrad Meyer error = ioat_reset_hw(ioat); 1753f7157235SConrad Meyer 1754f7157235SConrad Meyer return (error); 1755f7157235SConrad Meyer } 1756f7157235SConrad Meyer 1757e974f91cSConrad Meyer static void 1758e974f91cSConrad Meyer dump_descriptor(void *hw_desc) 1759e974f91cSConrad Meyer { 1760e974f91cSConrad Meyer int i, j; 1761e974f91cSConrad Meyer 1762e974f91cSConrad Meyer for (i = 0; i < 2; i++) { 1763e974f91cSConrad Meyer for (j = 0; j < 8; j++) 1764e974f91cSConrad Meyer printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]); 1765e974f91cSConrad Meyer printf("\n"); 1766e974f91cSConrad Meyer } 1767e974f91cSConrad Meyer } 1768e974f91cSConrad Meyer 1769e974f91cSConrad Meyer static void 1770e974f91cSConrad Meyer ioat_setup_sysctl(device_t device) 1771e974f91cSConrad Meyer { 177201fbbc88SConrad Meyer struct sysctl_oid_list *par, *statpar, *state, *hammer; 1773f7157235SConrad Meyer struct sysctl_ctx_list *ctx; 177401fbbc88SConrad Meyer struct sysctl_oid *tree, *tmp; 1775e974f91cSConrad Meyer struct ioat_softc *ioat; 1776e974f91cSConrad Meyer 1777e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 1778f7157235SConrad Meyer ctx = device_get_sysctl_ctx(device); 1779f7157235SConrad Meyer tree = device_get_sysctl_tree(device); 1780f7157235SConrad Meyer par = SYSCTL_CHILDREN(tree); 1781e974f91cSConrad Meyer 178265e4f8adSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD, 178365e4f8adSConrad Meyer &ioat->version, 0, "HW version (0xMM form)"); 178465e4f8adSConrad Meyer SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD, 178565e4f8adSConrad Meyer &ioat->max_xfer_size, 0, "HW maximum transfer size"); 17865ca9fc2aSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD, 17875ca9fc2aSConrad Meyer &ioat->intrdelay_supported, 0, "Is INTRDELAY supported"); 17885ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD, 17895ca9fc2aSConrad Meyer &ioat->intrdelay_max, 0, 17905ca9fc2aSConrad Meyer "Maximum configurable INTRDELAY on this channel (microseconds)"); 179165e4f8adSConrad Meyer 179201fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL, 179301fbbc88SConrad Meyer "IOAT channel internal state"); 179401fbbc88SConrad Meyer state = SYSCTL_CHILDREN(tmp); 179501fbbc88SConrad Meyer 179601fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD, 1797bf8553eaSConrad Meyer &ioat->ring_size_order, 0, "SW descriptor ring size order"); 179801fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 179901fbbc88SConrad Meyer 0, "SW descriptor head pointer index"); 180001fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 180101fbbc88SConrad Meyer 0, "SW descriptor tail pointer index"); 1802f7157235SConrad Meyer 180301fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD, 180465e4f8adSConrad Meyer ioat->comp_update, "HW addr of last completion"); 180565e4f8adSConrad Meyer 180625ad9585SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing", 180725ad9585SConrad Meyer CTLFLAG_RD, &ioat->is_submitter_processing, 0, 180825ad9585SConrad Meyer "submitter processing"); 180965e4f8adSConrad Meyer 181001fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts", 1811faefad9cSConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A", 1812faefad9cSConrad Meyer "String of the channel status"); 181301fbbc88SConrad Meyer 18145ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD, 18155ca9fc2aSConrad Meyer &ioat->cached_intrdelay, 0, 18165ca9fc2aSConrad Meyer "Current INTRDELAY on this channel (cached, microseconds)"); 18175ca9fc2aSConrad Meyer 181801fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL, 181901fbbc88SConrad Meyer "Big hammers (mostly for testing)"); 182001fbbc88SConrad Meyer hammer = SYSCTL_CHILDREN(tmp); 182101fbbc88SConrad Meyer 182201fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset", 182301fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I", 182401fbbc88SConrad Meyer "Set to non-zero to reset the hardware"); 182501fbbc88SConrad Meyer 182601fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL, 182701fbbc88SConrad Meyer "IOAT channel statistics"); 182801fbbc88SConrad Meyer statpar = SYSCTL_CHILDREN(tmp); 182901fbbc88SConrad Meyer 183001fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW, 183101fbbc88SConrad Meyer &ioat->stats.interrupts, 183201fbbc88SConrad Meyer "Number of interrupts processed on this channel"); 183301fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW, 183401fbbc88SConrad Meyer &ioat->stats.descriptors_processed, 183501fbbc88SConrad Meyer "Number of descriptors processed on this channel"); 183601fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW, 183701fbbc88SConrad Meyer &ioat->stats.descriptors_submitted, 183801fbbc88SConrad Meyer "Number of descriptors submitted to this channel"); 183901fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW, 184001fbbc88SConrad Meyer &ioat->stats.descriptors_error, 184101fbbc88SConrad Meyer "Number of descriptors failed by channel errors"); 184201fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW, 184301fbbc88SConrad Meyer &ioat->stats.channel_halts, 0, 184401fbbc88SConrad Meyer "Number of times the channel has halted"); 184501fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW, 184601fbbc88SConrad Meyer &ioat->stats.last_halt_chanerr, 0, 184701fbbc88SConrad Meyer "The raw CHANERR when the channel was last halted"); 184801fbbc88SConrad Meyer 184901fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt", 185001fbbc88SConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A", 185101fbbc88SConrad Meyer "Descriptors per interrupt"); 1852e974f91cSConrad Meyer } 1853466b3540SConrad Meyer 1854*2f03a95fSAlexander Motin static void 1855*2f03a95fSAlexander Motin ioat_get(struct ioat_softc *ioat) 1856466b3540SConrad Meyer { 1857faefad9cSConrad Meyer 1858*2f03a95fSAlexander Motin mtx_assert(&ioat->submit_lock, MA_OWNED); 1859*2f03a95fSAlexander Motin KASSERT(ioat->refcnt < UINT32_MAX, ("refcnt overflow")); 1860*2f03a95fSAlexander Motin 1861*2f03a95fSAlexander Motin ioat->refcnt++; 1862faefad9cSConrad Meyer } 1863faefad9cSConrad Meyer 1864*2f03a95fSAlexander Motin static void 1865*2f03a95fSAlexander Motin ioat_put(struct ioat_softc *ioat) 1866faefad9cSConrad Meyer { 1867faefad9cSConrad Meyer 1868*2f03a95fSAlexander Motin mtx_assert(&ioat->submit_lock, MA_OWNED); 1869*2f03a95fSAlexander Motin KASSERT(ioat->refcnt >= 1, ("refcnt error")); 1870faefad9cSConrad Meyer 1871*2f03a95fSAlexander Motin if (--ioat->refcnt == 0) 1872*2f03a95fSAlexander Motin wakeup(&ioat->refcnt); 1873466b3540SConrad Meyer } 1874466b3540SConrad Meyer 1875466b3540SConrad Meyer static void 18765f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat) 1877466b3540SConrad Meyer { 1878466b3540SConrad Meyer 1879*2f03a95fSAlexander Motin mtx_assert(&ioat->submit_lock, MA_OWNED); 1880*2f03a95fSAlexander Motin 1881466b3540SConrad Meyer while (ioat->refcnt > 0) 1882*2f03a95fSAlexander Motin msleep(&ioat->refcnt, &ioat->submit_lock, 0, "ioat_drain", 0); 1883466b3540SConrad Meyer } 18840bb35debSConrad Meyer 18850bb35debSConrad Meyer #ifdef DDB 18860bb35debSConrad Meyer #define _db_show_lock(lo) LOCK_CLASS(lo)->lc_ddb_show(lo) 18870bb35debSConrad Meyer #define db_show_lock(lk) _db_show_lock(&(lk)->lock_object) 18880bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat) 18890bb35debSConrad Meyer { 18900bb35debSConrad Meyer struct ioat_softc *sc; 18910bb35debSConrad Meyer unsigned idx; 18920bb35debSConrad Meyer 18930bb35debSConrad Meyer if (!have_addr) 18940bb35debSConrad Meyer goto usage; 18950bb35debSConrad Meyer idx = (unsigned)addr; 189635b7a45eSConrad Meyer if (idx >= ioat_channel_index) 18970bb35debSConrad Meyer goto usage; 18980bb35debSConrad Meyer 18990bb35debSConrad Meyer sc = ioat_channel[idx]; 19000bb35debSConrad Meyer db_printf("ioat softc at %p\n", sc); 19010bb35debSConrad Meyer if (sc == NULL) 19020bb35debSConrad Meyer return; 19030bb35debSConrad Meyer 19040bb35debSConrad Meyer db_printf(" version: %d\n", sc->version); 19050bb35debSConrad Meyer db_printf(" chan_idx: %u\n", sc->chan_idx); 19060bb35debSConrad Meyer db_printf(" submit_lock: "); 19070bb35debSConrad Meyer db_show_lock(&sc->submit_lock); 19080bb35debSConrad Meyer 19090bb35debSConrad Meyer db_printf(" capabilities: %b\n", (int)sc->capabilities, 19100bb35debSConrad Meyer IOAT_DMACAP_STR); 19110bb35debSConrad Meyer db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay); 19120bb35debSConrad Meyer db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update); 19130bb35debSConrad Meyer 19145ac77963SConrad Meyer db_printf(" poll_timer:\n"); 19155ac77963SConrad Meyer db_printf(" c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time); 19165ac77963SConrad Meyer db_printf(" c_arg: %p\n", sc->poll_timer.c_arg); 19175ac77963SConrad Meyer db_printf(" c_func: %p\n", sc->poll_timer.c_func); 19185ac77963SConrad Meyer db_printf(" c_lock: %p\n", sc->poll_timer.c_lock); 19195ac77963SConrad Meyer db_printf(" c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags); 19205ac77963SConrad Meyer 19210bb35debSConrad Meyer db_printf(" quiescing: %d\n", (int)sc->quiescing); 19220bb35debSConrad Meyer db_printf(" destroying: %d\n", (int)sc->destroying); 192325ad9585SConrad Meyer db_printf(" is_submitter_processing: %d\n", 192425ad9585SConrad Meyer (int)sc->is_submitter_processing); 19250bb35debSConrad Meyer db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported); 192693f7f84aSConrad Meyer db_printf(" resetting: %d\n", (int)sc->resetting); 19270bb35debSConrad Meyer 19280bb35debSConrad Meyer db_printf(" head: %u\n", sc->head); 19290bb35debSConrad Meyer db_printf(" tail: %u\n", sc->tail); 19300bb35debSConrad Meyer db_printf(" ring_size_order: %u\n", sc->ring_size_order); 19310bb35debSConrad Meyer db_printf(" last_seen: 0x%lx\n", sc->last_seen); 19320bb35debSConrad Meyer db_printf(" ring: %p\n", sc->ring); 19338e269d99SConrad Meyer db_printf(" descriptors: %p\n", sc->hw_desc_ring); 19348e269d99SConrad Meyer db_printf(" descriptors (phys): 0x%jx\n", 19358e269d99SConrad Meyer (uintmax_t)sc->hw_desc_bus_addr); 19360bb35debSConrad Meyer 193735b7a45eSConrad Meyer db_printf(" ring[%u] (tail):\n", sc->tail % 193835b7a45eSConrad Meyer (1 << sc->ring_size_order)); 193935b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id); 194035b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 19418e269d99SConrad Meyer RING_PHYS_ADDR(sc, sc->tail)); 194235b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 19438e269d99SConrad Meyer ioat_get_descriptor(sc, sc->tail)->generic.next); 194435b7a45eSConrad Meyer 194535b7a45eSConrad Meyer db_printf(" ring[%u] (head - 1):\n", (sc->head - 1) % 194635b7a45eSConrad Meyer (1 << sc->ring_size_order)); 194735b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id); 194835b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 19498e269d99SConrad Meyer RING_PHYS_ADDR(sc, sc->head - 1)); 195035b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 19518e269d99SConrad Meyer ioat_get_descriptor(sc, sc->head - 1)->generic.next); 195235b7a45eSConrad Meyer 195335b7a45eSConrad Meyer db_printf(" ring[%u] (head):\n", (sc->head) % 195435b7a45eSConrad Meyer (1 << sc->ring_size_order)); 195535b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head)->id); 195635b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 19578e269d99SConrad Meyer RING_PHYS_ADDR(sc, sc->head)); 195835b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 19598e269d99SConrad Meyer ioat_get_descriptor(sc, sc->head)->generic.next); 196035b7a45eSConrad Meyer 196135b7a45eSConrad Meyer for (idx = 0; idx < (1 << sc->ring_size_order); idx++) 196235b7a45eSConrad Meyer if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK) 19638e269d99SConrad Meyer == RING_PHYS_ADDR(sc, idx)) 196435b7a45eSConrad Meyer db_printf(" ring[%u] == hardware tail\n", idx); 196535b7a45eSConrad Meyer 19660bb35debSConrad Meyer db_printf(" cleanup_lock: "); 19670bb35debSConrad Meyer db_show_lock(&sc->cleanup_lock); 19680bb35debSConrad Meyer 19690bb35debSConrad Meyer db_printf(" refcnt: %u\n", sc->refcnt); 19700bb35debSConrad Meyer db_printf(" stats:\n"); 19710bb35debSConrad Meyer db_printf(" interrupts: %lu\n", sc->stats.interrupts); 19720bb35debSConrad Meyer db_printf(" descriptors_processed: %lu\n", sc->stats.descriptors_processed); 19730bb35debSConrad Meyer db_printf(" descriptors_error: %lu\n", sc->stats.descriptors_error); 19740bb35debSConrad Meyer db_printf(" descriptors_submitted: %lu\n", sc->stats.descriptors_submitted); 19750bb35debSConrad Meyer 19760bb35debSConrad Meyer db_printf(" channel_halts: %u\n", sc->stats.channel_halts); 19770bb35debSConrad Meyer db_printf(" last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr); 19780bb35debSConrad Meyer 19790bb35debSConrad Meyer if (db_pager_quit) 19800bb35debSConrad Meyer return; 19810bb35debSConrad Meyer 19820bb35debSConrad Meyer db_printf(" hw status:\n"); 19830bb35debSConrad Meyer db_printf(" status: 0x%lx\n", ioat_get_chansts(sc)); 19840bb35debSConrad Meyer db_printf(" chanctrl: 0x%x\n", 19850bb35debSConrad Meyer (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET)); 19860bb35debSConrad Meyer db_printf(" chancmd: 0x%x\n", 19870bb35debSConrad Meyer (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET)); 19880bb35debSConrad Meyer db_printf(" dmacount: 0x%x\n", 19890bb35debSConrad Meyer (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET)); 19900bb35debSConrad Meyer db_printf(" chainaddr: 0x%lx\n", 19910bb35debSConrad Meyer ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW)); 19920bb35debSConrad Meyer db_printf(" chancmp: 0x%lx\n", 19930bb35debSConrad Meyer ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW)); 19940bb35debSConrad Meyer db_printf(" chanerr: %b\n", 19950bb35debSConrad Meyer (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR); 19960bb35debSConrad Meyer return; 19970bb35debSConrad Meyer usage: 19980bb35debSConrad Meyer db_printf("usage: show ioat <0-%u>\n", ioat_channel_index); 19990bb35debSConrad Meyer return; 20000bb35debSConrad Meyer } 20010bb35debSConrad Meyer #endif /* DDB */ 2002