1e974f91cSConrad Meyer /*- 2e974f91cSConrad Meyer * Copyright (C) 2012 Intel Corporation 3e974f91cSConrad Meyer * All rights reserved. 4e974f91cSConrad Meyer * 5e974f91cSConrad Meyer * Redistribution and use in source and binary forms, with or without 6e974f91cSConrad Meyer * modification, are permitted provided that the following conditions 7e974f91cSConrad Meyer * are met: 8e974f91cSConrad Meyer * 1. Redistributions of source code must retain the above copyright 9e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer. 10e974f91cSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 11e974f91cSConrad Meyer * notice, this list of conditions and the following disclaimer in the 12e974f91cSConrad Meyer * documentation and/or other materials provided with the distribution. 13e974f91cSConrad Meyer * 14e974f91cSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15e974f91cSConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16e974f91cSConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17e974f91cSConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18e974f91cSConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19e974f91cSConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20e974f91cSConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21e974f91cSConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22e974f91cSConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23e974f91cSConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24e974f91cSConrad Meyer * SUCH DAMAGE. 25e974f91cSConrad Meyer */ 26e974f91cSConrad Meyer 27e974f91cSConrad Meyer #include <sys/cdefs.h> 28e974f91cSConrad Meyer __FBSDID("$FreeBSD$"); 29e974f91cSConrad Meyer 30e974f91cSConrad Meyer #include <sys/param.h> 31e974f91cSConrad Meyer #include <sys/systm.h> 32e974f91cSConrad Meyer #include <sys/bus.h> 33e974f91cSConrad Meyer #include <sys/conf.h> 34e974f91cSConrad Meyer #include <sys/ioccom.h> 35e974f91cSConrad Meyer #include <sys/kernel.h> 36e974f91cSConrad Meyer #include <sys/lock.h> 37e974f91cSConrad Meyer #include <sys/malloc.h> 38e974f91cSConrad Meyer #include <sys/module.h> 39e974f91cSConrad Meyer #include <sys/mutex.h> 40e974f91cSConrad Meyer #include <sys/rman.h> 41faefad9cSConrad Meyer #include <sys/sbuf.h> 42e974f91cSConrad Meyer #include <sys/sysctl.h> 43e974f91cSConrad Meyer #include <sys/time.h> 44e974f91cSConrad Meyer #include <dev/pci/pcireg.h> 45e974f91cSConrad Meyer #include <dev/pci/pcivar.h> 46e974f91cSConrad Meyer #include <machine/bus.h> 47e974f91cSConrad Meyer #include <machine/resource.h> 48e974f91cSConrad Meyer #include <machine/stdarg.h> 49e974f91cSConrad Meyer 50e974f91cSConrad Meyer #include "ioat.h" 51e974f91cSConrad Meyer #include "ioat_hw.h" 52e974f91cSConrad Meyer #include "ioat_internal.h" 53e974f91cSConrad Meyer 54fe720f5aSConrad Meyer #define IOAT_INTR_TIMO (hz / 10) 55466b3540SConrad Meyer #define IOAT_REFLK (&ioat->submit_lock) 56fe720f5aSConrad Meyer 57e974f91cSConrad Meyer static int ioat_probe(device_t device); 58e974f91cSConrad Meyer static int ioat_attach(device_t device); 59e974f91cSConrad Meyer static int ioat_detach(device_t device); 604253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat); 614253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat); 62e974f91cSConrad Meyer static int ioat3_attach(device_t device); 63cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat); 64e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat); 65e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, 66e974f91cSConrad Meyer int error); 67e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg); 680d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat); 69faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t); 70e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat); 71e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat); 72e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat); 73bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size, 74bf8553eaSConrad Meyer struct ioat_descriptor **); 75e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat, 76e974f91cSConrad Meyer struct ioat_descriptor *desc); 77bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *, 78bf8553eaSConrad Meyer int mflags); 79bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags); 80e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat, 81e974f91cSConrad Meyer uint32_t index); 82bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *, 83bf8553eaSConrad Meyer uint32_t size, boolean_t need_dscr, int mflags); 84bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder, 85bf8553eaSConrad Meyer struct ioat_descriptor **); 86bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder, 87bf8553eaSConrad Meyer struct ioat_descriptor **); 88faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t); 89e974f91cSConrad Meyer static void ioat_timer_callback(void *arg); 90e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc); 91e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat); 92e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, 93e974f91cSConrad Meyer int error); 94e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat); 95e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device); 96f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS); 97466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *, 98466b3540SConrad Meyer enum ioat_ref_kind); 99466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind); 100faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t, 101faefad9cSConrad Meyer enum ioat_ref_kind, boolean_t); 102466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t, 103466b3540SConrad Meyer enum ioat_ref_kind); 104faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t, 105faefad9cSConrad Meyer enum ioat_ref_kind); 1065f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *); 107e974f91cSConrad Meyer 1081c25420eSConrad Meyer #define ioat_log_message(v, ...) do { \ 1091c25420eSConrad Meyer if ((v) <= g_ioat_debug_level) { \ 1101c25420eSConrad Meyer device_printf(ioat->device, __VA_ARGS__); \ 1111c25420eSConrad Meyer } \ 1121c25420eSConrad Meyer } while (0) 1131c25420eSConrad Meyer 114e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations"); 115e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node"); 116e974f91cSConrad Meyer 117e974f91cSConrad Meyer static int g_force_legacy_interrupts; 118e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN, 119e974f91cSConrad Meyer &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled"); 120e974f91cSConrad Meyer 1211c25420eSConrad Meyer int g_ioat_debug_level = 0; 122e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level, 123e974f91cSConrad Meyer 0, "Set log level (0-3) for ioat(4). Higher is more verbose."); 124e974f91cSConrad Meyer 125e974f91cSConrad Meyer /* 126e974f91cSConrad Meyer * OS <-> Driver interface structures 127e974f91cSConrad Meyer */ 128e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = { 129e974f91cSConrad Meyer /* Device interface */ 130e974f91cSConrad Meyer DEVMETHOD(device_probe, ioat_probe), 131e974f91cSConrad Meyer DEVMETHOD(device_attach, ioat_attach), 132e974f91cSConrad Meyer DEVMETHOD(device_detach, ioat_detach), 133e974f91cSConrad Meyer { 0, 0 } 134e974f91cSConrad Meyer }; 135e974f91cSConrad Meyer 136e974f91cSConrad Meyer static driver_t ioat_pci_driver = { 137e974f91cSConrad Meyer "ioat", 138e974f91cSConrad Meyer ioat_pci_methods, 139e974f91cSConrad Meyer sizeof(struct ioat_softc), 140e974f91cSConrad Meyer }; 141e974f91cSConrad Meyer 142e974f91cSConrad Meyer static devclass_t ioat_devclass; 143e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0); 144c2b69205SConrad Meyer MODULE_VERSION(ioat, 1); 145e974f91cSConrad Meyer 146e974f91cSConrad Meyer /* 147e974f91cSConrad Meyer * Private data structures 148e974f91cSConrad Meyer */ 149e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS]; 150e974f91cSConrad Meyer static int ioat_channel_index = 0; 151e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0, 152e974f91cSConrad Meyer "Number of IOAT channels attached"); 153e974f91cSConrad Meyer 154e974f91cSConrad Meyer static struct _pcsid 155e974f91cSConrad Meyer { 156e974f91cSConrad Meyer u_int32_t type; 157e974f91cSConrad Meyer const char *desc; 158e974f91cSConrad Meyer } pci_ids[] = { 159e974f91cSConrad Meyer { 0x34308086, "TBG IOAT Ch0" }, 160e974f91cSConrad Meyer { 0x34318086, "TBG IOAT Ch1" }, 161e974f91cSConrad Meyer { 0x34328086, "TBG IOAT Ch2" }, 162e974f91cSConrad Meyer { 0x34338086, "TBG IOAT Ch3" }, 163e974f91cSConrad Meyer { 0x34298086, "TBG IOAT Ch4" }, 164e974f91cSConrad Meyer { 0x342a8086, "TBG IOAT Ch5" }, 165e974f91cSConrad Meyer { 0x342b8086, "TBG IOAT Ch6" }, 166e974f91cSConrad Meyer { 0x342c8086, "TBG IOAT Ch7" }, 167e974f91cSConrad Meyer 168e974f91cSConrad Meyer { 0x37108086, "JSF IOAT Ch0" }, 169e974f91cSConrad Meyer { 0x37118086, "JSF IOAT Ch1" }, 170e974f91cSConrad Meyer { 0x37128086, "JSF IOAT Ch2" }, 171e974f91cSConrad Meyer { 0x37138086, "JSF IOAT Ch3" }, 172e974f91cSConrad Meyer { 0x37148086, "JSF IOAT Ch4" }, 173e974f91cSConrad Meyer { 0x37158086, "JSF IOAT Ch5" }, 174e974f91cSConrad Meyer { 0x37168086, "JSF IOAT Ch6" }, 175e974f91cSConrad Meyer { 0x37178086, "JSF IOAT Ch7" }, 176e974f91cSConrad Meyer { 0x37188086, "JSF IOAT Ch0 (RAID)" }, 177e974f91cSConrad Meyer { 0x37198086, "JSF IOAT Ch1 (RAID)" }, 178e974f91cSConrad Meyer 179e974f91cSConrad Meyer { 0x3c208086, "SNB IOAT Ch0" }, 180e974f91cSConrad Meyer { 0x3c218086, "SNB IOAT Ch1" }, 181e974f91cSConrad Meyer { 0x3c228086, "SNB IOAT Ch2" }, 182e974f91cSConrad Meyer { 0x3c238086, "SNB IOAT Ch3" }, 183e974f91cSConrad Meyer { 0x3c248086, "SNB IOAT Ch4" }, 184e974f91cSConrad Meyer { 0x3c258086, "SNB IOAT Ch5" }, 185e974f91cSConrad Meyer { 0x3c268086, "SNB IOAT Ch6" }, 186e974f91cSConrad Meyer { 0x3c278086, "SNB IOAT Ch7" }, 187e974f91cSConrad Meyer { 0x3c2e8086, "SNB IOAT Ch0 (RAID)" }, 188e974f91cSConrad Meyer { 0x3c2f8086, "SNB IOAT Ch1 (RAID)" }, 189e974f91cSConrad Meyer 190e974f91cSConrad Meyer { 0x0e208086, "IVB IOAT Ch0" }, 191e974f91cSConrad Meyer { 0x0e218086, "IVB IOAT Ch1" }, 192e974f91cSConrad Meyer { 0x0e228086, "IVB IOAT Ch2" }, 193e974f91cSConrad Meyer { 0x0e238086, "IVB IOAT Ch3" }, 194e974f91cSConrad Meyer { 0x0e248086, "IVB IOAT Ch4" }, 195e974f91cSConrad Meyer { 0x0e258086, "IVB IOAT Ch5" }, 196e974f91cSConrad Meyer { 0x0e268086, "IVB IOAT Ch6" }, 197e974f91cSConrad Meyer { 0x0e278086, "IVB IOAT Ch7" }, 198e974f91cSConrad Meyer { 0x0e2e8086, "IVB IOAT Ch0 (RAID)" }, 199e974f91cSConrad Meyer { 0x0e2f8086, "IVB IOAT Ch1 (RAID)" }, 200e974f91cSConrad Meyer 201e974f91cSConrad Meyer { 0x2f208086, "HSW IOAT Ch0" }, 202e974f91cSConrad Meyer { 0x2f218086, "HSW IOAT Ch1" }, 203e974f91cSConrad Meyer { 0x2f228086, "HSW IOAT Ch2" }, 204e974f91cSConrad Meyer { 0x2f238086, "HSW IOAT Ch3" }, 205e974f91cSConrad Meyer { 0x2f248086, "HSW IOAT Ch4" }, 206e974f91cSConrad Meyer { 0x2f258086, "HSW IOAT Ch5" }, 207e974f91cSConrad Meyer { 0x2f268086, "HSW IOAT Ch6" }, 208e974f91cSConrad Meyer { 0x2f278086, "HSW IOAT Ch7" }, 209e974f91cSConrad Meyer { 0x2f2e8086, "HSW IOAT Ch0 (RAID)" }, 210e974f91cSConrad Meyer { 0x2f2f8086, "HSW IOAT Ch1 (RAID)" }, 211e974f91cSConrad Meyer 212e974f91cSConrad Meyer { 0x0c508086, "BWD IOAT Ch0" }, 213e974f91cSConrad Meyer { 0x0c518086, "BWD IOAT Ch1" }, 214e974f91cSConrad Meyer { 0x0c528086, "BWD IOAT Ch2" }, 215e974f91cSConrad Meyer { 0x0c538086, "BWD IOAT Ch3" }, 216e974f91cSConrad Meyer 217e974f91cSConrad Meyer { 0x6f508086, "BDXDE IOAT Ch0" }, 218e974f91cSConrad Meyer { 0x6f518086, "BDXDE IOAT Ch1" }, 219e974f91cSConrad Meyer { 0x6f528086, "BDXDE IOAT Ch2" }, 220e974f91cSConrad Meyer { 0x6f538086, "BDXDE IOAT Ch3" }, 221e974f91cSConrad Meyer 2225afc2508SConrad Meyer { 0x6f208086, "BDX IOAT Ch0" }, 2235afc2508SConrad Meyer { 0x6f218086, "BDX IOAT Ch1" }, 2245afc2508SConrad Meyer { 0x6f228086, "BDX IOAT Ch2" }, 2255afc2508SConrad Meyer { 0x6f238086, "BDX IOAT Ch3" }, 2265afc2508SConrad Meyer { 0x6f248086, "BDX IOAT Ch4" }, 2275afc2508SConrad Meyer { 0x6f258086, "BDX IOAT Ch5" }, 2285afc2508SConrad Meyer { 0x6f268086, "BDX IOAT Ch6" }, 2295afc2508SConrad Meyer { 0x6f278086, "BDX IOAT Ch7" }, 2305afc2508SConrad Meyer { 0x6f2e8086, "BDX IOAT Ch0 (RAID)" }, 2315afc2508SConrad Meyer { 0x6f2f8086, "BDX IOAT Ch1 (RAID)" }, 2325afc2508SConrad Meyer 233e974f91cSConrad Meyer { 0x00000000, NULL } 234e974f91cSConrad Meyer }; 235e974f91cSConrad Meyer 236e974f91cSConrad Meyer /* 237e974f91cSConrad Meyer * OS <-> Driver linkage functions 238e974f91cSConrad Meyer */ 239e974f91cSConrad Meyer static int 240e974f91cSConrad Meyer ioat_probe(device_t device) 241e974f91cSConrad Meyer { 242e974f91cSConrad Meyer struct _pcsid *ep; 243e974f91cSConrad Meyer u_int32_t type; 244e974f91cSConrad Meyer 245e974f91cSConrad Meyer type = pci_get_devid(device); 246e974f91cSConrad Meyer for (ep = pci_ids; ep->type; ep++) { 247e974f91cSConrad Meyer if (ep->type == type) { 248e974f91cSConrad Meyer device_set_desc(device, ep->desc); 249e974f91cSConrad Meyer return (0); 250e974f91cSConrad Meyer } 251e974f91cSConrad Meyer } 252e974f91cSConrad Meyer return (ENXIO); 253e974f91cSConrad Meyer } 254e974f91cSConrad Meyer 255e974f91cSConrad Meyer static int 256e974f91cSConrad Meyer ioat_attach(device_t device) 257e974f91cSConrad Meyer { 258e974f91cSConrad Meyer struct ioat_softc *ioat; 259e974f91cSConrad Meyer int error; 260e974f91cSConrad Meyer 261e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 262e974f91cSConrad Meyer ioat->device = device; 263e974f91cSConrad Meyer 264e974f91cSConrad Meyer error = ioat_map_pci_bar(ioat); 265e974f91cSConrad Meyer if (error != 0) 266e974f91cSConrad Meyer goto err; 267e974f91cSConrad Meyer 268e974f91cSConrad Meyer ioat->version = ioat_read_cbver(ioat); 269e974f91cSConrad Meyer if (ioat->version < IOAT_VER_3_0) { 270e974f91cSConrad Meyer error = ENODEV; 271e974f91cSConrad Meyer goto err; 272e974f91cSConrad Meyer } 273e974f91cSConrad Meyer 274e974f91cSConrad Meyer error = ioat3_attach(device); 275e974f91cSConrad Meyer if (error != 0) 276e974f91cSConrad Meyer goto err; 277e974f91cSConrad Meyer 278e974f91cSConrad Meyer error = pci_enable_busmaster(device); 279e974f91cSConrad Meyer if (error != 0) 280e974f91cSConrad Meyer goto err; 281e974f91cSConrad Meyer 282466b3540SConrad Meyer error = ioat_setup_intr(ioat); 283466b3540SConrad Meyer if (error != 0) 284466b3540SConrad Meyer goto err; 285466b3540SConrad Meyer 286cea5b880SConrad Meyer error = ioat_reset_hw(ioat); 2877afbb263SConrad Meyer if (error != 0) 288466b3540SConrad Meyer goto err; 2897afbb263SConrad Meyer 2907afbb263SConrad Meyer ioat_process_events(ioat); 2917afbb263SConrad Meyer ioat_setup_sysctl(device); 2927afbb263SConrad Meyer 2935f77bd3eSConrad Meyer ioat->chan_idx = ioat_channel_index; 294e974f91cSConrad Meyer ioat_channel[ioat_channel_index++] = ioat; 2957afbb263SConrad Meyer ioat_test_attach(); 296e974f91cSConrad Meyer 297e974f91cSConrad Meyer err: 298e974f91cSConrad Meyer if (error != 0) 299e974f91cSConrad Meyer ioat_detach(device); 300e974f91cSConrad Meyer return (error); 301e974f91cSConrad Meyer } 302e974f91cSConrad Meyer 303e974f91cSConrad Meyer static int 304e974f91cSConrad Meyer ioat_detach(device_t device) 305e974f91cSConrad Meyer { 306e974f91cSConrad Meyer struct ioat_softc *ioat; 307e974f91cSConrad Meyer 308e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3097afbb263SConrad Meyer 3107afbb263SConrad Meyer ioat_test_detach(); 3115f77bd3eSConrad Meyer 3125f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 3135f77bd3eSConrad Meyer ioat->quiescing = TRUE; 3145f77bd3eSConrad Meyer ioat_channel[ioat->chan_idx] = NULL; 3155f77bd3eSConrad Meyer 3165f77bd3eSConrad Meyer ioat_drain_locked(ioat); 3175f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 318fe720f5aSConrad Meyer 319fe720f5aSConrad Meyer ioat_teardown_intr(ioat); 320e974f91cSConrad Meyer callout_drain(&ioat->timer); 321e974f91cSConrad Meyer 322e974f91cSConrad Meyer pci_disable_busmaster(device); 323e974f91cSConrad Meyer 324e974f91cSConrad Meyer if (ioat->pci_resource != NULL) 325e974f91cSConrad Meyer bus_release_resource(device, SYS_RES_MEMORY, 326e974f91cSConrad Meyer ioat->pci_resource_id, ioat->pci_resource); 327e974f91cSConrad Meyer 328bf8553eaSConrad Meyer if (ioat->ring != NULL) 329bf8553eaSConrad Meyer ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring); 330e974f91cSConrad Meyer 331e974f91cSConrad Meyer if (ioat->comp_update != NULL) { 332e974f91cSConrad Meyer bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map); 333e974f91cSConrad Meyer bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update, 334e974f91cSConrad Meyer ioat->comp_update_map); 335e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->comp_update_tag); 336e974f91cSConrad Meyer } 337e974f91cSConrad Meyer 338e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->hw_desc_tag); 339e974f91cSConrad Meyer 3404253ea50SConrad Meyer return (0); 3414253ea50SConrad Meyer } 3424253ea50SConrad Meyer 3434253ea50SConrad Meyer static int 3444253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat) 3454253ea50SConrad Meyer { 3464253ea50SConrad Meyer 347e974f91cSConrad Meyer if (ioat->tag != NULL) 3484253ea50SConrad Meyer bus_teardown_intr(ioat->device, ioat->res, ioat->tag); 349e974f91cSConrad Meyer 350e974f91cSConrad Meyer if (ioat->res != NULL) 3514253ea50SConrad Meyer bus_release_resource(ioat->device, SYS_RES_IRQ, 352e974f91cSConrad Meyer rman_get_rid(ioat->res), ioat->res); 353e974f91cSConrad Meyer 3544253ea50SConrad Meyer pci_release_msi(ioat->device); 355e974f91cSConrad Meyer return (0); 356e974f91cSConrad Meyer } 357e974f91cSConrad Meyer 358e974f91cSConrad Meyer static int 359cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat) 360e974f91cSConrad Meyer { 361e974f91cSConrad Meyer uint64_t status; 362e974f91cSConrad Meyer uint32_t chanerr; 363e974f91cSConrad Meyer int i; 364e974f91cSConrad Meyer 365e974f91cSConrad Meyer ioat_acquire(&ioat->dmaengine); 366e974f91cSConrad Meyer ioat_null(&ioat->dmaengine, NULL, NULL, 0); 367e974f91cSConrad Meyer ioat_release(&ioat->dmaengine); 368e974f91cSConrad Meyer 369e974f91cSConrad Meyer for (i = 0; i < 100; i++) { 370e974f91cSConrad Meyer DELAY(1); 371e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 372e974f91cSConrad Meyer if (is_ioat_idle(status)) 373e974f91cSConrad Meyer return (0); 374e974f91cSConrad Meyer } 375e974f91cSConrad Meyer 376e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 377e974f91cSConrad Meyer ioat_log_message(0, "could not start channel: " 37859acd4baSConrad Meyer "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr, 37959acd4baSConrad Meyer IOAT_CHANERR_STR); 380e974f91cSConrad Meyer return (ENXIO); 381e974f91cSConrad Meyer } 382e974f91cSConrad Meyer 383e974f91cSConrad Meyer /* 384e974f91cSConrad Meyer * Initialize Hardware 385e974f91cSConrad Meyer */ 386e974f91cSConrad Meyer static int 387e974f91cSConrad Meyer ioat3_attach(device_t device) 388e974f91cSConrad Meyer { 389e974f91cSConrad Meyer struct ioat_softc *ioat; 390e974f91cSConrad Meyer struct ioat_descriptor **ring; 391e974f91cSConrad Meyer struct ioat_descriptor *next; 392e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *dma_hw_desc; 393e974f91cSConrad Meyer int i, num_descriptors; 394e974f91cSConrad Meyer int error; 395e974f91cSConrad Meyer uint8_t xfercap; 396e974f91cSConrad Meyer 397e974f91cSConrad Meyer error = 0; 398e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3991693d27bSConrad Meyer ioat->capabilities = ioat_read_dmacapability(ioat); 4001693d27bSConrad Meyer 4011693d27bSConrad Meyer ioat_log_message(1, "Capabilities: %b\n", (int)ioat->capabilities, 4021693d27bSConrad Meyer IOAT_DMACAP_STR); 403e974f91cSConrad Meyer 404e974f91cSConrad Meyer xfercap = ioat_read_xfercap(ioat); 405e974f91cSConrad Meyer ioat->max_xfer_size = 1 << xfercap; 406e974f91cSConrad Meyer 4075ca9fc2aSConrad Meyer ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & 4085ca9fc2aSConrad Meyer IOAT_INTRDELAY_SUPPORTED) != 0; 4095ca9fc2aSConrad Meyer if (ioat->intrdelay_supported) 4105ca9fc2aSConrad Meyer ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK; 4115ca9fc2aSConrad Meyer 412e974f91cSConrad Meyer /* TODO: need to check DCA here if we ever do XOR/PQ */ 413e974f91cSConrad Meyer 414e974f91cSConrad Meyer mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF); 415faefad9cSConrad Meyer mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF); 4167afbb263SConrad Meyer callout_init(&ioat->timer, 1); 417e974f91cSConrad Meyer 418faefad9cSConrad Meyer /* Establish lock order for Witness */ 419faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 420faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 421faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 422faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 423faefad9cSConrad Meyer 424e974f91cSConrad Meyer ioat->is_resize_pending = FALSE; 425e974f91cSConrad Meyer ioat->is_completion_pending = FALSE; 426e974f91cSConrad Meyer ioat->is_reset_pending = FALSE; 427e974f91cSConrad Meyer ioat->is_channel_running = FALSE; 428e974f91cSConrad Meyer 429e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0, 430e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 431e974f91cSConrad Meyer sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL, 432e974f91cSConrad Meyer &ioat->comp_update_tag); 433e974f91cSConrad Meyer 434e974f91cSConrad Meyer error = bus_dmamem_alloc(ioat->comp_update_tag, 435e974f91cSConrad Meyer (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map); 436e974f91cSConrad Meyer if (ioat->comp_update == NULL) 437e974f91cSConrad Meyer return (ENOMEM); 438e974f91cSConrad Meyer 439e974f91cSConrad Meyer error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map, 440e974f91cSConrad Meyer ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat, 441e974f91cSConrad Meyer 0); 442e974f91cSConrad Meyer if (error != 0) 443e974f91cSConrad Meyer return (error); 444e974f91cSConrad Meyer 445e974f91cSConrad Meyer ioat->ring_size_order = IOAT_MIN_ORDER; 446e974f91cSConrad Meyer 447e974f91cSConrad Meyer num_descriptors = 1 << ioat->ring_size_order; 448e974f91cSConrad Meyer 449e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0, 450e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 451e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 1, 452e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL, 453e974f91cSConrad Meyer &ioat->hw_desc_tag); 454e974f91cSConrad Meyer 455e974f91cSConrad Meyer ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT, 456bf8553eaSConrad Meyer M_ZERO | M_WAITOK); 457e974f91cSConrad Meyer if (ioat->ring == NULL) 458e974f91cSConrad Meyer return (ENOMEM); 459e974f91cSConrad Meyer 460e974f91cSConrad Meyer ring = ioat->ring; 461e974f91cSConrad Meyer for (i = 0; i < num_descriptors; i++) { 462bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK); 463e974f91cSConrad Meyer if (ring[i] == NULL) 464e974f91cSConrad Meyer return (ENOMEM); 465e974f91cSConrad Meyer 466e974f91cSConrad Meyer ring[i]->id = i; 467e974f91cSConrad Meyer } 468e974f91cSConrad Meyer 469e974f91cSConrad Meyer for (i = 0; i < num_descriptors - 1; i++) { 470e974f91cSConrad Meyer next = ring[i + 1]; 471e974f91cSConrad Meyer dma_hw_desc = ring[i]->u.dma; 472e974f91cSConrad Meyer 473e974f91cSConrad Meyer dma_hw_desc->next = next->hw_desc_bus_addr; 474e974f91cSConrad Meyer } 475e974f91cSConrad Meyer 476e974f91cSConrad Meyer ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr; 477e974f91cSConrad Meyer 478bf8553eaSConrad Meyer ioat->head = ioat->hw_head = 0; 479e974f91cSConrad Meyer ioat->tail = 0; 480e974f91cSConrad Meyer ioat->last_seen = 0; 481e974f91cSConrad Meyer return (0); 482e974f91cSConrad Meyer } 483e974f91cSConrad Meyer 484e974f91cSConrad Meyer static int 485e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat) 486e974f91cSConrad Meyer { 487e974f91cSConrad Meyer 488e974f91cSConrad Meyer ioat->pci_resource_id = PCIR_BAR(0); 489e88e14b9SConrad Meyer ioat->pci_resource = bus_alloc_resource_any(ioat->device, 490e88e14b9SConrad Meyer SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE); 491e974f91cSConrad Meyer 492e974f91cSConrad Meyer if (ioat->pci_resource == NULL) { 493e974f91cSConrad Meyer ioat_log_message(0, "unable to allocate pci resource\n"); 494e974f91cSConrad Meyer return (ENODEV); 495e974f91cSConrad Meyer } 496e974f91cSConrad Meyer 497e974f91cSConrad Meyer ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource); 498e974f91cSConrad Meyer ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource); 499e974f91cSConrad Meyer return (0); 500e974f91cSConrad Meyer } 501e974f91cSConrad Meyer 502e974f91cSConrad Meyer static void 503e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 504e974f91cSConrad Meyer { 505e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 506e974f91cSConrad Meyer 507cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 508e974f91cSConrad Meyer ioat->comp_update_bus_addr = seg[0].ds_addr; 509e974f91cSConrad Meyer } 510e974f91cSConrad Meyer 511e974f91cSConrad Meyer static void 512e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 513e974f91cSConrad Meyer { 514e974f91cSConrad Meyer bus_addr_t *baddr; 515e974f91cSConrad Meyer 516cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 517e974f91cSConrad Meyer baddr = arg; 518e974f91cSConrad Meyer *baddr = segs->ds_addr; 519e974f91cSConrad Meyer } 520e974f91cSConrad Meyer 521e974f91cSConrad Meyer /* 522e974f91cSConrad Meyer * Interrupt setup and handlers 523e974f91cSConrad Meyer */ 524e974f91cSConrad Meyer static int 5254253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat) 526e974f91cSConrad Meyer { 527e974f91cSConrad Meyer uint32_t num_vectors; 528e974f91cSConrad Meyer int error; 529e974f91cSConrad Meyer boolean_t use_msix; 530e974f91cSConrad Meyer boolean_t force_legacy_interrupts; 531e974f91cSConrad Meyer 532e974f91cSConrad Meyer use_msix = FALSE; 533e974f91cSConrad Meyer force_legacy_interrupts = FALSE; 534e974f91cSConrad Meyer 535e974f91cSConrad Meyer if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) { 536e974f91cSConrad Meyer num_vectors = 1; 537e974f91cSConrad Meyer pci_alloc_msix(ioat->device, &num_vectors); 538e974f91cSConrad Meyer if (num_vectors == 1) 539e974f91cSConrad Meyer use_msix = TRUE; 540e974f91cSConrad Meyer } 541e974f91cSConrad Meyer 542e974f91cSConrad Meyer if (use_msix) { 543e974f91cSConrad Meyer ioat->rid = 1; 544e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 545e974f91cSConrad Meyer &ioat->rid, RF_ACTIVE); 546e974f91cSConrad Meyer } else { 547e974f91cSConrad Meyer ioat->rid = 0; 548e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 549e974f91cSConrad Meyer &ioat->rid, RF_SHAREABLE | RF_ACTIVE); 550e974f91cSConrad Meyer } 551e974f91cSConrad Meyer if (ioat->res == NULL) { 552e974f91cSConrad Meyer ioat_log_message(0, "bus_alloc_resource failed\n"); 553e974f91cSConrad Meyer return (ENOMEM); 554e974f91cSConrad Meyer } 555e974f91cSConrad Meyer 556e974f91cSConrad Meyer ioat->tag = NULL; 557e974f91cSConrad Meyer error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE | 558e974f91cSConrad Meyer INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag); 559e974f91cSConrad Meyer if (error != 0) { 560e974f91cSConrad Meyer ioat_log_message(0, "bus_setup_intr failed\n"); 561e974f91cSConrad Meyer return (error); 562e974f91cSConrad Meyer } 563e974f91cSConrad Meyer 564e974f91cSConrad Meyer ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN); 565e974f91cSConrad Meyer return (0); 566e974f91cSConrad Meyer } 567e974f91cSConrad Meyer 5684253ea50SConrad Meyer static boolean_t 5690d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat) 5704253ea50SConrad Meyer { 5714253ea50SConrad Meyer u_int32_t pciid; 5724253ea50SConrad Meyer 5734253ea50SConrad Meyer pciid = pci_get_devid(ioat->device); 5744253ea50SConrad Meyer switch (pciid) { 5750d1a05d9SConrad Meyer /* BWD: */ 5760d1a05d9SConrad Meyer case 0x0c508086: 5770d1a05d9SConrad Meyer case 0x0c518086: 5780d1a05d9SConrad Meyer case 0x0c528086: 5790d1a05d9SConrad Meyer case 0x0c538086: 5800d1a05d9SConrad Meyer /* BDXDE: */ 5814253ea50SConrad Meyer case 0x6f508086: 5824253ea50SConrad Meyer case 0x6f518086: 5834253ea50SConrad Meyer case 0x6f528086: 5844253ea50SConrad Meyer case 0x6f538086: 5854253ea50SConrad Meyer return (TRUE); 5864253ea50SConrad Meyer } 5874253ea50SConrad Meyer 5884253ea50SConrad Meyer return (FALSE); 5894253ea50SConrad Meyer } 5904253ea50SConrad Meyer 591e974f91cSConrad Meyer static void 592e974f91cSConrad Meyer ioat_interrupt_handler(void *arg) 593e974f91cSConrad Meyer { 594e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 595e974f91cSConrad Meyer 59601fbbc88SConrad Meyer ioat->stats.interrupts++; 597e974f91cSConrad Meyer ioat_process_events(ioat); 598e974f91cSConrad Meyer } 599e974f91cSConrad Meyer 600faefad9cSConrad Meyer static int 601faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr) 602faefad9cSConrad Meyer { 603faefad9cSConrad Meyer 604faefad9cSConrad Meyer if (chanerr == 0) 605faefad9cSConrad Meyer return (0); 606faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0) 607faefad9cSConrad Meyer return (EFAULT); 608faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0) 609faefad9cSConrad Meyer return (EIO); 610faefad9cSConrad Meyer /* This one is probably our fault: */ 611faefad9cSConrad Meyer if ((chanerr & IOAT_CHANERR_NDADDERR) != 0) 612faefad9cSConrad Meyer return (EIO); 613faefad9cSConrad Meyer return (EIO); 614faefad9cSConrad Meyer } 615faefad9cSConrad Meyer 616e974f91cSConrad Meyer static void 617e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat) 618e974f91cSConrad Meyer { 619e974f91cSConrad Meyer struct ioat_descriptor *desc; 620e974f91cSConrad Meyer struct bus_dmadesc *dmadesc; 621e974f91cSConrad Meyer uint64_t comp_update, status; 622faefad9cSConrad Meyer uint32_t completed, chanerr; 623faefad9cSConrad Meyer int error; 624e974f91cSConrad Meyer 625e974f91cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 626e974f91cSConrad Meyer 627e974f91cSConrad Meyer completed = 0; 628e974f91cSConrad Meyer comp_update = *ioat->comp_update; 629e974f91cSConrad Meyer status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK; 630e974f91cSConrad Meyer 63143fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 632e974f91cSConrad Meyer 6334becebdfSConrad Meyer if (status == ioat->last_seen) 6344becebdfSConrad Meyer goto out; 635e974f91cSConrad Meyer 636e974f91cSConrad Meyer while (1) { 637e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 638e974f91cSConrad Meyer dmadesc = &desc->bus_dmadesc; 63943fc1847SConrad Meyer CTR1(KTR_IOAT, "completing desc %d", ioat->tail); 640e974f91cSConrad Meyer 641faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 642faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 0); 643e974f91cSConrad Meyer 644466b3540SConrad Meyer completed++; 645e974f91cSConrad Meyer ioat->tail++; 646e974f91cSConrad Meyer if (desc->hw_desc_bus_addr == status) 647e974f91cSConrad Meyer break; 648e974f91cSConrad Meyer } 649e974f91cSConrad Meyer 650e974f91cSConrad Meyer ioat->last_seen = desc->hw_desc_bus_addr; 651e974f91cSConrad Meyer 652e974f91cSConrad Meyer if (ioat->head == ioat->tail) { 653e974f91cSConrad Meyer ioat->is_completion_pending = FALSE; 654fe720f5aSConrad Meyer callout_reset(&ioat->timer, IOAT_INTR_TIMO, 655fe720f5aSConrad Meyer ioat_timer_callback, ioat); 656e974f91cSConrad Meyer } 657e974f91cSConrad Meyer 65801fbbc88SConrad Meyer ioat->stats.descriptors_processed += completed; 65901fbbc88SConrad Meyer 6604becebdfSConrad Meyer out: 661e974f91cSConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 662e974f91cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 663466b3540SConrad Meyer 664466b3540SConrad Meyer ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF); 665bf8553eaSConrad Meyer wakeup(&ioat->tail); 666faefad9cSConrad Meyer 667faefad9cSConrad Meyer if (!is_ioat_halted(comp_update)) 668faefad9cSConrad Meyer return; 669faefad9cSConrad Meyer 67001fbbc88SConrad Meyer ioat->stats.channel_halts++; 67101fbbc88SConrad Meyer 672faefad9cSConrad Meyer /* 673faefad9cSConrad Meyer * Fatal programming error on this DMA channel. Flush any outstanding 674faefad9cSConrad Meyer * work with error status and restart the engine. 675faefad9cSConrad Meyer */ 676faefad9cSConrad Meyer ioat_log_message(0, "Channel halted due to fatal programming error\n"); 677faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 678faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 679faefad9cSConrad Meyer ioat->quiescing = TRUE; 680faefad9cSConrad Meyer 681faefad9cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 682faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 68301fbbc88SConrad Meyer ioat->stats.last_halt_chanerr = chanerr; 684faefad9cSConrad Meyer 685faefad9cSConrad Meyer while (ioat_get_active(ioat) > 0) { 686faefad9cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 687faefad9cSConrad Meyer dmadesc = &desc->bus_dmadesc; 688faefad9cSConrad Meyer CTR1(KTR_IOAT, "completing err desc %d", ioat->tail); 689faefad9cSConrad Meyer 690faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 691faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 692faefad9cSConrad Meyer chanerr_to_errno(chanerr)); 693faefad9cSConrad Meyer 694faefad9cSConrad Meyer ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF); 695faefad9cSConrad Meyer ioat->tail++; 69601fbbc88SConrad Meyer ioat->stats.descriptors_processed++; 69701fbbc88SConrad Meyer ioat->stats.descriptors_error++; 698faefad9cSConrad Meyer } 699faefad9cSConrad Meyer 700faefad9cSConrad Meyer /* Clear error status */ 701faefad9cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 702faefad9cSConrad Meyer 703faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 704faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 705faefad9cSConrad Meyer 706faefad9cSConrad Meyer ioat_log_message(0, "Resetting channel to recover from error\n"); 707faefad9cSConrad Meyer error = ioat_reset_hw(ioat); 708faefad9cSConrad Meyer KASSERT(error == 0, ("%s: reset failed: %d", __func__, error)); 709e974f91cSConrad Meyer } 710e974f91cSConrad Meyer 711e974f91cSConrad Meyer /* 712e974f91cSConrad Meyer * User API functions 713e974f91cSConrad Meyer */ 714e974f91cSConrad Meyer bus_dmaengine_t 715e974f91cSConrad Meyer ioat_get_dmaengine(uint32_t index) 716e974f91cSConrad Meyer { 7175f77bd3eSConrad Meyer struct ioat_softc *sc; 718e974f91cSConrad Meyer 719466b3540SConrad Meyer if (index >= ioat_channel_index) 720e974f91cSConrad Meyer return (NULL); 7215f77bd3eSConrad Meyer 7225f77bd3eSConrad Meyer sc = ioat_channel[index]; 7235f77bd3eSConrad Meyer if (sc == NULL || sc->quiescing) 7245f77bd3eSConrad Meyer return (NULL); 7255f77bd3eSConrad Meyer 7265f77bd3eSConrad Meyer return (&ioat_get(sc, IOAT_DMAENGINE_REF)->dmaengine); 727466b3540SConrad Meyer } 728466b3540SConrad Meyer 729466b3540SConrad Meyer void 730466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine) 731466b3540SConrad Meyer { 732466b3540SConrad Meyer struct ioat_softc *ioat; 733466b3540SConrad Meyer 734466b3540SConrad Meyer ioat = to_ioat_softc(dmaengine); 735466b3540SConrad Meyer ioat_put(ioat, IOAT_DMAENGINE_REF); 736e974f91cSConrad Meyer } 737e974f91cSConrad Meyer 7385ca9fc2aSConrad Meyer int 73931bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine) 74031bf2875SConrad Meyer { 74131bf2875SConrad Meyer struct ioat_softc *ioat; 74231bf2875SConrad Meyer 74331bf2875SConrad Meyer ioat = to_ioat_softc(dmaengine); 74431bf2875SConrad Meyer return (ioat->version); 74531bf2875SConrad Meyer } 74631bf2875SConrad Meyer 747*bd81fe68SConrad Meyer size_t 748*bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine) 749*bd81fe68SConrad Meyer { 750*bd81fe68SConrad Meyer struct ioat_softc *ioat; 751*bd81fe68SConrad Meyer 752*bd81fe68SConrad Meyer ioat = to_ioat_softc(dmaengine); 753*bd81fe68SConrad Meyer return (ioat->max_xfer_size); 754*bd81fe68SConrad Meyer } 755*bd81fe68SConrad Meyer 75631bf2875SConrad Meyer int 7575ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay) 7585ca9fc2aSConrad Meyer { 7595ca9fc2aSConrad Meyer struct ioat_softc *ioat; 7605ca9fc2aSConrad Meyer 7615ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 7625ca9fc2aSConrad Meyer if (!ioat->intrdelay_supported) 7635ca9fc2aSConrad Meyer return (ENODEV); 7645ca9fc2aSConrad Meyer if (delay > ioat->intrdelay_max) 7655ca9fc2aSConrad Meyer return (ERANGE); 7665ca9fc2aSConrad Meyer 7675ca9fc2aSConrad Meyer ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay); 7685ca9fc2aSConrad Meyer ioat->cached_intrdelay = 7695ca9fc2aSConrad Meyer ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK; 7705ca9fc2aSConrad Meyer return (0); 7715ca9fc2aSConrad Meyer } 7725ca9fc2aSConrad Meyer 7735ca9fc2aSConrad Meyer uint16_t 7745ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine) 7755ca9fc2aSConrad Meyer { 7765ca9fc2aSConrad Meyer struct ioat_softc *ioat; 7775ca9fc2aSConrad Meyer 7785ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 7795ca9fc2aSConrad Meyer return (ioat->intrdelay_max); 7805ca9fc2aSConrad Meyer } 7815ca9fc2aSConrad Meyer 782e974f91cSConrad Meyer void 783e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine) 784e974f91cSConrad Meyer { 785e974f91cSConrad Meyer struct ioat_softc *ioat; 786e974f91cSConrad Meyer 787e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 788e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 78943fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 790e974f91cSConrad Meyer } 791e974f91cSConrad Meyer 792e974f91cSConrad Meyer void 793e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine) 794e974f91cSConrad Meyer { 795e974f91cSConrad Meyer struct ioat_softc *ioat; 796e974f91cSConrad Meyer 797e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 79843fc1847SConrad Meyer CTR0(KTR_IOAT, __func__); 799bf8553eaSConrad Meyer ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head); 800e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 801e974f91cSConrad Meyer } 802e974f91cSConrad Meyer 8039e3bbf26SConrad Meyer static struct ioat_descriptor * 8049e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op, 8059e3bbf26SConrad Meyer uint32_t size, uint64_t src, uint64_t dst, 8069e3bbf26SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, 8079e3bbf26SConrad Meyer uint32_t flags) 808e974f91cSConrad Meyer { 8099e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 810e974f91cSConrad Meyer struct ioat_descriptor *desc; 811bf8553eaSConrad Meyer int mflags; 812e974f91cSConrad Meyer 8139e3bbf26SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 8149e3bbf26SConrad Meyer 815e974f91cSConrad Meyer KASSERT((flags & ~DMA_ALL_FLAGS) == 0, ("Unrecognized flag(s): %#x", 816e974f91cSConrad Meyer flags & ~DMA_ALL_FLAGS)); 817bf8553eaSConrad Meyer if ((flags & DMA_NO_WAIT) != 0) 818bf8553eaSConrad Meyer mflags = M_NOWAIT; 819bf8553eaSConrad Meyer else 820bf8553eaSConrad Meyer mflags = M_WAITOK; 821e974f91cSConrad Meyer 8229e3bbf26SConrad Meyer if (size > ioat->max_xfer_size) { 8239e3bbf26SConrad Meyer ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n", 8249e3bbf26SConrad Meyer __func__, ioat->max_xfer_size, (unsigned)size); 8259e3bbf26SConrad Meyer return (NULL); 8269e3bbf26SConrad Meyer } 827e974f91cSConrad Meyer 828bf8553eaSConrad Meyer if (ioat_reserve_space(ioat, 1, mflags) != 0) 829e974f91cSConrad Meyer return (NULL); 830e974f91cSConrad Meyer 831e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 8329e3bbf26SConrad Meyer hw_desc = desc->u.generic; 833e974f91cSConrad Meyer 834e974f91cSConrad Meyer hw_desc->u.control_raw = 0; 8359e3bbf26SConrad Meyer hw_desc->u.control_generic.op = op; 8369e3bbf26SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 837e974f91cSConrad Meyer 838e974f91cSConrad Meyer if ((flags & DMA_INT_EN) != 0) 8399e3bbf26SConrad Meyer hw_desc->u.control_generic.int_enable = 1; 840e974f91cSConrad Meyer 8419e3bbf26SConrad Meyer hw_desc->size = size; 8429e3bbf26SConrad Meyer hw_desc->src_addr = src; 8439e3bbf26SConrad Meyer hw_desc->dest_addr = dst; 844e974f91cSConrad Meyer 845e974f91cSConrad Meyer desc->bus_dmadesc.callback_fn = callback_fn; 846e974f91cSConrad Meyer desc->bus_dmadesc.callback_arg = callback_arg; 8479e3bbf26SConrad Meyer return (desc); 8489e3bbf26SConrad Meyer } 849e974f91cSConrad Meyer 8509e3bbf26SConrad Meyer struct bus_dmadesc * 8519e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn, 8529e3bbf26SConrad Meyer void *callback_arg, uint32_t flags) 8539e3bbf26SConrad Meyer { 8549e3bbf26SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 8559e3bbf26SConrad Meyer struct ioat_descriptor *desc; 8569e3bbf26SConrad Meyer struct ioat_softc *ioat; 8579e3bbf26SConrad Meyer 8589e3bbf26SConrad Meyer CTR0(KTR_IOAT, __func__); 8599e3bbf26SConrad Meyer ioat = to_ioat_softc(dmaengine); 8609e3bbf26SConrad Meyer 8619e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn, 8629e3bbf26SConrad Meyer callback_arg, flags); 8639e3bbf26SConrad Meyer if (desc == NULL) 8649e3bbf26SConrad Meyer return (NULL); 8659e3bbf26SConrad Meyer 8669e3bbf26SConrad Meyer hw_desc = desc->u.dma; 8679e3bbf26SConrad Meyer hw_desc->u.control.null = 1; 868e974f91cSConrad Meyer ioat_submit_single(ioat); 869e974f91cSConrad Meyer return (&desc->bus_dmadesc); 870e974f91cSConrad Meyer } 871e974f91cSConrad Meyer 872e974f91cSConrad Meyer struct bus_dmadesc * 873e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst, 874e974f91cSConrad Meyer bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn, 875e974f91cSConrad Meyer void *callback_arg, uint32_t flags) 876e974f91cSConrad Meyer { 877e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 8789e3bbf26SConrad Meyer struct ioat_descriptor *desc; 879e974f91cSConrad Meyer struct ioat_softc *ioat; 880e974f91cSConrad Meyer 8819e3bbf26SConrad Meyer CTR0(KTR_IOAT, __func__); 882e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 883e974f91cSConrad Meyer 8849e3bbf26SConrad Meyer if (((src | dst) & (0xffffull << 48)) != 0) { 8859e3bbf26SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 8869e3bbf26SConrad Meyer __func__); 887e974f91cSConrad Meyer return (NULL); 888e974f91cSConrad Meyer } 889e974f91cSConrad Meyer 8909e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn, 8919e3bbf26SConrad Meyer callback_arg, flags); 8929e3bbf26SConrad Meyer if (desc == NULL) 893e974f91cSConrad Meyer return (NULL); 894e974f91cSConrad Meyer 895e974f91cSConrad Meyer hw_desc = desc->u.dma; 896e974f91cSConrad Meyer if (g_ioat_debug_level >= 3) 897e974f91cSConrad Meyer dump_descriptor(hw_desc); 898e974f91cSConrad Meyer 899e974f91cSConrad Meyer ioat_submit_single(ioat); 900e974f91cSConrad Meyer return (&desc->bus_dmadesc); 901e974f91cSConrad Meyer } 902e974f91cSConrad Meyer 9032a4fd6b1SConrad Meyer struct bus_dmadesc * 9049950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1, 9059950fde0SConrad Meyer bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2, 9069950fde0SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 9079950fde0SConrad Meyer { 9089950fde0SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 9099950fde0SConrad Meyer struct ioat_descriptor *desc; 9109950fde0SConrad Meyer struct ioat_softc *ioat; 9119950fde0SConrad Meyer 9129950fde0SConrad Meyer CTR0(KTR_IOAT, __func__); 9139950fde0SConrad Meyer ioat = to_ioat_softc(dmaengine); 9149950fde0SConrad Meyer 9159950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) { 9169950fde0SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 9179950fde0SConrad Meyer __func__); 9189950fde0SConrad Meyer return (NULL); 9199950fde0SConrad Meyer } 9209950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) { 9219950fde0SConrad Meyer ioat_log_message(0, "%s: Addresses must be page-aligned\n", 9229950fde0SConrad Meyer __func__); 9239950fde0SConrad Meyer return (NULL); 9249950fde0SConrad Meyer } 9259950fde0SConrad Meyer 9269950fde0SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1, 9279950fde0SConrad Meyer callback_fn, callback_arg, flags); 9289950fde0SConrad Meyer if (desc == NULL) 9299950fde0SConrad Meyer return (NULL); 9309950fde0SConrad Meyer 9319950fde0SConrad Meyer hw_desc = desc->u.dma; 9329950fde0SConrad Meyer if (src2 != src1 + PAGE_SIZE) { 9339950fde0SConrad Meyer hw_desc->u.control.src_page_break = 1; 9349950fde0SConrad Meyer hw_desc->next_src_addr = src2; 9359950fde0SConrad Meyer } 9369950fde0SConrad Meyer if (dst2 != dst1 + PAGE_SIZE) { 9379950fde0SConrad Meyer hw_desc->u.control.dest_page_break = 1; 9389950fde0SConrad Meyer hw_desc->next_dest_addr = dst2; 9399950fde0SConrad Meyer } 9409950fde0SConrad Meyer 9419950fde0SConrad Meyer if (g_ioat_debug_level >= 3) 9429950fde0SConrad Meyer dump_descriptor(hw_desc); 9439950fde0SConrad Meyer 9449950fde0SConrad Meyer ioat_submit_single(ioat); 9459950fde0SConrad Meyer return (&desc->bus_dmadesc); 9469950fde0SConrad Meyer } 9479950fde0SConrad Meyer 9489950fde0SConrad Meyer struct bus_dmadesc * 9492a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern, 9502a4fd6b1SConrad Meyer bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg, 9512a4fd6b1SConrad Meyer uint32_t flags) 9522a4fd6b1SConrad Meyer { 9532a4fd6b1SConrad Meyer struct ioat_fill_hw_descriptor *hw_desc; 9542a4fd6b1SConrad Meyer struct ioat_descriptor *desc; 9552a4fd6b1SConrad Meyer struct ioat_softc *ioat; 9562a4fd6b1SConrad Meyer 9572a4fd6b1SConrad Meyer CTR0(KTR_IOAT, __func__); 9582a4fd6b1SConrad Meyer ioat = to_ioat_softc(dmaengine); 9592a4fd6b1SConrad Meyer 9601693d27bSConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) { 9611693d27bSConrad Meyer ioat_log_message(0, "%s: Device lacks BFILL capability\n", 9621693d27bSConrad Meyer __func__); 9631693d27bSConrad Meyer return (NULL); 9641693d27bSConrad Meyer } 9651693d27bSConrad Meyer 9662a4fd6b1SConrad Meyer if ((dst & (0xffffull << 48)) != 0) { 9672a4fd6b1SConrad Meyer ioat_log_message(0, "%s: High 16 bits of dst invalid\n", 9682a4fd6b1SConrad Meyer __func__); 9692a4fd6b1SConrad Meyer return (NULL); 9702a4fd6b1SConrad Meyer } 9712a4fd6b1SConrad Meyer 9722a4fd6b1SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst, 9732a4fd6b1SConrad Meyer callback_fn, callback_arg, flags); 9742a4fd6b1SConrad Meyer if (desc == NULL) 9752a4fd6b1SConrad Meyer return (NULL); 9762a4fd6b1SConrad Meyer 9772a4fd6b1SConrad Meyer hw_desc = desc->u.fill; 9782a4fd6b1SConrad Meyer if (g_ioat_debug_level >= 3) 9792a4fd6b1SConrad Meyer dump_descriptor(hw_desc); 9802a4fd6b1SConrad Meyer 9812a4fd6b1SConrad Meyer ioat_submit_single(ioat); 9822a4fd6b1SConrad Meyer return (&desc->bus_dmadesc); 9832a4fd6b1SConrad Meyer } 9842a4fd6b1SConrad Meyer 985e974f91cSConrad Meyer /* 986e974f91cSConrad Meyer * Ring Management 987e974f91cSConrad Meyer */ 988e974f91cSConrad Meyer static inline uint32_t 989e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat) 990e974f91cSConrad Meyer { 991e974f91cSConrad Meyer 992e974f91cSConrad Meyer return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1)); 993e974f91cSConrad Meyer } 994e974f91cSConrad Meyer 995e974f91cSConrad Meyer static inline uint32_t 996e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat) 997e974f91cSConrad Meyer { 998e974f91cSConrad Meyer 999e974f91cSConrad Meyer return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1); 1000e974f91cSConrad Meyer } 1001e974f91cSConrad Meyer 1002e974f91cSConrad Meyer static struct ioat_descriptor * 1003bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags) 1004e974f91cSConrad Meyer { 10059e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 1006e974f91cSConrad Meyer struct ioat_descriptor *desc; 1007bf8553eaSConrad Meyer int error, busdmaflag; 1008e974f91cSConrad Meyer 1009f46011aeSConrad Meyer error = ENOMEM; 1010f46011aeSConrad Meyer hw_desc = NULL; 1011f46011aeSConrad Meyer 1012bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) 1013bf8553eaSConrad Meyer busdmaflag = BUS_DMA_WAITOK; 1014bf8553eaSConrad Meyer else 1015bf8553eaSConrad Meyer busdmaflag = BUS_DMA_NOWAIT; 1016bf8553eaSConrad Meyer 1017bf8553eaSConrad Meyer desc = malloc(sizeof(*desc), M_IOAT, mflags); 1018e974f91cSConrad Meyer if (desc == NULL) 1019f46011aeSConrad Meyer goto out; 1020e974f91cSConrad Meyer 1021f46011aeSConrad Meyer bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc, 1022bf8553eaSConrad Meyer BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map); 1023f46011aeSConrad Meyer if (hw_desc == NULL) 1024f46011aeSConrad Meyer goto out; 1025e974f91cSConrad Meyer 1026faefad9cSConrad Meyer memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc)); 10279e3bbf26SConrad Meyer desc->u.generic = hw_desc; 1028f46011aeSConrad Meyer 1029f46011aeSConrad Meyer error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc, 1030f46011aeSConrad Meyer sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr, 1031bf8553eaSConrad Meyer busdmaflag); 1032f46011aeSConrad Meyer if (error) 1033f46011aeSConrad Meyer goto out; 1034f46011aeSConrad Meyer 1035f46011aeSConrad Meyer out: 1036f46011aeSConrad Meyer if (error) { 1037f46011aeSConrad Meyer ioat_free_ring_entry(ioat, desc); 1038f46011aeSConrad Meyer return (NULL); 1039f46011aeSConrad Meyer } 1040e974f91cSConrad Meyer return (desc); 1041e974f91cSConrad Meyer } 1042e974f91cSConrad Meyer 1043e974f91cSConrad Meyer static void 1044e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc) 1045e974f91cSConrad Meyer { 1046e974f91cSConrad Meyer 1047e974f91cSConrad Meyer if (desc == NULL) 1048e974f91cSConrad Meyer return; 1049e974f91cSConrad Meyer 10509e3bbf26SConrad Meyer if (desc->u.generic) 10519e3bbf26SConrad Meyer bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic, 1052e974f91cSConrad Meyer ioat->hw_desc_map); 1053e974f91cSConrad Meyer free(desc, M_IOAT); 1054e974f91cSConrad Meyer } 1055e974f91cSConrad Meyer 1056bf8553eaSConrad Meyer /* 1057bf8553eaSConrad Meyer * Reserves space in this IOAT descriptor ring by ensuring enough slots remain 1058bf8553eaSConrad Meyer * for 'num_descs'. 1059bf8553eaSConrad Meyer * 1060bf8553eaSConrad Meyer * If mflags contains M_WAITOK, blocks until enough space is available. 1061bf8553eaSConrad Meyer * 1062bf8553eaSConrad Meyer * Returns zero on success, or an errno on error. If num_descs is beyond the 1063bf8553eaSConrad Meyer * maximum ring size, returns EINVAl; if allocation would block and mflags 1064bf8553eaSConrad Meyer * contains M_NOWAIT, returns EAGAIN. 1065bf8553eaSConrad Meyer * 1066bf8553eaSConrad Meyer * Must be called with the submit_lock held; returns with the lock held. The 1067bf8553eaSConrad Meyer * lock may be dropped to allocate the ring. 1068bf8553eaSConrad Meyer * 1069bf8553eaSConrad Meyer * (The submit_lock is needed to add any entries to the ring, so callers are 1070bf8553eaSConrad Meyer * assured enough room is available.) 1071bf8553eaSConrad Meyer */ 1072e974f91cSConrad Meyer static int 1073bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags) 1074e974f91cSConrad Meyer { 1075bf8553eaSConrad Meyer struct ioat_descriptor **new_ring; 1076bf8553eaSConrad Meyer uint32_t order; 1077bf8553eaSConrad Meyer int error; 1078e974f91cSConrad Meyer 1079bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1080bf8553eaSConrad Meyer error = 0; 1081e974f91cSConrad Meyer 1082bf8553eaSConrad Meyer if (num_descs < 1 || num_descs > (1 << IOAT_MAX_ORDER)) { 1083bf8553eaSConrad Meyer error = EINVAL; 1084bf8553eaSConrad Meyer goto out; 1085e974f91cSConrad Meyer } 10865f77bd3eSConrad Meyer if (ioat->quiescing) { 10875f77bd3eSConrad Meyer error = ENXIO; 10885f77bd3eSConrad Meyer goto out; 10895f77bd3eSConrad Meyer } 1090bf8553eaSConrad Meyer 1091bf8553eaSConrad Meyer for (;;) { 1092bf8553eaSConrad Meyer if (ioat_get_ring_space(ioat) >= num_descs) 1093bf8553eaSConrad Meyer goto out; 1094bf8553eaSConrad Meyer 1095bf8553eaSConrad Meyer order = ioat->ring_size_order; 1096bf8553eaSConrad Meyer if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) { 1097bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) { 1098bf8553eaSConrad Meyer msleep(&ioat->tail, &ioat->submit_lock, 0, 1099bf8553eaSConrad Meyer "ioat_rsz", 0); 1100bf8553eaSConrad Meyer continue; 1101bf8553eaSConrad Meyer } 1102bf8553eaSConrad Meyer 1103bf8553eaSConrad Meyer error = EAGAIN; 1104bf8553eaSConrad Meyer break; 1105bf8553eaSConrad Meyer } 1106bf8553eaSConrad Meyer 1107bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1108bf8553eaSConrad Meyer for (;;) { 1109bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1110bf8553eaSConrad Meyer 1111bf8553eaSConrad Meyer new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1), 1112bf8553eaSConrad Meyer TRUE, mflags); 1113bf8553eaSConrad Meyer 1114bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1115bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1116bf8553eaSConrad Meyer ("is_resize_pending should protect order")); 1117bf8553eaSConrad Meyer 1118bf8553eaSConrad Meyer if (new_ring == NULL) { 1119bf8553eaSConrad Meyer KASSERT((mflags & M_WAITOK) == 0, 1120bf8553eaSConrad Meyer ("allocation failed")); 1121bf8553eaSConrad Meyer error = EAGAIN; 1122bf8553eaSConrad Meyer break; 1123bf8553eaSConrad Meyer } 1124bf8553eaSConrad Meyer 1125bf8553eaSConrad Meyer error = ring_grow(ioat, order, new_ring); 1126bf8553eaSConrad Meyer if (error == 0) 1127bf8553eaSConrad Meyer break; 1128bf8553eaSConrad Meyer } 1129bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1130bf8553eaSConrad Meyer wakeup(&ioat->tail); 1131bf8553eaSConrad Meyer if (error) 1132bf8553eaSConrad Meyer break; 1133bf8553eaSConrad Meyer } 1134bf8553eaSConrad Meyer 1135bf8553eaSConrad Meyer out: 1136bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1137bf8553eaSConrad Meyer return (error); 1138bf8553eaSConrad Meyer } 1139bf8553eaSConrad Meyer 1140bf8553eaSConrad Meyer static struct ioat_descriptor ** 1141bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr, 1142bf8553eaSConrad Meyer int mflags) 1143bf8553eaSConrad Meyer { 1144bf8553eaSConrad Meyer struct ioat_descriptor **ring; 1145bf8553eaSConrad Meyer uint32_t i; 1146bf8553eaSConrad Meyer int error; 1147bf8553eaSConrad Meyer 1148bf8553eaSConrad Meyer KASSERT(size > 0 && powerof2(size), ("bogus size")); 1149bf8553eaSConrad Meyer 1150bf8553eaSConrad Meyer ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags); 1151bf8553eaSConrad Meyer if (ring == NULL) 1152bf8553eaSConrad Meyer return (NULL); 1153bf8553eaSConrad Meyer 1154bf8553eaSConrad Meyer if (need_dscr) { 1155bf8553eaSConrad Meyer error = ENOMEM; 1156bf8553eaSConrad Meyer for (i = size / 2; i < size; i++) { 1157bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, mflags); 1158bf8553eaSConrad Meyer if (ring[i] == NULL) 1159bf8553eaSConrad Meyer goto out; 1160bf8553eaSConrad Meyer ring[i]->id = i; 1161bf8553eaSConrad Meyer } 1162bf8553eaSConrad Meyer } 1163bf8553eaSConrad Meyer error = 0; 1164bf8553eaSConrad Meyer 1165bf8553eaSConrad Meyer out: 1166bf8553eaSConrad Meyer if (error != 0 && ring != NULL) { 1167bf8553eaSConrad Meyer ioat_free_ring(ioat, size, ring); 1168bf8553eaSConrad Meyer ring = NULL; 1169bf8553eaSConrad Meyer } 1170bf8553eaSConrad Meyer return (ring); 1171bf8553eaSConrad Meyer } 1172bf8553eaSConrad Meyer 1173bf8553eaSConrad Meyer static void 1174bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size, 1175bf8553eaSConrad Meyer struct ioat_descriptor **ring) 1176bf8553eaSConrad Meyer { 1177bf8553eaSConrad Meyer uint32_t i; 1178bf8553eaSConrad Meyer 1179bf8553eaSConrad Meyer for (i = 0; i < size; i++) { 1180bf8553eaSConrad Meyer if (ring[i] != NULL) 1181bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ring[i]); 1182bf8553eaSConrad Meyer } 1183bf8553eaSConrad Meyer free(ring, M_IOAT); 1184e974f91cSConrad Meyer } 1185e974f91cSConrad Meyer 1186e974f91cSConrad Meyer static struct ioat_descriptor * 1187e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index) 1188e974f91cSConrad Meyer { 1189e974f91cSConrad Meyer 1190e974f91cSConrad Meyer return (ioat->ring[index % (1 << ioat->ring_size_order)]); 1191e974f91cSConrad Meyer } 1192e974f91cSConrad Meyer 1193bf8553eaSConrad Meyer static int 1194bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder, 1195bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1196e974f91cSConrad Meyer { 1197bf8553eaSConrad Meyer struct ioat_descriptor *tmp, *next; 1198e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1199bf8553eaSConrad Meyer uint32_t oldsize, newsize, head, tail, i, end; 1200bf8553eaSConrad Meyer int error; 1201e974f91cSConrad Meyer 1202bf8553eaSConrad Meyer CTR0(KTR_IOAT, __func__); 1203e974f91cSConrad Meyer 1204bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1205bf8553eaSConrad Meyer 1206bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) { 1207bf8553eaSConrad Meyer error = EINVAL; 1208bf8553eaSConrad Meyer goto out; 1209bf8553eaSConrad Meyer } 1210bf8553eaSConrad Meyer 1211bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1212bf8553eaSConrad Meyer newsize = (1 << (oldorder + 1)); 1213bf8553eaSConrad Meyer 1214bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1215bf8553eaSConrad Meyer 1216bf8553eaSConrad Meyer head = ioat->head & (oldsize - 1); 1217bf8553eaSConrad Meyer tail = ioat->tail & (oldsize - 1); 1218bf8553eaSConrad Meyer 1219bf8553eaSConrad Meyer /* Copy old descriptors to new ring */ 1220bf8553eaSConrad Meyer for (i = 0; i < oldsize; i++) 1221bf8553eaSConrad Meyer newring[i] = ioat->ring[i]; 1222e974f91cSConrad Meyer 1223e974f91cSConrad Meyer /* 1224bf8553eaSConrad Meyer * If head has wrapped but tail hasn't, we must swap some descriptors 1225bf8553eaSConrad Meyer * around so that tail can increment directly to head. 1226e974f91cSConrad Meyer */ 1227bf8553eaSConrad Meyer if (head < tail) { 1228bf8553eaSConrad Meyer for (i = 0; i <= head; i++) { 1229bf8553eaSConrad Meyer tmp = newring[oldsize + i]; 1230e974f91cSConrad Meyer 1231bf8553eaSConrad Meyer newring[oldsize + i] = newring[i]; 1232bf8553eaSConrad Meyer newring[oldsize + i]->id = oldsize + i; 1233e974f91cSConrad Meyer 1234bf8553eaSConrad Meyer newring[i] = tmp; 1235bf8553eaSConrad Meyer newring[i]->id = i; 1236bf8553eaSConrad Meyer } 1237bf8553eaSConrad Meyer head += oldsize; 1238e974f91cSConrad Meyer } 1239e974f91cSConrad Meyer 1240bf8553eaSConrad Meyer KASSERT(head >= tail, ("invariants")); 1241e974f91cSConrad Meyer 1242bf8553eaSConrad Meyer /* Head didn't wrap; we only need to link in oldsize..newsize */ 1243bf8553eaSConrad Meyer if (head < oldsize) { 1244bf8553eaSConrad Meyer i = oldsize - 1; 1245bf8553eaSConrad Meyer end = newsize; 1246e974f91cSConrad Meyer } else { 1247bf8553eaSConrad Meyer /* Head did wrap; link newhead..newsize and 0..oldhead */ 1248bf8553eaSConrad Meyer i = head; 1249bf8553eaSConrad Meyer end = newsize + (head - oldsize) + 1; 1250bf8553eaSConrad Meyer } 1251bf8553eaSConrad Meyer 1252e974f91cSConrad Meyer /* 1253bf8553eaSConrad Meyer * Fix up hardware ring, being careful not to trample the active 1254bf8553eaSConrad Meyer * section (tail -> head). 1255e974f91cSConrad Meyer */ 1256bf8553eaSConrad Meyer for (; i < end; i++) { 1257bf8553eaSConrad Meyer KASSERT((i & (newsize - 1)) < tail || 1258bf8553eaSConrad Meyer (i & (newsize - 1)) >= head, ("trampling snake")); 1259e974f91cSConrad Meyer 1260bf8553eaSConrad Meyer next = newring[(i + 1) & (newsize - 1)]; 1261bf8553eaSConrad Meyer hw = newring[i & (newsize - 1)]->u.dma; 1262e974f91cSConrad Meyer hw->next = next->hw_desc_bus_addr; 1263e974f91cSConrad Meyer } 1264e974f91cSConrad Meyer 1265e974f91cSConrad Meyer free(ioat->ring, M_IOAT); 1266bf8553eaSConrad Meyer ioat->ring = newring; 1267bf8553eaSConrad Meyer ioat->ring_size_order = oldorder + 1; 1268bf8553eaSConrad Meyer ioat->tail = tail; 1269bf8553eaSConrad Meyer ioat->head = head; 1270bf8553eaSConrad Meyer error = 0; 1271e974f91cSConrad Meyer 1272bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1273bf8553eaSConrad Meyer out: 1274bf8553eaSConrad Meyer if (error) 1275bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder + 1)), newring); 1276bf8553eaSConrad Meyer return (error); 1277bf8553eaSConrad Meyer } 1278bf8553eaSConrad Meyer 1279bf8553eaSConrad Meyer static int 1280bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder, 1281bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1282bf8553eaSConrad Meyer { 1283bf8553eaSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1284bf8553eaSConrad Meyer struct ioat_descriptor *ent, *next; 1285bf8553eaSConrad Meyer uint32_t oldsize, newsize, current_idx, new_idx, i; 1286bf8553eaSConrad Meyer int error; 1287bf8553eaSConrad Meyer 1288bf8553eaSConrad Meyer CTR0(KTR_IOAT, __func__); 1289bf8553eaSConrad Meyer 1290bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1291bf8553eaSConrad Meyer 1292bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) { 1293bf8553eaSConrad Meyer error = EINVAL; 1294bf8553eaSConrad Meyer goto out_unlocked; 1295bf8553eaSConrad Meyer } 1296bf8553eaSConrad Meyer 1297bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1298bf8553eaSConrad Meyer newsize = (1 << (oldorder - 1)); 1299bf8553eaSConrad Meyer 1300bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1301bf8553eaSConrad Meyer 1302bf8553eaSConrad Meyer /* Can't shrink below current active set! */ 1303bf8553eaSConrad Meyer if (ioat_get_active(ioat) >= newsize) { 1304bf8553eaSConrad Meyer error = ENOMEM; 1305bf8553eaSConrad Meyer goto out; 1306bf8553eaSConrad Meyer } 1307bf8553eaSConrad Meyer 1308bf8553eaSConrad Meyer /* 1309bf8553eaSConrad Meyer * Copy current descriptors to the new ring, dropping the removed 1310bf8553eaSConrad Meyer * descriptors. 1311bf8553eaSConrad Meyer */ 1312bf8553eaSConrad Meyer for (i = 0; i < newsize; i++) { 1313bf8553eaSConrad Meyer current_idx = (ioat->tail + i) & (oldsize - 1); 1314bf8553eaSConrad Meyer new_idx = (ioat->tail + i) & (newsize - 1); 1315bf8553eaSConrad Meyer 1316bf8553eaSConrad Meyer newring[new_idx] = ioat->ring[current_idx]; 1317bf8553eaSConrad Meyer newring[new_idx]->id = new_idx; 1318bf8553eaSConrad Meyer } 1319bf8553eaSConrad Meyer 1320bf8553eaSConrad Meyer /* Free deleted descriptors */ 1321bf8553eaSConrad Meyer for (i = newsize; i < oldsize; i++) { 1322bf8553eaSConrad Meyer ent = ioat_get_ring_entry(ioat, ioat->tail + i); 1323bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ent); 1324bf8553eaSConrad Meyer } 1325bf8553eaSConrad Meyer 1326bf8553eaSConrad Meyer /* Fix up hardware ring. */ 1327bf8553eaSConrad Meyer hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma; 1328bf8553eaSConrad Meyer next = newring[(ioat->tail + newsize) & (newsize - 1)]; 1329bf8553eaSConrad Meyer hw->next = next->hw_desc_bus_addr; 1330bf8553eaSConrad Meyer 1331bf8553eaSConrad Meyer free(ioat->ring, M_IOAT); 1332bf8553eaSConrad Meyer ioat->ring = newring; 1333bf8553eaSConrad Meyer ioat->ring_size_order = oldorder - 1; 1334bf8553eaSConrad Meyer error = 0; 1335bf8553eaSConrad Meyer 1336bf8553eaSConrad Meyer out: 1337bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1338bf8553eaSConrad Meyer out_unlocked: 1339bf8553eaSConrad Meyer if (error) 1340bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder - 1)), newring); 1341bf8553eaSConrad Meyer return (error); 1342e974f91cSConrad Meyer } 1343e974f91cSConrad Meyer 1344e974f91cSConrad Meyer static void 13458f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr) 1346e974f91cSConrad Meyer { 1347e974f91cSConrad Meyer struct ioat_descriptor *desc; 13488f274637SConrad Meyer 134959acd4baSConrad Meyer ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr, 135059acd4baSConrad Meyer IOAT_CHANERR_STR); 13518f274637SConrad Meyer if (chanerr == 0) 13528f274637SConrad Meyer return; 13538f274637SConrad Meyer 1354faefad9cSConrad Meyer mtx_assert(&ioat->cleanup_lock, MA_OWNED); 1355faefad9cSConrad Meyer 13568f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 0); 13578f274637SConrad Meyer dump_descriptor(desc->u.raw); 13588f274637SConrad Meyer 13598f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 1); 13608f274637SConrad Meyer dump_descriptor(desc->u.raw); 13618f274637SConrad Meyer } 13628f274637SConrad Meyer 13638f274637SConrad Meyer static void 13648f274637SConrad Meyer ioat_timer_callback(void *arg) 13658f274637SConrad Meyer { 1366bf8553eaSConrad Meyer struct ioat_descriptor **newring; 1367e974f91cSConrad Meyer struct ioat_softc *ioat; 1368faefad9cSConrad Meyer uint32_t order; 1369e974f91cSConrad Meyer 1370e974f91cSConrad Meyer ioat = arg; 1371fe720f5aSConrad Meyer ioat_log_message(1, "%s\n", __func__); 1372e974f91cSConrad Meyer 1373e974f91cSConrad Meyer if (ioat->is_completion_pending) { 1374e974f91cSConrad Meyer ioat_process_events(ioat); 1375faefad9cSConrad Meyer return; 1376faefad9cSConrad Meyer } 1377faefad9cSConrad Meyer 1378faefad9cSConrad Meyer /* Slowly scale the ring down if idle. */ 1379e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 1380bf8553eaSConrad Meyer order = ioat->ring_size_order; 1381bf8553eaSConrad Meyer if (ioat->is_resize_pending || order == IOAT_MIN_ORDER) { 1382bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1383bf8553eaSConrad Meyer goto out; 1384bf8553eaSConrad Meyer } 1385bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1386e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 1387e974f91cSConrad Meyer 1388bf8553eaSConrad Meyer newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE, 1389bf8553eaSConrad Meyer M_NOWAIT); 1390bf8553eaSConrad Meyer 1391bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1392bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1393bf8553eaSConrad Meyer ("resize_pending protects order")); 1394bf8553eaSConrad Meyer 1395bf8553eaSConrad Meyer if (newring != NULL) 1396bf8553eaSConrad Meyer ring_shrink(ioat, order, newring); 1397bf8553eaSConrad Meyer 1398bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1399bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1400bf8553eaSConrad Meyer 1401bf8553eaSConrad Meyer out: 1402e974f91cSConrad Meyer if (ioat->ring_size_order > IOAT_MIN_ORDER) 1403bf8553eaSConrad Meyer callout_reset(&ioat->timer, 10 * hz, 1404e974f91cSConrad Meyer ioat_timer_callback, ioat); 1405e974f91cSConrad Meyer } 1406e974f91cSConrad Meyer 1407e974f91cSConrad Meyer /* 1408e974f91cSConrad Meyer * Support Functions 1409e974f91cSConrad Meyer */ 1410e974f91cSConrad Meyer static void 1411e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat) 1412e974f91cSConrad Meyer { 1413e974f91cSConrad Meyer 1414466b3540SConrad Meyer ioat_get(ioat, IOAT_ACTIVE_DESCR_REF); 1415e974f91cSConrad Meyer atomic_add_rel_int(&ioat->head, 1); 1416bf8553eaSConrad Meyer atomic_add_rel_int(&ioat->hw_head, 1); 1417e974f91cSConrad Meyer 1418e974f91cSConrad Meyer if (!ioat->is_completion_pending) { 1419e974f91cSConrad Meyer ioat->is_completion_pending = TRUE; 1420fe720f5aSConrad Meyer callout_reset(&ioat->timer, IOAT_INTR_TIMO, 1421fe720f5aSConrad Meyer ioat_timer_callback, ioat); 1422e974f91cSConrad Meyer } 142301fbbc88SConrad Meyer 142401fbbc88SConrad Meyer ioat->stats.descriptors_submitted++; 1425e974f91cSConrad Meyer } 1426e974f91cSConrad Meyer 1427e974f91cSConrad Meyer static int 1428e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat) 1429e974f91cSConrad Meyer { 1430e974f91cSConrad Meyer uint64_t status; 1431e974f91cSConrad Meyer uint32_t chanerr; 1432cea5b880SConrad Meyer unsigned timeout; 14335f77bd3eSConrad Meyer int error; 14345f77bd3eSConrad Meyer 14355f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 14365f77bd3eSConrad Meyer ioat->quiescing = TRUE; 14375f77bd3eSConrad Meyer ioat_drain_locked(ioat); 14385f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 1439e974f91cSConrad Meyer 1440e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1441e974f91cSConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) 1442e974f91cSConrad Meyer ioat_suspend(ioat); 1443e974f91cSConrad Meyer 1444e974f91cSConrad Meyer /* Wait at most 20 ms */ 1445e974f91cSConrad Meyer for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) && 1446e974f91cSConrad Meyer timeout < 20; timeout++) { 1447e974f91cSConrad Meyer DELAY(1000); 1448e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1449e974f91cSConrad Meyer } 14505f77bd3eSConrad Meyer if (timeout == 20) { 14515f77bd3eSConrad Meyer error = ETIMEDOUT; 14525f77bd3eSConrad Meyer goto out; 14535f77bd3eSConrad Meyer } 1454e974f91cSConrad Meyer 1455cea5b880SConrad Meyer KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce")); 1456cea5b880SConrad Meyer 1457e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 1458e974f91cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 1459e974f91cSConrad Meyer 1460e974f91cSConrad Meyer /* 1461e974f91cSConrad Meyer * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors 1462e974f91cSConrad Meyer * that can cause stability issues for IOAT v3. 1463e974f91cSConrad Meyer */ 1464e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07, 1465e974f91cSConrad Meyer 4); 1466e974f91cSConrad Meyer chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4); 1467e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4); 1468e974f91cSConrad Meyer 14690d1a05d9SConrad Meyer /* 14700d1a05d9SConrad Meyer * BDXDE and BWD models reset MSI-X registers on device reset. 14710d1a05d9SConrad Meyer * Save/restore their contents manually. 14720d1a05d9SConrad Meyer */ 1473f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1474f7157235SConrad Meyer ioat_log_message(1, "device resets MSI-X registers; saving\n"); 14750d1a05d9SConrad Meyer pci_save_state(ioat->device); 1476f7157235SConrad Meyer } 14770d1a05d9SConrad Meyer 1478e974f91cSConrad Meyer ioat_reset(ioat); 1479e974f91cSConrad Meyer 1480e974f91cSConrad Meyer /* Wait at most 20 ms */ 1481e974f91cSConrad Meyer for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++) 1482e974f91cSConrad Meyer DELAY(1000); 14835f77bd3eSConrad Meyer if (timeout == 20) { 14845f77bd3eSConrad Meyer error = ETIMEDOUT; 14855f77bd3eSConrad Meyer goto out; 14865f77bd3eSConrad Meyer } 1487e974f91cSConrad Meyer 1488f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1489f7157235SConrad Meyer ioat_log_message(1, "device resets registers; restored\n"); 14900d1a05d9SConrad Meyer pci_restore_state(ioat->device); 1491f7157235SConrad Meyer } 14924253ea50SConrad Meyer 1493cea5b880SConrad Meyer /* Reset attempts to return the hardware to "halted." */ 1494cea5b880SConrad Meyer status = ioat_get_chansts(ioat); 1495cea5b880SConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) { 1496cea5b880SConrad Meyer /* So this really shouldn't happen... */ 1497cea5b880SConrad Meyer ioat_log_message(0, "Device is active after a reset?\n"); 1498cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 14995f77bd3eSConrad Meyer error = 0; 15005f77bd3eSConrad Meyer goto out; 1501e974f91cSConrad Meyer } 1502e974f91cSConrad Meyer 1503cea5b880SConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 15045f77bd3eSConrad Meyer if (chanerr != 0) { 1505faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1506faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 1507faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 15085f77bd3eSConrad Meyer error = EIO; 15095f77bd3eSConrad Meyer goto out; 15105f77bd3eSConrad Meyer } 1511cea5b880SConrad Meyer 1512cea5b880SConrad Meyer /* 1513cea5b880SConrad Meyer * Bring device back online after reset. Writing CHAINADDR brings the 1514cea5b880SConrad Meyer * device back to active. 1515cea5b880SConrad Meyer * 1516cea5b880SConrad Meyer * The internal ring counter resets to zero, so we have to start over 1517cea5b880SConrad Meyer * at zero as well. 1518cea5b880SConrad Meyer */ 1519bf8553eaSConrad Meyer ioat->tail = ioat->head = ioat->hw_head = 0; 1520cea5b880SConrad Meyer ioat->last_seen = 0; 1521cea5b880SConrad Meyer 1522cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 1523cea5b880SConrad Meyer ioat_write_chancmp(ioat, ioat->comp_update_bus_addr); 1524cea5b880SConrad Meyer ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr); 15255f77bd3eSConrad Meyer error = 0; 15265f77bd3eSConrad Meyer 15275f77bd3eSConrad Meyer out: 15285f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 15295f77bd3eSConrad Meyer ioat->quiescing = FALSE; 15305f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 15315f77bd3eSConrad Meyer 15325f77bd3eSConrad Meyer if (error == 0) 15335f77bd3eSConrad Meyer error = ioat_start_channel(ioat); 15345f77bd3eSConrad Meyer 15355f77bd3eSConrad Meyer return (error); 1536cea5b880SConrad Meyer } 1537cea5b880SConrad Meyer 1538f7157235SConrad Meyer static int 1539faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS) 1540faefad9cSConrad Meyer { 1541faefad9cSConrad Meyer struct ioat_softc *ioat; 1542faefad9cSConrad Meyer struct sbuf sb; 1543faefad9cSConrad Meyer uint64_t status; 1544faefad9cSConrad Meyer int error; 1545faefad9cSConrad Meyer 1546faefad9cSConrad Meyer ioat = arg1; 1547faefad9cSConrad Meyer 1548faefad9cSConrad Meyer status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS; 1549faefad9cSConrad Meyer 1550faefad9cSConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 256, req); 1551faefad9cSConrad Meyer switch (status) { 1552faefad9cSConrad Meyer case IOAT_CHANSTS_ACTIVE: 1553faefad9cSConrad Meyer sbuf_printf(&sb, "ACTIVE"); 1554faefad9cSConrad Meyer break; 1555faefad9cSConrad Meyer case IOAT_CHANSTS_IDLE: 1556faefad9cSConrad Meyer sbuf_printf(&sb, "IDLE"); 1557faefad9cSConrad Meyer break; 1558faefad9cSConrad Meyer case IOAT_CHANSTS_SUSPENDED: 1559faefad9cSConrad Meyer sbuf_printf(&sb, "SUSPENDED"); 1560faefad9cSConrad Meyer break; 1561faefad9cSConrad Meyer case IOAT_CHANSTS_HALTED: 1562faefad9cSConrad Meyer sbuf_printf(&sb, "HALTED"); 1563faefad9cSConrad Meyer break; 1564faefad9cSConrad Meyer case IOAT_CHANSTS_ARMED: 1565faefad9cSConrad Meyer sbuf_printf(&sb, "ARMED"); 1566faefad9cSConrad Meyer break; 1567faefad9cSConrad Meyer default: 1568faefad9cSConrad Meyer sbuf_printf(&sb, "UNKNOWN"); 1569faefad9cSConrad Meyer break; 1570faefad9cSConrad Meyer } 1571faefad9cSConrad Meyer error = sbuf_finish(&sb); 1572faefad9cSConrad Meyer sbuf_delete(&sb); 1573faefad9cSConrad Meyer 1574faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 1575faefad9cSConrad Meyer return (error); 1576faefad9cSConrad Meyer return (EINVAL); 1577faefad9cSConrad Meyer } 1578faefad9cSConrad Meyer 1579faefad9cSConrad Meyer static int 158001fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS) 158101fbbc88SConrad Meyer { 158201fbbc88SConrad Meyer struct ioat_softc *ioat; 158301fbbc88SConrad Meyer struct sbuf sb; 158401fbbc88SConrad Meyer #define PRECISION "1" 158501fbbc88SConrad Meyer const uintmax_t factor = 10; 158601fbbc88SConrad Meyer uintmax_t rate; 158701fbbc88SConrad Meyer int error; 158801fbbc88SConrad Meyer 158901fbbc88SConrad Meyer ioat = arg1; 159001fbbc88SConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 16, req); 159101fbbc88SConrad Meyer 159201fbbc88SConrad Meyer if (ioat->stats.interrupts == 0) { 159301fbbc88SConrad Meyer sbuf_printf(&sb, "NaN"); 159401fbbc88SConrad Meyer goto out; 159501fbbc88SConrad Meyer } 159601fbbc88SConrad Meyer rate = ioat->stats.descriptors_processed * factor / 159701fbbc88SConrad Meyer ioat->stats.interrupts; 159801fbbc88SConrad Meyer sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor, 159901fbbc88SConrad Meyer rate % factor); 160001fbbc88SConrad Meyer #undef PRECISION 160101fbbc88SConrad Meyer out: 160201fbbc88SConrad Meyer error = sbuf_finish(&sb); 160301fbbc88SConrad Meyer sbuf_delete(&sb); 160401fbbc88SConrad Meyer if (error != 0 || req->newptr == NULL) 160501fbbc88SConrad Meyer return (error); 160601fbbc88SConrad Meyer return (EINVAL); 160701fbbc88SConrad Meyer } 160801fbbc88SConrad Meyer 160901fbbc88SConrad Meyer static int 1610faefad9cSConrad Meyer sysctl_handle_error(SYSCTL_HANDLER_ARGS) 1611faefad9cSConrad Meyer { 1612faefad9cSConrad Meyer struct ioat_descriptor *desc; 1613faefad9cSConrad Meyer struct ioat_softc *ioat; 1614faefad9cSConrad Meyer int error, arg; 1615faefad9cSConrad Meyer 1616faefad9cSConrad Meyer ioat = arg1; 1617faefad9cSConrad Meyer 1618faefad9cSConrad Meyer arg = 0; 1619faefad9cSConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 1620faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 1621faefad9cSConrad Meyer return (error); 1622faefad9cSConrad Meyer 1623faefad9cSConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 1624faefad9cSConrad Meyer if (error != 0) 1625faefad9cSConrad Meyer return (error); 1626faefad9cSConrad Meyer 1627faefad9cSConrad Meyer if (arg != 0) { 1628faefad9cSConrad Meyer ioat_acquire(&ioat->dmaengine); 1629faefad9cSConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 1, 1630faefad9cSConrad Meyer 0xffff000000000000ull, 0xffff000000000000ull, NULL, NULL, 1631faefad9cSConrad Meyer 0); 1632faefad9cSConrad Meyer if (desc == NULL) 1633faefad9cSConrad Meyer error = ENOMEM; 1634faefad9cSConrad Meyer else 1635faefad9cSConrad Meyer ioat_submit_single(ioat); 1636faefad9cSConrad Meyer ioat_release(&ioat->dmaengine); 1637faefad9cSConrad Meyer } 1638faefad9cSConrad Meyer return (error); 1639faefad9cSConrad Meyer } 1640faefad9cSConrad Meyer 1641faefad9cSConrad Meyer static int 1642f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS) 1643f7157235SConrad Meyer { 1644f7157235SConrad Meyer struct ioat_softc *ioat; 1645f7157235SConrad Meyer int error, arg; 1646f7157235SConrad Meyer 1647f7157235SConrad Meyer ioat = arg1; 1648f7157235SConrad Meyer 1649f7157235SConrad Meyer arg = 0; 1650f7157235SConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 1651f7157235SConrad Meyer if (error != 0 || req->newptr == NULL) 1652f7157235SConrad Meyer return (error); 1653f7157235SConrad Meyer 1654f7157235SConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 1655f7157235SConrad Meyer if (error != 0) 1656f7157235SConrad Meyer return (error); 1657f7157235SConrad Meyer 1658f7157235SConrad Meyer if (arg != 0) 1659f7157235SConrad Meyer error = ioat_reset_hw(ioat); 1660f7157235SConrad Meyer 1661f7157235SConrad Meyer return (error); 1662f7157235SConrad Meyer } 1663f7157235SConrad Meyer 1664e974f91cSConrad Meyer static void 1665e974f91cSConrad Meyer dump_descriptor(void *hw_desc) 1666e974f91cSConrad Meyer { 1667e974f91cSConrad Meyer int i, j; 1668e974f91cSConrad Meyer 1669e974f91cSConrad Meyer for (i = 0; i < 2; i++) { 1670e974f91cSConrad Meyer for (j = 0; j < 8; j++) 1671e974f91cSConrad Meyer printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]); 1672e974f91cSConrad Meyer printf("\n"); 1673e974f91cSConrad Meyer } 1674e974f91cSConrad Meyer } 1675e974f91cSConrad Meyer 1676e974f91cSConrad Meyer static void 1677e974f91cSConrad Meyer ioat_setup_sysctl(device_t device) 1678e974f91cSConrad Meyer { 167901fbbc88SConrad Meyer struct sysctl_oid_list *par, *statpar, *state, *hammer; 1680f7157235SConrad Meyer struct sysctl_ctx_list *ctx; 168101fbbc88SConrad Meyer struct sysctl_oid *tree, *tmp; 1682e974f91cSConrad Meyer struct ioat_softc *ioat; 1683e974f91cSConrad Meyer 1684e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 1685f7157235SConrad Meyer ctx = device_get_sysctl_ctx(device); 1686f7157235SConrad Meyer tree = device_get_sysctl_tree(device); 1687f7157235SConrad Meyer par = SYSCTL_CHILDREN(tree); 1688e974f91cSConrad Meyer 168965e4f8adSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD, 169065e4f8adSConrad Meyer &ioat->version, 0, "HW version (0xMM form)"); 169165e4f8adSConrad Meyer SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD, 169265e4f8adSConrad Meyer &ioat->max_xfer_size, 0, "HW maximum transfer size"); 16935ca9fc2aSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD, 16945ca9fc2aSConrad Meyer &ioat->intrdelay_supported, 0, "Is INTRDELAY supported"); 16955ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD, 16965ca9fc2aSConrad Meyer &ioat->intrdelay_max, 0, 16975ca9fc2aSConrad Meyer "Maximum configurable INTRDELAY on this channel (microseconds)"); 169865e4f8adSConrad Meyer 169901fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL, 170001fbbc88SConrad Meyer "IOAT channel internal state"); 170101fbbc88SConrad Meyer state = SYSCTL_CHILDREN(tmp); 170201fbbc88SConrad Meyer 170301fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD, 1704bf8553eaSConrad Meyer &ioat->ring_size_order, 0, "SW descriptor ring size order"); 170501fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 170601fbbc88SConrad Meyer 0, "SW descriptor head pointer index"); 170701fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 170801fbbc88SConrad Meyer 0, "SW descriptor tail pointer index"); 170901fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD, 1710bf8553eaSConrad Meyer &ioat->hw_head, 0, "HW DMACOUNT"); 1711f7157235SConrad Meyer 171201fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD, 171365e4f8adSConrad Meyer ioat->comp_update, "HW addr of last completion"); 171465e4f8adSConrad Meyer 171501fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD, 171665e4f8adSConrad Meyer &ioat->is_resize_pending, 0, "resize pending"); 171701fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending", 171801fbbc88SConrad Meyer CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending"); 171901fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD, 172065e4f8adSConrad Meyer &ioat->is_reset_pending, 0, "reset pending"); 172101fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD, 172265e4f8adSConrad Meyer &ioat->is_channel_running, 0, "channel running"); 172365e4f8adSConrad Meyer 172401fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts", 1725faefad9cSConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A", 1726faefad9cSConrad Meyer "String of the channel status"); 172701fbbc88SConrad Meyer 17285ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD, 17295ca9fc2aSConrad Meyer &ioat->cached_intrdelay, 0, 17305ca9fc2aSConrad Meyer "Current INTRDELAY on this channel (cached, microseconds)"); 17315ca9fc2aSConrad Meyer 173201fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL, 173301fbbc88SConrad Meyer "Big hammers (mostly for testing)"); 173401fbbc88SConrad Meyer hammer = SYSCTL_CHILDREN(tmp); 173501fbbc88SConrad Meyer 173601fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset", 173701fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I", 173801fbbc88SConrad Meyer "Set to non-zero to reset the hardware"); 173901fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_error", 174001fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_error, "I", 174101fbbc88SConrad Meyer "Set to non-zero to inject a recoverable hardware error"); 174201fbbc88SConrad Meyer 174301fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL, 174401fbbc88SConrad Meyer "IOAT channel statistics"); 174501fbbc88SConrad Meyer statpar = SYSCTL_CHILDREN(tmp); 174601fbbc88SConrad Meyer 174701fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW, 174801fbbc88SConrad Meyer &ioat->stats.interrupts, 174901fbbc88SConrad Meyer "Number of interrupts processed on this channel"); 175001fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW, 175101fbbc88SConrad Meyer &ioat->stats.descriptors_processed, 175201fbbc88SConrad Meyer "Number of descriptors processed on this channel"); 175301fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW, 175401fbbc88SConrad Meyer &ioat->stats.descriptors_submitted, 175501fbbc88SConrad Meyer "Number of descriptors submitted to this channel"); 175601fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW, 175701fbbc88SConrad Meyer &ioat->stats.descriptors_error, 175801fbbc88SConrad Meyer "Number of descriptors failed by channel errors"); 175901fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW, 176001fbbc88SConrad Meyer &ioat->stats.channel_halts, 0, 176101fbbc88SConrad Meyer "Number of times the channel has halted"); 176201fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW, 176301fbbc88SConrad Meyer &ioat->stats.last_halt_chanerr, 0, 176401fbbc88SConrad Meyer "The raw CHANERR when the channel was last halted"); 176501fbbc88SConrad Meyer 176601fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt", 176701fbbc88SConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A", 176801fbbc88SConrad Meyer "Descriptors per interrupt"); 1769e974f91cSConrad Meyer } 1770466b3540SConrad Meyer 1771466b3540SConrad Meyer static inline struct ioat_softc * 1772466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind) 1773466b3540SConrad Meyer { 1774466b3540SConrad Meyer uint32_t old; 1775466b3540SConrad Meyer 1776466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 1777466b3540SConrad Meyer 1778466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, 1); 1779466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt overflow")); 1780466b3540SConrad Meyer 1781466b3540SConrad Meyer #ifdef INVARIANTS 1782466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], 1); 1783466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt kind overflow")); 1784466b3540SConrad Meyer #endif 1785466b3540SConrad Meyer 1786466b3540SConrad Meyer return (ioat); 1787466b3540SConrad Meyer } 1788466b3540SConrad Meyer 1789466b3540SConrad Meyer static inline void 1790466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 1791466b3540SConrad Meyer { 1792faefad9cSConrad Meyer 1793faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, FALSE); 1794faefad9cSConrad Meyer } 1795faefad9cSConrad Meyer 1796faefad9cSConrad Meyer static inline void 1797faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 1798faefad9cSConrad Meyer { 1799faefad9cSConrad Meyer 1800faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, TRUE); 1801faefad9cSConrad Meyer } 1802faefad9cSConrad Meyer 1803faefad9cSConrad Meyer static inline void 1804faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind, 1805faefad9cSConrad Meyer boolean_t locked) 1806faefad9cSConrad Meyer { 1807466b3540SConrad Meyer uint32_t old; 1808466b3540SConrad Meyer 1809466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 1810466b3540SConrad Meyer 1811466b3540SConrad Meyer if (n == 0) 1812466b3540SConrad Meyer return; 1813466b3540SConrad Meyer 1814466b3540SConrad Meyer #ifdef INVARIANTS 1815466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], -n); 1816466b3540SConrad Meyer KASSERT(old >= n, ("refcnt kind underflow")); 1817466b3540SConrad Meyer #endif 1818466b3540SConrad Meyer 1819466b3540SConrad Meyer /* Skip acquiring the lock if resulting refcnt > 0. */ 1820466b3540SConrad Meyer for (;;) { 1821466b3540SConrad Meyer old = ioat->refcnt; 1822466b3540SConrad Meyer if (old <= n) 1823466b3540SConrad Meyer break; 1824466b3540SConrad Meyer if (atomic_cmpset_32(&ioat->refcnt, old, old - n)) 1825466b3540SConrad Meyer return; 1826466b3540SConrad Meyer } 1827466b3540SConrad Meyer 1828faefad9cSConrad Meyer if (locked) 1829faefad9cSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 1830faefad9cSConrad Meyer else 1831466b3540SConrad Meyer mtx_lock(IOAT_REFLK); 1832faefad9cSConrad Meyer 1833466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, -n); 1834466b3540SConrad Meyer KASSERT(old >= n, ("refcnt error")); 1835466b3540SConrad Meyer 1836466b3540SConrad Meyer if (old == n) 1837466b3540SConrad Meyer wakeup(IOAT_REFLK); 1838faefad9cSConrad Meyer if (!locked) 1839466b3540SConrad Meyer mtx_unlock(IOAT_REFLK); 1840466b3540SConrad Meyer } 1841466b3540SConrad Meyer 1842466b3540SConrad Meyer static inline void 1843466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind) 1844466b3540SConrad Meyer { 1845466b3540SConrad Meyer 1846466b3540SConrad Meyer ioat_putn(ioat, 1, kind); 1847466b3540SConrad Meyer } 1848466b3540SConrad Meyer 1849466b3540SConrad Meyer static void 18505f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat) 1851466b3540SConrad Meyer { 1852466b3540SConrad Meyer 18535f77bd3eSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 1854466b3540SConrad Meyer while (ioat->refcnt > 0) 1855466b3540SConrad Meyer msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0); 1856466b3540SConrad Meyer } 1857