1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
4 * Copyright (C) 2020 Marvell.
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/vmalloc.h>
16 #include "octep_config.h"
17 #include "octep_main.h"
18 #include "octep_ctrl_net.h"
20 #define OCTEP_INTR_POLL_TIME_MSECS 100
21 struct workqueue_struct *octep_wq;
23 /* Supported Devices */
24 static const struct pci_device_id octep_pci_id_tbl[] = {
25 {PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_PCI_DEVICE_ID_CN93_PF)},
26 {PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_PCI_DEVICE_ID_CNF95N_PF)},
29 MODULE_DEVICE_TABLE(pci, octep_pci_id_tbl);
31 MODULE_AUTHOR("Veerasenareddy Burru <vburru@marvell.com>");
32 MODULE_DESCRIPTION(OCTEP_DRV_STRING);
33 MODULE_LICENSE("GPL");
36 * octep_alloc_ioq_vectors() - Allocate Tx/Rx Queue interrupt info.
38 * @oct: Octeon device private data structure.
40 * Allocate resources to hold per Tx/Rx queue interrupt info.
41 * This is the information passed to interrupt handler, from which napi poll
42 * is scheduled and includes quick access to private data of Tx/Rx queue
43 * corresponding to the interrupt being handled.
45 * Return: 0, on successful allocation of resources for all queue interrupts.
46 * -1, if failed to allocate any resource.
48 static int octep_alloc_ioq_vectors(struct octep_device *oct)
51 struct octep_ioq_vector *ioq_vector;
53 for (i = 0; i < oct->num_oqs; i++) {
54 oct->ioq_vector[i] = vzalloc(sizeof(*oct->ioq_vector[i]));
55 if (!oct->ioq_vector[i])
58 ioq_vector = oct->ioq_vector[i];
59 ioq_vector->iq = oct->iq[i];
60 ioq_vector->oq = oct->oq[i];
61 ioq_vector->octep_dev = oct;
64 dev_info(&oct->pdev->dev, "Allocated %d IOQ vectors\n", oct->num_oqs);
70 vfree(oct->ioq_vector[i]);
71 oct->ioq_vector[i] = NULL;
77 * octep_free_ioq_vectors() - Free Tx/Rx Queue interrupt vector info.
79 * @oct: Octeon device private data structure.
81 static void octep_free_ioq_vectors(struct octep_device *oct)
85 for (i = 0; i < oct->num_oqs; i++) {
86 if (oct->ioq_vector[i]) {
87 vfree(oct->ioq_vector[i]);
88 oct->ioq_vector[i] = NULL;
91 netdev_info(oct->netdev, "Freed IOQ Vectors\n");
95 * octep_enable_msix_range() - enable MSI-x interrupts.
97 * @oct: Octeon device private data structure.
99 * Allocate and enable all MSI-x interrupts (queue and non-queue interrupts)
100 * for the Octeon device.
102 * Return: 0, on successfully enabling all MSI-x interrupts.
103 * -1, if failed to enable any MSI-x interrupt.
105 static int octep_enable_msix_range(struct octep_device *oct)
107 int num_msix, msix_allocated;
110 /* Generic interrupts apart from input/output queues */
111 num_msix = oct->num_oqs + CFG_GET_NON_IOQ_MSIX(oct->conf);
112 oct->msix_entries = kcalloc(num_msix,
113 sizeof(struct msix_entry), GFP_KERNEL);
114 if (!oct->msix_entries)
117 for (i = 0; i < num_msix; i++)
118 oct->msix_entries[i].entry = i;
120 msix_allocated = pci_enable_msix_range(oct->pdev, oct->msix_entries,
122 if (msix_allocated != num_msix) {
123 dev_err(&oct->pdev->dev,
124 "Failed to enable %d msix irqs; got only %d\n",
125 num_msix, msix_allocated);
126 goto enable_msix_err;
128 oct->num_irqs = msix_allocated;
129 dev_info(&oct->pdev->dev, "MSI-X enabled successfully\n");
134 if (msix_allocated > 0)
135 pci_disable_msix(oct->pdev);
136 kfree(oct->msix_entries);
137 oct->msix_entries = NULL;
143 * octep_disable_msix() - disable MSI-x interrupts.
145 * @oct: Octeon device private data structure.
147 * Disable MSI-x on the Octeon device.
149 static void octep_disable_msix(struct octep_device *oct)
151 pci_disable_msix(oct->pdev);
152 kfree(oct->msix_entries);
153 oct->msix_entries = NULL;
154 dev_info(&oct->pdev->dev, "Disabled MSI-X\n");
158 * octep_non_ioq_intr_handler() - common handler for all generic interrupts.
160 * @irq: Interrupt number.
161 * @data: interrupt data.
163 * this is common handler for all non-queue (generic) interrupts.
165 static irqreturn_t octep_non_ioq_intr_handler(int irq, void *data)
167 struct octep_device *oct = data;
169 return oct->hw_ops.non_ioq_intr_handler(oct);
173 * octep_ioq_intr_handler() - handler for all Tx/Rx queue interrupts.
175 * @irq: Interrupt number.
176 * @data: interrupt data contains pointers to Tx/Rx queue private data
177 * and correspong NAPI context.
179 * this is common handler for all non-queue (generic) interrupts.
181 static irqreturn_t octep_ioq_intr_handler(int irq, void *data)
183 struct octep_ioq_vector *ioq_vector = data;
184 struct octep_device *oct = ioq_vector->octep_dev;
186 return oct->hw_ops.ioq_intr_handler(ioq_vector);
190 * octep_request_irqs() - Register interrupt handlers.
192 * @oct: Octeon device private data structure.
194 * Register handlers for all queue and non-queue interrupts.
196 * Return: 0, on successful registration of all interrupt handlers.
199 static int octep_request_irqs(struct octep_device *oct)
201 struct net_device *netdev = oct->netdev;
202 struct octep_ioq_vector *ioq_vector;
203 struct msix_entry *msix_entry;
204 char **non_ioq_msix_names;
205 int num_non_ioq_msix;
208 num_non_ioq_msix = CFG_GET_NON_IOQ_MSIX(oct->conf);
209 non_ioq_msix_names = CFG_GET_NON_IOQ_MSIX_NAMES(oct->conf);
211 oct->non_ioq_irq_names = kcalloc(num_non_ioq_msix,
212 OCTEP_MSIX_NAME_SIZE, GFP_KERNEL);
213 if (!oct->non_ioq_irq_names)
216 /* First few MSI-X interrupts are non-queue interrupts */
217 for (i = 0; i < num_non_ioq_msix; i++) {
220 irq_name = &oct->non_ioq_irq_names[i * OCTEP_MSIX_NAME_SIZE];
221 msix_entry = &oct->msix_entries[i];
223 snprintf(irq_name, OCTEP_MSIX_NAME_SIZE,
224 "%s-%s", netdev->name, non_ioq_msix_names[i]);
225 ret = request_irq(msix_entry->vector,
226 octep_non_ioq_intr_handler, 0,
230 "request_irq failed for %s; err=%d",
232 goto non_ioq_irq_err;
236 /* Request IRQs for Tx/Rx queues */
237 for (j = 0; j < oct->num_oqs; j++) {
238 ioq_vector = oct->ioq_vector[j];
239 msix_entry = &oct->msix_entries[j + num_non_ioq_msix];
241 snprintf(ioq_vector->name, sizeof(ioq_vector->name),
242 "%s-q%d", netdev->name, j);
243 ret = request_irq(msix_entry->vector,
244 octep_ioq_intr_handler, 0,
245 ioq_vector->name, ioq_vector);
248 "request_irq failed for Q-%d; err=%d",
253 cpumask_set_cpu(j % num_online_cpus(),
254 &ioq_vector->affinity_mask);
255 irq_set_affinity_hint(msix_entry->vector,
256 &ioq_vector->affinity_mask);
263 ioq_vector = oct->ioq_vector[j];
264 msix_entry = &oct->msix_entries[j + num_non_ioq_msix];
266 irq_set_affinity_hint(msix_entry->vector, NULL);
267 free_irq(msix_entry->vector, ioq_vector);
272 free_irq(oct->msix_entries[i].vector, oct);
274 kfree(oct->non_ioq_irq_names);
275 oct->non_ioq_irq_names = NULL;
281 * octep_free_irqs() - free all registered interrupts.
283 * @oct: Octeon device private data structure.
285 * Free all queue and non-queue interrupts of the Octeon device.
287 static void octep_free_irqs(struct octep_device *oct)
291 /* First few MSI-X interrupts are non queue interrupts; free them */
292 for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
293 free_irq(oct->msix_entries[i].vector, oct);
294 kfree(oct->non_ioq_irq_names);
296 /* Free IRQs for Input/Output (Tx/Rx) queues */
297 for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
298 irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
299 free_irq(oct->msix_entries[i].vector,
300 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
302 netdev_info(oct->netdev, "IRQs freed\n");
306 * octep_setup_irqs() - setup interrupts for the Octeon device.
308 * @oct: Octeon device private data structure.
310 * Allocate data structures to hold per interrupt information, allocate/enable
311 * MSI-x interrupt and register interrupt handlers.
313 * Return: 0, on successful allocation and registration of all interrupts.
316 static int octep_setup_irqs(struct octep_device *oct)
318 if (octep_alloc_ioq_vectors(oct))
321 if (octep_enable_msix_range(oct))
322 goto enable_msix_err;
324 if (octep_request_irqs(oct))
325 goto request_irq_err;
330 octep_disable_msix(oct);
332 octep_free_ioq_vectors(oct);
338 * octep_clean_irqs() - free all interrupts and its resources.
340 * @oct: Octeon device private data structure.
342 static void octep_clean_irqs(struct octep_device *oct)
344 octep_free_irqs(oct);
345 octep_disable_msix(oct);
346 octep_free_ioq_vectors(oct);
350 * octep_enable_ioq_irq() - Enable MSI-x interrupt of a Tx/Rx queue.
352 * @iq: Octeon Tx queue data structure.
353 * @oq: Octeon Rx queue data structure.
355 static void octep_enable_ioq_irq(struct octep_iq *iq, struct octep_oq *oq)
357 u32 pkts_pend = oq->pkts_pending;
359 netdev_dbg(iq->netdev, "enabling intr for Q-%u\n", iq->q_no);
360 if (iq->pkts_processed) {
361 writel(iq->pkts_processed, iq->inst_cnt_reg);
362 iq->pkt_in_done -= iq->pkts_processed;
363 iq->pkts_processed = 0;
365 if (oq->last_pkt_count - pkts_pend) {
366 writel(oq->last_pkt_count - pkts_pend, oq->pkts_sent_reg);
367 oq->last_pkt_count = pkts_pend;
370 /* Flush the previous wrties before writing to RESEND bit */
372 writeq(1UL << OCTEP_OQ_INTR_RESEND_BIT, oq->pkts_sent_reg);
373 writeq(1UL << OCTEP_IQ_INTR_RESEND_BIT, iq->inst_cnt_reg);
377 * octep_napi_poll() - NAPI poll function for Tx/Rx.
379 * @napi: pointer to napi context.
380 * @budget: max number of packets to be processed in single invocation.
382 static int octep_napi_poll(struct napi_struct *napi, int budget)
384 struct octep_ioq_vector *ioq_vector =
385 container_of(napi, struct octep_ioq_vector, napi);
386 u32 tx_pending, rx_done;
388 tx_pending = octep_iq_process_completions(ioq_vector->iq, budget);
389 rx_done = octep_oq_process_rx(ioq_vector->oq, budget);
391 /* need more polling if tx completion processing is still pending or
392 * processed at least 'budget' number of rx packets.
394 if (tx_pending || rx_done >= budget)
398 octep_enable_ioq_irq(ioq_vector->iq, ioq_vector->oq);
403 * octep_napi_add() - Add NAPI poll for all Tx/Rx queues.
405 * @oct: Octeon device private data structure.
407 static void octep_napi_add(struct octep_device *oct)
411 for (i = 0; i < oct->num_oqs; i++) {
412 netdev_dbg(oct->netdev, "Adding NAPI on Q-%d\n", i);
413 netif_napi_add(oct->netdev, &oct->ioq_vector[i]->napi,
415 oct->oq[i]->napi = &oct->ioq_vector[i]->napi;
420 * octep_napi_delete() - delete NAPI poll callback for all Tx/Rx queues.
422 * @oct: Octeon device private data structure.
424 static void octep_napi_delete(struct octep_device *oct)
428 for (i = 0; i < oct->num_oqs; i++) {
429 netdev_dbg(oct->netdev, "Deleting NAPI on Q-%d\n", i);
430 netif_napi_del(&oct->ioq_vector[i]->napi);
431 oct->oq[i]->napi = NULL;
436 * octep_napi_enable() - enable NAPI for all Tx/Rx queues.
438 * @oct: Octeon device private data structure.
440 static void octep_napi_enable(struct octep_device *oct)
444 for (i = 0; i < oct->num_oqs; i++) {
445 netdev_dbg(oct->netdev, "Enabling NAPI on Q-%d\n", i);
446 napi_enable(&oct->ioq_vector[i]->napi);
451 * octep_napi_disable() - disable NAPI for all Tx/Rx queues.
453 * @oct: Octeon device private data structure.
455 static void octep_napi_disable(struct octep_device *oct)
459 for (i = 0; i < oct->num_oqs; i++) {
460 netdev_dbg(oct->netdev, "Disabling NAPI on Q-%d\n", i);
461 napi_disable(&oct->ioq_vector[i]->napi);
465 static void octep_link_up(struct net_device *netdev)
467 netif_carrier_on(netdev);
468 netif_tx_start_all_queues(netdev);
472 * octep_open() - start the octeon network device.
474 * @netdev: pointer to kernel network device.
476 * setup Tx/Rx queues, interrupts and enable hardware operation of Tx/Rx queues
479 * Return: 0, on successfully setting up device and bring it up.
482 static int octep_open(struct net_device *netdev)
484 struct octep_device *oct = netdev_priv(netdev);
487 netdev_info(netdev, "Starting netdev ...\n");
488 netif_carrier_off(netdev);
490 oct->hw_ops.reset_io_queues(oct);
492 if (octep_setup_iqs(oct))
494 if (octep_setup_oqs(oct))
496 if (octep_setup_irqs(oct))
499 err = netif_set_real_num_tx_queues(netdev, oct->num_oqs);
502 err = netif_set_real_num_rx_queues(netdev, oct->num_iqs);
507 octep_napi_enable(oct);
509 oct->link_info.admin_up = 1;
510 octep_ctrl_net_set_rx_state(oct, OCTEP_CTRL_NET_INVALID_VFID, true,
512 octep_ctrl_net_set_link_status(oct, OCTEP_CTRL_NET_INVALID_VFID, true,
514 oct->poll_non_ioq_intr = false;
516 /* Enable the input and output queues for this Octeon device */
517 oct->hw_ops.enable_io_queues(oct);
519 /* Enable Octeon device interrupts */
520 oct->hw_ops.enable_interrupts(oct);
522 octep_oq_dbell_init(oct);
524 ret = octep_ctrl_net_get_link_status(oct, OCTEP_CTRL_NET_INVALID_VFID);
526 octep_link_up(netdev);
531 octep_clean_irqs(oct);
541 * octep_stop() - stop the octeon network device.
543 * @netdev: pointer to kernel network device.
545 * stop the device Tx/Rx operations, bring down the link and
546 * free up all resources allocated for Tx/Rx queues and interrupts.
548 static int octep_stop(struct net_device *netdev)
550 struct octep_device *oct = netdev_priv(netdev);
552 netdev_info(netdev, "Stopping the device ...\n");
554 octep_ctrl_net_set_link_status(oct, OCTEP_CTRL_NET_INVALID_VFID, false,
556 octep_ctrl_net_set_rx_state(oct, OCTEP_CTRL_NET_INVALID_VFID, false,
559 /* Stop Tx from stack */
560 netif_tx_stop_all_queues(netdev);
561 netif_carrier_off(netdev);
562 netif_tx_disable(netdev);
564 oct->link_info.admin_up = 0;
565 oct->link_info.oper_up = 0;
567 oct->hw_ops.disable_interrupts(oct);
568 octep_napi_disable(oct);
569 octep_napi_delete(oct);
571 octep_clean_irqs(oct);
572 octep_clean_iqs(oct);
574 oct->hw_ops.disable_io_queues(oct);
575 oct->hw_ops.reset_io_queues(oct);
579 oct->poll_non_ioq_intr = true;
580 queue_delayed_work(octep_wq, &oct->intr_poll_task,
581 msecs_to_jiffies(OCTEP_INTR_POLL_TIME_MSECS));
583 netdev_info(netdev, "Device stopped !!\n");
588 * octep_iq_full_check() - check if a Tx queue is full.
590 * @iq: Octeon Tx queue data structure.
592 * Return: 0, if the Tx queue is not full.
593 * 1, if the Tx queue is full.
595 static inline int octep_iq_full_check(struct octep_iq *iq)
597 if (likely((iq->max_count - atomic_read(&iq->instr_pending)) >=
598 OCTEP_WAKE_QUEUE_THRESHOLD))
601 /* Stop the queue if unable to send */
602 netif_stop_subqueue(iq->netdev, iq->q_no);
604 /* check again and restart the queue, in case NAPI has just freed
605 * enough Tx ring entries.
607 if (unlikely((iq->max_count - atomic_read(&iq->instr_pending)) >=
608 OCTEP_WAKE_QUEUE_THRESHOLD)) {
609 netif_start_subqueue(iq->netdev, iq->q_no);
610 iq->stats.restart_cnt++;
618 * octep_start_xmit() - Enqueue packet to Octoen hardware Tx Queue.
620 * @skb: packet skbuff pointer.
621 * @netdev: kernel network device.
623 * Return: NETDEV_TX_BUSY, if Tx Queue is full.
624 * NETDEV_TX_OK, if successfully enqueued to hardware Tx queue.
626 static netdev_tx_t octep_start_xmit(struct sk_buff *skb,
627 struct net_device *netdev)
629 struct octep_device *oct = netdev_priv(netdev);
630 struct octep_tx_sglist_desc *sglist;
631 struct octep_tx_buffer *tx_buffer;
632 struct octep_tx_desc_hw *hw_desc;
633 struct skb_shared_info *shinfo;
634 struct octep_instr_hdr *ih;
640 q_no = skb_get_queue_mapping(skb);
641 if (q_no >= oct->num_iqs) {
642 netdev_err(netdev, "Invalid Tx skb->queue_mapping=%d\n", q_no);
643 q_no = q_no % oct->num_iqs;
647 if (octep_iq_full_check(iq)) {
649 return NETDEV_TX_BUSY;
652 shinfo = skb_shinfo(skb);
653 nr_frags = shinfo->nr_frags;
655 wi = iq->host_write_index;
656 hw_desc = &iq->desc_ring[wi];
659 tx_buffer = iq->buff_info + wi;
660 tx_buffer->skb = skb;
664 ih->pkind = oct->pkind;
667 tx_buffer->gather = 0;
668 tx_buffer->dma = dma_map_single(iq->dev, skb->data,
669 skb->len, DMA_TO_DEVICE);
670 if (dma_mapping_error(iq->dev, tx_buffer->dma))
672 hw_desc->dptr = tx_buffer->dma;
678 sglist = tx_buffer->sglist;
680 ih->gsz = nr_frags + 1;
682 tx_buffer->gather = 1;
684 len = skb_headlen(skb);
685 dma = dma_map_single(iq->dev, skb->data, len, DMA_TO_DEVICE);
686 if (dma_mapping_error(iq->dev, dma))
689 dma_sync_single_for_cpu(iq->dev, tx_buffer->sglist_dma,
690 OCTEP_SGLIST_SIZE_PER_PKT,
692 memset(sglist, 0, OCTEP_SGLIST_SIZE_PER_PKT);
693 sglist[0].len[3] = len;
694 sglist[0].dma_ptr[0] = dma;
696 si = 1; /* entry 0 is main skb, mapped above */
697 frag = &shinfo->frags[0];
699 len = skb_frag_size(frag);
700 dma = skb_frag_dma_map(iq->dev, frag, 0,
702 if (dma_mapping_error(iq->dev, dma))
705 sglist[si >> 2].len[3 - (si & 3)] = len;
706 sglist[si >> 2].dma_ptr[si & 3] = dma;
711 dma_sync_single_for_device(iq->dev, tx_buffer->sglist_dma,
712 OCTEP_SGLIST_SIZE_PER_PKT,
715 hw_desc->dptr = tx_buffer->sglist_dma;
718 netdev_tx_sent_queue(iq->netdev_q, skb->len);
719 skb_tx_timestamp(skb);
720 atomic_inc(&iq->instr_pending);
722 if (wi == iq->max_count)
724 iq->host_write_index = wi;
725 /* Flush the hw descriptor before writing to doorbell */
728 /* Ring Doorbell to notify the NIC there is a new packet */
729 writel(1, iq->doorbell_reg);
730 iq->stats.instr_posted++;
735 dma_unmap_single(iq->dev, sglist[0].dma_ptr[0],
736 sglist[0].len[3], DMA_TO_DEVICE);
737 sglist[0].len[3] = 0;
740 dma_unmap_page(iq->dev, sglist[si >> 2].dma_ptr[si & 3],
741 sglist[si >> 2].len[3 - (si & 3)], DMA_TO_DEVICE);
742 sglist[si >> 2].len[3 - (si & 3)] = 0;
745 tx_buffer->gather = 0;
747 dev_kfree_skb_any(skb);
752 * octep_get_stats64() - Get Octeon network device statistics.
754 * @netdev: kernel network device.
755 * @stats: pointer to stats structure to be filled in.
757 static void octep_get_stats64(struct net_device *netdev,
758 struct rtnl_link_stats64 *stats)
760 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
761 struct octep_device *oct = netdev_priv(netdev);
764 if (netif_running(netdev))
765 octep_ctrl_net_get_if_stats(oct,
766 OCTEP_CTRL_NET_INVALID_VFID,
767 &oct->iface_rx_stats,
768 &oct->iface_tx_stats);
774 for (q = 0; q < oct->num_oqs; q++) {
775 struct octep_iq *iq = oct->iq[q];
776 struct octep_oq *oq = oct->oq[q];
778 tx_packets += iq->stats.instr_completed;
779 tx_bytes += iq->stats.bytes_sent;
780 rx_packets += oq->stats.packets;
781 rx_bytes += oq->stats.bytes;
783 stats->tx_packets = tx_packets;
784 stats->tx_bytes = tx_bytes;
785 stats->rx_packets = rx_packets;
786 stats->rx_bytes = rx_bytes;
787 stats->multicast = oct->iface_rx_stats.mcast_pkts;
788 stats->rx_errors = oct->iface_rx_stats.err_pkts;
789 stats->collisions = oct->iface_tx_stats.xscol;
790 stats->tx_fifo_errors = oct->iface_tx_stats.undflw;
794 * octep_tx_timeout_task - work queue task to Handle Tx queue timeout.
796 * @work: pointer to Tx queue timeout work_struct
798 * Stop and start the device so that it frees up all queue resources
799 * and restarts the queues, that potentially clears a Tx queue timeout
802 static void octep_tx_timeout_task(struct work_struct *work)
804 struct octep_device *oct = container_of(work, struct octep_device,
806 struct net_device *netdev = oct->netdev;
809 if (netif_running(netdev)) {
817 * octep_tx_timeout() - Handle Tx Queue timeout.
819 * @netdev: pointer to kernel network device.
820 * @txqueue: Timed out Tx queue number.
822 * Schedule a work to handle Tx queue timeout.
824 static void octep_tx_timeout(struct net_device *netdev, unsigned int txqueue)
826 struct octep_device *oct = netdev_priv(netdev);
828 queue_work(octep_wq, &oct->tx_timeout_task);
831 static int octep_set_mac(struct net_device *netdev, void *p)
833 struct octep_device *oct = netdev_priv(netdev);
834 struct sockaddr *addr = (struct sockaddr *)p;
837 if (!is_valid_ether_addr(addr->sa_data))
838 return -EADDRNOTAVAIL;
840 err = octep_ctrl_net_set_mac_addr(oct, OCTEP_CTRL_NET_INVALID_VFID,
841 addr->sa_data, true);
845 memcpy(oct->mac_addr, addr->sa_data, ETH_ALEN);
846 eth_hw_addr_set(netdev, addr->sa_data);
851 static int octep_change_mtu(struct net_device *netdev, int new_mtu)
853 struct octep_device *oct = netdev_priv(netdev);
854 struct octep_iface_link_info *link_info;
857 link_info = &oct->link_info;
858 if (link_info->mtu == new_mtu)
861 err = octep_ctrl_net_set_mtu(oct, OCTEP_CTRL_NET_INVALID_VFID, new_mtu,
864 oct->link_info.mtu = new_mtu;
865 netdev->mtu = new_mtu;
871 static const struct net_device_ops octep_netdev_ops = {
872 .ndo_open = octep_open,
873 .ndo_stop = octep_stop,
874 .ndo_start_xmit = octep_start_xmit,
875 .ndo_get_stats64 = octep_get_stats64,
876 .ndo_tx_timeout = octep_tx_timeout,
877 .ndo_set_mac_address = octep_set_mac,
878 .ndo_change_mtu = octep_change_mtu,
882 * octep_intr_poll_task - work queue task to process non-ioq interrupts.
884 * @work: pointer to mbox work_struct
886 * Process non-ioq interrupts to handle control mailbox, pfvf mailbox.
888 static void octep_intr_poll_task(struct work_struct *work)
890 struct octep_device *oct = container_of(work, struct octep_device,
891 intr_poll_task.work);
893 if (!oct->poll_non_ioq_intr) {
894 dev_info(&oct->pdev->dev, "Interrupt poll task stopped.\n");
898 oct->hw_ops.poll_non_ioq_interrupts(oct);
899 queue_delayed_work(octep_wq, &oct->intr_poll_task,
900 msecs_to_jiffies(OCTEP_INTR_POLL_TIME_MSECS));
904 * octep_hb_timeout_task - work queue task to check firmware heartbeat.
906 * @work: pointer to hb work_struct
908 * Check for heartbeat miss count. Uninitialize oct device if miss count
909 * exceeds configured max heartbeat miss count.
912 static void octep_hb_timeout_task(struct work_struct *work)
914 struct octep_device *oct = container_of(work, struct octep_device,
919 miss_cnt = atomic_inc_return(&oct->hb_miss_cnt);
920 if (miss_cnt < oct->conf->max_hb_miss_cnt) {
921 queue_delayed_work(octep_wq, &oct->hb_task,
922 msecs_to_jiffies(oct->conf->hb_interval * 1000));
926 dev_err(&oct->pdev->dev, "Missed %u heartbeats. Uninitializing\n",
929 if (netif_running(oct->netdev))
930 octep_stop(oct->netdev);
935 * octep_ctrl_mbox_task - work queue task to handle ctrl mbox messages.
937 * @work: pointer to ctrl mbox work_struct
939 * Poll ctrl mbox message queue and handle control messages from firmware.
941 static void octep_ctrl_mbox_task(struct work_struct *work)
943 struct octep_device *oct = container_of(work, struct octep_device,
946 octep_ctrl_net_recv_fw_messages(oct);
949 static const char *octep_devid_to_str(struct octep_device *oct)
951 switch (oct->chip_id) {
952 case OCTEP_PCI_DEVICE_ID_CN93_PF:
954 case OCTEP_PCI_DEVICE_ID_CNF95N_PF:
957 return "Unsupported";
962 * octep_device_setup() - Setup Octeon Device.
964 * @oct: Octeon device private data structure.
966 * Setup Octeon device hardware operations, configuration, etc ...
968 int octep_device_setup(struct octep_device *oct)
970 struct pci_dev *pdev = oct->pdev;
973 /* allocate memory for oct->conf */
974 oct->conf = kzalloc(sizeof(*oct->conf), GFP_KERNEL);
978 /* Map BAR regions */
979 for (i = 0; i < OCTEP_MMIO_REGIONS; i++) {
980 oct->mmio[i].hw_addr =
981 ioremap(pci_resource_start(oct->pdev, i * 2),
982 pci_resource_len(oct->pdev, i * 2));
983 if (!oct->mmio[i].hw_addr)
986 oct->mmio[i].mapped = 1;
989 oct->chip_id = pdev->device;
990 oct->rev_id = pdev->revision;
991 dev_info(&pdev->dev, "chip_id = 0x%x\n", pdev->device);
993 switch (oct->chip_id) {
994 case OCTEP_PCI_DEVICE_ID_CN93_PF:
995 case OCTEP_PCI_DEVICE_ID_CNF95N_PF:
996 dev_info(&pdev->dev, "Setting up OCTEON %s PF PASS%d.%d\n",
997 octep_devid_to_str(oct), OCTEP_MAJOR_REV(oct),
998 OCTEP_MINOR_REV(oct));
999 octep_device_setup_cn93_pf(oct);
1003 "%s: unsupported device\n", __func__);
1004 goto unsupported_dev;
1007 oct->pkind = CFG_GET_IQ_PKIND(oct->conf);
1009 ret = octep_ctrl_net_init(oct);
1013 atomic_set(&oct->hb_miss_cnt, 0);
1014 INIT_DELAYED_WORK(&oct->hb_task, octep_hb_timeout_task);
1015 queue_delayed_work(octep_wq, &oct->hb_task,
1016 msecs_to_jiffies(oct->conf->hb_interval * 1000));
1020 i = OCTEP_MMIO_REGIONS;
1023 iounmap(oct->mmio[i].hw_addr);
1030 * octep_device_cleanup() - Cleanup Octeon Device.
1032 * @oct: Octeon device private data structure.
1034 * Cleanup Octeon device allocated resources.
1036 static void octep_device_cleanup(struct octep_device *oct)
1040 oct->poll_non_ioq_intr = false;
1041 cancel_delayed_work_sync(&oct->intr_poll_task);
1042 cancel_work_sync(&oct->ctrl_mbox_task);
1044 dev_info(&oct->pdev->dev, "Cleaning up Octeon Device ...\n");
1046 for (i = 0; i < OCTEP_MAX_VF; i++) {
1047 vfree(oct->mbox[i]);
1048 oct->mbox[i] = NULL;
1051 octep_ctrl_net_uninit(oct);
1052 cancel_delayed_work_sync(&oct->hb_task);
1054 oct->hw_ops.soft_reset(oct);
1055 for (i = 0; i < OCTEP_MMIO_REGIONS; i++) {
1056 if (oct->mmio[i].mapped)
1057 iounmap(oct->mmio[i].hw_addr);
1064 static bool get_fw_ready_status(struct pci_dev *pdev)
1070 while ((pos = pci_find_next_ext_capability(pdev, pos,
1071 PCI_EXT_CAP_ID_VNDR))) {
1072 pci_read_config_word(pdev, pos + 4, &vsec_id);
1073 #define FW_STATUS_VSEC_ID 0xA3
1074 if (vsec_id != FW_STATUS_VSEC_ID)
1077 pci_read_config_byte(pdev, (pos + 8), &status);
1078 dev_info(&pdev->dev, "Firmware ready status = %u\n", status);
1085 * octep_probe() - Octeon PCI device probe handler.
1087 * @pdev: PCI device structure.
1088 * @ent: entry in Octeon PCI device ID table.
1090 * Initializes and enables the Octeon PCI device for network operations.
1091 * Initializes Octeon private data structure and registers a network device.
1093 static int octep_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1095 struct octep_device *octep_dev = NULL;
1096 struct net_device *netdev;
1099 err = pci_enable_device(pdev);
1101 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1105 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1107 dev_err(&pdev->dev, "Failed to set DMA mask !!\n");
1111 err = pci_request_mem_regions(pdev, OCTEP_DRV_NAME);
1113 dev_err(&pdev->dev, "Failed to map PCI memory regions\n");
1114 goto err_pci_regions;
1117 pci_set_master(pdev);
1119 if (!get_fw_ready_status(pdev)) {
1120 dev_notice(&pdev->dev, "Firmware not ready; defer probe.\n");
1121 err = -EPROBE_DEFER;
1122 goto err_alloc_netdev;
1125 netdev = alloc_etherdev_mq(sizeof(struct octep_device),
1128 dev_err(&pdev->dev, "Failed to allocate netdev\n");
1130 goto err_alloc_netdev;
1132 SET_NETDEV_DEV(netdev, &pdev->dev);
1134 octep_dev = netdev_priv(netdev);
1135 octep_dev->netdev = netdev;
1136 octep_dev->pdev = pdev;
1137 octep_dev->dev = &pdev->dev;
1138 pci_set_drvdata(pdev, octep_dev);
1140 err = octep_device_setup(octep_dev);
1142 dev_err(&pdev->dev, "Device setup failed\n");
1143 goto err_octep_config;
1145 INIT_WORK(&octep_dev->tx_timeout_task, octep_tx_timeout_task);
1146 INIT_WORK(&octep_dev->ctrl_mbox_task, octep_ctrl_mbox_task);
1147 INIT_DELAYED_WORK(&octep_dev->intr_poll_task, octep_intr_poll_task);
1148 octep_dev->poll_non_ioq_intr = true;
1149 queue_delayed_work(octep_wq, &octep_dev->intr_poll_task,
1150 msecs_to_jiffies(OCTEP_INTR_POLL_TIME_MSECS));
1152 netdev->netdev_ops = &octep_netdev_ops;
1153 octep_set_ethtool_ops(netdev);
1154 netif_carrier_off(netdev);
1156 netdev->hw_features = NETIF_F_SG;
1157 netdev->features |= netdev->hw_features;
1158 netdev->min_mtu = OCTEP_MIN_MTU;
1159 netdev->max_mtu = OCTEP_MAX_MTU;
1160 netdev->mtu = OCTEP_DEFAULT_MTU;
1162 err = octep_ctrl_net_get_mac_addr(octep_dev, OCTEP_CTRL_NET_INVALID_VFID,
1163 octep_dev->mac_addr);
1165 dev_err(&pdev->dev, "Failed to get mac address\n");
1166 goto register_dev_err;
1168 eth_hw_addr_set(netdev, octep_dev->mac_addr);
1170 err = register_netdev(netdev);
1172 dev_err(&pdev->dev, "Failed to register netdev\n");
1173 goto register_dev_err;
1175 dev_info(&pdev->dev, "Device probe successful\n");
1179 octep_device_cleanup(octep_dev);
1181 free_netdev(netdev);
1183 pci_release_mem_regions(pdev);
1186 pci_disable_device(pdev);
1191 * octep_remove() - Remove Octeon PCI device from driver control.
1193 * @pdev: PCI device structure of the Octeon device.
1195 * Cleanup all resources allocated for the Octeon device.
1196 * Unregister from network device and disable the PCI device.
1198 static void octep_remove(struct pci_dev *pdev)
1200 struct octep_device *oct = pci_get_drvdata(pdev);
1201 struct net_device *netdev;
1206 netdev = oct->netdev;
1207 if (netdev->reg_state == NETREG_REGISTERED)
1208 unregister_netdev(netdev);
1210 cancel_work_sync(&oct->tx_timeout_task);
1211 octep_device_cleanup(oct);
1212 pci_release_mem_regions(pdev);
1213 free_netdev(netdev);
1214 pci_disable_device(pdev);
1217 static struct pci_driver octep_driver = {
1218 .name = OCTEP_DRV_NAME,
1219 .id_table = octep_pci_id_tbl,
1220 .probe = octep_probe,
1221 .remove = octep_remove,
1225 * octep_init_module() - Module initialiation.
1227 * create common resource for the driver and register PCI driver.
1229 static int __init octep_init_module(void)
1233 pr_info("%s: Loading %s ...\n", OCTEP_DRV_NAME, OCTEP_DRV_STRING);
1235 /* work queue for all deferred tasks */
1236 octep_wq = create_singlethread_workqueue(OCTEP_DRV_NAME);
1238 pr_err("%s: Failed to create common workqueue\n",
1243 ret = pci_register_driver(&octep_driver);
1245 pr_err("%s: Failed to register PCI driver; err=%d\n",
1246 OCTEP_DRV_NAME, ret);
1247 destroy_workqueue(octep_wq);
1251 pr_info("%s: Loaded successfully !\n", OCTEP_DRV_NAME);
1257 * octep_exit_module() - Module exit routine.
1259 * unregister the driver with PCI subsystem and cleanup common resources.
1261 static void __exit octep_exit_module(void)
1263 pr_info("%s: Unloading ...\n", OCTEP_DRV_NAME);
1265 pci_unregister_driver(&octep_driver);
1266 destroy_workqueue(octep_wq);
1268 pr_info("%s: Unloading complete\n", OCTEP_DRV_NAME);
1271 module_init(octep_init_module);
1272 module_exit(octep_exit_module);