blob: 8b573fac2a2567e68e9f3f591faa557479b78d24 [file] [log] [blame]
Dan Williamsbf40a682009-09-08 17:42:55 -07001/*
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 */
58
59#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/gfp.h>
Dan Williamsbf40a682009-09-08 17:42:55 -070061#include <linux/dmaengine.h>
62#include <linux/dma-mapping.h>
63#include "registers.h"
64#include "hw.h"
65#include "dma.h"
66#include "dma_v2.h"
67
Dan Williamsb094ad32009-09-08 17:42:57 -070068/* ioat hardware assumes at least two sources for raid operations */
69#define src_cnt_to_sw(x) ((x) + 2)
70#define src_cnt_to_hw(x) ((x) - 2)
71
72/* provide a lookup table for setting the source address in the base or
Dan Williamsd69d235b2009-09-08 17:42:59 -070073 * extended descriptor of an xor or pq descriptor
Dan Williamsb094ad32009-09-08 17:42:57 -070074 */
75static const u8 xor_idx_to_desc __read_mostly = 0xd0;
76static const u8 xor_idx_to_field[] __read_mostly = { 1, 4, 5, 6, 7, 0, 1, 2 };
Dan Williamsd69d235b2009-09-08 17:42:59 -070077static const u8 pq_idx_to_desc __read_mostly = 0xf8;
78static const u8 pq_idx_to_field[] __read_mostly = { 1, 4, 5, 0, 1, 2, 4, 5 };
Dan Williamsb094ad32009-09-08 17:42:57 -070079
80static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
81{
82 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
83
84 return raw->field[xor_idx_to_field[idx]];
85}
86
87static void xor_set_src(struct ioat_raw_descriptor *descs[2],
88 dma_addr_t addr, u32 offset, int idx)
89{
90 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
91
92 raw->field[xor_idx_to_field[idx]] = addr + offset;
93}
94
Dan Williamsd69d235b2009-09-08 17:42:59 -070095static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
96{
97 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
98
99 return raw->field[pq_idx_to_field[idx]];
100}
101
102static void pq_set_src(struct ioat_raw_descriptor *descs[2],
103 dma_addr_t addr, u32 offset, u8 coef, int idx)
104{
105 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
106 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
107
108 raw->field[pq_idx_to_field[idx]] = addr + offset;
109 pq->coef[idx] = coef;
110}
111
Dan Williamsbf40a682009-09-08 17:42:55 -0700112static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
Dan Williamsb094ad32009-09-08 17:42:57 -0700113 struct ioat_ring_ent *desc, int idx)
Dan Williamsbf40a682009-09-08 17:42:55 -0700114{
115 struct ioat_chan_common *chan = &ioat->base;
116 struct pci_dev *pdev = chan->device->pdev;
117 size_t len = desc->len;
118 size_t offset = len - desc->hw->size;
119 struct dma_async_tx_descriptor *tx = &desc->txd;
120 enum dma_ctrl_flags flags = tx->flags;
121
122 switch (desc->hw->ctl_f.op) {
123 case IOAT_OP_COPY:
Dan Williams58c86492009-09-08 17:43:00 -0700124 if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
125 ioat_dma_unmap(chan, flags, len, desc->hw);
Dan Williamsbf40a682009-09-08 17:42:55 -0700126 break;
127 case IOAT_OP_FILL: {
128 struct ioat_fill_descriptor *hw = desc->fill;
129
130 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
131 ioat_unmap(pdev, hw->dst_addr - offset, len,
132 PCI_DMA_FROMDEVICE, flags, 1);
133 break;
134 }
Dan Williamsb094ad32009-09-08 17:42:57 -0700135 case IOAT_OP_XOR_VAL:
136 case IOAT_OP_XOR: {
137 struct ioat_xor_descriptor *xor = desc->xor;
138 struct ioat_ring_ent *ext;
139 struct ioat_xor_ext_descriptor *xor_ex = NULL;
140 int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
141 struct ioat_raw_descriptor *descs[2];
142 int i;
143
144 if (src_cnt > 5) {
145 ext = ioat2_get_ring_ent(ioat, idx + 1);
146 xor_ex = ext->xor_ex;
147 }
148
149 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
150 descs[0] = (struct ioat_raw_descriptor *) xor;
151 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
152 for (i = 0; i < src_cnt; i++) {
153 dma_addr_t src = xor_get_src(descs, i);
154
155 ioat_unmap(pdev, src - offset, len,
156 PCI_DMA_TODEVICE, flags, 0);
157 }
158
159 /* dest is a source in xor validate operations */
160 if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
161 ioat_unmap(pdev, xor->dst_addr - offset, len,
162 PCI_DMA_TODEVICE, flags, 1);
163 break;
164 }
165 }
166
167 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
168 ioat_unmap(pdev, xor->dst_addr - offset, len,
169 PCI_DMA_FROMDEVICE, flags, 1);
170 break;
171 }
Dan Williamsd69d235b2009-09-08 17:42:59 -0700172 case IOAT_OP_PQ_VAL:
173 case IOAT_OP_PQ: {
174 struct ioat_pq_descriptor *pq = desc->pq;
175 struct ioat_ring_ent *ext;
176 struct ioat_pq_ext_descriptor *pq_ex = NULL;
177 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
178 struct ioat_raw_descriptor *descs[2];
179 int i;
180
181 if (src_cnt > 3) {
182 ext = ioat2_get_ring_ent(ioat, idx + 1);
183 pq_ex = ext->pq_ex;
184 }
185
186 /* in the 'continue' case don't unmap the dests as sources */
187 if (dmaf_p_disabled_continue(flags))
188 src_cnt--;
189 else if (dmaf_continue(flags))
190 src_cnt -= 3;
191
192 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
193 descs[0] = (struct ioat_raw_descriptor *) pq;
194 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
195 for (i = 0; i < src_cnt; i++) {
196 dma_addr_t src = pq_get_src(descs, i);
197
198 ioat_unmap(pdev, src - offset, len,
199 PCI_DMA_TODEVICE, flags, 0);
200 }
201
202 /* the dests are sources in pq validate operations */
203 if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
204 if (!(flags & DMA_PREP_PQ_DISABLE_P))
205 ioat_unmap(pdev, pq->p_addr - offset,
206 len, PCI_DMA_TODEVICE, flags, 0);
207 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
208 ioat_unmap(pdev, pq->q_addr - offset,
209 len, PCI_DMA_TODEVICE, flags, 0);
210 break;
211 }
212 }
213
214 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
215 if (!(flags & DMA_PREP_PQ_DISABLE_P))
216 ioat_unmap(pdev, pq->p_addr - offset, len,
217 PCI_DMA_BIDIRECTIONAL, flags, 1);
218 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
219 ioat_unmap(pdev, pq->q_addr - offset, len,
220 PCI_DMA_BIDIRECTIONAL, flags, 1);
221 }
222 break;
223 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700224 default:
225 dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
226 __func__, desc->hw->ctl_f.op);
227 }
228}
229
Dan Williamsb094ad32009-09-08 17:42:57 -0700230static bool desc_has_ext(struct ioat_ring_ent *desc)
231{
232 struct ioat_dma_descriptor *hw = desc->hw;
Dan Williamsbf40a682009-09-08 17:42:55 -0700233
Dan Williamsb094ad32009-09-08 17:42:57 -0700234 if (hw->ctl_f.op == IOAT_OP_XOR ||
235 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
236 struct ioat_xor_descriptor *xor = desc->xor;
237
238 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
239 return true;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700240 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
241 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
242 struct ioat_pq_descriptor *pq = desc->pq;
243
244 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
245 return true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700246 }
247
248 return false;
249}
250
251/**
252 * __cleanup - reclaim used descriptors
253 * @ioat: channel (ring) to clean
254 *
255 * The difference from the dma_v2.c __cleanup() is that this routine
256 * handles extended descriptors and dma-unmapping raid operations.
257 */
Dan Williamsbf40a682009-09-08 17:42:55 -0700258static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
259{
260 struct ioat_chan_common *chan = &ioat->base;
261 struct ioat_ring_ent *desc;
262 bool seen_current = false;
Dan Williams074cc472010-05-01 15:22:55 -0700263 int idx = ioat->tail, i;
Dan Williamsbf40a682009-09-08 17:42:55 -0700264 u16 active;
Dan Williamsbf40a682009-09-08 17:42:55 -0700265
266 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
267 __func__, ioat->head, ioat->tail, ioat->issued);
268
269 active = ioat2_ring_active(ioat);
270 for (i = 0; i < active && !seen_current; i++) {
271 struct dma_async_tx_descriptor *tx;
272
Dan Williams074cc472010-05-01 15:22:55 -0700273 smp_read_barrier_depends();
274 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
275 desc = ioat2_get_ring_ent(ioat, idx + i);
Dan Williamsbf40a682009-09-08 17:42:55 -0700276 dump_desc_dbg(ioat, desc);
277 tx = &desc->txd;
278 if (tx->cookie) {
279 chan->completed_cookie = tx->cookie;
Dan Williams074cc472010-05-01 15:22:55 -0700280 ioat3_dma_unmap(ioat, desc, idx + i);
Dan Williamsbf40a682009-09-08 17:42:55 -0700281 tx->cookie = 0;
282 if (tx->callback) {
283 tx->callback(tx->callback_param);
284 tx->callback = NULL;
285 }
286 }
287
288 if (tx->phys == phys_complete)
289 seen_current = true;
Dan Williamsb094ad32009-09-08 17:42:57 -0700290
291 /* skip extended descriptors */
292 if (desc_has_ext(desc)) {
293 BUG_ON(i + 1 >= active);
294 i++;
295 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700296 }
Dan Williams074cc472010-05-01 15:22:55 -0700297 smp_mb(); /* finish all descriptor reads before incrementing tail */
298 ioat->tail = idx + i;
Dan Williamsaa75db02010-03-03 21:21:10 -0700299 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
Dan Williamsbf40a682009-09-08 17:42:55 -0700300 chan->last_completion = phys_complete;
Dan Williamsb9cc9862010-03-03 21:21:13 -0700301
Dan Williams074cc472010-05-01 15:22:55 -0700302 if (active - i == 0) {
Dan Williamsbf40a682009-09-08 17:42:55 -0700303 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
304 __func__);
305 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
306 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
307 }
Dan Williamsb9cc9862010-03-03 21:21:13 -0700308 /* 5 microsecond delay per pending descriptor */
Dan Williams074cc472010-05-01 15:22:55 -0700309 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
Dan Williamsb9cc9862010-03-03 21:21:13 -0700310 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700311}
312
Dan Williams074cc472010-05-01 15:22:55 -0700313static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
Dan Williamsbf40a682009-09-08 17:42:55 -0700314{
315 struct ioat_chan_common *chan = &ioat->base;
316 unsigned long phys_complete;
317
Dan Williamsb9cc9862010-03-03 21:21:13 -0700318 spin_lock_bh(&chan->cleanup_lock);
Dan Williams074cc472010-05-01 15:22:55 -0700319 if (ioat_cleanup_preamble(chan, &phys_complete))
320 __cleanup(ioat, phys_complete);
Dan Williamsb9cc9862010-03-03 21:21:13 -0700321 spin_unlock_bh(&chan->cleanup_lock);
322}
323
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700324static void ioat3_cleanup_event(unsigned long data)
Dan Williamsbf40a682009-09-08 17:42:55 -0700325{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700326 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williamsbf40a682009-09-08 17:42:55 -0700327
Dan Williams074cc472010-05-01 15:22:55 -0700328 ioat3_cleanup(ioat);
Dan Williams773d9e22010-03-03 11:47:42 -0700329 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
Dan Williamsbf40a682009-09-08 17:42:55 -0700330}
331
332static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
333{
334 struct ioat_chan_common *chan = &ioat->base;
335 unsigned long phys_complete;
Dan Williamsbf40a682009-09-08 17:42:55 -0700336
Dan Williamsb372ec22010-03-03 11:47:42 -0700337 ioat2_quiesce(chan, 0);
Dan Williamsbf40a682009-09-08 17:42:55 -0700338 if (ioat_cleanup_preamble(chan, &phys_complete))
339 __cleanup(ioat, phys_complete);
340
341 __ioat2_restart_chan(ioat);
342}
343
344static void ioat3_timer_event(unsigned long data)
345{
Dan Williamsaa4d72a2010-03-03 21:21:13 -0700346 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
Dan Williamsbf40a682009-09-08 17:42:55 -0700347 struct ioat_chan_common *chan = &ioat->base;
348
Dan Williamsbf40a682009-09-08 17:42:55 -0700349 if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
350 unsigned long phys_complete;
351 u64 status;
352
Dan Williamsbf40a682009-09-08 17:42:55 -0700353 status = ioat_chansts(chan);
354
355 /* when halted due to errors check for channel
356 * programming errors before advancing the completion state
357 */
358 if (is_ioat_halted(status)) {
359 u32 chanerr;
360
361 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Dan Williamsb57014d2009-11-19 17:10:07 -0700362 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
363 __func__, chanerr);
Dan Williamsbf40a682009-09-08 17:42:55 -0700364 BUG_ON(is_ioat_bug(chanerr));
365 }
366
367 /* if we haven't made progress and we have already
368 * acknowledged a pending completion once, then be more
369 * forceful with a restart
370 */
Dan Williams074cc472010-05-01 15:22:55 -0700371 spin_lock_bh(&chan->cleanup_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700372 if (ioat_cleanup_preamble(chan, &phys_complete))
373 __cleanup(ioat, phys_complete);
Dan Williams074cc472010-05-01 15:22:55 -0700374 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
375 spin_lock_bh(&ioat->prep_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700376 ioat3_restart_channel(ioat);
Dan Williams074cc472010-05-01 15:22:55 -0700377 spin_unlock_bh(&ioat->prep_lock);
378 } else {
Dan Williamsbf40a682009-09-08 17:42:55 -0700379 set_bit(IOAT_COMPLETION_ACK, &chan->state);
380 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
381 }
Dan Williams074cc472010-05-01 15:22:55 -0700382 spin_unlock_bh(&chan->cleanup_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700383 } else {
384 u16 active;
385
386 /* if the ring is idle, empty, and oversized try to step
387 * down the size
388 */
Dan Williams074cc472010-05-01 15:22:55 -0700389 spin_lock_bh(&chan->cleanup_lock);
390 spin_lock_bh(&ioat->prep_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700391 active = ioat2_ring_active(ioat);
392 if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
393 reshape_ring(ioat, ioat->alloc_order-1);
Dan Williams074cc472010-05-01 15:22:55 -0700394 spin_unlock_bh(&ioat->prep_lock);
395 spin_unlock_bh(&chan->cleanup_lock);
Dan Williamsbf40a682009-09-08 17:42:55 -0700396
397 /* keep shrinking until we get back to our minimum
398 * default size
399 */
400 if (ioat->alloc_order > ioat_get_alloc_order())
401 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
402 }
Dan Williamsbf40a682009-09-08 17:42:55 -0700403}
404
405static enum dma_status
406ioat3_is_complete(struct dma_chan *c, dma_cookie_t cookie,
407 dma_cookie_t *done, dma_cookie_t *used)
408{
409 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
410
411 if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
412 return DMA_SUCCESS;
413
Dan Williams074cc472010-05-01 15:22:55 -0700414 ioat3_cleanup(ioat);
Dan Williamsbf40a682009-09-08 17:42:55 -0700415
416 return ioat_is_complete(c, cookie, done, used);
417}
418
419static struct dma_async_tx_descriptor *
420ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
421 size_t len, unsigned long flags)
422{
423 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
424 struct ioat_ring_ent *desc;
425 size_t total_len = len;
426 struct ioat_fill_descriptor *fill;
Dan Williamsbf40a682009-09-08 17:42:55 -0700427 u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
Dan Williams074cc472010-05-01 15:22:55 -0700428 int num_descs, idx, i;
Dan Williamsbf40a682009-09-08 17:42:55 -0700429
430 num_descs = ioat2_xferlen_to_descs(ioat, len);
Dan Williams074cc472010-05-01 15:22:55 -0700431 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0)
432 idx = ioat->head;
Dan Williamsbf40a682009-09-08 17:42:55 -0700433 else
434 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700435 i = 0;
436 do {
Dan Williamsbf40a682009-09-08 17:42:55 -0700437 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
438
439 desc = ioat2_get_ring_ent(ioat, idx + i);
440 fill = desc->fill;
441
442 fill->size = xfer_size;
443 fill->src_data = src_data;
444 fill->dst_addr = dest;
445 fill->ctl = 0;
446 fill->ctl_f.op = IOAT_OP_FILL;
447
448 len -= xfer_size;
449 dest += xfer_size;
450 dump_desc_dbg(ioat, desc);
Dan Williamscdef57d2009-09-21 09:22:29 -0700451 } while (++i < num_descs);
Dan Williamsbf40a682009-09-08 17:42:55 -0700452
453 desc->txd.flags = flags;
454 desc->len = total_len;
455 fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
456 fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
457 fill->ctl_f.compl_write = 1;
458 dump_desc_dbg(ioat, desc);
459
460 /* we leave the channel locked to ensure in order submission */
461 return &desc->txd;
462}
463
Dan Williamsb094ad32009-09-08 17:42:57 -0700464static struct dma_async_tx_descriptor *
465__ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
466 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
467 size_t len, unsigned long flags)
468{
469 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
470 struct ioat_ring_ent *compl_desc;
471 struct ioat_ring_ent *desc;
472 struct ioat_ring_ent *ext;
473 size_t total_len = len;
474 struct ioat_xor_descriptor *xor;
475 struct ioat_xor_ext_descriptor *xor_ex = NULL;
476 struct ioat_dma_descriptor *hw;
Dan Williams074cc472010-05-01 15:22:55 -0700477 int num_descs, with_ext, idx, i;
Dan Williamsb094ad32009-09-08 17:42:57 -0700478 u32 offset = 0;
Dan Williamsb094ad32009-09-08 17:42:57 -0700479 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
480
481 BUG_ON(src_cnt < 2);
482
483 num_descs = ioat2_xferlen_to_descs(ioat, len);
484 /* we need 2x the number of descriptors to cover greater than 5
485 * sources
486 */
487 if (src_cnt > 5) {
488 with_ext = 1;
489 num_descs *= 2;
490 } else
491 with_ext = 0;
492
493 /* completion writes from the raid engine may pass completion
494 * writes from the legacy engine, so we need one extra null
495 * (legacy) descriptor to ensure all completion writes arrive in
496 * order.
497 */
Dan Williams074cc472010-05-01 15:22:55 -0700498 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
499 idx = ioat->head;
Dan Williamsb094ad32009-09-08 17:42:57 -0700500 else
501 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700502 i = 0;
503 do {
Dan Williamsb094ad32009-09-08 17:42:57 -0700504 struct ioat_raw_descriptor *descs[2];
505 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
506 int s;
507
508 desc = ioat2_get_ring_ent(ioat, idx + i);
509 xor = desc->xor;
510
511 /* save a branch by unconditionally retrieving the
512 * extended descriptor xor_set_src() knows to not write
513 * to it in the single descriptor case
514 */
515 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
516 xor_ex = ext->xor_ex;
517
518 descs[0] = (struct ioat_raw_descriptor *) xor;
519 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
520 for (s = 0; s < src_cnt; s++)
521 xor_set_src(descs, src[s], offset, s);
522 xor->size = xfer_size;
523 xor->dst_addr = dest + offset;
524 xor->ctl = 0;
525 xor->ctl_f.op = op;
526 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
527
528 len -= xfer_size;
529 offset += xfer_size;
530 dump_desc_dbg(ioat, desc);
Dan Williamscdef57d2009-09-21 09:22:29 -0700531 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsb094ad32009-09-08 17:42:57 -0700532
533 /* last xor descriptor carries the unmap parameters and fence bit */
534 desc->txd.flags = flags;
535 desc->len = total_len;
536 if (result)
537 desc->result = result;
538 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
539
540 /* completion descriptor carries interrupt bit */
541 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
542 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
543 hw = compl_desc->hw;
544 hw->ctl = 0;
545 hw->ctl_f.null = 1;
546 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
547 hw->ctl_f.compl_write = 1;
548 hw->size = NULL_DESC_BUFFER_SIZE;
549 dump_desc_dbg(ioat, compl_desc);
550
551 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700552 return &compl_desc->txd;
Dan Williamsb094ad32009-09-08 17:42:57 -0700553}
554
555static struct dma_async_tx_descriptor *
556ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
557 unsigned int src_cnt, size_t len, unsigned long flags)
558{
559 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
560}
561
562struct dma_async_tx_descriptor *
563ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
564 unsigned int src_cnt, size_t len,
565 enum sum_check_flags *result, unsigned long flags)
566{
567 /* the cleanup routine only sets bits on validate failure, it
568 * does not clear bits on validate success... so clear it here
569 */
570 *result = 0;
571
572 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
573 src_cnt - 1, len, flags);
574}
575
Dan Williamsd69d235b2009-09-08 17:42:59 -0700576static void
577dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
578{
579 struct device *dev = to_dev(&ioat->base);
580 struct ioat_pq_descriptor *pq = desc->pq;
581 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
582 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
583 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
584 int i;
585
586 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
587 " sz: %#x ctl: %#x (op: %d int: %d compl: %d pq: '%s%s' src_cnt: %d)\n",
588 desc_id(desc), (unsigned long long) desc->txd.phys,
589 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
590 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
591 pq->ctl_f.compl_write,
592 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
593 pq->ctl_f.src_cnt);
594 for (i = 0; i < src_cnt; i++)
595 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
596 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
597 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
598 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
599}
600
601static struct dma_async_tx_descriptor *
602__ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
603 const dma_addr_t *dst, const dma_addr_t *src,
604 unsigned int src_cnt, const unsigned char *scf,
605 size_t len, unsigned long flags)
606{
607 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
608 struct ioat_chan_common *chan = &ioat->base;
609 struct ioat_ring_ent *compl_desc;
610 struct ioat_ring_ent *desc;
611 struct ioat_ring_ent *ext;
612 size_t total_len = len;
613 struct ioat_pq_descriptor *pq;
614 struct ioat_pq_ext_descriptor *pq_ex = NULL;
615 struct ioat_dma_descriptor *hw;
616 u32 offset = 0;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700617 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
Dan Williams074cc472010-05-01 15:22:55 -0700618 int i, s, idx, with_ext, num_descs;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700619
620 dev_dbg(to_dev(chan), "%s\n", __func__);
621 /* the engine requires at least two sources (we provide
622 * at least 1 implied source in the DMA_PREP_CONTINUE case)
623 */
624 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
625
626 num_descs = ioat2_xferlen_to_descs(ioat, len);
627 /* we need 2x the number of descriptors to cover greater than 3
Dan Williamscd788092009-12-17 13:52:39 -0700628 * sources (we need 1 extra source in the q-only continuation
629 * case and 3 extra sources in the p+q continuation case.
Dan Williamsd69d235b2009-09-08 17:42:59 -0700630 */
Dan Williamscd788092009-12-17 13:52:39 -0700631 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
632 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700633 with_ext = 1;
634 num_descs *= 2;
635 } else
636 with_ext = 0;
637
638 /* completion writes from the raid engine may pass completion
639 * writes from the legacy engine, so we need one extra null
640 * (legacy) descriptor to ensure all completion writes arrive in
641 * order.
642 */
643 if (likely(num_descs) &&
Dan Williams074cc472010-05-01 15:22:55 -0700644 ioat2_check_space_lock(ioat, num_descs+1) == 0)
645 idx = ioat->head;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700646 else
647 return NULL;
Dan Williamscdef57d2009-09-21 09:22:29 -0700648 i = 0;
649 do {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700650 struct ioat_raw_descriptor *descs[2];
651 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
652
653 desc = ioat2_get_ring_ent(ioat, idx + i);
654 pq = desc->pq;
655
656 /* save a branch by unconditionally retrieving the
657 * extended descriptor pq_set_src() knows to not write
658 * to it in the single descriptor case
659 */
660 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
661 pq_ex = ext->pq_ex;
662
663 descs[0] = (struct ioat_raw_descriptor *) pq;
664 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
665
666 for (s = 0; s < src_cnt; s++)
667 pq_set_src(descs, src[s], offset, scf[s], s);
668
669 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
670 if (dmaf_p_disabled_continue(flags))
671 pq_set_src(descs, dst[1], offset, 1, s++);
672 else if (dmaf_continue(flags)) {
673 pq_set_src(descs, dst[0], offset, 0, s++);
674 pq_set_src(descs, dst[1], offset, 1, s++);
675 pq_set_src(descs, dst[1], offset, 0, s++);
676 }
677 pq->size = xfer_size;
678 pq->p_addr = dst[0] + offset;
679 pq->q_addr = dst[1] + offset;
680 pq->ctl = 0;
681 pq->ctl_f.op = op;
682 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
683 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
684 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
685
686 len -= xfer_size;
687 offset += xfer_size;
Dan Williamscdef57d2009-09-21 09:22:29 -0700688 } while ((i += 1 + with_ext) < num_descs);
Dan Williamsd69d235b2009-09-08 17:42:59 -0700689
690 /* last pq descriptor carries the unmap parameters and fence bit */
691 desc->txd.flags = flags;
692 desc->len = total_len;
693 if (result)
694 desc->result = result;
695 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
696 dump_pq_desc_dbg(ioat, desc, ext);
697
698 /* completion descriptor carries interrupt bit */
699 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
700 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
701 hw = compl_desc->hw;
702 hw->ctl = 0;
703 hw->ctl_f.null = 1;
704 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
705 hw->ctl_f.compl_write = 1;
706 hw->size = NULL_DESC_BUFFER_SIZE;
707 dump_desc_dbg(ioat, compl_desc);
708
709 /* we leave the channel locked to ensure in order submission */
Dan Williams49954c12009-11-19 17:11:03 -0700710 return &compl_desc->txd;
Dan Williamsd69d235b2009-09-08 17:42:59 -0700711}
712
713static struct dma_async_tx_descriptor *
714ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
715 unsigned int src_cnt, const unsigned char *scf, size_t len,
716 unsigned long flags)
717{
Dan Williamsde581b62009-11-19 17:08:45 -0700718 /* specify valid address for disabled result */
719 if (flags & DMA_PREP_PQ_DISABLE_P)
720 dst[0] = dst[1];
721 if (flags & DMA_PREP_PQ_DISABLE_Q)
722 dst[1] = dst[0];
723
Dan Williamsd69d235b2009-09-08 17:42:59 -0700724 /* handle the single source multiply case from the raid6
725 * recovery path
726 */
Dan Williamsde581b62009-11-19 17:08:45 -0700727 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
Dan Williamsd69d235b2009-09-08 17:42:59 -0700728 dma_addr_t single_source[2];
729 unsigned char single_source_coef[2];
730
731 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
732 single_source[0] = src[0];
733 single_source[1] = src[0];
734 single_source_coef[0] = scf[0];
735 single_source_coef[1] = 0;
736
737 return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
738 single_source_coef, len, flags);
739 } else
740 return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
741 len, flags);
742}
743
744struct dma_async_tx_descriptor *
745ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
746 unsigned int src_cnt, const unsigned char *scf, size_t len,
747 enum sum_check_flags *pqres, unsigned long flags)
748{
Dan Williamsde581b62009-11-19 17:08:45 -0700749 /* specify valid address for disabled result */
750 if (flags & DMA_PREP_PQ_DISABLE_P)
751 pq[0] = pq[1];
752 if (flags & DMA_PREP_PQ_DISABLE_Q)
753 pq[1] = pq[0];
754
Dan Williamsd69d235b2009-09-08 17:42:59 -0700755 /* the cleanup routine only sets bits on validate failure, it
756 * does not clear bits on validate success... so clear it here
757 */
758 *pqres = 0;
759
760 return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
761 flags);
762}
763
Dan Williamsae786622009-09-08 17:43:00 -0700764static struct dma_async_tx_descriptor *
765ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
766 unsigned int src_cnt, size_t len, unsigned long flags)
767{
768 unsigned char scf[src_cnt];
769 dma_addr_t pq[2];
770
771 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -0700772 pq[0] = dst;
Dan Williamsde581b62009-11-19 17:08:45 -0700773 flags |= DMA_PREP_PQ_DISABLE_Q;
774 pq[1] = dst; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -0700775
776 return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
777 flags);
778}
779
780struct dma_async_tx_descriptor *
781ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
782 unsigned int src_cnt, size_t len,
783 enum sum_check_flags *result, unsigned long flags)
784{
785 unsigned char scf[src_cnt];
786 dma_addr_t pq[2];
787
788 /* the cleanup routine only sets bits on validate failure, it
789 * does not clear bits on validate success... so clear it here
790 */
791 *result = 0;
792
793 memset(scf, 0, src_cnt);
Dan Williamsae786622009-09-08 17:43:00 -0700794 pq[0] = src[0];
Dan Williamsde581b62009-11-19 17:08:45 -0700795 flags |= DMA_PREP_PQ_DISABLE_Q;
796 pq[1] = pq[0]; /* specify valid address for disabled result */
Dan Williamsae786622009-09-08 17:43:00 -0700797
798 return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
799 len, flags);
800}
801
Dan Williams58c86492009-09-08 17:43:00 -0700802static struct dma_async_tx_descriptor *
803ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
804{
805 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
806 struct ioat_ring_ent *desc;
807 struct ioat_dma_descriptor *hw;
Dan Williams58c86492009-09-08 17:43:00 -0700808
Dan Williams074cc472010-05-01 15:22:55 -0700809 if (ioat2_check_space_lock(ioat, 1) == 0)
810 desc = ioat2_get_ring_ent(ioat, ioat->head);
Dan Williams58c86492009-09-08 17:43:00 -0700811 else
812 return NULL;
813
814 hw = desc->hw;
815 hw->ctl = 0;
816 hw->ctl_f.null = 1;
817 hw->ctl_f.int_en = 1;
818 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
819 hw->ctl_f.compl_write = 1;
820 hw->size = NULL_DESC_BUFFER_SIZE;
821 hw->src_addr = 0;
822 hw->dst_addr = 0;
823
824 desc->txd.flags = flags;
825 desc->len = 1;
826
827 dump_desc_dbg(ioat, desc);
828
829 /* we leave the channel locked to ensure in order submission */
830 return &desc->txd;
831}
832
Dan Williams9de6fc72009-09-08 17:42:58 -0700833static void __devinit ioat3_dma_test_callback(void *dma_async_param)
834{
835 struct completion *cmp = dma_async_param;
836
837 complete(cmp);
838}
839
840#define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
841static int __devinit ioat_xor_val_self_test(struct ioatdma_device *device)
842{
843 int i, src_idx;
844 struct page *dest;
845 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
846 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
847 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
848 dma_addr_t dma_addr, dest_dma;
849 struct dma_async_tx_descriptor *tx;
850 struct dma_chan *dma_chan;
851 dma_cookie_t cookie;
852 u8 cmp_byte = 0;
853 u32 cmp_word;
854 u32 xor_val_result;
855 int err = 0;
856 struct completion cmp;
857 unsigned long tmo;
858 struct device *dev = &device->pdev->dev;
859 struct dma_device *dma = &device->common;
860
861 dev_dbg(dev, "%s\n", __func__);
862
863 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
864 return 0;
865
866 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
867 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
868 if (!xor_srcs[src_idx]) {
869 while (src_idx--)
870 __free_page(xor_srcs[src_idx]);
871 return -ENOMEM;
872 }
873 }
874
875 dest = alloc_page(GFP_KERNEL);
876 if (!dest) {
877 while (src_idx--)
878 __free_page(xor_srcs[src_idx]);
879 return -ENOMEM;
880 }
881
882 /* Fill in src buffers */
883 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
884 u8 *ptr = page_address(xor_srcs[src_idx]);
885 for (i = 0; i < PAGE_SIZE; i++)
886 ptr[i] = (1 << src_idx);
887 }
888
889 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
890 cmp_byte ^= (u8) (1 << src_idx);
891
892 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
893 (cmp_byte << 8) | cmp_byte;
894
895 memset(page_address(dest), 0, PAGE_SIZE);
896
897 dma_chan = container_of(dma->channels.next, struct dma_chan,
898 device_node);
899 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
900 err = -ENODEV;
901 goto out;
902 }
903
904 /* test xor */
905 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
906 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
907 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
908 DMA_TO_DEVICE);
909 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
910 IOAT_NUM_SRC_TEST, PAGE_SIZE,
911 DMA_PREP_INTERRUPT);
912
913 if (!tx) {
914 dev_err(dev, "Self-test xor prep failed\n");
915 err = -ENODEV;
916 goto free_resources;
917 }
918
919 async_tx_ack(tx);
920 init_completion(&cmp);
921 tx->callback = ioat3_dma_test_callback;
922 tx->callback_param = &cmp;
923 cookie = tx->tx_submit(tx);
924 if (cookie < 0) {
925 dev_err(dev, "Self-test xor setup failed\n");
926 err = -ENODEV;
927 goto free_resources;
928 }
929 dma->device_issue_pending(dma_chan);
930
931 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
932
933 if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
934 dev_err(dev, "Self-test xor timed out\n");
935 err = -ENODEV;
936 goto free_resources;
937 }
938
939 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
940 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
941 u32 *ptr = page_address(dest);
942 if (ptr[i] != cmp_word) {
943 dev_err(dev, "Self-test xor failed compare\n");
944 err = -ENODEV;
945 goto free_resources;
946 }
947 }
948 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_TO_DEVICE);
949
950 /* skip validate if the capability is not present */
951 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
952 goto free_resources;
953
954 /* validate the sources with the destintation page */
955 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
956 xor_val_srcs[i] = xor_srcs[i];
957 xor_val_srcs[i] = dest;
958
959 xor_val_result = 1;
960
961 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
962 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
963 DMA_TO_DEVICE);
964 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
965 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
966 &xor_val_result, DMA_PREP_INTERRUPT);
967 if (!tx) {
968 dev_err(dev, "Self-test zero prep failed\n");
969 err = -ENODEV;
970 goto free_resources;
971 }
972
973 async_tx_ack(tx);
974 init_completion(&cmp);
975 tx->callback = ioat3_dma_test_callback;
976 tx->callback_param = &cmp;
977 cookie = tx->tx_submit(tx);
978 if (cookie < 0) {
979 dev_err(dev, "Self-test zero setup failed\n");
980 err = -ENODEV;
981 goto free_resources;
982 }
983 dma->device_issue_pending(dma_chan);
984
985 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
986
987 if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
988 dev_err(dev, "Self-test validate timed out\n");
989 err = -ENODEV;
990 goto free_resources;
991 }
992
993 if (xor_val_result != 0) {
994 dev_err(dev, "Self-test validate failed compare\n");
995 err = -ENODEV;
996 goto free_resources;
997 }
998
999 /* skip memset if the capability is not present */
1000 if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
1001 goto free_resources;
1002
1003 /* test memset */
1004 dma_addr = dma_map_page(dev, dest, 0,
1005 PAGE_SIZE, DMA_FROM_DEVICE);
1006 tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
1007 DMA_PREP_INTERRUPT);
1008 if (!tx) {
1009 dev_err(dev, "Self-test memset prep failed\n");
1010 err = -ENODEV;
1011 goto free_resources;
1012 }
1013
1014 async_tx_ack(tx);
1015 init_completion(&cmp);
1016 tx->callback = ioat3_dma_test_callback;
1017 tx->callback_param = &cmp;
1018 cookie = tx->tx_submit(tx);
1019 if (cookie < 0) {
1020 dev_err(dev, "Self-test memset setup failed\n");
1021 err = -ENODEV;
1022 goto free_resources;
1023 }
1024 dma->device_issue_pending(dma_chan);
1025
1026 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1027
1028 if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1029 dev_err(dev, "Self-test memset timed out\n");
1030 err = -ENODEV;
1031 goto free_resources;
1032 }
1033
1034 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1035 u32 *ptr = page_address(dest);
1036 if (ptr[i]) {
1037 dev_err(dev, "Self-test memset failed compare\n");
1038 err = -ENODEV;
1039 goto free_resources;
1040 }
1041 }
1042
1043 /* test for non-zero parity sum */
1044 xor_val_result = 0;
1045 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1046 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1047 DMA_TO_DEVICE);
1048 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1049 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
1050 &xor_val_result, DMA_PREP_INTERRUPT);
1051 if (!tx) {
1052 dev_err(dev, "Self-test 2nd zero prep failed\n");
1053 err = -ENODEV;
1054 goto free_resources;
1055 }
1056
1057 async_tx_ack(tx);
1058 init_completion(&cmp);
1059 tx->callback = ioat3_dma_test_callback;
1060 tx->callback_param = &cmp;
1061 cookie = tx->tx_submit(tx);
1062 if (cookie < 0) {
1063 dev_err(dev, "Self-test 2nd zero setup failed\n");
1064 err = -ENODEV;
1065 goto free_resources;
1066 }
1067 dma->device_issue_pending(dma_chan);
1068
1069 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1070
1071 if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1072 dev_err(dev, "Self-test 2nd validate timed out\n");
1073 err = -ENODEV;
1074 goto free_resources;
1075 }
1076
1077 if (xor_val_result != SUM_CHECK_P_RESULT) {
1078 dev_err(dev, "Self-test validate failed compare\n");
1079 err = -ENODEV;
1080 goto free_resources;
1081 }
1082
1083free_resources:
1084 dma->device_free_chan_resources(dma_chan);
1085out:
1086 src_idx = IOAT_NUM_SRC_TEST;
1087 while (src_idx--)
1088 __free_page(xor_srcs[src_idx]);
1089 __free_page(dest);
1090 return err;
1091}
1092
1093static int __devinit ioat3_dma_self_test(struct ioatdma_device *device)
1094{
1095 int rc = ioat_dma_self_test(device);
1096
1097 if (rc)
1098 return rc;
1099
1100 rc = ioat_xor_val_self_test(device);
1101 if (rc)
1102 return rc;
1103
1104 return 0;
1105}
1106
Dan Williamsa6d52d72009-12-19 15:36:02 -07001107static int ioat3_reset_hw(struct ioat_chan_common *chan)
1108{
1109 /* throw away whatever the channel was doing and get it
1110 * initialized, with ioat3 specific workarounds
1111 */
1112 struct ioatdma_device *device = chan->device;
1113 struct pci_dev *pdev = device->pdev;
1114 u32 chanerr;
1115 u16 dev_id;
1116 int err;
1117
1118 ioat2_quiesce(chan, msecs_to_jiffies(100));
1119
1120 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1121 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1122
1123 /* -= IOAT ver.3 workarounds =- */
1124 /* Write CHANERRMSK_INT with 3E07h to mask out the errors
1125 * that can cause stability issues for IOAT ver.3, and clear any
1126 * pending errors
1127 */
1128 pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
1129 err = pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1130 if (err) {
1131 dev_err(&pdev->dev, "channel error register unreachable\n");
1132 return err;
1133 }
1134 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
1135
1136 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1137 * (workaround for spurious config parity error after restart)
1138 */
1139 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1140 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
1141 pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
1142
1143 return ioat2_reset_sync(chan, msecs_to_jiffies(200));
1144}
1145
Dan Williamsbf40a682009-09-08 17:42:55 -07001146int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
1147{
1148 struct pci_dev *pdev = device->pdev;
Dan Williams228c4f52009-11-19 17:07:10 -07001149 int dca_en = system_has_dca_enabled(pdev);
Dan Williamsbf40a682009-09-08 17:42:55 -07001150 struct dma_device *dma;
1151 struct dma_chan *c;
1152 struct ioat_chan_common *chan;
Dan Williamse3232712009-09-08 17:43:02 -07001153 bool is_raid_device = false;
Dan Williamsbf40a682009-09-08 17:42:55 -07001154 int err;
Dan Williamsbf40a682009-09-08 17:42:55 -07001155 u32 cap;
1156
1157 device->enumerate_channels = ioat2_enumerate_channels;
Dan Williamsa6d52d72009-12-19 15:36:02 -07001158 device->reset_hw = ioat3_reset_hw;
Dan Williams9de6fc72009-09-08 17:42:58 -07001159 device->self_test = ioat3_dma_self_test;
Dan Williamsbf40a682009-09-08 17:42:55 -07001160 dma = &device->common;
1161 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1162 dma->device_issue_pending = ioat2_issue_pending;
1163 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1164 dma->device_free_chan_resources = ioat2_free_chan_resources;
Dan Williams58c86492009-09-08 17:43:00 -07001165
1166 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1167 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1168
Dan Williamsbf40a682009-09-08 17:42:55 -07001169 cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
Dan Williams228c4f52009-11-19 17:07:10 -07001170
1171 /* dca is incompatible with raid operations */
1172 if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1173 cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1174
Dan Williamsb094ad32009-09-08 17:42:57 -07001175 if (cap & IOAT_CAP_XOR) {
Dan Williamse3232712009-09-08 17:43:02 -07001176 is_raid_device = true;
Dan Williamsb094ad32009-09-08 17:42:57 -07001177 dma->max_xor = 8;
1178 dma->xor_align = 2;
1179
1180 dma_cap_set(DMA_XOR, dma->cap_mask);
1181 dma->device_prep_dma_xor = ioat3_prep_xor;
1182
1183 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1184 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1185 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001186 if (cap & IOAT_CAP_PQ) {
Dan Williamse3232712009-09-08 17:43:02 -07001187 is_raid_device = true;
Dan Williamsd69d235b2009-09-08 17:42:59 -07001188 dma_set_maxpq(dma, 8, 0);
1189 dma->pq_align = 2;
1190
1191 dma_cap_set(DMA_PQ, dma->cap_mask);
1192 dma->device_prep_dma_pq = ioat3_prep_pq;
1193
1194 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1195 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
Dan Williamsae786622009-09-08 17:43:00 -07001196
1197 if (!(cap & IOAT_CAP_XOR)) {
1198 dma->max_xor = 8;
1199 dma->xor_align = 2;
1200
1201 dma_cap_set(DMA_XOR, dma->cap_mask);
1202 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1203
1204 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1205 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1206 }
Dan Williamsd69d235b2009-09-08 17:42:59 -07001207 }
Dan Williamse3232712009-09-08 17:43:02 -07001208 if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
1209 dma_cap_set(DMA_MEMSET, dma->cap_mask);
1210 dma->device_prep_dma_memset = ioat3_prep_memset_lock;
1211 }
1212
1213
1214 if (is_raid_device) {
1215 dma->device_is_tx_complete = ioat3_is_complete;
Dan Williamsaa4d72a2010-03-03 21:21:13 -07001216 device->cleanup_fn = ioat3_cleanup_event;
Dan Williamse3232712009-09-08 17:43:02 -07001217 device->timer_fn = ioat3_timer_event;
1218 } else {
Dan Williamsaa4d72a2010-03-03 21:21:13 -07001219 dma->device_is_tx_complete = ioat_is_dma_complete;
1220 device->cleanup_fn = ioat2_cleanup_event;
Dan Williamse3232712009-09-08 17:43:02 -07001221 device->timer_fn = ioat2_timer_event;
1222 }
Dan Williamsbf40a682009-09-08 17:42:55 -07001223
Dan Williams7b3cc2b2009-11-19 17:10:37 -07001224 #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
1225 dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
1226 dma->device_prep_dma_pq_val = NULL;
1227 #endif
1228
1229 #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
1230 dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
1231 dma->device_prep_dma_xor_val = NULL;
1232 #endif
1233
Dan Williamsbf40a682009-09-08 17:42:55 -07001234 err = ioat_probe(device);
1235 if (err)
1236 return err;
1237 ioat_set_tcp_copy_break(262144);
1238
1239 list_for_each_entry(c, &dma->channels, device_node) {
1240 chan = to_chan_common(c);
1241 writel(IOAT_DMA_DCA_ANY_CPU,
1242 chan->reg_base + IOAT_DCACTRL_OFFSET);
1243 }
1244
1245 err = ioat_register(device);
1246 if (err)
1247 return err;
Dan Williams5669e312009-09-08 17:42:56 -07001248
1249 ioat_kobject_add(device, &ioat2_ktype);
1250
Dan Williamsbf40a682009-09-08 17:42:55 -07001251 if (dca)
1252 device->dca = ioat3_dca_init(pdev, device->reg_base);
1253
1254 return 0;
1255}