net/ena: fix Rx checksum errors statistics
[dpdk.git] / drivers / net / sfc / base / ef10_vpd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2009-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9
10
11 #if EFSYS_OPT_VPD
12
13 #if EFX_OPTS_EF10()
14
15 #include "ef10_tlv_layout.h"
16
17         __checkReturn           efx_rc_t
18 ef10_vpd_init(
19         __in                    efx_nic_t *enp)
20 {
21         caddr_t svpd;
22         size_t svpd_size;
23         uint32_t pci_pf;
24         uint32_t tag;
25         efx_rc_t rc;
26
27         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
28         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
29
30         if (enp->en_nic_cfg.enc_vpd_is_global) {
31                 tag = TLV_TAG_GLOBAL_STATIC_VPD;
32         } else {
33                 pci_pf = enp->en_nic_cfg.enc_pf;
34                 tag = TLV_TAG_PF_STATIC_VPD(pci_pf);
35         }
36
37         /*
38          * The VPD interface exposes VPD resources from the combined static and
39          * dynamic VPD storage. As the static VPD configuration should *never*
40          * change, we can cache it.
41          */
42         svpd = NULL;
43         svpd_size = 0;
44         rc = ef10_nvram_partn_read_tlv(enp,
45             NVRAM_PARTITION_TYPE_STATIC_CONFIG,
46             tag, &svpd, &svpd_size);
47         if (rc != 0) {
48                 if (rc == EACCES) {
49                         /* Unprivileged functions cannot access VPD */
50                         goto out;
51                 }
52                 goto fail1;
53         }
54
55         if (svpd != NULL && svpd_size > 0) {
56                 if ((rc = efx_vpd_hunk_verify(svpd, svpd_size, NULL)) != 0)
57                         goto fail2;
58         }
59
60         enp->en_arch.ef10.ena_svpd = svpd;
61         enp->en_arch.ef10.ena_svpd_length = svpd_size;
62
63 out:
64         return (0);
65
66 fail2:
67         EFSYS_PROBE(fail2);
68
69         EFSYS_KMEM_FREE(enp->en_esip, svpd_size, svpd);
70 fail1:
71         EFSYS_PROBE1(fail1, efx_rc_t, rc);
72
73         return (rc);
74 }
75
76         __checkReturn           efx_rc_t
77 ef10_vpd_size(
78         __in                    efx_nic_t *enp,
79         __out                   size_t *sizep)
80 {
81         efx_rc_t rc;
82
83         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
84
85         /*
86          * This function returns the total size the user should allocate
87          * for all VPD operations. We've already cached the static vpd,
88          * so we just need to return an upper bound on the dynamic vpd,
89          * which is the size of the DYNAMIC_CONFIG partition.
90          */
91         if ((rc = efx_mcdi_nvram_info(enp, NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG,
92                     sizep, NULL, NULL, NULL)) != 0)
93                 goto fail1;
94
95         return (0);
96
97 fail1:
98         EFSYS_PROBE1(fail1, efx_rc_t, rc);
99
100         return (rc);
101 }
102
103         __checkReturn           efx_rc_t
104 ef10_vpd_read(
105         __in                    efx_nic_t *enp,
106         __out_bcount(size)      caddr_t data,
107         __in                    size_t size)
108 {
109         caddr_t dvpd;
110         size_t dvpd_size;
111         uint32_t pci_pf;
112         uint32_t tag;
113         efx_rc_t rc;
114
115         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
116
117         if (enp->en_nic_cfg.enc_vpd_is_global) {
118                 tag = TLV_TAG_GLOBAL_DYNAMIC_VPD;
119         } else {
120                 pci_pf = enp->en_nic_cfg.enc_pf;
121                 tag = TLV_TAG_PF_DYNAMIC_VPD(pci_pf);
122         }
123
124         if ((rc = ef10_nvram_partn_read_tlv(enp,
125                     NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG,
126                     tag, &dvpd, &dvpd_size)) != 0)
127                 goto fail1;
128
129         if (dvpd_size > size) {
130                 rc = ENOSPC;
131                 goto fail2;
132         }
133         if (dvpd != NULL)
134                 memcpy(data, dvpd, dvpd_size);
135
136         /* Pad data with all-1s, consistent with update operations */
137         memset(data + dvpd_size, 0xff, size - dvpd_size);
138
139         if (dvpd != NULL)
140                 EFSYS_KMEM_FREE(enp->en_esip, dvpd_size, dvpd);
141
142         return (0);
143
144 fail2:
145         EFSYS_PROBE(fail2);
146
147         if (dvpd != NULL)
148                 EFSYS_KMEM_FREE(enp->en_esip, dvpd_size, dvpd);
149 fail1:
150         EFSYS_PROBE1(fail1, efx_rc_t, rc);
151
152         return (rc);
153 }
154
155         __checkReturn           efx_rc_t
156 ef10_vpd_verify(
157         __in                    efx_nic_t *enp,
158         __in_bcount(size)       caddr_t data,
159         __in                    size_t size)
160 {
161         efx_vpd_tag_t stag;
162         efx_vpd_tag_t dtag;
163         efx_vpd_keyword_t skey;
164         efx_vpd_keyword_t dkey;
165         unsigned int scont;
166         unsigned int dcont;
167         efx_rc_t rc;
168
169         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
170
171         /*
172          * Strictly you could take the view that dynamic vpd is optional.
173          * Instead, to conform more closely to the read/verify/reinit()
174          * paradigm, we require dynamic vpd. ef10_vpd_reinit() will
175          * reinitialize it as required.
176          */
177         if ((rc = efx_vpd_hunk_verify(data, size, NULL)) != 0)
178                 goto fail1;
179
180         /*
181          * Verify that there is no duplication between the static and
182          * dynamic cfg sectors.
183          */
184         if (enp->en_arch.ef10.ena_svpd_length == 0)
185                 goto done;
186
187         dcont = 0;
188         _NOTE(CONSTANTCONDITION)
189         while (1) {
190                 if ((rc = efx_vpd_hunk_next(data, size, &dtag,
191                     &dkey, NULL, NULL, &dcont)) != 0)
192                         goto fail2;
193                 if (dcont == 0)
194                         break;
195
196                 /*
197                  * Skip the RV keyword. It should be present in both the static
198                  * and dynamic cfg sectors.
199                  */
200                 if (dtag == EFX_VPD_RO && dkey == EFX_VPD_KEYWORD('R', 'V'))
201                         continue;
202
203                 scont = 0;
204                 _NOTE(CONSTANTCONDITION)
205                 while (1) {
206                         if ((rc = efx_vpd_hunk_next(
207                             enp->en_arch.ef10.ena_svpd,
208                             enp->en_arch.ef10.ena_svpd_length, &stag, &skey,
209                             NULL, NULL, &scont)) != 0)
210                                 goto fail3;
211                         if (scont == 0)
212                                 break;
213
214                         if (stag == dtag && skey == dkey) {
215                                 rc = EEXIST;
216                                 goto fail4;
217                         }
218                 }
219         }
220
221 done:
222         return (0);
223
224 fail4:
225         EFSYS_PROBE(fail4);
226 fail3:
227         EFSYS_PROBE(fail3);
228 fail2:
229         EFSYS_PROBE(fail2);
230 fail1:
231         EFSYS_PROBE1(fail1, efx_rc_t, rc);
232
233         return (rc);
234 }
235
236         __checkReturn           efx_rc_t
237 ef10_vpd_reinit(
238         __in                    efx_nic_t *enp,
239         __in_bcount(size)       caddr_t data,
240         __in                    size_t size)
241 {
242         boolean_t wantpid;
243         efx_rc_t rc;
244
245         /*
246          * Only create an ID string if the dynamic cfg doesn't have one
247          */
248         if (enp->en_arch.ef10.ena_svpd_length == 0)
249                 wantpid = B_TRUE;
250         else {
251                 unsigned int offset;
252                 uint8_t length;
253
254                 rc = efx_vpd_hunk_get(enp->en_arch.ef10.ena_svpd,
255                                     enp->en_arch.ef10.ena_svpd_length,
256                                     EFX_VPD_ID, 0, &offset, &length);
257                 if (rc == 0)
258                         wantpid = B_FALSE;
259                 else if (rc == ENOENT)
260                         wantpid = B_TRUE;
261                 else
262                         goto fail1;
263         }
264
265         if ((rc = efx_vpd_hunk_reinit(data, size, wantpid)) != 0)
266                 goto fail2;
267
268         return (0);
269
270 fail2:
271         EFSYS_PROBE(fail2);
272 fail1:
273         EFSYS_PROBE1(fail1, efx_rc_t, rc);
274
275         return (rc);
276 }
277
278         __checkReturn           efx_rc_t
279 ef10_vpd_get(
280         __in                    efx_nic_t *enp,
281         __in_bcount(size)       caddr_t data,
282         __in                    size_t size,
283         __inout                 efx_vpd_value_t *evvp)
284 {
285         unsigned int offset;
286         uint8_t length;
287         efx_rc_t rc;
288
289         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
290
291         /* Attempt to satisfy the request from svpd first */
292         if (enp->en_arch.ef10.ena_svpd_length > 0) {
293                 if ((rc = efx_vpd_hunk_get(enp->en_arch.ef10.ena_svpd,
294                     enp->en_arch.ef10.ena_svpd_length, evvp->evv_tag,
295                     evvp->evv_keyword, &offset, &length)) == 0) {
296                         evvp->evv_length = length;
297                         memcpy(evvp->evv_value,
298                             enp->en_arch.ef10.ena_svpd + offset, length);
299                         return (0);
300                 } else if (rc != ENOENT)
301                         goto fail1;
302         }
303
304         /* And then from the provided data buffer */
305         if ((rc = efx_vpd_hunk_get(data, size, evvp->evv_tag,
306             evvp->evv_keyword, &offset, &length)) != 0) {
307                 if (rc == ENOENT)
308                         return (rc);
309                 goto fail2;
310         }
311
312         evvp->evv_length = length;
313         memcpy(evvp->evv_value, data + offset, length);
314
315         return (0);
316
317 fail2:
318         EFSYS_PROBE(fail2);
319 fail1:
320         EFSYS_PROBE1(fail1, efx_rc_t, rc);
321
322         return (rc);
323 }
324
325         __checkReturn           efx_rc_t
326 ef10_vpd_set(
327         __in                    efx_nic_t *enp,
328         __in_bcount(size)       caddr_t data,
329         __in                    size_t size,
330         __in                    efx_vpd_value_t *evvp)
331 {
332         efx_rc_t rc;
333
334         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
335
336         /* If the provided (tag,keyword) exists in svpd, then it is readonly */
337         if (enp->en_arch.ef10.ena_svpd_length > 0) {
338                 unsigned int offset;
339                 uint8_t length;
340
341                 if ((rc = efx_vpd_hunk_get(enp->en_arch.ef10.ena_svpd,
342                     enp->en_arch.ef10.ena_svpd_length, evvp->evv_tag,
343                     evvp->evv_keyword, &offset, &length)) == 0) {
344                         rc = EACCES;
345                         goto fail1;
346                 }
347         }
348
349         if ((rc = efx_vpd_hunk_set(data, size, evvp)) != 0)
350                 goto fail2;
351
352         return (0);
353
354 fail2:
355         EFSYS_PROBE(fail2);
356 fail1:
357         EFSYS_PROBE1(fail1, efx_rc_t, rc);
358
359         return (rc);
360 }
361
362         __checkReturn           efx_rc_t
363 ef10_vpd_next(
364         __in                    efx_nic_t *enp,
365         __in_bcount(size)       caddr_t data,
366         __in                    size_t size,
367         __out                   efx_vpd_value_t *evvp,
368         __inout                 unsigned int *contp)
369 {
370         _NOTE(ARGUNUSED(enp, data, size, evvp, contp))
371
372         return (ENOTSUP);
373 }
374
375         __checkReturn           efx_rc_t
376 ef10_vpd_write(
377         __in                    efx_nic_t *enp,
378         __in_bcount(size)       caddr_t data,
379         __in                    size_t size)
380 {
381         size_t vpd_length;
382         uint32_t pci_pf;
383         uint32_t tag;
384         efx_rc_t rc;
385
386         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
387
388         if (enp->en_nic_cfg.enc_vpd_is_global) {
389                 tag = TLV_TAG_GLOBAL_DYNAMIC_VPD;
390         } else {
391                 pci_pf = enp->en_nic_cfg.enc_pf;
392                 tag = TLV_TAG_PF_DYNAMIC_VPD(pci_pf);
393         }
394
395         /* Determine total length of new dynamic VPD */
396         if ((rc = efx_vpd_hunk_length(data, size, &vpd_length)) != 0)
397                 goto fail1;
398
399         /* Store new dynamic VPD in all segments in DYNAMIC_CONFIG partition */
400         if ((rc = ef10_nvram_partn_write_segment_tlv(enp,
401                     NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG,
402                     tag, data, vpd_length, B_TRUE)) != 0) {
403                 goto fail2;
404         }
405
406         return (0);
407
408 fail2:
409         EFSYS_PROBE(fail2);
410
411 fail1:
412         EFSYS_PROBE1(fail1, efx_rc_t, rc);
413
414         return (rc);
415 }
416
417                                 void
418 ef10_vpd_fini(
419         __in                    efx_nic_t *enp)
420 {
421         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
422
423         if (enp->en_arch.ef10.ena_svpd_length > 0) {
424                 EFSYS_KMEM_FREE(enp->en_esip, enp->en_arch.ef10.ena_svpd_length,
425                                 enp->en_arch.ef10.ena_svpd);
426
427                 enp->en_arch.ef10.ena_svpd = NULL;
428                 enp->en_arch.ef10.ena_svpd_length = 0;
429         }
430 }
431
432 #endif  /* EFX_OPTS_EF10() */
433
434 #endif  /* EFSYS_OPT_VPD */