1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2016 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 details.
17 ***********************************************************************/
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/pci.h>
21 #include <net/vxlan.h>
22 #include "liquidio_common.h"
23 #include "octeon_droq.h"
24 #include "octeon_iq.h"
25 #include "response_manager.h"
26 #include "octeon_device.h"
27 #include "octeon_nic.h"
28 #include "octeon_main.h"
29 #include "octeon_network.h"
30 #include "cn23xx_vf_device.h"
32 MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
33 MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Virtual Function Driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(LIQUIDIO_VERSION);
37 static int debug = -1;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
41 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
43 struct liquidio_if_cfg_context {
51 struct liquidio_if_cfg_resp {
53 struct liquidio_if_cfg_info cfg_info;
57 struct liquidio_rx_ctl_context {
65 struct oct_timestamp_resp {
74 #ifdef __BIG_ENDIAN_BITFIELD
86 #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
88 #define OCTNIC_GSO_MAX_HEADER_SIZE 128
89 #define OCTNIC_GSO_MAX_SIZE \
90 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
92 struct octnic_gather {
93 /* List manipulation. Next and prev pointers. */
94 struct list_head list;
96 /* Size of the gather component at sg in bytes. */
99 /* Number of bytes that sg was adjusted to make it 8B-aligned. */
102 /* Gather component that can accommodate max sized fragment list
103 * received from the IP layer.
105 struct octeon_sg_entry *sg;
107 dma_addr_t sg_dma_ptr;
111 liquidio_vf_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
112 static void liquidio_vf_remove(struct pci_dev *pdev);
113 static int octeon_device_init(struct octeon_device *oct);
114 static int liquidio_stop(struct net_device *netdev);
116 static int lio_wait_for_oq_pkts(struct octeon_device *oct)
118 struct octeon_device_priv *oct_priv =
119 (struct octeon_device_priv *)oct->priv;
120 int retry = MAX_IO_PENDING_PKT_COUNT;
121 int pkt_cnt = 0, pending_pkts;
127 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
128 if (!(oct->io_qmask.oq & BIT_ULL(i)))
130 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
133 pending_pkts += pkt_cnt;
134 tasklet_schedule(&oct_priv->droq_tasklet);
137 schedule_timeout_uninterruptible(1);
139 } while (retry-- && pending_pkts);
145 * \brief Cause device to go quiet so it can be safely removed/reset/etc
146 * @param oct Pointer to Octeon device
148 static void pcierror_quiesce_device(struct octeon_device *oct)
152 /* Disable the input and output queues now. No more packets will
153 * arrive from Octeon, but we should wait for all packet processing
157 /* To allow for in-flight requests */
158 schedule_timeout_uninterruptible(100);
160 if (wait_for_pending_requests(oct))
161 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
163 /* Force all requests waiting to be fetched by OCTEON to complete. */
164 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
165 struct octeon_instr_queue *iq;
167 if (!(oct->io_qmask.iq & BIT_ULL(i)))
169 iq = oct->instr_queue[i];
171 if (atomic_read(&iq->instr_pending)) {
172 spin_lock_bh(&iq->lock);
174 iq->octeon_read_index = iq->host_write_index;
175 iq->stats.instr_processed +=
176 atomic_read(&iq->instr_pending);
177 lio_process_iq_request_list(oct, iq, 0);
178 spin_unlock_bh(&iq->lock);
182 /* Force all pending ordered list requests to time out. */
183 lio_process_ordered_list(oct, 1);
185 /* We do not need to wait for output queue packets to be processed. */
189 * \brief Cleanup PCI AER uncorrectable error status
190 * @param dev Pointer to PCI device
192 static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
197 pr_info("%s :\n", __func__);
199 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
200 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
201 if (dev->error_state == pci_channel_io_normal)
202 status &= ~mask; /* Clear corresponding nonfatal bits */
204 status &= mask; /* Clear corresponding fatal bits */
205 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
209 * \brief Stop all PCI IO to a given device
210 * @param dev Pointer to Octeon device
212 static void stop_pci_io(struct octeon_device *oct)
214 struct msix_entry *msix_entries;
217 /* No more instructions will be forwarded. */
218 atomic_set(&oct->status, OCT_DEV_IN_RESET);
220 for (i = 0; i < oct->ifcount; i++)
221 netif_device_detach(oct->props[i].netdev);
223 /* Disable interrupts */
224 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
226 pcierror_quiesce_device(oct);
228 msix_entries = (struct msix_entry *)oct->msix_entries;
229 for (i = 0; i < oct->num_msix_irqs; i++) {
230 /* clear the affinity_cpumask */
231 irq_set_affinity_hint(msix_entries[i].vector,
233 free_irq(msix_entries[i].vector,
234 &oct->ioq_vector[i]);
236 pci_disable_msix(oct->pci_dev);
237 kfree(oct->msix_entries);
238 oct->msix_entries = NULL;
239 octeon_free_ioq_vector(oct);
241 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
242 lio_get_state_string(&oct->status));
244 /* making it a common function for all OCTEON models */
245 cleanup_aer_uncorrect_error_status(oct->pci_dev);
247 pci_disable_device(oct->pci_dev);
251 * \brief called when PCI error is detected
252 * @param pdev Pointer to PCI device
253 * @param state The current pci connection state
255 * This function is called after a PCI bus error affecting
256 * this device has been detected.
258 static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
259 pci_channel_state_t state)
261 struct octeon_device *oct = pci_get_drvdata(pdev);
263 /* Non-correctable Non-fatal errors */
264 if (state == pci_channel_io_normal) {
265 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
266 cleanup_aer_uncorrect_error_status(oct->pci_dev);
267 return PCI_ERS_RESULT_CAN_RECOVER;
270 /* Non-correctable Fatal errors */
271 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
274 return PCI_ERS_RESULT_DISCONNECT;
277 /* For PCI-E Advanced Error Recovery (AER) Interface */
278 static const struct pci_error_handlers liquidio_vf_err_handler = {
279 .error_detected = liquidio_pcie_error_detected,
282 static const struct pci_device_id liquidio_vf_pci_tbl[] = {
284 PCI_VENDOR_ID_CAVIUM, OCTEON_CN23XX_VF_VID,
285 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
291 MODULE_DEVICE_TABLE(pci, liquidio_vf_pci_tbl);
293 static struct pci_driver liquidio_vf_pci_driver = {
294 .name = "LiquidIO_VF",
295 .id_table = liquidio_vf_pci_tbl,
296 .probe = liquidio_vf_probe,
297 .remove = liquidio_vf_remove,
298 .err_handler = &liquidio_vf_err_handler, /* For AER */
302 * \brief Stop Tx queues
303 * @param netdev network device
305 static void txqs_stop(struct net_device *netdev)
307 if (netif_is_multiqueue(netdev)) {
310 for (i = 0; i < netdev->num_tx_queues; i++)
311 netif_stop_subqueue(netdev, i);
313 netif_stop_queue(netdev);
318 * \brief Start Tx queues
319 * @param netdev network device
321 static void txqs_start(struct net_device *netdev)
323 if (netif_is_multiqueue(netdev)) {
326 for (i = 0; i < netdev->num_tx_queues; i++)
327 netif_start_subqueue(netdev, i);
329 netif_start_queue(netdev);
334 * \brief Wake Tx queues
335 * @param netdev network device
337 static void txqs_wake(struct net_device *netdev)
339 struct lio *lio = GET_LIO(netdev);
341 if (netif_is_multiqueue(netdev)) {
344 for (i = 0; i < netdev->num_tx_queues; i++) {
345 int qno = lio->linfo.txpciq[i % lio->oct_dev->num_iqs]
347 if (__netif_subqueue_stopped(netdev, i)) {
348 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
350 netif_wake_subqueue(netdev, i);
354 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
356 netif_wake_queue(netdev);
361 * \brief Start Tx queue
362 * @param netdev network device
364 static void start_txq(struct net_device *netdev)
366 struct lio *lio = GET_LIO(netdev);
368 if (lio->linfo.link.s.link_up) {
375 * \brief Wake a queue
376 * @param netdev network device
377 * @param q which queue to wake
379 static void wake_q(struct net_device *netdev, int q)
381 if (netif_is_multiqueue(netdev))
382 netif_wake_subqueue(netdev, q);
384 netif_wake_queue(netdev);
388 * \brief Stop a queue
389 * @param netdev network device
390 * @param q which queue to stop
392 static void stop_q(struct net_device *netdev, int q)
394 if (netif_is_multiqueue(netdev))
395 netif_stop_subqueue(netdev, q);
397 netif_stop_queue(netdev);
401 * Remove the node at the head of the list. The list would be empty at
402 * the end of this call if there are no more nodes in the list.
404 static struct list_head *list_delete_head(struct list_head *root)
406 struct list_head *node;
408 if ((root->prev == root) && (root->next == root))
420 * \brief Delete gather lists
421 * @param lio per-network private data
423 static void delete_glists(struct lio *lio)
425 struct octnic_gather *g;
428 kfree(lio->glist_lock);
429 lio->glist_lock = NULL;
434 for (i = 0; i < lio->linfo.num_txpciq; i++) {
436 g = (struct octnic_gather *)
437 list_delete_head(&lio->glist[i]);
442 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
443 lio->glists_dma_base && lio->glists_dma_base[i]) {
444 lio_dma_free(lio->oct_dev,
445 lio->glist_entry_size * lio->tx_qsize,
446 lio->glists_virt_base[i],
447 lio->glists_dma_base[i]);
451 kfree(lio->glists_virt_base);
452 lio->glists_virt_base = NULL;
454 kfree(lio->glists_dma_base);
455 lio->glists_dma_base = NULL;
462 * \brief Setup gather lists
463 * @param lio per-network private data
465 static int setup_glists(struct lio *lio, int num_iqs)
467 struct octnic_gather *g;
471 kzalloc(sizeof(*lio->glist_lock) * num_iqs, GFP_KERNEL);
472 if (!lio->glist_lock)
476 kzalloc(sizeof(*lio->glist) * num_iqs, GFP_KERNEL);
478 kfree(lio->glist_lock);
479 lio->glist_lock = NULL;
483 lio->glist_entry_size =
484 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
486 /* allocate memory to store virtual and dma base address of
487 * per glist consistent memory
489 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
491 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
494 if (!lio->glists_virt_base || !lio->glists_dma_base) {
499 for (i = 0; i < num_iqs; i++) {
500 spin_lock_init(&lio->glist_lock[i]);
502 INIT_LIST_HEAD(&lio->glist[i]);
504 lio->glists_virt_base[i] =
505 lio_dma_alloc(lio->oct_dev,
506 lio->glist_entry_size * lio->tx_qsize,
507 &lio->glists_dma_base[i]);
509 if (!lio->glists_virt_base[i]) {
514 for (j = 0; j < lio->tx_qsize; j++) {
515 g = kzalloc(sizeof(*g), GFP_KERNEL);
519 g->sg = lio->glists_virt_base[i] +
520 (j * lio->glist_entry_size);
522 g->sg_dma_ptr = lio->glists_dma_base[i] +
523 (j * lio->glist_entry_size);
525 list_add_tail(&g->list, &lio->glist[i]);
528 if (j != lio->tx_qsize) {
538 * \brief Print link information
539 * @param netdev network device
541 static void print_link_info(struct net_device *netdev)
543 struct lio *lio = GET_LIO(netdev);
545 if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
546 ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
547 struct oct_link_info *linfo = &lio->linfo;
549 if (linfo->link.s.link_up) {
550 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
552 (linfo->link.s.duplex) ? "Full" : "Half");
554 netif_info(lio, link, lio->netdev, "Link Down\n");
560 * \brief Routine to notify MTU change
561 * @param work work_struct data structure
563 static void octnet_link_status_change(struct work_struct *work)
565 struct cavium_wk *wk = (struct cavium_wk *)work;
566 struct lio *lio = (struct lio *)wk->ctxptr;
569 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
574 * \brief Sets up the mtu status change work
575 * @param netdev network device
577 static int setup_link_status_change_wq(struct net_device *netdev)
579 struct lio *lio = GET_LIO(netdev);
580 struct octeon_device *oct = lio->oct_dev;
582 lio->link_status_wq.wq = alloc_workqueue("link-status",
584 if (!lio->link_status_wq.wq) {
585 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
588 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
589 octnet_link_status_change);
590 lio->link_status_wq.wk.ctxptr = lio;
595 static void cleanup_link_status_change_wq(struct net_device *netdev)
597 struct lio *lio = GET_LIO(netdev);
599 if (lio->link_status_wq.wq) {
600 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
601 destroy_workqueue(lio->link_status_wq.wq);
606 * \brief Update link status
607 * @param netdev network device
608 * @param ls link status structure
610 * Called on receipt of a link status response from the core application to
611 * update each interface's link status.
613 static void update_link_status(struct net_device *netdev,
614 union oct_link_status *ls)
616 struct lio *lio = GET_LIO(netdev);
617 struct octeon_device *oct = lio->oct_dev;
619 if ((lio->intf_open) && (lio->linfo.link.u64 != ls->u64)) {
620 lio->linfo.link.u64 = ls->u64;
622 print_link_info(netdev);
625 if (lio->linfo.link.s.link_up) {
626 netif_carrier_on(netdev);
629 netif_carrier_off(netdev);
633 if (lio->linfo.link.s.mtu != netdev->max_mtu) {
634 dev_info(&oct->pci_dev->dev, "Max MTU Changed from %d to %d\n",
635 netdev->max_mtu, lio->linfo.link.s.mtu);
636 netdev->max_mtu = lio->linfo.link.s.mtu;
639 if (lio->linfo.link.s.mtu < netdev->mtu) {
640 dev_warn(&oct->pci_dev->dev,
641 "PF has changed the MTU for gmx port. Reducing the mtu from %d to %d\n",
642 netdev->mtu, lio->linfo.link.s.mtu);
643 lio->mtu = lio->linfo.link.s.mtu;
644 netdev->mtu = lio->linfo.link.s.mtu;
645 queue_delayed_work(lio->link_status_wq.wq,
646 &lio->link_status_wq.wk.work, 0);
652 * \brief PCI probe handler
653 * @param pdev PCI device structure
657 liquidio_vf_probe(struct pci_dev *pdev,
658 const struct pci_device_id *ent __attribute__((unused)))
660 struct octeon_device *oct_dev = NULL;
662 oct_dev = octeon_allocate_device(pdev->device,
663 sizeof(struct octeon_device_priv));
666 dev_err(&pdev->dev, "Unable to allocate device\n");
669 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
671 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
672 (u32)pdev->vendor, (u32)pdev->device);
674 /* Assign octeon_device for this device to the private data area. */
675 pci_set_drvdata(pdev, oct_dev);
677 /* set linux specific device pointer */
678 oct_dev->pci_dev = pdev;
680 if (octeon_device_init(oct_dev)) {
681 liquidio_vf_remove(pdev);
685 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
691 * \brief PCI FLR for each Octeon device.
692 * @param oct octeon device
694 static void octeon_pci_flr(struct octeon_device *oct)
696 pci_save_state(oct->pci_dev);
698 pci_cfg_access_lock(oct->pci_dev);
700 /* Quiesce the device completely */
701 pci_write_config_word(oct->pci_dev, PCI_COMMAND,
702 PCI_COMMAND_INTX_DISABLE);
704 pcie_flr(oct->pci_dev);
706 pci_cfg_access_unlock(oct->pci_dev);
708 pci_restore_state(oct->pci_dev);
712 *\brief Destroy resources associated with octeon device
713 * @param pdev PCI device structure
716 static void octeon_destroy_resources(struct octeon_device *oct)
718 struct msix_entry *msix_entries;
721 switch (atomic_read(&oct->status)) {
722 case OCT_DEV_RUNNING:
723 case OCT_DEV_CORE_OK:
724 /* No more instructions will be forwarded. */
725 atomic_set(&oct->status, OCT_DEV_IN_RESET);
727 oct->app_mode = CVM_DRV_INVALID_APP;
728 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
729 lio_get_state_string(&oct->status));
731 schedule_timeout_uninterruptible(HZ / 10);
734 case OCT_DEV_HOST_OK:
736 case OCT_DEV_IO_QUEUES_DONE:
737 if (wait_for_pending_requests(oct))
738 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
740 if (lio_wait_for_instr_fetch(oct))
741 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
743 /* Disable the input and output queues now. No more packets will
744 * arrive from Octeon, but we should wait for all packet
745 * processing to finish.
747 oct->fn_list.disable_io_queues(oct);
749 if (lio_wait_for_oq_pkts(oct))
750 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
752 case OCT_DEV_INTR_SET_DONE:
753 /* Disable interrupts */
754 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
757 msix_entries = (struct msix_entry *)oct->msix_entries;
758 for (i = 0; i < oct->num_msix_irqs; i++) {
759 if (oct->ioq_vector[i].vector) {
760 irq_set_affinity_hint(
761 msix_entries[i].vector,
763 free_irq(msix_entries[i].vector,
764 &oct->ioq_vector[i]);
765 oct->ioq_vector[i].vector = 0;
768 pci_disable_msix(oct->pci_dev);
769 kfree(oct->msix_entries);
770 oct->msix_entries = NULL;
771 kfree(oct->irq_name_storage);
772 oct->irq_name_storage = NULL;
774 /* Soft reset the octeon device before exiting */
775 if (oct->pci_dev->reset_fn)
778 cn23xx_vf_ask_pf_to_do_flr(oct);
781 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
782 octeon_free_ioq_vector(oct);
785 case OCT_DEV_MBOX_SETUP_DONE:
786 oct->fn_list.free_mbox(oct);
789 case OCT_DEV_IN_RESET:
790 case OCT_DEV_DROQ_INIT_DONE:
792 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
793 if (!(oct->io_qmask.oq & BIT_ULL(i)))
795 octeon_delete_droq(oct, i);
799 case OCT_DEV_RESP_LIST_INIT_DONE:
800 octeon_delete_response_list(oct);
803 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
804 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
805 if (!(oct->io_qmask.iq & BIT_ULL(i)))
807 octeon_delete_instr_queue(oct, i);
811 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
812 octeon_free_sc_buffer_pool(oct);
815 case OCT_DEV_DISPATCH_INIT_DONE:
816 octeon_delete_dispatch_list(oct);
817 cancel_delayed_work_sync(&oct->nic_poll_work.work);
820 case OCT_DEV_PCI_MAP_DONE:
821 octeon_unmap_pci_barx(oct, 0);
822 octeon_unmap_pci_barx(oct, 1);
825 case OCT_DEV_PCI_ENABLE_DONE:
826 pci_clear_master(oct->pci_dev);
827 /* Disable the device, releasing the PCI INT */
828 pci_disable_device(oct->pci_dev);
831 case OCT_DEV_BEGIN_STATE:
832 /* Nothing to be done here either */
838 * \brief Callback for rx ctrl
839 * @param status status of request
840 * @param buf pointer to resp structure
842 static void rx_ctl_callback(struct octeon_device *oct,
843 u32 status, void *buf)
845 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
846 struct liquidio_rx_ctl_context *ctx;
848 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
850 oct = lio_get_device(ctx->octeon_id);
852 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
854 WRITE_ONCE(ctx->cond, 1);
856 /* This barrier is required to be sure that the response has been
857 * written fully before waking up the handler
861 wake_up_interruptible(&ctx->wc);
865 * \brief Send Rx control command
866 * @param lio per-network private data
867 * @param start_stop whether to start or stop
869 static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
871 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
872 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
873 struct liquidio_rx_ctl_context *ctx;
874 struct octeon_soft_command *sc;
875 union octnet_cmd *ncmd;
878 if (oct->props[lio->ifidx].rx_on == start_stop)
881 sc = (struct octeon_soft_command *)
882 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
885 ncmd = (union octnet_cmd *)sc->virtdptr;
886 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
888 WRITE_ONCE(ctx->cond, 0);
889 ctx->octeon_id = lio_get_device_id(oct);
890 init_waitqueue_head(&ctx->wc);
893 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
894 ncmd->s.param1 = start_stop;
896 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
898 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
900 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
901 OPCODE_NIC_CMD, 0, 0, 0);
903 sc->callback = rx_ctl_callback;
904 sc->callback_arg = sc;
905 sc->wait_time = 5000;
907 retval = octeon_send_soft_command(oct, sc);
908 if (retval == IQ_SEND_FAILED) {
909 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
911 /* Sleep on a wait queue till the cond flag indicates that the
912 * response arrived or timed-out.
914 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
916 oct->props[lio->ifidx].rx_on = start_stop;
919 octeon_free_soft_command(oct, sc);
923 * \brief Destroy NIC device interface
924 * @param oct octeon device
925 * @param ifidx which interface to destroy
927 * Cleanup associated with each interface for an Octeon device when NIC
928 * module is being unloaded or if initialization fails during load.
930 static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
932 struct net_device *netdev = oct->props[ifidx].netdev;
933 struct napi_struct *napi, *n;
937 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
942 lio = GET_LIO(netdev);
944 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
946 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
947 liquidio_stop(netdev);
949 if (oct->props[lio->ifidx].napi_enabled == 1) {
950 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
953 oct->props[lio->ifidx].napi_enabled = 0;
955 oct->droq[0]->ops.poll_mode = 0;
959 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
960 netif_napi_del(napi);
962 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
963 unregister_netdev(netdev);
965 cleanup_rx_oom_poll_fn(netdev);
967 cleanup_link_status_change_wq(netdev);
973 oct->props[ifidx].gmxport = -1;
975 oct->props[ifidx].netdev = NULL;
979 * \brief Stop complete NIC functionality
980 * @param oct octeon device
982 static int liquidio_stop_nic_module(struct octeon_device *oct)
987 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
989 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
993 spin_lock_bh(&oct->cmd_resp_wqlock);
994 oct->cmd_resp_state = OCT_DRV_OFFLINE;
995 spin_unlock_bh(&oct->cmd_resp_wqlock);
997 for (i = 0; i < oct->ifcount; i++) {
998 lio = GET_LIO(oct->props[i].netdev);
999 for (j = 0; j < oct->num_oqs; j++)
1000 octeon_unregister_droq_ops(oct,
1001 lio->linfo.rxpciq[j].s.q_no);
1004 for (i = 0; i < oct->ifcount; i++)
1005 liquidio_destroy_nic_device(oct, i);
1007 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1012 * \brief Cleans up resources at unload time
1013 * @param pdev PCI device structure
1015 static void liquidio_vf_remove(struct pci_dev *pdev)
1017 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1019 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1021 if (oct_dev->app_mode == CVM_DRV_NIC_APP)
1022 liquidio_stop_nic_module(oct_dev);
1024 /* Reset the octeon device and cleanup all memory allocated for
1025 * the octeon device by driver.
1027 octeon_destroy_resources(oct_dev);
1029 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1031 /* This octeon device has been removed. Update the global
1032 * data structure to reflect this. Free the device structure.
1034 octeon_free_device_mem(oct_dev);
1038 * \brief PCI initialization for each Octeon device.
1039 * @param oct octeon device
1041 static int octeon_pci_os_setup(struct octeon_device *oct)
1043 #ifdef CONFIG_PCI_IOV
1044 /* setup PCI stuff first */
1045 if (!oct->pci_dev->physfn)
1046 octeon_pci_flr(oct);
1049 if (pci_enable_device(oct->pci_dev)) {
1050 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1054 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1055 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1056 pci_disable_device(oct->pci_dev);
1060 /* Enable PCI DMA Master. */
1061 pci_set_master(oct->pci_dev);
1066 static int skb_iq(struct lio *lio, struct sk_buff *skb)
1070 if (netif_is_multiqueue(lio->netdev))
1071 q = skb->queue_mapping % lio->linfo.num_txpciq;
1077 * \brief Check Tx queue state for a given network buffer
1078 * @param lio per-network private data
1079 * @param skb network buffer
1081 static int check_txq_state(struct lio *lio, struct sk_buff *skb)
1085 if (netif_is_multiqueue(lio->netdev)) {
1086 q = skb->queue_mapping;
1087 iq = lio->linfo.txpciq[q % lio->oct_dev->num_iqs].s.q_no;
1093 if (octnet_iq_is_full(lio->oct_dev, iq))
1096 if (__netif_subqueue_stopped(lio->netdev, q)) {
1097 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
1098 wake_q(lio->netdev, q);
1105 * \brief Unmap and free network buffer
1108 static void free_netbuf(void *buf)
1110 struct octnet_buf_free_info *finfo;
1111 struct sk_buff *skb;
1114 finfo = (struct octnet_buf_free_info *)buf;
1118 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1121 check_txq_state(lio, skb);
1123 tx_buffer_free(skb);
1127 * \brief Unmap and free gather buffer
1130 static void free_netsgbuf(void *buf)
1132 struct octnet_buf_free_info *finfo;
1133 struct octnic_gather *g;
1134 struct sk_buff *skb;
1138 finfo = (struct octnet_buf_free_info *)buf;
1142 frags = skb_shinfo(skb)->nr_frags;
1144 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1145 g->sg[0].ptr[0], (skb->len - skb->data_len),
1150 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1152 pci_unmap_page((lio->oct_dev)->pci_dev,
1153 g->sg[(i >> 2)].ptr[(i & 3)],
1154 frag->size, DMA_TO_DEVICE);
1158 iq = skb_iq(lio, skb);
1160 spin_lock(&lio->glist_lock[iq]);
1161 list_add_tail(&g->list, &lio->glist[iq]);
1162 spin_unlock(&lio->glist_lock[iq]);
1164 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1166 tx_buffer_free(skb);
1170 * \brief Unmap and free gather buffer with response
1173 static void free_netsgbuf_with_resp(void *buf)
1175 struct octnet_buf_free_info *finfo;
1176 struct octeon_soft_command *sc;
1177 struct octnic_gather *g;
1178 struct sk_buff *skb;
1182 sc = (struct octeon_soft_command *)buf;
1183 skb = (struct sk_buff *)sc->callback_arg;
1184 finfo = (struct octnet_buf_free_info *)&skb->cb;
1188 frags = skb_shinfo(skb)->nr_frags;
1190 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1191 g->sg[0].ptr[0], (skb->len - skb->data_len),
1196 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1198 pci_unmap_page((lio->oct_dev)->pci_dev,
1199 g->sg[(i >> 2)].ptr[(i & 3)],
1200 frag->size, DMA_TO_DEVICE);
1204 iq = skb_iq(lio, skb);
1206 spin_lock(&lio->glist_lock[iq]);
1207 list_add_tail(&g->list, &lio->glist[iq]);
1208 spin_unlock(&lio->glist_lock[iq]);
1210 /* Don't free the skb yet */
1212 check_txq_state(lio, skb);
1216 * \brief Callback for getting interface configuration
1217 * @param status status of request
1218 * @param buf pointer to resp structure
1220 static void if_cfg_callback(struct octeon_device *oct,
1221 u32 status __attribute__((unused)), void *buf)
1223 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1224 struct liquidio_if_cfg_context *ctx;
1225 struct liquidio_if_cfg_resp *resp;
1227 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1228 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1230 oct = lio_get_device(ctx->octeon_id);
1232 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1233 CVM_CAST64(resp->status));
1234 WRITE_ONCE(ctx->cond, 1);
1236 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
1237 resp->cfg_info.liquidio_firmware_version);
1239 /* This barrier is required to be sure that the response has been
1240 * written fully before waking up the handler
1244 wake_up_interruptible(&ctx->wc);
1248 * \brief Net device open for LiquidIO
1249 * @param netdev network device
1251 static int liquidio_open(struct net_device *netdev)
1253 struct lio *lio = GET_LIO(netdev);
1254 struct octeon_device *oct = lio->oct_dev;
1255 struct napi_struct *napi, *n;
1257 if (!oct->props[lio->ifidx].napi_enabled) {
1258 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1261 oct->props[lio->ifidx].napi_enabled = 1;
1263 oct->droq[0]->ops.poll_mode = 1;
1266 ifstate_set(lio, LIO_IFSTATE_RUNNING);
1268 /* Ready for link status updates */
1271 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
1274 /* tell Octeon to start forwarding packets to host */
1275 send_rx_ctrl_cmd(lio, 1);
1277 dev_info(&oct->pci_dev->dev, "%s interface is opened\n", netdev->name);
1283 * \brief Net device stop for LiquidIO
1284 * @param netdev network device
1286 static int liquidio_stop(struct net_device *netdev)
1288 struct lio *lio = GET_LIO(netdev);
1289 struct octeon_device *oct = lio->oct_dev;
1290 struct napi_struct *napi, *n;
1292 /* tell Octeon to stop forwarding packets to host */
1293 send_rx_ctrl_cmd(lio, 0);
1295 if (oct->props[lio->ifidx].napi_enabled) {
1296 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1299 oct->props[lio->ifidx].napi_enabled = 0;
1301 oct->droq[0]->ops.poll_mode = 0;
1304 netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
1305 /* Inform that netif carrier is down */
1307 lio->linfo.link.s.link_up = 0;
1309 netif_carrier_off(netdev);
1310 lio->link_changes++;
1312 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
1316 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
1322 * \brief Converts a mask based on net device flags
1323 * @param netdev network device
1325 * This routine generates a octnet_ifflags mask from the net device flags
1326 * received from the OS.
1328 static enum octnet_ifflags get_new_flags(struct net_device *netdev)
1330 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
1332 if (netdev->flags & IFF_PROMISC)
1333 f |= OCTNET_IFFLAG_PROMISC;
1335 if (netdev->flags & IFF_ALLMULTI)
1336 f |= OCTNET_IFFLAG_ALLMULTI;
1338 if (netdev->flags & IFF_MULTICAST) {
1339 f |= OCTNET_IFFLAG_MULTICAST;
1341 /* Accept all multicast addresses if there are more than we
1344 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
1345 f |= OCTNET_IFFLAG_ALLMULTI;
1348 if (netdev->flags & IFF_BROADCAST)
1349 f |= OCTNET_IFFLAG_BROADCAST;
1354 static void liquidio_set_uc_list(struct net_device *netdev)
1356 struct lio *lio = GET_LIO(netdev);
1357 struct octeon_device *oct = lio->oct_dev;
1358 struct octnic_ctrl_pkt nctrl;
1359 struct netdev_hw_addr *ha;
1362 if (lio->netdev_uc_count == netdev_uc_count(netdev))
1365 if (netdev_uc_count(netdev) > MAX_NCTRL_UDD) {
1366 dev_err(&oct->pci_dev->dev, "too many MAC addresses in netdev uc list\n");
1370 lio->netdev_uc_count = netdev_uc_count(netdev);
1372 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1373 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_UC_LIST;
1374 nctrl.ncmd.s.more = lio->netdev_uc_count;
1375 nctrl.ncmd.s.param1 = oct->vf_num;
1376 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1377 nctrl.netpndev = (u64)netdev;
1378 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
1380 /* copy all the addresses into the udd */
1381 mac = &nctrl.udd[0];
1382 netdev_for_each_uc_addr(ha, netdev) {
1383 ether_addr_copy(((u8 *)mac) + 2, ha->addr);
1387 octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
1391 * \brief Net device set_multicast_list
1392 * @param netdev network device
1394 static void liquidio_set_mcast_list(struct net_device *netdev)
1396 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
1397 struct lio *lio = GET_LIO(netdev);
1398 struct octeon_device *oct = lio->oct_dev;
1399 struct octnic_ctrl_pkt nctrl;
1400 struct netdev_hw_addr *ha;
1404 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1406 /* Create a ctrl pkt command to be sent to core app. */
1408 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
1409 nctrl.ncmd.s.param1 = get_new_flags(netdev);
1410 nctrl.ncmd.s.param2 = mc_count;
1411 nctrl.ncmd.s.more = mc_count;
1412 nctrl.netpndev = (u64)netdev;
1413 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
1415 /* copy all the addresses into the udd */
1417 netdev_for_each_mc_addr(ha, netdev) {
1419 ether_addr_copy(((u8 *)mc) + 2, ha->addr);
1420 /* no need to swap bytes */
1421 if (++mc > &nctrl.udd[mc_count])
1425 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1427 /* Apparently, any activity in this call from the kernel has to
1428 * be atomic. So we won't wait for response.
1430 nctrl.wait_time = 0;
1432 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
1434 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
1438 liquidio_set_uc_list(netdev);
1442 * \brief Net device set_mac_address
1443 * @param netdev network device
1445 static int liquidio_set_mac(struct net_device *netdev, void *p)
1447 struct sockaddr *addr = (struct sockaddr *)p;
1448 struct lio *lio = GET_LIO(netdev);
1449 struct octeon_device *oct = lio->oct_dev;
1450 struct octnic_ctrl_pkt nctrl;
1453 if (!is_valid_ether_addr(addr->sa_data))
1454 return -EADDRNOTAVAIL;
1456 if (ether_addr_equal(addr->sa_data, netdev->dev_addr))
1459 if (lio->linfo.macaddr_is_admin_asgnd)
1462 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1465 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
1466 nctrl.ncmd.s.param1 = 0;
1467 nctrl.ncmd.s.more = 1;
1468 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1469 nctrl.netpndev = (u64)netdev;
1470 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
1471 nctrl.wait_time = 100;
1474 /* The MAC Address is presented in network byte order. */
1475 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, addr->sa_data);
1477 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
1479 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
1482 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1483 ether_addr_copy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data);
1489 * \brief Net device get_stats
1490 * @param netdev network device
1492 static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
1494 struct lio *lio = GET_LIO(netdev);
1495 struct net_device_stats *stats = &netdev->stats;
1496 u64 pkts = 0, drop = 0, bytes = 0;
1497 struct oct_droq_stats *oq_stats;
1498 struct oct_iq_stats *iq_stats;
1499 struct octeon_device *oct;
1500 int i, iq_no, oq_no;
1504 if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
1507 for (i = 0; i < oct->num_iqs; i++) {
1508 iq_no = lio->linfo.txpciq[i].s.q_no;
1509 iq_stats = &oct->instr_queue[iq_no]->stats;
1510 pkts += iq_stats->tx_done;
1511 drop += iq_stats->tx_dropped;
1512 bytes += iq_stats->tx_tot_bytes;
1515 stats->tx_packets = pkts;
1516 stats->tx_bytes = bytes;
1517 stats->tx_dropped = drop;
1523 for (i = 0; i < oct->num_oqs; i++) {
1524 oq_no = lio->linfo.rxpciq[i].s.q_no;
1525 oq_stats = &oct->droq[oq_no]->stats;
1526 pkts += oq_stats->rx_pkts_received;
1527 drop += (oq_stats->rx_dropped +
1528 oq_stats->dropped_nodispatch +
1529 oq_stats->dropped_toomany +
1530 oq_stats->dropped_nomem);
1531 bytes += oq_stats->rx_bytes_received;
1534 stats->rx_bytes = bytes;
1535 stats->rx_packets = pkts;
1536 stats->rx_dropped = drop;
1542 * \brief Net device change_mtu
1543 * @param netdev network device
1545 static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
1547 struct octnic_ctrl_pkt nctrl;
1548 struct octeon_device *oct;
1552 lio = GET_LIO(netdev);
1555 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1558 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
1559 nctrl.ncmd.s.param1 = new_mtu;
1560 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1561 nctrl.wait_time = LIO_CMD_WAIT_TM;
1562 nctrl.netpndev = (u64)netdev;
1563 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
1565 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
1567 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
1577 * \brief Handler for SIOCSHWTSTAMP ioctl
1578 * @param netdev network device
1579 * @param ifr interface request
1580 * @param cmd command
1582 static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
1584 struct lio *lio = GET_LIO(netdev);
1585 struct hwtstamp_config conf;
1587 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
1593 switch (conf.tx_type) {
1594 case HWTSTAMP_TX_ON:
1595 case HWTSTAMP_TX_OFF:
1601 switch (conf.rx_filter) {
1602 case HWTSTAMP_FILTER_NONE:
1604 case HWTSTAMP_FILTER_ALL:
1605 case HWTSTAMP_FILTER_SOME:
1606 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1607 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1608 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1609 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1610 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1611 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1612 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1613 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1614 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1615 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1616 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1617 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1618 case HWTSTAMP_FILTER_NTP_ALL:
1619 conf.rx_filter = HWTSTAMP_FILTER_ALL;
1625 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
1626 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
1629 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
1631 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
1635 * \brief ioctl handler
1636 * @param netdev network device
1637 * @param ifr interface request
1638 * @param cmd command
1640 static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1644 return hwtstamp_ioctl(netdev, ifr);
1650 static void handle_timestamp(struct octeon_device *oct, u32 status, void *buf)
1652 struct sk_buff *skb = (struct sk_buff *)buf;
1653 struct octnet_buf_free_info *finfo;
1654 struct oct_timestamp_resp *resp;
1655 struct octeon_soft_command *sc;
1658 finfo = (struct octnet_buf_free_info *)skb->cb;
1662 resp = (struct oct_timestamp_resp *)sc->virtrptr;
1664 if (status != OCTEON_REQUEST_DONE) {
1665 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
1666 CVM_CAST64(status));
1667 resp->timestamp = 0;
1670 octeon_swap_8B_data(&resp->timestamp, 1);
1672 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
1673 struct skb_shared_hwtstamps ts;
1674 u64 ns = resp->timestamp;
1676 netif_info(lio, tx_done, lio->netdev,
1677 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
1678 skb, (unsigned long long)ns);
1679 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
1680 skb_tstamp_tx(skb, &ts);
1683 octeon_free_soft_command(oct, sc);
1684 tx_buffer_free(skb);
1687 /* \brief Send a data packet that will be timestamped
1688 * @param oct octeon device
1689 * @param ndata pointer to network data
1690 * @param finfo pointer to private network data
1692 static int send_nic_timestamp_pkt(struct octeon_device *oct,
1693 struct octnic_data_pkt *ndata,
1694 struct octnet_buf_free_info *finfo)
1696 struct octeon_soft_command *sc;
1704 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
1705 sizeof(struct oct_timestamp_resp));
1709 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
1710 return IQ_SEND_FAILED;
1713 if (ndata->reqtype == REQTYPE_NORESP_NET)
1714 ndata->reqtype = REQTYPE_RESP_NET;
1715 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
1716 ndata->reqtype = REQTYPE_RESP_NET_SG;
1718 sc->callback = handle_timestamp;
1719 sc->callback_arg = finfo->skb;
1720 sc->iq_no = ndata->q_no;
1722 len = (u32)((struct octeon_instr_ih3 *)(&sc->cmd.cmd3.ih3))->dlengsz;
1726 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
1727 sc, len, ndata->reqtype);
1729 if (retval == IQ_SEND_FAILED) {
1730 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
1732 octeon_free_soft_command(oct, sc);
1734 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
1740 /** \brief Transmit networks packets to the Octeon interface
1741 * @param skbuff skbuff struct to be passed to network layer.
1742 * @param netdev pointer to network device
1743 * @returns whether the packet was transmitted to the device okay or not
1744 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
1746 static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
1748 struct octnet_buf_free_info *finfo;
1749 union octnic_cmd_setup cmdsetup;
1750 struct octnic_data_pkt ndata;
1751 struct octeon_instr_irh *irh;
1752 struct oct_iq_stats *stats;
1753 struct octeon_device *oct;
1754 int q_idx = 0, iq_no = 0;
1755 union tx_info *tx_info;
1762 lio = GET_LIO(netdev);
1765 if (netif_is_multiqueue(netdev)) {
1766 q_idx = skb->queue_mapping;
1767 q_idx = (q_idx % (lio->linfo.num_txpciq));
1769 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
1774 stats = &oct->instr_queue[iq_no]->stats;
1776 /* Check for all conditions in which the current packet cannot be
1779 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
1780 (!lio->linfo.link.s.link_up) || (skb->len <= 0)) {
1781 netif_info(lio, tx_err, lio->netdev, "Transmit failed link_status : %d\n",
1782 lio->linfo.link.s.link_up);
1783 goto lio_xmit_failed;
1786 /* Use space in skb->cb to store info used to unmap and
1789 finfo = (struct octnet_buf_free_info *)skb->cb;
1794 /* Prepare the attributes for the data to be passed to OSI. */
1795 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
1801 if (netif_is_multiqueue(netdev)) {
1802 if (octnet_iq_is_full(oct, ndata.q_no)) {
1803 /* defer sending if queue is full */
1804 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
1806 stats->tx_iq_busy++;
1807 return NETDEV_TX_BUSY;
1810 if (octnet_iq_is_full(oct, lio->txq)) {
1811 /* defer sending if queue is full */
1812 stats->tx_iq_busy++;
1813 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
1815 return NETDEV_TX_BUSY;
1819 ndata.datasize = skb->len;
1822 cmdsetup.s.iq_no = iq_no;
1824 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1825 if (skb->encapsulation) {
1826 cmdsetup.s.tnl_csum = 1;
1829 cmdsetup.s.transport_csum = 1;
1832 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
1833 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1834 cmdsetup.s.timestamp = 1;
1837 if (!skb_shinfo(skb)->nr_frags) {
1838 cmdsetup.s.u.datasize = skb->len;
1839 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
1840 /* Offload checksum calculation for TCP/UDP packets */
1841 dptr = dma_map_single(&oct->pci_dev->dev,
1845 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
1846 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
1848 return NETDEV_TX_BUSY;
1851 ndata.cmd.cmd3.dptr = dptr;
1853 ndata.reqtype = REQTYPE_NORESP_NET;
1856 struct skb_frag_struct *frag;
1857 struct octnic_gather *g;
1860 spin_lock(&lio->glist_lock[q_idx]);
1861 g = (struct octnic_gather *)list_delete_head(
1862 &lio->glist[q_idx]);
1863 spin_unlock(&lio->glist_lock[q_idx]);
1866 netif_info(lio, tx_err, lio->netdev,
1867 "Transmit scatter gather: glist null!\n");
1868 goto lio_xmit_failed;
1871 cmdsetup.s.gather = 1;
1872 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
1873 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
1875 memset(g->sg, 0, g->sg_size);
1877 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
1879 (skb->len - skb->data_len),
1881 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
1882 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
1884 return NETDEV_TX_BUSY;
1886 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
1888 frags = skb_shinfo(skb)->nr_frags;
1891 frag = &skb_shinfo(skb)->frags[i - 1];
1893 g->sg[(i >> 2)].ptr[(i & 3)] =
1894 dma_map_page(&oct->pci_dev->dev,
1899 if (dma_mapping_error(&oct->pci_dev->dev,
1900 g->sg[i >> 2].ptr[i & 3])) {
1901 dma_unmap_single(&oct->pci_dev->dev,
1903 skb->len - skb->data_len,
1905 for (j = 1; j < i; j++) {
1906 frag = &skb_shinfo(skb)->frags[j - 1];
1907 dma_unmap_page(&oct->pci_dev->dev,
1908 g->sg[j >> 2].ptr[j & 3],
1912 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
1914 return NETDEV_TX_BUSY;
1917 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
1921 dptr = g->sg_dma_ptr;
1923 ndata.cmd.cmd3.dptr = dptr;
1927 ndata.reqtype = REQTYPE_NORESP_NET_SG;
1930 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
1931 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
1933 if (skb_shinfo(skb)->gso_size) {
1934 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
1935 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
1938 /* HW insert VLAN tag */
1939 if (skb_vlan_tag_present(skb)) {
1940 irh->priority = skb_vlan_tag_get(skb) >> VLAN_PRIO_SHIFT;
1941 irh->vlan = skb_vlan_tag_get(skb) & VLAN_VID_MASK;
1944 if (unlikely(cmdsetup.s.timestamp))
1945 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
1947 status = octnet_send_nic_data_pkt(oct, &ndata);
1948 if (status == IQ_SEND_FAILED)
1949 goto lio_xmit_failed;
1951 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
1953 if (status == IQ_SEND_STOP) {
1954 dev_err(&oct->pci_dev->dev, "Rcvd IQ_SEND_STOP signal; stopping IQ-%d\n",
1956 stop_q(lio->netdev, q_idx);
1959 netif_trans_update(netdev);
1961 if (tx_info->s.gso_segs)
1962 stats->tx_done += tx_info->s.gso_segs;
1965 stats->tx_tot_bytes += ndata.datasize;
1967 return NETDEV_TX_OK;
1970 stats->tx_dropped++;
1971 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
1972 iq_no, stats->tx_dropped);
1974 dma_unmap_single(&oct->pci_dev->dev, dptr,
1975 ndata.datasize, DMA_TO_DEVICE);
1976 tx_buffer_free(skb);
1977 return NETDEV_TX_OK;
1980 /** \brief Network device Tx timeout
1981 * @param netdev pointer to network device
1983 static void liquidio_tx_timeout(struct net_device *netdev)
1987 lio = GET_LIO(netdev);
1989 netif_info(lio, tx_err, lio->netdev,
1990 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
1991 netdev->stats.tx_dropped);
1992 netif_trans_update(netdev);
1997 liquidio_vlan_rx_add_vid(struct net_device *netdev,
1998 __be16 proto __attribute__((unused)), u16 vid)
2000 struct lio *lio = GET_LIO(netdev);
2001 struct octeon_device *oct = lio->oct_dev;
2002 struct octnic_ctrl_pkt nctrl;
2003 struct completion compl;
2007 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2010 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
2011 nctrl.ncmd.s.param1 = vid;
2012 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2013 nctrl.wait_time = 100;
2014 nctrl.netpndev = (u64)netdev;
2015 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2016 init_completion(&compl);
2017 nctrl.completion = &compl;
2018 nctrl.response_code = &response_code;
2020 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2022 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2027 if (!wait_for_completion_timeout(&compl,
2028 msecs_to_jiffies(nctrl.wait_time)))
2031 if (READ_ONCE(response_code))
2038 liquidio_vlan_rx_kill_vid(struct net_device *netdev,
2039 __be16 proto __attribute__((unused)), u16 vid)
2041 struct lio *lio = GET_LIO(netdev);
2042 struct octeon_device *oct = lio->oct_dev;
2043 struct octnic_ctrl_pkt nctrl;
2046 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2049 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
2050 nctrl.ncmd.s.param1 = vid;
2051 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2052 nctrl.wait_time = 100;
2053 nctrl.netpndev = (u64)netdev;
2054 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2056 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2058 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2064 /** Sending command to enable/disable RX checksum offload
2065 * @param netdev pointer to network device
2066 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
2067 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
2068 * OCTNET_CMD_RXCSUM_DISABLE
2069 * @returns SUCCESS or FAILURE
2071 static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
2074 struct lio *lio = GET_LIO(netdev);
2075 struct octeon_device *oct = lio->oct_dev;
2076 struct octnic_ctrl_pkt nctrl;
2079 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2082 nctrl.ncmd.s.cmd = command;
2083 nctrl.ncmd.s.param1 = rx_cmd;
2084 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2085 nctrl.wait_time = 100;
2086 nctrl.netpndev = (u64)netdev;
2087 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2089 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2091 dev_err(&oct->pci_dev->dev, "DEVFLAGS RXCSUM change failed in core (ret:0x%x)\n",
2097 /** Sending command to add/delete VxLAN UDP port to firmware
2098 * @param netdev pointer to network device
2099 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
2100 * @param vxlan_port VxLAN port to be added or deleted
2101 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
2102 * OCTNET_CMD_VXLAN_PORT_DEL
2103 * @returns SUCCESS or FAILURE
2105 static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
2106 u16 vxlan_port, u8 vxlan_cmd_bit)
2108 struct lio *lio = GET_LIO(netdev);
2109 struct octeon_device *oct = lio->oct_dev;
2110 struct octnic_ctrl_pkt nctrl;
2113 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2116 nctrl.ncmd.s.cmd = command;
2117 nctrl.ncmd.s.more = vxlan_cmd_bit;
2118 nctrl.ncmd.s.param1 = vxlan_port;
2119 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2120 nctrl.wait_time = 100;
2121 nctrl.netpndev = (u64)netdev;
2122 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2124 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2126 dev_err(&oct->pci_dev->dev,
2127 "DEVFLAGS VxLAN port add/delete failed in core (ret : 0x%x)\n",
2133 /** \brief Net device fix features
2134 * @param netdev pointer to network device
2135 * @param request features requested
2136 * @returns updated features list
2138 static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2139 netdev_features_t request)
2141 struct lio *lio = netdev_priv(netdev);
2143 if ((request & NETIF_F_RXCSUM) &&
2144 !(lio->dev_capability & NETIF_F_RXCSUM))
2145 request &= ~NETIF_F_RXCSUM;
2147 if ((request & NETIF_F_HW_CSUM) &&
2148 !(lio->dev_capability & NETIF_F_HW_CSUM))
2149 request &= ~NETIF_F_HW_CSUM;
2151 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
2152 request &= ~NETIF_F_TSO;
2154 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
2155 request &= ~NETIF_F_TSO6;
2157 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
2158 request &= ~NETIF_F_LRO;
2160 /* Disable LRO if RXCSUM is off */
2161 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
2162 (lio->dev_capability & NETIF_F_LRO))
2163 request &= ~NETIF_F_LRO;
2168 /** \brief Net device set features
2169 * @param netdev pointer to network device
2170 * @param features features to enable/disable
2172 static int liquidio_set_features(struct net_device *netdev,
2173 netdev_features_t features)
2175 struct lio *lio = netdev_priv(netdev);
2177 if (!((netdev->features ^ features) & NETIF_F_LRO))
2180 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
2181 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
2182 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
2183 else if (!(features & NETIF_F_LRO) &&
2184 (lio->dev_capability & NETIF_F_LRO))
2185 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
2186 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
2187 if (!(netdev->features & NETIF_F_RXCSUM) &&
2188 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2189 (features & NETIF_F_RXCSUM))
2190 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
2191 OCTNET_CMD_RXCSUM_ENABLE);
2192 else if ((netdev->features & NETIF_F_RXCSUM) &&
2193 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2194 !(features & NETIF_F_RXCSUM))
2195 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
2196 OCTNET_CMD_RXCSUM_DISABLE);
2201 static void liquidio_add_vxlan_port(struct net_device *netdev,
2202 struct udp_tunnel_info *ti)
2204 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2207 liquidio_vxlan_port_command(netdev,
2208 OCTNET_CMD_VXLAN_PORT_CONFIG,
2210 OCTNET_CMD_VXLAN_PORT_ADD);
2213 static void liquidio_del_vxlan_port(struct net_device *netdev,
2214 struct udp_tunnel_info *ti)
2216 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2219 liquidio_vxlan_port_command(netdev,
2220 OCTNET_CMD_VXLAN_PORT_CONFIG,
2222 OCTNET_CMD_VXLAN_PORT_DEL);
2225 static const struct net_device_ops lionetdevops = {
2226 .ndo_open = liquidio_open,
2227 .ndo_stop = liquidio_stop,
2228 .ndo_start_xmit = liquidio_xmit,
2229 .ndo_get_stats = liquidio_get_stats,
2230 .ndo_set_mac_address = liquidio_set_mac,
2231 .ndo_set_rx_mode = liquidio_set_mcast_list,
2232 .ndo_tx_timeout = liquidio_tx_timeout,
2233 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
2234 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
2235 .ndo_change_mtu = liquidio_change_mtu,
2236 .ndo_do_ioctl = liquidio_ioctl,
2237 .ndo_fix_features = liquidio_fix_features,
2238 .ndo_set_features = liquidio_set_features,
2239 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
2240 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
2243 static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
2245 struct octeon_device *oct = (struct octeon_device *)buf;
2246 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
2247 union oct_link_status *ls;
2251 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
2252 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
2253 recv_pkt->buffer_size[0],
2254 recv_pkt->rh.r_nic_info.gmxport);
2258 gmxport = recv_pkt->rh.r_nic_info.gmxport;
2259 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
2260 OCT_DROQ_INFO_SIZE);
2262 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
2264 for (i = 0; i < oct->ifcount; i++) {
2265 if (oct->props[i].gmxport == gmxport) {
2266 update_link_status(oct->props[i].netdev, ls);
2272 for (i = 0; i < recv_pkt->buffer_count; i++)
2273 recv_buffer_free(recv_pkt->buffer_ptr[i]);
2274 octeon_free_recv_info(recv_info);
2279 * \brief Setup network interfaces
2280 * @param octeon_dev octeon device
2282 * Called during init time for each device. It assumes the NIC
2283 * is already up and running. The link information for each
2284 * interface is passed in link_info.
2286 static int setup_nic_devices(struct octeon_device *octeon_dev)
2288 int retval, num_iqueues, num_oqueues;
2289 struct liquidio_if_cfg_context *ctx;
2290 u32 resp_size, ctx_size, data_size;
2291 struct liquidio_if_cfg_resp *resp;
2292 struct octeon_soft_command *sc;
2293 union oct_nic_if_cfg if_cfg;
2294 struct octdev_props *props;
2295 struct net_device *netdev;
2296 struct lio_version *vdata;
2297 struct lio *lio = NULL;
2298 u8 mac[ETH_ALEN], i, j;
2301 ifidx_or_pfnum = octeon_dev->pf_num;
2303 /* This is to handle link status changes */
2304 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC, OPCODE_NIC_INFO,
2305 lio_nic_info, octeon_dev);
2307 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
2308 * They are handled directly.
2310 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
2313 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
2316 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
2317 free_netsgbuf_with_resp);
2319 for (i = 0; i < octeon_dev->ifcount; i++) {
2320 resp_size = sizeof(struct liquidio_if_cfg_resp);
2321 ctx_size = sizeof(struct liquidio_if_cfg_context);
2322 data_size = sizeof(struct lio_version);
2323 sc = (struct octeon_soft_command *)
2324 octeon_alloc_soft_command(octeon_dev, data_size,
2325 resp_size, ctx_size);
2326 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
2327 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
2328 vdata = (struct lio_version *)sc->virtdptr;
2330 *((u64 *)vdata) = 0;
2331 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
2332 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
2333 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
2335 WRITE_ONCE(ctx->cond, 0);
2336 ctx->octeon_id = lio_get_device_id(octeon_dev);
2337 init_waitqueue_head(&ctx->wc);
2341 if_cfg.s.num_iqueues = octeon_dev->sriov_info.rings_per_vf;
2342 if_cfg.s.num_oqueues = octeon_dev->sriov_info.rings_per_vf;
2343 if_cfg.s.base_queue = 0;
2347 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
2348 OPCODE_NIC_IF_CFG, 0, if_cfg.u64,
2351 sc->callback = if_cfg_callback;
2352 sc->callback_arg = sc;
2353 sc->wait_time = 5000;
2355 retval = octeon_send_soft_command(octeon_dev, sc);
2356 if (retval == IQ_SEND_FAILED) {
2357 dev_err(&octeon_dev->pci_dev->dev,
2358 "iq/oq config failed status: %x\n", retval);
2359 /* Soft instr is freed by driver in case of failure. */
2360 goto setup_nic_dev_fail;
2363 /* Sleep on a wait queue till the cond flag indicates that the
2364 * response arrived or timed-out.
2366 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
2367 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
2368 goto setup_nic_wait_intr;
2371 retval = resp->status;
2373 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
2374 goto setup_nic_dev_fail;
2377 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
2378 (sizeof(struct liquidio_if_cfg_info)) >> 3);
2380 num_iqueues = hweight64(resp->cfg_info.iqmask);
2381 num_oqueues = hweight64(resp->cfg_info.oqmask);
2383 if (!(num_iqueues) || !(num_oqueues)) {
2384 dev_err(&octeon_dev->pci_dev->dev,
2385 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
2386 resp->cfg_info.iqmask, resp->cfg_info.oqmask);
2387 goto setup_nic_dev_fail;
2389 dev_dbg(&octeon_dev->pci_dev->dev,
2390 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
2391 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
2392 num_iqueues, num_oqueues);
2394 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
2397 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
2398 goto setup_nic_dev_fail;
2401 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
2403 /* Associate the routines that will handle different
2406 netdev->netdev_ops = &lionetdevops;
2408 lio = GET_LIO(netdev);
2410 memset(lio, 0, sizeof(struct lio));
2412 lio->ifidx = ifidx_or_pfnum;
2414 props = &octeon_dev->props[i];
2415 props->gmxport = resp->cfg_info.linfo.gmxport;
2416 props->netdev = netdev;
2418 lio->linfo.num_rxpciq = num_oqueues;
2419 lio->linfo.num_txpciq = num_iqueues;
2421 for (j = 0; j < num_oqueues; j++) {
2422 lio->linfo.rxpciq[j].u64 =
2423 resp->cfg_info.linfo.rxpciq[j].u64;
2425 for (j = 0; j < num_iqueues; j++) {
2426 lio->linfo.txpciq[j].u64 =
2427 resp->cfg_info.linfo.txpciq[j].u64;
2430 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
2431 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
2432 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
2433 lio->linfo.macaddr_is_admin_asgnd =
2434 resp->cfg_info.linfo.macaddr_is_admin_asgnd;
2436 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
2438 lio->dev_capability = NETIF_F_HIGHDMA
2439 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
2440 | NETIF_F_SG | NETIF_F_RXCSUM
2441 | NETIF_F_TSO | NETIF_F_TSO6
2444 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
2446 /* Copy of transmit encapsulation capabilities:
2447 * TSO, TSO6, Checksums for this device
2449 lio->enc_dev_capability = NETIF_F_IP_CSUM
2451 | NETIF_F_GSO_UDP_TUNNEL
2452 | NETIF_F_HW_CSUM | NETIF_F_SG
2454 | NETIF_F_TSO | NETIF_F_TSO6
2457 netdev->hw_enc_features =
2458 (lio->enc_dev_capability & ~NETIF_F_LRO);
2459 netdev->vlan_features = lio->dev_capability;
2460 /* Add any unchangeable hw features */
2461 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
2462 NETIF_F_HW_VLAN_CTAG_RX |
2463 NETIF_F_HW_VLAN_CTAG_TX;
2465 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
2467 netdev->hw_features = lio->dev_capability;
2469 /* MTU range: 68 - 16000 */
2470 netdev->min_mtu = LIO_MIN_MTU_SIZE;
2471 netdev->max_mtu = LIO_MAX_MTU_SIZE;
2473 /* Point to the properties for octeon device to which this
2474 * interface belongs.
2476 lio->oct_dev = octeon_dev;
2477 lio->octprops = props;
2478 lio->netdev = netdev;
2480 dev_dbg(&octeon_dev->pci_dev->dev,
2481 "if%d gmx: %d hw_addr: 0x%llx\n", i,
2482 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
2484 /* 64-bit swap required on LE machines */
2485 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
2486 for (j = 0; j < ETH_ALEN; j++)
2487 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
2489 /* Copy MAC Address to OS network device structure */
2490 ether_addr_copy(netdev->dev_addr, mac);
2492 if (liquidio_setup_io_queues(octeon_dev, i,
2493 lio->linfo.num_txpciq,
2494 lio->linfo.num_rxpciq)) {
2495 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
2496 goto setup_nic_dev_fail;
2499 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
2501 /* For VFs, enable Octeon device interrupts here,
2502 * as this is contingent upon IO queue setup
2504 octeon_dev->fn_list.enable_interrupt(octeon_dev,
2507 /* By default all interfaces on a single Octeon uses the same
2510 lio->txq = lio->linfo.txpciq[0].s.q_no;
2511 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
2513 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
2514 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
2516 if (setup_glists(lio, num_iqueues)) {
2517 dev_err(&octeon_dev->pci_dev->dev,
2518 "Gather list allocation failed\n");
2519 goto setup_nic_dev_fail;
2522 /* Register ethtool support */
2523 liquidio_set_ethtool_ops(netdev);
2524 if (lio->oct_dev->chip_id == OCTEON_CN23XX_VF_VID)
2525 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
2527 octeon_dev->priv_flags = 0x0;
2529 if (netdev->features & NETIF_F_LRO)
2530 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
2531 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
2533 if (setup_link_status_change_wq(netdev))
2534 goto setup_nic_dev_fail;
2536 if (setup_rx_oom_poll_fn(netdev))
2537 goto setup_nic_dev_fail;
2539 /* Register the network device with the OS */
2540 if (register_netdev(netdev)) {
2541 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
2542 goto setup_nic_dev_fail;
2545 dev_dbg(&octeon_dev->pci_dev->dev,
2546 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
2547 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2548 netif_carrier_off(netdev);
2549 lio->link_changes++;
2551 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
2553 /* Sending command to firmware to enable Rx checksum offload
2554 * by default at the time of setup of Liquidio driver for
2557 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
2558 OCTNET_CMD_RXCSUM_ENABLE);
2559 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
2560 OCTNET_CMD_TXCSUM_ENABLE);
2562 dev_dbg(&octeon_dev->pci_dev->dev,
2563 "NIC ifidx:%d Setup successful\n", i);
2565 octeon_free_soft_command(octeon_dev, sc);
2572 octeon_free_soft_command(octeon_dev, sc);
2574 setup_nic_wait_intr:
2577 dev_err(&octeon_dev->pci_dev->dev,
2578 "NIC ifidx:%d Setup failed\n", i);
2579 liquidio_destroy_nic_device(octeon_dev, i);
2585 * \brief initialize the NIC
2586 * @param oct octeon device
2588 * This initialization routine is called once the Octeon device application is
2591 static int liquidio_init_nic_module(struct octeon_device *oct)
2593 int num_nic_ports = 1;
2596 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
2598 /* only default iq and oq were initialized
2599 * initialize the rest as well run port_config command for each port
2601 oct->ifcount = num_nic_ports;
2602 memset(oct->props, 0,
2603 sizeof(struct octdev_props) * num_nic_ports);
2605 for (i = 0; i < MAX_OCTEON_LINKS; i++)
2606 oct->props[i].gmxport = -1;
2608 retval = setup_nic_devices(oct);
2610 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
2611 goto octnet_init_failure;
2614 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
2618 octnet_init_failure:
2626 * \brief Device initialization for each Octeon device that is probed
2627 * @param octeon_dev octeon device
2629 static int octeon_device_init(struct octeon_device *oct)
2634 atomic_set(&oct->status, OCT_DEV_BEGIN_STATE);
2636 /* Enable access to the octeon device and make its DMA capability
2639 if (octeon_pci_os_setup(oct))
2641 atomic_set(&oct->status, OCT_DEV_PCI_ENABLE_DONE);
2643 oct->chip_id = OCTEON_CN23XX_VF_VID;
2644 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
2645 oct->rev_id = rev_id & 0xff;
2647 if (cn23xx_setup_octeon_vf_device(oct))
2650 atomic_set(&oct->status, OCT_DEV_PCI_MAP_DONE);
2652 oct->app_mode = CVM_DRV_NIC_APP;
2654 /* Initialize the dispatch mechanism used to push packets arriving on
2655 * Octeon Output queues.
2657 if (octeon_init_dispatch_list(oct))
2660 atomic_set(&oct->status, OCT_DEV_DISPATCH_INIT_DONE);
2662 if (octeon_set_io_queues_off(oct)) {
2663 dev_err(&oct->pci_dev->dev, "setting io queues off failed\n");
2667 if (oct->fn_list.setup_device_regs(oct)) {
2668 dev_err(&oct->pci_dev->dev, "device registers configuration failed\n");
2672 /* Initialize soft command buffer pool */
2673 if (octeon_setup_sc_buffer_pool(oct)) {
2674 dev_err(&oct->pci_dev->dev, "sc buffer pool allocation failed\n");
2677 atomic_set(&oct->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
2679 /* Setup the data structures that manage this Octeon's Input queues. */
2680 if (octeon_setup_instr_queues(oct)) {
2681 dev_err(&oct->pci_dev->dev, "instruction queue initialization failed\n");
2684 atomic_set(&oct->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
2686 /* Initialize lists to manage the requests of different types that
2687 * arrive from user & kernel applications for this octeon device.
2689 if (octeon_setup_response_list(oct)) {
2690 dev_err(&oct->pci_dev->dev, "Response list allocation failed\n");
2693 atomic_set(&oct->status, OCT_DEV_RESP_LIST_INIT_DONE);
2695 if (octeon_setup_output_queues(oct)) {
2696 dev_err(&oct->pci_dev->dev, "Output queue initialization failed\n");
2699 atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);
2701 if (oct->fn_list.setup_mbox(oct)) {
2702 dev_err(&oct->pci_dev->dev, "Mailbox setup failed\n");
2705 atomic_set(&oct->status, OCT_DEV_MBOX_SETUP_DONE);
2707 if (octeon_allocate_ioq_vector(oct)) {
2708 dev_err(&oct->pci_dev->dev, "ioq vector allocation failed\n");
2711 atomic_set(&oct->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
2713 dev_info(&oct->pci_dev->dev, "OCTEON_CN23XX VF Version: %s, %d ioqs\n",
2714 LIQUIDIO_VERSION, oct->sriov_info.rings_per_vf);
2716 /* Setup the interrupt handler and record the INT SUM register address*/
2717 if (octeon_setup_interrupt(oct, oct->sriov_info.rings_per_vf))
2720 atomic_set(&oct->status, OCT_DEV_INTR_SET_DONE);
2722 /* ***************************************************************
2723 * The interrupts need to be enabled for the PF<-->VF handshake.
2724 * They are [re]-enabled after the PF<-->VF handshake so that the
2725 * correct OQ tick value is used (i.e. the value retrieved from
2726 * the PF as part of the handshake).
2729 /* Enable Octeon device interrupts */
2730 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
2732 if (cn23xx_octeon_pfvf_handshake(oct))
2735 /* Here we [re]-enable the interrupts so that the correct OQ tick value
2736 * is used (i.e. the value that was retrieved during the handshake)
2739 /* Enable Octeon device interrupts */
2740 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
2741 /* *************************************************************** */
2743 /* Enable the input and output queues for this Octeon device */
2744 if (oct->fn_list.enable_io_queues(oct)) {
2745 dev_err(&oct->pci_dev->dev, "enabling io queues failed\n");
2749 atomic_set(&oct->status, OCT_DEV_IO_QUEUES_DONE);
2751 atomic_set(&oct->status, OCT_DEV_HOST_OK);
2753 /* Send Credit for Octeon Output queues. Credits are always sent after
2754 * the output queue is enabled.
2756 for (j = 0; j < oct->num_oqs; j++)
2757 writel(oct->droq[j]->max_count, oct->droq[j]->pkts_credit_reg);
2759 /* Packets can start arriving on the output queues from this point. */
2761 atomic_set(&oct->status, OCT_DEV_CORE_OK);
2763 atomic_set(&oct->status, OCT_DEV_RUNNING);
2765 if (liquidio_init_nic_module(oct))
2771 static int __init liquidio_vf_init(void)
2773 octeon_init_device_list(0);
2774 return pci_register_driver(&liquidio_vf_pci_driver);
2777 static void __exit liquidio_vf_exit(void)
2779 pci_unregister_driver(&liquidio_vf_pci_driver);
2781 pr_info("LiquidIO_VF network module is now unloaded\n");
2784 module_init(liquidio_vf_init);
2785 module_exit(liquidio_vf_exit);