1c0f28ce6SDave Jiang /* 2c0f28ce6SDave Jiang * Intel I/OAT DMA Linux driver 3c0f28ce6SDave Jiang * Copyright(c) 2004 - 2015 Intel Corporation. 4c0f28ce6SDave Jiang * 5c0f28ce6SDave Jiang * This program is free software; you can redistribute it and/or modify it 6c0f28ce6SDave Jiang * under the terms and conditions of the GNU General Public License, 7c0f28ce6SDave Jiang * version 2, as published by the Free Software Foundation. 8c0f28ce6SDave Jiang * 9c0f28ce6SDave Jiang * This program is distributed in the hope that it will be useful, but WITHOUT 10c0f28ce6SDave Jiang * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11c0f28ce6SDave Jiang * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12c0f28ce6SDave Jiang * more details. 13c0f28ce6SDave Jiang * 14c0f28ce6SDave Jiang * The full GNU General Public License is included in this distribution in 15c0f28ce6SDave Jiang * the file called "COPYING". 16c0f28ce6SDave Jiang * 17c0f28ce6SDave Jiang */ 18c0f28ce6SDave Jiang 19c0f28ce6SDave Jiang #include <linux/init.h> 20c0f28ce6SDave Jiang #include <linux/module.h> 21c0f28ce6SDave Jiang #include <linux/slab.h> 22c0f28ce6SDave Jiang #include <linux/pci.h> 23c0f28ce6SDave Jiang #include <linux/interrupt.h> 24c0f28ce6SDave Jiang #include <linux/dmaengine.h> 25c0f28ce6SDave Jiang #include <linux/delay.h> 26c0f28ce6SDave Jiang #include <linux/dma-mapping.h> 27c0f28ce6SDave Jiang #include <linux/workqueue.h> 28c0f28ce6SDave Jiang #include <linux/prefetch.h> 29c0f28ce6SDave Jiang #include <linux/dca.h> 30c0f28ce6SDave Jiang #include "dma.h" 31c0f28ce6SDave Jiang #include "registers.h" 32c0f28ce6SDave Jiang #include "hw.h" 33c0f28ce6SDave Jiang 34c0f28ce6SDave Jiang #include "../dmaengine.h" 35c0f28ce6SDave Jiang 36c0f28ce6SDave Jiang MODULE_VERSION(IOAT_DMA_VERSION); 37c0f28ce6SDave Jiang MODULE_LICENSE("Dual BSD/GPL"); 38c0f28ce6SDave Jiang MODULE_AUTHOR("Intel Corporation"); 39c0f28ce6SDave Jiang 40c0f28ce6SDave Jiang static struct pci_device_id ioat_pci_tbl[] = { 41c0f28ce6SDave Jiang /* I/OAT v3 platforms */ 42c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG0) }, 43c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG1) }, 44c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG2) }, 45c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG3) }, 46c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG4) }, 47c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG5) }, 48c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG6) }, 49c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG7) }, 50c0f28ce6SDave Jiang 51c0f28ce6SDave Jiang /* I/OAT v3.2 platforms */ 52c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF0) }, 53c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF1) }, 54c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF2) }, 55c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF3) }, 56c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF4) }, 57c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF5) }, 58c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF6) }, 59c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF7) }, 60c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF8) }, 61c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF9) }, 62c0f28ce6SDave Jiang 63c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB0) }, 64c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB1) }, 65c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB2) }, 66c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB3) }, 67c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB4) }, 68c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB5) }, 69c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB6) }, 70c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB7) }, 71c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB8) }, 72c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB9) }, 73c0f28ce6SDave Jiang 74c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB0) }, 75c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB1) }, 76c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB2) }, 77c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB3) }, 78c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB4) }, 79c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB5) }, 80c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB6) }, 81c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB7) }, 82c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB8) }, 83c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB9) }, 84c0f28ce6SDave Jiang 85c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW0) }, 86c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW1) }, 87c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW2) }, 88c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW3) }, 89c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW4) }, 90c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW5) }, 91c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW6) }, 92c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW7) }, 93c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW8) }, 94c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW9) }, 95c0f28ce6SDave Jiang 96c0f28ce6SDave Jiang /* I/OAT v3.3 platforms */ 97c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD0) }, 98c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD1) }, 99c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD2) }, 100c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD3) }, 101c0f28ce6SDave Jiang 102c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE0) }, 103c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE1) }, 104c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE2) }, 105c0f28ce6SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE3) }, 106c0f28ce6SDave Jiang 107c0f28ce6SDave Jiang { 0, } 108c0f28ce6SDave Jiang }; 109c0f28ce6SDave Jiang MODULE_DEVICE_TABLE(pci, ioat_pci_tbl); 110c0f28ce6SDave Jiang 111c0f28ce6SDave Jiang static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id); 112c0f28ce6SDave Jiang static void ioat_remove(struct pci_dev *pdev); 113599d49deSDave Jiang static void 114599d49deSDave Jiang ioat_init_channel(struct ioatdma_device *ioat_dma, 115599d49deSDave Jiang struct ioatdma_chan *ioat_chan, int idx); 116c0f28ce6SDave Jiang 117c0f28ce6SDave Jiang static int ioat_dca_enabled = 1; 118c0f28ce6SDave Jiang module_param(ioat_dca_enabled, int, 0644); 119c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_dca_enabled, "control support of dca service (default: 1)"); 120c0f28ce6SDave Jiang int ioat_pending_level = 4; 121c0f28ce6SDave Jiang module_param(ioat_pending_level, int, 0644); 122c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_pending_level, 123c0f28ce6SDave Jiang "high-water mark for pushing ioat descriptors (default: 4)"); 124c0f28ce6SDave Jiang int ioat_ring_alloc_order = 8; 125c0f28ce6SDave Jiang module_param(ioat_ring_alloc_order, int, 0644); 126c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_ring_alloc_order, 127c0f28ce6SDave Jiang "ioat+: allocate 2^n descriptors per channel (default: 8 max: 16)"); 128c0f28ce6SDave Jiang int ioat_ring_max_alloc_order = IOAT_MAX_ORDER; 129c0f28ce6SDave Jiang module_param(ioat_ring_max_alloc_order, int, 0644); 130c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_ring_max_alloc_order, 131c0f28ce6SDave Jiang "ioat+: upper limit for ring size (default: 16)"); 132c0f28ce6SDave Jiang static char ioat_interrupt_style[32] = "msix"; 133c0f28ce6SDave Jiang module_param_string(ioat_interrupt_style, ioat_interrupt_style, 134c0f28ce6SDave Jiang sizeof(ioat_interrupt_style), 0644); 135c0f28ce6SDave Jiang MODULE_PARM_DESC(ioat_interrupt_style, 136c0f28ce6SDave Jiang "set ioat interrupt style: msix (default), msi, intx"); 137c0f28ce6SDave Jiang 138c0f28ce6SDave Jiang struct kmem_cache *ioat_cache; 139c0f28ce6SDave Jiang struct kmem_cache *ioat_sed_cache; 140c0f28ce6SDave Jiang 141c0f28ce6SDave Jiang static bool is_jf_ioat(struct pci_dev *pdev) 142c0f28ce6SDave Jiang { 143c0f28ce6SDave Jiang switch (pdev->device) { 144c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF0: 145c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF1: 146c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF2: 147c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF3: 148c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF4: 149c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF5: 150c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF6: 151c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF7: 152c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF8: 153c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_JSF9: 154c0f28ce6SDave Jiang return true; 155c0f28ce6SDave Jiang default: 156c0f28ce6SDave Jiang return false; 157c0f28ce6SDave Jiang } 158c0f28ce6SDave Jiang } 159c0f28ce6SDave Jiang 160c0f28ce6SDave Jiang static bool is_snb_ioat(struct pci_dev *pdev) 161c0f28ce6SDave Jiang { 162c0f28ce6SDave Jiang switch (pdev->device) { 163c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB0: 164c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB1: 165c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB2: 166c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB3: 167c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB4: 168c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB5: 169c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB6: 170c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB7: 171c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB8: 172c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_SNB9: 173c0f28ce6SDave Jiang return true; 174c0f28ce6SDave Jiang default: 175c0f28ce6SDave Jiang return false; 176c0f28ce6SDave Jiang } 177c0f28ce6SDave Jiang } 178c0f28ce6SDave Jiang 179c0f28ce6SDave Jiang static bool is_ivb_ioat(struct pci_dev *pdev) 180c0f28ce6SDave Jiang { 181c0f28ce6SDave Jiang switch (pdev->device) { 182c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB0: 183c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB1: 184c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB2: 185c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB3: 186c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB4: 187c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB5: 188c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB6: 189c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB7: 190c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB8: 191c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_IVB9: 192c0f28ce6SDave Jiang return true; 193c0f28ce6SDave Jiang default: 194c0f28ce6SDave Jiang return false; 195c0f28ce6SDave Jiang } 196c0f28ce6SDave Jiang 197c0f28ce6SDave Jiang } 198c0f28ce6SDave Jiang 199c0f28ce6SDave Jiang static bool is_hsw_ioat(struct pci_dev *pdev) 200c0f28ce6SDave Jiang { 201c0f28ce6SDave Jiang switch (pdev->device) { 202c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW0: 203c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW1: 204c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW2: 205c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW3: 206c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW4: 207c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW5: 208c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW6: 209c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW7: 210c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW8: 211c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_HSW9: 212c0f28ce6SDave Jiang return true; 213c0f28ce6SDave Jiang default: 214c0f28ce6SDave Jiang return false; 215c0f28ce6SDave Jiang } 216c0f28ce6SDave Jiang 217c0f28ce6SDave Jiang } 218c0f28ce6SDave Jiang 219c0f28ce6SDave Jiang static bool is_xeon_cb32(struct pci_dev *pdev) 220c0f28ce6SDave Jiang { 221c0f28ce6SDave Jiang return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) || 222c0f28ce6SDave Jiang is_hsw_ioat(pdev); 223c0f28ce6SDave Jiang } 224c0f28ce6SDave Jiang 225c0f28ce6SDave Jiang bool is_bwd_ioat(struct pci_dev *pdev) 226c0f28ce6SDave Jiang { 227c0f28ce6SDave Jiang switch (pdev->device) { 228c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD0: 229c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD1: 230c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD2: 231c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD3: 232c0f28ce6SDave Jiang /* even though not Atom, BDX-DE has same DMA silicon */ 233c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0: 234c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1: 235c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2: 236c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3: 237c0f28ce6SDave Jiang return true; 238c0f28ce6SDave Jiang default: 239c0f28ce6SDave Jiang return false; 240c0f28ce6SDave Jiang } 241c0f28ce6SDave Jiang } 242c0f28ce6SDave Jiang 243c0f28ce6SDave Jiang static bool is_bwd_noraid(struct pci_dev *pdev) 244c0f28ce6SDave Jiang { 245c0f28ce6SDave Jiang switch (pdev->device) { 246c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD2: 247c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BWD3: 248c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0: 249c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1: 250c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2: 251c0f28ce6SDave Jiang case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3: 252c0f28ce6SDave Jiang return true; 253c0f28ce6SDave Jiang default: 254c0f28ce6SDave Jiang return false; 255c0f28ce6SDave Jiang } 256c0f28ce6SDave Jiang 257c0f28ce6SDave Jiang } 258c0f28ce6SDave Jiang 259c0f28ce6SDave Jiang /* 260c0f28ce6SDave Jiang * Perform a IOAT transaction to verify the HW works. 261c0f28ce6SDave Jiang */ 262c0f28ce6SDave Jiang #define IOAT_TEST_SIZE 2000 263c0f28ce6SDave Jiang 264c0f28ce6SDave Jiang static void ioat_dma_test_callback(void *dma_async_param) 265c0f28ce6SDave Jiang { 266c0f28ce6SDave Jiang struct completion *cmp = dma_async_param; 267c0f28ce6SDave Jiang 268c0f28ce6SDave Jiang complete(cmp); 269c0f28ce6SDave Jiang } 270c0f28ce6SDave Jiang 271c0f28ce6SDave Jiang /** 272c0f28ce6SDave Jiang * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works. 273c0f28ce6SDave Jiang * @ioat_dma: dma device to be tested 274c0f28ce6SDave Jiang */ 275599d49deSDave Jiang static int ioat_dma_self_test(struct ioatdma_device *ioat_dma) 276c0f28ce6SDave Jiang { 277c0f28ce6SDave Jiang int i; 278c0f28ce6SDave Jiang u8 *src; 279c0f28ce6SDave Jiang u8 *dest; 280c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 281c0f28ce6SDave Jiang struct device *dev = &ioat_dma->pdev->dev; 282c0f28ce6SDave Jiang struct dma_chan *dma_chan; 283c0f28ce6SDave Jiang struct dma_async_tx_descriptor *tx; 284c0f28ce6SDave Jiang dma_addr_t dma_dest, dma_src; 285c0f28ce6SDave Jiang dma_cookie_t cookie; 286c0f28ce6SDave Jiang int err = 0; 287c0f28ce6SDave Jiang struct completion cmp; 288c0f28ce6SDave Jiang unsigned long tmo; 289c0f28ce6SDave Jiang unsigned long flags; 290c0f28ce6SDave Jiang 291c0f28ce6SDave Jiang src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL); 292c0f28ce6SDave Jiang if (!src) 293c0f28ce6SDave Jiang return -ENOMEM; 294c0f28ce6SDave Jiang dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL); 295c0f28ce6SDave Jiang if (!dest) { 296c0f28ce6SDave Jiang kfree(src); 297c0f28ce6SDave Jiang return -ENOMEM; 298c0f28ce6SDave Jiang } 299c0f28ce6SDave Jiang 300c0f28ce6SDave Jiang /* Fill in src buffer */ 301c0f28ce6SDave Jiang for (i = 0; i < IOAT_TEST_SIZE; i++) 302c0f28ce6SDave Jiang src[i] = (u8)i; 303c0f28ce6SDave Jiang 304c0f28ce6SDave Jiang /* Start copy, using first DMA channel */ 305c0f28ce6SDave Jiang dma_chan = container_of(dma->channels.next, struct dma_chan, 306c0f28ce6SDave Jiang device_node); 307c0f28ce6SDave Jiang if (dma->device_alloc_chan_resources(dma_chan) < 1) { 308c0f28ce6SDave Jiang dev_err(dev, "selftest cannot allocate chan resource\n"); 309c0f28ce6SDave Jiang err = -ENODEV; 310c0f28ce6SDave Jiang goto out; 311c0f28ce6SDave Jiang } 312c0f28ce6SDave Jiang 313c0f28ce6SDave Jiang dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE); 314c0f28ce6SDave Jiang if (dma_mapping_error(dev, dma_src)) { 315c0f28ce6SDave Jiang dev_err(dev, "mapping src buffer failed\n"); 316c0f28ce6SDave Jiang goto free_resources; 317c0f28ce6SDave Jiang } 318c0f28ce6SDave Jiang dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE); 319c0f28ce6SDave Jiang if (dma_mapping_error(dev, dma_dest)) { 320c0f28ce6SDave Jiang dev_err(dev, "mapping dest buffer failed\n"); 321c0f28ce6SDave Jiang goto unmap_src; 322c0f28ce6SDave Jiang } 323c0f28ce6SDave Jiang flags = DMA_PREP_INTERRUPT; 324c0f28ce6SDave Jiang tx = ioat_dma->dma_dev.device_prep_dma_memcpy(dma_chan, dma_dest, 325c0f28ce6SDave Jiang dma_src, IOAT_TEST_SIZE, 326c0f28ce6SDave Jiang flags); 327c0f28ce6SDave Jiang if (!tx) { 328c0f28ce6SDave Jiang dev_err(dev, "Self-test prep failed, disabling\n"); 329c0f28ce6SDave Jiang err = -ENODEV; 330c0f28ce6SDave Jiang goto unmap_dma; 331c0f28ce6SDave Jiang } 332c0f28ce6SDave Jiang 333c0f28ce6SDave Jiang async_tx_ack(tx); 334c0f28ce6SDave Jiang init_completion(&cmp); 335c0f28ce6SDave Jiang tx->callback = ioat_dma_test_callback; 336c0f28ce6SDave Jiang tx->callback_param = &cmp; 337c0f28ce6SDave Jiang cookie = tx->tx_submit(tx); 338c0f28ce6SDave Jiang if (cookie < 0) { 339c0f28ce6SDave Jiang dev_err(dev, "Self-test setup failed, disabling\n"); 340c0f28ce6SDave Jiang err = -ENODEV; 341c0f28ce6SDave Jiang goto unmap_dma; 342c0f28ce6SDave Jiang } 343c0f28ce6SDave Jiang dma->device_issue_pending(dma_chan); 344c0f28ce6SDave Jiang 345c0f28ce6SDave Jiang tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)); 346c0f28ce6SDave Jiang 347c0f28ce6SDave Jiang if (tmo == 0 || 348c0f28ce6SDave Jiang dma->device_tx_status(dma_chan, cookie, NULL) 349c0f28ce6SDave Jiang != DMA_COMPLETE) { 350c0f28ce6SDave Jiang dev_err(dev, "Self-test copy timed out, disabling\n"); 351c0f28ce6SDave Jiang err = -ENODEV; 352c0f28ce6SDave Jiang goto unmap_dma; 353c0f28ce6SDave Jiang } 354c0f28ce6SDave Jiang if (memcmp(src, dest, IOAT_TEST_SIZE)) { 355c0f28ce6SDave Jiang dev_err(dev, "Self-test copy failed compare, disabling\n"); 356c0f28ce6SDave Jiang err = -ENODEV; 357c0f28ce6SDave Jiang goto free_resources; 358c0f28ce6SDave Jiang } 359c0f28ce6SDave Jiang 360c0f28ce6SDave Jiang unmap_dma: 361c0f28ce6SDave Jiang dma_unmap_single(dev, dma_dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE); 362c0f28ce6SDave Jiang unmap_src: 363c0f28ce6SDave Jiang dma_unmap_single(dev, dma_src, IOAT_TEST_SIZE, DMA_TO_DEVICE); 364c0f28ce6SDave Jiang free_resources: 365c0f28ce6SDave Jiang dma->device_free_chan_resources(dma_chan); 366c0f28ce6SDave Jiang out: 367c0f28ce6SDave Jiang kfree(src); 368c0f28ce6SDave Jiang kfree(dest); 369c0f28ce6SDave Jiang return err; 370c0f28ce6SDave Jiang } 371c0f28ce6SDave Jiang 372c0f28ce6SDave Jiang /** 373c0f28ce6SDave Jiang * ioat_dma_setup_interrupts - setup interrupt handler 374c0f28ce6SDave Jiang * @ioat_dma: ioat dma device 375c0f28ce6SDave Jiang */ 376c0f28ce6SDave Jiang int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma) 377c0f28ce6SDave Jiang { 378c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan; 379c0f28ce6SDave Jiang struct pci_dev *pdev = ioat_dma->pdev; 380c0f28ce6SDave Jiang struct device *dev = &pdev->dev; 381c0f28ce6SDave Jiang struct msix_entry *msix; 382c0f28ce6SDave Jiang int i, j, msixcnt; 383c0f28ce6SDave Jiang int err = -EINVAL; 384c0f28ce6SDave Jiang u8 intrctrl = 0; 385c0f28ce6SDave Jiang 386c0f28ce6SDave Jiang if (!strcmp(ioat_interrupt_style, "msix")) 387c0f28ce6SDave Jiang goto msix; 388c0f28ce6SDave Jiang if (!strcmp(ioat_interrupt_style, "msi")) 389c0f28ce6SDave Jiang goto msi; 390c0f28ce6SDave Jiang if (!strcmp(ioat_interrupt_style, "intx")) 391c0f28ce6SDave Jiang goto intx; 392c0f28ce6SDave Jiang dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style); 393c0f28ce6SDave Jiang goto err_no_irq; 394c0f28ce6SDave Jiang 395c0f28ce6SDave Jiang msix: 396c0f28ce6SDave Jiang /* The number of MSI-X vectors should equal the number of channels */ 397c0f28ce6SDave Jiang msixcnt = ioat_dma->dma_dev.chancnt; 398c0f28ce6SDave Jiang for (i = 0; i < msixcnt; i++) 399c0f28ce6SDave Jiang ioat_dma->msix_entries[i].entry = i; 400c0f28ce6SDave Jiang 401c0f28ce6SDave Jiang err = pci_enable_msix_exact(pdev, ioat_dma->msix_entries, msixcnt); 402c0f28ce6SDave Jiang if (err) 403c0f28ce6SDave Jiang goto msi; 404c0f28ce6SDave Jiang 405c0f28ce6SDave Jiang for (i = 0; i < msixcnt; i++) { 406c0f28ce6SDave Jiang msix = &ioat_dma->msix_entries[i]; 407c0f28ce6SDave Jiang ioat_chan = ioat_chan_by_index(ioat_dma, i); 408c0f28ce6SDave Jiang err = devm_request_irq(dev, msix->vector, 409c0f28ce6SDave Jiang ioat_dma_do_interrupt_msix, 0, 410c0f28ce6SDave Jiang "ioat-msix", ioat_chan); 411c0f28ce6SDave Jiang if (err) { 412c0f28ce6SDave Jiang for (j = 0; j < i; j++) { 413c0f28ce6SDave Jiang msix = &ioat_dma->msix_entries[j]; 414c0f28ce6SDave Jiang ioat_chan = ioat_chan_by_index(ioat_dma, j); 415c0f28ce6SDave Jiang devm_free_irq(dev, msix->vector, ioat_chan); 416c0f28ce6SDave Jiang } 417c0f28ce6SDave Jiang goto msi; 418c0f28ce6SDave Jiang } 419c0f28ce6SDave Jiang } 420c0f28ce6SDave Jiang intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL; 421c0f28ce6SDave Jiang ioat_dma->irq_mode = IOAT_MSIX; 422c0f28ce6SDave Jiang goto done; 423c0f28ce6SDave Jiang 424c0f28ce6SDave Jiang msi: 425c0f28ce6SDave Jiang err = pci_enable_msi(pdev); 426c0f28ce6SDave Jiang if (err) 427c0f28ce6SDave Jiang goto intx; 428c0f28ce6SDave Jiang 429c0f28ce6SDave Jiang err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0, 430c0f28ce6SDave Jiang "ioat-msi", ioat_dma); 431c0f28ce6SDave Jiang if (err) { 432c0f28ce6SDave Jiang pci_disable_msi(pdev); 433c0f28ce6SDave Jiang goto intx; 434c0f28ce6SDave Jiang } 435c0f28ce6SDave Jiang ioat_dma->irq_mode = IOAT_MSI; 436c0f28ce6SDave Jiang goto done; 437c0f28ce6SDave Jiang 438c0f28ce6SDave Jiang intx: 439c0f28ce6SDave Jiang err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 440c0f28ce6SDave Jiang IRQF_SHARED, "ioat-intx", ioat_dma); 441c0f28ce6SDave Jiang if (err) 442c0f28ce6SDave Jiang goto err_no_irq; 443c0f28ce6SDave Jiang 444c0f28ce6SDave Jiang ioat_dma->irq_mode = IOAT_INTX; 445c0f28ce6SDave Jiang done: 446c0f28ce6SDave Jiang if (ioat_dma->intr_quirk) 447c0f28ce6SDave Jiang ioat_dma->intr_quirk(ioat_dma); 448c0f28ce6SDave Jiang intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN; 449c0f28ce6SDave Jiang writeb(intrctrl, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET); 450c0f28ce6SDave Jiang return 0; 451c0f28ce6SDave Jiang 452c0f28ce6SDave Jiang err_no_irq: 453c0f28ce6SDave Jiang /* Disable all interrupt generation */ 454c0f28ce6SDave Jiang writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET); 455c0f28ce6SDave Jiang ioat_dma->irq_mode = IOAT_NOIRQ; 456c0f28ce6SDave Jiang dev_err(dev, "no usable interrupts\n"); 457c0f28ce6SDave Jiang return err; 458c0f28ce6SDave Jiang } 459c0f28ce6SDave Jiang 460c0f28ce6SDave Jiang static void ioat_disable_interrupts(struct ioatdma_device *ioat_dma) 461c0f28ce6SDave Jiang { 462c0f28ce6SDave Jiang /* Disable all interrupt generation */ 463c0f28ce6SDave Jiang writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET); 464c0f28ce6SDave Jiang } 465c0f28ce6SDave Jiang 466599d49deSDave Jiang static int ioat_probe(struct ioatdma_device *ioat_dma) 467c0f28ce6SDave Jiang { 468c0f28ce6SDave Jiang int err = -ENODEV; 469c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 470c0f28ce6SDave Jiang struct pci_dev *pdev = ioat_dma->pdev; 471c0f28ce6SDave Jiang struct device *dev = &pdev->dev; 472c0f28ce6SDave Jiang 473c0f28ce6SDave Jiang /* DMA coherent memory pool for DMA descriptor allocations */ 474c0f28ce6SDave Jiang ioat_dma->dma_pool = pci_pool_create("dma_desc_pool", pdev, 475c0f28ce6SDave Jiang sizeof(struct ioat_dma_descriptor), 476c0f28ce6SDave Jiang 64, 0); 477c0f28ce6SDave Jiang if (!ioat_dma->dma_pool) { 478c0f28ce6SDave Jiang err = -ENOMEM; 479c0f28ce6SDave Jiang goto err_dma_pool; 480c0f28ce6SDave Jiang } 481c0f28ce6SDave Jiang 482c0f28ce6SDave Jiang ioat_dma->completion_pool = pci_pool_create("completion_pool", pdev, 483c0f28ce6SDave Jiang sizeof(u64), 484c0f28ce6SDave Jiang SMP_CACHE_BYTES, 485c0f28ce6SDave Jiang SMP_CACHE_BYTES); 486c0f28ce6SDave Jiang 487c0f28ce6SDave Jiang if (!ioat_dma->completion_pool) { 488c0f28ce6SDave Jiang err = -ENOMEM; 489c0f28ce6SDave Jiang goto err_completion_pool; 490c0f28ce6SDave Jiang } 491c0f28ce6SDave Jiang 492c0f28ce6SDave Jiang ioat_dma->enumerate_channels(ioat_dma); 493c0f28ce6SDave Jiang 494c0f28ce6SDave Jiang dma_cap_set(DMA_MEMCPY, dma->cap_mask); 495c0f28ce6SDave Jiang dma->dev = &pdev->dev; 496c0f28ce6SDave Jiang 497c0f28ce6SDave Jiang if (!dma->chancnt) { 498c0f28ce6SDave Jiang dev_err(dev, "channel enumeration error\n"); 499c0f28ce6SDave Jiang goto err_setup_interrupts; 500c0f28ce6SDave Jiang } 501c0f28ce6SDave Jiang 502c0f28ce6SDave Jiang err = ioat_dma_setup_interrupts(ioat_dma); 503c0f28ce6SDave Jiang if (err) 504c0f28ce6SDave Jiang goto err_setup_interrupts; 505c0f28ce6SDave Jiang 506c0f28ce6SDave Jiang err = ioat_dma->self_test(ioat_dma); 507c0f28ce6SDave Jiang if (err) 508c0f28ce6SDave Jiang goto err_self_test; 509c0f28ce6SDave Jiang 510c0f28ce6SDave Jiang return 0; 511c0f28ce6SDave Jiang 512c0f28ce6SDave Jiang err_self_test: 513c0f28ce6SDave Jiang ioat_disable_interrupts(ioat_dma); 514c0f28ce6SDave Jiang err_setup_interrupts: 515c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->completion_pool); 516c0f28ce6SDave Jiang err_completion_pool: 517c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->dma_pool); 518c0f28ce6SDave Jiang err_dma_pool: 519c0f28ce6SDave Jiang return err; 520c0f28ce6SDave Jiang } 521c0f28ce6SDave Jiang 522599d49deSDave Jiang static int ioat_register(struct ioatdma_device *ioat_dma) 523c0f28ce6SDave Jiang { 524c0f28ce6SDave Jiang int err = dma_async_device_register(&ioat_dma->dma_dev); 525c0f28ce6SDave Jiang 526c0f28ce6SDave Jiang if (err) { 527c0f28ce6SDave Jiang ioat_disable_interrupts(ioat_dma); 528c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->completion_pool); 529c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->dma_pool); 530c0f28ce6SDave Jiang } 531c0f28ce6SDave Jiang 532c0f28ce6SDave Jiang return err; 533c0f28ce6SDave Jiang } 534c0f28ce6SDave Jiang 535599d49deSDave Jiang static void ioat_dma_remove(struct ioatdma_device *ioat_dma) 536c0f28ce6SDave Jiang { 537c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 538c0f28ce6SDave Jiang 539c0f28ce6SDave Jiang ioat_disable_interrupts(ioat_dma); 540c0f28ce6SDave Jiang 541c0f28ce6SDave Jiang ioat_kobject_del(ioat_dma); 542c0f28ce6SDave Jiang 543c0f28ce6SDave Jiang dma_async_device_unregister(dma); 544c0f28ce6SDave Jiang 545c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->dma_pool); 546c0f28ce6SDave Jiang pci_pool_destroy(ioat_dma->completion_pool); 547c0f28ce6SDave Jiang 548c0f28ce6SDave Jiang INIT_LIST_HEAD(&dma->channels); 549c0f28ce6SDave Jiang } 550c0f28ce6SDave Jiang 551c0f28ce6SDave Jiang /** 552c0f28ce6SDave Jiang * ioat_enumerate_channels - find and initialize the device's channels 553c0f28ce6SDave Jiang * @ioat_dma: the ioat dma device to be enumerated 554c0f28ce6SDave Jiang */ 555599d49deSDave Jiang static int ioat_enumerate_channels(struct ioatdma_device *ioat_dma) 556c0f28ce6SDave Jiang { 557c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan; 558c0f28ce6SDave Jiang struct device *dev = &ioat_dma->pdev->dev; 559c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 560c0f28ce6SDave Jiang u8 xfercap_log; 561c0f28ce6SDave Jiang int i; 562c0f28ce6SDave Jiang 563c0f28ce6SDave Jiang INIT_LIST_HEAD(&dma->channels); 564c0f28ce6SDave Jiang dma->chancnt = readb(ioat_dma->reg_base + IOAT_CHANCNT_OFFSET); 565c0f28ce6SDave Jiang dma->chancnt &= 0x1f; /* bits [4:0] valid */ 566c0f28ce6SDave Jiang if (dma->chancnt > ARRAY_SIZE(ioat_dma->idx)) { 567c0f28ce6SDave Jiang dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n", 568c0f28ce6SDave Jiang dma->chancnt, ARRAY_SIZE(ioat_dma->idx)); 569c0f28ce6SDave Jiang dma->chancnt = ARRAY_SIZE(ioat_dma->idx); 570c0f28ce6SDave Jiang } 571c0f28ce6SDave Jiang xfercap_log = readb(ioat_dma->reg_base + IOAT_XFERCAP_OFFSET); 572c0f28ce6SDave Jiang xfercap_log &= 0x1f; /* bits [4:0] valid */ 573c0f28ce6SDave Jiang if (xfercap_log == 0) 574c0f28ce6SDave Jiang return 0; 575c0f28ce6SDave Jiang dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log); 576c0f28ce6SDave Jiang 577c0f28ce6SDave Jiang for (i = 0; i < dma->chancnt; i++) { 578c0f28ce6SDave Jiang ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL); 579c0f28ce6SDave Jiang if (!ioat_chan) 580c0f28ce6SDave Jiang break; 581c0f28ce6SDave Jiang 582c0f28ce6SDave Jiang ioat_init_channel(ioat_dma, ioat_chan, i); 583c0f28ce6SDave Jiang ioat_chan->xfercap_log = xfercap_log; 584c0f28ce6SDave Jiang spin_lock_init(&ioat_chan->prep_lock); 585c0f28ce6SDave Jiang if (ioat_dma->reset_hw(ioat_chan)) { 586c0f28ce6SDave Jiang i = 0; 587c0f28ce6SDave Jiang break; 588c0f28ce6SDave Jiang } 589c0f28ce6SDave Jiang } 590c0f28ce6SDave Jiang dma->chancnt = i; 591c0f28ce6SDave Jiang return i; 592c0f28ce6SDave Jiang } 593c0f28ce6SDave Jiang 594c0f28ce6SDave Jiang /** 595c0f28ce6SDave Jiang * ioat_free_chan_resources - release all the descriptors 596c0f28ce6SDave Jiang * @chan: the channel to be cleaned 597c0f28ce6SDave Jiang */ 598599d49deSDave Jiang static void ioat_free_chan_resources(struct dma_chan *c) 599c0f28ce6SDave Jiang { 600c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan = to_ioat_chan(c); 601c0f28ce6SDave Jiang struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma; 602c0f28ce6SDave Jiang struct ioat_ring_ent *desc; 603c0f28ce6SDave Jiang const int total_descs = 1 << ioat_chan->alloc_order; 604c0f28ce6SDave Jiang int descs; 605c0f28ce6SDave Jiang int i; 606c0f28ce6SDave Jiang 607c0f28ce6SDave Jiang /* Before freeing channel resources first check 608c0f28ce6SDave Jiang * if they have been previously allocated for this channel. 609c0f28ce6SDave Jiang */ 610c0f28ce6SDave Jiang if (!ioat_chan->ring) 611c0f28ce6SDave Jiang return; 612c0f28ce6SDave Jiang 613c0f28ce6SDave Jiang ioat_stop(ioat_chan); 614c0f28ce6SDave Jiang ioat_dma->reset_hw(ioat_chan); 615c0f28ce6SDave Jiang 616c0f28ce6SDave Jiang spin_lock_bh(&ioat_chan->cleanup_lock); 617c0f28ce6SDave Jiang spin_lock_bh(&ioat_chan->prep_lock); 618c0f28ce6SDave Jiang descs = ioat_ring_space(ioat_chan); 619c0f28ce6SDave Jiang dev_dbg(to_dev(ioat_chan), "freeing %d idle descriptors\n", descs); 620c0f28ce6SDave Jiang for (i = 0; i < descs; i++) { 621c0f28ce6SDave Jiang desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head + i); 622c0f28ce6SDave Jiang ioat_free_ring_ent(desc, c); 623c0f28ce6SDave Jiang } 624c0f28ce6SDave Jiang 625c0f28ce6SDave Jiang if (descs < total_descs) 626c0f28ce6SDave Jiang dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n", 627c0f28ce6SDave Jiang total_descs - descs); 628c0f28ce6SDave Jiang 629c0f28ce6SDave Jiang for (i = 0; i < total_descs - descs; i++) { 630c0f28ce6SDave Jiang desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail + i); 631c0f28ce6SDave Jiang dump_desc_dbg(ioat_chan, desc); 632c0f28ce6SDave Jiang ioat_free_ring_ent(desc, c); 633c0f28ce6SDave Jiang } 634c0f28ce6SDave Jiang 635c0f28ce6SDave Jiang kfree(ioat_chan->ring); 636c0f28ce6SDave Jiang ioat_chan->ring = NULL; 637c0f28ce6SDave Jiang ioat_chan->alloc_order = 0; 638c0f28ce6SDave Jiang pci_pool_free(ioat_dma->completion_pool, ioat_chan->completion, 639c0f28ce6SDave Jiang ioat_chan->completion_dma); 640c0f28ce6SDave Jiang spin_unlock_bh(&ioat_chan->prep_lock); 641c0f28ce6SDave Jiang spin_unlock_bh(&ioat_chan->cleanup_lock); 642c0f28ce6SDave Jiang 643c0f28ce6SDave Jiang ioat_chan->last_completion = 0; 644c0f28ce6SDave Jiang ioat_chan->completion_dma = 0; 645c0f28ce6SDave Jiang ioat_chan->dmacount = 0; 646c0f28ce6SDave Jiang } 647c0f28ce6SDave Jiang 648c0f28ce6SDave Jiang /* ioat_alloc_chan_resources - allocate/initialize ioat descriptor ring 649c0f28ce6SDave Jiang * @chan: channel to be initialized 650c0f28ce6SDave Jiang */ 651599d49deSDave Jiang static int ioat_alloc_chan_resources(struct dma_chan *c) 652c0f28ce6SDave Jiang { 653c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan = to_ioat_chan(c); 654c0f28ce6SDave Jiang struct ioat_ring_ent **ring; 655c0f28ce6SDave Jiang u64 status; 656c0f28ce6SDave Jiang int order; 657c0f28ce6SDave Jiang int i = 0; 658c0f28ce6SDave Jiang u32 chanerr; 659c0f28ce6SDave Jiang 660c0f28ce6SDave Jiang /* have we already been set up? */ 661c0f28ce6SDave Jiang if (ioat_chan->ring) 662c0f28ce6SDave Jiang return 1 << ioat_chan->alloc_order; 663c0f28ce6SDave Jiang 664c0f28ce6SDave Jiang /* Setup register to interrupt and write completion status on error */ 665c0f28ce6SDave Jiang writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET); 666c0f28ce6SDave Jiang 667c0f28ce6SDave Jiang /* allocate a completion writeback area */ 668c0f28ce6SDave Jiang /* doing 2 32bit writes to mmio since 1 64b write doesn't work */ 669c0f28ce6SDave Jiang ioat_chan->completion = 670c0f28ce6SDave Jiang pci_pool_alloc(ioat_chan->ioat_dma->completion_pool, 671c0f28ce6SDave Jiang GFP_KERNEL, &ioat_chan->completion_dma); 672c0f28ce6SDave Jiang if (!ioat_chan->completion) 673c0f28ce6SDave Jiang return -ENOMEM; 674c0f28ce6SDave Jiang 675c0f28ce6SDave Jiang memset(ioat_chan->completion, 0, sizeof(*ioat_chan->completion)); 676c0f28ce6SDave Jiang writel(((u64)ioat_chan->completion_dma) & 0x00000000FFFFFFFF, 677c0f28ce6SDave Jiang ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW); 678c0f28ce6SDave Jiang writel(((u64)ioat_chan->completion_dma) >> 32, 679c0f28ce6SDave Jiang ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH); 680c0f28ce6SDave Jiang 681c0f28ce6SDave Jiang order = ioat_get_alloc_order(); 682c0f28ce6SDave Jiang ring = ioat_alloc_ring(c, order, GFP_KERNEL); 683c0f28ce6SDave Jiang if (!ring) 684c0f28ce6SDave Jiang return -ENOMEM; 685c0f28ce6SDave Jiang 686c0f28ce6SDave Jiang spin_lock_bh(&ioat_chan->cleanup_lock); 687c0f28ce6SDave Jiang spin_lock_bh(&ioat_chan->prep_lock); 688c0f28ce6SDave Jiang ioat_chan->ring = ring; 689c0f28ce6SDave Jiang ioat_chan->head = 0; 690c0f28ce6SDave Jiang ioat_chan->issued = 0; 691c0f28ce6SDave Jiang ioat_chan->tail = 0; 692c0f28ce6SDave Jiang ioat_chan->alloc_order = order; 693c0f28ce6SDave Jiang set_bit(IOAT_RUN, &ioat_chan->state); 694c0f28ce6SDave Jiang spin_unlock_bh(&ioat_chan->prep_lock); 695c0f28ce6SDave Jiang spin_unlock_bh(&ioat_chan->cleanup_lock); 696c0f28ce6SDave Jiang 697c0f28ce6SDave Jiang ioat_start_null_desc(ioat_chan); 698c0f28ce6SDave Jiang 699c0f28ce6SDave Jiang /* check that we got off the ground */ 700c0f28ce6SDave Jiang do { 701c0f28ce6SDave Jiang udelay(1); 702c0f28ce6SDave Jiang status = ioat_chansts(ioat_chan); 703c0f28ce6SDave Jiang } while (i++ < 20 && !is_ioat_active(status) && !is_ioat_idle(status)); 704c0f28ce6SDave Jiang 705c0f28ce6SDave Jiang if (is_ioat_active(status) || is_ioat_idle(status)) 706c0f28ce6SDave Jiang return 1 << ioat_chan->alloc_order; 707c0f28ce6SDave Jiang 708c0f28ce6SDave Jiang chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET); 709c0f28ce6SDave Jiang 710c0f28ce6SDave Jiang dev_WARN(to_dev(ioat_chan), 711c0f28ce6SDave Jiang "failed to start channel chanerr: %#x\n", chanerr); 712c0f28ce6SDave Jiang ioat_free_chan_resources(c); 713c0f28ce6SDave Jiang return -EFAULT; 714c0f28ce6SDave Jiang } 715c0f28ce6SDave Jiang 716c0f28ce6SDave Jiang /* common channel initialization */ 717599d49deSDave Jiang static void 718c0f28ce6SDave Jiang ioat_init_channel(struct ioatdma_device *ioat_dma, 719c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan, int idx) 720c0f28ce6SDave Jiang { 721c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 722c0f28ce6SDave Jiang struct dma_chan *c = &ioat_chan->dma_chan; 723c0f28ce6SDave Jiang unsigned long data = (unsigned long) c; 724c0f28ce6SDave Jiang 725c0f28ce6SDave Jiang ioat_chan->ioat_dma = ioat_dma; 726c0f28ce6SDave Jiang ioat_chan->reg_base = ioat_dma->reg_base + (0x80 * (idx + 1)); 727c0f28ce6SDave Jiang spin_lock_init(&ioat_chan->cleanup_lock); 728c0f28ce6SDave Jiang ioat_chan->dma_chan.device = dma; 729c0f28ce6SDave Jiang dma_cookie_init(&ioat_chan->dma_chan); 730c0f28ce6SDave Jiang list_add_tail(&ioat_chan->dma_chan.device_node, &dma->channels); 731c0f28ce6SDave Jiang ioat_dma->idx[idx] = ioat_chan; 732c0f28ce6SDave Jiang init_timer(&ioat_chan->timer); 733c0f28ce6SDave Jiang ioat_chan->timer.function = ioat_dma->timer_fn; 734c0f28ce6SDave Jiang ioat_chan->timer.data = data; 735c0f28ce6SDave Jiang tasklet_init(&ioat_chan->cleanup_task, ioat_dma->cleanup_fn, data); 736c0f28ce6SDave Jiang } 737c0f28ce6SDave Jiang 738c0f28ce6SDave Jiang #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */ 739c0f28ce6SDave Jiang static int ioat_xor_val_self_test(struct ioatdma_device *ioat_dma) 740c0f28ce6SDave Jiang { 741c0f28ce6SDave Jiang int i, src_idx; 742c0f28ce6SDave Jiang struct page *dest; 743c0f28ce6SDave Jiang struct page *xor_srcs[IOAT_NUM_SRC_TEST]; 744c0f28ce6SDave Jiang struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1]; 745c0f28ce6SDave Jiang dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1]; 746c0f28ce6SDave Jiang dma_addr_t dest_dma; 747c0f28ce6SDave Jiang struct dma_async_tx_descriptor *tx; 748c0f28ce6SDave Jiang struct dma_chan *dma_chan; 749c0f28ce6SDave Jiang dma_cookie_t cookie; 750c0f28ce6SDave Jiang u8 cmp_byte = 0; 751c0f28ce6SDave Jiang u32 cmp_word; 752c0f28ce6SDave Jiang u32 xor_val_result; 753c0f28ce6SDave Jiang int err = 0; 754c0f28ce6SDave Jiang struct completion cmp; 755c0f28ce6SDave Jiang unsigned long tmo; 756c0f28ce6SDave Jiang struct device *dev = &ioat_dma->pdev->dev; 757c0f28ce6SDave Jiang struct dma_device *dma = &ioat_dma->dma_dev; 758c0f28ce6SDave Jiang u8 op = 0; 759c0f28ce6SDave Jiang 760c0f28ce6SDave Jiang dev_dbg(dev, "%s\n", __func__); 761c0f28ce6SDave Jiang 762c0f28ce6SDave Jiang if (!dma_has_cap(DMA_XOR, dma->cap_mask)) 763c0f28ce6SDave Jiang return 0; 764c0f28ce6SDave Jiang 765c0f28ce6SDave Jiang for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) { 766c0f28ce6SDave Jiang xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 767c0f28ce6SDave Jiang if (!xor_srcs[src_idx]) { 768c0f28ce6SDave Jiang while (src_idx--) 769c0f28ce6SDave Jiang __free_page(xor_srcs[src_idx]); 770c0f28ce6SDave Jiang return -ENOMEM; 771c0f28ce6SDave Jiang } 772c0f28ce6SDave Jiang } 773c0f28ce6SDave Jiang 774c0f28ce6SDave Jiang dest = alloc_page(GFP_KERNEL); 775c0f28ce6SDave Jiang if (!dest) { 776c0f28ce6SDave Jiang while (src_idx--) 777c0f28ce6SDave Jiang __free_page(xor_srcs[src_idx]); 778c0f28ce6SDave Jiang return -ENOMEM; 779c0f28ce6SDave Jiang } 780c0f28ce6SDave Jiang 781c0f28ce6SDave Jiang /* Fill in src buffers */ 782c0f28ce6SDave Jiang for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) { 783c0f28ce6SDave Jiang u8 *ptr = page_address(xor_srcs[src_idx]); 784c0f28ce6SDave Jiang 785c0f28ce6SDave Jiang for (i = 0; i < PAGE_SIZE; i++) 786c0f28ce6SDave Jiang ptr[i] = (1 << src_idx); 787c0f28ce6SDave Jiang } 788c0f28ce6SDave Jiang 789c0f28ce6SDave Jiang for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) 790c0f28ce6SDave Jiang cmp_byte ^= (u8) (1 << src_idx); 791c0f28ce6SDave Jiang 792c0f28ce6SDave Jiang cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 793c0f28ce6SDave Jiang (cmp_byte << 8) | cmp_byte; 794c0f28ce6SDave Jiang 795c0f28ce6SDave Jiang memset(page_address(dest), 0, PAGE_SIZE); 796c0f28ce6SDave Jiang 797c0f28ce6SDave Jiang dma_chan = container_of(dma->channels.next, struct dma_chan, 798c0f28ce6SDave Jiang device_node); 799c0f28ce6SDave Jiang if (dma->device_alloc_chan_resources(dma_chan) < 1) { 800c0f28ce6SDave Jiang err = -ENODEV; 801c0f28ce6SDave Jiang goto out; 802c0f28ce6SDave Jiang } 803c0f28ce6SDave Jiang 804c0f28ce6SDave Jiang /* test xor */ 805c0f28ce6SDave Jiang op = IOAT_OP_XOR; 806c0f28ce6SDave Jiang 807c0f28ce6SDave Jiang dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE); 808c0f28ce6SDave Jiang if (dma_mapping_error(dev, dest_dma)) 809c0f28ce6SDave Jiang goto dma_unmap; 810c0f28ce6SDave Jiang 811c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST; i++) 812c0f28ce6SDave Jiang dma_srcs[i] = DMA_ERROR_CODE; 813c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST; i++) { 814c0f28ce6SDave Jiang dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE, 815c0f28ce6SDave Jiang DMA_TO_DEVICE); 816c0f28ce6SDave Jiang if (dma_mapping_error(dev, dma_srcs[i])) 817c0f28ce6SDave Jiang goto dma_unmap; 818c0f28ce6SDave Jiang } 819c0f28ce6SDave Jiang tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 820c0f28ce6SDave Jiang IOAT_NUM_SRC_TEST, PAGE_SIZE, 821c0f28ce6SDave Jiang DMA_PREP_INTERRUPT); 822c0f28ce6SDave Jiang 823c0f28ce6SDave Jiang if (!tx) { 824c0f28ce6SDave Jiang dev_err(dev, "Self-test xor prep failed\n"); 825c0f28ce6SDave Jiang err = -ENODEV; 826c0f28ce6SDave Jiang goto dma_unmap; 827c0f28ce6SDave Jiang } 828c0f28ce6SDave Jiang 829c0f28ce6SDave Jiang async_tx_ack(tx); 830c0f28ce6SDave Jiang init_completion(&cmp); 831*3372de58SDave Jiang tx->callback = ioat_dma_test_callback; 832c0f28ce6SDave Jiang tx->callback_param = &cmp; 833c0f28ce6SDave Jiang cookie = tx->tx_submit(tx); 834c0f28ce6SDave Jiang if (cookie < 0) { 835c0f28ce6SDave Jiang dev_err(dev, "Self-test xor setup failed\n"); 836c0f28ce6SDave Jiang err = -ENODEV; 837c0f28ce6SDave Jiang goto dma_unmap; 838c0f28ce6SDave Jiang } 839c0f28ce6SDave Jiang dma->device_issue_pending(dma_chan); 840c0f28ce6SDave Jiang 841c0f28ce6SDave Jiang tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)); 842c0f28ce6SDave Jiang 843c0f28ce6SDave Jiang if (tmo == 0 || 844c0f28ce6SDave Jiang dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) { 845c0f28ce6SDave Jiang dev_err(dev, "Self-test xor timed out\n"); 846c0f28ce6SDave Jiang err = -ENODEV; 847c0f28ce6SDave Jiang goto dma_unmap; 848c0f28ce6SDave Jiang } 849c0f28ce6SDave Jiang 850c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST; i++) 851c0f28ce6SDave Jiang dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE); 852c0f28ce6SDave Jiang 853c0f28ce6SDave Jiang dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE); 854c0f28ce6SDave Jiang for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 855c0f28ce6SDave Jiang u32 *ptr = page_address(dest); 856c0f28ce6SDave Jiang 857c0f28ce6SDave Jiang if (ptr[i] != cmp_word) { 858c0f28ce6SDave Jiang dev_err(dev, "Self-test xor failed compare\n"); 859c0f28ce6SDave Jiang err = -ENODEV; 860c0f28ce6SDave Jiang goto free_resources; 861c0f28ce6SDave Jiang } 862c0f28ce6SDave Jiang } 863c0f28ce6SDave Jiang dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE); 864c0f28ce6SDave Jiang 865c0f28ce6SDave Jiang dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE); 866c0f28ce6SDave Jiang 867c0f28ce6SDave Jiang /* skip validate if the capability is not present */ 868c0f28ce6SDave Jiang if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask)) 869c0f28ce6SDave Jiang goto free_resources; 870c0f28ce6SDave Jiang 871c0f28ce6SDave Jiang op = IOAT_OP_XOR_VAL; 872c0f28ce6SDave Jiang 873c0f28ce6SDave Jiang /* validate the sources with the destintation page */ 874c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST; i++) 875c0f28ce6SDave Jiang xor_val_srcs[i] = xor_srcs[i]; 876c0f28ce6SDave Jiang xor_val_srcs[i] = dest; 877c0f28ce6SDave Jiang 878c0f28ce6SDave Jiang xor_val_result = 1; 879c0f28ce6SDave Jiang 880c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) 881c0f28ce6SDave Jiang dma_srcs[i] = DMA_ERROR_CODE; 882c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) { 883c0f28ce6SDave Jiang dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE, 884c0f28ce6SDave Jiang DMA_TO_DEVICE); 885c0f28ce6SDave Jiang if (dma_mapping_error(dev, dma_srcs[i])) 886c0f28ce6SDave Jiang goto dma_unmap; 887c0f28ce6SDave Jiang } 888c0f28ce6SDave Jiang tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs, 889c0f28ce6SDave Jiang IOAT_NUM_SRC_TEST + 1, PAGE_SIZE, 890c0f28ce6SDave Jiang &xor_val_result, DMA_PREP_INTERRUPT); 891c0f28ce6SDave Jiang if (!tx) { 892c0f28ce6SDave Jiang dev_err(dev, "Self-test zero prep failed\n"); 893c0f28ce6SDave Jiang err = -ENODEV; 894c0f28ce6SDave Jiang goto dma_unmap; 895c0f28ce6SDave Jiang } 896c0f28ce6SDave Jiang 897c0f28ce6SDave Jiang async_tx_ack(tx); 898c0f28ce6SDave Jiang init_completion(&cmp); 899*3372de58SDave Jiang tx->callback = ioat_dma_test_callback; 900c0f28ce6SDave Jiang tx->callback_param = &cmp; 901c0f28ce6SDave Jiang cookie = tx->tx_submit(tx); 902c0f28ce6SDave Jiang if (cookie < 0) { 903c0f28ce6SDave Jiang dev_err(dev, "Self-test zero setup failed\n"); 904c0f28ce6SDave Jiang err = -ENODEV; 905c0f28ce6SDave Jiang goto dma_unmap; 906c0f28ce6SDave Jiang } 907c0f28ce6SDave Jiang dma->device_issue_pending(dma_chan); 908c0f28ce6SDave Jiang 909c0f28ce6SDave Jiang tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)); 910c0f28ce6SDave Jiang 911c0f28ce6SDave Jiang if (tmo == 0 || 912c0f28ce6SDave Jiang dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) { 913c0f28ce6SDave Jiang dev_err(dev, "Self-test validate timed out\n"); 914c0f28ce6SDave Jiang err = -ENODEV; 915c0f28ce6SDave Jiang goto dma_unmap; 916c0f28ce6SDave Jiang } 917c0f28ce6SDave Jiang 918c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) 919c0f28ce6SDave Jiang dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE); 920c0f28ce6SDave Jiang 921c0f28ce6SDave Jiang if (xor_val_result != 0) { 922c0f28ce6SDave Jiang dev_err(dev, "Self-test validate failed compare\n"); 923c0f28ce6SDave Jiang err = -ENODEV; 924c0f28ce6SDave Jiang goto free_resources; 925c0f28ce6SDave Jiang } 926c0f28ce6SDave Jiang 927c0f28ce6SDave Jiang memset(page_address(dest), 0, PAGE_SIZE); 928c0f28ce6SDave Jiang 929c0f28ce6SDave Jiang /* test for non-zero parity sum */ 930c0f28ce6SDave Jiang op = IOAT_OP_XOR_VAL; 931c0f28ce6SDave Jiang 932c0f28ce6SDave Jiang xor_val_result = 0; 933c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) 934c0f28ce6SDave Jiang dma_srcs[i] = DMA_ERROR_CODE; 935c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) { 936c0f28ce6SDave Jiang dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE, 937c0f28ce6SDave Jiang DMA_TO_DEVICE); 938c0f28ce6SDave Jiang if (dma_mapping_error(dev, dma_srcs[i])) 939c0f28ce6SDave Jiang goto dma_unmap; 940c0f28ce6SDave Jiang } 941c0f28ce6SDave Jiang tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs, 942c0f28ce6SDave Jiang IOAT_NUM_SRC_TEST + 1, PAGE_SIZE, 943c0f28ce6SDave Jiang &xor_val_result, DMA_PREP_INTERRUPT); 944c0f28ce6SDave Jiang if (!tx) { 945c0f28ce6SDave Jiang dev_err(dev, "Self-test 2nd zero prep failed\n"); 946c0f28ce6SDave Jiang err = -ENODEV; 947c0f28ce6SDave Jiang goto dma_unmap; 948c0f28ce6SDave Jiang } 949c0f28ce6SDave Jiang 950c0f28ce6SDave Jiang async_tx_ack(tx); 951c0f28ce6SDave Jiang init_completion(&cmp); 952*3372de58SDave Jiang tx->callback = ioat_dma_test_callback; 953c0f28ce6SDave Jiang tx->callback_param = &cmp; 954c0f28ce6SDave Jiang cookie = tx->tx_submit(tx); 955c0f28ce6SDave Jiang if (cookie < 0) { 956c0f28ce6SDave Jiang dev_err(dev, "Self-test 2nd zero setup failed\n"); 957c0f28ce6SDave Jiang err = -ENODEV; 958c0f28ce6SDave Jiang goto dma_unmap; 959c0f28ce6SDave Jiang } 960c0f28ce6SDave Jiang dma->device_issue_pending(dma_chan); 961c0f28ce6SDave Jiang 962c0f28ce6SDave Jiang tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)); 963c0f28ce6SDave Jiang 964c0f28ce6SDave Jiang if (tmo == 0 || 965c0f28ce6SDave Jiang dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) { 966c0f28ce6SDave Jiang dev_err(dev, "Self-test 2nd validate timed out\n"); 967c0f28ce6SDave Jiang err = -ENODEV; 968c0f28ce6SDave Jiang goto dma_unmap; 969c0f28ce6SDave Jiang } 970c0f28ce6SDave Jiang 971c0f28ce6SDave Jiang if (xor_val_result != SUM_CHECK_P_RESULT) { 972c0f28ce6SDave Jiang dev_err(dev, "Self-test validate failed compare\n"); 973c0f28ce6SDave Jiang err = -ENODEV; 974c0f28ce6SDave Jiang goto dma_unmap; 975c0f28ce6SDave Jiang } 976c0f28ce6SDave Jiang 977c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) 978c0f28ce6SDave Jiang dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE); 979c0f28ce6SDave Jiang 980c0f28ce6SDave Jiang goto free_resources; 981c0f28ce6SDave Jiang dma_unmap: 982c0f28ce6SDave Jiang if (op == IOAT_OP_XOR) { 983c0f28ce6SDave Jiang if (dest_dma != DMA_ERROR_CODE) 984c0f28ce6SDave Jiang dma_unmap_page(dev, dest_dma, PAGE_SIZE, 985c0f28ce6SDave Jiang DMA_FROM_DEVICE); 986c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST; i++) 987c0f28ce6SDave Jiang if (dma_srcs[i] != DMA_ERROR_CODE) 988c0f28ce6SDave Jiang dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, 989c0f28ce6SDave Jiang DMA_TO_DEVICE); 990c0f28ce6SDave Jiang } else if (op == IOAT_OP_XOR_VAL) { 991c0f28ce6SDave Jiang for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) 992c0f28ce6SDave Jiang if (dma_srcs[i] != DMA_ERROR_CODE) 993c0f28ce6SDave Jiang dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, 994c0f28ce6SDave Jiang DMA_TO_DEVICE); 995c0f28ce6SDave Jiang } 996c0f28ce6SDave Jiang free_resources: 997c0f28ce6SDave Jiang dma->device_free_chan_resources(dma_chan); 998c0f28ce6SDave Jiang out: 999c0f28ce6SDave Jiang src_idx = IOAT_NUM_SRC_TEST; 1000c0f28ce6SDave Jiang while (src_idx--) 1001c0f28ce6SDave Jiang __free_page(xor_srcs[src_idx]); 1002c0f28ce6SDave Jiang __free_page(dest); 1003c0f28ce6SDave Jiang return err; 1004c0f28ce6SDave Jiang } 1005c0f28ce6SDave Jiang 1006c0f28ce6SDave Jiang static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma) 1007c0f28ce6SDave Jiang { 1008c0f28ce6SDave Jiang int rc = ioat_dma_self_test(ioat_dma); 1009c0f28ce6SDave Jiang 1010c0f28ce6SDave Jiang if (rc) 1011c0f28ce6SDave Jiang return rc; 1012c0f28ce6SDave Jiang 1013c0f28ce6SDave Jiang rc = ioat_xor_val_self_test(ioat_dma); 1014c0f28ce6SDave Jiang if (rc) 1015c0f28ce6SDave Jiang return rc; 1016c0f28ce6SDave Jiang 1017c0f28ce6SDave Jiang return 0; 1018c0f28ce6SDave Jiang } 1019c0f28ce6SDave Jiang 1020*3372de58SDave Jiang static void ioat_intr_quirk(struct ioatdma_device *ioat_dma) 1021c0f28ce6SDave Jiang { 1022c0f28ce6SDave Jiang struct dma_device *dma; 1023c0f28ce6SDave Jiang struct dma_chan *c; 1024c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan; 1025c0f28ce6SDave Jiang u32 errmask; 1026c0f28ce6SDave Jiang 1027c0f28ce6SDave Jiang dma = &ioat_dma->dma_dev; 1028c0f28ce6SDave Jiang 1029c0f28ce6SDave Jiang /* 1030c0f28ce6SDave Jiang * if we have descriptor write back error status, we mask the 1031c0f28ce6SDave Jiang * error interrupts 1032c0f28ce6SDave Jiang */ 1033c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_DWBES) { 1034c0f28ce6SDave Jiang list_for_each_entry(c, &dma->channels, device_node) { 1035c0f28ce6SDave Jiang ioat_chan = to_ioat_chan(c); 1036c0f28ce6SDave Jiang errmask = readl(ioat_chan->reg_base + 1037c0f28ce6SDave Jiang IOAT_CHANERR_MASK_OFFSET); 1038c0f28ce6SDave Jiang errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR | 1039c0f28ce6SDave Jiang IOAT_CHANERR_XOR_Q_ERR; 1040c0f28ce6SDave Jiang writel(errmask, ioat_chan->reg_base + 1041c0f28ce6SDave Jiang IOAT_CHANERR_MASK_OFFSET); 1042c0f28ce6SDave Jiang } 1043c0f28ce6SDave Jiang } 1044c0f28ce6SDave Jiang } 1045c0f28ce6SDave Jiang 1046599d49deSDave Jiang static int ioat3_dma_probe(struct ioatdma_device *ioat_dma, int dca) 1047c0f28ce6SDave Jiang { 1048c0f28ce6SDave Jiang struct pci_dev *pdev = ioat_dma->pdev; 1049c0f28ce6SDave Jiang int dca_en = system_has_dca_enabled(pdev); 1050c0f28ce6SDave Jiang struct dma_device *dma; 1051c0f28ce6SDave Jiang struct dma_chan *c; 1052c0f28ce6SDave Jiang struct ioatdma_chan *ioat_chan; 1053c0f28ce6SDave Jiang bool is_raid_device = false; 1054c0f28ce6SDave Jiang int err; 1055c0f28ce6SDave Jiang 1056c0f28ce6SDave Jiang ioat_dma->enumerate_channels = ioat_enumerate_channels; 1057c0f28ce6SDave Jiang ioat_dma->reset_hw = ioat_reset_hw; 1058c0f28ce6SDave Jiang ioat_dma->self_test = ioat3_dma_self_test; 1059*3372de58SDave Jiang ioat_dma->intr_quirk = ioat_intr_quirk; 1060c0f28ce6SDave Jiang dma = &ioat_dma->dma_dev; 1061c0f28ce6SDave Jiang dma->device_prep_dma_memcpy = ioat_dma_prep_memcpy_lock; 1062c0f28ce6SDave Jiang dma->device_issue_pending = ioat_issue_pending; 1063c0f28ce6SDave Jiang dma->device_alloc_chan_resources = ioat_alloc_chan_resources; 1064c0f28ce6SDave Jiang dma->device_free_chan_resources = ioat_free_chan_resources; 1065c0f28ce6SDave Jiang 1066c0f28ce6SDave Jiang dma_cap_set(DMA_INTERRUPT, dma->cap_mask); 1067c0f28ce6SDave Jiang dma->device_prep_dma_interrupt = ioat_prep_interrupt_lock; 1068c0f28ce6SDave Jiang 1069c0f28ce6SDave Jiang ioat_dma->cap = readl(ioat_dma->reg_base + IOAT_DMA_CAP_OFFSET); 1070c0f28ce6SDave Jiang 1071c0f28ce6SDave Jiang if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev)) 1072c0f28ce6SDave Jiang ioat_dma->cap &= 1073c0f28ce6SDave Jiang ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS); 1074c0f28ce6SDave Jiang 1075c0f28ce6SDave Jiang /* dca is incompatible with raid operations */ 1076c0f28ce6SDave Jiang if (dca_en && (ioat_dma->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ))) 1077c0f28ce6SDave Jiang ioat_dma->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ); 1078c0f28ce6SDave Jiang 1079c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_XOR) { 1080c0f28ce6SDave Jiang is_raid_device = true; 1081c0f28ce6SDave Jiang dma->max_xor = 8; 1082c0f28ce6SDave Jiang 1083c0f28ce6SDave Jiang dma_cap_set(DMA_XOR, dma->cap_mask); 1084c0f28ce6SDave Jiang dma->device_prep_dma_xor = ioat_prep_xor; 1085c0f28ce6SDave Jiang 1086c0f28ce6SDave Jiang dma_cap_set(DMA_XOR_VAL, dma->cap_mask); 1087c0f28ce6SDave Jiang dma->device_prep_dma_xor_val = ioat_prep_xor_val; 1088c0f28ce6SDave Jiang } 1089c0f28ce6SDave Jiang 1090c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_PQ) { 1091c0f28ce6SDave Jiang is_raid_device = true; 1092c0f28ce6SDave Jiang 1093c0f28ce6SDave Jiang dma->device_prep_dma_pq = ioat_prep_pq; 1094c0f28ce6SDave Jiang dma->device_prep_dma_pq_val = ioat_prep_pq_val; 1095c0f28ce6SDave Jiang dma_cap_set(DMA_PQ, dma->cap_mask); 1096c0f28ce6SDave Jiang dma_cap_set(DMA_PQ_VAL, dma->cap_mask); 1097c0f28ce6SDave Jiang 1098c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_RAID16SS) 1099c0f28ce6SDave Jiang dma_set_maxpq(dma, 16, 0); 1100c0f28ce6SDave Jiang else 1101c0f28ce6SDave Jiang dma_set_maxpq(dma, 8, 0); 1102c0f28ce6SDave Jiang 1103c0f28ce6SDave Jiang if (!(ioat_dma->cap & IOAT_CAP_XOR)) { 1104c0f28ce6SDave Jiang dma->device_prep_dma_xor = ioat_prep_pqxor; 1105c0f28ce6SDave Jiang dma->device_prep_dma_xor_val = ioat_prep_pqxor_val; 1106c0f28ce6SDave Jiang dma_cap_set(DMA_XOR, dma->cap_mask); 1107c0f28ce6SDave Jiang dma_cap_set(DMA_XOR_VAL, dma->cap_mask); 1108c0f28ce6SDave Jiang 1109c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_RAID16SS) 1110c0f28ce6SDave Jiang dma->max_xor = 16; 1111c0f28ce6SDave Jiang else 1112c0f28ce6SDave Jiang dma->max_xor = 8; 1113c0f28ce6SDave Jiang } 1114c0f28ce6SDave Jiang } 1115c0f28ce6SDave Jiang 1116c0f28ce6SDave Jiang dma->device_tx_status = ioat_tx_status; 1117c0f28ce6SDave Jiang ioat_dma->cleanup_fn = ioat_cleanup_event; 1118c0f28ce6SDave Jiang ioat_dma->timer_fn = ioat_timer_event; 1119c0f28ce6SDave Jiang 1120c0f28ce6SDave Jiang /* starting with CB3.3 super extended descriptors are supported */ 1121c0f28ce6SDave Jiang if (ioat_dma->cap & IOAT_CAP_RAID16SS) { 1122c0f28ce6SDave Jiang char pool_name[14]; 1123c0f28ce6SDave Jiang int i; 1124c0f28ce6SDave Jiang 1125c0f28ce6SDave Jiang for (i = 0; i < MAX_SED_POOLS; i++) { 1126c0f28ce6SDave Jiang snprintf(pool_name, 14, "ioat_hw%d_sed", i); 1127c0f28ce6SDave Jiang 1128c0f28ce6SDave Jiang /* allocate SED DMA pool */ 1129c0f28ce6SDave Jiang ioat_dma->sed_hw_pool[i] = dmam_pool_create(pool_name, 1130c0f28ce6SDave Jiang &pdev->dev, 1131c0f28ce6SDave Jiang SED_SIZE * (i + 1), 64, 0); 1132c0f28ce6SDave Jiang if (!ioat_dma->sed_hw_pool[i]) 1133c0f28ce6SDave Jiang return -ENOMEM; 1134c0f28ce6SDave Jiang 1135c0f28ce6SDave Jiang } 1136c0f28ce6SDave Jiang } 1137c0f28ce6SDave Jiang 1138c0f28ce6SDave Jiang if (!(ioat_dma->cap & (IOAT_CAP_XOR | IOAT_CAP_PQ))) 1139c0f28ce6SDave Jiang dma_cap_set(DMA_PRIVATE, dma->cap_mask); 1140c0f28ce6SDave Jiang 1141c0f28ce6SDave Jiang err = ioat_probe(ioat_dma); 1142c0f28ce6SDave Jiang if (err) 1143c0f28ce6SDave Jiang return err; 1144c0f28ce6SDave Jiang 1145c0f28ce6SDave Jiang list_for_each_entry(c, &dma->channels, device_node) { 1146c0f28ce6SDave Jiang ioat_chan = to_ioat_chan(c); 1147c0f28ce6SDave Jiang writel(IOAT_DMA_DCA_ANY_CPU, 1148c0f28ce6SDave Jiang ioat_chan->reg_base + IOAT_DCACTRL_OFFSET); 1149c0f28ce6SDave Jiang } 1150c0f28ce6SDave Jiang 1151c0f28ce6SDave Jiang err = ioat_register(ioat_dma); 1152c0f28ce6SDave Jiang if (err) 1153c0f28ce6SDave Jiang return err; 1154c0f28ce6SDave Jiang 1155c0f28ce6SDave Jiang ioat_kobject_add(ioat_dma, &ioat_ktype); 1156c0f28ce6SDave Jiang 1157c0f28ce6SDave Jiang if (dca) 1158*3372de58SDave Jiang ioat_dma->dca = ioat_dca_init(pdev, ioat_dma->reg_base); 1159c0f28ce6SDave Jiang 1160c0f28ce6SDave Jiang return 0; 1161c0f28ce6SDave Jiang } 1162c0f28ce6SDave Jiang 1163c0f28ce6SDave Jiang #define DRV_NAME "ioatdma" 1164c0f28ce6SDave Jiang 1165c0f28ce6SDave Jiang static struct pci_driver ioat_pci_driver = { 1166c0f28ce6SDave Jiang .name = DRV_NAME, 1167c0f28ce6SDave Jiang .id_table = ioat_pci_tbl, 1168c0f28ce6SDave Jiang .probe = ioat_pci_probe, 1169c0f28ce6SDave Jiang .remove = ioat_remove, 1170c0f28ce6SDave Jiang }; 1171c0f28ce6SDave Jiang 1172c0f28ce6SDave Jiang static struct ioatdma_device * 1173c0f28ce6SDave Jiang alloc_ioatdma(struct pci_dev *pdev, void __iomem *iobase) 1174c0f28ce6SDave Jiang { 1175c0f28ce6SDave Jiang struct device *dev = &pdev->dev; 1176c0f28ce6SDave Jiang struct ioatdma_device *d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL); 1177c0f28ce6SDave Jiang 1178c0f28ce6SDave Jiang if (!d) 1179c0f28ce6SDave Jiang return NULL; 1180c0f28ce6SDave Jiang d->pdev = pdev; 1181c0f28ce6SDave Jiang d->reg_base = iobase; 1182c0f28ce6SDave Jiang return d; 1183c0f28ce6SDave Jiang } 1184c0f28ce6SDave Jiang 1185c0f28ce6SDave Jiang static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1186c0f28ce6SDave Jiang { 1187c0f28ce6SDave Jiang void __iomem * const *iomap; 1188c0f28ce6SDave Jiang struct device *dev = &pdev->dev; 1189c0f28ce6SDave Jiang struct ioatdma_device *device; 1190c0f28ce6SDave Jiang int err; 1191c0f28ce6SDave Jiang 1192c0f28ce6SDave Jiang err = pcim_enable_device(pdev); 1193c0f28ce6SDave Jiang if (err) 1194c0f28ce6SDave Jiang return err; 1195c0f28ce6SDave Jiang 1196c0f28ce6SDave Jiang err = pcim_iomap_regions(pdev, 1 << IOAT_MMIO_BAR, DRV_NAME); 1197c0f28ce6SDave Jiang if (err) 1198c0f28ce6SDave Jiang return err; 1199c0f28ce6SDave Jiang iomap = pcim_iomap_table(pdev); 1200c0f28ce6SDave Jiang if (!iomap) 1201c0f28ce6SDave Jiang return -ENOMEM; 1202c0f28ce6SDave Jiang 1203c0f28ce6SDave Jiang err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 1204c0f28ce6SDave Jiang if (err) 1205c0f28ce6SDave Jiang err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 1206c0f28ce6SDave Jiang if (err) 1207c0f28ce6SDave Jiang return err; 1208c0f28ce6SDave Jiang 1209c0f28ce6SDave Jiang err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 1210c0f28ce6SDave Jiang if (err) 1211c0f28ce6SDave Jiang err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 1212c0f28ce6SDave Jiang if (err) 1213c0f28ce6SDave Jiang return err; 1214c0f28ce6SDave Jiang 1215c0f28ce6SDave Jiang device = alloc_ioatdma(pdev, iomap[IOAT_MMIO_BAR]); 1216c0f28ce6SDave Jiang if (!device) 1217c0f28ce6SDave Jiang return -ENOMEM; 1218c0f28ce6SDave Jiang pci_set_master(pdev); 1219c0f28ce6SDave Jiang pci_set_drvdata(pdev, device); 1220c0f28ce6SDave Jiang 1221c0f28ce6SDave Jiang device->version = readb(device->reg_base + IOAT_VER_OFFSET); 1222c0f28ce6SDave Jiang if (device->version >= IOAT_VER_3_0) 1223c0f28ce6SDave Jiang err = ioat3_dma_probe(device, ioat_dca_enabled); 1224c0f28ce6SDave Jiang else 1225c0f28ce6SDave Jiang return -ENODEV; 1226c0f28ce6SDave Jiang 1227c0f28ce6SDave Jiang if (err) { 1228c0f28ce6SDave Jiang dev_err(dev, "Intel(R) I/OAT DMA Engine init failed\n"); 1229c0f28ce6SDave Jiang return -ENODEV; 1230c0f28ce6SDave Jiang } 1231c0f28ce6SDave Jiang 1232c0f28ce6SDave Jiang return 0; 1233c0f28ce6SDave Jiang } 1234c0f28ce6SDave Jiang 1235c0f28ce6SDave Jiang static void ioat_remove(struct pci_dev *pdev) 1236c0f28ce6SDave Jiang { 1237c0f28ce6SDave Jiang struct ioatdma_device *device = pci_get_drvdata(pdev); 1238c0f28ce6SDave Jiang 1239c0f28ce6SDave Jiang if (!device) 1240c0f28ce6SDave Jiang return; 1241c0f28ce6SDave Jiang 1242c0f28ce6SDave Jiang dev_err(&pdev->dev, "Removing dma and dca services\n"); 1243c0f28ce6SDave Jiang if (device->dca) { 1244c0f28ce6SDave Jiang unregister_dca_provider(device->dca, &pdev->dev); 1245c0f28ce6SDave Jiang free_dca_provider(device->dca); 1246c0f28ce6SDave Jiang device->dca = NULL; 1247c0f28ce6SDave Jiang } 1248c0f28ce6SDave Jiang ioat_dma_remove(device); 1249c0f28ce6SDave Jiang } 1250c0f28ce6SDave Jiang 1251c0f28ce6SDave Jiang static int __init ioat_init_module(void) 1252c0f28ce6SDave Jiang { 1253c0f28ce6SDave Jiang int err = -ENOMEM; 1254c0f28ce6SDave Jiang 1255c0f28ce6SDave Jiang pr_info("%s: Intel(R) QuickData Technology Driver %s\n", 1256c0f28ce6SDave Jiang DRV_NAME, IOAT_DMA_VERSION); 1257c0f28ce6SDave Jiang 1258c0f28ce6SDave Jiang ioat_cache = kmem_cache_create("ioat", sizeof(struct ioat_ring_ent), 1259c0f28ce6SDave Jiang 0, SLAB_HWCACHE_ALIGN, NULL); 1260c0f28ce6SDave Jiang if (!ioat_cache) 1261c0f28ce6SDave Jiang return -ENOMEM; 1262c0f28ce6SDave Jiang 1263c0f28ce6SDave Jiang ioat_sed_cache = KMEM_CACHE(ioat_sed_ent, 0); 1264c0f28ce6SDave Jiang if (!ioat_sed_cache) 1265c0f28ce6SDave Jiang goto err_ioat_cache; 1266c0f28ce6SDave Jiang 1267c0f28ce6SDave Jiang err = pci_register_driver(&ioat_pci_driver); 1268c0f28ce6SDave Jiang if (err) 1269c0f28ce6SDave Jiang goto err_ioat3_cache; 1270c0f28ce6SDave Jiang 1271c0f28ce6SDave Jiang return 0; 1272c0f28ce6SDave Jiang 1273c0f28ce6SDave Jiang err_ioat3_cache: 1274c0f28ce6SDave Jiang kmem_cache_destroy(ioat_sed_cache); 1275c0f28ce6SDave Jiang 1276c0f28ce6SDave Jiang err_ioat_cache: 1277c0f28ce6SDave Jiang kmem_cache_destroy(ioat_cache); 1278c0f28ce6SDave Jiang 1279c0f28ce6SDave Jiang return err; 1280c0f28ce6SDave Jiang } 1281c0f28ce6SDave Jiang module_init(ioat_init_module); 1282c0f28ce6SDave Jiang 1283c0f28ce6SDave Jiang static void __exit ioat_exit_module(void) 1284c0f28ce6SDave Jiang { 1285c0f28ce6SDave Jiang pci_unregister_driver(&ioat_pci_driver); 1286c0f28ce6SDave Jiang kmem_cache_destroy(ioat_cache); 1287c0f28ce6SDave Jiang } 1288c0f28ce6SDave Jiang module_exit(ioat_exit_module); 1289