blob: da572968a7db2a9502ef11b13e9408b0d25f0335 [file] [log] [blame]
Chris Leech0bbd5f42006-05-23 17:35:34 -07001/*
Shannon Nelson43d6e362007-10-16 01:27:39 -07002 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2007 Intel Corporation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
Shannon Nelson43d6e362007-10-16 01:27:39 -07006 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07008 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
Shannon Nelson43d6e362007-10-16 01:27:39 -070015 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Chris Leech0bbd5f42006-05-23 17:35:34 -070017 *
Shannon Nelson43d6e362007-10-16 01:27:39 -070018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
Chris Leech0bbd5f42006-05-23 17:35:34 -070021 */
22
23/*
24 * This driver supports an Intel I/OAT DMA engine, which does asynchronous
25 * copy operations.
26 */
27
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/interrupt.h>
32#include <linux/dmaengine.h>
33#include <linux/delay.h>
David S. Miller6b00c922006-05-23 17:37:58 -070034#include <linux/dma-mapping.h>
Maciej Sosnowski09177e82008-07-22 10:07:33 -070035#include <linux/workqueue.h>
Chris Leech0bbd5f42006-05-23 17:35:34 -070036#include "ioatdma.h"
Chris Leech0bbd5f42006-05-23 17:35:34 -070037#include "ioatdma_registers.h"
38#include "ioatdma_hw.h"
39
40#define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
Shannon Nelson8ab89562007-10-16 01:27:39 -070041#define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
Chris Leech0bbd5f42006-05-23 17:35:34 -070042#define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
Dan Williams7405f742007-01-02 11:10:43 -070043#define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
Chris Leech0bbd5f42006-05-23 17:35:34 -070044
Maciej Sosnowski09177e82008-07-22 10:07:33 -070045#define chan_num(ch) ((int)((ch)->reg_base - (ch)->device->reg_base) / 0x80)
Shannon Nelson7bb67c12007-11-14 16:59:51 -080046static int ioat_pending_level = 4;
47module_param(ioat_pending_level, int, 0644);
48MODULE_PARM_DESC(ioat_pending_level,
49 "high-water mark for pushing ioat descriptors (default: 4)");
50
Maciej Sosnowski09177e82008-07-22 10:07:33 -070051#define RESET_DELAY msecs_to_jiffies(100)
52#define WATCHDOG_DELAY round_jiffies(msecs_to_jiffies(2000))
53static void ioat_dma_chan_reset_part2(struct work_struct *work);
54static void ioat_dma_chan_watchdog(struct work_struct *work);
55
Chris Leech0bbd5f42006-05-23 17:35:34 -070056/* internal functions */
Shannon Nelson43d6e362007-10-16 01:27:39 -070057static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
58static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -080059
Shannon Nelson7f2b2912007-10-18 03:07:14 -070060static struct ioat_desc_sw *
Shannon Nelson7bb67c12007-11-14 16:59:51 -080061ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
62static struct ioat_desc_sw *
63ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -070064
Shannon Nelson7f2b2912007-10-18 03:07:14 -070065static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
66 struct ioatdma_device *device,
67 int index)
Shannon Nelson3e037452007-10-16 01:27:40 -070068{
69 return device->idx[index];
70}
71
72/**
73 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
74 * @irq: interrupt id
75 * @data: interrupt data
76 */
77static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
78{
79 struct ioatdma_device *instance = data;
80 struct ioat_dma_chan *ioat_chan;
81 unsigned long attnstatus;
82 int bit;
83 u8 intrctrl;
84
85 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
86
87 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
88 return IRQ_NONE;
89
90 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
91 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
92 return IRQ_NONE;
93 }
94
95 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
96 for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
97 ioat_chan = ioat_lookup_chan_by_index(instance, bit);
98 tasklet_schedule(&ioat_chan->cleanup_task);
99 }
100
101 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
102 return IRQ_HANDLED;
103}
104
105/**
106 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
107 * @irq: interrupt id
108 * @data: interrupt data
109 */
110static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
111{
112 struct ioat_dma_chan *ioat_chan = data;
113
114 tasklet_schedule(&ioat_chan->cleanup_task);
115
116 return IRQ_HANDLED;
117}
118
119static void ioat_dma_cleanup_tasklet(unsigned long data);
120
121/**
122 * ioat_dma_enumerate_channels - find and initialize the device's channels
123 * @device: the device to be enumerated
124 */
Shannon Nelson8ab89562007-10-16 01:27:39 -0700125static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700126{
127 u8 xfercap_scale;
128 u32 xfercap;
129 int i;
130 struct ioat_dma_chan *ioat_chan;
131
Chris Leeche3828812007-03-08 09:57:35 -0800132 device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
133 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700134 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
135
136 for (i = 0; i < device->common.chancnt; i++) {
137 ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
138 if (!ioat_chan) {
139 device->common.chancnt = i;
140 break;
141 }
142
143 ioat_chan->device = device;
144 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
145 ioat_chan->xfercap = xfercap;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800146 ioat_chan->desccount = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700147 INIT_DELAYED_WORK(&ioat_chan->work, ioat_dma_chan_reset_part2);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800148 if (ioat_chan->device->version != IOAT_VER_1_2) {
149 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE
150 | IOAT_DMA_DCA_ANY_CPU,
151 ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
152 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700153 spin_lock_init(&ioat_chan->cleanup_lock);
154 spin_lock_init(&ioat_chan->desc_lock);
155 INIT_LIST_HEAD(&ioat_chan->free_desc);
156 INIT_LIST_HEAD(&ioat_chan->used_desc);
157 /* This should be made common somewhere in dmaengine.c */
158 ioat_chan->common.device = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700159 list_add_tail(&ioat_chan->common.device_node,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700160 &device->common.channels);
Shannon Nelson3e037452007-10-16 01:27:40 -0700161 device->idx[i] = ioat_chan;
162 tasklet_init(&ioat_chan->cleanup_task,
163 ioat_dma_cleanup_tasklet,
164 (unsigned long) ioat_chan);
165 tasklet_disable(&ioat_chan->cleanup_task);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700166 }
167 return device->common.chancnt;
168}
169
Shannon Nelson711924b2007-12-17 16:20:08 -0800170/**
171 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
172 * descriptors to hw
173 * @chan: DMA channel handle
174 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800175static inline void __ioat1_dma_memcpy_issue_pending(
Shannon Nelson711924b2007-12-17 16:20:08 -0800176 struct ioat_dma_chan *ioat_chan)
177{
178 ioat_chan->pending = 0;
179 writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
180}
181
182static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
183{
184 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
185
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700186 if (ioat_chan->pending > 0) {
Shannon Nelson711924b2007-12-17 16:20:08 -0800187 spin_lock_bh(&ioat_chan->desc_lock);
188 __ioat1_dma_memcpy_issue_pending(ioat_chan);
189 spin_unlock_bh(&ioat_chan->desc_lock);
190 }
191}
192
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800193static inline void __ioat2_dma_memcpy_issue_pending(
Shannon Nelson711924b2007-12-17 16:20:08 -0800194 struct ioat_dma_chan *ioat_chan)
195{
196 ioat_chan->pending = 0;
197 writew(ioat_chan->dmacount,
198 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
199}
200
201static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
202{
203 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
204
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700205 if (ioat_chan->pending > 0) {
Shannon Nelson711924b2007-12-17 16:20:08 -0800206 spin_lock_bh(&ioat_chan->desc_lock);
207 __ioat2_dma_memcpy_issue_pending(ioat_chan);
208 spin_unlock_bh(&ioat_chan->desc_lock);
209 }
210}
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800211
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700212
213/**
214 * ioat_dma_chan_reset_part2 - reinit the channel after a reset
215 */
216static void ioat_dma_chan_reset_part2(struct work_struct *work)
217{
218 struct ioat_dma_chan *ioat_chan =
219 container_of(work, struct ioat_dma_chan, work.work);
220 struct ioat_desc_sw *desc;
221
222 spin_lock_bh(&ioat_chan->cleanup_lock);
223 spin_lock_bh(&ioat_chan->desc_lock);
224
225 ioat_chan->completion_virt->low = 0;
226 ioat_chan->completion_virt->high = 0;
227 ioat_chan->pending = 0;
228
229 /*
230 * count the descriptors waiting, and be sure to do it
231 * right for both the CB1 line and the CB2 ring
232 */
233 ioat_chan->dmacount = 0;
234 if (ioat_chan->used_desc.prev) {
235 desc = to_ioat_desc(ioat_chan->used_desc.prev);
236 do {
237 ioat_chan->dmacount++;
238 desc = to_ioat_desc(desc->node.next);
239 } while (&desc->node != ioat_chan->used_desc.next);
240 }
241
242 /*
243 * write the new starting descriptor address
244 * this puts channel engine into ARMED state
245 */
246 desc = to_ioat_desc(ioat_chan->used_desc.prev);
247 switch (ioat_chan->device->version) {
248 case IOAT_VER_1_2:
249 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
250 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
251 writel(((u64) desc->async_tx.phys) >> 32,
252 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
253
254 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
255 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
256 break;
257 case IOAT_VER_2_0:
258 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
259 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
260 writel(((u64) desc->async_tx.phys) >> 32,
261 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
262
263 /* tell the engine to go with what's left to be done */
264 writew(ioat_chan->dmacount,
265 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
266
267 break;
268 }
269 dev_err(&ioat_chan->device->pdev->dev,
270 "chan%d reset - %d descs waiting, %d total desc\n",
271 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
272
273 spin_unlock_bh(&ioat_chan->desc_lock);
274 spin_unlock_bh(&ioat_chan->cleanup_lock);
275}
276
277/**
278 * ioat_dma_reset_channel - restart a channel
279 * @ioat_chan: IOAT DMA channel handle
280 */
281static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat_chan)
282{
283 u32 chansts, chanerr;
284
285 if (!ioat_chan->used_desc.prev)
286 return;
287
288 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
289 chansts = (ioat_chan->completion_virt->low
290 & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
291 if (chanerr) {
292 dev_err(&ioat_chan->device->pdev->dev,
293 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
294 chan_num(ioat_chan), chansts, chanerr);
295 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
296 }
297
298 /*
299 * whack it upside the head with a reset
300 * and wait for things to settle out.
301 * force the pending count to a really big negative
302 * to make sure no one forces an issue_pending
303 * while we're waiting.
304 */
305
306 spin_lock_bh(&ioat_chan->desc_lock);
307 ioat_chan->pending = INT_MIN;
308 writeb(IOAT_CHANCMD_RESET,
309 ioat_chan->reg_base
310 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
311 spin_unlock_bh(&ioat_chan->desc_lock);
312
313 /* schedule the 2nd half instead of sleeping a long time */
314 schedule_delayed_work(&ioat_chan->work, RESET_DELAY);
315}
316
317/**
318 * ioat_dma_chan_watchdog - watch for stuck channels
319 */
320static void ioat_dma_chan_watchdog(struct work_struct *work)
321{
322 struct ioatdma_device *device =
323 container_of(work, struct ioatdma_device, work.work);
324 struct ioat_dma_chan *ioat_chan;
325 int i;
326
327 union {
328 u64 full;
329 struct {
330 u32 low;
331 u32 high;
332 };
333 } completion_hw;
334 unsigned long compl_desc_addr_hw;
335
336 for (i = 0; i < device->common.chancnt; i++) {
337 ioat_chan = ioat_lookup_chan_by_index(device, i);
338
339 if (ioat_chan->device->version == IOAT_VER_1_2
340 /* have we started processing anything yet */
341 && ioat_chan->last_completion
342 /* have we completed any since last watchdog cycle? */
343 && (ioat_chan->last_completion ==
344 ioat_chan->watchdog_completion)
345 /* has TCP stuck on one cookie since last watchdog? */
346 && (ioat_chan->watchdog_tcp_cookie ==
347 ioat_chan->watchdog_last_tcp_cookie)
348 && (ioat_chan->watchdog_tcp_cookie !=
349 ioat_chan->completed_cookie)
350 /* is there something in the chain to be processed? */
351 /* CB1 chain always has at least the last one processed */
352 && (ioat_chan->used_desc.prev != ioat_chan->used_desc.next)
353 && ioat_chan->pending == 0) {
354
355 /*
356 * check CHANSTS register for completed
357 * descriptor address.
358 * if it is different than completion writeback,
359 * it is not zero
360 * and it has changed since the last watchdog
361 * we can assume that channel
362 * is still working correctly
363 * and the problem is in completion writeback.
364 * update completion writeback
365 * with actual CHANSTS value
366 * else
367 * try resetting the channel
368 */
369
370 completion_hw.low = readl(ioat_chan->reg_base +
371 IOAT_CHANSTS_OFFSET_LOW(ioat_chan->device->version));
372 completion_hw.high = readl(ioat_chan->reg_base +
373 IOAT_CHANSTS_OFFSET_HIGH(ioat_chan->device->version));
374#if (BITS_PER_LONG == 64)
375 compl_desc_addr_hw =
376 completion_hw.full
377 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
378#else
379 compl_desc_addr_hw =
380 completion_hw.low & IOAT_LOW_COMPLETION_MASK;
381#endif
382
383 if ((compl_desc_addr_hw != 0)
384 && (compl_desc_addr_hw != ioat_chan->watchdog_completion)
385 && (compl_desc_addr_hw != ioat_chan->last_compl_desc_addr_hw)) {
386 ioat_chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
387 ioat_chan->completion_virt->low = completion_hw.low;
388 ioat_chan->completion_virt->high = completion_hw.high;
389 } else {
390 ioat_dma_reset_channel(ioat_chan);
391 ioat_chan->watchdog_completion = 0;
392 ioat_chan->last_compl_desc_addr_hw = 0;
393 }
394
395 /*
396 * for version 2.0 if there are descriptors yet to be processed
397 * and the last completed hasn't changed since the last watchdog
398 * if they haven't hit the pending level
399 * issue the pending to push them through
400 * else
401 * try resetting the channel
402 */
403 } else if (ioat_chan->device->version == IOAT_VER_2_0
404 && ioat_chan->used_desc.prev
405 && ioat_chan->last_completion
406 && ioat_chan->last_completion == ioat_chan->watchdog_completion) {
407
408 if (ioat_chan->pending < ioat_pending_level)
409 ioat2_dma_memcpy_issue_pending(&ioat_chan->common);
410 else {
411 ioat_dma_reset_channel(ioat_chan);
412 ioat_chan->watchdog_completion = 0;
413 }
414 } else {
415 ioat_chan->last_compl_desc_addr_hw = 0;
416 ioat_chan->watchdog_completion
417 = ioat_chan->last_completion;
418 }
419
420 ioat_chan->watchdog_last_tcp_cookie =
421 ioat_chan->watchdog_tcp_cookie;
422 }
423
424 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
425}
426
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800427static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700428{
429 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700430 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
431 struct ioat_desc_sw *prev, *new;
432 struct ioat_dma_descriptor *hw;
Dan Williams7405f742007-01-02 11:10:43 -0700433 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700434 LIST_HEAD(new_chain);
435 u32 copy;
436 size_t len;
437 dma_addr_t src, dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700438 unsigned long orig_flags;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700439 unsigned int desc_count = 0;
Dan Williams7405f742007-01-02 11:10:43 -0700440
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700441 /* src and dest and len are stored in the initial descriptor */
442 len = first->len;
443 src = first->src;
444 dst = first->dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700445 orig_flags = first->async_tx.flags;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700446 new = first;
447
Dan Williams7405f742007-01-02 11:10:43 -0700448 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700449 prev = to_ioat_desc(ioat_chan->used_desc.prev);
450 prefetch(prev->hw);
451 do {
Shannon Nelson711924b2007-12-17 16:20:08 -0800452 copy = min_t(size_t, len, ioat_chan->xfercap);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700453
Dan Williams636bdea2008-04-17 20:17:26 -0700454 async_tx_ack(&new->async_tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700455
456 hw = new->hw;
457 hw->size = copy;
458 hw->ctl = 0;
459 hw->src_addr = src;
460 hw->dst_addr = dst;
461 hw->next = 0;
462
463 /* chain together the physical address list for the HW */
464 wmb();
465 prev->hw->next = (u64) new->async_tx.phys;
466
467 len -= copy;
468 dst += copy;
469 src += copy;
470
471 list_add_tail(&new->node, &new_chain);
472 desc_count++;
473 prev = new;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800474 } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700475
476 hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
Shannon Nelson95218432007-10-18 03:07:15 -0700477 if (new->async_tx.callback) {
478 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
479 if (first != new) {
480 /* move callback into to last desc */
481 new->async_tx.callback = first->async_tx.callback;
482 new->async_tx.callback_param
483 = first->async_tx.callback_param;
484 first->async_tx.callback = NULL;
485 first->async_tx.callback_param = NULL;
486 }
487 }
488
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700489 new->tx_cnt = desc_count;
Dan Williams636bdea2008-04-17 20:17:26 -0700490 new->async_tx.flags = orig_flags; /* client is in control of this ack */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700491
492 /* store the original values for use in later cleanup */
493 if (new != first) {
494 new->src = first->src;
495 new->dst = first->dst;
496 new->len = first->len;
497 }
498
Dan Williams7405f742007-01-02 11:10:43 -0700499 /* cookie incr and addition to used_list must be atomic */
500 cookie = ioat_chan->common.cookie;
501 cookie++;
502 if (cookie < 0)
503 cookie = 1;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700504 ioat_chan->common.cookie = new->async_tx.cookie = cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700505
506 /* write address into NextDescriptor field of last desc in chain */
507 to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700508 first->async_tx.phys;
509 __list_splice(&new_chain, ioat_chan->used_desc.prev);
Dan Williams7405f742007-01-02 11:10:43 -0700510
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800511 ioat_chan->dmacount += desc_count;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700512 ioat_chan->pending += desc_count;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800513 if (ioat_chan->pending >= ioat_pending_level)
514 __ioat1_dma_memcpy_issue_pending(ioat_chan);
Dan Williams7405f742007-01-02 11:10:43 -0700515 spin_unlock_bh(&ioat_chan->desc_lock);
516
Dan Williams7405f742007-01-02 11:10:43 -0700517 return cookie;
518}
519
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800520static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
521{
522 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
523 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
524 struct ioat_desc_sw *new;
525 struct ioat_dma_descriptor *hw;
526 dma_cookie_t cookie;
527 u32 copy;
528 size_t len;
529 dma_addr_t src, dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700530 unsigned long orig_flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800531 unsigned int desc_count = 0;
532
533 /* src and dest and len are stored in the initial descriptor */
534 len = first->len;
535 src = first->src;
536 dst = first->dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700537 orig_flags = first->async_tx.flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800538 new = first;
539
Shannon Nelson711924b2007-12-17 16:20:08 -0800540 /*
541 * ioat_chan->desc_lock is still in force in version 2 path
542 * it gets unlocked at end of this function
543 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800544 do {
Shannon Nelson711924b2007-12-17 16:20:08 -0800545 copy = min_t(size_t, len, ioat_chan->xfercap);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800546
Dan Williams636bdea2008-04-17 20:17:26 -0700547 async_tx_ack(&new->async_tx);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800548
549 hw = new->hw;
550 hw->size = copy;
551 hw->ctl = 0;
552 hw->src_addr = src;
553 hw->dst_addr = dst;
554
555 len -= copy;
556 dst += copy;
557 src += copy;
558 desc_count++;
559 } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
560
561 hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
562 if (new->async_tx.callback) {
563 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
564 if (first != new) {
565 /* move callback into to last desc */
566 new->async_tx.callback = first->async_tx.callback;
567 new->async_tx.callback_param
568 = first->async_tx.callback_param;
569 first->async_tx.callback = NULL;
570 first->async_tx.callback_param = NULL;
571 }
572 }
573
574 new->tx_cnt = desc_count;
Dan Williams636bdea2008-04-17 20:17:26 -0700575 new->async_tx.flags = orig_flags; /* client is in control of this ack */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800576
577 /* store the original values for use in later cleanup */
578 if (new != first) {
579 new->src = first->src;
580 new->dst = first->dst;
581 new->len = first->len;
582 }
583
584 /* cookie incr and addition to used_list must be atomic */
585 cookie = ioat_chan->common.cookie;
586 cookie++;
587 if (cookie < 0)
588 cookie = 1;
589 ioat_chan->common.cookie = new->async_tx.cookie = cookie;
590
591 ioat_chan->dmacount += desc_count;
592 ioat_chan->pending += desc_count;
593 if (ioat_chan->pending >= ioat_pending_level)
594 __ioat2_dma_memcpy_issue_pending(ioat_chan);
595 spin_unlock_bh(&ioat_chan->desc_lock);
596
597 return cookie;
598}
599
600/**
601 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
602 * @ioat_chan: the channel supplying the memory pool for the descriptors
603 * @flags: allocation flags
604 */
Chris Leech0bbd5f42006-05-23 17:35:34 -0700605static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
Shannon Nelson43d6e362007-10-16 01:27:39 -0700606 struct ioat_dma_chan *ioat_chan,
607 gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700608{
609 struct ioat_dma_descriptor *desc;
610 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700611 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700612 dma_addr_t phys;
613
Shannon Nelson8ab89562007-10-16 01:27:39 -0700614 ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
615 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700616 if (unlikely(!desc))
617 return NULL;
618
619 desc_sw = kzalloc(sizeof(*desc_sw), flags);
620 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700621 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700622 return NULL;
623 }
624
625 memset(desc, 0, sizeof(*desc));
Dan Williams7405f742007-01-02 11:10:43 -0700626 dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800627 switch (ioat_chan->device->version) {
628 case IOAT_VER_1_2:
629 desc_sw->async_tx.tx_submit = ioat1_tx_submit;
630 break;
631 case IOAT_VER_2_0:
632 desc_sw->async_tx.tx_submit = ioat2_tx_submit;
633 break;
634 }
Dan Williams7405f742007-01-02 11:10:43 -0700635 INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800636
Chris Leech0bbd5f42006-05-23 17:35:34 -0700637 desc_sw->hw = desc;
Dan Williams7405f742007-01-02 11:10:43 -0700638 desc_sw->async_tx.phys = phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700639
640 return desc_sw;
641}
642
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800643static int ioat_initial_desc_count = 256;
644module_param(ioat_initial_desc_count, int, 0644);
645MODULE_PARM_DESC(ioat_initial_desc_count,
646 "initial descriptors per channel (default: 256)");
647
648/**
649 * ioat2_dma_massage_chan_desc - link the descriptors into a circle
650 * @ioat_chan: the channel to be massaged
651 */
652static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
653{
654 struct ioat_desc_sw *desc, *_desc;
655
656 /* setup used_desc */
657 ioat_chan->used_desc.next = ioat_chan->free_desc.next;
658 ioat_chan->used_desc.prev = NULL;
659
660 /* pull free_desc out of the circle so that every node is a hw
661 * descriptor, but leave it pointing to the list
662 */
663 ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
664 ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
665
666 /* circle link the hw descriptors */
667 desc = to_ioat_desc(ioat_chan->free_desc.next);
668 desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
669 list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
670 desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
671 }
672}
673
674/**
675 * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
676 * @chan: the channel to be filled out
677 */
Haavard Skinnemoen848c5362008-07-08 11:58:58 -0700678static int ioat_dma_alloc_chan_resources(struct dma_chan *chan,
679 struct dma_client *client)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700680{
681 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800682 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700683 u16 chanctrl;
684 u32 chanerr;
685 int i;
686 LIST_HEAD(tmp_list);
687
Shannon Nelsone4223972007-08-24 23:02:53 -0700688 /* have we already been set up? */
689 if (!list_empty(&ioat_chan->free_desc))
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800690 return ioat_chan->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700691
Shannon Nelson43d6e362007-10-16 01:27:39 -0700692 /* Setup register to interrupt and write completion status on error */
Shannon Nelsone4223972007-08-24 23:02:53 -0700693 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
Chris Leech0bbd5f42006-05-23 17:35:34 -0700694 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
695 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Shannon Nelson43d6e362007-10-16 01:27:39 -0700696 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700697
Chris Leeche3828812007-03-08 09:57:35 -0800698 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700699 if (chanerr) {
Shannon Nelson43d6e362007-10-16 01:27:39 -0700700 dev_err(&ioat_chan->device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -0700701 "CHANERR = %x, clearing\n", chanerr);
Chris Leeche3828812007-03-08 09:57:35 -0800702 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700703 }
704
705 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800706 for (i = 0; i < ioat_initial_desc_count; i++) {
Chris Leech0bbd5f42006-05-23 17:35:34 -0700707 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
708 if (!desc) {
Shannon Nelson43d6e362007-10-16 01:27:39 -0700709 dev_err(&ioat_chan->device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -0700710 "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700711 break;
712 }
713 list_add_tail(&desc->node, &tmp_list);
714 }
715 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800716 ioat_chan->desccount = i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700717 list_splice(&tmp_list, &ioat_chan->free_desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800718 if (ioat_chan->device->version != IOAT_VER_1_2)
719 ioat2_dma_massage_chan_desc(ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700720 spin_unlock_bh(&ioat_chan->desc_lock);
721
722 /* allocate a completion writeback area */
723 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
724 ioat_chan->completion_virt =
725 pci_pool_alloc(ioat_chan->device->completion_pool,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700726 GFP_KERNEL,
727 &ioat_chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700728 memset(ioat_chan->completion_virt, 0,
729 sizeof(*ioat_chan->completion_virt));
Chris Leeche3828812007-03-08 09:57:35 -0800730 writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
731 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
732 writel(((u64) ioat_chan->completion_addr) >> 32,
733 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700734
Shannon Nelson3e037452007-10-16 01:27:40 -0700735 tasklet_enable(&ioat_chan->cleanup_task);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800736 ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
737 return ioat_chan->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700738}
739
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800740/**
741 * ioat_dma_free_chan_resources - release all the descriptors
742 * @chan: the channel to be cleaned
743 */
Chris Leech0bbd5f42006-05-23 17:35:34 -0700744static void ioat_dma_free_chan_resources(struct dma_chan *chan)
745{
746 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700747 struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700748 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700749 int in_use_descs = 0;
750
Shannon Nelson3e037452007-10-16 01:27:40 -0700751 tasklet_disable(&ioat_chan->cleanup_task);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700752 ioat_dma_memcpy_cleanup(ioat_chan);
753
Shannon Nelson3e037452007-10-16 01:27:40 -0700754 /* Delay 100ms after reset to allow internal DMA logic to quiesce
755 * before removing DMA descriptor resources.
756 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800757 writeb(IOAT_CHANCMD_RESET,
758 ioat_chan->reg_base
759 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700760 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700761
762 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800763 switch (ioat_chan->device->version) {
764 case IOAT_VER_1_2:
765 list_for_each_entry_safe(desc, _desc,
766 &ioat_chan->used_desc, node) {
767 in_use_descs++;
768 list_del(&desc->node);
769 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
770 desc->async_tx.phys);
771 kfree(desc);
772 }
773 list_for_each_entry_safe(desc, _desc,
774 &ioat_chan->free_desc, node) {
775 list_del(&desc->node);
776 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
777 desc->async_tx.phys);
778 kfree(desc);
779 }
780 break;
781 case IOAT_VER_2_0:
782 list_for_each_entry_safe(desc, _desc,
783 ioat_chan->free_desc.next, node) {
784 list_del(&desc->node);
785 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
786 desc->async_tx.phys);
787 kfree(desc);
788 }
789 desc = to_ioat_desc(ioat_chan->free_desc.next);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700790 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williams7405f742007-01-02 11:10:43 -0700791 desc->async_tx.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700792 kfree(desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800793 INIT_LIST_HEAD(&ioat_chan->free_desc);
794 INIT_LIST_HEAD(&ioat_chan->used_desc);
795 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700796 }
797 spin_unlock_bh(&ioat_chan->desc_lock);
798
Shannon Nelson8ab89562007-10-16 01:27:39 -0700799 pci_pool_free(ioatdma_device->completion_pool,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700800 ioat_chan->completion_virt,
801 ioat_chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700802
803 /* one is ok since we left it on there on purpose */
804 if (in_use_descs > 1)
Shannon Nelson43d6e362007-10-16 01:27:39 -0700805 dev_err(&ioat_chan->device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -0700806 "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700807 in_use_descs - 1);
808
809 ioat_chan->last_completion = ioat_chan->completion_addr = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700810 ioat_chan->pending = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800811 ioat_chan->dmacount = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700812 ioat_chan->watchdog_completion = 0;
813 ioat_chan->last_compl_desc_addr_hw = 0;
814 ioat_chan->watchdog_tcp_cookie =
815 ioat_chan->watchdog_last_tcp_cookie = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700816}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700817
Shannon Nelson3e037452007-10-16 01:27:40 -0700818/**
819 * ioat_dma_get_next_descriptor - return the next available descriptor
820 * @ioat_chan: IOAT DMA channel handle
821 *
822 * Gets the next descriptor from the chain, and must be called with the
823 * channel's desc_lock held. Allocates more descriptors if the channel
824 * has run out.
825 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700826static struct ioat_desc_sw *
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800827ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
Shannon Nelson3e037452007-10-16 01:27:40 -0700828{
Shannon Nelson711924b2007-12-17 16:20:08 -0800829 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700830
831 if (!list_empty(&ioat_chan->free_desc)) {
832 new = to_ioat_desc(ioat_chan->free_desc.next);
833 list_del(&new->node);
834 } else {
835 /* try to get another desc */
836 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800837 if (!new) {
838 dev_err(&ioat_chan->device->pdev->dev,
839 "alloc failed\n");
840 return NULL;
841 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700842 }
843
844 prefetch(new->hw);
845 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700846}
847
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800848static struct ioat_desc_sw *
849ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
850{
Shannon Nelson711924b2007-12-17 16:20:08 -0800851 struct ioat_desc_sw *new;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800852
853 /*
854 * used.prev points to where to start processing
855 * used.next points to next free descriptor
856 * if used.prev == NULL, there are none waiting to be processed
857 * if used.next == used.prev.prev, there is only one free descriptor,
858 * and we need to use it to as a noop descriptor before
859 * linking in a new set of descriptors, since the device
860 * has probably already read the pointer to it
861 */
862 if (ioat_chan->used_desc.prev &&
863 ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
864
Shannon Nelson711924b2007-12-17 16:20:08 -0800865 struct ioat_desc_sw *desc;
866 struct ioat_desc_sw *noop_desc;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800867 int i;
868
869 /* set up the noop descriptor */
870 noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
871 noop_desc->hw->size = 0;
872 noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
873 noop_desc->hw->src_addr = 0;
874 noop_desc->hw->dst_addr = 0;
875
876 ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
877 ioat_chan->pending++;
878 ioat_chan->dmacount++;
879
Shannon Nelson711924b2007-12-17 16:20:08 -0800880 /* try to get a few more descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800881 for (i = 16; i; i--) {
882 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800883 if (!desc) {
884 dev_err(&ioat_chan->device->pdev->dev,
885 "alloc failed\n");
886 break;
887 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800888 list_add_tail(&desc->node, ioat_chan->used_desc.next);
889
890 desc->hw->next
891 = to_ioat_desc(desc->node.next)->async_tx.phys;
892 to_ioat_desc(desc->node.prev)->hw->next
893 = desc->async_tx.phys;
894 ioat_chan->desccount++;
895 }
896
897 ioat_chan->used_desc.next = noop_desc->node.next;
898 }
899 new = to_ioat_desc(ioat_chan->used_desc.next);
900 prefetch(new);
901 ioat_chan->used_desc.next = new->node.next;
902
903 if (ioat_chan->used_desc.prev == NULL)
904 ioat_chan->used_desc.prev = &new->node;
905
906 prefetch(new->hw);
907 return new;
908}
909
910static struct ioat_desc_sw *ioat_dma_get_next_descriptor(
911 struct ioat_dma_chan *ioat_chan)
912{
913 if (!ioat_chan)
914 return NULL;
915
916 switch (ioat_chan->device->version) {
917 case IOAT_VER_1_2:
918 return ioat1_dma_get_next_descriptor(ioat_chan);
919 break;
920 case IOAT_VER_2_0:
921 return ioat2_dma_get_next_descriptor(ioat_chan);
922 break;
923 }
924 return NULL;
925}
926
927static struct dma_async_tx_descriptor *ioat1_dma_prep_memcpy(
Shannon Nelson43d6e362007-10-16 01:27:39 -0700928 struct dma_chan *chan,
Dan Williams00367312008-02-02 19:49:57 -0700929 dma_addr_t dma_dest,
930 dma_addr_t dma_src,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700931 size_t len,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700932 unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700933{
Dan Williams7405f742007-01-02 11:10:43 -0700934 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700935 struct ioat_desc_sw *new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700936
937 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700938 new = ioat_dma_get_next_descriptor(ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700939 spin_unlock_bh(&ioat_chan->desc_lock);
940
Shannon Nelson711924b2007-12-17 16:20:08 -0800941 if (new) {
942 new->len = len;
Dan Williams00367312008-02-02 19:49:57 -0700943 new->dst = dma_dest;
944 new->src = dma_src;
Dan Williams636bdea2008-04-17 20:17:26 -0700945 new->async_tx.flags = flags;
Shannon Nelson711924b2007-12-17 16:20:08 -0800946 return &new->async_tx;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700947 } else {
948 dev_err(&ioat_chan->device->pdev->dev,
949 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
950 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
Shannon Nelson711924b2007-12-17 16:20:08 -0800951 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700952 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700953}
954
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800955static struct dma_async_tx_descriptor *ioat2_dma_prep_memcpy(
956 struct dma_chan *chan,
Dan Williams00367312008-02-02 19:49:57 -0700957 dma_addr_t dma_dest,
958 dma_addr_t dma_src,
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800959 size_t len,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700960 unsigned long flags)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800961{
962 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
963 struct ioat_desc_sw *new;
964
965 spin_lock_bh(&ioat_chan->desc_lock);
966 new = ioat2_dma_get_next_descriptor(ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800967
Shannon Nelson711924b2007-12-17 16:20:08 -0800968 /*
969 * leave ioat_chan->desc_lock set in ioat 2 path
970 * it will get unlocked at end of tx_submit
971 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800972
Shannon Nelson711924b2007-12-17 16:20:08 -0800973 if (new) {
974 new->len = len;
Dan Williams00367312008-02-02 19:49:57 -0700975 new->dst = dma_dest;
976 new->src = dma_src;
Dan Williams636bdea2008-04-17 20:17:26 -0700977 new->async_tx.flags = flags;
Shannon Nelson711924b2007-12-17 16:20:08 -0800978 return &new->async_tx;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700979 } else {
980 spin_unlock_bh(&ioat_chan->desc_lock);
981 dev_err(&ioat_chan->device->pdev->dev,
982 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
983 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
Shannon Nelson711924b2007-12-17 16:20:08 -0800984 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700985 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700986}
987
Shannon Nelson3e037452007-10-16 01:27:40 -0700988static void ioat_dma_cleanup_tasklet(unsigned long data)
989{
990 struct ioat_dma_chan *chan = (void *)data;
991 ioat_dma_memcpy_cleanup(chan);
992 writew(IOAT_CHANCTRL_INT_DISABLE,
993 chan->reg_base + IOAT_CHANCTRL_OFFSET);
994}
995
Dan Williamse1d181e2008-07-04 00:13:40 -0700996static void
997ioat_dma_unmap(struct ioat_dma_chan *ioat_chan, struct ioat_desc_sw *desc)
998{
999 /*
1000 * yes we are unmapping both _page and _single
1001 * alloc'd regions with unmap_page. Is this
1002 * *really* that bad?
1003 */
1004 if (!(desc->async_tx.flags & DMA_COMPL_SKIP_DEST_UNMAP))
1005 pci_unmap_page(ioat_chan->device->pdev,
1006 pci_unmap_addr(desc, dst),
1007 pci_unmap_len(desc, len),
1008 PCI_DMA_FROMDEVICE);
1009
1010 if (!(desc->async_tx.flags & DMA_COMPL_SKIP_SRC_UNMAP))
1011 pci_unmap_page(ioat_chan->device->pdev,
1012 pci_unmap_addr(desc, src),
1013 pci_unmap_len(desc, len),
1014 PCI_DMA_TODEVICE);
1015}
1016
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001017/**
1018 * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
1019 * @chan: ioat channel to be cleaned up
1020 */
Shannon Nelson43d6e362007-10-16 01:27:39 -07001021static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001022{
1023 unsigned long phys_complete;
1024 struct ioat_desc_sw *desc, *_desc;
1025 dma_cookie_t cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001026 unsigned long desc_phys;
1027 struct ioat_desc_sw *latest_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001028
Shannon Nelson43d6e362007-10-16 01:27:39 -07001029 prefetch(ioat_chan->completion_virt);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001030
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001031 if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
Chris Leech0bbd5f42006-05-23 17:35:34 -07001032 return;
1033
1034 /* The completion writeback can happen at any time,
1035 so reads by the driver need to be atomic operations
1036 The descriptor physical addresses are limited to 32-bits
1037 when the CPU can only do a 32-bit mov */
1038
1039#if (BITS_PER_LONG == 64)
1040 phys_complete =
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001041 ioat_chan->completion_virt->full
1042 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001043#else
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001044 phys_complete =
1045 ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001046#endif
1047
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001048 if ((ioat_chan->completion_virt->full
1049 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
Shannon Nelson43d6e362007-10-16 01:27:39 -07001050 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
1051 dev_err(&ioat_chan->device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -07001052 "Channel halted, chanerr = %x\n",
Shannon Nelson43d6e362007-10-16 01:27:39 -07001053 readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -07001054
1055 /* TODO do something to salvage the situation */
1056 }
1057
Shannon Nelson43d6e362007-10-16 01:27:39 -07001058 if (phys_complete == ioat_chan->last_completion) {
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001059 spin_unlock_bh(&ioat_chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001060 /*
1061 * perhaps we're stuck so hard that the watchdog can't go off?
1062 * try to catch it after 2 seconds
1063 */
1064 if (time_after(jiffies,
1065 ioat_chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
1066 ioat_dma_chan_watchdog(&(ioat_chan->device->work.work));
1067 ioat_chan->last_completion_time = jiffies;
1068 }
1069 return;
1070 }
1071 ioat_chan->last_completion_time = jiffies;
1072
1073 cookie = 0;
1074 if (!spin_trylock_bh(&ioat_chan->desc_lock)) {
1075 spin_unlock_bh(&ioat_chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001076 return;
1077 }
1078
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001079 switch (ioat_chan->device->version) {
1080 case IOAT_VER_1_2:
1081 list_for_each_entry_safe(desc, _desc,
1082 &ioat_chan->used_desc, node) {
Chris Leech0bbd5f42006-05-23 17:35:34 -07001083
Shannon Nelson43d6e362007-10-16 01:27:39 -07001084 /*
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001085 * Incoming DMA requests may use multiple descriptors,
1086 * due to exceeding xfercap, perhaps. If so, only the
1087 * last one will have a cookie, and require unmapping.
Shannon Nelson43d6e362007-10-16 01:27:39 -07001088 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001089 if (desc->async_tx.cookie) {
1090 cookie = desc->async_tx.cookie;
Dan Williamse1d181e2008-07-04 00:13:40 -07001091 ioat_dma_unmap(ioat_chan, desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001092 if (desc->async_tx.callback) {
1093 desc->async_tx.callback(desc->async_tx.callback_param);
1094 desc->async_tx.callback = NULL;
1095 }
1096 }
1097
1098 if (desc->async_tx.phys != phys_complete) {
1099 /*
1100 * a completed entry, but not the last, so clean
1101 * up if the client is done with the descriptor
1102 */
Dan Williams636bdea2008-04-17 20:17:26 -07001103 if (async_tx_test_ack(&desc->async_tx)) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001104 list_del(&desc->node);
1105 list_add_tail(&desc->node,
1106 &ioat_chan->free_desc);
1107 } else
1108 desc->async_tx.cookie = 0;
1109 } else {
1110 /*
1111 * last used desc. Do not remove, so we can
1112 * append from it, but don't look at it next
1113 * time, either
1114 */
1115 desc->async_tx.cookie = 0;
1116
1117 /* TODO check status bits? */
1118 break;
Shannon Nelson95218432007-10-18 03:07:15 -07001119 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001120 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001121 break;
1122 case IOAT_VER_2_0:
1123 /* has some other thread has already cleaned up? */
1124 if (ioat_chan->used_desc.prev == NULL)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001125 break;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001126
1127 /* work backwards to find latest finished desc */
1128 desc = to_ioat_desc(ioat_chan->used_desc.next);
1129 latest_desc = NULL;
1130 do {
1131 desc = to_ioat_desc(desc->node.prev);
1132 desc_phys = (unsigned long)desc->async_tx.phys
1133 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
1134 if (desc_phys == phys_complete) {
1135 latest_desc = desc;
1136 break;
1137 }
1138 } while (&desc->node != ioat_chan->used_desc.prev);
1139
1140 if (latest_desc != NULL) {
1141
1142 /* work forwards to clear finished descriptors */
1143 for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
1144 &desc->node != latest_desc->node.next &&
1145 &desc->node != ioat_chan->used_desc.next;
1146 desc = to_ioat_desc(desc->node.next)) {
1147 if (desc->async_tx.cookie) {
1148 cookie = desc->async_tx.cookie;
1149 desc->async_tx.cookie = 0;
Dan Williamse1d181e2008-07-04 00:13:40 -07001150 ioat_dma_unmap(ioat_chan, desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001151 if (desc->async_tx.callback) {
1152 desc->async_tx.callback(desc->async_tx.callback_param);
1153 desc->async_tx.callback = NULL;
1154 }
1155 }
1156 }
1157
1158 /* move used.prev up beyond those that are finished */
1159 if (&desc->node == ioat_chan->used_desc.next)
1160 ioat_chan->used_desc.prev = NULL;
1161 else
1162 ioat_chan->used_desc.prev = &desc->node;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001163 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001164 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001165 }
1166
Shannon Nelson43d6e362007-10-16 01:27:39 -07001167 spin_unlock_bh(&ioat_chan->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001168
Shannon Nelson43d6e362007-10-16 01:27:39 -07001169 ioat_chan->last_completion = phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001170 if (cookie != 0)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001171 ioat_chan->completed_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001172
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001173 spin_unlock_bh(&ioat_chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001174}
1175
1176/**
1177 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
1178 * @chan: IOAT DMA channel handle
1179 * @cookie: DMA transaction identifier
Randy Dunlap65088712006-07-03 19:45:31 -07001180 * @done: if not %NULL, updated with last completed transaction
1181 * @used: if not %NULL, updated with last used transaction
Chris Leech0bbd5f42006-05-23 17:35:34 -07001182 */
Chris Leech0bbd5f42006-05-23 17:35:34 -07001183static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -07001184 dma_cookie_t cookie,
1185 dma_cookie_t *done,
1186 dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001187{
1188 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
1189 dma_cookie_t last_used;
1190 dma_cookie_t last_complete;
1191 enum dma_status ret;
1192
1193 last_used = chan->cookie;
1194 last_complete = ioat_chan->completed_cookie;
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001195 ioat_chan->watchdog_tcp_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001196
1197 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001198 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001199 if (used)
1200 *used = last_used;
1201
1202 ret = dma_async_is_complete(cookie, last_complete, last_used);
1203 if (ret == DMA_SUCCESS)
1204 return ret;
1205
1206 ioat_dma_memcpy_cleanup(ioat_chan);
1207
1208 last_used = chan->cookie;
1209 last_complete = ioat_chan->completed_cookie;
1210
1211 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001212 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001213 if (used)
1214 *used = last_used;
1215
1216 return dma_async_is_complete(cookie, last_complete, last_used);
1217}
1218
Shannon Nelson43d6e362007-10-16 01:27:39 -07001219static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001220{
1221 struct ioat_desc_sw *desc;
1222
1223 spin_lock_bh(&ioat_chan->desc_lock);
1224
Shannon Nelson3e037452007-10-16 01:27:40 -07001225 desc = ioat_dma_get_next_descriptor(ioat_chan);
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001226 desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
1227 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
1228 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001229 desc->hw->size = 0;
1230 desc->hw->src_addr = 0;
1231 desc->hw->dst_addr = 0;
Dan Williams636bdea2008-04-17 20:17:26 -07001232 async_tx_ack(&desc->async_tx);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001233 switch (ioat_chan->device->version) {
1234 case IOAT_VER_1_2:
1235 desc->hw->next = 0;
1236 list_add_tail(&desc->node, &ioat_chan->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001237
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001238 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
1239 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
1240 writel(((u64) desc->async_tx.phys) >> 32,
1241 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
1242
1243 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
1244 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
1245 break;
1246 case IOAT_VER_2_0:
1247 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
1248 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
1249 writel(((u64) desc->async_tx.phys) >> 32,
1250 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
1251
1252 ioat_chan->dmacount++;
1253 __ioat2_dma_memcpy_issue_pending(ioat_chan);
1254 break;
1255 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001256 spin_unlock_bh(&ioat_chan->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001257}
1258
1259/*
1260 * Perform a IOAT transaction to verify the HW works.
1261 */
1262#define IOAT_TEST_SIZE 2000
1263
Shannon Nelson95218432007-10-18 03:07:15 -07001264static void ioat_dma_test_callback(void *dma_async_param)
1265{
1266 printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
Shannon Nelson711924b2007-12-17 16:20:08 -08001267 dma_async_param);
Shannon Nelson95218432007-10-18 03:07:15 -07001268}
1269
Shannon Nelson3e037452007-10-16 01:27:40 -07001270/**
1271 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
1272 * @device: device to be tested
1273 */
1274static int ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001275{
1276 int i;
1277 u8 *src;
1278 u8 *dest;
1279 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -08001280 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -07001281 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001282 dma_cookie_t cookie;
1283 int err = 0;
1284
Christoph Lametere94b1762006-12-06 20:33:17 -08001285 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001286 if (!src)
1287 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -08001288 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001289 if (!dest) {
1290 kfree(src);
1291 return -ENOMEM;
1292 }
1293
1294 /* Fill in src buffer */
1295 for (i = 0; i < IOAT_TEST_SIZE; i++)
1296 src[i] = (u8)i;
1297
1298 /* Start copy, using first DMA channel */
1299 dma_chan = container_of(device->common.channels.next,
Shannon Nelson43d6e362007-10-16 01:27:39 -07001300 struct dma_chan,
1301 device_node);
Haavard Skinnemoen848c5362008-07-08 11:58:58 -07001302 if (device->common.device_alloc_chan_resources(dma_chan, NULL) < 1) {
Shannon Nelson43d6e362007-10-16 01:27:39 -07001303 dev_err(&device->pdev->dev,
1304 "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001305 err = -ENODEV;
1306 goto out;
1307 }
1308
Dan Williams00367312008-02-02 19:49:57 -07001309 dma_src = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
1310 DMA_TO_DEVICE);
1311 dma_dest = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
1312 DMA_FROM_DEVICE);
1313 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
1314 IOAT_TEST_SIZE, 0);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001315 if (!tx) {
1316 dev_err(&device->pdev->dev,
1317 "Self-test prep failed, disabling\n");
1318 err = -ENODEV;
1319 goto free_resources;
1320 }
1321
Dan Williams7405f742007-01-02 11:10:43 -07001322 async_tx_ack(tx);
Shannon Nelson95218432007-10-18 03:07:15 -07001323 tx->callback = ioat_dma_test_callback;
1324 tx->callback_param = (void *)0x8086;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001325 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001326 if (cookie < 0) {
1327 dev_err(&device->pdev->dev,
1328 "Self-test setup failed, disabling\n");
1329 err = -ENODEV;
1330 goto free_resources;
1331 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001332 device->common.device_issue_pending(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001333 msleep(1);
1334
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001335 if (device->common.device_is_tx_complete(dma_chan, cookie, NULL, NULL)
1336 != DMA_SUCCESS) {
Shannon Nelson43d6e362007-10-16 01:27:39 -07001337 dev_err(&device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -07001338 "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001339 err = -ENODEV;
1340 goto free_resources;
1341 }
1342 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Shannon Nelson43d6e362007-10-16 01:27:39 -07001343 dev_err(&device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -07001344 "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001345 err = -ENODEV;
1346 goto free_resources;
1347 }
1348
1349free_resources:
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001350 device->common.device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001351out:
1352 kfree(src);
1353 kfree(dest);
1354 return err;
1355}
1356
Shannon Nelson3e037452007-10-16 01:27:40 -07001357static char ioat_interrupt_style[32] = "msix";
1358module_param_string(ioat_interrupt_style, ioat_interrupt_style,
1359 sizeof(ioat_interrupt_style), 0644);
1360MODULE_PARM_DESC(ioat_interrupt_style,
1361 "set ioat interrupt style: msix (default), "
1362 "msix-single-vector, msi, intx)");
1363
1364/**
1365 * ioat_dma_setup_interrupts - setup interrupt handler
1366 * @device: ioat device
1367 */
1368static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
1369{
1370 struct ioat_dma_chan *ioat_chan;
1371 int err, i, j, msixcnt;
1372 u8 intrctrl = 0;
1373
1374 if (!strcmp(ioat_interrupt_style, "msix"))
1375 goto msix;
1376 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
1377 goto msix_single_vector;
1378 if (!strcmp(ioat_interrupt_style, "msi"))
1379 goto msi;
1380 if (!strcmp(ioat_interrupt_style, "intx"))
1381 goto intx;
Shannon Nelson5149fd02007-10-18 03:07:13 -07001382 dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
1383 ioat_interrupt_style);
1384 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001385
1386msix:
1387 /* The number of MSI-X vectors should equal the number of channels */
1388 msixcnt = device->common.chancnt;
1389 for (i = 0; i < msixcnt; i++)
1390 device->msix_entries[i].entry = i;
1391
1392 err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
1393 if (err < 0)
1394 goto msi;
1395 if (err > 0)
1396 goto msix_single_vector;
1397
1398 for (i = 0; i < msixcnt; i++) {
1399 ioat_chan = ioat_lookup_chan_by_index(device, i);
1400 err = request_irq(device->msix_entries[i].vector,
1401 ioat_dma_do_interrupt_msix,
1402 0, "ioat-msix", ioat_chan);
1403 if (err) {
1404 for (j = 0; j < i; j++) {
1405 ioat_chan =
1406 ioat_lookup_chan_by_index(device, j);
1407 free_irq(device->msix_entries[j].vector,
1408 ioat_chan);
1409 }
1410 goto msix_single_vector;
1411 }
1412 }
1413 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
1414 device->irq_mode = msix_multi_vector;
1415 goto done;
1416
1417msix_single_vector:
1418 device->msix_entries[0].entry = 0;
1419 err = pci_enable_msix(device->pdev, device->msix_entries, 1);
1420 if (err)
1421 goto msi;
1422
1423 err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
1424 0, "ioat-msix", device);
1425 if (err) {
1426 pci_disable_msix(device->pdev);
1427 goto msi;
1428 }
1429 device->irq_mode = msix_single_vector;
1430 goto done;
1431
1432msi:
1433 err = pci_enable_msi(device->pdev);
1434 if (err)
1435 goto intx;
1436
1437 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
1438 0, "ioat-msi", device);
1439 if (err) {
1440 pci_disable_msi(device->pdev);
1441 goto intx;
1442 }
1443 /*
1444 * CB 1.2 devices need a bit set in configuration space to enable MSI
1445 */
1446 if (device->version == IOAT_VER_1_2) {
1447 u32 dmactrl;
1448 pci_read_config_dword(device->pdev,
1449 IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1450 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1451 pci_write_config_dword(device->pdev,
1452 IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1453 }
1454 device->irq_mode = msi;
1455 goto done;
1456
1457intx:
1458 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
1459 IRQF_SHARED, "ioat-intx", device);
1460 if (err)
1461 goto err_no_irq;
1462 device->irq_mode = intx;
1463
1464done:
1465 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1466 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1467 return 0;
1468
1469err_no_irq:
1470 /* Disable all interrupt generation */
1471 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
1472 dev_err(&device->pdev->dev, "no usable interrupts\n");
1473 device->irq_mode = none;
1474 return -1;
1475}
1476
1477/**
1478 * ioat_dma_remove_interrupts - remove whatever interrupts were set
1479 * @device: ioat device
1480 */
1481static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
1482{
1483 struct ioat_dma_chan *ioat_chan;
1484 int i;
1485
1486 /* Disable all interrupt generation */
1487 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
1488
1489 switch (device->irq_mode) {
1490 case msix_multi_vector:
1491 for (i = 0; i < device->common.chancnt; i++) {
1492 ioat_chan = ioat_lookup_chan_by_index(device, i);
1493 free_irq(device->msix_entries[i].vector, ioat_chan);
1494 }
1495 pci_disable_msix(device->pdev);
1496 break;
1497 case msix_single_vector:
1498 free_irq(device->msix_entries[0].vector, device);
1499 pci_disable_msix(device->pdev);
1500 break;
1501 case msi:
1502 free_irq(device->pdev->irq, device);
1503 pci_disable_msi(device->pdev);
1504 break;
1505 case intx:
1506 free_irq(device->pdev->irq, device);
1507 break;
1508 case none:
1509 dev_warn(&device->pdev->dev,
1510 "call to %s without interrupts setup\n", __func__);
1511 }
1512 device->irq_mode = none;
1513}
1514
Shannon Nelson8ab89562007-10-16 01:27:39 -07001515struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
1516 void __iomem *iobase)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001517{
1518 int err;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001519 struct ioatdma_device *device;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001520
1521 device = kzalloc(sizeof(*device), GFP_KERNEL);
1522 if (!device) {
1523 err = -ENOMEM;
1524 goto err_kzalloc;
1525 }
Shannon Nelson8ab89562007-10-16 01:27:39 -07001526 device->pdev = pdev;
1527 device->reg_base = iobase;
1528 device->version = readb(device->reg_base + IOAT_VER_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001529
1530 /* DMA coherent memory pool for DMA descriptor allocations */
1531 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001532 sizeof(struct ioat_dma_descriptor),
1533 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001534 if (!device->dma_pool) {
1535 err = -ENOMEM;
1536 goto err_dma_pool;
1537 }
1538
Shannon Nelson43d6e362007-10-16 01:27:39 -07001539 device->completion_pool = pci_pool_create("completion_pool", pdev,
1540 sizeof(u64), SMP_CACHE_BYTES,
1541 SMP_CACHE_BYTES);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001542 if (!device->completion_pool) {
1543 err = -ENOMEM;
1544 goto err_completion_pool;
1545 }
1546
Chris Leech0bbd5f42006-05-23 17:35:34 -07001547 INIT_LIST_HEAD(&device->common.channels);
Shannon Nelson43d6e362007-10-16 01:27:39 -07001548 ioat_dma_enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001549
Shannon Nelson43d6e362007-10-16 01:27:39 -07001550 device->common.device_alloc_chan_resources =
1551 ioat_dma_alloc_chan_resources;
1552 device->common.device_free_chan_resources =
1553 ioat_dma_free_chan_resources;
Dan Williams7405f742007-01-02 11:10:43 -07001554 device->common.dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001555
1556 dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
1557 device->common.device_is_tx_complete = ioat_dma_is_complete;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001558 switch (device->version) {
1559 case IOAT_VER_1_2:
1560 device->common.device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1561 device->common.device_issue_pending =
1562 ioat1_dma_memcpy_issue_pending;
1563 break;
1564 case IOAT_VER_2_0:
1565 device->common.device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
1566 device->common.device_issue_pending =
1567 ioat2_dma_memcpy_issue_pending;
1568 break;
1569 }
1570
Shannon Nelson3e037452007-10-16 01:27:40 -07001571 dev_err(&device->pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -07001572 "Intel(R) I/OAT DMA Engine found,"
1573 " %d channels, device version 0x%02x, driver version %s\n",
1574 device->common.chancnt, device->version, IOAT_DMA_VERSION);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001575
Shannon Nelson3e037452007-10-16 01:27:40 -07001576 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001577 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001578 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001579
Shannon Nelson3e037452007-10-16 01:27:40 -07001580 err = ioat_dma_self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001581 if (err)
1582 goto err_self_test;
1583
1584 dma_async_device_register(&device->common);
1585
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001586 INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
1587 schedule_delayed_work(&device->work,
1588 WATCHDOG_DELAY);
1589
Shannon Nelson8ab89562007-10-16 01:27:39 -07001590 return device;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001591
1592err_self_test:
Shannon Nelson3e037452007-10-16 01:27:40 -07001593 ioat_dma_remove_interrupts(device);
1594err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001595 pci_pool_destroy(device->completion_pool);
1596err_completion_pool:
1597 pci_pool_destroy(device->dma_pool);
1598err_dma_pool:
1599 kfree(device);
1600err_kzalloc:
Shannon Nelsonbb8e8bc2007-12-17 16:20:08 -08001601 dev_err(&pdev->dev,
Shannon Nelson5149fd02007-10-18 03:07:13 -07001602 "Intel(R) I/OAT DMA Engine initialization failed\n");
Shannon Nelson8ab89562007-10-16 01:27:39 -07001603 return NULL;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001604}
1605
Shannon Nelson8ab89562007-10-16 01:27:39 -07001606void ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001607{
Chris Leech0bbd5f42006-05-23 17:35:34 -07001608 struct dma_chan *chan, *_chan;
1609 struct ioat_dma_chan *ioat_chan;
1610
Shannon Nelson3e037452007-10-16 01:27:40 -07001611 ioat_dma_remove_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001612
Shannon Nelsondfe22992007-10-18 03:07:13 -07001613 dma_async_device_unregister(&device->common);
1614
Chris Leech0bbd5f42006-05-23 17:35:34 -07001615 pci_pool_destroy(device->dma_pool);
1616 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001617
Shannon Nelson7df7cf02007-10-18 03:07:12 -07001618 iounmap(device->reg_base);
1619 pci_release_regions(device->pdev);
1620 pci_disable_device(device->pdev);
1621
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001622 cancel_delayed_work(&device->work);
1623
Shannon Nelson43d6e362007-10-16 01:27:39 -07001624 list_for_each_entry_safe(chan, _chan,
1625 &device->common.channels, device_node) {
Chris Leech0bbd5f42006-05-23 17:35:34 -07001626 ioat_chan = to_ioat_chan(chan);
1627 list_del(&chan->device_node);
1628 kfree(ioat_chan);
1629 }
1630 kfree(device);
1631}
1632