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