net/sfc: set up and release Tx queues
[dpdk.git] / drivers / net / sfc / sfc_ethdev.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 <rte_dev.h>
31 #include <rte_ethdev.h>
32 #include <rte_pci.h>
33
34 #include "efx.h"
35
36 #include "sfc.h"
37 #include "sfc_debug.h"
38 #include "sfc_log.h"
39 #include "sfc_kvargs.h"
40 #include "sfc_ev.h"
41 #include "sfc_rx.h"
42 #include "sfc_tx.h"
43
44
45 static void
46 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
47 {
48         struct sfc_adapter *sa = dev->data->dev_private;
49
50         sfc_log_init(sa, "entry");
51
52         dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
53         dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
54
55         dev_info->max_rx_queues = sa->rxq_max;
56         dev_info->max_tx_queues = sa->txq_max;
57
58         /* By default packets are dropped if no descriptors are available */
59         dev_info->default_rxconf.rx_drop_en = 1;
60
61         dev_info->tx_offload_capa =
62                 DEV_TX_OFFLOAD_IPV4_CKSUM |
63                 DEV_TX_OFFLOAD_UDP_CKSUM |
64                 DEV_TX_OFFLOAD_TCP_CKSUM;
65
66         dev_info->default_txconf.txq_flags = ETH_TXQ_FLAGS_NOVLANOFFL |
67                                              ETH_TXQ_FLAGS_NOXSUMSCTP;
68
69         dev_info->rx_desc_lim.nb_max = EFX_RXQ_MAXNDESCS;
70         dev_info->rx_desc_lim.nb_min = EFX_RXQ_MINNDESCS;
71         /* The RXQ hardware requires that the descriptor count is a power
72          * of 2, but rx_desc_lim cannot properly describe that constraint.
73          */
74         dev_info->rx_desc_lim.nb_align = EFX_RXQ_MINNDESCS;
75
76         dev_info->tx_desc_lim.nb_max = sa->txq_max_entries;
77         dev_info->tx_desc_lim.nb_min = EFX_TXQ_MINNDESCS;
78         /*
79          * The TXQ hardware requires that the descriptor count is a power
80          * of 2, but tx_desc_lim cannot properly describe that constraint
81          */
82         dev_info->tx_desc_lim.nb_align = EFX_TXQ_MINNDESCS;
83 }
84
85 static int
86 sfc_dev_configure(struct rte_eth_dev *dev)
87 {
88         struct rte_eth_dev_data *dev_data = dev->data;
89         struct sfc_adapter *sa = dev_data->dev_private;
90         int rc;
91
92         sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
93                      dev_data->nb_rx_queues, dev_data->nb_tx_queues);
94
95         sfc_adapter_lock(sa);
96         switch (sa->state) {
97         case SFC_ADAPTER_CONFIGURED:
98                 sfc_close(sa);
99                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
100                 /* FALLTHROUGH */
101         case SFC_ADAPTER_INITIALIZED:
102                 rc = sfc_configure(sa);
103                 break;
104         default:
105                 sfc_err(sa, "unexpected adapter state %u to configure",
106                         sa->state);
107                 rc = EINVAL;
108                 break;
109         }
110         sfc_adapter_unlock(sa);
111
112         sfc_log_init(sa, "done %d", rc);
113         SFC_ASSERT(rc >= 0);
114         return -rc;
115 }
116
117 static int
118 sfc_dev_start(struct rte_eth_dev *dev)
119 {
120         struct sfc_adapter *sa = dev->data->dev_private;
121         int rc;
122
123         sfc_log_init(sa, "entry");
124
125         sfc_adapter_lock(sa);
126         rc = sfc_start(sa);
127         sfc_adapter_unlock(sa);
128
129         sfc_log_init(sa, "done %d", rc);
130         SFC_ASSERT(rc >= 0);
131         return -rc;
132 }
133
134 static int
135 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
136 {
137         struct sfc_adapter *sa = dev->data->dev_private;
138         struct rte_eth_link *dev_link = &dev->data->dev_link;
139         struct rte_eth_link old_link;
140         struct rte_eth_link current_link;
141
142         sfc_log_init(sa, "entry");
143
144         if (sa->state != SFC_ADAPTER_STARTED)
145                 return 0;
146
147 retry:
148         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
149         *(int64_t *)&old_link = rte_atomic64_read((rte_atomic64_t *)dev_link);
150
151         if (wait_to_complete) {
152                 efx_link_mode_t link_mode;
153
154                 efx_port_poll(sa->nic, &link_mode);
155                 sfc_port_link_mode_to_info(link_mode, &current_link);
156
157                 if (!rte_atomic64_cmpset((volatile uint64_t *)dev_link,
158                                          *(uint64_t *)&old_link,
159                                          *(uint64_t *)&current_link))
160                         goto retry;
161         } else {
162                 sfc_ev_mgmt_qpoll(sa);
163                 *(int64_t *)&current_link =
164                         rte_atomic64_read((rte_atomic64_t *)dev_link);
165         }
166
167         if (old_link.link_status != current_link.link_status)
168                 sfc_info(sa, "Link status is %s",
169                          current_link.link_status ? "UP" : "DOWN");
170
171         return old_link.link_status == current_link.link_status ? 0 : -1;
172 }
173
174 static void
175 sfc_dev_stop(struct rte_eth_dev *dev)
176 {
177         struct sfc_adapter *sa = dev->data->dev_private;
178
179         sfc_log_init(sa, "entry");
180
181         sfc_adapter_lock(sa);
182         sfc_stop(sa);
183         sfc_adapter_unlock(sa);
184
185         sfc_log_init(sa, "done");
186 }
187
188 static void
189 sfc_dev_close(struct rte_eth_dev *dev)
190 {
191         struct sfc_adapter *sa = dev->data->dev_private;
192
193         sfc_log_init(sa, "entry");
194
195         sfc_adapter_lock(sa);
196         switch (sa->state) {
197         case SFC_ADAPTER_STARTED:
198                 sfc_stop(sa);
199                 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
200                 /* FALLTHROUGH */
201         case SFC_ADAPTER_CONFIGURED:
202                 sfc_close(sa);
203                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
204                 /* FALLTHROUGH */
205         case SFC_ADAPTER_INITIALIZED:
206                 break;
207         default:
208                 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
209                 break;
210         }
211         sfc_adapter_unlock(sa);
212
213         sfc_log_init(sa, "done");
214 }
215
216 static int
217 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
218                    uint16_t nb_rx_desc, unsigned int socket_id,
219                    const struct rte_eth_rxconf *rx_conf,
220                    struct rte_mempool *mb_pool)
221 {
222         struct sfc_adapter *sa = dev->data->dev_private;
223         int rc;
224
225         sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u",
226                      rx_queue_id, nb_rx_desc, socket_id);
227
228         sfc_adapter_lock(sa);
229
230         rc = sfc_rx_qinit(sa, rx_queue_id, nb_rx_desc, socket_id,
231                           rx_conf, mb_pool);
232         if (rc != 0)
233                 goto fail_rx_qinit;
234
235         dev->data->rx_queues[rx_queue_id] = sa->rxq_info[rx_queue_id].rxq;
236
237         sfc_adapter_unlock(sa);
238
239         return 0;
240
241 fail_rx_qinit:
242         sfc_adapter_unlock(sa);
243         SFC_ASSERT(rc > 0);
244         return -rc;
245 }
246
247 static void
248 sfc_rx_queue_release(void *queue)
249 {
250         struct sfc_rxq *rxq = queue;
251         struct sfc_adapter *sa;
252         unsigned int sw_index;
253
254         if (rxq == NULL)
255                 return;
256
257         sa = rxq->evq->sa;
258         sfc_adapter_lock(sa);
259
260         sw_index = sfc_rxq_sw_index(rxq);
261
262         sfc_log_init(sa, "RxQ=%u", sw_index);
263
264         sa->eth_dev->data->rx_queues[sw_index] = NULL;
265
266         sfc_rx_qfini(sa, sw_index);
267
268         sfc_adapter_unlock(sa);
269 }
270
271 static int
272 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
273                    uint16_t nb_tx_desc, unsigned int socket_id,
274                    const struct rte_eth_txconf *tx_conf)
275 {
276         struct sfc_adapter *sa = dev->data->dev_private;
277         int rc;
278
279         sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u",
280                      tx_queue_id, nb_tx_desc, socket_id);
281
282         sfc_adapter_lock(sa);
283
284         rc = sfc_tx_qinit(sa, tx_queue_id, nb_tx_desc, socket_id, tx_conf);
285         if (rc != 0)
286                 goto fail_tx_qinit;
287
288         dev->data->tx_queues[tx_queue_id] = sa->txq_info[tx_queue_id].txq;
289
290         sfc_adapter_unlock(sa);
291         return 0;
292
293 fail_tx_qinit:
294         sfc_adapter_unlock(sa);
295         SFC_ASSERT(rc > 0);
296         return -rc;
297 }
298
299 static void
300 sfc_tx_queue_release(void *queue)
301 {
302         struct sfc_txq *txq = queue;
303         unsigned int sw_index;
304         struct sfc_adapter *sa;
305
306         if (txq == NULL)
307                 return;
308
309         sw_index = sfc_txq_sw_index(txq);
310
311         SFC_ASSERT(txq->evq != NULL);
312         sa = txq->evq->sa;
313
314         sfc_log_init(sa, "TxQ = %u", sw_index);
315
316         sfc_adapter_lock(sa);
317
318         SFC_ASSERT(sw_index < sa->eth_dev->data->nb_tx_queues);
319         sa->eth_dev->data->tx_queues[sw_index] = NULL;
320
321         sfc_tx_qfini(sa, sw_index);
322
323         sfc_adapter_unlock(sa);
324 }
325
326 static const struct eth_dev_ops sfc_eth_dev_ops = {
327         .dev_configure                  = sfc_dev_configure,
328         .dev_start                      = sfc_dev_start,
329         .dev_stop                       = sfc_dev_stop,
330         .dev_close                      = sfc_dev_close,
331         .link_update                    = sfc_dev_link_update,
332         .dev_infos_get                  = sfc_dev_infos_get,
333         .rx_queue_setup                 = sfc_rx_queue_setup,
334         .rx_queue_release               = sfc_rx_queue_release,
335         .tx_queue_setup                 = sfc_tx_queue_setup,
336         .tx_queue_release               = sfc_tx_queue_release,
337 };
338
339 static int
340 sfc_eth_dev_init(struct rte_eth_dev *dev)
341 {
342         struct sfc_adapter *sa = dev->data->dev_private;
343         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
344         int rc;
345         const efx_nic_cfg_t *encp;
346         const struct ether_addr *from;
347
348         /* Required for logging */
349         sa->eth_dev = dev;
350
351         /* Copy PCI device info to the dev->data */
352         rte_eth_copy_pci_info(dev, pci_dev);
353
354         rc = sfc_kvargs_parse(sa);
355         if (rc != 0)
356                 goto fail_kvargs_parse;
357
358         rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
359                                 sfc_kvarg_bool_handler, &sa->debug_init);
360         if (rc != 0)
361                 goto fail_kvarg_debug_init;
362
363         sfc_log_init(sa, "entry");
364
365         dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
366         if (dev->data->mac_addrs == NULL) {
367                 rc = ENOMEM;
368                 goto fail_mac_addrs;
369         }
370
371         sfc_adapter_lock_init(sa);
372         sfc_adapter_lock(sa);
373
374         sfc_log_init(sa, "attaching");
375         rc = sfc_attach(sa);
376         if (rc != 0)
377                 goto fail_attach;
378
379         encp = efx_nic_cfg_get(sa->nic);
380
381         /*
382          * The arguments are really reverse order in comparison to
383          * Linux kernel. Copy from NIC config to Ethernet device data.
384          */
385         from = (const struct ether_addr *)(encp->enc_mac_addr);
386         ether_addr_copy(from, &dev->data->mac_addrs[0]);
387
388         dev->dev_ops = &sfc_eth_dev_ops;
389         dev->rx_pkt_burst = &sfc_recv_pkts;
390
391         sfc_adapter_unlock(sa);
392
393         sfc_log_init(sa, "done");
394         return 0;
395
396 fail_attach:
397         sfc_adapter_unlock(sa);
398         sfc_adapter_lock_fini(sa);
399         rte_free(dev->data->mac_addrs);
400         dev->data->mac_addrs = NULL;
401
402 fail_mac_addrs:
403 fail_kvarg_debug_init:
404         sfc_kvargs_cleanup(sa);
405
406 fail_kvargs_parse:
407         sfc_log_init(sa, "failed %d", rc);
408         SFC_ASSERT(rc > 0);
409         return -rc;
410 }
411
412 static int
413 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
414 {
415         struct sfc_adapter *sa = dev->data->dev_private;
416
417         sfc_log_init(sa, "entry");
418
419         sfc_adapter_lock(sa);
420
421         sfc_detach(sa);
422
423         rte_free(dev->data->mac_addrs);
424         dev->data->mac_addrs = NULL;
425
426         dev->dev_ops = NULL;
427         dev->rx_pkt_burst = NULL;
428
429         sfc_kvargs_cleanup(sa);
430
431         sfc_adapter_unlock(sa);
432         sfc_adapter_lock_fini(sa);
433
434         sfc_log_init(sa, "done");
435
436         /* Required for logging, so cleanup last */
437         sa->eth_dev = NULL;
438         return 0;
439 }
440
441 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
442         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
443         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
444         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
445         { .vendor_id = 0 /* sentinel */ }
446 };
447
448 static struct eth_driver sfc_efx_pmd = {
449         .pci_drv = {
450                 .id_table = pci_id_sfc_efx_map,
451                 .drv_flags =
452                         RTE_PCI_DRV_NEED_MAPPING,
453                 .probe = rte_eth_dev_pci_probe,
454                 .remove = rte_eth_dev_pci_remove,
455         },
456         .eth_dev_init = sfc_eth_dev_init,
457         .eth_dev_uninit = sfc_eth_dev_uninit,
458         .dev_private_size = sizeof(struct sfc_adapter),
459 };
460
461 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
462 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
463 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
464         SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);