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