net/sfc/base: import MCDI proxy authorization
[dpdk.git] / drivers / net / sfc / base / efx_mcdi.c
1 /*
2  * Copyright (c) 2008-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34 #if EFSYS_OPT_MCDI
35
36 /*
37  * There are three versions of the MCDI interface:
38  *  - MCDIv0: Siena BootROM. Transport uses MCDIv1 headers.
39  *  - MCDIv1: Siena firmware and Huntington BootROM.
40  *  - MCDIv2: EF10 firmware (Huntington/Medford) and Medford BootROM.
41  *            Transport uses MCDIv2 headers.
42  *
43  * MCDIv2 Header NOT_EPOCH flag
44  * ----------------------------
45  * A new epoch begins at initial startup or after an MC reboot, and defines when
46  * the MC should reject stale MCDI requests.
47  *
48  * The first MCDI request sent by the host should contain NOT_EPOCH=0, and all
49  * subsequent requests (until the next MC reboot) should contain NOT_EPOCH=1.
50  *
51  * After rebooting the MC will fail all requests with NOT_EPOCH=1 by writing a
52  * response with ERROR=1 and DATALEN=0 until a request is seen with NOT_EPOCH=0.
53  */
54
55
56
57         __checkReturn   efx_rc_t
58 efx_mcdi_init(
59         __in            efx_nic_t *enp,
60         __in            const efx_mcdi_transport_t *emtp)
61 {
62         const efx_mcdi_ops_t *emcop;
63         efx_rc_t rc;
64
65         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
66         EFSYS_ASSERT3U(enp->en_mod_flags, ==, 0);
67
68         switch (enp->en_family) {
69
70         default:
71                 EFSYS_ASSERT(0);
72                 rc = ENOTSUP;
73                 goto fail1;
74         }
75
76         if (enp->en_features & EFX_FEATURE_MCDI_DMA) {
77                 /* MCDI requires a DMA buffer in host memory */
78                 if ((emtp == NULL) || (emtp->emt_dma_mem) == NULL) {
79                         rc = EINVAL;
80                         goto fail2;
81                 }
82         }
83         enp->en_mcdi.em_emtp = emtp;
84
85         if (emcop != NULL && emcop->emco_init != NULL) {
86                 if ((rc = emcop->emco_init(enp, emtp)) != 0)
87                         goto fail3;
88         }
89
90         enp->en_mcdi.em_emcop = emcop;
91         enp->en_mod_flags |= EFX_MOD_MCDI;
92
93         return (0);
94
95 fail3:
96         EFSYS_PROBE(fail3);
97 fail2:
98         EFSYS_PROBE(fail2);
99 fail1:
100         EFSYS_PROBE1(fail1, efx_rc_t, rc);
101
102         enp->en_mcdi.em_emcop = NULL;
103         enp->en_mcdi.em_emtp = NULL;
104         enp->en_mod_flags &= ~EFX_MOD_MCDI;
105
106         return (rc);
107 }
108
109                         void
110 efx_mcdi_fini(
111         __in            efx_nic_t *enp)
112 {
113         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
114         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
115
116         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
117         EFSYS_ASSERT3U(enp->en_mod_flags, ==, EFX_MOD_MCDI);
118
119         if (emcop != NULL && emcop->emco_fini != NULL)
120                 emcop->emco_fini(enp);
121
122         emip->emi_port = 0;
123         emip->emi_aborted = 0;
124
125         enp->en_mcdi.em_emcop = NULL;
126         enp->en_mod_flags &= ~EFX_MOD_MCDI;
127 }
128
129                         void
130 efx_mcdi_new_epoch(
131         __in            efx_nic_t *enp)
132 {
133         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
134         efsys_lock_state_t state;
135
136         /* Start a new epoch (allow fresh MCDI requests to succeed) */
137         EFSYS_LOCK(enp->en_eslp, state);
138         emip->emi_new_epoch = B_TRUE;
139         EFSYS_UNLOCK(enp->en_eslp, state);
140 }
141
142 static                  void
143 efx_mcdi_send_request(
144         __in            efx_nic_t *enp,
145         __in            void *hdrp,
146         __in            size_t hdr_len,
147         __in            void *sdup,
148         __in            size_t sdu_len)
149 {
150         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
151
152         emcop->emco_send_request(enp, hdrp, hdr_len, sdup, sdu_len);
153 }
154
155 static                  efx_rc_t
156 efx_mcdi_poll_reboot(
157         __in            efx_nic_t *enp)
158 {
159         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
160         efx_rc_t rc;
161
162         rc = emcop->emco_poll_reboot(enp);
163         return (rc);
164 }
165
166 static                  boolean_t
167 efx_mcdi_poll_response(
168         __in            efx_nic_t *enp)
169 {
170         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
171         boolean_t available;
172
173         available = emcop->emco_poll_response(enp);
174         return (available);
175 }
176
177 static                  void
178 efx_mcdi_read_response(
179         __in            efx_nic_t *enp,
180         __out           void *bufferp,
181         __in            size_t offset,
182         __in            size_t length)
183 {
184         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
185
186         emcop->emco_read_response(enp, bufferp, offset, length);
187 }
188
189                         void
190 efx_mcdi_request_start(
191         __in            efx_nic_t *enp,
192         __in            efx_mcdi_req_t *emrp,
193         __in            boolean_t ev_cpl)
194 {
195 #if EFSYS_OPT_MCDI_LOGGING
196         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
197 #endif
198         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
199         efx_dword_t hdr[2];
200         size_t hdr_len;
201         unsigned int max_version;
202         unsigned int seq;
203         unsigned int xflags;
204         boolean_t new_epoch;
205         efsys_lock_state_t state;
206
207         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
208         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
209         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
210
211         /*
212          * efx_mcdi_request_start() is naturally serialised against both
213          * efx_mcdi_request_poll() and efx_mcdi_ev_cpl()/efx_mcdi_ev_death(),
214          * by virtue of there only being one outstanding MCDI request.
215          * Unfortunately, upper layers may also call efx_mcdi_request_abort()
216          * at any time, to timeout a pending mcdi request, That request may
217          * then subsequently complete, meaning efx_mcdi_ev_cpl() or
218          * efx_mcdi_ev_death() may end up running in parallel with
219          * efx_mcdi_request_start(). This race is handled by ensuring that
220          * %emi_pending_req, %emi_ev_cpl and %emi_seq are protected by the
221          * en_eslp lock.
222          */
223         EFSYS_LOCK(enp->en_eslp, state);
224         EFSYS_ASSERT(emip->emi_pending_req == NULL);
225         emip->emi_pending_req = emrp;
226         emip->emi_ev_cpl = ev_cpl;
227         emip->emi_poll_cnt = 0;
228         seq = emip->emi_seq++ & EFX_MASK32(MCDI_HEADER_SEQ);
229         new_epoch = emip->emi_new_epoch;
230         max_version = emip->emi_max_version;
231         EFSYS_UNLOCK(enp->en_eslp, state);
232
233         xflags = 0;
234         if (ev_cpl)
235                 xflags |= MCDI_HEADER_XFLAGS_EVREQ;
236
237         /*
238          * Huntington firmware supports MCDIv2, but the Huntington BootROM only
239          * supports MCDIv1. Use MCDIv1 headers for MCDIv1 commands where
240          * possible to support this.
241          */
242         if ((max_version >= 2) &&
243             ((emrp->emr_cmd > MC_CMD_CMD_SPACE_ESCAPE_7) ||
244             (emrp->emr_in_length > MCDI_CTL_SDU_LEN_MAX_V1))) {
245                 /* Construct MCDI v2 header */
246                 hdr_len = sizeof (hdr);
247                 EFX_POPULATE_DWORD_8(hdr[0],
248                     MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
249                     MCDI_HEADER_RESYNC, 1,
250                     MCDI_HEADER_DATALEN, 0,
251                     MCDI_HEADER_SEQ, seq,
252                     MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
253                     MCDI_HEADER_ERROR, 0,
254                     MCDI_HEADER_RESPONSE, 0,
255                     MCDI_HEADER_XFLAGS, xflags);
256
257                 EFX_POPULATE_DWORD_2(hdr[1],
258                     MC_CMD_V2_EXTN_IN_EXTENDED_CMD, emrp->emr_cmd,
259                     MC_CMD_V2_EXTN_IN_ACTUAL_LEN, emrp->emr_in_length);
260         } else {
261                 /* Construct MCDI v1 header */
262                 hdr_len = sizeof (hdr[0]);
263                 EFX_POPULATE_DWORD_8(hdr[0],
264                     MCDI_HEADER_CODE, emrp->emr_cmd,
265                     MCDI_HEADER_RESYNC, 1,
266                     MCDI_HEADER_DATALEN, emrp->emr_in_length,
267                     MCDI_HEADER_SEQ, seq,
268                     MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
269                     MCDI_HEADER_ERROR, 0,
270                     MCDI_HEADER_RESPONSE, 0,
271                     MCDI_HEADER_XFLAGS, xflags);
272         }
273
274 #if EFSYS_OPT_MCDI_LOGGING
275         if (emtp->emt_logger != NULL) {
276                 emtp->emt_logger(emtp->emt_context, EFX_LOG_MCDI_REQUEST,
277                     &hdr[0], hdr_len,
278                     emrp->emr_in_buf, emrp->emr_in_length);
279         }
280 #endif /* EFSYS_OPT_MCDI_LOGGING */
281
282         efx_mcdi_send_request(enp, &hdr[0], hdr_len,
283             emrp->emr_in_buf, emrp->emr_in_length);
284 }
285
286
287 static                  void
288 efx_mcdi_read_response_header(
289         __in            efx_nic_t *enp,
290         __inout         efx_mcdi_req_t *emrp)
291 {
292 #if EFSYS_OPT_MCDI_LOGGING
293         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
294 #endif /* EFSYS_OPT_MCDI_LOGGING */
295         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
296         efx_dword_t hdr[2];
297         unsigned int hdr_len;
298         unsigned int data_len;
299         unsigned int seq;
300         unsigned int cmd;
301         unsigned int error;
302         efx_rc_t rc;
303
304         EFSYS_ASSERT(emrp != NULL);
305
306         efx_mcdi_read_response(enp, &hdr[0], 0, sizeof (hdr[0]));
307         hdr_len = sizeof (hdr[0]);
308
309         cmd = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE);
310         seq = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_SEQ);
311         error = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_ERROR);
312
313         if (cmd != MC_CMD_V2_EXTN) {
314                 data_len = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_DATALEN);
315         } else {
316                 efx_mcdi_read_response(enp, &hdr[1], hdr_len, sizeof (hdr[1]));
317                 hdr_len += sizeof (hdr[1]);
318
319                 cmd = EFX_DWORD_FIELD(hdr[1], MC_CMD_V2_EXTN_IN_EXTENDED_CMD);
320                 data_len =
321                     EFX_DWORD_FIELD(hdr[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
322         }
323
324         if (error && (data_len == 0)) {
325                 /* The MC has rebooted since the request was sent. */
326                 EFSYS_SPIN(EFX_MCDI_STATUS_SLEEP_US);
327                 efx_mcdi_poll_reboot(enp);
328                 rc = EIO;
329                 goto fail1;
330         }
331         if ((cmd != emrp->emr_cmd) ||
332             (seq != ((emip->emi_seq - 1) & EFX_MASK32(MCDI_HEADER_SEQ)))) {
333                 /* Response is for a different request */
334                 rc = EIO;
335                 goto fail2;
336         }
337         if (error) {
338                 efx_dword_t err[2];
339                 unsigned int err_len = MIN(data_len, sizeof (err));
340                 int err_code = MC_CMD_ERR_EPROTO;
341                 int err_arg = 0;
342
343                 /* Read error code (and arg num for MCDI v2 commands) */
344                 efx_mcdi_read_response(enp, &err, hdr_len, err_len);
345
346                 if (err_len >= (MC_CMD_ERR_CODE_OFST + sizeof (efx_dword_t)))
347                         err_code = EFX_DWORD_FIELD(err[0], EFX_DWORD_0);
348 #ifdef WITH_MCDI_V2
349                 if (err_len >= (MC_CMD_ERR_ARG_OFST + sizeof (efx_dword_t)))
350                         err_arg = EFX_DWORD_FIELD(err[1], EFX_DWORD_0);
351 #endif
352                 emrp->emr_err_code = err_code;
353                 emrp->emr_err_arg = err_arg;
354
355 #if EFSYS_OPT_MCDI_PROXY_AUTH
356                 if ((err_code == MC_CMD_ERR_PROXY_PENDING) &&
357                     (err_len == sizeof (err))) {
358                         /*
359                          * The MCDI request would normally fail with EPERM, but
360                          * firmware has forwarded it to an authorization agent
361                          * attached to a privileged PF.
362                          *
363                          * Save the authorization request handle. The client
364                          * must wait for a PROXY_RESPONSE event, or timeout.
365                          */
366                         emrp->emr_proxy_handle = err_arg;
367                 }
368 #endif /* EFSYS_OPT_MCDI_PROXY_AUTH */
369
370 #if EFSYS_OPT_MCDI_LOGGING
371                 if (emtp->emt_logger != NULL) {
372                         emtp->emt_logger(emtp->emt_context,
373                             EFX_LOG_MCDI_RESPONSE,
374                             &hdr[0], hdr_len,
375                             &err[0], err_len);
376                 }
377 #endif /* EFSYS_OPT_MCDI_LOGGING */
378
379                 if (!emrp->emr_quiet) {
380                         EFSYS_PROBE3(mcdi_err_arg, int, emrp->emr_cmd,
381                             int, err_code, int, err_arg);
382                 }
383
384                 rc = efx_mcdi_request_errcode(err_code);
385                 goto fail3;
386         }
387
388         emrp->emr_rc = 0;
389         emrp->emr_out_length_used = data_len;
390 #if EFSYS_OPT_MCDI_PROXY_AUTH
391         emrp->emr_proxy_handle = 0;
392 #endif /* EFSYS_OPT_MCDI_PROXY_AUTH */
393         return;
394
395 fail3:
396 fail2:
397 fail1:
398         emrp->emr_rc = rc;
399         emrp->emr_out_length_used = 0;
400 }
401
402 static                  void
403 efx_mcdi_finish_response(
404         __in            efx_nic_t *enp,
405         __in            efx_mcdi_req_t *emrp)
406 {
407 #if EFSYS_OPT_MCDI_LOGGING
408         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
409 #endif /* EFSYS_OPT_MCDI_LOGGING */
410         efx_dword_t hdr[2];
411         unsigned int hdr_len;
412         size_t bytes;
413
414         if (emrp->emr_out_buf == NULL)
415                 return;
416
417         /* Read the command header to detect MCDI response format */
418         hdr_len = sizeof (hdr[0]);
419         efx_mcdi_read_response(enp, &hdr[0], 0, hdr_len);
420         if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE) == MC_CMD_V2_EXTN) {
421                 /*
422                  * Read the actual payload length. The length given in the event
423                  * is only correct for responses with the V1 format.
424                  */
425                 efx_mcdi_read_response(enp, &hdr[1], hdr_len, sizeof (hdr[1]));
426                 hdr_len += sizeof (hdr[1]);
427
428                 emrp->emr_out_length_used = EFX_DWORD_FIELD(hdr[1],
429                                             MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
430         }
431
432         /* Copy payload out into caller supplied buffer */
433         bytes = MIN(emrp->emr_out_length_used, emrp->emr_out_length);
434         efx_mcdi_read_response(enp, emrp->emr_out_buf, hdr_len, bytes);
435
436 #if EFSYS_OPT_MCDI_LOGGING
437         if (emtp->emt_logger != NULL) {
438                 emtp->emt_logger(emtp->emt_context,
439                     EFX_LOG_MCDI_RESPONSE,
440                     &hdr[0], hdr_len,
441                     emrp->emr_out_buf, bytes);
442         }
443 #endif /* EFSYS_OPT_MCDI_LOGGING */
444 }
445
446
447         __checkReturn   boolean_t
448 efx_mcdi_request_poll(
449         __in            efx_nic_t *enp)
450 {
451         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
452         efx_mcdi_req_t *emrp;
453         efsys_lock_state_t state;
454         efx_rc_t rc;
455
456         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
457         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
458         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
459
460         /* Serialise against post-watchdog efx_mcdi_ev* */
461         EFSYS_LOCK(enp->en_eslp, state);
462
463         EFSYS_ASSERT(emip->emi_pending_req != NULL);
464         EFSYS_ASSERT(!emip->emi_ev_cpl);
465         emrp = emip->emi_pending_req;
466
467         /* Check for reboot atomically w.r.t efx_mcdi_request_start */
468         if (emip->emi_poll_cnt++ == 0) {
469                 if ((rc = efx_mcdi_poll_reboot(enp)) != 0) {
470                         emip->emi_pending_req = NULL;
471                         EFSYS_UNLOCK(enp->en_eslp, state);
472
473                         /* Reboot/Assertion */
474                         if (rc == EIO || rc == EINTR)
475                                 efx_mcdi_raise_exception(enp, emrp, rc);
476
477                         goto fail1;
478                 }
479         }
480
481         /* Check if a response is available */
482         if (efx_mcdi_poll_response(enp) == B_FALSE) {
483                 EFSYS_UNLOCK(enp->en_eslp, state);
484                 return (B_FALSE);
485         }
486
487         /* Read the response header */
488         efx_mcdi_read_response_header(enp, emrp);
489
490         /* Request complete */
491         emip->emi_pending_req = NULL;
492
493         /* Ensure stale MCDI requests fail after an MC reboot. */
494         emip->emi_new_epoch = B_FALSE;
495
496         EFSYS_UNLOCK(enp->en_eslp, state);
497
498         if ((rc = emrp->emr_rc) != 0)
499                 goto fail2;
500
501         efx_mcdi_finish_response(enp, emrp);
502         return (B_TRUE);
503
504 fail2:
505         if (!emrp->emr_quiet)
506                 EFSYS_PROBE(fail2);
507 fail1:
508         if (!emrp->emr_quiet)
509                 EFSYS_PROBE1(fail1, efx_rc_t, rc);
510
511         return (B_TRUE);
512 }
513
514         __checkReturn   boolean_t
515 efx_mcdi_request_abort(
516         __in            efx_nic_t *enp)
517 {
518         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
519         efx_mcdi_req_t *emrp;
520         boolean_t aborted;
521         efsys_lock_state_t state;
522
523         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
524         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
525         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
526
527         /*
528          * efx_mcdi_ev_* may have already completed this event, and be
529          * spinning/blocked on the upper layer lock. So it *is* legitimate
530          * to for emi_pending_req to be NULL. If there is a pending event
531          * completed request, then provide a "credit" to allow
532          * efx_mcdi_ev_cpl() to accept a single spurious completion.
533          */
534         EFSYS_LOCK(enp->en_eslp, state);
535         emrp = emip->emi_pending_req;
536         aborted = (emrp != NULL);
537         if (aborted) {
538                 emip->emi_pending_req = NULL;
539
540                 /* Error the request */
541                 emrp->emr_out_length_used = 0;
542                 emrp->emr_rc = ETIMEDOUT;
543
544                 /* Provide a credit for seqno/emr_pending_req mismatches */
545                 if (emip->emi_ev_cpl)
546                         ++emip->emi_aborted;
547
548                 /*
549                  * The upper layer has called us, so we don't
550                  * need to complete the request.
551                  */
552         }
553         EFSYS_UNLOCK(enp->en_eslp, state);
554
555         return (aborted);
556 }
557
558                         void
559 efx_mcdi_get_timeout(
560         __in            efx_nic_t *enp,
561         __in            efx_mcdi_req_t *emrp,
562         __out           uint32_t *timeoutp)
563 {
564         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
565
566         emcop->emco_get_timeout(enp, emrp, timeoutp);
567 }
568
569         __checkReturn   efx_rc_t
570 efx_mcdi_request_errcode(
571         __in            unsigned int err)
572 {
573
574         switch (err) {
575                 /* MCDI v1 */
576         case MC_CMD_ERR_EPERM:
577                 return (EACCES);
578         case MC_CMD_ERR_ENOENT:
579                 return (ENOENT);
580         case MC_CMD_ERR_EINTR:
581                 return (EINTR);
582         case MC_CMD_ERR_EACCES:
583                 return (EACCES);
584         case MC_CMD_ERR_EBUSY:
585                 return (EBUSY);
586         case MC_CMD_ERR_EINVAL:
587                 return (EINVAL);
588         case MC_CMD_ERR_EDEADLK:
589                 return (EDEADLK);
590         case MC_CMD_ERR_ENOSYS:
591                 return (ENOTSUP);
592         case MC_CMD_ERR_ETIME:
593                 return (ETIMEDOUT);
594         case MC_CMD_ERR_ENOTSUP:
595                 return (ENOTSUP);
596         case MC_CMD_ERR_EALREADY:
597                 return (EALREADY);
598
599                 /* MCDI v2 */
600         case MC_CMD_ERR_EEXIST:
601                 return (EEXIST);
602 #ifdef MC_CMD_ERR_EAGAIN
603         case MC_CMD_ERR_EAGAIN:
604                 return (EAGAIN);
605 #endif
606 #ifdef MC_CMD_ERR_ENOSPC
607         case MC_CMD_ERR_ENOSPC:
608                 return (ENOSPC);
609 #endif
610         case MC_CMD_ERR_ERANGE:
611                 return (ERANGE);
612
613         case MC_CMD_ERR_ALLOC_FAIL:
614                 return (ENOMEM);
615         case MC_CMD_ERR_NO_VADAPTOR:
616                 return (ENOENT);
617         case MC_CMD_ERR_NO_EVB_PORT:
618                 return (ENOENT);
619         case MC_CMD_ERR_NO_VSWITCH:
620                 return (ENODEV);
621         case MC_CMD_ERR_VLAN_LIMIT:
622                 return (EINVAL);
623         case MC_CMD_ERR_BAD_PCI_FUNC:
624                 return (ENODEV);
625         case MC_CMD_ERR_BAD_VLAN_MODE:
626                 return (EINVAL);
627         case MC_CMD_ERR_BAD_VSWITCH_TYPE:
628                 return (EINVAL);
629         case MC_CMD_ERR_BAD_VPORT_TYPE:
630                 return (EINVAL);
631         case MC_CMD_ERR_MAC_EXIST:
632                 return (EEXIST);
633
634         case MC_CMD_ERR_PROXY_PENDING:
635                 return (EAGAIN);
636
637         default:
638                 EFSYS_PROBE1(mc_pcol_error, int, err);
639                 return (EIO);
640         }
641 }
642
643                         void
644 efx_mcdi_raise_exception(
645         __in            efx_nic_t *enp,
646         __in_opt        efx_mcdi_req_t *emrp,
647         __in            int rc)
648 {
649         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
650         efx_mcdi_exception_t exception;
651
652         /* Reboot or Assertion failure only */
653         EFSYS_ASSERT(rc == EIO || rc == EINTR);
654
655         /*
656          * If MC_CMD_REBOOT causes a reboot (dependent on parameters),
657          * then the EIO is not worthy of an exception.
658          */
659         if (emrp != NULL && emrp->emr_cmd == MC_CMD_REBOOT && rc == EIO)
660                 return;
661
662         exception = (rc == EIO)
663                 ? EFX_MCDI_EXCEPTION_MC_REBOOT
664                 : EFX_MCDI_EXCEPTION_MC_BADASSERT;
665
666         emtp->emt_exception(emtp->emt_context, exception);
667 }
668
669                         void
670 efx_mcdi_execute(
671         __in            efx_nic_t *enp,
672         __inout         efx_mcdi_req_t *emrp)
673 {
674         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
675
676         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
677         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
678
679         emrp->emr_quiet = B_FALSE;
680         emtp->emt_execute(emtp->emt_context, emrp);
681 }
682
683                         void
684 efx_mcdi_execute_quiet(
685         __in            efx_nic_t *enp,
686         __inout         efx_mcdi_req_t *emrp)
687 {
688         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
689
690         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
691         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
692
693         emrp->emr_quiet = B_TRUE;
694         emtp->emt_execute(emtp->emt_context, emrp);
695 }
696
697                         void
698 efx_mcdi_ev_cpl(
699         __in            efx_nic_t *enp,
700         __in            unsigned int seq,
701         __in            unsigned int outlen,
702         __in            int errcode)
703 {
704         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
705         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
706         efx_mcdi_req_t *emrp;
707         efsys_lock_state_t state;
708
709         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
710         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
711
712         /*
713          * Serialise against efx_mcdi_request_poll()/efx_mcdi_request_start()
714          * when we're completing an aborted request.
715          */
716         EFSYS_LOCK(enp->en_eslp, state);
717         if (emip->emi_pending_req == NULL || !emip->emi_ev_cpl ||
718             (seq != ((emip->emi_seq - 1) & EFX_MASK32(MCDI_HEADER_SEQ)))) {
719                 EFSYS_ASSERT(emip->emi_aborted > 0);
720                 if (emip->emi_aborted > 0)
721                         --emip->emi_aborted;
722                 EFSYS_UNLOCK(enp->en_eslp, state);
723                 return;
724         }
725
726         emrp = emip->emi_pending_req;
727         emip->emi_pending_req = NULL;
728         EFSYS_UNLOCK(enp->en_eslp, state);
729
730         if (emip->emi_max_version >= 2) {
731                 /* MCDIv2 response details do not fit into an event. */
732                 efx_mcdi_read_response_header(enp, emrp);
733         } else {
734                 if (errcode != 0) {
735                         if (!emrp->emr_quiet) {
736                                 EFSYS_PROBE2(mcdi_err, int, emrp->emr_cmd,
737                                     int, errcode);
738                         }
739                         emrp->emr_out_length_used = 0;
740                         emrp->emr_rc = efx_mcdi_request_errcode(errcode);
741                 } else {
742                         emrp->emr_out_length_used = outlen;
743                         emrp->emr_rc = 0;
744                 }
745         }
746         if (errcode == 0) {
747                 efx_mcdi_finish_response(enp, emrp);
748         }
749
750         emtp->emt_ev_cpl(emtp->emt_context);
751 }
752
753 #if EFSYS_OPT_MCDI_PROXY_AUTH
754
755         __checkReturn   efx_rc_t
756 efx_mcdi_get_proxy_handle(
757         __in            efx_nic_t *enp,
758         __in            efx_mcdi_req_t *emrp,
759         __out           uint32_t *handlep)
760 {
761         efx_rc_t rc;
762
763         /*
764          * Return proxy handle from MCDI request that returned with error
765          * MC_MCD_ERR_PROXY_PENDING. This handle is used to wait for a matching
766          * PROXY_RESPONSE event.
767          */
768         if ((emrp == NULL) || (handlep == NULL)) {
769                 rc = EINVAL;
770                 goto fail1;
771         }
772         if ((emrp->emr_rc != 0) &&
773             (emrp->emr_err_code == MC_CMD_ERR_PROXY_PENDING)) {
774                 *handlep = emrp->emr_proxy_handle;
775                 rc = 0;
776         } else {
777                 *handlep = 0;
778                 rc = ENOENT;
779         }
780         return (rc);
781
782 fail1:
783         EFSYS_PROBE1(fail1, efx_rc_t, rc);
784         return (rc);
785 }
786
787                         void
788 efx_mcdi_ev_proxy_response(
789         __in            efx_nic_t *enp,
790         __in            unsigned int handle,
791         __in            unsigned int status)
792 {
793         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
794         efx_rc_t rc;
795
796         /*
797          * Handle results of an authorization request for a privileged MCDI
798          * command. If authorization was granted then we must re-issue the
799          * original MCDI request. If authorization failed or timed out,
800          * then the original MCDI request should be completed with the
801          * result code from this event.
802          */
803         rc = (status == 0) ? 0 : efx_mcdi_request_errcode(status);
804
805         emtp->emt_ev_proxy_response(emtp->emt_context, handle, rc);
806 }
807 #endif /* EFSYS_OPT_MCDI_PROXY_AUTH */
808
809                         void
810 efx_mcdi_ev_death(
811         __in            efx_nic_t *enp,
812         __in            int rc)
813 {
814         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
815         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
816         efx_mcdi_req_t *emrp = NULL;
817         boolean_t ev_cpl;
818         efsys_lock_state_t state;
819
820         /*
821          * The MCDI request (if there is one) has been terminated, either
822          * by a BADASSERT or REBOOT event.
823          *
824          * If there is an outstanding event-completed MCDI operation, then we
825          * will never receive the completion event (because both MCDI
826          * completions and BADASSERT events are sent to the same evq). So
827          * complete this MCDI op.
828          *
829          * This function might run in parallel with efx_mcdi_request_poll()
830          * for poll completed mcdi requests, and also with
831          * efx_mcdi_request_start() for post-watchdog completions.
832          */
833         EFSYS_LOCK(enp->en_eslp, state);
834         emrp = emip->emi_pending_req;
835         ev_cpl = emip->emi_ev_cpl;
836         if (emrp != NULL && emip->emi_ev_cpl) {
837                 emip->emi_pending_req = NULL;
838
839                 emrp->emr_out_length_used = 0;
840                 emrp->emr_rc = rc;
841                 ++emip->emi_aborted;
842         }
843
844         /*
845          * Since we're running in parallel with a request, consume the
846          * status word before dropping the lock.
847          */
848         if (rc == EIO || rc == EINTR) {
849                 EFSYS_SPIN(EFX_MCDI_STATUS_SLEEP_US);
850                 (void) efx_mcdi_poll_reboot(enp);
851                 emip->emi_new_epoch = B_TRUE;
852         }
853
854         EFSYS_UNLOCK(enp->en_eslp, state);
855
856         efx_mcdi_raise_exception(enp, emrp, rc);
857
858         if (emrp != NULL && ev_cpl)
859                 emtp->emt_ev_cpl(emtp->emt_context);
860 }
861
862         __checkReturn           efx_rc_t
863 efx_mcdi_version(
864         __in                    efx_nic_t *enp,
865         __out_ecount_opt(4)     uint16_t versionp[4],
866         __out_opt               uint32_t *buildp,
867         __out_opt               efx_mcdi_boot_t *statusp)
868 {
869         efx_mcdi_req_t req;
870         uint8_t payload[MAX(MAX(MC_CMD_GET_VERSION_IN_LEN,
871                                 MC_CMD_GET_VERSION_OUT_LEN),
872                             MAX(MC_CMD_GET_BOOT_STATUS_IN_LEN,
873                                 MC_CMD_GET_BOOT_STATUS_OUT_LEN))];
874         efx_word_t *ver_words;
875         uint16_t version[4];
876         uint32_t build;
877         efx_mcdi_boot_t status;
878         efx_rc_t rc;
879
880         EFSYS_ASSERT3U(enp->en_features, &, EFX_FEATURE_MCDI);
881
882         (void) memset(payload, 0, sizeof (payload));
883         req.emr_cmd = MC_CMD_GET_VERSION;
884         req.emr_in_buf = payload;
885         req.emr_in_length = MC_CMD_GET_VERSION_IN_LEN;
886         req.emr_out_buf = payload;
887         req.emr_out_length = MC_CMD_GET_VERSION_OUT_LEN;
888
889         efx_mcdi_execute(enp, &req);
890
891         if (req.emr_rc != 0) {
892                 rc = req.emr_rc;
893                 goto fail1;
894         }
895
896         /* bootrom support */
897         if (req.emr_out_length_used == MC_CMD_GET_VERSION_V0_OUT_LEN) {
898                 version[0] = version[1] = version[2] = version[3] = 0;
899                 build = MCDI_OUT_DWORD(req, GET_VERSION_OUT_FIRMWARE);
900
901                 goto version;
902         }
903
904         if (req.emr_out_length_used < MC_CMD_GET_VERSION_OUT_LEN) {
905                 rc = EMSGSIZE;
906                 goto fail2;
907         }
908
909         ver_words = MCDI_OUT2(req, efx_word_t, GET_VERSION_OUT_VERSION);
910         version[0] = EFX_WORD_FIELD(ver_words[0], EFX_WORD_0);
911         version[1] = EFX_WORD_FIELD(ver_words[1], EFX_WORD_0);
912         version[2] = EFX_WORD_FIELD(ver_words[2], EFX_WORD_0);
913         version[3] = EFX_WORD_FIELD(ver_words[3], EFX_WORD_0);
914         build = MCDI_OUT_DWORD(req, GET_VERSION_OUT_FIRMWARE);
915
916 version:
917         /* The bootrom doesn't understand BOOT_STATUS */
918         if (MC_FW_VERSION_IS_BOOTLOADER(build)) {
919                 status = EFX_MCDI_BOOT_ROM;
920                 goto out;
921         }
922
923         (void) memset(payload, 0, sizeof (payload));
924         req.emr_cmd = MC_CMD_GET_BOOT_STATUS;
925         req.emr_in_buf = payload;
926         req.emr_in_length = MC_CMD_GET_BOOT_STATUS_IN_LEN;
927         req.emr_out_buf = payload;
928         req.emr_out_length = MC_CMD_GET_BOOT_STATUS_OUT_LEN;
929
930         efx_mcdi_execute_quiet(enp, &req);
931
932         if (req.emr_rc == EACCES) {
933                 /* Unprivileged functions cannot access BOOT_STATUS */
934                 status = EFX_MCDI_BOOT_PRIMARY;
935                 version[0] = version[1] = version[2] = version[3] = 0;
936                 build = 0;
937                 goto out;
938         }
939
940         if (req.emr_rc != 0) {
941                 rc = req.emr_rc;
942                 goto fail3;
943         }
944
945         if (req.emr_out_length_used < MC_CMD_GET_BOOT_STATUS_OUT_LEN) {
946                 rc = EMSGSIZE;
947                 goto fail4;
948         }
949
950         if (MCDI_OUT_DWORD_FIELD(req, GET_BOOT_STATUS_OUT_FLAGS,
951             GET_BOOT_STATUS_OUT_FLAGS_PRIMARY))
952                 status = EFX_MCDI_BOOT_PRIMARY;
953         else
954                 status = EFX_MCDI_BOOT_SECONDARY;
955
956 out:
957         if (versionp != NULL)
958                 memcpy(versionp, version, sizeof (version));
959         if (buildp != NULL)
960                 *buildp = build;
961         if (statusp != NULL)
962                 *statusp = status;
963
964         return (0);
965
966 fail4:
967         EFSYS_PROBE(fail4);
968 fail3:
969         EFSYS_PROBE(fail3);
970 fail2:
971         EFSYS_PROBE(fail2);
972 fail1:
973         EFSYS_PROBE1(fail1, efx_rc_t, rc);
974
975         return (rc);
976 }
977
978 static  __checkReturn   efx_rc_t
979 efx_mcdi_do_reboot(
980         __in            efx_nic_t *enp,
981         __in            boolean_t after_assertion)
982 {
983         uint8_t payload[MAX(MC_CMD_REBOOT_IN_LEN, MC_CMD_REBOOT_OUT_LEN)];
984         efx_mcdi_req_t req;
985         efx_rc_t rc;
986
987         /*
988          * We could require the caller to have caused en_mod_flags=0 to
989          * call this function. This doesn't help the other port though,
990          * who's about to get the MC ripped out from underneath them.
991          * Since they have to cope with the subsequent fallout of MCDI
992          * failures, we should as well.
993          */
994         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
995
996         (void) memset(payload, 0, sizeof (payload));
997         req.emr_cmd = MC_CMD_REBOOT;
998         req.emr_in_buf = payload;
999         req.emr_in_length = MC_CMD_REBOOT_IN_LEN;
1000         req.emr_out_buf = payload;
1001         req.emr_out_length = MC_CMD_REBOOT_OUT_LEN;
1002
1003         MCDI_IN_SET_DWORD(req, REBOOT_IN_FLAGS,
1004             (after_assertion ? MC_CMD_REBOOT_FLAGS_AFTER_ASSERTION : 0));
1005
1006         efx_mcdi_execute_quiet(enp, &req);
1007
1008         if (req.emr_rc == EACCES) {
1009                 /* Unprivileged functions cannot reboot the MC. */
1010                 goto out;
1011         }
1012
1013         /* A successful reboot request returns EIO. */
1014         if (req.emr_rc != 0 && req.emr_rc != EIO) {
1015                 rc = req.emr_rc;
1016                 goto fail1;
1017         }
1018
1019 out:
1020         return (0);
1021
1022 fail1:
1023         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1024
1025         return (rc);
1026 }
1027
1028         __checkReturn   efx_rc_t
1029 efx_mcdi_reboot(
1030         __in            efx_nic_t *enp)
1031 {
1032         return (efx_mcdi_do_reboot(enp, B_FALSE));
1033 }
1034
1035         __checkReturn   efx_rc_t
1036 efx_mcdi_exit_assertion_handler(
1037         __in            efx_nic_t *enp)
1038 {
1039         return (efx_mcdi_do_reboot(enp, B_TRUE));
1040 }
1041
1042         __checkReturn   efx_rc_t
1043 efx_mcdi_read_assertion(
1044         __in            efx_nic_t *enp)
1045 {
1046         efx_mcdi_req_t req;
1047         uint8_t payload[MAX(MC_CMD_GET_ASSERTS_IN_LEN,
1048                             MC_CMD_GET_ASSERTS_OUT_LEN)];
1049         const char *reason;
1050         unsigned int flags;
1051         unsigned int index;
1052         unsigned int ofst;
1053         int retry;
1054         efx_rc_t rc;
1055
1056         /*
1057          * Before we attempt to chat to the MC, we should verify that the MC
1058          * isn't in it's assertion handler, either due to a previous reboot,
1059          * or because we're reinitializing due to an eec_exception().
1060          *
1061          * Use GET_ASSERTS to read any assertion state that may be present.
1062          * Retry this command twice. Once because a boot-time assertion failure
1063          * might cause the 1st MCDI request to fail. And once again because
1064          * we might race with efx_mcdi_exit_assertion_handler() running on
1065          * partner port(s) on the same NIC.
1066          */
1067         retry = 2;
1068         do {
1069                 (void) memset(payload, 0, sizeof (payload));
1070                 req.emr_cmd = MC_CMD_GET_ASSERTS;
1071                 req.emr_in_buf = payload;
1072                 req.emr_in_length = MC_CMD_GET_ASSERTS_IN_LEN;
1073                 req.emr_out_buf = payload;
1074                 req.emr_out_length = MC_CMD_GET_ASSERTS_OUT_LEN;
1075
1076                 MCDI_IN_SET_DWORD(req, GET_ASSERTS_IN_CLEAR, 1);
1077                 efx_mcdi_execute_quiet(enp, &req);
1078
1079         } while ((req.emr_rc == EINTR || req.emr_rc == EIO) && retry-- > 0);
1080
1081         if (req.emr_rc != 0) {
1082                 if (req.emr_rc == EACCES) {
1083                         /* Unprivileged functions cannot clear assertions. */
1084                         goto out;
1085                 }
1086                 rc = req.emr_rc;
1087                 goto fail1;
1088         }
1089
1090         if (req.emr_out_length_used < MC_CMD_GET_ASSERTS_OUT_LEN) {
1091                 rc = EMSGSIZE;
1092                 goto fail2;
1093         }
1094
1095         /* Print out any assertion state recorded */
1096         flags = MCDI_OUT_DWORD(req, GET_ASSERTS_OUT_GLOBAL_FLAGS);
1097         if (flags == MC_CMD_GET_ASSERTS_FLAGS_NO_FAILS)
1098                 return (0);
1099
1100         reason = (flags == MC_CMD_GET_ASSERTS_FLAGS_SYS_FAIL)
1101                 ? "system-level assertion"
1102                 : (flags == MC_CMD_GET_ASSERTS_FLAGS_THR_FAIL)
1103                 ? "thread-level assertion"
1104                 : (flags == MC_CMD_GET_ASSERTS_FLAGS_WDOG_FIRED)
1105                 ? "watchdog reset"
1106                 : (flags == MC_CMD_GET_ASSERTS_FLAGS_ADDR_TRAP)
1107                 ? "illegal address trap"
1108                 : "unknown assertion";
1109         EFSYS_PROBE3(mcpu_assertion,
1110             const char *, reason, unsigned int,
1111             MCDI_OUT_DWORD(req, GET_ASSERTS_OUT_SAVED_PC_OFFS),
1112             unsigned int,
1113             MCDI_OUT_DWORD(req, GET_ASSERTS_OUT_THREAD_OFFS));
1114
1115         /* Print out the registers (r1 ... r31) */
1116         ofst = MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_OFST;
1117         for (index = 1;
1118                 index < 1 + MC_CMD_GET_ASSERTS_OUT_GP_REGS_OFFS_NUM;
1119                 index++) {
1120                 EFSYS_PROBE2(mcpu_register, unsigned int, index, unsigned int,
1121                             EFX_DWORD_FIELD(*MCDI_OUT(req, efx_dword_t, ofst),
1122                                             EFX_DWORD_0));
1123                 ofst += sizeof (efx_dword_t);
1124         }
1125         EFSYS_ASSERT(ofst <= MC_CMD_GET_ASSERTS_OUT_LEN);
1126
1127 out:
1128         return (0);
1129
1130 fail2:
1131         EFSYS_PROBE(fail2);
1132 fail1:
1133         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1134
1135         return (rc);
1136 }
1137
1138
1139 /*
1140  * Internal routines for for specific MCDI requests.
1141  */
1142
1143         __checkReturn   efx_rc_t
1144 efx_mcdi_drv_attach(
1145         __in            efx_nic_t *enp,
1146         __in            boolean_t attach)
1147 {
1148         efx_mcdi_req_t req;
1149         uint8_t payload[MAX(MC_CMD_DRV_ATTACH_IN_LEN,
1150                             MC_CMD_DRV_ATTACH_EXT_OUT_LEN)];
1151         efx_rc_t rc;
1152
1153         (void) memset(payload, 0, sizeof (payload));
1154         req.emr_cmd = MC_CMD_DRV_ATTACH;
1155         req.emr_in_buf = payload;
1156         req.emr_in_length = MC_CMD_DRV_ATTACH_IN_LEN;
1157         req.emr_out_buf = payload;
1158         req.emr_out_length = MC_CMD_DRV_ATTACH_EXT_OUT_LEN;
1159
1160         /*
1161          * Use DONT_CARE for the datapath firmware type to ensure that the
1162          * driver can attach to an unprivileged function. The datapath firmware
1163          * type to use is controlled by the 'sfboot' utility.
1164          */
1165         MCDI_IN_SET_DWORD(req, DRV_ATTACH_IN_NEW_STATE, attach ? 1 : 0);
1166         MCDI_IN_SET_DWORD(req, DRV_ATTACH_IN_UPDATE, 1);
1167         MCDI_IN_SET_DWORD(req, DRV_ATTACH_IN_FIRMWARE_ID, MC_CMD_FW_DONT_CARE);
1168
1169         efx_mcdi_execute(enp, &req);
1170
1171         if (req.emr_rc != 0) {
1172                 rc = req.emr_rc;
1173                 goto fail1;
1174         }
1175
1176         if (req.emr_out_length_used < MC_CMD_DRV_ATTACH_OUT_LEN) {
1177                 rc = EMSGSIZE;
1178                 goto fail2;
1179         }
1180
1181         return (0);
1182
1183 fail2:
1184         EFSYS_PROBE(fail2);
1185 fail1:
1186         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1187
1188         return (rc);
1189 }
1190
1191         __checkReturn           efx_rc_t
1192 efx_mcdi_get_board_cfg(
1193         __in                    efx_nic_t *enp,
1194         __out_opt               uint32_t *board_typep,
1195         __out_opt               efx_dword_t *capabilitiesp,
1196         __out_ecount_opt(6)     uint8_t mac_addrp[6])
1197 {
1198         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
1199         efx_mcdi_req_t req;
1200         uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
1201                             MC_CMD_GET_BOARD_CFG_OUT_LENMIN)];
1202         efx_rc_t rc;
1203
1204         (void) memset(payload, 0, sizeof (payload));
1205         req.emr_cmd = MC_CMD_GET_BOARD_CFG;
1206         req.emr_in_buf = payload;
1207         req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
1208         req.emr_out_buf = payload;
1209         req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMIN;
1210
1211         efx_mcdi_execute(enp, &req);
1212
1213         if (req.emr_rc != 0) {
1214                 rc = req.emr_rc;
1215                 goto fail1;
1216         }
1217
1218         if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
1219                 rc = EMSGSIZE;
1220                 goto fail2;
1221         }
1222
1223         if (mac_addrp != NULL) {
1224                 uint8_t *addrp;
1225
1226                 if (emip->emi_port == 1) {
1227                         addrp = MCDI_OUT2(req, uint8_t,
1228                             GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT0);
1229                 } else if (emip->emi_port == 2) {
1230                         addrp = MCDI_OUT2(req, uint8_t,
1231                             GET_BOARD_CFG_OUT_MAC_ADDR_BASE_PORT1);
1232                 } else {
1233                         rc = EINVAL;
1234                         goto fail3;
1235                 }
1236
1237                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
1238         }
1239
1240         if (capabilitiesp != NULL) {
1241                 if (emip->emi_port == 1) {
1242                         *capabilitiesp = *MCDI_OUT2(req, efx_dword_t,
1243                             GET_BOARD_CFG_OUT_CAPABILITIES_PORT0);
1244                 } else if (emip->emi_port == 2) {
1245                         *capabilitiesp = *MCDI_OUT2(req, efx_dword_t,
1246                             GET_BOARD_CFG_OUT_CAPABILITIES_PORT1);
1247                 } else {
1248                         rc = EINVAL;
1249                         goto fail4;
1250                 }
1251         }
1252
1253         if (board_typep != NULL) {
1254                 *board_typep = MCDI_OUT_DWORD(req,
1255                     GET_BOARD_CFG_OUT_BOARD_TYPE);
1256         }
1257
1258         return (0);
1259
1260 fail4:
1261         EFSYS_PROBE(fail4);
1262 fail3:
1263         EFSYS_PROBE(fail3);
1264 fail2:
1265         EFSYS_PROBE(fail2);
1266 fail1:
1267         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1268
1269         return (rc);
1270 }
1271
1272         __checkReturn   efx_rc_t
1273 efx_mcdi_get_resource_limits(
1274         __in            efx_nic_t *enp,
1275         __out_opt       uint32_t *nevqp,
1276         __out_opt       uint32_t *nrxqp,
1277         __out_opt       uint32_t *ntxqp)
1278 {
1279         efx_mcdi_req_t req;
1280         uint8_t payload[MAX(MC_CMD_GET_RESOURCE_LIMITS_IN_LEN,
1281                             MC_CMD_GET_RESOURCE_LIMITS_OUT_LEN)];
1282         efx_rc_t rc;
1283
1284         (void) memset(payload, 0, sizeof (payload));
1285         req.emr_cmd = MC_CMD_GET_RESOURCE_LIMITS;
1286         req.emr_in_buf = payload;
1287         req.emr_in_length = MC_CMD_GET_RESOURCE_LIMITS_IN_LEN;
1288         req.emr_out_buf = payload;
1289         req.emr_out_length = MC_CMD_GET_RESOURCE_LIMITS_OUT_LEN;
1290
1291         efx_mcdi_execute(enp, &req);
1292
1293         if (req.emr_rc != 0) {
1294                 rc = req.emr_rc;
1295                 goto fail1;
1296         }
1297
1298         if (req.emr_out_length_used < MC_CMD_GET_RESOURCE_LIMITS_OUT_LEN) {
1299                 rc = EMSGSIZE;
1300                 goto fail2;
1301         }
1302
1303         if (nevqp != NULL)
1304                 *nevqp = MCDI_OUT_DWORD(req, GET_RESOURCE_LIMITS_OUT_EVQ);
1305         if (nrxqp != NULL)
1306                 *nrxqp = MCDI_OUT_DWORD(req, GET_RESOURCE_LIMITS_OUT_RXQ);
1307         if (ntxqp != NULL)
1308                 *ntxqp = MCDI_OUT_DWORD(req, GET_RESOURCE_LIMITS_OUT_TXQ);
1309
1310         return (0);
1311
1312 fail2:
1313         EFSYS_PROBE(fail2);
1314 fail1:
1315         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1316
1317         return (rc);
1318 }
1319
1320         __checkReturn   efx_rc_t
1321 efx_mcdi_get_phy_cfg(
1322         __in            efx_nic_t *enp)
1323 {
1324         efx_port_t *epp = &(enp->en_port);
1325         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1326         efx_mcdi_req_t req;
1327         uint8_t payload[MAX(MC_CMD_GET_PHY_CFG_IN_LEN,
1328                             MC_CMD_GET_PHY_CFG_OUT_LEN)];
1329         efx_rc_t rc;
1330
1331         (void) memset(payload, 0, sizeof (payload));
1332         req.emr_cmd = MC_CMD_GET_PHY_CFG;
1333         req.emr_in_buf = payload;
1334         req.emr_in_length = MC_CMD_GET_PHY_CFG_IN_LEN;
1335         req.emr_out_buf = payload;
1336         req.emr_out_length = MC_CMD_GET_PHY_CFG_OUT_LEN;
1337
1338         efx_mcdi_execute(enp, &req);
1339
1340         if (req.emr_rc != 0) {
1341                 rc = req.emr_rc;
1342                 goto fail1;
1343         }
1344
1345         if (req.emr_out_length_used < MC_CMD_GET_PHY_CFG_OUT_LEN) {
1346                 rc = EMSGSIZE;
1347                 goto fail2;
1348         }
1349
1350         encp->enc_phy_type = MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_TYPE);
1351 #if EFSYS_OPT_NAMES
1352         (void) strncpy(encp->enc_phy_name,
1353                 MCDI_OUT2(req, char, GET_PHY_CFG_OUT_NAME),
1354                 MIN(sizeof (encp->enc_phy_name) - 1,
1355                     MC_CMD_GET_PHY_CFG_OUT_NAME_LEN));
1356 #endif  /* EFSYS_OPT_NAMES */
1357         (void) memset(encp->enc_phy_revision, 0,
1358             sizeof (encp->enc_phy_revision));
1359         memcpy(encp->enc_phy_revision,
1360                 MCDI_OUT2(req, char, GET_PHY_CFG_OUT_REVISION),
1361                 MIN(sizeof (encp->enc_phy_revision) - 1,
1362                     MC_CMD_GET_PHY_CFG_OUT_REVISION_LEN));
1363
1364         /* Get the media type of the fixed port, if recognised. */
1365         EFX_STATIC_ASSERT(MC_CMD_MEDIA_XAUI == EFX_PHY_MEDIA_XAUI);
1366         EFX_STATIC_ASSERT(MC_CMD_MEDIA_CX4 == EFX_PHY_MEDIA_CX4);
1367         EFX_STATIC_ASSERT(MC_CMD_MEDIA_KX4 == EFX_PHY_MEDIA_KX4);
1368         EFX_STATIC_ASSERT(MC_CMD_MEDIA_XFP == EFX_PHY_MEDIA_XFP);
1369         EFX_STATIC_ASSERT(MC_CMD_MEDIA_SFP_PLUS == EFX_PHY_MEDIA_SFP_PLUS);
1370         EFX_STATIC_ASSERT(MC_CMD_MEDIA_BASE_T == EFX_PHY_MEDIA_BASE_T);
1371         EFX_STATIC_ASSERT(MC_CMD_MEDIA_QSFP_PLUS == EFX_PHY_MEDIA_QSFP_PLUS);
1372         epp->ep_fixed_port_type =
1373                 (efx_phy_media_type_t) MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_MEDIA_TYPE);
1374         if (epp->ep_fixed_port_type >= EFX_PHY_MEDIA_NTYPES)
1375                 epp->ep_fixed_port_type = EFX_PHY_MEDIA_INVALID;
1376
1377         epp->ep_phy_cap_mask =
1378                 MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_SUPPORTED_CAP);
1379
1380         encp->enc_port = (uint8_t)MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_PRT);
1381
1382         /* Populate internal state */
1383         encp->enc_mcdi_mdio_channel =
1384                 (uint8_t)MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_CHANNEL);
1385
1386         return (0);
1387
1388 fail2:
1389         EFSYS_PROBE(fail2);
1390 fail1:
1391         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1392
1393         return (rc);
1394 }
1395
1396         __checkReturn           efx_rc_t
1397 efx_mcdi_firmware_update_supported(
1398         __in                    efx_nic_t *enp,
1399         __out                   boolean_t *supportedp)
1400 {
1401         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
1402         efx_rc_t rc;
1403
1404         if (emcop != NULL) {
1405                 if ((rc = emcop->emco_feature_supported(enp,
1406                             EFX_MCDI_FEATURE_FW_UPDATE, supportedp)) != 0)
1407                         goto fail1;
1408         } else {
1409                 /* Earlier devices always supported updates */
1410                 *supportedp = B_TRUE;
1411         }
1412
1413         return (0);
1414
1415 fail1:
1416         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1417
1418         return (rc);
1419 }
1420
1421         __checkReturn           efx_rc_t
1422 efx_mcdi_macaddr_change_supported(
1423         __in                    efx_nic_t *enp,
1424         __out                   boolean_t *supportedp)
1425 {
1426         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
1427         efx_rc_t rc;
1428
1429         if (emcop != NULL) {
1430                 if ((rc = emcop->emco_feature_supported(enp,
1431                             EFX_MCDI_FEATURE_MACADDR_CHANGE, supportedp)) != 0)
1432                         goto fail1;
1433         } else {
1434                 /* Earlier devices always supported MAC changes */
1435                 *supportedp = B_TRUE;
1436         }
1437
1438         return (0);
1439
1440 fail1:
1441         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1442
1443         return (rc);
1444 }
1445
1446         __checkReturn           efx_rc_t
1447 efx_mcdi_link_control_supported(
1448         __in                    efx_nic_t *enp,
1449         __out                   boolean_t *supportedp)
1450 {
1451         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
1452         efx_rc_t rc;
1453
1454         if (emcop != NULL) {
1455                 if ((rc = emcop->emco_feature_supported(enp,
1456                             EFX_MCDI_FEATURE_LINK_CONTROL, supportedp)) != 0)
1457                         goto fail1;
1458         } else {
1459                 /* Earlier devices always supported link control */
1460                 *supportedp = B_TRUE;
1461         }
1462
1463         return (0);
1464
1465 fail1:
1466         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1467
1468         return (rc);
1469 }
1470
1471         __checkReturn           efx_rc_t
1472 efx_mcdi_mac_spoofing_supported(
1473         __in                    efx_nic_t *enp,
1474         __out                   boolean_t *supportedp)
1475 {
1476         const efx_mcdi_ops_t *emcop = enp->en_mcdi.em_emcop;
1477         efx_rc_t rc;
1478
1479         if (emcop != NULL) {
1480                 if ((rc = emcop->emco_feature_supported(enp,
1481                             EFX_MCDI_FEATURE_MAC_SPOOFING, supportedp)) != 0)
1482                         goto fail1;
1483         } else {
1484                 /* Earlier devices always supported MAC spoofing */
1485                 *supportedp = B_TRUE;
1486         }
1487
1488         return (0);
1489
1490 fail1:
1491         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1492
1493         return (rc);
1494 }
1495
1496
1497 /* Enable logging of some events (e.g. link state changes) */
1498         __checkReturn   efx_rc_t
1499 efx_mcdi_log_ctrl(
1500         __in            efx_nic_t *enp)
1501 {
1502         efx_mcdi_req_t req;
1503         uint8_t payload[MAX(MC_CMD_LOG_CTRL_IN_LEN,
1504                             MC_CMD_LOG_CTRL_OUT_LEN)];
1505         efx_rc_t rc;
1506
1507         (void) memset(payload, 0, sizeof (payload));
1508         req.emr_cmd = MC_CMD_LOG_CTRL;
1509         req.emr_in_buf = payload;
1510         req.emr_in_length = MC_CMD_LOG_CTRL_IN_LEN;
1511         req.emr_out_buf = payload;
1512         req.emr_out_length = MC_CMD_LOG_CTRL_OUT_LEN;
1513
1514         MCDI_IN_SET_DWORD(req, LOG_CTRL_IN_LOG_DEST,
1515                     MC_CMD_LOG_CTRL_IN_LOG_DEST_EVQ);
1516         MCDI_IN_SET_DWORD(req, LOG_CTRL_IN_LOG_DEST_EVQ, 0);
1517
1518         efx_mcdi_execute(enp, &req);
1519
1520         if (req.emr_rc != 0) {
1521                 rc = req.emr_rc;
1522                 goto fail1;
1523         }
1524
1525         return (0);
1526
1527 fail1:
1528         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1529
1530         return (rc);
1531 }
1532
1533
1534         __checkReturn           efx_rc_t
1535 efx_mcdi_set_workaround(
1536         __in                    efx_nic_t *enp,
1537         __in                    uint32_t type,
1538         __in                    boolean_t enabled,
1539         __out_opt               uint32_t *flagsp)
1540 {
1541         efx_mcdi_req_t req;
1542         uint8_t payload[MAX(MC_CMD_WORKAROUND_IN_LEN,
1543                             MC_CMD_WORKAROUND_EXT_OUT_LEN)];
1544         efx_rc_t rc;
1545
1546         (void) memset(payload, 0, sizeof (payload));
1547         req.emr_cmd = MC_CMD_WORKAROUND;
1548         req.emr_in_buf = payload;
1549         req.emr_in_length = MC_CMD_WORKAROUND_IN_LEN;
1550         req.emr_out_buf = payload;
1551         req.emr_out_length = MC_CMD_WORKAROUND_OUT_LEN;
1552
1553         MCDI_IN_SET_DWORD(req, WORKAROUND_IN_TYPE, type);
1554         MCDI_IN_SET_DWORD(req, WORKAROUND_IN_ENABLED, enabled ? 1 : 0);
1555
1556         efx_mcdi_execute_quiet(enp, &req);
1557
1558         if (req.emr_rc != 0) {
1559                 rc = req.emr_rc;
1560                 goto fail1;
1561         }
1562
1563         if (flagsp != NULL) {
1564                 if (req.emr_out_length_used >= MC_CMD_WORKAROUND_EXT_OUT_LEN)
1565                         *flagsp = MCDI_OUT_DWORD(req, WORKAROUND_EXT_OUT_FLAGS);
1566                 else
1567                         *flagsp = 0;
1568         }
1569
1570         return (0);
1571
1572 fail1:
1573         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1574
1575         return (rc);
1576 }
1577
1578
1579         __checkReturn           efx_rc_t
1580 efx_mcdi_get_workarounds(
1581         __in                    efx_nic_t *enp,
1582         __out_opt               uint32_t *implementedp,
1583         __out_opt               uint32_t *enabledp)
1584 {
1585         efx_mcdi_req_t req;
1586         uint8_t payload[MC_CMD_GET_WORKAROUNDS_OUT_LEN];
1587         efx_rc_t rc;
1588
1589         (void) memset(payload, 0, sizeof (payload));
1590         req.emr_cmd = MC_CMD_GET_WORKAROUNDS;
1591         req.emr_in_buf = NULL;
1592         req.emr_in_length = 0;
1593         req.emr_out_buf = payload;
1594         req.emr_out_length = MC_CMD_GET_WORKAROUNDS_OUT_LEN;
1595
1596         efx_mcdi_execute(enp, &req);
1597
1598         if (req.emr_rc != 0) {
1599                 rc = req.emr_rc;
1600                 goto fail1;
1601         }
1602
1603         if (implementedp != NULL) {
1604                 *implementedp =
1605                     MCDI_OUT_DWORD(req, GET_WORKAROUNDS_OUT_IMPLEMENTED);
1606         }
1607
1608         if (enabledp != NULL) {
1609                 *enabledp = MCDI_OUT_DWORD(req, GET_WORKAROUNDS_OUT_ENABLED);
1610         }
1611
1612         return (0);
1613
1614 fail1:
1615         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1616
1617         return (rc);
1618 }
1619
1620 /*
1621  * Size of media information page in accordance with SFF-8472 and SFF-8436.
1622  * It is used in MCDI interface as well.
1623  */
1624 #define EFX_PHY_MEDIA_INFO_PAGE_SIZE            0x80
1625
1626 static  __checkReturn           efx_rc_t
1627 efx_mcdi_get_phy_media_info(
1628         __in                    efx_nic_t *enp,
1629         __in                    uint32_t mcdi_page,
1630         __in                    uint8_t offset,
1631         __in                    uint8_t len,
1632         __out_bcount(len)       uint8_t *data)
1633 {
1634         efx_mcdi_req_t req;
1635         uint8_t payload[MAX(MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN,
1636                             MC_CMD_GET_PHY_MEDIA_INFO_OUT_LEN(
1637                                 EFX_PHY_MEDIA_INFO_PAGE_SIZE))];
1638         efx_rc_t rc;
1639
1640         EFSYS_ASSERT((uint32_t)offset + len <= EFX_PHY_MEDIA_INFO_PAGE_SIZE);
1641
1642         (void) memset(payload, 0, sizeof (payload));
1643         req.emr_cmd = MC_CMD_GET_PHY_MEDIA_INFO;
1644         req.emr_in_buf = payload;
1645         req.emr_in_length = MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN;
1646         req.emr_out_buf = payload;
1647         req.emr_out_length =
1648             MC_CMD_GET_PHY_MEDIA_INFO_OUT_LEN(EFX_PHY_MEDIA_INFO_PAGE_SIZE);
1649
1650         MCDI_IN_SET_DWORD(req, GET_PHY_MEDIA_INFO_IN_PAGE, mcdi_page);
1651
1652         efx_mcdi_execute(enp, &req);
1653
1654         if (req.emr_rc != 0) {
1655                 rc = req.emr_rc;
1656                 goto fail1;
1657         }
1658
1659         if (req.emr_out_length_used !=
1660             MC_CMD_GET_PHY_MEDIA_INFO_OUT_LEN(EFX_PHY_MEDIA_INFO_PAGE_SIZE)) {
1661                 rc = EMSGSIZE;
1662                 goto fail2;
1663         }
1664
1665         if (MCDI_OUT_DWORD(req, GET_PHY_MEDIA_INFO_OUT_DATALEN) !=
1666             EFX_PHY_MEDIA_INFO_PAGE_SIZE) {
1667                 rc = EIO;
1668                 goto fail3;
1669         }
1670
1671         memcpy(data,
1672             MCDI_OUT2(req, uint8_t, GET_PHY_MEDIA_INFO_OUT_DATA) + offset,
1673             len);
1674
1675         return (0);
1676
1677 fail3:
1678         EFSYS_PROBE(fail3);
1679 fail2:
1680         EFSYS_PROBE(fail2);
1681 fail1:
1682         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1683
1684         return (rc);
1685 }
1686
1687 /*
1688  * 2-wire device address of the base information in accordance with SFF-8472
1689  * Diagnostic Monitoring Interface for Optical Transceivers section
1690  * 4 Memory Organization.
1691  */
1692 #define EFX_PHY_MEDIA_INFO_DEV_ADDR_SFP_BASE    0xA0
1693
1694 /*
1695  * 2-wire device address of the digital diagnostics monitoring interface
1696  * in accordance with SFF-8472 Diagnostic Monitoring Interface for Optical
1697  * Transceivers section 4 Memory Organization.
1698  */
1699 #define EFX_PHY_MEDIA_INFO_DEV_ADDR_SFP_DDM     0xA2
1700
1701 /*
1702  * Hard wired 2-wire device address for QSFP+ in accordance with SFF-8436
1703  * QSFP+ 10 Gbs 4X PLUGGABLE TRANSCEIVER section 7.4 Device Addressing and
1704  * Operation.
1705  */
1706 #define EFX_PHY_MEDIA_INFO_DEV_ADDR_QSFP        0xA0
1707
1708         __checkReturn           efx_rc_t
1709 efx_mcdi_phy_module_get_info(
1710         __in                    efx_nic_t *enp,
1711         __in                    uint8_t dev_addr,
1712         __in                    uint8_t offset,
1713         __in                    uint8_t len,
1714         __out_bcount(len)       uint8_t *data)
1715 {
1716         efx_port_t *epp = &(enp->en_port);
1717         efx_rc_t rc;
1718         uint32_t mcdi_lower_page;
1719         uint32_t mcdi_upper_page;
1720
1721         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
1722
1723         /*
1724          * Map device address to MC_CMD_GET_PHY_MEDIA_INFO pages.
1725          * Offset plus length interface allows to access page 0 only.
1726          * I.e. non-zero upper pages are not accessible.
1727          * See SFF-8472 section 4 Memory Organization and SFF-8436 section 7.6
1728          * QSFP+ Memory Map for details on how information is structured
1729          * and accessible.
1730          */
1731         switch (epp->ep_fixed_port_type) {
1732         case EFX_PHY_MEDIA_SFP_PLUS:
1733                 /*
1734                  * In accordance with SFF-8472 Diagnostic Monitoring
1735                  * Interface for Optical Transceivers section 4 Memory
1736                  * Organization two 2-wire addresses are defined.
1737                  */
1738                 switch (dev_addr) {
1739                 /* Base information */
1740                 case EFX_PHY_MEDIA_INFO_DEV_ADDR_SFP_BASE:
1741                         /*
1742                          * MCDI page 0 should be used to access lower
1743                          * page 0 (0x00 - 0x7f) at the device address 0xA0.
1744                          */
1745                         mcdi_lower_page = 0;
1746                         /*
1747                          * MCDI page 1 should be used to access  upper
1748                          * page 0 (0x80 - 0xff) at the device address 0xA0.
1749                          */
1750                         mcdi_upper_page = 1;
1751                         break;
1752                 /* Diagnostics */
1753                 case EFX_PHY_MEDIA_INFO_DEV_ADDR_SFP_DDM:
1754                         /*
1755                          * MCDI page 2 should be used to access lower
1756                          * page 0 (0x00 - 0x7f) at the device address 0xA2.
1757                          */
1758                         mcdi_lower_page = 2;
1759                         /*
1760                          * MCDI page 3 should be used to access upper
1761                          * page 0 (0x80 - 0xff) at the device address 0xA2.
1762                          */
1763                         mcdi_upper_page = 3;
1764                         break;
1765                 default:
1766                         rc = ENOTSUP;
1767                         goto fail1;
1768                 }
1769                 break;
1770         case EFX_PHY_MEDIA_QSFP_PLUS:
1771                 switch (dev_addr) {
1772                 case EFX_PHY_MEDIA_INFO_DEV_ADDR_QSFP:
1773                         /*
1774                          * MCDI page -1 should be used to access lower page 0
1775                          * (0x00 - 0x7f).
1776                          */
1777                         mcdi_lower_page = (uint32_t)-1;
1778                         /*
1779                          * MCDI page 0 should be used to access upper page 0
1780                          * (0x80h - 0xff).
1781                          */
1782                         mcdi_upper_page = 0;
1783                         break;
1784                 default:
1785                         rc = ENOTSUP;
1786                         goto fail1;
1787                 }
1788                 break;
1789         default:
1790                 rc = ENOTSUP;
1791                 goto fail1;
1792         }
1793
1794         if (offset < EFX_PHY_MEDIA_INFO_PAGE_SIZE) {
1795                 uint8_t read_len =
1796                     MIN(len, EFX_PHY_MEDIA_INFO_PAGE_SIZE - offset);
1797
1798                 rc = efx_mcdi_get_phy_media_info(enp,
1799                     mcdi_lower_page, offset, read_len, data);
1800                 if (rc != 0)
1801                         goto fail2;
1802
1803                 data += read_len;
1804                 len -= read_len;
1805
1806                 offset = 0;
1807         } else {
1808                 offset -= EFX_PHY_MEDIA_INFO_PAGE_SIZE;
1809         }
1810
1811         if (len > 0) {
1812                 EFSYS_ASSERT3U(len, <=, EFX_PHY_MEDIA_INFO_PAGE_SIZE);
1813                 EFSYS_ASSERT3U(offset, <, EFX_PHY_MEDIA_INFO_PAGE_SIZE);
1814
1815                 rc = efx_mcdi_get_phy_media_info(enp,
1816                     mcdi_upper_page, offset, len, data);
1817                 if (rc != 0)
1818                         goto fail3;
1819         }
1820
1821         return (0);
1822
1823 fail3:
1824         EFSYS_PROBE(fail3);
1825 fail2:
1826         EFSYS_PROBE(fail2);
1827 fail1:
1828         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1829
1830         return (rc);
1831 }
1832
1833 #endif  /* EFSYS_OPT_MCDI */