8f446e8bc8e8d50fb7107a6e3914cdc2f305a9ee
[dpdk.git] / drivers / net / sfc / sfc_mcdi.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2016-2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <rte_cycles.h>
11
12 #include "efx.h"
13 #include "efx_mcdi.h"
14 #include "efx_regs_mcdi.h"
15
16 #include "sfc_mcdi.h"
17 #include "sfc.h"
18 #include "sfc_debug.h"
19 #include "sfc_log.h"
20 #include "sfc_ev.h"
21
22 #define SFC_EFX_MCDI_POLL_INTERVAL_MIN_US       10              /* 10us */
23 #define SFC_EFX_MCDI_POLL_INTERVAL_MAX_US       (US_PER_S / 10) /* 100ms */
24 #define SFC_EFX_MCDI_WATCHDOG_INTERVAL_US       (10 * US_PER_S) /* 10s */
25
26 #define sfc_efx_mcdi_log(mcdi, level, ...) \
27         do {                                                            \
28                 const struct sfc_efx_mcdi *_mcdi = (mcdi);              \
29                                                                         \
30                 rte_log(level, _mcdi->logtype,                          \
31                         RTE_FMT("%s" RTE_FMT_HEAD(__VA_ARGS__ ,) "\n",  \
32                                 _mcdi->log_prefix,                      \
33                                 RTE_FMT_TAIL(__VA_ARGS__,)));           \
34         } while (0)
35
36 #define sfc_efx_mcdi_err(mcdi, ...) \
37         sfc_efx_mcdi_log(mcdi, RTE_LOG_ERR, __VA_ARGS__)
38
39 #define sfc_efx_mcdi_warn(mcdi, ...) \
40         sfc_efx_mcdi_log(mcdi, RTE_LOG_WARNING, __VA_ARGS__)
41
42 #define sfc_efx_mcdi_info(mcdi, ...) \
43         sfc_efx_mcdi_log(mcdi, RTE_LOG_INFO, __VA_ARGS__)
44
45 /** Level value used by MCDI log statements */
46 #define SFC_EFX_LOG_LEVEL_MCDI  RTE_LOG_INFO
47
48 #define sfc_efx_log_mcdi(mcdi, ...) \
49         sfc_efx_mcdi_log(mcdi, SFC_EFX_LOG_LEVEL_MCDI, __VA_ARGS__)
50
51 static void
52 sfc_efx_mcdi_timeout(struct sfc_adapter *sa)
53 {
54         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
55
56         sfc_efx_mcdi_warn(mcdi, "MC TIMEOUT");
57
58         sfc_panic(sa, "MCDI timeout handling is not implemented\n");
59 }
60
61 static inline boolean_t
62 sfc_efx_mcdi_proxy_event_available(struct sfc_adapter *sa)
63 {
64         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
65
66         mcdi->proxy_handle = 0;
67         mcdi->proxy_result = ETIMEDOUT;
68         sfc_ev_mgmt_qpoll(sa);
69         if (mcdi->proxy_result != ETIMEDOUT)
70                 return B_TRUE;
71
72         return B_FALSE;
73 }
74
75 static void
76 sfc_efx_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
77 {
78         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
79         efx_nic_t *enp;
80         unsigned int delay_total;
81         unsigned int delay_us;
82         boolean_t aborted __rte_unused;
83
84         delay_total = 0;
85         delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
86         enp = mcdi->nic;
87
88         do {
89                 boolean_t poll_completed;
90
91                 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
92                                            efx_mcdi_request_poll(enp);
93                 if (poll_completed)
94                         return;
95
96                 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
97                         if (!proxy) {
98                                 aborted = efx_mcdi_request_abort(enp);
99                                 SFC_ASSERT(aborted);
100                                 sfc_efx_mcdi_timeout(sa);
101                         }
102
103                         return;
104                 }
105
106                 rte_delay_us(delay_us);
107
108                 delay_total += delay_us;
109
110                 /* Exponentially back off the poll frequency */
111                 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
112                                  UINT_MAX / 2);
113                 delay_us *= 2;
114                 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
115                         delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
116
117         } while (1);
118 }
119
120 static void
121 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
122 {
123         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
124         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
125         uint32_t proxy_handle;
126
127         rte_spinlock_lock(&mcdi->lock);
128
129         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
130
131         efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
132         sfc_efx_mcdi_poll(sa, B_FALSE);
133
134         if (efx_mcdi_get_proxy_handle(mcdi->nic, emrp, &proxy_handle) == 0) {
135                 /*
136                  * Authorization is required for the MCDI request;
137                  * wait for an MCDI proxy response event to bring
138                  * a non-zero proxy handle (should be the same as
139                  * the value obtained above) and operation status
140                  */
141                 sfc_efx_mcdi_poll(sa, B_TRUE);
142
143                 if ((mcdi->proxy_handle != 0) &&
144                     (mcdi->proxy_handle != proxy_handle)) {
145                         sfc_efx_mcdi_err(mcdi, "Unexpected MCDI proxy event");
146                         emrp->emr_rc = EFAULT;
147                 } else if (mcdi->proxy_result == 0) {
148                         /*
149                          * Authorization succeeded; re-issue the original
150                          * request and poll for an ordinary MCDI response
151                          */
152                         efx_mcdi_request_start(mcdi->nic, emrp, B_FALSE);
153                         sfc_efx_mcdi_poll(sa, B_FALSE);
154                 } else {
155                         emrp->emr_rc = mcdi->proxy_result;
156                         sfc_efx_mcdi_err(mcdi,
157                                 "MCDI proxy authorization failed (handle=%08x, result=%d)",
158                                 proxy_handle, mcdi->proxy_result);
159                 }
160         }
161
162         rte_spinlock_unlock(&mcdi->lock);
163 }
164
165 static void
166 sfc_efx_mcdi_ev_cpl(void *arg)
167 {
168         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
169         struct sfc_efx_mcdi *mcdi __rte_unused;
170
171         mcdi = &sa->mcdi;
172         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
173
174         /* MCDI is polled, completions are not expected */
175         SFC_ASSERT(0);
176 }
177
178 static void
179 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
180 {
181         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
182         struct sfc_efx_mcdi *mcdi  = &sa->mcdi;
183
184         sfc_efx_mcdi_warn(mcdi, "MC %s",
185             (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
186             (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
187
188         sfc_schedule_restart(sa);
189 }
190
191 #define SFC_MCDI_LOG_BUF_SIZE   128
192
193 static size_t
194 sfc_efx_mcdi_do_log(const struct sfc_efx_mcdi *mcdi,
195                 char *buffer, void *data, size_t data_size,
196                 size_t pfxsize, size_t position)
197 {
198         uint32_t *words = data;
199         /* Space separator plus 2 characters per byte */
200         const size_t word_str_space = 1 + 2 * sizeof(*words);
201         size_t i;
202
203         for (i = 0; i < data_size; i += sizeof(*words)) {
204                 if (position + word_str_space >=
205                     SFC_MCDI_LOG_BUF_SIZE) {
206                         /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
207                          * at the end which is required by netlogdecode.
208                          */
209                         buffer[position] = '\0';
210                         sfc_efx_log_mcdi(mcdi, "%s \\", buffer);
211                         /* Preserve prefix for the next log message */
212                         position = pfxsize;
213                 }
214                 position += snprintf(buffer + position,
215                                      SFC_MCDI_LOG_BUF_SIZE - position,
216                                      " %08x", *words);
217                 words++;
218         }
219         return position;
220 }
221
222 static void
223 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
224                 void *header, size_t header_size,
225                 void *data, size_t data_size)
226 {
227         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
228         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
229         char buffer[SFC_MCDI_LOG_BUF_SIZE];
230         size_t pfxsize;
231         size_t start;
232
233         /*
234          * Unlike the other cases, MCDI logging implies more onerous work
235          * needed to produce a message. If the dynamic log level prevents
236          * the end result from being printed, the CPU time will be wasted.
237          *
238          * To avoid wasting time, the actual level is examined in advance.
239          */
240         if (rte_log_get_level(mcdi->logtype) < (int)SFC_EFX_LOG_LEVEL_MCDI)
241                 return;
242
243         /* The format including prefix added by sfc_efx_log_mcdi() is the
244          * format consumed by the Solarflare netlogdecode tool.
245          */
246         pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
247                            type == EFX_LOG_MCDI_REQUEST ? "REQ" :
248                            type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
249         start = sfc_efx_mcdi_do_log(mcdi, buffer, header, header_size,
250                                     pfxsize, pfxsize);
251         start = sfc_efx_mcdi_do_log(mcdi, buffer, data, data_size,
252                                     pfxsize, start);
253         if (start != pfxsize) {
254                 buffer[start] = '\0';
255                 sfc_efx_log_mcdi(mcdi, "%s", buffer);
256         }
257 }
258
259 static void
260 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
261 {
262         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
263         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
264
265         mcdi->proxy_handle = handle;
266         mcdi->proxy_result = result;
267 }
268
269 static int
270 sfc_efx_mcdi_init(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi,
271                   uint32_t logtype, const char *log_prefix, efx_nic_t *nic)
272 {
273         size_t max_msg_size;
274         efx_mcdi_transport_t *emtp;
275         int rc;
276
277         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
278
279         rte_spinlock_init(&mcdi->lock);
280
281         mcdi->nic = nic;
282
283         mcdi->state = SFC_EFX_MCDI_INITIALIZED;
284
285         mcdi->logtype = logtype;
286         mcdi->log_prefix = log_prefix;
287
288         max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
289         rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
290                            &mcdi->mem);
291         if (rc != 0)
292                 goto fail_dma_alloc;
293
294         emtp = &mcdi->transport;
295         emtp->emt_context = sa;
296         emtp->emt_dma_mem = &mcdi->mem;
297         emtp->emt_execute = sfc_efx_mcdi_execute;
298         emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
299         emtp->emt_exception = sfc_efx_mcdi_exception;
300         emtp->emt_logger = sfc_efx_mcdi_logger;
301         emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
302
303         sfc_efx_mcdi_info(mcdi, "init MCDI");
304         rc = efx_mcdi_init(mcdi->nic, emtp);
305         if (rc != 0)
306                 goto fail_mcdi_init;
307
308         return 0;
309
310 fail_mcdi_init:
311         memset(emtp, 0, sizeof(*emtp));
312         sfc_dma_free(sa, &mcdi->mem);
313
314 fail_dma_alloc:
315         mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
316         return rc;
317 }
318
319 static void
320 sfc_efx_mcdi_fini(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi)
321 {
322         efx_mcdi_transport_t *emtp;
323
324         emtp = &mcdi->transport;
325
326         rte_spinlock_lock(&mcdi->lock);
327
328         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
329         mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
330
331         sfc_efx_mcdi_info(mcdi, "fini MCDI");
332         efx_mcdi_fini(mcdi->nic);
333         memset(emtp, 0, sizeof(*emtp));
334
335         rte_spinlock_unlock(&mcdi->lock);
336
337         sfc_dma_free(sa, &mcdi->mem);
338 }
339
340 int
341 sfc_mcdi_init(struct sfc_adapter *sa)
342 {
343         uint32_t logtype;
344
345         sfc_log_init(sa, "entry");
346
347         logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
348                                        SFC_LOGTYPE_MCDI_STR,
349                                        RTE_LOG_NOTICE);
350
351         return sfc_efx_mcdi_init(sa, &sa->mcdi, logtype,
352                                  sa->priv.shared->log_prefix, sa->nic);
353 }
354
355 void
356 sfc_mcdi_fini(struct sfc_adapter *sa)
357 {
358         sfc_log_init(sa, "entry");
359         sfc_efx_mcdi_fini(sa, &sa->mcdi);
360 }