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