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 300bb35debSConrad Meyer #include "opt_ddb.h" 310bb35debSConrad Meyer 32e974f91cSConrad Meyer #include <sys/param.h> 33e974f91cSConrad Meyer #include <sys/systm.h> 34e974f91cSConrad Meyer #include <sys/bus.h> 35e974f91cSConrad Meyer #include <sys/conf.h> 36*76305bb8SConrad Meyer #include <sys/fail.h> 37e974f91cSConrad Meyer #include <sys/ioccom.h> 38e974f91cSConrad Meyer #include <sys/kernel.h> 39e974f91cSConrad Meyer #include <sys/lock.h> 40e974f91cSConrad Meyer #include <sys/malloc.h> 41e974f91cSConrad Meyer #include <sys/module.h> 42e974f91cSConrad Meyer #include <sys/mutex.h> 43e974f91cSConrad Meyer #include <sys/rman.h> 44faefad9cSConrad Meyer #include <sys/sbuf.h> 45e974f91cSConrad Meyer #include <sys/sysctl.h> 46374b05e1SConrad Meyer #include <sys/taskqueue.h> 47e974f91cSConrad Meyer #include <sys/time.h> 48e974f91cSConrad Meyer #include <dev/pci/pcireg.h> 49e974f91cSConrad Meyer #include <dev/pci/pcivar.h> 50e974f91cSConrad Meyer #include <machine/bus.h> 51e974f91cSConrad Meyer #include <machine/resource.h> 52e974f91cSConrad Meyer #include <machine/stdarg.h> 53e974f91cSConrad Meyer 540bb35debSConrad Meyer #ifdef DDB 550bb35debSConrad Meyer #include <ddb/ddb.h> 560bb35debSConrad Meyer #endif 570bb35debSConrad Meyer 58e974f91cSConrad Meyer #include "ioat.h" 59e974f91cSConrad Meyer #include "ioat_hw.h" 60e974f91cSConrad Meyer #include "ioat_internal.h" 61e974f91cSConrad Meyer 62519e8baaSConrad Meyer #ifndef BUS_SPACE_MAXADDR_40BIT 63519e8baaSConrad Meyer #define BUS_SPACE_MAXADDR_40BIT 0xFFFFFFFFFFULL 64519e8baaSConrad Meyer #endif 65466b3540SConrad Meyer #define IOAT_REFLK (&ioat->submit_lock) 665ac77963SConrad Meyer #define IOAT_SHRINK_PERIOD (10 * hz) 67fe720f5aSConrad Meyer 68e974f91cSConrad Meyer static int ioat_probe(device_t device); 69e974f91cSConrad Meyer static int ioat_attach(device_t device); 70e974f91cSConrad Meyer static int ioat_detach(device_t device); 714253ea50SConrad Meyer static int ioat_setup_intr(struct ioat_softc *ioat); 724253ea50SConrad Meyer static int ioat_teardown_intr(struct ioat_softc *ioat); 73e974f91cSConrad Meyer static int ioat3_attach(device_t device); 74cea5b880SConrad Meyer static int ioat_start_channel(struct ioat_softc *ioat); 75e974f91cSConrad Meyer static int ioat_map_pci_bar(struct ioat_softc *ioat); 76e974f91cSConrad Meyer static void ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, 77e974f91cSConrad Meyer int error); 78e974f91cSConrad Meyer static void ioat_interrupt_handler(void *arg); 790d1a05d9SConrad Meyer static boolean_t ioat_model_resets_msix(struct ioat_softc *ioat); 80faefad9cSConrad Meyer static int chanerr_to_errno(uint32_t); 81e974f91cSConrad Meyer static void ioat_process_events(struct ioat_softc *ioat); 82e974f91cSConrad Meyer static inline uint32_t ioat_get_active(struct ioat_softc *ioat); 83e974f91cSConrad Meyer static inline uint32_t ioat_get_ring_space(struct ioat_softc *ioat); 84bf8553eaSConrad Meyer static void ioat_free_ring(struct ioat_softc *, uint32_t size, 85bf8553eaSConrad Meyer struct ioat_descriptor **); 86e974f91cSConrad Meyer static void ioat_free_ring_entry(struct ioat_softc *ioat, 87e974f91cSConrad Meyer struct ioat_descriptor *desc); 88bf8553eaSConrad Meyer static struct ioat_descriptor *ioat_alloc_ring_entry(struct ioat_softc *, 89bf8553eaSConrad Meyer int mflags); 90bf8553eaSConrad Meyer static int ioat_reserve_space(struct ioat_softc *, uint32_t, int mflags); 91e974f91cSConrad Meyer static struct ioat_descriptor *ioat_get_ring_entry(struct ioat_softc *ioat, 92e974f91cSConrad Meyer uint32_t index); 93bf8553eaSConrad Meyer static struct ioat_descriptor **ioat_prealloc_ring(struct ioat_softc *, 94bf8553eaSConrad Meyer uint32_t size, boolean_t need_dscr, int mflags); 95bf8553eaSConrad Meyer static int ring_grow(struct ioat_softc *, uint32_t oldorder, 96bf8553eaSConrad Meyer struct ioat_descriptor **); 97bf8553eaSConrad Meyer static int ring_shrink(struct ioat_softc *, uint32_t oldorder, 98bf8553eaSConrad Meyer struct ioat_descriptor **); 99faefad9cSConrad Meyer static void ioat_halted_debug(struct ioat_softc *, uint32_t); 1005ac77963SConrad Meyer static void ioat_poll_timer_callback(void *arg); 1015ac77963SConrad Meyer static void ioat_shrink_timer_callback(void *arg); 102e974f91cSConrad Meyer static void dump_descriptor(void *hw_desc); 103e974f91cSConrad Meyer static void ioat_submit_single(struct ioat_softc *ioat); 104e974f91cSConrad Meyer static void ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, 105e974f91cSConrad Meyer int error); 106e974f91cSConrad Meyer static int ioat_reset_hw(struct ioat_softc *ioat); 107374b05e1SConrad Meyer static void ioat_reset_hw_task(void *, int); 108e974f91cSConrad Meyer static void ioat_setup_sysctl(device_t device); 109f7157235SConrad Meyer static int sysctl_handle_reset(SYSCTL_HANDLER_ARGS); 110466b3540SConrad Meyer static inline struct ioat_softc *ioat_get(struct ioat_softc *, 111466b3540SConrad Meyer enum ioat_ref_kind); 112466b3540SConrad Meyer static inline void ioat_put(struct ioat_softc *, enum ioat_ref_kind); 113faefad9cSConrad Meyer static inline void _ioat_putn(struct ioat_softc *, uint32_t, 114faefad9cSConrad Meyer enum ioat_ref_kind, boolean_t); 115466b3540SConrad Meyer static inline void ioat_putn(struct ioat_softc *, uint32_t, 116466b3540SConrad Meyer enum ioat_ref_kind); 117faefad9cSConrad Meyer static inline void ioat_putn_locked(struct ioat_softc *, uint32_t, 118faefad9cSConrad Meyer enum ioat_ref_kind); 1195f77bd3eSConrad Meyer static void ioat_drain_locked(struct ioat_softc *); 120e974f91cSConrad Meyer 1211c25420eSConrad Meyer #define ioat_log_message(v, ...) do { \ 1221c25420eSConrad Meyer if ((v) <= g_ioat_debug_level) { \ 1231c25420eSConrad Meyer device_printf(ioat->device, __VA_ARGS__); \ 1241c25420eSConrad Meyer } \ 1251c25420eSConrad Meyer } while (0) 1261c25420eSConrad Meyer 127e974f91cSConrad Meyer MALLOC_DEFINE(M_IOAT, "ioat", "ioat driver memory allocations"); 128e974f91cSConrad Meyer SYSCTL_NODE(_hw, OID_AUTO, ioat, CTLFLAG_RD, 0, "ioat node"); 129e974f91cSConrad Meyer 130e974f91cSConrad Meyer static int g_force_legacy_interrupts; 131e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, force_legacy_interrupts, CTLFLAG_RDTUN, 132e974f91cSConrad Meyer &g_force_legacy_interrupts, 0, "Set to non-zero to force MSI-X disabled"); 133e974f91cSConrad Meyer 1341c25420eSConrad Meyer int g_ioat_debug_level = 0; 135e974f91cSConrad Meyer SYSCTL_INT(_hw_ioat, OID_AUTO, debug_level, CTLFLAG_RWTUN, &g_ioat_debug_level, 136e974f91cSConrad Meyer 0, "Set log level (0-3) for ioat(4). Higher is more verbose."); 137e974f91cSConrad Meyer 138e974f91cSConrad Meyer /* 139e974f91cSConrad Meyer * OS <-> Driver interface structures 140e974f91cSConrad Meyer */ 141e974f91cSConrad Meyer static device_method_t ioat_pci_methods[] = { 142e974f91cSConrad Meyer /* Device interface */ 143e974f91cSConrad Meyer DEVMETHOD(device_probe, ioat_probe), 144e974f91cSConrad Meyer DEVMETHOD(device_attach, ioat_attach), 145e974f91cSConrad Meyer DEVMETHOD(device_detach, ioat_detach), 146aa853cb4SEnji Cooper DEVMETHOD_END 147e974f91cSConrad Meyer }; 148e974f91cSConrad Meyer 149e974f91cSConrad Meyer static driver_t ioat_pci_driver = { 150e974f91cSConrad Meyer "ioat", 151e974f91cSConrad Meyer ioat_pci_methods, 152e974f91cSConrad Meyer sizeof(struct ioat_softc), 153e974f91cSConrad Meyer }; 154e974f91cSConrad Meyer 155e974f91cSConrad Meyer static devclass_t ioat_devclass; 156e974f91cSConrad Meyer DRIVER_MODULE(ioat, pci, ioat_pci_driver, ioat_devclass, 0, 0); 157c2b69205SConrad Meyer MODULE_VERSION(ioat, 1); 158e974f91cSConrad Meyer 159e974f91cSConrad Meyer /* 160e974f91cSConrad Meyer * Private data structures 161e974f91cSConrad Meyer */ 162e974f91cSConrad Meyer static struct ioat_softc *ioat_channel[IOAT_MAX_CHANNELS]; 163df1928aaSConrad Meyer static unsigned ioat_channel_index = 0; 164df1928aaSConrad Meyer SYSCTL_UINT(_hw_ioat, OID_AUTO, channels, CTLFLAG_RD, &ioat_channel_index, 0, 165e974f91cSConrad Meyer "Number of IOAT channels attached"); 166e974f91cSConrad Meyer 167e974f91cSConrad Meyer static struct _pcsid 168e974f91cSConrad Meyer { 169e974f91cSConrad Meyer u_int32_t type; 170e974f91cSConrad Meyer const char *desc; 171e974f91cSConrad Meyer } pci_ids[] = { 172e974f91cSConrad Meyer { 0x34308086, "TBG IOAT Ch0" }, 173e974f91cSConrad Meyer { 0x34318086, "TBG IOAT Ch1" }, 174e974f91cSConrad Meyer { 0x34328086, "TBG IOAT Ch2" }, 175e974f91cSConrad Meyer { 0x34338086, "TBG IOAT Ch3" }, 176e974f91cSConrad Meyer { 0x34298086, "TBG IOAT Ch4" }, 177e974f91cSConrad Meyer { 0x342a8086, "TBG IOAT Ch5" }, 178e974f91cSConrad Meyer { 0x342b8086, "TBG IOAT Ch6" }, 179e974f91cSConrad Meyer { 0x342c8086, "TBG IOAT Ch7" }, 180e974f91cSConrad Meyer 181e974f91cSConrad Meyer { 0x37108086, "JSF IOAT Ch0" }, 182e974f91cSConrad Meyer { 0x37118086, "JSF IOAT Ch1" }, 183e974f91cSConrad Meyer { 0x37128086, "JSF IOAT Ch2" }, 184e974f91cSConrad Meyer { 0x37138086, "JSF IOAT Ch3" }, 185e974f91cSConrad Meyer { 0x37148086, "JSF IOAT Ch4" }, 186e974f91cSConrad Meyer { 0x37158086, "JSF IOAT Ch5" }, 187e974f91cSConrad Meyer { 0x37168086, "JSF IOAT Ch6" }, 188e974f91cSConrad Meyer { 0x37178086, "JSF IOAT Ch7" }, 189e974f91cSConrad Meyer { 0x37188086, "JSF IOAT Ch0 (RAID)" }, 190e974f91cSConrad Meyer { 0x37198086, "JSF IOAT Ch1 (RAID)" }, 191e974f91cSConrad Meyer 192e974f91cSConrad Meyer { 0x3c208086, "SNB IOAT Ch0" }, 193e974f91cSConrad Meyer { 0x3c218086, "SNB IOAT Ch1" }, 194e974f91cSConrad Meyer { 0x3c228086, "SNB IOAT Ch2" }, 195e974f91cSConrad Meyer { 0x3c238086, "SNB IOAT Ch3" }, 196e974f91cSConrad Meyer { 0x3c248086, "SNB IOAT Ch4" }, 197e974f91cSConrad Meyer { 0x3c258086, "SNB IOAT Ch5" }, 198e974f91cSConrad Meyer { 0x3c268086, "SNB IOAT Ch6" }, 199e974f91cSConrad Meyer { 0x3c278086, "SNB IOAT Ch7" }, 200e974f91cSConrad Meyer { 0x3c2e8086, "SNB IOAT Ch0 (RAID)" }, 201e974f91cSConrad Meyer { 0x3c2f8086, "SNB IOAT Ch1 (RAID)" }, 202e974f91cSConrad Meyer 203e974f91cSConrad Meyer { 0x0e208086, "IVB IOAT Ch0" }, 204e974f91cSConrad Meyer { 0x0e218086, "IVB IOAT Ch1" }, 205e974f91cSConrad Meyer { 0x0e228086, "IVB IOAT Ch2" }, 206e974f91cSConrad Meyer { 0x0e238086, "IVB IOAT Ch3" }, 207e974f91cSConrad Meyer { 0x0e248086, "IVB IOAT Ch4" }, 208e974f91cSConrad Meyer { 0x0e258086, "IVB IOAT Ch5" }, 209e974f91cSConrad Meyer { 0x0e268086, "IVB IOAT Ch6" }, 210e974f91cSConrad Meyer { 0x0e278086, "IVB IOAT Ch7" }, 211e974f91cSConrad Meyer { 0x0e2e8086, "IVB IOAT Ch0 (RAID)" }, 212e974f91cSConrad Meyer { 0x0e2f8086, "IVB IOAT Ch1 (RAID)" }, 213e974f91cSConrad Meyer 214e974f91cSConrad Meyer { 0x2f208086, "HSW IOAT Ch0" }, 215e974f91cSConrad Meyer { 0x2f218086, "HSW IOAT Ch1" }, 216e974f91cSConrad Meyer { 0x2f228086, "HSW IOAT Ch2" }, 217e974f91cSConrad Meyer { 0x2f238086, "HSW IOAT Ch3" }, 218e974f91cSConrad Meyer { 0x2f248086, "HSW IOAT Ch4" }, 219e974f91cSConrad Meyer { 0x2f258086, "HSW IOAT Ch5" }, 220e974f91cSConrad Meyer { 0x2f268086, "HSW IOAT Ch6" }, 221e974f91cSConrad Meyer { 0x2f278086, "HSW IOAT Ch7" }, 222e974f91cSConrad Meyer { 0x2f2e8086, "HSW IOAT Ch0 (RAID)" }, 223e974f91cSConrad Meyer { 0x2f2f8086, "HSW IOAT Ch1 (RAID)" }, 224e974f91cSConrad Meyer 225e974f91cSConrad Meyer { 0x0c508086, "BWD IOAT Ch0" }, 226e974f91cSConrad Meyer { 0x0c518086, "BWD IOAT Ch1" }, 227e974f91cSConrad Meyer { 0x0c528086, "BWD IOAT Ch2" }, 228e974f91cSConrad Meyer { 0x0c538086, "BWD IOAT Ch3" }, 229e974f91cSConrad Meyer 230e974f91cSConrad Meyer { 0x6f508086, "BDXDE IOAT Ch0" }, 231e974f91cSConrad Meyer { 0x6f518086, "BDXDE IOAT Ch1" }, 232e974f91cSConrad Meyer { 0x6f528086, "BDXDE IOAT Ch2" }, 233e974f91cSConrad Meyer { 0x6f538086, "BDXDE IOAT Ch3" }, 234e974f91cSConrad Meyer 2355afc2508SConrad Meyer { 0x6f208086, "BDX IOAT Ch0" }, 2365afc2508SConrad Meyer { 0x6f218086, "BDX IOAT Ch1" }, 2375afc2508SConrad Meyer { 0x6f228086, "BDX IOAT Ch2" }, 2385afc2508SConrad Meyer { 0x6f238086, "BDX IOAT Ch3" }, 2395afc2508SConrad Meyer { 0x6f248086, "BDX IOAT Ch4" }, 2405afc2508SConrad Meyer { 0x6f258086, "BDX IOAT Ch5" }, 2415afc2508SConrad Meyer { 0x6f268086, "BDX IOAT Ch6" }, 2425afc2508SConrad Meyer { 0x6f278086, "BDX IOAT Ch7" }, 2435afc2508SConrad Meyer { 0x6f2e8086, "BDX IOAT Ch0 (RAID)" }, 2445afc2508SConrad Meyer { 0x6f2f8086, "BDX IOAT Ch1 (RAID)" }, 2455afc2508SConrad Meyer 246e974f91cSConrad Meyer { 0x00000000, NULL } 247e974f91cSConrad Meyer }; 248e974f91cSConrad Meyer 249e974f91cSConrad Meyer /* 250e974f91cSConrad Meyer * OS <-> Driver linkage functions 251e974f91cSConrad Meyer */ 252e974f91cSConrad Meyer static int 253e974f91cSConrad Meyer ioat_probe(device_t device) 254e974f91cSConrad Meyer { 255e974f91cSConrad Meyer struct _pcsid *ep; 256e974f91cSConrad Meyer u_int32_t type; 257e974f91cSConrad Meyer 258e974f91cSConrad Meyer type = pci_get_devid(device); 259e974f91cSConrad Meyer for (ep = pci_ids; ep->type; ep++) { 260e974f91cSConrad Meyer if (ep->type == type) { 261e974f91cSConrad Meyer device_set_desc(device, ep->desc); 262e974f91cSConrad Meyer return (0); 263e974f91cSConrad Meyer } 264e974f91cSConrad Meyer } 265e974f91cSConrad Meyer return (ENXIO); 266e974f91cSConrad Meyer } 267e974f91cSConrad Meyer 268e974f91cSConrad Meyer static int 269e974f91cSConrad Meyer ioat_attach(device_t device) 270e974f91cSConrad Meyer { 271e974f91cSConrad Meyer struct ioat_softc *ioat; 272e974f91cSConrad Meyer int error; 273e974f91cSConrad Meyer 274e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 275e974f91cSConrad Meyer ioat->device = device; 276e974f91cSConrad Meyer 277e974f91cSConrad Meyer error = ioat_map_pci_bar(ioat); 278e974f91cSConrad Meyer if (error != 0) 279e974f91cSConrad Meyer goto err; 280e974f91cSConrad Meyer 281e974f91cSConrad Meyer ioat->version = ioat_read_cbver(ioat); 282e974f91cSConrad Meyer if (ioat->version < IOAT_VER_3_0) { 283e974f91cSConrad Meyer error = ENODEV; 284e974f91cSConrad Meyer goto err; 285e974f91cSConrad Meyer } 286e974f91cSConrad Meyer 287e974f91cSConrad Meyer error = ioat3_attach(device); 288e974f91cSConrad Meyer if (error != 0) 289e974f91cSConrad Meyer goto err; 290e974f91cSConrad Meyer 291e974f91cSConrad Meyer error = pci_enable_busmaster(device); 292e974f91cSConrad Meyer if (error != 0) 293e974f91cSConrad Meyer goto err; 294e974f91cSConrad Meyer 295466b3540SConrad Meyer error = ioat_setup_intr(ioat); 296466b3540SConrad Meyer if (error != 0) 297466b3540SConrad Meyer goto err; 298466b3540SConrad Meyer 299cea5b880SConrad Meyer error = ioat_reset_hw(ioat); 3007afbb263SConrad Meyer if (error != 0) 301466b3540SConrad Meyer goto err; 3027afbb263SConrad Meyer 3037afbb263SConrad Meyer ioat_process_events(ioat); 3047afbb263SConrad Meyer ioat_setup_sysctl(device); 3057afbb263SConrad Meyer 3065f77bd3eSConrad Meyer ioat->chan_idx = ioat_channel_index; 307e974f91cSConrad Meyer ioat_channel[ioat_channel_index++] = ioat; 3087afbb263SConrad Meyer ioat_test_attach(); 309e974f91cSConrad Meyer 310e974f91cSConrad Meyer err: 311e974f91cSConrad Meyer if (error != 0) 312e974f91cSConrad Meyer ioat_detach(device); 313e974f91cSConrad Meyer return (error); 314e974f91cSConrad Meyer } 315e974f91cSConrad Meyer 316e974f91cSConrad Meyer static int 317e974f91cSConrad Meyer ioat_detach(device_t device) 318e974f91cSConrad Meyer { 319e974f91cSConrad Meyer struct ioat_softc *ioat; 320e974f91cSConrad Meyer 321e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 3227afbb263SConrad Meyer 3237afbb263SConrad Meyer ioat_test_detach(); 324374b05e1SConrad Meyer taskqueue_drain(taskqueue_thread, &ioat->reset_task); 3255f77bd3eSConrad Meyer 3265f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 3275f77bd3eSConrad Meyer ioat->quiescing = TRUE; 3280ff814e8SConrad Meyer ioat->destroying = TRUE; 3290ff814e8SConrad Meyer wakeup(&ioat->quiescing); 33093f7f84aSConrad Meyer wakeup(&ioat->resetting); 3310ff814e8SConrad Meyer 3325f77bd3eSConrad Meyer ioat_channel[ioat->chan_idx] = NULL; 3335f77bd3eSConrad Meyer 3345f77bd3eSConrad Meyer ioat_drain_locked(ioat); 3355f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 336fe720f5aSConrad Meyer 337fe720f5aSConrad Meyer ioat_teardown_intr(ioat); 3385ac77963SConrad Meyer callout_drain(&ioat->poll_timer); 3395ac77963SConrad Meyer callout_drain(&ioat->shrink_timer); 340e974f91cSConrad Meyer 341e974f91cSConrad Meyer pci_disable_busmaster(device); 342e974f91cSConrad Meyer 343e974f91cSConrad Meyer if (ioat->pci_resource != NULL) 344e974f91cSConrad Meyer bus_release_resource(device, SYS_RES_MEMORY, 345e974f91cSConrad Meyer ioat->pci_resource_id, ioat->pci_resource); 346e974f91cSConrad Meyer 347bf8553eaSConrad Meyer if (ioat->ring != NULL) 348bf8553eaSConrad Meyer ioat_free_ring(ioat, 1 << ioat->ring_size_order, ioat->ring); 349e974f91cSConrad Meyer 350e974f91cSConrad Meyer if (ioat->comp_update != NULL) { 351e974f91cSConrad Meyer bus_dmamap_unload(ioat->comp_update_tag, ioat->comp_update_map); 352e974f91cSConrad Meyer bus_dmamem_free(ioat->comp_update_tag, ioat->comp_update, 353e974f91cSConrad Meyer ioat->comp_update_map); 354e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->comp_update_tag); 355e974f91cSConrad Meyer } 356e974f91cSConrad Meyer 357e974f91cSConrad Meyer bus_dma_tag_destroy(ioat->hw_desc_tag); 358e974f91cSConrad Meyer 3594253ea50SConrad Meyer return (0); 3604253ea50SConrad Meyer } 3614253ea50SConrad Meyer 3624253ea50SConrad Meyer static int 3634253ea50SConrad Meyer ioat_teardown_intr(struct ioat_softc *ioat) 3644253ea50SConrad Meyer { 3654253ea50SConrad Meyer 366e974f91cSConrad Meyer if (ioat->tag != NULL) 3674253ea50SConrad Meyer bus_teardown_intr(ioat->device, ioat->res, ioat->tag); 368e974f91cSConrad Meyer 369e974f91cSConrad Meyer if (ioat->res != NULL) 3704253ea50SConrad Meyer bus_release_resource(ioat->device, SYS_RES_IRQ, 371e974f91cSConrad Meyer rman_get_rid(ioat->res), ioat->res); 372e974f91cSConrad Meyer 3734253ea50SConrad Meyer pci_release_msi(ioat->device); 374e974f91cSConrad Meyer return (0); 375e974f91cSConrad Meyer } 376e974f91cSConrad Meyer 377e974f91cSConrad Meyer static int 378cea5b880SConrad Meyer ioat_start_channel(struct ioat_softc *ioat) 379e974f91cSConrad Meyer { 380fe8712f8SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 381fe8712f8SConrad Meyer struct ioat_descriptor *desc; 382fe8712f8SConrad Meyer struct bus_dmadesc *dmadesc; 383e974f91cSConrad Meyer uint64_t status; 384e974f91cSConrad Meyer uint32_t chanerr; 385e974f91cSConrad Meyer int i; 386e974f91cSConrad Meyer 387e974f91cSConrad Meyer ioat_acquire(&ioat->dmaengine); 388fe8712f8SConrad Meyer 389fe8712f8SConrad Meyer /* Submit 'NULL' operation manually to avoid quiescing flag */ 390fe8712f8SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 391fe8712f8SConrad Meyer dmadesc = &desc->bus_dmadesc; 392fe8712f8SConrad Meyer hw_desc = desc->u.dma; 393fe8712f8SConrad Meyer 394fe8712f8SConrad Meyer dmadesc->callback_fn = NULL; 395fe8712f8SConrad Meyer dmadesc->callback_arg = NULL; 396fe8712f8SConrad Meyer 397fe8712f8SConrad Meyer hw_desc->u.control_raw = 0; 398fe8712f8SConrad Meyer hw_desc->u.control_generic.op = IOAT_OP_COPY; 399fe8712f8SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 400fe8712f8SConrad Meyer hw_desc->size = 8; 401fe8712f8SConrad Meyer hw_desc->src_addr = 0; 402fe8712f8SConrad Meyer hw_desc->dest_addr = 0; 403fe8712f8SConrad Meyer hw_desc->u.control.null = 1; 404fe8712f8SConrad Meyer 405fe8712f8SConrad Meyer ioat_submit_single(ioat); 406e974f91cSConrad Meyer ioat_release(&ioat->dmaengine); 407e974f91cSConrad Meyer 408e974f91cSConrad Meyer for (i = 0; i < 100; i++) { 409e974f91cSConrad Meyer DELAY(1); 410e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 411e974f91cSConrad Meyer if (is_ioat_idle(status)) 412e974f91cSConrad Meyer return (0); 413e974f91cSConrad Meyer } 414e974f91cSConrad Meyer 415e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 416e974f91cSConrad Meyer ioat_log_message(0, "could not start channel: " 41759acd4baSConrad Meyer "status = %#jx error = %b\n", (uintmax_t)status, (int)chanerr, 41859acd4baSConrad Meyer IOAT_CHANERR_STR); 419e974f91cSConrad Meyer return (ENXIO); 420e974f91cSConrad Meyer } 421e974f91cSConrad Meyer 422e974f91cSConrad Meyer /* 423e974f91cSConrad Meyer * Initialize Hardware 424e974f91cSConrad Meyer */ 425e974f91cSConrad Meyer static int 426e974f91cSConrad Meyer ioat3_attach(device_t device) 427e974f91cSConrad Meyer { 428e974f91cSConrad Meyer struct ioat_softc *ioat; 429e974f91cSConrad Meyer struct ioat_descriptor **ring; 430e974f91cSConrad Meyer struct ioat_descriptor *next; 431e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *dma_hw_desc; 432e974f91cSConrad Meyer int i, num_descriptors; 433e974f91cSConrad Meyer int error; 434e974f91cSConrad Meyer uint8_t xfercap; 435e974f91cSConrad Meyer 436e974f91cSConrad Meyer error = 0; 437e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 4381693d27bSConrad Meyer ioat->capabilities = ioat_read_dmacapability(ioat); 4391693d27bSConrad Meyer 44048fed014SConrad Meyer ioat_log_message(0, "Capabilities: %b\n", (int)ioat->capabilities, 4411693d27bSConrad Meyer IOAT_DMACAP_STR); 442e974f91cSConrad Meyer 443e974f91cSConrad Meyer xfercap = ioat_read_xfercap(ioat); 444e974f91cSConrad Meyer ioat->max_xfer_size = 1 << xfercap; 445e974f91cSConrad Meyer 4465ca9fc2aSConrad Meyer ioat->intrdelay_supported = (ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & 4475ca9fc2aSConrad Meyer IOAT_INTRDELAY_SUPPORTED) != 0; 4485ca9fc2aSConrad Meyer if (ioat->intrdelay_supported) 4495ca9fc2aSConrad Meyer ioat->intrdelay_max = IOAT_INTRDELAY_US_MASK; 4505ca9fc2aSConrad Meyer 451e974f91cSConrad Meyer /* TODO: need to check DCA here if we ever do XOR/PQ */ 452e974f91cSConrad Meyer 453e974f91cSConrad Meyer mtx_init(&ioat->submit_lock, "ioat_submit", NULL, MTX_DEF); 454faefad9cSConrad Meyer mtx_init(&ioat->cleanup_lock, "ioat_cleanup", NULL, MTX_DEF); 4555ac77963SConrad Meyer callout_init(&ioat->poll_timer, 1); 4565ac77963SConrad Meyer callout_init(&ioat->shrink_timer, 1); 457374b05e1SConrad Meyer TASK_INIT(&ioat->reset_task, 0, ioat_reset_hw_task, ioat); 458e974f91cSConrad Meyer 459faefad9cSConrad Meyer /* Establish lock order for Witness */ 460faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 461faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 462faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 463faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 464faefad9cSConrad Meyer 465e974f91cSConrad Meyer ioat->is_resize_pending = FALSE; 46625ad9585SConrad Meyer ioat->is_submitter_processing = FALSE; 467e974f91cSConrad Meyer ioat->is_completion_pending = FALSE; 468e974f91cSConrad Meyer ioat->is_reset_pending = FALSE; 469e974f91cSConrad Meyer ioat->is_channel_running = FALSE; 470e974f91cSConrad Meyer 471e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), sizeof(uint64_t), 0x0, 472e974f91cSConrad Meyer BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 473e974f91cSConrad Meyer sizeof(uint64_t), 1, sizeof(uint64_t), 0, NULL, NULL, 474e974f91cSConrad Meyer &ioat->comp_update_tag); 475e974f91cSConrad Meyer 476e974f91cSConrad Meyer error = bus_dmamem_alloc(ioat->comp_update_tag, 477e974f91cSConrad Meyer (void **)&ioat->comp_update, BUS_DMA_ZERO, &ioat->comp_update_map); 478e974f91cSConrad Meyer if (ioat->comp_update == NULL) 479e974f91cSConrad Meyer return (ENOMEM); 480e974f91cSConrad Meyer 481e974f91cSConrad Meyer error = bus_dmamap_load(ioat->comp_update_tag, ioat->comp_update_map, 482e974f91cSConrad Meyer ioat->comp_update, sizeof(uint64_t), ioat_comp_update_map, ioat, 483e974f91cSConrad Meyer 0); 484e974f91cSConrad Meyer if (error != 0) 485e974f91cSConrad Meyer return (error); 486e974f91cSConrad Meyer 487e974f91cSConrad Meyer ioat->ring_size_order = IOAT_MIN_ORDER; 488e974f91cSConrad Meyer 489e974f91cSConrad Meyer num_descriptors = 1 << ioat->ring_size_order; 490e974f91cSConrad Meyer 491e974f91cSConrad Meyer bus_dma_tag_create(bus_get_dma_tag(ioat->device), 0x40, 0x0, 492519e8baaSConrad Meyer BUS_SPACE_MAXADDR_40BIT, BUS_SPACE_MAXADDR, NULL, NULL, 493e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 1, 494e974f91cSConrad Meyer sizeof(struct ioat_dma_hw_descriptor), 0, NULL, NULL, 495e974f91cSConrad Meyer &ioat->hw_desc_tag); 496e974f91cSConrad Meyer 497e974f91cSConrad Meyer ioat->ring = malloc(num_descriptors * sizeof(*ring), M_IOAT, 498bf8553eaSConrad Meyer M_ZERO | M_WAITOK); 499e974f91cSConrad Meyer 500e974f91cSConrad Meyer ring = ioat->ring; 501e974f91cSConrad Meyer for (i = 0; i < num_descriptors; i++) { 502bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, M_WAITOK); 503e974f91cSConrad Meyer if (ring[i] == NULL) 504e974f91cSConrad Meyer return (ENOMEM); 505e974f91cSConrad Meyer 506e974f91cSConrad Meyer ring[i]->id = i; 507e974f91cSConrad Meyer } 508e974f91cSConrad Meyer 509e974f91cSConrad Meyer for (i = 0; i < num_descriptors - 1; i++) { 510e974f91cSConrad Meyer next = ring[i + 1]; 511e974f91cSConrad Meyer dma_hw_desc = ring[i]->u.dma; 512e974f91cSConrad Meyer 513e974f91cSConrad Meyer dma_hw_desc->next = next->hw_desc_bus_addr; 514e974f91cSConrad Meyer } 515e974f91cSConrad Meyer 516e974f91cSConrad Meyer ring[i]->u.dma->next = ring[0]->hw_desc_bus_addr; 517e974f91cSConrad Meyer 518bf8553eaSConrad Meyer ioat->head = ioat->hw_head = 0; 519e974f91cSConrad Meyer ioat->tail = 0; 520e974f91cSConrad Meyer ioat->last_seen = 0; 521fe8712f8SConrad Meyer *ioat->comp_update = 0; 522e974f91cSConrad Meyer return (0); 523e974f91cSConrad Meyer } 524e974f91cSConrad Meyer 525e974f91cSConrad Meyer static int 526e974f91cSConrad Meyer ioat_map_pci_bar(struct ioat_softc *ioat) 527e974f91cSConrad Meyer { 528e974f91cSConrad Meyer 529e974f91cSConrad Meyer ioat->pci_resource_id = PCIR_BAR(0); 530e88e14b9SConrad Meyer ioat->pci_resource = bus_alloc_resource_any(ioat->device, 531e88e14b9SConrad Meyer SYS_RES_MEMORY, &ioat->pci_resource_id, RF_ACTIVE); 532e974f91cSConrad Meyer 533e974f91cSConrad Meyer if (ioat->pci_resource == NULL) { 534e974f91cSConrad Meyer ioat_log_message(0, "unable to allocate pci resource\n"); 535e974f91cSConrad Meyer return (ENODEV); 536e974f91cSConrad Meyer } 537e974f91cSConrad Meyer 538e974f91cSConrad Meyer ioat->pci_bus_tag = rman_get_bustag(ioat->pci_resource); 539e974f91cSConrad Meyer ioat->pci_bus_handle = rman_get_bushandle(ioat->pci_resource); 540e974f91cSConrad Meyer return (0); 541e974f91cSConrad Meyer } 542e974f91cSConrad Meyer 543e974f91cSConrad Meyer static void 544e974f91cSConrad Meyer ioat_comp_update_map(void *arg, bus_dma_segment_t *seg, int nseg, int error) 545e974f91cSConrad Meyer { 546e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 547e974f91cSConrad Meyer 548cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 549e974f91cSConrad Meyer ioat->comp_update_bus_addr = seg[0].ds_addr; 550e974f91cSConrad Meyer } 551e974f91cSConrad Meyer 552e974f91cSConrad Meyer static void 553e974f91cSConrad Meyer ioat_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 554e974f91cSConrad Meyer { 555e974f91cSConrad Meyer bus_addr_t *baddr; 556e974f91cSConrad Meyer 557cea5b880SConrad Meyer KASSERT(error == 0, ("%s: error:%d", __func__, error)); 558e974f91cSConrad Meyer baddr = arg; 559e974f91cSConrad Meyer *baddr = segs->ds_addr; 560e974f91cSConrad Meyer } 561e974f91cSConrad Meyer 562e974f91cSConrad Meyer /* 563e974f91cSConrad Meyer * Interrupt setup and handlers 564e974f91cSConrad Meyer */ 565e974f91cSConrad Meyer static int 5664253ea50SConrad Meyer ioat_setup_intr(struct ioat_softc *ioat) 567e974f91cSConrad Meyer { 568e974f91cSConrad Meyer uint32_t num_vectors; 569e974f91cSConrad Meyer int error; 570e974f91cSConrad Meyer boolean_t use_msix; 571e974f91cSConrad Meyer boolean_t force_legacy_interrupts; 572e974f91cSConrad Meyer 573e974f91cSConrad Meyer use_msix = FALSE; 574e974f91cSConrad Meyer force_legacy_interrupts = FALSE; 575e974f91cSConrad Meyer 576e974f91cSConrad Meyer if (!g_force_legacy_interrupts && pci_msix_count(ioat->device) >= 1) { 577e974f91cSConrad Meyer num_vectors = 1; 578e974f91cSConrad Meyer pci_alloc_msix(ioat->device, &num_vectors); 579e974f91cSConrad Meyer if (num_vectors == 1) 580e974f91cSConrad Meyer use_msix = TRUE; 581e974f91cSConrad Meyer } 582e974f91cSConrad Meyer 583e974f91cSConrad Meyer if (use_msix) { 584e974f91cSConrad Meyer ioat->rid = 1; 585e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 586e974f91cSConrad Meyer &ioat->rid, RF_ACTIVE); 587e974f91cSConrad Meyer } else { 588e974f91cSConrad Meyer ioat->rid = 0; 589e974f91cSConrad Meyer ioat->res = bus_alloc_resource_any(ioat->device, SYS_RES_IRQ, 590e974f91cSConrad Meyer &ioat->rid, RF_SHAREABLE | RF_ACTIVE); 591e974f91cSConrad Meyer } 592e974f91cSConrad Meyer if (ioat->res == NULL) { 593e974f91cSConrad Meyer ioat_log_message(0, "bus_alloc_resource failed\n"); 594e974f91cSConrad Meyer return (ENOMEM); 595e974f91cSConrad Meyer } 596e974f91cSConrad Meyer 597e974f91cSConrad Meyer ioat->tag = NULL; 598e974f91cSConrad Meyer error = bus_setup_intr(ioat->device, ioat->res, INTR_MPSAFE | 599e974f91cSConrad Meyer INTR_TYPE_MISC, NULL, ioat_interrupt_handler, ioat, &ioat->tag); 600e974f91cSConrad Meyer if (error != 0) { 601e974f91cSConrad Meyer ioat_log_message(0, "bus_setup_intr failed\n"); 602e974f91cSConrad Meyer return (error); 603e974f91cSConrad Meyer } 604e974f91cSConrad Meyer 605e974f91cSConrad Meyer ioat_write_intrctrl(ioat, IOAT_INTRCTRL_MASTER_INT_EN); 606e974f91cSConrad Meyer return (0); 607e974f91cSConrad Meyer } 608e974f91cSConrad Meyer 6094253ea50SConrad Meyer static boolean_t 6100d1a05d9SConrad Meyer ioat_model_resets_msix(struct ioat_softc *ioat) 6114253ea50SConrad Meyer { 6124253ea50SConrad Meyer u_int32_t pciid; 6134253ea50SConrad Meyer 6144253ea50SConrad Meyer pciid = pci_get_devid(ioat->device); 6154253ea50SConrad Meyer switch (pciid) { 6160d1a05d9SConrad Meyer /* BWD: */ 6170d1a05d9SConrad Meyer case 0x0c508086: 6180d1a05d9SConrad Meyer case 0x0c518086: 6190d1a05d9SConrad Meyer case 0x0c528086: 6200d1a05d9SConrad Meyer case 0x0c538086: 6210d1a05d9SConrad Meyer /* BDXDE: */ 6224253ea50SConrad Meyer case 0x6f508086: 6234253ea50SConrad Meyer case 0x6f518086: 6244253ea50SConrad Meyer case 0x6f528086: 6254253ea50SConrad Meyer case 0x6f538086: 6264253ea50SConrad Meyer return (TRUE); 6274253ea50SConrad Meyer } 6284253ea50SConrad Meyer 6294253ea50SConrad Meyer return (FALSE); 6304253ea50SConrad Meyer } 6314253ea50SConrad Meyer 632e974f91cSConrad Meyer static void 633e974f91cSConrad Meyer ioat_interrupt_handler(void *arg) 634e974f91cSConrad Meyer { 635e974f91cSConrad Meyer struct ioat_softc *ioat = arg; 636e974f91cSConrad Meyer 63701fbbc88SConrad Meyer ioat->stats.interrupts++; 638e974f91cSConrad Meyer ioat_process_events(ioat); 639e974f91cSConrad Meyer } 640e974f91cSConrad Meyer 641faefad9cSConrad Meyer static int 642faefad9cSConrad Meyer chanerr_to_errno(uint32_t chanerr) 643faefad9cSConrad Meyer { 644faefad9cSConrad Meyer 645faefad9cSConrad Meyer if (chanerr == 0) 646faefad9cSConrad Meyer return (0); 647faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_XSADDERR | IOAT_CHANERR_XDADDERR)) != 0) 648faefad9cSConrad Meyer return (EFAULT); 649faefad9cSConrad Meyer if ((chanerr & (IOAT_CHANERR_RDERR | IOAT_CHANERR_WDERR)) != 0) 650faefad9cSConrad Meyer return (EIO); 651faefad9cSConrad Meyer /* This one is probably our fault: */ 652faefad9cSConrad Meyer if ((chanerr & IOAT_CHANERR_NDADDERR) != 0) 653faefad9cSConrad Meyer return (EIO); 654faefad9cSConrad Meyer return (EIO); 655faefad9cSConrad Meyer } 656faefad9cSConrad Meyer 657e974f91cSConrad Meyer static void 658e974f91cSConrad Meyer ioat_process_events(struct ioat_softc *ioat) 659e974f91cSConrad Meyer { 660e974f91cSConrad Meyer struct ioat_descriptor *desc; 661e974f91cSConrad Meyer struct bus_dmadesc *dmadesc; 662e974f91cSConrad Meyer uint64_t comp_update, status; 663faefad9cSConrad Meyer uint32_t completed, chanerr; 6645ac77963SConrad Meyer boolean_t pending; 665faefad9cSConrad Meyer int error; 666e974f91cSConrad Meyer 667e974f91cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 668e974f91cSConrad Meyer 669fe8712f8SConrad Meyer /* 670fe8712f8SConrad Meyer * Don't run while the hardware is being reset. Reset is responsible 671fe8712f8SConrad Meyer * for blocking new work and draining & completing existing work, so 672fe8712f8SConrad Meyer * there is nothing to do until new work is queued after reset anyway. 673fe8712f8SConrad Meyer */ 674fe8712f8SConrad Meyer if (ioat->resetting_cleanup) { 675fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 676fe8712f8SConrad Meyer return; 677fe8712f8SConrad Meyer } 678fe8712f8SConrad Meyer 679e974f91cSConrad Meyer completed = 0; 6800283c0f5SConrad Meyer comp_update = ioat_get_chansts(ioat); 681e974f91cSConrad Meyer status = comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK; 682e974f91cSConrad Meyer 6831bbc06b8SConrad Meyer if (status == ioat->last_seen) { 6841bbc06b8SConrad Meyer /* 6851bbc06b8SConrad Meyer * If we landed in process_events and nothing has been 6861bbc06b8SConrad Meyer * completed, check for a timeout due to channel halt. 6871bbc06b8SConrad Meyer */ 6881bbc06b8SConrad Meyer goto out; 6891bbc06b8SConrad Meyer } 690dc465059SConrad Meyer CTR4(KTR_IOAT, "%s channel=%u hw_status=0x%lx last_seen=0x%lx", 691dc465059SConrad Meyer __func__, ioat->chan_idx, comp_update, ioat->last_seen); 6921bbc06b8SConrad Meyer 6931bbc06b8SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail - 1); 6941bbc06b8SConrad Meyer while (desc->hw_desc_bus_addr != status && ioat_get_active(ioat) > 0) { 695e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 696e974f91cSConrad Meyer dmadesc = &desc->bus_dmadesc; 69705e4cffbSConrad Meyer CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) ok cb %p(%p)", 69805e4cffbSConrad Meyer ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn, 699520f6023SConrad Meyer dmadesc->callback_arg); 700e974f91cSConrad Meyer 701faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 702faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 0); 703e974f91cSConrad Meyer 704466b3540SConrad Meyer completed++; 705e974f91cSConrad Meyer ioat->tail++; 706e974f91cSConrad Meyer } 70705e4cffbSConrad Meyer CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__, 70805e4cffbSConrad Meyer ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat)); 709e974f91cSConrad Meyer 7100283c0f5SConrad Meyer if (completed != 0) { 711e974f91cSConrad Meyer ioat->last_seen = desc->hw_desc_bus_addr; 71201fbbc88SConrad Meyer ioat->stats.descriptors_processed += completed; 7130283c0f5SConrad Meyer } 71401fbbc88SConrad Meyer 7151bbc06b8SConrad Meyer out: 716e974f91cSConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 7175ac77963SConrad Meyer 7185ac77963SConrad Meyer /* Perform a racy check first; only take the locks if it passes. */ 7195ac77963SConrad Meyer pending = (ioat_get_active(ioat) != 0); 7205ac77963SConrad Meyer if (!pending && ioat->is_completion_pending) { 721e974f91cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 7225ac77963SConrad Meyer mtx_lock(&ioat->submit_lock); 7235ac77963SConrad Meyer mtx_lock(&ioat->cleanup_lock); 7245ac77963SConrad Meyer 7255ac77963SConrad Meyer pending = (ioat_get_active(ioat) != 0); 7265ac77963SConrad Meyer if (!pending && ioat->is_completion_pending) { 7275ac77963SConrad Meyer ioat->is_completion_pending = FALSE; 7285ac77963SConrad Meyer callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD, 7295ac77963SConrad Meyer ioat_shrink_timer_callback, ioat); 7305ac77963SConrad Meyer callout_stop(&ioat->poll_timer); 7315ac77963SConrad Meyer } 7325ac77963SConrad Meyer mtx_unlock(&ioat->submit_lock); 7335ac77963SConrad Meyer } 7345ac77963SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 7355ac77963SConrad Meyer 7365ac77963SConrad Meyer if (pending) 7375ac77963SConrad Meyer callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback, 7385ac77963SConrad Meyer ioat); 739466b3540SConrad Meyer 740007a7030SConrad Meyer if (completed != 0) { 741466b3540SConrad Meyer ioat_putn(ioat, completed, IOAT_ACTIVE_DESCR_REF); 742bf8553eaSConrad Meyer wakeup(&ioat->tail); 743007a7030SConrad Meyer } 744faefad9cSConrad Meyer 745d2c55e5aSConrad Meyer if (!is_ioat_halted(comp_update) && !is_ioat_suspended(comp_update)) 746faefad9cSConrad Meyer return; 747faefad9cSConrad Meyer 74801fbbc88SConrad Meyer ioat->stats.channel_halts++; 74901fbbc88SConrad Meyer 750faefad9cSConrad Meyer /* 751faefad9cSConrad Meyer * Fatal programming error on this DMA channel. Flush any outstanding 752faefad9cSConrad Meyer * work with error status and restart the engine. 753faefad9cSConrad Meyer */ 754faefad9cSConrad Meyer mtx_lock(&ioat->submit_lock); 755faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 756faefad9cSConrad Meyer ioat->quiescing = TRUE; 757faefad9cSConrad Meyer 758faefad9cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 759985efa77SConrad Meyer if (1 <= g_ioat_debug_level) 760faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 76101fbbc88SConrad Meyer ioat->stats.last_halt_chanerr = chanerr; 762faefad9cSConrad Meyer 763faefad9cSConrad Meyer while (ioat_get_active(ioat) > 0) { 764faefad9cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail); 765faefad9cSConrad Meyer dmadesc = &desc->bus_dmadesc; 76605e4cffbSConrad Meyer CTR5(KTR_IOAT, "channel=%u completing desc idx %u (%p) err cb %p(%p)", 76705e4cffbSConrad Meyer ioat->chan_idx, ioat->tail, dmadesc, dmadesc->callback_fn, 768520f6023SConrad Meyer dmadesc->callback_arg); 769faefad9cSConrad Meyer 770faefad9cSConrad Meyer if (dmadesc->callback_fn != NULL) 771faefad9cSConrad Meyer dmadesc->callback_fn(dmadesc->callback_arg, 772faefad9cSConrad Meyer chanerr_to_errno(chanerr)); 773faefad9cSConrad Meyer 774faefad9cSConrad Meyer ioat_putn_locked(ioat, 1, IOAT_ACTIVE_DESCR_REF); 775faefad9cSConrad Meyer ioat->tail++; 77601fbbc88SConrad Meyer ioat->stats.descriptors_processed++; 77701fbbc88SConrad Meyer ioat->stats.descriptors_error++; 778faefad9cSConrad Meyer } 77905e4cffbSConrad Meyer CTR5(KTR_IOAT, "%s channel=%u head=%u tail=%u active=%u", __func__, 78005e4cffbSConrad Meyer ioat->chan_idx, ioat->head, ioat->tail, ioat_get_active(ioat)); 781faefad9cSConrad Meyer 782c114e741SConrad Meyer if (ioat->is_completion_pending) { 783c114e741SConrad Meyer ioat->is_completion_pending = FALSE; 784c114e741SConrad Meyer callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD, 785c114e741SConrad Meyer ioat_shrink_timer_callback, ioat); 786c114e741SConrad Meyer callout_stop(&ioat->poll_timer); 787c114e741SConrad Meyer } 788c114e741SConrad Meyer 789faefad9cSConrad Meyer /* Clear error status */ 790faefad9cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 791faefad9cSConrad Meyer 792faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 793faefad9cSConrad Meyer mtx_unlock(&ioat->submit_lock); 794faefad9cSConrad Meyer 795faefad9cSConrad Meyer ioat_log_message(0, "Resetting channel to recover from error\n"); 796374b05e1SConrad Meyer error = taskqueue_enqueue(taskqueue_thread, &ioat->reset_task); 797374b05e1SConrad Meyer KASSERT(error == 0, 798374b05e1SConrad Meyer ("%s: taskqueue_enqueue failed: %d", __func__, error)); 799374b05e1SConrad Meyer } 800374b05e1SConrad Meyer 801374b05e1SConrad Meyer static void 802374b05e1SConrad Meyer ioat_reset_hw_task(void *ctx, int pending __unused) 803374b05e1SConrad Meyer { 804374b05e1SConrad Meyer struct ioat_softc *ioat; 805374b05e1SConrad Meyer int error; 806374b05e1SConrad Meyer 807374b05e1SConrad Meyer ioat = ctx; 808374b05e1SConrad Meyer ioat_log_message(1, "%s: Resetting channel\n", __func__); 809374b05e1SConrad Meyer 810faefad9cSConrad Meyer error = ioat_reset_hw(ioat); 811faefad9cSConrad Meyer KASSERT(error == 0, ("%s: reset failed: %d", __func__, error)); 812374b05e1SConrad Meyer (void)error; 813e974f91cSConrad Meyer } 814e974f91cSConrad Meyer 815e974f91cSConrad Meyer /* 816e974f91cSConrad Meyer * User API functions 817e974f91cSConrad Meyer */ 818f8f92e91SConrad Meyer unsigned 819f8f92e91SConrad Meyer ioat_get_nchannels(void) 820f8f92e91SConrad Meyer { 821f8f92e91SConrad Meyer 822f8f92e91SConrad Meyer return (ioat_channel_index); 823f8f92e91SConrad Meyer } 824f8f92e91SConrad Meyer 825e974f91cSConrad Meyer bus_dmaengine_t 8260ff814e8SConrad Meyer ioat_get_dmaengine(uint32_t index, int flags) 827e974f91cSConrad Meyer { 8280ff814e8SConrad Meyer struct ioat_softc *ioat; 8290ff814e8SConrad Meyer 8300ff814e8SConrad Meyer KASSERT((flags & ~(M_NOWAIT | M_WAITOK)) == 0, 8310ff814e8SConrad Meyer ("invalid flags: 0x%08x", flags)); 8320ff814e8SConrad Meyer KASSERT((flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK), 8330ff814e8SConrad Meyer ("invalid wait | nowait")); 834e974f91cSConrad Meyer 835466b3540SConrad Meyer if (index >= ioat_channel_index) 836e974f91cSConrad Meyer return (NULL); 8375f77bd3eSConrad Meyer 8380ff814e8SConrad Meyer ioat = ioat_channel[index]; 8390ff814e8SConrad Meyer if (ioat == NULL || ioat->destroying) 8405f77bd3eSConrad Meyer return (NULL); 8415f77bd3eSConrad Meyer 8420ff814e8SConrad Meyer if (ioat->quiescing) { 8430ff814e8SConrad Meyer if ((flags & M_NOWAIT) != 0) 8440ff814e8SConrad Meyer return (NULL); 8450ff814e8SConrad Meyer 8460ff814e8SConrad Meyer mtx_lock(IOAT_REFLK); 8470ff814e8SConrad Meyer while (ioat->quiescing && !ioat->destroying) 8480ff814e8SConrad Meyer msleep(&ioat->quiescing, IOAT_REFLK, 0, "getdma", 0); 8490ff814e8SConrad Meyer mtx_unlock(IOAT_REFLK); 8500ff814e8SConrad Meyer 8510ff814e8SConrad Meyer if (ioat->destroying) 8520ff814e8SConrad Meyer return (NULL); 8530ff814e8SConrad Meyer } 8540ff814e8SConrad Meyer 8550ff814e8SConrad Meyer /* 8560ff814e8SConrad Meyer * There's a race here between the quiescing check and HW reset or 8570ff814e8SConrad Meyer * module destroy. 8580ff814e8SConrad Meyer */ 8590ff814e8SConrad Meyer return (&ioat_get(ioat, IOAT_DMAENGINE_REF)->dmaengine); 860466b3540SConrad Meyer } 861466b3540SConrad Meyer 862466b3540SConrad Meyer void 863466b3540SConrad Meyer ioat_put_dmaengine(bus_dmaengine_t dmaengine) 864466b3540SConrad Meyer { 865466b3540SConrad Meyer struct ioat_softc *ioat; 866466b3540SConrad Meyer 867466b3540SConrad Meyer ioat = to_ioat_softc(dmaengine); 868466b3540SConrad Meyer ioat_put(ioat, IOAT_DMAENGINE_REF); 869e974f91cSConrad Meyer } 870e974f91cSConrad Meyer 8715ca9fc2aSConrad Meyer int 87231bf2875SConrad Meyer ioat_get_hwversion(bus_dmaengine_t dmaengine) 87331bf2875SConrad Meyer { 87431bf2875SConrad Meyer struct ioat_softc *ioat; 87531bf2875SConrad Meyer 87631bf2875SConrad Meyer ioat = to_ioat_softc(dmaengine); 87731bf2875SConrad Meyer return (ioat->version); 87831bf2875SConrad Meyer } 87931bf2875SConrad Meyer 880bd81fe68SConrad Meyer size_t 881bd81fe68SConrad Meyer ioat_get_max_io_size(bus_dmaengine_t dmaengine) 882bd81fe68SConrad Meyer { 883bd81fe68SConrad Meyer struct ioat_softc *ioat; 884bd81fe68SConrad Meyer 885bd81fe68SConrad Meyer ioat = to_ioat_softc(dmaengine); 886bd81fe68SConrad Meyer return (ioat->max_xfer_size); 887bd81fe68SConrad Meyer } 888bd81fe68SConrad Meyer 889f8253f1aSConrad Meyer uint32_t 890f8253f1aSConrad Meyer ioat_get_capabilities(bus_dmaengine_t dmaengine) 891f8253f1aSConrad Meyer { 892f8253f1aSConrad Meyer struct ioat_softc *ioat; 893f8253f1aSConrad Meyer 894f8253f1aSConrad Meyer ioat = to_ioat_softc(dmaengine); 895f8253f1aSConrad Meyer return (ioat->capabilities); 896f8253f1aSConrad Meyer } 897f8253f1aSConrad Meyer 89831bf2875SConrad Meyer int 8995ca9fc2aSConrad Meyer ioat_set_interrupt_coalesce(bus_dmaengine_t dmaengine, uint16_t delay) 9005ca9fc2aSConrad Meyer { 9015ca9fc2aSConrad Meyer struct ioat_softc *ioat; 9025ca9fc2aSConrad Meyer 9035ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 9045ca9fc2aSConrad Meyer if (!ioat->intrdelay_supported) 9055ca9fc2aSConrad Meyer return (ENODEV); 9065ca9fc2aSConrad Meyer if (delay > ioat->intrdelay_max) 9075ca9fc2aSConrad Meyer return (ERANGE); 9085ca9fc2aSConrad Meyer 9095ca9fc2aSConrad Meyer ioat_write_2(ioat, IOAT_INTRDELAY_OFFSET, delay); 9105ca9fc2aSConrad Meyer ioat->cached_intrdelay = 9115ca9fc2aSConrad Meyer ioat_read_2(ioat, IOAT_INTRDELAY_OFFSET) & IOAT_INTRDELAY_US_MASK; 9125ca9fc2aSConrad Meyer return (0); 9135ca9fc2aSConrad Meyer } 9145ca9fc2aSConrad Meyer 9155ca9fc2aSConrad Meyer uint16_t 9165ca9fc2aSConrad Meyer ioat_get_max_coalesce_period(bus_dmaengine_t dmaengine) 9175ca9fc2aSConrad Meyer { 9185ca9fc2aSConrad Meyer struct ioat_softc *ioat; 9195ca9fc2aSConrad Meyer 9205ca9fc2aSConrad Meyer ioat = to_ioat_softc(dmaengine); 9215ca9fc2aSConrad Meyer return (ioat->intrdelay_max); 9225ca9fc2aSConrad Meyer } 9235ca9fc2aSConrad Meyer 924e974f91cSConrad Meyer void 925e974f91cSConrad Meyer ioat_acquire(bus_dmaengine_t dmaengine) 926e974f91cSConrad Meyer { 927e974f91cSConrad Meyer struct ioat_softc *ioat; 928e974f91cSConrad Meyer 929e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 930e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 931520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 932e974f91cSConrad Meyer } 933e974f91cSConrad Meyer 9341502e363SConrad Meyer int 9351502e363SConrad Meyer ioat_acquire_reserve(bus_dmaengine_t dmaengine, unsigned n, int mflags) 9361502e363SConrad Meyer { 9371502e363SConrad Meyer struct ioat_softc *ioat; 9381502e363SConrad Meyer int error; 9391502e363SConrad Meyer 9401502e363SConrad Meyer ioat = to_ioat_softc(dmaengine); 9411502e363SConrad Meyer ioat_acquire(dmaengine); 9421502e363SConrad Meyer 9431502e363SConrad Meyer error = ioat_reserve_space(ioat, n, mflags); 9441502e363SConrad Meyer if (error != 0) 9451502e363SConrad Meyer ioat_release(dmaengine); 9461502e363SConrad Meyer return (error); 9471502e363SConrad Meyer } 9481502e363SConrad Meyer 949e974f91cSConrad Meyer void 950e974f91cSConrad Meyer ioat_release(bus_dmaengine_t dmaengine) 951e974f91cSConrad Meyer { 952e974f91cSConrad Meyer struct ioat_softc *ioat; 953e974f91cSConrad Meyer 954e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 955*76305bb8SConrad Meyer CTR4(KTR_IOAT, "%s channel=%u dispatch1 hw_head=%u head=%u", __func__, 956*76305bb8SConrad Meyer ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head); 957*76305bb8SConrad Meyer KFAIL_POINT_CODE(DEBUG_FP, ioat_release, /* do nothing */); 958*76305bb8SConrad Meyer CTR4(KTR_IOAT, "%s channel=%u dispatch2 hw_head=%u head=%u", __func__, 959*76305bb8SConrad Meyer ioat->chan_idx, ioat->hw_head & UINT16_MAX, ioat->head); 960*76305bb8SConrad Meyer 961bf8553eaSConrad Meyer ioat_write_2(ioat, IOAT_DMACOUNT_OFFSET, (uint16_t)ioat->hw_head); 962f44abf1fSConrad Meyer 963f44abf1fSConrad Meyer if (!ioat->is_completion_pending) { 964f44abf1fSConrad Meyer ioat->is_completion_pending = TRUE; 965f44abf1fSConrad Meyer callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback, 966f44abf1fSConrad Meyer ioat); 967f44abf1fSConrad Meyer callout_stop(&ioat->shrink_timer); 968f44abf1fSConrad Meyer } 969e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 970e974f91cSConrad Meyer } 971e974f91cSConrad Meyer 9729e3bbf26SConrad Meyer static struct ioat_descriptor * 9739e3bbf26SConrad Meyer ioat_op_generic(struct ioat_softc *ioat, uint8_t op, 9749e3bbf26SConrad Meyer uint32_t size, uint64_t src, uint64_t dst, 9759e3bbf26SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, 9769e3bbf26SConrad Meyer uint32_t flags) 977e974f91cSConrad Meyer { 9789e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 979e974f91cSConrad Meyer struct ioat_descriptor *desc; 980bf8553eaSConrad Meyer int mflags; 981e974f91cSConrad Meyer 9829e3bbf26SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 9839e3bbf26SConrad Meyer 984bec7ff79SConrad Meyer KASSERT((flags & ~_DMA_GENERIC_FLAGS) == 0, 985bec7ff79SConrad Meyer ("Unrecognized flag(s): %#x", flags & ~_DMA_GENERIC_FLAGS)); 986bf8553eaSConrad Meyer if ((flags & DMA_NO_WAIT) != 0) 987bf8553eaSConrad Meyer mflags = M_NOWAIT; 988bf8553eaSConrad Meyer else 989bf8553eaSConrad Meyer mflags = M_WAITOK; 990e974f91cSConrad Meyer 9919e3bbf26SConrad Meyer if (size > ioat->max_xfer_size) { 9929e3bbf26SConrad Meyer ioat_log_message(0, "%s: max_xfer_size = %d, requested = %u\n", 9939e3bbf26SConrad Meyer __func__, ioat->max_xfer_size, (unsigned)size); 9949e3bbf26SConrad Meyer return (NULL); 9959e3bbf26SConrad Meyer } 996e974f91cSConrad Meyer 997bf8553eaSConrad Meyer if (ioat_reserve_space(ioat, 1, mflags) != 0) 998e974f91cSConrad Meyer return (NULL); 999e974f91cSConrad Meyer 1000e974f91cSConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->head); 10019e3bbf26SConrad Meyer hw_desc = desc->u.generic; 1002e974f91cSConrad Meyer 1003e974f91cSConrad Meyer hw_desc->u.control_raw = 0; 10049e3bbf26SConrad Meyer hw_desc->u.control_generic.op = op; 10059e3bbf26SConrad Meyer hw_desc->u.control_generic.completion_update = 1; 1006e974f91cSConrad Meyer 1007e974f91cSConrad Meyer if ((flags & DMA_INT_EN) != 0) 10089e3bbf26SConrad Meyer hw_desc->u.control_generic.int_enable = 1; 10096ca07079SConrad Meyer if ((flags & DMA_FENCE) != 0) 10106ca07079SConrad Meyer hw_desc->u.control_generic.fence = 1; 1011e974f91cSConrad Meyer 10129e3bbf26SConrad Meyer hw_desc->size = size; 10139e3bbf26SConrad Meyer hw_desc->src_addr = src; 10149e3bbf26SConrad Meyer hw_desc->dest_addr = dst; 1015e974f91cSConrad Meyer 1016e974f91cSConrad Meyer desc->bus_dmadesc.callback_fn = callback_fn; 1017e974f91cSConrad Meyer desc->bus_dmadesc.callback_arg = callback_arg; 10189e3bbf26SConrad Meyer return (desc); 10199e3bbf26SConrad Meyer } 1020e974f91cSConrad Meyer 10219e3bbf26SConrad Meyer struct bus_dmadesc * 10229e3bbf26SConrad Meyer ioat_null(bus_dmaengine_t dmaengine, bus_dmaengine_callback_t callback_fn, 10239e3bbf26SConrad Meyer void *callback_arg, uint32_t flags) 10249e3bbf26SConrad Meyer { 10259e3bbf26SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 10269e3bbf26SConrad Meyer struct ioat_descriptor *desc; 10279e3bbf26SConrad Meyer struct ioat_softc *ioat; 10289e3bbf26SConrad Meyer 10299e3bbf26SConrad Meyer ioat = to_ioat_softc(dmaengine); 1030520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 10319e3bbf26SConrad Meyer 10329e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 8, 0, 0, callback_fn, 10339e3bbf26SConrad Meyer callback_arg, flags); 10349e3bbf26SConrad Meyer if (desc == NULL) 10359e3bbf26SConrad Meyer return (NULL); 10369e3bbf26SConrad Meyer 10379e3bbf26SConrad Meyer hw_desc = desc->u.dma; 10389e3bbf26SConrad Meyer hw_desc->u.control.null = 1; 1039e974f91cSConrad Meyer ioat_submit_single(ioat); 1040e974f91cSConrad Meyer return (&desc->bus_dmadesc); 1041e974f91cSConrad Meyer } 1042e974f91cSConrad Meyer 1043e974f91cSConrad Meyer struct bus_dmadesc * 1044e974f91cSConrad Meyer ioat_copy(bus_dmaengine_t dmaengine, bus_addr_t dst, 1045e974f91cSConrad Meyer bus_addr_t src, bus_size_t len, bus_dmaengine_callback_t callback_fn, 1046e974f91cSConrad Meyer void *callback_arg, uint32_t flags) 1047e974f91cSConrad Meyer { 1048e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 10499e3bbf26SConrad Meyer struct ioat_descriptor *desc; 1050e974f91cSConrad Meyer struct ioat_softc *ioat; 1051e974f91cSConrad Meyer 1052e974f91cSConrad Meyer ioat = to_ioat_softc(dmaengine); 1053e974f91cSConrad Meyer 10549e3bbf26SConrad Meyer if (((src | dst) & (0xffffull << 48)) != 0) { 10559e3bbf26SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 10569e3bbf26SConrad Meyer __func__); 1057e974f91cSConrad Meyer return (NULL); 1058e974f91cSConrad Meyer } 1059e974f91cSConrad Meyer 10609e3bbf26SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, len, src, dst, callback_fn, 10619e3bbf26SConrad Meyer callback_arg, flags); 10629e3bbf26SConrad Meyer if (desc == NULL) 1063e974f91cSConrad Meyer return (NULL); 1064e974f91cSConrad Meyer 1065e974f91cSConrad Meyer hw_desc = desc->u.dma; 1066e974f91cSConrad Meyer if (g_ioat_debug_level >= 3) 1067e974f91cSConrad Meyer dump_descriptor(hw_desc); 1068e974f91cSConrad Meyer 1069e974f91cSConrad Meyer ioat_submit_single(ioat); 107005e4cffbSConrad Meyer CTR6(KTR_IOAT, "%s channel=%u desc=%p dest=%lx src=%lx len=%lx", 107105e4cffbSConrad Meyer __func__, ioat->chan_idx, &desc->bus_dmadesc, dst, src, len); 1072e974f91cSConrad Meyer return (&desc->bus_dmadesc); 1073e974f91cSConrad Meyer } 1074e974f91cSConrad Meyer 10752a4fd6b1SConrad Meyer struct bus_dmadesc * 10769950fde0SConrad Meyer ioat_copy_8k_aligned(bus_dmaengine_t dmaengine, bus_addr_t dst1, 10779950fde0SConrad Meyer bus_addr_t dst2, bus_addr_t src1, bus_addr_t src2, 10789950fde0SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 10799950fde0SConrad Meyer { 10809950fde0SConrad Meyer struct ioat_dma_hw_descriptor *hw_desc; 10819950fde0SConrad Meyer struct ioat_descriptor *desc; 10829950fde0SConrad Meyer struct ioat_softc *ioat; 10839950fde0SConrad Meyer 10849950fde0SConrad Meyer ioat = to_ioat_softc(dmaengine); 1085520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 10869950fde0SConrad Meyer 10879950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & (0xffffull << 48)) != 0) { 10889950fde0SConrad Meyer ioat_log_message(0, "%s: High 16 bits of src/dst invalid\n", 10899950fde0SConrad Meyer __func__); 10909950fde0SConrad Meyer return (NULL); 10919950fde0SConrad Meyer } 10929950fde0SConrad Meyer if (((src1 | src2 | dst1 | dst2) & PAGE_MASK) != 0) { 10939950fde0SConrad Meyer ioat_log_message(0, "%s: Addresses must be page-aligned\n", 10949950fde0SConrad Meyer __func__); 10959950fde0SConrad Meyer return (NULL); 10969950fde0SConrad Meyer } 10979950fde0SConrad Meyer 10989950fde0SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_COPY, 2 * PAGE_SIZE, src1, dst1, 10999950fde0SConrad Meyer callback_fn, callback_arg, flags); 11009950fde0SConrad Meyer if (desc == NULL) 11019950fde0SConrad Meyer return (NULL); 11029950fde0SConrad Meyer 11039950fde0SConrad Meyer hw_desc = desc->u.dma; 11049950fde0SConrad Meyer if (src2 != src1 + PAGE_SIZE) { 11059950fde0SConrad Meyer hw_desc->u.control.src_page_break = 1; 11069950fde0SConrad Meyer hw_desc->next_src_addr = src2; 11079950fde0SConrad Meyer } 11089950fde0SConrad Meyer if (dst2 != dst1 + PAGE_SIZE) { 11099950fde0SConrad Meyer hw_desc->u.control.dest_page_break = 1; 11109950fde0SConrad Meyer hw_desc->next_dest_addr = dst2; 11119950fde0SConrad Meyer } 11129950fde0SConrad Meyer 11139950fde0SConrad Meyer if (g_ioat_debug_level >= 3) 11149950fde0SConrad Meyer dump_descriptor(hw_desc); 11159950fde0SConrad Meyer 11169950fde0SConrad Meyer ioat_submit_single(ioat); 11179950fde0SConrad Meyer return (&desc->bus_dmadesc); 11189950fde0SConrad Meyer } 11199950fde0SConrad Meyer 11209950fde0SConrad Meyer struct bus_dmadesc * 1121bec7ff79SConrad Meyer ioat_copy_crc(bus_dmaengine_t dmaengine, bus_addr_t dst, bus_addr_t src, 1122bec7ff79SConrad Meyer bus_size_t len, uint32_t *initialseed, bus_addr_t crcptr, 1123bec7ff79SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 1124bec7ff79SConrad Meyer { 1125bec7ff79SConrad Meyer struct ioat_crc32_hw_descriptor *hw_desc; 1126bec7ff79SConrad Meyer struct ioat_descriptor *desc; 1127bec7ff79SConrad Meyer struct ioat_softc *ioat; 1128bec7ff79SConrad Meyer uint32_t teststore; 1129bec7ff79SConrad Meyer uint8_t op; 1130bec7ff79SConrad Meyer 1131bec7ff79SConrad Meyer ioat = to_ioat_softc(dmaengine); 1132520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1133bec7ff79SConrad Meyer 1134bec7ff79SConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_MOVECRC) == 0) { 1135bec7ff79SConrad Meyer ioat_log_message(0, "%s: Device lacks MOVECRC capability\n", 1136bec7ff79SConrad Meyer __func__); 1137bec7ff79SConrad Meyer return (NULL); 1138bec7ff79SConrad Meyer } 1139bec7ff79SConrad Meyer if (((src | dst) & (0xffffffull << 40)) != 0) { 1140bec7ff79SConrad Meyer ioat_log_message(0, "%s: High 24 bits of src/dst invalid\n", 1141bec7ff79SConrad Meyer __func__); 1142bec7ff79SConrad Meyer return (NULL); 1143bec7ff79SConrad Meyer } 1144bec7ff79SConrad Meyer teststore = (flags & _DMA_CRC_TESTSTORE); 1145bec7ff79SConrad Meyer if (teststore == _DMA_CRC_TESTSTORE) { 1146bec7ff79SConrad Meyer ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__); 1147bec7ff79SConrad Meyer return (NULL); 1148bec7ff79SConrad Meyer } 1149bec7ff79SConrad Meyer if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) { 1150bec7ff79SConrad Meyer ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n", 1151bec7ff79SConrad Meyer __func__); 1152bec7ff79SConrad Meyer return (NULL); 1153bec7ff79SConrad Meyer } 1154bec7ff79SConrad Meyer 1155bec7ff79SConrad Meyer switch (teststore) { 1156bec7ff79SConrad Meyer case DMA_CRC_STORE: 1157bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC_STORE; 1158bec7ff79SConrad Meyer break; 1159bec7ff79SConrad Meyer case DMA_CRC_TEST: 1160bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC_TEST; 1161bec7ff79SConrad Meyer break; 1162bec7ff79SConrad Meyer default: 1163bec7ff79SConrad Meyer KASSERT(teststore == 0, ("bogus")); 1164bec7ff79SConrad Meyer op = IOAT_OP_MOVECRC; 1165bec7ff79SConrad Meyer break; 1166bec7ff79SConrad Meyer } 1167bec7ff79SConrad Meyer 1168bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0 && 1169bec7ff79SConrad Meyer (crcptr & (0xffffffull << 40)) != 0) { 1170bec7ff79SConrad Meyer ioat_log_message(0, 1171bec7ff79SConrad Meyer "%s: High 24 bits of crcptr invalid\n", __func__); 1172bec7ff79SConrad Meyer return (NULL); 1173bec7ff79SConrad Meyer } 1174bec7ff79SConrad Meyer 1175bec7ff79SConrad Meyer desc = ioat_op_generic(ioat, op, len, src, dst, callback_fn, 1176bec7ff79SConrad Meyer callback_arg, flags & ~_DMA_CRC_FLAGS); 1177bec7ff79SConrad Meyer if (desc == NULL) 1178bec7ff79SConrad Meyer return (NULL); 1179bec7ff79SConrad Meyer 1180bec7ff79SConrad Meyer hw_desc = desc->u.crc32; 1181bec7ff79SConrad Meyer 1182bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0) 1183bec7ff79SConrad Meyer hw_desc->crc_address = crcptr; 1184bec7ff79SConrad Meyer else 1185bec7ff79SConrad Meyer hw_desc->u.control.crc_location = 1; 1186bec7ff79SConrad Meyer 1187bec7ff79SConrad Meyer if (initialseed != NULL) { 1188bec7ff79SConrad Meyer hw_desc->u.control.use_seed = 1; 1189bec7ff79SConrad Meyer hw_desc->seed = *initialseed; 1190bec7ff79SConrad Meyer } 1191bec7ff79SConrad Meyer 1192bec7ff79SConrad Meyer if (g_ioat_debug_level >= 3) 1193bec7ff79SConrad Meyer dump_descriptor(hw_desc); 1194bec7ff79SConrad Meyer 1195bec7ff79SConrad Meyer ioat_submit_single(ioat); 1196bec7ff79SConrad Meyer return (&desc->bus_dmadesc); 1197bec7ff79SConrad Meyer } 1198bec7ff79SConrad Meyer 1199bec7ff79SConrad Meyer struct bus_dmadesc * 1200bec7ff79SConrad Meyer ioat_crc(bus_dmaengine_t dmaengine, bus_addr_t src, bus_size_t len, 1201bec7ff79SConrad Meyer uint32_t *initialseed, bus_addr_t crcptr, 1202bec7ff79SConrad Meyer bus_dmaengine_callback_t callback_fn, void *callback_arg, uint32_t flags) 1203bec7ff79SConrad Meyer { 1204bec7ff79SConrad Meyer struct ioat_crc32_hw_descriptor *hw_desc; 1205bec7ff79SConrad Meyer struct ioat_descriptor *desc; 1206bec7ff79SConrad Meyer struct ioat_softc *ioat; 1207bec7ff79SConrad Meyer uint32_t teststore; 1208bec7ff79SConrad Meyer uint8_t op; 1209bec7ff79SConrad Meyer 1210bec7ff79SConrad Meyer ioat = to_ioat_softc(dmaengine); 1211520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1212bec7ff79SConrad Meyer 1213bec7ff79SConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_CRC) == 0) { 1214bec7ff79SConrad Meyer ioat_log_message(0, "%s: Device lacks CRC capability\n", 1215bec7ff79SConrad Meyer __func__); 1216bec7ff79SConrad Meyer return (NULL); 1217bec7ff79SConrad Meyer } 1218bec7ff79SConrad Meyer if ((src & (0xffffffull << 40)) != 0) { 1219bec7ff79SConrad Meyer ioat_log_message(0, "%s: High 24 bits of src invalid\n", 1220bec7ff79SConrad Meyer __func__); 1221bec7ff79SConrad Meyer return (NULL); 1222bec7ff79SConrad Meyer } 1223bec7ff79SConrad Meyer teststore = (flags & _DMA_CRC_TESTSTORE); 1224bec7ff79SConrad Meyer if (teststore == _DMA_CRC_TESTSTORE) { 1225bec7ff79SConrad Meyer ioat_log_message(0, "%s: TEST and STORE invalid\n", __func__); 1226bec7ff79SConrad Meyer return (NULL); 1227bec7ff79SConrad Meyer } 1228bec7ff79SConrad Meyer if (teststore == 0 && (flags & DMA_CRC_INLINE) != 0) { 1229bec7ff79SConrad Meyer ioat_log_message(0, "%s: INLINE invalid without TEST or STORE\n", 1230bec7ff79SConrad Meyer __func__); 1231bec7ff79SConrad Meyer return (NULL); 1232bec7ff79SConrad Meyer } 1233bec7ff79SConrad Meyer 1234bec7ff79SConrad Meyer switch (teststore) { 1235bec7ff79SConrad Meyer case DMA_CRC_STORE: 1236bec7ff79SConrad Meyer op = IOAT_OP_CRC_STORE; 1237bec7ff79SConrad Meyer break; 1238bec7ff79SConrad Meyer case DMA_CRC_TEST: 1239bec7ff79SConrad Meyer op = IOAT_OP_CRC_TEST; 1240bec7ff79SConrad Meyer break; 1241bec7ff79SConrad Meyer default: 1242bec7ff79SConrad Meyer KASSERT(teststore == 0, ("bogus")); 1243bec7ff79SConrad Meyer op = IOAT_OP_CRC; 1244bec7ff79SConrad Meyer break; 1245bec7ff79SConrad Meyer } 1246bec7ff79SConrad Meyer 1247bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0 && 1248bec7ff79SConrad Meyer (crcptr & (0xffffffull << 40)) != 0) { 1249bec7ff79SConrad Meyer ioat_log_message(0, 1250bec7ff79SConrad Meyer "%s: High 24 bits of crcptr invalid\n", __func__); 1251bec7ff79SConrad Meyer return (NULL); 1252bec7ff79SConrad Meyer } 1253bec7ff79SConrad Meyer 1254bec7ff79SConrad Meyer desc = ioat_op_generic(ioat, op, len, src, 0, callback_fn, 1255bec7ff79SConrad Meyer callback_arg, flags & ~_DMA_CRC_FLAGS); 1256bec7ff79SConrad Meyer if (desc == NULL) 1257bec7ff79SConrad Meyer return (NULL); 1258bec7ff79SConrad Meyer 1259bec7ff79SConrad Meyer hw_desc = desc->u.crc32; 1260bec7ff79SConrad Meyer 1261bec7ff79SConrad Meyer if ((flags & DMA_CRC_INLINE) == 0) 1262bec7ff79SConrad Meyer hw_desc->crc_address = crcptr; 1263bec7ff79SConrad Meyer else 1264bec7ff79SConrad Meyer hw_desc->u.control.crc_location = 1; 1265bec7ff79SConrad Meyer 1266bec7ff79SConrad Meyer if (initialseed != NULL) { 1267bec7ff79SConrad Meyer hw_desc->u.control.use_seed = 1; 1268bec7ff79SConrad Meyer hw_desc->seed = *initialseed; 1269bec7ff79SConrad Meyer } 1270bec7ff79SConrad Meyer 1271bec7ff79SConrad Meyer if (g_ioat_debug_level >= 3) 1272bec7ff79SConrad Meyer dump_descriptor(hw_desc); 1273bec7ff79SConrad Meyer 1274bec7ff79SConrad Meyer ioat_submit_single(ioat); 1275bec7ff79SConrad Meyer return (&desc->bus_dmadesc); 1276bec7ff79SConrad Meyer } 1277bec7ff79SConrad Meyer 1278bec7ff79SConrad Meyer struct bus_dmadesc * 12792a4fd6b1SConrad Meyer ioat_blockfill(bus_dmaengine_t dmaengine, bus_addr_t dst, uint64_t fillpattern, 12802a4fd6b1SConrad Meyer bus_size_t len, bus_dmaengine_callback_t callback_fn, void *callback_arg, 12812a4fd6b1SConrad Meyer uint32_t flags) 12822a4fd6b1SConrad Meyer { 12832a4fd6b1SConrad Meyer struct ioat_fill_hw_descriptor *hw_desc; 12842a4fd6b1SConrad Meyer struct ioat_descriptor *desc; 12852a4fd6b1SConrad Meyer struct ioat_softc *ioat; 12862a4fd6b1SConrad Meyer 12872a4fd6b1SConrad Meyer ioat = to_ioat_softc(dmaengine); 1288520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 12892a4fd6b1SConrad Meyer 12901693d27bSConrad Meyer if ((ioat->capabilities & IOAT_DMACAP_BFILL) == 0) { 12911693d27bSConrad Meyer ioat_log_message(0, "%s: Device lacks BFILL capability\n", 12921693d27bSConrad Meyer __func__); 12931693d27bSConrad Meyer return (NULL); 12941693d27bSConrad Meyer } 12951693d27bSConrad Meyer 12962a4fd6b1SConrad Meyer if ((dst & (0xffffull << 48)) != 0) { 12972a4fd6b1SConrad Meyer ioat_log_message(0, "%s: High 16 bits of dst invalid\n", 12982a4fd6b1SConrad Meyer __func__); 12992a4fd6b1SConrad Meyer return (NULL); 13002a4fd6b1SConrad Meyer } 13012a4fd6b1SConrad Meyer 13022a4fd6b1SConrad Meyer desc = ioat_op_generic(ioat, IOAT_OP_FILL, len, fillpattern, dst, 13032a4fd6b1SConrad Meyer callback_fn, callback_arg, flags); 13042a4fd6b1SConrad Meyer if (desc == NULL) 13052a4fd6b1SConrad Meyer return (NULL); 13062a4fd6b1SConrad Meyer 13072a4fd6b1SConrad Meyer hw_desc = desc->u.fill; 13082a4fd6b1SConrad Meyer if (g_ioat_debug_level >= 3) 13092a4fd6b1SConrad Meyer dump_descriptor(hw_desc); 13102a4fd6b1SConrad Meyer 13112a4fd6b1SConrad Meyer ioat_submit_single(ioat); 13122a4fd6b1SConrad Meyer return (&desc->bus_dmadesc); 13132a4fd6b1SConrad Meyer } 13142a4fd6b1SConrad Meyer 1315e974f91cSConrad Meyer /* 1316e974f91cSConrad Meyer * Ring Management 1317e974f91cSConrad Meyer */ 1318e974f91cSConrad Meyer static inline uint32_t 1319e974f91cSConrad Meyer ioat_get_active(struct ioat_softc *ioat) 1320e974f91cSConrad Meyer { 1321e974f91cSConrad Meyer 1322e974f91cSConrad Meyer return ((ioat->head - ioat->tail) & ((1 << ioat->ring_size_order) - 1)); 1323e974f91cSConrad Meyer } 1324e974f91cSConrad Meyer 1325e974f91cSConrad Meyer static inline uint32_t 1326e974f91cSConrad Meyer ioat_get_ring_space(struct ioat_softc *ioat) 1327e974f91cSConrad Meyer { 1328e974f91cSConrad Meyer 1329e974f91cSConrad Meyer return ((1 << ioat->ring_size_order) - ioat_get_active(ioat) - 1); 1330e974f91cSConrad Meyer } 1331e974f91cSConrad Meyer 1332e974f91cSConrad Meyer static struct ioat_descriptor * 1333bf8553eaSConrad Meyer ioat_alloc_ring_entry(struct ioat_softc *ioat, int mflags) 1334e974f91cSConrad Meyer { 13359e3bbf26SConrad Meyer struct ioat_generic_hw_descriptor *hw_desc; 1336e974f91cSConrad Meyer struct ioat_descriptor *desc; 1337bf8553eaSConrad Meyer int error, busdmaflag; 1338e974f91cSConrad Meyer 1339f46011aeSConrad Meyer error = ENOMEM; 1340f46011aeSConrad Meyer hw_desc = NULL; 1341f46011aeSConrad Meyer 1342bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) 1343bf8553eaSConrad Meyer busdmaflag = BUS_DMA_WAITOK; 1344bf8553eaSConrad Meyer else 1345bf8553eaSConrad Meyer busdmaflag = BUS_DMA_NOWAIT; 1346bf8553eaSConrad Meyer 1347bf8553eaSConrad Meyer desc = malloc(sizeof(*desc), M_IOAT, mflags); 1348e974f91cSConrad Meyer if (desc == NULL) 1349f46011aeSConrad Meyer goto out; 1350e974f91cSConrad Meyer 1351f46011aeSConrad Meyer bus_dmamem_alloc(ioat->hw_desc_tag, (void **)&hw_desc, 1352bf8553eaSConrad Meyer BUS_DMA_ZERO | busdmaflag, &ioat->hw_desc_map); 1353f46011aeSConrad Meyer if (hw_desc == NULL) 1354f46011aeSConrad Meyer goto out; 1355e974f91cSConrad Meyer 1356faefad9cSConrad Meyer memset(&desc->bus_dmadesc, 0, sizeof(desc->bus_dmadesc)); 13579e3bbf26SConrad Meyer desc->u.generic = hw_desc; 1358f46011aeSConrad Meyer 1359f46011aeSConrad Meyer error = bus_dmamap_load(ioat->hw_desc_tag, ioat->hw_desc_map, hw_desc, 1360f46011aeSConrad Meyer sizeof(*hw_desc), ioat_dmamap_cb, &desc->hw_desc_bus_addr, 1361bf8553eaSConrad Meyer busdmaflag); 1362f46011aeSConrad Meyer if (error) 1363f46011aeSConrad Meyer goto out; 1364f46011aeSConrad Meyer 1365f46011aeSConrad Meyer out: 1366f46011aeSConrad Meyer if (error) { 1367f46011aeSConrad Meyer ioat_free_ring_entry(ioat, desc); 1368f46011aeSConrad Meyer return (NULL); 1369f46011aeSConrad Meyer } 1370e974f91cSConrad Meyer return (desc); 1371e974f91cSConrad Meyer } 1372e974f91cSConrad Meyer 1373e974f91cSConrad Meyer static void 1374e974f91cSConrad Meyer ioat_free_ring_entry(struct ioat_softc *ioat, struct ioat_descriptor *desc) 1375e974f91cSConrad Meyer { 1376e974f91cSConrad Meyer 1377e974f91cSConrad Meyer if (desc == NULL) 1378e974f91cSConrad Meyer return; 1379e974f91cSConrad Meyer 13809e3bbf26SConrad Meyer if (desc->u.generic) 13819e3bbf26SConrad Meyer bus_dmamem_free(ioat->hw_desc_tag, desc->u.generic, 1382e974f91cSConrad Meyer ioat->hw_desc_map); 1383e974f91cSConrad Meyer free(desc, M_IOAT); 1384e974f91cSConrad Meyer } 1385e974f91cSConrad Meyer 1386bf8553eaSConrad Meyer /* 1387bf8553eaSConrad Meyer * Reserves space in this IOAT descriptor ring by ensuring enough slots remain 1388bf8553eaSConrad Meyer * for 'num_descs'. 1389bf8553eaSConrad Meyer * 1390bf8553eaSConrad Meyer * If mflags contains M_WAITOK, blocks until enough space is available. 1391bf8553eaSConrad Meyer * 1392bf8553eaSConrad Meyer * Returns zero on success, or an errno on error. If num_descs is beyond the 1393bf8553eaSConrad Meyer * maximum ring size, returns EINVAl; if allocation would block and mflags 1394bf8553eaSConrad Meyer * contains M_NOWAIT, returns EAGAIN. 1395bf8553eaSConrad Meyer * 1396bf8553eaSConrad Meyer * Must be called with the submit_lock held; returns with the lock held. The 1397bf8553eaSConrad Meyer * lock may be dropped to allocate the ring. 1398bf8553eaSConrad Meyer * 1399bf8553eaSConrad Meyer * (The submit_lock is needed to add any entries to the ring, so callers are 1400bf8553eaSConrad Meyer * assured enough room is available.) 1401bf8553eaSConrad Meyer */ 1402e974f91cSConrad Meyer static int 1403bf8553eaSConrad Meyer ioat_reserve_space(struct ioat_softc *ioat, uint32_t num_descs, int mflags) 1404e974f91cSConrad Meyer { 1405bf8553eaSConrad Meyer struct ioat_descriptor **new_ring; 1406bf8553eaSConrad Meyer uint32_t order; 140725ad9585SConrad Meyer boolean_t dug; 1408bf8553eaSConrad Meyer int error; 1409e974f91cSConrad Meyer 1410bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1411bf8553eaSConrad Meyer error = 0; 141225ad9585SConrad Meyer dug = FALSE; 1413e974f91cSConrad Meyer 141408a6b96aSConrad Meyer if (num_descs < 1 || num_descs >= (1 << IOAT_MAX_ORDER)) { 1415bf8553eaSConrad Meyer error = EINVAL; 1416bf8553eaSConrad Meyer goto out; 1417e974f91cSConrad Meyer } 141808a6b96aSConrad Meyer 141908a6b96aSConrad Meyer for (;;) { 14205f77bd3eSConrad Meyer if (ioat->quiescing) { 14215f77bd3eSConrad Meyer error = ENXIO; 14225f77bd3eSConrad Meyer goto out; 14235f77bd3eSConrad Meyer } 1424bf8553eaSConrad Meyer 1425bf8553eaSConrad Meyer if (ioat_get_ring_space(ioat) >= num_descs) 1426bf8553eaSConrad Meyer goto out; 1427bf8553eaSConrad Meyer 142805e4cffbSConrad Meyer CTR3(KTR_IOAT, "%s channel=%u starved (%u)", __func__, 142905e4cffbSConrad Meyer ioat->chan_idx, num_descs); 143005e4cffbSConrad Meyer 143125ad9585SConrad Meyer if (!dug && !ioat->is_submitter_processing && 143225ad9585SConrad Meyer (1 << ioat->ring_size_order) > num_descs) { 143325ad9585SConrad Meyer ioat->is_submitter_processing = TRUE; 143425ad9585SConrad Meyer mtx_unlock(&ioat->submit_lock); 143525ad9585SConrad Meyer 143605e4cffbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u attempting to process events", 143705e4cffbSConrad Meyer __func__, ioat->chan_idx); 143825ad9585SConrad Meyer ioat_process_events(ioat); 143925ad9585SConrad Meyer 144025ad9585SConrad Meyer mtx_lock(&ioat->submit_lock); 144125ad9585SConrad Meyer dug = TRUE; 144225ad9585SConrad Meyer KASSERT(ioat->is_submitter_processing == TRUE, 144325ad9585SConrad Meyer ("is_submitter_processing")); 144425ad9585SConrad Meyer ioat->is_submitter_processing = FALSE; 144525ad9585SConrad Meyer wakeup(&ioat->tail); 144625ad9585SConrad Meyer continue; 144725ad9585SConrad Meyer } 144825ad9585SConrad Meyer 1449bf8553eaSConrad Meyer order = ioat->ring_size_order; 1450bf8553eaSConrad Meyer if (ioat->is_resize_pending || order == IOAT_MAX_ORDER) { 1451bf8553eaSConrad Meyer if ((mflags & M_WAITOK) != 0) { 145205e4cffbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u blocking on completions", 145305e4cffbSConrad Meyer __func__, ioat->chan_idx); 1454bf8553eaSConrad Meyer msleep(&ioat->tail, &ioat->submit_lock, 0, 1455bf8553eaSConrad Meyer "ioat_rsz", 0); 1456bf8553eaSConrad Meyer continue; 1457bf8553eaSConrad Meyer } 1458bf8553eaSConrad Meyer 1459bf8553eaSConrad Meyer error = EAGAIN; 1460bf8553eaSConrad Meyer break; 1461bf8553eaSConrad Meyer } 1462bf8553eaSConrad Meyer 1463bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1464bf8553eaSConrad Meyer for (;;) { 1465bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1466bf8553eaSConrad Meyer 1467bf8553eaSConrad Meyer new_ring = ioat_prealloc_ring(ioat, 1 << (order + 1), 1468bf8553eaSConrad Meyer TRUE, mflags); 1469bf8553eaSConrad Meyer 1470bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1471bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1472bf8553eaSConrad Meyer ("is_resize_pending should protect order")); 1473bf8553eaSConrad Meyer 1474bf8553eaSConrad Meyer if (new_ring == NULL) { 1475bf8553eaSConrad Meyer KASSERT((mflags & M_WAITOK) == 0, 1476bf8553eaSConrad Meyer ("allocation failed")); 1477bf8553eaSConrad Meyer error = EAGAIN; 1478bf8553eaSConrad Meyer break; 1479bf8553eaSConrad Meyer } 1480bf8553eaSConrad Meyer 1481bf8553eaSConrad Meyer error = ring_grow(ioat, order, new_ring); 1482bf8553eaSConrad Meyer if (error == 0) 1483bf8553eaSConrad Meyer break; 1484bf8553eaSConrad Meyer } 1485bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1486bf8553eaSConrad Meyer wakeup(&ioat->tail); 1487bf8553eaSConrad Meyer if (error) 1488bf8553eaSConrad Meyer break; 1489bf8553eaSConrad Meyer } 1490bf8553eaSConrad Meyer 1491bf8553eaSConrad Meyer out: 1492bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 149308a6b96aSConrad Meyer KASSERT(!ioat->quiescing || error == ENXIO, 149408a6b96aSConrad Meyer ("reserved during quiesce")); 1495bf8553eaSConrad Meyer return (error); 1496bf8553eaSConrad Meyer } 1497bf8553eaSConrad Meyer 1498bf8553eaSConrad Meyer static struct ioat_descriptor ** 1499bf8553eaSConrad Meyer ioat_prealloc_ring(struct ioat_softc *ioat, uint32_t size, boolean_t need_dscr, 1500bf8553eaSConrad Meyer int mflags) 1501bf8553eaSConrad Meyer { 1502bf8553eaSConrad Meyer struct ioat_descriptor **ring; 1503bf8553eaSConrad Meyer uint32_t i; 1504bf8553eaSConrad Meyer int error; 1505bf8553eaSConrad Meyer 1506bf8553eaSConrad Meyer KASSERT(size > 0 && powerof2(size), ("bogus size")); 1507bf8553eaSConrad Meyer 1508bf8553eaSConrad Meyer ring = malloc(size * sizeof(*ring), M_IOAT, M_ZERO | mflags); 1509bf8553eaSConrad Meyer if (ring == NULL) 1510bf8553eaSConrad Meyer return (NULL); 1511bf8553eaSConrad Meyer 1512bf8553eaSConrad Meyer if (need_dscr) { 1513bf8553eaSConrad Meyer error = ENOMEM; 1514bf8553eaSConrad Meyer for (i = size / 2; i < size; i++) { 1515bf8553eaSConrad Meyer ring[i] = ioat_alloc_ring_entry(ioat, mflags); 1516bf8553eaSConrad Meyer if (ring[i] == NULL) 1517bf8553eaSConrad Meyer goto out; 1518bf8553eaSConrad Meyer ring[i]->id = i; 1519bf8553eaSConrad Meyer } 1520bf8553eaSConrad Meyer } 1521bf8553eaSConrad Meyer error = 0; 1522bf8553eaSConrad Meyer 1523bf8553eaSConrad Meyer out: 1524bf8553eaSConrad Meyer if (error != 0 && ring != NULL) { 1525bf8553eaSConrad Meyer ioat_free_ring(ioat, size, ring); 1526bf8553eaSConrad Meyer ring = NULL; 1527bf8553eaSConrad Meyer } 1528bf8553eaSConrad Meyer return (ring); 1529bf8553eaSConrad Meyer } 1530bf8553eaSConrad Meyer 1531bf8553eaSConrad Meyer static void 1532bf8553eaSConrad Meyer ioat_free_ring(struct ioat_softc *ioat, uint32_t size, 1533bf8553eaSConrad Meyer struct ioat_descriptor **ring) 1534bf8553eaSConrad Meyer { 1535bf8553eaSConrad Meyer uint32_t i; 1536bf8553eaSConrad Meyer 1537bf8553eaSConrad Meyer for (i = 0; i < size; i++) { 1538bf8553eaSConrad Meyer if (ring[i] != NULL) 1539bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ring[i]); 1540bf8553eaSConrad Meyer } 1541bf8553eaSConrad Meyer free(ring, M_IOAT); 1542e974f91cSConrad Meyer } 1543e974f91cSConrad Meyer 1544e974f91cSConrad Meyer static struct ioat_descriptor * 1545e974f91cSConrad Meyer ioat_get_ring_entry(struct ioat_softc *ioat, uint32_t index) 1546e974f91cSConrad Meyer { 1547e974f91cSConrad Meyer 1548e974f91cSConrad Meyer return (ioat->ring[index % (1 << ioat->ring_size_order)]); 1549e974f91cSConrad Meyer } 1550e974f91cSConrad Meyer 1551bf8553eaSConrad Meyer static int 1552bf8553eaSConrad Meyer ring_grow(struct ioat_softc *ioat, uint32_t oldorder, 1553bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1554e974f91cSConrad Meyer { 1555bf8553eaSConrad Meyer struct ioat_descriptor *tmp, *next; 1556e974f91cSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1557bf8553eaSConrad Meyer uint32_t oldsize, newsize, head, tail, i, end; 1558bf8553eaSConrad Meyer int error; 1559e974f91cSConrad Meyer 1560520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1561e974f91cSConrad Meyer 1562bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1563bf8553eaSConrad Meyer 1564bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder >= IOAT_MAX_ORDER) { 1565bf8553eaSConrad Meyer error = EINVAL; 1566bf8553eaSConrad Meyer goto out; 1567bf8553eaSConrad Meyer } 1568bf8553eaSConrad Meyer 1569bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1570bf8553eaSConrad Meyer newsize = (1 << (oldorder + 1)); 1571bf8553eaSConrad Meyer 1572bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1573bf8553eaSConrad Meyer 1574bf8553eaSConrad Meyer head = ioat->head & (oldsize - 1); 1575bf8553eaSConrad Meyer tail = ioat->tail & (oldsize - 1); 1576bf8553eaSConrad Meyer 1577bf8553eaSConrad Meyer /* Copy old descriptors to new ring */ 1578bf8553eaSConrad Meyer for (i = 0; i < oldsize; i++) 1579bf8553eaSConrad Meyer newring[i] = ioat->ring[i]; 1580e974f91cSConrad Meyer 1581e974f91cSConrad Meyer /* 1582bf8553eaSConrad Meyer * If head has wrapped but tail hasn't, we must swap some descriptors 1583bf8553eaSConrad Meyer * around so that tail can increment directly to head. 1584e974f91cSConrad Meyer */ 1585bf8553eaSConrad Meyer if (head < tail) { 1586bf8553eaSConrad Meyer for (i = 0; i <= head; i++) { 1587bf8553eaSConrad Meyer tmp = newring[oldsize + i]; 1588e974f91cSConrad Meyer 1589bf8553eaSConrad Meyer newring[oldsize + i] = newring[i]; 1590bf8553eaSConrad Meyer newring[oldsize + i]->id = oldsize + i; 1591e974f91cSConrad Meyer 1592bf8553eaSConrad Meyer newring[i] = tmp; 1593bf8553eaSConrad Meyer newring[i]->id = i; 1594bf8553eaSConrad Meyer } 1595bf8553eaSConrad Meyer head += oldsize; 1596e974f91cSConrad Meyer } 1597e974f91cSConrad Meyer 1598bf8553eaSConrad Meyer KASSERT(head >= tail, ("invariants")); 1599e974f91cSConrad Meyer 1600bf8553eaSConrad Meyer /* Head didn't wrap; we only need to link in oldsize..newsize */ 1601bf8553eaSConrad Meyer if (head < oldsize) { 1602bf8553eaSConrad Meyer i = oldsize - 1; 1603bf8553eaSConrad Meyer end = newsize; 1604e974f91cSConrad Meyer } else { 1605bf8553eaSConrad Meyer /* Head did wrap; link newhead..newsize and 0..oldhead */ 1606bf8553eaSConrad Meyer i = head; 1607bf8553eaSConrad Meyer end = newsize + (head - oldsize) + 1; 1608bf8553eaSConrad Meyer } 1609bf8553eaSConrad Meyer 1610e974f91cSConrad Meyer /* 1611bf8553eaSConrad Meyer * Fix up hardware ring, being careful not to trample the active 1612bf8553eaSConrad Meyer * section (tail -> head). 1613e974f91cSConrad Meyer */ 1614bf8553eaSConrad Meyer for (; i < end; i++) { 1615bf8553eaSConrad Meyer KASSERT((i & (newsize - 1)) < tail || 1616bf8553eaSConrad Meyer (i & (newsize - 1)) >= head, ("trampling snake")); 1617e974f91cSConrad Meyer 1618bf8553eaSConrad Meyer next = newring[(i + 1) & (newsize - 1)]; 1619bf8553eaSConrad Meyer hw = newring[i & (newsize - 1)]->u.dma; 1620e974f91cSConrad Meyer hw->next = next->hw_desc_bus_addr; 1621e974f91cSConrad Meyer } 1622e974f91cSConrad Meyer 16231be25cc9SConrad Meyer #ifdef INVARIANTS 16241be25cc9SConrad Meyer for (i = 0; i < newsize; i++) { 16251be25cc9SConrad Meyer next = newring[(i + 1) & (newsize - 1)]; 16261be25cc9SConrad Meyer hw = newring[i & (newsize - 1)]->u.dma; 16271be25cc9SConrad Meyer 16281be25cc9SConrad Meyer KASSERT(hw->next == next->hw_desc_bus_addr, 16291be25cc9SConrad Meyer ("mismatch at i:%u (oldsize:%u); next=%p nextaddr=0x%lx" 16301be25cc9SConrad Meyer " (tail:%u)", i, oldsize, next, next->hw_desc_bus_addr, 16311be25cc9SConrad Meyer tail)); 16321be25cc9SConrad Meyer } 16331be25cc9SConrad Meyer #endif 16341be25cc9SConrad Meyer 1635e974f91cSConrad Meyer free(ioat->ring, M_IOAT); 1636bf8553eaSConrad Meyer ioat->ring = newring; 1637bf8553eaSConrad Meyer ioat->ring_size_order = oldorder + 1; 1638bf8553eaSConrad Meyer ioat->tail = tail; 1639bf8553eaSConrad Meyer ioat->head = head; 1640bf8553eaSConrad Meyer error = 0; 1641e974f91cSConrad Meyer 1642bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1643bf8553eaSConrad Meyer out: 1644bf8553eaSConrad Meyer if (error) 1645bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder + 1)), newring); 1646bf8553eaSConrad Meyer return (error); 1647bf8553eaSConrad Meyer } 1648bf8553eaSConrad Meyer 1649bf8553eaSConrad Meyer static int 1650bf8553eaSConrad Meyer ring_shrink(struct ioat_softc *ioat, uint32_t oldorder, 1651bf8553eaSConrad Meyer struct ioat_descriptor **newring) 1652bf8553eaSConrad Meyer { 1653bf8553eaSConrad Meyer struct ioat_dma_hw_descriptor *hw; 1654bf8553eaSConrad Meyer struct ioat_descriptor *ent, *next; 1655bf8553eaSConrad Meyer uint32_t oldsize, newsize, current_idx, new_idx, i; 1656bf8553eaSConrad Meyer int error; 1657bf8553eaSConrad Meyer 1658520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 1659bf8553eaSConrad Meyer 1660bf8553eaSConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 1661bf8553eaSConrad Meyer 1662bf8553eaSConrad Meyer if (oldorder != ioat->ring_size_order || oldorder <= IOAT_MIN_ORDER) { 1663bf8553eaSConrad Meyer error = EINVAL; 1664bf8553eaSConrad Meyer goto out_unlocked; 1665bf8553eaSConrad Meyer } 1666bf8553eaSConrad Meyer 1667bf8553eaSConrad Meyer oldsize = (1 << oldorder); 1668bf8553eaSConrad Meyer newsize = (1 << (oldorder - 1)); 1669bf8553eaSConrad Meyer 1670bf8553eaSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1671bf8553eaSConrad Meyer 1672bf8553eaSConrad Meyer /* Can't shrink below current active set! */ 1673bf8553eaSConrad Meyer if (ioat_get_active(ioat) >= newsize) { 1674bf8553eaSConrad Meyer error = ENOMEM; 1675bf8553eaSConrad Meyer goto out; 1676bf8553eaSConrad Meyer } 1677bf8553eaSConrad Meyer 1678bf8553eaSConrad Meyer /* 1679bf8553eaSConrad Meyer * Copy current descriptors to the new ring, dropping the removed 1680bf8553eaSConrad Meyer * descriptors. 1681bf8553eaSConrad Meyer */ 1682bf8553eaSConrad Meyer for (i = 0; i < newsize; i++) { 1683bf8553eaSConrad Meyer current_idx = (ioat->tail + i) & (oldsize - 1); 1684bf8553eaSConrad Meyer new_idx = (ioat->tail + i) & (newsize - 1); 1685bf8553eaSConrad Meyer 1686bf8553eaSConrad Meyer newring[new_idx] = ioat->ring[current_idx]; 1687bf8553eaSConrad Meyer newring[new_idx]->id = new_idx; 1688bf8553eaSConrad Meyer } 1689bf8553eaSConrad Meyer 1690bf8553eaSConrad Meyer /* Free deleted descriptors */ 1691bf8553eaSConrad Meyer for (i = newsize; i < oldsize; i++) { 1692bf8553eaSConrad Meyer ent = ioat_get_ring_entry(ioat, ioat->tail + i); 1693bf8553eaSConrad Meyer ioat_free_ring_entry(ioat, ent); 1694bf8553eaSConrad Meyer } 1695bf8553eaSConrad Meyer 1696bf8553eaSConrad Meyer /* Fix up hardware ring. */ 1697bf8553eaSConrad Meyer hw = newring[(ioat->tail + newsize - 1) & (newsize - 1)]->u.dma; 1698bf8553eaSConrad Meyer next = newring[(ioat->tail + newsize) & (newsize - 1)]; 1699bf8553eaSConrad Meyer hw->next = next->hw_desc_bus_addr; 1700bf8553eaSConrad Meyer 17011be25cc9SConrad Meyer #ifdef INVARIANTS 17021be25cc9SConrad Meyer for (i = 0; i < newsize; i++) { 17031be25cc9SConrad Meyer next = newring[(i + 1) & (newsize - 1)]; 17041be25cc9SConrad Meyer hw = newring[i & (newsize - 1)]->u.dma; 17051be25cc9SConrad Meyer 17061be25cc9SConrad Meyer KASSERT(hw->next == next->hw_desc_bus_addr, 17071be25cc9SConrad Meyer ("mismatch at i:%u (newsize:%u); next=%p nextaddr=0x%lx " 17081be25cc9SConrad Meyer "(tail:%u)", i, newsize, next, next->hw_desc_bus_addr, 17091be25cc9SConrad Meyer ioat->tail)); 17101be25cc9SConrad Meyer } 17111be25cc9SConrad Meyer #endif 17121be25cc9SConrad Meyer 1713bf8553eaSConrad Meyer free(ioat->ring, M_IOAT); 1714bf8553eaSConrad Meyer ioat->ring = newring; 1715bf8553eaSConrad Meyer ioat->ring_size_order = oldorder - 1; 1716bf8553eaSConrad Meyer error = 0; 1717bf8553eaSConrad Meyer 1718bf8553eaSConrad Meyer out: 1719bf8553eaSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1720bf8553eaSConrad Meyer out_unlocked: 1721bf8553eaSConrad Meyer if (error) 1722bf8553eaSConrad Meyer ioat_free_ring(ioat, (1 << (oldorder - 1)), newring); 1723bf8553eaSConrad Meyer return (error); 1724e974f91cSConrad Meyer } 1725e974f91cSConrad Meyer 1726e974f91cSConrad Meyer static void 17278f274637SConrad Meyer ioat_halted_debug(struct ioat_softc *ioat, uint32_t chanerr) 1728e974f91cSConrad Meyer { 1729e974f91cSConrad Meyer struct ioat_descriptor *desc; 17308f274637SConrad Meyer 173159acd4baSConrad Meyer ioat_log_message(0, "Channel halted (%b)\n", (int)chanerr, 173259acd4baSConrad Meyer IOAT_CHANERR_STR); 17338f274637SConrad Meyer if (chanerr == 0) 17348f274637SConrad Meyer return; 17358f274637SConrad Meyer 1736faefad9cSConrad Meyer mtx_assert(&ioat->cleanup_lock, MA_OWNED); 1737faefad9cSConrad Meyer 17388f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 0); 17398f274637SConrad Meyer dump_descriptor(desc->u.raw); 17408f274637SConrad Meyer 17418f274637SConrad Meyer desc = ioat_get_ring_entry(ioat, ioat->tail + 1); 17428f274637SConrad Meyer dump_descriptor(desc->u.raw); 17438f274637SConrad Meyer } 17448f274637SConrad Meyer 17458f274637SConrad Meyer static void 17465ac77963SConrad Meyer ioat_poll_timer_callback(void *arg) 17475ac77963SConrad Meyer { 17485ac77963SConrad Meyer struct ioat_softc *ioat; 17495ac77963SConrad Meyer 17505ac77963SConrad Meyer ioat = arg; 17515ac77963SConrad Meyer ioat_log_message(3, "%s\n", __func__); 17525ac77963SConrad Meyer 17535ac77963SConrad Meyer ioat_process_events(ioat); 17545ac77963SConrad Meyer } 17555ac77963SConrad Meyer 17565ac77963SConrad Meyer static void 17575ac77963SConrad Meyer ioat_shrink_timer_callback(void *arg) 17588f274637SConrad Meyer { 1759bf8553eaSConrad Meyer struct ioat_descriptor **newring; 1760e974f91cSConrad Meyer struct ioat_softc *ioat; 1761faefad9cSConrad Meyer uint32_t order; 1762e974f91cSConrad Meyer 1763e974f91cSConrad Meyer ioat = arg; 1764fe720f5aSConrad Meyer ioat_log_message(1, "%s\n", __func__); 1765e974f91cSConrad Meyer 1766faefad9cSConrad Meyer /* Slowly scale the ring down if idle. */ 1767e974f91cSConrad Meyer mtx_lock(&ioat->submit_lock); 1768fe8712f8SConrad Meyer 1769fe8712f8SConrad Meyer /* Don't run while the hardware is being reset. */ 1770fe8712f8SConrad Meyer if (ioat->resetting) { 1771fe8712f8SConrad Meyer mtx_unlock(&ioat->submit_lock); 1772fe8712f8SConrad Meyer return; 1773fe8712f8SConrad Meyer } 1774fe8712f8SConrad Meyer 1775bf8553eaSConrad Meyer order = ioat->ring_size_order; 1776c62db395SConrad Meyer if (ioat->is_completion_pending || ioat->is_resize_pending || 1777c62db395SConrad Meyer order == IOAT_MIN_ORDER) { 1778bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1779bf8553eaSConrad Meyer goto out; 1780bf8553eaSConrad Meyer } 1781bf8553eaSConrad Meyer ioat->is_resize_pending = TRUE; 1782e974f91cSConrad Meyer mtx_unlock(&ioat->submit_lock); 1783e974f91cSConrad Meyer 1784bf8553eaSConrad Meyer newring = ioat_prealloc_ring(ioat, 1 << (order - 1), FALSE, 1785bf8553eaSConrad Meyer M_NOWAIT); 1786bf8553eaSConrad Meyer 1787bf8553eaSConrad Meyer mtx_lock(&ioat->submit_lock); 1788bf8553eaSConrad Meyer KASSERT(ioat->ring_size_order == order, 1789bf8553eaSConrad Meyer ("resize_pending protects order")); 1790bf8553eaSConrad Meyer 1791c62db395SConrad Meyer if (newring != NULL && !ioat->is_completion_pending) 1792bf8553eaSConrad Meyer ring_shrink(ioat, order, newring); 1793c62db395SConrad Meyer else if (newring != NULL) 1794c62db395SConrad Meyer ioat_free_ring(ioat, (1 << (order - 1)), newring); 1795bf8553eaSConrad Meyer 1796bf8553eaSConrad Meyer ioat->is_resize_pending = FALSE; 1797bf8553eaSConrad Meyer mtx_unlock(&ioat->submit_lock); 1798bf8553eaSConrad Meyer 1799bf8553eaSConrad Meyer out: 1800e974f91cSConrad Meyer if (ioat->ring_size_order > IOAT_MIN_ORDER) 1801db16ab7eSConrad Meyer callout_reset(&ioat->shrink_timer, IOAT_SHRINK_PERIOD, 18025ac77963SConrad Meyer ioat_shrink_timer_callback, ioat); 1803e974f91cSConrad Meyer } 1804e974f91cSConrad Meyer 1805e974f91cSConrad Meyer /* 1806e974f91cSConrad Meyer * Support Functions 1807e974f91cSConrad Meyer */ 1808e974f91cSConrad Meyer static void 1809e974f91cSConrad Meyer ioat_submit_single(struct ioat_softc *ioat) 1810e974f91cSConrad Meyer { 1811e974f91cSConrad Meyer 18125a8af401SConrad Meyer mtx_assert(&ioat->submit_lock, MA_OWNED); 18135a8af401SConrad Meyer 1814466b3540SConrad Meyer ioat_get(ioat, IOAT_ACTIVE_DESCR_REF); 1815e974f91cSConrad Meyer atomic_add_rel_int(&ioat->head, 1); 1816bf8553eaSConrad Meyer atomic_add_rel_int(&ioat->hw_head, 1); 181705e4cffbSConrad Meyer CTR5(KTR_IOAT, "%s channel=%u head=%u hw_head=%u tail=%u", __func__, 181805e4cffbSConrad Meyer ioat->chan_idx, ioat->head, ioat->hw_head & UINT16_MAX, 181905e4cffbSConrad Meyer ioat->tail); 1820e974f91cSConrad Meyer 182101fbbc88SConrad Meyer ioat->stats.descriptors_submitted++; 1822e974f91cSConrad Meyer } 1823e974f91cSConrad Meyer 1824e974f91cSConrad Meyer static int 1825e974f91cSConrad Meyer ioat_reset_hw(struct ioat_softc *ioat) 1826e974f91cSConrad Meyer { 1827e974f91cSConrad Meyer uint64_t status; 1828e974f91cSConrad Meyer uint32_t chanerr; 1829cea5b880SConrad Meyer unsigned timeout; 18305f77bd3eSConrad Meyer int error; 18315f77bd3eSConrad Meyer 1832520f6023SConrad Meyer CTR2(KTR_IOAT, "%s channel=%u", __func__, ioat->chan_idx); 18336d41e1edSConrad Meyer 18345f77bd3eSConrad Meyer mtx_lock(IOAT_REFLK); 183593f7f84aSConrad Meyer while (ioat->resetting && !ioat->destroying) 183693f7f84aSConrad Meyer msleep(&ioat->resetting, IOAT_REFLK, 0, "IRH_drain", 0); 183793f7f84aSConrad Meyer if (ioat->destroying) { 183893f7f84aSConrad Meyer mtx_unlock(IOAT_REFLK); 183993f7f84aSConrad Meyer return (ENXIO); 184093f7f84aSConrad Meyer } 184193f7f84aSConrad Meyer ioat->resetting = TRUE; 184293f7f84aSConrad Meyer 18435f77bd3eSConrad Meyer ioat->quiescing = TRUE; 18445f77bd3eSConrad Meyer ioat_drain_locked(ioat); 18455f77bd3eSConrad Meyer mtx_unlock(IOAT_REFLK); 1846e974f91cSConrad Meyer 1847fe8712f8SConrad Meyer /* 1848fe8712f8SConrad Meyer * Suspend ioat_process_events while the hardware and softc are in an 1849fe8712f8SConrad Meyer * indeterminate state. 1850fe8712f8SConrad Meyer */ 1851fe8712f8SConrad Meyer mtx_lock(&ioat->cleanup_lock); 1852fe8712f8SConrad Meyer ioat->resetting_cleanup = TRUE; 1853fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 1854fe8712f8SConrad Meyer 1855ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u quiesced and drained", __func__, 1856ee5642bbSConrad Meyer ioat->chan_idx); 1857ee5642bbSConrad Meyer 1858e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1859e974f91cSConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) 1860e974f91cSConrad Meyer ioat_suspend(ioat); 1861e974f91cSConrad Meyer 1862e974f91cSConrad Meyer /* Wait at most 20 ms */ 1863e974f91cSConrad Meyer for (timeout = 0; (is_ioat_active(status) || is_ioat_idle(status)) && 1864e974f91cSConrad Meyer timeout < 20; timeout++) { 1865e974f91cSConrad Meyer DELAY(1000); 1866e974f91cSConrad Meyer status = ioat_get_chansts(ioat); 1867e974f91cSConrad Meyer } 18685f77bd3eSConrad Meyer if (timeout == 20) { 18695f77bd3eSConrad Meyer error = ETIMEDOUT; 18705f77bd3eSConrad Meyer goto out; 18715f77bd3eSConrad Meyer } 1872e974f91cSConrad Meyer 1873cea5b880SConrad Meyer KASSERT(ioat_get_active(ioat) == 0, ("active after quiesce")); 1874cea5b880SConrad Meyer 1875e974f91cSConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 1876e974f91cSConrad Meyer ioat_write_4(ioat, IOAT_CHANERR_OFFSET, chanerr); 1877e974f91cSConrad Meyer 1878ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u hardware suspended", __func__, 1879ee5642bbSConrad Meyer ioat->chan_idx); 1880ee5642bbSConrad Meyer 1881e974f91cSConrad Meyer /* 1882e974f91cSConrad Meyer * IOAT v3 workaround - CHANERRMSK_INT with 3E07h to masks out errors 1883e974f91cSConrad Meyer * that can cause stability issues for IOAT v3. 1884e974f91cSConrad Meyer */ 1885e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERRMASK_INT_OFFSET, 0x3e07, 1886e974f91cSConrad Meyer 4); 1887e974f91cSConrad Meyer chanerr = pci_read_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, 4); 1888e974f91cSConrad Meyer pci_write_config(ioat->device, IOAT_CFG_CHANERR_INT_OFFSET, chanerr, 4); 1889e974f91cSConrad Meyer 18900d1a05d9SConrad Meyer /* 18910d1a05d9SConrad Meyer * BDXDE and BWD models reset MSI-X registers on device reset. 18920d1a05d9SConrad Meyer * Save/restore their contents manually. 18930d1a05d9SConrad Meyer */ 1894f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1895f7157235SConrad Meyer ioat_log_message(1, "device resets MSI-X registers; saving\n"); 18960d1a05d9SConrad Meyer pci_save_state(ioat->device); 1897f7157235SConrad Meyer } 18980d1a05d9SConrad Meyer 1899e974f91cSConrad Meyer ioat_reset(ioat); 1900ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u hardware reset", __func__, 1901ee5642bbSConrad Meyer ioat->chan_idx); 1902e974f91cSConrad Meyer 1903e974f91cSConrad Meyer /* Wait at most 20 ms */ 1904e974f91cSConrad Meyer for (timeout = 0; ioat_reset_pending(ioat) && timeout < 20; timeout++) 1905e974f91cSConrad Meyer DELAY(1000); 19065f77bd3eSConrad Meyer if (timeout == 20) { 19075f77bd3eSConrad Meyer error = ETIMEDOUT; 19085f77bd3eSConrad Meyer goto out; 19095f77bd3eSConrad Meyer } 1910e974f91cSConrad Meyer 1911f7157235SConrad Meyer if (ioat_model_resets_msix(ioat)) { 1912f7157235SConrad Meyer ioat_log_message(1, "device resets registers; restored\n"); 19130d1a05d9SConrad Meyer pci_restore_state(ioat->device); 1914f7157235SConrad Meyer } 19154253ea50SConrad Meyer 1916cea5b880SConrad Meyer /* Reset attempts to return the hardware to "halted." */ 1917cea5b880SConrad Meyer status = ioat_get_chansts(ioat); 1918cea5b880SConrad Meyer if (is_ioat_active(status) || is_ioat_idle(status)) { 1919cea5b880SConrad Meyer /* So this really shouldn't happen... */ 1920cea5b880SConrad Meyer ioat_log_message(0, "Device is active after a reset?\n"); 1921cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 19225f77bd3eSConrad Meyer error = 0; 19235f77bd3eSConrad Meyer goto out; 1924e974f91cSConrad Meyer } 1925e974f91cSConrad Meyer 1926cea5b880SConrad Meyer chanerr = ioat_read_4(ioat, IOAT_CHANERR_OFFSET); 19275f77bd3eSConrad Meyer if (chanerr != 0) { 1928faefad9cSConrad Meyer mtx_lock(&ioat->cleanup_lock); 1929faefad9cSConrad Meyer ioat_halted_debug(ioat, chanerr); 1930faefad9cSConrad Meyer mtx_unlock(&ioat->cleanup_lock); 19315f77bd3eSConrad Meyer error = EIO; 19325f77bd3eSConrad Meyer goto out; 19335f77bd3eSConrad Meyer } 1934cea5b880SConrad Meyer 1935cea5b880SConrad Meyer /* 1936cea5b880SConrad Meyer * Bring device back online after reset. Writing CHAINADDR brings the 1937cea5b880SConrad Meyer * device back to active. 1938cea5b880SConrad Meyer * 1939cea5b880SConrad Meyer * The internal ring counter resets to zero, so we have to start over 1940cea5b880SConrad Meyer * at zero as well. 1941cea5b880SConrad Meyer */ 1942bf8553eaSConrad Meyer ioat->tail = ioat->head = ioat->hw_head = 0; 1943cea5b880SConrad Meyer ioat->last_seen = 0; 1944fe8712f8SConrad Meyer *ioat->comp_update = 0; 1945c114e741SConrad Meyer KASSERT(!ioat->is_completion_pending, ("bogus completion_pending")); 1946cea5b880SConrad Meyer 1947cea5b880SConrad Meyer ioat_write_chanctrl(ioat, IOAT_CHANCTRL_RUN); 1948cea5b880SConrad Meyer ioat_write_chancmp(ioat, ioat->comp_update_bus_addr); 1949cea5b880SConrad Meyer ioat_write_chainaddr(ioat, ioat->ring[0]->hw_desc_bus_addr); 19505f77bd3eSConrad Meyer error = 0; 1951ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u configured channel", __func__, 1952ee5642bbSConrad Meyer ioat->chan_idx); 19535f77bd3eSConrad Meyer 19545f77bd3eSConrad Meyer out: 19551bbc06b8SConrad Meyer /* Enqueues a null operation and ensures it completes. */ 1956ee5642bbSConrad Meyer if (error == 0) { 19571bbc06b8SConrad Meyer error = ioat_start_channel(ioat); 1958ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u started channel", __func__, 1959ee5642bbSConrad Meyer ioat->chan_idx); 1960ee5642bbSConrad Meyer } 19611bbc06b8SConrad Meyer 1962fe8712f8SConrad Meyer /* 1963fe8712f8SConrad Meyer * Resume completions now that ring state is consistent. 1964fe8712f8SConrad Meyer */ 1965fe8712f8SConrad Meyer mtx_lock(&ioat->cleanup_lock); 1966fe8712f8SConrad Meyer ioat->resetting_cleanup = FALSE; 1967fe8712f8SConrad Meyer mtx_unlock(&ioat->cleanup_lock); 196893f7f84aSConrad Meyer 1969fe8712f8SConrad Meyer /* Unblock submission of new work */ 1970fe8712f8SConrad Meyer mtx_lock(IOAT_REFLK); 1971fe8712f8SConrad Meyer ioat->quiescing = FALSE; 1972fe8712f8SConrad Meyer wakeup(&ioat->quiescing); 1973fe8712f8SConrad Meyer 1974fe8712f8SConrad Meyer ioat->resetting = FALSE; 1975fe8712f8SConrad Meyer wakeup(&ioat->resetting); 19761bbc06b8SConrad Meyer 19771bbc06b8SConrad Meyer if (ioat->is_completion_pending) 19781bbc06b8SConrad Meyer callout_reset(&ioat->poll_timer, 1, ioat_poll_timer_callback, 19791bbc06b8SConrad Meyer ioat); 1980ee5642bbSConrad Meyer CTR2(KTR_IOAT, "%s channel=%u reset done", __func__, ioat->chan_idx); 1981fe8712f8SConrad Meyer mtx_unlock(IOAT_REFLK); 1982fe8712f8SConrad Meyer 19835f77bd3eSConrad Meyer return (error); 1984cea5b880SConrad Meyer } 1985cea5b880SConrad Meyer 1986f7157235SConrad Meyer static int 1987faefad9cSConrad Meyer sysctl_handle_chansts(SYSCTL_HANDLER_ARGS) 1988faefad9cSConrad Meyer { 1989faefad9cSConrad Meyer struct ioat_softc *ioat; 1990faefad9cSConrad Meyer struct sbuf sb; 1991faefad9cSConrad Meyer uint64_t status; 1992faefad9cSConrad Meyer int error; 1993faefad9cSConrad Meyer 1994faefad9cSConrad Meyer ioat = arg1; 1995faefad9cSConrad Meyer 1996faefad9cSConrad Meyer status = ioat_get_chansts(ioat) & IOAT_CHANSTS_STATUS; 1997faefad9cSConrad Meyer 1998faefad9cSConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 256, req); 1999faefad9cSConrad Meyer switch (status) { 2000faefad9cSConrad Meyer case IOAT_CHANSTS_ACTIVE: 2001faefad9cSConrad Meyer sbuf_printf(&sb, "ACTIVE"); 2002faefad9cSConrad Meyer break; 2003faefad9cSConrad Meyer case IOAT_CHANSTS_IDLE: 2004faefad9cSConrad Meyer sbuf_printf(&sb, "IDLE"); 2005faefad9cSConrad Meyer break; 2006faefad9cSConrad Meyer case IOAT_CHANSTS_SUSPENDED: 2007faefad9cSConrad Meyer sbuf_printf(&sb, "SUSPENDED"); 2008faefad9cSConrad Meyer break; 2009faefad9cSConrad Meyer case IOAT_CHANSTS_HALTED: 2010faefad9cSConrad Meyer sbuf_printf(&sb, "HALTED"); 2011faefad9cSConrad Meyer break; 2012faefad9cSConrad Meyer case IOAT_CHANSTS_ARMED: 2013faefad9cSConrad Meyer sbuf_printf(&sb, "ARMED"); 2014faefad9cSConrad Meyer break; 2015faefad9cSConrad Meyer default: 2016faefad9cSConrad Meyer sbuf_printf(&sb, "UNKNOWN"); 2017faefad9cSConrad Meyer break; 2018faefad9cSConrad Meyer } 2019faefad9cSConrad Meyer error = sbuf_finish(&sb); 2020faefad9cSConrad Meyer sbuf_delete(&sb); 2021faefad9cSConrad Meyer 2022faefad9cSConrad Meyer if (error != 0 || req->newptr == NULL) 2023faefad9cSConrad Meyer return (error); 2024faefad9cSConrad Meyer return (EINVAL); 2025faefad9cSConrad Meyer } 2026faefad9cSConrad Meyer 2027faefad9cSConrad Meyer static int 202801fbbc88SConrad Meyer sysctl_handle_dpi(SYSCTL_HANDLER_ARGS) 202901fbbc88SConrad Meyer { 203001fbbc88SConrad Meyer struct ioat_softc *ioat; 203101fbbc88SConrad Meyer struct sbuf sb; 203201fbbc88SConrad Meyer #define PRECISION "1" 203301fbbc88SConrad Meyer const uintmax_t factor = 10; 203401fbbc88SConrad Meyer uintmax_t rate; 203501fbbc88SConrad Meyer int error; 203601fbbc88SConrad Meyer 203701fbbc88SConrad Meyer ioat = arg1; 203801fbbc88SConrad Meyer sbuf_new_for_sysctl(&sb, NULL, 16, req); 203901fbbc88SConrad Meyer 204001fbbc88SConrad Meyer if (ioat->stats.interrupts == 0) { 204101fbbc88SConrad Meyer sbuf_printf(&sb, "NaN"); 204201fbbc88SConrad Meyer goto out; 204301fbbc88SConrad Meyer } 204401fbbc88SConrad Meyer rate = ioat->stats.descriptors_processed * factor / 204501fbbc88SConrad Meyer ioat->stats.interrupts; 204601fbbc88SConrad Meyer sbuf_printf(&sb, "%ju.%." PRECISION "ju", rate / factor, 204701fbbc88SConrad Meyer rate % factor); 204801fbbc88SConrad Meyer #undef PRECISION 204901fbbc88SConrad Meyer out: 205001fbbc88SConrad Meyer error = sbuf_finish(&sb); 205101fbbc88SConrad Meyer sbuf_delete(&sb); 205201fbbc88SConrad Meyer if (error != 0 || req->newptr == NULL) 205301fbbc88SConrad Meyer return (error); 205401fbbc88SConrad Meyer return (EINVAL); 205501fbbc88SConrad Meyer } 205601fbbc88SConrad Meyer 205701fbbc88SConrad Meyer static int 2058f7157235SConrad Meyer sysctl_handle_reset(SYSCTL_HANDLER_ARGS) 2059f7157235SConrad Meyer { 2060f7157235SConrad Meyer struct ioat_softc *ioat; 2061f7157235SConrad Meyer int error, arg; 2062f7157235SConrad Meyer 2063f7157235SConrad Meyer ioat = arg1; 2064f7157235SConrad Meyer 2065f7157235SConrad Meyer arg = 0; 2066f7157235SConrad Meyer error = SYSCTL_OUT(req, &arg, sizeof(arg)); 2067f7157235SConrad Meyer if (error != 0 || req->newptr == NULL) 2068f7157235SConrad Meyer return (error); 2069f7157235SConrad Meyer 2070f7157235SConrad Meyer error = SYSCTL_IN(req, &arg, sizeof(arg)); 2071f7157235SConrad Meyer if (error != 0) 2072f7157235SConrad Meyer return (error); 2073f7157235SConrad Meyer 2074f7157235SConrad Meyer if (arg != 0) 2075f7157235SConrad Meyer error = ioat_reset_hw(ioat); 2076f7157235SConrad Meyer 2077f7157235SConrad Meyer return (error); 2078f7157235SConrad Meyer } 2079f7157235SConrad Meyer 2080e974f91cSConrad Meyer static void 2081e974f91cSConrad Meyer dump_descriptor(void *hw_desc) 2082e974f91cSConrad Meyer { 2083e974f91cSConrad Meyer int i, j; 2084e974f91cSConrad Meyer 2085e974f91cSConrad Meyer for (i = 0; i < 2; i++) { 2086e974f91cSConrad Meyer for (j = 0; j < 8; j++) 2087e974f91cSConrad Meyer printf("%08x ", ((uint32_t *)hw_desc)[i * 8 + j]); 2088e974f91cSConrad Meyer printf("\n"); 2089e974f91cSConrad Meyer } 2090e974f91cSConrad Meyer } 2091e974f91cSConrad Meyer 2092e974f91cSConrad Meyer static void 2093e974f91cSConrad Meyer ioat_setup_sysctl(device_t device) 2094e974f91cSConrad Meyer { 209501fbbc88SConrad Meyer struct sysctl_oid_list *par, *statpar, *state, *hammer; 2096f7157235SConrad Meyer struct sysctl_ctx_list *ctx; 209701fbbc88SConrad Meyer struct sysctl_oid *tree, *tmp; 2098e974f91cSConrad Meyer struct ioat_softc *ioat; 2099e974f91cSConrad Meyer 2100e974f91cSConrad Meyer ioat = DEVICE2SOFTC(device); 2101f7157235SConrad Meyer ctx = device_get_sysctl_ctx(device); 2102f7157235SConrad Meyer tree = device_get_sysctl_tree(device); 2103f7157235SConrad Meyer par = SYSCTL_CHILDREN(tree); 2104e974f91cSConrad Meyer 210565e4f8adSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "version", CTLFLAG_RD, 210665e4f8adSConrad Meyer &ioat->version, 0, "HW version (0xMM form)"); 210765e4f8adSConrad Meyer SYSCTL_ADD_UINT(ctx, par, OID_AUTO, "max_xfer_size", CTLFLAG_RD, 210865e4f8adSConrad Meyer &ioat->max_xfer_size, 0, "HW maximum transfer size"); 21095ca9fc2aSConrad Meyer SYSCTL_ADD_INT(ctx, par, OID_AUTO, "intrdelay_supported", CTLFLAG_RD, 21105ca9fc2aSConrad Meyer &ioat->intrdelay_supported, 0, "Is INTRDELAY supported"); 21115ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, par, OID_AUTO, "intrdelay_max", CTLFLAG_RD, 21125ca9fc2aSConrad Meyer &ioat->intrdelay_max, 0, 21135ca9fc2aSConrad Meyer "Maximum configurable INTRDELAY on this channel (microseconds)"); 211465e4f8adSConrad Meyer 211501fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "state", CTLFLAG_RD, NULL, 211601fbbc88SConrad Meyer "IOAT channel internal state"); 211701fbbc88SConrad Meyer state = SYSCTL_CHILDREN(tmp); 211801fbbc88SConrad Meyer 211901fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "ring_size_order", CTLFLAG_RD, 2120bf8553eaSConrad Meyer &ioat->ring_size_order, 0, "SW descriptor ring size order"); 212101fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "head", CTLFLAG_RD, &ioat->head, 212201fbbc88SConrad Meyer 0, "SW descriptor head pointer index"); 212301fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "tail", CTLFLAG_RD, &ioat->tail, 212401fbbc88SConrad Meyer 0, "SW descriptor tail pointer index"); 212501fbbc88SConrad Meyer SYSCTL_ADD_UINT(ctx, state, OID_AUTO, "hw_head", CTLFLAG_RD, 2126bf8553eaSConrad Meyer &ioat->hw_head, 0, "HW DMACOUNT"); 2127f7157235SConrad Meyer 212801fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, state, OID_AUTO, "last_completion", CTLFLAG_RD, 212965e4f8adSConrad Meyer ioat->comp_update, "HW addr of last completion"); 213065e4f8adSConrad Meyer 213101fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_resize_pending", CTLFLAG_RD, 213265e4f8adSConrad Meyer &ioat->is_resize_pending, 0, "resize pending"); 213325ad9585SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_submitter_processing", 213425ad9585SConrad Meyer CTLFLAG_RD, &ioat->is_submitter_processing, 0, 213525ad9585SConrad Meyer "submitter processing"); 213601fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_completion_pending", 213701fbbc88SConrad Meyer CTLFLAG_RD, &ioat->is_completion_pending, 0, "completion pending"); 213801fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_reset_pending", CTLFLAG_RD, 213965e4f8adSConrad Meyer &ioat->is_reset_pending, 0, "reset pending"); 214001fbbc88SConrad Meyer SYSCTL_ADD_INT(ctx, state, OID_AUTO, "is_channel_running", CTLFLAG_RD, 214165e4f8adSConrad Meyer &ioat->is_channel_running, 0, "channel running"); 214265e4f8adSConrad Meyer 214301fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, state, OID_AUTO, "chansts", 2144faefad9cSConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_chansts, "A", 2145faefad9cSConrad Meyer "String of the channel status"); 214601fbbc88SConrad Meyer 21475ca9fc2aSConrad Meyer SYSCTL_ADD_U16(ctx, state, OID_AUTO, "intrdelay", CTLFLAG_RD, 21485ca9fc2aSConrad Meyer &ioat->cached_intrdelay, 0, 21495ca9fc2aSConrad Meyer "Current INTRDELAY on this channel (cached, microseconds)"); 21505ca9fc2aSConrad Meyer 215101fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "hammer", CTLFLAG_RD, NULL, 215201fbbc88SConrad Meyer "Big hammers (mostly for testing)"); 215301fbbc88SConrad Meyer hammer = SYSCTL_CHILDREN(tmp); 215401fbbc88SConrad Meyer 215501fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, hammer, OID_AUTO, "force_hw_reset", 215601fbbc88SConrad Meyer CTLTYPE_INT | CTLFLAG_RW, ioat, 0, sysctl_handle_reset, "I", 215701fbbc88SConrad Meyer "Set to non-zero to reset the hardware"); 215801fbbc88SConrad Meyer 215901fbbc88SConrad Meyer tmp = SYSCTL_ADD_NODE(ctx, par, OID_AUTO, "stats", CTLFLAG_RD, NULL, 216001fbbc88SConrad Meyer "IOAT channel statistics"); 216101fbbc88SConrad Meyer statpar = SYSCTL_CHILDREN(tmp); 216201fbbc88SConrad Meyer 216301fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "interrupts", CTLFLAG_RW, 216401fbbc88SConrad Meyer &ioat->stats.interrupts, 216501fbbc88SConrad Meyer "Number of interrupts processed on this channel"); 216601fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "descriptors", CTLFLAG_RW, 216701fbbc88SConrad Meyer &ioat->stats.descriptors_processed, 216801fbbc88SConrad Meyer "Number of descriptors processed on this channel"); 216901fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "submitted", CTLFLAG_RW, 217001fbbc88SConrad Meyer &ioat->stats.descriptors_submitted, 217101fbbc88SConrad Meyer "Number of descriptors submitted to this channel"); 217201fbbc88SConrad Meyer SYSCTL_ADD_UQUAD(ctx, statpar, OID_AUTO, "errored", CTLFLAG_RW, 217301fbbc88SConrad Meyer &ioat->stats.descriptors_error, 217401fbbc88SConrad Meyer "Number of descriptors failed by channel errors"); 217501fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "halts", CTLFLAG_RW, 217601fbbc88SConrad Meyer &ioat->stats.channel_halts, 0, 217701fbbc88SConrad Meyer "Number of times the channel has halted"); 217801fbbc88SConrad Meyer SYSCTL_ADD_U32(ctx, statpar, OID_AUTO, "last_halt_chanerr", CTLFLAG_RW, 217901fbbc88SConrad Meyer &ioat->stats.last_halt_chanerr, 0, 218001fbbc88SConrad Meyer "The raw CHANERR when the channel was last halted"); 218101fbbc88SConrad Meyer 218201fbbc88SConrad Meyer SYSCTL_ADD_PROC(ctx, statpar, OID_AUTO, "desc_per_interrupt", 218301fbbc88SConrad Meyer CTLTYPE_STRING | CTLFLAG_RD, ioat, 0, sysctl_handle_dpi, "A", 218401fbbc88SConrad Meyer "Descriptors per interrupt"); 2185e974f91cSConrad Meyer } 2186466b3540SConrad Meyer 2187466b3540SConrad Meyer static inline struct ioat_softc * 2188466b3540SConrad Meyer ioat_get(struct ioat_softc *ioat, enum ioat_ref_kind kind) 2189466b3540SConrad Meyer { 2190466b3540SConrad Meyer uint32_t old; 2191466b3540SConrad Meyer 2192466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 2193466b3540SConrad Meyer 2194466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, 1); 2195466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt overflow")); 2196466b3540SConrad Meyer 2197466b3540SConrad Meyer #ifdef INVARIANTS 2198466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], 1); 2199466b3540SConrad Meyer KASSERT(old < UINT32_MAX, ("refcnt kind overflow")); 2200466b3540SConrad Meyer #endif 2201466b3540SConrad Meyer 2202466b3540SConrad Meyer return (ioat); 2203466b3540SConrad Meyer } 2204466b3540SConrad Meyer 2205466b3540SConrad Meyer static inline void 2206466b3540SConrad Meyer ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 2207466b3540SConrad Meyer { 2208faefad9cSConrad Meyer 2209faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, FALSE); 2210faefad9cSConrad Meyer } 2211faefad9cSConrad Meyer 2212faefad9cSConrad Meyer static inline void 2213faefad9cSConrad Meyer ioat_putn_locked(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind) 2214faefad9cSConrad Meyer { 2215faefad9cSConrad Meyer 2216faefad9cSConrad Meyer _ioat_putn(ioat, n, kind, TRUE); 2217faefad9cSConrad Meyer } 2218faefad9cSConrad Meyer 2219faefad9cSConrad Meyer static inline void 2220faefad9cSConrad Meyer _ioat_putn(struct ioat_softc *ioat, uint32_t n, enum ioat_ref_kind kind, 2221faefad9cSConrad Meyer boolean_t locked) 2222faefad9cSConrad Meyer { 2223466b3540SConrad Meyer uint32_t old; 2224466b3540SConrad Meyer 2225466b3540SConrad Meyer KASSERT(kind < IOAT_NUM_REF_KINDS, ("bogus")); 2226466b3540SConrad Meyer 2227466b3540SConrad Meyer if (n == 0) 2228466b3540SConrad Meyer return; 2229466b3540SConrad Meyer 2230466b3540SConrad Meyer #ifdef INVARIANTS 2231466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refkinds[kind], -n); 2232466b3540SConrad Meyer KASSERT(old >= n, ("refcnt kind underflow")); 2233466b3540SConrad Meyer #endif 2234466b3540SConrad Meyer 2235466b3540SConrad Meyer /* Skip acquiring the lock if resulting refcnt > 0. */ 2236466b3540SConrad Meyer for (;;) { 2237466b3540SConrad Meyer old = ioat->refcnt; 2238466b3540SConrad Meyer if (old <= n) 2239466b3540SConrad Meyer break; 2240466b3540SConrad Meyer if (atomic_cmpset_32(&ioat->refcnt, old, old - n)) 2241466b3540SConrad Meyer return; 2242466b3540SConrad Meyer } 2243466b3540SConrad Meyer 2244faefad9cSConrad Meyer if (locked) 2245faefad9cSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 2246faefad9cSConrad Meyer else 2247466b3540SConrad Meyer mtx_lock(IOAT_REFLK); 2248faefad9cSConrad Meyer 2249466b3540SConrad Meyer old = atomic_fetchadd_32(&ioat->refcnt, -n); 2250466b3540SConrad Meyer KASSERT(old >= n, ("refcnt error")); 2251466b3540SConrad Meyer 2252466b3540SConrad Meyer if (old == n) 2253466b3540SConrad Meyer wakeup(IOAT_REFLK); 2254faefad9cSConrad Meyer if (!locked) 2255466b3540SConrad Meyer mtx_unlock(IOAT_REFLK); 2256466b3540SConrad Meyer } 2257466b3540SConrad Meyer 2258466b3540SConrad Meyer static inline void 2259466b3540SConrad Meyer ioat_put(struct ioat_softc *ioat, enum ioat_ref_kind kind) 2260466b3540SConrad Meyer { 2261466b3540SConrad Meyer 2262466b3540SConrad Meyer ioat_putn(ioat, 1, kind); 2263466b3540SConrad Meyer } 2264466b3540SConrad Meyer 2265466b3540SConrad Meyer static void 22665f77bd3eSConrad Meyer ioat_drain_locked(struct ioat_softc *ioat) 2267466b3540SConrad Meyer { 2268466b3540SConrad Meyer 22695f77bd3eSConrad Meyer mtx_assert(IOAT_REFLK, MA_OWNED); 2270466b3540SConrad Meyer while (ioat->refcnt > 0) 2271466b3540SConrad Meyer msleep(IOAT_REFLK, IOAT_REFLK, 0, "ioat_drain", 0); 2272466b3540SConrad Meyer } 22730bb35debSConrad Meyer 22740bb35debSConrad Meyer #ifdef DDB 22750bb35debSConrad Meyer #define _db_show_lock(lo) LOCK_CLASS(lo)->lc_ddb_show(lo) 22760bb35debSConrad Meyer #define db_show_lock(lk) _db_show_lock(&(lk)->lock_object) 22770bb35debSConrad Meyer DB_SHOW_COMMAND(ioat, db_show_ioat) 22780bb35debSConrad Meyer { 22790bb35debSConrad Meyer struct ioat_softc *sc; 22800bb35debSConrad Meyer unsigned idx; 22810bb35debSConrad Meyer 22820bb35debSConrad Meyer if (!have_addr) 22830bb35debSConrad Meyer goto usage; 22840bb35debSConrad Meyer idx = (unsigned)addr; 228535b7a45eSConrad Meyer if (idx >= ioat_channel_index) 22860bb35debSConrad Meyer goto usage; 22870bb35debSConrad Meyer 22880bb35debSConrad Meyer sc = ioat_channel[idx]; 22890bb35debSConrad Meyer db_printf("ioat softc at %p\n", sc); 22900bb35debSConrad Meyer if (sc == NULL) 22910bb35debSConrad Meyer return; 22920bb35debSConrad Meyer 22930bb35debSConrad Meyer db_printf(" version: %d\n", sc->version); 22940bb35debSConrad Meyer db_printf(" chan_idx: %u\n", sc->chan_idx); 22950bb35debSConrad Meyer db_printf(" submit_lock: "); 22960bb35debSConrad Meyer db_show_lock(&sc->submit_lock); 22970bb35debSConrad Meyer 22980bb35debSConrad Meyer db_printf(" capabilities: %b\n", (int)sc->capabilities, 22990bb35debSConrad Meyer IOAT_DMACAP_STR); 23000bb35debSConrad Meyer db_printf(" cached_intrdelay: %u\n", sc->cached_intrdelay); 23010bb35debSConrad Meyer db_printf(" *comp_update: 0x%jx\n", (uintmax_t)*sc->comp_update); 23020bb35debSConrad Meyer 23035ac77963SConrad Meyer db_printf(" poll_timer:\n"); 23045ac77963SConrad Meyer db_printf(" c_time: %ju\n", (uintmax_t)sc->poll_timer.c_time); 23055ac77963SConrad Meyer db_printf(" c_arg: %p\n", sc->poll_timer.c_arg); 23065ac77963SConrad Meyer db_printf(" c_func: %p\n", sc->poll_timer.c_func); 23075ac77963SConrad Meyer db_printf(" c_lock: %p\n", sc->poll_timer.c_lock); 23085ac77963SConrad Meyer db_printf(" c_flags: 0x%x\n", (unsigned)sc->poll_timer.c_flags); 23095ac77963SConrad Meyer 23105ac77963SConrad Meyer db_printf(" shrink_timer:\n"); 23115ac77963SConrad Meyer db_printf(" c_time: %ju\n", (uintmax_t)sc->shrink_timer.c_time); 23125ac77963SConrad Meyer db_printf(" c_arg: %p\n", sc->shrink_timer.c_arg); 23135ac77963SConrad Meyer db_printf(" c_func: %p\n", sc->shrink_timer.c_func); 23145ac77963SConrad Meyer db_printf(" c_lock: %p\n", sc->shrink_timer.c_lock); 23155ac77963SConrad Meyer db_printf(" c_flags: 0x%x\n", (unsigned)sc->shrink_timer.c_flags); 23160bb35debSConrad Meyer 23170bb35debSConrad Meyer db_printf(" quiescing: %d\n", (int)sc->quiescing); 23180bb35debSConrad Meyer db_printf(" destroying: %d\n", (int)sc->destroying); 23190bb35debSConrad Meyer db_printf(" is_resize_pending: %d\n", (int)sc->is_resize_pending); 232025ad9585SConrad Meyer db_printf(" is_submitter_processing: %d\n", 232125ad9585SConrad Meyer (int)sc->is_submitter_processing); 23220bb35debSConrad Meyer db_printf(" is_completion_pending: %d\n", (int)sc->is_completion_pending); 23230bb35debSConrad Meyer db_printf(" is_reset_pending: %d\n", (int)sc->is_reset_pending); 23240bb35debSConrad Meyer db_printf(" is_channel_running: %d\n", (int)sc->is_channel_running); 23250bb35debSConrad Meyer db_printf(" intrdelay_supported: %d\n", (int)sc->intrdelay_supported); 232693f7f84aSConrad Meyer db_printf(" resetting: %d\n", (int)sc->resetting); 23270bb35debSConrad Meyer 23280bb35debSConrad Meyer db_printf(" head: %u\n", sc->head); 23290bb35debSConrad Meyer db_printf(" tail: %u\n", sc->tail); 23300bb35debSConrad Meyer db_printf(" hw_head: %u\n", sc->hw_head); 23310bb35debSConrad Meyer db_printf(" ring_size_order: %u\n", sc->ring_size_order); 23320bb35debSConrad Meyer db_printf(" last_seen: 0x%lx\n", sc->last_seen); 23330bb35debSConrad Meyer db_printf(" ring: %p\n", sc->ring); 23340bb35debSConrad Meyer 233535b7a45eSConrad Meyer db_printf(" ring[%u] (tail):\n", sc->tail % 233635b7a45eSConrad Meyer (1 << sc->ring_size_order)); 233735b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->tail)->id); 233835b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 233935b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->tail)->hw_desc_bus_addr); 234035b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 234135b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->tail)->u.generic->next); 234235b7a45eSConrad Meyer 234335b7a45eSConrad Meyer db_printf(" ring[%u] (head - 1):\n", (sc->head - 1) % 234435b7a45eSConrad Meyer (1 << sc->ring_size_order)); 234535b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head - 1)->id); 234635b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 234735b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->head - 1)->hw_desc_bus_addr); 234835b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 234935b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->head - 1)->u.generic->next); 235035b7a45eSConrad Meyer 235135b7a45eSConrad Meyer db_printf(" ring[%u] (head):\n", (sc->head) % 235235b7a45eSConrad Meyer (1 << sc->ring_size_order)); 235335b7a45eSConrad Meyer db_printf(" id: %u\n", ioat_get_ring_entry(sc, sc->head)->id); 235435b7a45eSConrad Meyer db_printf(" addr: 0x%lx\n", 235535b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->head)->hw_desc_bus_addr); 235635b7a45eSConrad Meyer db_printf(" next: 0x%lx\n", 235735b7a45eSConrad Meyer ioat_get_ring_entry(sc, sc->head)->u.generic->next); 235835b7a45eSConrad Meyer 235935b7a45eSConrad Meyer for (idx = 0; idx < (1 << sc->ring_size_order); idx++) 236035b7a45eSConrad Meyer if ((*sc->comp_update & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_MASK) 236135b7a45eSConrad Meyer == ioat_get_ring_entry(sc, idx)->hw_desc_bus_addr) 236235b7a45eSConrad Meyer db_printf(" ring[%u] == hardware tail\n", idx); 236335b7a45eSConrad Meyer 23640bb35debSConrad Meyer db_printf(" cleanup_lock: "); 23650bb35debSConrad Meyer db_show_lock(&sc->cleanup_lock); 23660bb35debSConrad Meyer 23670bb35debSConrad Meyer db_printf(" refcnt: %u\n", sc->refcnt); 23680bb35debSConrad Meyer #ifdef INVARIANTS 23690bb35debSConrad Meyer CTASSERT(IOAT_NUM_REF_KINDS == 2); 23700bb35debSConrad Meyer db_printf(" refkinds: [ENG=%u, DESCR=%u]\n", sc->refkinds[0], 23710bb35debSConrad Meyer sc->refkinds[1]); 23720bb35debSConrad Meyer #endif 23730bb35debSConrad Meyer db_printf(" stats:\n"); 23740bb35debSConrad Meyer db_printf(" interrupts: %lu\n", sc->stats.interrupts); 23750bb35debSConrad Meyer db_printf(" descriptors_processed: %lu\n", sc->stats.descriptors_processed); 23760bb35debSConrad Meyer db_printf(" descriptors_error: %lu\n", sc->stats.descriptors_error); 23770bb35debSConrad Meyer db_printf(" descriptors_submitted: %lu\n", sc->stats.descriptors_submitted); 23780bb35debSConrad Meyer 23790bb35debSConrad Meyer db_printf(" channel_halts: %u\n", sc->stats.channel_halts); 23800bb35debSConrad Meyer db_printf(" last_halt_chanerr: %u\n", sc->stats.last_halt_chanerr); 23810bb35debSConrad Meyer 23820bb35debSConrad Meyer if (db_pager_quit) 23830bb35debSConrad Meyer return; 23840bb35debSConrad Meyer 23850bb35debSConrad Meyer db_printf(" hw status:\n"); 23860bb35debSConrad Meyer db_printf(" status: 0x%lx\n", ioat_get_chansts(sc)); 23870bb35debSConrad Meyer db_printf(" chanctrl: 0x%x\n", 23880bb35debSConrad Meyer (unsigned)ioat_read_2(sc, IOAT_CHANCTRL_OFFSET)); 23890bb35debSConrad Meyer db_printf(" chancmd: 0x%x\n", 23900bb35debSConrad Meyer (unsigned)ioat_read_1(sc, IOAT_CHANCMD_OFFSET)); 23910bb35debSConrad Meyer db_printf(" dmacount: 0x%x\n", 23920bb35debSConrad Meyer (unsigned)ioat_read_2(sc, IOAT_DMACOUNT_OFFSET)); 23930bb35debSConrad Meyer db_printf(" chainaddr: 0x%lx\n", 23940bb35debSConrad Meyer ioat_read_double_4(sc, IOAT_CHAINADDR_OFFSET_LOW)); 23950bb35debSConrad Meyer db_printf(" chancmp: 0x%lx\n", 23960bb35debSConrad Meyer ioat_read_double_4(sc, IOAT_CHANCMP_OFFSET_LOW)); 23970bb35debSConrad Meyer db_printf(" chanerr: %b\n", 23980bb35debSConrad Meyer (int)ioat_read_4(sc, IOAT_CHANERR_OFFSET), IOAT_CHANERR_STR); 23990bb35debSConrad Meyer return; 24000bb35debSConrad Meyer usage: 24010bb35debSConrad Meyer db_printf("usage: show ioat <0-%u>\n", ioat_channel_index); 24020bb35debSConrad Meyer return; 24030bb35debSConrad Meyer } 24040bb35debSConrad Meyer #endif /* DDB */ 2405