1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2015 Cavium, Inc.
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * This file may also be available under a different license from Cavium.
20 * Contact Cavium, Inc. for more information
21 **********************************************************************/
22 #include <linux/version.h>
23 #include <linux/pci.h>
24 #include <linux/firmware.h>
25 #include <linux/ptp_clock_kernel.h>
26 #include <net/vxlan.h>
27 #include "liquidio_common.h"
28 #include "octeon_droq.h"
29 #include "octeon_iq.h"
30 #include "response_manager.h"
31 #include "octeon_device.h"
32 #include "octeon_nic.h"
33 #include "octeon_main.h"
34 #include "octeon_network.h"
35 #include "cn66xx_regs.h"
36 #include "cn66xx_device.h"
37 #include "cn68xx_device.h"
38 #include "cn23xx_pf_device.h"
39 #include "liquidio_image.h"
41 MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
42 MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
43 MODULE_LICENSE("GPL");
44 MODULE_VERSION(LIQUIDIO_VERSION);
45 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
46 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
47 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
49 static int ddr_timeout = 10000;
50 module_param(ddr_timeout, int, 0644);
51 MODULE_PARM_DESC(ddr_timeout,
52 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
54 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
56 #define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \
57 (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count)
59 static int debug = -1;
60 module_param(debug, int, 0644);
61 MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
63 static char fw_type[LIO_MAX_FW_TYPE_LEN];
64 module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
65 MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
68 module_param(conf_type, int, 0);
69 MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
71 static int ptp_enable = 1;
73 /* Bit mask values for lio->ifstate */
74 #define LIO_IFSTATE_DROQ_OPS 0x01
75 #define LIO_IFSTATE_REGISTERED 0x02
76 #define LIO_IFSTATE_RUNNING 0x04
77 #define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
79 /* Polling interval for determining when NIC application is alive */
80 #define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
82 /* runtime link query interval */
83 #define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
85 struct liquidio_if_cfg_context {
93 struct liquidio_if_cfg_resp {
95 struct liquidio_if_cfg_info cfg_info;
99 struct liquidio_rx_ctl_context {
102 wait_queue_head_t wc;
107 struct oct_link_status_resp {
109 struct oct_link_info link_info;
113 struct oct_timestamp_resp {
119 #define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
124 #ifdef __BIG_ENDIAN_BITFIELD
136 /** Octeon device properties to be used by the NIC module.
137 * Each octeon device in the system will be represented
138 * by this structure in the NIC module.
141 #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
143 #define OCTNIC_GSO_MAX_HEADER_SIZE 128
144 #define OCTNIC_GSO_MAX_SIZE \
145 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
147 /** Structure of a node in list of gather components maintained by
148 * NIC driver for each network device.
150 struct octnic_gather {
151 /** List manipulation. Next and prev pointers. */
152 struct list_head list;
154 /** Size of the gather component at sg in bytes. */
157 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
160 /** Gather component that can accommodate max sized fragment list
161 * received from the IP layer.
163 struct octeon_sg_entry *sg;
169 struct completion init;
170 struct completion started;
171 struct pci_dev *pci_dev;
176 struct octeon_device_priv {
177 /** Tasklet structures for this device. */
178 struct tasklet_struct droq_tasklet;
179 unsigned long napi_mask;
182 static int octeon_device_init(struct octeon_device *);
183 static int liquidio_stop(struct net_device *netdev);
184 static void liquidio_remove(struct pci_dev *pdev);
185 static int liquidio_probe(struct pci_dev *pdev,
186 const struct pci_device_id *ent);
188 static struct handshake handshake[MAX_OCTEON_DEVICES];
189 static struct completion first_stage;
191 static void octeon_droq_bh(unsigned long pdev)
195 struct octeon_device *oct = (struct octeon_device *)pdev;
196 struct octeon_device_priv *oct_priv =
197 (struct octeon_device_priv *)oct->priv;
199 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
200 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
201 if (!(oct->io_qmask.oq & (1ULL << q_no)))
203 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
205 lio_enable_irq(oct->droq[q_no], NULL);
207 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
208 /* set time and cnt interrupt thresholds for this DROQ
211 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
214 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
217 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
222 tasklet_schedule(&oct_priv->droq_tasklet);
225 static int lio_wait_for_oq_pkts(struct octeon_device *oct)
227 struct octeon_device_priv *oct_priv =
228 (struct octeon_device_priv *)oct->priv;
229 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
235 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
236 if (!(oct->io_qmask.oq & (1ULL << i)))
238 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
241 pending_pkts += pkt_cnt;
242 tasklet_schedule(&oct_priv->droq_tasklet);
245 schedule_timeout_uninterruptible(1);
247 } while (retry-- && pending_pkts);
253 * \brief Forces all IO queues off on a given device
254 * @param oct Pointer to Octeon device
256 static void force_io_queues_off(struct octeon_device *oct)
258 if ((oct->chip_id == OCTEON_CN66XX) ||
259 (oct->chip_id == OCTEON_CN68XX)) {
260 /* Reset the Enable bits for Input Queues. */
261 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
263 /* Reset the Enable bits for Output Queues. */
264 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
269 * \brief wait for all pending requests to complete
270 * @param oct Pointer to Octeon device
272 * Called during shutdown sequence
274 static int wait_for_pending_requests(struct octeon_device *oct)
278 for (i = 0; i < 100; i++) {
280 atomic_read(&oct->response_list
281 [OCTEON_ORDERED_SC_LIST].pending_req_count);
283 schedule_timeout_uninterruptible(HZ / 10);
295 * \brief Cause device to go quiet so it can be safely removed/reset/etc
296 * @param oct Pointer to Octeon device
298 static inline void pcierror_quiesce_device(struct octeon_device *oct)
302 /* Disable the input and output queues now. No more packets will
303 * arrive from Octeon, but we should wait for all packet processing
306 force_io_queues_off(oct);
308 /* To allow for in-flight requests */
309 schedule_timeout_uninterruptible(100);
311 if (wait_for_pending_requests(oct))
312 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
314 /* Force all requests waiting to be fetched by OCTEON to complete. */
315 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
316 struct octeon_instr_queue *iq;
318 if (!(oct->io_qmask.iq & (1ULL << i)))
320 iq = oct->instr_queue[i];
322 if (atomic_read(&iq->instr_pending)) {
323 spin_lock_bh(&iq->lock);
325 iq->octeon_read_index = iq->host_write_index;
326 iq->stats.instr_processed +=
327 atomic_read(&iq->instr_pending);
328 lio_process_iq_request_list(oct, iq, 0);
329 spin_unlock_bh(&iq->lock);
333 /* Force all pending ordered list requests to time out. */
334 lio_process_ordered_list(oct, 1);
336 /* We do not need to wait for output queue packets to be processed. */
340 * \brief Cleanup PCI AER uncorrectable error status
341 * @param dev Pointer to PCI device
343 static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
348 pr_info("%s :\n", __func__);
350 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
351 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
352 if (dev->error_state == pci_channel_io_normal)
353 status &= ~mask; /* Clear corresponding nonfatal bits */
355 status &= mask; /* Clear corresponding fatal bits */
356 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
360 * \brief Stop all PCI IO to a given device
361 * @param dev Pointer to Octeon device
363 static void stop_pci_io(struct octeon_device *oct)
365 /* No more instructions will be forwarded. */
366 atomic_set(&oct->status, OCT_DEV_IN_RESET);
368 pci_disable_device(oct->pci_dev);
370 /* Disable interrupts */
371 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
373 pcierror_quiesce_device(oct);
375 /* Release the interrupt line */
376 free_irq(oct->pci_dev->irq, oct);
378 if (oct->flags & LIO_FLAG_MSI_ENABLED)
379 pci_disable_msi(oct->pci_dev);
381 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
382 lio_get_state_string(&oct->status));
384 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
385 /* making it a common function for all OCTEON models */
386 cleanup_aer_uncorrect_error_status(oct->pci_dev);
390 * \brief called when PCI error is detected
391 * @param pdev Pointer to PCI device
392 * @param state The current pci connection state
394 * This function is called after a PCI bus error affecting
395 * this device has been detected.
397 static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
398 pci_channel_state_t state)
400 struct octeon_device *oct = pci_get_drvdata(pdev);
402 /* Non-correctable Non-fatal errors */
403 if (state == pci_channel_io_normal) {
404 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
405 cleanup_aer_uncorrect_error_status(oct->pci_dev);
406 return PCI_ERS_RESULT_CAN_RECOVER;
409 /* Non-correctable Fatal errors */
410 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
413 /* Always return a DISCONNECT. There is no support for recovery but only
414 * for a clean shutdown.
416 return PCI_ERS_RESULT_DISCONNECT;
420 * \brief mmio handler
421 * @param pdev Pointer to PCI device
423 static pci_ers_result_t liquidio_pcie_mmio_enabled(
424 struct pci_dev *pdev __attribute__((unused)))
426 /* We should never hit this since we never ask for a reset for a Fatal
427 * Error. We always return DISCONNECT in io_error above.
428 * But play safe and return RECOVERED for now.
430 return PCI_ERS_RESULT_RECOVERED;
434 * \brief called after the pci bus has been reset.
435 * @param pdev Pointer to PCI device
437 * Restart the card from scratch, as if from a cold-boot. Implementation
438 * resembles the first-half of the octeon_resume routine.
440 static pci_ers_result_t liquidio_pcie_slot_reset(
441 struct pci_dev *pdev __attribute__((unused)))
443 /* We should never hit this since we never ask for a reset for a Fatal
444 * Error. We always return DISCONNECT in io_error above.
445 * But play safe and return RECOVERED for now.
447 return PCI_ERS_RESULT_RECOVERED;
451 * \brief called when traffic can start flowing again.
452 * @param pdev Pointer to PCI device
454 * This callback is called when the error recovery driver tells us that
455 * its OK to resume normal operation. Implementation resembles the
456 * second-half of the octeon_resume routine.
458 static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
460 /* Nothing to be done here. */
465 * \brief called when suspending
466 * @param pdev Pointer to PCI device
467 * @param state state to suspend to
469 static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
470 pm_message_t state __attribute__((unused)))
476 * \brief called when resuming
477 * @param pdev Pointer to PCI device
479 static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
485 /* For PCI-E Advanced Error Recovery (AER) Interface */
486 static const struct pci_error_handlers liquidio_err_handler = {
487 .error_detected = liquidio_pcie_error_detected,
488 .mmio_enabled = liquidio_pcie_mmio_enabled,
489 .slot_reset = liquidio_pcie_slot_reset,
490 .resume = liquidio_pcie_resume,
493 static const struct pci_device_id liquidio_pci_tbl[] = {
495 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
498 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
501 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
507 MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
509 static struct pci_driver liquidio_pci_driver = {
511 .id_table = liquidio_pci_tbl,
512 .probe = liquidio_probe,
513 .remove = liquidio_remove,
514 .err_handler = &liquidio_err_handler, /* For AER */
517 .suspend = liquidio_suspend,
518 .resume = liquidio_resume,
523 * \brief register PCI driver
525 static int liquidio_init_pci(void)
527 return pci_register_driver(&liquidio_pci_driver);
531 * \brief unregister PCI driver
533 static void liquidio_deinit_pci(void)
535 pci_unregister_driver(&liquidio_pci_driver);
539 * \brief check interface state
540 * @param lio per-network private data
541 * @param state_flag flag state to check
543 static inline int ifstate_check(struct lio *lio, int state_flag)
545 return atomic_read(&lio->ifstate) & state_flag;
549 * \brief set interface state
550 * @param lio per-network private data
551 * @param state_flag flag state to set
553 static inline void ifstate_set(struct lio *lio, int state_flag)
555 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
559 * \brief clear interface state
560 * @param lio per-network private data
561 * @param state_flag flag state to clear
563 static inline void ifstate_reset(struct lio *lio, int state_flag)
565 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
569 * \brief Stop Tx queues
570 * @param netdev network device
572 static inline void txqs_stop(struct net_device *netdev)
574 if (netif_is_multiqueue(netdev)) {
577 for (i = 0; i < netdev->num_tx_queues; i++)
578 netif_stop_subqueue(netdev, i);
580 netif_stop_queue(netdev);
585 * \brief Start Tx queues
586 * @param netdev network device
588 static inline void txqs_start(struct net_device *netdev)
590 if (netif_is_multiqueue(netdev)) {
593 for (i = 0; i < netdev->num_tx_queues; i++)
594 netif_start_subqueue(netdev, i);
596 netif_start_queue(netdev);
601 * \brief Wake Tx queues
602 * @param netdev network device
604 static inline void txqs_wake(struct net_device *netdev)
606 struct lio *lio = GET_LIO(netdev);
608 if (netif_is_multiqueue(netdev)) {
611 for (i = 0; i < netdev->num_tx_queues; i++) {
612 int qno = lio->linfo.txpciq[i %
613 (lio->linfo.num_txpciq)].s.q_no;
615 if (__netif_subqueue_stopped(netdev, i)) {
616 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
618 netif_wake_subqueue(netdev, i);
622 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
624 netif_wake_queue(netdev);
629 * \brief Stop Tx queue
630 * @param netdev network device
632 static void stop_txq(struct net_device *netdev)
638 * \brief Start Tx queue
639 * @param netdev network device
641 static void start_txq(struct net_device *netdev)
643 struct lio *lio = GET_LIO(netdev);
645 if (lio->linfo.link.s.link_up) {
652 * \brief Wake a queue
653 * @param netdev network device
654 * @param q which queue to wake
656 static inline void wake_q(struct net_device *netdev, int q)
658 if (netif_is_multiqueue(netdev))
659 netif_wake_subqueue(netdev, q);
661 netif_wake_queue(netdev);
665 * \brief Stop a queue
666 * @param netdev network device
667 * @param q which queue to stop
669 static inline void stop_q(struct net_device *netdev, int q)
671 if (netif_is_multiqueue(netdev))
672 netif_stop_subqueue(netdev, q);
674 netif_stop_queue(netdev);
678 * \brief Check Tx queue status, and take appropriate action
679 * @param lio per-network private data
680 * @returns 0 if full, number of queues woken up otherwise
682 static inline int check_txq_status(struct lio *lio)
686 if (netif_is_multiqueue(lio->netdev)) {
687 int numqs = lio->netdev->num_tx_queues;
690 /* check each sub-queue state */
691 for (q = 0; q < numqs; q++) {
692 iq = lio->linfo.txpciq[q %
693 (lio->linfo.num_txpciq)].s.q_no;
694 if (octnet_iq_is_full(lio->oct_dev, iq))
696 if (__netif_subqueue_stopped(lio->netdev, q)) {
697 wake_q(lio->netdev, q);
698 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
704 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
706 wake_q(lio->netdev, lio->txq);
707 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
715 * Remove the node at the head of the list. The list would be empty at
716 * the end of this call if there are no more nodes in the list.
718 static inline struct list_head *list_delete_head(struct list_head *root)
720 struct list_head *node;
722 if ((root->prev == root) && (root->next == root))
734 * \brief Delete gather lists
735 * @param lio per-network private data
737 static void delete_glists(struct lio *lio)
739 struct octnic_gather *g;
745 for (i = 0; i < lio->linfo.num_txpciq; i++) {
747 g = (struct octnic_gather *)
748 list_delete_head(&lio->glist[i]);
751 dma_unmap_single(&lio->oct_dev->
756 kfree((void *)((unsigned long)g->sg -
764 kfree((void *)lio->glist);
768 * \brief Setup gather lists
769 * @param lio per-network private data
771 static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
774 struct octnic_gather *g;
776 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
778 if (!lio->glist_lock)
781 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
784 kfree((void *)lio->glist_lock);
788 for (i = 0; i < num_iqs; i++) {
789 int numa_node = cpu_to_node(i % num_online_cpus());
791 spin_lock_init(&lio->glist_lock[i]);
793 INIT_LIST_HEAD(&lio->glist[i]);
795 for (j = 0; j < lio->tx_qsize; j++) {
796 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
799 g = kzalloc(sizeof(*g), GFP_KERNEL);
803 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
806 g->sg = kmalloc_node(g->sg_size + 8,
807 GFP_KERNEL, numa_node);
809 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
815 /* The gather component should be aligned on 64-bit
818 if (((unsigned long)g->sg) & 7) {
819 g->adjust = 8 - (((unsigned long)g->sg) & 7);
820 g->sg = (struct octeon_sg_entry *)
821 ((unsigned long)g->sg + g->adjust);
823 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
826 if (dma_mapping_error(&oct->pci_dev->dev,
828 kfree((void *)((unsigned long)g->sg -
834 list_add_tail(&g->list, &lio->glist[i]);
837 if (j != lio->tx_qsize) {
847 * \brief Print link information
848 * @param netdev network device
850 static void print_link_info(struct net_device *netdev)
852 struct lio *lio = GET_LIO(netdev);
854 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
855 struct oct_link_info *linfo = &lio->linfo;
857 if (linfo->link.s.link_up) {
858 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
860 (linfo->link.s.duplex) ? "Full" : "Half");
862 netif_info(lio, link, lio->netdev, "Link Down\n");
868 * \brief Routine to notify MTU change
869 * @param work work_struct data structure
871 static void octnet_link_status_change(struct work_struct *work)
873 struct cavium_wk *wk = (struct cavium_wk *)work;
874 struct lio *lio = (struct lio *)wk->ctxptr;
877 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
882 * \brief Sets up the mtu status change work
883 * @param netdev network device
885 static inline int setup_link_status_change_wq(struct net_device *netdev)
887 struct lio *lio = GET_LIO(netdev);
888 struct octeon_device *oct = lio->oct_dev;
890 lio->link_status_wq.wq = alloc_workqueue("link-status",
892 if (!lio->link_status_wq.wq) {
893 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
896 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
897 octnet_link_status_change);
898 lio->link_status_wq.wk.ctxptr = lio;
903 static inline void cleanup_link_status_change_wq(struct net_device *netdev)
905 struct lio *lio = GET_LIO(netdev);
907 if (lio->link_status_wq.wq) {
908 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
909 destroy_workqueue(lio->link_status_wq.wq);
914 * \brief Update link status
915 * @param netdev network device
916 * @param ls link status structure
918 * Called on receipt of a link status response from the core application to
919 * update each interface's link status.
921 static inline void update_link_status(struct net_device *netdev,
922 union oct_link_status *ls)
924 struct lio *lio = GET_LIO(netdev);
925 int changed = (lio->linfo.link.u64 != ls->u64);
927 lio->linfo.link.u64 = ls->u64;
929 if ((lio->intf_open) && (changed)) {
930 print_link_info(netdev);
933 if (lio->linfo.link.s.link_up) {
934 netif_carrier_on(netdev);
935 /* start_txq(netdev); */
938 netif_carrier_off(netdev);
944 /* Runs in interrupt context. */
945 static void update_txq_status(struct octeon_device *oct, int iq_num)
947 struct net_device *netdev;
949 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
951 /*octeon_update_iq_read_idx(oct, iq);*/
953 netdev = oct->props[iq->ifidx].netdev;
955 /* This is needed because the first IQ does not have
956 * a netdev associated with it.
961 lio = GET_LIO(netdev);
962 if (netif_is_multiqueue(netdev)) {
963 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
964 lio->linfo.link.s.link_up &&
965 (!octnet_iq_is_full(oct, iq_num))) {
966 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
968 netif_wake_subqueue(netdev, iq->q_index);
970 if (!octnet_iq_is_full(oct, lio->txq)) {
971 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
974 wake_q(netdev, lio->txq);
981 int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
983 struct octeon_device *oct = droq->oct_dev;
984 struct octeon_device_priv *oct_priv =
985 (struct octeon_device_priv *)oct->priv;
987 if (droq->ops.poll_mode) {
988 droq->ops.napi_fn(droq);
990 if (ret & MSIX_PO_INT) {
991 tasklet_schedule(&oct_priv->droq_tasklet);
994 /* this will be flushed periodically by check iq db */
995 if (ret & MSIX_PI_INT)
1002 * \brief Droq packet processor sceduler
1003 * @param oct octeon device
1006 void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
1008 struct octeon_device_priv *oct_priv =
1009 (struct octeon_device_priv *)oct->priv;
1011 struct octeon_droq *droq;
1013 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
1014 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1016 if (!(oct->droq_intr & (1ULL << oq_no)))
1019 droq = oct->droq[oq_no];
1021 if (droq->ops.poll_mode) {
1022 droq->ops.napi_fn(droq);
1023 oct_priv->napi_mask |= (1 << oq_no);
1025 tasklet_schedule(&oct_priv->droq_tasklet);
1032 liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1035 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1036 struct octeon_device *oct = ioq_vector->oct_dev;
1037 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1039 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1041 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1042 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1048 * \brief Interrupt handler for octeon
1050 * @param dev octeon device
1053 irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1056 struct octeon_device *oct = (struct octeon_device *)dev;
1059 /* Disable our interrupts for the duration of ISR */
1060 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
1062 ret = oct->fn_list.process_interrupt_regs(oct);
1064 if (ret == IRQ_HANDLED)
1065 liquidio_schedule_droq_pkt_handlers(oct);
1067 /* Re-enable our interrupts */
1068 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
1069 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
1075 * \brief Setup interrupt for octeon device
1076 * @param oct octeon device
1078 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1080 static int octeon_setup_interrupt(struct octeon_device *oct)
1083 struct msix_entry *msix_entries;
1085 int num_ioq_vectors;
1086 int num_alloc_ioq_vectors;
1088 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1089 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1090 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1091 oct->num_msix_irqs += 1;
1093 oct->msix_entries = kcalloc(
1094 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
1095 if (!oct->msix_entries)
1098 msix_entries = (struct msix_entry *)oct->msix_entries;
1099 /*Assumption is that pf msix vectors start from pf srn to pf to
1100 * trs and not from 0. if not change this code
1102 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1103 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1104 msix_entries[oct->num_msix_irqs - 1].entry =
1105 oct->sriov_info.trs;
1106 num_alloc_ioq_vectors = pci_enable_msix_range(
1107 oct->pci_dev, msix_entries,
1109 oct->num_msix_irqs);
1110 if (num_alloc_ioq_vectors < 0) {
1111 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1112 kfree(oct->msix_entries);
1113 oct->msix_entries = NULL;
1116 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1118 num_ioq_vectors = oct->num_msix_irqs;
1120 /** For PF, there is one non-ioq interrupt handler */
1121 num_ioq_vectors -= 1;
1122 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
1123 liquidio_legacy_intr_handler, 0, "octeon",
1126 dev_err(&oct->pci_dev->dev,
1127 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1129 pci_disable_msix(oct->pci_dev);
1130 kfree(oct->msix_entries);
1131 oct->msix_entries = NULL;
1135 for (i = 0; i < num_ioq_vectors; i++) {
1136 irqret = request_irq(msix_entries[i].vector,
1137 liquidio_msix_intr_handler, 0,
1138 "octeon", &oct->ioq_vector[i]);
1140 dev_err(&oct->pci_dev->dev,
1141 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1143 /** Freeing the non-ioq irq vector here . */
1144 free_irq(msix_entries[num_ioq_vectors].vector,
1149 /** clearing affinity mask. */
1150 irq_set_affinity_hint(
1151 msix_entries[i].vector, NULL);
1152 free_irq(msix_entries[i].vector,
1153 &oct->ioq_vector[i]);
1155 pci_disable_msix(oct->pci_dev);
1156 kfree(oct->msix_entries);
1157 oct->msix_entries = NULL;
1160 oct->ioq_vector[i].vector = msix_entries[i].vector;
1161 /* assign the cpu mask for this msix interrupt vector */
1162 irq_set_affinity_hint(
1163 msix_entries[i].vector,
1164 (&oct->ioq_vector[i].affinity_mask));
1166 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1169 err = pci_enable_msi(oct->pci_dev);
1171 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1174 oct->flags |= LIO_FLAG_MSI_ENABLED;
1176 irqret = request_irq(oct->pci_dev->irq,
1177 liquidio_legacy_intr_handler, IRQF_SHARED,
1180 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1181 pci_disable_msi(oct->pci_dev);
1182 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1191 * \brief PCI probe handler
1192 * @param pdev PCI device structure
1196 liquidio_probe(struct pci_dev *pdev,
1197 const struct pci_device_id *ent __attribute__((unused)))
1199 struct octeon_device *oct_dev = NULL;
1200 struct handshake *hs;
1202 oct_dev = octeon_allocate_device(pdev->device,
1203 sizeof(struct octeon_device_priv));
1205 dev_err(&pdev->dev, "Unable to allocate device\n");
1209 if (pdev->device == OCTEON_CN23XX_PF_VID)
1210 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1212 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1213 (u32)pdev->vendor, (u32)pdev->device);
1215 /* Assign octeon_device for this device to the private data area. */
1216 pci_set_drvdata(pdev, oct_dev);
1218 /* set linux specific device pointer */
1219 oct_dev->pci_dev = (void *)pdev;
1221 hs = &handshake[oct_dev->octeon_id];
1222 init_completion(&hs->init);
1223 init_completion(&hs->started);
1226 if (oct_dev->octeon_id == 0)
1227 /* first LiquidIO NIC is detected */
1228 complete(&first_stage);
1230 if (octeon_device_init(oct_dev)) {
1231 liquidio_remove(pdev);
1235 oct_dev->rx_pause = 1;
1236 oct_dev->tx_pause = 1;
1238 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1244 *\brief Destroy resources associated with octeon device
1245 * @param pdev PCI device structure
1248 static void octeon_destroy_resources(struct octeon_device *oct)
1251 struct msix_entry *msix_entries;
1252 struct octeon_device_priv *oct_priv =
1253 (struct octeon_device_priv *)oct->priv;
1255 struct handshake *hs;
1257 switch (atomic_read(&oct->status)) {
1258 case OCT_DEV_RUNNING:
1259 case OCT_DEV_CORE_OK:
1261 /* No more instructions will be forwarded. */
1262 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1264 oct->app_mode = CVM_DRV_INVALID_APP;
1265 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1266 lio_get_state_string(&oct->status));
1268 schedule_timeout_uninterruptible(HZ / 10);
1271 case OCT_DEV_HOST_OK:
1274 case OCT_DEV_CONSOLE_INIT_DONE:
1275 /* Remove any consoles */
1276 octeon_remove_consoles(oct);
1279 case OCT_DEV_IO_QUEUES_DONE:
1280 if (wait_for_pending_requests(oct))
1281 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1283 if (lio_wait_for_instr_fetch(oct))
1284 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1286 /* Disable the input and output queues now. No more packets will
1287 * arrive from Octeon, but we should wait for all packet
1288 * processing to finish.
1290 oct->fn_list.disable_io_queues(oct);
1292 if (lio_wait_for_oq_pkts(oct))
1293 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1295 /* Disable interrupts */
1296 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
1299 msix_entries = (struct msix_entry *)oct->msix_entries;
1300 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1301 /* clear the affinity_cpumask */
1302 irq_set_affinity_hint(msix_entries[i].vector,
1304 free_irq(msix_entries[i].vector,
1305 &oct->ioq_vector[i]);
1307 /* non-iov vector's argument is oct struct */
1308 free_irq(msix_entries[i].vector, oct);
1310 pci_disable_msix(oct->pci_dev);
1311 kfree(oct->msix_entries);
1312 oct->msix_entries = NULL;
1314 /* Release the interrupt line */
1315 free_irq(oct->pci_dev->irq, oct);
1317 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1318 pci_disable_msi(oct->pci_dev);
1321 if (OCTEON_CN23XX_PF(oct))
1322 octeon_free_ioq_vector(oct);
1324 case OCT_DEV_IN_RESET:
1325 case OCT_DEV_DROQ_INIT_DONE:
1326 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1328 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
1329 if (!(oct->io_qmask.oq & BIT_ULL(i)))
1331 octeon_delete_droq(oct, i);
1334 /* Force any pending handshakes to complete */
1335 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1339 handshake[oct->octeon_id].init_ok = 0;
1340 complete(&handshake[oct->octeon_id].init);
1341 handshake[oct->octeon_id].started_ok = 0;
1342 complete(&handshake[oct->octeon_id].started);
1347 case OCT_DEV_RESP_LIST_INIT_DONE:
1348 octeon_delete_response_list(oct);
1351 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
1352 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
1353 if (!(oct->io_qmask.iq & BIT_ULL(i)))
1355 octeon_delete_instr_queue(oct, i);
1358 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1359 octeon_free_sc_buffer_pool(oct);
1362 case OCT_DEV_DISPATCH_INIT_DONE:
1363 octeon_delete_dispatch_list(oct);
1364 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1367 case OCT_DEV_PCI_MAP_DONE:
1368 /* Soft reset the octeon device before exiting */
1369 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1370 oct->fn_list.soft_reset(oct);
1372 octeon_unmap_pci_barx(oct, 0);
1373 octeon_unmap_pci_barx(oct, 1);
1376 case OCT_DEV_BEGIN_STATE:
1377 /* Disable the device, releasing the PCI INT */
1378 pci_disable_device(oct->pci_dev);
1380 /* Nothing to be done here either */
1382 } /* end switch (oct->status) */
1384 tasklet_kill(&oct_priv->droq_tasklet);
1388 * \brief Callback for rx ctrl
1389 * @param status status of request
1390 * @param buf pointer to resp structure
1392 static void rx_ctl_callback(struct octeon_device *oct,
1396 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1397 struct liquidio_rx_ctl_context *ctx;
1399 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1401 oct = lio_get_device(ctx->octeon_id);
1403 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1404 CVM_CAST64(status));
1405 WRITE_ONCE(ctx->cond, 1);
1407 /* This barrier is required to be sure that the response has been
1408 * written fully before waking up the handler
1412 wake_up_interruptible(&ctx->wc);
1416 * \brief Send Rx control command
1417 * @param lio per-network private data
1418 * @param start_stop whether to start or stop
1420 static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1422 struct octeon_soft_command *sc;
1423 struct liquidio_rx_ctl_context *ctx;
1424 union octnet_cmd *ncmd;
1425 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1426 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1429 if (oct->props[lio->ifidx].rx_on == start_stop)
1432 sc = (struct octeon_soft_command *)
1433 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1436 ncmd = (union octnet_cmd *)sc->virtdptr;
1437 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1439 WRITE_ONCE(ctx->cond, 0);
1440 ctx->octeon_id = lio_get_device_id(oct);
1441 init_waitqueue_head(&ctx->wc);
1444 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1445 ncmd->s.param1 = start_stop;
1447 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1449 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1451 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1452 OPCODE_NIC_CMD, 0, 0, 0);
1454 sc->callback = rx_ctl_callback;
1455 sc->callback_arg = sc;
1456 sc->wait_time = 5000;
1458 retval = octeon_send_soft_command(oct, sc);
1459 if (retval == IQ_SEND_FAILED) {
1460 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1462 /* Sleep on a wait queue till the cond flag indicates that the
1463 * response arrived or timed-out.
1465 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1467 oct->props[lio->ifidx].rx_on = start_stop;
1470 octeon_free_soft_command(oct, sc);
1474 * \brief Destroy NIC device interface
1475 * @param oct octeon device
1476 * @param ifidx which interface to destroy
1478 * Cleanup associated with each interface for an Octeon device when NIC
1479 * module is being unloaded or if initialization fails during load.
1481 static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1483 struct net_device *netdev = oct->props[ifidx].netdev;
1485 struct napi_struct *napi, *n;
1488 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1493 lio = GET_LIO(netdev);
1495 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1497 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1498 liquidio_stop(netdev);
1500 if (oct->props[lio->ifidx].napi_enabled == 1) {
1501 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1504 oct->props[lio->ifidx].napi_enabled = 0;
1506 if (OCTEON_CN23XX_PF(oct))
1507 oct->droq[0]->ops.poll_mode = 0;
1510 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1511 unregister_netdev(netdev);
1513 cleanup_link_status_change_wq(netdev);
1517 free_netdev(netdev);
1519 oct->props[ifidx].gmxport = -1;
1521 oct->props[ifidx].netdev = NULL;
1525 * \brief Stop complete NIC functionality
1526 * @param oct octeon device
1528 static int liquidio_stop_nic_module(struct octeon_device *oct)
1533 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1534 if (!oct->ifcount) {
1535 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1539 spin_lock_bh(&oct->cmd_resp_wqlock);
1540 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1541 spin_unlock_bh(&oct->cmd_resp_wqlock);
1543 for (i = 0; i < oct->ifcount; i++) {
1544 lio = GET_LIO(oct->props[i].netdev);
1545 for (j = 0; j < lio->linfo.num_rxpciq; j++)
1546 octeon_unregister_droq_ops(oct,
1547 lio->linfo.rxpciq[j].s.q_no);
1550 for (i = 0; i < oct->ifcount; i++)
1551 liquidio_destroy_nic_device(oct, i);
1553 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1558 * \brief Cleans up resources at unload time
1559 * @param pdev PCI device structure
1561 static void liquidio_remove(struct pci_dev *pdev)
1563 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1565 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1567 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1568 liquidio_stop_nic_module(oct_dev);
1570 /* Reset the octeon device and cleanup all memory allocated for
1571 * the octeon device by driver.
1573 octeon_destroy_resources(oct_dev);
1575 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1577 /* This octeon device has been removed. Update the global
1578 * data structure to reflect this. Free the device structure.
1580 octeon_free_device_mem(oct_dev);
1584 * \brief Identify the Octeon device and to map the BAR address space
1585 * @param oct octeon device
1587 static int octeon_chip_specific_setup(struct octeon_device *oct)
1593 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1594 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1595 oct->rev_id = rev_id & 0xff;
1598 case OCTEON_CN68XX_PCIID:
1599 oct->chip_id = OCTEON_CN68XX;
1600 ret = lio_setup_cn68xx_octeon_device(oct);
1604 case OCTEON_CN66XX_PCIID:
1605 oct->chip_id = OCTEON_CN66XX;
1606 ret = lio_setup_cn66xx_octeon_device(oct);
1610 case OCTEON_CN23XX_PCIID_PF:
1611 oct->chip_id = OCTEON_CN23XX_PF_VID;
1612 ret = setup_cn23xx_octeon_pf_device(oct);
1618 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1623 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
1624 OCTEON_MAJOR_REV(oct),
1625 OCTEON_MINOR_REV(oct),
1626 octeon_get_conf(oct)->card_name,
1633 * \brief PCI initialization for each Octeon device.
1634 * @param oct octeon device
1636 static int octeon_pci_os_setup(struct octeon_device *oct)
1638 /* setup PCI stuff first */
1639 if (pci_enable_device(oct->pci_dev)) {
1640 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1644 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1645 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1649 /* Enable PCI DMA Master. */
1650 pci_set_master(oct->pci_dev);
1655 static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1659 if (netif_is_multiqueue(lio->netdev))
1660 q = skb->queue_mapping % lio->linfo.num_txpciq;
1666 * \brief Check Tx queue state for a given network buffer
1667 * @param lio per-network private data
1668 * @param skb network buffer
1670 static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1674 if (netif_is_multiqueue(lio->netdev)) {
1675 q = skb->queue_mapping;
1676 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
1682 if (octnet_iq_is_full(lio->oct_dev, iq))
1685 if (__netif_subqueue_stopped(lio->netdev, q)) {
1686 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
1687 wake_q(lio->netdev, q);
1693 * \brief Unmap and free network buffer
1696 static void free_netbuf(void *buf)
1698 struct sk_buff *skb;
1699 struct octnet_buf_free_info *finfo;
1702 finfo = (struct octnet_buf_free_info *)buf;
1706 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1709 check_txq_state(lio, skb);
1711 tx_buffer_free(skb);
1715 * \brief Unmap and free gather buffer
1718 static void free_netsgbuf(void *buf)
1720 struct octnet_buf_free_info *finfo;
1721 struct sk_buff *skb;
1723 struct octnic_gather *g;
1726 finfo = (struct octnet_buf_free_info *)buf;
1730 frags = skb_shinfo(skb)->nr_frags;
1732 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1733 g->sg[0].ptr[0], (skb->len - skb->data_len),
1738 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1740 pci_unmap_page((lio->oct_dev)->pci_dev,
1741 g->sg[(i >> 2)].ptr[(i & 3)],
1742 frag->size, DMA_TO_DEVICE);
1746 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1747 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
1749 iq = skb_iq(lio, skb);
1750 spin_lock(&lio->glist_lock[iq]);
1751 list_add_tail(&g->list, &lio->glist[iq]);
1752 spin_unlock(&lio->glist_lock[iq]);
1754 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1756 tx_buffer_free(skb);
1760 * \brief Unmap and free gather buffer with response
1763 static void free_netsgbuf_with_resp(void *buf)
1765 struct octeon_soft_command *sc;
1766 struct octnet_buf_free_info *finfo;
1767 struct sk_buff *skb;
1769 struct octnic_gather *g;
1772 sc = (struct octeon_soft_command *)buf;
1773 skb = (struct sk_buff *)sc->callback_arg;
1774 finfo = (struct octnet_buf_free_info *)&skb->cb;
1778 frags = skb_shinfo(skb)->nr_frags;
1780 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1781 g->sg[0].ptr[0], (skb->len - skb->data_len),
1786 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1788 pci_unmap_page((lio->oct_dev)->pci_dev,
1789 g->sg[(i >> 2)].ptr[(i & 3)],
1790 frag->size, DMA_TO_DEVICE);
1794 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1795 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
1797 iq = skb_iq(lio, skb);
1799 spin_lock(&lio->glist_lock[iq]);
1800 list_add_tail(&g->list, &lio->glist[iq]);
1801 spin_unlock(&lio->glist_lock[iq]);
1803 /* Don't free the skb yet */
1805 check_txq_state(lio, skb);
1809 * \brief Adjust ptp frequency
1810 * @param ptp PTP clock info
1811 * @param ppb how much to adjust by, in parts-per-billion
1813 static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1815 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1816 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1818 unsigned long flags;
1819 bool neg_adj = false;
1826 /* The hardware adds the clock compensation value to the
1827 * PTP clock on every coprocessor clock cycle, so we
1828 * compute the delta in terms of coprocessor clocks.
1830 delta = (u64)ppb << 32;
1831 do_div(delta, oct->coproc_clock_rate);
1833 spin_lock_irqsave(&lio->ptp_lock, flags);
1834 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1839 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1840 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1846 * \brief Adjust ptp time
1847 * @param ptp PTP clock info
1848 * @param delta how much to adjust by, in nanosecs
1850 static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1852 unsigned long flags;
1853 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1855 spin_lock_irqsave(&lio->ptp_lock, flags);
1856 lio->ptp_adjust += delta;
1857 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1863 * \brief Get hardware clock time, including any adjustment
1864 * @param ptp PTP clock info
1865 * @param ts timespec
1867 static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1868 struct timespec64 *ts)
1871 unsigned long flags;
1872 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1873 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1875 spin_lock_irqsave(&lio->ptp_lock, flags);
1876 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1877 ns += lio->ptp_adjust;
1878 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1880 *ts = ns_to_timespec64(ns);
1886 * \brief Set hardware clock time. Reset adjustment
1887 * @param ptp PTP clock info
1888 * @param ts timespec
1890 static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1891 const struct timespec64 *ts)
1894 unsigned long flags;
1895 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1896 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1898 ns = timespec_to_ns(ts);
1900 spin_lock_irqsave(&lio->ptp_lock, flags);
1901 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1902 lio->ptp_adjust = 0;
1903 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1909 * \brief Check if PTP is enabled
1910 * @param ptp PTP clock info
1912 * @param on is it on
1915 liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
1916 struct ptp_clock_request *rq __attribute__((unused)),
1917 int on __attribute__((unused)))
1923 * \brief Open PTP clock source
1924 * @param netdev network device
1926 static void oct_ptp_open(struct net_device *netdev)
1928 struct lio *lio = GET_LIO(netdev);
1929 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1931 spin_lock_init(&lio->ptp_lock);
1933 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1934 lio->ptp_info.owner = THIS_MODULE;
1935 lio->ptp_info.max_adj = 250000000;
1936 lio->ptp_info.n_alarm = 0;
1937 lio->ptp_info.n_ext_ts = 0;
1938 lio->ptp_info.n_per_out = 0;
1939 lio->ptp_info.pps = 0;
1940 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1941 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1942 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1943 lio->ptp_info.settime64 = liquidio_ptp_settime;
1944 lio->ptp_info.enable = liquidio_ptp_enable;
1946 lio->ptp_adjust = 0;
1948 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1949 &oct->pci_dev->dev);
1951 if (IS_ERR(lio->ptp_clock))
1952 lio->ptp_clock = NULL;
1956 * \brief Init PTP clock
1957 * @param oct octeon device
1959 static void liquidio_ptp_init(struct octeon_device *oct)
1961 u64 clock_comp, cfg;
1963 clock_comp = (u64)NSEC_PER_SEC << 32;
1964 do_div(clock_comp, oct->coproc_clock_rate);
1965 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1968 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1969 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1973 * \brief Load firmware to device
1974 * @param oct octeon device
1976 * Maps device to firmware filename, requests firmware, and downloads it
1978 static int load_firmware(struct octeon_device *oct)
1981 const struct firmware *fw;
1982 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1985 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1986 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1987 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1991 if (fw_type[0] == '\0')
1992 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1994 tmp_fw_type = fw_type;
1996 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1997 octeon_get_conf(oct)->card_name, tmp_fw_type,
1998 LIO_FW_NAME_SUFFIX);
2000 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2002 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2004 release_firmware(fw);
2008 ret = octeon_download_firmware(oct, fw->data, fw->size);
2010 release_firmware(fw);
2016 * \brief Setup output queue
2017 * @param oct octeon device
2018 * @param q_no which queue
2019 * @param num_descs how many descriptors
2020 * @param desc_size size of each descriptor
2021 * @param app_ctx application context
2023 static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2024 int desc_size, void *app_ctx)
2028 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2029 /* droq creation and local register settings. */
2030 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
2035 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2038 /* tasklet creation for the droq */
2040 /* Enable the droq queues */
2041 octeon_set_droq_pkt_op(oct, q_no, 1);
2043 /* Send Credit for Octeon Output queues. Credits are always
2044 * sent after the output queue is enabled.
2046 writel(oct->droq[q_no]->max_count,
2047 oct->droq[q_no]->pkts_credit_reg);
2053 * \brief Callback for getting interface configuration
2054 * @param status status of request
2055 * @param buf pointer to resp structure
2057 static void if_cfg_callback(struct octeon_device *oct,
2058 u32 status __attribute__((unused)),
2061 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2062 struct liquidio_if_cfg_resp *resp;
2063 struct liquidio_if_cfg_context *ctx;
2065 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
2066 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
2068 oct = lio_get_device(ctx->octeon_id);
2070 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
2071 CVM_CAST64(resp->status));
2072 WRITE_ONCE(ctx->cond, 1);
2074 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2075 resp->cfg_info.liquidio_firmware_version);
2077 /* This barrier is required to be sure that the response has been
2078 * written fully before waking up the handler
2082 wake_up_interruptible(&ctx->wc);
2086 * \brief Select queue based on hash
2087 * @param dev Net device
2088 * @param skb sk_buff structure
2089 * @returns selected queue number
2091 static u16 select_q(struct net_device *dev, struct sk_buff *skb,
2092 void *accel_priv __attribute__((unused)),
2093 select_queue_fallback_t fallback __attribute__((unused)))
2099 qindex = skb_tx_hash(dev, skb);
2101 return (u16)(qindex % (lio->linfo.num_txpciq));
2104 /** Routine to push packets arriving on Octeon interface upto network layer.
2105 * @param oct_id - octeon device id.
2106 * @param skbuff - skbuff struct to be passed to network layer.
2107 * @param len - size of total data received.
2108 * @param rh - Control header associated with the packet
2109 * @param param - additional control data with the packet
2110 * @param arg - farg registered in droq_ops
2113 liquidio_push_packet(u32 octeon_id __attribute__((unused)),
2116 union octeon_rh *rh,
2120 struct napi_struct *napi = param;
2121 struct sk_buff *skb = (struct sk_buff *)skbuff;
2122 struct skb_shared_hwtstamps *shhwtstamps;
2125 struct net_device *netdev = (struct net_device *)arg;
2126 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2129 int packet_was_received;
2130 struct lio *lio = GET_LIO(netdev);
2131 struct octeon_device *oct = lio->oct_dev;
2133 /* Do not proceed if the interface is not in RUNNING state. */
2134 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2135 recv_buffer_free(skb);
2136 droq->stats.rx_dropped++;
2142 skb_record_rx_queue(skb, droq->q_no);
2143 if (likely(len > MIN_SKB_SIZE)) {
2144 struct octeon_skb_page_info *pg_info;
2147 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2148 if (pg_info->page) {
2149 /* For Paged allocation use the frags */
2150 va = page_address(pg_info->page) +
2151 pg_info->page_offset;
2152 memcpy(skb->data, va, MIN_SKB_SIZE);
2153 skb_put(skb, MIN_SKB_SIZE);
2154 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2156 pg_info->page_offset +
2162 struct octeon_skb_page_info *pg_info =
2163 ((struct octeon_skb_page_info *)(skb->cb));
2164 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2165 + pg_info->page_offset, len);
2167 put_page(pg_info->page);
2170 if (((oct->chip_id == OCTEON_CN66XX) ||
2171 (oct->chip_id == OCTEON_CN68XX)) &&
2173 if (rh->r_dh.has_hwtstamp) {
2174 /* timestamp is included from the hardware at
2175 * the beginning of the packet.
2178 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2179 /* Nanoseconds are in the first 64-bits
2182 memcpy(&ns, (skb->data), sizeof(ns));
2183 shhwtstamps = skb_hwtstamps(skb);
2184 shhwtstamps->hwtstamp =
2188 skb_pull(skb, sizeof(ns));
2192 skb->protocol = eth_type_trans(skb, skb->dev);
2193 if ((netdev->features & NETIF_F_RXCSUM) &&
2194 (((rh->r_dh.encap_on) &&
2195 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2196 (!(rh->r_dh.encap_on) &&
2197 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
2198 /* checksum has already been verified */
2199 skb->ip_summed = CHECKSUM_UNNECESSARY;
2201 skb->ip_summed = CHECKSUM_NONE;
2203 /* Setting Encapsulation field on basis of status received
2206 if (rh->r_dh.encap_on) {
2207 skb->encapsulation = 1;
2208 skb->csum_level = 1;
2209 droq->stats.rx_vxlan++;
2212 /* inbound VLAN tag */
2213 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2214 (rh->r_dh.vlan != 0)) {
2215 u16 vid = rh->r_dh.vlan;
2216 u16 priority = rh->r_dh.priority;
2218 vtag = priority << 13 | vid;
2219 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2222 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2224 if (packet_was_received) {
2225 droq->stats.rx_bytes_received += len;
2226 droq->stats.rx_pkts_received++;
2227 netdev->last_rx = jiffies;
2229 droq->stats.rx_dropped++;
2230 netif_info(lio, rx_err, lio->netdev,
2231 "droq:%d error rx_dropped:%llu\n",
2232 droq->q_no, droq->stats.rx_dropped);
2236 recv_buffer_free(skb);
2241 * \brief wrapper for calling napi_schedule
2242 * @param param parameters to pass to napi_schedule
2244 * Used when scheduling on different CPUs
2246 static void napi_schedule_wrapper(void *param)
2248 struct napi_struct *napi = param;
2250 napi_schedule(napi);
2254 * \brief callback when receive interrupt occurs and we are in NAPI mode
2255 * @param arg pointer to octeon output queue
2257 static void liquidio_napi_drv_callback(void *arg)
2259 struct octeon_droq *droq = arg;
2260 int this_cpu = smp_processor_id();
2262 if (droq->cpu_id == this_cpu) {
2263 napi_schedule(&droq->napi);
2265 struct call_single_data *csd = &droq->csd;
2267 csd->func = napi_schedule_wrapper;
2268 csd->info = &droq->napi;
2271 smp_call_function_single_async(droq->cpu_id, csd);
2276 * \brief Entry point for NAPI polling
2277 * @param napi NAPI structure
2278 * @param budget maximum number of items to process
2280 static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2282 struct octeon_droq *droq;
2284 int tx_done = 0, iq_no;
2285 struct octeon_instr_queue *iq;
2286 struct octeon_device *oct;
2288 droq = container_of(napi, struct octeon_droq, napi);
2289 oct = droq->oct_dev;
2291 /* Handle Droq descriptors */
2292 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2293 POLL_EVENT_PROCESS_PKTS,
2296 /* Flush the instruction queue */
2297 iq = oct->instr_queue[iq_no];
2299 /* Process iq buffers with in the budget limits */
2300 tx_done = octeon_flush_iq(oct, iq, 1, budget);
2301 /* Update iq read-index rather than waiting for next interrupt.
2302 * Return back if tx_done is false.
2304 update_txq_status(oct, iq_no);
2305 /*tx_done = (iq->flush_index == iq->octeon_read_index);*/
2307 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2311 if ((work_done < budget) && (tx_done)) {
2312 napi_complete(napi);
2313 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2314 POLL_EVENT_ENABLE_INTR, 0);
2318 return (!tx_done) ? (budget) : (work_done);
2322 * \brief Setup input and output queues
2323 * @param octeon_dev octeon device
2324 * @param ifidx Interface Index
2326 * Note: Queues are with respect to the octeon device. Thus
2327 * an input queue is for egress packets, and output queues
2328 * are for ingress packets.
2330 static inline int setup_io_queues(struct octeon_device *octeon_dev,
2333 struct octeon_droq_ops droq_ops;
2334 struct net_device *netdev;
2336 static int cpu_id_modulus;
2337 struct octeon_droq *droq;
2338 struct napi_struct *napi;
2339 int q, q_no, retval = 0;
2343 netdev = octeon_dev->props[ifidx].netdev;
2345 lio = GET_LIO(netdev);
2347 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2349 droq_ops.fptr = liquidio_push_packet;
2350 droq_ops.farg = (void *)netdev;
2352 droq_ops.poll_mode = 1;
2353 droq_ops.napi_fn = liquidio_napi_drv_callback;
2355 cpu_id_modulus = num_present_cpus();
2358 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
2359 q_no = lio->linfo.rxpciq[q].s.q_no;
2360 dev_dbg(&octeon_dev->pci_dev->dev,
2361 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2363 retval = octeon_setup_droq(octeon_dev, q_no,
2364 CFG_GET_NUM_RX_DESCS_NIC_IF
2365 (octeon_get_conf(octeon_dev),
2367 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2368 (octeon_get_conf(octeon_dev),
2371 dev_err(&octeon_dev->pci_dev->dev,
2372 "%s : Runtime DROQ(RxQ) creation failed.\n",
2377 droq = octeon_dev->droq[q_no];
2379 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2380 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
2381 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
2383 /* designate a CPU for this droq */
2384 droq->cpu_id = cpu_id;
2386 if (cpu_id >= cpu_id_modulus)
2389 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2392 if (OCTEON_CN23XX_PF(octeon_dev)) {
2393 /* 23XX PF can receive control messages (via the first PF-owned
2394 * droq) from the firmware even if the ethX interface is down,
2395 * so that's why poll_mode must be off for the first droq.
2397 octeon_dev->droq[0]->ops.poll_mode = 0;
2401 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2402 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2405 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2406 lio->linfo.txpciq[q], num_tx_descs,
2407 netdev_get_tx_queue(netdev, q));
2409 dev_err(&octeon_dev->pci_dev->dev,
2410 " %s : Runtime IQ(TxQ) creation failed.\n",
2420 * \brief Poll routine for checking transmit queue status
2421 * @param work work_struct data structure
2423 static void octnet_poll_check_txq_status(struct work_struct *work)
2425 struct cavium_wk *wk = (struct cavium_wk *)work;
2426 struct lio *lio = (struct lio *)wk->ctxptr;
2428 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2431 check_txq_status(lio);
2432 queue_delayed_work(lio->txq_status_wq.wq,
2433 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2437 * \brief Sets up the txq poll check
2438 * @param netdev network device
2440 static inline int setup_tx_poll_fn(struct net_device *netdev)
2442 struct lio *lio = GET_LIO(netdev);
2443 struct octeon_device *oct = lio->oct_dev;
2445 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2447 if (!lio->txq_status_wq.wq) {
2448 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2451 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2452 octnet_poll_check_txq_status);
2453 lio->txq_status_wq.wk.ctxptr = lio;
2454 queue_delayed_work(lio->txq_status_wq.wq,
2455 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2459 static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2461 struct lio *lio = GET_LIO(netdev);
2463 if (lio->txq_status_wq.wq) {
2464 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2465 destroy_workqueue(lio->txq_status_wq.wq);
2470 * \brief Net device open for LiquidIO
2471 * @param netdev network device
2473 static int liquidio_open(struct net_device *netdev)
2475 struct lio *lio = GET_LIO(netdev);
2476 struct octeon_device *oct = lio->oct_dev;
2477 struct napi_struct *napi, *n;
2479 if (oct->props[lio->ifidx].napi_enabled == 0) {
2480 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2483 oct->props[lio->ifidx].napi_enabled = 1;
2485 if (OCTEON_CN23XX_PF(oct))
2486 oct->droq[0]->ops.poll_mode = 1;
2489 oct_ptp_open(netdev);
2491 ifstate_set(lio, LIO_IFSTATE_RUNNING);
2493 /* Ready for link status updates */
2496 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2498 if (OCTEON_CN23XX_PF(oct)) {
2500 if (setup_tx_poll_fn(netdev))
2503 if (setup_tx_poll_fn(netdev))
2509 /* tell Octeon to start forwarding packets to host */
2510 send_rx_ctrl_cmd(lio, 1);
2512 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2519 * \brief Net device stop for LiquidIO
2520 * @param netdev network device
2522 static int liquidio_stop(struct net_device *netdev)
2524 struct lio *lio = GET_LIO(netdev);
2525 struct octeon_device *oct = lio->oct_dev;
2527 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2529 netif_tx_disable(netdev);
2531 /* Inform that netif carrier is down */
2532 netif_carrier_off(netdev);
2534 lio->linfo.link.s.link_up = 0;
2535 lio->link_changes++;
2537 /* Pause for a moment and wait for Octeon to flush out (to the wire) any
2538 * egress packets that are in-flight.
2540 set_current_state(TASK_INTERRUPTIBLE);
2541 schedule_timeout(msecs_to_jiffies(100));
2543 /* Now it should be safe to tell Octeon that nic interface is down. */
2544 send_rx_ctrl_cmd(lio, 0);
2546 if (OCTEON_CN23XX_PF(oct)) {
2548 cleanup_tx_poll_fn(netdev);
2550 cleanup_tx_poll_fn(netdev);
2553 if (lio->ptp_clock) {
2554 ptp_clock_unregister(lio->ptp_clock);
2555 lio->ptp_clock = NULL;
2558 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2564 * \brief Converts a mask based on net device flags
2565 * @param netdev network device
2567 * This routine generates a octnet_ifflags mask from the net device flags
2568 * received from the OS.
2570 static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2572 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2574 if (netdev->flags & IFF_PROMISC)
2575 f |= OCTNET_IFFLAG_PROMISC;
2577 if (netdev->flags & IFF_ALLMULTI)
2578 f |= OCTNET_IFFLAG_ALLMULTI;
2580 if (netdev->flags & IFF_MULTICAST) {
2581 f |= OCTNET_IFFLAG_MULTICAST;
2583 /* Accept all multicast addresses if there are more than we
2586 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2587 f |= OCTNET_IFFLAG_ALLMULTI;
2590 if (netdev->flags & IFF_BROADCAST)
2591 f |= OCTNET_IFFLAG_BROADCAST;
2597 * \brief Net device set_multicast_list
2598 * @param netdev network device
2600 static void liquidio_set_mcast_list(struct net_device *netdev)
2602 struct lio *lio = GET_LIO(netdev);
2603 struct octeon_device *oct = lio->oct_dev;
2604 struct octnic_ctrl_pkt nctrl;
2605 struct netdev_hw_addr *ha;
2608 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2610 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2612 /* Create a ctrl pkt command to be sent to core app. */
2614 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
2615 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2616 nctrl.ncmd.s.param2 = mc_count;
2617 nctrl.ncmd.s.more = mc_count;
2618 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2619 nctrl.netpndev = (u64)netdev;
2620 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2622 /* copy all the addresses into the udd */
2624 netdev_for_each_mc_addr(ha, netdev) {
2626 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2627 /* no need to swap bytes */
2629 if (++mc > &nctrl.udd[mc_count])
2633 /* Apparently, any activity in this call from the kernel has to
2634 * be atomic. So we won't wait for response.
2636 nctrl.wait_time = 0;
2638 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2640 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2646 * \brief Net device set_mac_address
2647 * @param netdev network device
2649 static int liquidio_set_mac(struct net_device *netdev, void *p)
2652 struct lio *lio = GET_LIO(netdev);
2653 struct octeon_device *oct = lio->oct_dev;
2654 struct sockaddr *addr = (struct sockaddr *)p;
2655 struct octnic_ctrl_pkt nctrl;
2657 if (!is_valid_ether_addr(addr->sa_data))
2658 return -EADDRNOTAVAIL;
2660 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2663 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
2664 nctrl.ncmd.s.param1 = 0;
2665 nctrl.ncmd.s.more = 1;
2666 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2667 nctrl.netpndev = (u64)netdev;
2668 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2669 nctrl.wait_time = 100;
2672 /* The MAC Address is presented in network byte order. */
2673 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2675 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2677 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2680 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2681 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2687 * \brief Net device get_stats
2688 * @param netdev network device
2690 static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2692 struct lio *lio = GET_LIO(netdev);
2693 struct net_device_stats *stats = &netdev->stats;
2694 struct octeon_device *oct;
2695 u64 pkts = 0, drop = 0, bytes = 0;
2696 struct oct_droq_stats *oq_stats;
2697 struct oct_iq_stats *iq_stats;
2698 int i, iq_no, oq_no;
2702 for (i = 0; i < lio->linfo.num_txpciq; i++) {
2703 iq_no = lio->linfo.txpciq[i].s.q_no;
2704 iq_stats = &oct->instr_queue[iq_no]->stats;
2705 pkts += iq_stats->tx_done;
2706 drop += iq_stats->tx_dropped;
2707 bytes += iq_stats->tx_tot_bytes;
2710 stats->tx_packets = pkts;
2711 stats->tx_bytes = bytes;
2712 stats->tx_dropped = drop;
2718 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
2719 oq_no = lio->linfo.rxpciq[i].s.q_no;
2720 oq_stats = &oct->droq[oq_no]->stats;
2721 pkts += oq_stats->rx_pkts_received;
2722 drop += (oq_stats->rx_dropped +
2723 oq_stats->dropped_nodispatch +
2724 oq_stats->dropped_toomany +
2725 oq_stats->dropped_nomem);
2726 bytes += oq_stats->rx_bytes_received;
2729 stats->rx_bytes = bytes;
2730 stats->rx_packets = pkts;
2731 stats->rx_dropped = drop;
2737 * \brief Net device change_mtu
2738 * @param netdev network device
2740 static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2742 struct lio *lio = GET_LIO(netdev);
2743 struct octeon_device *oct = lio->oct_dev;
2744 struct octnic_ctrl_pkt nctrl;
2747 /* Limit the MTU to make sure the ethernet packets are between 68 bytes
2750 if ((new_mtu < LIO_MIN_MTU_SIZE) ||
2751 (new_mtu > LIO_MAX_MTU_SIZE)) {
2752 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2753 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
2754 LIO_MIN_MTU_SIZE, LIO_MAX_MTU_SIZE);
2758 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2761 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
2762 nctrl.ncmd.s.param1 = new_mtu;
2763 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2764 nctrl.wait_time = 100;
2765 nctrl.netpndev = (u64)netdev;
2766 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2768 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2770 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2780 * \brief Handler for SIOCSHWTSTAMP ioctl
2781 * @param netdev network device
2782 * @param ifr interface request
2783 * @param cmd command
2785 static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
2787 struct hwtstamp_config conf;
2788 struct lio *lio = GET_LIO(netdev);
2790 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2796 switch (conf.tx_type) {
2797 case HWTSTAMP_TX_ON:
2798 case HWTSTAMP_TX_OFF:
2804 switch (conf.rx_filter) {
2805 case HWTSTAMP_FILTER_NONE:
2807 case HWTSTAMP_FILTER_ALL:
2808 case HWTSTAMP_FILTER_SOME:
2809 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2810 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2811 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2812 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2813 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2814 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2815 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2816 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2817 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2818 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2819 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2820 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2821 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2827 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2828 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2831 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2833 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2837 * \brief ioctl handler
2838 * @param netdev network device
2839 * @param ifr interface request
2840 * @param cmd command
2842 static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2846 return hwtstamp_ioctl(netdev, ifr);
2853 * \brief handle a Tx timestamp response
2854 * @param status response status
2855 * @param buf pointer to skb
2857 static void handle_timestamp(struct octeon_device *oct,
2861 struct octnet_buf_free_info *finfo;
2862 struct octeon_soft_command *sc;
2863 struct oct_timestamp_resp *resp;
2865 struct sk_buff *skb = (struct sk_buff *)buf;
2867 finfo = (struct octnet_buf_free_info *)skb->cb;
2871 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2873 if (status != OCTEON_REQUEST_DONE) {
2874 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2875 CVM_CAST64(status));
2876 resp->timestamp = 0;
2879 octeon_swap_8B_data(&resp->timestamp, 1);
2881 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
2882 struct skb_shared_hwtstamps ts;
2883 u64 ns = resp->timestamp;
2885 netif_info(lio, tx_done, lio->netdev,
2886 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2887 skb, (unsigned long long)ns);
2888 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2889 skb_tstamp_tx(skb, &ts);
2892 octeon_free_soft_command(oct, sc);
2893 tx_buffer_free(skb);
2896 /* \brief Send a data packet that will be timestamped
2897 * @param oct octeon device
2898 * @param ndata pointer to network data
2899 * @param finfo pointer to private network data
2901 static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2902 struct octnic_data_pkt *ndata,
2903 struct octnet_buf_free_info *finfo)
2906 struct octeon_soft_command *sc;
2913 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2914 sizeof(struct oct_timestamp_resp));
2918 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2919 return IQ_SEND_FAILED;
2922 if (ndata->reqtype == REQTYPE_NORESP_NET)
2923 ndata->reqtype = REQTYPE_RESP_NET;
2924 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2925 ndata->reqtype = REQTYPE_RESP_NET_SG;
2927 sc->callback = handle_timestamp;
2928 sc->callback_arg = finfo->skb;
2929 sc->iq_no = ndata->q_no;
2931 if (OCTEON_CN23XX_PF(oct))
2932 len = (u32)((struct octeon_instr_ih3 *)
2933 (&sc->cmd.cmd3.ih3))->dlengsz;
2935 len = (u32)((struct octeon_instr_ih2 *)
2936 (&sc->cmd.cmd2.ih2))->dlengsz;
2940 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
2941 sc, len, ndata->reqtype);
2943 if (retval == IQ_SEND_FAILED) {
2944 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2946 octeon_free_soft_command(oct, sc);
2948 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2954 /** \brief Transmit networks packets to the Octeon interface
2955 * @param skbuff skbuff struct to be passed to network layer.
2956 * @param netdev pointer to network device
2957 * @returns whether the packet was transmitted to the device okay or not
2958 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2960 static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2963 struct octnet_buf_free_info *finfo;
2964 union octnic_cmd_setup cmdsetup;
2965 struct octnic_data_pkt ndata;
2966 struct octeon_device *oct;
2967 struct oct_iq_stats *stats;
2968 struct octeon_instr_irh *irh;
2969 union tx_info *tx_info;
2971 int q_idx = 0, iq_no = 0;
2976 lio = GET_LIO(netdev);
2979 if (netif_is_multiqueue(netdev)) {
2980 q_idx = skb->queue_mapping;
2981 q_idx = (q_idx % (lio->linfo.num_txpciq));
2983 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
2988 stats = &oct->instr_queue[iq_no]->stats;
2990 /* Check for all conditions in which the current packet cannot be
2993 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
2994 (!lio->linfo.link.s.link_up) ||
2996 netif_info(lio, tx_err, lio->netdev,
2997 "Transmit failed link_status : %d\n",
2998 lio->linfo.link.s.link_up);
2999 goto lio_xmit_failed;
3002 /* Use space in skb->cb to store info used to unmap and
3005 finfo = (struct octnet_buf_free_info *)skb->cb;
3010 /* Prepare the attributes for the data to be passed to OSI. */
3011 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3013 ndata.buf = (void *)finfo;
3017 if (netif_is_multiqueue(netdev)) {
3018 if (octnet_iq_is_full(oct, ndata.q_no)) {
3019 /* defer sending if queue is full */
3020 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3022 stats->tx_iq_busy++;
3023 return NETDEV_TX_BUSY;
3026 if (octnet_iq_is_full(oct, lio->txq)) {
3027 /* defer sending if queue is full */
3028 stats->tx_iq_busy++;
3029 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3031 return NETDEV_TX_BUSY;
3034 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
3035 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
3038 ndata.datasize = skb->len;
3041 cmdsetup.s.iq_no = iq_no;
3043 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3044 if (skb->encapsulation) {
3045 cmdsetup.s.tnl_csum = 1;
3048 cmdsetup.s.transport_csum = 1;
3051 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3052 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3053 cmdsetup.s.timestamp = 1;
3056 if (skb_shinfo(skb)->nr_frags == 0) {
3057 cmdsetup.s.u.datasize = skb->len;
3058 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
3060 /* Offload checksum calculation for TCP/UDP packets */
3061 dptr = dma_map_single(&oct->pci_dev->dev,
3065 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
3066 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3068 return NETDEV_TX_BUSY;
3071 if (OCTEON_CN23XX_PF(oct))
3072 ndata.cmd.cmd3.dptr = dptr;
3074 ndata.cmd.cmd2.dptr = dptr;
3076 ndata.reqtype = REQTYPE_NORESP_NET;
3080 struct skb_frag_struct *frag;
3081 struct octnic_gather *g;
3083 spin_lock(&lio->glist_lock[q_idx]);
3084 g = (struct octnic_gather *)
3085 list_delete_head(&lio->glist[q_idx]);
3086 spin_unlock(&lio->glist_lock[q_idx]);
3089 netif_info(lio, tx_err, lio->netdev,
3090 "Transmit scatter gather: glist null!\n");
3091 goto lio_xmit_failed;
3094 cmdsetup.s.gather = 1;
3095 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
3096 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
3098 memset(g->sg, 0, g->sg_size);
3100 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3102 (skb->len - skb->data_len),
3104 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3105 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3107 return NETDEV_TX_BUSY;
3109 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3111 frags = skb_shinfo(skb)->nr_frags;
3114 frag = &skb_shinfo(skb)->frags[i - 1];
3116 g->sg[(i >> 2)].ptr[(i & 3)] =
3117 dma_map_page(&oct->pci_dev->dev,
3123 if (dma_mapping_error(&oct->pci_dev->dev,
3124 g->sg[i >> 2].ptr[i & 3])) {
3125 dma_unmap_single(&oct->pci_dev->dev,
3127 skb->len - skb->data_len,
3129 for (j = 1; j < i; j++) {
3130 frag = &skb_shinfo(skb)->frags[j - 1];
3131 dma_unmap_page(&oct->pci_dev->dev,
3132 g->sg[j >> 2].ptr[j & 3],
3136 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3138 return NETDEV_TX_BUSY;
3141 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3145 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
3146 g->sg_size, DMA_TO_DEVICE);
3147 dptr = g->sg_dma_ptr;
3149 if (OCTEON_CN23XX_PF(oct))
3150 ndata.cmd.cmd3.dptr = dptr;
3152 ndata.cmd.cmd2.dptr = dptr;
3156 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3159 if (OCTEON_CN23XX_PF(oct)) {
3160 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3161 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3163 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3164 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3167 if (skb_shinfo(skb)->gso_size) {
3168 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3169 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
3173 /* HW insert VLAN tag */
3174 if (skb_vlan_tag_present(skb)) {
3175 irh->priority = skb_vlan_tag_get(skb) >> 13;
3176 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3179 if (unlikely(cmdsetup.s.timestamp))
3180 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
3182 status = octnet_send_nic_data_pkt(oct, &ndata);
3183 if (status == IQ_SEND_FAILED)
3184 goto lio_xmit_failed;
3186 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3188 if (status == IQ_SEND_STOP)
3189 stop_q(lio->netdev, q_idx);
3191 netif_trans_update(netdev);
3193 if (skb_shinfo(skb)->gso_size)
3194 stats->tx_done += skb_shinfo(skb)->gso_segs;
3197 stats->tx_tot_bytes += skb->len;
3199 return NETDEV_TX_OK;
3202 stats->tx_dropped++;
3203 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3204 iq_no, stats->tx_dropped);
3206 dma_unmap_single(&oct->pci_dev->dev, dptr,
3207 ndata.datasize, DMA_TO_DEVICE);
3208 tx_buffer_free(skb);
3209 return NETDEV_TX_OK;
3212 /** \brief Network device Tx timeout
3213 * @param netdev pointer to network device
3215 static void liquidio_tx_timeout(struct net_device *netdev)
3219 lio = GET_LIO(netdev);
3221 netif_info(lio, tx_err, lio->netdev,
3222 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3223 netdev->stats.tx_dropped);
3224 netif_trans_update(netdev);
3228 static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3229 __be16 proto __attribute__((unused)),
3232 struct lio *lio = GET_LIO(netdev);
3233 struct octeon_device *oct = lio->oct_dev;
3234 struct octnic_ctrl_pkt nctrl;
3237 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3240 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3241 nctrl.ncmd.s.param1 = vid;
3242 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3243 nctrl.wait_time = 100;
3244 nctrl.netpndev = (u64)netdev;
3245 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3247 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3249 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3256 static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3257 __be16 proto __attribute__((unused)),
3260 struct lio *lio = GET_LIO(netdev);
3261 struct octeon_device *oct = lio->oct_dev;
3262 struct octnic_ctrl_pkt nctrl;
3265 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3268 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3269 nctrl.ncmd.s.param1 = vid;
3270 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3271 nctrl.wait_time = 100;
3272 nctrl.netpndev = (u64)netdev;
3273 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3275 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3277 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3283 /** Sending command to enable/disable RX checksum offload
3284 * @param netdev pointer to network device
3285 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3286 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3287 * OCTNET_CMD_RXCSUM_DISABLE
3288 * @returns SUCCESS or FAILURE
3290 static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3293 struct lio *lio = GET_LIO(netdev);
3294 struct octeon_device *oct = lio->oct_dev;
3295 struct octnic_ctrl_pkt nctrl;
3299 nctrl.ncmd.s.cmd = command;
3300 nctrl.ncmd.s.param1 = rx_cmd;
3301 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3302 nctrl.wait_time = 100;
3303 nctrl.netpndev = (u64)netdev;
3304 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3306 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3308 dev_err(&oct->pci_dev->dev,
3309 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3315 /** Sending command to add/delete VxLAN UDP port to firmware
3316 * @param netdev pointer to network device
3317 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3318 * @param vxlan_port VxLAN port to be added or deleted
3319 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3320 * OCTNET_CMD_VXLAN_PORT_DEL
3321 * @returns SUCCESS or FAILURE
3323 static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3324 u16 vxlan_port, u8 vxlan_cmd_bit)
3326 struct lio *lio = GET_LIO(netdev);
3327 struct octeon_device *oct = lio->oct_dev;
3328 struct octnic_ctrl_pkt nctrl;
3332 nctrl.ncmd.s.cmd = command;
3333 nctrl.ncmd.s.more = vxlan_cmd_bit;
3334 nctrl.ncmd.s.param1 = vxlan_port;
3335 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3336 nctrl.wait_time = 100;
3337 nctrl.netpndev = (u64)netdev;
3338 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3340 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3342 dev_err(&oct->pci_dev->dev,
3343 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3349 /** \brief Net device fix features
3350 * @param netdev pointer to network device
3351 * @param request features requested
3352 * @returns updated features list
3354 static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3355 netdev_features_t request)
3357 struct lio *lio = netdev_priv(netdev);
3359 if ((request & NETIF_F_RXCSUM) &&
3360 !(lio->dev_capability & NETIF_F_RXCSUM))
3361 request &= ~NETIF_F_RXCSUM;
3363 if ((request & NETIF_F_HW_CSUM) &&
3364 !(lio->dev_capability & NETIF_F_HW_CSUM))
3365 request &= ~NETIF_F_HW_CSUM;
3367 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3368 request &= ~NETIF_F_TSO;
3370 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3371 request &= ~NETIF_F_TSO6;
3373 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3374 request &= ~NETIF_F_LRO;
3376 /*Disable LRO if RXCSUM is off */
3377 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3378 (lio->dev_capability & NETIF_F_LRO))
3379 request &= ~NETIF_F_LRO;
3384 /** \brief Net device set features
3385 * @param netdev pointer to network device
3386 * @param features features to enable/disable
3388 static int liquidio_set_features(struct net_device *netdev,
3389 netdev_features_t features)
3391 struct lio *lio = netdev_priv(netdev);
3393 if (!((netdev->features ^ features) & NETIF_F_LRO))
3396 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
3397 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3398 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
3399 else if (!(features & NETIF_F_LRO) &&
3400 (lio->dev_capability & NETIF_F_LRO))
3401 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3402 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
3404 /* Sending command to firmware to enable/disable RX checksum
3405 * offload settings using ethtool
3407 if (!(netdev->features & NETIF_F_RXCSUM) &&
3408 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3409 (features & NETIF_F_RXCSUM))
3410 liquidio_set_rxcsum_command(netdev,
3411 OCTNET_CMD_TNL_RX_CSUM_CTL,
3412 OCTNET_CMD_RXCSUM_ENABLE);
3413 else if ((netdev->features & NETIF_F_RXCSUM) &&
3414 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3415 !(features & NETIF_F_RXCSUM))
3416 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3417 OCTNET_CMD_RXCSUM_DISABLE);
3422 static void liquidio_add_vxlan_port(struct net_device *netdev,
3423 struct udp_tunnel_info *ti)
3425 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3428 liquidio_vxlan_port_command(netdev,
3429 OCTNET_CMD_VXLAN_PORT_CONFIG,
3431 OCTNET_CMD_VXLAN_PORT_ADD);
3434 static void liquidio_del_vxlan_port(struct net_device *netdev,
3435 struct udp_tunnel_info *ti)
3437 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3440 liquidio_vxlan_port_command(netdev,
3441 OCTNET_CMD_VXLAN_PORT_CONFIG,
3443 OCTNET_CMD_VXLAN_PORT_DEL);
3446 static struct net_device_ops lionetdevops = {
3447 .ndo_open = liquidio_open,
3448 .ndo_stop = liquidio_stop,
3449 .ndo_start_xmit = liquidio_xmit,
3450 .ndo_get_stats = liquidio_get_stats,
3451 .ndo_set_mac_address = liquidio_set_mac,
3452 .ndo_set_rx_mode = liquidio_set_mcast_list,
3453 .ndo_tx_timeout = liquidio_tx_timeout,
3455 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3456 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
3457 .ndo_change_mtu = liquidio_change_mtu,
3458 .ndo_do_ioctl = liquidio_ioctl,
3459 .ndo_fix_features = liquidio_fix_features,
3460 .ndo_set_features = liquidio_set_features,
3461 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3462 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
3465 /** \brief Entry point for the liquidio module
3467 static int __init liquidio_init(void)
3470 struct handshake *hs;
3472 init_completion(&first_stage);
3474 octeon_init_device_list(conf_type);
3476 if (liquidio_init_pci())
3479 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3481 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3484 wait_for_completion(&hs->init);
3486 /* init handshake failed */
3487 dev_err(&hs->pci_dev->dev,
3488 "Failed to init device\n");
3489 liquidio_deinit_pci();
3495 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3498 wait_for_completion_timeout(&hs->started,
3499 msecs_to_jiffies(30000));
3500 if (!hs->started_ok) {
3501 /* starter handshake failed */
3502 dev_err(&hs->pci_dev->dev,
3503 "Firmware failed to start\n");
3504 liquidio_deinit_pci();
3513 static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
3515 struct octeon_device *oct = (struct octeon_device *)buf;
3516 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
3518 union oct_link_status *ls;
3521 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
3522 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3523 recv_pkt->buffer_size[0],
3524 recv_pkt->rh.r_nic_info.gmxport);
3528 gmxport = recv_pkt->rh.r_nic_info.gmxport;
3529 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3531 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
3532 for (i = 0; i < oct->ifcount; i++) {
3533 if (oct->props[i].gmxport == gmxport) {
3534 update_link_status(oct->props[i].netdev, ls);
3540 for (i = 0; i < recv_pkt->buffer_count; i++)
3541 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3542 octeon_free_recv_info(recv_info);
3547 * \brief Setup network interfaces
3548 * @param octeon_dev octeon device
3550 * Called during init time for each device. It assumes the NIC
3551 * is already up and running. The link information for each
3552 * interface is passed in link_info.
3554 static int setup_nic_devices(struct octeon_device *octeon_dev)
3556 struct lio *lio = NULL;
3557 struct net_device *netdev;
3559 struct octeon_soft_command *sc;
3560 struct liquidio_if_cfg_context *ctx;
3561 struct liquidio_if_cfg_resp *resp;
3562 struct octdev_props *props;
3563 int retval, num_iqueues, num_oqueues;
3564 union oct_nic_if_cfg if_cfg;
3565 unsigned int base_queue;
3566 unsigned int gmx_port_id;
3567 u32 resp_size, ctx_size, data_size;
3569 struct lio_version *vdata;
3571 /* This is to handle link status changes */
3572 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3574 lio_nic_info, octeon_dev);
3576 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3577 * They are handled directly.
3579 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3582 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3585 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3586 free_netsgbuf_with_resp);
3588 for (i = 0; i < octeon_dev->ifcount; i++) {
3589 resp_size = sizeof(struct liquidio_if_cfg_resp);
3590 ctx_size = sizeof(struct liquidio_if_cfg_context);
3591 data_size = sizeof(struct lio_version);
3592 sc = (struct octeon_soft_command *)
3593 octeon_alloc_soft_command(octeon_dev, data_size,
3594 resp_size, ctx_size);
3595 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3596 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
3597 vdata = (struct lio_version *)sc->virtdptr;
3599 *((u64 *)vdata) = 0;
3600 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3601 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3602 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
3604 if (OCTEON_CN23XX_PF(octeon_dev)) {
3605 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3606 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3607 base_queue = octeon_dev->sriov_info.pf_srn;
3609 gmx_port_id = octeon_dev->pf_num;
3610 ifidx_or_pfnum = octeon_dev->pf_num;
3612 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3613 octeon_get_conf(octeon_dev), i);
3614 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3615 octeon_get_conf(octeon_dev), i);
3616 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3617 octeon_get_conf(octeon_dev), i);
3618 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3619 octeon_get_conf(octeon_dev), i);
3623 dev_dbg(&octeon_dev->pci_dev->dev,
3624 "requesting config for interface %d, iqs %d, oqs %d\n",
3625 ifidx_or_pfnum, num_iqueues, num_oqueues);
3626 WRITE_ONCE(ctx->cond, 0);
3627 ctx->octeon_id = lio_get_device_id(octeon_dev);
3628 init_waitqueue_head(&ctx->wc);
3631 if_cfg.s.num_iqueues = num_iqueues;
3632 if_cfg.s.num_oqueues = num_oqueues;
3633 if_cfg.s.base_queue = base_queue;
3634 if_cfg.s.gmx_port_id = gmx_port_id;
3638 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
3639 OPCODE_NIC_IF_CFG, 0,
3642 sc->callback = if_cfg_callback;
3643 sc->callback_arg = sc;
3644 sc->wait_time = 3000;
3646 retval = octeon_send_soft_command(octeon_dev, sc);
3647 if (retval == IQ_SEND_FAILED) {
3648 dev_err(&octeon_dev->pci_dev->dev,
3649 "iq/oq config failed status: %x\n",
3651 /* Soft instr is freed by driver in case of failure. */
3652 goto setup_nic_dev_fail;
3655 /* Sleep on a wait queue till the cond flag indicates that the
3656 * response arrived or timed-out.
3658 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3659 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3660 goto setup_nic_wait_intr;
3663 retval = resp->status;
3665 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3666 goto setup_nic_dev_fail;
3669 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3670 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3672 num_iqueues = hweight64(resp->cfg_info.iqmask);
3673 num_oqueues = hweight64(resp->cfg_info.oqmask);
3675 if (!(num_iqueues) || !(num_oqueues)) {
3676 dev_err(&octeon_dev->pci_dev->dev,
3677 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3678 resp->cfg_info.iqmask,
3679 resp->cfg_info.oqmask);
3680 goto setup_nic_dev_fail;
3682 dev_dbg(&octeon_dev->pci_dev->dev,
3683 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3684 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3685 num_iqueues, num_oqueues);
3686 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3689 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3690 goto setup_nic_dev_fail;
3693 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
3695 if (num_iqueues > 1)
3696 lionetdevops.ndo_select_queue = select_q;
3698 /* Associate the routines that will handle different
3701 netdev->netdev_ops = &lionetdevops;
3703 lio = GET_LIO(netdev);
3705 memset(lio, 0, sizeof(struct lio));
3707 lio->ifidx = ifidx_or_pfnum;
3709 props = &octeon_dev->props[i];
3710 props->gmxport = resp->cfg_info.linfo.gmxport;
3711 props->netdev = netdev;
3713 lio->linfo.num_rxpciq = num_oqueues;
3714 lio->linfo.num_txpciq = num_iqueues;
3715 for (j = 0; j < num_oqueues; j++) {
3716 lio->linfo.rxpciq[j].u64 =
3717 resp->cfg_info.linfo.rxpciq[j].u64;
3719 for (j = 0; j < num_iqueues; j++) {
3720 lio->linfo.txpciq[j].u64 =
3721 resp->cfg_info.linfo.txpciq[j].u64;
3723 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3724 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3725 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3727 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3729 if (OCTEON_CN23XX_PF(octeon_dev) ||
3730 OCTEON_CN6XXX(octeon_dev)) {
3731 lio->dev_capability = NETIF_F_HIGHDMA
3734 | NETIF_F_SG | NETIF_F_RXCSUM
3736 | NETIF_F_TSO | NETIF_F_TSO6
3739 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3741 /* Copy of transmit encapsulation capabilities:
3742 * TSO, TSO6, Checksums for this device
3744 lio->enc_dev_capability = NETIF_F_IP_CSUM
3746 | NETIF_F_GSO_UDP_TUNNEL
3747 | NETIF_F_HW_CSUM | NETIF_F_SG
3749 | NETIF_F_TSO | NETIF_F_TSO6
3752 netdev->hw_enc_features = (lio->enc_dev_capability &
3755 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3757 netdev->vlan_features = lio->dev_capability;
3758 /* Add any unchangeable hw features */
3759 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3760 NETIF_F_HW_VLAN_CTAG_RX |
3761 NETIF_F_HW_VLAN_CTAG_TX;
3763 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3765 netdev->hw_features = lio->dev_capability;
3766 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3767 netdev->hw_features = netdev->hw_features &
3768 ~NETIF_F_HW_VLAN_CTAG_RX;
3770 /* Point to the properties for octeon device to which this
3771 * interface belongs.
3773 lio->oct_dev = octeon_dev;
3774 lio->octprops = props;
3775 lio->netdev = netdev;
3777 dev_dbg(&octeon_dev->pci_dev->dev,
3778 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3779 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3781 /* 64-bit swap required on LE machines */
3782 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3783 for (j = 0; j < 6; j++)
3784 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3786 /* Copy MAC Address to OS network device structure */
3788 ether_addr_copy(netdev->dev_addr, mac);
3790 /* By default all interfaces on a single Octeon uses the same
3793 lio->txq = lio->linfo.txpciq[0].s.q_no;
3794 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
3795 if (setup_io_queues(octeon_dev, i)) {
3796 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3797 goto setup_nic_dev_fail;
3800 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3802 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3803 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3805 if (setup_glists(octeon_dev, lio, num_iqueues)) {
3806 dev_err(&octeon_dev->pci_dev->dev,
3807 "Gather list allocation failed\n");
3808 goto setup_nic_dev_fail;
3811 /* Register ethtool support */
3812 liquidio_set_ethtool_ops(netdev);
3813 octeon_dev->priv_flags = 0x0;
3815 if (netdev->features & NETIF_F_LRO)
3816 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3817 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
3819 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
3821 if ((debug != -1) && (debug & NETIF_MSG_HW))
3822 liquidio_set_feature(netdev,
3823 OCTNET_CMD_VERBOSE_ENABLE, 0);
3825 if (setup_link_status_change_wq(netdev))
3826 goto setup_nic_dev_fail;
3828 /* Register the network device with the OS */
3829 if (register_netdev(netdev)) {
3830 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3831 goto setup_nic_dev_fail;
3834 dev_dbg(&octeon_dev->pci_dev->dev,
3835 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3836 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3837 netif_carrier_off(netdev);
3838 lio->link_changes++;
3840 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3842 /* Sending command to firmware to enable Rx checksum offload
3843 * by default at the time of setup of Liquidio driver for
3846 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3847 OCTNET_CMD_RXCSUM_ENABLE);
3848 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3849 OCTNET_CMD_TXCSUM_ENABLE);
3851 dev_dbg(&octeon_dev->pci_dev->dev,
3852 "NIC ifidx:%d Setup successful\n", i);
3854 octeon_free_soft_command(octeon_dev, sc);
3861 octeon_free_soft_command(octeon_dev, sc);
3863 setup_nic_wait_intr:
3866 dev_err(&octeon_dev->pci_dev->dev,
3867 "NIC ifidx:%d Setup failed\n", i);
3868 liquidio_destroy_nic_device(octeon_dev, i);
3874 * \brief initialize the NIC
3875 * @param oct octeon device
3877 * This initialization routine is called once the Octeon device application is
3880 static int liquidio_init_nic_module(struct octeon_device *oct)
3882 struct oct_intrmod_cfg *intrmod_cfg;
3884 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3886 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3888 /* only default iq and oq were initialized
3889 * initialize the rest as well
3891 /* run port_config command for each port */
3892 oct->ifcount = num_nic_ports;
3894 memset(oct->props, 0,
3895 sizeof(struct octdev_props) * num_nic_ports);
3897 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3898 oct->props[i].gmxport = -1;
3900 retval = setup_nic_devices(oct);
3902 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3903 goto octnet_init_failure;
3906 liquidio_ptp_init(oct);
3908 /* Initialize interrupt moderation params */
3909 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
3910 intrmod_cfg->rx_enable = 1;
3911 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3912 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3913 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3914 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
3915 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
3916 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
3917 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
3918 intrmod_cfg->tx_enable = 1;
3919 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
3920 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
3921 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
3922 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
3923 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
3924 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3928 octnet_init_failure:
3936 * \brief starter callback that invokes the remaining initialization work after
3937 * the NIC is up and running.
3938 * @param octptr work struct work_struct
3940 static void nic_starter(struct work_struct *work)
3942 struct octeon_device *oct;
3943 struct cavium_wk *wk = (struct cavium_wk *)work;
3945 oct = (struct octeon_device *)wk->ctxptr;
3947 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3950 /* If the status of the device is CORE_OK, the core
3951 * application has reported its application type. Call
3952 * any registered handlers now and move to the RUNNING
3955 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3956 schedule_delayed_work(&oct->nic_poll_work.work,
3957 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3961 atomic_set(&oct->status, OCT_DEV_RUNNING);
3963 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3964 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3966 if (liquidio_init_nic_module(oct))
3967 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3969 handshake[oct->octeon_id].started_ok = 1;
3971 dev_err(&oct->pci_dev->dev,
3972 "Unexpected application running on NIC (%d). Check firmware.\n",
3976 complete(&handshake[oct->octeon_id].started);
3980 * \brief Device initialization for each Octeon device that is probed
3981 * @param octeon_dev octeon device
3983 static int octeon_device_init(struct octeon_device *octeon_dev)
3987 char bootcmd[] = "\n";
3988 struct octeon_device_priv *oct_priv =
3989 (struct octeon_device_priv *)octeon_dev->priv;
3990 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3992 /* Enable access to the octeon device and make its DMA capability
3995 if (octeon_pci_os_setup(octeon_dev))
3998 /* Identify the Octeon type and map the BAR address space. */
3999 if (octeon_chip_specific_setup(octeon_dev)) {
4000 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4004 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4006 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4008 if (OCTEON_CN23XX_PF(octeon_dev)) {
4009 if (!cn23xx_fw_loaded(octeon_dev)) {
4011 /* Do a soft reset of the Octeon device. */
4012 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4014 /* things might have changed */
4015 if (!cn23xx_fw_loaded(octeon_dev))
4022 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
4026 /* Initialize the dispatch mechanism used to push packets arriving on
4027 * Octeon Output queues.
4029 if (octeon_init_dispatch_list(octeon_dev))
4032 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4033 OPCODE_NIC_CORE_DRV_ACTIVE,
4034 octeon_core_drv_init,
4037 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4038 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4039 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4040 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4042 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4044 octeon_set_io_queues_off(octeon_dev);
4046 if (OCTEON_CN23XX_PF(octeon_dev)) {
4047 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4049 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4054 /* Initialize soft command buffer pool
4056 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4057 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4060 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4062 /* Setup the data structures that manage this Octeon's Input queues. */
4063 if (octeon_setup_instr_queues(octeon_dev)) {
4064 dev_err(&octeon_dev->pci_dev->dev,
4065 "instruction queue initialization failed\n");
4066 /* On error, release any previously allocated queues */
4067 for (j = 0; j < octeon_dev->num_iqs; j++)
4068 octeon_delete_instr_queue(octeon_dev, j);
4071 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4073 /* Initialize lists to manage the requests of different types that
4074 * arrive from user & kernel applications for this octeon device.
4076 if (octeon_setup_response_list(octeon_dev)) {
4077 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4080 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4082 if (octeon_setup_output_queues(octeon_dev)) {
4083 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
4084 /* Release any previously allocated queues */
4085 for (j = 0; j < octeon_dev->num_oqs; j++)
4086 octeon_delete_droq(octeon_dev, j);
4090 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4092 if (OCTEON_CN23XX_PF(octeon_dev)) {
4093 if (octeon_allocate_ioq_vector(octeon_dev)) {
4094 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4099 /* The input and output queue registers were setup earlier (the
4100 * queues were not enabled). Any additional registers
4101 * that need to be programmed should be done now.
4103 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4105 dev_err(&octeon_dev->pci_dev->dev,
4106 "Failed to configure device registers\n");
4111 /* Initialize the tasklet that handles output queue packet processing.*/
4112 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4113 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4114 (unsigned long)octeon_dev);
4116 /* Setup the interrupt handler and record the INT SUM register address
4118 if (octeon_setup_interrupt(octeon_dev))
4121 /* Enable Octeon device interrupts */
4122 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
4124 /* Enable the input and output queues for this Octeon device */
4125 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4127 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4131 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4133 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4134 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4136 dev_info(&octeon_dev->pci_dev->dev,
4137 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4140 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
4142 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4143 while (!ddr_timeout) {
4144 set_current_state(TASK_INTERRUPTIBLE);
4145 if (schedule_timeout(HZ / 10)) {
4146 /* user probably pressed Control-C */
4150 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4152 dev_err(&octeon_dev->pci_dev->dev,
4153 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4158 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4159 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4163 /* Divert uboot to take commands from host instead. */
4164 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
4166 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4167 ret = octeon_init_consoles(octeon_dev);
4169 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4172 ret = octeon_add_console(octeon_dev, 0);
4174 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4178 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
4180 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4181 ret = load_firmware(octeon_dev);
4183 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4186 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4189 if (OCTEON_CN23XX_PF(octeon_dev))
4190 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4194 handshake[octeon_dev->octeon_id].init_ok = 1;
4195 complete(&handshake[octeon_dev->octeon_id].init);
4197 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4199 /* Send Credit for Octeon Output queues. Credits are always sent after
4200 * the output queue is enabled.
4202 for (j = 0; j < octeon_dev->num_oqs; j++)
4203 writel(octeon_dev->droq[j]->max_count,
4204 octeon_dev->droq[j]->pkts_credit_reg);
4206 /* Packets can start arriving on the output queues from this point. */
4211 * \brief Exits the module
4213 static void __exit liquidio_exit(void)
4215 liquidio_deinit_pci();
4217 pr_info("LiquidIO network module is now unloaded\n");
4220 module_init(liquidio_init);
4221 module_exit(liquidio_exit);