1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright(c) 2015 - 2018 Intel Corporation.
6 #include <linux/spinlock.h>
7 #include <linux/seqlock.h>
8 #include <linux/netdevice.h>
9 #include <linux/moduleparam.h>
10 #include <linux/bitops.h>
11 #include <linux/timer.h>
12 #include <linux/vmalloc.h>
13 #include <linux/highmem.h>
22 /* must be a power of 2 >= 64 <= 32768 */
23 #define SDMA_DESCQ_CNT 2048
24 #define SDMA_DESC_INTR 64
25 #define INVALID_TAIL 0xffff
26 #define SDMA_PAD max_t(size_t, MAX_16B_PADDING, sizeof(u32))
28 static uint sdma_descq_cnt
= SDMA_DESCQ_CNT
;
29 module_param(sdma_descq_cnt
, uint
, S_IRUGO
);
30 MODULE_PARM_DESC(sdma_descq_cnt
, "Number of SDMA descq entries");
32 static uint sdma_idle_cnt
= 250;
33 module_param(sdma_idle_cnt
, uint
, S_IRUGO
);
34 MODULE_PARM_DESC(sdma_idle_cnt
, "sdma interrupt idle delay (ns,default 250)");
37 module_param_named(num_sdma
, mod_num_sdma
, uint
, S_IRUGO
);
38 MODULE_PARM_DESC(num_sdma
, "Set max number SDMA engines to use");
40 static uint sdma_desct_intr
= SDMA_DESC_INTR
;
41 module_param_named(desct_intr
, sdma_desct_intr
, uint
, S_IRUGO
| S_IWUSR
);
42 MODULE_PARM_DESC(desct_intr
, "Number of SDMA descriptor before interrupt");
44 #define SDMA_WAIT_BATCH_SIZE 20
45 /* max wait time for a SDMA engine to indicate it has halted */
46 #define SDMA_ERR_HALT_TIMEOUT 10 /* ms */
47 /* all SDMA engine errors that cause a halt */
49 #define SD(name) SEND_DMA_##name
50 #define ALL_SDMA_ENG_HALT_ERRS \
51 (SD(ENG_ERR_STATUS_SDMA_WRONG_DW_ERR_SMASK) \
52 | SD(ENG_ERR_STATUS_SDMA_GEN_MISMATCH_ERR_SMASK) \
53 | SD(ENG_ERR_STATUS_SDMA_TOO_LONG_ERR_SMASK) \
54 | SD(ENG_ERR_STATUS_SDMA_TAIL_OUT_OF_BOUNDS_ERR_SMASK) \
55 | SD(ENG_ERR_STATUS_SDMA_FIRST_DESC_ERR_SMASK) \
56 | SD(ENG_ERR_STATUS_SDMA_MEM_READ_ERR_SMASK) \
57 | SD(ENG_ERR_STATUS_SDMA_HALT_ERR_SMASK) \
58 | SD(ENG_ERR_STATUS_SDMA_LENGTH_MISMATCH_ERR_SMASK) \
59 | SD(ENG_ERR_STATUS_SDMA_PACKET_DESC_OVERFLOW_ERR_SMASK) \
60 | SD(ENG_ERR_STATUS_SDMA_HEADER_SELECT_ERR_SMASK) \
61 | SD(ENG_ERR_STATUS_SDMA_HEADER_ADDRESS_ERR_SMASK) \
62 | SD(ENG_ERR_STATUS_SDMA_HEADER_LENGTH_ERR_SMASK) \
63 | SD(ENG_ERR_STATUS_SDMA_TIMEOUT_ERR_SMASK) \
64 | SD(ENG_ERR_STATUS_SDMA_DESC_TABLE_UNC_ERR_SMASK) \
65 | SD(ENG_ERR_STATUS_SDMA_ASSEMBLY_UNC_ERR_SMASK) \
66 | SD(ENG_ERR_STATUS_SDMA_PACKET_TRACKING_UNC_ERR_SMASK) \
67 | SD(ENG_ERR_STATUS_SDMA_HEADER_STORAGE_UNC_ERR_SMASK) \
68 | SD(ENG_ERR_STATUS_SDMA_HEADER_REQUEST_FIFO_UNC_ERR_SMASK))
70 /* sdma_sendctrl operations */
71 #define SDMA_SENDCTRL_OP_ENABLE BIT(0)
72 #define SDMA_SENDCTRL_OP_INTENABLE BIT(1)
73 #define SDMA_SENDCTRL_OP_HALT BIT(2)
74 #define SDMA_SENDCTRL_OP_CLEANUP BIT(3)
76 /* handle long defines */
77 #define SDMA_EGRESS_PACKET_OCCUPANCY_SMASK \
78 SEND_EGRESS_SEND_DMA_STATUS_SDMA_EGRESS_PACKET_OCCUPANCY_SMASK
79 #define SDMA_EGRESS_PACKET_OCCUPANCY_SHIFT \
80 SEND_EGRESS_SEND_DMA_STATUS_SDMA_EGRESS_PACKET_OCCUPANCY_SHIFT
82 static const char * const sdma_state_names
[] = {
83 [sdma_state_s00_hw_down
] = "s00_HwDown",
84 [sdma_state_s10_hw_start_up_halt_wait
] = "s10_HwStartUpHaltWait",
85 [sdma_state_s15_hw_start_up_clean_wait
] = "s15_HwStartUpCleanWait",
86 [sdma_state_s20_idle
] = "s20_Idle",
87 [sdma_state_s30_sw_clean_up_wait
] = "s30_SwCleanUpWait",
88 [sdma_state_s40_hw_clean_up_wait
] = "s40_HwCleanUpWait",
89 [sdma_state_s50_hw_halt_wait
] = "s50_HwHaltWait",
90 [sdma_state_s60_idle_halt_wait
] = "s60_IdleHaltWait",
91 [sdma_state_s80_hw_freeze
] = "s80_HwFreeze",
92 [sdma_state_s82_freeze_sw_clean
] = "s82_FreezeSwClean",
93 [sdma_state_s99_running
] = "s99_Running",
96 #ifdef CONFIG_SDMA_VERBOSITY
97 static const char * const sdma_event_names
[] = {
98 [sdma_event_e00_go_hw_down
] = "e00_GoHwDown",
99 [sdma_event_e10_go_hw_start
] = "e10_GoHwStart",
100 [sdma_event_e15_hw_halt_done
] = "e15_HwHaltDone",
101 [sdma_event_e25_hw_clean_up_done
] = "e25_HwCleanUpDone",
102 [sdma_event_e30_go_running
] = "e30_GoRunning",
103 [sdma_event_e40_sw_cleaned
] = "e40_SwCleaned",
104 [sdma_event_e50_hw_cleaned
] = "e50_HwCleaned",
105 [sdma_event_e60_hw_halted
] = "e60_HwHalted",
106 [sdma_event_e70_go_idle
] = "e70_GoIdle",
107 [sdma_event_e80_hw_freeze
] = "e80_HwFreeze",
108 [sdma_event_e81_hw_frozen
] = "e81_HwFrozen",
109 [sdma_event_e82_hw_unfreeze
] = "e82_HwUnfreeze",
110 [sdma_event_e85_link_down
] = "e85_LinkDown",
111 [sdma_event_e90_sw_halted
] = "e90_SwHalted",
115 static const struct sdma_set_state_action sdma_action_table
[] = {
116 [sdma_state_s00_hw_down
] = {
117 .go_s99_running_tofalse
= 1,
123 [sdma_state_s10_hw_start_up_halt_wait
] = {
129 [sdma_state_s15_hw_start_up_clean_wait
] = {
135 [sdma_state_s20_idle
] = {
141 [sdma_state_s30_sw_clean_up_wait
] = {
147 [sdma_state_s40_hw_clean_up_wait
] = {
153 [sdma_state_s50_hw_halt_wait
] = {
159 [sdma_state_s60_idle_halt_wait
] = {
160 .go_s99_running_tofalse
= 1,
166 [sdma_state_s80_hw_freeze
] = {
172 [sdma_state_s82_freeze_sw_clean
] = {
178 [sdma_state_s99_running
] = {
183 .go_s99_running_totrue
= 1,
187 #define SDMA_TAIL_UPDATE_THRESH 0x1F
189 /* declare all statics here rather than keep sorting */
190 static void sdma_complete(struct kref
*);
191 static void sdma_finalput(struct sdma_state
*);
192 static void sdma_get(struct sdma_state
*);
193 static void sdma_hw_clean_up_task(struct tasklet_struct
*);
194 static void sdma_put(struct sdma_state
*);
195 static void sdma_set_state(struct sdma_engine
*, enum sdma_states
);
196 static void sdma_start_hw_clean_up(struct sdma_engine
*);
197 static void sdma_sw_clean_up_task(struct tasklet_struct
*);
198 static void sdma_sendctrl(struct sdma_engine
*, unsigned);
199 static void init_sdma_regs(struct sdma_engine
*, u32
, uint
);
200 static void sdma_process_event(
201 struct sdma_engine
*sde
,
202 enum sdma_events event
);
203 static void __sdma_process_event(
204 struct sdma_engine
*sde
,
205 enum sdma_events event
);
206 static void dump_sdma_state(struct sdma_engine
*sde
);
207 static void sdma_make_progress(struct sdma_engine
*sde
, u64 status
);
208 static void sdma_desc_avail(struct sdma_engine
*sde
, uint avail
);
209 static void sdma_flush_descq(struct sdma_engine
*sde
);
212 * sdma_state_name() - return state string from enum
215 static const char *sdma_state_name(enum sdma_states state
)
217 return sdma_state_names
[state
];
220 static void sdma_get(struct sdma_state
*ss
)
225 static void sdma_complete(struct kref
*kref
)
227 struct sdma_state
*ss
=
228 container_of(kref
, struct sdma_state
, kref
);
233 static void sdma_put(struct sdma_state
*ss
)
235 kref_put(&ss
->kref
, sdma_complete
);
238 static void sdma_finalput(struct sdma_state
*ss
)
241 wait_for_completion(&ss
->comp
);
244 static inline void write_sde_csr(
245 struct sdma_engine
*sde
,
249 write_kctxt_csr(sde
->dd
, sde
->this_idx
, offset0
, value
);
252 static inline u64
read_sde_csr(
253 struct sdma_engine
*sde
,
256 return read_kctxt_csr(sde
->dd
, sde
->this_idx
, offset0
);
260 * sdma_wait_for_packet_egress() - wait for the VL FIFO occupancy for
261 * sdma engine 'sde' to drop to 0.
263 static void sdma_wait_for_packet_egress(struct sdma_engine
*sde
,
266 u64 off
= 8 * sde
->this_idx
;
267 struct hfi1_devdata
*dd
= sde
->dd
;
274 reg
= read_csr(dd
, off
+ SEND_EGRESS_SEND_DMA_STATUS
);
276 reg
&= SDMA_EGRESS_PACKET_OCCUPANCY_SMASK
;
277 reg
>>= SDMA_EGRESS_PACKET_OCCUPANCY_SHIFT
;
280 /* counter is reest if accupancy count changes */
284 /* timed out - bounce the link */
285 dd_dev_err(dd
, "%s: engine %u timeout waiting for packets to egress, remaining count %u, bouncing link\n",
286 __func__
, sde
->this_idx
, (u32
)reg
);
287 queue_work(dd
->pport
->link_wq
,
288 &dd
->pport
->link_bounce_work
);
296 * sdma_wait() - wait for packet egress to complete for all SDMA engines,
297 * and pause for credit return.
299 void sdma_wait(struct hfi1_devdata
*dd
)
303 for (i
= 0; i
< dd
->num_sdma
; i
++) {
304 struct sdma_engine
*sde
= &dd
->per_sdma
[i
];
306 sdma_wait_for_packet_egress(sde
, 0);
310 static inline void sdma_set_desc_cnt(struct sdma_engine
*sde
, unsigned cnt
)
314 if (!(sde
->dd
->flags
& HFI1_HAS_SDMA_TIMEOUT
))
317 reg
&= SD(DESC_CNT_CNT_MASK
);
318 reg
<<= SD(DESC_CNT_CNT_SHIFT
);
319 write_sde_csr(sde
, SD(DESC_CNT
), reg
);
322 static inline void complete_tx(struct sdma_engine
*sde
,
323 struct sdma_txreq
*tx
,
326 /* protect against complete modifying */
327 struct iowait
*wait
= tx
->wait
;
328 callback_t complete
= tx
->complete
;
330 #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
331 trace_hfi1_sdma_out_sn(sde
, tx
->sn
);
332 if (WARN_ON_ONCE(sde
->head_sn
!= tx
->sn
))
333 dd_dev_err(sde
->dd
, "expected %llu got %llu\n",
334 sde
->head_sn
, tx
->sn
);
337 __sdma_txclean(sde
->dd
, tx
);
339 (*complete
)(tx
, res
);
340 if (iowait_sdma_dec(wait
))
341 iowait_drain_wakeup(wait
);
345 * Complete all the sdma requests with a SDMA_TXREQ_S_ABORTED status
347 * Depending on timing there can be txreqs in two places:
348 * - in the descq ring
349 * - in the flush list
351 * To avoid ordering issues the descq ring needs to be flushed
352 * first followed by the flush list.
354 * This routine is called from two places
355 * - From a work queue item
356 * - Directly from the state machine just before setting the
359 * Must be called with head_lock held
362 static void sdma_flush(struct sdma_engine
*sde
)
364 struct sdma_txreq
*txp
, *txp_next
;
365 LIST_HEAD(flushlist
);
369 /* flush from head to tail */
370 sdma_flush_descq(sde
);
371 spin_lock_irqsave(&sde
->flushlist_lock
, flags
);
372 /* copy flush list */
373 list_splice_init(&sde
->flushlist
, &flushlist
);
374 spin_unlock_irqrestore(&sde
->flushlist_lock
, flags
);
375 /* flush from flush list */
376 list_for_each_entry_safe(txp
, txp_next
, &flushlist
, list
)
377 complete_tx(sde
, txp
, SDMA_TXREQ_S_ABORTED
);
378 /* wakeup QPs orphaned on the dmawait list */
380 struct iowait
*w
, *nw
;
382 seq
= read_seqbegin(&sde
->waitlock
);
383 if (!list_empty(&sde
->dmawait
)) {
384 write_seqlock(&sde
->waitlock
);
385 list_for_each_entry_safe(w
, nw
, &sde
->dmawait
, list
) {
387 w
->wakeup(w
, SDMA_AVAIL_REASON
);
388 list_del_init(&w
->list
);
391 write_sequnlock(&sde
->waitlock
);
393 } while (read_seqretry(&sde
->waitlock
, seq
));
397 * Fields a work request for flushing the descq ring
400 * If the engine has been brought to running during
401 * the scheduling delay, the flush is ignored, assuming
402 * that the process of bringing the engine to running
403 * would have done this flush prior to going to running.
406 static void sdma_field_flush(struct work_struct
*work
)
409 struct sdma_engine
*sde
=
410 container_of(work
, struct sdma_engine
, flush_worker
);
412 write_seqlock_irqsave(&sde
->head_lock
, flags
);
413 if (!__sdma_running(sde
))
415 write_sequnlock_irqrestore(&sde
->head_lock
, flags
);
418 static void sdma_err_halt_wait(struct work_struct
*work
)
420 struct sdma_engine
*sde
= container_of(work
, struct sdma_engine
,
423 unsigned long timeout
;
425 timeout
= jiffies
+ msecs_to_jiffies(SDMA_ERR_HALT_TIMEOUT
);
427 statuscsr
= read_sde_csr(sde
, SD(STATUS
));
428 statuscsr
&= SD(STATUS_ENG_HALTED_SMASK
);
431 if (time_after(jiffies
, timeout
)) {
433 "SDMA engine %d - timeout waiting for engine to halt\n",
436 * Continue anyway. This could happen if there was
437 * an uncorrectable error in the wrong spot.
441 usleep_range(80, 120);
444 sdma_process_event(sde
, sdma_event_e15_hw_halt_done
);
447 static void sdma_err_progress_check_schedule(struct sdma_engine
*sde
)
449 if (!is_bx(sde
->dd
) && HFI1_CAP_IS_KSET(SDMA_AHG
)) {
451 struct hfi1_devdata
*dd
= sde
->dd
;
453 for (index
= 0; index
< dd
->num_sdma
; index
++) {
454 struct sdma_engine
*curr_sdma
= &dd
->per_sdma
[index
];
456 if (curr_sdma
!= sde
)
457 curr_sdma
->progress_check_head
=
458 curr_sdma
->descq_head
;
461 "SDMA engine %d - check scheduled\n",
463 mod_timer(&sde
->err_progress_check_timer
, jiffies
+ 10);
467 static void sdma_err_progress_check(struct timer_list
*t
)
470 struct sdma_engine
*sde
= from_timer(sde
, t
, err_progress_check_timer
);
472 dd_dev_err(sde
->dd
, "SDE progress check event\n");
473 for (index
= 0; index
< sde
->dd
->num_sdma
; index
++) {
474 struct sdma_engine
*curr_sde
= &sde
->dd
->per_sdma
[index
];
477 /* check progress on each engine except the current one */
481 * We must lock interrupts when acquiring sde->lock,
482 * to avoid a deadlock if interrupt triggers and spins on
483 * the same lock on same CPU
485 spin_lock_irqsave(&curr_sde
->tail_lock
, flags
);
486 write_seqlock(&curr_sde
->head_lock
);
488 /* skip non-running queues */
489 if (curr_sde
->state
.current_state
!= sdma_state_s99_running
) {
490 write_sequnlock(&curr_sde
->head_lock
);
491 spin_unlock_irqrestore(&curr_sde
->tail_lock
, flags
);
495 if ((curr_sde
->descq_head
!= curr_sde
->descq_tail
) &&
496 (curr_sde
->descq_head
==
497 curr_sde
->progress_check_head
))
498 __sdma_process_event(curr_sde
,
499 sdma_event_e90_sw_halted
);
500 write_sequnlock(&curr_sde
->head_lock
);
501 spin_unlock_irqrestore(&curr_sde
->tail_lock
, flags
);
503 schedule_work(&sde
->err_halt_worker
);
506 static void sdma_hw_clean_up_task(struct tasklet_struct
*t
)
508 struct sdma_engine
*sde
= from_tasklet(sde
, t
,
509 sdma_hw_clean_up_task
);
513 #ifdef CONFIG_SDMA_VERBOSITY
514 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) %s:%d %s()\n",
515 sde
->this_idx
, slashstrip(__FILE__
), __LINE__
,
518 statuscsr
= read_sde_csr(sde
, SD(STATUS
));
519 statuscsr
&= SD(STATUS_ENG_CLEANED_UP_SMASK
);
525 sdma_process_event(sde
, sdma_event_e25_hw_clean_up_done
);
528 static inline struct sdma_txreq
*get_txhead(struct sdma_engine
*sde
)
530 return sde
->tx_ring
[sde
->tx_head
& sde
->sdma_mask
];
534 * flush ring for recovery
536 static void sdma_flush_descq(struct sdma_engine
*sde
)
540 struct sdma_txreq
*txp
= get_txhead(sde
);
542 /* The reason for some of the complexity of this code is that
543 * not all descriptors have corresponding txps. So, we have to
544 * be able to skip over descs until we wander into the range of
545 * the next txp on the list.
547 head
= sde
->descq_head
& sde
->sdma_mask
;
548 tail
= sde
->descq_tail
& sde
->sdma_mask
;
549 while (head
!= tail
) {
550 /* advance head, wrap if needed */
551 head
= ++sde
->descq_head
& sde
->sdma_mask
;
552 /* if now past this txp's descs, do the callback */
553 if (txp
&& txp
->next_descq_idx
== head
) {
554 /* remove from list */
555 sde
->tx_ring
[sde
->tx_head
++ & sde
->sdma_mask
] = NULL
;
556 complete_tx(sde
, txp
, SDMA_TXREQ_S_ABORTED
);
557 trace_hfi1_sdma_progress(sde
, head
, tail
, txp
);
558 txp
= get_txhead(sde
);
563 sdma_desc_avail(sde
, sdma_descq_freecnt(sde
));
566 static void sdma_sw_clean_up_task(struct tasklet_struct
*t
)
568 struct sdma_engine
*sde
= from_tasklet(sde
, t
, sdma_sw_clean_up_task
);
571 spin_lock_irqsave(&sde
->tail_lock
, flags
);
572 write_seqlock(&sde
->head_lock
);
575 * At this point, the following should always be true:
576 * - We are halted, so no more descriptors are getting retired.
577 * - We are not running, so no one is submitting new work.
578 * - Only we can send the e40_sw_cleaned, so we can't start
579 * running again until we say so. So, the active list and
580 * descq are ours to play with.
584 * In the error clean up sequence, software clean must be called
585 * before the hardware clean so we can use the hardware head in
586 * the progress routine. A hardware clean or SPC unfreeze will
587 * reset the hardware head.
589 * Process all retired requests. The progress routine will use the
590 * latest physical hardware head - we are not running so speed does
593 sdma_make_progress(sde
, 0);
598 * Reset our notion of head and tail.
599 * Note that the HW registers have been reset via an earlier
604 sde
->desc_avail
= sdma_descq_freecnt(sde
);
607 __sdma_process_event(sde
, sdma_event_e40_sw_cleaned
);
609 write_sequnlock(&sde
->head_lock
);
610 spin_unlock_irqrestore(&sde
->tail_lock
, flags
);
613 static void sdma_sw_tear_down(struct sdma_engine
*sde
)
615 struct sdma_state
*ss
= &sde
->state
;
617 /* Releasing this reference means the state machine has stopped. */
620 /* stop waiting for all unfreeze events to complete */
621 atomic_set(&sde
->dd
->sdma_unfreeze_count
, -1);
622 wake_up_interruptible(&sde
->dd
->sdma_unfreeze_wq
);
625 static void sdma_start_hw_clean_up(struct sdma_engine
*sde
)
627 tasklet_hi_schedule(&sde
->sdma_hw_clean_up_task
);
630 static void sdma_set_state(struct sdma_engine
*sde
,
631 enum sdma_states next_state
)
633 struct sdma_state
*ss
= &sde
->state
;
634 const struct sdma_set_state_action
*action
= sdma_action_table
;
637 trace_hfi1_sdma_state(
639 sdma_state_names
[ss
->current_state
],
640 sdma_state_names
[next_state
]);
642 /* debugging bookkeeping */
643 ss
->previous_state
= ss
->current_state
;
644 ss
->previous_op
= ss
->current_op
;
645 ss
->current_state
= next_state
;
647 if (ss
->previous_state
!= sdma_state_s99_running
&&
648 next_state
== sdma_state_s99_running
)
651 if (action
[next_state
].op_enable
)
652 op
|= SDMA_SENDCTRL_OP_ENABLE
;
654 if (action
[next_state
].op_intenable
)
655 op
|= SDMA_SENDCTRL_OP_INTENABLE
;
657 if (action
[next_state
].op_halt
)
658 op
|= SDMA_SENDCTRL_OP_HALT
;
660 if (action
[next_state
].op_cleanup
)
661 op
|= SDMA_SENDCTRL_OP_CLEANUP
;
663 if (action
[next_state
].go_s99_running_tofalse
)
664 ss
->go_s99_running
= 0;
666 if (action
[next_state
].go_s99_running_totrue
)
667 ss
->go_s99_running
= 1;
670 sdma_sendctrl(sde
, ss
->current_op
);
674 * sdma_get_descq_cnt() - called when device probed
676 * Return a validated descq count.
678 * This is currently only used in the verbs initialization to build the tx
681 * This will probably be deleted in favor of a more scalable approach to
685 u16
sdma_get_descq_cnt(void)
687 u16 count
= sdma_descq_cnt
;
690 return SDMA_DESCQ_CNT
;
691 /* count must be a power of 2 greater than 64 and less than
692 * 32768. Otherwise return default.
694 if (!is_power_of_2(count
))
695 return SDMA_DESCQ_CNT
;
696 if (count
< 64 || count
> 32768)
697 return SDMA_DESCQ_CNT
;
702 * sdma_engine_get_vl() - return vl for a given sdma engine
705 * This function returns the vl mapped to a given engine, or an error if
706 * the mapping can't be found. The mapping fields are protected by RCU.
708 int sdma_engine_get_vl(struct sdma_engine
*sde
)
710 struct hfi1_devdata
*dd
= sde
->dd
;
711 struct sdma_vl_map
*m
;
714 if (sde
->this_idx
>= TXE_NUM_SDMA_ENGINES
)
718 m
= rcu_dereference(dd
->sdma_map
);
723 vl
= m
->engine_to_vl
[sde
->this_idx
];
730 * sdma_select_engine_vl() - select sdma engine
732 * @selector: a spreading factor
736 * This function returns an engine based on the selector and a vl. The
737 * mapping fields are protected by RCU.
739 struct sdma_engine
*sdma_select_engine_vl(
740 struct hfi1_devdata
*dd
,
744 struct sdma_vl_map
*m
;
745 struct sdma_map_elem
*e
;
746 struct sdma_engine
*rval
;
748 /* NOTE This should only happen if SC->VL changed after the initial
749 * checks on the QP/AH
750 * Default will return engine 0 below
758 m
= rcu_dereference(dd
->sdma_map
);
761 return &dd
->per_sdma
[0];
763 e
= m
->map
[vl
& m
->mask
];
764 rval
= e
->sde
[selector
& e
->mask
];
768 rval
= !rval
? &dd
->per_sdma
[0] : rval
;
769 trace_hfi1_sdma_engine_select(dd
, selector
, vl
, rval
->this_idx
);
774 * sdma_select_engine_sc() - select sdma engine
776 * @selector: a spreading factor
780 * This function returns an engine based on the selector and an sc.
782 struct sdma_engine
*sdma_select_engine_sc(
783 struct hfi1_devdata
*dd
,
787 u8 vl
= sc_to_vlt(dd
, sc5
);
789 return sdma_select_engine_vl(dd
, selector
, vl
);
792 struct sdma_rht_map_elem
{
795 struct sdma_engine
*sde
[];
798 struct sdma_rht_node
{
799 unsigned long cpu_id
;
800 struct sdma_rht_map_elem
*map
[HFI1_MAX_VLS_SUPPORTED
];
801 struct rhash_head node
;
804 #define NR_CPUS_HINT 192
806 static const struct rhashtable_params sdma_rht_params
= {
807 .nelem_hint
= NR_CPUS_HINT
,
808 .head_offset
= offsetof(struct sdma_rht_node
, node
),
809 .key_offset
= offsetof(struct sdma_rht_node
, cpu_id
),
810 .key_len
= sizeof_field(struct sdma_rht_node
, cpu_id
),
813 .automatic_shrinking
= true,
817 * sdma_select_user_engine() - select sdma engine based on user setup
819 * @selector: a spreading factor
822 * This function returns an sdma engine for a user sdma request.
823 * User defined sdma engine affinity setting is honored when applicable,
824 * otherwise system default sdma engine mapping is used. To ensure correct
825 * ordering, the mapping from <selector, vl> to sde must remain unchanged.
827 struct sdma_engine
*sdma_select_user_engine(struct hfi1_devdata
*dd
,
830 struct sdma_rht_node
*rht_node
;
831 struct sdma_engine
*sde
= NULL
;
832 unsigned long cpu_id
;
835 * To ensure that always the same sdma engine(s) will be
836 * selected make sure the process is pinned to this CPU only.
838 if (current
->nr_cpus_allowed
!= 1)
842 cpu_id
= smp_processor_id();
843 rht_node
= rhashtable_lookup(dd
->sdma_rht
, &cpu_id
,
846 if (rht_node
&& rht_node
->map
[vl
]) {
847 struct sdma_rht_map_elem
*map
= rht_node
->map
[vl
];
849 sde
= map
->sde
[selector
& map
->mask
];
857 return sdma_select_engine_vl(dd
, selector
, vl
);
860 static void sdma_populate_sde_map(struct sdma_rht_map_elem
*map
)
864 for (i
= 0; i
< roundup_pow_of_two(map
->ctr
? : 1) - map
->ctr
; i
++)
865 map
->sde
[map
->ctr
+ i
] = map
->sde
[i
];
868 static void sdma_cleanup_sde_map(struct sdma_rht_map_elem
*map
,
869 struct sdma_engine
*sde
)
873 /* only need to check the first ctr entries for a match */
874 for (i
= 0; i
< map
->ctr
; i
++) {
875 if (map
->sde
[i
] == sde
) {
876 memmove(&map
->sde
[i
], &map
->sde
[i
+ 1],
877 (map
->ctr
- i
- 1) * sizeof(map
->sde
[0]));
879 pow
= roundup_pow_of_two(map
->ctr
? : 1);
881 sdma_populate_sde_map(map
);
888 * Prevents concurrent reads and writes of the sdma engine cpu_mask
890 static DEFINE_MUTEX(process_to_sde_mutex
);
892 ssize_t
sdma_set_cpu_to_sde_map(struct sdma_engine
*sde
, const char *buf
,
895 struct hfi1_devdata
*dd
= sde
->dd
;
896 cpumask_var_t mask
, new_mask
;
899 struct sdma_rht_node
*rht_node
;
901 vl
= sdma_engine_get_vl(sde
);
902 if (unlikely(vl
< 0 || vl
>= ARRAY_SIZE(rht_node
->map
)))
905 ret
= zalloc_cpumask_var(&mask
, GFP_KERNEL
);
909 ret
= zalloc_cpumask_var(&new_mask
, GFP_KERNEL
);
911 free_cpumask_var(mask
);
914 ret
= cpulist_parse(buf
, mask
);
918 if (!cpumask_subset(mask
, cpu_online_mask
)) {
919 dd_dev_warn(sde
->dd
, "Invalid CPU mask\n");
924 sz
= sizeof(struct sdma_rht_map_elem
) +
925 (TXE_NUM_SDMA_ENGINES
* sizeof(struct sdma_engine
*));
927 mutex_lock(&process_to_sde_mutex
);
929 for_each_cpu(cpu
, mask
) {
930 /* Check if we have this already mapped */
931 if (cpumask_test_cpu(cpu
, &sde
->cpu_mask
)) {
932 cpumask_set_cpu(cpu
, new_mask
);
936 rht_node
= rhashtable_lookup_fast(dd
->sdma_rht
, &cpu
,
939 rht_node
= kzalloc(sizeof(*rht_node
), GFP_KERNEL
);
945 rht_node
->map
[vl
] = kzalloc(sz
, GFP_KERNEL
);
946 if (!rht_node
->map
[vl
]) {
951 rht_node
->cpu_id
= cpu
;
952 rht_node
->map
[vl
]->mask
= 0;
953 rht_node
->map
[vl
]->ctr
= 1;
954 rht_node
->map
[vl
]->sde
[0] = sde
;
956 ret
= rhashtable_insert_fast(dd
->sdma_rht
,
960 kfree(rht_node
->map
[vl
]);
962 dd_dev_err(sde
->dd
, "Failed to set process to sde affinity for cpu %lu\n",
970 /* Add new user mappings */
971 if (!rht_node
->map
[vl
])
972 rht_node
->map
[vl
] = kzalloc(sz
, GFP_KERNEL
);
974 if (!rht_node
->map
[vl
]) {
979 rht_node
->map
[vl
]->ctr
++;
980 ctr
= rht_node
->map
[vl
]->ctr
;
981 rht_node
->map
[vl
]->sde
[ctr
- 1] = sde
;
982 pow
= roundup_pow_of_two(ctr
);
983 rht_node
->map
[vl
]->mask
= pow
- 1;
985 /* Populate the sde map table */
986 sdma_populate_sde_map(rht_node
->map
[vl
]);
988 cpumask_set_cpu(cpu
, new_mask
);
991 /* Clean up old mappings */
992 for_each_cpu(cpu
, cpu_online_mask
) {
993 struct sdma_rht_node
*rht_node
;
995 /* Don't cleanup sdes that are set in the new mask */
996 if (cpumask_test_cpu(cpu
, mask
))
999 rht_node
= rhashtable_lookup_fast(dd
->sdma_rht
, &cpu
,
1005 /* Remove mappings for old sde */
1006 for (i
= 0; i
< HFI1_MAX_VLS_SUPPORTED
; i
++)
1007 if (rht_node
->map
[i
])
1008 sdma_cleanup_sde_map(rht_node
->map
[i
],
1011 /* Free empty hash table entries */
1012 for (i
= 0; i
< HFI1_MAX_VLS_SUPPORTED
; i
++) {
1013 if (!rht_node
->map
[i
])
1016 if (rht_node
->map
[i
]->ctr
) {
1023 ret
= rhashtable_remove_fast(dd
->sdma_rht
,
1028 for (i
= 0; i
< HFI1_MAX_VLS_SUPPORTED
; i
++)
1029 kfree(rht_node
->map
[i
]);
1036 cpumask_copy(&sde
->cpu_mask
, new_mask
);
1038 mutex_unlock(&process_to_sde_mutex
);
1040 free_cpumask_var(mask
);
1041 free_cpumask_var(new_mask
);
1042 return ret
? : strnlen(buf
, PAGE_SIZE
);
1045 ssize_t
sdma_get_cpu_to_sde_map(struct sdma_engine
*sde
, char *buf
)
1047 mutex_lock(&process_to_sde_mutex
);
1048 if (cpumask_empty(&sde
->cpu_mask
))
1049 snprintf(buf
, PAGE_SIZE
, "%s\n", "empty");
1051 cpumap_print_to_pagebuf(true, buf
, &sde
->cpu_mask
);
1052 mutex_unlock(&process_to_sde_mutex
);
1053 return strnlen(buf
, PAGE_SIZE
);
1056 static void sdma_rht_free(void *ptr
, void *arg
)
1058 struct sdma_rht_node
*rht_node
= ptr
;
1061 for (i
= 0; i
< HFI1_MAX_VLS_SUPPORTED
; i
++)
1062 kfree(rht_node
->map
[i
]);
1068 * sdma_seqfile_dump_cpu_list() - debugfs dump the cpu to sdma mappings
1073 * This routine dumps the process to sde mappings per cpu
1075 void sdma_seqfile_dump_cpu_list(struct seq_file
*s
,
1076 struct hfi1_devdata
*dd
,
1077 unsigned long cpuid
)
1079 struct sdma_rht_node
*rht_node
;
1082 rht_node
= rhashtable_lookup_fast(dd
->sdma_rht
, &cpuid
,
1087 seq_printf(s
, "cpu%3lu: ", cpuid
);
1088 for (i
= 0; i
< HFI1_MAX_VLS_SUPPORTED
; i
++) {
1089 if (!rht_node
->map
[i
] || !rht_node
->map
[i
]->ctr
)
1092 seq_printf(s
, " vl%d: [", i
);
1094 for (j
= 0; j
< rht_node
->map
[i
]->ctr
; j
++) {
1095 if (!rht_node
->map
[i
]->sde
[j
])
1101 seq_printf(s
, " sdma%2d",
1102 rht_node
->map
[i
]->sde
[j
]->this_idx
);
1111 * Free the indicated map struct
1113 static void sdma_map_free(struct sdma_vl_map
*m
)
1117 for (i
= 0; m
&& i
< m
->actual_vls
; i
++)
1123 * Handle RCU callback
1125 static void sdma_map_rcu_callback(struct rcu_head
*list
)
1127 struct sdma_vl_map
*m
= container_of(list
, struct sdma_vl_map
, list
);
1133 * sdma_map_init - called when # vls change
1135 * @port: port number
1136 * @num_vls: number of vls
1137 * @vl_engines: per vl engine mapping (optional)
1139 * This routine changes the mapping based on the number of vls.
1141 * vl_engines is used to specify a non-uniform vl/engine loading. NULL
1142 * implies auto computing the loading and giving each VLs a uniform
1143 * distribution of engines per VL.
1145 * The auto algorithm computes the sde_per_vl and the number of extra
1146 * engines. Any extra engines are added from the last VL on down.
1148 * rcu locking is used here to control access to the mapping fields.
1150 * If either the num_vls or num_sdma are non-power of 2, the array sizes
1151 * in the struct sdma_vl_map and the struct sdma_map_elem are rounded
1152 * up to the next highest power of 2 and the first entry is reused
1153 * in a round robin fashion.
1155 * If an error occurs the map change is not done and the mapping is
1159 int sdma_map_init(struct hfi1_devdata
*dd
, u8 port
, u8 num_vls
, u8
*vl_engines
)
1162 int extra
, sde_per_vl
;
1164 u8 lvl_engines
[OPA_MAX_VLS
];
1165 struct sdma_vl_map
*oldmap
, *newmap
;
1167 if (!(dd
->flags
& HFI1_HAS_SEND_DMA
))
1171 /* truncate divide */
1172 sde_per_vl
= dd
->num_sdma
/ num_vls
;
1174 extra
= dd
->num_sdma
% num_vls
;
1175 vl_engines
= lvl_engines
;
1176 /* add extras from last vl down */
1177 for (i
= num_vls
- 1; i
>= 0; i
--, extra
--)
1178 vl_engines
[i
] = sde_per_vl
+ (extra
> 0 ? 1 : 0);
1182 sizeof(struct sdma_vl_map
) +
1183 roundup_pow_of_two(num_vls
) *
1184 sizeof(struct sdma_map_elem
*),
1188 newmap
->actual_vls
= num_vls
;
1189 newmap
->vls
= roundup_pow_of_two(num_vls
);
1190 newmap
->mask
= (1 << ilog2(newmap
->vls
)) - 1;
1191 /* initialize back-map */
1192 for (i
= 0; i
< TXE_NUM_SDMA_ENGINES
; i
++)
1193 newmap
->engine_to_vl
[i
] = -1;
1194 for (i
= 0; i
< newmap
->vls
; i
++) {
1195 /* save for wrap around */
1196 int first_engine
= engine
;
1198 if (i
< newmap
->actual_vls
) {
1199 int sz
= roundup_pow_of_two(vl_engines
[i
]);
1201 /* only allocate once */
1202 newmap
->map
[i
] = kzalloc(
1203 sizeof(struct sdma_map_elem
) +
1204 sz
* sizeof(struct sdma_engine
*),
1206 if (!newmap
->map
[i
])
1208 newmap
->map
[i
]->mask
= (1 << ilog2(sz
)) - 1;
1209 /* assign engines */
1210 for (j
= 0; j
< sz
; j
++) {
1211 newmap
->map
[i
]->sde
[j
] =
1212 &dd
->per_sdma
[engine
];
1213 if (++engine
>= first_engine
+ vl_engines
[i
])
1214 /* wrap back to first engine */
1215 engine
= first_engine
;
1217 /* assign back-map */
1218 for (j
= 0; j
< vl_engines
[i
]; j
++)
1219 newmap
->engine_to_vl
[first_engine
+ j
] = i
;
1221 /* just re-use entry without allocating */
1222 newmap
->map
[i
] = newmap
->map
[i
% num_vls
];
1224 engine
= first_engine
+ vl_engines
[i
];
1226 /* newmap in hand, save old map */
1227 spin_lock_irq(&dd
->sde_map_lock
);
1228 oldmap
= rcu_dereference_protected(dd
->sdma_map
,
1229 lockdep_is_held(&dd
->sde_map_lock
));
1231 /* publish newmap */
1232 rcu_assign_pointer(dd
->sdma_map
, newmap
);
1234 spin_unlock_irq(&dd
->sde_map_lock
);
1235 /* success, free any old map after grace period */
1237 call_rcu(&oldmap
->list
, sdma_map_rcu_callback
);
1240 /* free any partial allocation */
1241 sdma_map_free(newmap
);
1246 * sdma_clean - Clean up allocated memory
1247 * @dd: struct hfi1_devdata
1248 * @num_engines: num sdma engines
1250 * This routine can be called regardless of the success of
1253 void sdma_clean(struct hfi1_devdata
*dd
, size_t num_engines
)
1256 struct sdma_engine
*sde
;
1258 if (dd
->sdma_pad_dma
) {
1259 dma_free_coherent(&dd
->pcidev
->dev
, SDMA_PAD
,
1260 (void *)dd
->sdma_pad_dma
,
1262 dd
->sdma_pad_dma
= NULL
;
1263 dd
->sdma_pad_phys
= 0;
1265 if (dd
->sdma_heads_dma
) {
1266 dma_free_coherent(&dd
->pcidev
->dev
, dd
->sdma_heads_size
,
1267 (void *)dd
->sdma_heads_dma
,
1268 dd
->sdma_heads_phys
);
1269 dd
->sdma_heads_dma
= NULL
;
1270 dd
->sdma_heads_phys
= 0;
1272 for (i
= 0; dd
->per_sdma
&& i
< num_engines
; ++i
) {
1273 sde
= &dd
->per_sdma
[i
];
1275 sde
->head_dma
= NULL
;
1281 sde
->descq_cnt
* sizeof(u64
[2]),
1286 sde
->descq_phys
= 0;
1288 kvfree(sde
->tx_ring
);
1289 sde
->tx_ring
= NULL
;
1291 if (rcu_access_pointer(dd
->sdma_map
)) {
1292 spin_lock_irq(&dd
->sde_map_lock
);
1293 sdma_map_free(rcu_access_pointer(dd
->sdma_map
));
1294 RCU_INIT_POINTER(dd
->sdma_map
, NULL
);
1295 spin_unlock_irq(&dd
->sde_map_lock
);
1298 kfree(dd
->per_sdma
);
1299 dd
->per_sdma
= NULL
;
1302 rhashtable_free_and_destroy(dd
->sdma_rht
, sdma_rht_free
, NULL
);
1303 kfree(dd
->sdma_rht
);
1304 dd
->sdma_rht
= NULL
;
1309 * sdma_init() - called when device probed
1311 * @port: port number (currently only zero)
1313 * Initializes each sde and its csrs.
1314 * Interrupts are not required to be enabled.
1317 * 0 - success, -errno on failure
1319 int sdma_init(struct hfi1_devdata
*dd
, u8 port
)
1322 struct sdma_engine
*sde
;
1323 struct rhashtable
*tmp_sdma_rht
;
1326 struct hfi1_pportdata
*ppd
= dd
->pport
+ port
;
1327 u32 per_sdma_credits
;
1328 uint idle_cnt
= sdma_idle_cnt
;
1329 size_t num_engines
= chip_sdma_engines(dd
);
1332 if (!HFI1_CAP_IS_KSET(SDMA
)) {
1333 HFI1_CAP_CLEAR(SDMA_AHG
);
1337 /* can't exceed chip support */
1338 mod_num_sdma
<= chip_sdma_engines(dd
) &&
1339 /* count must be >= vls */
1340 mod_num_sdma
>= num_vls
)
1341 num_engines
= mod_num_sdma
;
1343 dd_dev_info(dd
, "SDMA mod_num_sdma: %u\n", mod_num_sdma
);
1344 dd_dev_info(dd
, "SDMA chip_sdma_engines: %u\n", chip_sdma_engines(dd
));
1345 dd_dev_info(dd
, "SDMA chip_sdma_mem_size: %u\n",
1346 chip_sdma_mem_size(dd
));
1349 chip_sdma_mem_size(dd
) / (num_engines
* SDMA_BLOCK_SIZE
);
1351 /* set up freeze waitqueue */
1352 init_waitqueue_head(&dd
->sdma_unfreeze_wq
);
1353 atomic_set(&dd
->sdma_unfreeze_count
, 0);
1355 descq_cnt
= sdma_get_descq_cnt();
1356 dd_dev_info(dd
, "SDMA engines %zu descq_cnt %u\n",
1357 num_engines
, descq_cnt
);
1359 /* alloc memory for array of send engines */
1360 dd
->per_sdma
= kcalloc_node(num_engines
, sizeof(*dd
->per_sdma
),
1361 GFP_KERNEL
, dd
->node
);
1365 idle_cnt
= ns_to_cclock(dd
, idle_cnt
);
1368 SDMA_DESC1_HEAD_TO_HOST_FLAG
;
1371 SDMA_DESC1_INT_REQ_FLAG
;
1373 if (!sdma_desct_intr
)
1374 sdma_desct_intr
= SDMA_DESC_INTR
;
1376 /* Allocate memory for SendDMA descriptor FIFOs */
1377 for (this_idx
= 0; this_idx
< num_engines
; ++this_idx
) {
1378 sde
= &dd
->per_sdma
[this_idx
];
1381 sde
->this_idx
= this_idx
;
1382 sde
->descq_cnt
= descq_cnt
;
1383 sde
->desc_avail
= sdma_descq_freecnt(sde
);
1384 sde
->sdma_shift
= ilog2(descq_cnt
);
1385 sde
->sdma_mask
= (1 << sde
->sdma_shift
) - 1;
1387 /* Create a mask specifically for each interrupt source */
1388 sde
->int_mask
= (u64
)1 << (0 * TXE_NUM_SDMA_ENGINES
+
1390 sde
->progress_mask
= (u64
)1 << (1 * TXE_NUM_SDMA_ENGINES
+
1392 sde
->idle_mask
= (u64
)1 << (2 * TXE_NUM_SDMA_ENGINES
+
1394 /* Create a combined mask to cover all 3 interrupt sources */
1395 sde
->imask
= sde
->int_mask
| sde
->progress_mask
|
1398 spin_lock_init(&sde
->tail_lock
);
1399 seqlock_init(&sde
->head_lock
);
1400 spin_lock_init(&sde
->senddmactrl_lock
);
1401 spin_lock_init(&sde
->flushlist_lock
);
1402 seqlock_init(&sde
->waitlock
);
1403 /* insure there is always a zero bit */
1404 sde
->ahg_bits
= 0xfffffffe00000000ULL
;
1406 sdma_set_state(sde
, sdma_state_s00_hw_down
);
1408 /* set up reference counting */
1409 kref_init(&sde
->state
.kref
);
1410 init_completion(&sde
->state
.comp
);
1412 INIT_LIST_HEAD(&sde
->flushlist
);
1413 INIT_LIST_HEAD(&sde
->dmawait
);
1416 get_kctxt_csr_addr(dd
, this_idx
, SD(TAIL
));
1418 tasklet_setup(&sde
->sdma_hw_clean_up_task
,
1419 sdma_hw_clean_up_task
);
1420 tasklet_setup(&sde
->sdma_sw_clean_up_task
,
1421 sdma_sw_clean_up_task
);
1422 INIT_WORK(&sde
->err_halt_worker
, sdma_err_halt_wait
);
1423 INIT_WORK(&sde
->flush_worker
, sdma_field_flush
);
1425 sde
->progress_check_head
= 0;
1427 timer_setup(&sde
->err_progress_check_timer
,
1428 sdma_err_progress_check
, 0);
1430 sde
->descq
= dma_alloc_coherent(&dd
->pcidev
->dev
,
1431 descq_cnt
* sizeof(u64
[2]),
1432 &sde
->descq_phys
, GFP_KERNEL
);
1436 kvzalloc_node(array_size(descq_cnt
,
1437 sizeof(struct sdma_txreq
*)),
1438 GFP_KERNEL
, dd
->node
);
1443 dd
->sdma_heads_size
= L1_CACHE_BYTES
* num_engines
;
1444 /* Allocate memory for DMA of head registers to memory */
1445 dd
->sdma_heads_dma
= dma_alloc_coherent(&dd
->pcidev
->dev
,
1446 dd
->sdma_heads_size
,
1447 &dd
->sdma_heads_phys
,
1449 if (!dd
->sdma_heads_dma
) {
1450 dd_dev_err(dd
, "failed to allocate SendDMA head memory\n");
1454 /* Allocate memory for pad */
1455 dd
->sdma_pad_dma
= dma_alloc_coherent(&dd
->pcidev
->dev
, SDMA_PAD
,
1456 &dd
->sdma_pad_phys
, GFP_KERNEL
);
1457 if (!dd
->sdma_pad_dma
) {
1458 dd_dev_err(dd
, "failed to allocate SendDMA pad memory\n");
1462 /* assign each engine to different cacheline and init registers */
1463 curr_head
= (void *)dd
->sdma_heads_dma
;
1464 for (this_idx
= 0; this_idx
< num_engines
; ++this_idx
) {
1465 unsigned long phys_offset
;
1467 sde
= &dd
->per_sdma
[this_idx
];
1469 sde
->head_dma
= curr_head
;
1470 curr_head
+= L1_CACHE_BYTES
;
1471 phys_offset
= (unsigned long)sde
->head_dma
-
1472 (unsigned long)dd
->sdma_heads_dma
;
1473 sde
->head_phys
= dd
->sdma_heads_phys
+ phys_offset
;
1474 init_sdma_regs(sde
, per_sdma_credits
, idle_cnt
);
1476 dd
->flags
|= HFI1_HAS_SEND_DMA
;
1477 dd
->flags
|= idle_cnt
? HFI1_HAS_SDMA_TIMEOUT
: 0;
1478 dd
->num_sdma
= num_engines
;
1479 ret
= sdma_map_init(dd
, port
, ppd
->vls_operational
, NULL
);
1483 tmp_sdma_rht
= kzalloc(sizeof(*tmp_sdma_rht
), GFP_KERNEL
);
1484 if (!tmp_sdma_rht
) {
1489 ret
= rhashtable_init(tmp_sdma_rht
, &sdma_rht_params
);
1491 kfree(tmp_sdma_rht
);
1495 dd
->sdma_rht
= tmp_sdma_rht
;
1497 dd_dev_info(dd
, "SDMA num_sdma: %u\n", dd
->num_sdma
);
1501 sdma_clean(dd
, num_engines
);
1506 * sdma_all_running() - called when the link goes up
1509 * This routine moves all engines to the running state.
1511 void sdma_all_running(struct hfi1_devdata
*dd
)
1513 struct sdma_engine
*sde
;
1516 /* move all engines to running */
1517 for (i
= 0; i
< dd
->num_sdma
; ++i
) {
1518 sde
= &dd
->per_sdma
[i
];
1519 sdma_process_event(sde
, sdma_event_e30_go_running
);
1524 * sdma_all_idle() - called when the link goes down
1527 * This routine moves all engines to the idle state.
1529 void sdma_all_idle(struct hfi1_devdata
*dd
)
1531 struct sdma_engine
*sde
;
1534 /* idle all engines */
1535 for (i
= 0; i
< dd
->num_sdma
; ++i
) {
1536 sde
= &dd
->per_sdma
[i
];
1537 sdma_process_event(sde
, sdma_event_e70_go_idle
);
1542 * sdma_start() - called to kick off state processing for all engines
1545 * This routine is for kicking off the state processing for all required
1546 * sdma engines. Interrupts need to be working at this point.
1549 void sdma_start(struct hfi1_devdata
*dd
)
1552 struct sdma_engine
*sde
;
1554 /* kick off the engines state processing */
1555 for (i
= 0; i
< dd
->num_sdma
; ++i
) {
1556 sde
= &dd
->per_sdma
[i
];
1557 sdma_process_event(sde
, sdma_event_e10_go_hw_start
);
1562 * sdma_exit() - used when module is removed
1565 void sdma_exit(struct hfi1_devdata
*dd
)
1568 struct sdma_engine
*sde
;
1570 for (this_idx
= 0; dd
->per_sdma
&& this_idx
< dd
->num_sdma
;
1572 sde
= &dd
->per_sdma
[this_idx
];
1573 if (!list_empty(&sde
->dmawait
))
1574 dd_dev_err(dd
, "sde %u: dmawait list not empty!\n",
1576 sdma_process_event(sde
, sdma_event_e00_go_hw_down
);
1578 del_timer_sync(&sde
->err_progress_check_timer
);
1581 * This waits for the state machine to exit so it is not
1582 * necessary to kill the sdma_sw_clean_up_task to make sure
1583 * it is not running.
1585 sdma_finalput(&sde
->state
);
1590 * unmap the indicated descriptor
1592 static inline void sdma_unmap_desc(
1593 struct hfi1_devdata
*dd
,
1594 struct sdma_desc
*descp
)
1596 switch (sdma_mapping_type(descp
)) {
1597 case SDMA_MAP_SINGLE
:
1598 dma_unmap_single(&dd
->pcidev
->dev
, sdma_mapping_addr(descp
),
1599 sdma_mapping_len(descp
), DMA_TO_DEVICE
);
1602 dma_unmap_page(&dd
->pcidev
->dev
, sdma_mapping_addr(descp
),
1603 sdma_mapping_len(descp
), DMA_TO_DEVICE
);
1607 if (descp
->pinning_ctx
&& descp
->ctx_put
)
1608 descp
->ctx_put(descp
->pinning_ctx
);
1609 descp
->pinning_ctx
= NULL
;
1613 * return the mode as indicated by the first
1614 * descriptor in the tx.
1616 static inline u8
ahg_mode(struct sdma_txreq
*tx
)
1618 return (tx
->descp
[0].qw
[1] & SDMA_DESC1_HEADER_MODE_SMASK
)
1619 >> SDMA_DESC1_HEADER_MODE_SHIFT
;
1623 * __sdma_txclean() - clean tx of mappings, descp *kmalloc's
1624 * @dd: hfi1_devdata for unmapping
1625 * @tx: tx request to clean
1627 * This is used in the progress routine to clean the tx or
1628 * by the ULP to toss an in-process tx build.
1630 * The code can be called multiple times without issue.
1633 void __sdma_txclean(
1634 struct hfi1_devdata
*dd
,
1635 struct sdma_txreq
*tx
)
1640 u8 skip
= 0, mode
= ahg_mode(tx
);
1643 sdma_unmap_desc(dd
, &tx
->descp
[0]);
1644 /* determine number of AHG descriptors to skip */
1645 if (mode
> SDMA_AHG_APPLY_UPDATE1
)
1647 for (i
= 1 + skip
; i
< tx
->num_desc
; i
++)
1648 sdma_unmap_desc(dd
, &tx
->descp
[i
]);
1651 kfree(tx
->coalesce_buf
);
1652 tx
->coalesce_buf
= NULL
;
1653 /* kmalloc'ed descp */
1654 if (unlikely(tx
->desc_limit
> ARRAY_SIZE(tx
->descs
))) {
1655 tx
->desc_limit
= ARRAY_SIZE(tx
->descs
);
1660 static inline u16
sdma_gethead(struct sdma_engine
*sde
)
1662 struct hfi1_devdata
*dd
= sde
->dd
;
1666 #ifdef CONFIG_SDMA_VERBOSITY
1667 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) %s:%d %s()\n",
1668 sde
->this_idx
, slashstrip(__FILE__
), __LINE__
, __func__
);
1672 use_dmahead
= HFI1_CAP_IS_KSET(USE_SDMA_HEAD
) && __sdma_running(sde
) &&
1673 (dd
->flags
& HFI1_HAS_SDMA_TIMEOUT
);
1674 hwhead
= use_dmahead
?
1675 (u16
)le64_to_cpu(*sde
->head_dma
) :
1676 (u16
)read_sde_csr(sde
, SD(HEAD
));
1678 if (unlikely(HFI1_CAP_IS_KSET(SDMA_HEAD_CHECK
))) {
1684 swhead
= sde
->descq_head
& sde
->sdma_mask
;
1685 /* this code is really bad for cache line trading */
1686 swtail
= READ_ONCE(sde
->descq_tail
) & sde
->sdma_mask
;
1687 cnt
= sde
->descq_cnt
;
1689 if (swhead
< swtail
)
1691 sane
= (hwhead
>= swhead
) & (hwhead
<= swtail
);
1692 else if (swhead
> swtail
)
1693 /* wrapped around */
1694 sane
= ((hwhead
>= swhead
) && (hwhead
< cnt
)) ||
1698 sane
= (hwhead
== swhead
);
1700 if (unlikely(!sane
)) {
1701 dd_dev_err(dd
, "SDMA(%u) bad head (%s) hwhd=%u swhd=%u swtl=%u cnt=%u\n",
1703 use_dmahead
? "dma" : "kreg",
1704 hwhead
, swhead
, swtail
, cnt
);
1706 /* try one more time, using csr */
1710 /* proceed as if no progress */
1718 * This is called when there are send DMA descriptors that might be
1721 * This is called with head_lock held.
1723 static void sdma_desc_avail(struct sdma_engine
*sde
, uint avail
)
1725 struct iowait
*wait
, *nw
, *twait
;
1726 struct iowait
*waits
[SDMA_WAIT_BATCH_SIZE
];
1727 uint i
, n
= 0, seq
, tidx
= 0;
1729 #ifdef CONFIG_SDMA_VERBOSITY
1730 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) %s:%d %s()\n", sde
->this_idx
,
1731 slashstrip(__FILE__
), __LINE__
, __func__
);
1732 dd_dev_err(sde
->dd
, "avail: %u\n", avail
);
1736 seq
= read_seqbegin(&sde
->waitlock
);
1737 if (!list_empty(&sde
->dmawait
)) {
1738 /* at least one item */
1739 write_seqlock(&sde
->waitlock
);
1740 /* Harvest waiters wanting DMA descriptors */
1741 list_for_each_entry_safe(
1750 if (n
== ARRAY_SIZE(waits
))
1752 iowait_init_priority(wait
);
1753 num_desc
= iowait_get_all_desc(wait
);
1754 if (num_desc
> avail
)
1757 /* Find the top-priority wait memeber */
1759 twait
= waits
[tidx
];
1761 iowait_priority_update_top(wait
,
1766 list_del_init(&wait
->list
);
1769 write_sequnlock(&sde
->waitlock
);
1772 } while (read_seqretry(&sde
->waitlock
, seq
));
1774 /* Schedule the top-priority entry first */
1776 waits
[tidx
]->wakeup(waits
[tidx
], SDMA_AVAIL_REASON
);
1778 for (i
= 0; i
< n
; i
++)
1780 waits
[i
]->wakeup(waits
[i
], SDMA_AVAIL_REASON
);
1783 /* head_lock must be held */
1784 static void sdma_make_progress(struct sdma_engine
*sde
, u64 status
)
1786 struct sdma_txreq
*txp
= NULL
;
1789 int idle_check_done
= 0;
1791 hwhead
= sdma_gethead(sde
);
1793 /* The reason for some of the complexity of this code is that
1794 * not all descriptors have corresponding txps. So, we have to
1795 * be able to skip over descs until we wander into the range of
1796 * the next txp on the list.
1800 txp
= get_txhead(sde
);
1801 swhead
= sde
->descq_head
& sde
->sdma_mask
;
1802 trace_hfi1_sdma_progress(sde
, hwhead
, swhead
, txp
);
1803 while (swhead
!= hwhead
) {
1804 /* advance head, wrap if needed */
1805 swhead
= ++sde
->descq_head
& sde
->sdma_mask
;
1807 /* if now past this txp's descs, do the callback */
1808 if (txp
&& txp
->next_descq_idx
== swhead
) {
1809 /* remove from list */
1810 sde
->tx_ring
[sde
->tx_head
++ & sde
->sdma_mask
] = NULL
;
1811 complete_tx(sde
, txp
, SDMA_TXREQ_S_OK
);
1812 /* see if there is another txp */
1813 txp
= get_txhead(sde
);
1815 trace_hfi1_sdma_progress(sde
, hwhead
, swhead
, txp
);
1820 * The SDMA idle interrupt is not guaranteed to be ordered with respect
1821 * to updates to the dma_head location in host memory. The head
1822 * value read might not be fully up to date. If there are pending
1823 * descriptors and the SDMA idle interrupt fired then read from the
1824 * CSR SDMA head instead to get the latest value from the hardware.
1825 * The hardware SDMA head should be read at most once in this invocation
1826 * of sdma_make_progress(..) which is ensured by idle_check_done flag
1828 if ((status
& sde
->idle_mask
) && !idle_check_done
) {
1831 swtail
= READ_ONCE(sde
->descq_tail
) & sde
->sdma_mask
;
1832 if (swtail
!= hwhead
) {
1833 hwhead
= (u16
)read_sde_csr(sde
, SD(HEAD
));
1834 idle_check_done
= 1;
1839 sde
->last_status
= status
;
1841 sdma_desc_avail(sde
, sdma_descq_freecnt(sde
));
1845 * sdma_engine_interrupt() - interrupt handler for engine
1847 * @status: sdma interrupt reason
1849 * Status is a mask of the 3 possible interrupts for this engine. It will
1850 * contain bits _only_ for this SDMA engine. It will contain at least one
1851 * bit, it may contain more.
1853 void sdma_engine_interrupt(struct sdma_engine
*sde
, u64 status
)
1855 trace_hfi1_sdma_engine_interrupt(sde
, status
);
1856 write_seqlock(&sde
->head_lock
);
1857 sdma_set_desc_cnt(sde
, sdma_desct_intr
);
1858 if (status
& sde
->idle_mask
)
1859 sde
->idle_int_cnt
++;
1860 else if (status
& sde
->progress_mask
)
1861 sde
->progress_int_cnt
++;
1862 else if (status
& sde
->int_mask
)
1863 sde
->sdma_int_cnt
++;
1864 sdma_make_progress(sde
, status
);
1865 write_sequnlock(&sde
->head_lock
);
1869 * sdma_engine_error() - error handler for engine
1871 * @status: sdma interrupt reason
1873 void sdma_engine_error(struct sdma_engine
*sde
, u64 status
)
1875 unsigned long flags
;
1877 #ifdef CONFIG_SDMA_VERBOSITY
1878 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) error status 0x%llx state %s\n",
1880 (unsigned long long)status
,
1881 sdma_state_names
[sde
->state
.current_state
]);
1883 spin_lock_irqsave(&sde
->tail_lock
, flags
);
1884 write_seqlock(&sde
->head_lock
);
1885 if (status
& ALL_SDMA_ENG_HALT_ERRS
)
1886 __sdma_process_event(sde
, sdma_event_e60_hw_halted
);
1887 if (status
& ~SD(ENG_ERR_STATUS_SDMA_HALT_ERR_SMASK
)) {
1889 "SDMA (%u) engine error: 0x%llx state %s\n",
1891 (unsigned long long)status
,
1892 sdma_state_names
[sde
->state
.current_state
]);
1893 dump_sdma_state(sde
);
1895 write_sequnlock(&sde
->head_lock
);
1896 spin_unlock_irqrestore(&sde
->tail_lock
, flags
);
1899 static void sdma_sendctrl(struct sdma_engine
*sde
, unsigned op
)
1901 u64 set_senddmactrl
= 0;
1902 u64 clr_senddmactrl
= 0;
1903 unsigned long flags
;
1905 #ifdef CONFIG_SDMA_VERBOSITY
1906 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) senddmactrl E=%d I=%d H=%d C=%d\n",
1908 (op
& SDMA_SENDCTRL_OP_ENABLE
) ? 1 : 0,
1909 (op
& SDMA_SENDCTRL_OP_INTENABLE
) ? 1 : 0,
1910 (op
& SDMA_SENDCTRL_OP_HALT
) ? 1 : 0,
1911 (op
& SDMA_SENDCTRL_OP_CLEANUP
) ? 1 : 0);
1914 if (op
& SDMA_SENDCTRL_OP_ENABLE
)
1915 set_senddmactrl
|= SD(CTRL_SDMA_ENABLE_SMASK
);
1917 clr_senddmactrl
|= SD(CTRL_SDMA_ENABLE_SMASK
);
1919 if (op
& SDMA_SENDCTRL_OP_INTENABLE
)
1920 set_senddmactrl
|= SD(CTRL_SDMA_INT_ENABLE_SMASK
);
1922 clr_senddmactrl
|= SD(CTRL_SDMA_INT_ENABLE_SMASK
);
1924 if (op
& SDMA_SENDCTRL_OP_HALT
)
1925 set_senddmactrl
|= SD(CTRL_SDMA_HALT_SMASK
);
1927 clr_senddmactrl
|= SD(CTRL_SDMA_HALT_SMASK
);
1929 spin_lock_irqsave(&sde
->senddmactrl_lock
, flags
);
1931 sde
->p_senddmactrl
|= set_senddmactrl
;
1932 sde
->p_senddmactrl
&= ~clr_senddmactrl
;
1934 if (op
& SDMA_SENDCTRL_OP_CLEANUP
)
1935 write_sde_csr(sde
, SD(CTRL
),
1936 sde
->p_senddmactrl
|
1937 SD(CTRL_SDMA_CLEANUP_SMASK
));
1939 write_sde_csr(sde
, SD(CTRL
), sde
->p_senddmactrl
);
1941 spin_unlock_irqrestore(&sde
->senddmactrl_lock
, flags
);
1943 #ifdef CONFIG_SDMA_VERBOSITY
1944 sdma_dumpstate(sde
);
1948 static void sdma_setlengen(struct sdma_engine
*sde
)
1950 #ifdef CONFIG_SDMA_VERBOSITY
1951 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) %s:%d %s()\n",
1952 sde
->this_idx
, slashstrip(__FILE__
), __LINE__
, __func__
);
1956 * Set SendDmaLenGen and clear-then-set the MSB of the generation
1957 * count to enable generation checking and load the internal
1958 * generation counter.
1960 write_sde_csr(sde
, SD(LEN_GEN
),
1961 (sde
->descq_cnt
/ 64) << SD(LEN_GEN_LENGTH_SHIFT
));
1962 write_sde_csr(sde
, SD(LEN_GEN
),
1963 ((sde
->descq_cnt
/ 64) << SD(LEN_GEN_LENGTH_SHIFT
)) |
1964 (4ULL << SD(LEN_GEN_GENERATION_SHIFT
)));
1967 static inline void sdma_update_tail(struct sdma_engine
*sde
, u16 tail
)
1969 /* Commit writes to memory and advance the tail on the chip */
1970 smp_wmb(); /* see get_txhead() */
1971 writeq(tail
, sde
->tail_csr
);
1975 * This is called when changing to state s10_hw_start_up_halt_wait as
1976 * a result of send buffer errors or send DMA descriptor errors.
1978 static void sdma_hw_start_up(struct sdma_engine
*sde
)
1982 #ifdef CONFIG_SDMA_VERBOSITY
1983 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) %s:%d %s()\n",
1984 sde
->this_idx
, slashstrip(__FILE__
), __LINE__
, __func__
);
1987 sdma_setlengen(sde
);
1988 sdma_update_tail(sde
, 0); /* Set SendDmaTail */
1991 reg
= SD(ENG_ERR_CLEAR_SDMA_HEADER_REQUEST_FIFO_UNC_ERR_MASK
) <<
1992 SD(ENG_ERR_CLEAR_SDMA_HEADER_REQUEST_FIFO_UNC_ERR_SHIFT
);
1993 write_sde_csr(sde
, SD(ENG_ERR_CLEAR
), reg
);
1997 * set_sdma_integrity
1999 * Set the SEND_DMA_CHECK_ENABLE register for send DMA engine 'sde'.
2001 static void set_sdma_integrity(struct sdma_engine
*sde
)
2003 struct hfi1_devdata
*dd
= sde
->dd
;
2005 write_sde_csr(sde
, SD(CHECK_ENABLE
),
2006 hfi1_pkt_base_sdma_integrity(dd
));
2009 static void init_sdma_regs(
2010 struct sdma_engine
*sde
,
2015 #ifdef CONFIG_SDMA_VERBOSITY
2016 struct hfi1_devdata
*dd
= sde
->dd
;
2018 dd_dev_err(dd
, "CONFIG SDMA(%u) %s:%d %s()\n",
2019 sde
->this_idx
, slashstrip(__FILE__
), __LINE__
, __func__
);
2022 write_sde_csr(sde
, SD(BASE_ADDR
), sde
->descq_phys
);
2023 sdma_setlengen(sde
);
2024 sdma_update_tail(sde
, 0); /* Set SendDmaTail */
2025 write_sde_csr(sde
, SD(RELOAD_CNT
), idle_cnt
);
2026 write_sde_csr(sde
, SD(DESC_CNT
), 0);
2027 write_sde_csr(sde
, SD(HEAD_ADDR
), sde
->head_phys
);
2028 write_sde_csr(sde
, SD(MEMORY
),
2029 ((u64
)credits
<< SD(MEMORY_SDMA_MEMORY_CNT_SHIFT
)) |
2030 ((u64
)(credits
* sde
->this_idx
) <<
2031 SD(MEMORY_SDMA_MEMORY_INDEX_SHIFT
)));
2032 write_sde_csr(sde
, SD(ENG_ERR_MASK
), ~0ull);
2033 set_sdma_integrity(sde
);
2034 opmask
= OPCODE_CHECK_MASK_DISABLED
;
2035 opval
= OPCODE_CHECK_VAL_DISABLED
;
2036 write_sde_csr(sde
, SD(CHECK_OPCODE
),
2037 (opmask
<< SEND_CTXT_CHECK_OPCODE_MASK_SHIFT
) |
2038 (opval
<< SEND_CTXT_CHECK_OPCODE_VALUE_SHIFT
));
2041 #ifdef CONFIG_SDMA_VERBOSITY
2043 #define sdma_dumpstate_helper0(reg) do { \
2044 csr = read_csr(sde->dd, reg); \
2045 dd_dev_err(sde->dd, "%36s 0x%016llx\n", #reg, csr); \
2048 #define sdma_dumpstate_helper(reg) do { \
2049 csr = read_sde_csr(sde, reg); \
2050 dd_dev_err(sde->dd, "%36s[%02u] 0x%016llx\n", \
2051 #reg, sde->this_idx, csr); \
2054 #define sdma_dumpstate_helper2(reg) do { \
2055 csr = read_csr(sde->dd, reg + (8 * i)); \
2056 dd_dev_err(sde->dd, "%33s_%02u 0x%016llx\n", \
2060 void sdma_dumpstate(struct sdma_engine
*sde
)
2065 sdma_dumpstate_helper(SD(CTRL
));
2066 sdma_dumpstate_helper(SD(STATUS
));
2067 sdma_dumpstate_helper0(SD(ERR_STATUS
));
2068 sdma_dumpstate_helper0(SD(ERR_MASK
));
2069 sdma_dumpstate_helper(SD(ENG_ERR_STATUS
));
2070 sdma_dumpstate_helper(SD(ENG_ERR_MASK
));
2072 for (i
= 0; i
< CCE_NUM_INT_CSRS
; ++i
) {
2073 sdma_dumpstate_helper2(CCE_INT_STATUS
);
2074 sdma_dumpstate_helper2(CCE_INT_MASK
);
2075 sdma_dumpstate_helper2(CCE_INT_BLOCKED
);
2078 sdma_dumpstate_helper(SD(TAIL
));
2079 sdma_dumpstate_helper(SD(HEAD
));
2080 sdma_dumpstate_helper(SD(PRIORITY_THLD
));
2081 sdma_dumpstate_helper(SD(IDLE_CNT
));
2082 sdma_dumpstate_helper(SD(RELOAD_CNT
));
2083 sdma_dumpstate_helper(SD(DESC_CNT
));
2084 sdma_dumpstate_helper(SD(DESC_FETCHED_CNT
));
2085 sdma_dumpstate_helper(SD(MEMORY
));
2086 sdma_dumpstate_helper0(SD(ENGINES
));
2087 sdma_dumpstate_helper0(SD(MEM_SIZE
));
2088 /* sdma_dumpstate_helper(SEND_EGRESS_SEND_DMA_STATUS); */
2089 sdma_dumpstate_helper(SD(BASE_ADDR
));
2090 sdma_dumpstate_helper(SD(LEN_GEN
));
2091 sdma_dumpstate_helper(SD(HEAD_ADDR
));
2092 sdma_dumpstate_helper(SD(CHECK_ENABLE
));
2093 sdma_dumpstate_helper(SD(CHECK_VL
));
2094 sdma_dumpstate_helper(SD(CHECK_JOB_KEY
));
2095 sdma_dumpstate_helper(SD(CHECK_PARTITION_KEY
));
2096 sdma_dumpstate_helper(SD(CHECK_SLID
));
2097 sdma_dumpstate_helper(SD(CHECK_OPCODE
));
2101 static void dump_sdma_state(struct sdma_engine
*sde
)
2103 struct hw_sdma_desc
*descqp
;
2108 u16 head
, tail
, cnt
;
2110 head
= sde
->descq_head
& sde
->sdma_mask
;
2111 tail
= sde
->descq_tail
& sde
->sdma_mask
;
2112 cnt
= sdma_descq_freecnt(sde
);
2115 "SDMA (%u) descq_head: %u descq_tail: %u freecnt: %u FLE %d\n",
2116 sde
->this_idx
, head
, tail
, cnt
,
2117 !list_empty(&sde
->flushlist
));
2119 /* print info for each entry in the descriptor queue */
2120 while (head
!= tail
) {
2121 char flags
[6] = { 'x', 'x', 'x', 'x', 0 };
2123 descqp
= &sde
->descq
[head
];
2124 desc
[0] = le64_to_cpu(descqp
->qw
[0]);
2125 desc
[1] = le64_to_cpu(descqp
->qw
[1]);
2126 flags
[0] = (desc
[1] & SDMA_DESC1_INT_REQ_FLAG
) ? 'I' : '-';
2127 flags
[1] = (desc
[1] & SDMA_DESC1_HEAD_TO_HOST_FLAG
) ?
2129 flags
[2] = (desc
[0] & SDMA_DESC0_FIRST_DESC_FLAG
) ? 'F' : '-';
2130 flags
[3] = (desc
[0] & SDMA_DESC0_LAST_DESC_FLAG
) ? 'L' : '-';
2131 addr
= (desc
[0] >> SDMA_DESC0_PHY_ADDR_SHIFT
)
2132 & SDMA_DESC0_PHY_ADDR_MASK
;
2133 gen
= (desc
[1] >> SDMA_DESC1_GENERATION_SHIFT
)
2134 & SDMA_DESC1_GENERATION_MASK
;
2135 len
= (desc
[0] >> SDMA_DESC0_BYTE_COUNT_SHIFT
)
2136 & SDMA_DESC0_BYTE_COUNT_MASK
;
2138 "SDMA sdmadesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes\n",
2139 head
, flags
, addr
, gen
, len
);
2141 "\tdesc0:0x%016llx desc1 0x%016llx\n",
2143 if (desc
[0] & SDMA_DESC0_FIRST_DESC_FLAG
)
2145 "\taidx: %u amode: %u alen: %u\n",
2147 SDMA_DESC1_HEADER_INDEX_SMASK
) >>
2148 SDMA_DESC1_HEADER_INDEX_SHIFT
),
2150 SDMA_DESC1_HEADER_MODE_SMASK
) >>
2151 SDMA_DESC1_HEADER_MODE_SHIFT
),
2153 SDMA_DESC1_HEADER_DWS_SMASK
) >>
2154 SDMA_DESC1_HEADER_DWS_SHIFT
));
2156 head
&= sde
->sdma_mask
;
2161 "SDE %u CPU %d STE %s C 0x%llx S 0x%016llx E 0x%llx T(HW) 0x%llx T(SW) 0x%x H(HW) 0x%llx H(SW) 0x%x H(D) 0x%llx DM 0x%llx GL 0x%llx R 0x%llx LIS 0x%llx AHGI 0x%llx TXT %u TXH %u DT %u DH %u FLNE %d DQF %u SLC 0x%llx\n"
2163 * sdma_seqfile_dump_sde() - debugfs dump of sde
2165 * @sde: send dma engine to dump
2167 * This routine dumps the sde to the indicated seq file.
2169 void sdma_seqfile_dump_sde(struct seq_file
*s
, struct sdma_engine
*sde
)
2172 struct hw_sdma_desc
*descqp
;
2178 head
= sde
->descq_head
& sde
->sdma_mask
;
2179 tail
= READ_ONCE(sde
->descq_tail
) & sde
->sdma_mask
;
2180 seq_printf(s
, SDE_FMT
, sde
->this_idx
,
2182 sdma_state_name(sde
->state
.current_state
),
2183 (unsigned long long)read_sde_csr(sde
, SD(CTRL
)),
2184 (unsigned long long)read_sde_csr(sde
, SD(STATUS
)),
2185 (unsigned long long)read_sde_csr(sde
, SD(ENG_ERR_STATUS
)),
2186 (unsigned long long)read_sde_csr(sde
, SD(TAIL
)), tail
,
2187 (unsigned long long)read_sde_csr(sde
, SD(HEAD
)), head
,
2188 (unsigned long long)le64_to_cpu(*sde
->head_dma
),
2189 (unsigned long long)read_sde_csr(sde
, SD(MEMORY
)),
2190 (unsigned long long)read_sde_csr(sde
, SD(LEN_GEN
)),
2191 (unsigned long long)read_sde_csr(sde
, SD(RELOAD_CNT
)),
2192 (unsigned long long)sde
->last_status
,
2193 (unsigned long long)sde
->ahg_bits
,
2198 !list_empty(&sde
->flushlist
),
2199 sde
->descq_full_count
,
2200 (unsigned long long)read_sde_csr(sde
, SEND_DMA_CHECK_SLID
));
2202 /* print info for each entry in the descriptor queue */
2203 while (head
!= tail
) {
2204 char flags
[6] = { 'x', 'x', 'x', 'x', 0 };
2206 descqp
= &sde
->descq
[head
];
2207 desc
[0] = le64_to_cpu(descqp
->qw
[0]);
2208 desc
[1] = le64_to_cpu(descqp
->qw
[1]);
2209 flags
[0] = (desc
[1] & SDMA_DESC1_INT_REQ_FLAG
) ? 'I' : '-';
2210 flags
[1] = (desc
[1] & SDMA_DESC1_HEAD_TO_HOST_FLAG
) ?
2212 flags
[2] = (desc
[0] & SDMA_DESC0_FIRST_DESC_FLAG
) ? 'F' : '-';
2213 flags
[3] = (desc
[0] & SDMA_DESC0_LAST_DESC_FLAG
) ? 'L' : '-';
2214 addr
= (desc
[0] >> SDMA_DESC0_PHY_ADDR_SHIFT
)
2215 & SDMA_DESC0_PHY_ADDR_MASK
;
2216 gen
= (desc
[1] >> SDMA_DESC1_GENERATION_SHIFT
)
2217 & SDMA_DESC1_GENERATION_MASK
;
2218 len
= (desc
[0] >> SDMA_DESC0_BYTE_COUNT_SHIFT
)
2219 & SDMA_DESC0_BYTE_COUNT_MASK
;
2221 "\tdesc[%u]: flags:%s addr:0x%016llx gen:%u len:%u bytes\n",
2222 head
, flags
, addr
, gen
, len
);
2223 if (desc
[0] & SDMA_DESC0_FIRST_DESC_FLAG
)
2224 seq_printf(s
, "\t\tahgidx: %u ahgmode: %u\n",
2226 SDMA_DESC1_HEADER_INDEX_SMASK
) >>
2227 SDMA_DESC1_HEADER_INDEX_SHIFT
),
2229 SDMA_DESC1_HEADER_MODE_SMASK
) >>
2230 SDMA_DESC1_HEADER_MODE_SHIFT
));
2231 head
= (head
+ 1) & sde
->sdma_mask
;
2236 * add the generation number into
2237 * the qw1 and return
2239 static inline u64
add_gen(struct sdma_engine
*sde
, u64 qw1
)
2241 u8 generation
= (sde
->descq_tail
>> sde
->sdma_shift
) & 3;
2243 qw1
&= ~SDMA_DESC1_GENERATION_SMASK
;
2244 qw1
|= ((u64
)generation
& SDMA_DESC1_GENERATION_MASK
)
2245 << SDMA_DESC1_GENERATION_SHIFT
;
2250 * This routine submits the indicated tx
2252 * Space has already been guaranteed and
2253 * tail side of ring is locked.
2255 * The hardware tail update is done
2256 * in the caller and that is facilitated
2257 * by returning the new tail.
2259 * There is special case logic for ahg
2260 * to not add the generation number for
2261 * up to 2 descriptors that follow the
2265 static inline u16
submit_tx(struct sdma_engine
*sde
, struct sdma_txreq
*tx
)
2269 struct sdma_desc
*descp
= tx
->descp
;
2270 u8 skip
= 0, mode
= ahg_mode(tx
);
2272 tail
= sde
->descq_tail
& sde
->sdma_mask
;
2273 sde
->descq
[tail
].qw
[0] = cpu_to_le64(descp
->qw
[0]);
2274 sde
->descq
[tail
].qw
[1] = cpu_to_le64(add_gen(sde
, descp
->qw
[1]));
2275 trace_hfi1_sdma_descriptor(sde
, descp
->qw
[0], descp
->qw
[1],
2276 tail
, &sde
->descq
[tail
]);
2277 tail
= ++sde
->descq_tail
& sde
->sdma_mask
;
2279 if (mode
> SDMA_AHG_APPLY_UPDATE1
)
2281 for (i
= 1; i
< tx
->num_desc
; i
++, descp
++) {
2284 sde
->descq
[tail
].qw
[0] = cpu_to_le64(descp
->qw
[0]);
2286 /* edits don't have generation */
2290 /* replace generation with real one for non-edits */
2291 qw1
= add_gen(sde
, descp
->qw
[1]);
2293 sde
->descq
[tail
].qw
[1] = cpu_to_le64(qw1
);
2294 trace_hfi1_sdma_descriptor(sde
, descp
->qw
[0], qw1
,
2295 tail
, &sde
->descq
[tail
]);
2296 tail
= ++sde
->descq_tail
& sde
->sdma_mask
;
2298 tx
->next_descq_idx
= tail
;
2299 #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
2300 tx
->sn
= sde
->tail_sn
++;
2301 trace_hfi1_sdma_in_sn(sde
, tx
->sn
);
2302 WARN_ON_ONCE(sde
->tx_ring
[sde
->tx_tail
& sde
->sdma_mask
]);
2304 sde
->tx_ring
[sde
->tx_tail
++ & sde
->sdma_mask
] = tx
;
2305 sde
->desc_avail
-= tx
->num_desc
;
2310 * Check for progress
2312 static int sdma_check_progress(
2313 struct sdma_engine
*sde
,
2314 struct iowait_work
*wait
,
2315 struct sdma_txreq
*tx
,
2320 sde
->desc_avail
= sdma_descq_freecnt(sde
);
2321 if (tx
->num_desc
<= sde
->desc_avail
)
2323 /* pulse the head_lock */
2324 if (wait
&& iowait_ioww_to_iow(wait
)->sleep
) {
2327 seq
= raw_seqcount_begin(
2328 (const seqcount_t
*)&sde
->head_lock
.seqcount
);
2329 ret
= wait
->iow
->sleep(sde
, wait
, tx
, seq
, pkts_sent
);
2331 sde
->desc_avail
= sdma_descq_freecnt(sde
);
2339 * sdma_send_txreq() - submit a tx req to ring
2340 * @sde: sdma engine to use
2341 * @wait: SE wait structure to use when full (may be NULL)
2342 * @tx: sdma_txreq to submit
2343 * @pkts_sent: has any packet been sent yet?
2345 * The call submits the tx into the ring. If a iowait structure is non-NULL
2346 * the packet will be queued to the list in wait.
2349 * 0 - Success, -EINVAL - sdma_txreq incomplete, -EBUSY - no space in
2350 * ring (wait == NULL)
2351 * -EIOCBQUEUED - tx queued to iowait, -ECOMM bad sdma state
2353 int sdma_send_txreq(struct sdma_engine
*sde
,
2354 struct iowait_work
*wait
,
2355 struct sdma_txreq
*tx
,
2360 unsigned long flags
;
2362 /* user should have supplied entire packet */
2363 if (unlikely(tx
->tlen
))
2365 tx
->wait
= iowait_ioww_to_iow(wait
);
2366 spin_lock_irqsave(&sde
->tail_lock
, flags
);
2368 if (unlikely(!__sdma_running(sde
)))
2370 if (unlikely(tx
->num_desc
> sde
->desc_avail
))
2372 tail
= submit_tx(sde
, tx
);
2374 iowait_sdma_inc(iowait_ioww_to_iow(wait
));
2375 sdma_update_tail(sde
, tail
);
2377 spin_unlock_irqrestore(&sde
->tail_lock
, flags
);
2381 iowait_sdma_inc(iowait_ioww_to_iow(wait
));
2382 tx
->next_descq_idx
= 0;
2383 #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
2384 tx
->sn
= sde
->tail_sn
++;
2385 trace_hfi1_sdma_in_sn(sde
, tx
->sn
);
2387 spin_lock(&sde
->flushlist_lock
);
2388 list_add_tail(&tx
->list
, &sde
->flushlist
);
2389 spin_unlock(&sde
->flushlist_lock
);
2390 iowait_inc_wait_count(wait
, tx
->num_desc
);
2391 queue_work_on(sde
->cpu
, system_highpri_wq
, &sde
->flush_worker
);
2395 ret
= sdma_check_progress(sde
, wait
, tx
, pkts_sent
);
2396 if (ret
== -EAGAIN
) {
2400 sde
->descq_full_count
++;
2405 * sdma_send_txlist() - submit a list of tx req to ring
2406 * @sde: sdma engine to use
2407 * @wait: SE wait structure to use when full (may be NULL)
2408 * @tx_list: list of sdma_txreqs to submit
2409 * @count_out: pointer to a u16 which, after return will contain the total number of
2410 * sdma_txreqs removed from the tx_list. This will include sdma_txreqs
2411 * whose SDMA descriptors are submitted to the ring and the sdma_txreqs
2412 * which are added to SDMA engine flush list if the SDMA engine state is
2415 * The call submits the list into the ring.
2417 * If the iowait structure is non-NULL and not equal to the iowait list
2418 * the unprocessed part of the list will be appended to the list in wait.
2420 * In all cases, the tx_list will be updated so the head of the tx_list is
2421 * the list of descriptors that have yet to be transmitted.
2423 * The intent of this call is to provide a more efficient
2424 * way of submitting multiple packets to SDMA while holding the tail
2429 * -EINVAL - sdma_txreq incomplete, -EBUSY - no space in ring (wait == NULL)
2430 * -EIOCBQUEUED - tx queued to iowait, -ECOMM bad sdma state
2432 int sdma_send_txlist(struct sdma_engine
*sde
, struct iowait_work
*wait
,
2433 struct list_head
*tx_list
, u16
*count_out
)
2435 struct sdma_txreq
*tx
, *tx_next
;
2437 unsigned long flags
;
2438 u16 tail
= INVALID_TAIL
;
2439 u32 submit_count
= 0, flush_count
= 0, total_count
;
2441 spin_lock_irqsave(&sde
->tail_lock
, flags
);
2443 list_for_each_entry_safe(tx
, tx_next
, tx_list
, list
) {
2444 tx
->wait
= iowait_ioww_to_iow(wait
);
2445 if (unlikely(!__sdma_running(sde
)))
2447 if (unlikely(tx
->num_desc
> sde
->desc_avail
))
2449 if (unlikely(tx
->tlen
)) {
2453 list_del_init(&tx
->list
);
2454 tail
= submit_tx(sde
, tx
);
2456 if (tail
!= INVALID_TAIL
&&
2457 (submit_count
& SDMA_TAIL_UPDATE_THRESH
) == 0) {
2458 sdma_update_tail(sde
, tail
);
2459 tail
= INVALID_TAIL
;
2463 total_count
= submit_count
+ flush_count
;
2465 iowait_sdma_add(iowait_ioww_to_iow(wait
), total_count
);
2466 iowait_starve_clear(submit_count
> 0,
2467 iowait_ioww_to_iow(wait
));
2469 if (tail
!= INVALID_TAIL
)
2470 sdma_update_tail(sde
, tail
);
2471 spin_unlock_irqrestore(&sde
->tail_lock
, flags
);
2472 *count_out
= total_count
;
2475 spin_lock(&sde
->flushlist_lock
);
2476 list_for_each_entry_safe(tx
, tx_next
, tx_list
, list
) {
2477 tx
->wait
= iowait_ioww_to_iow(wait
);
2478 list_del_init(&tx
->list
);
2479 tx
->next_descq_idx
= 0;
2480 #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
2481 tx
->sn
= sde
->tail_sn
++;
2482 trace_hfi1_sdma_in_sn(sde
, tx
->sn
);
2484 list_add_tail(&tx
->list
, &sde
->flushlist
);
2486 iowait_inc_wait_count(wait
, tx
->num_desc
);
2488 spin_unlock(&sde
->flushlist_lock
);
2489 queue_work_on(sde
->cpu
, system_highpri_wq
, &sde
->flush_worker
);
2493 ret
= sdma_check_progress(sde
, wait
, tx
, submit_count
> 0);
2494 if (ret
== -EAGAIN
) {
2498 sde
->descq_full_count
++;
2502 static void sdma_process_event(struct sdma_engine
*sde
, enum sdma_events event
)
2504 unsigned long flags
;
2506 spin_lock_irqsave(&sde
->tail_lock
, flags
);
2507 write_seqlock(&sde
->head_lock
);
2509 __sdma_process_event(sde
, event
);
2511 if (sde
->state
.current_state
== sdma_state_s99_running
)
2512 sdma_desc_avail(sde
, sdma_descq_freecnt(sde
));
2514 write_sequnlock(&sde
->head_lock
);
2515 spin_unlock_irqrestore(&sde
->tail_lock
, flags
);
2518 static void __sdma_process_event(struct sdma_engine
*sde
,
2519 enum sdma_events event
)
2521 struct sdma_state
*ss
= &sde
->state
;
2522 int need_progress
= 0;
2524 /* CONFIG SDMA temporary */
2525 #ifdef CONFIG_SDMA_VERBOSITY
2526 dd_dev_err(sde
->dd
, "CONFIG SDMA(%u) [%s] %s\n", sde
->this_idx
,
2527 sdma_state_names
[ss
->current_state
],
2528 sdma_event_names
[event
]);
2531 switch (ss
->current_state
) {
2532 case sdma_state_s00_hw_down
:
2534 case sdma_event_e00_go_hw_down
:
2536 case sdma_event_e30_go_running
:
2538 * If down, but running requested (usually result
2539 * of link up, then we need to start up.
2540 * This can happen when hw down is requested while
2541 * bringing the link up with traffic active on
2544 ss
->go_s99_running
= 1;
2545 fallthrough
; /* and start dma engine */
2546 case sdma_event_e10_go_hw_start
:
2547 /* This reference means the state machine is started */
2548 sdma_get(&sde
->state
);
2550 sdma_state_s10_hw_start_up_halt_wait
);
2552 case sdma_event_e15_hw_halt_done
:
2554 case sdma_event_e25_hw_clean_up_done
:
2556 case sdma_event_e40_sw_cleaned
:
2557 sdma_sw_tear_down(sde
);
2559 case sdma_event_e50_hw_cleaned
:
2561 case sdma_event_e60_hw_halted
:
2563 case sdma_event_e70_go_idle
:
2565 case sdma_event_e80_hw_freeze
:
2567 case sdma_event_e81_hw_frozen
:
2569 case sdma_event_e82_hw_unfreeze
:
2571 case sdma_event_e85_link_down
:
2573 case sdma_event_e90_sw_halted
:
2578 case sdma_state_s10_hw_start_up_halt_wait
:
2580 case sdma_event_e00_go_hw_down
:
2581 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2582 sdma_sw_tear_down(sde
);
2584 case sdma_event_e10_go_hw_start
:
2586 case sdma_event_e15_hw_halt_done
:
2588 sdma_state_s15_hw_start_up_clean_wait
);
2589 sdma_start_hw_clean_up(sde
);
2591 case sdma_event_e25_hw_clean_up_done
:
2593 case sdma_event_e30_go_running
:
2594 ss
->go_s99_running
= 1;
2596 case sdma_event_e40_sw_cleaned
:
2598 case sdma_event_e50_hw_cleaned
:
2600 case sdma_event_e60_hw_halted
:
2601 schedule_work(&sde
->err_halt_worker
);
2603 case sdma_event_e70_go_idle
:
2604 ss
->go_s99_running
= 0;
2606 case sdma_event_e80_hw_freeze
:
2608 case sdma_event_e81_hw_frozen
:
2610 case sdma_event_e82_hw_unfreeze
:
2612 case sdma_event_e85_link_down
:
2614 case sdma_event_e90_sw_halted
:
2619 case sdma_state_s15_hw_start_up_clean_wait
:
2621 case sdma_event_e00_go_hw_down
:
2622 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2623 sdma_sw_tear_down(sde
);
2625 case sdma_event_e10_go_hw_start
:
2627 case sdma_event_e15_hw_halt_done
:
2629 case sdma_event_e25_hw_clean_up_done
:
2630 sdma_hw_start_up(sde
);
2631 sdma_set_state(sde
, ss
->go_s99_running
?
2632 sdma_state_s99_running
:
2633 sdma_state_s20_idle
);
2635 case sdma_event_e30_go_running
:
2636 ss
->go_s99_running
= 1;
2638 case sdma_event_e40_sw_cleaned
:
2640 case sdma_event_e50_hw_cleaned
:
2642 case sdma_event_e60_hw_halted
:
2644 case sdma_event_e70_go_idle
:
2645 ss
->go_s99_running
= 0;
2647 case sdma_event_e80_hw_freeze
:
2649 case sdma_event_e81_hw_frozen
:
2651 case sdma_event_e82_hw_unfreeze
:
2653 case sdma_event_e85_link_down
:
2655 case sdma_event_e90_sw_halted
:
2660 case sdma_state_s20_idle
:
2662 case sdma_event_e00_go_hw_down
:
2663 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2664 sdma_sw_tear_down(sde
);
2666 case sdma_event_e10_go_hw_start
:
2668 case sdma_event_e15_hw_halt_done
:
2670 case sdma_event_e25_hw_clean_up_done
:
2672 case sdma_event_e30_go_running
:
2673 sdma_set_state(sde
, sdma_state_s99_running
);
2674 ss
->go_s99_running
= 1;
2676 case sdma_event_e40_sw_cleaned
:
2678 case sdma_event_e50_hw_cleaned
:
2680 case sdma_event_e60_hw_halted
:
2681 sdma_set_state(sde
, sdma_state_s50_hw_halt_wait
);
2682 schedule_work(&sde
->err_halt_worker
);
2684 case sdma_event_e70_go_idle
:
2686 case sdma_event_e85_link_down
:
2687 case sdma_event_e80_hw_freeze
:
2688 sdma_set_state(sde
, sdma_state_s80_hw_freeze
);
2689 atomic_dec(&sde
->dd
->sdma_unfreeze_count
);
2690 wake_up_interruptible(&sde
->dd
->sdma_unfreeze_wq
);
2692 case sdma_event_e81_hw_frozen
:
2694 case sdma_event_e82_hw_unfreeze
:
2696 case sdma_event_e90_sw_halted
:
2701 case sdma_state_s30_sw_clean_up_wait
:
2703 case sdma_event_e00_go_hw_down
:
2704 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2706 case sdma_event_e10_go_hw_start
:
2708 case sdma_event_e15_hw_halt_done
:
2710 case sdma_event_e25_hw_clean_up_done
:
2712 case sdma_event_e30_go_running
:
2713 ss
->go_s99_running
= 1;
2715 case sdma_event_e40_sw_cleaned
:
2716 sdma_set_state(sde
, sdma_state_s40_hw_clean_up_wait
);
2717 sdma_start_hw_clean_up(sde
);
2719 case sdma_event_e50_hw_cleaned
:
2721 case sdma_event_e60_hw_halted
:
2723 case sdma_event_e70_go_idle
:
2724 ss
->go_s99_running
= 0;
2726 case sdma_event_e80_hw_freeze
:
2728 case sdma_event_e81_hw_frozen
:
2730 case sdma_event_e82_hw_unfreeze
:
2732 case sdma_event_e85_link_down
:
2733 ss
->go_s99_running
= 0;
2735 case sdma_event_e90_sw_halted
:
2740 case sdma_state_s40_hw_clean_up_wait
:
2742 case sdma_event_e00_go_hw_down
:
2743 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2744 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2746 case sdma_event_e10_go_hw_start
:
2748 case sdma_event_e15_hw_halt_done
:
2750 case sdma_event_e25_hw_clean_up_done
:
2751 sdma_hw_start_up(sde
);
2752 sdma_set_state(sde
, ss
->go_s99_running
?
2753 sdma_state_s99_running
:
2754 sdma_state_s20_idle
);
2756 case sdma_event_e30_go_running
:
2757 ss
->go_s99_running
= 1;
2759 case sdma_event_e40_sw_cleaned
:
2761 case sdma_event_e50_hw_cleaned
:
2763 case sdma_event_e60_hw_halted
:
2765 case sdma_event_e70_go_idle
:
2766 ss
->go_s99_running
= 0;
2768 case sdma_event_e80_hw_freeze
:
2770 case sdma_event_e81_hw_frozen
:
2772 case sdma_event_e82_hw_unfreeze
:
2774 case sdma_event_e85_link_down
:
2775 ss
->go_s99_running
= 0;
2777 case sdma_event_e90_sw_halted
:
2782 case sdma_state_s50_hw_halt_wait
:
2784 case sdma_event_e00_go_hw_down
:
2785 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2786 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2788 case sdma_event_e10_go_hw_start
:
2790 case sdma_event_e15_hw_halt_done
:
2791 sdma_set_state(sde
, sdma_state_s30_sw_clean_up_wait
);
2792 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2794 case sdma_event_e25_hw_clean_up_done
:
2796 case sdma_event_e30_go_running
:
2797 ss
->go_s99_running
= 1;
2799 case sdma_event_e40_sw_cleaned
:
2801 case sdma_event_e50_hw_cleaned
:
2803 case sdma_event_e60_hw_halted
:
2804 schedule_work(&sde
->err_halt_worker
);
2806 case sdma_event_e70_go_idle
:
2807 ss
->go_s99_running
= 0;
2809 case sdma_event_e80_hw_freeze
:
2811 case sdma_event_e81_hw_frozen
:
2813 case sdma_event_e82_hw_unfreeze
:
2815 case sdma_event_e85_link_down
:
2816 ss
->go_s99_running
= 0;
2818 case sdma_event_e90_sw_halted
:
2823 case sdma_state_s60_idle_halt_wait
:
2825 case sdma_event_e00_go_hw_down
:
2826 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2827 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2829 case sdma_event_e10_go_hw_start
:
2831 case sdma_event_e15_hw_halt_done
:
2832 sdma_set_state(sde
, sdma_state_s30_sw_clean_up_wait
);
2833 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2835 case sdma_event_e25_hw_clean_up_done
:
2837 case sdma_event_e30_go_running
:
2838 ss
->go_s99_running
= 1;
2840 case sdma_event_e40_sw_cleaned
:
2842 case sdma_event_e50_hw_cleaned
:
2844 case sdma_event_e60_hw_halted
:
2845 schedule_work(&sde
->err_halt_worker
);
2847 case sdma_event_e70_go_idle
:
2848 ss
->go_s99_running
= 0;
2850 case sdma_event_e80_hw_freeze
:
2852 case sdma_event_e81_hw_frozen
:
2854 case sdma_event_e82_hw_unfreeze
:
2856 case sdma_event_e85_link_down
:
2858 case sdma_event_e90_sw_halted
:
2863 case sdma_state_s80_hw_freeze
:
2865 case sdma_event_e00_go_hw_down
:
2866 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2867 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2869 case sdma_event_e10_go_hw_start
:
2871 case sdma_event_e15_hw_halt_done
:
2873 case sdma_event_e25_hw_clean_up_done
:
2875 case sdma_event_e30_go_running
:
2876 ss
->go_s99_running
= 1;
2878 case sdma_event_e40_sw_cleaned
:
2880 case sdma_event_e50_hw_cleaned
:
2882 case sdma_event_e60_hw_halted
:
2884 case sdma_event_e70_go_idle
:
2885 ss
->go_s99_running
= 0;
2887 case sdma_event_e80_hw_freeze
:
2889 case sdma_event_e81_hw_frozen
:
2890 sdma_set_state(sde
, sdma_state_s82_freeze_sw_clean
);
2891 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2893 case sdma_event_e82_hw_unfreeze
:
2895 case sdma_event_e85_link_down
:
2897 case sdma_event_e90_sw_halted
:
2902 case sdma_state_s82_freeze_sw_clean
:
2904 case sdma_event_e00_go_hw_down
:
2905 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2906 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2908 case sdma_event_e10_go_hw_start
:
2910 case sdma_event_e15_hw_halt_done
:
2912 case sdma_event_e25_hw_clean_up_done
:
2914 case sdma_event_e30_go_running
:
2915 ss
->go_s99_running
= 1;
2917 case sdma_event_e40_sw_cleaned
:
2918 /* notify caller this engine is done cleaning */
2919 atomic_dec(&sde
->dd
->sdma_unfreeze_count
);
2920 wake_up_interruptible(&sde
->dd
->sdma_unfreeze_wq
);
2922 case sdma_event_e50_hw_cleaned
:
2924 case sdma_event_e60_hw_halted
:
2926 case sdma_event_e70_go_idle
:
2927 ss
->go_s99_running
= 0;
2929 case sdma_event_e80_hw_freeze
:
2931 case sdma_event_e81_hw_frozen
:
2933 case sdma_event_e82_hw_unfreeze
:
2934 sdma_hw_start_up(sde
);
2935 sdma_set_state(sde
, ss
->go_s99_running
?
2936 sdma_state_s99_running
:
2937 sdma_state_s20_idle
);
2939 case sdma_event_e85_link_down
:
2941 case sdma_event_e90_sw_halted
:
2946 case sdma_state_s99_running
:
2948 case sdma_event_e00_go_hw_down
:
2949 sdma_set_state(sde
, sdma_state_s00_hw_down
);
2950 tasklet_hi_schedule(&sde
->sdma_sw_clean_up_task
);
2952 case sdma_event_e10_go_hw_start
:
2954 case sdma_event_e15_hw_halt_done
:
2956 case sdma_event_e25_hw_clean_up_done
:
2958 case sdma_event_e30_go_running
:
2960 case sdma_event_e40_sw_cleaned
:
2962 case sdma_event_e50_hw_cleaned
:
2964 case sdma_event_e60_hw_halted
:
2966 sdma_err_progress_check_schedule(sde
);
2968 case sdma_event_e90_sw_halted
:
2970 * SW initiated halt does not perform engines
2973 sdma_set_state(sde
, sdma_state_s50_hw_halt_wait
);
2974 schedule_work(&sde
->err_halt_worker
);
2976 case sdma_event_e70_go_idle
:
2977 sdma_set_state(sde
, sdma_state_s60_idle_halt_wait
);
2979 case sdma_event_e85_link_down
:
2980 ss
->go_s99_running
= 0;
2982 case sdma_event_e80_hw_freeze
:
2983 sdma_set_state(sde
, sdma_state_s80_hw_freeze
);
2984 atomic_dec(&sde
->dd
->sdma_unfreeze_count
);
2985 wake_up_interruptible(&sde
->dd
->sdma_unfreeze_wq
);
2987 case sdma_event_e81_hw_frozen
:
2989 case sdma_event_e82_hw_unfreeze
:
2995 ss
->last_event
= event
;
2997 sdma_make_progress(sde
, 0);
3001 * _extend_sdma_tx_descs() - helper to extend txreq
3003 * This is called once the initial nominal allocation
3004 * of descriptors in the sdma_txreq is exhausted.
3006 * The code will bump the allocation up to the max
3007 * of MAX_DESC (64) descriptors. There doesn't seem
3008 * much point in an interim step. The last descriptor
3009 * is reserved for coalesce buffer in order to support
3010 * cases where input packet has >MAX_DESC iovecs.
3013 static int _extend_sdma_tx_descs(struct hfi1_devdata
*dd
, struct sdma_txreq
*tx
)
3016 struct sdma_desc
*descp
;
3018 /* Handle last descriptor */
3019 if (unlikely((tx
->num_desc
== (MAX_DESC
- 1)))) {
3020 /* if tlen is 0, it is for padding, release last descriptor */
3022 tx
->desc_limit
= MAX_DESC
;
3023 } else if (!tx
->coalesce_buf
) {
3024 /* allocate coalesce buffer with space for padding */
3025 tx
->coalesce_buf
= kmalloc(tx
->tlen
+ sizeof(u32
),
3027 if (!tx
->coalesce_buf
)
3029 tx
->coalesce_idx
= 0;
3034 if (unlikely(tx
->num_desc
== MAX_DESC
))
3037 descp
= kmalloc_array(MAX_DESC
, sizeof(struct sdma_desc
), GFP_ATOMIC
);
3042 /* reserve last descriptor for coalescing */
3043 tx
->desc_limit
= MAX_DESC
- 1;
3044 /* copy ones already built */
3045 for (i
= 0; i
< tx
->num_desc
; i
++)
3046 tx
->descp
[i
] = tx
->descs
[i
];
3049 __sdma_txclean(dd
, tx
);
3054 * ext_coal_sdma_tx_descs() - extend or coalesce sdma tx descriptors
3056 * This is called once the initial nominal allocation of descriptors
3057 * in the sdma_txreq is exhausted.
3059 * This function calls _extend_sdma_tx_descs to extend or allocate
3060 * coalesce buffer. If there is a allocated coalesce buffer, it will
3061 * copy the input packet data into the coalesce buffer. It also adds
3062 * coalesce buffer descriptor once when whole packet is received.
3066 * 0 - coalescing, don't populate descriptor
3067 * 1 - continue with populating descriptor
3069 int ext_coal_sdma_tx_descs(struct hfi1_devdata
*dd
, struct sdma_txreq
*tx
,
3070 int type
, void *kvaddr
, struct page
*page
,
3071 unsigned long offset
, u16 len
)
3076 rval
= _extend_sdma_tx_descs(dd
, tx
);
3078 __sdma_txclean(dd
, tx
);
3082 /* If coalesce buffer is allocated, copy data into it */
3083 if (tx
->coalesce_buf
) {
3084 if (type
== SDMA_MAP_NONE
) {
3085 __sdma_txclean(dd
, tx
);
3089 if (type
== SDMA_MAP_PAGE
) {
3090 kvaddr
= kmap_local_page(page
);
3092 } else if (WARN_ON(!kvaddr
)) {
3093 __sdma_txclean(dd
, tx
);
3097 memcpy(tx
->coalesce_buf
+ tx
->coalesce_idx
, kvaddr
, len
);
3098 tx
->coalesce_idx
+= len
;
3099 if (type
== SDMA_MAP_PAGE
)
3100 kunmap_local(kvaddr
);
3102 /* If there is more data, return */
3103 if (tx
->tlen
- tx
->coalesce_idx
)
3106 /* Whole packet is received; add any padding */
3107 pad_len
= tx
->packet_len
& (sizeof(u32
) - 1);
3109 pad_len
= sizeof(u32
) - pad_len
;
3110 memset(tx
->coalesce_buf
+ tx
->coalesce_idx
, 0, pad_len
);
3111 /* padding is taken care of for coalescing case */
3112 tx
->packet_len
+= pad_len
;
3113 tx
->tlen
+= pad_len
;
3116 /* dma map the coalesce buffer */
3117 addr
= dma_map_single(&dd
->pcidev
->dev
,
3122 if (unlikely(dma_mapping_error(&dd
->pcidev
->dev
, addr
))) {
3123 __sdma_txclean(dd
, tx
);
3127 /* Add descriptor for coalesce buffer */
3128 tx
->desc_limit
= MAX_DESC
;
3129 return _sdma_txadd_daddr(dd
, SDMA_MAP_SINGLE
, tx
,
3130 addr
, tx
->tlen
, NULL
, NULL
, NULL
);
3136 /* Update sdes when the lmc changes */
3137 void sdma_update_lmc(struct hfi1_devdata
*dd
, u64 mask
, u32 lid
)
3139 struct sdma_engine
*sde
;
3143 sreg
= ((mask
& SD(CHECK_SLID_MASK_MASK
)) <<
3144 SD(CHECK_SLID_MASK_SHIFT
)) |
3145 (((lid
& mask
) & SD(CHECK_SLID_VALUE_MASK
)) <<
3146 SD(CHECK_SLID_VALUE_SHIFT
));
3148 for (i
= 0; i
< dd
->num_sdma
; i
++) {
3149 hfi1_cdbg(LINKVERB
, "SendDmaEngine[%d].SLID_CHECK = 0x%x",
3151 sde
= &dd
->per_sdma
[i
];
3152 write_sde_csr(sde
, SD(CHECK_SLID
), sreg
);
3156 /* tx not dword sized - pad */
3157 int _pad_sdma_tx_descs(struct hfi1_devdata
*dd
, struct sdma_txreq
*tx
)
3161 if ((unlikely(tx
->num_desc
== tx
->desc_limit
))) {
3162 rval
= _extend_sdma_tx_descs(dd
, tx
);
3164 __sdma_txclean(dd
, tx
);
3169 /* finish the one just added */
3174 sizeof(u32
) - (tx
->packet_len
& (sizeof(u32
) - 1)),
3177 _sdma_close_tx(dd
, tx
);
3182 * Add ahg to the sdma_txreq
3184 * The logic will consume up to 3
3185 * descriptors at the beginning of
3188 void _sdma_txreq_ahgadd(
3189 struct sdma_txreq
*tx
,
3195 u32 i
, shift
= 0, desc
= 0;
3198 WARN_ON_ONCE(num_ahg
> 9 || (ahg_hlen
& 3) || ahg_hlen
== 4);
3201 mode
= SDMA_AHG_APPLY_UPDATE1
;
3202 else if (num_ahg
<= 5)
3203 mode
= SDMA_AHG_APPLY_UPDATE2
;
3205 mode
= SDMA_AHG_APPLY_UPDATE3
;
3207 /* initialize to consumed descriptors to zero */
3209 case SDMA_AHG_APPLY_UPDATE3
:
3211 tx
->descs
[2].qw
[0] = 0;
3212 tx
->descs
[2].qw
[1] = 0;
3214 case SDMA_AHG_APPLY_UPDATE2
:
3216 tx
->descs
[1].qw
[0] = 0;
3217 tx
->descs
[1].qw
[1] = 0;
3221 tx
->descs
[0].qw
[1] |=
3222 (((u64
)ahg_entry
& SDMA_DESC1_HEADER_INDEX_MASK
)
3223 << SDMA_DESC1_HEADER_INDEX_SHIFT
) |
3224 (((u64
)ahg_hlen
& SDMA_DESC1_HEADER_DWS_MASK
)
3225 << SDMA_DESC1_HEADER_DWS_SHIFT
) |
3226 (((u64
)mode
& SDMA_DESC1_HEADER_MODE_MASK
)
3227 << SDMA_DESC1_HEADER_MODE_SHIFT
) |
3228 (((u64
)ahg
[0] & SDMA_DESC1_HEADER_UPDATE1_MASK
)
3229 << SDMA_DESC1_HEADER_UPDATE1_SHIFT
);
3230 for (i
= 0; i
< (num_ahg
- 1); i
++) {
3231 if (!shift
&& !(i
& 2))
3233 tx
->descs
[desc
].qw
[!!(i
& 2)] |=
3236 shift
= (shift
+ 32) & 63;
3241 * sdma_ahg_alloc - allocate an AHG entry
3242 * @sde: engine to allocate from
3245 * 0-31 when successful, -EOPNOTSUPP if AHG is not enabled,
3246 * -ENOSPC if an entry is not available
3248 int sdma_ahg_alloc(struct sdma_engine
*sde
)
3254 trace_hfi1_ahg_allocate(sde
, -EINVAL
);
3258 nr
= ffz(READ_ONCE(sde
->ahg_bits
));
3260 trace_hfi1_ahg_allocate(sde
, -ENOSPC
);
3263 oldbit
= test_and_set_bit(nr
, &sde
->ahg_bits
);
3268 trace_hfi1_ahg_allocate(sde
, nr
);
3273 * sdma_ahg_free - free an AHG entry
3274 * @sde: engine to return AHG entry
3275 * @ahg_index: index to free
3277 * This routine frees the indicate AHG entry.
3279 void sdma_ahg_free(struct sdma_engine
*sde
, int ahg_index
)
3283 trace_hfi1_ahg_deallocate(sde
, ahg_index
);
3284 if (ahg_index
< 0 || ahg_index
> 31)
3286 clear_bit(ahg_index
, &sde
->ahg_bits
);
3290 * SPC freeze handling for SDMA engines. Called when the driver knows
3291 * the SPC is going into a freeze but before the freeze is fully
3292 * settled. Generally an error interrupt.
3294 * This event will pull the engine out of running so no more entries can be
3295 * added to the engine's queue.
3297 void sdma_freeze_notify(struct hfi1_devdata
*dd
, int link_down
)
3300 enum sdma_events event
= link_down
? sdma_event_e85_link_down
:
3301 sdma_event_e80_hw_freeze
;
3303 /* set up the wait but do not wait here */
3304 atomic_set(&dd
->sdma_unfreeze_count
, dd
->num_sdma
);
3306 /* tell all engines to stop running and wait */
3307 for (i
= 0; i
< dd
->num_sdma
; i
++)
3308 sdma_process_event(&dd
->per_sdma
[i
], event
);
3310 /* sdma_freeze() will wait for all engines to have stopped */
3314 * SPC freeze handling for SDMA engines. Called when the driver knows
3315 * the SPC is fully frozen.
3317 void sdma_freeze(struct hfi1_devdata
*dd
)
3323 * Make sure all engines have moved out of the running state before
3326 ret
= wait_event_interruptible(dd
->sdma_unfreeze_wq
,
3327 atomic_read(&dd
->sdma_unfreeze_count
) <=
3329 /* interrupted or count is negative, then unloading - just exit */
3330 if (ret
|| atomic_read(&dd
->sdma_unfreeze_count
) < 0)
3333 /* set up the count for the next wait */
3334 atomic_set(&dd
->sdma_unfreeze_count
, dd
->num_sdma
);
3336 /* tell all engines that the SPC is frozen, they can start cleaning */
3337 for (i
= 0; i
< dd
->num_sdma
; i
++)
3338 sdma_process_event(&dd
->per_sdma
[i
], sdma_event_e81_hw_frozen
);
3341 * Wait for everyone to finish software clean before exiting. The
3342 * software clean will read engine CSRs, so must be completed before
3343 * the next step, which will clear the engine CSRs.
3345 (void)wait_event_interruptible(dd
->sdma_unfreeze_wq
,
3346 atomic_read(&dd
->sdma_unfreeze_count
) <= 0);
3347 /* no need to check results - done no matter what */
3351 * SPC freeze handling for the SDMA engines. Called after the SPC is unfrozen.
3353 * The SPC freeze acts like a SDMA halt and a hardware clean combined. All
3354 * that is left is a software clean. We could do it after the SPC is fully
3355 * frozen, but then we'd have to add another state to wait for the unfreeze.
3356 * Instead, just defer the software clean until the unfreeze step.
3358 void sdma_unfreeze(struct hfi1_devdata
*dd
)
3362 /* tell all engines start freeze clean up */
3363 for (i
= 0; i
< dd
->num_sdma
; i
++)
3364 sdma_process_event(&dd
->per_sdma
[i
],
3365 sdma_event_e82_hw_unfreeze
);
3369 * _sdma_engine_progress_schedule() - schedule progress on engine
3370 * @sde: sdma_engine to schedule progress
3373 void _sdma_engine_progress_schedule(
3374 struct sdma_engine
*sde
)
3376 trace_hfi1_sdma_engine_progress(sde
, sde
->progress_mask
);
3377 /* assume we have selected a good cpu */
3379 CCE_INT_FORCE
+ (8 * (IS_SDMA_START
/ 64)),
3380 sde
->progress_mask
);