net/sfc: set up and release Tx queues
[dpdk.git] / drivers / net / sfc / sfc_tx.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "sfc.h"
31 #include "sfc_log.h"
32 #include "sfc_ev.h"
33 #include "sfc_tx.h"
34
35 static int
36 sfc_tx_qcheck_conf(struct sfc_adapter *sa,
37                    const struct rte_eth_txconf *tx_conf)
38 {
39         unsigned int flags = tx_conf->txq_flags;
40         int rc = 0;
41
42         if (tx_conf->tx_rs_thresh != 0) {
43                 sfc_err(sa, "RS bit in transmit descriptor is not supported");
44                 rc = EINVAL;
45         }
46
47         if (tx_conf->tx_free_thresh != 0) {
48                 sfc_err(sa,
49                         "setting explicit TX free threshold is not supported");
50                 rc = EINVAL;
51         }
52
53         if (tx_conf->tx_deferred_start != 0) {
54                 sfc_err(sa, "TX queue deferred start is not supported (yet)");
55                 rc = EINVAL;
56         }
57
58         if (tx_conf->tx_thresh.pthresh != 0 ||
59             tx_conf->tx_thresh.hthresh != 0 ||
60             tx_conf->tx_thresh.wthresh != 0) {
61                 sfc_err(sa,
62                         "prefetch/host/writeback thresholds are not supported");
63                 rc = EINVAL;
64         }
65
66         if ((flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
67                 sfc_err(sa, "VLAN offload is not supported");
68                 rc = EINVAL;
69         }
70
71         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
72                 sfc_err(sa, "SCTP offload is not supported");
73                 rc = EINVAL;
74         }
75
76         /* We either perform both TCP and UDP offload, or no offload at all */
77         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
78             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
79                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
80                 rc = EINVAL;
81         }
82
83         return rc;
84 }
85
86 int
87 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
88              uint16_t nb_tx_desc, unsigned int socket_id,
89              const struct rte_eth_txconf *tx_conf)
90 {
91         struct sfc_txq_info *txq_info;
92         struct sfc_evq *evq;
93         struct sfc_txq *txq;
94         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
95         int rc = 0;
96
97         sfc_log_init(sa, "TxQ = %u", sw_index);
98
99         rc = sfc_tx_qcheck_conf(sa, tx_conf);
100         if (rc != 0)
101                 goto fail_bad_conf;
102
103         SFC_ASSERT(sw_index < sa->txq_count);
104         txq_info = &sa->txq_info[sw_index];
105
106         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
107         txq_info->entries = nb_tx_desc;
108
109         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
110         if (rc != 0)
111                 goto fail_ev_qinit;
112
113         evq = sa->evq_info[evq_index].evq;
114
115         rc = ENOMEM;
116         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
117         if (txq == NULL)
118                 goto fail_txq_alloc;
119
120         rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
121                            socket_id, &txq->mem);
122         if (rc != 0)
123                 goto fail_dma_alloc;
124
125         rc = ENOMEM;
126         txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
127                                            EFX_TXQ_LIMIT(txq_info->entries),
128                                            sizeof(efx_desc_t), 0, socket_id);
129         if (txq->pend_desc == NULL)
130                 goto fail_pend_desc_alloc;
131
132         rc = ENOMEM;
133         txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
134                                          sizeof(*txq->sw_ring), 0, socket_id);
135         if (txq->sw_ring == NULL)
136                 goto fail_desc_alloc;
137
138         txq->state = SFC_TXQ_INITIALIZED;
139         txq->ptr_mask = txq_info->entries - 1;
140         txq->hw_index = sw_index;
141         txq->flags = tx_conf->txq_flags;
142         txq->evq = evq;
143
144         evq->txq = txq;
145
146         txq_info->txq = txq;
147
148         return 0;
149
150 fail_desc_alloc:
151         rte_free(txq->pend_desc);
152
153 fail_pend_desc_alloc:
154         sfc_dma_free(sa, &txq->mem);
155
156 fail_dma_alloc:
157         rte_free(txq);
158
159 fail_txq_alloc:
160         sfc_ev_qfini(sa, evq_index);
161
162 fail_ev_qinit:
163         txq_info->entries = 0;
164
165 fail_bad_conf:
166         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
167         return rc;
168 }
169
170 void
171 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
172 {
173         struct sfc_txq_info *txq_info;
174         struct sfc_txq *txq;
175
176         sfc_log_init(sa, "TxQ = %u", sw_index);
177
178         SFC_ASSERT(sw_index < sa->txq_count);
179         txq_info = &sa->txq_info[sw_index];
180
181         txq = txq_info->txq;
182         SFC_ASSERT(txq != NULL);
183         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
184
185         txq_info->txq = NULL;
186         txq_info->entries = 0;
187
188         rte_free(txq->sw_ring);
189         rte_free(txq->pend_desc);
190         sfc_dma_free(sa, &txq->mem);
191         rte_free(txq);
192 }
193
194 static int
195 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
196 {
197         sfc_log_init(sa, "TxQ = %u", sw_index);
198
199         return 0;
200 }
201
202 static int
203 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
204 {
205         int rc = 0;
206
207         switch (txmode->mq_mode) {
208         case ETH_MQ_TX_NONE:
209                 break;
210         default:
211                 sfc_err(sa, "Tx multi-queue mode %u not supported",
212                         txmode->mq_mode);
213                 rc = EINVAL;
214         }
215
216         /*
217          * These features are claimed to be i40e-specific,
218          * but it does make sense to double-check their absence
219          */
220         if (txmode->hw_vlan_reject_tagged) {
221                 sfc_err(sa, "Rejecting tagged packets not supported");
222                 rc = EINVAL;
223         }
224
225         if (txmode->hw_vlan_reject_untagged) {
226                 sfc_err(sa, "Rejecting untagged packets not supported");
227                 rc = EINVAL;
228         }
229
230         if (txmode->hw_vlan_insert_pvid) {
231                 sfc_err(sa, "Port-based VLAN insertion not supported");
232                 rc = EINVAL;
233         }
234
235         return rc;
236 }
237
238 int
239 sfc_tx_init(struct sfc_adapter *sa)
240 {
241         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
242         unsigned int sw_index;
243         int rc = 0;
244
245         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
246         if (rc != 0)
247                 goto fail_check_mode;
248
249         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
250
251         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
252                                          sizeof(sa->txq_info[0]), 0,
253                                          sa->socket_id);
254         if (sa->txq_info == NULL)
255                 goto fail_txqs_alloc;
256
257         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
258                 rc = sfc_tx_qinit_info(sa, sw_index);
259                 if (rc != 0)
260                         goto fail_tx_qinit_info;
261         }
262
263         return 0;
264
265 fail_tx_qinit_info:
266         rte_free(sa->txq_info);
267         sa->txq_info = NULL;
268
269 fail_txqs_alloc:
270         sa->txq_count = 0;
271
272 fail_check_mode:
273         sfc_log_init(sa, "failed (rc = %d)", rc);
274         return rc;
275 }
276
277 void
278 sfc_tx_fini(struct sfc_adapter *sa)
279 {
280         int sw_index;
281
282         sw_index = sa->txq_count;
283         while (--sw_index >= 0) {
284                 if (sa->txq_info[sw_index].txq != NULL)
285                         sfc_tx_qfini(sa, sw_index);
286         }
287
288         rte_free(sa->txq_info);
289         sa->txq_info = NULL;
290         sa->txq_count = 0;
291 }