net/sfc/base: add Medford2 support to NIC module
[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_vector_cfg(
428         __in            efx_nic_t *enp,
429         __out_opt       uint32_t *vec_basep,
430         __out_opt       uint32_t *pf_nvecp,
431         __out_opt       uint32_t *vf_nvecp)
432 {
433         efx_mcdi_req_t req;
434         uint8_t payload[MAX(MC_CMD_GET_VECTOR_CFG_IN_LEN,
435                             MC_CMD_GET_VECTOR_CFG_OUT_LEN)];
436         efx_rc_t rc;
437
438         (void) memset(payload, 0, sizeof (payload));
439         req.emr_cmd = MC_CMD_GET_VECTOR_CFG;
440         req.emr_in_buf = payload;
441         req.emr_in_length = MC_CMD_GET_VECTOR_CFG_IN_LEN;
442         req.emr_out_buf = payload;
443         req.emr_out_length = MC_CMD_GET_VECTOR_CFG_OUT_LEN;
444
445         efx_mcdi_execute(enp, &req);
446
447         if (req.emr_rc != 0) {
448                 rc = req.emr_rc;
449                 goto fail1;
450         }
451
452         if (req.emr_out_length_used < MC_CMD_GET_VECTOR_CFG_OUT_LEN) {
453                 rc = EMSGSIZE;
454                 goto fail2;
455         }
456
457         if (vec_basep != NULL)
458                 *vec_basep = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VEC_BASE);
459         if (pf_nvecp != NULL)
460                 *pf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_PF);
461         if (vf_nvecp != NULL)
462                 *vf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_VF);
463
464         return (0);
465
466 fail2:
467         EFSYS_PROBE(fail2);
468 fail1:
469         EFSYS_PROBE1(fail1, efx_rc_t, rc);
470
471         return (rc);
472 }
473
474 static  __checkReturn   efx_rc_t
475 efx_mcdi_alloc_vis(
476         __in            efx_nic_t *enp,
477         __in            uint32_t min_vi_count,
478         __in            uint32_t max_vi_count,
479         __out           uint32_t *vi_basep,
480         __out           uint32_t *vi_countp,
481         __out           uint32_t *vi_shiftp)
482 {
483         efx_mcdi_req_t req;
484         uint8_t payload[MAX(MC_CMD_ALLOC_VIS_IN_LEN,
485                             MC_CMD_ALLOC_VIS_EXT_OUT_LEN)];
486         efx_rc_t rc;
487
488         if (vi_countp == NULL) {
489                 rc = EINVAL;
490                 goto fail1;
491         }
492
493         (void) memset(payload, 0, sizeof (payload));
494         req.emr_cmd = MC_CMD_ALLOC_VIS;
495         req.emr_in_buf = payload;
496         req.emr_in_length = MC_CMD_ALLOC_VIS_IN_LEN;
497         req.emr_out_buf = payload;
498         req.emr_out_length = MC_CMD_ALLOC_VIS_EXT_OUT_LEN;
499
500         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MIN_VI_COUNT, min_vi_count);
501         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MAX_VI_COUNT, max_vi_count);
502
503         efx_mcdi_execute(enp, &req);
504
505         if (req.emr_rc != 0) {
506                 rc = req.emr_rc;
507                 goto fail2;
508         }
509
510         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_OUT_LEN) {
511                 rc = EMSGSIZE;
512                 goto fail3;
513         }
514
515         *vi_basep = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_BASE);
516         *vi_countp = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_COUNT);
517
518         /* Report VI_SHIFT if available (always zero for Huntington) */
519         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_EXT_OUT_LEN)
520                 *vi_shiftp = 0;
521         else
522                 *vi_shiftp = MCDI_OUT_DWORD(req, ALLOC_VIS_EXT_OUT_VI_SHIFT);
523
524         return (0);
525
526 fail3:
527         EFSYS_PROBE(fail3);
528 fail2:
529         EFSYS_PROBE(fail2);
530 fail1:
531         EFSYS_PROBE1(fail1, efx_rc_t, rc);
532
533         return (rc);
534 }
535
536
537 static  __checkReturn   efx_rc_t
538 efx_mcdi_free_vis(
539         __in            efx_nic_t *enp)
540 {
541         efx_mcdi_req_t req;
542         efx_rc_t rc;
543
544         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_IN_LEN == 0);
545         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_OUT_LEN == 0);
546
547         req.emr_cmd = MC_CMD_FREE_VIS;
548         req.emr_in_buf = NULL;
549         req.emr_in_length = 0;
550         req.emr_out_buf = NULL;
551         req.emr_out_length = 0;
552
553         efx_mcdi_execute_quiet(enp, &req);
554
555         /* Ignore ELREADY (no allocated VIs, so nothing to free) */
556         if ((req.emr_rc != 0) && (req.emr_rc != EALREADY)) {
557                 rc = req.emr_rc;
558                 goto fail1;
559         }
560
561         return (0);
562
563 fail1:
564         EFSYS_PROBE1(fail1, efx_rc_t, rc);
565
566         return (rc);
567 }
568
569
570 static  __checkReturn   efx_rc_t
571 efx_mcdi_alloc_piobuf(
572         __in            efx_nic_t *enp,
573         __out           efx_piobuf_handle_t *handlep)
574 {
575         efx_mcdi_req_t req;
576         uint8_t payload[MAX(MC_CMD_ALLOC_PIOBUF_IN_LEN,
577                             MC_CMD_ALLOC_PIOBUF_OUT_LEN)];
578         efx_rc_t rc;
579
580         if (handlep == NULL) {
581                 rc = EINVAL;
582                 goto fail1;
583         }
584
585         (void) memset(payload, 0, sizeof (payload));
586         req.emr_cmd = MC_CMD_ALLOC_PIOBUF;
587         req.emr_in_buf = payload;
588         req.emr_in_length = MC_CMD_ALLOC_PIOBUF_IN_LEN;
589         req.emr_out_buf = payload;
590         req.emr_out_length = MC_CMD_ALLOC_PIOBUF_OUT_LEN;
591
592         efx_mcdi_execute_quiet(enp, &req);
593
594         if (req.emr_rc != 0) {
595                 rc = req.emr_rc;
596                 goto fail2;
597         }
598
599         if (req.emr_out_length_used < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
600                 rc = EMSGSIZE;
601                 goto fail3;
602         }
603
604         *handlep = MCDI_OUT_DWORD(req, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
605
606         return (0);
607
608 fail3:
609         EFSYS_PROBE(fail3);
610 fail2:
611         EFSYS_PROBE(fail2);
612 fail1:
613         EFSYS_PROBE1(fail1, efx_rc_t, rc);
614
615         return (rc);
616 }
617
618 static  __checkReturn   efx_rc_t
619 efx_mcdi_free_piobuf(
620         __in            efx_nic_t *enp,
621         __in            efx_piobuf_handle_t handle)
622 {
623         efx_mcdi_req_t req;
624         uint8_t payload[MAX(MC_CMD_FREE_PIOBUF_IN_LEN,
625                             MC_CMD_FREE_PIOBUF_OUT_LEN)];
626         efx_rc_t rc;
627
628         (void) memset(payload, 0, sizeof (payload));
629         req.emr_cmd = MC_CMD_FREE_PIOBUF;
630         req.emr_in_buf = payload;
631         req.emr_in_length = MC_CMD_FREE_PIOBUF_IN_LEN;
632         req.emr_out_buf = payload;
633         req.emr_out_length = MC_CMD_FREE_PIOBUF_OUT_LEN;
634
635         MCDI_IN_SET_DWORD(req, FREE_PIOBUF_IN_PIOBUF_HANDLE, handle);
636
637         efx_mcdi_execute_quiet(enp, &req);
638
639         if (req.emr_rc != 0) {
640                 rc = req.emr_rc;
641                 goto fail1;
642         }
643
644         return (0);
645
646 fail1:
647         EFSYS_PROBE1(fail1, efx_rc_t, rc);
648
649         return (rc);
650 }
651
652 static  __checkReturn   efx_rc_t
653 efx_mcdi_link_piobuf(
654         __in            efx_nic_t *enp,
655         __in            uint32_t vi_index,
656         __in            efx_piobuf_handle_t handle)
657 {
658         efx_mcdi_req_t req;
659         uint8_t payload[MAX(MC_CMD_LINK_PIOBUF_IN_LEN,
660                             MC_CMD_LINK_PIOBUF_OUT_LEN)];
661         efx_rc_t rc;
662
663         (void) memset(payload, 0, sizeof (payload));
664         req.emr_cmd = MC_CMD_LINK_PIOBUF;
665         req.emr_in_buf = payload;
666         req.emr_in_length = MC_CMD_LINK_PIOBUF_IN_LEN;
667         req.emr_out_buf = payload;
668         req.emr_out_length = MC_CMD_LINK_PIOBUF_OUT_LEN;
669
670         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_PIOBUF_HANDLE, handle);
671         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
672
673         efx_mcdi_execute(enp, &req);
674
675         if (req.emr_rc != 0) {
676                 rc = req.emr_rc;
677                 goto fail1;
678         }
679
680         return (0);
681
682 fail1:
683         EFSYS_PROBE1(fail1, efx_rc_t, rc);
684
685         return (rc);
686 }
687
688 static  __checkReturn   efx_rc_t
689 efx_mcdi_unlink_piobuf(
690         __in            efx_nic_t *enp,
691         __in            uint32_t vi_index)
692 {
693         efx_mcdi_req_t req;
694         uint8_t payload[MAX(MC_CMD_UNLINK_PIOBUF_IN_LEN,
695                             MC_CMD_UNLINK_PIOBUF_OUT_LEN)];
696         efx_rc_t rc;
697
698         (void) memset(payload, 0, sizeof (payload));
699         req.emr_cmd = MC_CMD_UNLINK_PIOBUF;
700         req.emr_in_buf = payload;
701         req.emr_in_length = MC_CMD_UNLINK_PIOBUF_IN_LEN;
702         req.emr_out_buf = payload;
703         req.emr_out_length = MC_CMD_UNLINK_PIOBUF_OUT_LEN;
704
705         MCDI_IN_SET_DWORD(req, UNLINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
706
707         efx_mcdi_execute_quiet(enp, &req);
708
709         if (req.emr_rc != 0) {
710                 rc = req.emr_rc;
711                 goto fail1;
712         }
713
714         return (0);
715
716 fail1:
717         EFSYS_PROBE1(fail1, efx_rc_t, rc);
718
719         return (rc);
720 }
721
722 static                  void
723 ef10_nic_alloc_piobufs(
724         __in            efx_nic_t *enp,
725         __in            uint32_t max_piobuf_count)
726 {
727         efx_piobuf_handle_t *handlep;
728         unsigned int i;
729
730         EFSYS_ASSERT3U(max_piobuf_count, <=,
731             EFX_ARRAY_SIZE(enp->en_arch.ef10.ena_piobuf_handle));
732
733         enp->en_arch.ef10.ena_piobuf_count = 0;
734
735         for (i = 0; i < max_piobuf_count; i++) {
736                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
737
738                 if (efx_mcdi_alloc_piobuf(enp, handlep) != 0)
739                         goto fail1;
740
741                 enp->en_arch.ef10.ena_pio_alloc_map[i] = 0;
742                 enp->en_arch.ef10.ena_piobuf_count++;
743         }
744
745         return;
746
747 fail1:
748         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
749                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
750
751                 efx_mcdi_free_piobuf(enp, *handlep);
752                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
753         }
754         enp->en_arch.ef10.ena_piobuf_count = 0;
755 }
756
757
758 static                  void
759 ef10_nic_free_piobufs(
760         __in            efx_nic_t *enp)
761 {
762         efx_piobuf_handle_t *handlep;
763         unsigned int i;
764
765         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
766                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
767
768                 efx_mcdi_free_piobuf(enp, *handlep);
769                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
770         }
771         enp->en_arch.ef10.ena_piobuf_count = 0;
772 }
773
774 /* Sub-allocate a block from a piobuf */
775         __checkReturn   efx_rc_t
776 ef10_nic_pio_alloc(
777         __inout         efx_nic_t *enp,
778         __out           uint32_t *bufnump,
779         __out           efx_piobuf_handle_t *handlep,
780         __out           uint32_t *blknump,
781         __out           uint32_t *offsetp,
782         __out           size_t *sizep)
783 {
784         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
785         efx_drv_cfg_t *edcp = &enp->en_drv_cfg;
786         uint32_t blk_per_buf;
787         uint32_t buf, blk;
788         efx_rc_t rc;
789
790         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
791             enp->en_family == EFX_FAMILY_MEDFORD ||
792             enp->en_family == EFX_FAMILY_MEDFORD2);
793         EFSYS_ASSERT(bufnump);
794         EFSYS_ASSERT(handlep);
795         EFSYS_ASSERT(blknump);
796         EFSYS_ASSERT(offsetp);
797         EFSYS_ASSERT(sizep);
798
799         if ((edcp->edc_pio_alloc_size == 0) ||
800             (enp->en_arch.ef10.ena_piobuf_count == 0)) {
801                 rc = ENOMEM;
802                 goto fail1;
803         }
804         blk_per_buf = encp->enc_piobuf_size / edcp->edc_pio_alloc_size;
805
806         for (buf = 0; buf < enp->en_arch.ef10.ena_piobuf_count; buf++) {
807                 uint32_t *map = &enp->en_arch.ef10.ena_pio_alloc_map[buf];
808
809                 if (~(*map) == 0)
810                         continue;
811
812                 EFSYS_ASSERT3U(blk_per_buf, <=, (8 * sizeof (*map)));
813                 for (blk = 0; blk < blk_per_buf; blk++) {
814                         if ((*map & (1u << blk)) == 0) {
815                                 *map |= (1u << blk);
816                                 goto done;
817                         }
818                 }
819         }
820         rc = ENOMEM;
821         goto fail2;
822
823 done:
824         *handlep = enp->en_arch.ef10.ena_piobuf_handle[buf];
825         *bufnump = buf;
826         *blknump = blk;
827         *sizep = edcp->edc_pio_alloc_size;
828         *offsetp = blk * (*sizep);
829
830         return (0);
831
832 fail2:
833         EFSYS_PROBE(fail2);
834 fail1:
835         EFSYS_PROBE1(fail1, efx_rc_t, rc);
836
837         return (rc);
838 }
839
840 /* Free a piobuf sub-allocated block */
841         __checkReturn   efx_rc_t
842 ef10_nic_pio_free(
843         __inout         efx_nic_t *enp,
844         __in            uint32_t bufnum,
845         __in            uint32_t blknum)
846 {
847         uint32_t *map;
848         efx_rc_t rc;
849
850         if ((bufnum >= enp->en_arch.ef10.ena_piobuf_count) ||
851             (blknum >= (8 * sizeof (*map)))) {
852                 rc = EINVAL;
853                 goto fail1;
854         }
855
856         map = &enp->en_arch.ef10.ena_pio_alloc_map[bufnum];
857         if ((*map & (1u << blknum)) == 0) {
858                 rc = ENOENT;
859                 goto fail2;
860         }
861         *map &= ~(1u << blknum);
862
863         return (0);
864
865 fail2:
866         EFSYS_PROBE(fail2);
867 fail1:
868         EFSYS_PROBE1(fail1, efx_rc_t, rc);
869
870         return (rc);
871 }
872
873         __checkReturn   efx_rc_t
874 ef10_nic_pio_link(
875         __inout         efx_nic_t *enp,
876         __in            uint32_t vi_index,
877         __in            efx_piobuf_handle_t handle)
878 {
879         return (efx_mcdi_link_piobuf(enp, vi_index, handle));
880 }
881
882         __checkReturn   efx_rc_t
883 ef10_nic_pio_unlink(
884         __inout         efx_nic_t *enp,
885         __in            uint32_t vi_index)
886 {
887         return (efx_mcdi_unlink_piobuf(enp, vi_index));
888 }
889
890 static  __checkReturn   efx_rc_t
891 ef10_mcdi_get_pf_count(
892         __in            efx_nic_t *enp,
893         __out           uint32_t *pf_countp)
894 {
895         efx_mcdi_req_t req;
896         uint8_t payload[MAX(MC_CMD_GET_PF_COUNT_IN_LEN,
897                             MC_CMD_GET_PF_COUNT_OUT_LEN)];
898         efx_rc_t rc;
899
900         (void) memset(payload, 0, sizeof (payload));
901         req.emr_cmd = MC_CMD_GET_PF_COUNT;
902         req.emr_in_buf = payload;
903         req.emr_in_length = MC_CMD_GET_PF_COUNT_IN_LEN;
904         req.emr_out_buf = payload;
905         req.emr_out_length = MC_CMD_GET_PF_COUNT_OUT_LEN;
906
907         efx_mcdi_execute(enp, &req);
908
909         if (req.emr_rc != 0) {
910                 rc = req.emr_rc;
911                 goto fail1;
912         }
913
914         if (req.emr_out_length_used < MC_CMD_GET_PF_COUNT_OUT_LEN) {
915                 rc = EMSGSIZE;
916                 goto fail2;
917         }
918
919         *pf_countp = *MCDI_OUT(req, uint8_t,
920                                 MC_CMD_GET_PF_COUNT_OUT_PF_COUNT_OFST);
921
922         EFSYS_ASSERT(*pf_countp != 0);
923
924         return (0);
925
926 fail2:
927         EFSYS_PROBE(fail2);
928 fail1:
929         EFSYS_PROBE1(fail1, efx_rc_t, rc);
930
931         return (rc);
932 }
933
934         __checkReturn   efx_rc_t
935 ef10_get_datapath_caps(
936         __in            efx_nic_t *enp)
937 {
938         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
939         uint32_t flags;
940         uint32_t flags2;
941         uint32_t tso2nc;
942         efx_rc_t rc;
943
944         if ((rc = efx_mcdi_get_capabilities(enp, &flags, NULL, NULL,
945                                             &flags2, &tso2nc)) != 0)
946                 goto fail1;
947
948         if ((rc = ef10_mcdi_get_pf_count(enp, &encp->enc_hw_pf_count)) != 0)
949                 goto fail1;
950
951 #define CAP_FLAG(flags1, field)         \
952         ((flags1) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
953
954 #define CAP_FLAG2(flags2, field)        \
955         ((flags2) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
956
957         /*
958          * Huntington RXDP firmware inserts a 0 or 14 byte prefix.
959          * We only support the 14 byte prefix here.
960          */
961         if (CAP_FLAG(flags, RX_PREFIX_LEN_14) == 0) {
962                 rc = ENOTSUP;
963                 goto fail2;
964         }
965         encp->enc_rx_prefix_size = 14;
966
967         /* Check if the firmware supports TSO */
968         encp->enc_fw_assisted_tso_enabled =
969             CAP_FLAG(flags, TX_TSO) ? B_TRUE : B_FALSE;
970
971         /* Check if the firmware supports FATSOv2 */
972         encp->enc_fw_assisted_tso_v2_enabled =
973             CAP_FLAG2(flags2, TX_TSO_V2) ? B_TRUE : B_FALSE;
974
975         /* Get the number of TSO contexts (FATSOv2) */
976         encp->enc_fw_assisted_tso_v2_n_contexts =
977                 CAP_FLAG2(flags2, TX_TSO_V2) ? tso2nc : 0;
978
979         /* Check if the firmware has vadapter/vport/vswitch support */
980         encp->enc_datapath_cap_evb =
981             CAP_FLAG(flags, EVB) ? B_TRUE : B_FALSE;
982
983         /* Check if the firmware supports VLAN insertion */
984         encp->enc_hw_tx_insert_vlan_enabled =
985             CAP_FLAG(flags, TX_VLAN_INSERTION) ? B_TRUE : B_FALSE;
986
987         /* Check if the firmware supports RX event batching */
988         encp->enc_rx_batching_enabled =
989             CAP_FLAG(flags, RX_BATCHING) ? B_TRUE : B_FALSE;
990
991         /*
992          * Even if batching isn't reported as supported, we may still get
993          * batched events (see bug61153).
994          */
995         encp->enc_rx_batch_max = 16;
996
997         /* Check if the firmware supports disabling scatter on RXQs */
998         encp->enc_rx_disable_scatter_supported =
999             CAP_FLAG(flags, RX_DISABLE_SCATTER) ? B_TRUE : B_FALSE;
1000
1001         /* Check if the firmware supports packed stream mode */
1002         encp->enc_rx_packed_stream_supported =
1003             CAP_FLAG(flags, RX_PACKED_STREAM) ? B_TRUE : B_FALSE;
1004
1005         /*
1006          * Check if the firmware supports configurable buffer sizes
1007          * for packed stream mode (otherwise buffer size is 1Mbyte)
1008          */
1009         encp->enc_rx_var_packed_stream_supported =
1010             CAP_FLAG(flags, RX_PACKED_STREAM_VAR_BUFFERS) ? B_TRUE : B_FALSE;
1011
1012         /* Check if the firmware supports set mac with running filters */
1013         encp->enc_allow_set_mac_with_installed_filters =
1014             CAP_FLAG(flags, VADAPTOR_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED) ?
1015             B_TRUE : B_FALSE;
1016
1017         /*
1018          * Check if firmware supports the extended MC_CMD_SET_MAC, which allows
1019          * specifying which parameters to configure.
1020          */
1021         encp->enc_enhanced_set_mac_supported =
1022                 CAP_FLAG(flags, SET_MAC_ENHANCED) ? B_TRUE : B_FALSE;
1023
1024         /*
1025          * Check if firmware supports version 2 of MC_CMD_INIT_EVQ, which allows
1026          * us to let the firmware choose the settings to use on an EVQ.
1027          */
1028         encp->enc_init_evq_v2_supported =
1029                 CAP_FLAG2(flags2, INIT_EVQ_V2) ? B_TRUE : B_FALSE;
1030
1031         /*
1032          * Check if firmware-verified NVRAM updates must be used.
1033          *
1034          * The firmware trusted installer requires all NVRAM updates to use
1035          * version 2 of MC_CMD_NVRAM_UPDATE_START (to enable verified update)
1036          * and version 2 of MC_CMD_NVRAM_UPDATE_FINISH (to verify the updated
1037          * partition and report the result).
1038          */
1039         encp->enc_nvram_update_verify_result_supported =
1040             CAP_FLAG2(flags2, NVRAM_UPDATE_REPORT_VERIFY_RESULT) ?
1041             B_TRUE : B_FALSE;
1042
1043         /*
1044          * Check if firmware provides packet memory and Rx datapath
1045          * counters.
1046          */
1047         encp->enc_pm_and_rxdp_counters =
1048             CAP_FLAG(flags, PM_AND_RXDP_COUNTERS) ? B_TRUE : B_FALSE;
1049
1050         /*
1051          * Check if the 40G MAC hardware is capable of reporting
1052          * statistics for Tx size bins.
1053          */
1054         encp->enc_mac_stats_40g_tx_size_bins =
1055             CAP_FLAG2(flags2, MAC_STATS_40G_TX_SIZE_BINS) ? B_TRUE : B_FALSE;
1056
1057         /*
1058          * Check if firmware supports VXLAN and NVGRE tunnels.
1059          * The capability indicates Geneve protocol support as well.
1060          */
1061         if (CAP_FLAG(flags, VXLAN_NVGRE)) {
1062                 encp->enc_tunnel_encapsulations_supported =
1063                     (1u << EFX_TUNNEL_PROTOCOL_VXLAN) |
1064                     (1u << EFX_TUNNEL_PROTOCOL_GENEVE) |
1065                     (1u << EFX_TUNNEL_PROTOCOL_NVGRE);
1066
1067                 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
1068                     MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
1069                 encp->enc_tunnel_config_udp_entries_max =
1070                     EFX_TUNNEL_MAXNENTRIES;
1071         } else {
1072                 encp->enc_tunnel_config_udp_entries_max = 0;
1073         }
1074
1075 #undef CAP_FLAG
1076 #undef CAP_FLAG2
1077
1078         return (0);
1079
1080 fail2:
1081         EFSYS_PROBE(fail2);
1082 fail1:
1083         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1084
1085         return (rc);
1086 }
1087
1088
1089 #define EF10_LEGACY_PF_PRIVILEGE_MASK                                   \
1090         (MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN                     |       \
1091         MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK                       |       \
1092         MC_CMD_PRIVILEGE_MASK_IN_GRP_ONLOAD                     |       \
1093         MC_CMD_PRIVILEGE_MASK_IN_GRP_PTP                        |       \
1094         MC_CMD_PRIVILEGE_MASK_IN_GRP_INSECURE_FILTERS           |       \
1095         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING               |       \
1096         MC_CMD_PRIVILEGE_MASK_IN_GRP_UNICAST                    |       \
1097         MC_CMD_PRIVILEGE_MASK_IN_GRP_MULTICAST                  |       \
1098         MC_CMD_PRIVILEGE_MASK_IN_GRP_BROADCAST                  |       \
1099         MC_CMD_PRIVILEGE_MASK_IN_GRP_ALL_MULTICAST              |       \
1100         MC_CMD_PRIVILEGE_MASK_IN_GRP_PROMISCUOUS)
1101
1102 #define EF10_LEGACY_VF_PRIVILEGE_MASK   0
1103
1104
1105         __checkReturn           efx_rc_t
1106 ef10_get_privilege_mask(
1107         __in                    efx_nic_t *enp,
1108         __out                   uint32_t *maskp)
1109 {
1110         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1111         uint32_t mask;
1112         efx_rc_t rc;
1113
1114         if ((rc = efx_mcdi_privilege_mask(enp, encp->enc_pf, encp->enc_vf,
1115                                             &mask)) != 0) {
1116                 if (rc != ENOTSUP)
1117                         goto fail1;
1118
1119                 /* Fallback for old firmware without privilege mask support */
1120                 if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1121                         /* Assume PF has admin privilege */
1122                         mask = EF10_LEGACY_PF_PRIVILEGE_MASK;
1123                 } else {
1124                         /* VF is always unprivileged by default */
1125                         mask = EF10_LEGACY_VF_PRIVILEGE_MASK;
1126                 }
1127         }
1128
1129         *maskp = mask;
1130
1131         return (0);
1132
1133 fail1:
1134         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1135
1136         return (rc);
1137 }
1138
1139
1140 /*
1141  * Table of mapping schemes from port number to the number of the external
1142  * connector on the board. The external numbering does not distinguish
1143  * off-board separated outputs such as from multi-headed cables.
1144  *
1145  * The count of adjacent port numbers that map to each external port
1146  * and the offset in the numbering, is determined by the chip family and
1147  * current port mode.
1148  *
1149  * For the Huntington family, the current port mode cannot be discovered,
1150  * so the mapping used is instead the last match in the table to the full
1151  * set of port modes to which the NIC can be configured. Therefore the
1152  * ordering of entries in the mapping table is significant.
1153  */
1154 static struct {
1155         efx_family_t    family;
1156         uint32_t        modes_mask;
1157         int32_t         count;
1158         int32_t         offset;
1159 }       __ef10_external_port_mappings[] = {
1160         /* Supported modes with 1 output per external port */
1161         {
1162                 EFX_FAMILY_HUNTINGTON,
1163                 (1 << TLV_PORT_MODE_10G) |
1164                 (1 << TLV_PORT_MODE_10G_10G) |
1165                 (1 << TLV_PORT_MODE_10G_10G_10G_10G),
1166                 1,
1167                 1
1168         },
1169         {
1170                 EFX_FAMILY_MEDFORD,
1171                 (1 << TLV_PORT_MODE_10G) |
1172                 (1 << TLV_PORT_MODE_10G_10G),
1173                 1,
1174                 1
1175         },
1176         /* Supported modes with 2 outputs per external port */
1177         {
1178                 EFX_FAMILY_HUNTINGTON,
1179                 (1 << TLV_PORT_MODE_40G) |
1180                 (1 << TLV_PORT_MODE_40G_40G) |
1181                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1182                 (1 << TLV_PORT_MODE_10G_10G_40G),
1183                 2,
1184                 1
1185         },
1186         {
1187                 EFX_FAMILY_MEDFORD,
1188                 (1 << TLV_PORT_MODE_40G) |
1189                 (1 << TLV_PORT_MODE_40G_40G) |
1190                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1191                 (1 << TLV_PORT_MODE_10G_10G_40G) |
1192                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2),
1193                 2,
1194                 1
1195         },
1196         /* Supported modes with 4 outputs per external port */
1197         {
1198                 EFX_FAMILY_MEDFORD,
1199                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q) |
1200                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1),
1201                 4,
1202                 1,
1203         },
1204         {
1205                 EFX_FAMILY_MEDFORD,
1206                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q2),
1207                 4,
1208                 2
1209         },
1210 };
1211
1212         __checkReturn   efx_rc_t
1213 ef10_external_port_mapping(
1214         __in            efx_nic_t *enp,
1215         __in            uint32_t port,
1216         __out           uint8_t *external_portp)
1217 {
1218         efx_rc_t rc;
1219         int i;
1220         uint32_t port_modes;
1221         uint32_t matches;
1222         uint32_t current;
1223         int32_t count = 1; /* Default 1-1 mapping */
1224         int32_t offset = 1; /* Default starting external port number */
1225
1226         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, &current)) != 0) {
1227                 /*
1228                  * No current port mode information
1229                  * - infer mapping from available modes
1230                  */
1231                 if ((rc = efx_mcdi_get_port_modes(enp,
1232                             &port_modes, NULL)) != 0) {
1233                         /*
1234                          * No port mode information available
1235                          * - use default mapping
1236                          */
1237                         goto out;
1238                 }
1239         } else {
1240                 /* Only need to scan the current mode */
1241                 port_modes = 1 << current;
1242         }
1243
1244         /*
1245          * Infer the internal port -> external port mapping from
1246          * the possible port modes for this NIC.
1247          */
1248         for (i = 0; i < EFX_ARRAY_SIZE(__ef10_external_port_mappings); ++i) {
1249                 if (__ef10_external_port_mappings[i].family !=
1250                     enp->en_family)
1251                         continue;
1252                 matches = (__ef10_external_port_mappings[i].modes_mask &
1253                     port_modes);
1254                 if (matches != 0) {
1255                         count = __ef10_external_port_mappings[i].count;
1256                         offset = __ef10_external_port_mappings[i].offset;
1257                         port_modes &= ~matches;
1258                 }
1259         }
1260
1261         if (port_modes != 0) {
1262                 /* Some advertised modes are not supported */
1263                 rc = ENOTSUP;
1264                 goto fail1;
1265         }
1266
1267 out:
1268         /*
1269          * Scale as required by last matched mode and then convert to
1270          * correctly offset numbering
1271          */
1272         *external_portp = (uint8_t)((port / count) + offset);
1273         return (0);
1274
1275 fail1:
1276         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1277
1278         return (rc);
1279 }
1280
1281
1282         __checkReturn   efx_rc_t
1283 ef10_nic_probe(
1284         __in            efx_nic_t *enp)
1285 {
1286         const efx_nic_ops_t *enop = enp->en_enop;
1287         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1288         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1289         efx_rc_t rc;
1290
1291         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1292             enp->en_family == EFX_FAMILY_MEDFORD ||
1293             enp->en_family == EFX_FAMILY_MEDFORD2);
1294
1295         /* Read and clear any assertion state */
1296         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1297                 goto fail1;
1298
1299         /* Exit the assertion handler */
1300         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1301                 if (rc != EACCES)
1302                         goto fail2;
1303
1304         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
1305                 goto fail3;
1306
1307         if ((rc = enop->eno_board_cfg(enp)) != 0)
1308                 if (rc != EACCES)
1309                         goto fail4;
1310
1311         /*
1312          * Set default driver config limits (based on board config).
1313          *
1314          * FIXME: For now allocate a fixed number of VIs which is likely to be
1315          * sufficient and small enough to allow multiple functions on the same
1316          * port.
1317          */
1318         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
1319             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
1320
1321         /* The client driver must configure and enable PIO buffer support */
1322         edcp->edc_max_piobuf_count = 0;
1323         edcp->edc_pio_alloc_size = 0;
1324
1325 #if EFSYS_OPT_MAC_STATS
1326         /* Wipe the MAC statistics */
1327         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
1328                 goto fail5;
1329 #endif
1330
1331 #if EFSYS_OPT_LOOPBACK
1332         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
1333                 goto fail6;
1334 #endif
1335
1336 #if EFSYS_OPT_MON_STATS
1337         if ((rc = mcdi_mon_cfg_build(enp)) != 0) {
1338                 /* Unprivileged functions do not have access to sensors */
1339                 if (rc != EACCES)
1340                         goto fail7;
1341         }
1342 #endif
1343
1344         encp->enc_features = enp->en_features;
1345
1346         return (0);
1347
1348 #if EFSYS_OPT_MON_STATS
1349 fail7:
1350         EFSYS_PROBE(fail7);
1351 #endif
1352 #if EFSYS_OPT_LOOPBACK
1353 fail6:
1354         EFSYS_PROBE(fail6);
1355 #endif
1356 #if EFSYS_OPT_MAC_STATS
1357 fail5:
1358         EFSYS_PROBE(fail5);
1359 #endif
1360 fail4:
1361         EFSYS_PROBE(fail4);
1362 fail3:
1363         EFSYS_PROBE(fail3);
1364 fail2:
1365         EFSYS_PROBE(fail2);
1366 fail1:
1367         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1368
1369         return (rc);
1370 }
1371
1372         __checkReturn   efx_rc_t
1373 ef10_nic_set_drv_limits(
1374         __inout         efx_nic_t *enp,
1375         __in            efx_drv_limits_t *edlp)
1376 {
1377         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1378         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1379         uint32_t min_evq_count, max_evq_count;
1380         uint32_t min_rxq_count, max_rxq_count;
1381         uint32_t min_txq_count, max_txq_count;
1382         efx_rc_t rc;
1383
1384         if (edlp == NULL) {
1385                 rc = EINVAL;
1386                 goto fail1;
1387         }
1388
1389         /* Get minimum required and maximum usable VI limits */
1390         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
1391         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
1392         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
1393
1394         edcp->edc_min_vi_count =
1395             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
1396
1397         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
1398         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
1399         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
1400
1401         edcp->edc_max_vi_count =
1402             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
1403
1404         /*
1405          * Check limits for sub-allocated piobuf blocks.
1406          * PIO is optional, so don't fail if the limits are incorrect.
1407          */
1408         if ((encp->enc_piobuf_size == 0) ||
1409             (encp->enc_piobuf_limit == 0) ||
1410             (edlp->edl_min_pio_alloc_size == 0) ||
1411             (edlp->edl_min_pio_alloc_size > encp->enc_piobuf_size)) {
1412                 /* Disable PIO */
1413                 edcp->edc_max_piobuf_count = 0;
1414                 edcp->edc_pio_alloc_size = 0;
1415         } else {
1416                 uint32_t blk_size, blk_count, blks_per_piobuf;
1417
1418                 blk_size =
1419                     MAX(edlp->edl_min_pio_alloc_size,
1420                             encp->enc_piobuf_min_alloc_size);
1421
1422                 blks_per_piobuf = encp->enc_piobuf_size / blk_size;
1423                 EFSYS_ASSERT3U(blks_per_piobuf, <=, 32);
1424
1425                 blk_count = (encp->enc_piobuf_limit * blks_per_piobuf);
1426
1427                 /* A zero max pio alloc count means unlimited */
1428                 if ((edlp->edl_max_pio_alloc_count > 0) &&
1429                     (edlp->edl_max_pio_alloc_count < blk_count)) {
1430                         blk_count = edlp->edl_max_pio_alloc_count;
1431                 }
1432
1433                 edcp->edc_pio_alloc_size = blk_size;
1434                 edcp->edc_max_piobuf_count =
1435                     (blk_count + (blks_per_piobuf - 1)) / blks_per_piobuf;
1436         }
1437
1438         return (0);
1439
1440 fail1:
1441         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1442
1443         return (rc);
1444 }
1445
1446
1447         __checkReturn   efx_rc_t
1448 ef10_nic_reset(
1449         __in            efx_nic_t *enp)
1450 {
1451         efx_mcdi_req_t req;
1452         uint8_t payload[MAX(MC_CMD_ENTITY_RESET_IN_LEN,
1453                             MC_CMD_ENTITY_RESET_OUT_LEN)];
1454         efx_rc_t rc;
1455
1456         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
1457         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1458                 goto fail1;
1459         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1460                 goto fail2;
1461
1462         (void) memset(payload, 0, sizeof (payload));
1463         req.emr_cmd = MC_CMD_ENTITY_RESET;
1464         req.emr_in_buf = payload;
1465         req.emr_in_length = MC_CMD_ENTITY_RESET_IN_LEN;
1466         req.emr_out_buf = payload;
1467         req.emr_out_length = MC_CMD_ENTITY_RESET_OUT_LEN;
1468
1469         MCDI_IN_POPULATE_DWORD_1(req, ENTITY_RESET_IN_FLAG,
1470             ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1471
1472         efx_mcdi_execute(enp, &req);
1473
1474         if (req.emr_rc != 0) {
1475                 rc = req.emr_rc;
1476                 goto fail3;
1477         }
1478
1479         /* Clear RX/TX DMA queue errors */
1480         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
1481
1482         return (0);
1483
1484 fail3:
1485         EFSYS_PROBE(fail3);
1486 fail2:
1487         EFSYS_PROBE(fail2);
1488 fail1:
1489         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1490
1491         return (rc);
1492 }
1493
1494         __checkReturn   efx_rc_t
1495 ef10_nic_init(
1496         __in            efx_nic_t *enp)
1497 {
1498         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1499         uint32_t min_vi_count, max_vi_count;
1500         uint32_t vi_count, vi_base, vi_shift;
1501         uint32_t i;
1502         uint32_t retry;
1503         uint32_t delay_us;
1504         efx_rc_t rc;
1505
1506         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1507             enp->en_family == EFX_FAMILY_MEDFORD ||
1508             enp->en_family == EFX_FAMILY_MEDFORD2);
1509
1510         /* Enable reporting of some events (e.g. link change) */
1511         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
1512                 goto fail1;
1513
1514         /* Allocate (optional) on-chip PIO buffers */
1515         ef10_nic_alloc_piobufs(enp, edcp->edc_max_piobuf_count);
1516
1517         /*
1518          * For best performance, PIO writes should use a write-combined
1519          * (WC) memory mapping. Using a separate WC mapping for the PIO
1520          * aperture of each VI would be a burden to drivers (and not
1521          * possible if the host page size is >4Kbyte).
1522          *
1523          * To avoid this we use a single uncached (UC) mapping for VI
1524          * register access, and a single WC mapping for extra VIs used
1525          * for PIO writes.
1526          *
1527          * Each piobuf must be linked to a VI in the WC mapping, and to
1528          * each VI that is using a sub-allocated block from the piobuf.
1529          */
1530         min_vi_count = edcp->edc_min_vi_count;
1531         max_vi_count =
1532             edcp->edc_max_vi_count + enp->en_arch.ef10.ena_piobuf_count;
1533
1534         /* Ensure that the previously attached driver's VIs are freed */
1535         if ((rc = efx_mcdi_free_vis(enp)) != 0)
1536                 goto fail2;
1537
1538         /*
1539          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
1540          * fails then retrying the request for fewer VI resources may succeed.
1541          */
1542         vi_count = 0;
1543         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
1544                     &vi_base, &vi_count, &vi_shift)) != 0)
1545                 goto fail3;
1546
1547         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
1548
1549         if (vi_count < min_vi_count) {
1550                 rc = ENOMEM;
1551                 goto fail4;
1552         }
1553
1554         enp->en_arch.ef10.ena_vi_base = vi_base;
1555         enp->en_arch.ef10.ena_vi_count = vi_count;
1556         enp->en_arch.ef10.ena_vi_shift = vi_shift;
1557
1558         if (vi_count < min_vi_count + enp->en_arch.ef10.ena_piobuf_count) {
1559                 /* Not enough extra VIs to map piobufs */
1560                 ef10_nic_free_piobufs(enp);
1561         }
1562
1563         enp->en_arch.ef10.ena_pio_write_vi_base =
1564             vi_count - enp->en_arch.ef10.ena_piobuf_count;
1565
1566         /* Save UC memory mapping details */
1567         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
1568         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1569                 enp->en_arch.ef10.ena_uc_mem_map_size =
1570                     (ER_DZ_TX_PIOBUF_STEP *
1571                     enp->en_arch.ef10.ena_pio_write_vi_base);
1572         } else {
1573                 enp->en_arch.ef10.ena_uc_mem_map_size =
1574                     (ER_DZ_TX_PIOBUF_STEP *
1575                     enp->en_arch.ef10.ena_vi_count);
1576         }
1577
1578         /* Save WC memory mapping details */
1579         enp->en_arch.ef10.ena_wc_mem_map_offset =
1580             enp->en_arch.ef10.ena_uc_mem_map_offset +
1581             enp->en_arch.ef10.ena_uc_mem_map_size;
1582
1583         enp->en_arch.ef10.ena_wc_mem_map_size =
1584             (ER_DZ_TX_PIOBUF_STEP *
1585             enp->en_arch.ef10.ena_piobuf_count);
1586
1587         /* Link piobufs to extra VIs in WC mapping */
1588         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1589                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1590                         rc = efx_mcdi_link_piobuf(enp,
1591                             enp->en_arch.ef10.ena_pio_write_vi_base + i,
1592                             enp->en_arch.ef10.ena_piobuf_handle[i]);
1593                         if (rc != 0)
1594                                 break;
1595                 }
1596         }
1597
1598         /*
1599          * Allocate a vAdaptor attached to our upstream vPort/pPort.
1600          *
1601          * On a VF, this may fail with MC_CMD_ERR_NO_EVB_PORT (ENOENT) if the PF
1602          * driver has yet to bring up the EVB port. See bug 56147. In this case,
1603          * retry the request several times after waiting a while. The wait time
1604          * between retries starts small (10ms) and exponentially increases.
1605          * Total wait time is a little over two seconds. Retry logic in the
1606          * client driver may mean this whole loop is repeated if it continues to
1607          * fail.
1608          */
1609         retry = 0;
1610         delay_us = 10000;
1611         while ((rc = efx_mcdi_vadaptor_alloc(enp, EVB_PORT_ID_ASSIGNED)) != 0) {
1612                 if (EFX_PCI_FUNCTION_IS_PF(&enp->en_nic_cfg) ||
1613                     (rc != ENOENT)) {
1614                         /*
1615                          * Do not retry alloc for PF, or for other errors on
1616                          * a VF.
1617                          */
1618                         goto fail5;
1619                 }
1620
1621                 /* VF startup before PF is ready. Retry allocation. */
1622                 if (retry > 5) {
1623                         /* Too many attempts */
1624                         rc = EINVAL;
1625                         goto fail6;
1626                 }
1627                 EFSYS_PROBE1(mcdi_no_evb_port_retry, int, retry);
1628                 EFSYS_SLEEP(delay_us);
1629                 retry++;
1630                 if (delay_us < 500000)
1631                         delay_us <<= 2;
1632         }
1633
1634         enp->en_vport_id = EVB_PORT_ID_ASSIGNED;
1635         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
1636
1637         return (0);
1638
1639 fail6:
1640         EFSYS_PROBE(fail6);
1641 fail5:
1642         EFSYS_PROBE(fail5);
1643 fail4:
1644         EFSYS_PROBE(fail4);
1645 fail3:
1646         EFSYS_PROBE(fail3);
1647 fail2:
1648         EFSYS_PROBE(fail2);
1649
1650         ef10_nic_free_piobufs(enp);
1651
1652 fail1:
1653         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1654
1655         return (rc);
1656 }
1657
1658         __checkReturn   efx_rc_t
1659 ef10_nic_get_vi_pool(
1660         __in            efx_nic_t *enp,
1661         __out           uint32_t *vi_countp)
1662 {
1663         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1664             enp->en_family == EFX_FAMILY_MEDFORD ||
1665             enp->en_family == EFX_FAMILY_MEDFORD2);
1666
1667         /*
1668          * Report VIs that the client driver can use.
1669          * Do not include VIs used for PIO buffer writes.
1670          */
1671         *vi_countp = enp->en_arch.ef10.ena_pio_write_vi_base;
1672
1673         return (0);
1674 }
1675
1676         __checkReturn   efx_rc_t
1677 ef10_nic_get_bar_region(
1678         __in            efx_nic_t *enp,
1679         __in            efx_nic_region_t region,
1680         __out           uint32_t *offsetp,
1681         __out           size_t *sizep)
1682 {
1683         efx_rc_t rc;
1684
1685         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1686             enp->en_family == EFX_FAMILY_MEDFORD ||
1687             enp->en_family == EFX_FAMILY_MEDFORD2);
1688
1689         /*
1690          * TODO: Specify host memory mapping alignment and granularity
1691          * in efx_drv_limits_t so that they can be taken into account
1692          * when allocating extra VIs for PIO writes.
1693          */
1694         switch (region) {
1695         case EFX_REGION_VI:
1696                 /* UC mapped memory BAR region for VI registers */
1697                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
1698                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
1699                 break;
1700
1701         case EFX_REGION_PIO_WRITE_VI:
1702                 /* WC mapped memory BAR region for piobuf writes */
1703                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
1704                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
1705                 break;
1706
1707         default:
1708                 rc = EINVAL;
1709                 goto fail1;
1710         }
1711
1712         return (0);
1713
1714 fail1:
1715         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1716
1717         return (rc);
1718 }
1719
1720                         void
1721 ef10_nic_fini(
1722         __in            efx_nic_t *enp)
1723 {
1724         uint32_t i;
1725         efx_rc_t rc;
1726
1727         (void) efx_mcdi_vadaptor_free(enp, enp->en_vport_id);
1728         enp->en_vport_id = 0;
1729
1730         /* Unlink piobufs from extra VIs in WC mapping */
1731         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1732                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1733                         rc = efx_mcdi_unlink_piobuf(enp,
1734                             enp->en_arch.ef10.ena_pio_write_vi_base + i);
1735                         if (rc != 0)
1736                                 break;
1737                 }
1738         }
1739
1740         ef10_nic_free_piobufs(enp);
1741
1742         (void) efx_mcdi_free_vis(enp);
1743         enp->en_arch.ef10.ena_vi_count = 0;
1744 }
1745
1746                         void
1747 ef10_nic_unprobe(
1748         __in            efx_nic_t *enp)
1749 {
1750 #if EFSYS_OPT_MON_STATS
1751         mcdi_mon_cfg_free(enp);
1752 #endif /* EFSYS_OPT_MON_STATS */
1753         (void) efx_mcdi_drv_attach(enp, B_FALSE);
1754 }
1755
1756 #if EFSYS_OPT_DIAG
1757
1758         __checkReturn   efx_rc_t
1759 ef10_nic_register_test(
1760         __in            efx_nic_t *enp)
1761 {
1762         efx_rc_t rc;
1763
1764         /* FIXME */
1765         _NOTE(ARGUNUSED(enp))
1766         _NOTE(CONSTANTCONDITION)
1767         if (B_FALSE) {
1768                 rc = ENOTSUP;
1769                 goto fail1;
1770         }
1771         /* FIXME */
1772
1773         return (0);
1774
1775 fail1:
1776         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1777
1778         return (rc);
1779 }
1780
1781 #endif  /* EFSYS_OPT_DIAG */
1782
1783
1784 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */