5f6ad0a4aa7ed021a4ba0dc893530d40adedcde0
[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         efx_nic_t *enp;
79         unsigned int delay_total;
80         unsigned int delay_us;
81         boolean_t aborted __rte_unused;
82
83         delay_total = 0;
84         delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MIN_US;
85         enp = sa->nic;
86
87         do {
88                 boolean_t poll_completed;
89
90                 poll_completed = (proxy) ? sfc_efx_mcdi_proxy_event_available(sa) :
91                                            efx_mcdi_request_poll(enp);
92                 if (poll_completed)
93                         return;
94
95                 if (delay_total > SFC_EFX_MCDI_WATCHDOG_INTERVAL_US) {
96                         if (!proxy) {
97                                 aborted = efx_mcdi_request_abort(enp);
98                                 SFC_ASSERT(aborted);
99                                 sfc_efx_mcdi_timeout(sa);
100                         }
101
102                         return;
103                 }
104
105                 rte_delay_us(delay_us);
106
107                 delay_total += delay_us;
108
109                 /* Exponentially back off the poll frequency */
110                 RTE_BUILD_BUG_ON(SFC_EFX_MCDI_POLL_INTERVAL_MAX_US >
111                                  UINT_MAX / 2);
112                 delay_us *= 2;
113                 if (delay_us > SFC_EFX_MCDI_POLL_INTERVAL_MAX_US)
114                         delay_us = SFC_EFX_MCDI_POLL_INTERVAL_MAX_US;
115
116         } while (1);
117 }
118
119 static void
120 sfc_efx_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
121 {
122         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
123         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
124         uint32_t proxy_handle;
125
126         rte_spinlock_lock(&mcdi->lock);
127
128         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
129
130         efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
131         sfc_efx_mcdi_poll(sa, B_FALSE);
132
133         if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
134                 /*
135                  * Authorization is required for the MCDI request;
136                  * wait for an MCDI proxy response event to bring
137                  * a non-zero proxy handle (should be the same as
138                  * the value obtained above) and operation status
139                  */
140                 sfc_efx_mcdi_poll(sa, B_TRUE);
141
142                 if ((mcdi->proxy_handle != 0) &&
143                     (mcdi->proxy_handle != proxy_handle)) {
144                         sfc_efx_mcdi_err(mcdi, "Unexpected MCDI proxy event");
145                         emrp->emr_rc = EFAULT;
146                 } else if (mcdi->proxy_result == 0) {
147                         /*
148                          * Authorization succeeded; re-issue the original
149                          * request and poll for an ordinary MCDI response
150                          */
151                         efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
152                         sfc_efx_mcdi_poll(sa, B_FALSE);
153                 } else {
154                         emrp->emr_rc = mcdi->proxy_result;
155                         sfc_efx_mcdi_err(mcdi,
156                                 "MCDI proxy authorization failed (handle=%08x, result=%d)",
157                                 proxy_handle, mcdi->proxy_result);
158                 }
159         }
160
161         rte_spinlock_unlock(&mcdi->lock);
162 }
163
164 static void
165 sfc_efx_mcdi_ev_cpl(void *arg)
166 {
167         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
168         struct sfc_efx_mcdi *mcdi __rte_unused;
169
170         mcdi = &sa->mcdi;
171         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
172
173         /* MCDI is polled, completions are not expected */
174         SFC_ASSERT(0);
175 }
176
177 static void
178 sfc_efx_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
179 {
180         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
181         struct sfc_efx_mcdi *mcdi  = &sa->mcdi;
182
183         sfc_efx_mcdi_warn(mcdi, "MC %s",
184             (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
185             (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
186
187         sfc_schedule_restart(sa);
188 }
189
190 #define SFC_MCDI_LOG_BUF_SIZE   128
191
192 static size_t
193 sfc_efx_mcdi_do_log(const struct sfc_efx_mcdi *mcdi,
194                 char *buffer, void *data, size_t data_size,
195                 size_t pfxsize, size_t position)
196 {
197         uint32_t *words = data;
198         /* Space separator plus 2 characters per byte */
199         const size_t word_str_space = 1 + 2 * sizeof(*words);
200         size_t i;
201
202         for (i = 0; i < data_size; i += sizeof(*words)) {
203                 if (position + word_str_space >=
204                     SFC_MCDI_LOG_BUF_SIZE) {
205                         /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
206                          * at the end which is required by netlogdecode.
207                          */
208                         buffer[position] = '\0';
209                         sfc_efx_log_mcdi(mcdi, "%s \\", buffer);
210                         /* Preserve prefix for the next log message */
211                         position = pfxsize;
212                 }
213                 position += snprintf(buffer + position,
214                                      SFC_MCDI_LOG_BUF_SIZE - position,
215                                      " %08x", *words);
216                 words++;
217         }
218         return position;
219 }
220
221 static void
222 sfc_efx_mcdi_logger(void *arg, efx_log_msg_t type,
223                 void *header, size_t header_size,
224                 void *data, size_t data_size)
225 {
226         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
227         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
228         char buffer[SFC_MCDI_LOG_BUF_SIZE];
229         size_t pfxsize;
230         size_t start;
231
232         /*
233          * Unlike the other cases, MCDI logging implies more onerous work
234          * needed to produce a message. If the dynamic log level prevents
235          * the end result from being printed, the CPU time will be wasted.
236          *
237          * To avoid wasting time, the actual level is examined in advance.
238          */
239         if (rte_log_get_level(mcdi->logtype) < (int)SFC_EFX_LOG_LEVEL_MCDI)
240                 return;
241
242         /* The format including prefix added by sfc_efx_log_mcdi() is the
243          * format consumed by the Solarflare netlogdecode tool.
244          */
245         pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
246                            type == EFX_LOG_MCDI_REQUEST ? "REQ" :
247                            type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
248         start = sfc_efx_mcdi_do_log(mcdi, buffer, header, header_size,
249                                     pfxsize, pfxsize);
250         start = sfc_efx_mcdi_do_log(mcdi, buffer, data, data_size,
251                                     pfxsize, start);
252         if (start != pfxsize) {
253                 buffer[start] = '\0';
254                 sfc_efx_log_mcdi(mcdi, "%s", buffer);
255         }
256 }
257
258 static void
259 sfc_efx_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
260 {
261         struct sfc_adapter *sa = (struct sfc_adapter *)arg;
262         struct sfc_efx_mcdi *mcdi = &sa->mcdi;
263
264         mcdi->proxy_handle = handle;
265         mcdi->proxy_result = result;
266 }
267
268 static int
269 sfc_efx_mcdi_init(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi,
270                   uint32_t logtype, const char *log_prefix)
271 {
272         size_t max_msg_size;
273         efx_mcdi_transport_t *emtp;
274         int rc;
275
276         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_UNINITIALIZED);
277
278         rte_spinlock_init(&mcdi->lock);
279
280         mcdi->state = SFC_EFX_MCDI_INITIALIZED;
281
282         mcdi->logtype = logtype;
283         mcdi->log_prefix = log_prefix;
284
285         max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
286         rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
287                            &mcdi->mem);
288         if (rc != 0)
289                 goto fail_dma_alloc;
290
291         emtp = &mcdi->transport;
292         emtp->emt_context = sa;
293         emtp->emt_dma_mem = &mcdi->mem;
294         emtp->emt_execute = sfc_efx_mcdi_execute;
295         emtp->emt_ev_cpl = sfc_efx_mcdi_ev_cpl;
296         emtp->emt_exception = sfc_efx_mcdi_exception;
297         emtp->emt_logger = sfc_efx_mcdi_logger;
298         emtp->emt_ev_proxy_response = sfc_efx_mcdi_ev_proxy_response;
299
300         sfc_efx_mcdi_info(mcdi, "init MCDI");
301         rc = efx_mcdi_init(sa->nic, emtp);
302         if (rc != 0)
303                 goto fail_mcdi_init;
304
305         return 0;
306
307 fail_mcdi_init:
308         memset(emtp, 0, sizeof(*emtp));
309         sfc_dma_free(sa, &mcdi->mem);
310
311 fail_dma_alloc:
312         mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
313         return rc;
314 }
315
316 static void
317 sfc_efx_mcdi_fini(struct sfc_adapter *sa, struct sfc_efx_mcdi *mcdi)
318 {
319         efx_mcdi_transport_t *emtp;
320
321         emtp = &mcdi->transport;
322
323         rte_spinlock_lock(&mcdi->lock);
324
325         SFC_ASSERT(mcdi->state == SFC_EFX_MCDI_INITIALIZED);
326         mcdi->state = SFC_EFX_MCDI_UNINITIALIZED;
327
328         sfc_efx_mcdi_info(mcdi, "fini MCDI");
329         efx_mcdi_fini(sa->nic);
330         memset(emtp, 0, sizeof(*emtp));
331
332         rte_spinlock_unlock(&mcdi->lock);
333
334         sfc_dma_free(sa, &mcdi->mem);
335 }
336
337 int
338 sfc_mcdi_init(struct sfc_adapter *sa)
339 {
340         uint32_t logtype;
341
342         sfc_log_init(sa, "entry");
343
344         logtype = sfc_register_logtype(&sa->priv.shared->pci_addr,
345                                        SFC_LOGTYPE_MCDI_STR,
346                                        RTE_LOG_NOTICE);
347
348         return sfc_efx_mcdi_init(sa, &sa->mcdi, logtype,
349                                  sa->priv.shared->log_prefix);
350 }
351
352 void
353 sfc_mcdi_fini(struct sfc_adapter *sa)
354 {
355         sfc_log_init(sa, "entry");
356         sfc_efx_mcdi_fini(sa, &sa->mcdi);
357 }