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