1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2018 Chelsio Communications.
14 #include <netinet/in.h>
16 #include <rte_byteorder.h>
17 #include <rte_common.h>
18 #include <rte_cycles.h>
19 #include <rte_interrupts.h>
21 #include <rte_debug.h>
23 #include <rte_bus_pci.h>
24 #include <rte_atomic.h>
25 #include <rte_branch_prediction.h>
26 #include <rte_memory.h>
27 #include <rte_tailq.h>
29 #include <rte_alarm.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev_driver.h>
32 #include <rte_ethdev_pci.h>
33 #include <rte_malloc.h>
34 #include <rte_random.h>
38 #include "cxgbe_pfvf.h"
39 #include "cxgbe_flow.h"
44 * Macros needed to support the PCI Device ID Table ...
46 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
47 static const struct rte_pci_id cxgb4_pci_tbl[] = {
48 #define CH_PCI_DEVICE_ID_FUNCTION 0x4
50 #define PCI_VENDOR_ID_CHELSIO 0x1425
52 #define CH_PCI_ID_TABLE_ENTRY(devid) \
53 { RTE_PCI_DEVICE(PCI_VENDOR_ID_CHELSIO, (devid)) }
55 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
60 *... and the PCI ID Table itself ...
62 #include "base/t4_pci_id_tbl.h"
64 uint16_t cxgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
67 struct sge_eth_txq *txq = (struct sge_eth_txq *)tx_queue;
68 uint16_t pkts_sent, pkts_remain;
69 uint16_t total_sent = 0;
72 CXGBE_DEBUG_TX(adapter, "%s: txq = %p; tx_pkts = %p; nb_pkts = %d\n",
73 __func__, txq, tx_pkts, nb_pkts);
75 t4_os_lock(&txq->txq_lock);
76 /* free up desc from already completed tx */
77 reclaim_completed_tx(&txq->q);
78 while (total_sent < nb_pkts) {
79 pkts_remain = nb_pkts - total_sent;
81 for (pkts_sent = 0; pkts_sent < pkts_remain; pkts_sent++) {
82 ret = t4_eth_xmit(txq, tx_pkts[total_sent + pkts_sent],
89 total_sent += pkts_sent;
90 /* reclaim as much as possible */
91 reclaim_completed_tx(&txq->q);
94 t4_os_unlock(&txq->txq_lock);
98 uint16_t cxgbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
101 struct sge_eth_rxq *rxq = (struct sge_eth_rxq *)rx_queue;
102 unsigned int work_done;
104 CXGBE_DEBUG_RX(adapter, "%s: rxq->rspq.cntxt_id = %u; nb_pkts = %d\n",
105 __func__, rxq->rspq.cntxt_id, nb_pkts);
107 if (cxgbe_poll(&rxq->rspq, rx_pkts, (unsigned int)nb_pkts, &work_done))
108 dev_err(adapter, "error in cxgbe poll\n");
110 CXGBE_DEBUG_RX(adapter, "%s: work_done = %u\n", __func__, work_done);
114 void cxgbe_dev_info_get(struct rte_eth_dev *eth_dev,
115 struct rte_eth_dev_info *device_info)
117 struct port_info *pi = eth_dev->data->dev_private;
118 struct adapter *adapter = pi->adapter;
119 int max_queues = adapter->sge.max_ethqsets / adapter->params.nports;
121 static const struct rte_eth_desc_lim cxgbe_desc_lim = {
122 .nb_max = CXGBE_MAX_RING_DESC_SIZE,
123 .nb_min = CXGBE_MIN_RING_DESC_SIZE,
127 device_info->min_rx_bufsize = CXGBE_MIN_RX_BUFSIZE;
128 device_info->max_rx_pktlen = CXGBE_MAX_RX_PKTLEN;
129 device_info->max_rx_queues = max_queues;
130 device_info->max_tx_queues = max_queues;
131 device_info->max_mac_addrs = 1;
132 /* XXX: For now we support one MAC/port */
133 device_info->max_vfs = adapter->params.arch.vfcount;
134 device_info->max_vmdq_pools = 0; /* XXX: For now no support for VMDQ */
136 device_info->rx_queue_offload_capa = 0UL;
137 device_info->rx_offload_capa = CXGBE_RX_OFFLOADS;
139 device_info->tx_queue_offload_capa = 0UL;
140 device_info->tx_offload_capa = CXGBE_TX_OFFLOADS;
142 device_info->reta_size = pi->rss_size;
143 device_info->hash_key_size = CXGBE_DEFAULT_RSS_KEY_LEN;
144 device_info->flow_type_rss_offloads = CXGBE_RSS_HF_ALL;
146 device_info->rx_desc_lim = cxgbe_desc_lim;
147 device_info->tx_desc_lim = cxgbe_desc_lim;
148 cxgbe_get_speed_caps(pi, &device_info->speed_capa);
151 void cxgbe_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
153 struct port_info *pi = eth_dev->data->dev_private;
154 struct adapter *adapter = pi->adapter;
156 t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
157 1, -1, 1, -1, false);
160 void cxgbe_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
162 struct port_info *pi = eth_dev->data->dev_private;
163 struct adapter *adapter = pi->adapter;
165 t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
166 0, -1, 1, -1, false);
169 void cxgbe_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
171 struct port_info *pi = eth_dev->data->dev_private;
172 struct adapter *adapter = pi->adapter;
174 /* TODO: address filters ?? */
176 t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
177 -1, 1, 1, -1, false);
180 void cxgbe_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
182 struct port_info *pi = eth_dev->data->dev_private;
183 struct adapter *adapter = pi->adapter;
185 /* TODO: address filters ?? */
187 t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
188 -1, 0, 1, -1, false);
191 int cxgbe_dev_link_update(struct rte_eth_dev *eth_dev,
192 int wait_to_complete)
194 struct port_info *pi = eth_dev->data->dev_private;
195 struct adapter *adapter = pi->adapter;
196 struct sge *s = &adapter->sge;
197 struct rte_eth_link new_link = { 0 };
198 unsigned int i, work_done, budget = 32;
199 u8 old_link = pi->link_cfg.link_ok;
201 for (i = 0; i < CXGBE_LINK_STATUS_POLL_CNT; i++) {
202 cxgbe_poll(&s->fw_evtq, NULL, budget, &work_done);
204 /* Exit if link status changed or always forced up */
205 if (pi->link_cfg.link_ok != old_link ||
206 cxgbe_force_linkup(adapter))
209 if (!wait_to_complete)
212 rte_delay_ms(CXGBE_LINK_STATUS_POLL_MS);
215 new_link.link_status = cxgbe_force_linkup(adapter) ?
216 ETH_LINK_UP : pi->link_cfg.link_ok;
217 new_link.link_autoneg = pi->link_cfg.autoneg;
218 new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
219 new_link.link_speed = pi->link_cfg.speed;
221 return rte_eth_linkstatus_set(eth_dev, &new_link);
225 * Set device link up.
227 int cxgbe_dev_set_link_up(struct rte_eth_dev *dev)
229 struct port_info *pi = dev->data->dev_private;
230 struct adapter *adapter = pi->adapter;
231 unsigned int work_done, budget = 32;
232 struct sge *s = &adapter->sge;
235 /* Flush all link events */
236 cxgbe_poll(&s->fw_evtq, NULL, budget, &work_done);
238 /* If link already up, nothing to do */
239 if (pi->link_cfg.link_ok)
242 ret = cxgbe_set_link_status(pi, true);
246 cxgbe_dev_link_update(dev, 1);
251 * Set device link down.
253 int cxgbe_dev_set_link_down(struct rte_eth_dev *dev)
255 struct port_info *pi = dev->data->dev_private;
256 struct adapter *adapter = pi->adapter;
257 unsigned int work_done, budget = 32;
258 struct sge *s = &adapter->sge;
261 /* Flush all link events */
262 cxgbe_poll(&s->fw_evtq, NULL, budget, &work_done);
264 /* If link already down, nothing to do */
265 if (!pi->link_cfg.link_ok)
268 ret = cxgbe_set_link_status(pi, false);
272 cxgbe_dev_link_update(dev, 0);
276 int cxgbe_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
278 struct port_info *pi = eth_dev->data->dev_private;
279 struct adapter *adapter = pi->adapter;
280 struct rte_eth_dev_info dev_info;
282 uint16_t new_mtu = mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
284 cxgbe_dev_info_get(eth_dev, &dev_info);
286 /* Must accommodate at least RTE_ETHER_MIN_MTU */
287 if (new_mtu < RTE_ETHER_MIN_MTU || new_mtu > dev_info.max_rx_pktlen)
290 /* set to jumbo mode if needed */
291 if (new_mtu > RTE_ETHER_MAX_LEN)
292 eth_dev->data->dev_conf.rxmode.offloads |=
293 DEV_RX_OFFLOAD_JUMBO_FRAME;
295 eth_dev->data->dev_conf.rxmode.offloads &=
296 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
298 err = t4_set_rxmode(adapter, adapter->mbox, pi->viid, new_mtu, -1, -1,
301 eth_dev->data->dev_conf.rxmode.max_rx_pkt_len = new_mtu;
309 void cxgbe_dev_close(struct rte_eth_dev *eth_dev)
311 struct port_info *pi = eth_dev->data->dev_private;
312 struct adapter *adapter = pi->adapter;
316 if (!(adapter->flags & FULL_INIT_DONE))
322 * We clear queues only if both tx and rx path of the port
325 t4_sge_eth_clear_queues(pi);
329 * It returns 0 on success.
331 int cxgbe_dev_start(struct rte_eth_dev *eth_dev)
333 struct port_info *pi = eth_dev->data->dev_private;
334 struct rte_eth_rxmode *rx_conf = ð_dev->data->dev_conf.rxmode;
335 struct adapter *adapter = pi->adapter;
341 * If we don't have a connection to the firmware there's nothing we
344 if (!(adapter->flags & FW_OK)) {
349 if (!(adapter->flags & FULL_INIT_DONE)) {
350 err = cxgbe_up(adapter);
355 if (rx_conf->offloads & DEV_RX_OFFLOAD_SCATTER)
356 eth_dev->data->scattered_rx = 1;
358 eth_dev->data->scattered_rx = 0;
360 cxgbe_enable_rx_queues(pi);
362 err = cxgbe_setup_rss(pi);
366 for (i = 0; i < pi->n_tx_qsets; i++) {
367 err = cxgbe_dev_tx_queue_start(eth_dev, i);
372 for (i = 0; i < pi->n_rx_qsets; i++) {
373 err = cxgbe_dev_rx_queue_start(eth_dev, i);
378 err = cxgbe_link_start(pi);
387 * Stop device: disable rx and tx functions to allow for reconfiguring.
389 void cxgbe_dev_stop(struct rte_eth_dev *eth_dev)
391 struct port_info *pi = eth_dev->data->dev_private;
392 struct adapter *adapter = pi->adapter;
396 if (!(adapter->flags & FULL_INIT_DONE))
402 * We clear queues only if both tx and rx path of the port
405 t4_sge_eth_clear_queues(pi);
406 eth_dev->data->scattered_rx = 0;
409 int cxgbe_dev_configure(struct rte_eth_dev *eth_dev)
411 struct port_info *pi = eth_dev->data->dev_private;
412 struct adapter *adapter = pi->adapter;
417 if (!(adapter->flags & FW_QUEUE_BOUND)) {
418 err = cxgbe_setup_sge_fwevtq(adapter);
421 adapter->flags |= FW_QUEUE_BOUND;
422 if (is_pf4(adapter)) {
423 err = cxgbe_setup_sge_ctrl_txq(adapter);
429 err = cxgbe_cfg_queue_count(eth_dev);
436 int cxgbe_dev_tx_queue_start(struct rte_eth_dev *eth_dev, uint16_t tx_queue_id)
439 struct sge_eth_txq *txq = (struct sge_eth_txq *)
440 (eth_dev->data->tx_queues[tx_queue_id]);
442 dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
444 ret = t4_sge_eth_txq_start(txq);
446 eth_dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
451 int cxgbe_dev_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t tx_queue_id)
454 struct sge_eth_txq *txq = (struct sge_eth_txq *)
455 (eth_dev->data->tx_queues[tx_queue_id]);
457 dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
459 ret = t4_sge_eth_txq_stop(txq);
461 eth_dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
466 int cxgbe_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
467 uint16_t queue_idx, uint16_t nb_desc,
468 unsigned int socket_id,
469 const struct rte_eth_txconf *tx_conf __rte_unused)
471 struct port_info *pi = eth_dev->data->dev_private;
472 struct adapter *adapter = pi->adapter;
473 struct sge *s = &adapter->sge;
474 struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset + queue_idx];
476 unsigned int temp_nb_desc;
478 dev_debug(adapter, "%s: eth_dev->data->nb_tx_queues = %d; queue_idx = %d; nb_desc = %d; socket_id = %d; pi->first_qset = %u\n",
479 __func__, eth_dev->data->nb_tx_queues, queue_idx, nb_desc,
480 socket_id, pi->first_qset);
482 /* Free up the existing queue */
483 if (eth_dev->data->tx_queues[queue_idx]) {
484 cxgbe_dev_tx_queue_release(eth_dev->data->tx_queues[queue_idx]);
485 eth_dev->data->tx_queues[queue_idx] = NULL;
488 eth_dev->data->tx_queues[queue_idx] = (void *)txq;
492 * nb_desc should be > 1023 and <= CXGBE_MAX_RING_DESC_SIZE
494 temp_nb_desc = nb_desc;
495 if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
496 dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
497 __func__, CXGBE_MIN_RING_DESC_SIZE,
498 CXGBE_DEFAULT_TX_DESC_SIZE);
499 temp_nb_desc = CXGBE_DEFAULT_TX_DESC_SIZE;
500 } else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
501 dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
502 __func__, CXGBE_MIN_RING_DESC_SIZE,
503 CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_TX_DESC_SIZE);
507 txq->q.size = temp_nb_desc;
509 err = t4_sge_alloc_eth_txq(adapter, txq, eth_dev, queue_idx,
510 s->fw_evtq.cntxt_id, socket_id);
512 dev_debug(adapter, "%s: txq->q.cntxt_id= %u txq->q.abs_id= %u err = %d\n",
513 __func__, txq->q.cntxt_id, txq->q.abs_id, err);
517 void cxgbe_dev_tx_queue_release(void *q)
519 struct sge_eth_txq *txq = (struct sge_eth_txq *)q;
522 struct port_info *pi = (struct port_info *)
523 (txq->eth_dev->data->dev_private);
524 struct adapter *adap = pi->adapter;
526 dev_debug(adapter, "%s: pi->port_id = %d; tx_queue_id = %d\n",
527 __func__, pi->port_id, txq->q.cntxt_id);
529 t4_sge_eth_txq_release(adap, txq);
533 int cxgbe_dev_rx_queue_start(struct rte_eth_dev *eth_dev, uint16_t rx_queue_id)
536 struct port_info *pi = eth_dev->data->dev_private;
537 struct adapter *adap = pi->adapter;
540 dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
541 __func__, pi->port_id, rx_queue_id);
543 q = eth_dev->data->rx_queues[rx_queue_id];
545 ret = t4_sge_eth_rxq_start(adap, q);
547 eth_dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
552 int cxgbe_dev_rx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t rx_queue_id)
555 struct port_info *pi = eth_dev->data->dev_private;
556 struct adapter *adap = pi->adapter;
559 dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
560 __func__, pi->port_id, rx_queue_id);
562 q = eth_dev->data->rx_queues[rx_queue_id];
563 ret = t4_sge_eth_rxq_stop(adap, q);
565 eth_dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
570 int cxgbe_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
571 uint16_t queue_idx, uint16_t nb_desc,
572 unsigned int socket_id,
573 const struct rte_eth_rxconf *rx_conf __rte_unused,
574 struct rte_mempool *mp)
576 struct port_info *pi = eth_dev->data->dev_private;
577 struct adapter *adapter = pi->adapter;
578 struct sge *s = &adapter->sge;
579 struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset + queue_idx];
582 unsigned int temp_nb_desc;
583 struct rte_eth_dev_info dev_info;
584 unsigned int pkt_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
586 dev_debug(adapter, "%s: eth_dev->data->nb_rx_queues = %d; queue_idx = %d; nb_desc = %d; socket_id = %d; mp = %p\n",
587 __func__, eth_dev->data->nb_rx_queues, queue_idx, nb_desc,
590 cxgbe_dev_info_get(eth_dev, &dev_info);
592 /* Must accommodate at least RTE_ETHER_MIN_MTU */
593 if ((pkt_len < dev_info.min_rx_bufsize) ||
594 (pkt_len > dev_info.max_rx_pktlen)) {
595 dev_err(adap, "%s: max pkt len must be > %d and <= %d\n",
596 __func__, dev_info.min_rx_bufsize,
597 dev_info.max_rx_pktlen);
601 /* Free up the existing queue */
602 if (eth_dev->data->rx_queues[queue_idx]) {
603 cxgbe_dev_rx_queue_release(eth_dev->data->rx_queues[queue_idx]);
604 eth_dev->data->rx_queues[queue_idx] = NULL;
607 eth_dev->data->rx_queues[queue_idx] = (void *)rxq;
611 * nb_desc should be > 0 and <= CXGBE_MAX_RING_DESC_SIZE
613 temp_nb_desc = nb_desc;
614 if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
615 dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
616 __func__, CXGBE_MIN_RING_DESC_SIZE,
617 CXGBE_DEFAULT_RX_DESC_SIZE);
618 temp_nb_desc = CXGBE_DEFAULT_RX_DESC_SIZE;
619 } else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
620 dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
621 __func__, CXGBE_MIN_RING_DESC_SIZE,
622 CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_RX_DESC_SIZE);
626 rxq->rspq.size = temp_nb_desc;
627 if ((&rxq->fl) != NULL)
628 rxq->fl.size = temp_nb_desc;
630 /* Set to jumbo mode if necessary */
631 if (pkt_len > RTE_ETHER_MAX_LEN)
632 eth_dev->data->dev_conf.rxmode.offloads |=
633 DEV_RX_OFFLOAD_JUMBO_FRAME;
635 eth_dev->data->dev_conf.rxmode.offloads &=
636 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
638 err = t4_sge_alloc_rxq(adapter, &rxq->rspq, false, eth_dev, msi_idx,
641 t4_get_tp_ch_map(adapter, pi->tx_chan) : 0, mp,
642 queue_idx, socket_id);
644 dev_debug(adapter, "%s: err = %d; port_id = %d; cntxt_id = %u; abs_id = %u\n",
645 __func__, err, pi->port_id, rxq->rspq.cntxt_id,
650 void cxgbe_dev_rx_queue_release(void *q)
652 struct sge_eth_rxq *rxq = (struct sge_eth_rxq *)q;
653 struct sge_rspq *rq = &rxq->rspq;
656 struct port_info *pi = (struct port_info *)
657 (rq->eth_dev->data->dev_private);
658 struct adapter *adap = pi->adapter;
660 dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
661 __func__, pi->port_id, rxq->rspq.cntxt_id);
663 t4_sge_eth_rxq_release(adap, rxq);
668 * Get port statistics.
670 static int cxgbe_dev_stats_get(struct rte_eth_dev *eth_dev,
671 struct rte_eth_stats *eth_stats)
673 struct port_info *pi = eth_dev->data->dev_private;
674 struct adapter *adapter = pi->adapter;
675 struct sge *s = &adapter->sge;
676 struct port_stats ps;
679 cxgbe_stats_get(pi, &ps);
682 eth_stats->imissed = ps.rx_ovflow0 + ps.rx_ovflow1 +
683 ps.rx_ovflow2 + ps.rx_ovflow3 +
684 ps.rx_trunc0 + ps.rx_trunc1 +
685 ps.rx_trunc2 + ps.rx_trunc3;
686 eth_stats->ierrors = ps.rx_symbol_err + ps.rx_fcs_err +
687 ps.rx_jabber + ps.rx_too_long + ps.rx_runt +
691 eth_stats->opackets = ps.tx_frames;
692 eth_stats->obytes = ps.tx_octets;
693 eth_stats->oerrors = ps.tx_error_frames;
695 for (i = 0; i < pi->n_rx_qsets; i++) {
696 struct sge_eth_rxq *rxq =
697 &s->ethrxq[pi->first_qset + i];
699 eth_stats->q_ipackets[i] = rxq->stats.pkts;
700 eth_stats->q_ibytes[i] = rxq->stats.rx_bytes;
701 eth_stats->ipackets += eth_stats->q_ipackets[i];
702 eth_stats->ibytes += eth_stats->q_ibytes[i];
705 for (i = 0; i < pi->n_tx_qsets; i++) {
706 struct sge_eth_txq *txq =
707 &s->ethtxq[pi->first_qset + i];
709 eth_stats->q_opackets[i] = txq->stats.pkts;
710 eth_stats->q_obytes[i] = txq->stats.tx_bytes;
716 * Reset port statistics.
718 static void cxgbe_dev_stats_reset(struct rte_eth_dev *eth_dev)
720 struct port_info *pi = eth_dev->data->dev_private;
721 struct adapter *adapter = pi->adapter;
722 struct sge *s = &adapter->sge;
725 cxgbe_stats_reset(pi);
726 for (i = 0; i < pi->n_rx_qsets; i++) {
727 struct sge_eth_rxq *rxq =
728 &s->ethrxq[pi->first_qset + i];
731 rxq->stats.rx_bytes = 0;
733 for (i = 0; i < pi->n_tx_qsets; i++) {
734 struct sge_eth_txq *txq =
735 &s->ethtxq[pi->first_qset + i];
738 txq->stats.tx_bytes = 0;
739 txq->stats.mapping_err = 0;
743 static int cxgbe_flow_ctrl_get(struct rte_eth_dev *eth_dev,
744 struct rte_eth_fc_conf *fc_conf)
746 struct port_info *pi = eth_dev->data->dev_private;
747 struct link_config *lc = &pi->link_cfg;
748 int rx_pause, tx_pause;
750 fc_conf->autoneg = lc->fc & PAUSE_AUTONEG;
751 rx_pause = lc->fc & PAUSE_RX;
752 tx_pause = lc->fc & PAUSE_TX;
754 if (rx_pause && tx_pause)
755 fc_conf->mode = RTE_FC_FULL;
757 fc_conf->mode = RTE_FC_RX_PAUSE;
759 fc_conf->mode = RTE_FC_TX_PAUSE;
761 fc_conf->mode = RTE_FC_NONE;
765 static int cxgbe_flow_ctrl_set(struct rte_eth_dev *eth_dev,
766 struct rte_eth_fc_conf *fc_conf)
768 struct port_info *pi = eth_dev->data->dev_private;
769 struct adapter *adapter = pi->adapter;
770 struct link_config *lc = &pi->link_cfg;
772 if (lc->pcaps & FW_PORT_CAP32_ANEG) {
773 if (fc_conf->autoneg)
774 lc->requested_fc |= PAUSE_AUTONEG;
776 lc->requested_fc &= ~PAUSE_AUTONEG;
779 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
780 (fc_conf->mode & RTE_FC_RX_PAUSE))
781 lc->requested_fc |= PAUSE_RX;
783 lc->requested_fc &= ~PAUSE_RX;
785 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
786 (fc_conf->mode & RTE_FC_TX_PAUSE))
787 lc->requested_fc |= PAUSE_TX;
789 lc->requested_fc &= ~PAUSE_TX;
791 return t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
796 cxgbe_dev_supported_ptypes_get(struct rte_eth_dev *eth_dev)
798 static const uint32_t ptypes[] = {
804 if (eth_dev->rx_pkt_burst == cxgbe_recv_pkts)
809 /* Update RSS hash configuration
811 static int cxgbe_dev_rss_hash_update(struct rte_eth_dev *dev,
812 struct rte_eth_rss_conf *rss_conf)
814 struct port_info *pi = dev->data->dev_private;
815 struct adapter *adapter = pi->adapter;
818 err = cxgbe_write_rss_conf(pi, rss_conf->rss_hf);
822 pi->rss_hf = rss_conf->rss_hf;
824 if (rss_conf->rss_key) {
825 u32 key[10], mod_key[10];
828 memcpy(key, rss_conf->rss_key, CXGBE_DEFAULT_RSS_KEY_LEN);
830 for (i = 9, j = 0; i >= 0; i--, j++)
831 mod_key[j] = cpu_to_be32(key[i]);
833 t4_write_rss_key(adapter, mod_key, -1);
839 /* Get RSS hash configuration
841 static int cxgbe_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
842 struct rte_eth_rss_conf *rss_conf)
844 struct port_info *pi = dev->data->dev_private;
845 struct adapter *adapter = pi->adapter;
850 err = t4_read_config_vi_rss(adapter, adapter->mbox, pi->viid,
856 if (flags & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) {
857 rss_hf |= CXGBE_RSS_HF_TCP_IPV6_MASK;
858 if (flags & F_FW_RSS_VI_CONFIG_CMD_UDPEN)
859 rss_hf |= CXGBE_RSS_HF_UDP_IPV6_MASK;
862 if (flags & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
863 rss_hf |= CXGBE_RSS_HF_IPV6_MASK;
865 if (flags & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) {
866 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
867 if (flags & F_FW_RSS_VI_CONFIG_CMD_UDPEN)
868 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
871 if (flags & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
872 rss_hf |= CXGBE_RSS_HF_IPV4_MASK;
874 rss_conf->rss_hf = rss_hf;
876 if (rss_conf->rss_key) {
877 u32 key[10], mod_key[10];
880 t4_read_rss_key(adapter, key);
882 for (i = 9, j = 0; i >= 0; i--, j++)
883 mod_key[j] = be32_to_cpu(key[i]);
885 memcpy(rss_conf->rss_key, mod_key, CXGBE_DEFAULT_RSS_KEY_LEN);
891 static int cxgbe_get_eeprom_length(struct rte_eth_dev *dev)
898 * eeprom_ptov - translate a physical EEPROM address to virtual
899 * @phys_addr: the physical EEPROM address
900 * @fn: the PCI function number
901 * @sz: size of function-specific area
903 * Translate a physical EEPROM address to virtual. The first 1K is
904 * accessed through virtual addresses starting at 31K, the rest is
905 * accessed through virtual addresses starting at 0.
907 * The mapping is as follows:
908 * [0..1K) -> [31K..32K)
909 * [1K..1K+A) -> [31K-A..31K)
910 * [1K+A..ES) -> [0..ES-A-1K)
912 * where A = @fn * @sz, and ES = EEPROM size.
914 static int eeprom_ptov(unsigned int phys_addr, unsigned int fn, unsigned int sz)
917 if (phys_addr < 1024)
918 return phys_addr + (31 << 10);
919 if (phys_addr < 1024 + fn)
920 return fn + phys_addr - 1024;
921 if (phys_addr < EEPROMSIZE)
922 return phys_addr - 1024 - fn;
923 if (phys_addr < EEPROMVSIZE)
924 return phys_addr - 1024;
928 /* The next two routines implement eeprom read/write from physical addresses.
930 static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
932 int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
935 vaddr = t4_seeprom_read(adap, vaddr, v);
936 return vaddr < 0 ? vaddr : 0;
939 static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
941 int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
944 vaddr = t4_seeprom_write(adap, vaddr, v);
945 return vaddr < 0 ? vaddr : 0;
948 #define EEPROM_MAGIC 0x38E2F10C
950 static int cxgbe_get_eeprom(struct rte_eth_dev *dev,
951 struct rte_dev_eeprom_info *e)
953 struct port_info *pi = dev->data->dev_private;
954 struct adapter *adapter = pi->adapter;
956 u8 *buf = rte_zmalloc(NULL, EEPROMSIZE, 0);
961 e->magic = EEPROM_MAGIC;
962 for (i = e->offset & ~3; !err && i < e->offset + e->length; i += 4)
963 err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
966 rte_memcpy(e->data, buf + e->offset, e->length);
971 static int cxgbe_set_eeprom(struct rte_eth_dev *dev,
972 struct rte_dev_eeprom_info *eeprom)
974 struct port_info *pi = dev->data->dev_private;
975 struct adapter *adapter = pi->adapter;
978 u32 aligned_offset, aligned_len, *p;
980 if (eeprom->magic != EEPROM_MAGIC)
983 aligned_offset = eeprom->offset & ~3;
984 aligned_len = (eeprom->length + (eeprom->offset & 3) + 3) & ~3;
986 if (adapter->pf > 0) {
987 u32 start = 1024 + adapter->pf * EEPROMPFSIZE;
989 if (aligned_offset < start ||
990 aligned_offset + aligned_len > start + EEPROMPFSIZE)
994 if (aligned_offset != eeprom->offset || aligned_len != eeprom->length) {
995 /* RMW possibly needed for first or last words.
997 buf = rte_zmalloc(NULL, aligned_len, 0);
1000 err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
1001 if (!err && aligned_len > 4)
1002 err = eeprom_rd_phys(adapter,
1003 aligned_offset + aligned_len - 4,
1004 (u32 *)&buf[aligned_len - 4]);
1007 rte_memcpy(buf + (eeprom->offset & 3), eeprom->data,
1013 err = t4_seeprom_wp(adapter, false);
1017 for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
1018 err = eeprom_wr_phys(adapter, aligned_offset, *p);
1019 aligned_offset += 4;
1023 err = t4_seeprom_wp(adapter, true);
1025 if (buf != eeprom->data)
1030 static int cxgbe_get_regs_len(struct rte_eth_dev *eth_dev)
1032 struct port_info *pi = eth_dev->data->dev_private;
1033 struct adapter *adapter = pi->adapter;
1035 return t4_get_regs_len(adapter) / sizeof(uint32_t);
1038 static int cxgbe_get_regs(struct rte_eth_dev *eth_dev,
1039 struct rte_dev_reg_info *regs)
1041 struct port_info *pi = eth_dev->data->dev_private;
1042 struct adapter *adapter = pi->adapter;
1044 regs->version = CHELSIO_CHIP_VERSION(adapter->params.chip) |
1045 (CHELSIO_CHIP_RELEASE(adapter->params.chip) << 10) |
1048 if (regs->data == NULL) {
1049 regs->length = cxgbe_get_regs_len(eth_dev);
1050 regs->width = sizeof(uint32_t);
1055 t4_get_regs(adapter, regs->data, (regs->length * sizeof(uint32_t)));
1060 int cxgbe_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
1062 struct port_info *pi = dev->data->dev_private;
1065 ret = cxgbe_mpstcam_modify(pi, (int)pi->xact_addr_filt, (u8 *)addr);
1067 dev_err(adapter, "failed to set mac addr; err = %d\n",
1071 pi->xact_addr_filt = ret;
1075 static const struct eth_dev_ops cxgbe_eth_dev_ops = {
1076 .dev_start = cxgbe_dev_start,
1077 .dev_stop = cxgbe_dev_stop,
1078 .dev_close = cxgbe_dev_close,
1079 .promiscuous_enable = cxgbe_dev_promiscuous_enable,
1080 .promiscuous_disable = cxgbe_dev_promiscuous_disable,
1081 .allmulticast_enable = cxgbe_dev_allmulticast_enable,
1082 .allmulticast_disable = cxgbe_dev_allmulticast_disable,
1083 .dev_configure = cxgbe_dev_configure,
1084 .dev_infos_get = cxgbe_dev_info_get,
1085 .dev_supported_ptypes_get = cxgbe_dev_supported_ptypes_get,
1086 .link_update = cxgbe_dev_link_update,
1087 .dev_set_link_up = cxgbe_dev_set_link_up,
1088 .dev_set_link_down = cxgbe_dev_set_link_down,
1089 .mtu_set = cxgbe_dev_mtu_set,
1090 .tx_queue_setup = cxgbe_dev_tx_queue_setup,
1091 .tx_queue_start = cxgbe_dev_tx_queue_start,
1092 .tx_queue_stop = cxgbe_dev_tx_queue_stop,
1093 .tx_queue_release = cxgbe_dev_tx_queue_release,
1094 .rx_queue_setup = cxgbe_dev_rx_queue_setup,
1095 .rx_queue_start = cxgbe_dev_rx_queue_start,
1096 .rx_queue_stop = cxgbe_dev_rx_queue_stop,
1097 .rx_queue_release = cxgbe_dev_rx_queue_release,
1098 .filter_ctrl = cxgbe_dev_filter_ctrl,
1099 .stats_get = cxgbe_dev_stats_get,
1100 .stats_reset = cxgbe_dev_stats_reset,
1101 .flow_ctrl_get = cxgbe_flow_ctrl_get,
1102 .flow_ctrl_set = cxgbe_flow_ctrl_set,
1103 .get_eeprom_length = cxgbe_get_eeprom_length,
1104 .get_eeprom = cxgbe_get_eeprom,
1105 .set_eeprom = cxgbe_set_eeprom,
1106 .get_reg = cxgbe_get_regs,
1107 .rss_hash_update = cxgbe_dev_rss_hash_update,
1108 .rss_hash_conf_get = cxgbe_dev_rss_hash_conf_get,
1109 .mac_addr_set = cxgbe_mac_addr_set,
1114 * It returns 0 on success.
1116 static int eth_cxgbe_dev_init(struct rte_eth_dev *eth_dev)
1118 struct rte_pci_device *pci_dev;
1119 struct port_info *pi = eth_dev->data->dev_private;
1120 struct adapter *adapter = NULL;
1121 char name[RTE_ETH_NAME_MAX_LEN];
1126 eth_dev->dev_ops = &cxgbe_eth_dev_ops;
1127 eth_dev->rx_pkt_burst = &cxgbe_recv_pkts;
1128 eth_dev->tx_pkt_burst = &cxgbe_xmit_pkts;
1129 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1131 /* for secondary processes, we attach to ethdevs allocated by primary
1132 * and do minimal initialization.
1134 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1137 for (i = 1; i < MAX_NPORTS; i++) {
1138 struct rte_eth_dev *rest_eth_dev;
1139 char namei[RTE_ETH_NAME_MAX_LEN];
1141 snprintf(namei, sizeof(namei), "%s_%d",
1142 pci_dev->device.name, i);
1143 rest_eth_dev = rte_eth_dev_attach_secondary(namei);
1145 rest_eth_dev->device = &pci_dev->device;
1146 rest_eth_dev->dev_ops =
1148 rest_eth_dev->rx_pkt_burst =
1149 eth_dev->rx_pkt_burst;
1150 rest_eth_dev->tx_pkt_burst =
1151 eth_dev->tx_pkt_burst;
1152 rte_eth_dev_probing_finish(rest_eth_dev);
1158 snprintf(name, sizeof(name), "cxgbeadapter%d", eth_dev->data->port_id);
1159 adapter = rte_zmalloc(name, sizeof(*adapter), 0);
1163 adapter->use_unpacked_mode = 1;
1164 adapter->regs = (void *)pci_dev->mem_resource[0].addr;
1165 if (!adapter->regs) {
1166 dev_err(adapter, "%s: cannot map device registers\n", __func__);
1168 goto out_free_adapter;
1170 adapter->pdev = pci_dev;
1171 adapter->eth_dev = eth_dev;
1172 pi->adapter = adapter;
1174 err = cxgbe_probe(adapter);
1176 dev_err(adapter, "%s: cxgbe probe failed with err %d\n",
1178 goto out_free_adapter;
1188 static int eth_cxgbe_dev_uninit(struct rte_eth_dev *eth_dev)
1190 struct port_info *pi = eth_dev->data->dev_private;
1191 struct adapter *adap = pi->adapter;
1193 /* Free up other ports and all resources */
1198 static int eth_cxgbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1199 struct rte_pci_device *pci_dev)
1201 return rte_eth_dev_pci_generic_probe(pci_dev,
1202 sizeof(struct port_info), eth_cxgbe_dev_init);
1205 static int eth_cxgbe_pci_remove(struct rte_pci_device *pci_dev)
1207 return rte_eth_dev_pci_generic_remove(pci_dev, eth_cxgbe_dev_uninit);
1210 static struct rte_pci_driver rte_cxgbe_pmd = {
1211 .id_table = cxgb4_pci_tbl,
1212 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1213 .probe = eth_cxgbe_pci_probe,
1214 .remove = eth_cxgbe_pci_remove,
1217 RTE_PMD_REGISTER_PCI(net_cxgbe, rte_cxgbe_pmd);
1218 RTE_PMD_REGISTER_PCI_TABLE(net_cxgbe, cxgb4_pci_tbl);
1219 RTE_PMD_REGISTER_KMOD_DEP(net_cxgbe, "* igb_uio | uio_pci_generic | vfio-pci");
1220 RTE_PMD_REGISTER_PARAM_STRING(net_cxgbe,
1221 CXGBE_DEVARG_KEEP_OVLAN "=<0|1> "
1222 CXGBE_DEVARG_FORCE_LINK_UP "=<0|1> ");
1224 RTE_INIT(cxgbe_init_log)
1226 cxgbe_logtype = rte_log_register("pmd.net.cxgbe");
1227 if (cxgbe_logtype >= 0)
1228 rte_log_set_level(cxgbe_logtype, RTE_LOG_NOTICE);