ioatdma: S1200 platforms ioatdma channel 2 and 3 falsely advertise RAID cap
[deliverable/linux.git] / drivers / dma / ioat / dma_v3.c
CommitLineData
bf40a686
DW
1/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * The full GNU General Public License is included in this distribution in
23 * the file called "COPYING".
24 *
25 * BSD LICENSE
26 *
27 * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions are met:
31 *
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in
36 * the documentation and/or other materials provided with the
37 * distribution.
38 * * Neither the name of Intel Corporation nor the names of its
39 * contributors may be used to endorse or promote products derived
40 * from this software without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
53 */
54
55/*
56 * Support routines for v3+ hardware
57 */
7727eaa4 58#include <linux/module.h>
bf40a686 59#include <linux/pci.h>
5a0e3ad6 60#include <linux/gfp.h>
bf40a686
DW
61#include <linux/dmaengine.h>
62#include <linux/dma-mapping.h>
70c71606 63#include <linux/prefetch.h>
949ff5b8 64#include "../dmaengine.h"
bf40a686
DW
65#include "registers.h"
66#include "hw.h"
67#include "dma.h"
68#include "dma_v2.h"
69
b094ad3b
DW
70/* ioat hardware assumes at least two sources for raid operations */
71#define src_cnt_to_sw(x) ((x) + 2)
72#define src_cnt_to_hw(x) ((x) - 2)
7727eaa4
DJ
73#define ndest_to_sw(x) ((x) + 1)
74#define ndest_to_hw(x) ((x) - 1)
75#define src16_cnt_to_sw(x) ((x) + 9)
76#define src16_cnt_to_hw(x) ((x) - 9)
b094ad3b
DW
77
78/* provide a lookup table for setting the source address in the base or
d69d235b 79 * extended descriptor of an xor or pq descriptor
b094ad3b 80 */
d0b0c8c7 81static const u8 xor_idx_to_desc = 0xe0;
9b487ced
AK
82static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
83static const u8 pq_idx_to_desc = 0xf8;
7727eaa4
DJ
84static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
85 2, 2, 2, 2, 2, 2, 2 };
9b487ced 86static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
7727eaa4
DJ
87static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
88 0, 1, 2, 3, 4, 5, 6 };
89
90/*
91 * technically sources 1 and 2 do not require SED, but the op will have
92 * at least 9 descriptors so that's irrelevant.
93 */
94static const u8 pq16_idx_to_sed[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 1, 1, 1, 1, 1, 1, 1 };
b094ad3b 96
3f09ede4
DJ
97static void ioat3_eh(struct ioat2_dma_chan *ioat);
98
b094ad3b
DW
99static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
100{
101 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
102
103 return raw->field[xor_idx_to_field[idx]];
104}
105
106static void xor_set_src(struct ioat_raw_descriptor *descs[2],
107 dma_addr_t addr, u32 offset, int idx)
108{
109 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
110
111 raw->field[xor_idx_to_field[idx]] = addr + offset;
112}
113
d69d235b
DW
114static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
115{
116 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
117
118 return raw->field[pq_idx_to_field[idx]];
119}
120
7727eaa4
DJ
121static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
122{
123 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
124
125 return raw->field[pq16_idx_to_field[idx]];
126}
127
d69d235b
DW
128static void pq_set_src(struct ioat_raw_descriptor *descs[2],
129 dma_addr_t addr, u32 offset, u8 coef, int idx)
130{
131 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
132 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
133
134 raw->field[pq_idx_to_field[idx]] = addr + offset;
135 pq->coef[idx] = coef;
136}
137
7727eaa4
DJ
138static int sed_get_pq16_pool_idx(int src_cnt)
139{
140
141 return pq16_idx_to_sed[src_cnt];
142}
143
8a52b9ff
DJ
144static bool is_jf_ioat(struct pci_dev *pdev)
145{
146 switch (pdev->device) {
147 case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
148 case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
149 case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
150 case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
151 case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
152 case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
153 case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
154 case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
155 case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
156 case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
157 return true;
158 default:
159 return false;
160 }
161}
162
163static bool is_snb_ioat(struct pci_dev *pdev)
164{
165 switch (pdev->device) {
166 case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
167 case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
168 case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
169 case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
170 case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
171 case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
172 case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
173 case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
174 case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
175 case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
176 return true;
177 default:
178 return false;
179 }
180}
181
182static bool is_ivb_ioat(struct pci_dev *pdev)
183{
184 switch (pdev->device) {
185 case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
186 case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
187 case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
188 case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
189 case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
190 case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
191 case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
192 case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
193 case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
194 case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
195 return true;
196 default:
197 return false;
198 }
199
200}
201
202static bool is_hsw_ioat(struct pci_dev *pdev)
203{
204 switch (pdev->device) {
205 case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
206 case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
207 case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
208 case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
209 case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
210 case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
211 case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
212 case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
213 case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
214 case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
215 return true;
216 default:
217 return false;
218 }
219
220}
221
222static bool is_xeon_cb32(struct pci_dev *pdev)
223{
224 return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
225 is_hsw_ioat(pdev);
226}
227
228static bool is_bwd_ioat(struct pci_dev *pdev)
229{
230 switch (pdev->device) {
231 case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
232 case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
233 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
234 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
235 return true;
236 default:
237 return false;
238 }
239}
240
d302398d
DJ
241static bool is_bwd_noraid(struct pci_dev *pdev)
242{
243 switch (pdev->device) {
244 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
245 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
246 return true;
247 default:
248 return false;
249 }
250
251}
252
7727eaa4
DJ
253static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
254 dma_addr_t addr, u32 offset, u8 coef, int idx)
255{
256 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
257 struct ioat_pq16a_descriptor *pq16 =
258 (struct ioat_pq16a_descriptor *)desc[1];
259 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
260
261 raw->field[pq16_idx_to_field[idx]] = addr + offset;
262
263 if (idx < 8)
264 pq->coef[idx] = coef;
265 else
266 pq16->coef[idx - 8] = coef;
267}
268
269struct ioat_sed_ent *
270ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
271{
272 struct ioat_sed_ent *sed;
273 gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
274
275 sed = kmem_cache_alloc(device->sed_pool, flags);
276 if (!sed)
277 return NULL;
278
279 sed->hw_pool = hw_pool;
280 sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
281 flags, &sed->dma);
282 if (!sed->hw) {
283 kmem_cache_free(device->sed_pool, sed);
284 return NULL;
285 }
286
287 return sed;
288}
289
290void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
291{
292 if (!sed)
293 return;
294
295 dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
296 kmem_cache_free(device->sed_pool, sed);
297}
298
bf40a686 299static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
b094ad3b 300 struct ioat_ring_ent *desc, int idx)
bf40a686
DW
301{
302 struct ioat_chan_common *chan = &ioat->base;
303 struct pci_dev *pdev = chan->device->pdev;
304 size_t len = desc->len;
305 size_t offset = len - desc->hw->size;
306 struct dma_async_tx_descriptor *tx = &desc->txd;
307 enum dma_ctrl_flags flags = tx->flags;
308
309 switch (desc->hw->ctl_f.op) {
310 case IOAT_OP_COPY:
58c8649e
DW
311 if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
312 ioat_dma_unmap(chan, flags, len, desc->hw);
bf40a686
DW
313 break;
314 case IOAT_OP_FILL: {
315 struct ioat_fill_descriptor *hw = desc->fill;
316
317 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
318 ioat_unmap(pdev, hw->dst_addr - offset, len,
319 PCI_DMA_FROMDEVICE, flags, 1);
320 break;
321 }
b094ad3b
DW
322 case IOAT_OP_XOR_VAL:
323 case IOAT_OP_XOR: {
324 struct ioat_xor_descriptor *xor = desc->xor;
325 struct ioat_ring_ent *ext;
326 struct ioat_xor_ext_descriptor *xor_ex = NULL;
327 int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
328 struct ioat_raw_descriptor *descs[2];
329 int i;
330
331 if (src_cnt > 5) {
332 ext = ioat2_get_ring_ent(ioat, idx + 1);
333 xor_ex = ext->xor_ex;
334 }
335
336 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
337 descs[0] = (struct ioat_raw_descriptor *) xor;
338 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
339 for (i = 0; i < src_cnt; i++) {
340 dma_addr_t src = xor_get_src(descs, i);
341
342 ioat_unmap(pdev, src - offset, len,
343 PCI_DMA_TODEVICE, flags, 0);
344 }
345
346 /* dest is a source in xor validate operations */
347 if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
348 ioat_unmap(pdev, xor->dst_addr - offset, len,
349 PCI_DMA_TODEVICE, flags, 1);
350 break;
351 }
352 }
353
354 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
355 ioat_unmap(pdev, xor->dst_addr - offset, len,
356 PCI_DMA_FROMDEVICE, flags, 1);
357 break;
358 }
d69d235b
DW
359 case IOAT_OP_PQ_VAL:
360 case IOAT_OP_PQ: {
361 struct ioat_pq_descriptor *pq = desc->pq;
362 struct ioat_ring_ent *ext;
363 struct ioat_pq_ext_descriptor *pq_ex = NULL;
364 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
365 struct ioat_raw_descriptor *descs[2];
366 int i;
367
368 if (src_cnt > 3) {
369 ext = ioat2_get_ring_ent(ioat, idx + 1);
370 pq_ex = ext->pq_ex;
371 }
372
373 /* in the 'continue' case don't unmap the dests as sources */
374 if (dmaf_p_disabled_continue(flags))
375 src_cnt--;
376 else if (dmaf_continue(flags))
377 src_cnt -= 3;
378
379 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
380 descs[0] = (struct ioat_raw_descriptor *) pq;
381 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
382 for (i = 0; i < src_cnt; i++) {
383 dma_addr_t src = pq_get_src(descs, i);
384
385 ioat_unmap(pdev, src - offset, len,
386 PCI_DMA_TODEVICE, flags, 0);
387 }
388
389 /* the dests are sources in pq validate operations */
390 if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
391 if (!(flags & DMA_PREP_PQ_DISABLE_P))
392 ioat_unmap(pdev, pq->p_addr - offset,
393 len, PCI_DMA_TODEVICE, flags, 0);
394 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
395 ioat_unmap(pdev, pq->q_addr - offset,
396 len, PCI_DMA_TODEVICE, flags, 0);
397 break;
398 }
399 }
400
401 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
402 if (!(flags & DMA_PREP_PQ_DISABLE_P))
403 ioat_unmap(pdev, pq->p_addr - offset, len,
404 PCI_DMA_BIDIRECTIONAL, flags, 1);
405 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
406 ioat_unmap(pdev, pq->q_addr - offset, len,
407 PCI_DMA_BIDIRECTIONAL, flags, 1);
408 }
409 break;
410 }
7727eaa4
DJ
411 case IOAT_OP_PQ_16S:
412 case IOAT_OP_PQ_VAL_16S: {
413 struct ioat_pq_descriptor *pq = desc->pq;
414 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
415 struct ioat_raw_descriptor *descs[4];
416 int i;
417
418 /* in the 'continue' case don't unmap the dests as sources */
419 if (dmaf_p_disabled_continue(flags))
420 src_cnt--;
421 else if (dmaf_continue(flags))
422 src_cnt -= 3;
423
424 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
425 descs[0] = (struct ioat_raw_descriptor *)pq;
426 descs[1] = (struct ioat_raw_descriptor *)(desc->sed->hw);
427 descs[2] = (struct ioat_raw_descriptor *)(&desc->sed->hw->b[0]);
428 for (i = 0; i < src_cnt; i++) {
429 dma_addr_t src = pq16_get_src(descs, i);
430
431 ioat_unmap(pdev, src - offset, len,
432 PCI_DMA_TODEVICE, flags, 0);
433 }
434
435 /* the dests are sources in pq validate operations */
436 if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
437 if (!(flags & DMA_PREP_PQ_DISABLE_P))
438 ioat_unmap(pdev, pq->p_addr - offset,
439 len, PCI_DMA_TODEVICE,
440 flags, 0);
441 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
442 ioat_unmap(pdev, pq->q_addr - offset,
443 len, PCI_DMA_TODEVICE,
444 flags, 0);
445 break;
446 }
447 }
448
449 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
450 if (!(flags & DMA_PREP_PQ_DISABLE_P))
451 ioat_unmap(pdev, pq->p_addr - offset, len,
452 PCI_DMA_BIDIRECTIONAL, flags, 1);
453 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
454 ioat_unmap(pdev, pq->q_addr - offset, len,
455 PCI_DMA_BIDIRECTIONAL, flags, 1);
456 }
457 break;
458 }
bf40a686
DW
459 default:
460 dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
461 __func__, desc->hw->ctl_f.op);
462 }
463}
464
b094ad3b
DW
465static bool desc_has_ext(struct ioat_ring_ent *desc)
466{
467 struct ioat_dma_descriptor *hw = desc->hw;
468
469 if (hw->ctl_f.op == IOAT_OP_XOR ||
470 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
471 struct ioat_xor_descriptor *xor = desc->xor;
bf40a686 472
b094ad3b
DW
473 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
474 return true;
d69d235b
DW
475 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
476 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
477 struct ioat_pq_descriptor *pq = desc->pq;
478
479 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
480 return true;
b094ad3b
DW
481 }
482
483 return false;
484}
485
3f09ede4
DJ
486static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
487{
488 u64 phys_complete;
489 u64 completion;
490
491 completion = *chan->completion;
492 phys_complete = ioat_chansts_to_addr(completion);
493
494 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
495 (unsigned long long) phys_complete);
496
497 return phys_complete;
498}
499
500static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
501 u64 *phys_complete)
502{
503 *phys_complete = ioat3_get_current_completion(chan);
504 if (*phys_complete == chan->last_completion)
505 return false;
506
507 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
508 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
509
510 return true;
511}
512
b094ad3b
DW
513/**
514 * __cleanup - reclaim used descriptors
515 * @ioat: channel (ring) to clean
516 *
517 * The difference from the dma_v2.c __cleanup() is that this routine
518 * handles extended descriptors and dma-unmapping raid operations.
519 */
27502935 520static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
bf40a686
DW
521{
522 struct ioat_chan_common *chan = &ioat->base;
7727eaa4 523 struct ioatdma_device *device = chan->device;
bf40a686
DW
524 struct ioat_ring_ent *desc;
525 bool seen_current = false;
074cc476 526 int idx = ioat->tail, i;
bf40a686 527 u16 active;
bf40a686
DW
528
529 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
530 __func__, ioat->head, ioat->tail, ioat->issued);
531
3f09ede4
DJ
532 /*
533 * At restart of the channel, the completion address and the
534 * channel status will be 0 due to starting a new chain. Since
535 * it's new chain and the first descriptor "fails", there is
536 * nothing to clean up. We do not want to reap the entire submitted
537 * chain due to this 0 address value and then BUG.
538 */
539 if (!phys_complete)
540 return;
541
bf40a686
DW
542 active = ioat2_ring_active(ioat);
543 for (i = 0; i < active && !seen_current; i++) {
544 struct dma_async_tx_descriptor *tx;
545
074cc476
DW
546 smp_read_barrier_depends();
547 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
548 desc = ioat2_get_ring_ent(ioat, idx + i);
bf40a686
DW
549 dump_desc_dbg(ioat, desc);
550 tx = &desc->txd;
551 if (tx->cookie) {
f7fbce07 552 dma_cookie_complete(tx);
074cc476 553 ioat3_dma_unmap(ioat, desc, idx + i);
bf40a686
DW
554 if (tx->callback) {
555 tx->callback(tx->callback_param);
556 tx->callback = NULL;
557 }
558 }
559
560 if (tx->phys == phys_complete)
561 seen_current = true;
b094ad3b
DW
562
563 /* skip extended descriptors */
564 if (desc_has_ext(desc)) {
565 BUG_ON(i + 1 >= active);
566 i++;
567 }
7727eaa4
DJ
568
569 /* cleanup super extended descriptors */
570 if (desc->sed) {
571 ioat3_free_sed(device, desc->sed);
572 desc->sed = NULL;
573 }
bf40a686 574 }
074cc476
DW
575 smp_mb(); /* finish all descriptor reads before incrementing tail */
576 ioat->tail = idx + i;
aa75db00 577 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
bf40a686 578 chan->last_completion = phys_complete;
b9cc9869 579
074cc476 580 if (active - i == 0) {
bf40a686
DW
581 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
582 __func__);
583 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
584 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
585 }
b9cc9869 586 /* 5 microsecond delay per pending descriptor */
074cc476 587 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
b9cc9869 588 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
bf40a686
DW
589}
590
074cc476 591static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
bf40a686
DW
592{
593 struct ioat_chan_common *chan = &ioat->base;
3f09ede4 594 u64 phys_complete;
bf40a686 595
b9cc9869 596 spin_lock_bh(&chan->cleanup_lock);
3f09ede4
DJ
597
598 if (ioat3_cleanup_preamble(chan, &phys_complete))
074cc476 599 __cleanup(ioat, phys_complete);
3f09ede4
DJ
600
601 if (is_ioat_halted(*chan->completion)) {
602 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
603
604 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
605 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
606 ioat3_eh(ioat);
607 }
608 }
609
b9cc9869
DW
610 spin_unlock_bh(&chan->cleanup_lock);
611}
612
aa4d72ae 613static void ioat3_cleanup_event(unsigned long data)
bf40a686 614{
aa4d72ae 615 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
bf40a686 616
074cc476 617 ioat3_cleanup(ioat);
773d9e2d 618 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
bf40a686
DW
619}
620
621static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
622{
623 struct ioat_chan_common *chan = &ioat->base;
3f09ede4 624 u64 phys_complete;
bf40a686 625
b372ec2d 626 ioat2_quiesce(chan, 0);
3f09ede4 627 if (ioat3_cleanup_preamble(chan, &phys_complete))
bf40a686
DW
628 __cleanup(ioat, phys_complete);
629
630 __ioat2_restart_chan(ioat);
631}
632
3f09ede4
DJ
633static void ioat3_eh(struct ioat2_dma_chan *ioat)
634{
635 struct ioat_chan_common *chan = &ioat->base;
636 struct pci_dev *pdev = to_pdev(chan);
637 struct ioat_dma_descriptor *hw;
638 u64 phys_complete;
639 struct ioat_ring_ent *desc;
640 u32 err_handled = 0;
641 u32 chanerr_int;
642 u32 chanerr;
643
644 /* cleanup so tail points to descriptor that caused the error */
645 if (ioat3_cleanup_preamble(chan, &phys_complete))
646 __cleanup(ioat, phys_complete);
647
648 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
649 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
650
651 dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
652 __func__, chanerr, chanerr_int);
653
654 desc = ioat2_get_ring_ent(ioat, ioat->tail);
655 hw = desc->hw;
656 dump_desc_dbg(ioat, desc);
657
658 switch (hw->ctl_f.op) {
659 case IOAT_OP_XOR_VAL:
660 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
661 *desc->result |= SUM_CHECK_P_RESULT;
662 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
663 }
664 break;
665 case IOAT_OP_PQ_VAL:
7727eaa4 666 case IOAT_OP_PQ_VAL_16S:
3f09ede4
DJ
667 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
668 *desc->result |= SUM_CHECK_P_RESULT;
669 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
670 }
671 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
672 *desc->result |= SUM_CHECK_Q_RESULT;
673 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
674 }
675 break;
676 }
677
678 /* fault on unhandled error or spurious halt */
679 if (chanerr ^ err_handled || chanerr == 0) {
680 dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
681 __func__, chanerr, err_handled);
682 BUG();
683 }
684
685 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
686 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
687
688 /* mark faulting descriptor as complete */
689 *chan->completion = desc->txd.phys;
690
691 spin_lock_bh(&ioat->prep_lock);
692 ioat3_restart_channel(ioat);
693 spin_unlock_bh(&ioat->prep_lock);
694}
695
4dec23d7 696static void check_active(struct ioat2_dma_chan *ioat)
bf40a686 697{
bf40a686
DW
698 struct ioat_chan_common *chan = &ioat->base;
699
4dec23d7
DJ
700 if (ioat2_ring_active(ioat)) {
701 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
702 return;
703 }
bf40a686 704
4dec23d7
DJ
705 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
706 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
707 else if (ioat->alloc_order > ioat_get_alloc_order()) {
bf40a686
DW
708 /* if the ring is idle, empty, and oversized try to step
709 * down the size
710 */
4dec23d7 711 reshape_ring(ioat, ioat->alloc_order - 1);
bf40a686
DW
712
713 /* keep shrinking until we get back to our minimum
714 * default size
715 */
716 if (ioat->alloc_order > ioat_get_alloc_order())
717 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
718 }
4dec23d7
DJ
719
720}
721
a20702b8 722static void ioat3_timer_event(unsigned long data)
4dec23d7
DJ
723{
724 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
725 struct ioat_chan_common *chan = &ioat->base;
726 dma_addr_t phys_complete;
727 u64 status;
728
729 status = ioat_chansts(chan);
730
731 /* when halted due to errors check for channel
732 * programming errors before advancing the completion state
733 */
734 if (is_ioat_halted(status)) {
735 u32 chanerr;
736
737 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
738 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
739 __func__, chanerr);
740 if (test_bit(IOAT_RUN, &chan->state))
741 BUG_ON(is_ioat_bug(chanerr));
742 else /* we never got off the ground */
743 return;
744 }
745
746 /* if we haven't made progress and we have already
747 * acknowledged a pending completion once, then be more
748 * forceful with a restart
749 */
750 spin_lock_bh(&chan->cleanup_lock);
751 if (ioat_cleanup_preamble(chan, &phys_complete))
752 __cleanup(ioat, phys_complete);
753 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
754 spin_lock_bh(&ioat->prep_lock);
755 ioat3_restart_channel(ioat);
756 spin_unlock_bh(&ioat->prep_lock);
757 spin_unlock_bh(&chan->cleanup_lock);
758 return;
759 } else {
760 set_bit(IOAT_COMPLETION_ACK, &chan->state);
761 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
762 }
763
764
765 if (ioat2_ring_active(ioat))
766 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
767 else {
768 spin_lock_bh(&ioat->prep_lock);
769 check_active(ioat);
770 spin_unlock_bh(&ioat->prep_lock);
771 }
772 spin_unlock_bh(&chan->cleanup_lock);
bf40a686
DW
773}
774
775static enum dma_status
07934481
LW
776ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
777 struct dma_tx_state *txstate)
bf40a686
DW
778{
779 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
96a2af41 780 enum dma_status ret;
bf40a686 781
96a2af41
RKAL
782 ret = dma_cookie_status(c, cookie, txstate);
783 if (ret == DMA_SUCCESS)
784 return ret;
bf40a686 785
074cc476 786 ioat3_cleanup(ioat);
bf40a686 787
96a2af41 788 return dma_cookie_status(c, cookie, txstate);
bf40a686
DW
789}
790
791static struct dma_async_tx_descriptor *
792ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
793 size_t len, unsigned long flags)
794{
795 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
796 struct ioat_ring_ent *desc;
797 size_t total_len = len;
798 struct ioat_fill_descriptor *fill;
bf40a686 799 u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
074cc476 800 int num_descs, idx, i;
bf40a686
DW
801
802 num_descs = ioat2_xferlen_to_descs(ioat, len);
074cc476
DW
803 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0)
804 idx = ioat->head;
bf40a686
DW
805 else
806 return NULL;
cdef57db
DW
807 i = 0;
808 do {
bf40a686
DW
809 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
810
811 desc = ioat2_get_ring_ent(ioat, idx + i);
812 fill = desc->fill;
813
814 fill->size = xfer_size;
815 fill->src_data = src_data;
816 fill->dst_addr = dest;
817 fill->ctl = 0;
818 fill->ctl_f.op = IOAT_OP_FILL;
819
820 len -= xfer_size;
821 dest += xfer_size;
822 dump_desc_dbg(ioat, desc);
cdef57db 823 } while (++i < num_descs);
bf40a686
DW
824
825 desc->txd.flags = flags;
826 desc->len = total_len;
827 fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
828 fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
829 fill->ctl_f.compl_write = 1;
830 dump_desc_dbg(ioat, desc);
831
832 /* we leave the channel locked to ensure in order submission */
833 return &desc->txd;
834}
835
b094ad3b
DW
836static struct dma_async_tx_descriptor *
837__ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
838 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
839 size_t len, unsigned long flags)
840{
841 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
842 struct ioat_ring_ent *compl_desc;
843 struct ioat_ring_ent *desc;
844 struct ioat_ring_ent *ext;
845 size_t total_len = len;
846 struct ioat_xor_descriptor *xor;
847 struct ioat_xor_ext_descriptor *xor_ex = NULL;
848 struct ioat_dma_descriptor *hw;
074cc476 849 int num_descs, with_ext, idx, i;
b094ad3b 850 u32 offset = 0;
b094ad3b
DW
851 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
852
853 BUG_ON(src_cnt < 2);
854
855 num_descs = ioat2_xferlen_to_descs(ioat, len);
856 /* we need 2x the number of descriptors to cover greater than 5
857 * sources
858 */
859 if (src_cnt > 5) {
860 with_ext = 1;
861 num_descs *= 2;
862 } else
863 with_ext = 0;
864
865 /* completion writes from the raid engine may pass completion
866 * writes from the legacy engine, so we need one extra null
867 * (legacy) descriptor to ensure all completion writes arrive in
868 * order.
869 */
074cc476
DW
870 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
871 idx = ioat->head;
b094ad3b
DW
872 else
873 return NULL;
cdef57db
DW
874 i = 0;
875 do {
b094ad3b
DW
876 struct ioat_raw_descriptor *descs[2];
877 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
878 int s;
879
880 desc = ioat2_get_ring_ent(ioat, idx + i);
881 xor = desc->xor;
882
883 /* save a branch by unconditionally retrieving the
884 * extended descriptor xor_set_src() knows to not write
885 * to it in the single descriptor case
886 */
887 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
888 xor_ex = ext->xor_ex;
889
890 descs[0] = (struct ioat_raw_descriptor *) xor;
891 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
892 for (s = 0; s < src_cnt; s++)
893 xor_set_src(descs, src[s], offset, s);
894 xor->size = xfer_size;
895 xor->dst_addr = dest + offset;
896 xor->ctl = 0;
897 xor->ctl_f.op = op;
898 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
899
900 len -= xfer_size;
901 offset += xfer_size;
902 dump_desc_dbg(ioat, desc);
cdef57db 903 } while ((i += 1 + with_ext) < num_descs);
b094ad3b
DW
904
905 /* last xor descriptor carries the unmap parameters and fence bit */
906 desc->txd.flags = flags;
907 desc->len = total_len;
908 if (result)
909 desc->result = result;
910 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
911
912 /* completion descriptor carries interrupt bit */
913 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
914 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
915 hw = compl_desc->hw;
916 hw->ctl = 0;
917 hw->ctl_f.null = 1;
918 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
919 hw->ctl_f.compl_write = 1;
920 hw->size = NULL_DESC_BUFFER_SIZE;
921 dump_desc_dbg(ioat, compl_desc);
922
923 /* we leave the channel locked to ensure in order submission */
49954c15 924 return &compl_desc->txd;
b094ad3b
DW
925}
926
927static struct dma_async_tx_descriptor *
928ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
929 unsigned int src_cnt, size_t len, unsigned long flags)
930{
931 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
932}
933
934struct dma_async_tx_descriptor *
935ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
936 unsigned int src_cnt, size_t len,
937 enum sum_check_flags *result, unsigned long flags)
938{
939 /* the cleanup routine only sets bits on validate failure, it
940 * does not clear bits on validate success... so clear it here
941 */
942 *result = 0;
943
944 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
945 src_cnt - 1, len, flags);
946}
947
d69d235b
DW
948static void
949dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
950{
951 struct device *dev = to_dev(&ioat->base);
952 struct ioat_pq_descriptor *pq = desc->pq;
953 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
954 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
955 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
956 int i;
957
958 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
7727eaa4
DJ
959 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
960 " src_cnt: %d)\n",
d69d235b
DW
961 desc_id(desc), (unsigned long long) desc->txd.phys,
962 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
963 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
964 pq->ctl_f.compl_write,
965 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
966 pq->ctl_f.src_cnt);
967 for (i = 0; i < src_cnt; i++)
968 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
969 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
970 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
971 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
50f9f97e 972 dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
d69d235b
DW
973}
974
7727eaa4
DJ
975static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
976 struct ioat_ring_ent *desc)
977{
978 struct device *dev = to_dev(&ioat->base);
979 struct ioat_pq_descriptor *pq = desc->pq;
980 struct ioat_raw_descriptor *descs[] = { (void *)pq,
981 (void *)pq,
982 (void *)pq };
983 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
984 int i;
985
986 if (desc->sed) {
987 descs[1] = (void *)desc->sed->hw;
988 descs[2] = (void *)desc->sed->hw + 64;
989 }
990
991 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
992 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
993 " src_cnt: %d)\n",
994 desc_id(desc), (unsigned long long) desc->txd.phys,
995 (unsigned long long) pq->next,
996 desc->txd.flags, pq->size, pq->ctl,
997 pq->ctl_f.op, pq->ctl_f.int_en,
998 pq->ctl_f.compl_write,
999 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
1000 pq->ctl_f.src_cnt);
1001 for (i = 0; i < src_cnt; i++) {
1002 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
1003 (unsigned long long) pq16_get_src(descs, i),
1004 pq->coef[i]);
1005 }
1006 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
1007 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
1008}
1009
d69d235b
DW
1010static struct dma_async_tx_descriptor *
1011__ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
1012 const dma_addr_t *dst, const dma_addr_t *src,
1013 unsigned int src_cnt, const unsigned char *scf,
1014 size_t len, unsigned long flags)
1015{
1016 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1017 struct ioat_chan_common *chan = &ioat->base;
e0884772 1018 struct ioatdma_device *device = chan->device;
d69d235b
DW
1019 struct ioat_ring_ent *compl_desc;
1020 struct ioat_ring_ent *desc;
1021 struct ioat_ring_ent *ext;
1022 size_t total_len = len;
1023 struct ioat_pq_descriptor *pq;
1024 struct ioat_pq_ext_descriptor *pq_ex = NULL;
1025 struct ioat_dma_descriptor *hw;
1026 u32 offset = 0;
d69d235b 1027 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
074cc476 1028 int i, s, idx, with_ext, num_descs;
e0884772 1029 int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
d69d235b
DW
1030
1031 dev_dbg(to_dev(chan), "%s\n", __func__);
1032 /* the engine requires at least two sources (we provide
1033 * at least 1 implied source in the DMA_PREP_CONTINUE case)
1034 */
1035 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
1036
1037 num_descs = ioat2_xferlen_to_descs(ioat, len);
1038 /* we need 2x the number of descriptors to cover greater than 3
cd78809f
DW
1039 * sources (we need 1 extra source in the q-only continuation
1040 * case and 3 extra sources in the p+q continuation case.
d69d235b 1041 */
cd78809f
DW
1042 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
1043 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
d69d235b
DW
1044 with_ext = 1;
1045 num_descs *= 2;
1046 } else
1047 with_ext = 0;
1048
1049 /* completion writes from the raid engine may pass completion
1050 * writes from the legacy engine, so we need one extra null
1051 * (legacy) descriptor to ensure all completion writes arrive in
1052 * order.
1053 */
1054 if (likely(num_descs) &&
e0884772 1055 ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
074cc476 1056 idx = ioat->head;
d69d235b
DW
1057 else
1058 return NULL;
cdef57db
DW
1059 i = 0;
1060 do {
d69d235b
DW
1061 struct ioat_raw_descriptor *descs[2];
1062 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
1063
1064 desc = ioat2_get_ring_ent(ioat, idx + i);
1065 pq = desc->pq;
1066
1067 /* save a branch by unconditionally retrieving the
1068 * extended descriptor pq_set_src() knows to not write
1069 * to it in the single descriptor case
1070 */
1071 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
1072 pq_ex = ext->pq_ex;
1073
1074 descs[0] = (struct ioat_raw_descriptor *) pq;
1075 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
1076
1077 for (s = 0; s < src_cnt; s++)
1078 pq_set_src(descs, src[s], offset, scf[s], s);
1079
1080 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1081 if (dmaf_p_disabled_continue(flags))
1082 pq_set_src(descs, dst[1], offset, 1, s++);
1083 else if (dmaf_continue(flags)) {
1084 pq_set_src(descs, dst[0], offset, 0, s++);
1085 pq_set_src(descs, dst[1], offset, 1, s++);
1086 pq_set_src(descs, dst[1], offset, 0, s++);
1087 }
1088 pq->size = xfer_size;
1089 pq->p_addr = dst[0] + offset;
1090 pq->q_addr = dst[1] + offset;
1091 pq->ctl = 0;
1092 pq->ctl_f.op = op;
1093 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
1094 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1095 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1096
1097 len -= xfer_size;
1098 offset += xfer_size;
cdef57db 1099 } while ((i += 1 + with_ext) < num_descs);
d69d235b
DW
1100
1101 /* last pq descriptor carries the unmap parameters and fence bit */
1102 desc->txd.flags = flags;
1103 desc->len = total_len;
1104 if (result)
1105 desc->result = result;
1106 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1107 dump_pq_desc_dbg(ioat, desc, ext);
1108
e0884772
DJ
1109 if (!cb32) {
1110 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1111 pq->ctl_f.compl_write = 1;
1112 compl_desc = desc;
1113 } else {
1114 /* completion descriptor carries interrupt bit */
1115 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
1116 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
1117 hw = compl_desc->hw;
1118 hw->ctl = 0;
1119 hw->ctl_f.null = 1;
1120 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1121 hw->ctl_f.compl_write = 1;
1122 hw->size = NULL_DESC_BUFFER_SIZE;
1123 dump_desc_dbg(ioat, compl_desc);
1124 }
1125
d69d235b
DW
1126
1127 /* we leave the channel locked to ensure in order submission */
49954c15 1128 return &compl_desc->txd;
d69d235b
DW
1129}
1130
7727eaa4
DJ
1131static struct dma_async_tx_descriptor *
1132__ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
1133 const dma_addr_t *dst, const dma_addr_t *src,
1134 unsigned int src_cnt, const unsigned char *scf,
1135 size_t len, unsigned long flags)
1136{
1137 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1138 struct ioat_chan_common *chan = &ioat->base;
1139 struct ioatdma_device *device = chan->device;
1140 struct ioat_ring_ent *desc;
1141 size_t total_len = len;
1142 struct ioat_pq_descriptor *pq;
1143 u32 offset = 0;
1144 u8 op;
1145 int i, s, idx, num_descs;
1146
1147 /* this function only handles src_cnt 9 - 16 */
1148 BUG_ON(src_cnt < 9);
1149
1150 /* this function is only called with 9-16 sources */
1151 op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
1152
1153 dev_dbg(to_dev(chan), "%s\n", __func__);
1154
1155 num_descs = ioat2_xferlen_to_descs(ioat, len);
1156
1157 /*
1158 * 16 source pq is only available on cb3.3 and has no completion
1159 * write hw bug.
1160 */
1161 if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
1162 idx = ioat->head;
1163 else
1164 return NULL;
1165
1166 i = 0;
1167
1168 do {
1169 struct ioat_raw_descriptor *descs[4];
1170 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
1171
1172 desc = ioat2_get_ring_ent(ioat, idx + i);
1173 pq = desc->pq;
1174
1175 descs[0] = (struct ioat_raw_descriptor *) pq;
1176
1177 desc->sed = ioat3_alloc_sed(device,
1178 sed_get_pq16_pool_idx(src_cnt));
1179 if (!desc->sed) {
1180 dev_err(to_dev(chan),
1181 "%s: no free sed entries\n", __func__);
1182 return NULL;
1183 }
1184
1185 pq->sed_addr = desc->sed->dma;
1186 desc->sed->parent = desc;
1187
1188 descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
1189 descs[2] = (void *)descs[1] + 64;
1190
1191 for (s = 0; s < src_cnt; s++)
1192 pq16_set_src(descs, src[s], offset, scf[s], s);
1193
1194 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1195 if (dmaf_p_disabled_continue(flags))
1196 pq16_set_src(descs, dst[1], offset, 1, s++);
1197 else if (dmaf_continue(flags)) {
1198 pq16_set_src(descs, dst[0], offset, 0, s++);
1199 pq16_set_src(descs, dst[1], offset, 1, s++);
1200 pq16_set_src(descs, dst[1], offset, 0, s++);
1201 }
1202
1203 pq->size = xfer_size;
1204 pq->p_addr = dst[0] + offset;
1205 pq->q_addr = dst[1] + offset;
1206 pq->ctl = 0;
1207 pq->ctl_f.op = op;
1208 pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
1209 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1210 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1211
1212 len -= xfer_size;
1213 offset += xfer_size;
1214 } while (++i < num_descs);
1215
1216 /* last pq descriptor carries the unmap parameters and fence bit */
1217 desc->txd.flags = flags;
1218 desc->len = total_len;
1219 if (result)
1220 desc->result = result;
1221 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1222
1223 /* with cb3.3 we should be able to do completion w/o a null desc */
1224 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1225 pq->ctl_f.compl_write = 1;
1226
1227 dump_pq16_desc_dbg(ioat, desc);
1228
1229 /* we leave the channel locked to ensure in order submission */
1230 return &desc->txd;
1231}
1232
d69d235b
DW
1233static struct dma_async_tx_descriptor *
1234ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
1235 unsigned int src_cnt, const unsigned char *scf, size_t len,
1236 unsigned long flags)
1237{
7727eaa4
DJ
1238 struct dma_device *dma = chan->device;
1239
de581b65
DW
1240 /* specify valid address for disabled result */
1241 if (flags & DMA_PREP_PQ_DISABLE_P)
1242 dst[0] = dst[1];
1243 if (flags & DMA_PREP_PQ_DISABLE_Q)
1244 dst[1] = dst[0];
1245
d69d235b
DW
1246 /* handle the single source multiply case from the raid6
1247 * recovery path
1248 */
de581b65 1249 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
d69d235b
DW
1250 dma_addr_t single_source[2];
1251 unsigned char single_source_coef[2];
1252
1253 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
1254 single_source[0] = src[0];
1255 single_source[1] = src[0];
1256 single_source_coef[0] = scf[0];
1257 single_source_coef[1] = 0;
1258
7727eaa4
DJ
1259 return (src_cnt > 8) && (dma->max_pq > 8) ?
1260 __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
1261 2, single_source_coef, len,
1262 flags) :
1263 __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
1264 single_source_coef, len, flags);
1265
1266 } else {
1267 return (src_cnt > 8) && (dma->max_pq > 8) ?
1268 __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
1269 scf, len, flags) :
1270 __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
1271 scf, len, flags);
1272 }
d69d235b
DW
1273}
1274
1275struct dma_async_tx_descriptor *
1276ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
1277 unsigned int src_cnt, const unsigned char *scf, size_t len,
1278 enum sum_check_flags *pqres, unsigned long flags)
1279{
7727eaa4
DJ
1280 struct dma_device *dma = chan->device;
1281
de581b65
DW
1282 /* specify valid address for disabled result */
1283 if (flags & DMA_PREP_PQ_DISABLE_P)
1284 pq[0] = pq[1];
1285 if (flags & DMA_PREP_PQ_DISABLE_Q)
1286 pq[1] = pq[0];
1287
d69d235b
DW
1288 /* the cleanup routine only sets bits on validate failure, it
1289 * does not clear bits on validate success... so clear it here
1290 */
1291 *pqres = 0;
1292
7727eaa4
DJ
1293 return (src_cnt > 8) && (dma->max_pq > 8) ?
1294 __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
1295 flags) :
1296 __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
1297 flags);
d69d235b
DW
1298}
1299
ae786624
DW
1300static struct dma_async_tx_descriptor *
1301ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
1302 unsigned int src_cnt, size_t len, unsigned long flags)
1303{
7727eaa4 1304 struct dma_device *dma = chan->device;
ae786624
DW
1305 unsigned char scf[src_cnt];
1306 dma_addr_t pq[2];
1307
1308 memset(scf, 0, src_cnt);
ae786624 1309 pq[0] = dst;
de581b65
DW
1310 flags |= DMA_PREP_PQ_DISABLE_Q;
1311 pq[1] = dst; /* specify valid address for disabled result */
ae786624 1312
7727eaa4
DJ
1313 return (src_cnt > 8) && (dma->max_pq > 8) ?
1314 __ioat3_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
1315 flags) :
1316 __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
1317 flags);
ae786624
DW
1318}
1319
1320struct dma_async_tx_descriptor *
1321ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
1322 unsigned int src_cnt, size_t len,
1323 enum sum_check_flags *result, unsigned long flags)
1324{
7727eaa4 1325 struct dma_device *dma = chan->device;
ae786624
DW
1326 unsigned char scf[src_cnt];
1327 dma_addr_t pq[2];
1328
1329 /* the cleanup routine only sets bits on validate failure, it
1330 * does not clear bits on validate success... so clear it here
1331 */
1332 *result = 0;
1333
1334 memset(scf, 0, src_cnt);
ae786624 1335 pq[0] = src[0];
de581b65
DW
1336 flags |= DMA_PREP_PQ_DISABLE_Q;
1337 pq[1] = pq[0]; /* specify valid address for disabled result */
ae786624 1338
7727eaa4
DJ
1339
1340 return (src_cnt > 8) && (dma->max_pq > 8) ?
1341 __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
1342 scf, len, flags) :
1343 __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
1344 scf, len, flags);
ae786624
DW
1345}
1346
58c8649e
DW
1347static struct dma_async_tx_descriptor *
1348ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
1349{
1350 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1351 struct ioat_ring_ent *desc;
1352 struct ioat_dma_descriptor *hw;
58c8649e 1353
074cc476
DW
1354 if (ioat2_check_space_lock(ioat, 1) == 0)
1355 desc = ioat2_get_ring_ent(ioat, ioat->head);
58c8649e
DW
1356 else
1357 return NULL;
1358
1359 hw = desc->hw;
1360 hw->ctl = 0;
1361 hw->ctl_f.null = 1;
1362 hw->ctl_f.int_en = 1;
1363 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1364 hw->ctl_f.compl_write = 1;
1365 hw->size = NULL_DESC_BUFFER_SIZE;
1366 hw->src_addr = 0;
1367 hw->dst_addr = 0;
1368
1369 desc->txd.flags = flags;
1370 desc->len = 1;
1371
1372 dump_desc_dbg(ioat, desc);
1373
1374 /* we leave the channel locked to ensure in order submission */
1375 return &desc->txd;
1376}
1377
4bf27b8b 1378static void ioat3_dma_test_callback(void *dma_async_param)
9de6fc71
DW
1379{
1380 struct completion *cmp = dma_async_param;
1381
1382 complete(cmp);
1383}
1384
1385#define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
4bf27b8b 1386static int ioat_xor_val_self_test(struct ioatdma_device *device)
9de6fc71
DW
1387{
1388 int i, src_idx;
1389 struct page *dest;
1390 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
1391 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
1392 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
1393 dma_addr_t dma_addr, dest_dma;
1394 struct dma_async_tx_descriptor *tx;
1395 struct dma_chan *dma_chan;
1396 dma_cookie_t cookie;
1397 u8 cmp_byte = 0;
1398 u32 cmp_word;
1399 u32 xor_val_result;
1400 int err = 0;
1401 struct completion cmp;
1402 unsigned long tmo;
1403 struct device *dev = &device->pdev->dev;
1404 struct dma_device *dma = &device->common;
7369f56e 1405 u8 op = 0;
9de6fc71
DW
1406
1407 dev_dbg(dev, "%s\n", __func__);
1408
1409 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
1410 return 0;
1411
1412 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1413 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
1414 if (!xor_srcs[src_idx]) {
1415 while (src_idx--)
1416 __free_page(xor_srcs[src_idx]);
1417 return -ENOMEM;
1418 }
1419 }
1420
1421 dest = alloc_page(GFP_KERNEL);
1422 if (!dest) {
1423 while (src_idx--)
1424 __free_page(xor_srcs[src_idx]);
1425 return -ENOMEM;
1426 }
1427
1428 /* Fill in src buffers */
1429 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1430 u8 *ptr = page_address(xor_srcs[src_idx]);
1431 for (i = 0; i < PAGE_SIZE; i++)
1432 ptr[i] = (1 << src_idx);
1433 }
1434
1435 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
1436 cmp_byte ^= (u8) (1 << src_idx);
1437
1438 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1439 (cmp_byte << 8) | cmp_byte;
1440
1441 memset(page_address(dest), 0, PAGE_SIZE);
1442
1443 dma_chan = container_of(dma->channels.next, struct dma_chan,
1444 device_node);
1445 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1446 err = -ENODEV;
1447 goto out;
1448 }
1449
1450 /* test xor */
7369f56e
BZ
1451 op = IOAT_OP_XOR;
1452
9de6fc71
DW
1453 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1454 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1455 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
1456 DMA_TO_DEVICE);
1457 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
1458 IOAT_NUM_SRC_TEST, PAGE_SIZE,
7369f56e
BZ
1459 DMA_PREP_INTERRUPT |
1460 DMA_COMPL_SKIP_SRC_UNMAP |
1461 DMA_COMPL_SKIP_DEST_UNMAP);
9de6fc71
DW
1462
1463 if (!tx) {
1464 dev_err(dev, "Self-test xor prep failed\n");
1465 err = -ENODEV;
7369f56e 1466 goto dma_unmap;
9de6fc71
DW
1467 }
1468
1469 async_tx_ack(tx);
1470 init_completion(&cmp);
1471 tx->callback = ioat3_dma_test_callback;
1472 tx->callback_param = &cmp;
1473 cookie = tx->tx_submit(tx);
1474 if (cookie < 0) {
1475 dev_err(dev, "Self-test xor setup failed\n");
1476 err = -ENODEV;
7369f56e 1477 goto dma_unmap;
9de6fc71
DW
1478 }
1479 dma->device_issue_pending(dma_chan);
1480
1481 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1482
07934481 1483 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
9de6fc71
DW
1484 dev_err(dev, "Self-test xor timed out\n");
1485 err = -ENODEV;
7369f56e 1486 goto dma_unmap;
9de6fc71
DW
1487 }
1488
7369f56e
BZ
1489 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1490 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1491 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1492
9de6fc71
DW
1493 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1494 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1495 u32 *ptr = page_address(dest);
1496 if (ptr[i] != cmp_word) {
1497 dev_err(dev, "Self-test xor failed compare\n");
1498 err = -ENODEV;
1499 goto free_resources;
1500 }
1501 }
ac498987 1502 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
9de6fc71
DW
1503
1504 /* skip validate if the capability is not present */
1505 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
1506 goto free_resources;
1507
7369f56e
BZ
1508 op = IOAT_OP_XOR_VAL;
1509
9de6fc71
DW
1510 /* validate the sources with the destintation page */
1511 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1512 xor_val_srcs[i] = xor_srcs[i];
1513 xor_val_srcs[i] = dest;
1514
1515 xor_val_result = 1;
1516
1517 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1518 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1519 DMA_TO_DEVICE);
1520 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1521 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
7369f56e
BZ
1522 &xor_val_result, DMA_PREP_INTERRUPT |
1523 DMA_COMPL_SKIP_SRC_UNMAP |
1524 DMA_COMPL_SKIP_DEST_UNMAP);
9de6fc71
DW
1525 if (!tx) {
1526 dev_err(dev, "Self-test zero prep failed\n");
1527 err = -ENODEV;
7369f56e 1528 goto dma_unmap;
9de6fc71
DW
1529 }
1530
1531 async_tx_ack(tx);
1532 init_completion(&cmp);
1533 tx->callback = ioat3_dma_test_callback;
1534 tx->callback_param = &cmp;
1535 cookie = tx->tx_submit(tx);
1536 if (cookie < 0) {
1537 dev_err(dev, "Self-test zero setup failed\n");
1538 err = -ENODEV;
7369f56e 1539 goto dma_unmap;
9de6fc71
DW
1540 }
1541 dma->device_issue_pending(dma_chan);
1542
1543 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1544
07934481 1545 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
9de6fc71
DW
1546 dev_err(dev, "Self-test validate timed out\n");
1547 err = -ENODEV;
7369f56e 1548 goto dma_unmap;
9de6fc71
DW
1549 }
1550
7369f56e
BZ
1551 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1552 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1553
9de6fc71
DW
1554 if (xor_val_result != 0) {
1555 dev_err(dev, "Self-test validate failed compare\n");
1556 err = -ENODEV;
1557 goto free_resources;
1558 }
1559
1560 /* skip memset if the capability is not present */
1561 if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
1562 goto free_resources;
1563
1564 /* test memset */
7369f56e
BZ
1565 op = IOAT_OP_FILL;
1566
9de6fc71
DW
1567 dma_addr = dma_map_page(dev, dest, 0,
1568 PAGE_SIZE, DMA_FROM_DEVICE);
1569 tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
7369f56e
BZ
1570 DMA_PREP_INTERRUPT |
1571 DMA_COMPL_SKIP_SRC_UNMAP |
1572 DMA_COMPL_SKIP_DEST_UNMAP);
9de6fc71
DW
1573 if (!tx) {
1574 dev_err(dev, "Self-test memset prep failed\n");
1575 err = -ENODEV;
7369f56e 1576 goto dma_unmap;
9de6fc71
DW
1577 }
1578
1579 async_tx_ack(tx);
1580 init_completion(&cmp);
1581 tx->callback = ioat3_dma_test_callback;
1582 tx->callback_param = &cmp;
1583 cookie = tx->tx_submit(tx);
1584 if (cookie < 0) {
1585 dev_err(dev, "Self-test memset setup failed\n");
1586 err = -ENODEV;
7369f56e 1587 goto dma_unmap;
9de6fc71
DW
1588 }
1589 dma->device_issue_pending(dma_chan);
1590
1591 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1592
07934481 1593 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
9de6fc71
DW
1594 dev_err(dev, "Self-test memset timed out\n");
1595 err = -ENODEV;
7369f56e 1596 goto dma_unmap;
9de6fc71
DW
1597 }
1598
7369f56e
BZ
1599 dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
1600
9de6fc71
DW
1601 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1602 u32 *ptr = page_address(dest);
1603 if (ptr[i]) {
1604 dev_err(dev, "Self-test memset failed compare\n");
1605 err = -ENODEV;
1606 goto free_resources;
1607 }
1608 }
1609
1610 /* test for non-zero parity sum */
7369f56e
BZ
1611 op = IOAT_OP_XOR_VAL;
1612
9de6fc71
DW
1613 xor_val_result = 0;
1614 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1615 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1616 DMA_TO_DEVICE);
1617 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1618 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
7369f56e
BZ
1619 &xor_val_result, DMA_PREP_INTERRUPT |
1620 DMA_COMPL_SKIP_SRC_UNMAP |
1621 DMA_COMPL_SKIP_DEST_UNMAP);
9de6fc71
DW
1622 if (!tx) {
1623 dev_err(dev, "Self-test 2nd zero prep failed\n");
1624 err = -ENODEV;
7369f56e 1625 goto dma_unmap;
9de6fc71
DW
1626 }
1627
1628 async_tx_ack(tx);
1629 init_completion(&cmp);
1630 tx->callback = ioat3_dma_test_callback;
1631 tx->callback_param = &cmp;
1632 cookie = tx->tx_submit(tx);
1633 if (cookie < 0) {
1634 dev_err(dev, "Self-test 2nd zero setup failed\n");
1635 err = -ENODEV;
7369f56e 1636 goto dma_unmap;
9de6fc71
DW
1637 }
1638 dma->device_issue_pending(dma_chan);
1639
1640 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1641
07934481 1642 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
9de6fc71
DW
1643 dev_err(dev, "Self-test 2nd validate timed out\n");
1644 err = -ENODEV;
7369f56e 1645 goto dma_unmap;
9de6fc71
DW
1646 }
1647
1648 if (xor_val_result != SUM_CHECK_P_RESULT) {
1649 dev_err(dev, "Self-test validate failed compare\n");
1650 err = -ENODEV;
7369f56e 1651 goto dma_unmap;
9de6fc71
DW
1652 }
1653
7369f56e
BZ
1654 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1655 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1656
1657 goto free_resources;
1658dma_unmap:
1659 if (op == IOAT_OP_XOR) {
1660 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1661 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1662 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1663 DMA_TO_DEVICE);
1664 } else if (op == IOAT_OP_XOR_VAL) {
1665 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1666 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1667 DMA_TO_DEVICE);
1668 } else if (op == IOAT_OP_FILL)
1669 dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
9de6fc71
DW
1670free_resources:
1671 dma->device_free_chan_resources(dma_chan);
1672out:
1673 src_idx = IOAT_NUM_SRC_TEST;
1674 while (src_idx--)
1675 __free_page(xor_srcs[src_idx]);
1676 __free_page(dest);
1677 return err;
1678}
1679
4bf27b8b 1680static int ioat3_dma_self_test(struct ioatdma_device *device)
9de6fc71
DW
1681{
1682 int rc = ioat_dma_self_test(device);
1683
1684 if (rc)
1685 return rc;
1686
1687 rc = ioat_xor_val_self_test(device);
1688 if (rc)
1689 return rc;
1690
1691 return 0;
1692}
1693
8a52b9ff
DJ
1694static int ioat3_irq_reinit(struct ioatdma_device *device)
1695{
1696 int msixcnt = device->common.chancnt;
1697 struct pci_dev *pdev = device->pdev;
1698 int i;
1699 struct msix_entry *msix;
1700 struct ioat_chan_common *chan;
1701 int err = 0;
1702
1703 switch (device->irq_mode) {
1704 case IOAT_MSIX:
1705
1706 for (i = 0; i < msixcnt; i++) {
1707 msix = &device->msix_entries[i];
1708 chan = ioat_chan_by_index(device, i);
1709 devm_free_irq(&pdev->dev, msix->vector, chan);
1710 }
1711
1712 pci_disable_msix(pdev);
1713 break;
1714
1715 case IOAT_MSIX_SINGLE:
1716 msix = &device->msix_entries[0];
1717 chan = ioat_chan_by_index(device, 0);
1718 devm_free_irq(&pdev->dev, msix->vector, chan);
1719 pci_disable_msix(pdev);
1720 break;
1721
1722 case IOAT_MSI:
1723 chan = ioat_chan_by_index(device, 0);
1724 devm_free_irq(&pdev->dev, pdev->irq, chan);
1725 pci_disable_msi(pdev);
1726 break;
1727
1728 case IOAT_INTX:
1729 chan = ioat_chan_by_index(device, 0);
1730 devm_free_irq(&pdev->dev, pdev->irq, chan);
1731 break;
1732
1733 default:
1734 return 0;
1735 }
1736
1737 device->irq_mode = IOAT_NOIRQ;
1738
1739 err = ioat_dma_setup_interrupts(device);
1740
1741 return err;
1742}
1743
a6d52d70
DW
1744static int ioat3_reset_hw(struct ioat_chan_common *chan)
1745{
1746 /* throw away whatever the channel was doing and get it
1747 * initialized, with ioat3 specific workarounds
1748 */
1749 struct ioatdma_device *device = chan->device;
1750 struct pci_dev *pdev = device->pdev;
1751 u32 chanerr;
1752 u16 dev_id;
1753 int err;
1754
1755 ioat2_quiesce(chan, msecs_to_jiffies(100));
1756
1757 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1758 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1759
6ead7e48
DJ
1760 if (device->version < IOAT_VER_3_3) {
1761 /* clear any pending errors */
1762 err = pci_read_config_dword(pdev,
1763 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1764 if (err) {
1765 dev_err(&pdev->dev,
1766 "channel error register unreachable\n");
1767 return err;
1768 }
1769 pci_write_config_dword(pdev,
1770 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
a6d52d70 1771
6ead7e48
DJ
1772 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1773 * (workaround for spurious config parity error after restart)
1774 */
1775 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1776 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
1777 pci_write_config_dword(pdev,
1778 IOAT_PCI_DMAUNCERRSTS_OFFSET,
1779 0x10);
1780 }
1781 }
a6d52d70 1782
8a52b9ff
DJ
1783 err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
1784 if (err) {
1785 dev_err(&pdev->dev, "Failed to reset!\n");
1786 return err;
570727b5
DJ
1787 }
1788
8a52b9ff
DJ
1789 if (device->irq_mode != IOAT_NOIRQ && is_bwd_ioat(pdev))
1790 err = ioat3_irq_reinit(device);
570727b5 1791
8a52b9ff 1792 return err;
570727b5
DJ
1793}
1794
4bf27b8b 1795int ioat3_dma_probe(struct ioatdma_device *device, int dca)
bf40a686
DW
1796{
1797 struct pci_dev *pdev = device->pdev;
228c4f5c 1798 int dca_en = system_has_dca_enabled(pdev);
bf40a686
DW
1799 struct dma_device *dma;
1800 struct dma_chan *c;
1801 struct ioat_chan_common *chan;
e3232714 1802 bool is_raid_device = false;
bf40a686 1803 int err;
bf40a686
DW
1804 u32 cap;
1805
1806 device->enumerate_channels = ioat2_enumerate_channels;
a6d52d70 1807 device->reset_hw = ioat3_reset_hw;
9de6fc71 1808 device->self_test = ioat3_dma_self_test;
bf40a686
DW
1809 dma = &device->common;
1810 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1811 dma->device_issue_pending = ioat2_issue_pending;
1812 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1813 dma->device_free_chan_resources = ioat2_free_chan_resources;
58c8649e 1814
570727b5 1815 if (is_xeon_cb32(pdev))
f26df1a1
DJ
1816 dma->copy_align = 6;
1817
58c8649e
DW
1818 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1819 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1820
bf40a686 1821 cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
228c4f5c 1822
d302398d
DJ
1823 if (is_bwd_noraid(pdev))
1824 cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
1825
228c4f5c
DW
1826 /* dca is incompatible with raid operations */
1827 if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1828 cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1829
b094ad3b 1830 if (cap & IOAT_CAP_XOR) {
e3232714 1831 is_raid_device = true;
b094ad3b 1832 dma->max_xor = 8;
2adfc550 1833 dma->xor_align = 6;
b094ad3b
DW
1834
1835 dma_cap_set(DMA_XOR, dma->cap_mask);
1836 dma->device_prep_dma_xor = ioat3_prep_xor;
1837
1838 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1839 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1840 }
eceec44e 1841
d69d235b 1842 if (cap & IOAT_CAP_PQ) {
e3232714 1843 is_raid_device = true;
7727eaa4
DJ
1844
1845 if (cap & IOAT_CAP_RAID16SS) {
1846 dma_set_maxpq(dma, 16, 0);
eceec44e 1847 dma->pq_align = 0;
7727eaa4
DJ
1848 } else {
1849 dma_set_maxpq(dma, 8, 0);
1850 if (is_xeon_cb32(pdev))
1851 dma->pq_align = 6;
1852 else
1853 dma->pq_align = 0;
1854 }
d69d235b
DW
1855
1856 dma_cap_set(DMA_PQ, dma->cap_mask);
1857 dma->device_prep_dma_pq = ioat3_prep_pq;
1858
1859 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1860 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
ae786624
DW
1861
1862 if (!(cap & IOAT_CAP_XOR)) {
7727eaa4
DJ
1863 if (cap & IOAT_CAP_RAID16SS) {
1864 dma->max_xor = 16;
eceec44e 1865 dma->xor_align = 0;
7727eaa4
DJ
1866 } else {
1867 dma->max_xor = 8;
1868 if (is_xeon_cb32(pdev))
1869 dma->xor_align = 6;
1870 else
1871 dma->xor_align = 0;
1872 }
ae786624
DW
1873
1874 dma_cap_set(DMA_XOR, dma->cap_mask);
1875 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1876
1877 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1878 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1879 }
d69d235b 1880 }
eceec44e 1881
e3232714
DW
1882 if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
1883 dma_cap_set(DMA_MEMSET, dma->cap_mask);
1884 dma->device_prep_dma_memset = ioat3_prep_memset_lock;
1885 }
1886
1887
9a37f644
DJ
1888 dma->device_tx_status = ioat3_tx_status;
1889 device->cleanup_fn = ioat3_cleanup_event;
1890 device->timer_fn = ioat3_timer_event;
bf40a686 1891
3f09ede4
DJ
1892 if (is_xeon_cb32(pdev)) {
1893 dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
1894 dma->device_prep_dma_xor_val = NULL;
7b3cc2b1 1895
3f09ede4
DJ
1896 dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
1897 dma->device_prep_dma_pq_val = NULL;
1898 }
7b3cc2b1 1899
7727eaa4
DJ
1900 /* starting with CB3.3 super extended descriptors are supported */
1901 if (cap & IOAT_CAP_RAID16SS) {
1902 char pool_name[14];
1903 int i;
1904
1905 /* allocate sw descriptor pool for SED */
1906 device->sed_pool = kmem_cache_create("ioat_sed",
1907 sizeof(struct ioat_sed_ent), 0, 0, NULL);
1908 if (!device->sed_pool)
1909 return -ENOMEM;
1910
1911 for (i = 0; i < MAX_SED_POOLS; i++) {
1912 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1913
1914 /* allocate SED DMA pool */
1915 device->sed_hw_pool[i] = dma_pool_create(pool_name,
1916 &pdev->dev,
1917 SED_SIZE * (i + 1), 64, 0);
1918 if (!device->sed_hw_pool[i])
1919 goto sed_pool_cleanup;
1920
1921 }
1922 }
1923
bf40a686
DW
1924 err = ioat_probe(device);
1925 if (err)
1926 return err;
1927 ioat_set_tcp_copy_break(262144);
1928
1929 list_for_each_entry(c, &dma->channels, device_node) {
1930 chan = to_chan_common(c);
1931 writel(IOAT_DMA_DCA_ANY_CPU,
1932 chan->reg_base + IOAT_DCACTRL_OFFSET);
1933 }
1934
1935 err = ioat_register(device);
1936 if (err)
1937 return err;
5669e31c
DW
1938
1939 ioat_kobject_add(device, &ioat2_ktype);
1940
bf40a686
DW
1941 if (dca)
1942 device->dca = ioat3_dca_init(pdev, device->reg_base);
1943
1944 return 0;
7727eaa4
DJ
1945
1946sed_pool_cleanup:
1947 if (device->sed_pool) {
1948 int i;
1949 kmem_cache_destroy(device->sed_pool);
1950
1951 for (i = 0; i < MAX_SED_POOLS; i++)
1952 if (device->sed_hw_pool[i])
1953 dma_pool_destroy(device->sed_hw_pool[i]);
1954 }
1955
1956 return -ENOMEM;
1957}
1958
1959void ioat3_dma_remove(struct ioatdma_device *device)
1960{
1961 if (device->sed_pool) {
1962 int i;
1963 kmem_cache_destroy(device->sed_pool);
1964
1965 for (i = 0; i < MAX_SED_POOLS; i++)
1966 if (device->sed_hw_pool[i])
1967 dma_pool_destroy(device->sed_hw_pool[i]);
1968 }
bf40a686 1969}
This page took 0.300521 seconds and 5 git commands to generate.