0f163765fe58c46fb2ec9527852e3e2a6ad48343
[dpdk.git] / drivers / net / sfc / base / efx_sram.c
1 /*
2  * Copyright (c) 2007-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34         __checkReturn   efx_rc_t
35 efx_sram_buf_tbl_set(
36         __in            efx_nic_t *enp,
37         __in            uint32_t id,
38         __in            efsys_mem_t *esmp,
39         __in            size_t n)
40 {
41         efx_qword_t qword;
42         uint32_t start = id;
43         uint32_t stop = start + n;
44         efsys_dma_addr_t addr;
45         efx_oword_t oword;
46         unsigned int count;
47         efx_rc_t rc;
48
49         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
50         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
51
52         if (stop >= EFX_BUF_TBL_SIZE) {
53                 rc = EFBIG;
54                 goto fail1;
55         }
56
57         /* Add the entries into the buffer table */
58         addr = EFSYS_MEM_ADDR(esmp);
59         for (id = start; id != stop; id++) {
60                 EFX_POPULATE_QWORD_5(qword,
61                     FRF_AZ_IP_DAT_BUF_SIZE, 0, FRF_AZ_BUF_ADR_REGION, 0,
62                     FRF_AZ_BUF_ADR_FBUF_DW0,
63                     (uint32_t)((addr >> 12) & 0xffffffff),
64                     FRF_AZ_BUF_ADR_FBUF_DW1,
65                     (uint32_t)((addr >> 12) >> 32),
66                     FRF_AZ_BUF_OWNER_ID_FBUF, 0);
67
68                 EFX_BAR_TBL_WRITEQ(enp, FR_AZ_BUF_FULL_TBL,
69                                     id, &qword);
70
71                 addr += EFX_BUF_SIZE;
72         }
73
74         EFSYS_PROBE2(buf, uint32_t, start, uint32_t, stop - 1);
75
76         /* Flush the write buffer */
77         EFX_POPULATE_OWORD_2(oword, FRF_AZ_BUF_UPD_CMD, 1,
78             FRF_AZ_BUF_CLR_CMD, 0);
79         EFX_BAR_WRITEO(enp, FR_AZ_BUF_TBL_UPD_REG, &oword);
80
81         /* Poll for the last entry being written to the buffer table */
82         EFSYS_ASSERT3U(id, ==, stop);
83         addr -= EFX_BUF_SIZE;
84
85         count = 0;
86         do {
87                 EFSYS_PROBE1(wait, unsigned int, count);
88
89                 /* Spin for 1 ms */
90                 EFSYS_SPIN(1000);
91
92                 EFX_BAR_TBL_READQ(enp, FR_AZ_BUF_FULL_TBL,
93                                     id - 1, &qword);
94
95                 if (EFX_QWORD_FIELD(qword, FRF_AZ_BUF_ADR_FBUF_DW0) ==
96                     (uint32_t)((addr >> 12) & 0xffffffff) &&
97                     EFX_QWORD_FIELD(qword, FRF_AZ_BUF_ADR_FBUF_DW1) ==
98                     (uint32_t)((addr >> 12) >> 32))
99                         goto verify;
100
101         } while (++count < 100);
102
103         rc = ETIMEDOUT;
104         goto fail2;
105
106 verify:
107         /* Verify the rest of the entries in the buffer table */
108         while (--id != start) {
109                 addr -= EFX_BUF_SIZE;
110
111                 /* Read the buffer table entry */
112                 EFX_BAR_TBL_READQ(enp, FR_AZ_BUF_FULL_TBL,
113                                     id - 1, &qword);
114
115                 if (EFX_QWORD_FIELD(qword, FRF_AZ_BUF_ADR_FBUF_DW0) !=
116                     (uint32_t)((addr >> 12) & 0xffffffff) ||
117                     EFX_QWORD_FIELD(qword, FRF_AZ_BUF_ADR_FBUF_DW1) !=
118                     (uint32_t)((addr >> 12) >> 32)) {
119                         rc = EFAULT;
120                         goto fail3;
121                 }
122         }
123
124         return (0);
125
126 fail3:
127         EFSYS_PROBE(fail3);
128
129         id = stop;
130
131 fail2:
132         EFSYS_PROBE(fail2);
133
134         EFX_POPULATE_OWORD_4(oword, FRF_AZ_BUF_UPD_CMD, 0,
135             FRF_AZ_BUF_CLR_CMD, 1, FRF_AZ_BUF_CLR_END_ID, id - 1,
136             FRF_AZ_BUF_CLR_START_ID, start);
137         EFX_BAR_WRITEO(enp, FR_AZ_BUF_TBL_UPD_REG, &oword);
138
139 fail1:
140         EFSYS_PROBE1(fail1, efx_rc_t, rc);
141
142         return (rc);
143 }
144
145                 void
146 efx_sram_buf_tbl_clear(
147         __in    efx_nic_t *enp,
148         __in    uint32_t id,
149         __in    size_t n)
150 {
151         efx_oword_t oword;
152         uint32_t start = id;
153         uint32_t stop = start + n;
154
155         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
156         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
157
158         EFSYS_ASSERT3U(stop, <, EFX_BUF_TBL_SIZE);
159
160         EFSYS_PROBE2(buf, uint32_t, start, uint32_t, stop - 1);
161
162         EFX_POPULATE_OWORD_4(oword, FRF_AZ_BUF_UPD_CMD, 0,
163             FRF_AZ_BUF_CLR_CMD, 1, FRF_AZ_BUF_CLR_END_ID, stop - 1,
164             FRF_AZ_BUF_CLR_START_ID, start);
165         EFX_BAR_WRITEO(enp, FR_AZ_BUF_TBL_UPD_REG, &oword);
166 }
167
168