enic: new driver
[dpdk.git] / lib / librte_pmd_enic / enic_ethdev.c
1 /*
2  * Copyright 2008-2014 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 #ident "$Id$"
35
36 #include <stdio.h>
37 #include <stdint.h>
38
39 #include <rte_dev.h>
40 #include <rte_pci.h>
41 #include <rte_ethdev.h>
42 #include <rte_string_fns.h>
43
44 #include "vnic_intr.h"
45 #include "vnic_cq.h"
46 #include "vnic_wq.h"
47 #include "vnic_rq.h"
48 #include "vnic_enet.h"
49 #include "enic.h"
50
51 #define ENICPMD_FUNC_TRACE() \
52         RTE_LOG(DEBUG, PMD, "ENICPMD trace: %s\n", __func__)
53
54 /*
55  * The set of PCI devices this driver supports
56  */
57 static struct rte_pci_id pci_id_enic_map[] = {
58 #define RTE_PCI_DEV_ID_DECL_ENIC(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
59 #ifndef PCI_VENDOR_ID_CISCO
60 #define PCI_VENDOR_ID_CISCO     0x1137
61 #endif
62 #include "rte_pci_dev_ids.h"
63 RTE_PCI_DEV_ID_DECL_ENIC(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET)
64 RTE_PCI_DEV_ID_DECL_ENIC(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF)
65 {.vendor_id = 0, /* Sentinal */},
66 };
67
68 static int enicpmd_fdir_remove_perfect_filter(struct rte_eth_dev *eth_dev,
69                 struct rte_fdir_filter *fdir_filter,
70                 uint16_t soft_id)
71 {
72         struct enic *enic = pmd_priv(eth_dev);
73
74         ENICPMD_FUNC_TRACE();
75         return enic_fdir_del_fltr(enic, fdir_filter);
76 }
77
78 static int enicpmd_fdir_add_perfect_filter(struct rte_eth_dev *eth_dev,
79         struct rte_fdir_filter *fdir_filter, uint16_t soft_id,
80         uint8_t queue, uint8_t drop)
81 {
82         struct enic *enic = pmd_priv(eth_dev);
83
84         ENICPMD_FUNC_TRACE();
85         return enic_fdir_add_fltr(enic, fdir_filter, (uint16_t)queue, drop);
86 }
87
88 static void enicpmd_fdir_info_get(struct rte_eth_dev *eth_dev,
89         struct rte_eth_fdir *fdir)
90 {
91         struct enic *enic = pmd_priv(eth_dev);
92
93         ENICPMD_FUNC_TRACE();
94         *fdir = enic->fdir.stats;
95 }
96
97 static void enicpmd_dev_tx_queue_release(void *txq)
98 {
99         ENICPMD_FUNC_TRACE();
100         enic_free_wq(txq);
101 }
102
103 static int enicpmd_dev_setup_intr(struct enic *enic)
104 {
105         int ret;
106         int index;
107
108         ENICPMD_FUNC_TRACE();
109
110         /* Are we done with the init of all the queues? */
111         for (index = 0; index < enic->cq_count; index++) {
112                 if (!enic->cq[index].ctrl)
113                         break;
114         }
115
116         if (enic->cq_count != index)
117                 return 0;
118
119         ret = enic_alloc_intr_resources(enic);
120         if (ret) {
121                 dev_err(enic, "alloc intr failed\n");
122                 return ret;
123         }
124         enic_init_vnic_resources(enic);
125
126         ret = enic_setup_finish(enic);
127         if (ret)
128                 dev_err(enic, "setup could not be finished\n");
129
130         return ret;
131 }
132
133 static int enicpmd_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
134         uint16_t queue_idx,
135         uint16_t nb_desc,
136         unsigned int socket_id,
137         const struct rte_eth_txconf *tx_conf)
138 {
139         int ret;
140         struct enic *enic = pmd_priv(eth_dev);
141
142         ENICPMD_FUNC_TRACE();
143         eth_dev->data->tx_queues[queue_idx] = (void *)&enic->wq[queue_idx];
144
145         ret = enic_alloc_wq(enic, queue_idx, socket_id, nb_desc);
146         if (ret) {
147                 dev_err(enic, "error in allocating wq\n");
148                 return ret;
149         }
150
151         return enicpmd_dev_setup_intr(enic);
152 }
153
154 static int enicpmd_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
155         uint16_t queue_idx)
156 {
157         struct enic *enic = pmd_priv(eth_dev);
158
159         ENICPMD_FUNC_TRACE();
160
161         enic_start_wq(enic, queue_idx);
162
163         return 0;
164 }
165
166 static int enicpmd_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
167         uint16_t queue_idx)
168 {
169         int ret;
170         struct enic *enic = pmd_priv(eth_dev);
171
172         ENICPMD_FUNC_TRACE();
173
174         ret = enic_stop_wq(enic, queue_idx);
175         if (ret)
176                 dev_err(enic, "error in stopping wq %d\n", queue_idx);
177
178         return ret;
179 }
180
181 static int enicpmd_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
182         uint16_t queue_idx)
183 {
184         struct enic *enic = pmd_priv(eth_dev);
185
186         ENICPMD_FUNC_TRACE();
187
188         enic_start_rq(enic, queue_idx);
189
190         return 0;
191 }
192
193 static int enicpmd_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
194         uint16_t queue_idx)
195 {
196         int ret;
197         struct enic *enic = pmd_priv(eth_dev);
198
199         ENICPMD_FUNC_TRACE();
200
201         ret = enic_stop_rq(enic, queue_idx);
202         if (ret)
203                 dev_err(enic, "error in stopping rq %d\n", queue_idx);
204
205         return ret;
206 }
207
208 static void enicpmd_dev_rx_queue_release(void *rxq)
209 {
210         ENICPMD_FUNC_TRACE();
211         enic_free_rq(rxq);
212 }
213
214 static int enicpmd_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
215         uint16_t queue_idx,
216         uint16_t nb_desc,
217         unsigned int socket_id,
218         const struct rte_eth_rxconf *rx_conf,
219         struct rte_mempool *mp)
220 {
221         int ret;
222         struct enic *enic = pmd_priv(eth_dev);
223
224         ENICPMD_FUNC_TRACE();
225         eth_dev->data->rx_queues[queue_idx] = (void *)&enic->rq[queue_idx];
226
227         ret = enic_alloc_rq(enic, queue_idx, socket_id, mp, nb_desc);
228         if (ret) {
229                 dev_err(enic, "error in allocating rq\n");
230                 return ret;
231         }
232
233         return enicpmd_dev_setup_intr(enic);
234 }
235
236 static int enicpmd_vlan_filter_set(struct rte_eth_dev *eth_dev,
237         uint16_t vlan_id, int on)
238 {
239         struct enic *enic = pmd_priv(eth_dev);
240
241         ENICPMD_FUNC_TRACE();
242         if (on)
243                 enic_add_vlan(enic, vlan_id);
244         else
245                 enic_del_vlan(enic, vlan_id);
246         return 0;
247 }
248
249 static void enicpmd_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
250 {
251         struct enic *enic = pmd_priv(eth_dev);
252
253         ENICPMD_FUNC_TRACE();
254
255         if (mask & ETH_VLAN_STRIP_MASK) {
256                 if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
257                         enic->ig_vlan_strip_en = 1;
258                 else
259                         enic->ig_vlan_strip_en = 0;
260         }
261         enic_set_rss_nic_cfg(enic);
262
263
264         if (mask & ETH_VLAN_FILTER_MASK) {
265                 dev_warning(enic,
266                         "Configuration of VLAN filter is not supported\n");
267         }
268
269         if (mask & ETH_VLAN_EXTEND_MASK) {
270                 dev_warning(enic,
271                         "Configuration of extended VLAN is not supported\n");
272         }
273 }
274
275 static int enicpmd_dev_configure(struct rte_eth_dev *eth_dev)
276 {
277         int ret;
278         struct enic *enic = pmd_priv(eth_dev);
279
280         ENICPMD_FUNC_TRACE();
281         ret = enic_set_vnic_res(enic);
282         if (ret) {
283                 dev_err(enic, "Set vNIC resource num  failed, aborting\n");
284                 return ret;
285         }
286
287         if (eth_dev->data->dev_conf.rxmode.split_hdr_size &&
288                 eth_dev->data->dev_conf.rxmode.header_split) {
289                 /* Enable header-data-split */
290                 enic_set_hdr_split_size(enic,
291                         eth_dev->data->dev_conf.rxmode.split_hdr_size);
292         }
293
294         enic->hw_ip_checksum = eth_dev->data->dev_conf.rxmode.hw_ip_checksum;
295         return 0;
296 }
297
298 /* Start the device.
299  * It returns 0 on success.
300  */
301 static int enicpmd_dev_start(struct rte_eth_dev *eth_dev)
302 {
303         struct enic *enic = pmd_priv(eth_dev);
304
305         ENICPMD_FUNC_TRACE();
306         return enic_enable(enic);
307 }
308
309 /*
310  * Stop device: disable rx and tx functions to allow for reconfiguring.
311  */
312 static void enicpmd_dev_stop(struct rte_eth_dev *eth_dev)
313 {
314         struct rte_eth_link link;
315         struct enic *enic = pmd_priv(eth_dev);
316
317         ENICPMD_FUNC_TRACE();
318         enic_disable(enic);
319         memset(&link, 0, sizeof(link));
320         rte_atomic64_cmpset((uint64_t *)&eth_dev->data->dev_link,
321                 *(uint64_t *)&eth_dev->data->dev_link,
322                 *(uint64_t *)&link);
323 }
324
325 /*
326  * Stop device.
327  */
328 static void enicpmd_dev_close(struct rte_eth_dev *eth_dev)
329 {
330         struct enic *enic = pmd_priv(eth_dev);
331
332         ENICPMD_FUNC_TRACE();
333         enic_remove(enic);
334 }
335
336 static int enicpmd_dev_link_update(struct rte_eth_dev *eth_dev,
337         int wait_to_complete)
338 {
339         struct enic *enic = pmd_priv(eth_dev);
340         int ret;
341         int link_status = 0;
342
343         ENICPMD_FUNC_TRACE();
344         link_status = enic_get_link_status(enic);
345         ret = (link_status == enic->link_status);
346         enic->link_status = link_status;
347         eth_dev->data->dev_link.link_status = link_status;
348         eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
349         eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
350         return ret;
351 }
352
353 static void enicpmd_dev_stats_get(struct rte_eth_dev *eth_dev,
354         struct rte_eth_stats *stats)
355 {
356         struct enic *enic = pmd_priv(eth_dev);
357
358         ENICPMD_FUNC_TRACE();
359         enic_dev_stats_get(enic, stats);
360 }
361
362 static void enicpmd_dev_stats_reset(struct rte_eth_dev *eth_dev)
363 {
364         struct enic *enic = pmd_priv(eth_dev);
365
366         ENICPMD_FUNC_TRACE();
367         enic_dev_stats_clear(enic);
368 }
369
370 static void enicpmd_dev_info_get(struct rte_eth_dev *eth_dev,
371         struct rte_eth_dev_info *device_info)
372 {
373         struct enic *enic = pmd_priv(eth_dev);
374
375         ENICPMD_FUNC_TRACE();
376         device_info->max_rx_queues = enic->rq_count;
377         device_info->max_tx_queues = enic->wq_count;
378         device_info->min_rx_bufsize = ENIC_MIN_MTU;
379         device_info->max_rx_pktlen = enic->config.mtu;
380         device_info->max_mac_addrs = 1;
381         device_info->rx_offload_capa =
382                 DEV_RX_OFFLOAD_VLAN_STRIP |
383                 DEV_RX_OFFLOAD_IPV4_CKSUM |
384                 DEV_RX_OFFLOAD_UDP_CKSUM  |
385                 DEV_RX_OFFLOAD_TCP_CKSUM;
386         device_info->tx_offload_capa =
387                 DEV_TX_OFFLOAD_VLAN_INSERT |
388                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
389                 DEV_TX_OFFLOAD_UDP_CKSUM   |
390                 DEV_TX_OFFLOAD_TCP_CKSUM;
391 }
392
393 static void enicpmd_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
394 {
395         struct enic *enic = pmd_priv(eth_dev);
396
397         ENICPMD_FUNC_TRACE();
398         enic->promisc = 1;
399         enic_add_packet_filter(enic);
400 }
401
402 static void enicpmd_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
403 {
404         struct enic *enic = pmd_priv(eth_dev);
405
406         ENICPMD_FUNC_TRACE();
407         enic->promisc = 0;
408         enic_add_packet_filter(enic);
409 }
410
411 static void enicpmd_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
412 {
413         struct enic *enic = pmd_priv(eth_dev);
414
415         ENICPMD_FUNC_TRACE();
416         enic->allmulti = 1;
417         enic_add_packet_filter(enic);
418 }
419
420 static void enicpmd_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
421 {
422         struct enic *enic = pmd_priv(eth_dev);
423
424         ENICPMD_FUNC_TRACE();
425         enic->allmulti = 0;
426         enic_add_packet_filter(enic);
427 }
428
429 static void enicpmd_add_mac_addr(struct rte_eth_dev *eth_dev,
430         struct ether_addr *mac_addr,
431         uint32_t index, uint32_t pool)
432 {
433         struct enic *enic = pmd_priv(eth_dev);
434
435         ENICPMD_FUNC_TRACE();
436         enic_set_mac_address(enic, mac_addr->addr_bytes);
437 }
438
439 static void enicpmd_remove_mac_addr(struct rte_eth_dev *eth_dev, uint32_t index)
440 {
441         struct enic *enic = pmd_priv(eth_dev);
442
443         ENICPMD_FUNC_TRACE();
444         enic_del_mac_address(enic);
445 }
446
447
448 static uint16_t enicpmd_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
449         uint16_t nb_pkts)
450 {
451         unsigned int index;
452         unsigned int frags;
453         unsigned int pkt_len;
454         unsigned int seg_len;
455         unsigned int inc_len;
456         unsigned int nb_segs;
457         struct rte_mbuf *tx_pkt;
458         struct vnic_wq *wq = (struct vnic_wq *)tx_queue;
459         struct enic *enic = vnic_dev_priv(wq->vdev);
460         unsigned char *buf;
461         unsigned short vlan_id;
462         unsigned short ol_flags;
463
464         for (index = 0; index < nb_pkts; index++) {
465                 tx_pkt = *tx_pkts++;
466                 inc_len = 0;
467                 nb_segs = tx_pkt->nb_segs;
468                 if (nb_segs > vnic_wq_desc_avail(wq)) {
469                         /* wq cleanup and try again */
470                         if (!enic_cleanup_wq(enic, wq) ||
471                                 (nb_segs > vnic_wq_desc_avail(wq)))
472                                 return index;
473                 }
474                 pkt_len = tx_pkt->pkt_len;
475                 vlan_id = tx_pkt->vlan_tci;
476                 ol_flags = tx_pkt->ol_flags;
477                 for (frags = 0; inc_len < pkt_len; frags++) {
478                         if (!tx_pkt)
479                                 break;
480                         seg_len = tx_pkt->data_len;
481                         inc_len += seg_len;
482                         if (enic_send_pkt(enic, wq, tx_pkt,
483                                     (unsigned short)seg_len, !frags,
484                                     (pkt_len == inc_len), ol_flags, vlan_id)) {
485                                 break;
486                         }
487                         tx_pkt = tx_pkt->next;
488                 }
489         }
490
491         enic_cleanup_wq(enic, wq);
492         return index;
493 }
494
495 static uint16_t enicpmd_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
496         uint16_t nb_pkts)
497 {
498         struct vnic_rq *rq = (struct vnic_rq *)rx_queue;
499         unsigned int work_done;
500
501         if (enic_poll(rq, rx_pkts, (unsigned int)nb_pkts, &work_done))
502                 dev_err(enic, "error in enicpmd poll\n");
503
504         return work_done;
505 }
506
507 static struct eth_dev_ops enicpmd_eth_dev_ops = {
508         .dev_configure        = enicpmd_dev_configure,
509         .dev_start            = enicpmd_dev_start,
510         .dev_stop             = enicpmd_dev_stop,
511         .dev_set_link_up      = NULL,
512         .dev_set_link_down    = NULL,
513         .dev_close            = enicpmd_dev_close,
514         .promiscuous_enable   = enicpmd_dev_promiscuous_enable,
515         .promiscuous_disable  = enicpmd_dev_promiscuous_disable,
516         .allmulticast_enable  = enicpmd_dev_allmulticast_enable,
517         .allmulticast_disable = enicpmd_dev_allmulticast_disable,
518         .link_update          = enicpmd_dev_link_update,
519         .stats_get            = enicpmd_dev_stats_get,
520         .stats_reset          = enicpmd_dev_stats_reset,
521         .queue_stats_mapping_set = NULL,
522         .dev_infos_get        = enicpmd_dev_info_get,
523         .mtu_set              = NULL,
524         .vlan_filter_set      = enicpmd_vlan_filter_set,
525         .vlan_tpid_set        = NULL,
526         .vlan_offload_set     = enicpmd_vlan_offload_set,
527         .vlan_strip_queue_set = NULL,
528         .rx_queue_start       = enicpmd_dev_rx_queue_start,
529         .rx_queue_stop        = enicpmd_dev_rx_queue_stop,
530         .tx_queue_start       = enicpmd_dev_tx_queue_start,
531         .tx_queue_stop        = enicpmd_dev_tx_queue_stop,
532         .rx_queue_setup       = enicpmd_dev_rx_queue_setup,
533         .rx_queue_release     = enicpmd_dev_rx_queue_release,
534         .rx_queue_count       = NULL,
535         .rx_descriptor_done   = NULL,
536         .tx_queue_setup       = enicpmd_dev_tx_queue_setup,
537         .tx_queue_release     = enicpmd_dev_tx_queue_release,
538         .dev_led_on           = NULL,
539         .dev_led_off          = NULL,
540         .flow_ctrl_get        = NULL,
541         .flow_ctrl_set        = NULL,
542         .priority_flow_ctrl_set = NULL,
543         .mac_addr_add         = enicpmd_add_mac_addr,
544         .mac_addr_remove      = enicpmd_remove_mac_addr,
545         .fdir_add_signature_filter    = NULL,
546         .fdir_update_signature_filter = NULL,
547         .fdir_remove_signature_filter = NULL,
548         .fdir_infos_get               = enicpmd_fdir_info_get,
549         .fdir_add_perfect_filter      = enicpmd_fdir_add_perfect_filter,
550         .fdir_update_perfect_filter   = enicpmd_fdir_add_perfect_filter,
551         .fdir_remove_perfect_filter   = enicpmd_fdir_remove_perfect_filter,
552         .fdir_set_masks               = NULL,
553 };
554
555 struct enic *enicpmd_list_head = NULL;
556 /* Initialize the driver
557  * It returns 0 on success.
558  */
559 static int eth_enicpmd_dev_init(
560         __attribute__((unused))struct eth_driver *eth_drv,
561         struct rte_eth_dev *eth_dev)
562 {
563         struct rte_pci_device *pdev;
564         struct rte_pci_addr *addr;
565         struct enic *enic = pmd_priv(eth_dev);
566
567         ENICPMD_FUNC_TRACE();
568
569         enic->rte_dev = eth_dev;
570         eth_dev->dev_ops = &enicpmd_eth_dev_ops;
571         eth_dev->rx_pkt_burst = &enicpmd_recv_pkts;
572         eth_dev->tx_pkt_burst = &enicpmd_xmit_pkts;
573
574         pdev = eth_dev->pci_dev;
575         enic->pdev = pdev;
576         addr = &pdev->addr;
577
578         snprintf(enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
579                 addr->domain, addr->bus, addr->devid, addr->function);
580
581         return enic_probe(enic);
582 }
583
584 static struct eth_driver rte_enic_pmd = {
585         {
586                 .name = "rte_enic_pmd",
587                 .id_table = pci_id_enic_map,
588                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
589         },
590         .eth_dev_init = eth_enicpmd_dev_init,
591         .dev_private_size = sizeof(struct enic),
592 };
593
594 /* Driver initialization routine.
595  * Invoked once at EAL init time.
596  * Register as the [Poll Mode] Driver of Cisco ENIC device.
597  */
598 int rte_enic_pmd_init(const char *name __rte_unused,
599         const char *params __rte_unused)
600 {
601         ENICPMD_FUNC_TRACE();
602
603         rte_eth_driver_register(&rte_enic_pmd);
604         return 0;
605 }
606
607 static struct rte_driver rte_enic_driver = {
608         .type = PMD_PDEV,
609         .init = rte_enic_pmd_init,
610 };
611
612 PMD_REGISTER_DRIVER(rte_enic_driver);