net/ark: add memory write barriers in critical paths
[dpdk.git] / drivers / net / ark / ark_udm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2018 Atomic Rules LLC
3  */
4
5 #include <unistd.h>
6
7 #include "ark_logs.h"
8 #include "ark_udm.h"
9
10 static_assert(sizeof(struct ark_rx_meta) == 32, "Unexpected struct size ark_rx_meta");
11
12 int
13 ark_udm_verify(struct ark_udm_t *udm)
14 {
15         if (sizeof(struct ark_udm_t) != ARK_UDM_EXPECT_SIZE) {
16                 ARK_PMD_LOG(ERR,
17                             "ARK: UDM structure looks incorrect %d vs %zd\n",
18                             ARK_UDM_EXPECT_SIZE, sizeof(struct ark_udm_t));
19                 return -1;
20         }
21
22         if (udm->setup.const0 != ARK_UDM_CONST) {
23                 ARK_PMD_LOG(ERR,
24                             "ARK: UDM module not found as expected 0x%08x\n",
25                             udm->setup.const0);
26                 return -1;
27         }
28         return 0;
29 }
30
31 int
32 ark_udm_stop(struct ark_udm_t *udm, const int wait)
33 {
34         int cnt = 0;
35
36         udm->cfg.command = 2;
37         rte_wmb();
38
39         while (wait && (udm->cfg.stop_flushed & 0x01) == 0) {
40                 if (cnt++ > 1000)
41                         return 1;
42
43                 usleep(10);
44         }
45         return 0;
46 }
47
48 int
49 ark_udm_reset(struct ark_udm_t *udm)
50 {
51         int status;
52
53         status = ark_udm_stop(udm, 1);
54         if (status != 0) {
55                 ARK_PMD_LOG(NOTICE, "%s  stop failed  doing forced reset\n",
56                               __func__);
57                 udm->cfg.command = 4;
58                 usleep(10);
59                 udm->cfg.command = 3;
60                 status = ark_udm_stop(udm, 0);
61                 ARK_PMD_LOG(INFO, "%s  stop status %d post failure"
62                               " and forced reset\n",
63                               __func__, status);
64         } else {
65                 udm->cfg.command = 3;
66         }
67
68         return status;
69 }
70
71 void
72 ark_udm_start(struct ark_udm_t *udm)
73 {
74         udm->cfg.command = 1;
75 }
76
77 void
78 ark_udm_stats_reset(struct ark_udm_t *udm)
79 {
80         udm->pcibp.pci_clear = 1;
81         udm->tlp_ps.tlp_clear = 1;
82 }
83
84 void
85 ark_udm_configure(struct ark_udm_t *udm,
86                   uint32_t headroom,
87                   uint32_t dataroom,
88                   uint32_t write_interval_ns)
89 {
90         /* headroom and data room are in DWords in the UDM */
91         udm->cfg.dataroom = dataroom / 4;
92         udm->cfg.headroom = headroom / 4;
93
94         /* 4 NS period ns */
95         udm->rt_cfg.write_interval = write_interval_ns / 4;
96 }
97
98 void
99 ark_udm_write_addr(struct ark_udm_t *udm, rte_iova_t addr)
100 {
101         udm->rt_cfg.hw_prod_addr = addr;
102 }
103
104 int
105 ark_udm_is_flushed(struct ark_udm_t *udm)
106 {
107         return (udm->cfg.stop_flushed & 0x01) != 0;
108 }
109
110 uint64_t
111 ark_udm_dropped(struct ark_udm_t *udm)
112 {
113         return udm->qstats.q_pkt_drop;
114 }
115
116 uint64_t
117 ark_udm_bytes(struct ark_udm_t *udm)
118 {
119         return udm->qstats.q_byte_count;
120 }
121
122 uint64_t
123 ark_udm_packets(struct ark_udm_t *udm)
124 {
125         return udm->qstats.q_ff_packet_count;
126 }
127
128 void
129 ark_udm_dump_stats(struct ark_udm_t *udm, const char *msg)
130 {
131         ARK_PMD_LOG(INFO, "UDM Stats: %s"
132                       ARK_SU64 ARK_SU64 ARK_SU64 ARK_SU64 ARK_SU64 "\n",
133                       msg,
134                       "Pkts Received", udm->stats.rx_packet_count,
135                       "Pkts Finalized", udm->stats.rx_sent_packets,
136                       "Pkts Dropped", udm->tlp.pkt_drop,
137                       "Bytes Count", udm->stats.rx_byte_count,
138                       "MBuf Count", udm->stats.rx_mbuf_count);
139 }
140
141 void
142 ark_udm_dump_queue_stats(struct ark_udm_t *udm, const char *msg, uint16_t qid)
143 {
144         ARK_PMD_LOG(INFO, "UDM Queue %3u Stats: %s"
145                       ARK_SU64 ARK_SU64
146                       ARK_SU64 ARK_SU64
147                       ARK_SU64 "\n",
148                       qid, msg,
149                       "Pkts Received", udm->qstats.q_packet_count,
150                       "Pkts Finalized", udm->qstats.q_ff_packet_count,
151                       "Pkts Dropped", udm->qstats.q_pkt_drop,
152                       "Bytes Count", udm->qstats.q_byte_count,
153                       "MBuf Count", udm->qstats.q_mbuf_count);
154 }
155
156 void
157 ark_udm_dump(struct ark_udm_t *udm, const char *msg)
158 {
159         ARK_PMD_LOG(DEBUG, "UDM Dump: %s Stopped: %d\n", msg,
160                       udm->cfg.stop_flushed);
161 }
162
163 void
164 ark_udm_dump_setup(struct ark_udm_t *udm, uint16_t q_id)
165 {
166         ARK_PMD_LOG(DEBUG, "UDM Setup Q: %u"
167                       ARK_SU64X ARK_SU32 "\n",
168                       q_id,
169                       "hw_prod_addr", udm->rt_cfg.hw_prod_addr,
170                       "prod_idx", udm->rt_cfg.prod_idx);
171 }
172
173 void
174 ark_udm_dump_perf(struct ark_udm_t *udm, const char *msg)
175 {
176         struct ark_udm_pcibp_t *bp = &udm->pcibp;
177
178         ARK_PMD_LOG(INFO, "UDM Performance %s"
179                       ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32
180                       "\n",
181                       msg,
182                       "PCI Empty", bp->pci_empty,
183                       "PCI Q1", bp->pci_q1,
184                       "PCI Q2", bp->pci_q2,
185                       "PCI Q3", bp->pci_q3,
186                       "PCI Q4", bp->pci_q4,
187                       "PCI Full", bp->pci_full);
188 }
189
190 void
191 ark_udm_queue_stats_reset(struct ark_udm_t *udm)
192 {
193         udm->qstats.q_byte_count = 1;
194 }
195
196 void
197 ark_udm_queue_enable(struct ark_udm_t *udm, int enable)
198 {
199         udm->qstats.q_enable = enable ? 1 : 0;
200 }