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