Imported Upstream version 16.11
[deb_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_rte_rq_idx_to_sop_idx(index));
244
245                 vnic_rq_init(&enic->rq[enic_rte_rq_idx_to_sop_idx(index)],
246                         cq_idx,
247                         error_interrupt_enable,
248                         error_interrupt_offset);
249
250                 data_rq = &enic->rq[enic_rte_rq_idx_to_data_idx(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_rte_rq_idx_to_sop_idx(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_rte_rq_idx_to_data_idx(index)]);
471                 if (err) {
472                         /* release the allocated mbufs for the sop rq*/
473                         enic_rxmbuf_queue_release(enic,
474                                 &enic->rq[enic_rte_rq_idx_to_sop_idx(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[enic_sop_rq_idx_to_cq_idx(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;
572         struct vnic_rq *rq_data;
573         rq_sop = &enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
574         rq_data = &enic->rq[rq_sop->data_queue_idx];
575         struct rte_eth_dev *eth_dev = enic->rte_dev;
576
577         if (rq_data->in_use)
578                 vnic_rq_enable(rq_data);
579         rte_mb();
580         vnic_rq_enable(rq_sop);
581         eth_dev->data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STARTED;
582 }
583
584 int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
585 {
586         int ret1 = 0, ret2 = 0;
587         struct rte_eth_dev *eth_dev = enic->rte_dev;
588         struct vnic_rq *rq_sop;
589         struct vnic_rq *rq_data;
590         rq_sop = &enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
591         rq_data = &enic->rq[rq_sop->data_queue_idx];
592
593         ret2 = vnic_rq_disable(rq_sop);
594         rte_mb();
595         if (rq_data->in_use)
596                 ret1 = vnic_rq_disable(rq_data);
597
598         if (ret2)
599                 return ret2;
600         else if (ret1)
601                 return ret1;
602
603         eth_dev->data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
604         return 0;
605 }
606
607 int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
608         unsigned int socket_id, struct rte_mempool *mp,
609         uint16_t nb_desc, uint16_t free_thresh)
610 {
611         int rc;
612         uint16_t sop_queue_idx = enic_rte_rq_idx_to_sop_idx(queue_idx);
613         uint16_t data_queue_idx = enic_rte_rq_idx_to_data_idx(queue_idx);
614         struct vnic_rq *rq_sop = &enic->rq[sop_queue_idx];
615         struct vnic_rq *rq_data = &enic->rq[data_queue_idx];
616         unsigned int mbuf_size, mbufs_per_pkt;
617         unsigned int nb_sop_desc, nb_data_desc;
618         uint16_t min_sop, max_sop, min_data, max_data;
619         uint16_t mtu = enic->rte_dev->data->mtu;
620
621         rq_sop->is_sop = 1;
622         rq_sop->data_queue_idx = data_queue_idx;
623         rq_data->is_sop = 0;
624         rq_data->data_queue_idx = 0;
625         rq_sop->socket_id = socket_id;
626         rq_sop->mp = mp;
627         rq_data->socket_id = socket_id;
628         rq_data->mp = mp;
629         rq_sop->in_use = 1;
630         rq_sop->rx_free_thresh = free_thresh;
631         rq_data->rx_free_thresh = free_thresh;
632         dev_debug(enic, "Set queue_id:%u free thresh:%u\n", queue_idx,
633                   free_thresh);
634
635         mbuf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
636                                RTE_PKTMBUF_HEADROOM);
637
638         if (enic->rte_dev->data->dev_conf.rxmode.enable_scatter) {
639                 dev_info(enic, "Rq %u Scatter rx mode enabled\n", queue_idx);
640                 /* ceil((mtu + ETHER_HDR_LEN + 4)/mbuf_size) */
641                 mbufs_per_pkt = ((mtu + ETHER_HDR_LEN + 4) +
642                                  (mbuf_size - 1)) / mbuf_size;
643         } else {
644                 dev_info(enic, "Scatter rx mode disabled\n");
645                 mbufs_per_pkt = 1;
646         }
647
648         if (mbufs_per_pkt > 1) {
649                 dev_info(enic, "Rq %u Scatter rx mode in use\n", queue_idx);
650                 rq_sop->data_queue_enable = 1;
651                 rq_data->in_use = 1;
652         } else {
653                 dev_info(enic, "Rq %u Scatter rx mode not being used\n",
654                          queue_idx);
655                 rq_sop->data_queue_enable = 0;
656                 rq_data->in_use = 0;
657         }
658
659         /* number of descriptors have to be a multiple of 32 */
660         nb_sop_desc = (nb_desc / mbufs_per_pkt) & ~0x1F;
661         nb_data_desc = (nb_desc - nb_sop_desc) & ~0x1F;
662
663         rq_sop->max_mbufs_per_pkt = mbufs_per_pkt;
664         rq_data->max_mbufs_per_pkt = mbufs_per_pkt;
665
666         if (mbufs_per_pkt > 1) {
667                 min_sop = 64;
668                 max_sop = ((enic->config.rq_desc_count /
669                             (mbufs_per_pkt - 1)) & ~0x1F);
670                 min_data = min_sop * (mbufs_per_pkt - 1);
671                 max_data = enic->config.rq_desc_count;
672         } else {
673                 min_sop = 64;
674                 max_sop = enic->config.rq_desc_count;
675                 min_data = 0;
676                 max_data = 0;
677         }
678
679         if (nb_desc < (min_sop + min_data)) {
680                 dev_warning(enic,
681                             "Number of rx descs too low, adjusting to minimum\n");
682                 nb_sop_desc = min_sop;
683                 nb_data_desc = min_data;
684         } else if (nb_desc > (max_sop + max_data)) {
685                 dev_warning(enic,
686                             "Number of rx_descs too high, adjusting to maximum\n");
687                 nb_sop_desc = max_sop;
688                 nb_data_desc = max_data;
689         }
690         if (mbufs_per_pkt > 1) {
691                 dev_info(enic, "For mtu %d and mbuf size %d valid rx descriptor range is %d to %d\n",
692                          mtu, mbuf_size, min_sop + min_data,
693                          max_sop + max_data);
694         }
695         dev_info(enic, "Using %d rx descriptors (sop %d, data %d)\n",
696                  nb_sop_desc + nb_data_desc, nb_sop_desc, nb_data_desc);
697
698         /* Allocate sop queue resources */
699         rc = vnic_rq_alloc(enic->vdev, rq_sop, sop_queue_idx,
700                 nb_sop_desc, sizeof(struct rq_enet_desc));
701         if (rc) {
702                 dev_err(enic, "error in allocation of sop rq\n");
703                 goto err_exit;
704         }
705         nb_sop_desc = rq_sop->ring.desc_count;
706
707         if (rq_data->in_use) {
708                 /* Allocate data queue resources */
709                 rc = vnic_rq_alloc(enic->vdev, rq_data, data_queue_idx,
710                                    nb_data_desc,
711                                    sizeof(struct rq_enet_desc));
712                 if (rc) {
713                         dev_err(enic, "error in allocation of data rq\n");
714                         goto err_free_rq_sop;
715                 }
716                 nb_data_desc = rq_data->ring.desc_count;
717         }
718         rc = vnic_cq_alloc(enic->vdev, &enic->cq[queue_idx], queue_idx,
719                            socket_id, nb_sop_desc + nb_data_desc,
720                            sizeof(struct cq_enet_rq_desc));
721         if (rc) {
722                 dev_err(enic, "error in allocation of cq for rq\n");
723                 goto err_free_rq_data;
724         }
725
726         /* Allocate the mbuf rings */
727         rq_sop->mbuf_ring = (struct rte_mbuf **)
728                 rte_zmalloc_socket("rq->mbuf_ring",
729                                    sizeof(struct rte_mbuf *) * nb_sop_desc,
730                                    RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
731         if (rq_sop->mbuf_ring == NULL)
732                 goto err_free_cq;
733
734         if (rq_data->in_use) {
735                 rq_data->mbuf_ring = (struct rte_mbuf **)
736                         rte_zmalloc_socket("rq->mbuf_ring",
737                                 sizeof(struct rte_mbuf *) * nb_data_desc,
738                                 RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
739                 if (rq_data->mbuf_ring == NULL)
740                         goto err_free_sop_mbuf;
741         }
742
743         rq_sop->tot_nb_desc = nb_desc; /* squirl away for MTU update function */
744
745         return 0;
746
747 err_free_sop_mbuf:
748         rte_free(rq_sop->mbuf_ring);
749 err_free_cq:
750         /* cleanup on error */
751         vnic_cq_free(&enic->cq[queue_idx]);
752 err_free_rq_data:
753         if (rq_data->in_use)
754                 vnic_rq_free(rq_data);
755 err_free_rq_sop:
756         vnic_rq_free(rq_sop);
757 err_exit:
758         return -ENOMEM;
759 }
760
761 void enic_free_wq(void *txq)
762 {
763         struct vnic_wq *wq;
764         struct enic *enic;
765
766         if (txq == NULL)
767                 return;
768
769         wq = (struct vnic_wq *)txq;
770         enic = vnic_dev_priv(wq->vdev);
771         rte_memzone_free(wq->cqmsg_rz);
772         vnic_wq_free(wq);
773         vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
774 }
775
776 int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
777         unsigned int socket_id, uint16_t nb_desc)
778 {
779         int err;
780         struct vnic_wq *wq = &enic->wq[queue_idx];
781         unsigned int cq_index = enic_cq_wq(enic, queue_idx);
782         char name[NAME_MAX];
783         static int instance;
784
785         wq->socket_id = socket_id;
786         if (nb_desc) {
787                 if (nb_desc > enic->config.wq_desc_count) {
788                         dev_warning(enic,
789                                 "WQ %d - number of tx desc in cmd line (%d)"\
790                                 "is greater than that in the UCSM/CIMC adapter"\
791                                 "policy.  Applying the value in the adapter "\
792                                 "policy (%d)\n",
793                                 queue_idx, nb_desc, enic->config.wq_desc_count);
794                 } else if (nb_desc != enic->config.wq_desc_count) {
795                         enic->config.wq_desc_count = nb_desc;
796                         dev_info(enic,
797                                 "TX Queues - effective number of descs:%d\n",
798                                 nb_desc);
799                 }
800         }
801
802         /* Allocate queue resources */
803         err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
804                 enic->config.wq_desc_count,
805                 sizeof(struct wq_enet_desc));
806         if (err) {
807                 dev_err(enic, "error in allocation of wq\n");
808                 return err;
809         }
810
811         err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
812                 socket_id, enic->config.wq_desc_count,
813                 sizeof(struct cq_enet_wq_desc));
814         if (err) {
815                 vnic_wq_free(wq);
816                 dev_err(enic, "error in allocation of cq for wq\n");
817         }
818
819         /* setup up CQ message */
820         snprintf((char *)name, sizeof(name),
821                  "vnic_cqmsg-%s-%d-%d", enic->bdf_name, queue_idx,
822                 instance++);
823
824         wq->cqmsg_rz = rte_memzone_reserve_aligned((const char *)name,
825                                                    sizeof(uint32_t),
826                                                    SOCKET_ID_ANY, 0,
827                                                    ENIC_ALIGN);
828         if (!wq->cqmsg_rz)
829                 return -ENOMEM;
830
831         return err;
832 }
833
834 int enic_disable(struct enic *enic)
835 {
836         unsigned int i;
837         int err;
838
839         vnic_intr_mask(&enic->intr);
840         (void)vnic_intr_masked(&enic->intr); /* flush write */
841         rte_intr_disable(&enic->pdev->intr_handle);
842         rte_intr_callback_unregister(&enic->pdev->intr_handle,
843                                      enic_intr_handler,
844                                      (void *)enic->rte_dev);
845
846         vnic_dev_disable(enic->vdev);
847
848         enic_clsf_destroy(enic);
849
850         if (!enic_is_sriov_vf(enic))
851                 vnic_dev_del_addr(enic->vdev, enic->mac_addr);
852
853         for (i = 0; i < enic->wq_count; i++) {
854                 err = vnic_wq_disable(&enic->wq[i]);
855                 if (err)
856                         return err;
857         }
858         for (i = 0; i < enic_vnic_rq_count(enic); i++) {
859                 if (enic->rq[i].in_use) {
860                         err = vnic_rq_disable(&enic->rq[i]);
861                         if (err)
862                                 return err;
863                 }
864         }
865
866         /* If we were using interrupts, set the interrupt vector to -1
867          * to disable interrupts.  We are not disabling link notifcations,
868          * though, as we want the polling of link status to continue working.
869          */
870         if (enic->rte_dev->data->dev_conf.intr_conf.lsc)
871                 vnic_dev_notify_set(enic->vdev, -1);
872
873         vnic_dev_set_reset_flag(enic->vdev, 1);
874
875         for (i = 0; i < enic->wq_count; i++)
876                 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
877
878         for (i = 0; i < enic_vnic_rq_count(enic); i++)
879                 if (enic->rq[i].in_use)
880                         vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
881         for (i = 0; i < enic->cq_count; i++)
882                 vnic_cq_clean(&enic->cq[i]);
883         vnic_intr_clean(&enic->intr);
884
885         return 0;
886 }
887
888 static int enic_dev_wait(struct vnic_dev *vdev,
889         int (*start)(struct vnic_dev *, int),
890         int (*finished)(struct vnic_dev *, int *),
891         int arg)
892 {
893         int done;
894         int err;
895         int i;
896
897         err = start(vdev, arg);
898         if (err)
899                 return err;
900
901         /* Wait for func to complete...2 seconds max */
902         for (i = 0; i < 2000; i++) {
903                 err = finished(vdev, &done);
904                 if (err)
905                         return err;
906                 if (done)
907                         return 0;
908                 usleep(1000);
909         }
910         return -ETIMEDOUT;
911 }
912
913 static int enic_dev_open(struct enic *enic)
914 {
915         int err;
916
917         err = enic_dev_wait(enic->vdev, vnic_dev_open,
918                 vnic_dev_open_done, 0);
919         if (err)
920                 dev_err(enic_get_dev(enic),
921                         "vNIC device open failed, err %d\n", err);
922
923         return err;
924 }
925
926 static int enic_set_rsskey(struct enic *enic)
927 {
928         dma_addr_t rss_key_buf_pa;
929         union vnic_rss_key *rss_key_buf_va = NULL;
930         static union vnic_rss_key rss_key = {
931                 .key = {
932                         [0] = {.b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101}},
933                         [1] = {.b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101}},
934                         [2] = {.b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115}},
935                         [3] = {.b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108}},
936                 }
937         };
938         int err;
939         u8 name[NAME_MAX];
940
941         snprintf((char *)name, NAME_MAX, "rss_key-%s", enic->bdf_name);
942         rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
943                 &rss_key_buf_pa, name);
944         if (!rss_key_buf_va)
945                 return -ENOMEM;
946
947         rte_memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
948
949         err = enic_set_rss_key(enic,
950                 rss_key_buf_pa,
951                 sizeof(union vnic_rss_key));
952
953         enic_free_consistent(enic, sizeof(union vnic_rss_key),
954                 rss_key_buf_va, rss_key_buf_pa);
955
956         return err;
957 }
958
959 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
960 {
961         dma_addr_t rss_cpu_buf_pa;
962         union vnic_rss_cpu *rss_cpu_buf_va = NULL;
963         int i;
964         int err;
965         u8 name[NAME_MAX];
966
967         snprintf((char *)name, NAME_MAX, "rss_cpu-%s", enic->bdf_name);
968         rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
969                 &rss_cpu_buf_pa, name);
970         if (!rss_cpu_buf_va)
971                 return -ENOMEM;
972
973         for (i = 0; i < (1 << rss_hash_bits); i++)
974                 (*rss_cpu_buf_va).cpu[i / 4].b[i % 4] =
975                         enic_rte_rq_idx_to_sop_idx(i % enic->rq_count);
976
977         err = enic_set_rss_cpu(enic,
978                 rss_cpu_buf_pa,
979                 sizeof(union vnic_rss_cpu));
980
981         enic_free_consistent(enic, sizeof(union vnic_rss_cpu),
982                 rss_cpu_buf_va, rss_cpu_buf_pa);
983
984         return err;
985 }
986
987 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
988         u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
989 {
990         const u8 tso_ipid_split_en = 0;
991         int err;
992
993         /* Enable VLAN tag stripping */
994
995         err = enic_set_nic_cfg(enic,
996                 rss_default_cpu, rss_hash_type,
997                 rss_hash_bits, rss_base_cpu,
998                 rss_enable, tso_ipid_split_en,
999                 enic->ig_vlan_strip_en);
1000
1001         return err;
1002 }
1003
1004 int enic_set_rss_nic_cfg(struct enic *enic)
1005 {
1006         const u8 rss_default_cpu = 0;
1007         const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1008             NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1009             NIC_CFG_RSS_HASH_TYPE_IPV6 |
1010             NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1011         const u8 rss_hash_bits = 7;
1012         const u8 rss_base_cpu = 0;
1013         u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1014
1015         if (rss_enable) {
1016                 if (!enic_set_rsskey(enic)) {
1017                         if (enic_set_rsscpu(enic, rss_hash_bits)) {
1018                                 rss_enable = 0;
1019                                 dev_warning(enic, "RSS disabled, "\
1020                                         "Failed to set RSS cpu indirection table.");
1021                         }
1022                 } else {
1023                         rss_enable = 0;
1024                         dev_warning(enic,
1025                                 "RSS disabled, Failed to set RSS key.\n");
1026                 }
1027         }
1028
1029         return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1030                 rss_hash_bits, rss_base_cpu, rss_enable);
1031 }
1032
1033 int enic_setup_finish(struct enic *enic)
1034 {
1035         int ret;
1036
1037         enic_init_soft_stats(enic);
1038
1039         ret = enic_set_rss_nic_cfg(enic);
1040         if (ret) {
1041                 dev_err(enic, "Failed to config nic, aborting.\n");
1042                 return -1;
1043         }
1044
1045         /* Default conf */
1046         vnic_dev_packet_filter(enic->vdev,
1047                 1 /* directed  */,
1048                 1 /* multicast */,
1049                 1 /* broadcast */,
1050                 0 /* promisc   */,
1051                 1 /* allmulti  */);
1052
1053         enic->promisc = 0;
1054         enic->allmulti = 1;
1055
1056         return 0;
1057 }
1058
1059 void enic_add_packet_filter(struct enic *enic)
1060 {
1061         /* Args -> directed, multicast, broadcast, promisc, allmulti */
1062         vnic_dev_packet_filter(enic->vdev, 1, 1, 1,
1063                 enic->promisc, enic->allmulti);
1064 }
1065
1066 int enic_get_link_status(struct enic *enic)
1067 {
1068         return vnic_dev_link_status(enic->vdev);
1069 }
1070
1071 static void enic_dev_deinit(struct enic *enic)
1072 {
1073         struct rte_eth_dev *eth_dev = enic->rte_dev;
1074
1075         /* stop link status checking */
1076         vnic_dev_notify_unset(enic->vdev);
1077
1078         rte_free(eth_dev->data->mac_addrs);
1079 }
1080
1081
1082 int enic_set_vnic_res(struct enic *enic)
1083 {
1084         struct rte_eth_dev *eth_dev = enic->rte_dev;
1085         int rc = 0;
1086
1087         /* With Rx scatter support, two RQs are now used per RQ used by
1088          * the application.
1089          */
1090         if (enic->conf_rq_count < eth_dev->data->nb_rx_queues) {
1091                 dev_err(dev, "Not enough Receive queues. Requested:%u which uses %d RQs on VIC, Configured:%u\n",
1092                         eth_dev->data->nb_rx_queues,
1093                         eth_dev->data->nb_rx_queues * 2, enic->conf_rq_count);
1094                 rc = -EINVAL;
1095         }
1096         if (enic->conf_wq_count < eth_dev->data->nb_tx_queues) {
1097                 dev_err(dev, "Not enough Transmit queues. Requested:%u, Configured:%u\n",
1098                         eth_dev->data->nb_tx_queues, enic->conf_wq_count);
1099                 rc = -EINVAL;
1100         }
1101
1102         if (enic->conf_cq_count < (eth_dev->data->nb_rx_queues +
1103                                    eth_dev->data->nb_tx_queues)) {
1104                 dev_err(dev, "Not enough Completion queues. Required:%u, Configured:%u\n",
1105                         (eth_dev->data->nb_rx_queues +
1106                          eth_dev->data->nb_tx_queues), enic->conf_cq_count);
1107                 rc = -EINVAL;
1108         }
1109
1110         if (rc == 0) {
1111                 enic->rq_count = eth_dev->data->nb_rx_queues;
1112                 enic->wq_count = eth_dev->data->nb_tx_queues;
1113                 enic->cq_count = enic->rq_count + enic->wq_count;
1114         }
1115
1116         return rc;
1117 }
1118
1119 /* Initialize the completion queue for an RQ */
1120 static int
1121 enic_reinit_rq(struct enic *enic, unsigned int rq_idx)
1122 {
1123         struct vnic_rq *sop_rq, *data_rq;
1124         unsigned int cq_idx = enic_cq_rq(enic, rq_idx);
1125         int rc = 0;
1126
1127         sop_rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
1128         data_rq = &enic->rq[enic_rte_rq_idx_to_data_idx(rq_idx)];
1129
1130         vnic_cq_clean(&enic->cq[cq_idx]);
1131         vnic_cq_init(&enic->cq[cq_idx],
1132                      0 /* flow_control_enable */,
1133                      1 /* color_enable */,
1134                      0 /* cq_head */,
1135                      0 /* cq_tail */,
1136                      1 /* cq_tail_color */,
1137                      0 /* interrupt_enable */,
1138                      1 /* cq_entry_enable */,
1139                      0 /* cq_message_enable */,
1140                      0 /* interrupt offset */,
1141                      0 /* cq_message_addr */);
1142
1143
1144         vnic_rq_init_start(sop_rq, enic_cq_rq(enic,
1145                            enic_rte_rq_idx_to_sop_idx(rq_idx)), 0,
1146                            sop_rq->ring.desc_count - 1, 1, 0);
1147         if (data_rq->in_use) {
1148                 vnic_rq_init_start(data_rq,
1149                                    enic_cq_rq(enic,
1150                                    enic_rte_rq_idx_to_data_idx(rq_idx)), 0,
1151                                    data_rq->ring.desc_count - 1, 1, 0);
1152         }
1153
1154         rc = enic_alloc_rx_queue_mbufs(enic, sop_rq);
1155         if (rc)
1156                 return rc;
1157
1158         if (data_rq->in_use) {
1159                 rc = enic_alloc_rx_queue_mbufs(enic, data_rq);
1160                 if (rc) {
1161                         enic_rxmbuf_queue_release(enic, sop_rq);
1162                         return rc;
1163                 }
1164         }
1165
1166         return 0;
1167 }
1168
1169 /* The Cisco NIC can send and receive packets up to a max packet size
1170  * determined by the NIC type and firmware. There is also an MTU
1171  * configured into the NIC via the CIMC/UCSM management interface
1172  * which can be overridden by this function (up to the max packet size).
1173  * Depending on the network setup, doing so may cause packet drops
1174  * and unexpected behavior.
1175  */
1176 int enic_set_mtu(struct enic *enic, uint16_t new_mtu)
1177 {
1178         unsigned int rq_idx;
1179         struct vnic_rq *rq;
1180         int rc = 0;
1181         uint16_t old_mtu;       /* previous setting */
1182         uint16_t config_mtu;    /* Value configured into NIC via CIMC/UCSM */
1183         struct rte_eth_dev *eth_dev = enic->rte_dev;
1184
1185         old_mtu = eth_dev->data->mtu;
1186         config_mtu = enic->config.mtu;
1187
1188         if (new_mtu > enic->max_mtu) {
1189                 dev_err(enic,
1190                         "MTU not updated: requested (%u) greater than max (%u)\n",
1191                         new_mtu, enic->max_mtu);
1192                 return -EINVAL;
1193         }
1194         if (new_mtu < ENIC_MIN_MTU) {
1195                 dev_info(enic,
1196                         "MTU not updated: requested (%u) less than min (%u)\n",
1197                         new_mtu, ENIC_MIN_MTU);
1198                 return -EINVAL;
1199         }
1200         if (new_mtu > config_mtu)
1201                 dev_warning(enic,
1202                         "MTU (%u) is greater than value configured in NIC (%u)\n",
1203                         new_mtu, config_mtu);
1204
1205         /* The easy case is when scatter is disabled. However if the MTU
1206          * becomes greater than the mbuf data size, packet drops will ensue.
1207          */
1208         if (!enic->rte_dev->data->dev_conf.rxmode.enable_scatter) {
1209                 eth_dev->data->mtu = new_mtu;
1210                 goto set_mtu_done;
1211         }
1212
1213         /* Rx scatter is enabled so reconfigure RQ's on the fly. The point is to
1214          * change Rx scatter mode if necessary for better performance. I.e. if
1215          * MTU was greater than the mbuf size and now it's less, scatter Rx
1216          * doesn't have to be used and vice versa.
1217           */
1218         rte_spinlock_lock(&enic->mtu_lock);
1219
1220         /* Stop traffic on all RQs */
1221         for (rq_idx = 0; rq_idx < enic->rq_count * 2; rq_idx++) {
1222                 rq = &enic->rq[rq_idx];
1223                 if (rq->is_sop && rq->in_use) {
1224                         rc = enic_stop_rq(enic,
1225                                           enic_sop_rq_idx_to_rte_idx(rq_idx));
1226                         if (rc) {
1227                                 dev_err(enic, "Failed to stop Rq %u\n", rq_idx);
1228                                 goto set_mtu_done;
1229                         }
1230                 }
1231         }
1232
1233         /* replace Rx funciton with a no-op to avoid getting stale pkts */
1234         eth_dev->rx_pkt_burst = enic_dummy_recv_pkts;
1235         rte_mb();
1236
1237         /* Allow time for threads to exit the real Rx function. */
1238         usleep(100000);
1239
1240         /* now it is safe to reconfigure the RQs */
1241
1242         /* update the mtu */
1243         eth_dev->data->mtu = new_mtu;
1244
1245         /* free and reallocate RQs with the new MTU */
1246         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
1247                 rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
1248
1249                 enic_free_rq(rq);
1250                 rc = enic_alloc_rq(enic, rq_idx, rq->socket_id, rq->mp,
1251                                    rq->tot_nb_desc, rq->rx_free_thresh);
1252                 if (rc) {
1253                         dev_err(enic,
1254                                 "Fatal MTU alloc error- No traffic will pass\n");
1255                         goto set_mtu_done;
1256                 }
1257
1258                 rc = enic_reinit_rq(enic, rq_idx);
1259                 if (rc) {
1260                         dev_err(enic,
1261                                 "Fatal MTU RQ reinit- No traffic will pass\n");
1262                         goto set_mtu_done;
1263                 }
1264         }
1265
1266         /* put back the real receive function */
1267         rte_mb();
1268         eth_dev->rx_pkt_burst = enic_recv_pkts;
1269         rte_mb();
1270
1271         /* restart Rx traffic */
1272         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
1273                 rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
1274                 if (rq->is_sop && rq->in_use)
1275                         enic_start_rq(enic, rq_idx);
1276         }
1277
1278 set_mtu_done:
1279         dev_info(enic, "MTU changed from %u to %u\n",  old_mtu, new_mtu);
1280         rte_spinlock_unlock(&enic->mtu_lock);
1281         return rc;
1282 }
1283
1284 static int enic_dev_init(struct enic *enic)
1285 {
1286         int err;
1287         struct rte_eth_dev *eth_dev = enic->rte_dev;
1288
1289         vnic_dev_intr_coal_timer_info_default(enic->vdev);
1290
1291         /* Get vNIC configuration
1292         */
1293         err = enic_get_vnic_config(enic);
1294         if (err) {
1295                 dev_err(dev, "Get vNIC configuration failed, aborting\n");
1296                 return err;
1297         }
1298
1299         /* Get available resource counts */
1300         enic_get_res_counts(enic);
1301         if (enic->conf_rq_count == 1) {
1302                 dev_err(enic, "Running with only 1 RQ configured in the vNIC is not supported.\n");
1303                 dev_err(enic, "Please configure 2 RQs in the vNIC for each Rx queue used by DPDK.\n");
1304                 dev_err(enic, "See the ENIC PMD guide for more information.\n");
1305                 return -EINVAL;
1306         }
1307
1308         /* Get the supported filters */
1309         enic_fdir_info(enic);
1310
1311         eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr", ETH_ALEN, 0);
1312         if (!eth_dev->data->mac_addrs) {
1313                 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
1314                 return -1;
1315         }
1316         ether_addr_copy((struct ether_addr *) enic->mac_addr,
1317                 &eth_dev->data->mac_addrs[0]);
1318
1319         vnic_dev_set_reset_flag(enic->vdev, 0);
1320
1321         /* set up link status checking */
1322         vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
1323
1324         return 0;
1325
1326 }
1327
1328 int enic_probe(struct enic *enic)
1329 {
1330         struct rte_pci_device *pdev = enic->pdev;
1331         int err = -1;
1332
1333         dev_debug(enic, " Initializing ENIC PMD\n");
1334
1335         enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
1336         enic->bar0.len = pdev->mem_resource[0].len;
1337
1338         /* Register vNIC device */
1339         enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
1340         if (!enic->vdev) {
1341                 dev_err(enic, "vNIC registration failed, aborting\n");
1342                 goto err_out;
1343         }
1344
1345         LIST_INIT(&enic->memzone_list);
1346         rte_spinlock_init(&enic->memzone_list_lock);
1347
1348         vnic_register_cbacks(enic->vdev,
1349                 enic_alloc_consistent,
1350                 enic_free_consistent);
1351
1352         /* Issue device open to get device in known state */
1353         err = enic_dev_open(enic);
1354         if (err) {
1355                 dev_err(enic, "vNIC dev open failed, aborting\n");
1356                 goto err_out_unregister;
1357         }
1358
1359         /* Set ingress vlan rewrite mode before vnic initialization */
1360         err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1361                 IG_VLAN_REWRITE_MODE_PASS_THRU);
1362         if (err) {
1363                 dev_err(enic,
1364                         "Failed to set ingress vlan rewrite mode, aborting.\n");
1365                 goto err_out_dev_close;
1366         }
1367
1368         /* Issue device init to initialize the vnic-to-switch link.
1369          * We'll start with carrier off and wait for link UP
1370          * notification later to turn on carrier.  We don't need
1371          * to wait here for the vnic-to-switch link initialization
1372          * to complete; link UP notification is the indication that
1373          * the process is complete.
1374          */
1375
1376         err = vnic_dev_init(enic->vdev, 0);
1377         if (err) {
1378                 dev_err(enic, "vNIC dev init failed, aborting\n");
1379                 goto err_out_dev_close;
1380         }
1381
1382         err = enic_dev_init(enic);
1383         if (err) {
1384                 dev_err(enic, "Device initialization failed, aborting\n");
1385                 goto err_out_dev_close;
1386         }
1387
1388         return 0;
1389
1390 err_out_dev_close:
1391         vnic_dev_close(enic->vdev);
1392 err_out_unregister:
1393         vnic_dev_unregister(enic->vdev);
1394 err_out:
1395         return err;
1396 }
1397
1398 void enic_remove(struct enic *enic)
1399 {
1400         enic_dev_deinit(enic);
1401         vnic_dev_close(enic->vdev);
1402         vnic_dev_unregister(enic->vdev);
1403 }