net/enic: revert truncated packets counter fix
[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 void
83 enic_rxmbuf_queue_release(__rte_unused struct enic *enic, struct vnic_rq *rq)
84 {
85         uint16_t i;
86
87         if (!rq || !rq->mbuf_ring) {
88                 dev_debug(enic, "Pointer to rq or mbuf_ring is NULL");
89                 return;
90         }
91
92         for (i = 0; i < rq->ring.desc_count; i++) {
93                 if (rq->mbuf_ring[i]) {
94                         rte_pktmbuf_free_seg(rq->mbuf_ring[i]);
95                         rq->mbuf_ring[i] = NULL;
96                 }
97         }
98 }
99
100 void enic_set_hdr_split_size(struct enic *enic, u16 split_hdr_size)
101 {
102         vnic_set_hdr_split_size(enic->vdev, split_hdr_size);
103 }
104
105 static void enic_free_wq_buf(struct vnic_wq_buf *buf)
106 {
107         struct rte_mbuf *mbuf = (struct rte_mbuf *)buf->mb;
108
109         rte_pktmbuf_free_seg(mbuf);
110         buf->mb = NULL;
111 }
112
113 static void enic_log_q_error(struct enic *enic)
114 {
115         unsigned int i;
116         u32 error_status;
117
118         for (i = 0; i < enic->wq_count; i++) {
119                 error_status = vnic_wq_error_status(&enic->wq[i]);
120                 if (error_status)
121                         dev_err(enic, "WQ[%d] error_status %d\n", i,
122                                 error_status);
123         }
124
125         for (i = 0; i < enic_vnic_rq_count(enic); i++) {
126                 if (!enic->rq[i].in_use)
127                         continue;
128                 error_status = vnic_rq_error_status(&enic->rq[i]);
129                 if (error_status)
130                         dev_err(enic, "RQ[%d] error_status %d\n", i,
131                                 error_status);
132         }
133 }
134
135 static void enic_clear_soft_stats(struct enic *enic)
136 {
137         struct enic_soft_stats *soft_stats = &enic->soft_stats;
138         rte_atomic64_clear(&soft_stats->rx_nombuf);
139         rte_atomic64_clear(&soft_stats->rx_packet_errors);
140 }
141
142 static void enic_init_soft_stats(struct enic *enic)
143 {
144         struct enic_soft_stats *soft_stats = &enic->soft_stats;
145         rte_atomic64_init(&soft_stats->rx_nombuf);
146         rte_atomic64_init(&soft_stats->rx_packet_errors);
147         enic_clear_soft_stats(enic);
148 }
149
150 void enic_dev_stats_clear(struct enic *enic)
151 {
152         if (vnic_dev_stats_clear(enic->vdev))
153                 dev_err(enic, "Error in clearing stats\n");
154         enic_clear_soft_stats(enic);
155 }
156
157 void enic_dev_stats_get(struct enic *enic, struct rte_eth_stats *r_stats)
158 {
159         struct vnic_stats *stats;
160         struct enic_soft_stats *soft_stats = &enic->soft_stats;
161         int64_t rx_truncated;
162         uint64_t rx_packet_errors;
163
164         if (vnic_dev_stats_dump(enic->vdev, &stats)) {
165                 dev_err(enic, "Error in getting stats\n");
166                 return;
167         }
168
169         /* The number of truncated packets can only be calculated by
170          * subtracting a hardware counter from error packets received by
171          * the driver. Note: this causes transient inaccuracies in the
172          * ipackets count. Also, the length of truncated packets are
173          * counted in ibytes even though truncated packets are dropped
174          * which can make ibytes be slightly higher than it should be.
175          */
176         rx_packet_errors = rte_atomic64_read(&soft_stats->rx_packet_errors);
177         rx_truncated = rx_packet_errors - stats->rx.rx_errors;
178
179         r_stats->ipackets = stats->rx.rx_frames_ok - rx_truncated;
180         r_stats->opackets = stats->tx.tx_frames_ok;
181
182         r_stats->ibytes = stats->rx.rx_bytes_ok;
183         r_stats->obytes = stats->tx.tx_bytes_ok;
184
185         r_stats->ierrors = stats->rx.rx_errors + stats->rx.rx_drop;
186         r_stats->oerrors = stats->tx.tx_errors;
187
188         r_stats->imissed = stats->rx.rx_no_bufs + rx_truncated;
189
190         r_stats->rx_nombuf = rte_atomic64_read(&soft_stats->rx_nombuf);
191 }
192
193 void enic_del_mac_address(struct enic *enic)
194 {
195         if (vnic_dev_del_addr(enic->vdev, enic->mac_addr))
196                 dev_err(enic, "del mac addr failed\n");
197 }
198
199 void enic_set_mac_address(struct enic *enic, uint8_t *mac_addr)
200 {
201         int err;
202
203         if (!is_eth_addr_valid(mac_addr)) {
204                 dev_err(enic, "invalid mac address\n");
205                 return;
206         }
207
208         err = vnic_dev_del_addr(enic->vdev, enic->mac_addr);
209         if (err) {
210                 dev_err(enic, "del mac addr failed\n");
211                 return;
212         }
213
214         ether_addr_copy((struct ether_addr *)mac_addr,
215                 (struct ether_addr *)enic->mac_addr);
216
217         err = vnic_dev_add_addr(enic->vdev, mac_addr);
218         if (err) {
219                 dev_err(enic, "add mac addr failed\n");
220                 return;
221         }
222 }
223
224 static void
225 enic_free_rq_buf(struct rte_mbuf **mbuf)
226 {
227         if (*mbuf == NULL)
228                 return;
229
230         rte_pktmbuf_free(*mbuf);
231         mbuf = NULL;
232 }
233
234 void enic_init_vnic_resources(struct enic *enic)
235 {
236         unsigned int error_interrupt_enable = 1;
237         unsigned int error_interrupt_offset = 0;
238         unsigned int index = 0;
239         unsigned int cq_idx;
240         struct vnic_rq *data_rq;
241
242         for (index = 0; index < enic->rq_count; index++) {
243                 cq_idx = enic_cq_rq(enic, enic_sop_rq(index));
244
245                 vnic_rq_init(&enic->rq[enic_sop_rq(index)],
246                         cq_idx,
247                         error_interrupt_enable,
248                         error_interrupt_offset);
249
250                 data_rq = &enic->rq[enic_data_rq(index)];
251                 if (data_rq->in_use)
252                         vnic_rq_init(data_rq,
253                                      cq_idx,
254                                      error_interrupt_enable,
255                                      error_interrupt_offset);
256
257                 vnic_cq_init(&enic->cq[cq_idx],
258                         0 /* flow_control_enable */,
259                         1 /* color_enable */,
260                         0 /* cq_head */,
261                         0 /* cq_tail */,
262                         1 /* cq_tail_color */,
263                         0 /* interrupt_enable */,
264                         1 /* cq_entry_enable */,
265                         0 /* cq_message_enable */,
266                         0 /* interrupt offset */,
267                         0 /* cq_message_addr */);
268         }
269
270         for (index = 0; index < enic->wq_count; index++) {
271                 vnic_wq_init(&enic->wq[index],
272                         enic_cq_wq(enic, index),
273                         error_interrupt_enable,
274                         error_interrupt_offset);
275
276                 cq_idx = enic_cq_wq(enic, index);
277                 vnic_cq_init(&enic->cq[cq_idx],
278                         0 /* flow_control_enable */,
279                         1 /* color_enable */,
280                         0 /* cq_head */,
281                         0 /* cq_tail */,
282                         1 /* cq_tail_color */,
283                         0 /* interrupt_enable */,
284                         0 /* cq_entry_enable */,
285                         1 /* cq_message_enable */,
286                         0 /* interrupt offset */,
287                         (u64)enic->wq[index].cqmsg_rz->phys_addr);
288         }
289
290         vnic_intr_init(&enic->intr,
291                 enic->config.intr_timer_usec,
292                 enic->config.intr_timer_type,
293                 /*mask_on_assertion*/1);
294 }
295
296
297 static int
298 enic_alloc_rx_queue_mbufs(struct enic *enic, struct vnic_rq *rq)
299 {
300         struct rte_mbuf *mb;
301         struct rq_enet_desc *rqd = rq->ring.descs;
302         unsigned i;
303         dma_addr_t dma_addr;
304
305         if (!rq->in_use)
306                 return 0;
307
308         dev_debug(enic, "queue %u, allocating %u rx queue mbufs\n", rq->index,
309                   rq->ring.desc_count);
310
311         for (i = 0; i < rq->ring.desc_count; i++, rqd++) {
312                 mb = rte_mbuf_raw_alloc(rq->mp);
313                 if (mb == NULL) {
314                         dev_err(enic, "RX mbuf alloc failed queue_id=%u\n",
315                         (unsigned)rq->index);
316                         return -ENOMEM;
317                 }
318
319                 mb->data_off = RTE_PKTMBUF_HEADROOM;
320                 dma_addr = (dma_addr_t)(mb->buf_physaddr
321                            + RTE_PKTMBUF_HEADROOM);
322                 rq_enet_desc_enc(rqd, dma_addr,
323                                 (rq->is_sop ? RQ_ENET_TYPE_ONLY_SOP
324                                 : RQ_ENET_TYPE_NOT_SOP),
325                                 mb->buf_len - RTE_PKTMBUF_HEADROOM);
326                 rq->mbuf_ring[i] = mb;
327         }
328
329         /* make sure all prior writes are complete before doing the PIO write */
330         rte_rmb();
331
332         /* Post all but the last buffer to VIC. */
333         rq->posted_index = rq->ring.desc_count - 1;
334
335         rq->rx_nb_hold = 0;
336
337         dev_debug(enic, "port=%u, qidx=%u, Write %u posted idx, %u sw held\n",
338                 enic->port_id, rq->index, rq->posted_index, rq->rx_nb_hold);
339         iowrite32(rq->posted_index, &rq->ctrl->posted_index);
340         iowrite32(0, &rq->ctrl->fetch_index);
341         rte_rmb();
342
343         return 0;
344
345 }
346
347 static void *
348 enic_alloc_consistent(void *priv, size_t size,
349         dma_addr_t *dma_handle, u8 *name)
350 {
351         void *vaddr;
352         const struct rte_memzone *rz;
353         *dma_handle = 0;
354         struct enic *enic = (struct enic *)priv;
355         struct enic_memzone_entry *mze;
356
357         rz = rte_memzone_reserve_aligned((const char *)name,
358                                          size, SOCKET_ID_ANY, 0, ENIC_ALIGN);
359         if (!rz) {
360                 pr_err("%s : Failed to allocate memory requested for %s\n",
361                         __func__, name);
362                 return NULL;
363         }
364
365         vaddr = rz->addr;
366         *dma_handle = (dma_addr_t)rz->phys_addr;
367
368         mze = rte_malloc("enic memzone entry",
369                          sizeof(struct enic_memzone_entry), 0);
370
371         if (!mze) {
372                 pr_err("%s : Failed to allocate memory for memzone list\n",
373                        __func__);
374                 rte_memzone_free(rz);
375         }
376
377         mze->rz = rz;
378
379         rte_spinlock_lock(&enic->memzone_list_lock);
380         LIST_INSERT_HEAD(&enic->memzone_list, mze, entries);
381         rte_spinlock_unlock(&enic->memzone_list_lock);
382
383         return vaddr;
384 }
385
386 static void
387 enic_free_consistent(void *priv,
388                      __rte_unused size_t size,
389                      void *vaddr,
390                      dma_addr_t dma_handle)
391 {
392         struct enic_memzone_entry *mze;
393         struct enic *enic = (struct enic *)priv;
394
395         rte_spinlock_lock(&enic->memzone_list_lock);
396         LIST_FOREACH(mze, &enic->memzone_list, entries) {
397                 if (mze->rz->addr == vaddr &&
398                     mze->rz->phys_addr == dma_handle)
399                         break;
400         }
401         if (mze == NULL) {
402                 rte_spinlock_unlock(&enic->memzone_list_lock);
403                 dev_warning(enic,
404                             "Tried to free memory, but couldn't find it in the memzone list\n");
405                 return;
406         }
407         LIST_REMOVE(mze, entries);
408         rte_spinlock_unlock(&enic->memzone_list_lock);
409         rte_memzone_free(mze->rz);
410         rte_free(mze);
411 }
412
413 int enic_link_update(struct enic *enic)
414 {
415         struct rte_eth_dev *eth_dev = enic->rte_dev;
416         int ret;
417         int link_status = 0;
418
419         link_status = enic_get_link_status(enic);
420         ret = (link_status == enic->link_status);
421         enic->link_status = link_status;
422         eth_dev->data->dev_link.link_status = link_status;
423         eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
424         eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
425         return ret;
426 }
427
428 static void
429 enic_intr_handler(__rte_unused struct rte_intr_handle *handle,
430         void *arg)
431 {
432         struct rte_eth_dev *dev = (struct rte_eth_dev *)arg;
433         struct enic *enic = pmd_priv(dev);
434
435         vnic_intr_return_all_credits(&enic->intr);
436
437         enic_link_update(enic);
438         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
439         enic_log_q_error(enic);
440 }
441
442 int enic_enable(struct enic *enic)
443 {
444         unsigned int index;
445         int err;
446         struct rte_eth_dev *eth_dev = enic->rte_dev;
447
448         eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
449         eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
450
451         /* vnic notification of link status has already been turned on in
452          * enic_dev_init() which is called during probe time.  Here we are
453          * just turning on interrupt vector 0 if needed.
454          */
455         if (eth_dev->data->dev_conf.intr_conf.lsc)
456                 vnic_dev_notify_set(enic->vdev, 0);
457
458         if (enic_clsf_init(enic))
459                 dev_warning(enic, "Init of hash table for clsf failed."\
460                         "Flow director feature will not work\n");
461
462         for (index = 0; index < enic->rq_count; index++) {
463                 err = enic_alloc_rx_queue_mbufs(enic,
464                         &enic->rq[enic_sop_rq(index)]);
465                 if (err) {
466                         dev_err(enic, "Failed to alloc sop RX queue mbufs\n");
467                         return err;
468                 }
469                 err = enic_alloc_rx_queue_mbufs(enic,
470                         &enic->rq[enic_data_rq(index)]);
471                 if (err) {
472                         /* release the allocated mbufs for the sop rq*/
473                         enic_rxmbuf_queue_release(enic,
474                                 &enic->rq[enic_sop_rq(index)]);
475
476                         dev_err(enic, "Failed to alloc data RX queue mbufs\n");
477                         return err;
478                 }
479         }
480
481         for (index = 0; index < enic->wq_count; index++)
482                 enic_start_wq(enic, index);
483         for (index = 0; index < enic->rq_count; index++)
484                 enic_start_rq(enic, index);
485
486         vnic_dev_add_addr(enic->vdev, enic->mac_addr);
487
488         vnic_dev_enable_wait(enic->vdev);
489
490         /* Register and enable error interrupt */
491         rte_intr_callback_register(&(enic->pdev->intr_handle),
492                 enic_intr_handler, (void *)enic->rte_dev);
493
494         rte_intr_enable(&(enic->pdev->intr_handle));
495         vnic_intr_unmask(&enic->intr);
496
497         return 0;
498 }
499
500 int enic_alloc_intr_resources(struct enic *enic)
501 {
502         int err;
503
504         dev_info(enic, "vNIC resources used:  "\
505                 "wq %d rq %d cq %d intr %d\n",
506                 enic->wq_count, enic_vnic_rq_count(enic),
507                 enic->cq_count, enic->intr_count);
508
509         err = vnic_intr_alloc(enic->vdev, &enic->intr, 0);
510         if (err)
511                 enic_free_vnic_resources(enic);
512
513         return err;
514 }
515
516 void enic_free_rq(void *rxq)
517 {
518         struct vnic_rq *rq_sop, *rq_data;
519         struct enic *enic;
520
521         if (rxq == NULL)
522                 return;
523
524         rq_sop = (struct vnic_rq *)rxq;
525         enic = vnic_dev_priv(rq_sop->vdev);
526         rq_data = &enic->rq[rq_sop->data_queue_idx];
527
528         enic_rxmbuf_queue_release(enic, rq_sop);
529         if (rq_data->in_use)
530                 enic_rxmbuf_queue_release(enic, rq_data);
531
532         rte_free(rq_sop->mbuf_ring);
533         if (rq_data->in_use)
534                 rte_free(rq_data->mbuf_ring);
535
536         rq_sop->mbuf_ring = NULL;
537         rq_data->mbuf_ring = NULL;
538
539         vnic_rq_free(rq_sop);
540         if (rq_data->in_use)
541                 vnic_rq_free(rq_data);
542
543         vnic_cq_free(&enic->cq[rq_sop->index]);
544
545         rq_sop->in_use = 0;
546         rq_data->in_use = 0;
547 }
548
549 void enic_start_wq(struct enic *enic, uint16_t queue_idx)
550 {
551         struct rte_eth_dev *eth_dev = enic->rte_dev;
552         vnic_wq_enable(&enic->wq[queue_idx]);
553         eth_dev->data->tx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STARTED;
554 }
555
556 int enic_stop_wq(struct enic *enic, uint16_t queue_idx)
557 {
558         struct rte_eth_dev *eth_dev = enic->rte_dev;
559         int ret;
560
561         ret = vnic_wq_disable(&enic->wq[queue_idx]);
562         if (ret)
563                 return ret;
564
565         eth_dev->data->tx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
566         return 0;
567 }
568
569 void enic_start_rq(struct enic *enic, uint16_t queue_idx)
570 {
571         struct vnic_rq *rq_sop = &enic->rq[enic_sop_rq(queue_idx)];
572         struct vnic_rq *rq_data = &enic->rq[rq_sop->data_queue_idx];
573         struct rte_eth_dev *eth_dev = enic->rte_dev;
574
575         if (rq_data->in_use)
576                 vnic_rq_enable(rq_data);
577         rte_mb();
578         vnic_rq_enable(rq_sop);
579         eth_dev->data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STARTED;
580 }
581
582 int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
583 {
584         int ret1 = 0, ret2 = 0;
585         struct rte_eth_dev *eth_dev = enic->rte_dev;
586         struct vnic_rq *rq_sop = &enic->rq[enic_sop_rq(queue_idx)];
587         struct vnic_rq *rq_data = &enic->rq[rq_sop->data_queue_idx];
588
589         ret2 = vnic_rq_disable(rq_sop);
590         rte_mb();
591         if (rq_data->in_use)
592                 ret1 = vnic_rq_disable(rq_data);
593
594         if (ret2)
595                 return ret2;
596         else if (ret1)
597                 return ret1;
598
599         eth_dev->data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
600         return 0;
601 }
602
603 int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
604         unsigned int socket_id, struct rte_mempool *mp,
605         uint16_t nb_desc)
606 {
607         int rc;
608         uint16_t sop_queue_idx = enic_sop_rq(queue_idx);
609         uint16_t data_queue_idx = enic_data_rq(queue_idx);
610         struct vnic_rq *rq_sop = &enic->rq[sop_queue_idx];
611         struct vnic_rq *rq_data = &enic->rq[data_queue_idx];
612         unsigned int mbuf_size, mbufs_per_pkt;
613         unsigned int nb_sop_desc, nb_data_desc;
614         uint16_t min_sop, max_sop, min_data, max_data;
615         uint16_t mtu = enic->rte_dev->data->mtu;
616
617         rq_sop->is_sop = 1;
618         rq_sop->data_queue_idx = data_queue_idx;
619         rq_data->is_sop = 0;
620         rq_data->data_queue_idx = 0;
621         rq_sop->socket_id = socket_id;
622         rq_sop->mp = mp;
623         rq_data->socket_id = socket_id;
624         rq_data->mp = mp;
625         rq_sop->in_use = 1;
626
627         mbuf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
628                                RTE_PKTMBUF_HEADROOM);
629
630         if (enic->rte_dev->data->dev_conf.rxmode.enable_scatter) {
631                 dev_info(enic, "Rq %u Scatter rx mode enabled\n", queue_idx);
632                 /* ceil((mtu + ETHER_HDR_LEN + 4)/mbuf_size) */
633                 mbufs_per_pkt = ((mtu + ETHER_HDR_LEN + 4) +
634                                  (mbuf_size - 1)) / mbuf_size;
635         } else {
636                 dev_info(enic, "Scatter rx mode disabled\n");
637                 mbufs_per_pkt = 1;
638         }
639
640         if (mbufs_per_pkt > 1) {
641                 dev_info(enic, "Rq %u Scatter rx mode in use\n", queue_idx);
642                 rq_sop->data_queue_enable = 1;
643                 rq_data->in_use = 1;
644         } else {
645                 dev_info(enic, "Rq %u Scatter rx mode not being used\n",
646                          queue_idx);
647                 rq_sop->data_queue_enable = 0;
648                 rq_data->in_use = 0;
649         }
650
651         /* number of descriptors have to be a multiple of 32 */
652         nb_sop_desc = (nb_desc / mbufs_per_pkt) & ~0x1F;
653         nb_data_desc = (nb_desc - nb_sop_desc) & ~0x1F;
654
655         rq_sop->max_mbufs_per_pkt = mbufs_per_pkt;
656         rq_data->max_mbufs_per_pkt = mbufs_per_pkt;
657
658         if (mbufs_per_pkt > 1) {
659                 min_sop = 64;
660                 max_sop = ((enic->config.rq_desc_count /
661                             (mbufs_per_pkt - 1)) & ~0x1F);
662                 min_data = min_sop * (mbufs_per_pkt - 1);
663                 max_data = enic->config.rq_desc_count;
664         } else {
665                 min_sop = 64;
666                 max_sop = enic->config.rq_desc_count;
667                 min_data = 0;
668                 max_data = 0;
669         }
670
671         if (nb_desc < (min_sop + min_data)) {
672                 dev_warning(enic,
673                             "Number of rx descs too low, adjusting to minimum\n");
674                 nb_sop_desc = min_sop;
675                 nb_data_desc = min_data;
676         } else if (nb_desc > (max_sop + max_data)) {
677                 dev_warning(enic,
678                             "Number of rx_descs too high, adjusting to maximum\n");
679                 nb_sop_desc = max_sop;
680                 nb_data_desc = max_data;
681         }
682         if (mbufs_per_pkt > 1) {
683                 dev_info(enic, "For mtu %d and mbuf size %d valid rx descriptor range is %d to %d\n",
684                          mtu, mbuf_size, min_sop + min_data,
685                          max_sop + max_data);
686         }
687         dev_info(enic, "Using %d rx descriptors (sop %d, data %d)\n",
688                  nb_sop_desc + nb_data_desc, nb_sop_desc, nb_data_desc);
689
690         /* Allocate sop queue resources */
691         rc = vnic_rq_alloc(enic->vdev, rq_sop, sop_queue_idx,
692                 nb_sop_desc, sizeof(struct rq_enet_desc));
693         if (rc) {
694                 dev_err(enic, "error in allocation of sop rq\n");
695                 goto err_exit;
696         }
697         nb_sop_desc = rq_sop->ring.desc_count;
698
699         if (rq_data->in_use) {
700                 /* Allocate data queue resources */
701                 rc = vnic_rq_alloc(enic->vdev, rq_data, data_queue_idx,
702                                    nb_data_desc,
703                                    sizeof(struct rq_enet_desc));
704                 if (rc) {
705                         dev_err(enic, "error in allocation of data rq\n");
706                         goto err_free_rq_sop;
707                 }
708                 nb_data_desc = rq_data->ring.desc_count;
709         }
710         rc = vnic_cq_alloc(enic->vdev, &enic->cq[queue_idx], queue_idx,
711                            socket_id, nb_sop_desc + nb_data_desc,
712                            sizeof(struct cq_enet_rq_desc));
713         if (rc) {
714                 dev_err(enic, "error in allocation of cq for rq\n");
715                 goto err_free_rq_data;
716         }
717
718         /* Allocate the mbuf rings */
719         rq_sop->mbuf_ring = (struct rte_mbuf **)
720                 rte_zmalloc_socket("rq->mbuf_ring",
721                                    sizeof(struct rte_mbuf *) * nb_sop_desc,
722                                    RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
723         if (rq_sop->mbuf_ring == NULL)
724                 goto err_free_cq;
725
726         if (rq_data->in_use) {
727                 rq_data->mbuf_ring = (struct rte_mbuf **)
728                         rte_zmalloc_socket("rq->mbuf_ring",
729                                 sizeof(struct rte_mbuf *) * nb_data_desc,
730                                 RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
731                 if (rq_data->mbuf_ring == NULL)
732                         goto err_free_sop_mbuf;
733         }
734
735         rq_sop->tot_nb_desc = nb_desc; /* squirl away for MTU update function */
736
737         return 0;
738
739 err_free_sop_mbuf:
740         rte_free(rq_sop->mbuf_ring);
741 err_free_cq:
742         /* cleanup on error */
743         vnic_cq_free(&enic->cq[queue_idx]);
744 err_free_rq_data:
745         if (rq_data->in_use)
746                 vnic_rq_free(rq_data);
747 err_free_rq_sop:
748         vnic_rq_free(rq_sop);
749 err_exit:
750         return -ENOMEM;
751 }
752
753 void enic_free_wq(void *txq)
754 {
755         struct vnic_wq *wq;
756         struct enic *enic;
757
758         if (txq == NULL)
759                 return;
760
761         wq = (struct vnic_wq *)txq;
762         enic = vnic_dev_priv(wq->vdev);
763         rte_memzone_free(wq->cqmsg_rz);
764         vnic_wq_free(wq);
765         vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
766 }
767
768 int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
769         unsigned int socket_id, uint16_t nb_desc)
770 {
771         int err;
772         struct vnic_wq *wq = &enic->wq[queue_idx];
773         unsigned int cq_index = enic_cq_wq(enic, queue_idx);
774         char name[NAME_MAX];
775         static int instance;
776
777         wq->socket_id = socket_id;
778         if (nb_desc) {
779                 if (nb_desc > enic->config.wq_desc_count) {
780                         dev_warning(enic,
781                                 "WQ %d - number of tx desc in cmd line (%d)"\
782                                 "is greater than that in the UCSM/CIMC adapter"\
783                                 "policy.  Applying the value in the adapter "\
784                                 "policy (%d)\n",
785                                 queue_idx, nb_desc, enic->config.wq_desc_count);
786                 } else if (nb_desc != enic->config.wq_desc_count) {
787                         enic->config.wq_desc_count = nb_desc;
788                         dev_info(enic,
789                                 "TX Queues - effective number of descs:%d\n",
790                                 nb_desc);
791                 }
792         }
793
794         /* Allocate queue resources */
795         err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
796                 enic->config.wq_desc_count,
797                 sizeof(struct wq_enet_desc));
798         if (err) {
799                 dev_err(enic, "error in allocation of wq\n");
800                 return err;
801         }
802
803         err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
804                 socket_id, enic->config.wq_desc_count,
805                 sizeof(struct cq_enet_wq_desc));
806         if (err) {
807                 vnic_wq_free(wq);
808                 dev_err(enic, "error in allocation of cq for wq\n");
809         }
810
811         /* setup up CQ message */
812         snprintf((char *)name, sizeof(name),
813                  "vnic_cqmsg-%s-%d-%d", enic->bdf_name, queue_idx,
814                 instance++);
815
816         wq->cqmsg_rz = rte_memzone_reserve_aligned((const char *)name,
817                                                    sizeof(uint32_t),
818                                                    SOCKET_ID_ANY, 0,
819                                                    ENIC_ALIGN);
820         if (!wq->cqmsg_rz)
821                 return -ENOMEM;
822
823         return err;
824 }
825
826 int enic_disable(struct enic *enic)
827 {
828         unsigned int i;
829         int err;
830
831         vnic_intr_mask(&enic->intr);
832         (void)vnic_intr_masked(&enic->intr); /* flush write */
833         rte_intr_disable(&enic->pdev->intr_handle);
834         rte_intr_callback_unregister(&enic->pdev->intr_handle,
835                                      enic_intr_handler,
836                                      (void *)enic->rte_dev);
837
838         vnic_dev_disable(enic->vdev);
839
840         enic_clsf_destroy(enic);
841
842         if (!enic_is_sriov_vf(enic))
843                 vnic_dev_del_addr(enic->vdev, enic->mac_addr);
844
845         for (i = 0; i < enic->wq_count; i++) {
846                 err = vnic_wq_disable(&enic->wq[i]);
847                 if (err)
848                         return err;
849         }
850         for (i = 0; i < enic_vnic_rq_count(enic); i++) {
851                 if (enic->rq[i].in_use) {
852                         err = vnic_rq_disable(&enic->rq[i]);
853                         if (err)
854                                 return err;
855                 }
856         }
857
858         /* If we were using interrupts, set the interrupt vector to -1
859          * to disable interrupts.  We are not disabling link notifcations,
860          * though, as we want the polling of link status to continue working.
861          */
862         if (enic->rte_dev->data->dev_conf.intr_conf.lsc)
863                 vnic_dev_notify_set(enic->vdev, -1);
864
865         vnic_dev_set_reset_flag(enic->vdev, 1);
866
867         for (i = 0; i < enic->wq_count; i++)
868                 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
869
870         for (i = 0; i < enic_vnic_rq_count(enic); i++)
871                 if (enic->rq[i].in_use)
872                         vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
873         for (i = 0; i < enic->cq_count; i++)
874                 vnic_cq_clean(&enic->cq[i]);
875         vnic_intr_clean(&enic->intr);
876
877         return 0;
878 }
879
880 static int enic_dev_wait(struct vnic_dev *vdev,
881         int (*start)(struct vnic_dev *, int),
882         int (*finished)(struct vnic_dev *, int *),
883         int arg)
884 {
885         int done;
886         int err;
887         int i;
888
889         err = start(vdev, arg);
890         if (err)
891                 return err;
892
893         /* Wait for func to complete...2 seconds max */
894         for (i = 0; i < 2000; i++) {
895                 err = finished(vdev, &done);
896                 if (err)
897                         return err;
898                 if (done)
899                         return 0;
900                 usleep(1000);
901         }
902         return -ETIMEDOUT;
903 }
904
905 static int enic_dev_open(struct enic *enic)
906 {
907         int err;
908
909         err = enic_dev_wait(enic->vdev, vnic_dev_open,
910                 vnic_dev_open_done, 0);
911         if (err)
912                 dev_err(enic_get_dev(enic),
913                         "vNIC device open failed, err %d\n", err);
914
915         return err;
916 }
917
918 static int enic_set_rsskey(struct enic *enic)
919 {
920         dma_addr_t rss_key_buf_pa;
921         union vnic_rss_key *rss_key_buf_va = NULL;
922         static union vnic_rss_key rss_key = {
923                 .key = {
924                         [0] = {.b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101}},
925                         [1] = {.b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101}},
926                         [2] = {.b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115}},
927                         [3] = {.b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108}},
928                 }
929         };
930         int err;
931         u8 name[NAME_MAX];
932
933         snprintf((char *)name, NAME_MAX, "rss_key-%s", enic->bdf_name);
934         rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
935                 &rss_key_buf_pa, name);
936         if (!rss_key_buf_va)
937                 return -ENOMEM;
938
939         rte_memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
940
941         err = enic_set_rss_key(enic,
942                 rss_key_buf_pa,
943                 sizeof(union vnic_rss_key));
944
945         enic_free_consistent(enic, sizeof(union vnic_rss_key),
946                 rss_key_buf_va, rss_key_buf_pa);
947
948         return err;
949 }
950
951 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
952 {
953         dma_addr_t rss_cpu_buf_pa;
954         union vnic_rss_cpu *rss_cpu_buf_va = NULL;
955         int i;
956         int err;
957         u8 name[NAME_MAX];
958
959         snprintf((char *)name, NAME_MAX, "rss_cpu-%s", enic->bdf_name);
960         rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
961                 &rss_cpu_buf_pa, name);
962         if (!rss_cpu_buf_va)
963                 return -ENOMEM;
964
965         for (i = 0; i < (1 << rss_hash_bits); i++)
966                 (*rss_cpu_buf_va).cpu[i / 4].b[i % 4] =
967                         enic_sop_rq(i % enic->rq_count);
968
969         err = enic_set_rss_cpu(enic,
970                 rss_cpu_buf_pa,
971                 sizeof(union vnic_rss_cpu));
972
973         enic_free_consistent(enic, sizeof(union vnic_rss_cpu),
974                 rss_cpu_buf_va, rss_cpu_buf_pa);
975
976         return err;
977 }
978
979 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
980         u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
981 {
982         const u8 tso_ipid_split_en = 0;
983         int err;
984
985         /* Enable VLAN tag stripping */
986
987         err = enic_set_nic_cfg(enic,
988                 rss_default_cpu, rss_hash_type,
989                 rss_hash_bits, rss_base_cpu,
990                 rss_enable, tso_ipid_split_en,
991                 enic->ig_vlan_strip_en);
992
993         return err;
994 }
995
996 int enic_set_rss_nic_cfg(struct enic *enic)
997 {
998         const u8 rss_default_cpu = 0;
999         const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1000             NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1001             NIC_CFG_RSS_HASH_TYPE_IPV6 |
1002             NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1003         const u8 rss_hash_bits = 7;
1004         const u8 rss_base_cpu = 0;
1005         u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1006
1007         if (rss_enable) {
1008                 if (!enic_set_rsskey(enic)) {
1009                         if (enic_set_rsscpu(enic, rss_hash_bits)) {
1010                                 rss_enable = 0;
1011                                 dev_warning(enic, "RSS disabled, "\
1012                                         "Failed to set RSS cpu indirection table.");
1013                         }
1014                 } else {
1015                         rss_enable = 0;
1016                         dev_warning(enic,
1017                                 "RSS disabled, Failed to set RSS key.\n");
1018                 }
1019         }
1020
1021         return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1022                 rss_hash_bits, rss_base_cpu, rss_enable);
1023 }
1024
1025 int enic_setup_finish(struct enic *enic)
1026 {
1027         int ret;
1028
1029         enic_init_soft_stats(enic);
1030
1031         ret = enic_set_rss_nic_cfg(enic);
1032         if (ret) {
1033                 dev_err(enic, "Failed to config nic, aborting.\n");
1034                 return -1;
1035         }
1036
1037         /* Default conf */
1038         vnic_dev_packet_filter(enic->vdev,
1039                 1 /* directed  */,
1040                 1 /* multicast */,
1041                 1 /* broadcast */,
1042                 0 /* promisc   */,
1043                 1 /* allmulti  */);
1044
1045         enic->promisc = 0;
1046         enic->allmulti = 1;
1047
1048         return 0;
1049 }
1050
1051 void enic_add_packet_filter(struct enic *enic)
1052 {
1053         /* Args -> directed, multicast, broadcast, promisc, allmulti */
1054         vnic_dev_packet_filter(enic->vdev, 1, 1, 1,
1055                 enic->promisc, enic->allmulti);
1056 }
1057
1058 int enic_get_link_status(struct enic *enic)
1059 {
1060         return vnic_dev_link_status(enic->vdev);
1061 }
1062
1063 static void enic_dev_deinit(struct enic *enic)
1064 {
1065         struct rte_eth_dev *eth_dev = enic->rte_dev;
1066
1067         /* stop link status checking */
1068         vnic_dev_notify_unset(enic->vdev);
1069
1070         rte_free(eth_dev->data->mac_addrs);
1071 }
1072
1073
1074 int enic_set_vnic_res(struct enic *enic)
1075 {
1076         struct rte_eth_dev *eth_dev = enic->rte_dev;
1077         int rc = 0;
1078
1079         /* With Rx scatter support, two RQs are now used per RQ used by
1080          * the application.
1081          */
1082         if (enic->conf_rq_count < eth_dev->data->nb_rx_queues) {
1083                 dev_err(dev, "Not enough Receive queues. Requested:%u which uses %d RQs on VIC, Configured:%u\n",
1084                         eth_dev->data->nb_rx_queues,
1085                         eth_dev->data->nb_rx_queues * 2, enic->conf_rq_count);
1086                 rc = -EINVAL;
1087         }
1088         if (enic->conf_wq_count < eth_dev->data->nb_tx_queues) {
1089                 dev_err(dev, "Not enough Transmit queues. Requested:%u, Configured:%u\n",
1090                         eth_dev->data->nb_tx_queues, enic->conf_wq_count);
1091                 rc = -EINVAL;
1092         }
1093
1094         if (enic->conf_cq_count < (eth_dev->data->nb_rx_queues +
1095                                    eth_dev->data->nb_tx_queues)) {
1096                 dev_err(dev, "Not enough Completion queues. Required:%u, Configured:%u\n",
1097                         (eth_dev->data->nb_rx_queues +
1098                          eth_dev->data->nb_tx_queues), enic->conf_cq_count);
1099                 rc = -EINVAL;
1100         }
1101
1102         if (rc == 0) {
1103                 enic->rq_count = eth_dev->data->nb_rx_queues;
1104                 enic->wq_count = eth_dev->data->nb_tx_queues;
1105                 enic->cq_count = enic->rq_count + enic->wq_count;
1106         }
1107
1108         return rc;
1109 }
1110
1111 /* Initialize the completion queue for an RQ */
1112 static int
1113 enic_reinit_rq(struct enic *enic, unsigned int rq_idx)
1114 {
1115         struct vnic_rq *sop_rq, *data_rq;
1116         unsigned int cq_idx = enic_cq_rq(enic, rq_idx);
1117         int rc = 0;
1118
1119         sop_rq = &enic->rq[enic_sop_rq(rq_idx)];
1120         data_rq = &enic->rq[enic_data_rq(rq_idx)];
1121
1122         vnic_cq_clean(&enic->cq[cq_idx]);
1123         vnic_cq_init(&enic->cq[cq_idx],
1124                      0 /* flow_control_enable */,
1125                      1 /* color_enable */,
1126                      0 /* cq_head */,
1127                      0 /* cq_tail */,
1128                      1 /* cq_tail_color */,
1129                      0 /* interrupt_enable */,
1130                      1 /* cq_entry_enable */,
1131                      0 /* cq_message_enable */,
1132                      0 /* interrupt offset */,
1133                      0 /* cq_message_addr */);
1134
1135
1136         vnic_rq_init_start(sop_rq, enic_cq_rq(enic, enic_sop_rq(rq_idx)),
1137                            0, sop_rq->ring.desc_count - 1, 1, 0);
1138         if (data_rq->in_use) {
1139                 vnic_rq_init_start(data_rq,
1140                                    enic_cq_rq(enic, enic_data_rq(rq_idx)),
1141                                    0, data_rq->ring.desc_count - 1, 1, 0);
1142         }
1143
1144         rc = enic_alloc_rx_queue_mbufs(enic, sop_rq);
1145         if (rc)
1146                 return rc;
1147
1148         if (data_rq->in_use) {
1149                 rc = enic_alloc_rx_queue_mbufs(enic, data_rq);
1150                 if (rc) {
1151                         enic_rxmbuf_queue_release(enic, sop_rq);
1152                         return rc;
1153                 }
1154         }
1155
1156         return 0;
1157 }
1158
1159 /* The Cisco NIC can send and receive packets up to a max packet size
1160  * determined by the NIC type and firmware. There is also an MTU
1161  * configured into the NIC via the CIMC/UCSM management interface
1162  * which can be overridden by this function (up to the max packet size).
1163  * Depending on the network setup, doing so may cause packet drops
1164  * and unexpected behavior.
1165  */
1166 int enic_set_mtu(struct enic *enic, uint16_t new_mtu)
1167 {
1168         unsigned int rq_idx;
1169         struct vnic_rq *rq;
1170         int rc = 0;
1171         uint16_t old_mtu;       /* previous setting */
1172         uint16_t config_mtu;    /* Value configured into NIC via CIMC/UCSM */
1173         struct rte_eth_dev *eth_dev = enic->rte_dev;
1174
1175         old_mtu = eth_dev->data->mtu;
1176         config_mtu = enic->config.mtu;
1177
1178         if (new_mtu > enic->max_mtu) {
1179                 dev_err(enic,
1180                         "MTU not updated: requested (%u) greater than max (%u)\n",
1181                         new_mtu, enic->max_mtu);
1182                 return -EINVAL;
1183         }
1184         if (new_mtu < ENIC_MIN_MTU) {
1185                 dev_info(enic,
1186                         "MTU not updated: requested (%u) less than min (%u)\n",
1187                         new_mtu, ENIC_MIN_MTU);
1188                 return -EINVAL;
1189         }
1190         if (new_mtu > config_mtu)
1191                 dev_warning(enic,
1192                         "MTU (%u) is greater than value configured in NIC (%u)\n",
1193                         new_mtu, config_mtu);
1194
1195         /* The easy case is when scatter is disabled. However if the MTU
1196          * becomes greater than the mbuf data size, packet drops will ensue.
1197          */
1198         if (!enic->rte_dev->data->dev_conf.rxmode.enable_scatter) {
1199                 eth_dev->data->mtu = new_mtu;
1200                 goto set_mtu_done;
1201         }
1202
1203         /* Rx scatter is enabled so reconfigure RQ's on the fly. The point is to
1204          * change Rx scatter mode if necessary for better performance. I.e. if
1205          * MTU was greater than the mbuf size and now it's less, scatter Rx
1206          * doesn't have to be used and vice versa.
1207           */
1208         rte_spinlock_lock(&enic->mtu_lock);
1209
1210         /* Stop traffic on all RQs */
1211         for (rq_idx = 0; rq_idx < enic->rq_count * 2; rq_idx++) {
1212                 rq = &enic->rq[rq_idx];
1213                 if (rq->is_sop && rq->in_use) {
1214                         rc = enic_stop_rq(enic, enic_rq_sop(rq_idx));
1215                         if (rc) {
1216                                 dev_err(enic, "Failed to stop Rq %u\n", rq_idx);
1217                                 goto set_mtu_done;
1218                         }
1219                 }
1220         }
1221
1222         /* replace Rx funciton with a no-op to avoid getting stale pkts */
1223         eth_dev->rx_pkt_burst = enic_dummy_recv_pkts;
1224         rte_mb();
1225
1226         /* Allow time for threads to exit the real Rx function. */
1227         usleep(100000);
1228
1229         /* now it is safe to reconfigure the RQs */
1230
1231         /* update the mtu */
1232         eth_dev->data->mtu = new_mtu;
1233
1234         /* free and reallocate RQs with the new MTU */
1235         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
1236                 rq = &enic->rq[enic_sop_rq(rq_idx)];
1237
1238                 enic_free_rq(rq);
1239                 rc = enic_alloc_rq(enic, rq_idx, rq->socket_id, rq->mp,
1240                                    rq->tot_nb_desc);
1241                 if (rc) {
1242                         dev_err(enic,
1243                                 "Fatal MTU alloc error- No traffic will pass\n");
1244                         goto set_mtu_done;
1245                 }
1246
1247                 rc = enic_reinit_rq(enic, rq_idx);
1248                 if (rc) {
1249                         dev_err(enic,
1250                                 "Fatal MTU RQ reinit- No traffic will pass\n");
1251                         goto set_mtu_done;
1252                 }
1253         }
1254
1255         /* put back the real receive function */
1256         rte_mb();
1257         eth_dev->rx_pkt_burst = enic_recv_pkts;
1258         rte_mb();
1259
1260         /* restart Rx traffic */
1261         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
1262                 rq = &enic->rq[enic_sop_rq(rq_idx)];
1263                 if (rq->is_sop && rq->in_use)
1264                         enic_start_rq(enic, rq_idx);
1265         }
1266
1267 set_mtu_done:
1268         dev_info(enic, "MTU changed from %u to %u\n",  old_mtu, new_mtu);
1269         rte_spinlock_unlock(&enic->mtu_lock);
1270         return rc;
1271 }
1272
1273 static int enic_dev_init(struct enic *enic)
1274 {
1275         int err;
1276         struct rte_eth_dev *eth_dev = enic->rte_dev;
1277
1278         vnic_dev_intr_coal_timer_info_default(enic->vdev);
1279
1280         /* Get vNIC configuration
1281         */
1282         err = enic_get_vnic_config(enic);
1283         if (err) {
1284                 dev_err(dev, "Get vNIC configuration failed, aborting\n");
1285                 return err;
1286         }
1287
1288         /* Get available resource counts */
1289         enic_get_res_counts(enic);
1290         if (enic->conf_rq_count == 1) {
1291                 dev_err(enic, "Running with only 1 RQ configured in the vNIC is not supported.\n");
1292                 dev_err(enic, "Please configure 2 RQs in the vNIC for each Rx queue used by DPDK.\n");
1293                 dev_err(enic, "See the ENIC PMD guide for more information.\n");
1294                 return -EINVAL;
1295         }
1296
1297         /* Get the supported filters */
1298         enic_fdir_info(enic);
1299
1300         eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr", ETH_ALEN, 0);
1301         if (!eth_dev->data->mac_addrs) {
1302                 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
1303                 return -1;
1304         }
1305         ether_addr_copy((struct ether_addr *) enic->mac_addr,
1306                 &eth_dev->data->mac_addrs[0]);
1307
1308         vnic_dev_set_reset_flag(enic->vdev, 0);
1309
1310         /* set up link status checking */
1311         vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
1312
1313         return 0;
1314
1315 }
1316
1317 int enic_probe(struct enic *enic)
1318 {
1319         struct rte_pci_device *pdev = enic->pdev;
1320         int err = -1;
1321
1322         dev_debug(enic, " Initializing ENIC PMD\n");
1323
1324         enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
1325         enic->bar0.len = pdev->mem_resource[0].len;
1326
1327         /* Register vNIC device */
1328         enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
1329         if (!enic->vdev) {
1330                 dev_err(enic, "vNIC registration failed, aborting\n");
1331                 goto err_out;
1332         }
1333
1334         LIST_INIT(&enic->memzone_list);
1335         rte_spinlock_init(&enic->memzone_list_lock);
1336
1337         vnic_register_cbacks(enic->vdev,
1338                 enic_alloc_consistent,
1339                 enic_free_consistent);
1340
1341         /* Issue device open to get device in known state */
1342         err = enic_dev_open(enic);
1343         if (err) {
1344                 dev_err(enic, "vNIC dev open failed, aborting\n");
1345                 goto err_out_unregister;
1346         }
1347
1348         /* Set ingress vlan rewrite mode before vnic initialization */
1349         err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1350                 IG_VLAN_REWRITE_MODE_PASS_THRU);
1351         if (err) {
1352                 dev_err(enic,
1353                         "Failed to set ingress vlan rewrite mode, aborting.\n");
1354                 goto err_out_dev_close;
1355         }
1356
1357         /* Issue device init to initialize the vnic-to-switch link.
1358          * We'll start with carrier off and wait for link UP
1359          * notification later to turn on carrier.  We don't need
1360          * to wait here for the vnic-to-switch link initialization
1361          * to complete; link UP notification is the indication that
1362          * the process is complete.
1363          */
1364
1365         err = vnic_dev_init(enic->vdev, 0);
1366         if (err) {
1367                 dev_err(enic, "vNIC dev init failed, aborting\n");
1368                 goto err_out_dev_close;
1369         }
1370
1371         err = enic_dev_init(enic);
1372         if (err) {
1373                 dev_err(enic, "Device initialization failed, aborting\n");
1374                 goto err_out_dev_close;
1375         }
1376
1377         return 0;
1378
1379 err_out_dev_close:
1380         vnic_dev_close(enic->vdev);
1381 err_out_unregister:
1382         vnic_dev_unregister(enic->vdev);
1383 err_out:
1384         return err;
1385 }
1386
1387 void enic_remove(struct enic *enic)
1388 {
1389         enic_dev_deinit(enic);
1390         vnic_dev_close(enic->vdev);
1391         vnic_dev_unregister(enic->vdev);
1392 }