net: ethernet: ti: cpts: optimize packet to event matching
[linux-2.6-microblaze.git] / drivers / net / ethernet / ti / cpts.c
index 729ce09..6efb809 100644 (file)
 #define CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
 
 struct cpts_skb_cb_data {
+       u32 skb_mtype_seqid;
        unsigned long tmo;
 };
 
 #define cpts_read32(c, r)      readl_relaxed(&c->reg->r)
 #define cpts_write32(c, v, r)  writel_relaxed(v, &c->reg->r)
 
-static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
-                     u16 ts_seqid, u8 ts_msgtype);
-
 static int event_expired(struct cpts_event *event)
 {
        return time_after(jiffies, event->tmo);
@@ -71,7 +69,7 @@ static int cpts_purge_events(struct cpts *cpts)
        }
 
        if (removed)
-               pr_debug("cpts: event pool cleaned up %d\n", removed);
+               dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed);
        return removed ? 0 : -1;
 }
 
@@ -97,31 +95,29 @@ static void cpts_purge_txq(struct cpts *cpts)
 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
 {
        struct sk_buff *skb, *tmp;
-       u16 seqid;
-       u8 mtype;
        bool found = false;
+       u32 mtype_seqid;
 
-       mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
-       seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
+       mtype_seqid = event->high &
+                     ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
+                      (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
+                      (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
 
        /* no need to grab txq.lock as access is always done under cpts->lock */
        skb_queue_walk_safe(&cpts->txq, skb, tmp) {
                struct skb_shared_hwtstamps ssh;
-               unsigned int class = ptp_classify_raw(skb);
                struct cpts_skb_cb_data *skb_cb =
                                        (struct cpts_skb_cb_data *)skb->cb;
 
-               if (cpts_match(skb, class, seqid, mtype)) {
-                       u64 ns = timecounter_cyc2time(&cpts->tc, event->low);
-
+               if (mtype_seqid == skb_cb->skb_mtype_seqid) {
                        memset(&ssh, 0, sizeof(ssh));
-                       ssh.hwtstamp = ns_to_ktime(ns);
+                       ssh.hwtstamp = ns_to_ktime(event->timestamp);
                        skb_tstamp_tx(skb, &ssh);
                        found = true;
                        __skb_unlink(skb, &cpts->txq);
                        dev_consume_skb_any(skb);
-                       dev_dbg(cpts->dev, "match tx timestamp mtype %u seqid %04x\n",
-                               mtypeseqid);
+                       dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n",
+                               mtype_seqid);
                        break;
                }
 
@@ -150,7 +146,7 @@ static int cpts_fifo_read(struct cpts *cpts, int match)
                        break;
 
                if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
-                       pr_err("cpts: event pool empty\n");
+                       dev_warn(cpts->dev, "cpts: event pool empty\n");
                        return -1;
                }
 
@@ -158,8 +154,20 @@ static int cpts_fifo_read(struct cpts *cpts, int match)
                event->tmo = jiffies + 2;
                event->high = hi;
                event->low = lo;
+               event->timestamp = timecounter_cyc2time(&cpts->tc, event->low);
                type = event_type(event);
+
+               dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n",
+                       type, event->high, event->low);
                switch (type) {
+               case CPTS_EV_PUSH:
+                       WRITE_ONCE(cpts->cur_timestamp, lo);
+                       timecounter_read(&cpts->tc);
+                       if (cpts->mult_new) {
+                               cpts->cc.mult = cpts->mult_new;
+                               cpts->mult_new = 0;
+                       }
+                       break;
                case CPTS_EV_TX:
                        if (cpts_match_tx_ts(cpts, event)) {
                                /* if the new event matches an existing skb,
@@ -168,7 +176,6 @@ static int cpts_fifo_read(struct cpts *cpts, int match)
                                break;
                        }
                        /* fall through */
-               case CPTS_EV_PUSH:
                case CPTS_EV_RX:
                        list_del_init(&event->list);
                        list_add_tail(&event->list, &cpts->events);
@@ -178,7 +185,7 @@ static int cpts_fifo_read(struct cpts *cpts, int match)
                case CPTS_EV_HW:
                        break;
                default:
-                       pr_err("cpts: unknown event type\n");
+                       dev_err(cpts->dev, "cpts: unknown event type\n");
                        break;
                }
                if (type == match)
@@ -189,26 +196,21 @@ static int cpts_fifo_read(struct cpts *cpts, int match)
 
 static u64 cpts_systim_read(const struct cyclecounter *cc)
 {
-       u64 val = 0;
-       struct cpts_event *event;
-       struct list_head *this, *next;
        struct cpts *cpts = container_of(cc, struct cpts, cc);
 
-       cpts_write32(cpts, TS_PUSH, ts_push);
-       if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
-               pr_err("cpts: unable to obtain a time stamp\n");
+       return READ_ONCE(cpts->cur_timestamp);
+}
 
-       list_for_each_safe(this, next, &cpts->events) {
-               event = list_entry(this, struct cpts_event, list);
-               if (event_type(event) == CPTS_EV_PUSH) {
-                       list_del_init(&event->list);
-                       list_add(&event->list, &cpts->pool);
-                       val = event->low;
-                       break;
-               }
-       }
+static void cpts_update_cur_time(struct cpts *cpts, int match,
+                                struct ptp_system_timestamp *sts)
+{
+       ptp_read_system_prets(sts);
+       cpts_write32(cpts, TS_PUSH, ts_push);
+       cpts_read32(cpts, ts_push);
+       ptp_read_system_postts(sts);
 
-       return val;
+       if (cpts_fifo_read(cpts, match) && match != -1)
+               dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n");
 }
 
 /* PTP clock operations */
@@ -232,9 +234,9 @@ static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
 
        spin_lock_irqsave(&cpts->lock, flags);
 
-       timecounter_read(&cpts->tc);
+       cpts->mult_new = neg_adj ? mult - diff : mult + diff;
 
-       cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
+       cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL);
 
        spin_unlock_irqrestore(&cpts->lock, flags);
 
@@ -253,13 +255,18 @@ static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
        return 0;
 }
 
-static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
+static int cpts_ptp_gettimeex(struct ptp_clock_info *ptp,
+                             struct timespec64 *ts,
+                             struct ptp_system_timestamp *sts)
 {
-       u64 ns;
-       unsigned long flags;
        struct cpts *cpts = container_of(ptp, struct cpts, info);
+       unsigned long flags;
+       u64 ns;
 
        spin_lock_irqsave(&cpts->lock, flags);
+
+       cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts);
+
        ns = timecounter_read(&cpts->tc);
        spin_unlock_irqrestore(&cpts->lock, flags);
 
@@ -294,11 +301,14 @@ static long cpts_overflow_check(struct ptp_clock_info *ptp)
 {
        struct cpts *cpts = container_of(ptp, struct cpts, info);
        unsigned long delay = cpts->ov_check_period;
-       struct timespec64 ts;
        unsigned long flags;
+       u64 ns;
 
        spin_lock_irqsave(&cpts->lock, flags);
-       ts = ns_to_timespec64(timecounter_read(&cpts->tc));
+
+       cpts_update_cur_time(cpts, -1, NULL);
+
+       ns = timecounter_read(&cpts->tc);
 
        if (!skb_queue_empty(&cpts->txq)) {
                cpts_purge_txq(cpts);
@@ -307,8 +317,7 @@ static long cpts_overflow_check(struct ptp_clock_info *ptp)
        }
        spin_unlock_irqrestore(&cpts->lock, flags);
 
-       pr_debug("cpts overflow check at %lld.%09ld\n",
-                (long long)ts.tv_sec, ts.tv_nsec);
+       dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns);
        return (long)delay;
 }
 
@@ -321,18 +330,21 @@ static const struct ptp_clock_info cpts_info = {
        .pps            = 0,
        .adjfreq        = cpts_ptp_adjfreq,
        .adjtime        = cpts_ptp_adjtime,
-       .gettime64      = cpts_ptp_gettime,
+       .gettimex64     = cpts_ptp_gettimeex,
        .settime64      = cpts_ptp_settime,
        .enable         = cpts_ptp_enable,
        .do_aux_work    = cpts_overflow_check,
 };
 
-static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
-                     u16 ts_seqid, u8 ts_msgtype)
+static int cpts_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
 {
-       u16 *seqid;
-       unsigned int offset = 0;
+       unsigned int ptp_class = ptp_classify_raw(skb);
        u8 *msgtype, *data = skb->data;
+       unsigned int offset = 0;
+       u16 *seqid;
+
+       if (ptp_class == PTP_CLASS_NONE)
+               return 0;
 
        if (ptp_class & PTP_CLASS_VLAN)
                offset += VLAN_HLEN;
@@ -360,22 +372,20 @@ static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
                msgtype = data + offset;
 
        seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
+       *mtype_seqid = (*msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
+       *mtype_seqid |= (ntohs(*seqid) & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
 
-       return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
+       return 1;
 }
 
-static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
+static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb,
+                       int ev_type, u32 skb_mtype_seqid)
 {
-       u64 ns = 0;
-       struct cpts_event *event;
        struct list_head *this, *next;
-       unsigned int class = ptp_classify_raw(skb);
+       struct cpts_event *event;
        unsigned long flags;
-       u16 seqid;
-       u8 mtype;
-
-       if (class == PTP_CLASS_NONE)
-               return 0;
+       u32 mtype_seqid;
+       u64 ns = 0;
 
        spin_lock_irqsave(&cpts->lock, flags);
        cpts_fifo_read(cpts, -1);
@@ -386,11 +396,14 @@ static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
                        list_add(&event->list, &cpts->pool);
                        continue;
                }
-               mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
-               seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
-               if (ev_type == event_type(event) &&
-                   cpts_match(skb, class, seqid, mtype)) {
-                       ns = timecounter_cyc2time(&cpts->tc, event->low);
+
+               mtype_seqid = event->high &
+                             ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
+                              (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
+                              (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
+
+               if (mtype_seqid == skb_mtype_seqid) {
+                       ns = event->timestamp;
                        list_del_init(&event->list);
                        list_add(&event->list, &cpts->pool);
                        break;
@@ -416,10 +429,21 @@ static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
 
 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
 {
-       u64 ns;
+       struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
        struct skb_shared_hwtstamps *ssh;
+       int ret;
+       u64 ns;
+
+       ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
+       if (!ret)
+               return;
 
-       ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
+       skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT);
+
+       dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
+               __func__, skb_cb->skb_mtype_seqid);
+
+       ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid);
        if (!ns)
                return;
        ssh = skb_hwtstamps(skb);
@@ -430,12 +454,24 @@ EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
 
 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
 {
-       u64 ns;
+       struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
        struct skb_shared_hwtstamps ssh;
+       int ret;
+       u64 ns;
 
        if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
                return;
-       ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
+
+       ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
+       if (!ret)
+               return;
+
+       skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT);
+
+       dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
+               __func__, skb_cb->skb_mtype_seqid);
+
+       ns = cpts_find_ts(cpts, skb, CPTS_EV_TX, skb_cb->skb_mtype_seqid);
        if (!ns)
                return;
        memset(&ssh, 0, sizeof(ssh));