1e974f91cSConrad Meyer /*- 2e974f91cSConrad Meyer * Copyright (C) 2012 Intel Corporation 3e974f91cSConrad Meyer * All rights reserved. 4e974f91cSConrad Meyer * 5e974f91cSConrad Meyer * Redistribution and use in source and binary forms, with or without 6e974f91cSConrad Meyer * modification, are permitted provided that the following conditions 7e974f91cSConrad Meyer * are met: 8e974f91cSConrad Meyer * 1. Redistributions of source code must retain the above copyright 9e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer. 10e974f91cSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 11e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer in the 12e974f91cSConrad Meyer * documentation and/or other materials provided with the distribution. 13e974f91cSConrad Meyer * 14e974f91cSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15e974f91cSConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16e974f91cSConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17e974f91cSConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18e974f91cSConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19e974f91cSConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20e974f91cSConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21e974f91cSConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22e974f91cSConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23e974f91cSConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24e974f91cSConrad Meyer * SUCH DAMAGE. 25e974f91cSConrad Meyer */ 26e974f91cSConrad Meyer 27e974f91cSConrad Meyer #include <sys/cdefs.h> 28e974f91cSConrad Meyer __FBSDID("$FreeBSD$"); 29e974f91cSConrad Meyer 30e974f91cSConrad Meyer #include <sys/param.h> 31e974f91cSConrad Meyer #include <sys/systm.h> 32e974f91cSConrad Meyer #include <sys/bus.h> 33e974f91cSConrad Meyer #include <sys/conf.h> 34e974f91cSConrad Meyer #include <sys/ioccom.h> 35e974f91cSConrad Meyer #include <sys/kernel.h> 36e974f91cSConrad Meyer #include <sys/lock.h> 37e974f91cSConrad Meyer #include <sys/malloc.h> 38e974f91cSConrad Meyer #include <sys/module.h> 39e974f91cSConrad Meyer #include <sys/mutex.h> 40e974f91cSConrad Meyer #include <sys/rman.h> 41faefad9cSConrad Meyer #include <sys/sbuf.h> 42e974f91cSConrad Meyer #include <sys/sysctl.h> 43e974f91cSConrad Meyer #include <sys/time.h> 44e974f91cSConrad Meyer #include <dev/pci/pcireg.h> 45e974f91cSConrad Meyer #include <dev/pci/pcivar.h> 46e974f91cSConrad Meyer #include <machine/bus.h> 47e974f91cSConrad Meyer #include <machine/resource.h> 48e974f91cSConrad Meyer #include <machine/stdarg.h> 49e974f91cSConrad Meyer 50e974f91cSConrad Meyer #include "ioat.h" 51e974f91cSConrad Meyer #include "ioat_hw.h" 52e974f91cSConrad Meyer #include "ioat_internal.h" 53e974f91cSConrad Meyer 54fe720f5aSConrad Meyer #define IOAT_INTR_TIMO (hz / 10) 55466b3540SConrad Meyer #define IOAT_REFLK (&ioat->submit_lock) 56fe720f5aSConrad Meyer 57e974f91cSConrad Meyer static int ioat_probe(device_t device); 58e974f91cSConrad Meyer static int ioat_attach(device_t device); 59e974f91cSConrad Meyer static int ioat_detach(device_t device); 604253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat); 614253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat); 62e974f91cSConrad Meyer static int ioat3_attach(device_t device); 63cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat); 64e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat); 65e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, 66e974f91cSConrad Meyer int error); 67e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg); 680d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat); 69faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t); 70e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat); 71e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat); 72e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat); 73bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size, 74bf8553eaSConrad Meyer struct ioat_descriptor **); 75e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat, 76e974f91cSConrad Meyer struct ioat_descriptor *desc); 77bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *, 78bf8553eaSConrad Meyer int mflags); 79bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags); 80e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat, 81e974f91cSConrad Meyer uint32_t index); 82bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *, 83bf8553eaSConrad Meyer uint32_t size, boolean_t need_dscr, int mflags); 84bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder, 85bf8553eaSConrad Meyer struct ioat_descriptor **); 86bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder, 87bf8553eaSConrad Meyer struct ioat_descriptor **); 88faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t); 89e974f91cSConrad Meyer static void ioat_timer_callback(void *arg); 90e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc); 91e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat); 92e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, 93e974f91cSConrad Meyer int error); 94e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat); 95e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device); 96f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS); 97466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *, 98466b3540SConrad Meyer enum ioat_ref_kind); 99466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind); 100faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t, 101faefad9cSConrad Meyer enum ioat_ref_kind, boolean_t); 102466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t, 103466b3540SConrad Meyer enum ioat_ref_kind); 104faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t, 105faefad9cSConrad Meyer enum ioat_ref_kind); 1065f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *); 107e974f91cSConrad Meyer 1081c25420eSConrad Meyer #define ioat_log_message(v, ...) do { \ 1091c25420eSConrad Meyer if ((v) <= g_ioat_debug_level) { \ 1101c25420eSConrad Meyer device_printf(ioat->device, __VA_ARGS__); \ 1111c25420eSConrad Meyer } \ 1121c25420eSConrad Meyer } while (0) 1131c25420eSConrad Meyer 114e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations"); 115e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node"); 116e974f91cSConrad Meyer 117e974f91cSConrad Meyer static int g_force_legacy_interrupts; 118e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN, 119e974f91cSConrad Meyer &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled"); 120e974f91cSConrad Meyer 1211c25420eSConrad Meyer int g_ioat_debug_level = 0; 122e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level, 123e974f91cSConrad Meyer 0, "Set log level (0-3) for ioat(4). Higher is more verbose."); 124e974f91cSConrad Meyer 125e974f91cSConrad Meyer /* 126e974f91cSConrad Meyer * OS <-> Driver interface structures 127e974f91cSConrad Meyer */ 128e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = { 129e974f91cSConrad Meyer /* Device interface */ 130e974f91cSConrad Meyer DEVMETHOD(device_probe, ioat_probe), 131e974f91cSConrad Meyer DEVMETHOD(device_attach, ioat_attach), 132e974f91cSConrad Meyer DEVMETHOD(device_detach, ioat_detach), 133e974f91cSConrad Meyer { 0, 0 } 134e974f91cSConrad Meyer }; 135e974f91cSConrad Meyer 136e974f91cSConrad Meyer static driver_t ioat_pci_driver = { 137e974f91cSConrad Meyer "ioat", 138e974f91cSConrad Meyer ioat_pci_methods, 139e974f91cSConrad Meyer sizeof(struct ioat_softc), 140e974f91cSConrad Meyer }; 141e974f91cSConrad Meyer 142e974f91cSConrad Meyer static devclass_t ioat_devclass; 143e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0); 144c2b69205SConrad Meyer MODULE_VERSION(ioat, 1); 145e974f91cSConrad Meyer 146e974f91cSConrad Meyer /* 147e974f91cSConrad Meyer * Private data structures 148e974f91cSConrad Meyer */ 149e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS]; 150e974f91cSConrad Meyer static int ioat_channel_index = 0; 151e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0, 152e974f91cSConrad Meyer "Number of IOAT channels attached"); 153e974f91cSConrad Meyer 154e974f91cSConrad Meyer static struct _pcsid 155e974f91cSConrad Meyer { 156e974f91cSConrad Meyer u_int32_t type; 157e974f91cSConrad Meyer const char *desc; 158e974f91cSConrad Meyer } pci_ids[] = { 159e974f91cSConrad Meyer { 0x34308086, "TBG IOAT Ch0" }, 160e974f91cSConrad Meyer { 0x34318086, "TBG IOAT Ch1" }, 161e974f91cSConrad Meyer { 0x34328086, "TBG IOAT Ch2" }, 162e974f91cSConrad Meyer { 0x34338086, "TBG IOAT Ch3" }, 163e974f91cSConrad Meyer { 0x34298086, "TBG IOAT Ch4" }, 164e974f91cSConrad Meyer { 0x342a8086, "TBG IOAT Ch5" }, 165e974f91cSConrad Meyer { 0x342b8086, "TBG IOAT Ch6" }, 166e974f91cSConrad Meyer { 0x342c8086, "TBG IOAT Ch7" }, 167e974f91cSConrad Meyer 168e974f91cSConrad Meyer { 0x37108086, "JSF IOAT Ch0" }, 169e974f91cSConrad Meyer { 0x37118086, "JSF IOAT Ch1" }, 170e974f91cSConrad Meyer { 0x37128086, "JSF IOAT Ch2" }, 171e974f91cSConrad Meyer { 0x37138086, "JSF IOAT Ch3" }, 172e974f91cSConrad Meyer { 0x37148086, "JSF IOAT Ch4" }, 173e974f91cSConrad Meyer { 0x37158086, "JSF IOAT Ch5" }, 174e974f91cSConrad Meyer { 0x37168086, "JSF IOAT Ch6" }, 175e974f91cSConrad Meyer { 0x37178086, "JSF IOAT Ch7" }, 176e974f91cSConrad Meyer { 0x37188086, "JSF IOAT Ch0 (RAID)" }, 177e974f91cSConrad Meyer { 0x37198086, "JSF IOAT Ch1 (RAID)" }, 178e974f91cSConrad Meyer 179e974f91cSConrad Meyer { 0x3c208086, "SNB IOAT Ch0" }, 180e974f91cSConrad Meyer { 0x3c218086, "SNB IOAT Ch1" }, 181e974f91cSConrad Meyer { 0x3c228086, "SNB IOAT Ch2" }, 182e974f91cSConrad Meyer { 0x3c238086, "SNB IOAT Ch3" }, 183e974f91cSConrad Meyer { 0x3c248086, "SNB IOAT Ch4" }, 184e974f91cSConrad Meyer { 0x3c258086, "SNB IOAT Ch5" }, 185e974f91cSConrad Meyer { 0x3c268086, "SNB IOAT Ch6" }, 186e974f91cSConrad Meyer { 0x3c278086, "SNB IOAT Ch7" }, 187e974f91cSConrad Meyer { 0x3c2e8086, "SNB IOAT Ch0 (RAID)" }, 188e974f91cSConrad Meyer { 0x3c2f8086, "SNB IOAT Ch1 (RAID)" }, 189e974f91cSConrad Meyer 190e974f91cSConrad Meyer { 0x0e208086, "IVB IOAT Ch0" }, 191e974f91cSConrad Meyer { 0x0e218086, "IVB IOAT Ch1" }, 192e974f91cSConrad Meyer { 0x0e228086, "IVB IOAT Ch2" }, 193e974f91cSConrad Meyer { 0x0e238086, "IVB IOAT Ch3" }, 194e974f91cSConrad Meyer { 0x0e248086, "IVB IOAT Ch4" }, 195e974f91cSConrad Meyer { 0x0e258086, "IVB IOAT Ch5" }, 196e974f91cSConrad Meyer { 0x0e268086, "IVB IOAT Ch6" }, 197e974f91cSConrad Meyer { 0x0e278086, "IVB IOAT Ch7" }, 198e974f91cSConrad Meyer { 0x0e2e8086, "IVB IOAT Ch0 (RAID)" }, 199e974f91cSConrad Meyer { 0x0e2f8086, "IVB IOAT Ch1 (RAID)" }, 200e974f91cSConrad Meyer 201e974f91cSConrad Meyer { 0x2f208086, "HSW IOAT Ch0" }, 202e974f91cSConrad Meyer { 0x2f218086, "HSW IOAT Ch1" }, 203e974f91cSConrad Meyer { 0x2f228086, "HSW IOAT Ch2" }, 204e974f91cSConrad Meyer { 0x2f238086, "HSW IOAT Ch3" }, 205e974f91cSConrad Meyer { 0x2f248086, "HSW IOAT Ch4" }, 206e974f91cSConrad Meyer { 0x2f258086, "HSW IOAT Ch5" }, 207e974f91cSConrad Meyer { 0x2f268086, "HSW IOAT Ch6" }, 208e974f91cSConrad Meyer { 0x2f278086, "HSW IOAT Ch7" }, 209e974f91cSConrad Meyer { 0x2f2e8086, "HSW IOAT Ch0 (RAID)" }, 210e974f91cSConrad Meyer { 0x2f2f8086, "HSW IOAT Ch1 (RAID)" }, 211e974f91cSConrad Meyer 212e974f91cSConrad Meyer { 0x0c508086, "BWD IOAT Ch0" }, 213e974f91cSConrad Meyer { 0x0c518086, "BWD IOAT Ch1" }, 214e974f91cSConrad Meyer { 0x0c528086, "BWD IOAT Ch2" }, 215e974f91cSConrad Meyer { 0x0c538086, "BWD IOAT Ch3" }, 216e974f91cSConrad Meyer 217e974f91cSConrad Meyer { 0x6f508086, "BDXDE IOAT Ch0" }, 218e974f91cSConrad Meyer { 0x6f518086, "BDXDE IOAT Ch1" }, 219e974f91cSConrad Meyer { 0x6f528086, "BDXDE IOAT Ch2" }, 220e974f91cSConrad Meyer { 0x6f538086, "BDXDE IOAT Ch3" }, 221e974f91cSConrad Meyer 2225afc2508SConrad Meyer { 0x6f208086, "BDX IOAT Ch0" }, 2235afc2508SConrad Meyer { 0x6f218086, "BDX IOAT Ch1" }, 2245afc2508SConrad Meyer { 0x6f228086, "BDX IOAT Ch2" }, 2255afc2508SConrad Meyer { 0x6f238086, "BDX IOAT Ch3" }, 2265afc2508SConrad Meyer { 0x6f248086, "BDX IOAT Ch4" }, 2275afc2508SConrad Meyer { 0x6f258086, "BDX IOAT Ch5" }, 2285afc2508SConrad Meyer { 0x6f268086, "BDX IOAT Ch6" }, 2295afc2508SConrad Meyer { 0x6f278086, "BDX IOAT Ch7" }, 2305afc2508SConrad Meyer { 0x6f2e8086, "BDX IOAT Ch0 (RAID)" }, 2315afc2508SConrad Meyer { 0x6f2f8086, "BDX IOAT Ch1 (RAID)" }, 2325afc2508SConrad Meyer 233e974f91cSConrad Meyer { 0x00000000, NULL } 234e974f91cSConrad Meyer }; 235e974f91cSConrad Meyer 236e974f91cSConrad Meyer /* 237e974f91cSConrad Meyer * OS <-> Driver linkage functions 238e974f91cSConrad Meyer */ 239e974f91cSConrad Meyer static int 240e974f91cSConrad Meyer ioat_probe(device_t device) 241e974f91cSConrad Meyer { 242e974f91cSConrad Meyer struct _pcsid *ep; 243e974f91cSConrad Meyer u_int32_t type; 244e974f91cSConrad Meyer 245e974f91cSConrad Meyer type = pci_get_devid(device); 246e974f91cSConrad Meyer for (ep = pci_ids; ep->type; ep++) { 247e974f91cSConrad Meyer if (ep->type == type) { 248e974f91cSConrad Meyer device_set_desc(device, ep->desc); 249e974f91cSConrad Meyer return (0); 250e974f91cSConrad Meyer } 251e974f91cSConrad Meyer } 252e974f91cSConrad Meyer return (ENXIO); 253e974f91cSConrad Meyer } 254e974f91cSConrad Meyer 255e974f91cSConrad Meyer static int 256e974f91cSConrad Meyer ioat_attach(device_t device) 257e974f91cSConrad Meyer { 258e974f91cSConrad Meyer struct ioat_softc *ioat; 259e974f91cSConrad Meyer int error; 260e974f91cSConrad Meyer 261e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 262e974f91cSConrad Meyer ioat->device = device; 263e974f91cSConrad Meyer 264e974f91cSConrad Meyer error = ioat_map_pci_bar(ioat); 265e974f91cSConrad Meyer if (error != 0) 266e974f91cSConrad Meyer goto err; 267e974f91cSConrad Meyer 268e974f91cSConrad Meyer ioat->version = ioat_read_cbver(ioat); 269e974f91cSConrad Meyer if (ioat->version < IOAT_VER_3_0) { 270e974f91cSConrad Meyer error = ENODEV; 271e974f91cSConrad Meyer goto err; 272e974f91cSConrad Meyer } 273e974f91cSConrad Meyer 274e974f91cSConrad Meyer error = ioat3_attach(device); 275e974f91cSConrad Meyer if (error != 0) 276e974f91cSConrad Meyer goto err; 277e974f91cSConrad Meyer 278e974f91cSConrad Meyer error = pci_enable_busmaster(device); 279e974f91cSConrad Meyer if (error != 0) 280e974f91cSConrad Meyer goto err; 281e974f91cSConrad Meyer 282466b3540SConrad Meyer error = ioat_setup_intr(ioat); 283466b3540SConrad Meyer if (error != 0) 284466b3540SConrad Meyer goto err; 285466b3540SConrad Meyer 286cea5b880SConrad Meyer error = ioat_reset_hw(ioat); 2877afbb263SConrad Meyer if (error != 0) 288466b3540SConrad Meyer goto err; 2897afbb263SConrad Meyer 2907afbb263SConrad Meyer ioat_process_events(ioat); 2917afbb263SConrad Meyer ioat_setup_sysctl(device); 2927afbb263SConrad Meyer 2935f77bd3eSConrad Meyer ioat->chan_idx = ioat_channel_index; 294e974f91cSConrad Meyer ioat_channel[ioat_channel_index++] = ioat; 2957afbb263SConrad Meyer ioat_test_attach(); 296e974f91cSConrad Meyer 297e974f91cSConrad Meyer err: 298e974f91cSConrad Meyer if (error != 0) 299e974f91cSConrad Meyer ioat_detach(device); 300e974f91cSConrad Meyer return (error); 301e974f91cSConrad Meyer } 302e974f91cSConrad Meyer 303e974f91cSConrad Meyer static int 304e974f91cSConrad Meyer ioat_detach(device_t device) 305e974f91cSConrad Meyer { 306e974f91cSConrad Meyer struct ioat_softc *ioat; 307e974f91cSConrad Meyer 308e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3097afbb263SConrad Meyer 3107afbb263SConrad Meyer ioat_test_detach(); 3115f77bd3eSConrad Meyer 3125f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 3135f77bd3eSConrad Meyer ioat->quiescing = TRUE; 3145f77bd3eSConrad Meyer ioat_channel[ioat->chan_idx] = NULL; 3155f77bd3eSConrad Meyer 3165f77bd3eSConrad Meyer ioat_drain_locked(ioat); 3175f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 318fe720f5aSConrad Meyer 319fe720f5aSConrad Meyer ioat_teardown_intr(ioat); 320e974f91cSConrad Meyer callout_drain(&ioat->timer); 321e974f91cSConrad Meyer 322e974f91cSConrad Meyer pci_disable_busmaster(device); 323e974f91cSConrad Meyer 324e974f91cSConrad Meyer if (ioat->pci_resource != NULL) 325e974f91cSConrad Meyer bus_release_resource(device, SYS_RES_MEMORY, 326e974f91cSConrad Meyer ioat->pci_resource_id, ioat->pci_resource); 327e974f91cSConrad Meyer 328bf8553eaSConrad Meyer if (ioat->ring != NULL) 329bf8553eaSConrad Meyer ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring); 330e974f91cSConrad Meyer 331e974f91cSConrad Meyer if (ioat->comp_update != NULL) { 332e974f91cSConrad Meyer bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map); 333e974f91cSConrad Meyer bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update, 334e974f91cSConrad Meyer ioat->comp_update_map); 335e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->comp_update_tag); 336e974f91cSConrad Meyer } 337e974f91cSConrad Meyer 338e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->hw_desc_tag); 339e974f91cSConrad Meyer 3404253ea50SConrad Meyer return (0); 3414253ea50SConrad Meyer } 3424253ea50SConrad Meyer 3434253ea50SConrad Meyer static int 3444253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat) 3454253ea50SConrad Meyer { 3464253ea50SConrad Meyer 347e974f91cSConrad Meyer if (ioat->tag != NULL) 3484253ea50SConrad Meyer bus_teardown_intr(ioat->device, ioat->res, ioat->tag); 349e974f91cSConrad Meyer 350e974f91cSConrad Meyer if (ioat->res != NULL) 3514253ea50SConrad Meyer bus_release_resource(ioat->device, SYS_RES_IRQ, 352e974f91cSConrad Meyer rman_get_rid(ioat->res), ioat->res); 353e974f91cSConrad Meyer 3544253ea50SConrad Meyer pci_release_msi(ioat->device); 355e974f91cSConrad Meyer return (0); 356e974f91cSConrad Meyer } 357e974f91cSConrad Meyer 358e974f91cSConrad Meyer static int 359cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat) 360e974f91cSConrad Meyer { 361e974f91cSConrad Meyer uint64_t status; 362e974f91cSConrad Meyer uint32_t chanerr; 363e974f91cSConrad Meyer int i; 364e974f91cSConrad Meyer 365e974f91cSConrad Meyer ioat_acquire(&ioat->dmaengine); 366e974f91cSConrad Meyer ioat_null(&ioat->dmaengine, NULL, NULL, 0); 367e974f91cSConrad Meyer ioat_release(&ioat->dmaengine); 368e974f91cSConrad Meyer 369e974f91cSConrad Meyer for (i = 0; i < 100; i++) { 370e974f91cSConrad Meyer DELAY(1); 371e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 372e974f91cSConrad Meyer if (is_ioat_idle(status)) 373e974f91cSConrad Meyer return (0); 374e974f91cSConrad Meyer } 375e974f91cSConrad Meyer 376e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 377e974f91cSConrad Meyer ioat_log_message(0, "could not start channel: " 37859acd4baSConrad Meyer "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr, 37959acd4baSConrad Meyer IOAT_CHANERR_STR); 380e974f91cSConrad Meyer return (ENXIO); 381e974f91cSConrad Meyer } 382e974f91cSConrad Meyer 383e974f91cSConrad Meyer /* 384e974f91cSConrad Meyer * Initialize Hardware 385e974f91cSConrad Meyer */ 386e974f91cSConrad Meyer static int 387e974f91cSConrad Meyer ioat3_attach(device_t device) 388e974f91cSConrad Meyer { 389e974f91cSConrad Meyer struct ioat_softc *ioat; 390e974f91cSConrad Meyer struct ioat_descriptor **ring; 391e974f91cSConrad Meyer struct ioat_descriptor *next; 392e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *dma_hw_desc; 393e974f91cSConrad Meyer int i, num_descriptors; 394e974f91cSConrad Meyer int error; 395e974f91cSConrad Meyer uint8_t xfercap; 396e974f91cSConrad Meyer 397e974f91cSConrad Meyer error = 0; 398e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3991693d27bSConrad Meyer ioat->capabilities = ioat_read_dmacapability(ioat); 4001693d27bSConrad Meyer 4011693d27bSConrad Meyer ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities, 4021693d27bSConrad Meyer IOAT_DMACAP_STR); 403e974f91cSConrad Meyer 404e974f91cSConrad Meyer xfercap = ioat_read_xfercap(ioat); 405e974f91cSConrad Meyer ioat->max_xfer_size = 1 << xfercap; 406e974f91cSConrad Meyer 4075ca9fc2aSConrad Meyer ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & 4085ca9fc2aSConrad Meyer IOAT_INTRDELAY_SUPPORTED) != 0; 4095ca9fc2aSConrad Meyer if (ioat->intrdelay_supported) 4105ca9fc2aSConrad Meyer ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK; 4115ca9fc2aSConrad Meyer 412e974f91cSConrad Meyer /* TODO: need to check DCA here if we ever do XOR/PQ */ 413e974f91cSConrad Meyer 414e974f91cSConrad Meyer mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF); 415faefad9cSConrad Meyer mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF); 4167afbb263SConrad Meyer callout_init(&ioat->timer, 1); 417e974f91cSConrad Meyer 418faefad9cSConrad Meyer /* Establish lock order for Witness */ 419faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 420faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 421faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 422faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 423faefad9cSConrad Meyer 424e974f91cSConrad Meyer ioat->is_resize_pending = FALSE; 425e974f91cSConrad Meyer ioat->is_completion_pending = FALSE; 426e974f91cSConrad Meyer ioat->is_reset_pending = FALSE; 427e974f91cSConrad Meyer ioat->is_channel_running = FALSE; 428e974f91cSConrad Meyer 429e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0, 430e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 431e974f91cSConrad Meyer sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL, 432e974f91cSConrad Meyer &ioat->comp_update_tag); 433e974f91cSConrad Meyer 434e974f91cSConrad Meyer error = bus_dmamem_alloc(ioat->comp_update_tag, 435e974f91cSConrad Meyer (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map); 436e974f91cSConrad Meyer if (ioat->comp_update == NULL) 437e974f91cSConrad Meyer return (ENOMEM); 438e974f91cSConrad Meyer 439e974f91cSConrad Meyer error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map, 440e974f91cSConrad Meyer ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat, 441e974f91cSConrad Meyer 0); 442e974f91cSConrad Meyer if (error != 0) 443e974f91cSConrad Meyer return (error); 444e974f91cSConrad Meyer 445e974f91cSConrad Meyer ioat->ring_size_order = IOAT_MIN_ORDER; 446e974f91cSConrad Meyer 447e974f91cSConrad Meyer num_descriptors = 1 << ioat->ring_size_order; 448e974f91cSConrad Meyer 449e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0, 450e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 451e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 1, 452e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL, 453e974f91cSConrad Meyer &ioat->hw_desc_tag); 454e974f91cSConrad Meyer 455e974f91cSConrad Meyer ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT, 456bf8553eaSConrad Meyer M_ZERO | M_WAITOK); 457e974f91cSConrad Meyer if (ioat->ring == NULL) 458e974f91cSConrad Meyer return (ENOMEM); 459e974f91cSConrad Meyer 460e974f91cSConrad Meyer ring = ioat->ring; 461e974f91cSConrad Meyer for (i = 0; i < num_descriptors; i++) { 462bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK); 463e974f91cSConrad Meyer if (ring[i] == NULL) 464e974f91cSConrad Meyer return (ENOMEM); 465e974f91cSConrad Meyer 466e974f91cSConrad Meyer ring[i]->id = i; 467e974f91cSConrad Meyer } 468e974f91cSConrad Meyer 469e974f91cSConrad Meyer for (i = 0; i < num_descriptors - 1; i++) { 470e974f91cSConrad Meyer next = ring[i + 1]; 471e974f91cSConrad Meyer dma_hw_desc = ring[i]->u.dma; 472e974f91cSConrad Meyer 473e974f91cSConrad Meyer dma_hw_desc->next = next->hw_desc_bus_addr; 474e974f91cSConrad Meyer } 475e974f91cSConrad Meyer 476e974f91cSConrad Meyer ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr; 477e974f91cSConrad Meyer 478bf8553eaSConrad Meyer ioat->head = ioat->hw_head = 0; 479e974f91cSConrad Meyer ioat->tail = 0; 480e974f91cSConrad Meyer ioat->last_seen = 0; 481e974f91cSConrad Meyer return (0); 482e974f91cSConrad Meyer } 483e974f91cSConrad Meyer 484e974f91cSConrad Meyer static int 485e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat) 486e974f91cSConrad Meyer { 487e974f91cSConrad Meyer 488e974f91cSConrad Meyer ioat->pci_resource_id = PCIR_BAR(0); 489e88e14b9SConrad Meyer ioat->pci_resource = bus_alloc_resource_any(ioat->device, 490e88e14b9SConrad Meyer SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE); 491e974f91cSConrad Meyer 492e974f91cSConrad Meyer if (ioat->pci_resource == NULL) { 493e974f91cSConrad Meyer ioat_log_message(0, "unable to allocate pci resource\n"); 494e974f91cSConrad Meyer return (ENODEV); 495e974f91cSConrad Meyer } 496e974f91cSConrad Meyer 497e974f91cSConrad Meyer ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource); 498e974f91cSConrad Meyer ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource); 499e974f91cSConrad Meyer return (0); 500e974f91cSConrad Meyer } 501e974f91cSConrad Meyer 502e974f91cSConrad Meyer static void 503e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 504e974f91cSConrad Meyer { 505e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 506e974f91cSConrad Meyer 507cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 508e974f91cSConrad Meyer ioat->comp_update_bus_addr = seg[0].ds_addr; 509e974f91cSConrad Meyer } 510e974f91cSConrad Meyer 511e974f91cSConrad Meyer static void 512e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 513e974f91cSConrad Meyer { 514e974f91cSConrad Meyer bus_addr_t *baddr; 515e974f91cSConrad Meyer 516cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 517e974f91cSConrad Meyer baddr = arg; 518e974f91cSConrad Meyer *baddr = segs->ds_addr; 519e974f91cSConrad Meyer } 520e974f91cSConrad Meyer 521e974f91cSConrad Meyer /* 522e974f91cSConrad Meyer * Interrupt setup and handlers 523e974f91cSConrad Meyer */ 524e974f91cSConrad Meyer static int 5254253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat) 526e974f91cSConrad Meyer { 527e974f91cSConrad Meyer uint32_t num_vectors; 528e974f91cSConrad Meyer int error; 529e974f91cSConrad Meyer boolean_t use_msix; 530e974f91cSConrad Meyer boolean_t force_legacy_interrupts; 531e974f91cSConrad Meyer 532e974f91cSConrad Meyer use_msix = FALSE; 533e974f91cSConrad Meyer force_legacy_interrupts = FALSE; 534e974f91cSConrad Meyer 535e974f91cSConrad Meyer if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) { 536e974f91cSConrad Meyer num_vectors = 1; 537e974f91cSConrad Meyer pci_alloc_msix(ioat->device, &num_vectors); 538e974f91cSConrad Meyer if (num_vectors == 1) 539e974f91cSConrad Meyer use_msix = TRUE; 540e974f91cSConrad Meyer } 541e974f91cSConrad Meyer 542e974f91cSConrad Meyer if (use_msix) { 543e974f91cSConrad Meyer ioat->rid = 1; 544e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 545e974f91cSConrad Meyer &ioat->rid, RF_ACTIVE); 546e974f91cSConrad Meyer } else { 547e974f91cSConrad Meyer ioat->rid = 0; 548e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 549e974f91cSConrad Meyer &ioat->rid, RF_SHAREABLE | RF_ACTIVE); 550e974f91cSConrad Meyer } 551e974f91cSConrad Meyer if (ioat->res == NULL) { 552e974f91cSConrad Meyer ioat_log_message(0, "bus_alloc_resource failed\n"); 553e974f91cSConrad Meyer return (ENOMEM); 554e974f91cSConrad Meyer } 555e974f91cSConrad Meyer 556e974f91cSConrad Meyer ioat->tag = NULL; 557e974f91cSConrad Meyer error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE | 558e974f91cSConrad Meyer INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag); 559e974f91cSConrad Meyer if (error != 0) { 560e974f91cSConrad Meyer ioat_log_message(0, "bus_setup_intr failed\n"); 561e974f91cSConrad Meyer return (error); 562e974f91cSConrad Meyer } 563e974f91cSConrad Meyer 564e974f91cSConrad Meyer ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN); 565e974f91cSConrad Meyer return (0); 566e974f91cSConrad Meyer } 567e974f91cSConrad Meyer 5684253ea50SConrad Meyer static boolean_t 5690d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat) 5704253ea50SConrad Meyer { 5714253ea50SConrad Meyer u_int32_t pciid; 5724253ea50SConrad Meyer 5734253ea50SConrad Meyer pciid = pci_get_devid(ioat->device); 5744253ea50SConrad Meyer switch (pciid) { 5750d1a05d9SConrad Meyer /* BWD: */ 5760d1a05d9SConrad Meyer case 0x0c508086: 5770d1a05d9SConrad Meyer case 0x0c518086: 5780d1a05d9SConrad Meyer case 0x0c528086: 5790d1a05d9SConrad Meyer case 0x0c538086: 5800d1a05d9SConrad Meyer /* BDXDE: */ 5814253ea50SConrad Meyer case 0x6f508086: 5824253ea50SConrad Meyer case 0x6f518086: 5834253ea50SConrad Meyer case 0x6f528086: 5844253ea50SConrad Meyer case 0x6f538086: 5854253ea50SConrad Meyer return (TRUE); 5864253ea50SConrad Meyer } 5874253ea50SConrad Meyer 5884253ea50SConrad Meyer return (FALSE); 5894253ea50SConrad Meyer } 5904253ea50SConrad Meyer 591e974f91cSConrad Meyer static void 592e974f91cSConrad Meyer ioat_interrupt_handler(void *arg) 593e974f91cSConrad Meyer { 594e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 595e974f91cSConrad Meyer 59601fbbc88SConrad Meyer ioat->stats.interrupts++; 597e974f91cSConrad Meyer ioat_process_events(ioat); 598e974f91cSConrad Meyer } 599e974f91cSConrad Meyer 600faefad9cSConrad Meyer static int 601faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr) 602faefad9cSConrad Meyer { 603faefad9cSConrad Meyer 604faefad9cSConrad Meyer if (chanerr == 0) 605faefad9cSConrad Meyer return (0); 606faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0) 607faefad9cSConrad Meyer return (EFAULT); 608faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0) 609faefad9cSConrad Meyer return (EIO); 610faefad9cSConrad Meyer /* This one is probably our fault: */ 611faefad9cSConrad Meyer if ((chanerr & IOAT_CHANERR_NDADDERR) != 0) 612faefad9cSConrad Meyer return (EIO); 613faefad9cSConrad Meyer return (EIO); 614faefad9cSConrad Meyer } 615faefad9cSConrad Meyer 616e974f91cSConrad Meyer static void 617e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat) 618e974f91cSConrad Meyer { 619e974f91cSConrad Meyer struct ioat_descriptor *desc; 620e974f91cSConrad Meyer struct bus_dmadesc *dmadesc; 621e974f91cSConrad Meyer uint64_t comp_update, status; 622faefad9cSConrad Meyer uint32_t completed, chanerr; 623faefad9cSConrad Meyer int error; 624e974f91cSConrad Meyer 625e974f91cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 626e974f91cSConrad Meyer 627e974f91cSConrad Meyer completed = 0; 628e974f91cSConrad Meyer comp_update = *ioat->comp_update; 629e974f91cSConrad Meyer status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK; 630e974f91cSConrad Meyer 63143fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 632e974f91cSConrad Meyer 6334becebdfSConrad Meyer if (status == ioat->last_seen) 6344becebdfSConrad Meyer goto out; 635e974f91cSConrad Meyer 636e974f91cSConrad Meyer while (1) { 637e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 638e974f91cSConrad Meyer dmadesc = &desc->bus_dmadesc; 63943fc1847SConrad Meyer CTR1(KTR_IOAT, "completing desc %d", ioat->tail); 640e974f91cSConrad Meyer 641faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 642faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 0); 643e974f91cSConrad Meyer 644466b3540SConrad Meyer completed++; 645e974f91cSConrad Meyer ioat->tail++; 646e974f91cSConrad Meyer if (desc->hw_desc_bus_addr == status) 647e974f91cSConrad Meyer break; 648e974f91cSConrad Meyer } 649e974f91cSConrad Meyer 650e974f91cSConrad Meyer ioat->last_seen = desc->hw_desc_bus_addr; 651e974f91cSConrad Meyer 652e974f91cSConrad Meyer if (ioat->head == ioat->tail) { 653e974f91cSConrad Meyer ioat->is_completion_pending = FALSE; 654fe720f5aSConrad Meyer callout_reset(&ioat->timer, IOAT_INTR_TIMO, 655fe720f5aSConrad Meyer ioat_timer_callback, ioat); 656e974f91cSConrad Meyer } 657e974f91cSConrad Meyer 65801fbbc88SConrad Meyer ioat->stats.descriptors_processed += completed; 65901fbbc88SConrad Meyer 6604becebdfSConrad Meyer out: 661e974f91cSConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 662e974f91cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 663466b3540SConrad Meyer 664466b3540SConrad Meyer ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF); 665bf8553eaSConrad Meyer wakeup(&ioat->tail); 666faefad9cSConrad Meyer 667faefad9cSConrad Meyer if (!is_ioat_halted(comp_update)) 668faefad9cSConrad Meyer return; 669faefad9cSConrad Meyer 67001fbbc88SConrad Meyer ioat->stats.channel_halts++; 67101fbbc88SConrad Meyer 672faefad9cSConrad Meyer /* 673faefad9cSConrad Meyer * Fatal programming error on this DMA channel. Flush any outstanding 674faefad9cSConrad Meyer * work with error status and restart the engine. 675faefad9cSConrad Meyer */ 676faefad9cSConrad Meyer ioat_log_message(0, "Channel halted due to fatal programming error\n"); 677faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 678faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 679faefad9cSConrad Meyer ioat->quiescing = TRUE; 680faefad9cSConrad Meyer 681faefad9cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 682faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 68301fbbc88SConrad Meyer ioat->stats.last_halt_chanerr = chanerr; 684faefad9cSConrad Meyer 685faefad9cSConrad Meyer while (ioat_get_active(ioat) > 0) { 686faefad9cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 687faefad9cSConrad Meyer dmadesc = &desc->bus_dmadesc; 688faefad9cSConrad Meyer CTR1(KTR_IOAT, "completing err desc %d", ioat->tail); 689faefad9cSConrad Meyer 690faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 691faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 692faefad9cSConrad Meyer chanerr_to_errno(chanerr)); 693faefad9cSConrad Meyer 694faefad9cSConrad Meyer ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF); 695faefad9cSConrad Meyer ioat->tail++; 69601fbbc88SConrad Meyer ioat->stats.descriptors_processed++; 69701fbbc88SConrad Meyer ioat->stats.descriptors_error++; 698faefad9cSConrad Meyer } 699faefad9cSConrad Meyer 700faefad9cSConrad Meyer /* Clear error status */ 701faefad9cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 702faefad9cSConrad Meyer 703faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 704faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 705faefad9cSConrad Meyer 706faefad9cSConrad Meyer ioat_log_message(0, "Resetting channel to recover from error\n"); 707faefad9cSConrad Meyer error = ioat_reset_hw(ioat); 708faefad9cSConrad Meyer KASSERT(error == 0, ("%s: reset failed: %d", __func__, error)); 709e974f91cSConrad Meyer } 710e974f91cSConrad Meyer 711e974f91cSConrad Meyer /* 712e974f91cSConrad Meyer * User API functions 713e974f91cSConrad Meyer */ 714e974f91cSConrad Meyer bus_dmaengine_t 715e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index) 716e974f91cSConrad Meyer { 7175f77bd3eSConrad Meyer struct ioat_softc *sc; 718e974f91cSConrad Meyer 719466b3540SConrad Meyer if (index >= ioat_channel_index) 720e974f91cSConrad Meyer return (NULL); 7215f77bd3eSConrad Meyer 7225f77bd3eSConrad Meyer sc = ioat_channel[index]; 7235f77bd3eSConrad Meyer if (sc == NULL || sc->quiescing) 7245f77bd3eSConrad Meyer return (NULL); 7255f77bd3eSConrad Meyer 7265f77bd3eSConrad Meyer return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine); 727466b3540SConrad Meyer } 728466b3540SConrad Meyer 729466b3540SConrad Meyer void 730466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine) 731466b3540SConrad Meyer { 732466b3540SConrad Meyer struct ioat_softc *ioat; 733466b3540SConrad Meyer 734466b3540SConrad Meyer ioat = to_ioat_softc(dmaengine); 735466b3540SConrad Meyer ioat_put(ioat, IOAT_DMAENGINE_REF); 736e974f91cSConrad Meyer } 737e974f91cSConrad Meyer 7385ca9fc2aSConrad Meyer int 739*31bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine) 740*31bf2875SConrad Meyer { 741*31bf2875SConrad Meyer struct ioat_softc *ioat; 742*31bf2875SConrad Meyer 743*31bf2875SConrad Meyer ioat = to_ioat_softc(dmaengine); 744*31bf2875SConrad Meyer return (ioat->version); 745*31bf2875SConrad Meyer } 746*31bf2875SConrad Meyer 747*31bf2875SConrad Meyer int 7485ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay) 7495ca9fc2aSConrad Meyer { 7505ca9fc2aSConrad Meyer struct ioat_softc *ioat; 7515ca9fc2aSConrad Meyer 7525ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 7535ca9fc2aSConrad Meyer if (!ioat->intrdelay_supported) 7545ca9fc2aSConrad Meyer return (ENODEV); 7555ca9fc2aSConrad Meyer if (delay > ioat->intrdelay_max) 7565ca9fc2aSConrad Meyer return (ERANGE); 7575ca9fc2aSConrad Meyer 7585ca9fc2aSConrad Meyer ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay); 7595ca9fc2aSConrad Meyer ioat->cached_intrdelay = 7605ca9fc2aSConrad Meyer ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK; 7615ca9fc2aSConrad Meyer return (0); 7625ca9fc2aSConrad Meyer } 7635ca9fc2aSConrad Meyer 7645ca9fc2aSConrad Meyer uint16_t 7655ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine) 7665ca9fc2aSConrad Meyer { 7675ca9fc2aSConrad Meyer struct ioat_softc *ioat; 7685ca9fc2aSConrad Meyer 7695ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 7705ca9fc2aSConrad Meyer return (ioat->intrdelay_max); 7715ca9fc2aSConrad Meyer } 7725ca9fc2aSConrad Meyer 773e974f91cSConrad Meyer void 774e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine) 775e974f91cSConrad Meyer { 776e974f91cSConrad Meyer struct ioat_softc *ioat; 777e974f91cSConrad Meyer 778e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 779e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 78043fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 781e974f91cSConrad Meyer } 782e974f91cSConrad Meyer 783e974f91cSConrad Meyer void 784e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine) 785e974f91cSConrad Meyer { 786e974f91cSConrad Meyer struct ioat_softc *ioat; 787e974f91cSConrad Meyer 788e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 78943fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 790bf8553eaSConrad Meyer ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head); 791e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 792e974f91cSConrad Meyer } 793e974f91cSConrad Meyer 7949e3bbf26SConrad Meyer static struct ioat_descriptor * 7959e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op, 7969e3bbf26SConrad Meyer uint32_t size, uint64_t src, uint64_t dst, 7979e3bbf26SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, 7989e3bbf26SConrad Meyer uint32_t flags) 799e974f91cSConrad Meyer { 8009e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 801e974f91cSConrad Meyer struct ioat_descriptor *desc; 802bf8553eaSConrad Meyer int mflags; 803e974f91cSConrad Meyer 8049e3bbf26SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 8059e3bbf26SConrad Meyer 806e974f91cSConrad Meyer KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x", 807e974f91cSConrad Meyer flags & ~DMA_ALL_FLAGS)); 808bf8553eaSConrad Meyer if ((flags & DMA_NO_WAIT) != 0) 809bf8553eaSConrad Meyer mflags = M_NOWAIT; 810bf8553eaSConrad Meyer else 811bf8553eaSConrad Meyer mflags = M_WAITOK; 812e974f91cSConrad Meyer 8139e3bbf26SConrad Meyer if (size > ioat->max_xfer_size) { 8149e3bbf26SConrad Meyer ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n", 8159e3bbf26SConrad Meyer __func__, ioat->max_xfer_size, (unsigned)size); 8169e3bbf26SConrad Meyer return (NULL); 8179e3bbf26SConrad Meyer } 818e974f91cSConrad Meyer 819bf8553eaSConrad Meyer if (ioat_reserve_space(ioat, 1, mflags) != 0) 820e974f91cSConrad Meyer return (NULL); 821e974f91cSConrad Meyer 822e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 8239e3bbf26SConrad Meyer hw_desc = desc->u.generic; 824e974f91cSConrad Meyer 825e974f91cSConrad Meyer hw_desc->u.control_raw = 0; 8269e3bbf26SConrad Meyer hw_desc->u.control_generic.op = op; 8279e3bbf26SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 828e974f91cSConrad Meyer 829e974f91cSConrad Meyer if ((flags & DMA_INT_EN) != 0) 8309e3bbf26SConrad Meyer hw_desc->u.control_generic.int_enable = 1; 831e974f91cSConrad Meyer 8329e3bbf26SConrad Meyer hw_desc->size = size; 8339e3bbf26SConrad Meyer hw_desc->src_addr = src; 8349e3bbf26SConrad Meyer hw_desc->dest_addr = dst; 835e974f91cSConrad Meyer 836e974f91cSConrad Meyer desc->bus_dmadesc.callback_fn = callback_fn; 837e974f91cSConrad Meyer desc->bus_dmadesc.callback_arg = callback_arg; 8389e3bbf26SConrad Meyer return (desc); 8399e3bbf26SConrad Meyer } 840e974f91cSConrad Meyer 8419e3bbf26SConrad Meyer struct bus_dmadesc * 8429e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn, 8439e3bbf26SConrad Meyer void *callback_arg, uint32_t flags) 8449e3bbf26SConrad Meyer { 8459e3bbf26SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 8469e3bbf26SConrad Meyer struct ioat_descriptor *desc; 8479e3bbf26SConrad Meyer struct ioat_softc *ioat; 8489e3bbf26SConrad Meyer 8499e3bbf26SConrad Meyer CTR0(KTR_IOAT, __func__); 8509e3bbf26SConrad Meyer ioat = to_ioat_softc(dmaengine); 8519e3bbf26SConrad Meyer 8529e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn, 8539e3bbf26SConrad Meyer callback_arg, flags); 8549e3bbf26SConrad Meyer if (desc == NULL) 8559e3bbf26SConrad Meyer return (NULL); 8569e3bbf26SConrad Meyer 8579e3bbf26SConrad Meyer hw_desc = desc->u.dma; 8589e3bbf26SConrad Meyer hw_desc->u.control.null = 1; 859e974f91cSConrad Meyer ioat_submit_single(ioat); 860e974f91cSConrad Meyer return (&desc->bus_dmadesc); 861e974f91cSConrad Meyer } 862e974f91cSConrad Meyer 863e974f91cSConrad Meyer struct bus_dmadesc * 864e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst, 865e974f91cSConrad Meyer bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn, 866e974f91cSConrad Meyer void *callback_arg, uint32_t flags) 867e974f91cSConrad Meyer { 868e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 8699e3bbf26SConrad Meyer struct ioat_descriptor *desc; 870e974f91cSConrad Meyer struct ioat_softc *ioat; 871e974f91cSConrad Meyer 8729e3bbf26SConrad Meyer CTR0(KTR_IOAT, __func__); 873e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 874e974f91cSConrad Meyer 8759e3bbf26SConrad Meyer if (((src | dst) & (0xffffull << 48)) != 0) { 8769e3bbf26SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 8779e3bbf26SConrad Meyer __func__); 878e974f91cSConrad Meyer return (NULL); 879e974f91cSConrad Meyer } 880e974f91cSConrad Meyer 8819e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn, 8829e3bbf26SConrad Meyer callback_arg, flags); 8839e3bbf26SConrad Meyer if (desc == NULL) 884e974f91cSConrad Meyer return (NULL); 885e974f91cSConrad Meyer 886e974f91cSConrad Meyer hw_desc = desc->u.dma; 887e974f91cSConrad Meyer if (g_ioat_debug_level >= 3) 888e974f91cSConrad Meyer dump_descriptor(hw_desc); 889e974f91cSConrad Meyer 890e974f91cSConrad Meyer ioat_submit_single(ioat); 891e974f91cSConrad Meyer return (&desc->bus_dmadesc); 892e974f91cSConrad Meyer } 893e974f91cSConrad Meyer 8942a4fd6b1SConrad Meyer struct bus_dmadesc * 8959950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1, 8969950fde0SConrad Meyer bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2, 8979950fde0SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 8989950fde0SConrad Meyer { 8999950fde0SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 9009950fde0SConrad Meyer struct ioat_descriptor *desc; 9019950fde0SConrad Meyer struct ioat_softc *ioat; 9029950fde0SConrad Meyer 9039950fde0SConrad Meyer CTR0(KTR_IOAT, __func__); 9049950fde0SConrad Meyer ioat = to_ioat_softc(dmaengine); 9059950fde0SConrad Meyer 9069950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) { 9079950fde0SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 9089950fde0SConrad Meyer __func__); 9099950fde0SConrad Meyer return (NULL); 9109950fde0SConrad Meyer } 9119950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) { 9129950fde0SConrad Meyer ioat_log_message(0, "%s: Addresses must be page-aligned\n", 9139950fde0SConrad Meyer __func__); 9149950fde0SConrad Meyer return (NULL); 9159950fde0SConrad Meyer } 9169950fde0SConrad Meyer 9179950fde0SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1, 9189950fde0SConrad Meyer callback_fn, callback_arg, flags); 9199950fde0SConrad Meyer if (desc == NULL) 9209950fde0SConrad Meyer return (NULL); 9219950fde0SConrad Meyer 9229950fde0SConrad Meyer hw_desc = desc->u.dma; 9239950fde0SConrad Meyer if (src2 != src1 + PAGE_SIZE) { 9249950fde0SConrad Meyer hw_desc->u.control.src_page_break = 1; 9259950fde0SConrad Meyer hw_desc->next_src_addr = src2; 9269950fde0SConrad Meyer } 9279950fde0SConrad Meyer if (dst2 != dst1 + PAGE_SIZE) { 9289950fde0SConrad Meyer hw_desc->u.control.dest_page_break = 1; 9299950fde0SConrad Meyer hw_desc->next_dest_addr = dst2; 9309950fde0SConrad Meyer } 9319950fde0SConrad Meyer 9329950fde0SConrad Meyer if (g_ioat_debug_level >= 3) 9339950fde0SConrad Meyer dump_descriptor(hw_desc); 9349950fde0SConrad Meyer 9359950fde0SConrad Meyer ioat_submit_single(ioat); 9369950fde0SConrad Meyer return (&desc->bus_dmadesc); 9379950fde0SConrad Meyer } 9389950fde0SConrad Meyer 9399950fde0SConrad Meyer struct bus_dmadesc * 9402a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern, 9412a4fd6b1SConrad Meyer bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg, 9422a4fd6b1SConrad Meyer uint32_t flags) 9432a4fd6b1SConrad Meyer { 9442a4fd6b1SConrad Meyer struct ioat_fill_hw_descriptor *hw_desc; 9452a4fd6b1SConrad Meyer struct ioat_descriptor *desc; 9462a4fd6b1SConrad Meyer struct ioat_softc *ioat; 9472a4fd6b1SConrad Meyer 9482a4fd6b1SConrad Meyer CTR0(KTR_IOAT, __func__); 9492a4fd6b1SConrad Meyer ioat = to_ioat_softc(dmaengine); 9502a4fd6b1SConrad Meyer 9511693d27bSConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) { 9521693d27bSConrad Meyer ioat_log_message(0, "%s: Device lacks BFILL capability\n", 9531693d27bSConrad Meyer __func__); 9541693d27bSConrad Meyer return (NULL); 9551693d27bSConrad Meyer } 9561693d27bSConrad Meyer 9572a4fd6b1SConrad Meyer if ((dst & (0xffffull << 48)) != 0) { 9582a4fd6b1SConrad Meyer ioat_log_message(0, "%s: High 16 bits of dst invalid\n", 9592a4fd6b1SConrad Meyer __func__); 9602a4fd6b1SConrad Meyer return (NULL); 9612a4fd6b1SConrad Meyer } 9622a4fd6b1SConrad Meyer 9632a4fd6b1SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst, 9642a4fd6b1SConrad Meyer callback_fn, callback_arg, flags); 9652a4fd6b1SConrad Meyer if (desc == NULL) 9662a4fd6b1SConrad Meyer return (NULL); 9672a4fd6b1SConrad Meyer 9682a4fd6b1SConrad Meyer hw_desc = desc->u.fill; 9692a4fd6b1SConrad Meyer if (g_ioat_debug_level >= 3) 9702a4fd6b1SConrad Meyer dump_descriptor(hw_desc); 9712a4fd6b1SConrad Meyer 9722a4fd6b1SConrad Meyer ioat_submit_single(ioat); 9732a4fd6b1SConrad Meyer return (&desc->bus_dmadesc); 9742a4fd6b1SConrad Meyer } 9752a4fd6b1SConrad Meyer 976e974f91cSConrad Meyer /* 977e974f91cSConrad Meyer * Ring Management 978e974f91cSConrad Meyer */ 979e974f91cSConrad Meyer static inline uint32_t 980e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat) 981e974f91cSConrad Meyer { 982e974f91cSConrad Meyer 983e974f91cSConrad Meyer return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1)); 984e974f91cSConrad Meyer } 985e974f91cSConrad Meyer 986e974f91cSConrad Meyer static inline uint32_t 987e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat) 988e974f91cSConrad Meyer { 989e974f91cSConrad Meyer 990e974f91cSConrad Meyer return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1); 991e974f91cSConrad Meyer } 992e974f91cSConrad Meyer 993e974f91cSConrad Meyer static struct ioat_descriptor * 994bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags) 995e974f91cSConrad Meyer { 9969e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 997e974f91cSConrad Meyer struct ioat_descriptor *desc; 998bf8553eaSConrad Meyer int error, busdmaflag; 999e974f91cSConrad Meyer 1000f46011aeSConrad Meyer error = ENOMEM; 1001f46011aeSConrad Meyer hw_desc = NULL; 1002f46011aeSConrad Meyer 1003bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) 1004bf8553eaSConrad Meyer busdmaflag = BUS_DMA_WAITOK; 1005bf8553eaSConrad Meyer else 1006bf8553eaSConrad Meyer busdmaflag = BUS_DMA_NOWAIT; 1007bf8553eaSConrad Meyer 1008bf8553eaSConrad Meyer desc = malloc(sizeof(*desc), M_IOAT, mflags); 1009e974f91cSConrad Meyer if (desc == NULL) 1010f46011aeSConrad Meyer goto out; 1011e974f91cSConrad Meyer 1012f46011aeSConrad Meyer bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc, 1013bf8553eaSConrad Meyer BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map); 1014f46011aeSConrad Meyer if (hw_desc == NULL) 1015f46011aeSConrad Meyer goto out; 1016e974f91cSConrad Meyer 1017faefad9cSConrad Meyer memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc)); 10189e3bbf26SConrad Meyer desc->u.generic = hw_desc; 1019f46011aeSConrad Meyer 1020f46011aeSConrad Meyer error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc, 1021f46011aeSConrad Meyer sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr, 1022bf8553eaSConrad Meyer busdmaflag); 1023f46011aeSConrad Meyer if (error) 1024f46011aeSConrad Meyer goto out; 1025f46011aeSConrad Meyer 1026f46011aeSConrad Meyer out: 1027f46011aeSConrad Meyer if (error) { 1028f46011aeSConrad Meyer ioat_free_ring_entry(ioat, desc); 1029f46011aeSConrad Meyer return (NULL); 1030f46011aeSConrad Meyer } 1031e974f91cSConrad Meyer return (desc); 1032e974f91cSConrad Meyer } 1033e974f91cSConrad Meyer 1034e974f91cSConrad Meyer static void 1035e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc) 1036e974f91cSConrad Meyer { 1037e974f91cSConrad Meyer 1038e974f91cSConrad Meyer if (desc == NULL) 1039e974f91cSConrad Meyer return; 1040e974f91cSConrad Meyer 10419e3bbf26SConrad Meyer if (desc->u.generic) 10429e3bbf26SConrad Meyer bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic, 1043e974f91cSConrad Meyer ioat->hw_desc_map); 1044e974f91cSConrad Meyer free(desc, M_IOAT); 1045e974f91cSConrad Meyer } 1046e974f91cSConrad Meyer 1047bf8553eaSConrad Meyer /* 1048bf8553eaSConrad Meyer * Reserves space in this IOAT descriptor ring by ensuring enough slots remain 1049bf8553eaSConrad Meyer * for 'num_descs'. 1050bf8553eaSConrad Meyer * 1051bf8553eaSConrad Meyer * If mflags contains M_WAITOK, blocks until enough space is available. 1052bf8553eaSConrad Meyer * 1053bf8553eaSConrad Meyer * Returns zero on success, or an errno on error. If num_descs is beyond the 1054bf8553eaSConrad Meyer * maximum ring size, returns EINVAl; if allocation would block and mflags 1055bf8553eaSConrad Meyer * contains M_NOWAIT, returns EAGAIN. 1056bf8553eaSConrad Meyer * 1057bf8553eaSConrad Meyer * Must be called with the submit_lock held; returns with the lock held. The 1058bf8553eaSConrad Meyer * lock may be dropped to allocate the ring. 1059bf8553eaSConrad Meyer * 1060bf8553eaSConrad Meyer * (The submit_lock is needed to add any entries to the ring, so callers are 1061bf8553eaSConrad Meyer * assured enough room is available.) 1062bf8553eaSConrad Meyer */ 1063e974f91cSConrad Meyer static int 1064bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags) 1065e974f91cSConrad Meyer { 1066bf8553eaSConrad Meyer struct ioat_descriptor **new_ring; 1067bf8553eaSConrad Meyer uint32_t order; 1068bf8553eaSConrad Meyer int error; 1069e974f91cSConrad Meyer 1070bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1071bf8553eaSConrad Meyer error = 0; 1072e974f91cSConrad Meyer 1073bf8553eaSConrad Meyer if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) { 1074bf8553eaSConrad Meyer error = EINVAL; 1075bf8553eaSConrad Meyer goto out; 1076e974f91cSConrad Meyer } 10775f77bd3eSConrad Meyer if (ioat->quiescing) { 10785f77bd3eSConrad Meyer error = ENXIO; 10795f77bd3eSConrad Meyer goto out; 10805f77bd3eSConrad Meyer } 1081bf8553eaSConrad Meyer 1082bf8553eaSConrad Meyer for (;;) { 1083bf8553eaSConrad Meyer if (ioat_get_ring_space(ioat) >= num_descs) 1084bf8553eaSConrad Meyer goto out; 1085bf8553eaSConrad Meyer 1086bf8553eaSConrad Meyer order = ioat->ring_size_order; 1087bf8553eaSConrad Meyer if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) { 1088bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) { 1089bf8553eaSConrad Meyer msleep(&ioat->tail, &ioat->submit_lock, 0, 1090bf8553eaSConrad Meyer "ioat_rsz", 0); 1091bf8553eaSConrad Meyer continue; 1092bf8553eaSConrad Meyer } 1093bf8553eaSConrad Meyer 1094bf8553eaSConrad Meyer error = EAGAIN; 1095bf8553eaSConrad Meyer break; 1096bf8553eaSConrad Meyer } 1097bf8553eaSConrad Meyer 1098bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1099bf8553eaSConrad Meyer for (;;) { 1100bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1101bf8553eaSConrad Meyer 1102bf8553eaSConrad Meyer new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1), 1103bf8553eaSConrad Meyer TRUE, mflags); 1104bf8553eaSConrad Meyer 1105bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1106bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1107bf8553eaSConrad Meyer ("is_resize_pending should protect order")); 1108bf8553eaSConrad Meyer 1109bf8553eaSConrad Meyer if (new_ring == NULL) { 1110bf8553eaSConrad Meyer KASSERT((mflags & M_WAITOK) == 0, 1111bf8553eaSConrad Meyer ("allocation failed")); 1112bf8553eaSConrad Meyer error = EAGAIN; 1113bf8553eaSConrad Meyer break; 1114bf8553eaSConrad Meyer } 1115bf8553eaSConrad Meyer 1116bf8553eaSConrad Meyer error = ring_grow(ioat, order, new_ring); 1117bf8553eaSConrad Meyer if (error == 0) 1118bf8553eaSConrad Meyer break; 1119bf8553eaSConrad Meyer } 1120bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1121bf8553eaSConrad Meyer wakeup(&ioat->tail); 1122bf8553eaSConrad Meyer if (error) 1123bf8553eaSConrad Meyer break; 1124bf8553eaSConrad Meyer } 1125bf8553eaSConrad Meyer 1126bf8553eaSConrad Meyer out: 1127bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1128bf8553eaSConrad Meyer return (error); 1129bf8553eaSConrad Meyer } 1130bf8553eaSConrad Meyer 1131bf8553eaSConrad Meyer static struct ioat_descriptor ** 1132bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr, 1133bf8553eaSConrad Meyer int mflags) 1134bf8553eaSConrad Meyer { 1135bf8553eaSConrad Meyer struct ioat_descriptor **ring; 1136bf8553eaSConrad Meyer uint32_t i; 1137bf8553eaSConrad Meyer int error; 1138bf8553eaSConrad Meyer 1139bf8553eaSConrad Meyer KASSERT(size > 0 && powerof2(size), ("bogus size")); 1140bf8553eaSConrad Meyer 1141bf8553eaSConrad Meyer ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags); 1142bf8553eaSConrad Meyer if (ring == NULL) 1143bf8553eaSConrad Meyer return (NULL); 1144bf8553eaSConrad Meyer 1145bf8553eaSConrad Meyer if (need_dscr) { 1146bf8553eaSConrad Meyer error = ENOMEM; 1147bf8553eaSConrad Meyer for (i = size / 2; i < size; i++) { 1148bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, mflags); 1149bf8553eaSConrad Meyer if (ring[i] == NULL) 1150bf8553eaSConrad Meyer goto out; 1151bf8553eaSConrad Meyer ring[i]->id = i; 1152bf8553eaSConrad Meyer } 1153bf8553eaSConrad Meyer } 1154bf8553eaSConrad Meyer error = 0; 1155bf8553eaSConrad Meyer 1156bf8553eaSConrad Meyer out: 1157bf8553eaSConrad Meyer if (error != 0 && ring != NULL) { 1158bf8553eaSConrad Meyer ioat_free_ring(ioat, size, ring); 1159bf8553eaSConrad Meyer ring = NULL; 1160bf8553eaSConrad Meyer } 1161bf8553eaSConrad Meyer return (ring); 1162bf8553eaSConrad Meyer } 1163bf8553eaSConrad Meyer 1164bf8553eaSConrad Meyer static void 1165bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size, 1166bf8553eaSConrad Meyer struct ioat_descriptor **ring) 1167bf8553eaSConrad Meyer { 1168bf8553eaSConrad Meyer uint32_t i; 1169bf8553eaSConrad Meyer 1170bf8553eaSConrad Meyer for (i = 0; i < size; i++) { 1171bf8553eaSConrad Meyer if (ring[i] != NULL) 1172bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ring[i]); 1173bf8553eaSConrad Meyer } 1174bf8553eaSConrad Meyer free(ring, M_IOAT); 1175e974f91cSConrad Meyer } 1176e974f91cSConrad Meyer 1177e974f91cSConrad Meyer static struct ioat_descriptor * 1178e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index) 1179e974f91cSConrad Meyer { 1180e974f91cSConrad Meyer 1181e974f91cSConrad Meyer return (ioat->ring[index % (1 << ioat->ring_size_order)]); 1182e974f91cSConrad Meyer } 1183e974f91cSConrad Meyer 1184bf8553eaSConrad Meyer static int 1185bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder, 1186bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1187e974f91cSConrad Meyer { 1188bf8553eaSConrad Meyer struct ioat_descriptor *tmp, *next; 1189e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1190bf8553eaSConrad Meyer uint32_t oldsize, newsize, head, tail, i, end; 1191bf8553eaSConrad Meyer int error; 1192e974f91cSConrad Meyer 1193bf8553eaSConrad Meyer CTR0(KTR_IOAT, __func__); 1194e974f91cSConrad Meyer 1195bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1196bf8553eaSConrad Meyer 1197bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) { 1198bf8553eaSConrad Meyer error = EINVAL; 1199bf8553eaSConrad Meyer goto out; 1200bf8553eaSConrad Meyer } 1201bf8553eaSConrad Meyer 1202bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1203bf8553eaSConrad Meyer newsize = (1 << (oldorder + 1)); 1204bf8553eaSConrad Meyer 1205bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1206bf8553eaSConrad Meyer 1207bf8553eaSConrad Meyer head = ioat->head & (oldsize - 1); 1208bf8553eaSConrad Meyer tail = ioat->tail & (oldsize - 1); 1209bf8553eaSConrad Meyer 1210bf8553eaSConrad Meyer /* Copy old descriptors to new ring */ 1211bf8553eaSConrad Meyer for (i = 0; i < oldsize; i++) 1212bf8553eaSConrad Meyer newring[i] = ioat->ring[i]; 1213e974f91cSConrad Meyer 1214e974f91cSConrad Meyer /* 1215bf8553eaSConrad Meyer * If head has wrapped but tail hasn't, we must swap some descriptors 1216bf8553eaSConrad Meyer * around so that tail can increment directly to head. 1217e974f91cSConrad Meyer */ 1218bf8553eaSConrad Meyer if (head < tail) { 1219bf8553eaSConrad Meyer for (i = 0; i <= head; i++) { 1220bf8553eaSConrad Meyer tmp = newring[oldsize + i]; 1221e974f91cSConrad Meyer 1222bf8553eaSConrad Meyer newring[oldsize + i] = newring[i]; 1223bf8553eaSConrad Meyer newring[oldsize + i]->id = oldsize + i; 1224e974f91cSConrad Meyer 1225bf8553eaSConrad Meyer newring[i] = tmp; 1226bf8553eaSConrad Meyer newring[i]->id = i; 1227bf8553eaSConrad Meyer } 1228bf8553eaSConrad Meyer head += oldsize; 1229e974f91cSConrad Meyer } 1230e974f91cSConrad Meyer 1231bf8553eaSConrad Meyer KASSERT(head >= tail, ("invariants")); 1232e974f91cSConrad Meyer 1233bf8553eaSConrad Meyer /* Head didn't wrap; we only need to link in oldsize..newsize */ 1234bf8553eaSConrad Meyer if (head < oldsize) { 1235bf8553eaSConrad Meyer i = oldsize - 1; 1236bf8553eaSConrad Meyer end = newsize; 1237e974f91cSConrad Meyer } else { 1238bf8553eaSConrad Meyer /* Head did wrap; link newhead..newsize and 0..oldhead */ 1239bf8553eaSConrad Meyer i = head; 1240bf8553eaSConrad Meyer end = newsize + (head - oldsize) + 1; 1241bf8553eaSConrad Meyer } 1242bf8553eaSConrad Meyer 1243e974f91cSConrad Meyer /* 1244bf8553eaSConrad Meyer * Fix up hardware ring, being careful not to trample the active 1245bf8553eaSConrad Meyer * section (tail -> head). 1246e974f91cSConrad Meyer */ 1247bf8553eaSConrad Meyer for (; i < end; i++) { 1248bf8553eaSConrad Meyer KASSERT((i & (newsize - 1)) < tail || 1249bf8553eaSConrad Meyer (i & (newsize - 1)) >= head, ("trampling snake")); 1250e974f91cSConrad Meyer 1251bf8553eaSConrad Meyer next = newring[(i + 1) & (newsize - 1)]; 1252bf8553eaSConrad Meyer hw = newring[i & (newsize - 1)]->u.dma; 1253e974f91cSConrad Meyer hw->next = next->hw_desc_bus_addr; 1254e974f91cSConrad Meyer } 1255e974f91cSConrad Meyer 1256e974f91cSConrad Meyer free(ioat->ring, M_IOAT); 1257bf8553eaSConrad Meyer ioat->ring = newring; 1258bf8553eaSConrad Meyer ioat->ring_size_order = oldorder + 1; 1259bf8553eaSConrad Meyer ioat->tail = tail; 1260bf8553eaSConrad Meyer ioat->head = head; 1261bf8553eaSConrad Meyer error = 0; 1262e974f91cSConrad Meyer 1263bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1264bf8553eaSConrad Meyer out: 1265bf8553eaSConrad Meyer if (error) 1266bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder + 1)), newring); 1267bf8553eaSConrad Meyer return (error); 1268bf8553eaSConrad Meyer } 1269bf8553eaSConrad Meyer 1270bf8553eaSConrad Meyer static int 1271bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder, 1272bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1273bf8553eaSConrad Meyer { 1274bf8553eaSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1275bf8553eaSConrad Meyer struct ioat_descriptor *ent, *next; 1276bf8553eaSConrad Meyer uint32_t oldsize, newsize, current_idx, new_idx, i; 1277bf8553eaSConrad Meyer int error; 1278bf8553eaSConrad Meyer 1279bf8553eaSConrad Meyer CTR0(KTR_IOAT, __func__); 1280bf8553eaSConrad Meyer 1281bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1282bf8553eaSConrad Meyer 1283bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) { 1284bf8553eaSConrad Meyer error = EINVAL; 1285bf8553eaSConrad Meyer goto out_unlocked; 1286bf8553eaSConrad Meyer } 1287bf8553eaSConrad Meyer 1288bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1289bf8553eaSConrad Meyer newsize = (1 << (oldorder - 1)); 1290bf8553eaSConrad Meyer 1291bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1292bf8553eaSConrad Meyer 1293bf8553eaSConrad Meyer /* Can't shrink below current active set! */ 1294bf8553eaSConrad Meyer if (ioat_get_active(ioat) >= newsize) { 1295bf8553eaSConrad Meyer error = ENOMEM; 1296bf8553eaSConrad Meyer goto out; 1297bf8553eaSConrad Meyer } 1298bf8553eaSConrad Meyer 1299bf8553eaSConrad Meyer /* 1300bf8553eaSConrad Meyer * Copy current descriptors to the new ring, dropping the removed 1301bf8553eaSConrad Meyer * descriptors. 1302bf8553eaSConrad Meyer */ 1303bf8553eaSConrad Meyer for (i = 0; i < newsize; i++) { 1304bf8553eaSConrad Meyer current_idx = (ioat->tail + i) & (oldsize - 1); 1305bf8553eaSConrad Meyer new_idx = (ioat->tail + i) & (newsize - 1); 1306bf8553eaSConrad Meyer 1307bf8553eaSConrad Meyer newring[new_idx] = ioat->ring[current_idx]; 1308bf8553eaSConrad Meyer newring[new_idx]->id = new_idx; 1309bf8553eaSConrad Meyer } 1310bf8553eaSConrad Meyer 1311bf8553eaSConrad Meyer /* Free deleted descriptors */ 1312bf8553eaSConrad Meyer for (i = newsize; i < oldsize; i++) { 1313bf8553eaSConrad Meyer ent = ioat_get_ring_entry(ioat, ioat->tail + i); 1314bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ent); 1315bf8553eaSConrad Meyer } 1316bf8553eaSConrad Meyer 1317bf8553eaSConrad Meyer /* Fix up hardware ring. */ 1318bf8553eaSConrad Meyer hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma; 1319bf8553eaSConrad Meyer next = newring[(ioat->tail + newsize) & (newsize - 1)]; 1320bf8553eaSConrad Meyer hw->next = next->hw_desc_bus_addr; 1321bf8553eaSConrad Meyer 1322bf8553eaSConrad Meyer free(ioat->ring, M_IOAT); 1323bf8553eaSConrad Meyer ioat->ring = newring; 1324bf8553eaSConrad Meyer ioat->ring_size_order = oldorder - 1; 1325bf8553eaSConrad Meyer error = 0; 1326bf8553eaSConrad Meyer 1327bf8553eaSConrad Meyer out: 1328bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1329bf8553eaSConrad Meyer out_unlocked: 1330bf8553eaSConrad Meyer if (error) 1331bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder - 1)), newring); 1332bf8553eaSConrad Meyer return (error); 1333e974f91cSConrad Meyer } 1334e974f91cSConrad Meyer 1335e974f91cSConrad Meyer static void 13368f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr) 1337e974f91cSConrad Meyer { 1338e974f91cSConrad Meyer struct ioat_descriptor *desc; 13398f274637SConrad Meyer 134059acd4baSConrad Meyer ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr, 134159acd4baSConrad Meyer IOAT_CHANERR_STR); 13428f274637SConrad Meyer if (chanerr == 0) 13438f274637SConrad Meyer return; 13448f274637SConrad Meyer 1345faefad9cSConrad Meyer mtx_assert(&ioat->cleanup_lock, MA_OWNED); 1346faefad9cSConrad Meyer 13478f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 0); 13488f274637SConrad Meyer dump_descriptor(desc->u.raw); 13498f274637SConrad Meyer 13508f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 1); 13518f274637SConrad Meyer dump_descriptor(desc->u.raw); 13528f274637SConrad Meyer } 13538f274637SConrad Meyer 13548f274637SConrad Meyer static void 13558f274637SConrad Meyer ioat_timer_callback(void *arg) 13568f274637SConrad Meyer { 1357bf8553eaSConrad Meyer struct ioat_descriptor **newring; 1358e974f91cSConrad Meyer struct ioat_softc *ioat; 1359faefad9cSConrad Meyer uint32_t order; 1360e974f91cSConrad Meyer 1361e974f91cSConrad Meyer ioat = arg; 1362fe720f5aSConrad Meyer ioat_log_message(1, "%s\n", __func__); 1363e974f91cSConrad Meyer 1364e974f91cSConrad Meyer if (ioat->is_completion_pending) { 1365e974f91cSConrad Meyer ioat_process_events(ioat); 1366faefad9cSConrad Meyer return; 1367faefad9cSConrad Meyer } 1368faefad9cSConrad Meyer 1369faefad9cSConrad Meyer /* Slowly scale the ring down if idle. */ 1370e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 1371bf8553eaSConrad Meyer order = ioat->ring_size_order; 1372bf8553eaSConrad Meyer if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) { 1373bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1374bf8553eaSConrad Meyer goto out; 1375bf8553eaSConrad Meyer } 1376bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1377e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 1378e974f91cSConrad Meyer 1379bf8553eaSConrad Meyer newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE, 1380bf8553eaSConrad Meyer M_NOWAIT); 1381bf8553eaSConrad Meyer 1382bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1383bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1384bf8553eaSConrad Meyer ("resize_pending protects order")); 1385bf8553eaSConrad Meyer 1386bf8553eaSConrad Meyer if (newring != NULL) 1387bf8553eaSConrad Meyer ring_shrink(ioat, order, newring); 1388bf8553eaSConrad Meyer 1389bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1390bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1391bf8553eaSConrad Meyer 1392bf8553eaSConrad Meyer out: 1393e974f91cSConrad Meyer if (ioat->ring_size_order > IOAT_MIN_ORDER) 1394bf8553eaSConrad Meyer callout_reset(&ioat->timer, 10 * hz, 1395e974f91cSConrad Meyer ioat_timer_callback, ioat); 1396e974f91cSConrad Meyer } 1397e974f91cSConrad Meyer 1398e974f91cSConrad Meyer /* 1399e974f91cSConrad Meyer * Support Functions 1400e974f91cSConrad Meyer */ 1401e974f91cSConrad Meyer static void 1402e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat) 1403e974f91cSConrad Meyer { 1404e974f91cSConrad Meyer 1405466b3540SConrad Meyer ioat_get(ioat, IOAT_ACTIVE_DESCR_REF); 1406e974f91cSConrad Meyer atomic_add_rel_int(&ioat->head, 1); 1407bf8553eaSConrad Meyer atomic_add_rel_int(&ioat->hw_head, 1); 1408e974f91cSConrad Meyer 1409e974f91cSConrad Meyer if (!ioat->is_completion_pending) { 1410e974f91cSConrad Meyer ioat->is_completion_pending = TRUE; 1411fe720f5aSConrad Meyer callout_reset(&ioat->timer, IOAT_INTR_TIMO, 1412fe720f5aSConrad Meyer ioat_timer_callback, ioat); 1413e974f91cSConrad Meyer } 141401fbbc88SConrad Meyer 141501fbbc88SConrad Meyer ioat->stats.descriptors_submitted++; 1416e974f91cSConrad Meyer } 1417e974f91cSConrad Meyer 1418e974f91cSConrad Meyer static int 1419e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat) 1420e974f91cSConrad Meyer { 1421e974f91cSConrad Meyer uint64_t status; 1422e974f91cSConrad Meyer uint32_t chanerr; 1423cea5b880SConrad Meyer unsigned timeout; 14245f77bd3eSConrad Meyer int error; 14255f77bd3eSConrad Meyer 14265f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 14275f77bd3eSConrad Meyer ioat->quiescing = TRUE; 14285f77bd3eSConrad Meyer ioat_drain_locked(ioat); 14295f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 1430e974f91cSConrad Meyer 1431e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1432e974f91cSConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) 1433e974f91cSConrad Meyer ioat_suspend(ioat); 1434e974f91cSConrad Meyer 1435e974f91cSConrad Meyer /* Wait at most 20 ms */ 1436e974f91cSConrad Meyer for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) && 1437e974f91cSConrad Meyer timeout < 20; timeout++) { 1438e974f91cSConrad Meyer DELAY(1000); 1439e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1440e974f91cSConrad Meyer } 14415f77bd3eSConrad Meyer if (timeout == 20) { 14425f77bd3eSConrad Meyer error = ETIMEDOUT; 14435f77bd3eSConrad Meyer goto out; 14445f77bd3eSConrad Meyer } 1445e974f91cSConrad Meyer 1446cea5b880SConrad Meyer KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce")); 1447cea5b880SConrad Meyer 1448e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 1449e974f91cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 1450e974f91cSConrad Meyer 1451e974f91cSConrad Meyer /* 1452e974f91cSConrad Meyer * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors 1453e974f91cSConrad Meyer * that can cause stability issues for IOAT v3. 1454e974f91cSConrad Meyer */ 1455e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07, 1456e974f91cSConrad Meyer 4); 1457e974f91cSConrad Meyer chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4); 1458e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4); 1459e974f91cSConrad Meyer 14600d1a05d9SConrad Meyer /* 14610d1a05d9SConrad Meyer * BDXDE and BWD models reset MSI-X registers on device reset. 14620d1a05d9SConrad Meyer * Save/restore their contents manually. 14630d1a05d9SConrad Meyer */ 1464f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1465f7157235SConrad Meyer ioat_log_message(1, "device resets MSI-X registers; saving\n"); 14660d1a05d9SConrad Meyer pci_save_state(ioat->device); 1467f7157235SConrad Meyer } 14680d1a05d9SConrad Meyer 1469e974f91cSConrad Meyer ioat_reset(ioat); 1470e974f91cSConrad Meyer 1471e974f91cSConrad Meyer /* Wait at most 20 ms */ 1472e974f91cSConrad Meyer for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++) 1473e974f91cSConrad Meyer DELAY(1000); 14745f77bd3eSConrad Meyer if (timeout == 20) { 14755f77bd3eSConrad Meyer error = ETIMEDOUT; 14765f77bd3eSConrad Meyer goto out; 14775f77bd3eSConrad Meyer } 1478e974f91cSConrad Meyer 1479f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1480f7157235SConrad Meyer ioat_log_message(1, "device resets registers; restored\n"); 14810d1a05d9SConrad Meyer pci_restore_state(ioat->device); 1482f7157235SConrad Meyer } 14834253ea50SConrad Meyer 1484cea5b880SConrad Meyer /* Reset attempts to return the hardware to "halted." */ 1485cea5b880SConrad Meyer status = ioat_get_chansts(ioat); 1486cea5b880SConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) { 1487cea5b880SConrad Meyer /* So this really shouldn't happen... */ 1488cea5b880SConrad Meyer ioat_log_message(0, "Device is active after a reset?\n"); 1489cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 14905f77bd3eSConrad Meyer error = 0; 14915f77bd3eSConrad Meyer goto out; 1492e974f91cSConrad Meyer } 1493e974f91cSConrad Meyer 1494cea5b880SConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 14955f77bd3eSConrad Meyer if (chanerr != 0) { 1496faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1497faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 1498faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 14995f77bd3eSConrad Meyer error = EIO; 15005f77bd3eSConrad Meyer goto out; 15015f77bd3eSConrad Meyer } 1502cea5b880SConrad Meyer 1503cea5b880SConrad Meyer /* 1504cea5b880SConrad Meyer * Bring device back online after reset. Writing CHAINADDR brings the 1505cea5b880SConrad Meyer * device back to active. 1506cea5b880SConrad Meyer * 1507cea5b880SConrad Meyer * The internal ring counter resets to zero, so we have to start over 1508cea5b880SConrad Meyer * at zero as well. 1509cea5b880SConrad Meyer */ 1510bf8553eaSConrad Meyer ioat->tail = ioat->head = ioat->hw_head = 0; 1511cea5b880SConrad Meyer ioat->last_seen = 0; 1512cea5b880SConrad Meyer 1513cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 1514cea5b880SConrad Meyer ioat_write_chancmp(ioat, ioat->comp_update_bus_addr); 1515cea5b880SConrad Meyer ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr); 15165f77bd3eSConrad Meyer error = 0; 15175f77bd3eSConrad Meyer 15185f77bd3eSConrad Meyer out: 15195f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 15205f77bd3eSConrad Meyer ioat->quiescing = FALSE; 15215f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 15225f77bd3eSConrad Meyer 15235f77bd3eSConrad Meyer if (error == 0) 15245f77bd3eSConrad Meyer error = ioat_start_channel(ioat); 15255f77bd3eSConrad Meyer 15265f77bd3eSConrad Meyer return (error); 1527cea5b880SConrad Meyer } 1528cea5b880SConrad Meyer 1529f7157235SConrad Meyer static int 1530faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS) 1531faefad9cSConrad Meyer { 1532faefad9cSConrad Meyer struct ioat_softc *ioat; 1533faefad9cSConrad Meyer struct sbuf sb; 1534faefad9cSConrad Meyer uint64_t status; 1535faefad9cSConrad Meyer int error; 1536faefad9cSConrad Meyer 1537faefad9cSConrad Meyer ioat = arg1; 1538faefad9cSConrad Meyer 1539faefad9cSConrad Meyer status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS; 1540faefad9cSConrad Meyer 1541faefad9cSConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 256, req); 1542faefad9cSConrad Meyer switch (status) { 1543faefad9cSConrad Meyer case IOAT_CHANSTS_ACTIVE: 1544faefad9cSConrad Meyer sbuf_printf(&sb, "ACTIVE"); 1545faefad9cSConrad Meyer break; 1546faefad9cSConrad Meyer case IOAT_CHANSTS_IDLE: 1547faefad9cSConrad Meyer sbuf_printf(&sb, "IDLE"); 1548faefad9cSConrad Meyer break; 1549faefad9cSConrad Meyer case IOAT_CHANSTS_SUSPENDED: 1550faefad9cSConrad Meyer sbuf_printf(&sb, "SUSPENDED"); 1551faefad9cSConrad Meyer break; 1552faefad9cSConrad Meyer case IOAT_CHANSTS_HALTED: 1553faefad9cSConrad Meyer sbuf_printf(&sb, "HALTED"); 1554faefad9cSConrad Meyer break; 1555faefad9cSConrad Meyer case IOAT_CHANSTS_ARMED: 1556faefad9cSConrad Meyer sbuf_printf(&sb, "ARMED"); 1557faefad9cSConrad Meyer break; 1558faefad9cSConrad Meyer default: 1559faefad9cSConrad Meyer sbuf_printf(&sb, "UNKNOWN"); 1560faefad9cSConrad Meyer break; 1561faefad9cSConrad Meyer } 1562faefad9cSConrad Meyer error = sbuf_finish(&sb); 1563faefad9cSConrad Meyer sbuf_delete(&sb); 1564faefad9cSConrad Meyer 1565faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 1566faefad9cSConrad Meyer return (error); 1567faefad9cSConrad Meyer return (EINVAL); 1568faefad9cSConrad Meyer } 1569faefad9cSConrad Meyer 1570faefad9cSConrad Meyer static int 157101fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS) 157201fbbc88SConrad Meyer { 157301fbbc88SConrad Meyer struct ioat_softc *ioat; 157401fbbc88SConrad Meyer struct sbuf sb; 157501fbbc88SConrad Meyer #define PRECISION "1" 157601fbbc88SConrad Meyer const uintmax_t factor = 10; 157701fbbc88SConrad Meyer uintmax_t rate; 157801fbbc88SConrad Meyer int error; 157901fbbc88SConrad Meyer 158001fbbc88SConrad Meyer ioat = arg1; 158101fbbc88SConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 16, req); 158201fbbc88SConrad Meyer 158301fbbc88SConrad Meyer if (ioat->stats.interrupts == 0) { 158401fbbc88SConrad Meyer sbuf_printf(&sb, "NaN"); 158501fbbc88SConrad Meyer goto out; 158601fbbc88SConrad Meyer } 158701fbbc88SConrad Meyer rate = ioat->stats.descriptors_processed * factor / 158801fbbc88SConrad Meyer ioat->stats.interrupts; 158901fbbc88SConrad Meyer sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor, 159001fbbc88SConrad Meyer rate % factor); 159101fbbc88SConrad Meyer #undef PRECISION 159201fbbc88SConrad Meyer out: 159301fbbc88SConrad Meyer error = sbuf_finish(&sb); 159401fbbc88SConrad Meyer sbuf_delete(&sb); 159501fbbc88SConrad Meyer if (error != 0 || req->newptr == NULL) 159601fbbc88SConrad Meyer return (error); 159701fbbc88SConrad Meyer return (EINVAL); 159801fbbc88SConrad Meyer } 159901fbbc88SConrad Meyer 160001fbbc88SConrad Meyer static int 1601faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS) 1602faefad9cSConrad Meyer { 1603faefad9cSConrad Meyer struct ioat_descriptor *desc; 1604faefad9cSConrad Meyer struct ioat_softc *ioat; 1605faefad9cSConrad Meyer int error, arg; 1606faefad9cSConrad Meyer 1607faefad9cSConrad Meyer ioat = arg1; 1608faefad9cSConrad Meyer 1609faefad9cSConrad Meyer arg = 0; 1610faefad9cSConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 1611faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 1612faefad9cSConrad Meyer return (error); 1613faefad9cSConrad Meyer 1614faefad9cSConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 1615faefad9cSConrad Meyer if (error != 0) 1616faefad9cSConrad Meyer return (error); 1617faefad9cSConrad Meyer 1618faefad9cSConrad Meyer if (arg != 0) { 1619faefad9cSConrad Meyer ioat_acquire(&ioat->dmaengine); 1620faefad9cSConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1, 1621faefad9cSConrad Meyer 0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL, 1622faefad9cSConrad Meyer 0); 1623faefad9cSConrad Meyer if (desc == NULL) 1624faefad9cSConrad Meyer error = ENOMEM; 1625faefad9cSConrad Meyer else 1626faefad9cSConrad Meyer ioat_submit_single(ioat); 1627faefad9cSConrad Meyer ioat_release(&ioat->dmaengine); 1628faefad9cSConrad Meyer } 1629faefad9cSConrad Meyer return (error); 1630faefad9cSConrad Meyer } 1631faefad9cSConrad Meyer 1632faefad9cSConrad Meyer static int 1633f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS) 1634f7157235SConrad Meyer { 1635f7157235SConrad Meyer struct ioat_softc *ioat; 1636f7157235SConrad Meyer int error, arg; 1637f7157235SConrad Meyer 1638f7157235SConrad Meyer ioat = arg1; 1639f7157235SConrad Meyer 1640f7157235SConrad Meyer arg = 0; 1641f7157235SConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 1642f7157235SConrad Meyer if (error != 0 || req->newptr == NULL) 1643f7157235SConrad Meyer return (error); 1644f7157235SConrad Meyer 1645f7157235SConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 1646f7157235SConrad Meyer if (error != 0) 1647f7157235SConrad Meyer return (error); 1648f7157235SConrad Meyer 1649f7157235SConrad Meyer if (arg != 0) 1650f7157235SConrad Meyer error = ioat_reset_hw(ioat); 1651f7157235SConrad Meyer 1652f7157235SConrad Meyer return (error); 1653f7157235SConrad Meyer } 1654f7157235SConrad Meyer 1655e974f91cSConrad Meyer static void 1656e974f91cSConrad Meyer dump_descriptor(void *hw_desc) 1657e974f91cSConrad Meyer { 1658e974f91cSConrad Meyer int i, j; 1659e974f91cSConrad Meyer 1660e974f91cSConrad Meyer for (i = 0; i < 2; i++) { 1661e974f91cSConrad Meyer for (j = 0; j < 8; j++) 1662e974f91cSConrad Meyer printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]); 1663e974f91cSConrad Meyer printf("\n"); 1664e974f91cSConrad Meyer } 1665e974f91cSConrad Meyer } 1666e974f91cSConrad Meyer 1667e974f91cSConrad Meyer static void 1668e974f91cSConrad Meyer ioat_setup_sysctl(device_t device) 1669e974f91cSConrad Meyer { 167001fbbc88SConrad Meyer struct sysctl_oid_list *par, *statpar, *state, *hammer; 1671f7157235SConrad Meyer struct sysctl_ctx_list *ctx; 167201fbbc88SConrad Meyer struct sysctl_oid *tree, *tmp; 1673e974f91cSConrad Meyer struct ioat_softc *ioat; 1674e974f91cSConrad Meyer 1675e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 1676f7157235SConrad Meyer ctx = device_get_sysctl_ctx(device); 1677f7157235SConrad Meyer tree = device_get_sysctl_tree(device); 1678f7157235SConrad Meyer par = SYSCTL_CHILDREN(tree); 1679e974f91cSConrad Meyer 168065e4f8adSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD, 168165e4f8adSConrad Meyer &ioat->version, 0, "HW version (0xMM form)"); 168265e4f8adSConrad Meyer SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD, 168365e4f8adSConrad Meyer &ioat->max_xfer_size, 0, "HW maximum transfer size"); 16845ca9fc2aSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD, 16855ca9fc2aSConrad Meyer &ioat->intrdelay_supported, 0, "Is INTRDELAY supported"); 16865ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD, 16875ca9fc2aSConrad Meyer &ioat->intrdelay_max, 0, 16885ca9fc2aSConrad Meyer "Maximum configurable INTRDELAY on this channel (microseconds)"); 168965e4f8adSConrad Meyer 169001fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL, 169101fbbc88SConrad Meyer "IOAT channel internal state"); 169201fbbc88SConrad Meyer state = SYSCTL_CHILDREN(tmp); 169301fbbc88SConrad Meyer 169401fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD, 1695bf8553eaSConrad Meyer &ioat->ring_size_order, 0, "SW descriptor ring size order"); 169601fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 169701fbbc88SConrad Meyer 0, "SW descriptor head pointer index"); 169801fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 169901fbbc88SConrad Meyer 0, "SW descriptor tail pointer index"); 170001fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD, 1701bf8553eaSConrad Meyer &ioat->hw_head, 0, "HW DMACOUNT"); 1702f7157235SConrad Meyer 170301fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD, 170465e4f8adSConrad Meyer ioat->comp_update, "HW addr of last completion"); 170565e4f8adSConrad Meyer 170601fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD, 170765e4f8adSConrad Meyer &ioat->is_resize_pending, 0, "resize pending"); 170801fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending", 170901fbbc88SConrad Meyer CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending"); 171001fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD, 171165e4f8adSConrad Meyer &ioat->is_reset_pending, 0, "reset pending"); 171201fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD, 171365e4f8adSConrad Meyer &ioat->is_channel_running, 0, "channel running"); 171465e4f8adSConrad Meyer 171501fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts", 1716faefad9cSConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A", 1717faefad9cSConrad Meyer "String of the channel status"); 171801fbbc88SConrad Meyer 17195ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD, 17205ca9fc2aSConrad Meyer &ioat->cached_intrdelay, 0, 17215ca9fc2aSConrad Meyer "Current INTRDELAY on this channel (cached, microseconds)"); 17225ca9fc2aSConrad Meyer 172301fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL, 172401fbbc88SConrad Meyer "Big hammers (mostly for testing)"); 172501fbbc88SConrad Meyer hammer = SYSCTL_CHILDREN(tmp); 172601fbbc88SConrad Meyer 172701fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset", 172801fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I", 172901fbbc88SConrad Meyer "Set to non-zero to reset the hardware"); 173001fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error", 173101fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I", 173201fbbc88SConrad Meyer "Set to non-zero to inject a recoverable hardware error"); 173301fbbc88SConrad Meyer 173401fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL, 173501fbbc88SConrad Meyer "IOAT channel statistics"); 173601fbbc88SConrad Meyer statpar = SYSCTL_CHILDREN(tmp); 173701fbbc88SConrad Meyer 173801fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW, 173901fbbc88SConrad Meyer &ioat->stats.interrupts, 174001fbbc88SConrad Meyer "Number of interrupts processed on this channel"); 174101fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW, 174201fbbc88SConrad Meyer &ioat->stats.descriptors_processed, 174301fbbc88SConrad Meyer "Number of descriptors processed on this channel"); 174401fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW, 174501fbbc88SConrad Meyer &ioat->stats.descriptors_submitted, 174601fbbc88SConrad Meyer "Number of descriptors submitted to this channel"); 174701fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW, 174801fbbc88SConrad Meyer &ioat->stats.descriptors_error, 174901fbbc88SConrad Meyer "Number of descriptors failed by channel errors"); 175001fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW, 175101fbbc88SConrad Meyer &ioat->stats.channel_halts, 0, 175201fbbc88SConrad Meyer "Number of times the channel has halted"); 175301fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW, 175401fbbc88SConrad Meyer &ioat->stats.last_halt_chanerr, 0, 175501fbbc88SConrad Meyer "The raw CHANERR when the channel was last halted"); 175601fbbc88SConrad Meyer 175701fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt", 175801fbbc88SConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A", 175901fbbc88SConrad Meyer "Descriptors per interrupt"); 1760e974f91cSConrad Meyer } 1761466b3540SConrad Meyer 1762466b3540SConrad Meyer static inline struct ioat_softc * 1763466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind) 1764466b3540SConrad Meyer { 1765466b3540SConrad Meyer uint32_t old; 1766466b3540SConrad Meyer 1767466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 1768466b3540SConrad Meyer 1769466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, 1); 1770466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt overflow")); 1771466b3540SConrad Meyer 1772466b3540SConrad Meyer #ifdef INVARIANTS 1773466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], 1); 1774466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt kind overflow")); 1775466b3540SConrad Meyer #endif 1776466b3540SConrad Meyer 1777466b3540SConrad Meyer return (ioat); 1778466b3540SConrad Meyer } 1779466b3540SConrad Meyer 1780466b3540SConrad Meyer static inline void 1781466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 1782466b3540SConrad Meyer { 1783faefad9cSConrad Meyer 1784faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, FALSE); 1785faefad9cSConrad Meyer } 1786faefad9cSConrad Meyer 1787faefad9cSConrad Meyer static inline void 1788faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 1789faefad9cSConrad Meyer { 1790faefad9cSConrad Meyer 1791faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, TRUE); 1792faefad9cSConrad Meyer } 1793faefad9cSConrad Meyer 1794faefad9cSConrad Meyer static inline void 1795faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind, 1796faefad9cSConrad Meyer boolean_t locked) 1797faefad9cSConrad Meyer { 1798466b3540SConrad Meyer uint32_t old; 1799466b3540SConrad Meyer 1800466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 1801466b3540SConrad Meyer 1802466b3540SConrad Meyer if (n == 0) 1803466b3540SConrad Meyer return; 1804466b3540SConrad Meyer 1805466b3540SConrad Meyer #ifdef INVARIANTS 1806466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], -n); 1807466b3540SConrad Meyer KASSERT(old >= n, ("refcnt kind underflow")); 1808466b3540SConrad Meyer #endif 1809466b3540SConrad Meyer 1810466b3540SConrad Meyer /* Skip acquiring the lock if resulting refcnt > 0. */ 1811466b3540SConrad Meyer for (;;) { 1812466b3540SConrad Meyer old = ioat->refcnt; 1813466b3540SConrad Meyer if (old <= n) 1814466b3540SConrad Meyer break; 1815466b3540SConrad Meyer if (atomic_cmpset_32(&ioat->refcnt, old, old - n)) 1816466b3540SConrad Meyer return; 1817466b3540SConrad Meyer } 1818466b3540SConrad Meyer 1819faefad9cSConrad Meyer if (locked) 1820faefad9cSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 1821faefad9cSConrad Meyer else 1822466b3540SConrad Meyer mtx_lock(IOAT_REFLK); 1823faefad9cSConrad Meyer 1824466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, -n); 1825466b3540SConrad Meyer KASSERT(old >= n, ("refcnt error")); 1826466b3540SConrad Meyer 1827466b3540SConrad Meyer if (old == n) 1828466b3540SConrad Meyer wakeup(IOAT_REFLK); 1829faefad9cSConrad Meyer if (!locked) 1830466b3540SConrad Meyer mtx_unlock(IOAT_REFLK); 1831466b3540SConrad Meyer } 1832466b3540SConrad Meyer 1833466b3540SConrad Meyer static inline void 1834466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind) 1835466b3540SConrad Meyer { 1836466b3540SConrad Meyer 1837466b3540SConrad Meyer ioat_putn(ioat, 1, kind); 1838466b3540SConrad Meyer } 1839466b3540SConrad Meyer 1840466b3540SConrad Meyer static void 18415f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat) 1842466b3540SConrad Meyer { 1843466b3540SConrad Meyer 18445f77bd3eSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 1845466b3540SConrad Meyer while (ioat->refcnt > 0) 1846466b3540SConrad Meyer msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0); 1847466b3540SConrad Meyer } 1848