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