New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / net / ark / ark_udm.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_udm.h"
38
39 int
40 ark_udm_verify(struct ark_udm_t *udm)
41 {
42         if (sizeof(struct ark_udm_t) != ARK_UDM_EXPECT_SIZE) {
43                 PMD_DRV_LOG(ERR,
44                             "ARK: UDM structure looks incorrect %d vs %zd\n",
45                             ARK_UDM_EXPECT_SIZE, sizeof(struct ark_udm_t));
46                 return -1;
47         }
48
49         if (udm->setup.const0 != ARK_UDM_CONST) {
50                 PMD_DRV_LOG(ERR,
51                             "ARK: UDM module not found as expected 0x%08x\n",
52                             udm->setup.const0);
53                 return -1;
54         }
55         return 0;
56 }
57
58 int
59 ark_udm_stop(struct ark_udm_t *udm, const int wait)
60 {
61         int cnt = 0;
62
63         udm->cfg.command = 2;
64
65         while (wait && (udm->cfg.stop_flushed & 0x01) == 0) {
66                 if (cnt++ > 1000)
67                         return 1;
68
69                 usleep(10);
70         }
71         return 0;
72 }
73
74 int
75 ark_udm_reset(struct ark_udm_t *udm)
76 {
77         int status;
78
79         status = ark_udm_stop(udm, 1);
80         if (status != 0) {
81                 PMD_DEBUG_LOG(INFO, "%s  stop failed  doing forced reset\n",
82                               __func__);
83                 udm->cfg.command = 4;
84                 usleep(10);
85                 udm->cfg.command = 3;
86                 status = ark_udm_stop(udm, 0);
87                 PMD_DEBUG_LOG(INFO, "%s  stop status %d post failure"
88                               " and forced reset\n",
89                               __func__, status);
90         } else {
91                 udm->cfg.command = 3;
92         }
93
94         return status;
95 }
96
97 void
98 ark_udm_start(struct ark_udm_t *udm)
99 {
100         udm->cfg.command = 1;
101 }
102
103 void
104 ark_udm_stats_reset(struct ark_udm_t *udm)
105 {
106         udm->pcibp.pci_clear = 1;
107         udm->tlp_ps.tlp_clear = 1;
108 }
109
110 void
111 ark_udm_configure(struct ark_udm_t *udm,
112                   uint32_t headroom,
113                   uint32_t dataroom,
114                   uint32_t write_interval_ns)
115 {
116         /* headroom and data room are in DWords in the UDM */
117         udm->cfg.dataroom = dataroom / 4;
118         udm->cfg.headroom = headroom / 4;
119
120         /* 4 NS period ns */
121         udm->rt_cfg.write_interval = write_interval_ns / 4;
122 }
123
124 void
125 ark_udm_write_addr(struct ark_udm_t *udm, rte_iova_t addr)
126 {
127         udm->rt_cfg.hw_prod_addr = addr;
128 }
129
130 int
131 ark_udm_is_flushed(struct ark_udm_t *udm)
132 {
133         return (udm->cfg.stop_flushed & 0x01) != 0;
134 }
135
136 uint64_t
137 ark_udm_dropped(struct ark_udm_t *udm)
138 {
139         return udm->qstats.q_pkt_drop;
140 }
141
142 uint64_t
143 ark_udm_bytes(struct ark_udm_t *udm)
144 {
145         return udm->qstats.q_byte_count;
146 }
147
148 uint64_t
149 ark_udm_packets(struct ark_udm_t *udm)
150 {
151         return udm->qstats.q_ff_packet_count;
152 }
153
154 void
155 ark_udm_dump_stats(struct ark_udm_t *udm, const char *msg)
156 {
157         PMD_STATS_LOG(INFO, "UDM Stats: %s"
158                       ARK_SU64 ARK_SU64 ARK_SU64 ARK_SU64 ARK_SU64 "\n",
159                       msg,
160                       "Pkts Received", udm->stats.rx_packet_count,
161                       "Pkts Finalized", udm->stats.rx_sent_packets,
162                       "Pkts Dropped", udm->tlp.pkt_drop,
163                       "Bytes Count", udm->stats.rx_byte_count,
164                       "MBuf Count", udm->stats.rx_mbuf_count);
165 }
166
167 void
168 ark_udm_dump_queue_stats(struct ark_udm_t *udm, const char *msg, uint16_t qid)
169 {
170         PMD_STATS_LOG(INFO, "UDM Queue %3u Stats: %s"
171                       ARK_SU64 ARK_SU64
172                       ARK_SU64 ARK_SU64
173                       ARK_SU64 "\n",
174                       qid, msg,
175                       "Pkts Received", udm->qstats.q_packet_count,
176                       "Pkts Finalized", udm->qstats.q_ff_packet_count,
177                       "Pkts Dropped", udm->qstats.q_pkt_drop,
178                       "Bytes Count", udm->qstats.q_byte_count,
179                       "MBuf Count", udm->qstats.q_mbuf_count);
180 }
181
182 void
183 ark_udm_dump(struct ark_udm_t *udm, const char *msg)
184 {
185         PMD_DEBUG_LOG(DEBUG, "UDM Dump: %s Stopped: %d\n", msg,
186                       udm->cfg.stop_flushed);
187 }
188
189 void
190 ark_udm_dump_setup(struct ark_udm_t *udm, uint16_t q_id)
191 {
192         PMD_DEBUG_LOG(DEBUG, "UDM Setup Q: %u"
193                       ARK_SU64X ARK_SU32 "\n",
194                       q_id,
195                       "hw_prod_addr", udm->rt_cfg.hw_prod_addr,
196                       "prod_idx", udm->rt_cfg.prod_idx);
197 }
198
199 void
200 ark_udm_dump_perf(struct ark_udm_t *udm, const char *msg)
201 {
202         struct ark_udm_pcibp_t *bp = &udm->pcibp;
203
204         PMD_STATS_LOG(INFO, "UDM Performance %s"
205                       ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32
206                       "\n",
207                       msg,
208                       "PCI Empty", bp->pci_empty,
209                       "PCI Q1", bp->pci_q1,
210                       "PCI Q2", bp->pci_q2,
211                       "PCI Q3", bp->pci_q3,
212                       "PCI Q4", bp->pci_q4,
213                       "PCI Full", bp->pci_full);
214 }
215
216 void
217 ark_udm_queue_stats_reset(struct ark_udm_t *udm)
218 {
219         udm->qstats.q_byte_count = 1;
220 }
221
222 void
223 ark_udm_queue_enable(struct ark_udm_t *udm, int enable)
224 {
225         udm->qstats.q_enable = enable ? 1 : 0;
226 }