Merge branch 'sfc-next'
[linux-2.6-block.git] / drivers / net / ethernet / sfc / mcdi.c
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2008-2013 Solarflare Communications Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, incorporated herein by reference.
8  */
9
10 #include <linux/delay.h>
11 #include <asm/cmpxchg.h>
12 #include "net_driver.h"
13 #include "nic.h"
14 #include "io.h"
15 #include "farch_regs.h"
16 #include "mcdi_pcol.h"
17 #include "phy.h"
18
19 /**************************************************************************
20  *
21  * Management-Controller-to-Driver Interface
22  *
23  **************************************************************************
24  */
25
26 #define MCDI_RPC_TIMEOUT       (10 * HZ)
27
28 /* A reboot/assertion causes the MCDI status word to be set after the
29  * command word is set or a REBOOT event is sent. If we notice a reboot
30  * via these mechanisms then wait 250ms for the status word to be set.
31  */
32 #define MCDI_STATUS_DELAY_US            100
33 #define MCDI_STATUS_DELAY_COUNT         2500
34 #define MCDI_STATUS_SLEEP_MS                                            \
35         (MCDI_STATUS_DELAY_US * MCDI_STATUS_DELAY_COUNT / 1000)
36
37 #define SEQ_MASK                                                        \
38         EFX_MASK32(EFX_WIDTH(MCDI_HEADER_SEQ))
39
40 struct efx_mcdi_async_param {
41         struct list_head list;
42         unsigned int cmd;
43         size_t inlen;
44         size_t outlen;
45         bool quiet;
46         efx_mcdi_async_completer *complete;
47         unsigned long cookie;
48         /* followed by request/response buffer */
49 };
50
51 static void efx_mcdi_timeout_async(unsigned long context);
52 static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
53                                bool *was_attached_out);
54 static bool efx_mcdi_poll_once(struct efx_nic *efx);
55 static void efx_mcdi_abandon(struct efx_nic *efx);
56
57 int efx_mcdi_init(struct efx_nic *efx)
58 {
59         struct efx_mcdi_iface *mcdi;
60         bool already_attached;
61         int rc;
62
63         efx->mcdi = kzalloc(sizeof(*efx->mcdi), GFP_KERNEL);
64         if (!efx->mcdi)
65                 return -ENOMEM;
66
67         mcdi = efx_mcdi(efx);
68         mcdi->efx = efx;
69         init_waitqueue_head(&mcdi->wq);
70         spin_lock_init(&mcdi->iface_lock);
71         mcdi->state = MCDI_STATE_QUIESCENT;
72         mcdi->mode = MCDI_MODE_POLL;
73         spin_lock_init(&mcdi->async_lock);
74         INIT_LIST_HEAD(&mcdi->async_list);
75         setup_timer(&mcdi->async_timer, efx_mcdi_timeout_async,
76                     (unsigned long)mcdi);
77
78         (void) efx_mcdi_poll_reboot(efx);
79         mcdi->new_epoch = true;
80
81         /* Recover from a failed assertion before probing */
82         rc = efx_mcdi_handle_assertion(efx);
83         if (rc)
84                 return rc;
85
86         /* Let the MC (and BMC, if this is a LOM) know that the driver
87          * is loaded. We should do this before we reset the NIC.
88          */
89         rc = efx_mcdi_drv_attach(efx, true, &already_attached);
90         if (rc) {
91                 netif_err(efx, probe, efx->net_dev,
92                           "Unable to register driver with MCPU\n");
93                 return rc;
94         }
95         if (already_attached)
96                 /* Not a fatal error */
97                 netif_err(efx, probe, efx->net_dev,
98                           "Host already registered with MCPU\n");
99
100         if (efx->mcdi->fn_flags &
101             (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
102                 efx->primary = efx;
103
104         return 0;
105 }
106
107 void efx_mcdi_fini(struct efx_nic *efx)
108 {
109         if (!efx->mcdi)
110                 return;
111
112         BUG_ON(efx->mcdi->iface.state != MCDI_STATE_QUIESCENT);
113
114         /* Relinquish the device (back to the BMC, if this is a LOM) */
115         efx_mcdi_drv_attach(efx, false, NULL);
116
117         kfree(efx->mcdi);
118 }
119
120 static void efx_mcdi_send_request(struct efx_nic *efx, unsigned cmd,
121                                   const efx_dword_t *inbuf, size_t inlen)
122 {
123         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
124         efx_dword_t hdr[2];
125         size_t hdr_len;
126         u32 xflags, seqno;
127
128         BUG_ON(mcdi->state == MCDI_STATE_QUIESCENT);
129
130         /* Serialise with efx_mcdi_ev_cpl() and efx_mcdi_ev_death() */
131         spin_lock_bh(&mcdi->iface_lock);
132         ++mcdi->seqno;
133         spin_unlock_bh(&mcdi->iface_lock);
134
135         seqno = mcdi->seqno & SEQ_MASK;
136         xflags = 0;
137         if (mcdi->mode == MCDI_MODE_EVENTS)
138                 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
139
140         if (efx->type->mcdi_max_ver == 1) {
141                 /* MCDI v1 */
142                 EFX_POPULATE_DWORD_7(hdr[0],
143                                      MCDI_HEADER_RESPONSE, 0,
144                                      MCDI_HEADER_RESYNC, 1,
145                                      MCDI_HEADER_CODE, cmd,
146                                      MCDI_HEADER_DATALEN, inlen,
147                                      MCDI_HEADER_SEQ, seqno,
148                                      MCDI_HEADER_XFLAGS, xflags,
149                                      MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
150                 hdr_len = 4;
151         } else {
152                 /* MCDI v2 */
153                 BUG_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2);
154                 EFX_POPULATE_DWORD_7(hdr[0],
155                                      MCDI_HEADER_RESPONSE, 0,
156                                      MCDI_HEADER_RESYNC, 1,
157                                      MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
158                                      MCDI_HEADER_DATALEN, 0,
159                                      MCDI_HEADER_SEQ, seqno,
160                                      MCDI_HEADER_XFLAGS, xflags,
161                                      MCDI_HEADER_NOT_EPOCH, !mcdi->new_epoch);
162                 EFX_POPULATE_DWORD_2(hdr[1],
163                                      MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd,
164                                      MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen);
165                 hdr_len = 8;
166         }
167
168         efx->type->mcdi_request(efx, hdr, hdr_len, inbuf, inlen);
169
170         mcdi->new_epoch = false;
171 }
172
173 static int efx_mcdi_errno(unsigned int mcdi_err)
174 {
175         switch (mcdi_err) {
176         case 0:
177                 return 0;
178 #define TRANSLATE_ERROR(name)                                   \
179         case MC_CMD_ERR_ ## name:                               \
180                 return -name;
181         TRANSLATE_ERROR(EPERM);
182         TRANSLATE_ERROR(ENOENT);
183         TRANSLATE_ERROR(EINTR);
184         TRANSLATE_ERROR(EAGAIN);
185         TRANSLATE_ERROR(EACCES);
186         TRANSLATE_ERROR(EBUSY);
187         TRANSLATE_ERROR(EINVAL);
188         TRANSLATE_ERROR(EDEADLK);
189         TRANSLATE_ERROR(ENOSYS);
190         TRANSLATE_ERROR(ETIME);
191         TRANSLATE_ERROR(EALREADY);
192         TRANSLATE_ERROR(ENOSPC);
193 #undef TRANSLATE_ERROR
194         case MC_CMD_ERR_ENOTSUP:
195                 return -EOPNOTSUPP;
196         case MC_CMD_ERR_ALLOC_FAIL:
197                 return -ENOBUFS;
198         case MC_CMD_ERR_MAC_EXIST:
199                 return -EADDRINUSE;
200         default:
201                 return -EPROTO;
202         }
203 }
204
205 static void efx_mcdi_read_response_header(struct efx_nic *efx)
206 {
207         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
208         unsigned int respseq, respcmd, error;
209         efx_dword_t hdr;
210
211         efx->type->mcdi_read_response(efx, &hdr, 0, 4);
212         respseq = EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ);
213         respcmd = EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE);
214         error = EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR);
215
216         if (respcmd != MC_CMD_V2_EXTN) {
217                 mcdi->resp_hdr_len = 4;
218                 mcdi->resp_data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
219         } else {
220                 efx->type->mcdi_read_response(efx, &hdr, 4, 4);
221                 mcdi->resp_hdr_len = 8;
222                 mcdi->resp_data_len =
223                         EFX_DWORD_FIELD(hdr, MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
224         }
225
226         if (error && mcdi->resp_data_len == 0) {
227                 netif_err(efx, hw, efx->net_dev, "MC rebooted\n");
228                 mcdi->resprc = -EIO;
229         } else if ((respseq ^ mcdi->seqno) & SEQ_MASK) {
230                 netif_err(efx, hw, efx->net_dev,
231                           "MC response mismatch tx seq 0x%x rx seq 0x%x\n",
232                           respseq, mcdi->seqno);
233                 mcdi->resprc = -EIO;
234         } else if (error) {
235                 efx->type->mcdi_read_response(efx, &hdr, mcdi->resp_hdr_len, 4);
236                 mcdi->resprc =
237                         efx_mcdi_errno(EFX_DWORD_FIELD(hdr, EFX_DWORD_0));
238         } else {
239                 mcdi->resprc = 0;
240         }
241 }
242
243 static bool efx_mcdi_poll_once(struct efx_nic *efx)
244 {
245         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
246
247         rmb();
248         if (!efx->type->mcdi_poll_response(efx))
249                 return false;
250
251         spin_lock_bh(&mcdi->iface_lock);
252         efx_mcdi_read_response_header(efx);
253         spin_unlock_bh(&mcdi->iface_lock);
254
255         return true;
256 }
257
258 static int efx_mcdi_poll(struct efx_nic *efx)
259 {
260         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
261         unsigned long time, finish;
262         unsigned int spins;
263         int rc;
264
265         /* Check for a reboot atomically with respect to efx_mcdi_copyout() */
266         rc = efx_mcdi_poll_reboot(efx);
267         if (rc) {
268                 spin_lock_bh(&mcdi->iface_lock);
269                 mcdi->resprc = rc;
270                 mcdi->resp_hdr_len = 0;
271                 mcdi->resp_data_len = 0;
272                 spin_unlock_bh(&mcdi->iface_lock);
273                 return 0;
274         }
275
276         /* Poll for completion. Poll quickly (once a us) for the 1st jiffy,
277          * because generally mcdi responses are fast. After that, back off
278          * and poll once a jiffy (approximately)
279          */
280         spins = TICK_USEC;
281         finish = jiffies + MCDI_RPC_TIMEOUT;
282
283         while (1) {
284                 if (spins != 0) {
285                         --spins;
286                         udelay(1);
287                 } else {
288                         schedule_timeout_uninterruptible(1);
289                 }
290
291                 time = jiffies;
292
293                 if (efx_mcdi_poll_once(efx))
294                         break;
295
296                 if (time_after(time, finish))
297                         return -ETIMEDOUT;
298         }
299
300         /* Return rc=0 like wait_event_timeout() */
301         return 0;
302 }
303
304 /* Test and clear MC-rebooted flag for this port/function; reset
305  * software state as necessary.
306  */
307 int efx_mcdi_poll_reboot(struct efx_nic *efx)
308 {
309         if (!efx->mcdi)
310                 return 0;
311
312         return efx->type->mcdi_poll_reboot(efx);
313 }
314
315 static bool efx_mcdi_acquire_async(struct efx_mcdi_iface *mcdi)
316 {
317         return cmpxchg(&mcdi->state,
318                        MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_ASYNC) ==
319                 MCDI_STATE_QUIESCENT;
320 }
321
322 static void efx_mcdi_acquire_sync(struct efx_mcdi_iface *mcdi)
323 {
324         /* Wait until the interface becomes QUIESCENT and we win the race
325          * to mark it RUNNING_SYNC.
326          */
327         wait_event(mcdi->wq,
328                    cmpxchg(&mcdi->state,
329                            MCDI_STATE_QUIESCENT, MCDI_STATE_RUNNING_SYNC) ==
330                    MCDI_STATE_QUIESCENT);
331 }
332
333 static int efx_mcdi_await_completion(struct efx_nic *efx)
334 {
335         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
336
337         if (wait_event_timeout(mcdi->wq, mcdi->state == MCDI_STATE_COMPLETED,
338                                MCDI_RPC_TIMEOUT) == 0)
339                 return -ETIMEDOUT;
340
341         /* Check if efx_mcdi_set_mode() switched us back to polled completions.
342          * In which case, poll for completions directly. If efx_mcdi_ev_cpl()
343          * completed the request first, then we'll just end up completing the
344          * request again, which is safe.
345          *
346          * We need an smp_rmb() to synchronise with efx_mcdi_mode_poll(), which
347          * wait_event_timeout() implicitly provides.
348          */
349         if (mcdi->mode == MCDI_MODE_POLL)
350                 return efx_mcdi_poll(efx);
351
352         return 0;
353 }
354
355 /* If the interface is RUNNING_SYNC, switch to COMPLETED and wake the
356  * requester.  Return whether this was done.  Does not take any locks.
357  */
358 static bool efx_mcdi_complete_sync(struct efx_mcdi_iface *mcdi)
359 {
360         if (cmpxchg(&mcdi->state,
361                     MCDI_STATE_RUNNING_SYNC, MCDI_STATE_COMPLETED) ==
362             MCDI_STATE_RUNNING_SYNC) {
363                 wake_up(&mcdi->wq);
364                 return true;
365         }
366
367         return false;
368 }
369
370 static void efx_mcdi_release(struct efx_mcdi_iface *mcdi)
371 {
372         if (mcdi->mode == MCDI_MODE_EVENTS) {
373                 struct efx_mcdi_async_param *async;
374                 struct efx_nic *efx = mcdi->efx;
375
376                 /* Process the asynchronous request queue */
377                 spin_lock_bh(&mcdi->async_lock);
378                 async = list_first_entry_or_null(
379                         &mcdi->async_list, struct efx_mcdi_async_param, list);
380                 if (async) {
381                         mcdi->state = MCDI_STATE_RUNNING_ASYNC;
382                         efx_mcdi_send_request(efx, async->cmd,
383                                               (const efx_dword_t *)(async + 1),
384                                               async->inlen);
385                         mod_timer(&mcdi->async_timer,
386                                   jiffies + MCDI_RPC_TIMEOUT);
387                 }
388                 spin_unlock_bh(&mcdi->async_lock);
389
390                 if (async)
391                         return;
392         }
393
394         mcdi->state = MCDI_STATE_QUIESCENT;
395         wake_up(&mcdi->wq);
396 }
397
398 /* If the interface is RUNNING_ASYNC, switch to COMPLETED, call the
399  * asynchronous completion function, and release the interface.
400  * Return whether this was done.  Must be called in bh-disabled
401  * context.  Will take iface_lock and async_lock.
402  */
403 static bool efx_mcdi_complete_async(struct efx_mcdi_iface *mcdi, bool timeout)
404 {
405         struct efx_nic *efx = mcdi->efx;
406         struct efx_mcdi_async_param *async;
407         size_t hdr_len, data_len, err_len;
408         efx_dword_t *outbuf;
409         MCDI_DECLARE_BUF_OUT_OR_ERR(errbuf, 0);
410         int rc;
411
412         if (cmpxchg(&mcdi->state,
413                     MCDI_STATE_RUNNING_ASYNC, MCDI_STATE_COMPLETED) !=
414             MCDI_STATE_RUNNING_ASYNC)
415                 return false;
416
417         spin_lock(&mcdi->iface_lock);
418         if (timeout) {
419                 /* Ensure that if the completion event arrives later,
420                  * the seqno check in efx_mcdi_ev_cpl() will fail
421                  */
422                 ++mcdi->seqno;
423                 ++mcdi->credits;
424                 rc = -ETIMEDOUT;
425                 hdr_len = 0;
426                 data_len = 0;
427         } else {
428                 rc = mcdi->resprc;
429                 hdr_len = mcdi->resp_hdr_len;
430                 data_len = mcdi->resp_data_len;
431         }
432         spin_unlock(&mcdi->iface_lock);
433
434         /* Stop the timer.  In case the timer function is running, we
435          * must wait for it to return so that there is no possibility
436          * of it aborting the next request.
437          */
438         if (!timeout)
439                 del_timer_sync(&mcdi->async_timer);
440
441         spin_lock(&mcdi->async_lock);
442         async = list_first_entry(&mcdi->async_list,
443                                  struct efx_mcdi_async_param, list);
444         list_del(&async->list);
445         spin_unlock(&mcdi->async_lock);
446
447         outbuf = (efx_dword_t *)(async + 1);
448         efx->type->mcdi_read_response(efx, outbuf, hdr_len,
449                                       min(async->outlen, data_len));
450         if (!timeout && rc && !async->quiet) {
451                 err_len = min(sizeof(errbuf), data_len);
452                 efx->type->mcdi_read_response(efx, errbuf, hdr_len,
453                                               sizeof(errbuf));
454                 efx_mcdi_display_error(efx, async->cmd, async->inlen, errbuf,
455                                        err_len, rc);
456         }
457         async->complete(efx, async->cookie, rc, outbuf, data_len);
458         kfree(async);
459
460         efx_mcdi_release(mcdi);
461
462         return true;
463 }
464
465 static void efx_mcdi_ev_cpl(struct efx_nic *efx, unsigned int seqno,
466                             unsigned int datalen, unsigned int mcdi_err)
467 {
468         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
469         bool wake = false;
470
471         spin_lock(&mcdi->iface_lock);
472
473         if ((seqno ^ mcdi->seqno) & SEQ_MASK) {
474                 if (mcdi->credits)
475                         /* The request has been cancelled */
476                         --mcdi->credits;
477                 else
478                         netif_err(efx, hw, efx->net_dev,
479                                   "MC response mismatch tx seq 0x%x rx "
480                                   "seq 0x%x\n", seqno, mcdi->seqno);
481         } else {
482                 if (efx->type->mcdi_max_ver >= 2) {
483                         /* MCDI v2 responses don't fit in an event */
484                         efx_mcdi_read_response_header(efx);
485                 } else {
486                         mcdi->resprc = efx_mcdi_errno(mcdi_err);
487                         mcdi->resp_hdr_len = 4;
488                         mcdi->resp_data_len = datalen;
489                 }
490
491                 wake = true;
492         }
493
494         spin_unlock(&mcdi->iface_lock);
495
496         if (wake) {
497                 if (!efx_mcdi_complete_async(mcdi, false))
498                         (void) efx_mcdi_complete_sync(mcdi);
499
500                 /* If the interface isn't RUNNING_ASYNC or
501                  * RUNNING_SYNC then we've received a duplicate
502                  * completion after we've already transitioned back to
503                  * QUIESCENT. [A subsequent invocation would increment
504                  * seqno, so would have failed the seqno check].
505                  */
506         }
507 }
508
509 static void efx_mcdi_timeout_async(unsigned long context)
510 {
511         struct efx_mcdi_iface *mcdi = (struct efx_mcdi_iface *)context;
512
513         efx_mcdi_complete_async(mcdi, true);
514 }
515
516 static int
517 efx_mcdi_check_supported(struct efx_nic *efx, unsigned int cmd, size_t inlen)
518 {
519         if (efx->type->mcdi_max_ver < 0 ||
520              (efx->type->mcdi_max_ver < 2 &&
521               cmd > MC_CMD_CMD_SPACE_ESCAPE_7))
522                 return -EINVAL;
523
524         if (inlen > MCDI_CTL_SDU_LEN_MAX_V2 ||
525             (efx->type->mcdi_max_ver < 2 &&
526              inlen > MCDI_CTL_SDU_LEN_MAX_V1))
527                 return -EMSGSIZE;
528
529         return 0;
530 }
531
532 static int _efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
533                                 efx_dword_t *outbuf, size_t outlen,
534                                 size_t *outlen_actual, bool quiet)
535 {
536         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
537         MCDI_DECLARE_BUF_OUT_OR_ERR(errbuf, 0);
538         int rc;
539
540         if (mcdi->mode == MCDI_MODE_POLL)
541                 rc = efx_mcdi_poll(efx);
542         else
543                 rc = efx_mcdi_await_completion(efx);
544
545         if (rc != 0) {
546                 netif_err(efx, hw, efx->net_dev,
547                           "MC command 0x%x inlen %d mode %d timed out\n",
548                           cmd, (int)inlen, mcdi->mode);
549
550                 if (mcdi->mode == MCDI_MODE_EVENTS && efx_mcdi_poll_once(efx)) {
551                         netif_err(efx, hw, efx->net_dev,
552                                   "MCDI request was completed without an event\n");
553                         rc = 0;
554                 }
555
556                 efx_mcdi_abandon(efx);
557
558                 /* Close the race with efx_mcdi_ev_cpl() executing just too late
559                  * and completing a request we've just cancelled, by ensuring
560                  * that the seqno check therein fails.
561                  */
562                 spin_lock_bh(&mcdi->iface_lock);
563                 ++mcdi->seqno;
564                 ++mcdi->credits;
565                 spin_unlock_bh(&mcdi->iface_lock);
566         }
567
568         if (rc != 0) {
569                 if (outlen_actual)
570                         *outlen_actual = 0;
571         } else {
572                 size_t hdr_len, data_len, err_len;
573
574                 /* At the very least we need a memory barrier here to ensure
575                  * we pick up changes from efx_mcdi_ev_cpl(). Protect against
576                  * a spurious efx_mcdi_ev_cpl() running concurrently by
577                  * acquiring the iface_lock. */
578                 spin_lock_bh(&mcdi->iface_lock);
579                 rc = mcdi->resprc;
580                 hdr_len = mcdi->resp_hdr_len;
581                 data_len = mcdi->resp_data_len;
582                 err_len = min(sizeof(errbuf), data_len);
583                 spin_unlock_bh(&mcdi->iface_lock);
584
585                 BUG_ON(rc > 0);
586
587                 efx->type->mcdi_read_response(efx, outbuf, hdr_len,
588                                               min(outlen, data_len));
589                 if (outlen_actual)
590                         *outlen_actual = data_len;
591
592                 efx->type->mcdi_read_response(efx, errbuf, hdr_len, err_len);
593
594                 if (cmd == MC_CMD_REBOOT && rc == -EIO) {
595                         /* Don't reset if MC_CMD_REBOOT returns EIO */
596                 } else if (rc == -EIO || rc == -EINTR) {
597                         netif_err(efx, hw, efx->net_dev, "MC fatal error %d\n",
598                                   -rc);
599                         efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
600                 } else if (rc && !quiet) {
601                         efx_mcdi_display_error(efx, cmd, inlen, errbuf, err_len,
602                                                rc);
603                 }
604
605                 if (rc == -EIO || rc == -EINTR) {
606                         msleep(MCDI_STATUS_SLEEP_MS);
607                         efx_mcdi_poll_reboot(efx);
608                         mcdi->new_epoch = true;
609                 }
610         }
611
612         efx_mcdi_release(mcdi);
613         return rc;
614 }
615
616 static int _efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
617                          const efx_dword_t *inbuf, size_t inlen,
618                          efx_dword_t *outbuf, size_t outlen,
619                          size_t *outlen_actual, bool quiet)
620 {
621         int rc;
622
623         rc = efx_mcdi_rpc_start(efx, cmd, inbuf, inlen);
624         if (rc) {
625                 if (outlen_actual)
626                         *outlen_actual = 0;
627                 return rc;
628         }
629         return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
630                                     outlen_actual, quiet);
631 }
632
633 int efx_mcdi_rpc(struct efx_nic *efx, unsigned cmd,
634                  const efx_dword_t *inbuf, size_t inlen,
635                  efx_dword_t *outbuf, size_t outlen,
636                  size_t *outlen_actual)
637 {
638         return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
639                              outlen_actual, false);
640 }
641
642 /* Normally, on receiving an error code in the MCDI response,
643  * efx_mcdi_rpc will log an error message containing (among other
644  * things) the raw error code, by means of efx_mcdi_display_error.
645  * This _quiet version suppresses that; if the caller wishes to log
646  * the error conditionally on the return code, it should call this
647  * function and is then responsible for calling efx_mcdi_display_error
648  * as needed.
649  */
650 int efx_mcdi_rpc_quiet(struct efx_nic *efx, unsigned cmd,
651                        const efx_dword_t *inbuf, size_t inlen,
652                        efx_dword_t *outbuf, size_t outlen,
653                        size_t *outlen_actual)
654 {
655         return _efx_mcdi_rpc(efx, cmd, inbuf, inlen, outbuf, outlen,
656                              outlen_actual, true);
657 }
658
659 int efx_mcdi_rpc_start(struct efx_nic *efx, unsigned cmd,
660                        const efx_dword_t *inbuf, size_t inlen)
661 {
662         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
663         int rc;
664
665         rc = efx_mcdi_check_supported(efx, cmd, inlen);
666         if (rc)
667                 return rc;
668
669         if (efx->mc_bist_for_other_fn)
670                 return -ENETDOWN;
671
672         if (mcdi->mode == MCDI_MODE_FAIL)
673                 return -ENETDOWN;
674
675         efx_mcdi_acquire_sync(mcdi);
676         efx_mcdi_send_request(efx, cmd, inbuf, inlen);
677         return 0;
678 }
679
680 static int _efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
681                                const efx_dword_t *inbuf, size_t inlen,
682                                size_t outlen,
683                                efx_mcdi_async_completer *complete,
684                                unsigned long cookie, bool quiet)
685 {
686         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
687         struct efx_mcdi_async_param *async;
688         int rc;
689
690         rc = efx_mcdi_check_supported(efx, cmd, inlen);
691         if (rc)
692                 return rc;
693
694         if (efx->mc_bist_for_other_fn)
695                 return -ENETDOWN;
696
697         async = kmalloc(sizeof(*async) + ALIGN(max(inlen, outlen), 4),
698                         GFP_ATOMIC);
699         if (!async)
700                 return -ENOMEM;
701
702         async->cmd = cmd;
703         async->inlen = inlen;
704         async->outlen = outlen;
705         async->quiet = quiet;
706         async->complete = complete;
707         async->cookie = cookie;
708         memcpy(async + 1, inbuf, inlen);
709
710         spin_lock_bh(&mcdi->async_lock);
711
712         if (mcdi->mode == MCDI_MODE_EVENTS) {
713                 list_add_tail(&async->list, &mcdi->async_list);
714
715                 /* If this is at the front of the queue, try to start it
716                  * immediately
717                  */
718                 if (mcdi->async_list.next == &async->list &&
719                     efx_mcdi_acquire_async(mcdi)) {
720                         efx_mcdi_send_request(efx, cmd, inbuf, inlen);
721                         mod_timer(&mcdi->async_timer,
722                                   jiffies + MCDI_RPC_TIMEOUT);
723                 }
724         } else {
725                 kfree(async);
726                 rc = -ENETDOWN;
727         }
728
729         spin_unlock_bh(&mcdi->async_lock);
730
731         return rc;
732 }
733
734 /**
735  * efx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously
736  * @efx: NIC through which to issue the command
737  * @cmd: Command type number
738  * @inbuf: Command parameters
739  * @inlen: Length of command parameters, in bytes
740  * @outlen: Length to allocate for response buffer, in bytes
741  * @complete: Function to be called on completion or cancellation.
742  * @cookie: Arbitrary value to be passed to @complete.
743  *
744  * This function does not sleep and therefore may be called in atomic
745  * context.  It will fail if event queues are disabled or if MCDI
746  * event completions have been disabled due to an error.
747  *
748  * If it succeeds, the @complete function will be called exactly once
749  * in atomic context, when one of the following occurs:
750  * (a) the completion event is received (in NAPI context)
751  * (b) event queues are disabled (in the process that disables them)
752  * (c) the request times-out (in timer context)
753  */
754 int
755 efx_mcdi_rpc_async(struct efx_nic *efx, unsigned int cmd,
756                    const efx_dword_t *inbuf, size_t inlen, size_t outlen,
757                    efx_mcdi_async_completer *complete, unsigned long cookie)
758 {
759         return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
760                                    cookie, false);
761 }
762
763 int efx_mcdi_rpc_async_quiet(struct efx_nic *efx, unsigned int cmd,
764                              const efx_dword_t *inbuf, size_t inlen,
765                              size_t outlen, efx_mcdi_async_completer *complete,
766                              unsigned long cookie)
767 {
768         return _efx_mcdi_rpc_async(efx, cmd, inbuf, inlen, outlen, complete,
769                                    cookie, true);
770 }
771
772 int efx_mcdi_rpc_finish(struct efx_nic *efx, unsigned cmd, size_t inlen,
773                         efx_dword_t *outbuf, size_t outlen,
774                         size_t *outlen_actual)
775 {
776         return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
777                                     outlen_actual, false);
778 }
779
780 int efx_mcdi_rpc_finish_quiet(struct efx_nic *efx, unsigned cmd, size_t inlen,
781                               efx_dword_t *outbuf, size_t outlen,
782                               size_t *outlen_actual)
783 {
784         return _efx_mcdi_rpc_finish(efx, cmd, inlen, outbuf, outlen,
785                                     outlen_actual, true);
786 }
787
788 void efx_mcdi_display_error(struct efx_nic *efx, unsigned cmd,
789                             size_t inlen, efx_dword_t *outbuf,
790                             size_t outlen, int rc)
791 {
792         int code = 0, err_arg = 0;
793
794         if (outlen >= MC_CMD_ERR_CODE_OFST + 4)
795                 code = MCDI_DWORD(outbuf, ERR_CODE);
796         if (outlen >= MC_CMD_ERR_ARG_OFST + 4)
797                 err_arg = MCDI_DWORD(outbuf, ERR_ARG);
798         netif_err(efx, hw, efx->net_dev,
799                   "MC command 0x%x inlen %d failed rc=%d (raw=%d) arg=%d\n",
800                   cmd, (int)inlen, rc, code, err_arg);
801 }
802
803 /* Switch to polled MCDI completions.  This can be called in various
804  * error conditions with various locks held, so it must be lockless.
805  * Caller is responsible for flushing asynchronous requests later.
806  */
807 void efx_mcdi_mode_poll(struct efx_nic *efx)
808 {
809         struct efx_mcdi_iface *mcdi;
810
811         if (!efx->mcdi)
812                 return;
813
814         mcdi = efx_mcdi(efx);
815         /* If already in polling mode, nothing to do.
816          * If in fail-fast state, don't switch to polled completion.
817          * FLR recovery will do that later.
818          */
819         if (mcdi->mode == MCDI_MODE_POLL || mcdi->mode == MCDI_MODE_FAIL)
820                 return;
821
822         /* We can switch from event completion to polled completion, because
823          * mcdi requests are always completed in shared memory. We do this by
824          * switching the mode to POLL'd then completing the request.
825          * efx_mcdi_await_completion() will then call efx_mcdi_poll().
826          *
827          * We need an smp_wmb() to synchronise with efx_mcdi_await_completion(),
828          * which efx_mcdi_complete_sync() provides for us.
829          */
830         mcdi->mode = MCDI_MODE_POLL;
831
832         efx_mcdi_complete_sync(mcdi);
833 }
834
835 /* Flush any running or queued asynchronous requests, after event processing
836  * is stopped
837  */
838 void efx_mcdi_flush_async(struct efx_nic *efx)
839 {
840         struct efx_mcdi_async_param *async, *next;
841         struct efx_mcdi_iface *mcdi;
842
843         if (!efx->mcdi)
844                 return;
845
846         mcdi = efx_mcdi(efx);
847
848         /* We must be in poll or fail mode so no more requests can be queued */
849         BUG_ON(mcdi->mode == MCDI_MODE_EVENTS);
850
851         del_timer_sync(&mcdi->async_timer);
852
853         /* If a request is still running, make sure we give the MC
854          * time to complete it so that the response won't overwrite our
855          * next request.
856          */
857         if (mcdi->state == MCDI_STATE_RUNNING_ASYNC) {
858                 efx_mcdi_poll(efx);
859                 mcdi->state = MCDI_STATE_QUIESCENT;
860         }
861
862         /* Nothing else will access the async list now, so it is safe
863          * to walk it without holding async_lock.  If we hold it while
864          * calling a completer then lockdep may warn that we have
865          * acquired locks in the wrong order.
866          */
867         list_for_each_entry_safe(async, next, &mcdi->async_list, list) {
868                 async->complete(efx, async->cookie, -ENETDOWN, NULL, 0);
869                 list_del(&async->list);
870                 kfree(async);
871         }
872 }
873
874 void efx_mcdi_mode_event(struct efx_nic *efx)
875 {
876         struct efx_mcdi_iface *mcdi;
877
878         if (!efx->mcdi)
879                 return;
880
881         mcdi = efx_mcdi(efx);
882         /* If already in event completion mode, nothing to do.
883          * If in fail-fast state, don't switch to event completion.  FLR
884          * recovery will do that later.
885          */
886         if (mcdi->mode == MCDI_MODE_EVENTS || mcdi->mode == MCDI_MODE_FAIL)
887                 return;
888
889         /* We can't switch from polled to event completion in the middle of a
890          * request, because the completion method is specified in the request.
891          * So acquire the interface to serialise the requestors. We don't need
892          * to acquire the iface_lock to change the mode here, but we do need a
893          * write memory barrier ensure that efx_mcdi_rpc() sees it, which
894          * efx_mcdi_acquire() provides.
895          */
896         efx_mcdi_acquire_sync(mcdi);
897         mcdi->mode = MCDI_MODE_EVENTS;
898         efx_mcdi_release(mcdi);
899 }
900
901 static void efx_mcdi_ev_death(struct efx_nic *efx, int rc)
902 {
903         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
904
905         /* If there is an outstanding MCDI request, it has been terminated
906          * either by a BADASSERT or REBOOT event. If the mcdi interface is
907          * in polled mode, then do nothing because the MC reboot handler will
908          * set the header correctly. However, if the mcdi interface is waiting
909          * for a CMDDONE event it won't receive it [and since all MCDI events
910          * are sent to the same queue, we can't be racing with
911          * efx_mcdi_ev_cpl()]
912          *
913          * If there is an outstanding asynchronous request, we can't
914          * complete it now (efx_mcdi_complete() would deadlock).  The
915          * reset process will take care of this.
916          *
917          * There's a race here with efx_mcdi_send_request(), because
918          * we might receive a REBOOT event *before* the request has
919          * been copied out. In polled mode (during startup) this is
920          * irrelevant, because efx_mcdi_complete_sync() is ignored. In
921          * event mode, this condition is just an edge-case of
922          * receiving a REBOOT event after posting the MCDI
923          * request. Did the mc reboot before or after the copyout? The
924          * best we can do always is just return failure.
925          */
926         spin_lock(&mcdi->iface_lock);
927         if (efx_mcdi_complete_sync(mcdi)) {
928                 if (mcdi->mode == MCDI_MODE_EVENTS) {
929                         mcdi->resprc = rc;
930                         mcdi->resp_hdr_len = 0;
931                         mcdi->resp_data_len = 0;
932                         ++mcdi->credits;
933                 }
934         } else {
935                 int count;
936
937                 /* Consume the status word since efx_mcdi_rpc_finish() won't */
938                 for (count = 0; count < MCDI_STATUS_DELAY_COUNT; ++count) {
939                         if (efx_mcdi_poll_reboot(efx))
940                                 break;
941                         udelay(MCDI_STATUS_DELAY_US);
942                 }
943                 mcdi->new_epoch = true;
944
945                 /* Nobody was waiting for an MCDI request, so trigger a reset */
946                 efx_schedule_reset(efx, RESET_TYPE_MC_FAILURE);
947         }
948
949         spin_unlock(&mcdi->iface_lock);
950 }
951
952 /* The MC is going down in to BIST mode. set the BIST flag to block
953  * new MCDI, cancel any outstanding MCDI and and schedule a BIST-type reset
954  * (which doesn't actually execute a reset, it waits for the controlling
955  * function to reset it).
956  */
957 static void efx_mcdi_ev_bist(struct efx_nic *efx)
958 {
959         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
960
961         spin_lock(&mcdi->iface_lock);
962         efx->mc_bist_for_other_fn = true;
963         if (efx_mcdi_complete_sync(mcdi)) {
964                 if (mcdi->mode == MCDI_MODE_EVENTS) {
965                         mcdi->resprc = -EIO;
966                         mcdi->resp_hdr_len = 0;
967                         mcdi->resp_data_len = 0;
968                         ++mcdi->credits;
969                 }
970         }
971         mcdi->new_epoch = true;
972         efx_schedule_reset(efx, RESET_TYPE_MC_BIST);
973         spin_unlock(&mcdi->iface_lock);
974 }
975
976 /* MCDI timeouts seen, so make all MCDI calls fail-fast and issue an FLR to try
977  * to recover.
978  */
979 static void efx_mcdi_abandon(struct efx_nic *efx)
980 {
981         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
982
983         if (xchg(&mcdi->mode, MCDI_MODE_FAIL) == MCDI_MODE_FAIL)
984                 return; /* it had already been done */
985         netif_dbg(efx, hw, efx->net_dev, "MCDI is timing out; trying to recover\n");
986         efx_schedule_reset(efx, RESET_TYPE_MCDI_TIMEOUT);
987 }
988
989 /* Called from  falcon_process_eventq for MCDI events */
990 void efx_mcdi_process_event(struct efx_channel *channel,
991                             efx_qword_t *event)
992 {
993         struct efx_nic *efx = channel->efx;
994         int code = EFX_QWORD_FIELD(*event, MCDI_EVENT_CODE);
995         u32 data = EFX_QWORD_FIELD(*event, MCDI_EVENT_DATA);
996
997         switch (code) {
998         case MCDI_EVENT_CODE_BADSSERT:
999                 netif_err(efx, hw, efx->net_dev,
1000                           "MC watchdog or assertion failure at 0x%x\n", data);
1001                 efx_mcdi_ev_death(efx, -EINTR);
1002                 break;
1003
1004         case MCDI_EVENT_CODE_PMNOTICE:
1005                 netif_info(efx, wol, efx->net_dev, "MCDI PM event.\n");
1006                 break;
1007
1008         case MCDI_EVENT_CODE_CMDDONE:
1009                 efx_mcdi_ev_cpl(efx,
1010                                 MCDI_EVENT_FIELD(*event, CMDDONE_SEQ),
1011                                 MCDI_EVENT_FIELD(*event, CMDDONE_DATALEN),
1012                                 MCDI_EVENT_FIELD(*event, CMDDONE_ERRNO));
1013                 break;
1014
1015         case MCDI_EVENT_CODE_LINKCHANGE:
1016                 efx_mcdi_process_link_change(efx, event);
1017                 break;
1018         case MCDI_EVENT_CODE_SENSOREVT:
1019                 efx_mcdi_sensor_event(efx, event);
1020                 break;
1021         case MCDI_EVENT_CODE_SCHEDERR:
1022                 netif_dbg(efx, hw, efx->net_dev,
1023                           "MC Scheduler alert (0x%x)\n", data);
1024                 break;
1025         case MCDI_EVENT_CODE_REBOOT:
1026         case MCDI_EVENT_CODE_MC_REBOOT:
1027                 netif_info(efx, hw, efx->net_dev, "MC Reboot\n");
1028                 efx_mcdi_ev_death(efx, -EIO);
1029                 break;
1030         case MCDI_EVENT_CODE_MC_BIST:
1031                 netif_info(efx, hw, efx->net_dev, "MC entered BIST mode\n");
1032                 efx_mcdi_ev_bist(efx);
1033                 break;
1034         case MCDI_EVENT_CODE_MAC_STATS_DMA:
1035                 /* MAC stats are gather lazily.  We can ignore this. */
1036                 break;
1037         case MCDI_EVENT_CODE_FLR:
1038                 if (efx->type->sriov_flr)
1039                         efx->type->sriov_flr(efx,
1040                                              MCDI_EVENT_FIELD(*event, FLR_VF));
1041                 break;
1042         case MCDI_EVENT_CODE_PTP_RX:
1043         case MCDI_EVENT_CODE_PTP_FAULT:
1044         case MCDI_EVENT_CODE_PTP_PPS:
1045                 efx_ptp_event(efx, event);
1046                 break;
1047         case MCDI_EVENT_CODE_PTP_TIME:
1048                 efx_time_sync_event(channel, event);
1049                 break;
1050         case MCDI_EVENT_CODE_TX_FLUSH:
1051         case MCDI_EVENT_CODE_RX_FLUSH:
1052                 /* Two flush events will be sent: one to the same event
1053                  * queue as completions, and one to event queue 0.
1054                  * In the latter case the {RX,TX}_FLUSH_TO_DRIVER
1055                  * flag will be set, and we should ignore the event
1056                  * because we want to wait for all completions.
1057                  */
1058                 BUILD_BUG_ON(MCDI_EVENT_TX_FLUSH_TO_DRIVER_LBN !=
1059                              MCDI_EVENT_RX_FLUSH_TO_DRIVER_LBN);
1060                 if (!MCDI_EVENT_FIELD(*event, TX_FLUSH_TO_DRIVER))
1061                         efx_ef10_handle_drain_event(efx);
1062                 break;
1063         case MCDI_EVENT_CODE_TX_ERR:
1064         case MCDI_EVENT_CODE_RX_ERR:
1065                 netif_err(efx, hw, efx->net_dev,
1066                           "%s DMA error (event: "EFX_QWORD_FMT")\n",
1067                           code == MCDI_EVENT_CODE_TX_ERR ? "TX" : "RX",
1068                           EFX_QWORD_VAL(*event));
1069                 efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
1070                 break;
1071         default:
1072                 netif_err(efx, hw, efx->net_dev, "Unknown MCDI event 0x%x\n",
1073                           code);
1074         }
1075 }
1076
1077 /**************************************************************************
1078  *
1079  * Specific request functions
1080  *
1081  **************************************************************************
1082  */
1083
1084 void efx_mcdi_print_fwver(struct efx_nic *efx, char *buf, size_t len)
1085 {
1086         MCDI_DECLARE_BUF(outbuf,
1087                          max(MC_CMD_GET_VERSION_OUT_LEN,
1088                              MC_CMD_GET_CAPABILITIES_OUT_LEN));
1089         size_t outlength;
1090         const __le16 *ver_words;
1091         size_t offset;
1092         int rc;
1093
1094         BUILD_BUG_ON(MC_CMD_GET_VERSION_IN_LEN != 0);
1095         rc = efx_mcdi_rpc(efx, MC_CMD_GET_VERSION, NULL, 0,
1096                           outbuf, sizeof(outbuf), &outlength);
1097         if (rc)
1098                 goto fail;
1099         if (outlength < MC_CMD_GET_VERSION_OUT_LEN) {
1100                 rc = -EIO;
1101                 goto fail;
1102         }
1103
1104         ver_words = (__le16 *)MCDI_PTR(outbuf, GET_VERSION_OUT_VERSION);
1105         offset = snprintf(buf, len, "%u.%u.%u.%u",
1106                           le16_to_cpu(ver_words[0]), le16_to_cpu(ver_words[1]),
1107                           le16_to_cpu(ver_words[2]), le16_to_cpu(ver_words[3]));
1108
1109         /* EF10 may have multiple datapath firmware variants within a
1110          * single version.  Report which variants are running.
1111          */
1112         if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
1113                 BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
1114                 rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
1115                                   outbuf, sizeof(outbuf), &outlength);
1116                 if (rc || outlength < MC_CMD_GET_CAPABILITIES_OUT_LEN)
1117                         offset += snprintf(
1118                                 buf + offset, len - offset, " rx? tx?");
1119                 else
1120                         offset += snprintf(
1121                                 buf + offset, len - offset, " rx%x tx%x",
1122                                 MCDI_WORD(outbuf,
1123                                           GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID),
1124                                 MCDI_WORD(outbuf,
1125                                           GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID));
1126
1127                 /* It's theoretically possible for the string to exceed 31
1128                  * characters, though in practice the first three version
1129                  * components are short enough that this doesn't happen.
1130                  */
1131                 if (WARN_ON(offset >= len))
1132                         buf[0] = 0;
1133         }
1134
1135         return;
1136
1137 fail:
1138         netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1139         buf[0] = 0;
1140 }
1141
1142 static int efx_mcdi_drv_attach(struct efx_nic *efx, bool driver_operating,
1143                                bool *was_attached)
1144 {
1145         MCDI_DECLARE_BUF(inbuf, MC_CMD_DRV_ATTACH_IN_LEN);
1146         MCDI_DECLARE_BUF(outbuf, MC_CMD_DRV_ATTACH_EXT_OUT_LEN);
1147         size_t outlen;
1148         int rc;
1149
1150         MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_NEW_STATE,
1151                        driver_operating ? 1 : 0);
1152         MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_UPDATE, 1);
1153         MCDI_SET_DWORD(inbuf, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_LOW_LATENCY);
1154
1155         rc = efx_mcdi_rpc(efx, MC_CMD_DRV_ATTACH, inbuf, sizeof(inbuf),
1156                           outbuf, sizeof(outbuf), &outlen);
1157         if (rc)
1158                 goto fail;
1159         if (outlen < MC_CMD_DRV_ATTACH_OUT_LEN) {
1160                 rc = -EIO;
1161                 goto fail;
1162         }
1163
1164         if (driver_operating) {
1165                 if (outlen >= MC_CMD_DRV_ATTACH_EXT_OUT_LEN) {
1166                         efx->mcdi->fn_flags =
1167                                 MCDI_DWORD(outbuf,
1168                                            DRV_ATTACH_EXT_OUT_FUNC_FLAGS);
1169                 } else {
1170                         /* Synthesise flags for Siena */
1171                         efx->mcdi->fn_flags =
1172                                 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1173                                 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED |
1174                                 (efx_port_num(efx) == 0) <<
1175                                 MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY;
1176                 }
1177         }
1178
1179         /* We currently assume we have control of the external link
1180          * and are completely trusted by firmware.  Abort probing
1181          * if that's not true for this function.
1182          */
1183         if (driver_operating &&
1184             (efx->mcdi->fn_flags &
1185              (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1186               1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) !=
1187             (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL |
1188              1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_TRUSTED)) {
1189                 netif_err(efx, probe, efx->net_dev,
1190                           "This driver version only supports one function per port\n");
1191                 return -ENODEV;
1192         }
1193
1194         if (was_attached != NULL)
1195                 *was_attached = MCDI_DWORD(outbuf, DRV_ATTACH_OUT_OLD_STATE);
1196         return 0;
1197
1198 fail:
1199         netif_err(efx, probe, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1200         return rc;
1201 }
1202
1203 int efx_mcdi_get_board_cfg(struct efx_nic *efx, u8 *mac_address,
1204                            u16 *fw_subtype_list, u32 *capabilities)
1205 {
1206         MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_BOARD_CFG_OUT_LENMAX);
1207         size_t outlen, i;
1208         int port_num = efx_port_num(efx);
1209         int rc;
1210
1211         BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_IN_LEN != 0);
1212         /* we need __aligned(2) for ether_addr_copy */
1213         BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0_OFST & 1);
1214         BUILD_BUG_ON(MC_CMD_GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1_OFST & 1);
1215
1216         rc = efx_mcdi_rpc(efx, MC_CMD_GET_BOARD_CFG, NULL, 0,
1217                           outbuf, sizeof(outbuf), &outlen);
1218         if (rc)
1219                 goto fail;
1220
1221         if (outlen < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
1222                 rc = -EIO;
1223                 goto fail;
1224         }
1225
1226         if (mac_address)
1227                 ether_addr_copy(mac_address,
1228                                 port_num ?
1229                                 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1) :
1230                                 MCDI_PTR(outbuf, GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0));
1231         if (fw_subtype_list) {
1232                 for (i = 0;
1233                      i < MCDI_VAR_ARRAY_LEN(outlen,
1234                                             GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
1235                      i++)
1236                         fw_subtype_list[i] = MCDI_ARRAY_WORD(
1237                                 outbuf, GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST, i);
1238                 for (; i < MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM; i++)
1239                         fw_subtype_list[i] = 0;
1240         }
1241         if (capabilities) {
1242                 if (port_num)
1243                         *capabilities = MCDI_DWORD(outbuf,
1244                                         GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1245                 else
1246                         *capabilities = MCDI_DWORD(outbuf,
1247                                         GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1248         }
1249
1250         return 0;
1251
1252 fail:
1253         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d len=%d\n",
1254                   __func__, rc, (int)outlen);
1255
1256         return rc;
1257 }
1258
1259 int efx_mcdi_log_ctrl(struct efx_nic *efx, bool evq, bool uart, u32 dest_evq)
1260 {
1261         MCDI_DECLARE_BUF(inbuf, MC_CMD_LOG_CTRL_IN_LEN);
1262         u32 dest = 0;
1263         int rc;
1264
1265         if (uart)
1266                 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_UART;
1267         if (evq)
1268                 dest |= MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ;
1269
1270         MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST, dest);
1271         MCDI_SET_DWORD(inbuf, LOG_CTRL_IN_LOG_DEST_EVQ, dest_evq);
1272
1273         BUILD_BUG_ON(MC_CMD_LOG_CTRL_OUT_LEN != 0);
1274
1275         rc = efx_mcdi_rpc(efx, MC_CMD_LOG_CTRL, inbuf, sizeof(inbuf),
1276                           NULL, 0, NULL);
1277         return rc;
1278 }
1279
1280 int efx_mcdi_nvram_types(struct efx_nic *efx, u32 *nvram_types_out)
1281 {
1282         MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TYPES_OUT_LEN);
1283         size_t outlen;
1284         int rc;
1285
1286         BUILD_BUG_ON(MC_CMD_NVRAM_TYPES_IN_LEN != 0);
1287
1288         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TYPES, NULL, 0,
1289                           outbuf, sizeof(outbuf), &outlen);
1290         if (rc)
1291                 goto fail;
1292         if (outlen < MC_CMD_NVRAM_TYPES_OUT_LEN) {
1293                 rc = -EIO;
1294                 goto fail;
1295         }
1296
1297         *nvram_types_out = MCDI_DWORD(outbuf, NVRAM_TYPES_OUT_TYPES);
1298         return 0;
1299
1300 fail:
1301         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
1302                   __func__, rc);
1303         return rc;
1304 }
1305
1306 int efx_mcdi_nvram_info(struct efx_nic *efx, unsigned int type,
1307                         size_t *size_out, size_t *erase_size_out,
1308                         bool *protected_out)
1309 {
1310         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_INFO_IN_LEN);
1311         MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_INFO_OUT_LEN);
1312         size_t outlen;
1313         int rc;
1314
1315         MCDI_SET_DWORD(inbuf, NVRAM_INFO_IN_TYPE, type);
1316
1317         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_INFO, inbuf, sizeof(inbuf),
1318                           outbuf, sizeof(outbuf), &outlen);
1319         if (rc)
1320                 goto fail;
1321         if (outlen < MC_CMD_NVRAM_INFO_OUT_LEN) {
1322                 rc = -EIO;
1323                 goto fail;
1324         }
1325
1326         *size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_SIZE);
1327         *erase_size_out = MCDI_DWORD(outbuf, NVRAM_INFO_OUT_ERASESIZE);
1328         *protected_out = !!(MCDI_DWORD(outbuf, NVRAM_INFO_OUT_FLAGS) &
1329                                 (1 << MC_CMD_NVRAM_INFO_OUT_PROTECTED_LBN));
1330         return 0;
1331
1332 fail:
1333         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1334         return rc;
1335 }
1336
1337 static int efx_mcdi_nvram_test(struct efx_nic *efx, unsigned int type)
1338 {
1339         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_TEST_IN_LEN);
1340         MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_TEST_OUT_LEN);
1341         int rc;
1342
1343         MCDI_SET_DWORD(inbuf, NVRAM_TEST_IN_TYPE, type);
1344
1345         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_TEST, inbuf, sizeof(inbuf),
1346                           outbuf, sizeof(outbuf), NULL);
1347         if (rc)
1348                 return rc;
1349
1350         switch (MCDI_DWORD(outbuf, NVRAM_TEST_OUT_RESULT)) {
1351         case MC_CMD_NVRAM_TEST_PASS:
1352         case MC_CMD_NVRAM_TEST_NOTSUPP:
1353                 return 0;
1354         default:
1355                 return -EIO;
1356         }
1357 }
1358
1359 int efx_mcdi_nvram_test_all(struct efx_nic *efx)
1360 {
1361         u32 nvram_types;
1362         unsigned int type;
1363         int rc;
1364
1365         rc = efx_mcdi_nvram_types(efx, &nvram_types);
1366         if (rc)
1367                 goto fail1;
1368
1369         type = 0;
1370         while (nvram_types != 0) {
1371                 if (nvram_types & 1) {
1372                         rc = efx_mcdi_nvram_test(efx, type);
1373                         if (rc)
1374                                 goto fail2;
1375                 }
1376                 type++;
1377                 nvram_types >>= 1;
1378         }
1379
1380         return 0;
1381
1382 fail2:
1383         netif_err(efx, hw, efx->net_dev, "%s: failed type=%u\n",
1384                   __func__, type);
1385 fail1:
1386         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1387         return rc;
1388 }
1389
1390 static int efx_mcdi_read_assertion(struct efx_nic *efx)
1391 {
1392         MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_ASSERTS_IN_LEN);
1393         MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, MC_CMD_GET_ASSERTS_OUT_LEN);
1394         unsigned int flags, index;
1395         const char *reason;
1396         size_t outlen;
1397         int retry;
1398         int rc;
1399
1400         /* Attempt to read any stored assertion state before we reboot
1401          * the mcfw out of the assertion handler. Retry twice, once
1402          * because a boot-time assertion might cause this command to fail
1403          * with EINTR. And once again because GET_ASSERTS can race with
1404          * MC_CMD_REBOOT running on the other port. */
1405         retry = 2;
1406         do {
1407                 MCDI_SET_DWORD(inbuf, GET_ASSERTS_IN_CLEAR, 1);
1408                 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_GET_ASSERTS,
1409                                         inbuf, MC_CMD_GET_ASSERTS_IN_LEN,
1410                                         outbuf, sizeof(outbuf), &outlen);
1411         } while ((rc == -EINTR || rc == -EIO) && retry-- > 0);
1412
1413         if (rc) {
1414                 efx_mcdi_display_error(efx, MC_CMD_GET_ASSERTS,
1415                                        MC_CMD_GET_ASSERTS_IN_LEN, outbuf,
1416                                        outlen, rc);
1417                 return rc;
1418         }
1419         if (outlen < MC_CMD_GET_ASSERTS_OUT_LEN)
1420                 return -EIO;
1421
1422         /* Print out any recorded assertion state */
1423         flags = MCDI_DWORD(outbuf, GET_ASSERTS_OUT_GLOBAL_FLAGS);
1424         if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1425                 return 0;
1426
1427         reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1428                 ? "system-level assertion"
1429                 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1430                 ? "thread-level assertion"
1431                 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1432                 ? "watchdog reset"
1433                 : "unknown assertion";
1434         netif_err(efx, hw, efx->net_dev,
1435                   "MCPU %s at PC = 0x%.8x in thread 0x%.8x\n", reason,
1436                   MCDI_DWORD(outbuf, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1437                   MCDI_DWORD(outbuf, GET_ASSERTS_OUT_THREAD_OFFS));
1438
1439         /* Print out the registers */
1440         for (index = 0;
1441              index < MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1442              index++)
1443                 netif_err(efx, hw, efx->net_dev, "R%.2d (?): 0x%.8x\n",
1444                           1 + index,
1445                           MCDI_ARRAY_DWORD(outbuf, GET_ASSERTS_OUT_GP_REGS_OFFS,
1446                                            index));
1447
1448         return 0;
1449 }
1450
1451 static void efx_mcdi_exit_assertion(struct efx_nic *efx)
1452 {
1453         MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
1454
1455         /* If the MC is running debug firmware, it might now be
1456          * waiting for a debugger to attach, but we just want it to
1457          * reboot.  We set a flag that makes the command a no-op if it
1458          * has already done so.  We don't know what return code to
1459          * expect (0 or -EIO), so ignore it.
1460          */
1461         BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1462         MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS,
1463                        MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION);
1464         (void) efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, MC_CMD_REBOOT_IN_LEN,
1465                             NULL, 0, NULL);
1466 }
1467
1468 int efx_mcdi_handle_assertion(struct efx_nic *efx)
1469 {
1470         int rc;
1471
1472         rc = efx_mcdi_read_assertion(efx);
1473         if (rc)
1474                 return rc;
1475
1476         efx_mcdi_exit_assertion(efx);
1477
1478         return 0;
1479 }
1480
1481 void efx_mcdi_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1482 {
1483         MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_ID_LED_IN_LEN);
1484         int rc;
1485
1486         BUILD_BUG_ON(EFX_LED_OFF != MC_CMD_LED_OFF);
1487         BUILD_BUG_ON(EFX_LED_ON != MC_CMD_LED_ON);
1488         BUILD_BUG_ON(EFX_LED_DEFAULT != MC_CMD_LED_DEFAULT);
1489
1490         BUILD_BUG_ON(MC_CMD_SET_ID_LED_OUT_LEN != 0);
1491
1492         MCDI_SET_DWORD(inbuf, SET_ID_LED_IN_STATE, mode);
1493
1494         rc = efx_mcdi_rpc(efx, MC_CMD_SET_ID_LED, inbuf, sizeof(inbuf),
1495                           NULL, 0, NULL);
1496 }
1497
1498 static int efx_mcdi_reset_func(struct efx_nic *efx)
1499 {
1500         MCDI_DECLARE_BUF(inbuf, MC_CMD_ENTITY_RESET_IN_LEN);
1501         int rc;
1502
1503         BUILD_BUG_ON(MC_CMD_ENTITY_RESET_OUT_LEN != 0);
1504         MCDI_POPULATE_DWORD_1(inbuf, ENTITY_RESET_IN_FLAG,
1505                               ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1506         rc = efx_mcdi_rpc(efx, MC_CMD_ENTITY_RESET, inbuf, sizeof(inbuf),
1507                           NULL, 0, NULL);
1508         return rc;
1509 }
1510
1511 static int efx_mcdi_reset_mc(struct efx_nic *efx)
1512 {
1513         MCDI_DECLARE_BUF(inbuf, MC_CMD_REBOOT_IN_LEN);
1514         int rc;
1515
1516         BUILD_BUG_ON(MC_CMD_REBOOT_OUT_LEN != 0);
1517         MCDI_SET_DWORD(inbuf, REBOOT_IN_FLAGS, 0);
1518         rc = efx_mcdi_rpc(efx, MC_CMD_REBOOT, inbuf, sizeof(inbuf),
1519                           NULL, 0, NULL);
1520         /* White is black, and up is down */
1521         if (rc == -EIO)
1522                 return 0;
1523         if (rc == 0)
1524                 rc = -EIO;
1525         return rc;
1526 }
1527
1528 enum reset_type efx_mcdi_map_reset_reason(enum reset_type reason)
1529 {
1530         return RESET_TYPE_RECOVER_OR_ALL;
1531 }
1532
1533 int efx_mcdi_reset(struct efx_nic *efx, enum reset_type method)
1534 {
1535         int rc;
1536
1537         /* If MCDI is down, we can't handle_assertion */
1538         if (method == RESET_TYPE_MCDI_TIMEOUT) {
1539                 rc = pci_reset_function(efx->pci_dev);
1540                 if (rc)
1541                         return rc;
1542                 /* Re-enable polled MCDI completion */
1543                 if (efx->mcdi) {
1544                         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1545                         mcdi->mode = MCDI_MODE_POLL;
1546                 }
1547                 return 0;
1548         }
1549
1550         /* Recover from a failed assertion pre-reset */
1551         rc = efx_mcdi_handle_assertion(efx);
1552         if (rc)
1553                 return rc;
1554
1555         if (method == RESET_TYPE_WORLD)
1556                 return efx_mcdi_reset_mc(efx);
1557         else
1558                 return efx_mcdi_reset_func(efx);
1559 }
1560
1561 static int efx_mcdi_wol_filter_set(struct efx_nic *efx, u32 type,
1562                                    const u8 *mac, int *id_out)
1563 {
1564         MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_SET_IN_LEN);
1565         MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_SET_OUT_LEN);
1566         size_t outlen;
1567         int rc;
1568
1569         MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_WOL_TYPE, type);
1570         MCDI_SET_DWORD(inbuf, WOL_FILTER_SET_IN_FILTER_MODE,
1571                        MC_CMD_FILTER_MODE_SIMPLE);
1572         ether_addr_copy(MCDI_PTR(inbuf, WOL_FILTER_SET_IN_MAGIC_MAC), mac);
1573
1574         rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_SET, inbuf, sizeof(inbuf),
1575                           outbuf, sizeof(outbuf), &outlen);
1576         if (rc)
1577                 goto fail;
1578
1579         if (outlen < MC_CMD_WOL_FILTER_SET_OUT_LEN) {
1580                 rc = -EIO;
1581                 goto fail;
1582         }
1583
1584         *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_SET_OUT_FILTER_ID);
1585
1586         return 0;
1587
1588 fail:
1589         *id_out = -1;
1590         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1591         return rc;
1592
1593 }
1594
1595
1596 int
1597 efx_mcdi_wol_filter_set_magic(struct efx_nic *efx,  const u8 *mac, int *id_out)
1598 {
1599         return efx_mcdi_wol_filter_set(efx, MC_CMD_WOL_TYPE_MAGIC, mac, id_out);
1600 }
1601
1602
1603 int efx_mcdi_wol_filter_get_magic(struct efx_nic *efx, int *id_out)
1604 {
1605         MCDI_DECLARE_BUF(outbuf, MC_CMD_WOL_FILTER_GET_OUT_LEN);
1606         size_t outlen;
1607         int rc;
1608
1609         rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_GET, NULL, 0,
1610                           outbuf, sizeof(outbuf), &outlen);
1611         if (rc)
1612                 goto fail;
1613
1614         if (outlen < MC_CMD_WOL_FILTER_GET_OUT_LEN) {
1615                 rc = -EIO;
1616                 goto fail;
1617         }
1618
1619         *id_out = (int)MCDI_DWORD(outbuf, WOL_FILTER_GET_OUT_FILTER_ID);
1620
1621         return 0;
1622
1623 fail:
1624         *id_out = -1;
1625         netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
1626         return rc;
1627 }
1628
1629
1630 int efx_mcdi_wol_filter_remove(struct efx_nic *efx, int id)
1631 {
1632         MCDI_DECLARE_BUF(inbuf, MC_CMD_WOL_FILTER_REMOVE_IN_LEN);
1633         int rc;
1634
1635         MCDI_SET_DWORD(inbuf, WOL_FILTER_REMOVE_IN_FILTER_ID, (u32)id);
1636
1637         rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_REMOVE, inbuf, sizeof(inbuf),
1638                           NULL, 0, NULL);
1639         return rc;
1640 }
1641
1642 int efx_mcdi_flush_rxqs(struct efx_nic *efx)
1643 {
1644         struct efx_channel *channel;
1645         struct efx_rx_queue *rx_queue;
1646         MCDI_DECLARE_BUF(inbuf,
1647                          MC_CMD_FLUSH_RX_QUEUES_IN_LEN(EFX_MAX_CHANNELS));
1648         int rc, count;
1649
1650         BUILD_BUG_ON(EFX_MAX_CHANNELS >
1651                      MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
1652
1653         count = 0;
1654         efx_for_each_channel(channel, efx) {
1655                 efx_for_each_channel_rx_queue(rx_queue, channel) {
1656                         if (rx_queue->flush_pending) {
1657                                 rx_queue->flush_pending = false;
1658                                 atomic_dec(&efx->rxq_flush_pending);
1659                                 MCDI_SET_ARRAY_DWORD(
1660                                         inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
1661                                         count, efx_rx_queue_index(rx_queue));
1662                                 count++;
1663                         }
1664                 }
1665         }
1666
1667         rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
1668                           MC_CMD_FLUSH_RX_QUEUES_IN_LEN(count), NULL, 0, NULL);
1669         WARN_ON(rc < 0);
1670
1671         return rc;
1672 }
1673
1674 int efx_mcdi_wol_filter_reset(struct efx_nic *efx)
1675 {
1676         int rc;
1677
1678         rc = efx_mcdi_rpc(efx, MC_CMD_WOL_FILTER_RESET, NULL, 0, NULL, 0, NULL);
1679         return rc;
1680 }
1681
1682 int efx_mcdi_set_workaround(struct efx_nic *efx, u32 type, bool enabled)
1683 {
1684         MCDI_DECLARE_BUF(inbuf, MC_CMD_WORKAROUND_IN_LEN);
1685
1686         BUILD_BUG_ON(MC_CMD_WORKAROUND_OUT_LEN != 0);
1687         MCDI_SET_DWORD(inbuf, WORKAROUND_IN_TYPE, type);
1688         MCDI_SET_DWORD(inbuf, WORKAROUND_IN_ENABLED, enabled);
1689         return efx_mcdi_rpc(efx, MC_CMD_WORKAROUND, inbuf, sizeof(inbuf),
1690                             NULL, 0, NULL);
1691 }
1692
1693 #ifdef CONFIG_SFC_MTD
1694
1695 #define EFX_MCDI_NVRAM_LEN_MAX 128
1696
1697 static int efx_mcdi_nvram_update_start(struct efx_nic *efx, unsigned int type)
1698 {
1699         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_START_IN_LEN);
1700         int rc;
1701
1702         MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_START_IN_TYPE, type);
1703
1704         BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_START_OUT_LEN != 0);
1705
1706         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_START, inbuf, sizeof(inbuf),
1707                           NULL, 0, NULL);
1708         return rc;
1709 }
1710
1711 static int efx_mcdi_nvram_read(struct efx_nic *efx, unsigned int type,
1712                                loff_t offset, u8 *buffer, size_t length)
1713 {
1714         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_READ_IN_LEN);
1715         MCDI_DECLARE_BUF(outbuf,
1716                          MC_CMD_NVRAM_READ_OUT_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1717         size_t outlen;
1718         int rc;
1719
1720         MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_TYPE, type);
1721         MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_OFFSET, offset);
1722         MCDI_SET_DWORD(inbuf, NVRAM_READ_IN_LENGTH, length);
1723
1724         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_READ, inbuf, sizeof(inbuf),
1725                           outbuf, sizeof(outbuf), &outlen);
1726         if (rc)
1727                 return rc;
1728
1729         memcpy(buffer, MCDI_PTR(outbuf, NVRAM_READ_OUT_READ_BUFFER), length);
1730         return 0;
1731 }
1732
1733 static int efx_mcdi_nvram_write(struct efx_nic *efx, unsigned int type,
1734                                 loff_t offset, const u8 *buffer, size_t length)
1735 {
1736         MCDI_DECLARE_BUF(inbuf,
1737                          MC_CMD_NVRAM_WRITE_IN_LEN(EFX_MCDI_NVRAM_LEN_MAX));
1738         int rc;
1739
1740         MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_TYPE, type);
1741         MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_OFFSET, offset);
1742         MCDI_SET_DWORD(inbuf, NVRAM_WRITE_IN_LENGTH, length);
1743         memcpy(MCDI_PTR(inbuf, NVRAM_WRITE_IN_WRITE_BUFFER), buffer, length);
1744
1745         BUILD_BUG_ON(MC_CMD_NVRAM_WRITE_OUT_LEN != 0);
1746
1747         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_WRITE, inbuf,
1748                           ALIGN(MC_CMD_NVRAM_WRITE_IN_LEN(length), 4),
1749                           NULL, 0, NULL);
1750         return rc;
1751 }
1752
1753 static int efx_mcdi_nvram_erase(struct efx_nic *efx, unsigned int type,
1754                                 loff_t offset, size_t length)
1755 {
1756         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_ERASE_IN_LEN);
1757         int rc;
1758
1759         MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_TYPE, type);
1760         MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_OFFSET, offset);
1761         MCDI_SET_DWORD(inbuf, NVRAM_ERASE_IN_LENGTH, length);
1762
1763         BUILD_BUG_ON(MC_CMD_NVRAM_ERASE_OUT_LEN != 0);
1764
1765         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_ERASE, inbuf, sizeof(inbuf),
1766                           NULL, 0, NULL);
1767         return rc;
1768 }
1769
1770 static int efx_mcdi_nvram_update_finish(struct efx_nic *efx, unsigned int type)
1771 {
1772         MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_UPDATE_FINISH_IN_LEN);
1773         int rc;
1774
1775         MCDI_SET_DWORD(inbuf, NVRAM_UPDATE_FINISH_IN_TYPE, type);
1776
1777         BUILD_BUG_ON(MC_CMD_NVRAM_UPDATE_FINISH_OUT_LEN != 0);
1778
1779         rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_UPDATE_FINISH, inbuf, sizeof(inbuf),
1780                           NULL, 0, NULL);
1781         return rc;
1782 }
1783
1784 int efx_mcdi_mtd_read(struct mtd_info *mtd, loff_t start,
1785                       size_t len, size_t *retlen, u8 *buffer)
1786 {
1787         struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1788         struct efx_nic *efx = mtd->priv;
1789         loff_t offset = start;
1790         loff_t end = min_t(loff_t, start + len, mtd->size);
1791         size_t chunk;
1792         int rc = 0;
1793
1794         while (offset < end) {
1795                 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1796                 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
1797                                          buffer, chunk);
1798                 if (rc)
1799                         goto out;
1800                 offset += chunk;
1801                 buffer += chunk;
1802         }
1803 out:
1804         *retlen = offset - start;
1805         return rc;
1806 }
1807
1808 int efx_mcdi_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
1809 {
1810         struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1811         struct efx_nic *efx = mtd->priv;
1812         loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
1813         loff_t end = min_t(loff_t, start + len, mtd->size);
1814         size_t chunk = part->common.mtd.erasesize;
1815         int rc = 0;
1816
1817         if (!part->updating) {
1818                 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1819                 if (rc)
1820                         goto out;
1821                 part->updating = true;
1822         }
1823
1824         /* The MCDI interface can in fact do multiple erase blocks at once;
1825          * but erasing may be slow, so we make multiple calls here to avoid
1826          * tripping the MCDI RPC timeout. */
1827         while (offset < end) {
1828                 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
1829                                           chunk);
1830                 if (rc)
1831                         goto out;
1832                 offset += chunk;
1833         }
1834 out:
1835         return rc;
1836 }
1837
1838 int efx_mcdi_mtd_write(struct mtd_info *mtd, loff_t start,
1839                        size_t len, size_t *retlen, const u8 *buffer)
1840 {
1841         struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1842         struct efx_nic *efx = mtd->priv;
1843         loff_t offset = start;
1844         loff_t end = min_t(loff_t, start + len, mtd->size);
1845         size_t chunk;
1846         int rc = 0;
1847
1848         if (!part->updating) {
1849                 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
1850                 if (rc)
1851                         goto out;
1852                 part->updating = true;
1853         }
1854
1855         while (offset < end) {
1856                 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
1857                 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
1858                                           buffer, chunk);
1859                 if (rc)
1860                         goto out;
1861                 offset += chunk;
1862                 buffer += chunk;
1863         }
1864 out:
1865         *retlen = offset - start;
1866         return rc;
1867 }
1868
1869 int efx_mcdi_mtd_sync(struct mtd_info *mtd)
1870 {
1871         struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
1872         struct efx_nic *efx = mtd->priv;
1873         int rc = 0;
1874
1875         if (part->updating) {
1876                 part->updating = false;
1877                 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
1878         }
1879
1880         return rc;
1881 }
1882
1883 void efx_mcdi_mtd_rename(struct efx_mtd_partition *part)
1884 {
1885         struct efx_mcdi_mtd_partition *mcdi_part =
1886                 container_of(part, struct efx_mcdi_mtd_partition, common);
1887         struct efx_nic *efx = part->mtd.priv;
1888
1889         snprintf(part->name, sizeof(part->name), "%s %s:%02x",
1890                  efx->name, part->type_name, mcdi_part->fw_subtype);
1891 }
1892
1893 #endif /* CONFIG_SFC_MTD */