]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge_mcdi.c
MFC r310818
[FreeBSD/stable/10.git] / sys / dev / sfxge / sfxge_mcdi.c
1 /*-
2  * Copyright (c) 2010-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of the FreeBSD Project.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/condvar.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/syslog.h>
43 #include <sys/taskqueue.h>
44 #include <sys/malloc.h>
45
46 #include "common/efx.h"
47 #include "common/efx_mcdi.h"
48 #include "common/efx_regs_mcdi.h"
49
50 #include "sfxge.h"
51
52 #if EFSYS_OPT_MCDI_LOGGING
53 #include <dev/pci/pcivar.h>
54 #endif
55
56 #define SFXGE_MCDI_POLL_INTERVAL_MIN 10         /* 10us in 1us units */
57 #define SFXGE_MCDI_POLL_INTERVAL_MAX 100000     /* 100ms in 1us units */
58
59 static void
60 sfxge_mcdi_timeout(struct sfxge_softc *sc)
61 {
62         device_t dev = sc->dev;
63
64         log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev),
65                 device_get_unit(dev));
66
67         EFSYS_PROBE(mcdi_timeout);
68         sfxge_schedule_reset(sc);
69 }
70
71 static void
72 sfxge_mcdi_poll(struct sfxge_softc *sc, uint32_t timeout_us)
73 {
74         efx_nic_t *enp;
75         clock_t delay_total;
76         clock_t delay_us;
77         boolean_t aborted;
78
79         delay_total = 0;
80         delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN;
81         enp = sc->enp;
82
83         do {
84                 if (efx_mcdi_request_poll(enp)) {
85                         EFSYS_PROBE1(mcdi_delay, clock_t, delay_total);
86                         return;
87                 }
88
89                 if (delay_total > timeout_us) {
90                         aborted = efx_mcdi_request_abort(enp);
91                         KASSERT(aborted, ("abort failed"));
92                         sfxge_mcdi_timeout(sc);
93                         return;
94                 }
95
96                 /* Spin or block depending on delay interval. */
97                 if (delay_us < 1000000)
98                         DELAY(delay_us);
99                 else
100                         pause("mcdi wait", delay_us * hz / 1000000);
101
102                 delay_total += delay_us;
103
104                 /* Exponentially back off the poll frequency. */
105                 delay_us = delay_us * 2;
106                 if (delay_us > SFXGE_MCDI_POLL_INTERVAL_MAX)
107                         delay_us = SFXGE_MCDI_POLL_INTERVAL_MAX;
108
109         } while (1);
110 }
111
112 static void
113 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
114 {
115         struct sfxge_softc *sc;
116         struct sfxge_mcdi *mcdi;
117         uint32_t timeout_us = 0;
118
119         sc = (struct sfxge_softc *)arg;
120         mcdi = &sc->mcdi;
121
122         SFXGE_MCDI_LOCK(mcdi);
123
124         KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
125             ("MCDI not initialized"));
126
127         /* Issue request and poll for completion. */
128         efx_mcdi_get_timeout(sc->enp, emrp, &timeout_us);
129         KASSERT(timeout_us > 0, ("MCDI timeout not initialized"));
130
131         efx_mcdi_request_start(sc->enp, emrp, B_FALSE);
132         sfxge_mcdi_poll(sc, timeout_us);
133
134         SFXGE_MCDI_UNLOCK(mcdi);
135 }
136
137 static void
138 sfxge_mcdi_ev_cpl(void *arg)
139 {
140         struct sfxge_softc *sc;
141         struct sfxge_mcdi *mcdi;
142
143         sc = (struct sfxge_softc *)arg;
144         mcdi = &sc->mcdi;
145
146         KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
147             ("MCDI not initialized"));
148
149         /* We do not use MCDI completion, MCDI is simply polled */
150 }
151
152 static void
153 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
154 {
155         struct sfxge_softc *sc;
156         device_t dev;
157
158         sc = (struct sfxge_softc *)arg;
159         dev = sc->dev;
160
161         log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev),
162             device_get_unit(dev),
163             (eme == EFX_MCDI_EXCEPTION_MC_REBOOT)
164             ? "REBOOT"
165             : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT)
166             ? "BADASSERT" : "UNKNOWN");
167
168         EFSYS_PROBE(mcdi_exception);
169
170         sfxge_schedule_reset(sc);
171 }
172
173 #if EFSYS_OPT_MCDI_LOGGING
174
175 #define SFXGE_MCDI_LOG_BUF_SIZE 128
176
177 static size_t
178 sfxge_mcdi_do_log(char *buffer, void *data, size_t data_size,
179                   size_t pfxsize, size_t position)
180 {
181         uint32_t *words = data;
182         size_t i;
183
184         for (i = 0; i < data_size; i += sizeof(*words)) {
185                 if (position + 2 * sizeof(*words) + 1 >= SFXGE_MCDI_LOG_BUF_SIZE) {
186                         buffer[position] = '\0';
187                         printf("%s \\\n", buffer);
188                         position = pfxsize;
189                 }
190                 snprintf(buffer + position, SFXGE_MCDI_LOG_BUF_SIZE - position,
191                          " %08x", *words);
192                 words++;
193                 position += 2 * sizeof(uint32_t) + 1;
194         }
195         return (position);
196 }
197
198 static void
199 sfxge_mcdi_logger(void *arg, efx_log_msg_t type,
200                   void *header, size_t header_size,
201                   void *data, size_t data_size)
202 {
203         struct sfxge_softc *sc = (struct sfxge_softc *)arg;
204         char buffer[SFXGE_MCDI_LOG_BUF_SIZE];
205         size_t pfxsize;
206         size_t start;
207
208         if (!sc->mcdi_logging)
209                 return;
210
211         pfxsize = snprintf(buffer, sizeof(buffer),
212                            "sfc %04x:%02x:%02x.%02x %s MCDI RPC %s:",
213                            pci_get_domain(sc->dev),
214                            pci_get_bus(sc->dev),
215                            pci_get_slot(sc->dev),
216                            pci_get_function(sc->dev),
217                            device_get_nameunit(sc->dev),
218                            type == EFX_LOG_MCDI_REQUEST ? "REQ" :
219                            type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
220         start = sfxge_mcdi_do_log(buffer, header, header_size,
221                                   pfxsize, pfxsize);
222         start = sfxge_mcdi_do_log(buffer, data, data_size, pfxsize, start);
223         if (start != pfxsize) {
224                 buffer[start] = '\0';
225                 printf("%s\n", buffer);
226         }
227 }
228
229 #endif
230
231 int
232 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip)
233 {
234         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
235         struct sfxge_mcdi *mp = &(sc->mcdi);
236         efx_mcdi_req_t emr;
237         uint8_t *mcdibuf;
238         int rc;
239
240         if (mp->state == SFXGE_MCDI_UNINITIALIZED) {
241                 rc = ENODEV;
242                 goto fail1;
243         }
244
245         if (!(encp->enc_features & EFX_FEATURE_MCDI)) {
246                 rc = ENOTSUP;
247                 goto fail2;
248         }
249
250         if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) {
251                 rc = EINVAL;
252                 goto fail3;
253         }
254
255         mcdibuf = malloc(SFXGE_MCDI_MAX_PAYLOAD, M_TEMP, M_WAITOK | M_ZERO);
256         if (mcdibuf == NULL) {
257                 rc = ENOMEM;
258                 goto fail4;
259         }
260         if ((rc = copyin(ip->u.mcdi.payload, mcdibuf, ip->u.mcdi.len)) != 0) {
261                 goto fail5;
262         }
263
264         emr.emr_cmd = ip->u.mcdi.cmd;
265         emr.emr_in_buf = mcdibuf;
266         emr.emr_in_length = ip->u.mcdi.len;
267
268         emr.emr_out_buf = mcdibuf;
269         emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD;
270
271         sfxge_mcdi_execute(sc, &emr);
272
273         ip->u.mcdi.rc = emr.emr_rc;
274         ip->u.mcdi.cmd = emr.emr_cmd;
275         ip->u.mcdi.len = emr.emr_out_length_used;
276         if ((rc = copyout(mcdibuf, ip->u.mcdi.payload, ip->u.mcdi.len)) != 0) {
277                 goto fail6;
278         }
279
280         /*
281          * Helpfully trigger a device reset in response to an MCDI_CMD_REBOOT
282          * Both ports will see ->emt_exception callbacks on the next MCDI poll
283          */
284         if (ip->u.mcdi.cmd == MC_CMD_REBOOT) {
285
286                 EFSYS_PROBE(mcdi_ioctl_mc_reboot);
287                 /* sfxge_t->s_state_lock held */
288                 (void) sfxge_schedule_reset(sc);
289         }
290
291         free(mcdibuf, M_TEMP);
292
293         return (0);
294
295 fail6:
296 fail5:
297         free(mcdibuf, M_TEMP);
298 fail4:
299 fail3:
300 fail2:
301 fail1:
302         return (rc);
303 }
304
305
306 int
307 sfxge_mcdi_init(struct sfxge_softc *sc)
308 {
309         efx_nic_t *enp;
310         struct sfxge_mcdi *mcdi;
311         efx_mcdi_transport_t *emtp;
312         efsys_mem_t *esmp;
313         int max_msg_size;
314         int rc;
315
316         enp = sc->enp;
317         mcdi = &sc->mcdi;
318         emtp = &mcdi->transport;
319         esmp = &mcdi->mem;
320         max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
321
322         KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED,
323             ("MCDI already initialized"));
324
325         SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev));
326
327         mcdi->state = SFXGE_MCDI_INITIALIZED;
328
329         if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0)
330                 goto fail;
331
332         emtp->emt_context = sc;
333         emtp->emt_dma_mem = esmp;
334         emtp->emt_execute = sfxge_mcdi_execute;
335         emtp->emt_ev_cpl = sfxge_mcdi_ev_cpl;
336         emtp->emt_exception = sfxge_mcdi_exception;
337 #if EFSYS_OPT_MCDI_LOGGING
338         emtp->emt_logger = sfxge_mcdi_logger;
339         SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
340                        SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
341                        OID_AUTO, "mcdi_logging", CTLFLAG_RW,
342                        &sc->mcdi_logging, 0,
343                        "MCDI logging");
344 #endif
345
346         if ((rc = efx_mcdi_init(enp, emtp)) != 0)
347                 goto fail;
348
349         return (0);
350
351 fail:
352         SFXGE_MCDI_LOCK_DESTROY(mcdi);
353         mcdi->state = SFXGE_MCDI_UNINITIALIZED;
354         return (rc);
355 }
356
357 void
358 sfxge_mcdi_fini(struct sfxge_softc *sc)
359 {
360         struct sfxge_mcdi *mcdi;
361         efx_nic_t *enp;
362         efx_mcdi_transport_t *emtp;
363         efsys_mem_t *esmp;
364
365         enp = sc->enp;
366         mcdi = &sc->mcdi;
367         emtp = &mcdi->transport;
368         esmp = &mcdi->mem;
369
370         SFXGE_MCDI_LOCK(mcdi);
371         KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
372             ("MCDI not initialized"));
373
374         efx_mcdi_fini(enp);
375         bzero(emtp, sizeof(*emtp));
376
377         SFXGE_MCDI_UNLOCK(mcdi);
378
379         sfxge_dma_free(esmp);
380
381         SFXGE_MCDI_LOCK_DESTROY(mcdi);
382 }