1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016-2018 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
10 #include <rte_cycles.h>
14 #include "efx_regs_mcdi.h"
18 #include "sfc_kvargs.h"
21 #define SFC_MCDI_POLL_INTERVAL_MIN_US 10 /* 10us in 1us units */
22 #define SFC_MCDI_POLL_INTERVAL_MAX_US (US_PER_S / 10) /* 100ms in 1us units */
23 #define SFC_MCDI_WATCHDOG_INTERVAL_US (10 * US_PER_S) /* 10s in 1us units */
26 sfc_mcdi_timeout(struct sfc_adapter *sa)
28 sfc_warn(sa, "MC TIMEOUT");
30 sfc_panic(sa, "MCDI timeout handling is not implemented\n");
33 static inline boolean_t
34 sfc_mcdi_proxy_event_available(struct sfc_adapter *sa)
36 struct sfc_mcdi *mcdi = &sa->mcdi;
38 mcdi->proxy_handle = 0;
39 mcdi->proxy_result = ETIMEDOUT;
40 sfc_ev_mgmt_qpoll(sa);
41 if (mcdi->proxy_result != ETIMEDOUT)
48 sfc_mcdi_poll(struct sfc_adapter *sa, boolean_t proxy)
51 unsigned int delay_total;
52 unsigned int delay_us;
53 boolean_t aborted __rte_unused;
56 delay_us = SFC_MCDI_POLL_INTERVAL_MIN_US;
60 boolean_t poll_completed;
62 poll_completed = (proxy) ? sfc_mcdi_proxy_event_available(sa) :
63 efx_mcdi_request_poll(enp);
67 if (delay_total > SFC_MCDI_WATCHDOG_INTERVAL_US) {
69 aborted = efx_mcdi_request_abort(enp);
77 rte_delay_us(delay_us);
79 delay_total += delay_us;
81 /* Exponentially back off the poll frequency */
82 RTE_BUILD_BUG_ON(SFC_MCDI_POLL_INTERVAL_MAX_US > UINT_MAX / 2);
84 if (delay_us > SFC_MCDI_POLL_INTERVAL_MAX_US)
85 delay_us = SFC_MCDI_POLL_INTERVAL_MAX_US;
91 sfc_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
93 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
94 struct sfc_mcdi *mcdi = &sa->mcdi;
95 uint32_t proxy_handle;
97 rte_spinlock_lock(&mcdi->lock);
99 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
101 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
102 sfc_mcdi_poll(sa, B_FALSE);
104 if (efx_mcdi_get_proxy_handle(sa->nic, emrp, &proxy_handle) == 0) {
106 * Authorization is required for the MCDI request;
107 * wait for an MCDI proxy response event to bring
108 * a non-zero proxy handle (should be the same as
109 * the value obtained above) and operation status
111 sfc_mcdi_poll(sa, B_TRUE);
113 if ((mcdi->proxy_handle != 0) &&
114 (mcdi->proxy_handle != proxy_handle)) {
115 sfc_err(sa, "Unexpected MCDI proxy event");
116 emrp->emr_rc = EFAULT;
117 } else if (mcdi->proxy_result == 0) {
119 * Authorization succeeded; re-issue the original
120 * request and poll for an ordinary MCDI response
122 efx_mcdi_request_start(sa->nic, emrp, B_FALSE);
123 sfc_mcdi_poll(sa, B_FALSE);
125 emrp->emr_rc = mcdi->proxy_result;
126 sfc_err(sa, "MCDI proxy authorization failed "
127 "(handle=%08x, result=%d)",
128 proxy_handle, mcdi->proxy_result);
132 rte_spinlock_unlock(&mcdi->lock);
136 sfc_mcdi_ev_cpl(void *arg)
138 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
139 struct sfc_mcdi *mcdi __rte_unused;
142 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
144 /* MCDI is polled, completions are not expected */
149 sfc_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
151 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
153 sfc_warn(sa, "MC %s",
154 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) ? "REBOOT" :
155 (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) ? "BADASSERT" : "UNKNOWN");
157 sfc_schedule_restart(sa);
160 #define SFC_MCDI_LOG_BUF_SIZE 128
163 sfc_mcdi_do_log(const struct sfc_adapter *sa,
164 char *buffer, void *data, size_t data_size,
165 size_t pfxsize, size_t position)
167 uint32_t *words = data;
168 /* Space separator plus 2 characters per byte */
169 const size_t word_str_space = 1 + 2 * sizeof(*words);
172 for (i = 0; i < data_size; i += sizeof(*words)) {
173 if (position + word_str_space >=
174 SFC_MCDI_LOG_BUF_SIZE) {
175 /* Flush at SFC_MCDI_LOG_BUF_SIZE with backslash
176 * at the end which is required by netlogdecode.
178 buffer[position] = '\0';
179 sfc_info(sa, "%s \\", buffer);
180 /* Preserve prefix for the next log message */
183 position += snprintf(buffer + position,
184 SFC_MCDI_LOG_BUF_SIZE - position,
192 sfc_mcdi_logger(void *arg, efx_log_msg_t type,
193 void *header, size_t header_size,
194 void *data, size_t data_size)
196 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
197 char buffer[SFC_MCDI_LOG_BUF_SIZE];
201 if (!sa->mcdi.logging)
204 /* The format including prefix added by sfc_info() is the format
205 * consumed by the Solarflare netlogdecode tool.
207 pfxsize = snprintf(buffer, sizeof(buffer), "MCDI RPC %s:",
208 type == EFX_LOG_MCDI_REQUEST ? "REQ" :
209 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
210 start = sfc_mcdi_do_log(sa, buffer, header, header_size,
212 start = sfc_mcdi_do_log(sa, buffer, data, data_size, pfxsize, start);
213 if (start != pfxsize) {
214 buffer[start] = '\0';
215 sfc_info(sa, "%s", buffer);
220 sfc_mcdi_ev_proxy_response(void *arg, uint32_t handle, efx_rc_t result)
222 struct sfc_adapter *sa = (struct sfc_adapter *)arg;
223 struct sfc_mcdi *mcdi = &sa->mcdi;
225 mcdi->proxy_handle = handle;
226 mcdi->proxy_result = result;
230 sfc_mcdi_init(struct sfc_adapter *sa)
232 struct sfc_mcdi *mcdi;
234 efx_mcdi_transport_t *emtp;
237 sfc_log_init(sa, "entry");
241 SFC_ASSERT(mcdi->state == SFC_MCDI_UNINITIALIZED);
243 rte_spinlock_init(&mcdi->lock);
245 mcdi->state = SFC_MCDI_INITIALIZED;
247 max_msg_size = sizeof(uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
248 rc = sfc_dma_alloc(sa, "mcdi", 0, max_msg_size, sa->socket_id,
253 /* Convert negative error to positive used in the driver */
254 rc = sfc_kvargs_process(sa, SFC_KVARG_MCDI_LOGGING,
255 sfc_kvarg_bool_handler, &mcdi->logging);
257 goto fail_kvargs_process;
259 emtp = &mcdi->transport;
260 emtp->emt_context = sa;
261 emtp->emt_dma_mem = &mcdi->mem;
262 emtp->emt_execute = sfc_mcdi_execute;
263 emtp->emt_ev_cpl = sfc_mcdi_ev_cpl;
264 emtp->emt_exception = sfc_mcdi_exception;
265 emtp->emt_logger = sfc_mcdi_logger;
266 emtp->emt_ev_proxy_response = sfc_mcdi_ev_proxy_response;
268 sfc_log_init(sa, "init MCDI");
269 rc = efx_mcdi_init(sa->nic, emtp);
276 memset(emtp, 0, sizeof(*emtp));
279 sfc_dma_free(sa, &mcdi->mem);
282 mcdi->state = SFC_MCDI_UNINITIALIZED;
287 sfc_mcdi_fini(struct sfc_adapter *sa)
289 struct sfc_mcdi *mcdi;
290 efx_mcdi_transport_t *emtp;
292 sfc_log_init(sa, "entry");
295 emtp = &mcdi->transport;
297 rte_spinlock_lock(&mcdi->lock);
299 SFC_ASSERT(mcdi->state == SFC_MCDI_INITIALIZED);
300 mcdi->state = SFC_MCDI_UNINITIALIZED;
302 sfc_log_init(sa, "fini MCDI");
303 efx_mcdi_fini(sa->nic);
304 memset(emtp, 0, sizeof(*emtp));
306 rte_spinlock_unlock(&mcdi->lock);
308 sfc_dma_free(sa, &mcdi->mem);