1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6 * Philip Kelleher <pjk1939@linux.vnet.ibm.com>
8 * (C) Copyright 2013 IBM Corporation
11 #include <linux/slab.h>
12 #include "rsxx_priv.h"
15 struct list_head list
;
17 unsigned int laddr
; /* Logical address */
24 unsigned int pg_off
; /* Page Offset */
29 /* This timeout is used to detect a stalled DMA channel */
30 #define DMA_ACTIVITY_TIMEOUT msecs_to_jiffies(10000)
40 enum rsxx_dma_status
{
50 u8 sub_page
; /* Bit[0:2]: 512byte offset */
51 /* Bit[4:6]: 512byte count */
57 HW_CMD_BLK_DISCARD
= 0x70,
58 HW_CMD_BLK_WRITE
= 0x80,
59 HW_CMD_BLK_READ
= 0xC0,
60 HW_CMD_BLK_RECON_READ
= 0xE0,
65 HW_STATUS_HARD_ERR
= 0x02,
66 HW_STATUS_SOFT_ERR
= 0x04,
67 HW_STATUS_FAULT
= 0x08,
70 static struct kmem_cache
*rsxx_dma_pool
;
77 #define DMA_TRACKER_LIST_SIZE8 (sizeof(struct dma_tracker_list) + \
78 (sizeof(struct dma_tracker) * RSXX_MAX_OUTSTANDING_CMDS))
80 struct dma_tracker_list
{
83 struct dma_tracker list
[0];
87 /*----------------- Misc Utility Functions -------------------*/
88 static unsigned int rsxx_addr8_to_laddr(u64 addr8
, struct rsxx_cardinfo
*card
)
90 unsigned long long tgt_addr8
;
92 tgt_addr8
= ((addr8
>> card
->_stripe
.upper_shift
) &
93 card
->_stripe
.upper_mask
) |
94 ((addr8
) & card
->_stripe
.lower_mask
);
95 do_div(tgt_addr8
, RSXX_HW_BLK_SIZE
);
99 static unsigned int rsxx_get_dma_tgt(struct rsxx_cardinfo
*card
, u64 addr8
)
103 tgt
= (addr8
>> card
->_stripe
.target_shift
) & card
->_stripe
.target_mask
;
108 void rsxx_dma_queue_reset(struct rsxx_cardinfo
*card
)
110 /* Reset all DMA Command/Status Queues */
111 iowrite32(DMA_QUEUE_RESET
, card
->regmap
+ RESET
);
114 static unsigned int get_dma_size(struct rsxx_dma
*dma
)
116 if (dma
->sub_page
.cnt
)
117 return dma
->sub_page
.cnt
<< 9;
119 return RSXX_HW_BLK_SIZE
;
123 /*----------------- DMA Tracker -------------------*/
124 static void set_tracker_dma(struct dma_tracker_list
*trackers
,
126 struct rsxx_dma
*dma
)
128 trackers
->list
[tag
].dma
= dma
;
131 static struct rsxx_dma
*get_tracker_dma(struct dma_tracker_list
*trackers
,
134 return trackers
->list
[tag
].dma
;
137 static int pop_tracker(struct dma_tracker_list
*trackers
)
141 spin_lock(&trackers
->lock
);
142 tag
= trackers
->head
;
144 trackers
->head
= trackers
->list
[tag
].next_tag
;
145 trackers
->list
[tag
].next_tag
= -1;
147 spin_unlock(&trackers
->lock
);
152 static void push_tracker(struct dma_tracker_list
*trackers
, int tag
)
154 spin_lock(&trackers
->lock
);
155 trackers
->list
[tag
].next_tag
= trackers
->head
;
156 trackers
->head
= tag
;
157 trackers
->list
[tag
].dma
= NULL
;
158 spin_unlock(&trackers
->lock
);
162 /*----------------- Interrupt Coalescing -------------*/
164 * Interrupt Coalescing Register Format:
165 * Interrupt Timer (64ns units) [15:0]
166 * Interrupt Count [24:16]
169 #define INTR_COAL_LATENCY_MASK (0x0000ffff)
171 #define INTR_COAL_COUNT_SHIFT 16
172 #define INTR_COAL_COUNT_BITS 9
173 #define INTR_COAL_COUNT_MASK (((1 << INTR_COAL_COUNT_BITS) - 1) << \
174 INTR_COAL_COUNT_SHIFT)
175 #define INTR_COAL_LATENCY_UNITS_NS 64
178 static u32
dma_intr_coal_val(u32 mode
, u32 count
, u32 latency
)
180 u32 latency_units
= latency
/ INTR_COAL_LATENCY_UNITS_NS
;
182 if (mode
== RSXX_INTR_COAL_DISABLED
)
185 return ((count
<< INTR_COAL_COUNT_SHIFT
) & INTR_COAL_COUNT_MASK
) |
186 (latency_units
& INTR_COAL_LATENCY_MASK
);
190 static void dma_intr_coal_auto_tune(struct rsxx_cardinfo
*card
)
196 if (card
->config
.data
.intr_coal
.mode
!= RSXX_INTR_COAL_AUTO_TUNE
||
197 unlikely(card
->eeh_state
))
200 for (i
= 0; i
< card
->n_targets
; i
++)
201 q_depth
+= atomic_read(&card
->ctrl
[i
].stats
.hw_q_depth
);
203 intr_coal
= dma_intr_coal_val(card
->config
.data
.intr_coal
.mode
,
205 card
->config
.data
.intr_coal
.latency
);
206 iowrite32(intr_coal
, card
->regmap
+ INTR_COAL
);
209 /*----------------- RSXX DMA Handling -------------------*/
210 static void rsxx_free_dma(struct rsxx_dma_ctrl
*ctrl
, struct rsxx_dma
*dma
)
212 if (dma
->cmd
!= HW_CMD_BLK_DISCARD
) {
213 if (!dma_mapping_error(&ctrl
->card
->dev
->dev
, dma
->dma_addr
)) {
214 dma_unmap_page(&ctrl
->card
->dev
->dev
, dma
->dma_addr
,
216 dma
->cmd
== HW_CMD_BLK_WRITE
?
222 kmem_cache_free(rsxx_dma_pool
, dma
);
225 static void rsxx_complete_dma(struct rsxx_dma_ctrl
*ctrl
,
226 struct rsxx_dma
*dma
,
229 if (status
& DMA_SW_ERR
)
230 ctrl
->stats
.dma_sw_err
++;
231 if (status
& DMA_HW_FAULT
)
232 ctrl
->stats
.dma_hw_fault
++;
233 if (status
& DMA_CANCELLED
)
234 ctrl
->stats
.dma_cancelled
++;
237 dma
->cb(ctrl
->card
, dma
->cb_data
, status
? 1 : 0);
239 rsxx_free_dma(ctrl
, dma
);
242 int rsxx_cleanup_dma_queue(struct rsxx_dma_ctrl
*ctrl
,
243 struct list_head
*q
, unsigned int done
)
245 struct rsxx_dma
*dma
;
246 struct rsxx_dma
*tmp
;
249 list_for_each_entry_safe(dma
, tmp
, q
, list
) {
250 list_del(&dma
->list
);
251 if (done
& COMPLETE_DMA
)
252 rsxx_complete_dma(ctrl
, dma
, DMA_CANCELLED
);
254 rsxx_free_dma(ctrl
, dma
);
261 static void rsxx_requeue_dma(struct rsxx_dma_ctrl
*ctrl
,
262 struct rsxx_dma
*dma
)
265 * Requeued DMAs go to the front of the queue so they are issued
268 spin_lock_bh(&ctrl
->queue_lock
);
269 ctrl
->stats
.sw_q_depth
++;
270 list_add(&dma
->list
, &ctrl
->queue
);
271 spin_unlock_bh(&ctrl
->queue_lock
);
274 static void rsxx_handle_dma_error(struct rsxx_dma_ctrl
*ctrl
,
275 struct rsxx_dma
*dma
,
278 unsigned int status
= 0;
281 dev_dbg(CARD_TO_DEV(ctrl
->card
),
282 "Handling DMA error(cmd x%02x, laddr x%08x st:x%02x)\n",
283 dma
->cmd
, dma
->laddr
, hw_st
);
285 if (hw_st
& HW_STATUS_CRC
)
286 ctrl
->stats
.crc_errors
++;
287 if (hw_st
& HW_STATUS_HARD_ERR
)
288 ctrl
->stats
.hard_errors
++;
289 if (hw_st
& HW_STATUS_SOFT_ERR
)
290 ctrl
->stats
.soft_errors
++;
293 case HW_CMD_BLK_READ
:
294 if (hw_st
& (HW_STATUS_CRC
| HW_STATUS_HARD_ERR
)) {
295 if (ctrl
->card
->scrub_hard
) {
296 dma
->cmd
= HW_CMD_BLK_RECON_READ
;
298 ctrl
->stats
.reads_retried
++;
300 status
|= DMA_HW_FAULT
;
301 ctrl
->stats
.reads_failed
++;
303 } else if (hw_st
& HW_STATUS_FAULT
) {
304 status
|= DMA_HW_FAULT
;
305 ctrl
->stats
.reads_failed
++;
309 case HW_CMD_BLK_RECON_READ
:
310 if (hw_st
& (HW_STATUS_CRC
| HW_STATUS_HARD_ERR
)) {
311 /* Data could not be reconstructed. */
312 status
|= DMA_HW_FAULT
;
313 ctrl
->stats
.reads_failed
++;
317 case HW_CMD_BLK_WRITE
:
318 status
|= DMA_HW_FAULT
;
319 ctrl
->stats
.writes_failed
++;
322 case HW_CMD_BLK_DISCARD
:
323 status
|= DMA_HW_FAULT
;
324 ctrl
->stats
.discards_failed
++;
328 dev_err(CARD_TO_DEV(ctrl
->card
),
329 "Unknown command in DMA!(cmd: x%02x "
330 "laddr x%08x st: x%02x\n",
331 dma
->cmd
, dma
->laddr
, hw_st
);
332 status
|= DMA_SW_ERR
;
338 rsxx_requeue_dma(ctrl
, dma
);
340 rsxx_complete_dma(ctrl
, dma
, status
);
343 static void dma_engine_stalled(struct timer_list
*t
)
345 struct rsxx_dma_ctrl
*ctrl
= from_timer(ctrl
, t
, activity_timer
);
348 if (atomic_read(&ctrl
->stats
.hw_q_depth
) == 0 ||
349 unlikely(ctrl
->card
->eeh_state
))
352 if (ctrl
->cmd
.idx
!= ioread32(ctrl
->regmap
+ SW_CMD_IDX
)) {
354 * The dma engine was stalled because the SW_CMD_IDX write
355 * was lost. Issue it again to recover.
357 dev_warn(CARD_TO_DEV(ctrl
->card
),
358 "SW_CMD_IDX write was lost, re-writing...\n");
359 iowrite32(ctrl
->cmd
.idx
, ctrl
->regmap
+ SW_CMD_IDX
);
360 mod_timer(&ctrl
->activity_timer
,
361 jiffies
+ DMA_ACTIVITY_TIMEOUT
);
363 dev_warn(CARD_TO_DEV(ctrl
->card
),
364 "DMA channel %d has stalled, faulting interface.\n",
366 ctrl
->card
->dma_fault
= 1;
368 /* Clean up the DMA queue */
369 spin_lock(&ctrl
->queue_lock
);
370 cnt
= rsxx_cleanup_dma_queue(ctrl
, &ctrl
->queue
, COMPLETE_DMA
);
371 spin_unlock(&ctrl
->queue_lock
);
373 cnt
+= rsxx_dma_cancel(ctrl
);
376 dev_info(CARD_TO_DEV(ctrl
->card
),
377 "Freed %d queued DMAs on channel %d\n",
382 static void rsxx_issue_dmas(struct rsxx_dma_ctrl
*ctrl
)
384 struct rsxx_dma
*dma
;
386 int cmds_pending
= 0;
387 struct hw_cmd
*hw_cmd_buf
;
390 hw_cmd_buf
= ctrl
->cmd
.buf
;
392 if (unlikely(ctrl
->card
->halt
) ||
393 unlikely(ctrl
->card
->eeh_state
))
397 spin_lock_bh(&ctrl
->queue_lock
);
398 if (list_empty(&ctrl
->queue
)) {
399 spin_unlock_bh(&ctrl
->queue_lock
);
402 spin_unlock_bh(&ctrl
->queue_lock
);
404 tag
= pop_tracker(ctrl
->trackers
);
408 spin_lock_bh(&ctrl
->queue_lock
);
409 dma
= list_entry(ctrl
->queue
.next
, struct rsxx_dma
, list
);
410 list_del(&dma
->list
);
411 ctrl
->stats
.sw_q_depth
--;
412 spin_unlock_bh(&ctrl
->queue_lock
);
415 * This will catch any DMAs that slipped in right before the
416 * fault, but was queued after all the other DMAs were
419 if (unlikely(ctrl
->card
->dma_fault
)) {
420 push_tracker(ctrl
->trackers
, tag
);
421 rsxx_complete_dma(ctrl
, dma
, DMA_CANCELLED
);
425 if (dma
->cmd
!= HW_CMD_BLK_DISCARD
) {
426 if (dma
->cmd
== HW_CMD_BLK_WRITE
)
429 dir
= DMA_FROM_DEVICE
;
432 * The function dma_map_page is placed here because we
433 * can only, by design, issue up to 255 commands to the
434 * hardware at one time per DMA channel. So the maximum
435 * amount of mapped memory would be 255 * 4 channels *
436 * 4096 Bytes which is less than 2GB, the limit of a x8
437 * Non-HWWD PCIe slot. This way the dma_map_page
438 * function should never fail because of a lack of
441 dma
->dma_addr
= dma_map_page(&ctrl
->card
->dev
->dev
, dma
->page
,
442 dma
->pg_off
, dma
->sub_page
.cnt
<< 9, dir
);
443 if (dma_mapping_error(&ctrl
->card
->dev
->dev
, dma
->dma_addr
)) {
444 push_tracker(ctrl
->trackers
, tag
);
445 rsxx_complete_dma(ctrl
, dma
, DMA_CANCELLED
);
450 set_tracker_dma(ctrl
->trackers
, tag
, dma
);
451 hw_cmd_buf
[ctrl
->cmd
.idx
].command
= dma
->cmd
;
452 hw_cmd_buf
[ctrl
->cmd
.idx
].tag
= tag
;
453 hw_cmd_buf
[ctrl
->cmd
.idx
]._rsvd
= 0;
454 hw_cmd_buf
[ctrl
->cmd
.idx
].sub_page
=
455 ((dma
->sub_page
.cnt
& 0x7) << 4) |
456 (dma
->sub_page
.off
& 0x7);
458 hw_cmd_buf
[ctrl
->cmd
.idx
].device_addr
=
459 cpu_to_le32(dma
->laddr
);
461 hw_cmd_buf
[ctrl
->cmd
.idx
].host_addr
=
462 cpu_to_le64(dma
->dma_addr
);
464 dev_dbg(CARD_TO_DEV(ctrl
->card
),
465 "Issue DMA%d(laddr %d tag %d) to idx %d\n",
466 ctrl
->id
, dma
->laddr
, tag
, ctrl
->cmd
.idx
);
468 ctrl
->cmd
.idx
= (ctrl
->cmd
.idx
+ 1) & RSXX_CS_IDX_MASK
;
471 if (dma
->cmd
== HW_CMD_BLK_WRITE
)
472 ctrl
->stats
.writes_issued
++;
473 else if (dma
->cmd
== HW_CMD_BLK_DISCARD
)
474 ctrl
->stats
.discards_issued
++;
476 ctrl
->stats
.reads_issued
++;
479 /* Let HW know we've queued commands. */
481 atomic_add(cmds_pending
, &ctrl
->stats
.hw_q_depth
);
482 mod_timer(&ctrl
->activity_timer
,
483 jiffies
+ DMA_ACTIVITY_TIMEOUT
);
485 if (unlikely(ctrl
->card
->eeh_state
)) {
486 del_timer_sync(&ctrl
->activity_timer
);
490 iowrite32(ctrl
->cmd
.idx
, ctrl
->regmap
+ SW_CMD_IDX
);
494 static void rsxx_dma_done(struct rsxx_dma_ctrl
*ctrl
)
496 struct rsxx_dma
*dma
;
501 struct hw_status
*hw_st_buf
;
503 hw_st_buf
= ctrl
->status
.buf
;
505 if (unlikely(ctrl
->card
->halt
) ||
506 unlikely(ctrl
->card
->dma_fault
) ||
507 unlikely(ctrl
->card
->eeh_state
))
510 count
= le16_to_cpu(hw_st_buf
[ctrl
->status
.idx
].count
);
512 while (count
== ctrl
->e_cnt
) {
514 * The read memory-barrier is necessary to keep aggressive
515 * processors/optimizers (such as the PPC Apple G5) from
516 * reordering the following status-buffer tag & status read
517 * *before* the count read on subsequent iterations of the
522 status
= hw_st_buf
[ctrl
->status
.idx
].status
;
523 tag
= hw_st_buf
[ctrl
->status
.idx
].tag
;
525 dma
= get_tracker_dma(ctrl
->trackers
, tag
);
527 spin_lock_irqsave(&ctrl
->card
->irq_lock
, flags
);
528 rsxx_disable_ier(ctrl
->card
, CR_INTR_DMA_ALL
);
529 spin_unlock_irqrestore(&ctrl
->card
->irq_lock
, flags
);
531 dev_err(CARD_TO_DEV(ctrl
->card
),
532 "No tracker for tag %d "
534 tag
, ctrl
->status
.idx
, ctrl
->id
);
538 dev_dbg(CARD_TO_DEV(ctrl
->card
),
540 "(laddr x%x tag %d st: x%x cnt: x%04x) from idx %d.\n",
541 ctrl
->id
, dma
->laddr
, tag
, status
, count
,
544 atomic_dec(&ctrl
->stats
.hw_q_depth
);
546 mod_timer(&ctrl
->activity_timer
,
547 jiffies
+ DMA_ACTIVITY_TIMEOUT
);
550 rsxx_handle_dma_error(ctrl
, dma
, status
);
552 rsxx_complete_dma(ctrl
, dma
, 0);
554 push_tracker(ctrl
->trackers
, tag
);
556 ctrl
->status
.idx
= (ctrl
->status
.idx
+ 1) &
560 count
= le16_to_cpu(hw_st_buf
[ctrl
->status
.idx
].count
);
563 dma_intr_coal_auto_tune(ctrl
->card
);
565 if (atomic_read(&ctrl
->stats
.hw_q_depth
) == 0)
566 del_timer_sync(&ctrl
->activity_timer
);
568 spin_lock_irqsave(&ctrl
->card
->irq_lock
, flags
);
569 rsxx_enable_ier(ctrl
->card
, CR_INTR_DMA(ctrl
->id
));
570 spin_unlock_irqrestore(&ctrl
->card
->irq_lock
, flags
);
572 spin_lock_bh(&ctrl
->queue_lock
);
573 if (ctrl
->stats
.sw_q_depth
)
574 queue_work(ctrl
->issue_wq
, &ctrl
->issue_dma_work
);
575 spin_unlock_bh(&ctrl
->queue_lock
);
578 static void rsxx_schedule_issue(struct work_struct
*work
)
580 struct rsxx_dma_ctrl
*ctrl
;
582 ctrl
= container_of(work
, struct rsxx_dma_ctrl
, issue_dma_work
);
584 mutex_lock(&ctrl
->work_lock
);
585 rsxx_issue_dmas(ctrl
);
586 mutex_unlock(&ctrl
->work_lock
);
589 static void rsxx_schedule_done(struct work_struct
*work
)
591 struct rsxx_dma_ctrl
*ctrl
;
593 ctrl
= container_of(work
, struct rsxx_dma_ctrl
, dma_done_work
);
595 mutex_lock(&ctrl
->work_lock
);
597 mutex_unlock(&ctrl
->work_lock
);
600 static blk_status_t
rsxx_queue_discard(struct rsxx_cardinfo
*card
,
606 struct rsxx_dma
*dma
;
608 dma
= kmem_cache_alloc(rsxx_dma_pool
, GFP_KERNEL
);
610 return BLK_STS_RESOURCE
;
612 dma
->cmd
= HW_CMD_BLK_DISCARD
;
615 dma
->sub_page
.off
= 0;
616 dma
->sub_page
.cnt
= 0;
620 dma
->cb_data
= cb_data
;
622 dev_dbg(CARD_TO_DEV(card
), "Queuing[D] laddr %x\n", dma
->laddr
);
624 list_add_tail(&dma
->list
, q
);
629 static blk_status_t
rsxx_queue_dma(struct rsxx_cardinfo
*card
,
632 unsigned int dma_off
,
633 unsigned int dma_len
,
640 struct rsxx_dma
*dma
;
642 dma
= kmem_cache_alloc(rsxx_dma_pool
, GFP_KERNEL
);
644 return BLK_STS_RESOURCE
;
646 dma
->cmd
= dir
? HW_CMD_BLK_WRITE
: HW_CMD_BLK_READ
;
648 dma
->sub_page
.off
= (dma_off
>> 9);
649 dma
->sub_page
.cnt
= (dma_len
>> 9);
651 dma
->pg_off
= pg_off
;
653 dma
->cb_data
= cb_data
;
655 dev_dbg(CARD_TO_DEV(card
),
656 "Queuing[%c] laddr %x off %d cnt %d page %p pg_off %d\n",
657 dir
? 'W' : 'R', dma
->laddr
, dma
->sub_page
.off
,
658 dma
->sub_page
.cnt
, dma
->page
, dma
->pg_off
);
661 list_add_tail(&dma
->list
, q
);
666 blk_status_t
rsxx_dma_queue_bio(struct rsxx_cardinfo
*card
,
672 struct list_head dma_list
[RSXX_MAX_TARGETS
];
674 struct bvec_iter iter
;
675 unsigned long long addr8
;
679 unsigned int dma_off
;
680 unsigned int dma_len
;
681 int dma_cnt
[RSXX_MAX_TARGETS
];
686 addr8
= bio
->bi_iter
.bi_sector
<< 9; /* sectors are 512 bytes */
687 atomic_set(n_dmas
, 0);
689 for (i
= 0; i
< card
->n_targets
; i
++) {
690 INIT_LIST_HEAD(&dma_list
[i
]);
694 if (bio_op(bio
) == REQ_OP_DISCARD
) {
695 bv_len
= bio
->bi_iter
.bi_size
;
698 tgt
= rsxx_get_dma_tgt(card
, addr8
);
699 laddr
= rsxx_addr8_to_laddr(addr8
, card
);
701 st
= rsxx_queue_discard(card
, &dma_list
[tgt
], laddr
,
708 addr8
+= RSXX_HW_BLK_SIZE
;
709 bv_len
-= RSXX_HW_BLK_SIZE
;
712 bio_for_each_segment(bvec
, bio
, iter
) {
713 bv_len
= bvec
.bv_len
;
714 bv_off
= bvec
.bv_offset
;
717 tgt
= rsxx_get_dma_tgt(card
, addr8
);
718 laddr
= rsxx_addr8_to_laddr(addr8
, card
);
719 dma_off
= addr8
& RSXX_HW_BLK_MASK
;
720 dma_len
= min(bv_len
,
721 RSXX_HW_BLK_SIZE
- dma_off
);
723 st
= rsxx_queue_dma(card
, &dma_list
[tgt
],
727 bv_off
, cb
, cb_data
);
740 for (i
= 0; i
< card
->n_targets
; i
++) {
741 if (!list_empty(&dma_list
[i
])) {
742 spin_lock_bh(&card
->ctrl
[i
].queue_lock
);
743 card
->ctrl
[i
].stats
.sw_q_depth
+= dma_cnt
[i
];
744 list_splice_tail(&dma_list
[i
], &card
->ctrl
[i
].queue
);
745 spin_unlock_bh(&card
->ctrl
[i
].queue_lock
);
747 queue_work(card
->ctrl
[i
].issue_wq
,
748 &card
->ctrl
[i
].issue_dma_work
);
755 for (i
= 0; i
< card
->n_targets
; i
++)
756 rsxx_cleanup_dma_queue(&card
->ctrl
[i
], &dma_list
[i
],
762 /*----------------- DMA Engine Initialization & Setup -------------------*/
763 int rsxx_hw_buffers_init(struct pci_dev
*dev
, struct rsxx_dma_ctrl
*ctrl
)
765 ctrl
->status
.buf
= dma_alloc_coherent(&dev
->dev
, STATUS_BUFFER_SIZE8
,
766 &ctrl
->status
.dma_addr
, GFP_KERNEL
);
767 ctrl
->cmd
.buf
= dma_alloc_coherent(&dev
->dev
, COMMAND_BUFFER_SIZE8
,
768 &ctrl
->cmd
.dma_addr
, GFP_KERNEL
);
769 if (ctrl
->status
.buf
== NULL
|| ctrl
->cmd
.buf
== NULL
)
772 memset(ctrl
->status
.buf
, 0xac, STATUS_BUFFER_SIZE8
);
773 iowrite32(lower_32_bits(ctrl
->status
.dma_addr
),
774 ctrl
->regmap
+ SB_ADD_LO
);
775 iowrite32(upper_32_bits(ctrl
->status
.dma_addr
),
776 ctrl
->regmap
+ SB_ADD_HI
);
778 memset(ctrl
->cmd
.buf
, 0x83, COMMAND_BUFFER_SIZE8
);
779 iowrite32(lower_32_bits(ctrl
->cmd
.dma_addr
), ctrl
->regmap
+ CB_ADD_LO
);
780 iowrite32(upper_32_bits(ctrl
->cmd
.dma_addr
), ctrl
->regmap
+ CB_ADD_HI
);
782 ctrl
->status
.idx
= ioread32(ctrl
->regmap
+ HW_STATUS_CNT
);
783 if (ctrl
->status
.idx
> RSXX_MAX_OUTSTANDING_CMDS
) {
784 dev_crit(&dev
->dev
, "Failed reading status cnt x%x\n",
788 iowrite32(ctrl
->status
.idx
, ctrl
->regmap
+ HW_STATUS_CNT
);
789 iowrite32(ctrl
->status
.idx
, ctrl
->regmap
+ SW_STATUS_CNT
);
791 ctrl
->cmd
.idx
= ioread32(ctrl
->regmap
+ HW_CMD_IDX
);
792 if (ctrl
->cmd
.idx
> RSXX_MAX_OUTSTANDING_CMDS
) {
793 dev_crit(&dev
->dev
, "Failed reading cmd cnt x%x\n",
797 iowrite32(ctrl
->cmd
.idx
, ctrl
->regmap
+ HW_CMD_IDX
);
798 iowrite32(ctrl
->cmd
.idx
, ctrl
->regmap
+ SW_CMD_IDX
);
803 static int rsxx_dma_ctrl_init(struct pci_dev
*dev
,
804 struct rsxx_dma_ctrl
*ctrl
)
809 memset(&ctrl
->stats
, 0, sizeof(ctrl
->stats
));
811 ctrl
->trackers
= vmalloc(DMA_TRACKER_LIST_SIZE8
);
815 ctrl
->trackers
->head
= 0;
816 for (i
= 0; i
< RSXX_MAX_OUTSTANDING_CMDS
; i
++) {
817 ctrl
->trackers
->list
[i
].next_tag
= i
+ 1;
818 ctrl
->trackers
->list
[i
].dma
= NULL
;
820 ctrl
->trackers
->list
[RSXX_MAX_OUTSTANDING_CMDS
-1].next_tag
= -1;
821 spin_lock_init(&ctrl
->trackers
->lock
);
823 spin_lock_init(&ctrl
->queue_lock
);
824 mutex_init(&ctrl
->work_lock
);
825 INIT_LIST_HEAD(&ctrl
->queue
);
827 timer_setup(&ctrl
->activity_timer
, dma_engine_stalled
, 0);
829 ctrl
->issue_wq
= alloc_ordered_workqueue(DRIVER_NAME
"_issue", 0);
833 ctrl
->done_wq
= alloc_ordered_workqueue(DRIVER_NAME
"_done", 0);
837 INIT_WORK(&ctrl
->issue_dma_work
, rsxx_schedule_issue
);
838 INIT_WORK(&ctrl
->dma_done_work
, rsxx_schedule_done
);
840 st
= rsxx_hw_buffers_init(dev
, ctrl
);
847 static int rsxx_dma_stripe_setup(struct rsxx_cardinfo
*card
,
848 unsigned int stripe_size8
)
850 if (!is_power_of_2(stripe_size8
)) {
851 dev_err(CARD_TO_DEV(card
),
852 "stripe_size is NOT a power of 2!\n");
856 card
->_stripe
.lower_mask
= stripe_size8
- 1;
858 card
->_stripe
.upper_mask
= ~(card
->_stripe
.lower_mask
);
859 card
->_stripe
.upper_shift
= ffs(card
->n_targets
) - 1;
861 card
->_stripe
.target_mask
= card
->n_targets
- 1;
862 card
->_stripe
.target_shift
= ffs(stripe_size8
) - 1;
864 dev_dbg(CARD_TO_DEV(card
), "_stripe.lower_mask = x%016llx\n",
865 card
->_stripe
.lower_mask
);
866 dev_dbg(CARD_TO_DEV(card
), "_stripe.upper_shift = x%016llx\n",
867 card
->_stripe
.upper_shift
);
868 dev_dbg(CARD_TO_DEV(card
), "_stripe.upper_mask = x%016llx\n",
869 card
->_stripe
.upper_mask
);
870 dev_dbg(CARD_TO_DEV(card
), "_stripe.target_mask = x%016llx\n",
871 card
->_stripe
.target_mask
);
872 dev_dbg(CARD_TO_DEV(card
), "_stripe.target_shift = x%016llx\n",
873 card
->_stripe
.target_shift
);
878 int rsxx_dma_configure(struct rsxx_cardinfo
*card
)
882 intr_coal
= dma_intr_coal_val(card
->config
.data
.intr_coal
.mode
,
883 card
->config
.data
.intr_coal
.count
,
884 card
->config
.data
.intr_coal
.latency
);
885 iowrite32(intr_coal
, card
->regmap
+ INTR_COAL
);
887 return rsxx_dma_stripe_setup(card
, card
->config
.data
.stripe_size
);
890 int rsxx_dma_setup(struct rsxx_cardinfo
*card
)
896 dev_info(CARD_TO_DEV(card
),
897 "Initializing %d DMA targets\n",
900 /* Regmap is divided up into 4K chunks. One for each DMA channel */
901 for (i
= 0; i
< card
->n_targets
; i
++)
902 card
->ctrl
[i
].regmap
= card
->regmap
+ (i
* 4096);
906 /* Reset the DMA queues */
907 rsxx_dma_queue_reset(card
);
909 /************* Setup DMA Control *************/
910 for (i
= 0; i
< card
->n_targets
; i
++) {
911 st
= rsxx_dma_ctrl_init(card
->dev
, &card
->ctrl
[i
]);
913 goto failed_dma_setup
;
915 card
->ctrl
[i
].card
= card
;
916 card
->ctrl
[i
].id
= i
;
919 card
->scrub_hard
= 1;
921 if (card
->config_valid
)
922 rsxx_dma_configure(card
);
924 /* Enable the interrupts after all setup has completed. */
925 for (i
= 0; i
< card
->n_targets
; i
++) {
926 spin_lock_irqsave(&card
->irq_lock
, flags
);
927 rsxx_enable_ier_and_isr(card
, CR_INTR_DMA(i
));
928 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
934 for (i
= 0; i
< card
->n_targets
; i
++) {
935 struct rsxx_dma_ctrl
*ctrl
= &card
->ctrl
[i
];
937 if (ctrl
->issue_wq
) {
938 destroy_workqueue(ctrl
->issue_wq
);
939 ctrl
->issue_wq
= NULL
;
943 destroy_workqueue(ctrl
->done_wq
);
944 ctrl
->done_wq
= NULL
;
948 vfree(ctrl
->trackers
);
950 if (ctrl
->status
.buf
)
951 dma_free_coherent(&card
->dev
->dev
, STATUS_BUFFER_SIZE8
,
953 ctrl
->status
.dma_addr
);
955 dma_free_coherent(&card
->dev
->dev
, COMMAND_BUFFER_SIZE8
,
956 ctrl
->cmd
.buf
, ctrl
->cmd
.dma_addr
);
962 int rsxx_dma_cancel(struct rsxx_dma_ctrl
*ctrl
)
964 struct rsxx_dma
*dma
;
968 /* Clean up issued DMAs */
969 for (i
= 0; i
< RSXX_MAX_OUTSTANDING_CMDS
; i
++) {
970 dma
= get_tracker_dma(ctrl
->trackers
, i
);
972 atomic_dec(&ctrl
->stats
.hw_q_depth
);
973 rsxx_complete_dma(ctrl
, dma
, DMA_CANCELLED
);
974 push_tracker(ctrl
->trackers
, i
);
982 void rsxx_dma_destroy(struct rsxx_cardinfo
*card
)
984 struct rsxx_dma_ctrl
*ctrl
;
987 for (i
= 0; i
< card
->n_targets
; i
++) {
988 ctrl
= &card
->ctrl
[i
];
990 if (ctrl
->issue_wq
) {
991 destroy_workqueue(ctrl
->issue_wq
);
992 ctrl
->issue_wq
= NULL
;
996 destroy_workqueue(ctrl
->done_wq
);
997 ctrl
->done_wq
= NULL
;
1000 if (timer_pending(&ctrl
->activity_timer
))
1001 del_timer_sync(&ctrl
->activity_timer
);
1003 /* Clean up the DMA queue */
1004 spin_lock_bh(&ctrl
->queue_lock
);
1005 rsxx_cleanup_dma_queue(ctrl
, &ctrl
->queue
, COMPLETE_DMA
);
1006 spin_unlock_bh(&ctrl
->queue_lock
);
1008 rsxx_dma_cancel(ctrl
);
1010 vfree(ctrl
->trackers
);
1012 dma_free_coherent(&card
->dev
->dev
, STATUS_BUFFER_SIZE8
,
1013 ctrl
->status
.buf
, ctrl
->status
.dma_addr
);
1014 dma_free_coherent(&card
->dev
->dev
, COMMAND_BUFFER_SIZE8
,
1015 ctrl
->cmd
.buf
, ctrl
->cmd
.dma_addr
);
1019 int rsxx_eeh_save_issued_dmas(struct rsxx_cardinfo
*card
)
1024 struct rsxx_dma
*dma
;
1025 struct list_head
*issued_dmas
;
1027 issued_dmas
= kcalloc(card
->n_targets
, sizeof(*issued_dmas
),
1032 for (i
= 0; i
< card
->n_targets
; i
++) {
1033 INIT_LIST_HEAD(&issued_dmas
[i
]);
1035 for (j
= 0; j
< RSXX_MAX_OUTSTANDING_CMDS
; j
++) {
1036 dma
= get_tracker_dma(card
->ctrl
[i
].trackers
, j
);
1040 if (dma
->cmd
== HW_CMD_BLK_WRITE
)
1041 card
->ctrl
[i
].stats
.writes_issued
--;
1042 else if (dma
->cmd
== HW_CMD_BLK_DISCARD
)
1043 card
->ctrl
[i
].stats
.discards_issued
--;
1045 card
->ctrl
[i
].stats
.reads_issued
--;
1047 if (dma
->cmd
!= HW_CMD_BLK_DISCARD
) {
1048 dma_unmap_page(&card
->dev
->dev
, dma
->dma_addr
,
1050 dma
->cmd
== HW_CMD_BLK_WRITE
?
1055 list_add_tail(&dma
->list
, &issued_dmas
[i
]);
1056 push_tracker(card
->ctrl
[i
].trackers
, j
);
1060 spin_lock_bh(&card
->ctrl
[i
].queue_lock
);
1061 list_splice(&issued_dmas
[i
], &card
->ctrl
[i
].queue
);
1063 atomic_sub(cnt
, &card
->ctrl
[i
].stats
.hw_q_depth
);
1064 card
->ctrl
[i
].stats
.sw_q_depth
+= cnt
;
1065 card
->ctrl
[i
].e_cnt
= 0;
1066 spin_unlock_bh(&card
->ctrl
[i
].queue_lock
);
1074 int rsxx_dma_init(void)
1076 rsxx_dma_pool
= KMEM_CACHE(rsxx_dma
, SLAB_HWCACHE_ALIGN
);
1084 void rsxx_dma_cleanup(void)
1086 kmem_cache_destroy(rsxx_dma_pool
);