Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  lio_main.c   Sprache: C

 
/**********************************************************************
 * Author: Cavium, Inc.
 *
 * Contact: support@cavium.com
 *          Please include "LiquidIO" in the subject.
 *
 * Copyright (c) 2003-2016 Cavium, Inc.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, Version 2, as
 * published by the Free Software Foundation.
 *
 * This file is distributed in the hope that it will be useful, but
 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
 * NONINFRINGEMENT.  See the GNU General Public License for more details.
 ***********************************************************************/

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/firmware.h>
#include <net/vxlan.h>
#include <linux/kthread.h>
#include "liquidio_common.h"
#include "octeon_droq.h"
#include "octeon_iq.h"
#include "response_manager.h"
#include "octeon_device.h"
#include "octeon_nic.h"
#include "octeon_main.h"
#include "octeon_network.h"
#include "cn66xx_regs.h"
#include "cn66xx_device.h"
#include "cn68xx_device.h"
#include "cn23xx_pf_device.h"
#include "liquidio_image.h"
#include "lio_vf_rep.h"

MODULE_AUTHOR("Cavium Networks, ");
MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
MODULE_LICENSE("GPL");
MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
  "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
  "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
  "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
  "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);

static int ddr_timeout = 10000;
module_param(ddr_timeout, int, 0644);
MODULE_PARM_DESC(ddr_timeout,
   "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");

#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)

static int debug = -1;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");

static char fw_type[LIO_MAX_FW_TYPE_LEN] = LIO_FW_NAME_TYPE_AUTO;
module_param_string(fw_type, fw_type, sizeof(fw_type), 0444);
MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded (default is \"auto\"), which uses firmware in flash, if present, else loads \"nic\".");

static u32 console_bitmask;
module_param(console_bitmask, int, 0644);
MODULE_PARM_DESC(console_bitmask,
   "Bitmask indicating which consoles have debug output redirected to syslog.");

/**
 * octeon_console_debug_enabled - determines if a given console has debug enabled.
 * @console: console to check
 * Return:  1 = enabled. 0 otherwise
 */

static int octeon_console_debug_enabled(u32 console)
{
 return (console_bitmask >> (console)) & 0x1;
}

/* Polling interval for determining when NIC application is alive */
#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100

/* runtime link query interval */
#define LIQUIDIO_LINK_QUERY_INTERVAL_MS         1000
/* update localtime to octeon firmware every 60 seconds.
 * make firmware to use same time reference, so that it will be easy to
 * correlate firmware logged events/errors with host events, for debugging.
 */

#define LIO_SYNC_OCTEON_TIME_INTERVAL_MS 60000

/* time to wait for possible in-flight requests in milliseconds */
#define WAIT_INFLIGHT_REQUEST msecs_to_jiffies(1000)

struct oct_timestamp_resp {
 u64 rh;
 u64 timestamp;
 u64 status;
};

#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))

union tx_info {
 u64 u64;
 struct {
#ifdef __BIG_ENDIAN_BITFIELD
  u16 gso_size;
  u16 gso_segs;
  u32 reserved;
#else
  u32 reserved;
  u16 gso_segs;
  u16 gso_size;
#endif
 } s;
};

/* Octeon device properties to be used by the NIC module.
 * Each octeon device in the system will be represented
 * by this structure in the NIC module.
 */


#define OCTNIC_GSO_MAX_HEADER_SIZE 128
#define OCTNIC_GSO_MAX_SIZE                                                    \
 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)

struct handshake {
 struct completion init;
 struct completion started;
 struct pci_dev *pci_dev;
 int init_ok;
 int started_ok;
};

#ifdef CONFIG_PCI_IOV
static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
#endif

static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
        char *prefix, char *suffix);

static int octeon_device_init(struct octeon_device *);
static int liquidio_stop(struct net_device *netdev);
static void liquidio_remove(struct pci_dev *pdev);
static int liquidio_probe(struct pci_dev *pdev,
     const struct pci_device_id *ent);
static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
          int linkstate);

static struct handshake handshake[MAX_OCTEON_DEVICES];
static struct completion first_stage;

static void octeon_droq_bh(struct tasklet_struct *t)
{
 int q_no;
 int reschedule = 0;
 struct octeon_device_priv *oct_priv = from_tasklet(oct_priv, t,
         droq_tasklet);
 struct octeon_device *oct = oct_priv->dev;

 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
  if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
   continue;
  reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
         MAX_PACKET_BUDGET);
  lio_enable_irq(oct->droq[q_no], NULL);

  if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
   /* set time and cnt interrupt thresholds for this DROQ
 * for NAPI
 */

   int adjusted_q_no = q_no + oct->sriov_info.pf_srn;

   octeon_write_csr64(
       oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
       0x5700000040ULL);
   octeon_write_csr64(
       oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
  }
 }

 if (reschedule)
  tasklet_schedule(&oct_priv->droq_tasklet);
}

static int lio_wait_for_oq_pkts(struct octeon_device *oct)
{
 struct octeon_device_priv *oct_priv = oct->priv;
 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
 int i;

 do {
  pending_pkts = 0;

  for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
   if (!(oct->io_qmask.oq & BIT_ULL(i)))
    continue;
   pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
  }
  if (pkt_cnt > 0) {
   pending_pkts += pkt_cnt;
   tasklet_schedule(&oct_priv->droq_tasklet);
  }
  pkt_cnt = 0;
  schedule_timeout_uninterruptible(1);

 } while (retry-- && pending_pkts);

 return pkt_cnt;
}

/**
 * force_io_queues_off - Forces all IO queues off on a given device
 * @oct: Pointer to Octeon device
 */

static void force_io_queues_off(struct octeon_device *oct)
{
 if ((oct->chip_id == OCTEON_CN66XX) ||
     (oct->chip_id == OCTEON_CN68XX)) {
  /* Reset the Enable bits for Input Queues. */
  octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);

  /* Reset the Enable bits for Output Queues. */
  octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
 }
}

/**
 * pcierror_quiesce_device - Cause device to go quiet so it can be safely removed/reset/etc
 * @oct: Pointer to Octeon device
 */

static inline void pcierror_quiesce_device(struct octeon_device *oct)
{
 int i;

 /* Disable the input and output queues now. No more packets will
 * arrive from Octeon, but we should wait for all packet processing
 * to finish.
 */

 force_io_queues_off(oct);

 /* To allow for in-flight requests */
 schedule_timeout_uninterruptible(WAIT_INFLIGHT_REQUEST);

 if (wait_for_pending_requests(oct))
  dev_err(&oct->pci_dev->dev, "There were pending requests\n");

 /* Force all requests waiting to be fetched by OCTEON to complete. */
 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  struct octeon_instr_queue *iq;

  if (!(oct->io_qmask.iq & BIT_ULL(i)))
   continue;
  iq = oct->instr_queue[i];

  if (atomic_read(&iq->instr_pending)) {
   spin_lock_bh(&iq->lock);
   iq->fill_cnt = 0;
   iq->octeon_read_index = iq->host_write_index;
   iq->stats.instr_processed +=
    atomic_read(&iq->instr_pending);
   lio_process_iq_request_list(oct, iq, 0);
   spin_unlock_bh(&iq->lock);
  }
 }

 /* Force all pending ordered list requests to time out. */
 lio_process_ordered_list(oct, 1);

 /* We do not need to wait for output queue packets to be processed. */
}

/**
 * cleanup_aer_uncorrect_error_status - Cleanup PCI AER uncorrectable error status
 * @dev: Pointer to PCI device
 */

static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
{
 int pos = 0x100;
 u32 status, mask;

 pr_info("%s :\n", __func__);

 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
 if (dev->error_state == pci_channel_io_normal)
  status &= ~mask;        /* Clear corresponding nonfatal bits */
 else
  status &= mask;         /* Clear corresponding fatal bits */
 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
}

/**
 * stop_pci_io - Stop all PCI IO to a given device
 * @oct: Pointer to Octeon device
 */

static void stop_pci_io(struct octeon_device *oct)
{
 /* No more instructions will be forwarded. */
 atomic_set(&oct->status, OCT_DEV_IN_RESET);

 pci_disable_device(oct->pci_dev);

 /* Disable interrupts  */
 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);

 pcierror_quiesce_device(oct);

 /* Release the interrupt line */
 free_irq(oct->pci_dev->irq, oct);

 if (oct->flags & LIO_FLAG_MSI_ENABLED)
  pci_disable_msi(oct->pci_dev);

 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  lio_get_state_string(&oct->status));

 /* making it a common function for all OCTEON models */
 cleanup_aer_uncorrect_error_status(oct->pci_dev);
}

/**
 * liquidio_pcie_error_detected - called when PCI error is detected
 * @pdev: Pointer to PCI device
 * @state: The current pci connection state
 *
 * This function is called after a PCI bus error affecting
 * this device has been detected.
 */

static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
           pci_channel_state_t state)
{
 struct octeon_device *oct = pci_get_drvdata(pdev);

 /* Non-correctable Non-fatal errors */
 if (state == pci_channel_io_normal) {
  dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
  cleanup_aer_uncorrect_error_status(oct->pci_dev);
  return PCI_ERS_RESULT_CAN_RECOVER;
 }

 /* Non-correctable Fatal errors */
 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
 stop_pci_io(oct);

 /* Always return a DISCONNECT. There is no support for recovery but only
 * for a clean shutdown.
 */

 return PCI_ERS_RESULT_DISCONNECT;
}

/**
 * liquidio_pcie_mmio_enabled - mmio handler
 * @pdev: Pointer to PCI device
 */

static pci_ers_result_t liquidio_pcie_mmio_enabled(struct pci_dev __maybe_unused *pdev)
{
 /* We should never hit this since we never ask for a reset for a Fatal
 * Error. We always return DISCONNECT in io_error above.
 * But play safe and return RECOVERED for now.
 */

 return PCI_ERS_RESULT_RECOVERED;
}

/**
 * liquidio_pcie_slot_reset - called after the pci bus has been reset.
 * @pdev: Pointer to PCI device
 *
 * Restart the card from scratch, as if from a cold-boot. Implementation
 * resembles the first-half of the octeon_resume routine.
 */

static pci_ers_result_t liquidio_pcie_slot_reset(struct pci_dev __maybe_unused *pdev)
{
 /* We should never hit this since we never ask for a reset for a Fatal
 * Error. We always return DISCONNECT in io_error above.
 * But play safe and return RECOVERED for now.
 */

 return PCI_ERS_RESULT_RECOVERED;
}

/**
 * liquidio_pcie_resume - called when traffic can start flowing again.
 * @pdev: Pointer to PCI device
 *
 * This callback is called when the error recovery driver tells us that
 * its OK to resume normal operation. Implementation resembles the
 * second-half of the octeon_resume routine.
 */

static void liquidio_pcie_resume(struct pci_dev __maybe_unused *pdev)
{
 /* Nothing to be done here. */
}

#define liquidio_suspend NULL
#define liquidio_resume NULL

/* For PCI-E Advanced Error Recovery (AER) Interface */
static const struct pci_error_handlers liquidio_err_handler = {
 .error_detected = liquidio_pcie_error_detected,
 .mmio_enabled = liquidio_pcie_mmio_enabled,
 .slot_reset = liquidio_pcie_slot_reset,
 .resume  = liquidio_pcie_resume,
};

static const struct pci_device_id liquidio_pci_tbl[] = {
 {       /* 68xx */
  PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
 },
 {       /* 66xx */
  PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
 },
 {       /* 23xx pf */
  PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
 },
 {
  0, 0, 0, 0, 0, 0, 0
 }
};
MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);

static SIMPLE_DEV_PM_OPS(liquidio_pm_ops, liquidio_suspend, liquidio_resume);

static struct pci_driver liquidio_pci_driver = {
 .name  = "LiquidIO",
 .id_table = liquidio_pci_tbl,
 .probe  = liquidio_probe,
 .remove  = liquidio_remove,
 .err_handler = &liquidio_err_handler,    /* For AER */
 .driver.pm = &liquidio_pm_ops,
#ifdef CONFIG_PCI_IOV
 .sriov_configure = liquidio_enable_sriov,
#endif
};

/**
 * liquidio_init_pci - register PCI driver
 */

static int liquidio_init_pci(void)
{
 return pci_register_driver(&liquidio_pci_driver);
}

/**
 * liquidio_deinit_pci - unregister PCI driver
 */

static void liquidio_deinit_pci(void)
{
 pci_unregister_driver(&liquidio_pci_driver);
}

/**
 * check_txq_status - Check Tx queue status, and take appropriate action
 * @lio: per-network private data
 * Return: 0 if full, number of queues woken up otherwise
 */

static inline int check_txq_status(struct lio *lio)
{
 int numqs = lio->netdev->real_num_tx_queues;
 int ret_val = 0;
 int q, iq;

 /* check each sub-queue state */
 for (q = 0; q < numqs; q++) {
  iq = lio->linfo.txpciq[q %
   lio->oct_dev->num_iqs].s.q_no;
  if (octnet_iq_is_full(lio->oct_dev, iq))
   continue;
  if (__netif_subqueue_stopped(lio->netdev, q)) {
   netif_wake_subqueue(lio->netdev, q);
   INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
        tx_restart, 1);
   ret_val++;
  }
 }

 return ret_val;
}

/**
 * print_link_info -  Print link information
 * @netdev: network device
 */

static void print_link_info(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);

 if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
     ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
  struct oct_link_info *linfo = &lio->linfo;

  if (linfo->link.s.link_up) {
   netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
       linfo->link.s.speed,
       (linfo->link.s.duplex) ? "Full" : "Half");
  } else {
   netif_info(lio, link, lio->netdev, "Link Down\n");
  }
 }
}

/**
 * octnet_link_status_change - Routine to notify MTU change
 * @work: work_struct data structure
 */

static void octnet_link_status_change(struct work_struct *work)
{
 struct cavium_wk *wk = (struct cavium_wk *)work;
 struct lio *lio = (struct lio *)wk->ctxptr;

 /* lio->linfo.link.s.mtu always contains max MTU of the lio interface.
 * this API is invoked only when new max-MTU of the interface is
 * less than current MTU.
 */

 rtnl_lock();
 dev_set_mtu(lio->netdev, lio->linfo.link.s.mtu);
 rtnl_unlock();
}

/**
 * setup_link_status_change_wq - Sets up the mtu status change work
 * @netdev: network device
 */

static inline int setup_link_status_change_wq(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;

 lio->link_status_wq.wq = alloc_workqueue("link-status",
       WQ_MEM_RECLAIM, 0);
 if (!lio->link_status_wq.wq) {
  dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
  return -1;
 }
 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
     octnet_link_status_change);
 lio->link_status_wq.wk.ctxptr = lio;

 return 0;
}

static inline void cleanup_link_status_change_wq(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);

 if (lio->link_status_wq.wq) {
  cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
  destroy_workqueue(lio->link_status_wq.wq);
 }
}

/**
 * update_link_status - Update link status
 * @netdev: network device
 * @ls: link status structure
 *
 * Called on receipt of a link status response from the core application to
 * update each interface's link status.
 */

static inline void update_link_status(struct net_device *netdev,
          union oct_link_status *ls)
{
 struct lio *lio = GET_LIO(netdev);
 int changed = (lio->linfo.link.u64 != ls->u64);
 int current_max_mtu = lio->linfo.link.s.mtu;
 struct octeon_device *oct = lio->oct_dev;

 dev_dbg(&oct->pci_dev->dev, "%s: lio->linfo.link.u64=%llx, ls->u64=%llx\n",
  __func__, lio->linfo.link.u64, ls->u64);
 lio->linfo.link.u64 = ls->u64;

 if ((lio->intf_open) && (changed)) {
  print_link_info(netdev);
  lio->link_changes++;

  if (lio->linfo.link.s.link_up) {
   dev_dbg(&oct->pci_dev->dev, "%s: link_up", __func__);
   netif_carrier_on(netdev);
   wake_txqs(netdev);
  } else {
   dev_dbg(&oct->pci_dev->dev, "%s: link_off", __func__);
   netif_carrier_off(netdev);
   stop_txqs(netdev);
  }
  if (lio->linfo.link.s.mtu != current_max_mtu) {
   netif_info(lio, probe, lio->netdev, "Max MTU changed from %d to %d\n",
       current_max_mtu, lio->linfo.link.s.mtu);
   netdev->max_mtu = lio->linfo.link.s.mtu;
  }
  if (lio->linfo.link.s.mtu < netdev->mtu) {
   dev_warn(&oct->pci_dev->dev,
     "Current MTU is higher than new max MTU; Reducing the current mtu from %d to %d\n",
         netdev->mtu, lio->linfo.link.s.mtu);
   queue_delayed_work(lio->link_status_wq.wq,
        &lio->link_status_wq.wk.work, 0);
  }
 }
}

/**
 * lio_sync_octeon_time - send latest localtime to octeon firmware so that
 * firmware will correct it's time, in case there is a time skew
 *
 * @work: work scheduled to send time update to octeon firmware
 **/

static void lio_sync_octeon_time(struct work_struct *work)
{
 struct cavium_wk *wk = (struct cavium_wk *)work;
 struct lio *lio = (struct lio *)wk->ctxptr;
 struct octeon_device *oct = lio->oct_dev;
 struct octeon_soft_command *sc;
 struct timespec64 ts;
 struct lio_time *lt;
 int ret;

 sc = octeon_alloc_soft_command(oct, sizeof(struct lio_time), 16, 0);
 if (!sc) {
  dev_err(&oct->pci_dev->dev,
   "Failed to sync time to octeon: soft command allocation failed\n");
  return;
 }

 lt = (struct lio_time *)sc->virtdptr;

 /* Get time of the day */
 ktime_get_real_ts64(&ts);
 lt->sec = ts.tv_sec;
 lt->nsec = ts.tv_nsec;
 octeon_swap_8B_data((u64 *)lt, (sizeof(struct lio_time)) / 8);

 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
        OPCODE_NIC_SYNC_OCTEON_TIME, 0, 0, 0);

 init_completion(&sc->complete);
 sc->sc_status = OCTEON_REQUEST_PENDING;

 ret = octeon_send_soft_command(oct, sc);
 if (ret == IQ_SEND_FAILED) {
  dev_err(&oct->pci_dev->dev,
   "Failed to sync time to octeon: failed to send soft command\n");
  octeon_free_soft_command(oct, sc);
 } else {
  WRITE_ONCE(sc->caller_is_done, true);
 }

 queue_delayed_work(lio->sync_octeon_time_wq.wq,
      &lio->sync_octeon_time_wq.wk.work,
      msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
}

/**
 * setup_sync_octeon_time_wq - prepare work to periodically update local time to octeon firmware
 *
 * @netdev: network device which should send time update to firmware
 **/

static inline int setup_sync_octeon_time_wq(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;

 lio->sync_octeon_time_wq.wq =
  alloc_workqueue("update-octeon-time", WQ_MEM_RECLAIM, 0);
 if (!lio->sync_octeon_time_wq.wq) {
  dev_err(&oct->pci_dev->dev, "Unable to create wq to update octeon time\n");
  return -1;
 }
 INIT_DELAYED_WORK(&lio->sync_octeon_time_wq.wk.work,
     lio_sync_octeon_time);
 lio->sync_octeon_time_wq.wk.ctxptr = lio;
 queue_delayed_work(lio->sync_octeon_time_wq.wq,
      &lio->sync_octeon_time_wq.wk.work,
      msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));

 return 0;
}

/**
 * cleanup_sync_octeon_time_wq - destroy wq
 *
 * @netdev: network device which should send time update to firmware
 *
 * Stop scheduling and destroy the work created to periodically update local
 * time to octeon firmware.
 **/

static inline void cleanup_sync_octeon_time_wq(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct cavium_wq *time_wq = &lio->sync_octeon_time_wq;

 if (time_wq->wq) {
  cancel_delayed_work_sync(&time_wq->wk.work);
  destroy_workqueue(time_wq->wq);
 }
}

static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
{
 struct octeon_device *other_oct;

 other_oct = lio_get_device(oct->octeon_id + 1);

 if (other_oct && other_oct->pci_dev) {
  int oct_busnum, other_oct_busnum;

  oct_busnum = oct->pci_dev->bus->number;
  other_oct_busnum = other_oct->pci_dev->bus->number;

  if (oct_busnum == other_oct_busnum) {
   int oct_slot, other_oct_slot;

   oct_slot = PCI_SLOT(oct->pci_dev->devfn);
   other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);

   if (oct_slot == other_oct_slot)
    return other_oct;
  }
 }

 return NULL;
}

static void disable_all_vf_links(struct octeon_device *oct)
{
 struct net_device *netdev;
 int max_vfs, vf, i;

 if (!oct)
  return;

 max_vfs = oct->sriov_info.max_vfs;

 for (i = 0; i < oct->ifcount; i++) {
  netdev = oct->props[i].netdev;
  if (!netdev)
   continue;

  for (vf = 0; vf < max_vfs; vf++)
   liquidio_set_vf_link_state(netdev, vf,
         IFLA_VF_LINK_STATE_DISABLE);
 }
}

static int liquidio_watchdog(void *param)
{
 bool err_msg_was_printed[LIO_MAX_CORES];
 u16 mask_of_crashed_or_stuck_cores = 0;
 bool all_vf_links_are_disabled = false;
 struct octeon_device *oct = param;
 struct octeon_device *other_oct;
#ifdef CONFIG_MODULE_UNLOAD
 long refcount, vfs_referencing_pf;
 u64 vfs_mask1, vfs_mask2;
#endif
 int core;

 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));

 while (!kthread_should_stop()) {
  /* sleep for a couple of seconds so that we don't hog the CPU */
  set_current_state(TASK_INTERRUPTIBLE);
  schedule_timeout(msecs_to_jiffies(2000));

  mask_of_crashed_or_stuck_cores =
      (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);

  if (!mask_of_crashed_or_stuck_cores)
   continue;

  WRITE_ONCE(oct->cores_crashed, true);
  other_oct = get_other_octeon_device(oct);
  if (other_oct)
   WRITE_ONCE(other_oct->cores_crashed, true);

  for (core = 0; core < LIO_MAX_CORES; core++) {
   bool core_crashed_or_got_stuck;

   core_crashed_or_got_stuck =
      (mask_of_crashed_or_stuck_cores
       >> core) & 1;

   if (core_crashed_or_got_stuck &&
       !err_msg_was_printed[core]) {
    dev_err(&oct->pci_dev->dev,
     "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
     core);
    err_msg_was_printed[core] = true;
   }
  }

  if (all_vf_links_are_disabled)
   continue;

  disable_all_vf_links(oct);
  disable_all_vf_links(other_oct);
  all_vf_links_are_disabled = true;

#ifdef CONFIG_MODULE_UNLOAD
  vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
  vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);

  vfs_referencing_pf  = hweight64(vfs_mask1);
  vfs_referencing_pf += hweight64(vfs_mask2);

  refcount = module_refcount(THIS_MODULE);
  if (refcount >= vfs_referencing_pf) {
   while (vfs_referencing_pf) {
    module_put(THIS_MODULE);
    vfs_referencing_pf--;
   }
  }
#endif
 }

 return 0;
}

/**
 * liquidio_probe - PCI probe handler
 * @pdev: PCI device structure
 * @ent: unused
 */

static int
liquidio_probe(struct pci_dev *pdev, const struct pci_device_id __maybe_unused *ent)
{
 struct octeon_device *oct_dev = NULL;
 struct handshake *hs;

 oct_dev = octeon_allocate_device(pdev->device,
      sizeof(struct octeon_device_priv));
 if (!oct_dev) {
  dev_err(&pdev->dev, "Unable to allocate device\n");
  return -ENOMEM;
 }

 if (pdev->device == OCTEON_CN23XX_PF_VID)
  oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;

 /* Enable PTP for 6XXX Device */
 if (((pdev->device == OCTEON_CN66XX) ||
      (pdev->device == OCTEON_CN68XX)))
  oct_dev->ptp_enable = true;
 else
  oct_dev->ptp_enable = false;

 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
   (u32)pdev->vendor, (u32)pdev->device);

 /* Assign octeon_device for this device to the private data area. */
 pci_set_drvdata(pdev, oct_dev);

 /* set linux specific device pointer */
 oct_dev->pci_dev = (void *)pdev;

 oct_dev->subsystem_id = pdev->subsystem_vendor |
  (pdev->subsystem_device << 16);

 hs = &handshake[oct_dev->octeon_id];
 init_completion(&hs->init);
 init_completion(&hs->started);
 hs->pci_dev = pdev;

 if (oct_dev->octeon_id == 0)
  /* first LiquidIO NIC is detected */
  complete(&first_stage);

 if (octeon_device_init(oct_dev)) {
  complete(&hs->init);
  liquidio_remove(pdev);
  return -ENOMEM;
 }

 if (OCTEON_CN23XX_PF(oct_dev)) {
  u8 bus, device, function;

  if (atomic_read(oct_dev->adapter_refcount) == 1) {
   /* Each NIC gets one watchdog kernel thread.  The first
 * PF (of each NIC) that gets pci_driver->probe()'d
 * creates that thread.
 */

   bus = pdev->bus->number;
   device = PCI_SLOT(pdev->devfn);
   function = PCI_FUNC(pdev->devfn);
   oct_dev->watchdog_task = kthread_run(liquidio_watchdog,
            oct_dev,
            "liowd/%02hhx:%02hhx.%hhx",
            bus, device, function);
   if (IS_ERR(oct_dev->watchdog_task)) {
    oct_dev->watchdog_task = NULL;
    dev_err(&oct_dev->pci_dev->dev,
     "failed to create kernel_thread\n");
    liquidio_remove(pdev);
    return -1;
   }
  }
 }

 oct_dev->rx_pause = 1;
 oct_dev->tx_pause = 1;

 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");

 return 0;
}

static bool fw_type_is_auto(void)
{
 return strncmp(fw_type, LIO_FW_NAME_TYPE_AUTO,
         sizeof(LIO_FW_NAME_TYPE_AUTO)) == 0;
}

/**
 * octeon_pci_flr - PCI FLR for each Octeon device.
 * @oct: octeon device
 */

static void octeon_pci_flr(struct octeon_device *oct)
{
 int rc;

 pci_save_state(oct->pci_dev);

 pci_cfg_access_lock(oct->pci_dev);

 /* Quiesce the device completely */
 pci_write_config_word(oct->pci_dev, PCI_COMMAND,
         PCI_COMMAND_INTX_DISABLE);

 rc = __pci_reset_function_locked(oct->pci_dev);

 if (rc != 0)
  dev_err(&oct->pci_dev->dev, "Error %d resetting PCI function %d\n",
   rc, oct->pf_num);

 pci_cfg_access_unlock(oct->pci_dev);

 pci_restore_state(oct->pci_dev);
}

/**
 * octeon_destroy_resources - Destroy resources associated with octeon device
 * @oct: octeon device
 */

static void octeon_destroy_resources(struct octeon_device *oct)
{
 int i, refcount;
 struct msix_entry *msix_entries;
 struct octeon_device_priv *oct_priv = oct->priv;

 struct handshake *hs;

 switch (atomic_read(&oct->status)) {
 case OCT_DEV_RUNNING:
 case OCT_DEV_CORE_OK:

  /* No more instructions will be forwarded. */
  atomic_set(&oct->status, OCT_DEV_IN_RESET);

  oct->app_mode = CVM_DRV_INVALID_APP;
  dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
   lio_get_state_string(&oct->status));

  schedule_timeout_uninterruptible(HZ / 10);

  fallthrough;
 case OCT_DEV_HOST_OK:

 case OCT_DEV_CONSOLE_INIT_DONE:
  /* Remove any consoles */
  octeon_remove_consoles(oct);

  fallthrough;
 case OCT_DEV_IO_QUEUES_DONE:
  if (lio_wait_for_instr_fetch(oct))
   dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");

  if (wait_for_pending_requests(oct))
   dev_err(&oct->pci_dev->dev, "There were pending requests\n");

  /* Disable the input and output queues now. No more packets will
 * arrive from Octeon, but we should wait for all packet
 * processing to finish.
 */

  oct->fn_list.disable_io_queues(oct);

  if (lio_wait_for_oq_pkts(oct))
   dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");

  /* Force all requests waiting to be fetched by OCTEON to
 * complete.
 */

  for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
   struct octeon_instr_queue *iq;

   if (!(oct->io_qmask.iq & BIT_ULL(i)))
    continue;
   iq = oct->instr_queue[i];

   if (atomic_read(&iq->instr_pending)) {
    spin_lock_bh(&iq->lock);
    iq->fill_cnt = 0;
    iq->octeon_read_index = iq->host_write_index;
    iq->stats.instr_processed +=
     atomic_read(&iq->instr_pending);
    lio_process_iq_request_list(oct, iq, 0);
    spin_unlock_bh(&iq->lock);
   }
  }

  lio_process_ordered_list(oct, 1);
  octeon_free_sc_done_list(oct);
  octeon_free_sc_zombie_list(oct);

  fallthrough;
 case OCT_DEV_INTR_SET_DONE:
  /* Disable interrupts  */
  oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);

  if (oct->msix_on) {
   msix_entries = (struct msix_entry *)oct->msix_entries;
   for (i = 0; i < oct->num_msix_irqs - 1; i++) {
    if (oct->ioq_vector[i].vector) {
     /* clear the affinity_cpumask */
     irq_set_affinity_hint(
       msix_entries[i].vector,
       NULL);
     free_irq(msix_entries[i].vector,
       &oct->ioq_vector[i]);
     oct->ioq_vector[i].vector = 0;
    }
   }
   /* non-iov vector's argument is oct struct */
   free_irq(msix_entries[i].vector, oct);

   pci_disable_msix(oct->pci_dev);
   kfree(oct->msix_entries);
   oct->msix_entries = NULL;
  } else {
   /* Release the interrupt line */
   free_irq(oct->pci_dev->irq, oct);

   if (oct->flags & LIO_FLAG_MSI_ENABLED)
    pci_disable_msi(oct->pci_dev);
  }

  kfree(oct->irq_name_storage);
  oct->irq_name_storage = NULL;

  fallthrough;
 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
  if (OCTEON_CN23XX_PF(oct))
   octeon_free_ioq_vector(oct);

  fallthrough;
 case OCT_DEV_MBOX_SETUP_DONE:
  if (OCTEON_CN23XX_PF(oct))
   oct->fn_list.free_mbox(oct);

  fallthrough;
 case OCT_DEV_IN_RESET:
 case OCT_DEV_DROQ_INIT_DONE:
  /* Wait for any pending operations */
  mdelay(100);
  for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
   if (!(oct->io_qmask.oq & BIT_ULL(i)))
    continue;
   octeon_delete_droq(oct, i);
  }

  /* Force any pending handshakes to complete */
  for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
   hs = &handshake[i];

   if (hs->pci_dev) {
    handshake[oct->octeon_id].init_ok = 0;
    complete(&handshake[oct->octeon_id].init);
    handshake[oct->octeon_id].started_ok = 0;
    complete(&handshake[oct->octeon_id].started);
   }
  }

  fallthrough;
 case OCT_DEV_RESP_LIST_INIT_DONE:
  octeon_delete_response_list(oct);

  fallthrough;
 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
  for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
   if (!(oct->io_qmask.iq & BIT_ULL(i)))
    continue;
   octeon_delete_instr_queue(oct, i);
  }
#ifdef CONFIG_PCI_IOV
  if (oct->sriov_info.sriov_enabled)
   pci_disable_sriov(oct->pci_dev);
#endif
  fallthrough;
 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
  octeon_free_sc_buffer_pool(oct);

  fallthrough;
 case OCT_DEV_DISPATCH_INIT_DONE:
  octeon_delete_dispatch_list(oct);
  cancel_delayed_work_sync(&oct->nic_poll_work.work);

  fallthrough;
 case OCT_DEV_PCI_MAP_DONE:
  refcount = octeon_deregister_device(oct);

  /* Soft reset the octeon device before exiting.
 * However, if fw was loaded from card (i.e. autoboot),
 * perform an FLR instead.
 * Implementation note: only soft-reset the device
 * if it is a CN6XXX OR the LAST CN23XX device.
 */

  if (atomic_read(oct->adapter_fw_state) == FW_IS_PRELOADED)
   octeon_pci_flr(oct);
  else if (OCTEON_CN6XXX(oct) || !refcount)
   oct->fn_list.soft_reset(oct);

  octeon_unmap_pci_barx(oct, 0);
  octeon_unmap_pci_barx(oct, 1);

  fallthrough;
 case OCT_DEV_PCI_ENABLE_DONE:
  /* Disable the device, releasing the PCI INT */
  pci_disable_device(oct->pci_dev);

  fallthrough;
 case OCT_DEV_BEGIN_STATE:
  /* Nothing to be done here either */
  break;
 }                       /* end switch (oct->status) */

 tasklet_kill(&oct_priv->droq_tasklet);
}

/**
 * send_rx_ctrl_cmd - Send Rx control command
 * @lio: per-network private data
 * @start_stop: whether to start or stop
 */

static int send_rx_ctrl_cmd(struct lio *lio, int start_stop)
{
 struct octeon_soft_command *sc;
 union octnet_cmd *ncmd;
 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
 int retval;

 if (oct->props[lio->ifidx].rx_on == start_stop)
  return 0;

 sc = (struct octeon_soft_command *)
  octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
       16, 0);
 if (!sc) {
  netif_info(lio, rx_err, lio->netdev,
      "Failed to allocate octeon_soft_command struct\n");
  return -ENOMEM;
 }

 ncmd = (union octnet_cmd *)sc->virtdptr;

 ncmd->u64 = 0;
 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
 ncmd->s.param1 = start_stop;

 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));

 sc->iq_no = lio->linfo.txpciq[0].s.q_no;

 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
        OPCODE_NIC_CMD, 0, 0, 0);

 init_completion(&sc->complete);
 sc->sc_status = OCTEON_REQUEST_PENDING;

 retval = octeon_send_soft_command(oct, sc);
 if (retval == IQ_SEND_FAILED) {
  netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
  octeon_free_soft_command(oct, sc);
 } else {
  /* Sleep on a wait queue till the cond flag indicates that the
 * response arrived or timed-out.
 */

  retval = wait_for_sc_completion_timeout(oct, sc, 0);
  if (retval)
   return retval;

  oct->props[lio->ifidx].rx_on = start_stop;
  WRITE_ONCE(sc->caller_is_done, true);
 }

 return retval;
}

/**
 * liquidio_destroy_nic_device - Destroy NIC device interface
 * @oct: octeon device
 * @ifidx: which interface to destroy
 *
 * Cleanup associated with each interface for an Octeon device  when NIC
 * module is being unloaded or if initialization fails during load.
 */

static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
{
 struct net_device *netdev = oct->props[ifidx].netdev;
 struct octeon_device_priv *oct_priv = oct->priv;
 struct napi_struct *napi, *n;
 struct lio *lio;

 if (!netdev) {
  dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
   __func__, ifidx);
  return;
 }

 lio = GET_LIO(netdev);

 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");

 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
  liquidio_stop(netdev);

 if (oct->props[lio->ifidx].napi_enabled == 1) {
  list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
   napi_disable(napi);

  oct->props[lio->ifidx].napi_enabled = 0;

  if (OCTEON_CN23XX_PF(oct))
   oct->droq[0]->ops.poll_mode = 0;
 }

 /* Delete NAPI */
 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  netif_napi_del(napi);

 tasklet_enable(&oct_priv->droq_tasklet);

 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
  unregister_netdev(netdev);

 cleanup_sync_octeon_time_wq(netdev);
 cleanup_link_status_change_wq(netdev);

 cleanup_rx_oom_poll_fn(netdev);

 lio_delete_glists(lio);

 free_netdev(netdev);

 oct->props[ifidx].gmxport = -1;

 oct->props[ifidx].netdev = NULL;
}

/**
 * liquidio_stop_nic_module - Stop complete NIC functionality
 * @oct: octeon device
 */

static int liquidio_stop_nic_module(struct octeon_device *oct)
{
 int i, j;
 struct lio *lio;

 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
 device_lock(&oct->pci_dev->dev);
 if (oct->devlink) {
  devlink_unregister(oct->devlink);
  devlink_free(oct->devlink);
  oct->devlink = NULL;
 }
 device_unlock(&oct->pci_dev->dev);

 if (!oct->ifcount) {
  dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
  return 1;
 }

 spin_lock_bh(&oct->cmd_resp_wqlock);
 oct->cmd_resp_state = OCT_DRV_OFFLINE;
 spin_unlock_bh(&oct->cmd_resp_wqlock);

 lio_vf_rep_destroy(oct);

 for (i = 0; i < oct->ifcount; i++) {
  lio = GET_LIO(oct->props[i].netdev);
  for (j = 0; j < oct->num_oqs; j++)
   octeon_unregister_droq_ops(oct,
         lio->linfo.rxpciq[j].s.q_no);
 }

 for (i = 0; i < oct->ifcount; i++)
  liquidio_destroy_nic_device(oct, i);

 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
 return 0;
}

/**
 * liquidio_remove - Cleans up resources at unload time
 * @pdev: PCI device structure
 */

static void liquidio_remove(struct pci_dev *pdev)
{
 struct octeon_device *oct_dev = pci_get_drvdata(pdev);

 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");

 if (oct_dev->watchdog_task)
  kthread_stop(oct_dev->watchdog_task);

 if (!oct_dev->octeon_id &&
     oct_dev->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP)
  lio_vf_rep_modexit();

 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
  liquidio_stop_nic_module(oct_dev);

 /* Reset the octeon device and cleanup all memory allocated for
 * the octeon device by driver.
 */

 octeon_destroy_resources(oct_dev);

 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");

 /* This octeon device has been removed. Update the global
 * data structure to reflect this. Free the device structure.
 */

 octeon_free_device_mem(oct_dev);
}

/**
 * octeon_chip_specific_setup - Identify the Octeon device and to map the BAR address space
 * @oct: octeon device
 */

static int octeon_chip_specific_setup(struct octeon_device *oct)
{
 u32 dev_id, rev_id;
 int ret = 1;

 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
 oct->rev_id = rev_id & 0xff;

 switch (dev_id) {
 case OCTEON_CN68XX_PCIID:
  oct->chip_id = OCTEON_CN68XX;
  ret = lio_setup_cn68xx_octeon_device(oct);
  break;

 case OCTEON_CN66XX_PCIID:
  oct->chip_id = OCTEON_CN66XX;
  ret = lio_setup_cn66xx_octeon_device(oct);
  break;

 case OCTEON_CN23XX_PCIID_PF:
  oct->chip_id = OCTEON_CN23XX_PF_VID;
  ret = setup_cn23xx_octeon_pf_device(oct);
  if (ret)
   break;
#ifdef CONFIG_PCI_IOV
  if (!ret)
   pci_sriov_set_totalvfs(oct->pci_dev,
            oct->sriov_info.max_vfs);
#endif
  break;

 default:
  dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
   dev_id);
 }

 return ret;
}

/**
 * octeon_pci_os_setup - PCI initialization for each Octeon device.
 * @oct: octeon device
 */

static int octeon_pci_os_setup(struct octeon_device *oct)
{
 /* setup PCI stuff first */
 if (pci_enable_device(oct->pci_dev)) {
  dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
  return 1;
 }

 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
  dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
  pci_disable_device(oct->pci_dev);
  return 1;
 }

 /* Enable PCI DMA Master. */
 pci_set_master(oct->pci_dev);

 return 0;
}

/**
 * free_netbuf - Unmap and free network buffer
 * @buf: buffer
 */

static void free_netbuf(void *buf)
{
 struct sk_buff *skb;
 struct octnet_buf_free_info *finfo;
 struct lio *lio;

 finfo = (struct octnet_buf_free_info *)buf;
 skb = finfo->skb;
 lio = finfo->lio;

 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
    DMA_TO_DEVICE);

 tx_buffer_free(skb);
}

/**
 * free_netsgbuf - Unmap and free gather buffer
 * @buf: buffer
 */

static void free_netsgbuf(void *buf)
{
 struct octnet_buf_free_info *finfo;
 struct sk_buff *skb;
 struct lio *lio;
 struct octnic_gather *g;
 int i, frags, iq;

 finfo = (struct octnet_buf_free_info *)buf;
 skb = finfo->skb;
 lio = finfo->lio;
 g = finfo->g;
 frags = skb_shinfo(skb)->nr_frags;

 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
    g->sg[0].ptr[0], (skb->len - skb->data_len),
    DMA_TO_DEVICE);

 i = 1;
 while (frags--) {
  skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];

  dma_unmap_page(&lio->oct_dev->pci_dev->dev,
          g->sg[(i >> 2)].ptr[(i & 3)],
          skb_frag_size(frag), DMA_TO_DEVICE);
  i++;
 }

 iq = skb_iq(lio->oct_dev, skb);
 spin_lock(&lio->glist_lock[iq]);
 list_add_tail(&g->list, &lio->glist[iq]);
 spin_unlock(&lio->glist_lock[iq]);

 tx_buffer_free(skb);
}

/**
 * free_netsgbuf_with_resp - Unmap and free gather buffer with response
 * @buf: buffer
 */

static void free_netsgbuf_with_resp(void *buf)
{
 struct octeon_soft_command *sc;
 struct octnet_buf_free_info *finfo;
 struct sk_buff *skb;
 struct lio *lio;
 struct octnic_gather *g;
 int i, frags, iq;

 sc = (struct octeon_soft_command *)buf;
 skb = (struct sk_buff *)sc->callback_arg;
 finfo = (struct octnet_buf_free_info *)&skb->cb;

 lio = finfo->lio;
 g = finfo->g;
 frags = skb_shinfo(skb)->nr_frags;

 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
    g->sg[0].ptr[0], (skb->len - skb->data_len),
    DMA_TO_DEVICE);

 i = 1;
 while (frags--) {
  skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];

  dma_unmap_page(&lio->oct_dev->pci_dev->dev,
          g->sg[(i >> 2)].ptr[(i & 3)],
          skb_frag_size(frag), DMA_TO_DEVICE);
  i++;
 }

 iq = skb_iq(lio->oct_dev, skb);

 spin_lock(&lio->glist_lock[iq]);
 list_add_tail(&g->list, &lio->glist[iq]);
 spin_unlock(&lio->glist_lock[iq]);

 /* Don't free the skb yet */
}

/**
 * liquidio_ptp_adjfine - Adjust ptp frequency
 * @ptp: PTP clock info
 * @scaled_ppm: how much to adjust by, in scaled parts-per-million
 *
 * Scaled parts per million is ppm with a 16-bit binary fractional field.
 */

static int liquidio_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
{
 struct lio *lio = container_of(ptp, struct lio, ptp_info);
 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
 s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
 u64 comp, delta;
 unsigned long flags;
 bool neg_adj = false;

 if (ppb < 0) {
  neg_adj = true;
  ppb = -ppb;
 }

 /* The hardware adds the clock compensation value to the
 * PTP clock on every coprocessor clock cycle, so we
 * compute the delta in terms of coprocessor clocks.
 */

 delta = (u64)ppb << 32;
 do_div(delta, oct->coproc_clock_rate);

 spin_lock_irqsave(&lio->ptp_lock, flags);
 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
 if (neg_adj)
  comp -= delta;
 else
  comp += delta;
 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
 spin_unlock_irqrestore(&lio->ptp_lock, flags);

 return 0;
}

/**
 * liquidio_ptp_adjtime - Adjust ptp time
 * @ptp: PTP clock info
 * @delta: how much to adjust by, in nanosecs
 */

static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
{
 unsigned long flags;
 struct lio *lio = container_of(ptp, struct lio, ptp_info);

 spin_lock_irqsave(&lio->ptp_lock, flags);
 lio->ptp_adjust += delta;
 spin_unlock_irqrestore(&lio->ptp_lock, flags);

 return 0;
}

/**
 * liquidio_ptp_gettime - Get hardware clock time, including any adjustment
 * @ptp: PTP clock info
 * @ts: timespec
 */

static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
    struct timespec64 *ts)
{
 u64 ns;
 unsigned long flags;
 struct lio *lio = container_of(ptp, struct lio, ptp_info);
 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;

 spin_lock_irqsave(&lio->ptp_lock, flags);
 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
 ns += lio->ptp_adjust;
 spin_unlock_irqrestore(&lio->ptp_lock, flags);

 *ts = ns_to_timespec64(ns);

 return 0;
}

/**
 * liquidio_ptp_settime - Set hardware clock time. Reset adjustment
 * @ptp: PTP clock info
 * @ts: timespec
 */

static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
    const struct timespec64 *ts)
{
 u64 ns;
 unsigned long flags;
 struct lio *lio = container_of(ptp, struct lio, ptp_info);
 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;

 ns = timespec64_to_ns(ts);

 spin_lock_irqsave(&lio->ptp_lock, flags);
 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
 lio->ptp_adjust = 0;
 spin_unlock_irqrestore(&lio->ptp_lock, flags);

 return 0;
}

/**
 * liquidio_ptp_enable - Check if PTP is enabled
 * @ptp: PTP clock info
 * @rq: request
 * @on: is it on
 */

static int
liquidio_ptp_enable(struct ptp_clock_info __maybe_unused *ptp,
      struct ptp_clock_request __maybe_unused *rq,
      int __maybe_unused on)
{
 return -EOPNOTSUPP;
}

/**
 * oct_ptp_open - Open PTP clock source
 * @netdev: network device
 */

static void oct_ptp_open(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;

 spin_lock_init(&lio->ptp_lock);

 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
 lio->ptp_info.owner = THIS_MODULE;
 lio->ptp_info.max_adj = 250000000;
 lio->ptp_info.n_alarm = 0;
 lio->ptp_info.n_ext_ts = 0;
 lio->ptp_info.n_per_out = 0;
 lio->ptp_info.pps = 0;
 lio->ptp_info.adjfine = liquidio_ptp_adjfine;
 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
 lio->ptp_info.settime64 = liquidio_ptp_settime;
 lio->ptp_info.enable = liquidio_ptp_enable;

 lio->ptp_adjust = 0;

 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
          &oct->pci_dev->dev);

 if (IS_ERR(lio->ptp_clock))
  lio->ptp_clock = NULL;
}

/**
 * liquidio_ptp_init - Init PTP clock
 * @oct: octeon device
 */

static void liquidio_ptp_init(struct octeon_device *oct)
{
 u64 clock_comp, cfg;

 clock_comp = (u64)NSEC_PER_SEC << 32;
 do_div(clock_comp, oct->coproc_clock_rate);
 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);

 /* Enable */
 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
}

/**
 * load_firmware - Load firmware to device
 * @oct: octeon device
 *
 * Maps device to firmware filename, requests firmware, and downloads it
 */

static int load_firmware(struct octeon_device *oct)
{
 int ret = 0;
 const struct firmware *fw;
 char fw_name[LIO_MAX_FW_FILENAME_LEN];
 char *tmp_fw_type;

 if (fw_type_is_auto()) {
  tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
  strscpy_pad(fw_type, tmp_fw_type, sizeof(fw_type));
 } else {
  tmp_fw_type = fw_type;
 }

 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
  octeon_get_conf(oct)->card_name, tmp_fw_type,
  LIO_FW_NAME_SUFFIX);

 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
 if (ret) {
  dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n",
   fw_name);
  release_firmware(fw);
  return ret;
 }

 ret = octeon_download_firmware(oct, fw->data, fw->size);

 release_firmware(fw);

 return ret;
}

/**
 * octnet_poll_check_txq_status - Poll routine for checking transmit queue status
 * @work: work_struct data structure
 */

static void octnet_poll_check_txq_status(struct work_struct *work)
{
 struct cavium_wk *wk = (struct cavium_wk *)work;
 struct lio *lio = (struct lio *)wk->ctxptr;

 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
  return;

 check_txq_status(lio);
 queue_delayed_work(lio->txq_status_wq.wq,
      &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
}

/**
 * setup_tx_poll_fn - Sets up the txq poll check
 * @netdev: network device
 */

static inline int setup_tx_poll_fn(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;

 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
      WQ_MEM_RECLAIM, 0);
 if (!lio->txq_status_wq.wq) {
  dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
  return -1;
 }
 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
     octnet_poll_check_txq_status);
 lio->txq_status_wq.wk.ctxptr = lio;
 queue_delayed_work(lio->txq_status_wq.wq,
      &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
 return 0;
}

static inline void cleanup_tx_poll_fn(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);

 if (lio->txq_status_wq.wq) {
  cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
  destroy_workqueue(lio->txq_status_wq.wq);
 }
}

/**
 * liquidio_open - Net device open for LiquidIO
 * @netdev: network device
 */

static int liquidio_open(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;
 struct octeon_device_priv *oct_priv = oct->priv;
 struct napi_struct *napi, *n;
 int ret = 0;

 if (oct->props[lio->ifidx].napi_enabled == 0) {
  tasklet_disable(&oct_priv->droq_tasklet);

  list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
   napi_enable(napi);

  oct->props[lio->ifidx].napi_enabled = 1;

  if (OCTEON_CN23XX_PF(oct))
   oct->droq[0]->ops.poll_mode = 1;
 }

 if (oct->ptp_enable)
  oct_ptp_open(netdev);

 ifstate_set(lio, LIO_IFSTATE_RUNNING);

 if (!OCTEON_CN23XX_PF(oct) || !oct->msix_on) {
  ret = setup_tx_poll_fn(netdev);
  if (ret)
   goto err_poll;
 }

 netif_tx_start_all_queues(netdev);

 /* Ready for link status updates */
 lio->intf_open = 1;

 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");

 /* tell Octeon to start forwarding packets to host */
 ret = send_rx_ctrl_cmd(lio, 1);
 if (ret)
  goto err_rx_ctrl;

 /* start periodical statistics fetch */
 INIT_DELAYED_WORK(&lio->stats_wk.work, lio_fetch_stats);
 lio->stats_wk.ctxptr = lio;
 schedule_delayed_work(&lio->stats_wk.work, msecs_to_jiffies
     (LIQUIDIO_NDEV_STATS_POLL_TIME_MS));

 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
   netdev->name);

 return 0;

err_rx_ctrl:
 if (!OCTEON_CN23XX_PF(oct) || !oct->msix_on)
  cleanup_tx_poll_fn(netdev);
err_poll:
 if (lio->ptp_clock) {
  ptp_clock_unregister(lio->ptp_clock);
  lio->ptp_clock = NULL;
 }

 if (oct->props[lio->ifidx].napi_enabled == 1) {
  list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
   napi_disable(napi);

  oct->props[lio->ifidx].napi_enabled = 0;

  if (OCTEON_CN23XX_PF(oct))
   oct->droq[0]->ops.poll_mode = 0;
 }

 return ret;
}

/**
 * liquidio_stop - Net device stop for LiquidIO
 * @netdev: network device
 */

static int liquidio_stop(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;
 struct octeon_device_priv *oct_priv = oct->priv;
 struct napi_struct *napi, *n;
 int ret = 0;

 ifstate_reset(lio, LIO_IFSTATE_RUNNING);

 /* Stop any link updates */
 lio->intf_open = 0;

 stop_txqs(netdev);

 /* Inform that netif carrier is down */
 netif_carrier_off(netdev);
 netif_tx_disable(netdev);

 lio->linfo.link.s.link_up = 0;
 lio->link_changes++;

 /* Tell Octeon that nic interface is down. */
 ret = send_rx_ctrl_cmd(lio, 0);
 if (ret)
  return ret;

 if (OCTEON_CN23XX_PF(oct)) {
  if (!oct->msix_on)
   cleanup_tx_poll_fn(netdev);
 } else {
  cleanup_tx_poll_fn(netdev);
 }

 cancel_delayed_work_sync(&lio->stats_wk.work);

 if (lio->ptp_clock) {
  ptp_clock_unregister(lio->ptp_clock);
  lio->ptp_clock = NULL;
 }

 /* Wait for any pending Rx descriptors */
 if (lio_wait_for_clean_oq(oct))
  netif_info(lio, rx_err, lio->netdev,
      "Proceeding with stop interface after partial RX desc processing\n");

 if (oct->props[lio->ifidx].napi_enabled == 1) {
  list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
   napi_disable(napi);

  oct->props[lio->ifidx].napi_enabled = 0;

  if (OCTEON_CN23XX_PF(oct))
   oct->droq[0]->ops.poll_mode = 0;

  tasklet_enable(&oct_priv->droq_tasklet);
 }

 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);

 return ret;
}

/**
 * get_new_flags - Converts a mask based on net device flags
 * @netdev: network device
 *
 * This routine generates a octnet_ifflags mask from the net device flags
 * received from the OS.
 */

static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
{
 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;

 if (netdev->flags & IFF_PROMISC)
  f |= OCTNET_IFFLAG_PROMISC;

 if (netdev->flags & IFF_ALLMULTI)
  f |= OCTNET_IFFLAG_ALLMULTI;

 if (netdev->flags & IFF_MULTICAST) {
  f |= OCTNET_IFFLAG_MULTICAST;

  /* Accept all multicast addresses if there are more than we
 * can handle
 */

  if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
   f |= OCTNET_IFFLAG_ALLMULTI;
 }

 if (netdev->flags & IFF_BROADCAST)
  f |= OCTNET_IFFLAG_BROADCAST;

 return f;
}

/**
 * liquidio_set_mcast_list - Net device set_multicast_list
 * @netdev: network device
 */

static void liquidio_set_mcast_list(struct net_device *netdev)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;
 struct octnic_ctrl_pkt nctrl;
 struct netdev_hw_addr *ha;
 u64 *mc;
 int ret;
 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);

 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));

 /* Create a ctrl pkt command to be sent to core app. */
 nctrl.ncmd.u64 = 0;
 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
 nctrl.ncmd.s.param1 = get_new_flags(netdev);
 nctrl.ncmd.s.param2 = mc_count;
 nctrl.ncmd.s.more = mc_count;
 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
 nctrl.netpndev = (u64)netdev;
 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;

 /* copy all the addresses into the udd */
 mc = &nctrl.udd[0];
 netdev_for_each_mc_addr(ha, netdev) {
  *mc = 0;
  memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
  /* no need to swap bytes */

  if (++mc > &nctrl.udd[mc_count])
   break;
 }

 /* Apparently, any activity in this call from the kernel has to
 * be atomic. So we won't wait for response.
 */


 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
 if (ret) {
  dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
   ret);
 }
}

/**
 * liquidio_set_mac - Net device set_mac_address
 * @netdev: network device
 * @p: pointer to sockaddr
 */

static int liquidio_set_mac(struct net_device *netdev, void *p)
{
 int ret = 0;
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct = lio->oct_dev;
 struct sockaddr *addr = (struct sockaddr *)p;
 struct octnic_ctrl_pkt nctrl;

 if (!is_valid_ether_addr(addr->sa_data))
  return -EADDRNOTAVAIL;

 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));

 nctrl.ncmd.u64 = 0;
 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
 nctrl.ncmd.s.param1 = 0;
 nctrl.ncmd.s.more = 1;
 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
 nctrl.netpndev = (u64)netdev;

 nctrl.udd[0] = 0;
 /* The MAC Address is presented in network byte order. */
 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);

 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
 if (ret < 0) {
  dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
  return -ENOMEM;
 }

 if (nctrl.sc_status) {
  dev_err(&oct->pci_dev->dev,
   "%s: MAC Address change failed. sc return=%x\n",
    __func__, nctrl.sc_status);
  return -EIO;
 }

 eth_hw_addr_set(netdev, addr->sa_data);
 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);

 return 0;
}

static void
liquidio_get_stats64(struct net_device *netdev,
       struct rtnl_link_stats64 *lstats)
{
 struct lio *lio = GET_LIO(netdev);
 struct octeon_device *oct;
 u64 pkts = 0, drop = 0, bytes = 0;
 struct oct_droq_stats *oq_stats;
 struct oct_iq_stats *iq_stats;
 int i, iq_no, oq_no;

 oct = lio->oct_dev;

 if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
  return;

 for (i = 0; i < oct->num_iqs; i++) {
  iq_no = lio->linfo.txpciq[i].s.q_no;
  iq_stats = &oct->instr_queue[iq_no]->stats;
  pkts += iq_stats->tx_done;
  drop += iq_stats->tx_dropped;
  bytes += iq_stats->tx_tot_bytes;
 }

 lstats->tx_packets = pkts;
 lstats->tx_bytes = bytes;
 lstats->tx_dropped = drop;

 pkts = 0;
 drop = 0;
 bytes = 0;

 for (i = 0; i < oct->num_oqs; i++) {
  oq_no = lio->linfo.rxpciq[i].s.q_no;
  oq_stats = &oct->droq[oq_no]->stats;
  pkts += oq_stats->rx_pkts_received;
  drop += (oq_stats->rx_dropped +
    oq_stats->dropped_nodispatch +
    oq_stats->dropped_toomany +
    oq_stats->dropped_nomem);
  bytes += oq_stats->rx_bytes_received;
 }

 lstats->rx_bytes = bytes;
 lstats->rx_packets = pkts;
 lstats->rx_dropped = drop;

 lstats->multicast = oct->link_stats.fromwire.fw_total_mcast;
 lstats->collisions = oct->link_stats.fromhost.total_collisions;

 /* detailed rx_errors: */
 lstats->rx_length_errors = oct->link_stats.fromwire.l2_err;
 /* recved pkt with crc error    */
 lstats->rx_crc_errors = oct->link_stats.fromwire.fcs_err;
 /* recv'd frame alignment error */
 lstats->rx_frame_errors = oct->link_stats.fromwire.frame_err;
 /* recv'r fifo overrun */
 lstats->rx_fifo_errors = oct->link_stats.fromwire.fifo_err;

 lstats->rx_errors = lstats->rx_length_errors + lstats->rx_crc_errors +
  lstats->rx_frame_errors + lstats->rx_fifo_errors;

 /* detailed tx_errors */
 lstats->tx_aborted_errors = oct->link_stats.fromhost.fw_err_pko;
 lstats->tx_carrier_errors = oct->link_stats.fromhost.fw_err_link;
 lstats->tx_fifo_errors = oct->link_stats.fromhost.fifo_err;

 lstats->tx_errors = lstats->tx_aborted_errors +
  lstats->tx_carrier_errors +
  lstats->tx_fifo_errors;
}

/**
 * hwtstamp_ioctl - Handler for SIOCSHWTSTAMP ioctl
 * @netdev: network device
 * @ifr: interface request
 */

static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
{
 struct hwtstamp_config conf;
 struct lio *lio = GET_LIO(netdev);

 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
  return -EFAULT;

 switch (conf.tx_type) {
 case HWTSTAMP_TX_ON:
 case HWTSTAMP_TX_OFF:
  break;
 default:
  return -ERANGE;
 }

 switch (conf.rx_filter) {
 case HWTSTAMP_FILTER_NONE:
  break;
 case HWTSTAMP_FILTER_ALL:
 case HWTSTAMP_FILTER_SOME:
 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
 case HWTSTAMP_FILTER_PTP_V2_EVENT:
 case HWTSTAMP_FILTER_PTP_V2_SYNC:
 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
 case HWTSTAMP_FILTER_NTP_ALL:
  conf.rx_filter = HWTSTAMP_FILTER_ALL;
  break;
 default:
  return -ERANGE;
 }

 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
  ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);

 else
  ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);

 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
}

/**
 * liquidio_ioctl - ioctl handler
 * @netdev: network device
 * @ifr: interface request
 * @cmd: command
 */

static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
{
 struct lio *lio = GET_LIO(netdev);

 switch (cmd) {
 case SIOCSHWTSTAMP:
  if (lio->oct_dev->ptp_enable)
   return hwtstamp_ioctl(netdev, ifr);
  fallthrough;
 default:
  return -EOPNOTSUPP;
 }
}

/**
 * handle_timestamp - handle a Tx timestamp response
 * @oct: octeon device
 * @status: response status
 * @buf: pointer to skb
 */

static void handle_timestamp(struct octeon_device *oct,
        u32 status,
        void *buf)
{
 struct octnet_buf_free_info *finfo;
 struct octeon_soft_command *sc;
 struct oct_timestamp_resp *resp;
 struct lio *lio;
 struct sk_buff *skb = (struct sk_buff *)buf;

 finfo = (struct octnet_buf_free_info *)skb->cb;
 lio = finfo->lio;
 sc = finfo->sc;
 oct = lio->oct_dev;
 resp = (struct oct_timestamp_resp *)sc->virtrptr;

 if (status != OCTEON_REQUEST_DONE) {
  dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
   CVM_CAST64(status));
  resp->timestamp = 0;
 }

 octeon_swap_8B_data(&resp->timestamp, 1);

 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
  struct skb_shared_hwtstamps ts;
  u64 ns = resp->timestamp;

  netif_info(lio, tx_done, lio->netdev,
      "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
      skb, (unsigned long long)ns);
  ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
  skb_tstamp_tx(skb, &ts);
 }

 octeon_free_soft_command(oct, sc);
 tx_buffer_free(skb);
}

/**
 * send_nic_timestamp_pkt - Send a data packet that will be timestamped
 * @oct: octeon device
 * @ndata: pointer to network data
 * @finfo: pointer to private network data
 * @xmit_more: more is coming
 */

static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
      struct octnic_data_pkt *ndata,
      struct octnet_buf_free_info *finfo,
      int xmit_more)
{
 int retval;
 struct octeon_soft_command *sc;
 struct lio *lio;
 int ring_doorbell;
 u32 len;

 lio = finfo->lio;

 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
         sizeof(struct oct_timestamp_resp));
 finfo->sc = sc;

 if (!sc) {
  dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
  return IQ_SEND_FAILED;
 }

 if (ndata->reqtype == REQTYPE_NORESP_NET)
  ndata->reqtype = REQTYPE_RESP_NET;
 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
  ndata->reqtype = REQTYPE_RESP_NET_SG;

 sc->callback = handle_timestamp;
 sc->callback_arg = finfo->skb;
 sc->iq_no = ndata->q_no;

 if (OCTEON_CN23XX_PF(oct))
  len = (u32)((struct octeon_instr_ih3 *)
       (&sc->cmd.cmd3.ih3))->dlengsz;
 else
  len = (u32)((struct octeon_instr_ih2 *)
       (&sc->cmd.cmd2.ih2))->dlengsz;

 ring_doorbell = !xmit_more;

 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
         sc, len, ndata->reqtype);

 if (retval == IQ_SEND_FAILED) {
  dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
   retval);
  octeon_free_soft_command(oct, sc);
 } else {
  netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
 }

 return retval;
}

/**
 * liquidio_xmit - Transmit networks packets to the Octeon interface
 * @skb: skbuff struct to be passed to network layer.
 * @netdev: pointer to network device
 *
 * Return: whether the packet was transmitted to the device okay or not
 *             (NETDEV_TX_OK or NETDEV_TX_BUSY)
 */

static netdev_tx_t liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
{
 struct lio *lio;
 struct octnet_buf_free_info *finfo;
 union octnic_cmd_setup cmdsetup;
 struct octnic_data_pkt ndata;
 struct octeon_device *oct;
 struct oct_iq_stats *stats;
 struct octeon_instr_irh *irh;
 union tx_info *tx_info;
 int status = 0;
 int q_idx = 0, iq_no = 0;
 int j, xmit_more = 0;
 u64 dptr = 0;
 u32 tag = 0;

 lio = GET_LIO(netdev);
 oct = lio->oct_dev;

 q_idx = skb_iq(oct, skb);
 tag = q_idx;
 iq_no = lio->linfo.txpciq[q_idx].s.q_no;

 stats = &oct->instr_queue[iq_no]->stats;

 /* Check for all conditions in which the current packet cannot be
 * transmitted.
 */

 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
     (!lio->linfo.link.s.link_up) ||
     (skb->len <= 0)) {
  netif_info(lio, tx_err, lio->netdev,
      "Transmit failed link_status : %d\n",
      lio->linfo.link.s.link_up);
  goto lio_xmit_failed;
 }

 /* Use space in skb->cb to store info used to unmap and
 * free the buffers.
 */

 finfo = (struct octnet_buf_free_info *)skb->cb;
 finfo->lio = lio;
 finfo->skb = skb;
 finfo->sc = NULL;

 /* Prepare the attributes for the data to be passed to OSI. */
 memset(&ndata, 0, sizeof(struct octnic_data_pkt));

 ndata.buf = (void *)finfo;

 ndata.q_no = iq_no;

 if (octnet_iq_is_full(oct, ndata.q_no)) {
  /* defer sending if queue is full */
  netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
      ndata.q_no);
  stats->tx_iq_busy++;
  return NETDEV_TX_BUSY;
 }

 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu:  %d, q_no:%d\n",
 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
 */


 ndata.datasize = skb->len;

 cmdsetup.u64 = 0;
 cmdsetup.s.iq_no = iq_no;

 if (skb->ip_summed == CHECKSUM_PARTIAL) {
  if (skb->encapsulation) {
   cmdsetup.s.tnl_csum = 1;
   stats->tx_vxlan++;
  } else {
   cmdsetup.s.transport_csum = 1;
  }
 }
 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  cmdsetup.s.timestamp = 1;
 }

 if (skb_shinfo(skb)->nr_frags == 0) {
  cmdsetup.s.u.datasize = skb->len;
  octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);

  /* Offload checksum calculation for TCP/UDP packets */
  dptr = dma_map_single(&oct->pci_dev->dev,
          skb->data,
          skb->len,
          DMA_TO_DEVICE);
  if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
   dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
    __func__);
   stats->tx_dmamap_fail++;
   return NETDEV_TX_BUSY;
  }

  if (OCTEON_CN23XX_PF(oct))
   ndata.cmd.cmd3.dptr = dptr;
  else
   ndata.cmd.cmd2.dptr = dptr;
  finfo->dptr = dptr;
  ndata.reqtype = REQTYPE_NORESP_NET;

 } else {
  int i, frags;
  skb_frag_t *frag;
  struct octnic_gather *g;

  spin_lock(&lio->glist_lock[q_idx]);
  g = (struct octnic_gather *)
   lio_list_delete_head(&lio->glist[q_idx]);
  spin_unlock(&lio->glist_lock[q_idx]);

  if (!g) {
   netif_info(lio, tx_err, lio->netdev,
       "Transmit scatter gather: glist null!\n");
   goto lio_xmit_failed;
  }

  cmdsetup.s.gather = 1;
  cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
  octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);

  memset(g->sg, 0, g->sg_size);

  g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
       skb->data,
       (skb->len - skb->data_len),
       DMA_TO_DEVICE);
  if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
   dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
    __func__);
   stats->tx_dmamap_fail++;
   return NETDEV_TX_BUSY;
  }
  add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);

  frags = skb_shinfo(skb)->nr_frags;
  i = 1;
  while (frags--) {
   frag = &skb_shinfo(skb)->frags[i - 1];

   g->sg[(i >> 2)].ptr[(i & 3)] =
    skb_frag_dma_map(&oct->pci_dev->dev,
              frag, 0, skb_frag_size(frag),
       DMA_TO_DEVICE);

   if (dma_mapping_error(&oct->pci_dev->dev,
           g->sg[i >> 2].ptr[i & 3])) {
    dma_unmap_single(&oct->pci_dev->dev,
       g->sg[0].ptr[0],
       skb->len - skb->data_len,
       DMA_TO_DEVICE);
    for (j = 1; j < i; j++) {
     frag = &skb_shinfo(skb)->frags[j - 1];
     dma_unmap_page(&oct->pci_dev->dev,
             g->sg[j >> 2].ptr[j & 3],
             skb_frag_size(frag),
             DMA_TO_DEVICE);
    }
    dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
     __func__);
    return NETDEV_TX_BUSY;
   }

   add_sg_size(&g->sg[(i >> 2)], skb_frag_size(frag),
        (i & 3));
   i++;
  }

  dptr = g->sg_dma_ptr;

  if (OCTEON_CN23XX_PF(oct))
   ndata.cmd.cmd3.dptr = dptr;
  else
   ndata.cmd.cmd2.dptr = dptr;
  finfo->dptr = dptr;
  finfo->g = g;

  ndata.reqtype = REQTYPE_NORESP_NET_SG;
 }

 if (OCTEON_CN23XX_PF(oct)) {
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=95 H=97 G=95

¤ Dauer der Verarbeitung: 0.26 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge