7af893524472af468c899f6ab387353bad15eaa5
[dpdk.git] / drivers / net / sfc / base / ef10_nic.c
1 /*
2  * Copyright (c) 2012-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 #if EFSYS_OPT_MON_MCDI
34 #include "mcdi_mon.h"
35 #endif
36
37 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
38
39 #include "ef10_tlv_layout.h"
40
41         __checkReturn   efx_rc_t
42 efx_mcdi_get_port_assignment(
43         __in            efx_nic_t *enp,
44         __out           uint32_t *portp)
45 {
46         efx_mcdi_req_t req;
47         uint8_t payload[MAX(MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN,
48                             MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN)];
49         efx_rc_t rc;
50
51         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
52                     enp->en_family == EFX_FAMILY_MEDFORD);
53
54         (void) memset(payload, 0, sizeof (payload));
55         req.emr_cmd = MC_CMD_GET_PORT_ASSIGNMENT;
56         req.emr_in_buf = payload;
57         req.emr_in_length = MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN;
58         req.emr_out_buf = payload;
59         req.emr_out_length = MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN;
60
61         efx_mcdi_execute(enp, &req);
62
63         if (req.emr_rc != 0) {
64                 rc = req.emr_rc;
65                 goto fail1;
66         }
67
68         if (req.emr_out_length_used < MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN) {
69                 rc = EMSGSIZE;
70                 goto fail2;
71         }
72
73         *portp = MCDI_OUT_DWORD(req, GET_PORT_ASSIGNMENT_OUT_PORT);
74
75         return (0);
76
77 fail2:
78         EFSYS_PROBE(fail2);
79 fail1:
80         EFSYS_PROBE1(fail1, efx_rc_t, rc);
81
82         return (rc);
83 }
84
85         __checkReturn   efx_rc_t
86 efx_mcdi_get_port_modes(
87         __in            efx_nic_t *enp,
88         __out           uint32_t *modesp,
89         __out_opt       uint32_t *current_modep)
90 {
91         efx_mcdi_req_t req;
92         uint8_t payload[MAX(MC_CMD_GET_PORT_MODES_IN_LEN,
93                             MC_CMD_GET_PORT_MODES_OUT_LEN)];
94         efx_rc_t rc;
95
96         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
97                     enp->en_family == EFX_FAMILY_MEDFORD);
98
99         (void) memset(payload, 0, sizeof (payload));
100         req.emr_cmd = MC_CMD_GET_PORT_MODES;
101         req.emr_in_buf = payload;
102         req.emr_in_length = MC_CMD_GET_PORT_MODES_IN_LEN;
103         req.emr_out_buf = payload;
104         req.emr_out_length = MC_CMD_GET_PORT_MODES_OUT_LEN;
105
106         efx_mcdi_execute(enp, &req);
107
108         if (req.emr_rc != 0) {
109                 rc = req.emr_rc;
110                 goto fail1;
111         }
112
113         /*
114          * Require only Modes and DefaultMode fields, unless the current mode
115          * was requested (CurrentMode field was added for Medford).
116          */
117         if (req.emr_out_length_used <
118             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST) {
119                 rc = EMSGSIZE;
120                 goto fail2;
121         }
122         if ((current_modep != NULL) && (req.emr_out_length_used <
123             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST + 4)) {
124                 rc = EMSGSIZE;
125                 goto fail3;
126         }
127
128         *modesp = MCDI_OUT_DWORD(req, GET_PORT_MODES_OUT_MODES);
129
130         if (current_modep != NULL) {
131                 *current_modep = MCDI_OUT_DWORD(req,
132                                             GET_PORT_MODES_OUT_CURRENT_MODE);
133         }
134
135         return (0);
136
137 fail3:
138         EFSYS_PROBE(fail3);
139 fail2:
140         EFSYS_PROBE(fail2);
141 fail1:
142         EFSYS_PROBE1(fail1, efx_rc_t, rc);
143
144         return (rc);
145 }
146
147         __checkReturn   efx_rc_t
148 ef10_nic_get_port_mode_bandwidth(
149         __in            uint32_t port_mode,
150         __out           uint32_t *bandwidth_mbpsp)
151 {
152         uint32_t bandwidth;
153         efx_rc_t rc;
154
155         switch (port_mode) {
156         case TLV_PORT_MODE_10G:
157                 bandwidth = 10000;
158                 break;
159         case TLV_PORT_MODE_10G_10G:
160                 bandwidth = 10000 * 2;
161                 break;
162         case TLV_PORT_MODE_10G_10G_10G_10G:
163         case TLV_PORT_MODE_10G_10G_10G_10G_Q:
164         case TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2:
165         case TLV_PORT_MODE_10G_10G_10G_10G_Q2:
166                 bandwidth = 10000 * 4;
167                 break;
168         case TLV_PORT_MODE_40G:
169                 bandwidth = 40000;
170                 break;
171         case TLV_PORT_MODE_40G_40G:
172                 bandwidth = 40000 * 2;
173                 break;
174         case TLV_PORT_MODE_40G_10G_10G:
175         case TLV_PORT_MODE_10G_10G_40G:
176                 bandwidth = 40000 + (10000 * 2);
177                 break;
178         default:
179                 rc = EINVAL;
180                 goto fail1;
181         }
182
183         *bandwidth_mbpsp = bandwidth;
184
185         return (0);
186
187 fail1:
188         EFSYS_PROBE1(fail1, efx_rc_t, rc);
189
190         return (rc);
191 }
192
193 static  __checkReturn           efx_rc_t
194 efx_mcdi_vadaptor_alloc(
195         __in                    efx_nic_t *enp,
196         __in                    uint32_t port_id)
197 {
198         efx_mcdi_req_t req;
199         uint8_t payload[MAX(MC_CMD_VADAPTOR_ALLOC_IN_LEN,
200                             MC_CMD_VADAPTOR_ALLOC_OUT_LEN)];
201         efx_rc_t rc;
202
203         EFSYS_ASSERT3U(enp->en_vport_id, ==, EVB_PORT_ID_NULL);
204
205         (void) memset(payload, 0, sizeof (payload));
206         req.emr_cmd = MC_CMD_VADAPTOR_ALLOC;
207         req.emr_in_buf = payload;
208         req.emr_in_length = MC_CMD_VADAPTOR_ALLOC_IN_LEN;
209         req.emr_out_buf = payload;
210         req.emr_out_length = MC_CMD_VADAPTOR_ALLOC_OUT_LEN;
211
212         MCDI_IN_SET_DWORD(req, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
213         MCDI_IN_POPULATE_DWORD_1(req, VADAPTOR_ALLOC_IN_FLAGS,
214             VADAPTOR_ALLOC_IN_FLAG_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED,
215             enp->en_nic_cfg.enc_allow_set_mac_with_installed_filters ? 1 : 0);
216
217         efx_mcdi_execute(enp, &req);
218
219         if (req.emr_rc != 0) {
220                 rc = req.emr_rc;
221                 goto fail1;
222         }
223
224         return (0);
225
226 fail1:
227         EFSYS_PROBE1(fail1, efx_rc_t, rc);
228
229         return (rc);
230 }
231
232 static  __checkReturn           efx_rc_t
233 efx_mcdi_vadaptor_free(
234         __in                    efx_nic_t *enp,
235         __in                    uint32_t port_id)
236 {
237         efx_mcdi_req_t req;
238         uint8_t payload[MAX(MC_CMD_VADAPTOR_FREE_IN_LEN,
239                             MC_CMD_VADAPTOR_FREE_OUT_LEN)];
240         efx_rc_t rc;
241
242         (void) memset(payload, 0, sizeof (payload));
243         req.emr_cmd = MC_CMD_VADAPTOR_FREE;
244         req.emr_in_buf = payload;
245         req.emr_in_length = MC_CMD_VADAPTOR_FREE_IN_LEN;
246         req.emr_out_buf = payload;
247         req.emr_out_length = MC_CMD_VADAPTOR_FREE_OUT_LEN;
248
249         MCDI_IN_SET_DWORD(req, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
250
251         efx_mcdi_execute(enp, &req);
252
253         if (req.emr_rc != 0) {
254                 rc = req.emr_rc;
255                 goto fail1;
256         }
257
258         return (0);
259
260 fail1:
261         EFSYS_PROBE1(fail1, efx_rc_t, rc);
262
263         return (rc);
264 }
265
266         __checkReturn   efx_rc_t
267 efx_mcdi_get_mac_address_pf(
268         __in                    efx_nic_t *enp,
269         __out_ecount_opt(6)     uint8_t mac_addrp[6])
270 {
271         efx_mcdi_req_t req;
272         uint8_t payload[MAX(MC_CMD_GET_MAC_ADDRESSES_IN_LEN,
273                             MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)];
274         efx_rc_t rc;
275
276         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
277                     enp->en_family == EFX_FAMILY_MEDFORD);
278
279         (void) memset(payload, 0, sizeof (payload));
280         req.emr_cmd = MC_CMD_GET_MAC_ADDRESSES;
281         req.emr_in_buf = payload;
282         req.emr_in_length = MC_CMD_GET_MAC_ADDRESSES_IN_LEN;
283         req.emr_out_buf = payload;
284         req.emr_out_length = MC_CMD_GET_MAC_ADDRESSES_OUT_LEN;
285
286         efx_mcdi_execute(enp, &req);
287
288         if (req.emr_rc != 0) {
289                 rc = req.emr_rc;
290                 goto fail1;
291         }
292
293         if (req.emr_out_length_used < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN) {
294                 rc = EMSGSIZE;
295                 goto fail2;
296         }
297
298         if (MCDI_OUT_DWORD(req, GET_MAC_ADDRESSES_OUT_MAC_COUNT) < 1) {
299                 rc = ENOENT;
300                 goto fail3;
301         }
302
303         if (mac_addrp != NULL) {
304                 uint8_t *addrp;
305
306                 addrp = MCDI_OUT2(req, uint8_t,
307                     GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE);
308
309                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
310         }
311
312         return (0);
313
314 fail3:
315         EFSYS_PROBE(fail3);
316 fail2:
317         EFSYS_PROBE(fail2);
318 fail1:
319         EFSYS_PROBE1(fail1, efx_rc_t, rc);
320
321         return (rc);
322 }
323
324         __checkReturn   efx_rc_t
325 efx_mcdi_get_mac_address_vf(
326         __in                    efx_nic_t *enp,
327         __out_ecount_opt(6)     uint8_t mac_addrp[6])
328 {
329         efx_mcdi_req_t req;
330         uint8_t payload[MAX(MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN,
331                             MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX)];
332         efx_rc_t rc;
333
334         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
335                     enp->en_family == EFX_FAMILY_MEDFORD);
336
337         (void) memset(payload, 0, sizeof (payload));
338         req.emr_cmd = MC_CMD_VPORT_GET_MAC_ADDRESSES;
339         req.emr_in_buf = payload;
340         req.emr_in_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN;
341         req.emr_out_buf = payload;
342         req.emr_out_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX;
343
344         MCDI_IN_SET_DWORD(req, VPORT_GET_MAC_ADDRESSES_IN_VPORT_ID,
345             EVB_PORT_ID_ASSIGNED);
346
347         efx_mcdi_execute(enp, &req);
348
349         if (req.emr_rc != 0) {
350                 rc = req.emr_rc;
351                 goto fail1;
352         }
353
354         if (req.emr_out_length_used <
355             MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMIN) {
356                 rc = EMSGSIZE;
357                 goto fail2;
358         }
359
360         if (MCDI_OUT_DWORD(req,
361                 VPORT_GET_MAC_ADDRESSES_OUT_MACADDR_COUNT) < 1) {
362                 rc = ENOENT;
363                 goto fail3;
364         }
365
366         if (mac_addrp != NULL) {
367                 uint8_t *addrp;
368
369                 addrp = MCDI_OUT2(req, uint8_t,
370                     VPORT_GET_MAC_ADDRESSES_OUT_MACADDR);
371
372                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
373         }
374
375         return (0);
376
377 fail3:
378         EFSYS_PROBE(fail3);
379 fail2:
380         EFSYS_PROBE(fail2);
381 fail1:
382         EFSYS_PROBE1(fail1, efx_rc_t, rc);
383
384         return (rc);
385 }
386
387         __checkReturn   efx_rc_t
388 efx_mcdi_get_clock(
389         __in            efx_nic_t *enp,
390         __out           uint32_t *sys_freqp,
391         __out           uint32_t *dpcpu_freqp)
392 {
393         efx_mcdi_req_t req;
394         uint8_t payload[MAX(MC_CMD_GET_CLOCK_IN_LEN,
395                             MC_CMD_GET_CLOCK_OUT_LEN)];
396         efx_rc_t rc;
397
398         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
399                     enp->en_family == EFX_FAMILY_MEDFORD);
400
401         (void) memset(payload, 0, sizeof (payload));
402         req.emr_cmd = MC_CMD_GET_CLOCK;
403         req.emr_in_buf = payload;
404         req.emr_in_length = MC_CMD_GET_CLOCK_IN_LEN;
405         req.emr_out_buf = payload;
406         req.emr_out_length = MC_CMD_GET_CLOCK_OUT_LEN;
407
408         efx_mcdi_execute(enp, &req);
409
410         if (req.emr_rc != 0) {
411                 rc = req.emr_rc;
412                 goto fail1;
413         }
414
415         if (req.emr_out_length_used < MC_CMD_GET_CLOCK_OUT_LEN) {
416                 rc = EMSGSIZE;
417                 goto fail2;
418         }
419
420         *sys_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_SYS_FREQ);
421         if (*sys_freqp == 0) {
422                 rc = EINVAL;
423                 goto fail3;
424         }
425         *dpcpu_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_DPCPU_FREQ);
426         if (*dpcpu_freqp == 0) {
427                 rc = EINVAL;
428                 goto fail4;
429         }
430
431         return (0);
432
433 fail4:
434         EFSYS_PROBE(fail4);
435 fail3:
436         EFSYS_PROBE(fail3);
437 fail2:
438         EFSYS_PROBE(fail2);
439 fail1:
440         EFSYS_PROBE1(fail1, efx_rc_t, rc);
441
442         return (rc);
443 }
444
445         __checkReturn   efx_rc_t
446 efx_mcdi_get_vector_cfg(
447         __in            efx_nic_t *enp,
448         __out_opt       uint32_t *vec_basep,
449         __out_opt       uint32_t *pf_nvecp,
450         __out_opt       uint32_t *vf_nvecp)
451 {
452         efx_mcdi_req_t req;
453         uint8_t payload[MAX(MC_CMD_GET_VECTOR_CFG_IN_LEN,
454                             MC_CMD_GET_VECTOR_CFG_OUT_LEN)];
455         efx_rc_t rc;
456
457         (void) memset(payload, 0, sizeof (payload));
458         req.emr_cmd = MC_CMD_GET_VECTOR_CFG;
459         req.emr_in_buf = payload;
460         req.emr_in_length = MC_CMD_GET_VECTOR_CFG_IN_LEN;
461         req.emr_out_buf = payload;
462         req.emr_out_length = MC_CMD_GET_VECTOR_CFG_OUT_LEN;
463
464         efx_mcdi_execute(enp, &req);
465
466         if (req.emr_rc != 0) {
467                 rc = req.emr_rc;
468                 goto fail1;
469         }
470
471         if (req.emr_out_length_used < MC_CMD_GET_VECTOR_CFG_OUT_LEN) {
472                 rc = EMSGSIZE;
473                 goto fail2;
474         }
475
476         if (vec_basep != NULL)
477                 *vec_basep = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VEC_BASE);
478         if (pf_nvecp != NULL)
479                 *pf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_PF);
480         if (vf_nvecp != NULL)
481                 *vf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_VF);
482
483         return (0);
484
485 fail2:
486         EFSYS_PROBE(fail2);
487 fail1:
488         EFSYS_PROBE1(fail1, efx_rc_t, rc);
489
490         return (rc);
491 }
492
493 static  __checkReturn   efx_rc_t
494 efx_mcdi_get_capabilities(
495         __in            efx_nic_t *enp,
496         __out           uint32_t *flagsp,
497         __out           uint32_t *flags2p,
498         __out           uint32_t *tso2ncp)
499 {
500         efx_mcdi_req_t req;
501         uint8_t payload[MAX(MC_CMD_GET_CAPABILITIES_IN_LEN,
502                             MC_CMD_GET_CAPABILITIES_V2_OUT_LEN)];
503         efx_rc_t rc;
504
505         (void) memset(payload, 0, sizeof (payload));
506         req.emr_cmd = MC_CMD_GET_CAPABILITIES;
507         req.emr_in_buf = payload;
508         req.emr_in_length = MC_CMD_GET_CAPABILITIES_IN_LEN;
509         req.emr_out_buf = payload;
510         req.emr_out_length = MC_CMD_GET_CAPABILITIES_V2_OUT_LEN;
511
512         efx_mcdi_execute(enp, &req);
513
514         if (req.emr_rc != 0) {
515                 rc = req.emr_rc;
516                 goto fail1;
517         }
518
519         if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_OUT_LEN) {
520                 rc = EMSGSIZE;
521                 goto fail2;
522         }
523
524         *flagsp = MCDI_OUT_DWORD(req, GET_CAPABILITIES_OUT_FLAGS1);
525
526         if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_V2_OUT_LEN) {
527                 *flags2p = 0;
528                 *tso2ncp = 0;
529         } else {
530                 *flags2p = MCDI_OUT_DWORD(req, GET_CAPABILITIES_V2_OUT_FLAGS2);
531                 *tso2ncp = MCDI_OUT_WORD(req,
532                                 GET_CAPABILITIES_V2_OUT_TX_TSO_V2_N_CONTEXTS);
533         }
534
535         return (0);
536
537 fail2:
538         EFSYS_PROBE(fail2);
539 fail1:
540         EFSYS_PROBE1(fail1, efx_rc_t, rc);
541
542         return (rc);
543 }
544
545
546 static  __checkReturn   efx_rc_t
547 efx_mcdi_alloc_vis(
548         __in            efx_nic_t *enp,
549         __in            uint32_t min_vi_count,
550         __in            uint32_t max_vi_count,
551         __out           uint32_t *vi_basep,
552         __out           uint32_t *vi_countp,
553         __out           uint32_t *vi_shiftp)
554 {
555         efx_mcdi_req_t req;
556         uint8_t payload[MAX(MC_CMD_ALLOC_VIS_IN_LEN,
557                             MC_CMD_ALLOC_VIS_OUT_LEN)];
558         efx_rc_t rc;
559
560         if (vi_countp == NULL) {
561                 rc = EINVAL;
562                 goto fail1;
563         }
564
565         (void) memset(payload, 0, sizeof (payload));
566         req.emr_cmd = MC_CMD_ALLOC_VIS;
567         req.emr_in_buf = payload;
568         req.emr_in_length = MC_CMD_ALLOC_VIS_IN_LEN;
569         req.emr_out_buf = payload;
570         req.emr_out_length = MC_CMD_ALLOC_VIS_OUT_LEN;
571
572         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MIN_VI_COUNT, min_vi_count);
573         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MAX_VI_COUNT, max_vi_count);
574
575         efx_mcdi_execute(enp, &req);
576
577         if (req.emr_rc != 0) {
578                 rc = req.emr_rc;
579                 goto fail2;
580         }
581
582         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_OUT_LEN) {
583                 rc = EMSGSIZE;
584                 goto fail3;
585         }
586
587         *vi_basep = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_BASE);
588         *vi_countp = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_COUNT);
589
590         /* Report VI_SHIFT if available (always zero for Huntington) */
591         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_EXT_OUT_LEN)
592                 *vi_shiftp = 0;
593         else
594                 *vi_shiftp = MCDI_OUT_DWORD(req, ALLOC_VIS_EXT_OUT_VI_SHIFT);
595
596         return (0);
597
598 fail3:
599         EFSYS_PROBE(fail3);
600 fail2:
601         EFSYS_PROBE(fail2);
602 fail1:
603         EFSYS_PROBE1(fail1, efx_rc_t, rc);
604
605         return (rc);
606 }
607
608
609 static  __checkReturn   efx_rc_t
610 efx_mcdi_free_vis(
611         __in            efx_nic_t *enp)
612 {
613         efx_mcdi_req_t req;
614         efx_rc_t rc;
615
616         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_IN_LEN == 0);
617         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_OUT_LEN == 0);
618
619         req.emr_cmd = MC_CMD_FREE_VIS;
620         req.emr_in_buf = NULL;
621         req.emr_in_length = 0;
622         req.emr_out_buf = NULL;
623         req.emr_out_length = 0;
624
625         efx_mcdi_execute_quiet(enp, &req);
626
627         /* Ignore ELREADY (no allocated VIs, so nothing to free) */
628         if ((req.emr_rc != 0) && (req.emr_rc != EALREADY)) {
629                 rc = req.emr_rc;
630                 goto fail1;
631         }
632
633         return (0);
634
635 fail1:
636         EFSYS_PROBE1(fail1, efx_rc_t, rc);
637
638         return (rc);
639 }
640
641
642 static  __checkReturn   efx_rc_t
643 efx_mcdi_alloc_piobuf(
644         __in            efx_nic_t *enp,
645         __out           efx_piobuf_handle_t *handlep)
646 {
647         efx_mcdi_req_t req;
648         uint8_t payload[MAX(MC_CMD_ALLOC_PIOBUF_IN_LEN,
649                             MC_CMD_ALLOC_PIOBUF_OUT_LEN)];
650         efx_rc_t rc;
651
652         if (handlep == NULL) {
653                 rc = EINVAL;
654                 goto fail1;
655         }
656
657         (void) memset(payload, 0, sizeof (payload));
658         req.emr_cmd = MC_CMD_ALLOC_PIOBUF;
659         req.emr_in_buf = payload;
660         req.emr_in_length = MC_CMD_ALLOC_PIOBUF_IN_LEN;
661         req.emr_out_buf = payload;
662         req.emr_out_length = MC_CMD_ALLOC_PIOBUF_OUT_LEN;
663
664         efx_mcdi_execute_quiet(enp, &req);
665
666         if (req.emr_rc != 0) {
667                 rc = req.emr_rc;
668                 goto fail2;
669         }
670
671         if (req.emr_out_length_used < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
672                 rc = EMSGSIZE;
673                 goto fail3;
674         }
675
676         *handlep = MCDI_OUT_DWORD(req, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
677
678         return (0);
679
680 fail3:
681         EFSYS_PROBE(fail3);
682 fail2:
683         EFSYS_PROBE(fail2);
684 fail1:
685         EFSYS_PROBE1(fail1, efx_rc_t, rc);
686
687         return (rc);
688 }
689
690 static  __checkReturn   efx_rc_t
691 efx_mcdi_free_piobuf(
692         __in            efx_nic_t *enp,
693         __in            efx_piobuf_handle_t handle)
694 {
695         efx_mcdi_req_t req;
696         uint8_t payload[MAX(MC_CMD_FREE_PIOBUF_IN_LEN,
697                             MC_CMD_FREE_PIOBUF_OUT_LEN)];
698         efx_rc_t rc;
699
700         (void) memset(payload, 0, sizeof (payload));
701         req.emr_cmd = MC_CMD_FREE_PIOBUF;
702         req.emr_in_buf = payload;
703         req.emr_in_length = MC_CMD_FREE_PIOBUF_IN_LEN;
704         req.emr_out_buf = payload;
705         req.emr_out_length = MC_CMD_FREE_PIOBUF_OUT_LEN;
706
707         MCDI_IN_SET_DWORD(req, FREE_PIOBUF_IN_PIOBUF_HANDLE, handle);
708
709         efx_mcdi_execute_quiet(enp, &req);
710
711         if (req.emr_rc != 0) {
712                 rc = req.emr_rc;
713                 goto fail1;
714         }
715
716         return (0);
717
718 fail1:
719         EFSYS_PROBE1(fail1, efx_rc_t, rc);
720
721         return (rc);
722 }
723
724 static  __checkReturn   efx_rc_t
725 efx_mcdi_link_piobuf(
726         __in            efx_nic_t *enp,
727         __in            uint32_t vi_index,
728         __in            efx_piobuf_handle_t handle)
729 {
730         efx_mcdi_req_t req;
731         uint8_t payload[MAX(MC_CMD_LINK_PIOBUF_IN_LEN,
732                             MC_CMD_LINK_PIOBUF_OUT_LEN)];
733         efx_rc_t rc;
734
735         (void) memset(payload, 0, sizeof (payload));
736         req.emr_cmd = MC_CMD_LINK_PIOBUF;
737         req.emr_in_buf = payload;
738         req.emr_in_length = MC_CMD_LINK_PIOBUF_IN_LEN;
739         req.emr_out_buf = payload;
740         req.emr_out_length = MC_CMD_LINK_PIOBUF_OUT_LEN;
741
742         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_PIOBUF_HANDLE, handle);
743         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
744
745         efx_mcdi_execute(enp, &req);
746
747         if (req.emr_rc != 0) {
748                 rc = req.emr_rc;
749                 goto fail1;
750         }
751
752         return (0);
753
754 fail1:
755         EFSYS_PROBE1(fail1, efx_rc_t, rc);
756
757         return (rc);
758 }
759
760 static  __checkReturn   efx_rc_t
761 efx_mcdi_unlink_piobuf(
762         __in            efx_nic_t *enp,
763         __in            uint32_t vi_index)
764 {
765         efx_mcdi_req_t req;
766         uint8_t payload[MAX(MC_CMD_UNLINK_PIOBUF_IN_LEN,
767                             MC_CMD_UNLINK_PIOBUF_OUT_LEN)];
768         efx_rc_t rc;
769
770         (void) memset(payload, 0, sizeof (payload));
771         req.emr_cmd = MC_CMD_UNLINK_PIOBUF;
772         req.emr_in_buf = payload;
773         req.emr_in_length = MC_CMD_UNLINK_PIOBUF_IN_LEN;
774         req.emr_out_buf = payload;
775         req.emr_out_length = MC_CMD_UNLINK_PIOBUF_OUT_LEN;
776
777         MCDI_IN_SET_DWORD(req, UNLINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
778
779         efx_mcdi_execute_quiet(enp, &req);
780
781         if (req.emr_rc != 0) {
782                 rc = req.emr_rc;
783                 goto fail1;
784         }
785
786         return (0);
787
788 fail1:
789         EFSYS_PROBE1(fail1, efx_rc_t, rc);
790
791         return (rc);
792 }
793
794 static                  void
795 ef10_nic_alloc_piobufs(
796         __in            efx_nic_t *enp,
797         __in            uint32_t max_piobuf_count)
798 {
799         efx_piobuf_handle_t *handlep;
800         unsigned int i;
801
802         EFSYS_ASSERT3U(max_piobuf_count, <=,
803             EFX_ARRAY_SIZE(enp->en_arch.ef10.ena_piobuf_handle));
804
805         enp->en_arch.ef10.ena_piobuf_count = 0;
806
807         for (i = 0; i < max_piobuf_count; i++) {
808                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
809
810                 if (efx_mcdi_alloc_piobuf(enp, handlep) != 0)
811                         goto fail1;
812
813                 enp->en_arch.ef10.ena_pio_alloc_map[i] = 0;
814                 enp->en_arch.ef10.ena_piobuf_count++;
815         }
816
817         return;
818
819 fail1:
820         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
821                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
822
823                 efx_mcdi_free_piobuf(enp, *handlep);
824                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
825         }
826         enp->en_arch.ef10.ena_piobuf_count = 0;
827 }
828
829
830 static                  void
831 ef10_nic_free_piobufs(
832         __in            efx_nic_t *enp)
833 {
834         efx_piobuf_handle_t *handlep;
835         unsigned int i;
836
837         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
838                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
839
840                 efx_mcdi_free_piobuf(enp, *handlep);
841                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
842         }
843         enp->en_arch.ef10.ena_piobuf_count = 0;
844 }
845
846 /* Sub-allocate a block from a piobuf */
847         __checkReturn   efx_rc_t
848 ef10_nic_pio_alloc(
849         __inout         efx_nic_t *enp,
850         __out           uint32_t *bufnump,
851         __out           efx_piobuf_handle_t *handlep,
852         __out           uint32_t *blknump,
853         __out           uint32_t *offsetp,
854         __out           size_t *sizep)
855 {
856         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
857         efx_drv_cfg_t *edcp = &enp->en_drv_cfg;
858         uint32_t blk_per_buf;
859         uint32_t buf, blk;
860         efx_rc_t rc;
861
862         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
863                     enp->en_family == EFX_FAMILY_MEDFORD);
864         EFSYS_ASSERT(bufnump);
865         EFSYS_ASSERT(handlep);
866         EFSYS_ASSERT(blknump);
867         EFSYS_ASSERT(offsetp);
868         EFSYS_ASSERT(sizep);
869
870         if ((edcp->edc_pio_alloc_size == 0) ||
871             (enp->en_arch.ef10.ena_piobuf_count == 0)) {
872                 rc = ENOMEM;
873                 goto fail1;
874         }
875         blk_per_buf = encp->enc_piobuf_size / edcp->edc_pio_alloc_size;
876
877         for (buf = 0; buf < enp->en_arch.ef10.ena_piobuf_count; buf++) {
878                 uint32_t *map = &enp->en_arch.ef10.ena_pio_alloc_map[buf];
879
880                 if (~(*map) == 0)
881                         continue;
882
883                 EFSYS_ASSERT3U(blk_per_buf, <=, (8 * sizeof (*map)));
884                 for (blk = 0; blk < blk_per_buf; blk++) {
885                         if ((*map & (1u << blk)) == 0) {
886                                 *map |= (1u << blk);
887                                 goto done;
888                         }
889                 }
890         }
891         rc = ENOMEM;
892         goto fail2;
893
894 done:
895         *handlep = enp->en_arch.ef10.ena_piobuf_handle[buf];
896         *bufnump = buf;
897         *blknump = blk;
898         *sizep = edcp->edc_pio_alloc_size;
899         *offsetp = blk * (*sizep);
900
901         return (0);
902
903 fail2:
904         EFSYS_PROBE(fail2);
905 fail1:
906         EFSYS_PROBE1(fail1, efx_rc_t, rc);
907
908         return (rc);
909 }
910
911 /* Free a piobuf sub-allocated block */
912         __checkReturn   efx_rc_t
913 ef10_nic_pio_free(
914         __inout         efx_nic_t *enp,
915         __in            uint32_t bufnum,
916         __in            uint32_t blknum)
917 {
918         uint32_t *map;
919         efx_rc_t rc;
920
921         if ((bufnum >= enp->en_arch.ef10.ena_piobuf_count) ||
922             (blknum >= (8 * sizeof (*map)))) {
923                 rc = EINVAL;
924                 goto fail1;
925         }
926
927         map = &enp->en_arch.ef10.ena_pio_alloc_map[bufnum];
928         if ((*map & (1u << blknum)) == 0) {
929                 rc = ENOENT;
930                 goto fail2;
931         }
932         *map &= ~(1u << blknum);
933
934         return (0);
935
936 fail2:
937         EFSYS_PROBE(fail2);
938 fail1:
939         EFSYS_PROBE1(fail1, efx_rc_t, rc);
940
941         return (rc);
942 }
943
944         __checkReturn   efx_rc_t
945 ef10_nic_pio_link(
946         __inout         efx_nic_t *enp,
947         __in            uint32_t vi_index,
948         __in            efx_piobuf_handle_t handle)
949 {
950         return (efx_mcdi_link_piobuf(enp, vi_index, handle));
951 }
952
953         __checkReturn   efx_rc_t
954 ef10_nic_pio_unlink(
955         __inout         efx_nic_t *enp,
956         __in            uint32_t vi_index)
957 {
958         return (efx_mcdi_unlink_piobuf(enp, vi_index));
959 }
960
961 static  __checkReturn   efx_rc_t
962 ef10_mcdi_get_pf_count(
963         __in            efx_nic_t *enp,
964         __out           uint32_t *pf_countp)
965 {
966         efx_mcdi_req_t req;
967         uint8_t payload[MAX(MC_CMD_GET_PF_COUNT_IN_LEN,
968                             MC_CMD_GET_PF_COUNT_OUT_LEN)];
969         efx_rc_t rc;
970
971         (void) memset(payload, 0, sizeof (payload));
972         req.emr_cmd = MC_CMD_GET_PF_COUNT;
973         req.emr_in_buf = payload;
974         req.emr_in_length = MC_CMD_GET_PF_COUNT_IN_LEN;
975         req.emr_out_buf = payload;
976         req.emr_out_length = MC_CMD_GET_PF_COUNT_OUT_LEN;
977
978         efx_mcdi_execute(enp, &req);
979
980         if (req.emr_rc != 0) {
981                 rc = req.emr_rc;
982                 goto fail1;
983         }
984
985         if (req.emr_out_length_used < MC_CMD_GET_PF_COUNT_OUT_LEN) {
986                 rc = EMSGSIZE;
987                 goto fail2;
988         }
989
990         *pf_countp = *MCDI_OUT(req, uint8_t,
991                                 MC_CMD_GET_PF_COUNT_OUT_PF_COUNT_OFST);
992
993         EFSYS_ASSERT(*pf_countp != 0);
994
995         return (0);
996
997 fail2:
998         EFSYS_PROBE(fail2);
999 fail1:
1000         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1001
1002         return (rc);
1003 }
1004
1005         __checkReturn   efx_rc_t
1006 ef10_get_datapath_caps(
1007         __in            efx_nic_t *enp)
1008 {
1009         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1010         uint32_t flags;
1011         uint32_t flags2;
1012         uint32_t tso2nc;
1013         efx_rc_t rc;
1014
1015         if ((rc = efx_mcdi_get_capabilities(enp, &flags, &flags2,
1016                                             &tso2nc)) != 0)
1017                 goto fail1;
1018
1019         if ((rc = ef10_mcdi_get_pf_count(enp, &encp->enc_hw_pf_count)) != 0)
1020                 goto fail1;
1021
1022 #define CAP_FLAG(flags1, field)         \
1023         ((flags1) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
1024
1025 #define CAP_FLAG2(flags2, field)        \
1026         ((flags2) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
1027
1028         /*
1029          * Huntington RXDP firmware inserts a 0 or 14 byte prefix.
1030          * We only support the 14 byte prefix here.
1031          */
1032         if (CAP_FLAG(flags, RX_PREFIX_LEN_14) == 0) {
1033                 rc = ENOTSUP;
1034                 goto fail2;
1035         }
1036         encp->enc_rx_prefix_size = 14;
1037
1038         /* Check if the firmware supports TSO */
1039         encp->enc_fw_assisted_tso_enabled =
1040             CAP_FLAG(flags, TX_TSO) ? B_TRUE : B_FALSE;
1041
1042         /* Check if the firmware supports FATSOv2 */
1043         encp->enc_fw_assisted_tso_v2_enabled =
1044             CAP_FLAG2(flags2, TX_TSO_V2) ? B_TRUE : B_FALSE;
1045
1046         /* Get the number of TSO contexts (FATSOv2) */
1047         encp->enc_fw_assisted_tso_v2_n_contexts =
1048                 CAP_FLAG2(flags2, TX_TSO_V2) ? tso2nc : 0;
1049
1050         /* Check if the firmware has vadapter/vport/vswitch support */
1051         encp->enc_datapath_cap_evb =
1052             CAP_FLAG(flags, EVB) ? B_TRUE : B_FALSE;
1053
1054         /* Check if the firmware supports VLAN insertion */
1055         encp->enc_hw_tx_insert_vlan_enabled =
1056             CAP_FLAG(flags, TX_VLAN_INSERTION) ? B_TRUE : B_FALSE;
1057
1058         /* Check if the firmware supports RX event batching */
1059         encp->enc_rx_batching_enabled =
1060             CAP_FLAG(flags, RX_BATCHING) ? B_TRUE : B_FALSE;
1061
1062         /*
1063          * Even if batching isn't reported as supported, we may still get
1064          * batched events (see bug61153).
1065          */
1066         encp->enc_rx_batch_max = 16;
1067
1068         /* Check if the firmware supports disabling scatter on RXQs */
1069         encp->enc_rx_disable_scatter_supported =
1070             CAP_FLAG(flags, RX_DISABLE_SCATTER) ? B_TRUE : B_FALSE;
1071
1072         /* Check if the firmware supports packed stream mode */
1073         encp->enc_rx_packed_stream_supported =
1074             CAP_FLAG(flags, RX_PACKED_STREAM) ? B_TRUE : B_FALSE;
1075
1076         /*
1077          * Check if the firmware supports configurable buffer sizes
1078          * for packed stream mode (otherwise buffer size is 1Mbyte)
1079          */
1080         encp->enc_rx_var_packed_stream_supported =
1081             CAP_FLAG(flags, RX_PACKED_STREAM_VAR_BUFFERS) ? B_TRUE : B_FALSE;
1082
1083         /* Check if the firmware supports set mac with running filters */
1084         encp->enc_allow_set_mac_with_installed_filters =
1085             CAP_FLAG(flags, VADAPTOR_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED) ?
1086             B_TRUE : B_FALSE;
1087
1088         /*
1089          * Check if firmware supports the extended MC_CMD_SET_MAC, which allows
1090          * specifying which parameters to configure.
1091          */
1092         encp->enc_enhanced_set_mac_supported =
1093                 CAP_FLAG(flags, SET_MAC_ENHANCED) ? B_TRUE : B_FALSE;
1094
1095         /*
1096          * Check if firmware supports version 2 of MC_CMD_INIT_EVQ, which allows
1097          * us to let the firmware choose the settings to use on an EVQ.
1098          */
1099         encp->enc_init_evq_v2_supported =
1100                 CAP_FLAG2(flags2, INIT_EVQ_V2) ? B_TRUE : B_FALSE;
1101
1102         /*
1103          * Check if firmware-verified NVRAM updates must be used.
1104          *
1105          * The firmware trusted installer requires all NVRAM updates to use
1106          * version 2 of MC_CMD_NVRAM_UPDATE_START (to enable verified update)
1107          * and version 2 of MC_CMD_NVRAM_UPDATE_FINISH (to verify the updated
1108          * partition and report the result).
1109          */
1110         encp->enc_fw_verified_nvram_update_required =
1111             CAP_FLAG2(flags2, NVRAM_UPDATE_REPORT_VERIFY_RESULT) ?
1112             B_TRUE : B_FALSE;
1113
1114         /*
1115          * Check if firmware provides packet memory and Rx datapath
1116          * counters.
1117          */
1118         encp->enc_pm_and_rxdp_counters =
1119             CAP_FLAG(flags, PM_AND_RXDP_COUNTERS) ? B_TRUE : B_FALSE;
1120
1121         /*
1122          * Check if the 40G MAC hardware is capable of reporting
1123          * statistics for Tx size bins.
1124          */
1125         encp->enc_mac_stats_40g_tx_size_bins =
1126             CAP_FLAG2(flags2, MAC_STATS_40G_TX_SIZE_BINS) ? B_TRUE : B_FALSE;
1127
1128 #undef CAP_FLAG
1129 #undef CAP_FLAG2
1130
1131         return (0);
1132
1133 fail2:
1134         EFSYS_PROBE(fail2);
1135 fail1:
1136         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1137
1138         return (rc);
1139 }
1140
1141
1142 #define EF10_LEGACY_PF_PRIVILEGE_MASK                                   \
1143         (MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN                     |       \
1144         MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK                       |       \
1145         MC_CMD_PRIVILEGE_MASK_IN_GRP_ONLOAD                     |       \
1146         MC_CMD_PRIVILEGE_MASK_IN_GRP_PTP                        |       \
1147         MC_CMD_PRIVILEGE_MASK_IN_GRP_INSECURE_FILTERS           |       \
1148         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING               |       \
1149         MC_CMD_PRIVILEGE_MASK_IN_GRP_UNICAST                    |       \
1150         MC_CMD_PRIVILEGE_MASK_IN_GRP_MULTICAST                  |       \
1151         MC_CMD_PRIVILEGE_MASK_IN_GRP_BROADCAST                  |       \
1152         MC_CMD_PRIVILEGE_MASK_IN_GRP_ALL_MULTICAST              |       \
1153         MC_CMD_PRIVILEGE_MASK_IN_GRP_PROMISCUOUS)
1154
1155 #define EF10_LEGACY_VF_PRIVILEGE_MASK   0
1156
1157
1158         __checkReturn           efx_rc_t
1159 ef10_get_privilege_mask(
1160         __in                    efx_nic_t *enp,
1161         __out                   uint32_t *maskp)
1162 {
1163         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1164         uint32_t mask;
1165         efx_rc_t rc;
1166
1167         if ((rc = efx_mcdi_privilege_mask(enp, encp->enc_pf, encp->enc_vf,
1168                                             &mask)) != 0) {
1169                 if (rc != ENOTSUP)
1170                         goto fail1;
1171
1172                 /* Fallback for old firmware without privilege mask support */
1173                 if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1174                         /* Assume PF has admin privilege */
1175                         mask = EF10_LEGACY_PF_PRIVILEGE_MASK;
1176                 } else {
1177                         /* VF is always unprivileged by default */
1178                         mask = EF10_LEGACY_VF_PRIVILEGE_MASK;
1179                 }
1180         }
1181
1182         *maskp = mask;
1183
1184         return (0);
1185
1186 fail1:
1187         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1188
1189         return (rc);
1190 }
1191
1192
1193 /*
1194  * Table of mapping schemes from port number to the number of the external
1195  * connector on the board. The external numbering does not distinguish
1196  * off-board separated outputs such as from multi-headed cables.
1197  *
1198  * The count of adjacent port numbers that map to each external port
1199  * and the offset in the numbering, is determined by the chip family and
1200  * current port mode.
1201  *
1202  * For the Huntington family, the current port mode cannot be discovered,
1203  * so the mapping used is instead the last match in the table to the full
1204  * set of port modes to which the NIC can be configured. Therefore the
1205  * ordering of entries in the the mapping table is significant.
1206  */
1207 static struct {
1208         efx_family_t    family;
1209         uint32_t        modes_mask;
1210         int32_t         count;
1211         int32_t         offset;
1212 }       __ef10_external_port_mappings[] = {
1213         /* Supported modes with 1 output per external port */
1214         {
1215                 EFX_FAMILY_HUNTINGTON,
1216                 (1 << TLV_PORT_MODE_10G) |
1217                 (1 << TLV_PORT_MODE_10G_10G) |
1218                 (1 << TLV_PORT_MODE_10G_10G_10G_10G),
1219                 1,
1220                 1
1221         },
1222         {
1223                 EFX_FAMILY_MEDFORD,
1224                 (1 << TLV_PORT_MODE_10G) |
1225                 (1 << TLV_PORT_MODE_10G_10G),
1226                 1,
1227                 1
1228         },
1229         /* Supported modes with 2 outputs per external port */
1230         {
1231                 EFX_FAMILY_HUNTINGTON,
1232                 (1 << TLV_PORT_MODE_40G) |
1233                 (1 << TLV_PORT_MODE_40G_40G) |
1234                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1235                 (1 << TLV_PORT_MODE_10G_10G_40G),
1236                 2,
1237                 1
1238         },
1239         {
1240                 EFX_FAMILY_MEDFORD,
1241                 (1 << TLV_PORT_MODE_40G) |
1242                 (1 << TLV_PORT_MODE_40G_40G) |
1243                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1244                 (1 << TLV_PORT_MODE_10G_10G_40G) |
1245                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2),
1246                 2,
1247                 1
1248         },
1249         /* Supported modes with 4 outputs per external port */
1250         {
1251                 EFX_FAMILY_MEDFORD,
1252                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q) |
1253                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1),
1254                 4,
1255                 1,
1256         },
1257         {
1258                 EFX_FAMILY_MEDFORD,
1259                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q2),
1260                 4,
1261                 2
1262         },
1263 };
1264
1265         __checkReturn   efx_rc_t
1266 ef10_external_port_mapping(
1267         __in            efx_nic_t *enp,
1268         __in            uint32_t port,
1269         __out           uint8_t *external_portp)
1270 {
1271         efx_rc_t rc;
1272         int i;
1273         uint32_t port_modes;
1274         uint32_t matches;
1275         uint32_t current;
1276         int32_t count = 1; /* Default 1-1 mapping */
1277         int32_t offset = 1; /* Default starting external port number */
1278
1279         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, &current)) != 0) {
1280                 /*
1281                  * No current port mode information
1282                  * - infer mapping from available modes
1283                  */
1284                 if ((rc = efx_mcdi_get_port_modes(enp,
1285                             &port_modes, NULL)) != 0) {
1286                         /*
1287                          * No port mode information available
1288                          * - use default mapping
1289                          */
1290                         goto out;
1291                 }
1292         } else {
1293                 /* Only need to scan the current mode */
1294                 port_modes = 1 << current;
1295         }
1296
1297         /*
1298          * Infer the internal port -> external port mapping from
1299          * the possible port modes for this NIC.
1300          */
1301         for (i = 0; i < EFX_ARRAY_SIZE(__ef10_external_port_mappings); ++i) {
1302                 if (__ef10_external_port_mappings[i].family !=
1303                     enp->en_family)
1304                         continue;
1305                 matches = (__ef10_external_port_mappings[i].modes_mask &
1306                     port_modes);
1307                 if (matches != 0) {
1308                         count = __ef10_external_port_mappings[i].count;
1309                         offset = __ef10_external_port_mappings[i].offset;
1310                         port_modes &= ~matches;
1311                 }
1312         }
1313
1314         if (port_modes != 0) {
1315                 /* Some advertised modes are not supported */
1316                 rc = ENOTSUP;
1317                 goto fail1;
1318         }
1319
1320 out:
1321         /*
1322          * Scale as required by last matched mode and then convert to
1323          * correctly offset numbering
1324          */
1325         *external_portp = (uint8_t)((port / count) + offset);
1326         return (0);
1327
1328 fail1:
1329         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1330
1331         return (rc);
1332 }
1333
1334
1335         __checkReturn   efx_rc_t
1336 ef10_nic_probe(
1337         __in            efx_nic_t *enp)
1338 {
1339         const efx_nic_ops_t *enop = enp->en_enop;
1340         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1341         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1342         efx_rc_t rc;
1343
1344         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1345                     enp->en_family == EFX_FAMILY_MEDFORD);
1346
1347         /* Read and clear any assertion state */
1348         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1349                 goto fail1;
1350
1351         /* Exit the assertion handler */
1352         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1353                 if (rc != EACCES)
1354                         goto fail2;
1355
1356         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
1357                 goto fail3;
1358
1359         if ((rc = enop->eno_board_cfg(enp)) != 0)
1360                 if (rc != EACCES)
1361                         goto fail4;
1362
1363         /*
1364          * Set default driver config limits (based on board config).
1365          *
1366          * FIXME: For now allocate a fixed number of VIs which is likely to be
1367          * sufficient and small enough to allow multiple functions on the same
1368          * port.
1369          */
1370         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
1371             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
1372
1373         /* The client driver must configure and enable PIO buffer support */
1374         edcp->edc_max_piobuf_count = 0;
1375         edcp->edc_pio_alloc_size = 0;
1376
1377 #if EFSYS_OPT_MAC_STATS
1378         /* Wipe the MAC statistics */
1379         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
1380                 goto fail5;
1381 #endif
1382
1383 #if EFSYS_OPT_LOOPBACK
1384         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
1385                 goto fail6;
1386 #endif
1387
1388 #if EFSYS_OPT_MON_STATS
1389         if ((rc = mcdi_mon_cfg_build(enp)) != 0) {
1390                 /* Unprivileged functions do not have access to sensors */
1391                 if (rc != EACCES)
1392                         goto fail7;
1393         }
1394 #endif
1395
1396         encp->enc_features = enp->en_features;
1397
1398         return (0);
1399
1400 #if EFSYS_OPT_MON_STATS
1401 fail7:
1402         EFSYS_PROBE(fail7);
1403 #endif
1404 #if EFSYS_OPT_LOOPBACK
1405 fail6:
1406         EFSYS_PROBE(fail6);
1407 #endif
1408 #if EFSYS_OPT_MAC_STATS
1409 fail5:
1410         EFSYS_PROBE(fail5);
1411 #endif
1412 fail4:
1413         EFSYS_PROBE(fail4);
1414 fail3:
1415         EFSYS_PROBE(fail3);
1416 fail2:
1417         EFSYS_PROBE(fail2);
1418 fail1:
1419         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1420
1421         return (rc);
1422 }
1423
1424         __checkReturn   efx_rc_t
1425 ef10_nic_set_drv_limits(
1426         __inout         efx_nic_t *enp,
1427         __in            efx_drv_limits_t *edlp)
1428 {
1429         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1430         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1431         uint32_t min_evq_count, max_evq_count;
1432         uint32_t min_rxq_count, max_rxq_count;
1433         uint32_t min_txq_count, max_txq_count;
1434         efx_rc_t rc;
1435
1436         if (edlp == NULL) {
1437                 rc = EINVAL;
1438                 goto fail1;
1439         }
1440
1441         /* Get minimum required and maximum usable VI limits */
1442         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
1443         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
1444         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
1445
1446         edcp->edc_min_vi_count =
1447             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
1448
1449         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
1450         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
1451         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
1452
1453         edcp->edc_max_vi_count =
1454             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
1455
1456         /*
1457          * Check limits for sub-allocated piobuf blocks.
1458          * PIO is optional, so don't fail if the limits are incorrect.
1459          */
1460         if ((encp->enc_piobuf_size == 0) ||
1461             (encp->enc_piobuf_limit == 0) ||
1462             (edlp->edl_min_pio_alloc_size == 0) ||
1463             (edlp->edl_min_pio_alloc_size > encp->enc_piobuf_size)) {
1464                 /* Disable PIO */
1465                 edcp->edc_max_piobuf_count = 0;
1466                 edcp->edc_pio_alloc_size = 0;
1467         } else {
1468                 uint32_t blk_size, blk_count, blks_per_piobuf;
1469
1470                 blk_size =
1471                     MAX(edlp->edl_min_pio_alloc_size,
1472                             encp->enc_piobuf_min_alloc_size);
1473
1474                 blks_per_piobuf = encp->enc_piobuf_size / blk_size;
1475                 EFSYS_ASSERT3U(blks_per_piobuf, <=, 32);
1476
1477                 blk_count = (encp->enc_piobuf_limit * blks_per_piobuf);
1478
1479                 /* A zero max pio alloc count means unlimited */
1480                 if ((edlp->edl_max_pio_alloc_count > 0) &&
1481                     (edlp->edl_max_pio_alloc_count < blk_count)) {
1482                         blk_count = edlp->edl_max_pio_alloc_count;
1483                 }
1484
1485                 edcp->edc_pio_alloc_size = blk_size;
1486                 edcp->edc_max_piobuf_count =
1487                     (blk_count + (blks_per_piobuf - 1)) / blks_per_piobuf;
1488         }
1489
1490         return (0);
1491
1492 fail1:
1493         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1494
1495         return (rc);
1496 }
1497
1498
1499         __checkReturn   efx_rc_t
1500 ef10_nic_reset(
1501         __in            efx_nic_t *enp)
1502 {
1503         efx_mcdi_req_t req;
1504         uint8_t payload[MAX(MC_CMD_ENTITY_RESET_IN_LEN,
1505                             MC_CMD_ENTITY_RESET_OUT_LEN)];
1506         efx_rc_t rc;
1507
1508         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
1509         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1510                 goto fail1;
1511         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1512                 goto fail2;
1513
1514         (void) memset(payload, 0, sizeof (payload));
1515         req.emr_cmd = MC_CMD_ENTITY_RESET;
1516         req.emr_in_buf = payload;
1517         req.emr_in_length = MC_CMD_ENTITY_RESET_IN_LEN;
1518         req.emr_out_buf = payload;
1519         req.emr_out_length = MC_CMD_ENTITY_RESET_OUT_LEN;
1520
1521         MCDI_IN_POPULATE_DWORD_1(req, ENTITY_RESET_IN_FLAG,
1522             ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1523
1524         efx_mcdi_execute(enp, &req);
1525
1526         if (req.emr_rc != 0) {
1527                 rc = req.emr_rc;
1528                 goto fail3;
1529         }
1530
1531         /* Clear RX/TX DMA queue errors */
1532         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
1533
1534         return (0);
1535
1536 fail3:
1537         EFSYS_PROBE(fail3);
1538 fail2:
1539         EFSYS_PROBE(fail2);
1540 fail1:
1541         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1542
1543         return (rc);
1544 }
1545
1546         __checkReturn   efx_rc_t
1547 ef10_nic_init(
1548         __in            efx_nic_t *enp)
1549 {
1550         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1551         uint32_t min_vi_count, max_vi_count;
1552         uint32_t vi_count, vi_base, vi_shift;
1553         uint32_t i;
1554         uint32_t retry;
1555         uint32_t delay_us;
1556         efx_rc_t rc;
1557
1558         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1559                     enp->en_family == EFX_FAMILY_MEDFORD);
1560
1561         /* Enable reporting of some events (e.g. link change) */
1562         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
1563                 goto fail1;
1564
1565         /* Allocate (optional) on-chip PIO buffers */
1566         ef10_nic_alloc_piobufs(enp, edcp->edc_max_piobuf_count);
1567
1568         /*
1569          * For best performance, PIO writes should use a write-combined
1570          * (WC) memory mapping. Using a separate WC mapping for the PIO
1571          * aperture of each VI would be a burden to drivers (and not
1572          * possible if the host page size is >4Kbyte).
1573          *
1574          * To avoid this we use a single uncached (UC) mapping for VI
1575          * register access, and a single WC mapping for extra VIs used
1576          * for PIO writes.
1577          *
1578          * Each piobuf must be linked to a VI in the WC mapping, and to
1579          * each VI that is using a sub-allocated block from the piobuf.
1580          */
1581         min_vi_count = edcp->edc_min_vi_count;
1582         max_vi_count =
1583             edcp->edc_max_vi_count + enp->en_arch.ef10.ena_piobuf_count;
1584
1585         /* Ensure that the previously attached driver's VIs are freed */
1586         if ((rc = efx_mcdi_free_vis(enp)) != 0)
1587                 goto fail2;
1588
1589         /*
1590          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
1591          * fails then retrying the request for fewer VI resources may succeed.
1592          */
1593         vi_count = 0;
1594         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
1595                     &vi_base, &vi_count, &vi_shift)) != 0)
1596                 goto fail3;
1597
1598         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
1599
1600         if (vi_count < min_vi_count) {
1601                 rc = ENOMEM;
1602                 goto fail4;
1603         }
1604
1605         enp->en_arch.ef10.ena_vi_base = vi_base;
1606         enp->en_arch.ef10.ena_vi_count = vi_count;
1607         enp->en_arch.ef10.ena_vi_shift = vi_shift;
1608
1609         if (vi_count < min_vi_count + enp->en_arch.ef10.ena_piobuf_count) {
1610                 /* Not enough extra VIs to map piobufs */
1611                 ef10_nic_free_piobufs(enp);
1612         }
1613
1614         enp->en_arch.ef10.ena_pio_write_vi_base =
1615             vi_count - enp->en_arch.ef10.ena_piobuf_count;
1616
1617         /* Save UC memory mapping details */
1618         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
1619         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1620                 enp->en_arch.ef10.ena_uc_mem_map_size =
1621                     (ER_DZ_TX_PIOBUF_STEP *
1622                     enp->en_arch.ef10.ena_pio_write_vi_base);
1623         } else {
1624                 enp->en_arch.ef10.ena_uc_mem_map_size =
1625                     (ER_DZ_TX_PIOBUF_STEP *
1626                     enp->en_arch.ef10.ena_vi_count);
1627         }
1628
1629         /* Save WC memory mapping details */
1630         enp->en_arch.ef10.ena_wc_mem_map_offset =
1631             enp->en_arch.ef10.ena_uc_mem_map_offset +
1632             enp->en_arch.ef10.ena_uc_mem_map_size;
1633
1634         enp->en_arch.ef10.ena_wc_mem_map_size =
1635             (ER_DZ_TX_PIOBUF_STEP *
1636             enp->en_arch.ef10.ena_piobuf_count);
1637
1638         /* Link piobufs to extra VIs in WC mapping */
1639         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1640                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1641                         rc = efx_mcdi_link_piobuf(enp,
1642                             enp->en_arch.ef10.ena_pio_write_vi_base + i,
1643                             enp->en_arch.ef10.ena_piobuf_handle[i]);
1644                         if (rc != 0)
1645                                 break;
1646                 }
1647         }
1648
1649         /*
1650          * Allocate a vAdaptor attached to our upstream vPort/pPort.
1651          *
1652          * On a VF, this may fail with MC_CMD_ERR_NO_EVB_PORT (ENOENT) if the PF
1653          * driver has yet to bring up the EVB port. See bug 56147. In this case,
1654          * retry the request several times after waiting a while. The wait time
1655          * between retries starts small (10ms) and exponentially increases.
1656          * Total wait time is a little over two seconds. Retry logic in the
1657          * client driver may mean this whole loop is repeated if it continues to
1658          * fail.
1659          */
1660         retry = 0;
1661         delay_us = 10000;
1662         while ((rc = efx_mcdi_vadaptor_alloc(enp, EVB_PORT_ID_ASSIGNED)) != 0) {
1663                 if (EFX_PCI_FUNCTION_IS_PF(&enp->en_nic_cfg) ||
1664                     (rc != ENOENT)) {
1665                         /*
1666                          * Do not retry alloc for PF, or for other errors on
1667                          * a VF.
1668                          */
1669                         goto fail5;
1670                 }
1671
1672                 /* VF startup before PF is ready. Retry allocation. */
1673                 if (retry > 5) {
1674                         /* Too many attempts */
1675                         rc = EINVAL;
1676                         goto fail6;
1677                 }
1678                 EFSYS_PROBE1(mcdi_no_evb_port_retry, int, retry);
1679                 EFSYS_SLEEP(delay_us);
1680                 retry++;
1681                 if (delay_us < 500000)
1682                         delay_us <<= 2;
1683         }
1684
1685         enp->en_vport_id = EVB_PORT_ID_ASSIGNED;
1686         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
1687
1688         return (0);
1689
1690 fail6:
1691         EFSYS_PROBE(fail6);
1692 fail5:
1693         EFSYS_PROBE(fail5);
1694 fail4:
1695         EFSYS_PROBE(fail4);
1696 fail3:
1697         EFSYS_PROBE(fail3);
1698 fail2:
1699         EFSYS_PROBE(fail2);
1700
1701         ef10_nic_free_piobufs(enp);
1702
1703 fail1:
1704         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1705
1706         return (rc);
1707 }
1708
1709         __checkReturn   efx_rc_t
1710 ef10_nic_get_vi_pool(
1711         __in            efx_nic_t *enp,
1712         __out           uint32_t *vi_countp)
1713 {
1714         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1715                     enp->en_family == EFX_FAMILY_MEDFORD);
1716
1717         /*
1718          * Report VIs that the client driver can use.
1719          * Do not include VIs used for PIO buffer writes.
1720          */
1721         *vi_countp = enp->en_arch.ef10.ena_pio_write_vi_base;
1722
1723         return (0);
1724 }
1725
1726         __checkReturn   efx_rc_t
1727 ef10_nic_get_bar_region(
1728         __in            efx_nic_t *enp,
1729         __in            efx_nic_region_t region,
1730         __out           uint32_t *offsetp,
1731         __out           size_t *sizep)
1732 {
1733         efx_rc_t rc;
1734
1735         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1736                     enp->en_family == EFX_FAMILY_MEDFORD);
1737
1738         /*
1739          * TODO: Specify host memory mapping alignment and granularity
1740          * in efx_drv_limits_t so that they can be taken into account
1741          * when allocating extra VIs for PIO writes.
1742          */
1743         switch (region) {
1744         case EFX_REGION_VI:
1745                 /* UC mapped memory BAR region for VI registers */
1746                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
1747                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
1748                 break;
1749
1750         case EFX_REGION_PIO_WRITE_VI:
1751                 /* WC mapped memory BAR region for piobuf writes */
1752                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
1753                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
1754                 break;
1755
1756         default:
1757                 rc = EINVAL;
1758                 goto fail1;
1759         }
1760
1761         return (0);
1762
1763 fail1:
1764         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1765
1766         return (rc);
1767 }
1768
1769                         void
1770 ef10_nic_fini(
1771         __in            efx_nic_t *enp)
1772 {
1773         uint32_t i;
1774         efx_rc_t rc;
1775
1776         (void) efx_mcdi_vadaptor_free(enp, enp->en_vport_id);
1777         enp->en_vport_id = 0;
1778
1779         /* Unlink piobufs from extra VIs in WC mapping */
1780         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1781                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1782                         rc = efx_mcdi_unlink_piobuf(enp,
1783                             enp->en_arch.ef10.ena_pio_write_vi_base + i);
1784                         if (rc != 0)
1785                                 break;
1786                 }
1787         }
1788
1789         ef10_nic_free_piobufs(enp);
1790
1791         (void) efx_mcdi_free_vis(enp);
1792         enp->en_arch.ef10.ena_vi_count = 0;
1793 }
1794
1795                         void
1796 ef10_nic_unprobe(
1797         __in            efx_nic_t *enp)
1798 {
1799 #if EFSYS_OPT_MON_STATS
1800         mcdi_mon_cfg_free(enp);
1801 #endif /* EFSYS_OPT_MON_STATS */
1802         (void) efx_mcdi_drv_attach(enp, B_FALSE);
1803 }
1804
1805 #if EFSYS_OPT_DIAG
1806
1807         __checkReturn   efx_rc_t
1808 ef10_nic_register_test(
1809         __in            efx_nic_t *enp)
1810 {
1811         efx_rc_t rc;
1812
1813         /* FIXME */
1814         _NOTE(ARGUNUSED(enp))
1815         _NOTE(CONSTANTCONDITION)
1816         if (B_FALSE) {
1817                 rc = ENOTSUP;
1818                 goto fail1;
1819         }
1820         /* FIXME */
1821
1822         return (0);
1823
1824 fail1:
1825         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1826
1827         return (rc);
1828 }
1829
1830 #endif  /* EFSYS_OPT_DIAG */
1831
1832
1833 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */