4b857bb19bff8dbcba39a1bc964b6960f0b53200
[dpdk.git] / lib / librte_pmd_enic / enic_main.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
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <fcntl.h>
41 #include <libgen.h>
42 #ifdef RTE_EAL_VFIO
43 #include <linux/vfio.h>
44 #endif
45
46 #include <rte_pci.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_mbuf.h>
50 #include <rte_string_fns.h>
51 #include <rte_ethdev.h>
52
53 #include "enic_compat.h"
54 #include "enic.h"
55 #include "wq_enet_desc.h"
56 #include "rq_enet_desc.h"
57 #include "cq_enet_desc.h"
58 #include "vnic_enet.h"
59 #include "vnic_dev.h"
60 #include "vnic_wq.h"
61 #include "vnic_rq.h"
62 #include "vnic_cq.h"
63 #include "vnic_intr.h"
64 #include "vnic_nic.h"
65
66 static inline int enic_is_sriov_vf(struct enic *enic)
67 {
68         return enic->pdev->id.device_id == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
69 }
70
71 static int is_zero_addr(char *addr)
72 {
73         return !(addr[0] |  addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
74 }
75
76 static int is_mcast_addr(char *addr)
77 {
78         return addr[0] & 1;
79 }
80
81 static int is_eth_addr_valid(char *addr)
82 {
83         return !is_mcast_addr(addr) && !is_zero_addr(addr);
84 }
85
86 static inline struct rte_mbuf *
87 enic_rxmbuf_alloc(struct rte_mempool *mp)
88 {
89         struct rte_mbuf *m;
90
91         m = __rte_mbuf_raw_alloc(mp);
92         __rte_mbuf_sanity_check_raw(m, 0);
93         return m;
94 }
95
96 static const struct rte_memzone *ring_dma_zone_reserve(
97         struct rte_eth_dev *dev, const char *ring_name,
98         uint16_t queue_id, uint32_t ring_size, int socket_id)
99 {
100         char z_name[RTE_MEMZONE_NAMESIZE];
101         const struct rte_memzone *mz;
102
103         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
104                 dev->driver->pci_drv.name, ring_name,
105                 dev->data->port_id, queue_id);
106
107         mz = rte_memzone_lookup(z_name);
108         if (mz)
109                 return mz;
110
111         return rte_memzone_reserve_aligned(z_name, (uint64_t) ring_size,
112                 socket_id, RTE_MEMZONE_1GB, ENIC_ALIGN);
113 }
114
115 void enic_set_hdr_split_size(struct enic *enic, u16 split_hdr_size)
116 {
117         vnic_set_hdr_split_size(enic->vdev, split_hdr_size);
118 }
119
120 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
121 {
122         struct rte_mbuf *mbuf = (struct rte_mbuf *)buf->os_buf;
123
124         rte_mempool_put(mbuf->pool, mbuf);
125         buf->os_buf = NULL;
126 }
127
128 static void enic_wq_free_buf(struct vnic_wq *wq,
129         struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
130 {
131         enic_free_wq_buf(wq, buf);
132 }
133
134 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
135         u8 type, u16 q_number, u16 completed_index, void *opaque)
136 {
137         struct enic *enic = vnic_dev_priv(vdev);
138
139         vnic_wq_service(&enic->wq[q_number], cq_desc,
140                 completed_index, enic_wq_free_buf,
141                 opaque);
142
143         return 0;
144 }
145
146 static void enic_log_q_error(struct enic *enic)
147 {
148         unsigned int i;
149         u32 error_status;
150
151         for (i = 0; i < enic->wq_count; i++) {
152                 error_status = vnic_wq_error_status(&enic->wq[i]);
153                 if (error_status)
154                         dev_err(enic, "WQ[%d] error_status %d\n", i,
155                                 error_status);
156         }
157
158         for (i = 0; i < enic->rq_count; i++) {
159                 error_status = vnic_rq_error_status(&enic->rq[i]);
160                 if (error_status)
161                         dev_err(enic, "RQ[%d] error_status %d\n", i,
162                                 error_status);
163         }
164 }
165
166 unsigned int enic_cleanup_wq(struct enic *enic, struct vnic_wq *wq)
167 {
168         unsigned int cq = enic_cq_wq(enic, wq->index);
169
170         /* Return the work done */
171         return vnic_cq_service(&enic->cq[cq],
172                 -1 /*wq_work_to_do*/, enic_wq_service, NULL);
173 }
174
175
176 int enic_send_pkt(struct enic *enic, struct vnic_wq *wq,
177         struct rte_mbuf *tx_pkt, unsigned short len,
178         u_int8_t sop, u_int8_t eop,
179         u_int16_t ol_flags, u_int16_t vlan_tag)
180 {
181         struct wq_enet_desc *desc = vnic_wq_next_desc(wq);
182         u_int16_t mss = 0;
183         u_int16_t header_length = 0;
184         u_int8_t cq_entry = eop;
185         u_int8_t vlan_tag_insert = 0;
186         unsigned char *buf = (unsigned char *)(tx_pkt->buf_addr) +
187             RTE_PKTMBUF_HEADROOM;
188         u_int64_t bus_addr = (dma_addr_t)
189             (tx_pkt->buf_physaddr + RTE_PKTMBUF_HEADROOM);
190
191         if (sop) {
192                 if (ol_flags & PKT_TX_VLAN_PKT)
193                         vlan_tag_insert = 1;
194
195                 if (enic->hw_ip_checksum) {
196                         if (ol_flags & PKT_TX_IP_CKSUM)
197                                 mss |= ENIC_CALC_IP_CKSUM;
198
199                         if (ol_flags & PKT_TX_TCP_UDP_CKSUM)
200                                 mss |= ENIC_CALC_TCP_UDP_CKSUM;
201                 }
202         }
203
204         wq_enet_desc_enc(desc,
205                 bus_addr,
206                 len,
207                 mss,
208                 0 /* header_length */,
209                 0 /* offload_mode WQ_ENET_OFFLOAD_MODE_CSUM */,
210                 eop,
211                 cq_entry,
212                 0 /* fcoe_encap */,
213                 vlan_tag_insert,
214                 vlan_tag,
215                 0 /* loopback */);
216
217         vnic_wq_post(wq, (void *)tx_pkt, bus_addr, len,
218                 sop, eop,
219                 1 /*desc_skip_cnt*/,
220                 cq_entry,
221                 0 /*compressed send*/,
222                 0 /*wrid*/);
223
224         return 0;
225 }
226
227 void enic_dev_stats_clear(struct enic *enic)
228 {
229         if (vnic_dev_stats_clear(enic->vdev))
230                 dev_err(enic, "Error in clearing stats\n");
231 }
232
233 void enic_dev_stats_get(struct enic *enic, struct rte_eth_stats *r_stats)
234 {
235         struct vnic_stats *stats;
236
237         memset(r_stats, 0, sizeof(*r_stats));
238         if (vnic_dev_stats_dump(enic->vdev, &stats)) {
239                 dev_err(enic, "Error in getting stats\n");
240                 return;
241         }
242
243         r_stats->ipackets = stats->rx.rx_frames_ok;
244         r_stats->opackets = stats->tx.tx_frames_ok;
245
246         r_stats->ibytes = stats->rx.rx_bytes_ok;
247         r_stats->obytes = stats->tx.tx_bytes_ok;
248
249         r_stats->ierrors = stats->rx.rx_errors;
250         r_stats->oerrors = stats->tx.tx_errors;
251
252         r_stats->imcasts = stats->rx.rx_multicast_frames_ok;
253         r_stats->rx_nombuf = stats->rx.rx_no_bufs;
254 }
255
256 void enic_del_mac_address(struct enic *enic)
257 {
258         if (vnic_dev_del_addr(enic->vdev, enic->mac_addr))
259                 dev_err(enic, "del mac addr failed\n");
260 }
261
262 void enic_set_mac_address(struct enic *enic, uint8_t *mac_addr)
263 {
264         int err;
265
266         if (!is_eth_addr_valid(mac_addr)) {
267                 dev_err(enic, "invalid mac address\n");
268                 return;
269         }
270
271         err = vnic_dev_del_addr(enic->vdev, mac_addr);
272         if (err) {
273                 dev_err(enic, "del mac addr failed\n");
274                 return;
275         }
276
277         ether_addr_copy((struct ether_addr *)mac_addr,
278                 (struct ether_addr *)enic->mac_addr);
279
280         err = vnic_dev_add_addr(enic->vdev, mac_addr);
281         if (err) {
282                 dev_err(enic, "add mac addr failed\n");
283                 return;
284         }
285 }
286
287 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
288 {
289         struct enic *enic = vnic_dev_priv(rq->vdev);
290
291         if (!buf->os_buf)
292                 return;
293
294         rte_pktmbuf_free((struct rte_mbuf *)buf->os_buf);
295         buf->os_buf = NULL;
296 }
297
298 void enic_init_vnic_resources(struct enic *enic)
299 {
300         unsigned int error_interrupt_enable = 1;
301         unsigned int error_interrupt_offset = 0;
302         int index = 0;
303         unsigned int cq_index = 0;
304
305         for (index = 0; index < enic->rq_count; index++) {
306                 vnic_rq_init(&enic->rq[index],
307                         enic_cq_rq(enic, index),
308                         error_interrupt_enable,
309                         error_interrupt_offset);
310         }
311
312         for (index = 0; index < enic->wq_count; index++) {
313                 vnic_wq_init(&enic->wq[index],
314                         enic_cq_wq(enic, index),
315                         error_interrupt_enable,
316                         error_interrupt_offset);
317         }
318
319         vnic_dev_stats_clear(enic->vdev);
320
321         for (index = 0; index < enic->cq_count; index++) {
322                 vnic_cq_init(&enic->cq[index],
323                         0 /* flow_control_enable */,
324                         1 /* color_enable */,
325                         0 /* cq_head */,
326                         0 /* cq_tail */,
327                         1 /* cq_tail_color */,
328                         0 /* interrupt_enable */,
329                         1 /* cq_entry_enable */,
330                         0 /* cq_message_enable */,
331                         0 /* interrupt offset */,
332                         0 /* cq_message_addr */);
333         }
334
335         vnic_intr_init(&enic->intr,
336                 enic->config.intr_timer_usec,
337                 enic->config.intr_timer_type,
338                 /*mask_on_assertion*/1);
339 }
340
341
342 static int enic_rq_alloc_buf(struct vnic_rq *rq)
343 {
344         struct enic *enic = vnic_dev_priv(rq->vdev);
345         void *buf;
346         dma_addr_t dma_addr;
347         struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
348         u_int8_t type = RQ_ENET_TYPE_ONLY_SOP;
349         u_int16_t len = ENIC_MAX_MTU + VLAN_ETH_HLEN;
350         u16 split_hdr_size = vnic_get_hdr_split_size(enic->vdev);
351         struct rte_mbuf *mbuf = enic_rxmbuf_alloc(rq->mp);
352         struct rte_mbuf *hdr_mbuf = NULL;
353
354         if (!mbuf) {
355                 dev_err(enic, "mbuf alloc in enic_rq_alloc_buf failed\n");
356                 return -1;
357         }
358
359         if (unlikely(split_hdr_size)) {
360                 if (vnic_rq_desc_avail(rq) < 2) {
361                         rte_mempool_put(mbuf->pool, mbuf);
362                         return -1;
363                 }
364                 hdr_mbuf = enic_rxmbuf_alloc(rq->mp);
365                 if (!hdr_mbuf) {
366                         rte_mempool_put(mbuf->pool, mbuf);
367                         dev_err(enic,
368                                 "hdr_mbuf alloc in enic_rq_alloc_buf failed\n");
369                         return -1;
370                 }
371
372                 hdr_mbuf->data_off = RTE_PKTMBUF_HEADROOM;
373                 buf = rte_pktmbuf_mtod(hdr_mbuf, void *);
374
375                 hdr_mbuf->nb_segs = 2;
376                 hdr_mbuf->port = rq->index;
377                 hdr_mbuf->next = mbuf;
378
379                 dma_addr = (dma_addr_t)
380                     (hdr_mbuf->buf_physaddr + hdr_mbuf->data_off);
381
382                 rq_enet_desc_enc(desc, dma_addr, type, split_hdr_size);
383
384                 vnic_rq_post(rq, (void *)hdr_mbuf, 0 /*os_buf_index*/, dma_addr,
385                         (unsigned int)split_hdr_size, 0 /*wrid*/);
386
387                 desc = vnic_rq_next_desc(rq);
388                 type = RQ_ENET_TYPE_NOT_SOP;
389         } else {
390                 mbuf->nb_segs = 1;
391                 mbuf->port = rq->index;
392         }
393
394         mbuf->data_off = RTE_PKTMBUF_HEADROOM;
395         buf = rte_pktmbuf_mtod(mbuf, void *);
396         mbuf->next = NULL;
397
398         dma_addr = (dma_addr_t)
399             (mbuf->buf_physaddr + mbuf->data_off);
400
401         rq_enet_desc_enc(desc, dma_addr, type, mbuf->buf_len);
402
403         vnic_rq_post(rq, (void *)mbuf, 0 /*os_buf_index*/, dma_addr,
404                 (unsigned int)mbuf->buf_len, 0 /*wrid*/);
405
406         return 0;
407 }
408
409 static int enic_rq_indicate_buf(struct vnic_rq *rq,
410         struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
411         int skipped, void *opaque)
412 {
413         struct enic *enic = vnic_dev_priv(rq->vdev);
414         struct rte_mbuf **rx_pkt_bucket = (struct rte_mbuf **)opaque;
415         struct rte_mbuf *rx_pkt = NULL;
416         struct rte_mbuf *hdr_rx_pkt = NULL;
417
418         u8 type, color, eop, sop, ingress_port, vlan_stripped;
419         u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
420         u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
421         u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
422         u8 packet_error;
423         u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
424         u32 rss_hash;
425
426         cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
427                 &type, &color, &q_number, &completed_index,
428                 &ingress_port, &fcoe, &eop, &sop, &rss_type,
429                 &csum_not_calc, &rss_hash, &bytes_written,
430                 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
431                 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
432                 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
433                 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
434                 &fcs_ok);
435
436         if (packet_error) {
437                 dev_err(enic, "packet error\n");
438                 return;
439         }
440
441         rx_pkt = (struct rte_mbuf *)buf->os_buf;
442         buf->os_buf = NULL;
443
444         if (unlikely(skipped)) {
445                 rx_pkt->data_len = 0;
446                 return 0;
447         }
448
449         if (likely(!vnic_get_hdr_split_size(enic->vdev))) {
450                 /* No header split configured */
451                 *rx_pkt_bucket = rx_pkt;
452                 rx_pkt->pkt_len = bytes_written;
453
454                 if (ipv4) {
455                         rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
456                         if (!csum_not_calc) {
457                                 if (unlikely(!ipv4_csum_ok))
458                                         rx_pkt->ol_flags |= PKT_RX_IP_CKSUM_BAD;
459
460                                 if ((tcp || udp) && (!tcp_udp_csum_ok))
461                                         rx_pkt->ol_flags |= PKT_RX_L4_CKSUM_BAD;
462                         }
463                 } else if (ipv6)
464                         rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
465         } else {
466                 /* Header split */
467                 if (sop && !eop) {
468                         /* This piece is header */
469                         *rx_pkt_bucket = rx_pkt;
470                         rx_pkt->pkt_len = bytes_written;
471                 } else {
472                         if (sop && eop) {
473                                 /* The packet is smaller than split_hdr_size */
474                                 *rx_pkt_bucket = rx_pkt;
475                                 rx_pkt->pkt_len = bytes_written;
476                                 if (ipv4) {
477                                         rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
478                                         if (!csum_not_calc) {
479                                                 if (unlikely(!ipv4_csum_ok))
480                                                         rx_pkt->ol_flags |=
481                                                             PKT_RX_IP_CKSUM_BAD;
482
483                                                 if ((tcp || udp) &&
484                                                     (!tcp_udp_csum_ok))
485                                                         rx_pkt->ol_flags |=
486                                                             PKT_RX_L4_CKSUM_BAD;
487                                         }
488                                 } else if (ipv6)
489                                         rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
490                         } else {
491                                 /* Payload */
492                                 hdr_rx_pkt = *rx_pkt_bucket;
493                                 hdr_rx_pkt->pkt_len += bytes_written;
494                                 if (ipv4) {
495                                         hdr_rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
496                                         if (!csum_not_calc) {
497                                                 if (unlikely(!ipv4_csum_ok))
498                                                         hdr_rx_pkt->ol_flags |=
499                                                             PKT_RX_IP_CKSUM_BAD;
500
501                                                 if ((tcp || udp) &&
502                                                     (!tcp_udp_csum_ok))
503                                                         hdr_rx_pkt->ol_flags |=
504                                                             PKT_RX_L4_CKSUM_BAD;
505                                         }
506                                 } else if (ipv6)
507                                         hdr_rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
508
509                         }
510                 }
511         }
512
513         rx_pkt->data_len = bytes_written;
514
515         if (rss_hash) {
516                 rx_pkt->ol_flags |= PKT_RX_RSS_HASH;
517                 rx_pkt->hash.rss = rss_hash;
518         }
519
520         if (vlan_tci) {
521                 rx_pkt->ol_flags |= PKT_RX_VLAN_PKT;
522                 rx_pkt->vlan_tci = vlan_tci;
523         }
524
525         return eop;
526 }
527
528 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
529         u8 type, u16 q_number, u16 completed_index, void *opaque)
530 {
531         struct enic *enic = vnic_dev_priv(vdev);
532
533         return vnic_rq_service(&enic->rq[q_number], cq_desc,
534                 completed_index, VNIC_RQ_RETURN_DESC,
535                 enic_rq_indicate_buf, opaque);
536
537 }
538
539 int enic_poll(struct vnic_rq *rq, struct rte_mbuf **rx_pkts,
540         unsigned int budget, unsigned int *work_done)
541 {
542         struct enic *enic = vnic_dev_priv(rq->vdev);
543         unsigned int cq = enic_cq_rq(enic, rq->index);
544         int err = 0;
545
546         *work_done = vnic_cq_service(&enic->cq[cq],
547                 budget, enic_rq_service, (void *)rx_pkts);
548
549         if (*work_done) {
550                 vnic_rq_fill(rq, enic_rq_alloc_buf);
551
552                 /* Need at least one buffer on ring to get going */
553                 if (vnic_rq_desc_used(rq) == 0) {
554                         dev_err(enic, "Unable to alloc receive buffers\n");
555                         err = -1;
556                 }
557         }
558         return err;
559 }
560
561 void *enic_alloc_consistent(void *priv, size_t size,
562         dma_addr_t *dma_handle, u8 *name)
563 {
564         struct enic *enic = (struct enic *)priv;
565         void *vaddr;
566         const struct rte_memzone *rz;
567         *dma_handle = 0;
568
569         rz = rte_memzone_reserve_aligned(name, size, 0, 0, ENIC_ALIGN);
570         if (!rz) {
571                 pr_err("%s : Failed to allocate memory requested for %s",
572                         __func__, name);
573                 return NULL;
574         }
575
576         vaddr = rz->addr;
577         *dma_handle = (dma_addr_t)rz->phys_addr;
578
579         return vaddr;
580 }
581
582 void enic_free_consistent(struct rte_pci_device *hwdev, size_t size,
583         void *vaddr, dma_addr_t dma_handle)
584 {
585         /* Nothing to be done */
586 }
587
588 void enic_intr_handler(__rte_unused struct rte_intr_handle *handle,
589         void *arg)
590 {
591         struct enic *enic = pmd_priv((struct rte_eth_dev *)arg);
592
593         dev_err(enic, "Err intr.\n");
594         vnic_intr_return_all_credits(&enic->intr);
595
596         enic_log_q_error(enic);
597 }
598
599 int enic_enable(struct enic *enic)
600 {
601         int index;
602         void *res;
603         char mz_name[RTE_MEMZONE_NAMESIZE];
604         const struct rte_memzone *rmz;
605         struct rte_eth_dev *eth_dev = enic->rte_dev;
606
607         eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
608         eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
609         vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
610
611         if (enic_clsf_init(enic))
612                 dev_warning(enic, "Init of hash table for clsf failed."\
613                         "Flow director feature will not work\n");
614
615         /* Fill RQ bufs */
616         for (index = 0; index < enic->rq_count; index++) {
617                 vnic_rq_fill(&enic->rq[index], enic_rq_alloc_buf);
618
619                 /* Need at least one buffer on ring to get going
620                 */
621                 if (vnic_rq_desc_used(&enic->rq[index]) == 0) {
622                         dev_err(enic, "Unable to alloc receive buffers\n");
623                         return -1;
624                 }
625         }
626
627         for (index = 0; index < enic->wq_count; index++)
628                 vnic_wq_enable(&enic->wq[index]);
629         for (index = 0; index < enic->rq_count; index++)
630                 vnic_rq_enable(&enic->rq[index]);
631
632         vnic_dev_enable_wait(enic->vdev);
633
634 #ifndef RTE_EAL_VFIO
635         /* Register and enable error interrupt */
636         rte_intr_callback_register(&(enic->pdev->intr_handle),
637                 enic_intr_handler, (void *)enic->rte_dev);
638
639         rte_intr_enable(&(enic->pdev->intr_handle));
640 #endif
641         vnic_intr_unmask(&enic->intr);
642
643         return 0;
644 }
645
646 int enic_alloc_intr_resources(struct enic *enic)
647 {
648         int err;
649
650         dev_info(enic, "vNIC resources used:  "\
651                 "wq %d rq %d cq %d intr %d\n",
652                 enic->wq_count, enic->rq_count,
653                 enic->cq_count, enic->intr_count);
654
655         err = vnic_intr_alloc(enic->vdev, &enic->intr, 0);
656         if (err)
657                 enic_free_vnic_resources(enic);
658
659         return err;
660 }
661
662 void enic_free_rq(void *rxq)
663 {
664         struct vnic_rq *rq = (struct vnic_rq *)rxq;
665         struct enic *enic = vnic_dev_priv(rq->vdev);
666
667         vnic_rq_free(rq);
668         vnic_cq_free(&enic->cq[rq->index]);
669 }
670
671 void enic_start_wq(struct enic *enic, uint16_t queue_idx)
672 {
673         vnic_wq_enable(&enic->wq[queue_idx]);
674 }
675
676 int enic_stop_wq(struct enic *enic, uint16_t queue_idx)
677 {
678         return vnic_wq_disable(&enic->wq[queue_idx]);
679 }
680
681 void enic_start_rq(struct enic *enic, uint16_t queue_idx)
682 {
683         vnic_rq_enable(&enic->rq[queue_idx]);
684 }
685
686 int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
687 {
688         return vnic_rq_disable(&enic->rq[queue_idx]);
689 }
690
691 int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
692         unsigned int socket_id, struct rte_mempool *mp,
693         uint16_t nb_desc)
694 {
695         int err;
696         struct vnic_rq *rq = &enic->rq[queue_idx];
697
698         rq->socket_id = socket_id;
699         rq->mp = mp;
700
701         if (nb_desc) {
702                 if (nb_desc > enic->config.rq_desc_count) {
703                         dev_warning(enic,
704                                 "RQ %d - number of rx desc in cmd line (%d)"\
705                                 "is greater than that in the UCSM/CIMC adapter"\
706                                 "policy.  Applying the value in the adapter "\
707                                 "policy (%d).\n",
708                                 queue_idx, nb_desc, enic->config.rq_desc_count);
709                 } else if (nb_desc != enic->config.rq_desc_count) {
710                         enic->config.rq_desc_count = nb_desc;
711                         dev_info(enic,
712                                 "RX Queues - effective number of descs:%d\n",
713                                 nb_desc);
714                 }
715         }
716
717         /* Allocate queue resources */
718         err = vnic_rq_alloc(enic->vdev, &enic->rq[queue_idx], queue_idx,
719                 enic->config.rq_desc_count,
720                 sizeof(struct rq_enet_desc));
721         if (err) {
722                 dev_err(enic, "error in allocation of rq\n");
723                 return err;
724         }
725
726         err = vnic_cq_alloc(enic->vdev, &enic->cq[queue_idx], queue_idx,
727                 socket_id, enic->config.rq_desc_count,
728                 sizeof(struct cq_enet_rq_desc));
729         if (err) {
730                 vnic_rq_free(rq);
731                 dev_err(enic, "error in allocation of cq for rq\n");
732         }
733
734         return err;
735 }
736
737 void enic_free_wq(void *txq)
738 {
739         struct vnic_wq *wq = (struct vnic_wq *)txq;
740         struct enic *enic = vnic_dev_priv(wq->vdev);
741
742         vnic_wq_free(wq);
743         vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
744 }
745
746 int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
747         unsigned int socket_id, uint16_t nb_desc)
748 {
749         int err;
750         struct vnic_wq *wq = &enic->wq[queue_idx];
751         unsigned int cq_index = enic_cq_wq(enic, queue_idx);
752
753         wq->socket_id = socket_id;
754         if (nb_desc) {
755                 if (nb_desc > enic->config.wq_desc_count) {
756                         dev_warning(enic,
757                                 "WQ %d - number of tx desc in cmd line (%d)"\
758                                 "is greater than that in the UCSM/CIMC adapter"\
759                                 "policy.  Applying the value in the adapter "\
760                                 "policy (%d)\n",
761                                 queue_idx, nb_desc, enic->config.wq_desc_count);
762                 } else if (nb_desc != enic->config.wq_desc_count) {
763                         enic->config.wq_desc_count = nb_desc;
764                         dev_info(enic,
765                                 "TX Queues - effective number of descs:%d\n",
766                                 nb_desc);
767                 }
768         }
769
770         /* Allocate queue resources */
771         err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
772                 enic->config.wq_desc_count,
773                 sizeof(struct wq_enet_desc));
774         if (err) {
775                 dev_err(enic, "error in allocation of wq\n");
776                 return err;
777         }
778
779         err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
780                 socket_id, enic->config.wq_desc_count,
781                 sizeof(struct cq_enet_wq_desc));
782         if (err) {
783                 vnic_wq_free(wq);
784                 dev_err(enic, "error in allocation of cq for wq\n");
785         }
786
787         return err;
788 }
789
790 int enic_disable(struct enic *enic)
791 {
792         unsigned int i;
793         int err;
794
795         vnic_intr_mask(&enic->intr);
796         (void)vnic_intr_masked(&enic->intr); /* flush write */
797
798         vnic_dev_disable(enic->vdev);
799
800         enic_clsf_destroy(enic);
801
802         if (!enic_is_sriov_vf(enic))
803                 vnic_dev_del_addr(enic->vdev, enic->mac_addr);
804
805         for (i = 0; i < enic->wq_count; i++) {
806                 err = vnic_wq_disable(&enic->wq[i]);
807                 if (err)
808                         return err;
809         }
810         for (i = 0; i < enic->rq_count; i++) {
811                 err = vnic_rq_disable(&enic->rq[i]);
812                 if (err)
813                         return err;
814         }
815
816         vnic_dev_set_reset_flag(enic->vdev, 1);
817         vnic_dev_notify_unset(enic->vdev);
818
819         for (i = 0; i < enic->wq_count; i++)
820                 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
821         for (i = 0; i < enic->rq_count; i++)
822                 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
823         for (i = 0; i < enic->cq_count; i++)
824                 vnic_cq_clean(&enic->cq[i]);
825         vnic_intr_clean(&enic->intr);
826
827         return 0;
828 }
829
830 static int enic_dev_wait(struct vnic_dev *vdev,
831         int (*start)(struct vnic_dev *, int),
832         int (*finished)(struct vnic_dev *, int *),
833         int arg)
834 {
835         int done;
836         int err;
837         int i;
838
839         err = start(vdev, arg);
840         if (err)
841                 return err;
842
843         /* Wait for func to complete...2 seconds max */
844         for (i = 0; i < 2000; i++) {
845                 err = finished(vdev, &done);
846                 if (err)
847                         return err;
848                 if (done)
849                         return 0;
850                 usleep(1000);
851         }
852         return -ETIMEDOUT;
853 }
854
855 static int enic_dev_open(struct enic *enic)
856 {
857         int err;
858
859         err = enic_dev_wait(enic->vdev, vnic_dev_open,
860                 vnic_dev_open_done, 0);
861         if (err)
862                 dev_err(enic_get_dev(enic),
863                         "vNIC device open failed, err %d\n", err);
864
865         return err;
866 }
867
868 static int enic_set_rsskey(struct enic *enic)
869 {
870         dma_addr_t rss_key_buf_pa;
871         union vnic_rss_key *rss_key_buf_va = NULL;
872         union vnic_rss_key rss_key = {
873                 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
874                 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
875                 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
876                 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
877         };
878         int err;
879         char name[NAME_MAX];
880
881         snprintf(name, NAME_MAX, "rss_key-%s", enic->bdf_name);
882         rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
883                 &rss_key_buf_pa, name);
884         if (!rss_key_buf_va)
885                 return -ENOMEM;
886
887         rte_memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
888
889         err = enic_set_rss_key(enic,
890                 rss_key_buf_pa,
891                 sizeof(union vnic_rss_key));
892
893         enic_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
894                 rss_key_buf_va, rss_key_buf_pa);
895
896         return err;
897 }
898
899 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
900 {
901         dma_addr_t rss_cpu_buf_pa;
902         union vnic_rss_cpu *rss_cpu_buf_va = NULL;
903         unsigned int i;
904         int err;
905         char name[NAME_MAX];
906
907         snprintf(name, NAME_MAX, "rss_cpu-%s", enic->bdf_name);
908         rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
909                 &rss_cpu_buf_pa, name);
910         if (!rss_cpu_buf_va)
911                 return -ENOMEM;
912
913         for (i = 0; i < (1 << rss_hash_bits); i++)
914                 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
915
916         err = enic_set_rss_cpu(enic,
917                 rss_cpu_buf_pa,
918                 sizeof(union vnic_rss_cpu));
919
920         enic_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
921                 rss_cpu_buf_va, rss_cpu_buf_pa);
922
923         return err;
924 }
925
926 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
927         u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
928 {
929         const u8 tso_ipid_split_en = 0;
930         int err;
931
932         /* Enable VLAN tag stripping */
933
934         err = enic_set_nic_cfg(enic,
935                 rss_default_cpu, rss_hash_type,
936                 rss_hash_bits, rss_base_cpu,
937                 rss_enable, tso_ipid_split_en,
938                 enic->ig_vlan_strip_en);
939
940         return err;
941 }
942
943 int enic_set_rss_nic_cfg(struct enic *enic)
944 {
945         const u8 rss_default_cpu = 0;
946         const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
947             NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
948             NIC_CFG_RSS_HASH_TYPE_IPV6 |
949             NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
950         const u8 rss_hash_bits = 7;
951         const u8 rss_base_cpu = 0;
952         u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
953
954         if (rss_enable) {
955                 if (!enic_set_rsskey(enic)) {
956                         if (enic_set_rsscpu(enic, rss_hash_bits)) {
957                                 rss_enable = 0;
958                                 dev_warning(enic, "RSS disabled, "\
959                                         "Failed to set RSS cpu indirection table.");
960                         }
961                 } else {
962                         rss_enable = 0;
963                         dev_warning(enic,
964                                 "RSS disabled, Failed to set RSS key.\n");
965                 }
966         }
967
968         return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
969                 rss_hash_bits, rss_base_cpu, rss_enable);
970 }
971
972 int enic_setup_finish(struct enic *enic)
973 {
974         int ret;
975
976         ret = enic_set_rss_nic_cfg(enic);
977         if (ret) {
978                 dev_err(enic, "Failed to config nic, aborting.\n");
979                 return -1;
980         }
981
982         vnic_dev_add_addr(enic->vdev, enic->mac_addr);
983
984         /* Default conf */
985         vnic_dev_packet_filter(enic->vdev,
986                 1 /* directed  */,
987                 1 /* multicast */,
988                 1 /* broadcast */,
989                 0 /* promisc   */,
990                 1 /* allmulti  */);
991
992         enic->promisc = 0;
993         enic->allmulti = 1;
994
995         return 0;
996 }
997
998 #ifdef RTE_EAL_VFIO
999 static void enic_eventfd_init(struct enic *enic)
1000 {
1001         enic->eventfd = enic->pdev->intr_handle.fd;
1002 }
1003
1004 void *enic_err_intr_handler(void *arg)
1005 {
1006         struct enic *enic = (struct enic *)arg;
1007         unsigned int intr = enic_msix_err_intr(enic);
1008         ssize_t size;
1009         uint64_t data;
1010
1011         while (1) {
1012                 size = read(enic->eventfd, &data, sizeof(data));
1013                 dev_err(enic, "Err intr.\n");
1014                 vnic_intr_return_all_credits(&enic->intr);
1015
1016                 enic_log_q_error(enic);
1017         }
1018
1019         return NULL;
1020 }
1021 #endif
1022
1023 void enic_add_packet_filter(struct enic *enic)
1024 {
1025         /* Args -> directed, multicast, broadcast, promisc, allmulti */
1026         vnic_dev_packet_filter(enic->vdev, 1, 1, 1,
1027                 enic->promisc, enic->allmulti);
1028 }
1029
1030 int enic_get_link_status(struct enic *enic)
1031 {
1032         return vnic_dev_link_status(enic->vdev);
1033 }
1034
1035
1036 #ifdef RTE_EAL_VFIO
1037 static int enic_create_err_intr_thread(struct enic *enic)
1038 {
1039         pthread_attr_t intr_attr;
1040
1041         /* create threads for error interrupt handling */
1042         pthread_attr_init(&intr_attr);
1043         pthread_attr_setstacksize(&intr_attr, 0x100000);
1044
1045         /* ERR */
1046         if (pthread_create(&enic->err_intr_thread, &intr_attr,
1047                     enic_err_intr_handler, (void *)enic)) {
1048                 dev_err(enic, "Failed to create err interrupt handler threads\n");
1049                 return -1;
1050         }
1051
1052         pthread_attr_destroy(&intr_attr);
1053
1054         return 0;
1055 }
1056
1057
1058 static int enic_set_intr_mode(struct enic *enic)
1059 {
1060         struct vfio_irq_set *irq_set;
1061         int *fds;
1062         int size;
1063         int ret = -1;
1064         int index;
1065
1066         if (enic->intr_count < 1) {
1067                 dev_err(enic, "Unsupported resource conf.\n");
1068                 return -1;
1069         }
1070         vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1071
1072         enic->intr_count = 1;
1073
1074         enic_eventfd_init(enic);
1075         size = sizeof(*irq_set) + (sizeof(int));
1076
1077         irq_set = rte_zmalloc("enic_vfio_irq", size, 0);
1078         irq_set->argsz = size;
1079         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
1080         irq_set->start = 0;
1081         irq_set->count = 1; /* For error interrupt only */
1082         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
1083             VFIO_IRQ_SET_ACTION_TRIGGER;
1084         fds = (int *)&irq_set->data;
1085
1086         fds[0] = enic->eventfd;
1087
1088         ret = ioctl(enic->pdev->intr_handle.vfio_dev_fd,
1089                 VFIO_DEVICE_SET_IRQS, irq_set);
1090         rte_free(irq_set);
1091         if (ret) {
1092                 dev_err(enic, "Failed to set eventfds for interrupts\n");
1093                 return -1;
1094         }
1095
1096         enic_create_err_intr_thread(enic);
1097         return 0;
1098 }
1099
1100 static void enic_clear_intr_mode(struct enic *enic)
1101 {
1102         vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1103 }
1104 #endif
1105
1106 static void enic_dev_deinit(struct enic *enic)
1107 {
1108         unsigned int i;
1109         struct rte_eth_dev *eth_dev = enic->rte_dev;
1110
1111         if (eth_dev->data->mac_addrs)
1112                 rte_free(eth_dev->data->mac_addrs);
1113
1114 #ifdef RTE_EAL_VFIO
1115         enic_clear_intr_mode(enic);
1116 #endif
1117 }
1118
1119
1120 int enic_set_vnic_res(struct enic *enic)
1121 {
1122         struct rte_eth_dev *eth_dev = enic->rte_dev;
1123
1124         if ((enic->rq_count < eth_dev->data->nb_rx_queues) ||
1125                 (enic->wq_count < eth_dev->data->nb_tx_queues)) {
1126                 dev_err(dev, "Not enough resources configured, aborting\n");
1127                 return -1;
1128         }
1129
1130         enic->rq_count = eth_dev->data->nb_rx_queues;
1131         enic->wq_count = eth_dev->data->nb_tx_queues;
1132         if (enic->cq_count < (enic->rq_count + enic->wq_count)) {
1133                 dev_err(dev, "Not enough resources configured, aborting\n");
1134                 return -1;
1135         }
1136
1137         enic->cq_count = enic->rq_count + enic->wq_count;
1138         return 0;
1139 }
1140
1141 static int enic_dev_init(struct enic *enic)
1142 {
1143         unsigned int i;
1144         int err;
1145         struct rte_eth_dev *eth_dev = enic->rte_dev;
1146
1147         vnic_dev_intr_coal_timer_info_default(enic->vdev);
1148
1149         /* Get vNIC configuration
1150         */
1151         err = enic_get_vnic_config(enic);
1152         if (err) {
1153                 dev_err(dev, "Get vNIC configuration failed, aborting\n");
1154                 return err;
1155         }
1156
1157         eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr", ETH_ALEN, 0);
1158         if (!eth_dev->data->mac_addrs) {
1159                 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
1160                 return -1;
1161         }
1162         ether_addr_copy((struct ether_addr *) enic->mac_addr,
1163                 &eth_dev->data->mac_addrs[0]);
1164
1165
1166         /* Get available resource counts
1167         */
1168         enic_get_res_counts(enic);
1169
1170 #ifdef RTE_EAL_VFIO
1171         /* Set interrupt mode based on resource counts and system
1172          * capabilities
1173          */
1174         err = enic_set_intr_mode(enic);
1175         if (err) {
1176                 rte_free(eth_dev->data->mac_addrs);
1177                 enic_clear_intr_mode(enic);
1178                 dev_err(dev, "Failed to set intr mode based on resource "\
1179                         "counts and system capabilities, aborting\n");
1180                 return err;
1181         }
1182 #endif
1183
1184         vnic_dev_set_reset_flag(enic->vdev, 0);
1185
1186         return 0;
1187
1188 }
1189
1190 int enic_probe(struct enic *enic)
1191 {
1192         const char *bdf = enic->bdf_name;
1193         struct rte_pci_device *pdev = enic->pdev;
1194         struct rte_eth_dev *eth_dev = enic->rte_dev;
1195         unsigned int i;
1196         int err = -1;
1197
1198         dev_info(enic, " Initializing ENIC PMD version %s\n", DRV_VERSION);
1199
1200         enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
1201         enic->bar0.len = pdev->mem_resource[0].len;
1202
1203         /* Register vNIC device */
1204         enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
1205         if (!enic->vdev) {
1206                 dev_err(enic, "vNIC registration failed, aborting\n");
1207                 goto err_out;
1208         }
1209
1210         vnic_register_cbacks(enic->vdev,
1211                 enic_alloc_consistent,
1212                 enic_free_consistent);
1213
1214         /* Issue device open to get device in known state */
1215         err = enic_dev_open(enic);
1216         if (err) {
1217                 dev_err(enic, "vNIC dev open failed, aborting\n");
1218                 goto err_out_unregister;
1219         }
1220
1221         /* Set ingress vlan rewrite mode before vnic initialization */
1222         err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1223                 IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
1224         if (err) {
1225                 dev_err(enic,
1226                         "Failed to set ingress vlan rewrite mode, aborting.\n");
1227                 goto err_out_dev_close;
1228         }
1229
1230         /* Issue device init to initialize the vnic-to-switch link.
1231          * We'll start with carrier off and wait for link UP
1232          * notification later to turn on carrier.  We don't need
1233          * to wait here for the vnic-to-switch link initialization
1234          * to complete; link UP notification is the indication that
1235          * the process is complete.
1236          */
1237
1238         err = vnic_dev_init(enic->vdev, 0);
1239         if (err) {
1240                 dev_err(enic, "vNIC dev init failed, aborting\n");
1241                 goto err_out_dev_close;
1242         }
1243
1244         err = enic_dev_init(enic);
1245         if (err) {
1246                 dev_err(enic, "Device initialization failed, aborting\n");
1247                 goto err_out_dev_close;
1248         }
1249
1250         return 0;
1251
1252 err_out_dev_close:
1253         vnic_dev_close(enic->vdev);
1254 err_out_unregister:
1255         vnic_dev_unregister(enic->vdev);
1256 err_out:
1257         return err;
1258 }
1259
1260 void enic_remove(struct enic *enic)
1261 {
1262         enic_dev_deinit(enic);
1263         vnic_dev_close(enic->vdev);
1264         vnic_dev_unregister(enic->vdev);
1265 }