blob: d0ab97c15f4a36bad643ee741c55b6b489818d91 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2* Author: Cavium, Inc.
3*
4* Contact: support@cavium.com
5* Please include "LiquidIO" in the subject.
6*
7* Copyright (c) 2003-2015 Cavium, Inc.
8*
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.
12*
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
17* details.
18*
19* This file may also be available under a different license from Cavium.
20* Contact Cavium, Inc. for more information
21**********************************************************************/
22#include <linux/version.h>
23#include <linux/module.h>
24#include <linux/crc32.h>
25#include <linux/dma-mapping.h>
26#include <linux/pci.h>
27#include <linux/pci_ids.h>
28#include <linux/ip.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070029#include <net/ip.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070030#include <linux/ipv6.h>
31#include <linux/net_tstamp.h>
32#include <linux/if_vlan.h>
33#include <linux/firmware.h>
34#include <linux/ethtool.h>
35#include <linux/ptp_clock_kernel.h>
36#include <linux/types.h>
37#include <linux/list.h>
38#include <linux/workqueue.h>
39#include <linux/interrupt.h>
40#include "octeon_config.h"
41#include "liquidio_common.h"
42#include "octeon_droq.h"
43#include "octeon_iq.h"
44#include "response_manager.h"
45#include "octeon_device.h"
46#include "octeon_nic.h"
47#include "octeon_main.h"
48#include "octeon_network.h"
49#include "cn66xx_regs.h"
50#include "cn66xx_device.h"
51#include "cn68xx_regs.h"
52#include "cn68xx_device.h"
53#include "liquidio_image.h"
54
55MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
56MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
57MODULE_LICENSE("GPL");
58MODULE_VERSION(LIQUIDIO_VERSION);
59MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
60MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
61MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
62
63static int ddr_timeout = 10000;
64module_param(ddr_timeout, int, 0644);
65MODULE_PARM_DESC(ddr_timeout,
66 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
67
68static u32 console_bitmask;
69module_param(console_bitmask, int, 0644);
70MODULE_PARM_DESC(console_bitmask,
71 "Bitmask indicating which consoles have debug output redirected to syslog.");
72
73#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
74
75static int debug = -1;
76module_param(debug, int, 0644);
77MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
78
79static char fw_type[LIO_MAX_FW_TYPE_LEN];
80module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
81MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
82
83static int conf_type;
84module_param(conf_type, int, 0);
85MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
86
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070087static int ptp_enable = 1;
88
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070089/* Bit mask values for lio->ifstate */
90#define LIO_IFSTATE_DROQ_OPS 0x01
91#define LIO_IFSTATE_REGISTERED 0x02
92#define LIO_IFSTATE_RUNNING 0x04
93#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
94
95/* Polling interval for determining when NIC application is alive */
96#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
97
98/* runtime link query interval */
99#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
100
101struct liquidio_if_cfg_context {
102 int octeon_id;
103
104 wait_queue_head_t wc;
105
106 int cond;
107};
108
109struct liquidio_if_cfg_resp {
110 u64 rh;
111 struct liquidio_if_cfg_info cfg_info;
112 u64 status;
113};
114
115struct oct_link_status_resp {
116 u64 rh;
117 struct oct_link_info link_info;
118 u64 status;
119};
120
121struct oct_timestamp_resp {
122 u64 rh;
123 u64 timestamp;
124 u64 status;
125};
126
127#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
128
129union tx_info {
130 u64 u64;
131 struct {
132#ifdef __BIG_ENDIAN_BITFIELD
133 u16 gso_size;
134 u16 gso_segs;
135 u32 reserved;
136#else
137 u32 reserved;
138 u16 gso_segs;
139 u16 gso_size;
140#endif
141 } s;
142};
143
144/** Octeon device properties to be used by the NIC module.
145 * Each octeon device in the system will be represented
146 * by this structure in the NIC module.
147 */
148
149#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
150
151#define OCTNIC_GSO_MAX_HEADER_SIZE 128
152#define OCTNIC_GSO_MAX_SIZE (GSO_MAX_SIZE - OCTNIC_GSO_MAX_HEADER_SIZE)
153
154/** Structure of a node in list of gather components maintained by
155 * NIC driver for each network device.
156 */
157struct octnic_gather {
158 /** List manipulation. Next and prev pointers. */
159 struct list_head list;
160
161 /** Size of the gather component at sg in bytes. */
162 int sg_size;
163
164 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
165 int adjust;
166
167 /** Gather component that can accommodate max sized fragment list
168 * received from the IP layer.
169 */
170 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700171
172 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700173};
174
175/** This structure is used by NIC driver to store information required
176 * to free the sk_buff when the packet has been fetched by Octeon.
177 * Bytes offset below assume worst-case of a 64-bit system.
178 */
179struct octnet_buf_free_info {
180 /** Bytes 1-8. Pointer to network device private structure. */
181 struct lio *lio;
182
183 /** Bytes 9-16. Pointer to sk_buff. */
184 struct sk_buff *skb;
185
186 /** Bytes 17-24. Pointer to gather list. */
187 struct octnic_gather *g;
188
189 /** Bytes 25-32. Physical address of skb->data or gather list. */
190 u64 dptr;
191
192 /** Bytes 33-47. Piggybacked soft command, if any */
193 struct octeon_soft_command *sc;
194};
195
196struct handshake {
197 struct completion init;
198 struct completion started;
199 struct pci_dev *pci_dev;
200 int init_ok;
201 int started_ok;
202};
203
204struct octeon_device_priv {
205 /** Tasklet structures for this device. */
206 struct tasklet_struct droq_tasklet;
207 unsigned long napi_mask;
208};
209
210static int octeon_device_init(struct octeon_device *);
211static void liquidio_remove(struct pci_dev *pdev);
212static int liquidio_probe(struct pci_dev *pdev,
213 const struct pci_device_id *ent);
214
215static struct handshake handshake[MAX_OCTEON_DEVICES];
216static struct completion first_stage;
217
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700218static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700219{
220 int q_no;
221 int reschedule = 0;
222 struct octeon_device *oct = (struct octeon_device *)pdev;
223 struct octeon_device_priv *oct_priv =
224 (struct octeon_device_priv *)oct->priv;
225
226 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
227 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES; q_no++) {
228 if (!(oct->io_qmask.oq & (1UL << q_no)))
229 continue;
230 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
231 MAX_PACKET_BUDGET);
232 }
233
234 if (reschedule)
235 tasklet_schedule(&oct_priv->droq_tasklet);
236}
237
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700238static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700239{
240 struct octeon_device_priv *oct_priv =
241 (struct octeon_device_priv *)oct->priv;
242 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
243 int i;
244
245 do {
246 pending_pkts = 0;
247
248 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
249 if (!(oct->io_qmask.oq & (1UL << i)))
250 continue;
251 pkt_cnt += octeon_droq_check_hw_for_pkts(oct,
252 oct->droq[i]);
253 }
254 if (pkt_cnt > 0) {
255 pending_pkts += pkt_cnt;
256 tasklet_schedule(&oct_priv->droq_tasklet);
257 }
258 pkt_cnt = 0;
259 schedule_timeout_uninterruptible(1);
260
261 } while (retry-- && pending_pkts);
262
263 return pkt_cnt;
264}
265
266void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
267 unsigned int bytes_compl)
268{
269 struct netdev_queue *netdev_queue = txq;
270
271 netdev_tx_completed_queue(netdev_queue, pkts_compl, bytes_compl);
272}
273
274void octeon_update_tx_completion_counters(void *buf, int reqtype,
275 unsigned int *pkts_compl,
276 unsigned int *bytes_compl)
277{
278 struct octnet_buf_free_info *finfo;
279 struct sk_buff *skb = NULL;
280 struct octeon_soft_command *sc;
281
282 switch (reqtype) {
283 case REQTYPE_NORESP_NET:
284 case REQTYPE_NORESP_NET_SG:
285 finfo = buf;
286 skb = finfo->skb;
287 break;
288
289 case REQTYPE_RESP_NET_SG:
290 case REQTYPE_RESP_NET:
291 sc = buf;
292 skb = sc->callback_arg;
293 break;
294
295 default:
296 return;
297 }
298
299 (*pkts_compl)++;
300 *bytes_compl += skb->len;
301}
302
303void octeon_report_sent_bytes_to_bql(void *buf, int reqtype)
304{
305 struct octnet_buf_free_info *finfo;
306 struct sk_buff *skb;
307 struct octeon_soft_command *sc;
308 struct netdev_queue *txq;
309
310 switch (reqtype) {
311 case REQTYPE_NORESP_NET:
312 case REQTYPE_NORESP_NET_SG:
313 finfo = buf;
314 skb = finfo->skb;
315 break;
316
317 case REQTYPE_RESP_NET_SG:
318 case REQTYPE_RESP_NET:
319 sc = buf;
320 skb = sc->callback_arg;
321 break;
322
323 default:
324 return;
325 }
326
327 txq = netdev_get_tx_queue(skb->dev, skb_get_queue_mapping(skb));
328 netdev_tx_sent_queue(txq, skb->len);
329}
330
331int octeon_console_debug_enabled(u32 console)
332{
333 return (console_bitmask >> (console)) & 0x1;
334}
335
336/**
337 * \brief Forces all IO queues off on a given device
338 * @param oct Pointer to Octeon device
339 */
340static void force_io_queues_off(struct octeon_device *oct)
341{
342 if ((oct->chip_id == OCTEON_CN66XX) ||
343 (oct->chip_id == OCTEON_CN68XX)) {
344 /* Reset the Enable bits for Input Queues. */
345 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
346
347 /* Reset the Enable bits for Output Queues. */
348 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
349 }
350}
351
352/**
353 * \brief wait for all pending requests to complete
354 * @param oct Pointer to Octeon device
355 *
356 * Called during shutdown sequence
357 */
358static int wait_for_pending_requests(struct octeon_device *oct)
359{
360 int i, pcount = 0;
361
362 for (i = 0; i < 100; i++) {
363 pcount =
364 atomic_read(&oct->response_list
365 [OCTEON_ORDERED_SC_LIST].pending_req_count);
366 if (pcount)
367 schedule_timeout_uninterruptible(HZ / 10);
368 else
369 break;
370 }
371
372 if (pcount)
373 return 1;
374
375 return 0;
376}
377
378/**
379 * \brief Cause device to go quiet so it can be safely removed/reset/etc
380 * @param oct Pointer to Octeon device
381 */
382static inline void pcierror_quiesce_device(struct octeon_device *oct)
383{
384 int i;
385
386 /* Disable the input and output queues now. No more packets will
387 * arrive from Octeon, but we should wait for all packet processing
388 * to finish.
389 */
390 force_io_queues_off(oct);
391
392 /* To allow for in-flight requests */
393 schedule_timeout_uninterruptible(100);
394
395 if (wait_for_pending_requests(oct))
396 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
397
398 /* Force all requests waiting to be fetched by OCTEON to complete. */
399 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
400 struct octeon_instr_queue *iq;
401
402 if (!(oct->io_qmask.iq & (1UL << i)))
403 continue;
404 iq = oct->instr_queue[i];
405
406 if (atomic_read(&iq->instr_pending)) {
407 spin_lock_bh(&iq->lock);
408 iq->fill_cnt = 0;
409 iq->octeon_read_index = iq->host_write_index;
410 iq->stats.instr_processed +=
411 atomic_read(&iq->instr_pending);
412 lio_process_iq_request_list(oct, iq);
413 spin_unlock_bh(&iq->lock);
414 }
415 }
416
417 /* Force all pending ordered list requests to time out. */
418 lio_process_ordered_list(oct, 1);
419
420 /* We do not need to wait for output queue packets to be processed. */
421}
422
423/**
424 * \brief Cleanup PCI AER uncorrectable error status
425 * @param dev Pointer to PCI device
426 */
427static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
428{
429 int pos = 0x100;
430 u32 status, mask;
431
432 pr_info("%s :\n", __func__);
433
434 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
435 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
436 if (dev->error_state == pci_channel_io_normal)
437 status &= ~mask; /* Clear corresponding nonfatal bits */
438 else
439 status &= mask; /* Clear corresponding fatal bits */
440 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
441}
442
443/**
444 * \brief Stop all PCI IO to a given device
445 * @param dev Pointer to Octeon device
446 */
447static void stop_pci_io(struct octeon_device *oct)
448{
449 /* No more instructions will be forwarded. */
450 atomic_set(&oct->status, OCT_DEV_IN_RESET);
451
452 pci_disable_device(oct->pci_dev);
453
454 /* Disable interrupts */
455 oct->fn_list.disable_interrupt(oct->chip);
456
457 pcierror_quiesce_device(oct);
458
459 /* Release the interrupt line */
460 free_irq(oct->pci_dev->irq, oct);
461
462 if (oct->flags & LIO_FLAG_MSI_ENABLED)
463 pci_disable_msi(oct->pci_dev);
464
465 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
466 lio_get_state_string(&oct->status));
467
468 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
469 /* making it a common function for all OCTEON models */
470 cleanup_aer_uncorrect_error_status(oct->pci_dev);
471}
472
473/**
474 * \brief called when PCI error is detected
475 * @param pdev Pointer to PCI device
476 * @param state The current pci connection state
477 *
478 * This function is called after a PCI bus error affecting
479 * this device has been detected.
480 */
481static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
482 pci_channel_state_t state)
483{
484 struct octeon_device *oct = pci_get_drvdata(pdev);
485
486 /* Non-correctable Non-fatal errors */
487 if (state == pci_channel_io_normal) {
488 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
489 cleanup_aer_uncorrect_error_status(oct->pci_dev);
490 return PCI_ERS_RESULT_CAN_RECOVER;
491 }
492
493 /* Non-correctable Fatal errors */
494 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
495 stop_pci_io(oct);
496
497 /* Always return a DISCONNECT. There is no support for recovery but only
498 * for a clean shutdown.
499 */
500 return PCI_ERS_RESULT_DISCONNECT;
501}
502
503/**
504 * \brief mmio handler
505 * @param pdev Pointer to PCI device
506 */
507static pci_ers_result_t liquidio_pcie_mmio_enabled(struct pci_dev *pdev)
508{
509 /* We should never hit this since we never ask for a reset for a Fatal
510 * Error. We always return DISCONNECT in io_error above.
511 * But play safe and return RECOVERED for now.
512 */
513 return PCI_ERS_RESULT_RECOVERED;
514}
515
516/**
517 * \brief called after the pci bus has been reset.
518 * @param pdev Pointer to PCI device
519 *
520 * Restart the card from scratch, as if from a cold-boot. Implementation
521 * resembles the first-half of the octeon_resume routine.
522 */
523static pci_ers_result_t liquidio_pcie_slot_reset(struct pci_dev *pdev)
524{
525 /* We should never hit this since we never ask for a reset for a Fatal
526 * Error. We always return DISCONNECT in io_error above.
527 * But play safe and return RECOVERED for now.
528 */
529 return PCI_ERS_RESULT_RECOVERED;
530}
531
532/**
533 * \brief called when traffic can start flowing again.
534 * @param pdev Pointer to PCI device
535 *
536 * This callback is called when the error recovery driver tells us that
537 * its OK to resume normal operation. Implementation resembles the
538 * second-half of the octeon_resume routine.
539 */
540static void liquidio_pcie_resume(struct pci_dev *pdev)
541{
542 /* Nothing to be done here. */
543}
544
545#ifdef CONFIG_PM
546/**
547 * \brief called when suspending
548 * @param pdev Pointer to PCI device
549 * @param state state to suspend to
550 */
551static int liquidio_suspend(struct pci_dev *pdev, pm_message_t state)
552{
553 return 0;
554}
555
556/**
557 * \brief called when resuming
558 * @param pdev Pointer to PCI device
559 */
560static int liquidio_resume(struct pci_dev *pdev)
561{
562 return 0;
563}
564#endif
565
566/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100567static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700568 .error_detected = liquidio_pcie_error_detected,
569 .mmio_enabled = liquidio_pcie_mmio_enabled,
570 .slot_reset = liquidio_pcie_slot_reset,
571 .resume = liquidio_pcie_resume,
572};
573
574static const struct pci_device_id liquidio_pci_tbl[] = {
575 { /* 68xx */
576 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
577 },
578 { /* 66xx */
579 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
580 },
581 {
582 0, 0, 0, 0, 0, 0, 0
583 }
584};
585MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
586
587static struct pci_driver liquidio_pci_driver = {
588 .name = "LiquidIO",
589 .id_table = liquidio_pci_tbl,
590 .probe = liquidio_probe,
591 .remove = liquidio_remove,
592 .err_handler = &liquidio_err_handler, /* For AER */
593
594#ifdef CONFIG_PM
595 .suspend = liquidio_suspend,
596 .resume = liquidio_resume,
597#endif
598
599};
600
601/**
602 * \brief register PCI driver
603 */
604static int liquidio_init_pci(void)
605{
606 return pci_register_driver(&liquidio_pci_driver);
607}
608
609/**
610 * \brief unregister PCI driver
611 */
612static void liquidio_deinit_pci(void)
613{
614 pci_unregister_driver(&liquidio_pci_driver);
615}
616
617/**
618 * \brief check interface state
619 * @param lio per-network private data
620 * @param state_flag flag state to check
621 */
622static inline int ifstate_check(struct lio *lio, int state_flag)
623{
624 return atomic_read(&lio->ifstate) & state_flag;
625}
626
627/**
628 * \brief set interface state
629 * @param lio per-network private data
630 * @param state_flag flag state to set
631 */
632static inline void ifstate_set(struct lio *lio, int state_flag)
633{
634 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
635}
636
637/**
638 * \brief clear interface state
639 * @param lio per-network private data
640 * @param state_flag flag state to clear
641 */
642static inline void ifstate_reset(struct lio *lio, int state_flag)
643{
644 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
645}
646
647/**
648 * \brief Stop Tx queues
649 * @param netdev network device
650 */
651static inline void txqs_stop(struct net_device *netdev)
652{
653 if (netif_is_multiqueue(netdev)) {
654 int i;
655
656 for (i = 0; i < netdev->num_tx_queues; i++)
657 netif_stop_subqueue(netdev, i);
658 } else {
659 netif_stop_queue(netdev);
660 }
661}
662
663/**
664 * \brief Start Tx queues
665 * @param netdev network device
666 */
667static inline void txqs_start(struct net_device *netdev)
668{
669 if (netif_is_multiqueue(netdev)) {
670 int i;
671
672 for (i = 0; i < netdev->num_tx_queues; i++)
673 netif_start_subqueue(netdev, i);
674 } else {
675 netif_start_queue(netdev);
676 }
677}
678
679/**
680 * \brief Wake Tx queues
681 * @param netdev network device
682 */
683static inline void txqs_wake(struct net_device *netdev)
684{
685 if (netif_is_multiqueue(netdev)) {
686 int i;
687
688 for (i = 0; i < netdev->num_tx_queues; i++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700689 if (__netif_subqueue_stopped(netdev, i))
690 netif_wake_subqueue(netdev, i);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700691 } else {
692 netif_wake_queue(netdev);
693 }
694}
695
696/**
697 * \brief Stop Tx queue
698 * @param netdev network device
699 */
700static void stop_txq(struct net_device *netdev)
701{
702 txqs_stop(netdev);
703}
704
705/**
706 * \brief Start Tx queue
707 * @param netdev network device
708 */
709static void start_txq(struct net_device *netdev)
710{
711 struct lio *lio = GET_LIO(netdev);
712
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700713 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700714 txqs_start(netdev);
715 return;
716 }
717}
718
719/**
720 * \brief Wake a queue
721 * @param netdev network device
722 * @param q which queue to wake
723 */
724static inline void wake_q(struct net_device *netdev, int q)
725{
726 if (netif_is_multiqueue(netdev))
727 netif_wake_subqueue(netdev, q);
728 else
729 netif_wake_queue(netdev);
730}
731
732/**
733 * \brief Stop a queue
734 * @param netdev network device
735 * @param q which queue to stop
736 */
737static inline void stop_q(struct net_device *netdev, int q)
738{
739 if (netif_is_multiqueue(netdev))
740 netif_stop_subqueue(netdev, q);
741 else
742 netif_stop_queue(netdev);
743}
744
745/**
746 * \brief Check Tx queue status, and take appropriate action
747 * @param lio per-network private data
748 * @returns 0 if full, number of queues woken up otherwise
749 */
750static inline int check_txq_status(struct lio *lio)
751{
752 int ret_val = 0;
753
754 if (netif_is_multiqueue(lio->netdev)) {
755 int numqs = lio->netdev->num_tx_queues;
756 int q, iq = 0;
757
758 /* check each sub-queue state */
759 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700760 iq = lio->linfo.txpciq[q %
761 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700762 if (octnet_iq_is_full(lio->oct_dev, iq))
763 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700764 if (__netif_subqueue_stopped(lio->netdev, q)) {
765 wake_q(lio->netdev, q);
766 ret_val++;
767 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700768 }
769 } else {
770 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
771 return 0;
772 wake_q(lio->netdev, lio->txq);
773 ret_val = 1;
774 }
775 return ret_val;
776}
777
778/**
779 * Remove the node at the head of the list. The list would be empty at
780 * the end of this call if there are no more nodes in the list.
781 */
782static inline struct list_head *list_delete_head(struct list_head *root)
783{
784 struct list_head *node;
785
786 if ((root->prev == root) && (root->next == root))
787 node = NULL;
788 else
789 node = root->next;
790
791 if (node)
792 list_del(node);
793
794 return node;
795}
796
797/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700798 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700799 * @param lio per-network private data
800 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700801static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700802{
803 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700804 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700805
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700806 if (!lio->glist)
807 return;
808
809 for (i = 0; i < lio->linfo.num_txpciq; i++) {
810 do {
811 g = (struct octnic_gather *)
812 list_delete_head(&lio->glist[i]);
813 if (g) {
814 if (g->sg) {
815 dma_unmap_single(&lio->oct_dev->
816 pci_dev->dev,
817 g->sg_dma_ptr,
818 g->sg_size,
819 DMA_TO_DEVICE);
820 kfree((void *)((unsigned long)g->sg -
821 g->adjust));
822 }
823 kfree(g);
824 }
825 } while (g);
826 }
827
828 kfree((void *)lio->glist);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700829}
830
831/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700832 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700833 * @param lio per-network private data
834 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700835static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700836{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700837 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700838 struct octnic_gather *g;
839
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700840 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
841 GFP_KERNEL);
842 if (!lio->glist_lock)
843 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700844
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700845 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
846 GFP_KERNEL);
847 if (!lio->glist) {
848 kfree((void *)lio->glist_lock);
849 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700850 }
851
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700852 for (i = 0; i < num_iqs; i++) {
853 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700854
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700855 spin_lock_init(&lio->glist_lock[i]);
856
857 INIT_LIST_HEAD(&lio->glist[i]);
858
859 for (j = 0; j < lio->tx_qsize; j++) {
860 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
861 numa_node);
862 if (!g)
863 g = kzalloc(sizeof(*g), GFP_KERNEL);
864 if (!g)
865 break;
866
867 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
868 OCT_SG_ENTRY_SIZE);
869
870 g->sg = kmalloc_node(g->sg_size + 8,
871 GFP_KERNEL, numa_node);
872 if (!g->sg)
873 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
874 if (!g->sg) {
875 kfree(g);
876 break;
877 }
878
879 /* The gather component should be aligned on 64-bit
880 * boundary
881 */
882 if (((unsigned long)g->sg) & 7) {
883 g->adjust = 8 - (((unsigned long)g->sg) & 7);
884 g->sg = (struct octeon_sg_entry *)
885 ((unsigned long)g->sg + g->adjust);
886 }
887 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
888 g->sg, g->sg_size,
889 DMA_TO_DEVICE);
890 if (dma_mapping_error(&oct->pci_dev->dev,
891 g->sg_dma_ptr)) {
892 kfree((void *)((unsigned long)g->sg -
893 g->adjust));
894 kfree(g);
895 break;
896 }
897
898 list_add_tail(&g->list, &lio->glist[i]);
899 }
900
901 if (j != lio->tx_qsize) {
902 delete_glists(lio);
903 return 1;
904 }
905 }
906
907 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700908}
909
910/**
911 * \brief Print link information
912 * @param netdev network device
913 */
914static void print_link_info(struct net_device *netdev)
915{
916 struct lio *lio = GET_LIO(netdev);
917
918 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
919 struct oct_link_info *linfo = &lio->linfo;
920
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700921 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700922 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
923 linfo->link.s.speed,
924 (linfo->link.s.duplex) ? "Full" : "Half");
925 } else {
926 netif_info(lio, link, lio->netdev, "Link Down\n");
927 }
928 }
929}
930
931/**
932 * \brief Update link status
933 * @param netdev network device
934 * @param ls link status structure
935 *
936 * Called on receipt of a link status response from the core application to
937 * update each interface's link status.
938 */
939static inline void update_link_status(struct net_device *netdev,
940 union oct_link_status *ls)
941{
942 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700943 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700944
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700945 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700946
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700947 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700948 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700949 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700950
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700951 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700952 netif_carrier_on(netdev);
953 /* start_txq(netdev); */
954 txqs_wake(netdev);
955 } else {
956 netif_carrier_off(netdev);
957 stop_txq(netdev);
958 }
959 }
960}
961
962/**
963 * \brief Droq packet processor sceduler
964 * @param oct octeon device
965 */
966static
967void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
968{
969 struct octeon_device_priv *oct_priv =
970 (struct octeon_device_priv *)oct->priv;
971 u64 oq_no;
972 struct octeon_droq *droq;
973
974 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
975 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES; oq_no++) {
976 if (!(oct->droq_intr & (1 << oq_no)))
977 continue;
978
979 droq = oct->droq[oq_no];
980
981 if (droq->ops.poll_mode) {
982 droq->ops.napi_fn(droq);
983 oct_priv->napi_mask |= (1 << oq_no);
984 } else {
985 tasklet_schedule(&oct_priv->droq_tasklet);
986 }
987 }
988 }
989}
990
991/**
992 * \brief Interrupt handler for octeon
993 * @param irq unused
994 * @param dev octeon device
995 */
996static
997irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
998{
999 struct octeon_device *oct = (struct octeon_device *)dev;
1000 irqreturn_t ret;
1001
1002 /* Disable our interrupts for the duration of ISR */
1003 oct->fn_list.disable_interrupt(oct->chip);
1004
1005 ret = oct->fn_list.process_interrupt_regs(oct);
1006
1007 if (ret == IRQ_HANDLED)
1008 liquidio_schedule_droq_pkt_handlers(oct);
1009
1010 /* Re-enable our interrupts */
1011 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
1012 oct->fn_list.enable_interrupt(oct->chip);
1013
1014 return ret;
1015}
1016
1017/**
1018 * \brief Setup interrupt for octeon device
1019 * @param oct octeon device
1020 *
1021 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1022 */
1023static int octeon_setup_interrupt(struct octeon_device *oct)
1024{
1025 int irqret, err;
1026
1027 err = pci_enable_msi(oct->pci_dev);
1028 if (err)
1029 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1030 err);
1031 else
1032 oct->flags |= LIO_FLAG_MSI_ENABLED;
1033
1034 irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
1035 IRQF_SHARED, "octeon", oct);
1036 if (irqret) {
1037 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1038 pci_disable_msi(oct->pci_dev);
1039 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1040 irqret);
1041 return 1;
1042 }
1043
1044 return 0;
1045}
1046
1047/**
1048 * \brief PCI probe handler
1049 * @param pdev PCI device structure
1050 * @param ent unused
1051 */
1052static int liquidio_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1053{
1054 struct octeon_device *oct_dev = NULL;
1055 struct handshake *hs;
1056
1057 oct_dev = octeon_allocate_device(pdev->device,
1058 sizeof(struct octeon_device_priv));
1059 if (!oct_dev) {
1060 dev_err(&pdev->dev, "Unable to allocate device\n");
1061 return -ENOMEM;
1062 }
1063
1064 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1065 (u32)pdev->vendor, (u32)pdev->device);
1066
1067 /* Assign octeon_device for this device to the private data area. */
1068 pci_set_drvdata(pdev, oct_dev);
1069
1070 /* set linux specific device pointer */
1071 oct_dev->pci_dev = (void *)pdev;
1072
1073 hs = &handshake[oct_dev->octeon_id];
1074 init_completion(&hs->init);
1075 init_completion(&hs->started);
1076 hs->pci_dev = pdev;
1077
1078 if (oct_dev->octeon_id == 0)
1079 /* first LiquidIO NIC is detected */
1080 complete(&first_stage);
1081
1082 if (octeon_device_init(oct_dev)) {
1083 liquidio_remove(pdev);
1084 return -ENOMEM;
1085 }
1086
1087 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1088
1089 return 0;
1090}
1091
1092/**
1093 *\brief Destroy resources associated with octeon device
1094 * @param pdev PCI device structure
1095 * @param ent unused
1096 */
1097static void octeon_destroy_resources(struct octeon_device *oct)
1098{
1099 int i;
1100 struct octeon_device_priv *oct_priv =
1101 (struct octeon_device_priv *)oct->priv;
1102
1103 struct handshake *hs;
1104
1105 switch (atomic_read(&oct->status)) {
1106 case OCT_DEV_RUNNING:
1107 case OCT_DEV_CORE_OK:
1108
1109 /* No more instructions will be forwarded. */
1110 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1111
1112 oct->app_mode = CVM_DRV_INVALID_APP;
1113 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1114 lio_get_state_string(&oct->status));
1115
1116 schedule_timeout_uninterruptible(HZ / 10);
1117
1118 /* fallthrough */
1119 case OCT_DEV_HOST_OK:
1120
1121 /* fallthrough */
1122 case OCT_DEV_CONSOLE_INIT_DONE:
1123 /* Remove any consoles */
1124 octeon_remove_consoles(oct);
1125
1126 /* fallthrough */
1127 case OCT_DEV_IO_QUEUES_DONE:
1128 if (wait_for_pending_requests(oct))
1129 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1130
1131 if (lio_wait_for_instr_fetch(oct))
1132 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1133
1134 /* Disable the input and output queues now. No more packets will
1135 * arrive from Octeon, but we should wait for all packet
1136 * processing to finish.
1137 */
1138 oct->fn_list.disable_io_queues(oct);
1139
1140 if (lio_wait_for_oq_pkts(oct))
1141 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1142
1143 /* Disable interrupts */
1144 oct->fn_list.disable_interrupt(oct->chip);
1145
1146 /* Release the interrupt line */
1147 free_irq(oct->pci_dev->irq, oct);
1148
1149 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1150 pci_disable_msi(oct->pci_dev);
1151
1152 /* Soft reset the octeon device before exiting */
1153 oct->fn_list.soft_reset(oct);
1154
1155 /* Disable the device, releasing the PCI INT */
1156 pci_disable_device(oct->pci_dev);
1157
1158 /* fallthrough */
1159 case OCT_DEV_IN_RESET:
1160 case OCT_DEV_DROQ_INIT_DONE:
1161 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1162 mdelay(100);
1163 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
1164 if (!(oct->io_qmask.oq & (1UL << i)))
1165 continue;
1166 octeon_delete_droq(oct, i);
1167 }
1168
1169 /* Force any pending handshakes to complete */
1170 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1171 hs = &handshake[i];
1172
1173 if (hs->pci_dev) {
1174 handshake[oct->octeon_id].init_ok = 0;
1175 complete(&handshake[oct->octeon_id].init);
1176 handshake[oct->octeon_id].started_ok = 0;
1177 complete(&handshake[oct->octeon_id].started);
1178 }
1179 }
1180
1181 /* fallthrough */
1182 case OCT_DEV_RESP_LIST_INIT_DONE:
1183 octeon_delete_response_list(oct);
1184
1185 /* fallthrough */
1186 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1187 octeon_free_sc_buffer_pool(oct);
1188
1189 /* fallthrough */
1190 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
1191 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
1192 if (!(oct->io_qmask.iq & (1UL << i)))
1193 continue;
1194 octeon_delete_instr_queue(oct, i);
1195 }
1196
1197 /* fallthrough */
1198 case OCT_DEV_DISPATCH_INIT_DONE:
1199 octeon_delete_dispatch_list(oct);
1200 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1201
1202 /* fallthrough */
1203 case OCT_DEV_PCI_MAP_DONE:
1204 octeon_unmap_pci_barx(oct, 0);
1205 octeon_unmap_pci_barx(oct, 1);
1206
1207 /* fallthrough */
1208 case OCT_DEV_BEGIN_STATE:
1209 /* Nothing to be done here either */
1210 break;
1211 } /* end switch(oct->status) */
1212
1213 tasklet_kill(&oct_priv->droq_tasklet);
1214}
1215
1216/**
1217 * \brief Send Rx control command
1218 * @param lio per-network private data
1219 * @param start_stop whether to start or stop
1220 */
1221static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1222{
1223 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001224
1225 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1226
1227 nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001228 nctrl.ncmd.s.param1 = start_stop;
1229 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001230 nctrl.netpndev = (u64)lio->netdev;
1231
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001232 if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl) < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001233 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1234}
1235
1236/**
1237 * \brief Destroy NIC device interface
1238 * @param oct octeon device
1239 * @param ifidx which interface to destroy
1240 *
1241 * Cleanup associated with each interface for an Octeon device when NIC
1242 * module is being unloaded or if initialization fails during load.
1243 */
1244static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1245{
1246 struct net_device *netdev = oct->props[ifidx].netdev;
1247 struct lio *lio;
1248
1249 if (!netdev) {
1250 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1251 __func__, ifidx);
1252 return;
1253 }
1254
1255 lio = GET_LIO(netdev);
1256
1257 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1258
1259 send_rx_ctrl_cmd(lio, 0);
1260
1261 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1262 txqs_stop(netdev);
1263
1264 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1265 unregister_netdev(netdev);
1266
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001267 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001268
1269 free_netdev(netdev);
1270
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001271 oct->props[ifidx].gmxport = -1;
1272
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001273 oct->props[ifidx].netdev = NULL;
1274}
1275
1276/**
1277 * \brief Stop complete NIC functionality
1278 * @param oct octeon device
1279 */
1280static int liquidio_stop_nic_module(struct octeon_device *oct)
1281{
1282 int i, j;
1283 struct lio *lio;
1284
1285 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1286 if (!oct->ifcount) {
1287 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1288 return 1;
1289 }
1290
1291 for (i = 0; i < oct->ifcount; i++) {
1292 lio = GET_LIO(oct->props[i].netdev);
1293 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001294 octeon_unregister_droq_ops(oct,
1295 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001296 }
1297
1298 for (i = 0; i < oct->ifcount; i++)
1299 liquidio_destroy_nic_device(oct, i);
1300
1301 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1302 return 0;
1303}
1304
1305/**
1306 * \brief Cleans up resources at unload time
1307 * @param pdev PCI device structure
1308 */
1309static void liquidio_remove(struct pci_dev *pdev)
1310{
1311 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1312
1313 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1314
1315 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1316 liquidio_stop_nic_module(oct_dev);
1317
1318 /* Reset the octeon device and cleanup all memory allocated for
1319 * the octeon device by driver.
1320 */
1321 octeon_destroy_resources(oct_dev);
1322
1323 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1324
1325 /* This octeon device has been removed. Update the global
1326 * data structure to reflect this. Free the device structure.
1327 */
1328 octeon_free_device_mem(oct_dev);
1329}
1330
1331/**
1332 * \brief Identify the Octeon device and to map the BAR address space
1333 * @param oct octeon device
1334 */
1335static int octeon_chip_specific_setup(struct octeon_device *oct)
1336{
1337 u32 dev_id, rev_id;
1338 int ret = 1;
1339
1340 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1341 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1342 oct->rev_id = rev_id & 0xff;
1343
1344 switch (dev_id) {
1345 case OCTEON_CN68XX_PCIID:
1346 oct->chip_id = OCTEON_CN68XX;
1347 ret = lio_setup_cn68xx_octeon_device(oct);
1348 break;
1349
1350 case OCTEON_CN66XX_PCIID:
1351 oct->chip_id = OCTEON_CN66XX;
1352 ret = lio_setup_cn66xx_octeon_device(oct);
1353 break;
1354 default:
1355 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1356 dev_id);
1357 }
1358
1359 if (!ret)
1360 dev_info(&oct->pci_dev->dev, "CN68XX PASS%d.%d %s\n",
1361 OCTEON_MAJOR_REV(oct),
1362 OCTEON_MINOR_REV(oct),
1363 octeon_get_conf(oct)->card_name);
1364
1365 return ret;
1366}
1367
1368/**
1369 * \brief PCI initialization for each Octeon device.
1370 * @param oct octeon device
1371 */
1372static int octeon_pci_os_setup(struct octeon_device *oct)
1373{
1374 /* setup PCI stuff first */
1375 if (pci_enable_device(oct->pci_dev)) {
1376 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1377 return 1;
1378 }
1379
1380 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1381 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1382 return 1;
1383 }
1384
1385 /* Enable PCI DMA Master. */
1386 pci_set_master(oct->pci_dev);
1387
1388 return 0;
1389}
1390
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001391static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1392{
1393 int q = 0;
1394
1395 if (netif_is_multiqueue(lio->netdev))
1396 q = skb->queue_mapping % lio->linfo.num_txpciq;
1397
1398 return q;
1399}
1400
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001401/**
1402 * \brief Check Tx queue state for a given network buffer
1403 * @param lio per-network private data
1404 * @param skb network buffer
1405 */
1406static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1407{
1408 int q = 0, iq = 0;
1409
1410 if (netif_is_multiqueue(lio->netdev)) {
1411 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001412 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001413 } else {
1414 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001415 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001416 }
1417
1418 if (octnet_iq_is_full(lio->oct_dev, iq))
1419 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001420
1421 if (__netif_subqueue_stopped(lio->netdev, q))
1422 wake_q(lio->netdev, q);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001423 return 1;
1424}
1425
1426/**
1427 * \brief Unmap and free network buffer
1428 * @param buf buffer
1429 */
1430static void free_netbuf(void *buf)
1431{
1432 struct sk_buff *skb;
1433 struct octnet_buf_free_info *finfo;
1434 struct lio *lio;
1435
1436 finfo = (struct octnet_buf_free_info *)buf;
1437 skb = finfo->skb;
1438 lio = finfo->lio;
1439
1440 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1441 DMA_TO_DEVICE);
1442
1443 check_txq_state(lio, skb);
1444
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001445 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001446}
1447
1448/**
1449 * \brief Unmap and free gather buffer
1450 * @param buf buffer
1451 */
1452static void free_netsgbuf(void *buf)
1453{
1454 struct octnet_buf_free_info *finfo;
1455 struct sk_buff *skb;
1456 struct lio *lio;
1457 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001458 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001459
1460 finfo = (struct octnet_buf_free_info *)buf;
1461 skb = finfo->skb;
1462 lio = finfo->lio;
1463 g = finfo->g;
1464 frags = skb_shinfo(skb)->nr_frags;
1465
1466 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1467 g->sg[0].ptr[0], (skb->len - skb->data_len),
1468 DMA_TO_DEVICE);
1469
1470 i = 1;
1471 while (frags--) {
1472 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1473
1474 pci_unmap_page((lio->oct_dev)->pci_dev,
1475 g->sg[(i >> 2)].ptr[(i & 3)],
1476 frag->size, DMA_TO_DEVICE);
1477 i++;
1478 }
1479
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001480 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1481 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001482
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001483 iq = skb_iq(lio, skb);
1484 spin_lock(&lio->glist_lock[iq]);
1485 list_add_tail(&g->list, &lio->glist[iq]);
1486 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001487
1488 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1489
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001490 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001491}
1492
1493/**
1494 * \brief Unmap and free gather buffer with response
1495 * @param buf buffer
1496 */
1497static void free_netsgbuf_with_resp(void *buf)
1498{
1499 struct octeon_soft_command *sc;
1500 struct octnet_buf_free_info *finfo;
1501 struct sk_buff *skb;
1502 struct lio *lio;
1503 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001504 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001505
1506 sc = (struct octeon_soft_command *)buf;
1507 skb = (struct sk_buff *)sc->callback_arg;
1508 finfo = (struct octnet_buf_free_info *)&skb->cb;
1509
1510 lio = finfo->lio;
1511 g = finfo->g;
1512 frags = skb_shinfo(skb)->nr_frags;
1513
1514 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1515 g->sg[0].ptr[0], (skb->len - skb->data_len),
1516 DMA_TO_DEVICE);
1517
1518 i = 1;
1519 while (frags--) {
1520 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1521
1522 pci_unmap_page((lio->oct_dev)->pci_dev,
1523 g->sg[(i >> 2)].ptr[(i & 3)],
1524 frag->size, DMA_TO_DEVICE);
1525 i++;
1526 }
1527
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001528 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1529 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001530
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001531 iq = skb_iq(lio, skb);
1532
1533 spin_lock(&lio->glist_lock[iq]);
1534 list_add_tail(&g->list, &lio->glist[iq]);
1535 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001536
1537 /* Don't free the skb yet */
1538
1539 check_txq_state(lio, skb);
1540}
1541
1542/**
1543 * \brief Adjust ptp frequency
1544 * @param ptp PTP clock info
1545 * @param ppb how much to adjust by, in parts-per-billion
1546 */
1547static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1548{
1549 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1550 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1551 u64 comp, delta;
1552 unsigned long flags;
1553 bool neg_adj = false;
1554
1555 if (ppb < 0) {
1556 neg_adj = true;
1557 ppb = -ppb;
1558 }
1559
1560 /* The hardware adds the clock compensation value to the
1561 * PTP clock on every coprocessor clock cycle, so we
1562 * compute the delta in terms of coprocessor clocks.
1563 */
1564 delta = (u64)ppb << 32;
1565 do_div(delta, oct->coproc_clock_rate);
1566
1567 spin_lock_irqsave(&lio->ptp_lock, flags);
1568 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1569 if (neg_adj)
1570 comp -= delta;
1571 else
1572 comp += delta;
1573 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1574 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1575
1576 return 0;
1577}
1578
1579/**
1580 * \brief Adjust ptp time
1581 * @param ptp PTP clock info
1582 * @param delta how much to adjust by, in nanosecs
1583 */
1584static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1585{
1586 unsigned long flags;
1587 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1588
1589 spin_lock_irqsave(&lio->ptp_lock, flags);
1590 lio->ptp_adjust += delta;
1591 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1592
1593 return 0;
1594}
1595
1596/**
1597 * \brief Get hardware clock time, including any adjustment
1598 * @param ptp PTP clock info
1599 * @param ts timespec
1600 */
1601static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1602 struct timespec64 *ts)
1603{
1604 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001605 unsigned long flags;
1606 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1607 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1608
1609 spin_lock_irqsave(&lio->ptp_lock, flags);
1610 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1611 ns += lio->ptp_adjust;
1612 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1613
Kefeng Wang286af312016-01-27 17:34:37 +08001614 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001615
1616 return 0;
1617}
1618
1619/**
1620 * \brief Set hardware clock time. Reset adjustment
1621 * @param ptp PTP clock info
1622 * @param ts timespec
1623 */
1624static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1625 const struct timespec64 *ts)
1626{
1627 u64 ns;
1628 unsigned long flags;
1629 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1630 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1631
1632 ns = timespec_to_ns(ts);
1633
1634 spin_lock_irqsave(&lio->ptp_lock, flags);
1635 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1636 lio->ptp_adjust = 0;
1637 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1638
1639 return 0;
1640}
1641
1642/**
1643 * \brief Check if PTP is enabled
1644 * @param ptp PTP clock info
1645 * @param rq request
1646 * @param on is it on
1647 */
1648static int liquidio_ptp_enable(struct ptp_clock_info *ptp,
1649 struct ptp_clock_request *rq, int on)
1650{
1651 return -EOPNOTSUPP;
1652}
1653
1654/**
1655 * \brief Open PTP clock source
1656 * @param netdev network device
1657 */
1658static void oct_ptp_open(struct net_device *netdev)
1659{
1660 struct lio *lio = GET_LIO(netdev);
1661 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1662
1663 spin_lock_init(&lio->ptp_lock);
1664
1665 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1666 lio->ptp_info.owner = THIS_MODULE;
1667 lio->ptp_info.max_adj = 250000000;
1668 lio->ptp_info.n_alarm = 0;
1669 lio->ptp_info.n_ext_ts = 0;
1670 lio->ptp_info.n_per_out = 0;
1671 lio->ptp_info.pps = 0;
1672 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1673 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1674 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1675 lio->ptp_info.settime64 = liquidio_ptp_settime;
1676 lio->ptp_info.enable = liquidio_ptp_enable;
1677
1678 lio->ptp_adjust = 0;
1679
1680 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1681 &oct->pci_dev->dev);
1682
1683 if (IS_ERR(lio->ptp_clock))
1684 lio->ptp_clock = NULL;
1685}
1686
1687/**
1688 * \brief Init PTP clock
1689 * @param oct octeon device
1690 */
1691static void liquidio_ptp_init(struct octeon_device *oct)
1692{
1693 u64 clock_comp, cfg;
1694
1695 clock_comp = (u64)NSEC_PER_SEC << 32;
1696 do_div(clock_comp, oct->coproc_clock_rate);
1697 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1698
1699 /* Enable */
1700 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1701 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1702}
1703
1704/**
1705 * \brief Load firmware to device
1706 * @param oct octeon device
1707 *
1708 * Maps device to firmware filename, requests firmware, and downloads it
1709 */
1710static int load_firmware(struct octeon_device *oct)
1711{
1712 int ret = 0;
1713 const struct firmware *fw;
1714 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1715 char *tmp_fw_type;
1716
1717 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1718 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1719 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1720 return ret;
1721 }
1722
1723 if (fw_type[0] == '\0')
1724 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1725 else
1726 tmp_fw_type = fw_type;
1727
1728 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1729 octeon_get_conf(oct)->card_name, tmp_fw_type,
1730 LIO_FW_NAME_SUFFIX);
1731
1732 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1733 if (ret) {
1734 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1735 fw_name);
1736 return ret;
1737 }
1738
1739 ret = octeon_download_firmware(oct, fw->data, fw->size);
1740
1741 release_firmware(fw);
1742
1743 return ret;
1744}
1745
1746/**
1747 * \brief Setup output queue
1748 * @param oct octeon device
1749 * @param q_no which queue
1750 * @param num_descs how many descriptors
1751 * @param desc_size size of each descriptor
1752 * @param app_ctx application context
1753 */
1754static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
1755 int desc_size, void *app_ctx)
1756{
1757 int ret_val = 0;
1758
1759 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
1760 /* droq creation and local register settings. */
1761 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301762 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001763 return ret_val;
1764
1765 if (ret_val == 1) {
1766 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
1767 return 0;
1768 }
1769 /* tasklet creation for the droq */
1770
1771 /* Enable the droq queues */
1772 octeon_set_droq_pkt_op(oct, q_no, 1);
1773
1774 /* Send Credit for Octeon Output queues. Credits are always
1775 * sent after the output queue is enabled.
1776 */
1777 writel(oct->droq[q_no]->max_count,
1778 oct->droq[q_no]->pkts_credit_reg);
1779
1780 return ret_val;
1781}
1782
1783/**
1784 * \brief Callback for getting interface configuration
1785 * @param status status of request
1786 * @param buf pointer to resp structure
1787 */
1788static void if_cfg_callback(struct octeon_device *oct,
1789 u32 status,
1790 void *buf)
1791{
1792 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1793 struct liquidio_if_cfg_resp *resp;
1794 struct liquidio_if_cfg_context *ctx;
1795
1796 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1797 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1798
1799 oct = lio_get_device(ctx->octeon_id);
1800 if (resp->status)
1801 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1802 CVM_CAST64(resp->status));
1803 ACCESS_ONCE(ctx->cond) = 1;
1804
1805 /* This barrier is required to be sure that the response has been
1806 * written fully before waking up the handler
1807 */
1808 wmb();
1809
1810 wake_up_interruptible(&ctx->wc);
1811}
1812
1813/**
1814 * \brief Select queue based on hash
1815 * @param dev Net device
1816 * @param skb sk_buff structure
1817 * @returns selected queue number
1818 */
1819static u16 select_q(struct net_device *dev, struct sk_buff *skb,
1820 void *accel_priv, select_queue_fallback_t fallback)
1821{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001822 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001823 struct lio *lio;
1824
1825 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001826 qindex = skb_tx_hash(dev, skb);
1827
1828 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001829}
1830
1831/** Routine to push packets arriving on Octeon interface upto network layer.
1832 * @param oct_id - octeon device id.
1833 * @param skbuff - skbuff struct to be passed to network layer.
1834 * @param len - size of total data received.
1835 * @param rh - Control header associated with the packet
1836 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001837 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001838 */
1839static void
1840liquidio_push_packet(u32 octeon_id,
1841 void *skbuff,
1842 u32 len,
1843 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001844 void *param,
1845 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001846{
1847 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001848 struct sk_buff *skb = (struct sk_buff *)skbuff;
1849 struct skb_shared_hwtstamps *shhwtstamps;
1850 u64 ns;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001851 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001852 struct octeon_droq *droq = container_of(param, struct octeon_droq,
1853 napi);
1854 if (netdev) {
1855 int packet_was_received;
1856 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001857 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001858
1859 /* Do not proceed if the interface is not in RUNNING state. */
1860 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1861 recv_buffer_free(skb);
1862 droq->stats.rx_dropped++;
1863 return;
1864 }
1865
1866 skb->dev = netdev;
1867
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001868 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001869 if (likely(len > MIN_SKB_SIZE)) {
1870 struct octeon_skb_page_info *pg_info;
1871 unsigned char *va;
1872
1873 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
1874 if (pg_info->page) {
1875 /* For Paged allocation use the frags */
1876 va = page_address(pg_info->page) +
1877 pg_info->page_offset;
1878 memcpy(skb->data, va, MIN_SKB_SIZE);
1879 skb_put(skb, MIN_SKB_SIZE);
1880 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1881 pg_info->page,
1882 pg_info->page_offset +
1883 MIN_SKB_SIZE,
1884 len - MIN_SKB_SIZE,
1885 LIO_RXBUFFER_SZ);
1886 }
1887 } else {
1888 struct octeon_skb_page_info *pg_info =
1889 ((struct octeon_skb_page_info *)(skb->cb));
1890 skb_copy_to_linear_data(skb, page_address(pg_info->page)
1891 + pg_info->page_offset, len);
1892 skb_put(skb, len);
1893 put_page(pg_info->page);
1894 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001895
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001896 if (((oct->chip_id == OCTEON_CN66XX) ||
1897 (oct->chip_id == OCTEON_CN68XX)) &&
1898 ptp_enable) {
1899 if (rh->r_dh.has_hwtstamp) {
1900 /* timestamp is included from the hardware at
1901 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001902 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001903 if (ifstate_check
1904 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
1905 /* Nanoseconds are in the first 64-bits
1906 * of the packet.
1907 */
1908 memcpy(&ns, (skb->data), sizeof(ns));
1909 shhwtstamps = skb_hwtstamps(skb);
1910 shhwtstamps->hwtstamp =
1911 ns_to_ktime(ns +
1912 lio->ptp_adjust);
1913 }
1914 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001915 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001916 }
1917
1918 skb->protocol = eth_type_trans(skb, skb->dev);
1919
1920 if ((netdev->features & NETIF_F_RXCSUM) &&
1921 (rh->r_dh.csum_verified == CNNIC_CSUM_VERIFIED))
1922 /* checksum has already been verified */
1923 skb->ip_summed = CHECKSUM_UNNECESSARY;
1924 else
1925 skb->ip_summed = CHECKSUM_NONE;
1926
1927 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
1928
1929 if (packet_was_received) {
1930 droq->stats.rx_bytes_received += len;
1931 droq->stats.rx_pkts_received++;
1932 netdev->last_rx = jiffies;
1933 } else {
1934 droq->stats.rx_dropped++;
1935 netif_info(lio, rx_err, lio->netdev,
1936 "droq:%d error rx_dropped:%llu\n",
1937 droq->q_no, droq->stats.rx_dropped);
1938 }
1939
1940 } else {
1941 recv_buffer_free(skb);
1942 }
1943}
1944
1945/**
1946 * \brief wrapper for calling napi_schedule
1947 * @param param parameters to pass to napi_schedule
1948 *
1949 * Used when scheduling on different CPUs
1950 */
1951static void napi_schedule_wrapper(void *param)
1952{
1953 struct napi_struct *napi = param;
1954
1955 napi_schedule(napi);
1956}
1957
1958/**
1959 * \brief callback when receive interrupt occurs and we are in NAPI mode
1960 * @param arg pointer to octeon output queue
1961 */
1962static void liquidio_napi_drv_callback(void *arg)
1963{
1964 struct octeon_droq *droq = arg;
1965 int this_cpu = smp_processor_id();
1966
1967 if (droq->cpu_id == this_cpu) {
1968 napi_schedule(&droq->napi);
1969 } else {
1970 struct call_single_data *csd = &droq->csd;
1971
1972 csd->func = napi_schedule_wrapper;
1973 csd->info = &droq->napi;
1974 csd->flags = 0;
1975
1976 smp_call_function_single_async(droq->cpu_id, csd);
1977 }
1978}
1979
1980/**
1981 * \brief Main NAPI poll function
1982 * @param droq octeon output queue
1983 * @param budget maximum number of items to process
1984 */
1985static int liquidio_napi_do_rx(struct octeon_droq *droq, int budget)
1986{
1987 int work_done;
1988 struct lio *lio = GET_LIO(droq->napi.dev);
1989 struct octeon_device *oct = lio->oct_dev;
1990
1991 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
1992 POLL_EVENT_PROCESS_PKTS,
1993 budget);
1994 if (work_done < 0) {
1995 netif_info(lio, rx_err, lio->netdev,
1996 "Receive work_done < 0, rxq:%d\n", droq->q_no);
1997 goto octnet_napi_finish;
1998 }
1999
2000 if (work_done > budget)
2001 dev_err(&oct->pci_dev->dev, ">>>> %s work_done: %d budget: %d\n",
2002 __func__, work_done, budget);
2003
2004 return work_done;
2005
2006octnet_napi_finish:
2007 napi_complete(&droq->napi);
2008 octeon_process_droq_poll_cmd(oct, droq->q_no, POLL_EVENT_ENABLE_INTR,
2009 0);
2010 return 0;
2011}
2012
2013/**
2014 * \brief Entry point for NAPI polling
2015 * @param napi NAPI structure
2016 * @param budget maximum number of items to process
2017 */
2018static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2019{
2020 struct octeon_droq *droq;
2021 int work_done;
2022
2023 droq = container_of(napi, struct octeon_droq, napi);
2024
2025 work_done = liquidio_napi_do_rx(droq, budget);
2026
2027 if (work_done < budget) {
2028 napi_complete(napi);
2029 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2030 POLL_EVENT_ENABLE_INTR, 0);
2031 return 0;
2032 }
2033
2034 return work_done;
2035}
2036
2037/**
2038 * \brief Setup input and output queues
2039 * @param octeon_dev octeon device
2040 * @param net_device Net device
2041 *
2042 * Note: Queues are with respect to the octeon device. Thus
2043 * an input queue is for egress packets, and output queues
2044 * are for ingress packets.
2045 */
2046static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002047 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002048{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002049 struct octeon_droq_ops droq_ops;
2050 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002051 static int cpu_id;
2052 static int cpu_id_modulus;
2053 struct octeon_droq *droq;
2054 struct napi_struct *napi;
2055 int q, q_no, retval = 0;
2056 struct lio *lio;
2057 int num_tx_descs;
2058
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002059 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002060
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002061 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002062
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002063 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2064
2065 droq_ops.fptr = liquidio_push_packet;
2066 droq_ops.farg = (void *)netdev;
2067
2068 droq_ops.poll_mode = 1;
2069 droq_ops.napi_fn = liquidio_napi_drv_callback;
2070 cpu_id = 0;
2071 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002072
2073 /* set up DROQs. */
2074 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002075 q_no = lio->linfo.rxpciq[q].s.q_no;
2076 dev_dbg(&octeon_dev->pci_dev->dev,
2077 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2078 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002079 retval = octeon_setup_droq(octeon_dev, q_no,
2080 CFG_GET_NUM_RX_DESCS_NIC_IF
2081 (octeon_get_conf(octeon_dev),
2082 lio->ifidx),
2083 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2084 (octeon_get_conf(octeon_dev),
2085 lio->ifidx), NULL);
2086 if (retval) {
2087 dev_err(&octeon_dev->pci_dev->dev,
2088 " %s : Runtime DROQ(RxQ) creation failed.\n",
2089 __func__);
2090 return 1;
2091 }
2092
2093 droq = octeon_dev->droq[q_no];
2094 napi = &droq->napi;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002095 dev_dbg(&octeon_dev->pci_dev->dev,
2096 "netif_napi_add netdev:%llx oct:%llx\n",
2097 (u64)netdev,
2098 (u64)octeon_dev);
2099 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002100
2101 /* designate a CPU for this droq */
2102 droq->cpu_id = cpu_id;
2103 cpu_id++;
2104 if (cpu_id >= cpu_id_modulus)
2105 cpu_id = 0;
2106
2107 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2108 }
2109
2110 /* set up IQs. */
2111 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2112 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2113 (octeon_dev),
2114 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002115 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2116 lio->linfo.txpciq[q], num_tx_descs,
2117 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002118 if (retval) {
2119 dev_err(&octeon_dev->pci_dev->dev,
2120 " %s : Runtime IQ(TxQ) creation failed.\n",
2121 __func__);
2122 return 1;
2123 }
2124 }
2125
2126 return 0;
2127}
2128
2129/**
2130 * \brief Poll routine for checking transmit queue status
2131 * @param work work_struct data structure
2132 */
2133static void octnet_poll_check_txq_status(struct work_struct *work)
2134{
2135 struct cavium_wk *wk = (struct cavium_wk *)work;
2136 struct lio *lio = (struct lio *)wk->ctxptr;
2137
2138 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2139 return;
2140
2141 check_txq_status(lio);
2142 queue_delayed_work(lio->txq_status_wq.wq,
2143 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2144}
2145
2146/**
2147 * \brief Sets up the txq poll check
2148 * @param netdev network device
2149 */
2150static inline void setup_tx_poll_fn(struct net_device *netdev)
2151{
2152 struct lio *lio = GET_LIO(netdev);
2153 struct octeon_device *oct = lio->oct_dev;
2154
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302155 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2156 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002157 if (!lio->txq_status_wq.wq) {
2158 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2159 return;
2160 }
2161 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2162 octnet_poll_check_txq_status);
2163 lio->txq_status_wq.wk.ctxptr = lio;
2164 queue_delayed_work(lio->txq_status_wq.wq,
2165 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2166}
2167
2168/**
2169 * \brief Net device open for LiquidIO
2170 * @param netdev network device
2171 */
2172static int liquidio_open(struct net_device *netdev)
2173{
2174 struct lio *lio = GET_LIO(netdev);
2175 struct octeon_device *oct = lio->oct_dev;
2176 struct napi_struct *napi, *n;
2177
2178 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2179 napi_enable(napi);
2180
2181 oct_ptp_open(netdev);
2182
2183 ifstate_set(lio, LIO_IFSTATE_RUNNING);
2184 setup_tx_poll_fn(netdev);
2185 start_txq(netdev);
2186
2187 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2188 try_module_get(THIS_MODULE);
2189
2190 /* tell Octeon to start forwarding packets to host */
2191 send_rx_ctrl_cmd(lio, 1);
2192
2193 /* Ready for link status updates */
2194 lio->intf_open = 1;
2195
2196 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2197 netdev->name);
2198
2199 return 0;
2200}
2201
2202/**
2203 * \brief Net device stop for LiquidIO
2204 * @param netdev network device
2205 */
2206static int liquidio_stop(struct net_device *netdev)
2207{
2208 struct napi_struct *napi, *n;
2209 struct lio *lio = GET_LIO(netdev);
2210 struct octeon_device *oct = lio->oct_dev;
2211
2212 netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
2213 /* Inform that netif carrier is down */
2214 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002215 lio->linfo.link.s.link_up = 0;
2216 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002217
2218 netif_carrier_off(netdev);
2219
2220 /* tell Octeon to stop forwarding packets to host */
2221 send_rx_ctrl_cmd(lio, 0);
2222
2223 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002224 destroy_workqueue(lio->txq_status_wq.wq);
2225
2226 if (lio->ptp_clock) {
2227 ptp_clock_unregister(lio->ptp_clock);
2228 lio->ptp_clock = NULL;
2229 }
2230
2231 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2232
2233 /* This is a hack that allows DHCP to continue working. */
2234 set_bit(__LINK_STATE_START, &lio->netdev->state);
2235
2236 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2237 napi_disable(napi);
2238
2239 txqs_stop(netdev);
2240
2241 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2242 module_put(THIS_MODULE);
2243
2244 return 0;
2245}
2246
2247void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr)
2248{
2249 struct octnic_ctrl_pkt *nctrl = (struct octnic_ctrl_pkt *)nctrl_ptr;
2250 struct net_device *netdev = (struct net_device *)nctrl->netpndev;
2251 struct lio *lio = GET_LIO(netdev);
2252 struct octeon_device *oct = lio->oct_dev;
2253
2254 switch (nctrl->ncmd.s.cmd) {
2255 case OCTNET_CMD_CHANGE_DEVFLAGS:
2256 case OCTNET_CMD_SET_MULTI_LIST:
2257 break;
2258
2259 case OCTNET_CMD_CHANGE_MACADDR:
2260 /* If command is successful, change the MACADDR. */
2261 netif_info(lio, probe, lio->netdev, " MACAddr changed to 0x%llx\n",
2262 CVM_CAST64(nctrl->udd[0]));
2263 dev_info(&oct->pci_dev->dev, "%s MACAddr changed to 0x%llx\n",
2264 netdev->name, CVM_CAST64(nctrl->udd[0]));
2265 memcpy(netdev->dev_addr, ((u8 *)&nctrl->udd[0]) + 2, ETH_ALEN);
2266 break;
2267
2268 case OCTNET_CMD_CHANGE_MTU:
2269 /* If command is successful, change the MTU. */
2270 netif_info(lio, probe, lio->netdev, " MTU Changed from %d to %d\n",
2271 netdev->mtu, nctrl->ncmd.s.param2);
2272 dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
2273 netdev->name, netdev->mtu,
2274 nctrl->ncmd.s.param2);
2275 netdev->mtu = nctrl->ncmd.s.param2;
2276 break;
2277
2278 case OCTNET_CMD_GPIO_ACCESS:
2279 netif_info(lio, probe, lio->netdev, "LED Flashing visual identification\n");
2280
2281 break;
2282
2283 case OCTNET_CMD_LRO_ENABLE:
2284 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2285 break;
2286
2287 case OCTNET_CMD_LRO_DISABLE:
2288 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2289 netdev->name);
2290 break;
2291
2292 case OCTNET_CMD_VERBOSE_ENABLE:
2293 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2294 break;
2295
2296 case OCTNET_CMD_VERBOSE_DISABLE:
2297 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2298 netdev->name);
2299 break;
2300
2301 case OCTNET_CMD_SET_SETTINGS:
2302 dev_info(&oct->pci_dev->dev, "%s settings changed\n",
2303 netdev->name);
2304
2305 break;
2306
2307 default:
2308 dev_err(&oct->pci_dev->dev, "%s Unknown cmd %d\n", __func__,
2309 nctrl->ncmd.s.cmd);
2310 }
2311}
2312
2313/**
2314 * \brief Converts a mask based on net device flags
2315 * @param netdev network device
2316 *
2317 * This routine generates a octnet_ifflags mask from the net device flags
2318 * received from the OS.
2319 */
2320static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2321{
2322 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2323
2324 if (netdev->flags & IFF_PROMISC)
2325 f |= OCTNET_IFFLAG_PROMISC;
2326
2327 if (netdev->flags & IFF_ALLMULTI)
2328 f |= OCTNET_IFFLAG_ALLMULTI;
2329
2330 if (netdev->flags & IFF_MULTICAST) {
2331 f |= OCTNET_IFFLAG_MULTICAST;
2332
2333 /* Accept all multicast addresses if there are more than we
2334 * can handle
2335 */
2336 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2337 f |= OCTNET_IFFLAG_ALLMULTI;
2338 }
2339
2340 if (netdev->flags & IFF_BROADCAST)
2341 f |= OCTNET_IFFLAG_BROADCAST;
2342
2343 return f;
2344}
2345
2346/**
2347 * \brief Net device set_multicast_list
2348 * @param netdev network device
2349 */
2350static void liquidio_set_mcast_list(struct net_device *netdev)
2351{
2352 struct lio *lio = GET_LIO(netdev);
2353 struct octeon_device *oct = lio->oct_dev;
2354 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002355 struct netdev_hw_addr *ha;
2356 u64 *mc;
2357 int ret, i;
2358 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2359
2360 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2361
2362 /* Create a ctrl pkt command to be sent to core app. */
2363 nctrl.ncmd.u64 = 0;
2364 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002365 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2366 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002367 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002368 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002369 nctrl.netpndev = (u64)netdev;
2370 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2371
2372 /* copy all the addresses into the udd */
2373 i = 0;
2374 mc = &nctrl.udd[0];
2375 netdev_for_each_mc_addr(ha, netdev) {
2376 *mc = 0;
2377 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2378 /* no need to swap bytes */
2379
2380 if (++mc > &nctrl.udd[mc_count])
2381 break;
2382 }
2383
2384 /* Apparently, any activity in this call from the kernel has to
2385 * be atomic. So we won't wait for response.
2386 */
2387 nctrl.wait_time = 0;
2388
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002389 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002390 if (ret < 0) {
2391 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2392 ret);
2393 }
2394}
2395
2396/**
2397 * \brief Net device set_mac_address
2398 * @param netdev network device
2399 */
2400static int liquidio_set_mac(struct net_device *netdev, void *p)
2401{
2402 int ret = 0;
2403 struct lio *lio = GET_LIO(netdev);
2404 struct octeon_device *oct = lio->oct_dev;
2405 struct sockaddr *addr = (struct sockaddr *)p;
2406 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002407
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002408 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002409 return -EADDRNOTAVAIL;
2410
2411 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2412
2413 nctrl.ncmd.u64 = 0;
2414 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002415 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002416 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002417 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002418 nctrl.netpndev = (u64)netdev;
2419 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2420 nctrl.wait_time = 100;
2421
2422 nctrl.udd[0] = 0;
2423 /* The MAC Address is presented in network byte order. */
2424 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2425
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002426 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002427 if (ret < 0) {
2428 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2429 return -ENOMEM;
2430 }
2431 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2432 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2433
2434 return 0;
2435}
2436
2437/**
2438 * \brief Net device get_stats
2439 * @param netdev network device
2440 */
2441static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2442{
2443 struct lio *lio = GET_LIO(netdev);
2444 struct net_device_stats *stats = &netdev->stats;
2445 struct octeon_device *oct;
2446 u64 pkts = 0, drop = 0, bytes = 0;
2447 struct oct_droq_stats *oq_stats;
2448 struct oct_iq_stats *iq_stats;
2449 int i, iq_no, oq_no;
2450
2451 oct = lio->oct_dev;
2452
2453 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002454 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002455 iq_stats = &oct->instr_queue[iq_no]->stats;
2456 pkts += iq_stats->tx_done;
2457 drop += iq_stats->tx_dropped;
2458 bytes += iq_stats->tx_tot_bytes;
2459 }
2460
2461 stats->tx_packets = pkts;
2462 stats->tx_bytes = bytes;
2463 stats->tx_dropped = drop;
2464
2465 pkts = 0;
2466 drop = 0;
2467 bytes = 0;
2468
2469 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002470 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002471 oq_stats = &oct->droq[oq_no]->stats;
2472 pkts += oq_stats->rx_pkts_received;
2473 drop += (oq_stats->rx_dropped +
2474 oq_stats->dropped_nodispatch +
2475 oq_stats->dropped_toomany +
2476 oq_stats->dropped_nomem);
2477 bytes += oq_stats->rx_bytes_received;
2478 }
2479
2480 stats->rx_bytes = bytes;
2481 stats->rx_packets = pkts;
2482 stats->rx_dropped = drop;
2483
2484 return stats;
2485}
2486
2487/**
2488 * \brief Net device change_mtu
2489 * @param netdev network device
2490 */
2491static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2492{
2493 struct lio *lio = GET_LIO(netdev);
2494 struct octeon_device *oct = lio->oct_dev;
2495 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002496 int max_frm_size = new_mtu + OCTNET_FRM_HEADER_SIZE;
2497 int ret = 0;
2498
2499 /* Limit the MTU to make sure the ethernet packets are between 64 bytes
2500 * and 65535 bytes
2501 */
2502 if ((max_frm_size < OCTNET_MIN_FRM_SIZE) ||
2503 (max_frm_size > OCTNET_MAX_FRM_SIZE)) {
2504 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2505 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
2506 (OCTNET_MIN_FRM_SIZE - OCTNET_FRM_HEADER_SIZE),
2507 (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE));
2508 return -EINVAL;
2509 }
2510
2511 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2512
2513 nctrl.ncmd.u64 = 0;
2514 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002515 nctrl.ncmd.s.param1 = new_mtu;
2516 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002517 nctrl.wait_time = 100;
2518 nctrl.netpndev = (u64)netdev;
2519 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2520
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002521 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002522 if (ret < 0) {
2523 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2524 return -1;
2525 }
2526
2527 lio->mtu = new_mtu;
2528
2529 return 0;
2530}
2531
2532/**
2533 * \brief Handler for SIOCSHWTSTAMP ioctl
2534 * @param netdev network device
2535 * @param ifr interface request
2536 * @param cmd command
2537 */
2538static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2539{
2540 struct hwtstamp_config conf;
2541 struct lio *lio = GET_LIO(netdev);
2542
2543 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2544 return -EFAULT;
2545
2546 if (conf.flags)
2547 return -EINVAL;
2548
2549 switch (conf.tx_type) {
2550 case HWTSTAMP_TX_ON:
2551 case HWTSTAMP_TX_OFF:
2552 break;
2553 default:
2554 return -ERANGE;
2555 }
2556
2557 switch (conf.rx_filter) {
2558 case HWTSTAMP_FILTER_NONE:
2559 break;
2560 case HWTSTAMP_FILTER_ALL:
2561 case HWTSTAMP_FILTER_SOME:
2562 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2563 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2564 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2565 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2566 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2567 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2568 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2569 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2570 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2571 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2572 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2573 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2574 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2575 break;
2576 default:
2577 return -ERANGE;
2578 }
2579
2580 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2581 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2582
2583 else
2584 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2585
2586 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2587}
2588
2589/**
2590 * \brief ioctl handler
2591 * @param netdev network device
2592 * @param ifr interface request
2593 * @param cmd command
2594 */
2595static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2596{
2597 switch (cmd) {
2598 case SIOCSHWTSTAMP:
2599 return hwtstamp_ioctl(netdev, ifr, cmd);
2600 default:
2601 return -EOPNOTSUPP;
2602 }
2603}
2604
2605/**
2606 * \brief handle a Tx timestamp response
2607 * @param status response status
2608 * @param buf pointer to skb
2609 */
2610static void handle_timestamp(struct octeon_device *oct,
2611 u32 status,
2612 void *buf)
2613{
2614 struct octnet_buf_free_info *finfo;
2615 struct octeon_soft_command *sc;
2616 struct oct_timestamp_resp *resp;
2617 struct lio *lio;
2618 struct sk_buff *skb = (struct sk_buff *)buf;
2619
2620 finfo = (struct octnet_buf_free_info *)skb->cb;
2621 lio = finfo->lio;
2622 sc = finfo->sc;
2623 oct = lio->oct_dev;
2624 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2625
2626 if (status != OCTEON_REQUEST_DONE) {
2627 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2628 CVM_CAST64(status));
2629 resp->timestamp = 0;
2630 }
2631
2632 octeon_swap_8B_data(&resp->timestamp, 1);
2633
Colin Ian King19a6d152016-02-05 16:30:39 +00002634 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002635 struct skb_shared_hwtstamps ts;
2636 u64 ns = resp->timestamp;
2637
2638 netif_info(lio, tx_done, lio->netdev,
2639 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2640 skb, (unsigned long long)ns);
2641 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2642 skb_tstamp_tx(skb, &ts);
2643 }
2644
2645 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002646 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002647}
2648
2649/* \brief Send a data packet that will be timestamped
2650 * @param oct octeon device
2651 * @param ndata pointer to network data
2652 * @param finfo pointer to private network data
2653 */
2654static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2655 struct octnic_data_pkt *ndata,
2656 struct octnet_buf_free_info *finfo,
2657 int xmit_more)
2658{
2659 int retval;
2660 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002661 struct lio *lio;
2662 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002663 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002664
2665 lio = finfo->lio;
2666
2667 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2668 sizeof(struct oct_timestamp_resp));
2669 finfo->sc = sc;
2670
2671 if (!sc) {
2672 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2673 return IQ_SEND_FAILED;
2674 }
2675
2676 if (ndata->reqtype == REQTYPE_NORESP_NET)
2677 ndata->reqtype = REQTYPE_RESP_NET;
2678 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2679 ndata->reqtype = REQTYPE_RESP_NET_SG;
2680
2681 sc->callback = handle_timestamp;
2682 sc->callback_arg = finfo->skb;
2683 sc->iq_no = ndata->q_no;
2684
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002685 len = (u32)((struct octeon_instr_ih2 *)(&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002686
2687 ring_doorbell = !xmit_more;
2688 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002689 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002690
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002691 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002692 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2693 retval);
2694 octeon_free_soft_command(oct, sc);
2695 } else {
2696 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2697 }
2698
2699 return retval;
2700}
2701
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002702/** \brief Transmit networks packets to the Octeon interface
2703 * @param skbuff skbuff struct to be passed to network layer.
2704 * @param netdev pointer to network device
2705 * @returns whether the packet was transmitted to the device okay or not
2706 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2707 */
2708static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2709{
2710 struct lio *lio;
2711 struct octnet_buf_free_info *finfo;
2712 union octnic_cmd_setup cmdsetup;
2713 struct octnic_data_pkt ndata;
2714 struct octeon_device *oct;
2715 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002716 struct octeon_instr_irh *irh;
2717 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002718 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002719 int q_idx = 0, iq_no = 0;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002720 int xmit_more, j;
2721 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002722 u32 tag = 0;
2723
2724 lio = GET_LIO(netdev);
2725 oct = lio->oct_dev;
2726
2727 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002728 q_idx = skb->queue_mapping;
2729 q_idx = (q_idx % (lio->linfo.num_txpciq));
2730 tag = q_idx;
2731 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002732 } else {
2733 iq_no = lio->txq;
2734 }
2735
2736 stats = &oct->instr_queue[iq_no]->stats;
2737
2738 /* Check for all conditions in which the current packet cannot be
2739 * transmitted.
2740 */
2741 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002742 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002743 (skb->len <= 0)) {
2744 netif_info(lio, tx_err, lio->netdev,
2745 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002746 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002747 goto lio_xmit_failed;
2748 }
2749
2750 /* Use space in skb->cb to store info used to unmap and
2751 * free the buffers.
2752 */
2753 finfo = (struct octnet_buf_free_info *)skb->cb;
2754 finfo->lio = lio;
2755 finfo->skb = skb;
2756 finfo->sc = NULL;
2757
2758 /* Prepare the attributes for the data to be passed to OSI. */
2759 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2760
2761 ndata.buf = (void *)finfo;
2762
2763 ndata.q_no = iq_no;
2764
2765 if (netif_is_multiqueue(netdev)) {
2766 if (octnet_iq_is_full(oct, ndata.q_no)) {
2767 /* defer sending if queue is full */
2768 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2769 ndata.q_no);
2770 stats->tx_iq_busy++;
2771 return NETDEV_TX_BUSY;
2772 }
2773 } else {
2774 if (octnet_iq_is_full(oct, lio->txq)) {
2775 /* defer sending if queue is full */
2776 stats->tx_iq_busy++;
2777 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2778 ndata.q_no);
2779 return NETDEV_TX_BUSY;
2780 }
2781 }
2782 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
2783 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no );
2784 */
2785
2786 ndata.datasize = skb->len;
2787
2788 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002789 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002790
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002791 if (skb->ip_summed == CHECKSUM_PARTIAL)
2792 cmdsetup.s.transport_csum = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002793
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002794 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2795 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2796 cmdsetup.s.timestamp = 1;
2797 }
2798
2799 if (skb_shinfo(skb)->nr_frags == 0) {
2800 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002801 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002803 dptr = dma_map_single(&oct->pci_dev->dev,
2804 skb->data,
2805 skb->len,
2806 DMA_TO_DEVICE);
2807 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002808 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2809 __func__);
2810 return NETDEV_TX_BUSY;
2811 }
2812
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002813 ndata.cmd.cmd2.dptr = dptr;
2814 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002815 ndata.reqtype = REQTYPE_NORESP_NET;
2816
2817 } else {
2818 int i, frags;
2819 struct skb_frag_struct *frag;
2820 struct octnic_gather *g;
2821
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002822 spin_lock(&lio->glist_lock[q_idx]);
2823 g = (struct octnic_gather *)
2824 list_delete_head(&lio->glist[q_idx]);
2825 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002826
2827 if (!g) {
2828 netif_info(lio, tx_err, lio->netdev,
2829 "Transmit scatter gather: glist null!\n");
2830 goto lio_xmit_failed;
2831 }
2832
2833 cmdsetup.s.gather = 1;
2834 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002835 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002836
2837 memset(g->sg, 0, g->sg_size);
2838
2839 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2840 skb->data,
2841 (skb->len - skb->data_len),
2842 DMA_TO_DEVICE);
2843 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2844 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2845 __func__);
2846 return NETDEV_TX_BUSY;
2847 }
2848 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2849
2850 frags = skb_shinfo(skb)->nr_frags;
2851 i = 1;
2852 while (frags--) {
2853 frag = &skb_shinfo(skb)->frags[i - 1];
2854
2855 g->sg[(i >> 2)].ptr[(i & 3)] =
2856 dma_map_page(&oct->pci_dev->dev,
2857 frag->page.p,
2858 frag->page_offset,
2859 frag->size,
2860 DMA_TO_DEVICE);
2861
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002862 if (dma_mapping_error(&oct->pci_dev->dev,
2863 g->sg[i >> 2].ptr[i & 3])) {
2864 dma_unmap_single(&oct->pci_dev->dev,
2865 g->sg[0].ptr[0],
2866 skb->len - skb->data_len,
2867 DMA_TO_DEVICE);
2868 for (j = 1; j < i; j++) {
2869 frag = &skb_shinfo(skb)->frags[j - 1];
2870 dma_unmap_page(&oct->pci_dev->dev,
2871 g->sg[j >> 2].ptr[j & 3],
2872 frag->size,
2873 DMA_TO_DEVICE);
2874 }
2875 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2876 __func__);
2877 return NETDEV_TX_BUSY;
2878 }
2879
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002880 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2881 i++;
2882 }
2883
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002884 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
2885 g->sg_size, DMA_TO_DEVICE);
2886 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002887
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002888 ndata.cmd.cmd2.dptr = dptr;
2889 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002890 finfo->g = g;
2891
2892 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2893 }
2894
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002895 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2896 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002897
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002898 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002899 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2900 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
2901 }
2902
2903 xmit_more = skb->xmit_more;
2904
2905 if (unlikely(cmdsetup.s.timestamp))
2906 status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
2907 else
2908 status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
2909 if (status == IQ_SEND_FAILED)
2910 goto lio_xmit_failed;
2911
2912 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2913
2914 if (status == IQ_SEND_STOP)
2915 stop_q(lio->netdev, q_idx);
2916
Florian Westphal860e9532016-05-03 16:33:13 +02002917 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002918
2919 stats->tx_done++;
2920 stats->tx_tot_bytes += skb->len;
2921
2922 return NETDEV_TX_OK;
2923
2924lio_xmit_failed:
2925 stats->tx_dropped++;
2926 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2927 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002928 if (dptr)
2929 dma_unmap_single(&oct->pci_dev->dev, dptr,
2930 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002931 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002932 return NETDEV_TX_OK;
2933}
2934
2935/** \brief Network device Tx timeout
2936 * @param netdev pointer to network device
2937 */
2938static void liquidio_tx_timeout(struct net_device *netdev)
2939{
2940 struct lio *lio;
2941
2942 lio = GET_LIO(netdev);
2943
2944 netif_info(lio, tx_err, lio->netdev,
2945 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2946 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002947 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002948 txqs_wake(netdev);
2949}
2950
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002951int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002952{
2953 struct lio *lio = GET_LIO(netdev);
2954 struct octeon_device *oct = lio->oct_dev;
2955 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002956 int ret = 0;
2957
2958 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2959
2960 nctrl.ncmd.u64 = 0;
2961 nctrl.ncmd.s.cmd = cmd;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002962 nctrl.ncmd.s.param1 = param1;
2963 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002964 nctrl.wait_time = 100;
2965 nctrl.netpndev = (u64)netdev;
2966 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2967
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002968 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002969 if (ret < 0) {
2970 dev_err(&oct->pci_dev->dev, "Feature change failed in core (ret: 0x%x)\n",
2971 ret);
2972 }
2973 return ret;
2974}
2975
2976/** \brief Net device fix features
2977 * @param netdev pointer to network device
2978 * @param request features requested
2979 * @returns updated features list
2980 */
2981static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2982 netdev_features_t request)
2983{
2984 struct lio *lio = netdev_priv(netdev);
2985
2986 if ((request & NETIF_F_RXCSUM) &&
2987 !(lio->dev_capability & NETIF_F_RXCSUM))
2988 request &= ~NETIF_F_RXCSUM;
2989
2990 if ((request & NETIF_F_HW_CSUM) &&
2991 !(lio->dev_capability & NETIF_F_HW_CSUM))
2992 request &= ~NETIF_F_HW_CSUM;
2993
2994 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
2995 request &= ~NETIF_F_TSO;
2996
2997 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
2998 request &= ~NETIF_F_TSO6;
2999
3000 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3001 request &= ~NETIF_F_LRO;
3002
3003 /*Disable LRO if RXCSUM is off */
3004 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3005 (lio->dev_capability & NETIF_F_LRO))
3006 request &= ~NETIF_F_LRO;
3007
3008 return request;
3009}
3010
3011/** \brief Net device set features
3012 * @param netdev pointer to network device
3013 * @param features features to enable/disable
3014 */
3015static int liquidio_set_features(struct net_device *netdev,
3016 netdev_features_t features)
3017{
3018 struct lio *lio = netdev_priv(netdev);
3019
3020 if (!((netdev->features ^ features) & NETIF_F_LRO))
3021 return 0;
3022
3023 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003024 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3025 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003026 else if (!(features & NETIF_F_LRO) &&
3027 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003028 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3029 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003030
3031 return 0;
3032}
3033
3034static struct net_device_ops lionetdevops = {
3035 .ndo_open = liquidio_open,
3036 .ndo_stop = liquidio_stop,
3037 .ndo_start_xmit = liquidio_xmit,
3038 .ndo_get_stats = liquidio_get_stats,
3039 .ndo_set_mac_address = liquidio_set_mac,
3040 .ndo_set_rx_mode = liquidio_set_mcast_list,
3041 .ndo_tx_timeout = liquidio_tx_timeout,
3042 .ndo_change_mtu = liquidio_change_mtu,
3043 .ndo_do_ioctl = liquidio_ioctl,
3044 .ndo_fix_features = liquidio_fix_features,
3045 .ndo_set_features = liquidio_set_features,
3046};
3047
3048/** \brief Entry point for the liquidio module
3049 */
3050static int __init liquidio_init(void)
3051{
3052 int i;
3053 struct handshake *hs;
3054
3055 init_completion(&first_stage);
3056
3057 octeon_init_device_list(conf_type);
3058
3059 if (liquidio_init_pci())
3060 return -EINVAL;
3061
3062 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3063
3064 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3065 hs = &handshake[i];
3066 if (hs->pci_dev) {
3067 wait_for_completion(&hs->init);
3068 if (!hs->init_ok) {
3069 /* init handshake failed */
3070 dev_err(&hs->pci_dev->dev,
3071 "Failed to init device\n");
3072 liquidio_deinit_pci();
3073 return -EIO;
3074 }
3075 }
3076 }
3077
3078 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3079 hs = &handshake[i];
3080 if (hs->pci_dev) {
3081 wait_for_completion_timeout(&hs->started,
3082 msecs_to_jiffies(30000));
3083 if (!hs->started_ok) {
3084 /* starter handshake failed */
3085 dev_err(&hs->pci_dev->dev,
3086 "Firmware failed to start\n");
3087 liquidio_deinit_pci();
3088 return -EIO;
3089 }
3090 }
3091 }
3092
3093 return 0;
3094}
3095
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003096static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003097{
3098 struct octeon_device *oct = (struct octeon_device *)buf;
3099 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003100 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003101 union oct_link_status *ls;
3102 int i;
3103
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003104 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003105 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3106 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003107 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003108 goto nic_info_err;
3109 }
3110
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003111 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003112 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3113
3114 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003115 for (i = 0; i < oct->ifcount; i++) {
3116 if (oct->props[i].gmxport == gmxport) {
3117 update_link_status(oct->props[i].netdev, ls);
3118 break;
3119 }
3120 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003121
3122nic_info_err:
3123 for (i = 0; i < recv_pkt->buffer_count; i++)
3124 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3125 octeon_free_recv_info(recv_info);
3126 return 0;
3127}
3128
3129/**
3130 * \brief Setup network interfaces
3131 * @param octeon_dev octeon device
3132 *
3133 * Called during init time for each device. It assumes the NIC
3134 * is already up and running. The link information for each
3135 * interface is passed in link_info.
3136 */
3137static int setup_nic_devices(struct octeon_device *octeon_dev)
3138{
3139 struct lio *lio = NULL;
3140 struct net_device *netdev;
3141 u8 mac[6], i, j;
3142 struct octeon_soft_command *sc;
3143 struct liquidio_if_cfg_context *ctx;
3144 struct liquidio_if_cfg_resp *resp;
3145 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003146 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003147 int num_cpus = num_online_cpus();
3148 union oct_nic_if_cfg if_cfg;
3149 unsigned int base_queue;
3150 unsigned int gmx_port_id;
3151 u32 resp_size, ctx_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003152 u32 ifidx_or_pfnum;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003153
3154 /* This is to handle link status changes */
3155 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3156 OPCODE_NIC_INFO,
3157 lio_nic_info, octeon_dev);
3158
3159 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3160 * They are handled directly.
3161 */
3162 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3163 free_netbuf);
3164
3165 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3166 free_netsgbuf);
3167
3168 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3169 free_netsgbuf_with_resp);
3170
3171 for (i = 0; i < octeon_dev->ifcount; i++) {
3172 resp_size = sizeof(struct liquidio_if_cfg_resp);
3173 ctx_size = sizeof(struct liquidio_if_cfg_context);
3174 sc = (struct octeon_soft_command *)
3175 octeon_alloc_soft_command(octeon_dev, 0,
3176 resp_size, ctx_size);
3177 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3178 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
3179
3180 num_iqueues =
3181 CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3182 num_oqueues =
3183 CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3184 base_queue =
3185 CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
3186 gmx_port_id =
3187 CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003188 ifidx_or_pfnum = i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003189 if (num_iqueues > num_cpus)
3190 num_iqueues = num_cpus;
3191 if (num_oqueues > num_cpus)
3192 num_oqueues = num_cpus;
3193 dev_dbg(&octeon_dev->pci_dev->dev,
3194 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003195 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003196 ACCESS_ONCE(ctx->cond) = 0;
3197 ctx->octeon_id = lio_get_device_id(octeon_dev);
3198 init_waitqueue_head(&ctx->wc);
3199
3200 if_cfg.u64 = 0;
3201 if_cfg.s.num_iqueues = num_iqueues;
3202 if_cfg.s.num_oqueues = num_oqueues;
3203 if_cfg.s.base_queue = base_queue;
3204 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003205
3206 sc->iq_no = 0;
3207
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003208 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003209 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003210 if_cfg.u64, 0);
3211
3212 sc->callback = if_cfg_callback;
3213 sc->callback_arg = sc;
3214 sc->wait_time = 1000;
3215
3216 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003217 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003218 dev_err(&octeon_dev->pci_dev->dev,
3219 "iq/oq config failed status: %x\n",
3220 retval);
3221 /* Soft instr is freed by driver in case of failure. */
3222 goto setup_nic_dev_fail;
3223 }
3224
3225 /* Sleep on a wait queue till the cond flag indicates that the
3226 * response arrived or timed-out.
3227 */
3228 sleep_cond(&ctx->wc, &ctx->cond);
3229 retval = resp->status;
3230 if (retval) {
3231 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3232 goto setup_nic_dev_fail;
3233 }
3234
3235 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3236 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3237
3238 num_iqueues = hweight64(resp->cfg_info.iqmask);
3239 num_oqueues = hweight64(resp->cfg_info.oqmask);
3240
3241 if (!(num_iqueues) || !(num_oqueues)) {
3242 dev_err(&octeon_dev->pci_dev->dev,
3243 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3244 resp->cfg_info.iqmask,
3245 resp->cfg_info.oqmask);
3246 goto setup_nic_dev_fail;
3247 }
3248 dev_dbg(&octeon_dev->pci_dev->dev,
3249 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3250 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3251 num_iqueues, num_oqueues);
3252 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3253
3254 if (!netdev) {
3255 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3256 goto setup_nic_dev_fail;
3257 }
3258
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003259 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003260
3261 if (num_iqueues > 1)
3262 lionetdevops.ndo_select_queue = select_q;
3263
3264 /* Associate the routines that will handle different
3265 * netdev tasks.
3266 */
3267 netdev->netdev_ops = &lionetdevops;
3268
3269 lio = GET_LIO(netdev);
3270
3271 memset(lio, 0, sizeof(struct lio));
3272
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003273 lio->ifidx = ifidx_or_pfnum;
3274
3275 props = &octeon_dev->props[i];
3276 props->gmxport = resp->cfg_info.linfo.gmxport;
3277 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003278
3279 lio->linfo.num_rxpciq = num_oqueues;
3280 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003281 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003282 lio->linfo.rxpciq[j].u64 =
3283 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003284 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003285 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003286 lio->linfo.txpciq[j].u64 =
3287 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003288 }
3289 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3290 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3291 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3292
3293 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3294
3295 lio->dev_capability = NETIF_F_HIGHDMA
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003296 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3297 | NETIF_F_SG | NETIF_F_RXCSUM
3298 | NETIF_F_GRO
3299 | NETIF_F_TSO | NETIF_F_TSO6
3300 | NETIF_F_LRO;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003301 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3302
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003303 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3304
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003305 netdev->vlan_features = lio->dev_capability;
3306
3307 netdev->hw_features = lio->dev_capability;
3308
3309 /* Point to the properties for octeon device to which this
3310 * interface belongs.
3311 */
3312 lio->oct_dev = octeon_dev;
3313 lio->octprops = props;
3314 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003315
3316 dev_dbg(&octeon_dev->pci_dev->dev,
3317 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3318 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3319
3320 /* 64-bit swap required on LE machines */
3321 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3322 for (j = 0; j < 6; j++)
3323 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3324
3325 /* Copy MAC Address to OS network device structure */
3326
3327 ether_addr_copy(netdev->dev_addr, mac);
3328
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003329 /* By default all interfaces on a single Octeon uses the same
3330 * tx and rx queues
3331 */
3332 lio->txq = lio->linfo.txpciq[0].s.q_no;
3333 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003334 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003335 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3336 goto setup_nic_dev_fail;
3337 }
3338
3339 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3340
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003341 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3342 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3343
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003344 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003345 dev_err(&octeon_dev->pci_dev->dev,
3346 "Gather list allocation failed\n");
3347 goto setup_nic_dev_fail;
3348 }
3349
3350 /* Register ethtool support */
3351 liquidio_set_ethtool_ops(netdev);
3352
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003353 if (netdev->features & NETIF_F_LRO)
3354 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3355 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003356
3357 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003358 liquidio_set_feature(netdev, OCTNET_CMD_VERBOSE_ENABLE,
3359 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003360
3361 /* Register the network device with the OS */
3362 if (register_netdev(netdev)) {
3363 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3364 goto setup_nic_dev_fail;
3365 }
3366
3367 dev_dbg(&octeon_dev->pci_dev->dev,
3368 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3369 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3370 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003371 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003372
3373 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3374
3375 dev_dbg(&octeon_dev->pci_dev->dev,
3376 "NIC ifidx:%d Setup successful\n", i);
3377
3378 octeon_free_soft_command(octeon_dev, sc);
3379 }
3380
3381 return 0;
3382
3383setup_nic_dev_fail:
3384
3385 octeon_free_soft_command(octeon_dev, sc);
3386
3387 while (i--) {
3388 dev_err(&octeon_dev->pci_dev->dev,
3389 "NIC ifidx:%d Setup failed\n", i);
3390 liquidio_destroy_nic_device(octeon_dev, i);
3391 }
3392 return -ENODEV;
3393}
3394
3395/**
3396 * \brief initialize the NIC
3397 * @param oct octeon device
3398 *
3399 * This initialization routine is called once the Octeon device application is
3400 * up and running
3401 */
3402static int liquidio_init_nic_module(struct octeon_device *oct)
3403{
3404 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003405 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003406 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3407
3408 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3409
3410 /* only default iq and oq were initialized
3411 * initialize the rest as well
3412 */
3413 /* run port_config command for each port */
3414 oct->ifcount = num_nic_ports;
3415
3416 memset(oct->props, 0,
3417 sizeof(struct octdev_props) * num_nic_ports);
3418
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003419 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3420 oct->props[i].gmxport = -1;
3421
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003422 retval = setup_nic_devices(oct);
3423 if (retval) {
3424 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3425 goto octnet_init_failure;
3426 }
3427
3428 liquidio_ptp_init(oct);
3429
3430 /* Initialize interrupt moderation params */
3431 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
3432 intrmod_cfg->intrmod_enable = 1;
3433 intrmod_cfg->intrmod_check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3434 intrmod_cfg->intrmod_maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3435 intrmod_cfg->intrmod_minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3436 intrmod_cfg->intrmod_maxcnt_trigger = LIO_INTRMOD_MAXCNT_TRIGGER;
3437 intrmod_cfg->intrmod_maxtmr_trigger = LIO_INTRMOD_MAXTMR_TRIGGER;
3438 intrmod_cfg->intrmod_mintmr_trigger = LIO_INTRMOD_MINTMR_TRIGGER;
3439 intrmod_cfg->intrmod_mincnt_trigger = LIO_INTRMOD_MINCNT_TRIGGER;
3440
3441 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3442
3443 return retval;
3444
3445octnet_init_failure:
3446
3447 oct->ifcount = 0;
3448
3449 return retval;
3450}
3451
3452/**
3453 * \brief starter callback that invokes the remaining initialization work after
3454 * the NIC is up and running.
3455 * @param octptr work struct work_struct
3456 */
3457static void nic_starter(struct work_struct *work)
3458{
3459 struct octeon_device *oct;
3460 struct cavium_wk *wk = (struct cavium_wk *)work;
3461
3462 oct = (struct octeon_device *)wk->ctxptr;
3463
3464 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3465 return;
3466
3467 /* If the status of the device is CORE_OK, the core
3468 * application has reported its application type. Call
3469 * any registered handlers now and move to the RUNNING
3470 * state.
3471 */
3472 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3473 schedule_delayed_work(&oct->nic_poll_work.work,
3474 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3475 return;
3476 }
3477
3478 atomic_set(&oct->status, OCT_DEV_RUNNING);
3479
3480 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3481 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3482
3483 if (liquidio_init_nic_module(oct))
3484 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3485 else
3486 handshake[oct->octeon_id].started_ok = 1;
3487 } else {
3488 dev_err(&oct->pci_dev->dev,
3489 "Unexpected application running on NIC (%d). Check firmware.\n",
3490 oct->app_mode);
3491 }
3492
3493 complete(&handshake[oct->octeon_id].started);
3494}
3495
3496/**
3497 * \brief Device initialization for each Octeon device that is probed
3498 * @param octeon_dev octeon device
3499 */
3500static int octeon_device_init(struct octeon_device *octeon_dev)
3501{
3502 int j, ret;
3503 struct octeon_device_priv *oct_priv =
3504 (struct octeon_device_priv *)octeon_dev->priv;
3505 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3506
3507 /* Enable access to the octeon device and make its DMA capability
3508 * known to the OS.
3509 */
3510 if (octeon_pci_os_setup(octeon_dev))
3511 return 1;
3512
3513 /* Identify the Octeon type and map the BAR address space. */
3514 if (octeon_chip_specific_setup(octeon_dev)) {
3515 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3516 return 1;
3517 }
3518
3519 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3520
3521 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3522
3523 /* Do a soft reset of the Octeon device. */
3524 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3525 return 1;
3526
3527 /* Initialize the dispatch mechanism used to push packets arriving on
3528 * Octeon Output queues.
3529 */
3530 if (octeon_init_dispatch_list(octeon_dev))
3531 return 1;
3532
3533 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3534 OPCODE_NIC_CORE_DRV_ACTIVE,
3535 octeon_core_drv_init,
3536 octeon_dev);
3537
3538 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3539 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3540 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3541 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3542
3543 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3544
3545 octeon_set_io_queues_off(octeon_dev);
3546
3547 /* Setup the data structures that manage this Octeon's Input queues. */
3548 if (octeon_setup_instr_queues(octeon_dev)) {
3549 dev_err(&octeon_dev->pci_dev->dev,
3550 "instruction queue initialization failed\n");
3551 /* On error, release any previously allocated queues */
3552 for (j = 0; j < octeon_dev->num_iqs; j++)
3553 octeon_delete_instr_queue(octeon_dev, j);
3554 return 1;
3555 }
3556 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3557
3558 /* Initialize soft command buffer pool
3559 */
3560 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3561 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3562 return 1;
3563 }
3564 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3565
3566 /* Initialize lists to manage the requests of different types that
3567 * arrive from user & kernel applications for this octeon device.
3568 */
3569 if (octeon_setup_response_list(octeon_dev)) {
3570 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3571 return 1;
3572 }
3573 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3574
3575 if (octeon_setup_output_queues(octeon_dev)) {
3576 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
3577 /* Release any previously allocated queues */
3578 for (j = 0; j < octeon_dev->num_oqs; j++)
3579 octeon_delete_droq(octeon_dev, j);
3580 }
3581
3582 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
3583
3584 /* The input and output queue registers were setup earlier (the queues
3585 * were not enabled). Any additional registers that need to be
3586 * programmed should be done now.
3587 */
3588 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3589 if (ret) {
3590 dev_err(&octeon_dev->pci_dev->dev,
3591 "Failed to configure device registers\n");
3592 return ret;
3593 }
3594
3595 /* Initialize the tasklet that handles output queue packet processing.*/
3596 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
3597 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
3598 (unsigned long)octeon_dev);
3599
3600 /* Setup the interrupt handler and record the INT SUM register address
3601 */
3602 octeon_setup_interrupt(octeon_dev);
3603
3604 /* Enable Octeon device interrupts */
3605 octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
3606
3607 /* Enable the input and output queues for this Octeon device */
3608 octeon_dev->fn_list.enable_io_queues(octeon_dev);
3609
3610 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
3611
3612 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
3613
3614 if (ddr_timeout == 0) {
3615 dev_info(&octeon_dev->pci_dev->dev,
3616 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
3617 }
3618
3619 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
3620
3621 /* Wait for the octeon to initialize DDR after the soft-reset. */
3622 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
3623 if (ret) {
3624 dev_err(&octeon_dev->pci_dev->dev,
3625 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
3626 ret);
3627 return 1;
3628 }
3629
3630 if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
3631 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
3632 return 1;
3633 }
3634
3635 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
3636 ret = octeon_init_consoles(octeon_dev);
3637 if (ret) {
3638 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
3639 return 1;
3640 }
3641 ret = octeon_add_console(octeon_dev, 0);
3642 if (ret) {
3643 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
3644 return 1;
3645 }
3646
3647 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
3648
3649 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
3650 ret = load_firmware(octeon_dev);
3651 if (ret) {
3652 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
3653 return 1;
3654 }
3655
3656 handshake[octeon_dev->octeon_id].init_ok = 1;
3657 complete(&handshake[octeon_dev->octeon_id].init);
3658
3659 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
3660
3661 /* Send Credit for Octeon Output queues. Credits are always sent after
3662 * the output queue is enabled.
3663 */
3664 for (j = 0; j < octeon_dev->num_oqs; j++)
3665 writel(octeon_dev->droq[j]->max_count,
3666 octeon_dev->droq[j]->pkts_credit_reg);
3667
3668 /* Packets can start arriving on the output queues from this point. */
3669
3670 return 0;
3671}
3672
3673/**
3674 * \brief Exits the module
3675 */
3676static void __exit liquidio_exit(void)
3677{
3678 liquidio_deinit_pci();
3679
3680 pr_info("LiquidIO network module is now unloaded\n");
3681}
3682
3683module_init(liquidio_init);
3684module_exit(liquidio_exit);