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