New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / net / ark / ark_ddm.c
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright (c) 2015-2017 Atomic Rules LLC
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35
36 #include "ark_logs.h"
37 #include "ark_ddm.h"
38
39 /* ************************************************************************* */
40 int
41 ark_ddm_verify(struct ark_ddm_t *ddm)
42 {
43         if (sizeof(struct ark_ddm_t) != ARK_DDM_EXPECTED_SIZE) {
44                 PMD_DRV_LOG(ERR, "ARK: DDM structure looks incorrect %d vs %zd\n",
45                             ARK_DDM_EXPECTED_SIZE, sizeof(struct ark_ddm_t));
46                 return -1;
47         }
48
49         if (ddm->cfg.const0 != ARK_DDM_CONST) {
50                 PMD_DRV_LOG(ERR, "ARK: DDM module not found as expected 0x%08x\n",
51                             ddm->cfg.const0);
52                 return -1;
53         }
54         return 0;
55 }
56
57 void
58 ark_ddm_start(struct ark_ddm_t *ddm)
59 {
60         ddm->cfg.command = 1;
61 }
62
63 int
64 ark_ddm_stop(struct ark_ddm_t *ddm, const int wait)
65 {
66         int cnt = 0;
67
68         ddm->cfg.command = 2;
69         while (wait && (ddm->cfg.stop_flushed & 0x01) == 0) {
70                 if (cnt++ > 1000)
71                         return 1;
72
73                 usleep(10);
74         }
75         return 0;
76 }
77
78 void
79 ark_ddm_reset(struct ark_ddm_t *ddm)
80 {
81         int status;
82
83         /* reset only works if ddm has stopped properly. */
84         status = ark_ddm_stop(ddm, 1);
85
86         if (status != 0) {
87                 PMD_DEBUG_LOG(INFO, "%s  stop failed  doing forced reset\n",
88                               __func__);
89                 ddm->cfg.command = 4;
90                 usleep(10);
91         }
92         ddm->cfg.command = 3;
93 }
94
95 void
96 ark_ddm_setup(struct ark_ddm_t *ddm, rte_iova_t cons_addr, uint32_t interval)
97 {
98         ddm->setup.cons_write_index_addr = cons_addr;
99         ddm->setup.write_index_interval = interval / 4; /* 4 ns period */
100 }
101
102 void
103 ark_ddm_stats_reset(struct ark_ddm_t *ddm)
104 {
105         ddm->cfg.tlp_stats_clear = 1;
106 }
107
108 void
109 ark_ddm_dump(struct ark_ddm_t *ddm, const char *msg)
110 {
111         PMD_FUNC_LOG(DEBUG, "%s Stopped: %d\n", msg,
112                      ark_ddm_is_stopped(ddm)
113                      );
114 }
115
116 void
117 ark_ddm_dump_stats(struct ark_ddm_t *ddm, const char *msg)
118 {
119         struct ark_ddm_stats_t *stats = &ddm->stats;
120
121         PMD_STATS_LOG(INFO, "DDM Stats: %s"
122                       ARK_SU64 ARK_SU64 ARK_SU64
123                       "\n", msg,
124                       "Bytes:", stats->tx_byte_count,
125                       "Packets:", stats->tx_pkt_count,
126                       "MBufs", stats->tx_mbuf_count);
127 }
128
129 int
130 ark_ddm_is_stopped(struct ark_ddm_t *ddm)
131 {
132         return (ddm->cfg.stop_flushed & 0x01) != 0;
133 }
134
135 uint64_t
136 ark_ddm_queue_byte_count(struct ark_ddm_t *ddm)
137 {
138         return ddm->queue_stats.byte_count;
139 }
140
141 uint64_t
142 ark_ddm_queue_pkt_count(struct ark_ddm_t *ddm)
143 {
144         return ddm->queue_stats.pkt_count;
145 }
146
147 void
148 ark_ddm_queue_reset_stats(struct ark_ddm_t *ddm)
149 {
150         ddm->queue_stats.byte_count = 1;
151 }