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