#include "mad.h"
 #include "sdma.h"
 #include "ipoib.h"
+#include "user_sdma.h"
 
 const char *parse_sdma_flags(struct trace_seq *p, u64 desc0, u64 desc1);
 
                      __entry->code)
 );
 
+TRACE_EVENT(hfi1_usdma_defer,
+           TP_PROTO(struct hfi1_user_sdma_pkt_q *pq,
+                    struct sdma_engine *sde,
+                    struct iowait *wait),
+           TP_ARGS(pq, sde, wait),
+           TP_STRUCT__entry(DD_DEV_ENTRY(pq->dd)
+                            __field(struct hfi1_user_sdma_pkt_q *, pq)
+                            __field(struct sdma_engine *, sde)
+                            __field(struct iowait *, wait)
+                            __field(int, engine)
+                            __field(int, empty)
+                            ),
+            TP_fast_assign(DD_DEV_ASSIGN(pq->dd);
+                           __entry->pq = pq;
+                           __entry->sde = sde;
+                           __entry->wait = wait;
+                           __entry->engine = sde->this_idx;
+                           __entry->empty = list_empty(&__entry->wait->list);
+                           ),
+            TP_printk("[%s] pq %llx sde %llx wait %llx engine %d empty %d",
+                      __get_str(dev),
+                      (unsigned long long)__entry->pq,
+                      (unsigned long long)__entry->sde,
+                      (unsigned long long)__entry->wait,
+                      __entry->engine,
+                      __entry->empty
+               )
+);
+
+TRACE_EVENT(hfi1_usdma_activate,
+           TP_PROTO(struct hfi1_user_sdma_pkt_q *pq,
+                    struct iowait *wait,
+                    int reason),
+           TP_ARGS(pq, wait, reason),
+           TP_STRUCT__entry(DD_DEV_ENTRY(pq->dd)
+                            __field(struct hfi1_user_sdma_pkt_q *, pq)
+                            __field(struct iowait *, wait)
+                            __field(int, reason)
+                            ),
+            TP_fast_assign(DD_DEV_ASSIGN(pq->dd);
+                           __entry->pq = pq;
+                           __entry->wait = wait;
+                           __entry->reason = reason;
+                           ),
+            TP_printk("[%s] pq %llx wait %llx reason %d",
+                      __get_str(dev),
+                      (unsigned long long)__entry->pq,
+                      (unsigned long long)__entry->wait,
+                      __entry->reason
+               )
+);
+
+TRACE_EVENT(hfi1_usdma_we,
+           TP_PROTO(struct hfi1_user_sdma_pkt_q *pq,
+                    int we_ret),
+           TP_ARGS(pq, we_ret),
+           TP_STRUCT__entry(DD_DEV_ENTRY(pq->dd)
+                            __field(struct hfi1_user_sdma_pkt_q *, pq)
+                            __field(int, state)
+                            __field(int, we_ret)
+                            ),
+            TP_fast_assign(DD_DEV_ASSIGN(pq->dd);
+                           __entry->pq = pq;
+                           __entry->state = pq->state;
+                           __entry->we_ret = we_ret;
+                           ),
+            TP_printk("[%s] pq %llx state %d we_ret %d",
+                      __get_str(dev),
+                      (unsigned long long)__entry->pq,
+                      __entry->state,
+                      __entry->we_ret
+               )
+);
+
 const char *print_u32_array(struct trace_seq *, u32 *, int);
 #define __print_u32_hex(arr, len) print_u32_array(p, arr, len)
 
 
                container_of(wait->iow, struct hfi1_user_sdma_pkt_q, busy);
 
        write_seqlock(&sde->waitlock);
+       trace_hfi1_usdma_defer(pq, sde, &pq->busy);
        if (sdma_progress(sde, seq, txreq))
                goto eagain;
        /*
 {
        struct hfi1_user_sdma_pkt_q *pq =
                container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
-       pq->busy.lock = NULL;
+
+       trace_hfi1_usdma_activate(pq, wait, reason);
        xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
        wake_up(&wait->wait_dma);
 };
        while (req->seqsubmitted != req->info.npkts) {
                ret = user_sdma_send_pkts(req, pcount);
                if (ret < 0) {
+                       int we_ret;
+
                        if (ret != -EBUSY)
                                goto free_req;
-                       if (wait_event_interruptible_timeout(
+                       we_ret = wait_event_interruptible_timeout(
                                pq->busy.wait_dma,
                                pq->state == SDMA_PKT_Q_ACTIVE,
                                msecs_to_jiffies(
-                                       SDMA_IOWAIT_TIMEOUT)) <= 0)
+                                       SDMA_IOWAIT_TIMEOUT));
+                       trace_hfi1_usdma_we(pq, we_ret);
+                       if (we_ret <= 0)
                                flush_pq_iowait(pq);
                }
        }