blob: 4b95dbf80747edc8aa9cab1b7fef26ec24b952f8 [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++) { */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700227 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
228 if (!(oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700229 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
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700248 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
249 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700250 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. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700399 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700400 struct octeon_instr_queue *iq;
401
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700402 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700403 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) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700975 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
976 oq_no++) {
977 if (!(oct->droq_intr & (1ULL << oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700978 continue;
979
980 droq = oct->droq[oq_no];
981
982 if (droq->ops.poll_mode) {
983 droq->ops.napi_fn(droq);
984 oct_priv->napi_mask |= (1 << oq_no);
985 } else {
986 tasklet_schedule(&oct_priv->droq_tasklet);
987 }
988 }
989 }
990}
991
992/**
993 * \brief Interrupt handler for octeon
994 * @param irq unused
995 * @param dev octeon device
996 */
997static
998irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
999{
1000 struct octeon_device *oct = (struct octeon_device *)dev;
1001 irqreturn_t ret;
1002
1003 /* Disable our interrupts for the duration of ISR */
1004 oct->fn_list.disable_interrupt(oct->chip);
1005
1006 ret = oct->fn_list.process_interrupt_regs(oct);
1007
1008 if (ret == IRQ_HANDLED)
1009 liquidio_schedule_droq_pkt_handlers(oct);
1010
1011 /* Re-enable our interrupts */
1012 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
1013 oct->fn_list.enable_interrupt(oct->chip);
1014
1015 return ret;
1016}
1017
1018/**
1019 * \brief Setup interrupt for octeon device
1020 * @param oct octeon device
1021 *
1022 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1023 */
1024static int octeon_setup_interrupt(struct octeon_device *oct)
1025{
1026 int irqret, err;
1027
1028 err = pci_enable_msi(oct->pci_dev);
1029 if (err)
1030 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1031 err);
1032 else
1033 oct->flags |= LIO_FLAG_MSI_ENABLED;
1034
1035 irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
1036 IRQF_SHARED, "octeon", oct);
1037 if (irqret) {
1038 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1039 pci_disable_msi(oct->pci_dev);
1040 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1041 irqret);
1042 return 1;
1043 }
1044
1045 return 0;
1046}
1047
1048/**
1049 * \brief PCI probe handler
1050 * @param pdev PCI device structure
1051 * @param ent unused
1052 */
1053static int liquidio_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1054{
1055 struct octeon_device *oct_dev = NULL;
1056 struct handshake *hs;
1057
1058 oct_dev = octeon_allocate_device(pdev->device,
1059 sizeof(struct octeon_device_priv));
1060 if (!oct_dev) {
1061 dev_err(&pdev->dev, "Unable to allocate device\n");
1062 return -ENOMEM;
1063 }
1064
1065 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1066 (u32)pdev->vendor, (u32)pdev->device);
1067
1068 /* Assign octeon_device for this device to the private data area. */
1069 pci_set_drvdata(pdev, oct_dev);
1070
1071 /* set linux specific device pointer */
1072 oct_dev->pci_dev = (void *)pdev;
1073
1074 hs = &handshake[oct_dev->octeon_id];
1075 init_completion(&hs->init);
1076 init_completion(&hs->started);
1077 hs->pci_dev = pdev;
1078
1079 if (oct_dev->octeon_id == 0)
1080 /* first LiquidIO NIC is detected */
1081 complete(&first_stage);
1082
1083 if (octeon_device_init(oct_dev)) {
1084 liquidio_remove(pdev);
1085 return -ENOMEM;
1086 }
1087
1088 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1089
1090 return 0;
1091}
1092
1093/**
1094 *\brief Destroy resources associated with octeon device
1095 * @param pdev PCI device structure
1096 * @param ent unused
1097 */
1098static void octeon_destroy_resources(struct octeon_device *oct)
1099{
1100 int i;
1101 struct octeon_device_priv *oct_priv =
1102 (struct octeon_device_priv *)oct->priv;
1103
1104 struct handshake *hs;
1105
1106 switch (atomic_read(&oct->status)) {
1107 case OCT_DEV_RUNNING:
1108 case OCT_DEV_CORE_OK:
1109
1110 /* No more instructions will be forwarded. */
1111 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1112
1113 oct->app_mode = CVM_DRV_INVALID_APP;
1114 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1115 lio_get_state_string(&oct->status));
1116
1117 schedule_timeout_uninterruptible(HZ / 10);
1118
1119 /* fallthrough */
1120 case OCT_DEV_HOST_OK:
1121
1122 /* fallthrough */
1123 case OCT_DEV_CONSOLE_INIT_DONE:
1124 /* Remove any consoles */
1125 octeon_remove_consoles(oct);
1126
1127 /* fallthrough */
1128 case OCT_DEV_IO_QUEUES_DONE:
1129 if (wait_for_pending_requests(oct))
1130 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1131
1132 if (lio_wait_for_instr_fetch(oct))
1133 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1134
1135 /* Disable the input and output queues now. No more packets will
1136 * arrive from Octeon, but we should wait for all packet
1137 * processing to finish.
1138 */
1139 oct->fn_list.disable_io_queues(oct);
1140
1141 if (lio_wait_for_oq_pkts(oct))
1142 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1143
1144 /* Disable interrupts */
1145 oct->fn_list.disable_interrupt(oct->chip);
1146
1147 /* Release the interrupt line */
1148 free_irq(oct->pci_dev->irq, oct);
1149
1150 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1151 pci_disable_msi(oct->pci_dev);
1152
1153 /* Soft reset the octeon device before exiting */
1154 oct->fn_list.soft_reset(oct);
1155
1156 /* Disable the device, releasing the PCI INT */
1157 pci_disable_device(oct->pci_dev);
1158
1159 /* fallthrough */
1160 case OCT_DEV_IN_RESET:
1161 case OCT_DEV_DROQ_INIT_DONE:
1162 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1163 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001164 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
1165 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001166 continue;
1167 octeon_delete_droq(oct, i);
1168 }
1169
1170 /* Force any pending handshakes to complete */
1171 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1172 hs = &handshake[i];
1173
1174 if (hs->pci_dev) {
1175 handshake[oct->octeon_id].init_ok = 0;
1176 complete(&handshake[oct->octeon_id].init);
1177 handshake[oct->octeon_id].started_ok = 0;
1178 complete(&handshake[oct->octeon_id].started);
1179 }
1180 }
1181
1182 /* fallthrough */
1183 case OCT_DEV_RESP_LIST_INIT_DONE:
1184 octeon_delete_response_list(oct);
1185
1186 /* fallthrough */
1187 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1188 octeon_free_sc_buffer_pool(oct);
1189
1190 /* fallthrough */
1191 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001192 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
1193 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001194 continue;
1195 octeon_delete_instr_queue(oct, i);
1196 }
1197
1198 /* fallthrough */
1199 case OCT_DEV_DISPATCH_INIT_DONE:
1200 octeon_delete_dispatch_list(oct);
1201 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1202
1203 /* fallthrough */
1204 case OCT_DEV_PCI_MAP_DONE:
1205 octeon_unmap_pci_barx(oct, 0);
1206 octeon_unmap_pci_barx(oct, 1);
1207
1208 /* fallthrough */
1209 case OCT_DEV_BEGIN_STATE:
1210 /* Nothing to be done here either */
1211 break;
1212 } /* end switch(oct->status) */
1213
1214 tasklet_kill(&oct_priv->droq_tasklet);
1215}
1216
1217/**
1218 * \brief Send Rx control command
1219 * @param lio per-network private data
1220 * @param start_stop whether to start or stop
1221 */
1222static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1223{
1224 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001225
1226 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1227
1228 nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001229 nctrl.ncmd.s.param1 = start_stop;
1230 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001231 nctrl.netpndev = (u64)lio->netdev;
1232
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001233 if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl) < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001234 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1235}
1236
1237/**
1238 * \brief Destroy NIC device interface
1239 * @param oct octeon device
1240 * @param ifidx which interface to destroy
1241 *
1242 * Cleanup associated with each interface for an Octeon device when NIC
1243 * module is being unloaded or if initialization fails during load.
1244 */
1245static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1246{
1247 struct net_device *netdev = oct->props[ifidx].netdev;
1248 struct lio *lio;
1249
1250 if (!netdev) {
1251 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1252 __func__, ifidx);
1253 return;
1254 }
1255
1256 lio = GET_LIO(netdev);
1257
1258 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1259
1260 send_rx_ctrl_cmd(lio, 0);
1261
1262 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1263 txqs_stop(netdev);
1264
1265 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1266 unregister_netdev(netdev);
1267
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001268 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001269
1270 free_netdev(netdev);
1271
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001272 oct->props[ifidx].gmxport = -1;
1273
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001274 oct->props[ifidx].netdev = NULL;
1275}
1276
1277/**
1278 * \brief Stop complete NIC functionality
1279 * @param oct octeon device
1280 */
1281static int liquidio_stop_nic_module(struct octeon_device *oct)
1282{
1283 int i, j;
1284 struct lio *lio;
1285
1286 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1287 if (!oct->ifcount) {
1288 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1289 return 1;
1290 }
1291
1292 for (i = 0; i < oct->ifcount; i++) {
1293 lio = GET_LIO(oct->props[i].netdev);
1294 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001295 octeon_unregister_droq_ops(oct,
1296 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001297 }
1298
1299 for (i = 0; i < oct->ifcount; i++)
1300 liquidio_destroy_nic_device(oct, i);
1301
1302 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1303 return 0;
1304}
1305
1306/**
1307 * \brief Cleans up resources at unload time
1308 * @param pdev PCI device structure
1309 */
1310static void liquidio_remove(struct pci_dev *pdev)
1311{
1312 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1313
1314 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1315
1316 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1317 liquidio_stop_nic_module(oct_dev);
1318
1319 /* Reset the octeon device and cleanup all memory allocated for
1320 * the octeon device by driver.
1321 */
1322 octeon_destroy_resources(oct_dev);
1323
1324 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1325
1326 /* This octeon device has been removed. Update the global
1327 * data structure to reflect this. Free the device structure.
1328 */
1329 octeon_free_device_mem(oct_dev);
1330}
1331
1332/**
1333 * \brief Identify the Octeon device and to map the BAR address space
1334 * @param oct octeon device
1335 */
1336static int octeon_chip_specific_setup(struct octeon_device *oct)
1337{
1338 u32 dev_id, rev_id;
1339 int ret = 1;
1340
1341 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1342 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1343 oct->rev_id = rev_id & 0xff;
1344
1345 switch (dev_id) {
1346 case OCTEON_CN68XX_PCIID:
1347 oct->chip_id = OCTEON_CN68XX;
1348 ret = lio_setup_cn68xx_octeon_device(oct);
1349 break;
1350
1351 case OCTEON_CN66XX_PCIID:
1352 oct->chip_id = OCTEON_CN66XX;
1353 ret = lio_setup_cn66xx_octeon_device(oct);
1354 break;
1355 default:
1356 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1357 dev_id);
1358 }
1359
1360 if (!ret)
1361 dev_info(&oct->pci_dev->dev, "CN68XX PASS%d.%d %s\n",
1362 OCTEON_MAJOR_REV(oct),
1363 OCTEON_MINOR_REV(oct),
1364 octeon_get_conf(oct)->card_name);
1365
1366 return ret;
1367}
1368
1369/**
1370 * \brief PCI initialization for each Octeon device.
1371 * @param oct octeon device
1372 */
1373static int octeon_pci_os_setup(struct octeon_device *oct)
1374{
1375 /* setup PCI stuff first */
1376 if (pci_enable_device(oct->pci_dev)) {
1377 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1378 return 1;
1379 }
1380
1381 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1382 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1383 return 1;
1384 }
1385
1386 /* Enable PCI DMA Master. */
1387 pci_set_master(oct->pci_dev);
1388
1389 return 0;
1390}
1391
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001392static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1393{
1394 int q = 0;
1395
1396 if (netif_is_multiqueue(lio->netdev))
1397 q = skb->queue_mapping % lio->linfo.num_txpciq;
1398
1399 return q;
1400}
1401
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001402/**
1403 * \brief Check Tx queue state for a given network buffer
1404 * @param lio per-network private data
1405 * @param skb network buffer
1406 */
1407static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1408{
1409 int q = 0, iq = 0;
1410
1411 if (netif_is_multiqueue(lio->netdev)) {
1412 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001413 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001414 } else {
1415 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001416 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001417 }
1418
1419 if (octnet_iq_is_full(lio->oct_dev, iq))
1420 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001421
1422 if (__netif_subqueue_stopped(lio->netdev, q))
1423 wake_q(lio->netdev, q);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001424 return 1;
1425}
1426
1427/**
1428 * \brief Unmap and free network buffer
1429 * @param buf buffer
1430 */
1431static void free_netbuf(void *buf)
1432{
1433 struct sk_buff *skb;
1434 struct octnet_buf_free_info *finfo;
1435 struct lio *lio;
1436
1437 finfo = (struct octnet_buf_free_info *)buf;
1438 skb = finfo->skb;
1439 lio = finfo->lio;
1440
1441 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1442 DMA_TO_DEVICE);
1443
1444 check_txq_state(lio, skb);
1445
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001446 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001447}
1448
1449/**
1450 * \brief Unmap and free gather buffer
1451 * @param buf buffer
1452 */
1453static void free_netsgbuf(void *buf)
1454{
1455 struct octnet_buf_free_info *finfo;
1456 struct sk_buff *skb;
1457 struct lio *lio;
1458 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001459 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001460
1461 finfo = (struct octnet_buf_free_info *)buf;
1462 skb = finfo->skb;
1463 lio = finfo->lio;
1464 g = finfo->g;
1465 frags = skb_shinfo(skb)->nr_frags;
1466
1467 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1468 g->sg[0].ptr[0], (skb->len - skb->data_len),
1469 DMA_TO_DEVICE);
1470
1471 i = 1;
1472 while (frags--) {
1473 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1474
1475 pci_unmap_page((lio->oct_dev)->pci_dev,
1476 g->sg[(i >> 2)].ptr[(i & 3)],
1477 frag->size, DMA_TO_DEVICE);
1478 i++;
1479 }
1480
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001481 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1482 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001483
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001484 iq = skb_iq(lio, skb);
1485 spin_lock(&lio->glist_lock[iq]);
1486 list_add_tail(&g->list, &lio->glist[iq]);
1487 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001488
1489 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1490
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001491 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001492}
1493
1494/**
1495 * \brief Unmap and free gather buffer with response
1496 * @param buf buffer
1497 */
1498static void free_netsgbuf_with_resp(void *buf)
1499{
1500 struct octeon_soft_command *sc;
1501 struct octnet_buf_free_info *finfo;
1502 struct sk_buff *skb;
1503 struct lio *lio;
1504 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001505 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001506
1507 sc = (struct octeon_soft_command *)buf;
1508 skb = (struct sk_buff *)sc->callback_arg;
1509 finfo = (struct octnet_buf_free_info *)&skb->cb;
1510
1511 lio = finfo->lio;
1512 g = finfo->g;
1513 frags = skb_shinfo(skb)->nr_frags;
1514
1515 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1516 g->sg[0].ptr[0], (skb->len - skb->data_len),
1517 DMA_TO_DEVICE);
1518
1519 i = 1;
1520 while (frags--) {
1521 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1522
1523 pci_unmap_page((lio->oct_dev)->pci_dev,
1524 g->sg[(i >> 2)].ptr[(i & 3)],
1525 frag->size, DMA_TO_DEVICE);
1526 i++;
1527 }
1528
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001529 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1530 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001531
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001532 iq = skb_iq(lio, skb);
1533
1534 spin_lock(&lio->glist_lock[iq]);
1535 list_add_tail(&g->list, &lio->glist[iq]);
1536 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001537
1538 /* Don't free the skb yet */
1539
1540 check_txq_state(lio, skb);
1541}
1542
1543/**
1544 * \brief Adjust ptp frequency
1545 * @param ptp PTP clock info
1546 * @param ppb how much to adjust by, in parts-per-billion
1547 */
1548static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1549{
1550 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1551 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1552 u64 comp, delta;
1553 unsigned long flags;
1554 bool neg_adj = false;
1555
1556 if (ppb < 0) {
1557 neg_adj = true;
1558 ppb = -ppb;
1559 }
1560
1561 /* The hardware adds the clock compensation value to the
1562 * PTP clock on every coprocessor clock cycle, so we
1563 * compute the delta in terms of coprocessor clocks.
1564 */
1565 delta = (u64)ppb << 32;
1566 do_div(delta, oct->coproc_clock_rate);
1567
1568 spin_lock_irqsave(&lio->ptp_lock, flags);
1569 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1570 if (neg_adj)
1571 comp -= delta;
1572 else
1573 comp += delta;
1574 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1575 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1576
1577 return 0;
1578}
1579
1580/**
1581 * \brief Adjust ptp time
1582 * @param ptp PTP clock info
1583 * @param delta how much to adjust by, in nanosecs
1584 */
1585static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1586{
1587 unsigned long flags;
1588 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1589
1590 spin_lock_irqsave(&lio->ptp_lock, flags);
1591 lio->ptp_adjust += delta;
1592 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1593
1594 return 0;
1595}
1596
1597/**
1598 * \brief Get hardware clock time, including any adjustment
1599 * @param ptp PTP clock info
1600 * @param ts timespec
1601 */
1602static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1603 struct timespec64 *ts)
1604{
1605 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001606 unsigned long flags;
1607 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1608 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1609
1610 spin_lock_irqsave(&lio->ptp_lock, flags);
1611 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1612 ns += lio->ptp_adjust;
1613 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1614
Kefeng Wang286af312016-01-27 17:34:37 +08001615 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001616
1617 return 0;
1618}
1619
1620/**
1621 * \brief Set hardware clock time. Reset adjustment
1622 * @param ptp PTP clock info
1623 * @param ts timespec
1624 */
1625static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1626 const struct timespec64 *ts)
1627{
1628 u64 ns;
1629 unsigned long flags;
1630 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1631 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1632
1633 ns = timespec_to_ns(ts);
1634
1635 spin_lock_irqsave(&lio->ptp_lock, flags);
1636 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1637 lio->ptp_adjust = 0;
1638 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1639
1640 return 0;
1641}
1642
1643/**
1644 * \brief Check if PTP is enabled
1645 * @param ptp PTP clock info
1646 * @param rq request
1647 * @param on is it on
1648 */
1649static int liquidio_ptp_enable(struct ptp_clock_info *ptp,
1650 struct ptp_clock_request *rq, int on)
1651{
1652 return -EOPNOTSUPP;
1653}
1654
1655/**
1656 * \brief Open PTP clock source
1657 * @param netdev network device
1658 */
1659static void oct_ptp_open(struct net_device *netdev)
1660{
1661 struct lio *lio = GET_LIO(netdev);
1662 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1663
1664 spin_lock_init(&lio->ptp_lock);
1665
1666 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1667 lio->ptp_info.owner = THIS_MODULE;
1668 lio->ptp_info.max_adj = 250000000;
1669 lio->ptp_info.n_alarm = 0;
1670 lio->ptp_info.n_ext_ts = 0;
1671 lio->ptp_info.n_per_out = 0;
1672 lio->ptp_info.pps = 0;
1673 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1674 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1675 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1676 lio->ptp_info.settime64 = liquidio_ptp_settime;
1677 lio->ptp_info.enable = liquidio_ptp_enable;
1678
1679 lio->ptp_adjust = 0;
1680
1681 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1682 &oct->pci_dev->dev);
1683
1684 if (IS_ERR(lio->ptp_clock))
1685 lio->ptp_clock = NULL;
1686}
1687
1688/**
1689 * \brief Init PTP clock
1690 * @param oct octeon device
1691 */
1692static void liquidio_ptp_init(struct octeon_device *oct)
1693{
1694 u64 clock_comp, cfg;
1695
1696 clock_comp = (u64)NSEC_PER_SEC << 32;
1697 do_div(clock_comp, oct->coproc_clock_rate);
1698 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1699
1700 /* Enable */
1701 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1702 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1703}
1704
1705/**
1706 * \brief Load firmware to device
1707 * @param oct octeon device
1708 *
1709 * Maps device to firmware filename, requests firmware, and downloads it
1710 */
1711static int load_firmware(struct octeon_device *oct)
1712{
1713 int ret = 0;
1714 const struct firmware *fw;
1715 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1716 char *tmp_fw_type;
1717
1718 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1719 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1720 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1721 return ret;
1722 }
1723
1724 if (fw_type[0] == '\0')
1725 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1726 else
1727 tmp_fw_type = fw_type;
1728
1729 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1730 octeon_get_conf(oct)->card_name, tmp_fw_type,
1731 LIO_FW_NAME_SUFFIX);
1732
1733 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1734 if (ret) {
1735 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1736 fw_name);
1737 return ret;
1738 }
1739
1740 ret = octeon_download_firmware(oct, fw->data, fw->size);
1741
1742 release_firmware(fw);
1743
1744 return ret;
1745}
1746
1747/**
1748 * \brief Setup output queue
1749 * @param oct octeon device
1750 * @param q_no which queue
1751 * @param num_descs how many descriptors
1752 * @param desc_size size of each descriptor
1753 * @param app_ctx application context
1754 */
1755static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
1756 int desc_size, void *app_ctx)
1757{
1758 int ret_val = 0;
1759
1760 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
1761 /* droq creation and local register settings. */
1762 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301763 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001764 return ret_val;
1765
1766 if (ret_val == 1) {
1767 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
1768 return 0;
1769 }
1770 /* tasklet creation for the droq */
1771
1772 /* Enable the droq queues */
1773 octeon_set_droq_pkt_op(oct, q_no, 1);
1774
1775 /* Send Credit for Octeon Output queues. Credits are always
1776 * sent after the output queue is enabled.
1777 */
1778 writel(oct->droq[q_no]->max_count,
1779 oct->droq[q_no]->pkts_credit_reg);
1780
1781 return ret_val;
1782}
1783
1784/**
1785 * \brief Callback for getting interface configuration
1786 * @param status status of request
1787 * @param buf pointer to resp structure
1788 */
1789static void if_cfg_callback(struct octeon_device *oct,
1790 u32 status,
1791 void *buf)
1792{
1793 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1794 struct liquidio_if_cfg_resp *resp;
1795 struct liquidio_if_cfg_context *ctx;
1796
1797 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1798 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1799
1800 oct = lio_get_device(ctx->octeon_id);
1801 if (resp->status)
1802 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1803 CVM_CAST64(resp->status));
1804 ACCESS_ONCE(ctx->cond) = 1;
1805
1806 /* This barrier is required to be sure that the response has been
1807 * written fully before waking up the handler
1808 */
1809 wmb();
1810
1811 wake_up_interruptible(&ctx->wc);
1812}
1813
1814/**
1815 * \brief Select queue based on hash
1816 * @param dev Net device
1817 * @param skb sk_buff structure
1818 * @returns selected queue number
1819 */
1820static u16 select_q(struct net_device *dev, struct sk_buff *skb,
1821 void *accel_priv, select_queue_fallback_t fallback)
1822{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001823 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001824 struct lio *lio;
1825
1826 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001827 qindex = skb_tx_hash(dev, skb);
1828
1829 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001830}
1831
1832/** Routine to push packets arriving on Octeon interface upto network layer.
1833 * @param oct_id - octeon device id.
1834 * @param skbuff - skbuff struct to be passed to network layer.
1835 * @param len - size of total data received.
1836 * @param rh - Control header associated with the packet
1837 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001838 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001839 */
1840static void
1841liquidio_push_packet(u32 octeon_id,
1842 void *skbuff,
1843 u32 len,
1844 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001845 void *param,
1846 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001847{
1848 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001849 struct sk_buff *skb = (struct sk_buff *)skbuff;
1850 struct skb_shared_hwtstamps *shhwtstamps;
1851 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07001852 u16 vtag = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001853 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001854 struct octeon_droq *droq = container_of(param, struct octeon_droq,
1855 napi);
1856 if (netdev) {
1857 int packet_was_received;
1858 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001859 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001860
1861 /* Do not proceed if the interface is not in RUNNING state. */
1862 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1863 recv_buffer_free(skb);
1864 droq->stats.rx_dropped++;
1865 return;
1866 }
1867
1868 skb->dev = netdev;
1869
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001870 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001871 if (likely(len > MIN_SKB_SIZE)) {
1872 struct octeon_skb_page_info *pg_info;
1873 unsigned char *va;
1874
1875 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
1876 if (pg_info->page) {
1877 /* For Paged allocation use the frags */
1878 va = page_address(pg_info->page) +
1879 pg_info->page_offset;
1880 memcpy(skb->data, va, MIN_SKB_SIZE);
1881 skb_put(skb, MIN_SKB_SIZE);
1882 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1883 pg_info->page,
1884 pg_info->page_offset +
1885 MIN_SKB_SIZE,
1886 len - MIN_SKB_SIZE,
1887 LIO_RXBUFFER_SZ);
1888 }
1889 } else {
1890 struct octeon_skb_page_info *pg_info =
1891 ((struct octeon_skb_page_info *)(skb->cb));
1892 skb_copy_to_linear_data(skb, page_address(pg_info->page)
1893 + pg_info->page_offset, len);
1894 skb_put(skb, len);
1895 put_page(pg_info->page);
1896 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001897
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001898 if (((oct->chip_id == OCTEON_CN66XX) ||
1899 (oct->chip_id == OCTEON_CN68XX)) &&
1900 ptp_enable) {
1901 if (rh->r_dh.has_hwtstamp) {
1902 /* timestamp is included from the hardware at
1903 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001904 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001905 if (ifstate_check
1906 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
1907 /* Nanoseconds are in the first 64-bits
1908 * of the packet.
1909 */
1910 memcpy(&ns, (skb->data), sizeof(ns));
1911 shhwtstamps = skb_hwtstamps(skb);
1912 shhwtstamps->hwtstamp =
1913 ns_to_ktime(ns +
1914 lio->ptp_adjust);
1915 }
1916 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001917 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001918 }
1919
1920 skb->protocol = eth_type_trans(skb, skb->dev);
1921
1922 if ((netdev->features & NETIF_F_RXCSUM) &&
1923 (rh->r_dh.csum_verified == CNNIC_CSUM_VERIFIED))
1924 /* checksum has already been verified */
1925 skb->ip_summed = CHECKSUM_UNNECESSARY;
1926 else
1927 skb->ip_summed = CHECKSUM_NONE;
1928
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07001929 /* inbound VLAN tag */
1930 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1931 (rh->r_dh.vlan != 0)) {
1932 u16 vid = rh->r_dh.vlan;
1933 u16 priority = rh->r_dh.priority;
1934
1935 vtag = priority << 13 | vid;
1936 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
1937 }
1938
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001939 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
1940
1941 if (packet_was_received) {
1942 droq->stats.rx_bytes_received += len;
1943 droq->stats.rx_pkts_received++;
1944 netdev->last_rx = jiffies;
1945 } else {
1946 droq->stats.rx_dropped++;
1947 netif_info(lio, rx_err, lio->netdev,
1948 "droq:%d error rx_dropped:%llu\n",
1949 droq->q_no, droq->stats.rx_dropped);
1950 }
1951
1952 } else {
1953 recv_buffer_free(skb);
1954 }
1955}
1956
1957/**
1958 * \brief wrapper for calling napi_schedule
1959 * @param param parameters to pass to napi_schedule
1960 *
1961 * Used when scheduling on different CPUs
1962 */
1963static void napi_schedule_wrapper(void *param)
1964{
1965 struct napi_struct *napi = param;
1966
1967 napi_schedule(napi);
1968}
1969
1970/**
1971 * \brief callback when receive interrupt occurs and we are in NAPI mode
1972 * @param arg pointer to octeon output queue
1973 */
1974static void liquidio_napi_drv_callback(void *arg)
1975{
1976 struct octeon_droq *droq = arg;
1977 int this_cpu = smp_processor_id();
1978
1979 if (droq->cpu_id == this_cpu) {
1980 napi_schedule(&droq->napi);
1981 } else {
1982 struct call_single_data *csd = &droq->csd;
1983
1984 csd->func = napi_schedule_wrapper;
1985 csd->info = &droq->napi;
1986 csd->flags = 0;
1987
1988 smp_call_function_single_async(droq->cpu_id, csd);
1989 }
1990}
1991
1992/**
1993 * \brief Main NAPI poll function
1994 * @param droq octeon output queue
1995 * @param budget maximum number of items to process
1996 */
1997static int liquidio_napi_do_rx(struct octeon_droq *droq, int budget)
1998{
1999 int work_done;
2000 struct lio *lio = GET_LIO(droq->napi.dev);
2001 struct octeon_device *oct = lio->oct_dev;
2002
2003 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2004 POLL_EVENT_PROCESS_PKTS,
2005 budget);
2006 if (work_done < 0) {
2007 netif_info(lio, rx_err, lio->netdev,
2008 "Receive work_done < 0, rxq:%d\n", droq->q_no);
2009 goto octnet_napi_finish;
2010 }
2011
2012 if (work_done > budget)
2013 dev_err(&oct->pci_dev->dev, ">>>> %s work_done: %d budget: %d\n",
2014 __func__, work_done, budget);
2015
2016 return work_done;
2017
2018octnet_napi_finish:
2019 napi_complete(&droq->napi);
2020 octeon_process_droq_poll_cmd(oct, droq->q_no, POLL_EVENT_ENABLE_INTR,
2021 0);
2022 return 0;
2023}
2024
2025/**
2026 * \brief Entry point for NAPI polling
2027 * @param napi NAPI structure
2028 * @param budget maximum number of items to process
2029 */
2030static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2031{
2032 struct octeon_droq *droq;
2033 int work_done;
2034
2035 droq = container_of(napi, struct octeon_droq, napi);
2036
2037 work_done = liquidio_napi_do_rx(droq, budget);
2038
2039 if (work_done < budget) {
2040 napi_complete(napi);
2041 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2042 POLL_EVENT_ENABLE_INTR, 0);
2043 return 0;
2044 }
2045
2046 return work_done;
2047}
2048
2049/**
2050 * \brief Setup input and output queues
2051 * @param octeon_dev octeon device
2052 * @param net_device Net device
2053 *
2054 * Note: Queues are with respect to the octeon device. Thus
2055 * an input queue is for egress packets, and output queues
2056 * are for ingress packets.
2057 */
2058static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002059 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002060{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002061 struct octeon_droq_ops droq_ops;
2062 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002063 static int cpu_id;
2064 static int cpu_id_modulus;
2065 struct octeon_droq *droq;
2066 struct napi_struct *napi;
2067 int q, q_no, retval = 0;
2068 struct lio *lio;
2069 int num_tx_descs;
2070
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002071 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002072
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002073 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002074
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002075 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2076
2077 droq_ops.fptr = liquidio_push_packet;
2078 droq_ops.farg = (void *)netdev;
2079
2080 droq_ops.poll_mode = 1;
2081 droq_ops.napi_fn = liquidio_napi_drv_callback;
2082 cpu_id = 0;
2083 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002084
2085 /* set up DROQs. */
2086 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002087 q_no = lio->linfo.rxpciq[q].s.q_no;
2088 dev_dbg(&octeon_dev->pci_dev->dev,
2089 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2090 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002091 retval = octeon_setup_droq(octeon_dev, q_no,
2092 CFG_GET_NUM_RX_DESCS_NIC_IF
2093 (octeon_get_conf(octeon_dev),
2094 lio->ifidx),
2095 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2096 (octeon_get_conf(octeon_dev),
2097 lio->ifidx), NULL);
2098 if (retval) {
2099 dev_err(&octeon_dev->pci_dev->dev,
2100 " %s : Runtime DROQ(RxQ) creation failed.\n",
2101 __func__);
2102 return 1;
2103 }
2104
2105 droq = octeon_dev->droq[q_no];
2106 napi = &droq->napi;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002107 dev_dbg(&octeon_dev->pci_dev->dev,
2108 "netif_napi_add netdev:%llx oct:%llx\n",
2109 (u64)netdev,
2110 (u64)octeon_dev);
2111 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002112
2113 /* designate a CPU for this droq */
2114 droq->cpu_id = cpu_id;
2115 cpu_id++;
2116 if (cpu_id >= cpu_id_modulus)
2117 cpu_id = 0;
2118
2119 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2120 }
2121
2122 /* set up IQs. */
2123 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2124 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2125 (octeon_dev),
2126 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002127 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2128 lio->linfo.txpciq[q], num_tx_descs,
2129 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002130 if (retval) {
2131 dev_err(&octeon_dev->pci_dev->dev,
2132 " %s : Runtime IQ(TxQ) creation failed.\n",
2133 __func__);
2134 return 1;
2135 }
2136 }
2137
2138 return 0;
2139}
2140
2141/**
2142 * \brief Poll routine for checking transmit queue status
2143 * @param work work_struct data structure
2144 */
2145static void octnet_poll_check_txq_status(struct work_struct *work)
2146{
2147 struct cavium_wk *wk = (struct cavium_wk *)work;
2148 struct lio *lio = (struct lio *)wk->ctxptr;
2149
2150 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2151 return;
2152
2153 check_txq_status(lio);
2154 queue_delayed_work(lio->txq_status_wq.wq,
2155 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2156}
2157
2158/**
2159 * \brief Sets up the txq poll check
2160 * @param netdev network device
2161 */
2162static inline void setup_tx_poll_fn(struct net_device *netdev)
2163{
2164 struct lio *lio = GET_LIO(netdev);
2165 struct octeon_device *oct = lio->oct_dev;
2166
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302167 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2168 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002169 if (!lio->txq_status_wq.wq) {
2170 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2171 return;
2172 }
2173 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2174 octnet_poll_check_txq_status);
2175 lio->txq_status_wq.wk.ctxptr = lio;
2176 queue_delayed_work(lio->txq_status_wq.wq,
2177 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2178}
2179
2180/**
2181 * \brief Net device open for LiquidIO
2182 * @param netdev network device
2183 */
2184static int liquidio_open(struct net_device *netdev)
2185{
2186 struct lio *lio = GET_LIO(netdev);
2187 struct octeon_device *oct = lio->oct_dev;
2188 struct napi_struct *napi, *n;
2189
2190 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2191 napi_enable(napi);
2192
2193 oct_ptp_open(netdev);
2194
2195 ifstate_set(lio, LIO_IFSTATE_RUNNING);
2196 setup_tx_poll_fn(netdev);
2197 start_txq(netdev);
2198
2199 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2200 try_module_get(THIS_MODULE);
2201
2202 /* tell Octeon to start forwarding packets to host */
2203 send_rx_ctrl_cmd(lio, 1);
2204
2205 /* Ready for link status updates */
2206 lio->intf_open = 1;
2207
2208 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2209 netdev->name);
2210
2211 return 0;
2212}
2213
2214/**
2215 * \brief Net device stop for LiquidIO
2216 * @param netdev network device
2217 */
2218static int liquidio_stop(struct net_device *netdev)
2219{
2220 struct napi_struct *napi, *n;
2221 struct lio *lio = GET_LIO(netdev);
2222 struct octeon_device *oct = lio->oct_dev;
2223
2224 netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
2225 /* Inform that netif carrier is down */
2226 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002227 lio->linfo.link.s.link_up = 0;
2228 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002229
2230 netif_carrier_off(netdev);
2231
2232 /* tell Octeon to stop forwarding packets to host */
2233 send_rx_ctrl_cmd(lio, 0);
2234
2235 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002236 destroy_workqueue(lio->txq_status_wq.wq);
2237
2238 if (lio->ptp_clock) {
2239 ptp_clock_unregister(lio->ptp_clock);
2240 lio->ptp_clock = NULL;
2241 }
2242
2243 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2244
2245 /* This is a hack that allows DHCP to continue working. */
2246 set_bit(__LINK_STATE_START, &lio->netdev->state);
2247
2248 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2249 napi_disable(napi);
2250
2251 txqs_stop(netdev);
2252
2253 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2254 module_put(THIS_MODULE);
2255
2256 return 0;
2257}
2258
2259void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr)
2260{
2261 struct octnic_ctrl_pkt *nctrl = (struct octnic_ctrl_pkt *)nctrl_ptr;
2262 struct net_device *netdev = (struct net_device *)nctrl->netpndev;
2263 struct lio *lio = GET_LIO(netdev);
2264 struct octeon_device *oct = lio->oct_dev;
2265
2266 switch (nctrl->ncmd.s.cmd) {
2267 case OCTNET_CMD_CHANGE_DEVFLAGS:
2268 case OCTNET_CMD_SET_MULTI_LIST:
2269 break;
2270
2271 case OCTNET_CMD_CHANGE_MACADDR:
2272 /* If command is successful, change the MACADDR. */
2273 netif_info(lio, probe, lio->netdev, " MACAddr changed to 0x%llx\n",
2274 CVM_CAST64(nctrl->udd[0]));
2275 dev_info(&oct->pci_dev->dev, "%s MACAddr changed to 0x%llx\n",
2276 netdev->name, CVM_CAST64(nctrl->udd[0]));
2277 memcpy(netdev->dev_addr, ((u8 *)&nctrl->udd[0]) + 2, ETH_ALEN);
2278 break;
2279
2280 case OCTNET_CMD_CHANGE_MTU:
2281 /* If command is successful, change the MTU. */
2282 netif_info(lio, probe, lio->netdev, " MTU Changed from %d to %d\n",
2283 netdev->mtu, nctrl->ncmd.s.param2);
2284 dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
2285 netdev->name, netdev->mtu,
2286 nctrl->ncmd.s.param2);
2287 netdev->mtu = nctrl->ncmd.s.param2;
2288 break;
2289
2290 case OCTNET_CMD_GPIO_ACCESS:
2291 netif_info(lio, probe, lio->netdev, "LED Flashing visual identification\n");
2292
2293 break;
2294
2295 case OCTNET_CMD_LRO_ENABLE:
2296 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2297 break;
2298
2299 case OCTNET_CMD_LRO_DISABLE:
2300 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2301 netdev->name);
2302 break;
2303
2304 case OCTNET_CMD_VERBOSE_ENABLE:
2305 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2306 break;
2307
2308 case OCTNET_CMD_VERBOSE_DISABLE:
2309 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2310 netdev->name);
2311 break;
2312
2313 case OCTNET_CMD_SET_SETTINGS:
2314 dev_info(&oct->pci_dev->dev, "%s settings changed\n",
2315 netdev->name);
2316
2317 break;
2318
2319 default:
2320 dev_err(&oct->pci_dev->dev, "%s Unknown cmd %d\n", __func__,
2321 nctrl->ncmd.s.cmd);
2322 }
2323}
2324
2325/**
2326 * \brief Converts a mask based on net device flags
2327 * @param netdev network device
2328 *
2329 * This routine generates a octnet_ifflags mask from the net device flags
2330 * received from the OS.
2331 */
2332static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2333{
2334 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2335
2336 if (netdev->flags & IFF_PROMISC)
2337 f |= OCTNET_IFFLAG_PROMISC;
2338
2339 if (netdev->flags & IFF_ALLMULTI)
2340 f |= OCTNET_IFFLAG_ALLMULTI;
2341
2342 if (netdev->flags & IFF_MULTICAST) {
2343 f |= OCTNET_IFFLAG_MULTICAST;
2344
2345 /* Accept all multicast addresses if there are more than we
2346 * can handle
2347 */
2348 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2349 f |= OCTNET_IFFLAG_ALLMULTI;
2350 }
2351
2352 if (netdev->flags & IFF_BROADCAST)
2353 f |= OCTNET_IFFLAG_BROADCAST;
2354
2355 return f;
2356}
2357
2358/**
2359 * \brief Net device set_multicast_list
2360 * @param netdev network device
2361 */
2362static void liquidio_set_mcast_list(struct net_device *netdev)
2363{
2364 struct lio *lio = GET_LIO(netdev);
2365 struct octeon_device *oct = lio->oct_dev;
2366 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002367 struct netdev_hw_addr *ha;
2368 u64 *mc;
2369 int ret, i;
2370 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2371
2372 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2373
2374 /* Create a ctrl pkt command to be sent to core app. */
2375 nctrl.ncmd.u64 = 0;
2376 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002377 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2378 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002379 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002380 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002381 nctrl.netpndev = (u64)netdev;
2382 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2383
2384 /* copy all the addresses into the udd */
2385 i = 0;
2386 mc = &nctrl.udd[0];
2387 netdev_for_each_mc_addr(ha, netdev) {
2388 *mc = 0;
2389 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2390 /* no need to swap bytes */
2391
2392 if (++mc > &nctrl.udd[mc_count])
2393 break;
2394 }
2395
2396 /* Apparently, any activity in this call from the kernel has to
2397 * be atomic. So we won't wait for response.
2398 */
2399 nctrl.wait_time = 0;
2400
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002401 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002402 if (ret < 0) {
2403 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2404 ret);
2405 }
2406}
2407
2408/**
2409 * \brief Net device set_mac_address
2410 * @param netdev network device
2411 */
2412static int liquidio_set_mac(struct net_device *netdev, void *p)
2413{
2414 int ret = 0;
2415 struct lio *lio = GET_LIO(netdev);
2416 struct octeon_device *oct = lio->oct_dev;
2417 struct sockaddr *addr = (struct sockaddr *)p;
2418 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002419
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002420 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002421 return -EADDRNOTAVAIL;
2422
2423 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2424
2425 nctrl.ncmd.u64 = 0;
2426 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002427 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002428 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002429 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002430 nctrl.netpndev = (u64)netdev;
2431 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2432 nctrl.wait_time = 100;
2433
2434 nctrl.udd[0] = 0;
2435 /* The MAC Address is presented in network byte order. */
2436 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2437
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002438 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002439 if (ret < 0) {
2440 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2441 return -ENOMEM;
2442 }
2443 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2444 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2445
2446 return 0;
2447}
2448
2449/**
2450 * \brief Net device get_stats
2451 * @param netdev network device
2452 */
2453static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2454{
2455 struct lio *lio = GET_LIO(netdev);
2456 struct net_device_stats *stats = &netdev->stats;
2457 struct octeon_device *oct;
2458 u64 pkts = 0, drop = 0, bytes = 0;
2459 struct oct_droq_stats *oq_stats;
2460 struct oct_iq_stats *iq_stats;
2461 int i, iq_no, oq_no;
2462
2463 oct = lio->oct_dev;
2464
2465 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002466 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002467 iq_stats = &oct->instr_queue[iq_no]->stats;
2468 pkts += iq_stats->tx_done;
2469 drop += iq_stats->tx_dropped;
2470 bytes += iq_stats->tx_tot_bytes;
2471 }
2472
2473 stats->tx_packets = pkts;
2474 stats->tx_bytes = bytes;
2475 stats->tx_dropped = drop;
2476
2477 pkts = 0;
2478 drop = 0;
2479 bytes = 0;
2480
2481 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002482 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002483 oq_stats = &oct->droq[oq_no]->stats;
2484 pkts += oq_stats->rx_pkts_received;
2485 drop += (oq_stats->rx_dropped +
2486 oq_stats->dropped_nodispatch +
2487 oq_stats->dropped_toomany +
2488 oq_stats->dropped_nomem);
2489 bytes += oq_stats->rx_bytes_received;
2490 }
2491
2492 stats->rx_bytes = bytes;
2493 stats->rx_packets = pkts;
2494 stats->rx_dropped = drop;
2495
2496 return stats;
2497}
2498
2499/**
2500 * \brief Net device change_mtu
2501 * @param netdev network device
2502 */
2503static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2504{
2505 struct lio *lio = GET_LIO(netdev);
2506 struct octeon_device *oct = lio->oct_dev;
2507 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002508 int max_frm_size = new_mtu + OCTNET_FRM_HEADER_SIZE;
2509 int ret = 0;
2510
2511 /* Limit the MTU to make sure the ethernet packets are between 64 bytes
2512 * and 65535 bytes
2513 */
2514 if ((max_frm_size < OCTNET_MIN_FRM_SIZE) ||
2515 (max_frm_size > OCTNET_MAX_FRM_SIZE)) {
2516 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2517 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
2518 (OCTNET_MIN_FRM_SIZE - OCTNET_FRM_HEADER_SIZE),
2519 (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE));
2520 return -EINVAL;
2521 }
2522
2523 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2524
2525 nctrl.ncmd.u64 = 0;
2526 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002527 nctrl.ncmd.s.param1 = new_mtu;
2528 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002529 nctrl.wait_time = 100;
2530 nctrl.netpndev = (u64)netdev;
2531 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2532
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002533 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002534 if (ret < 0) {
2535 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2536 return -1;
2537 }
2538
2539 lio->mtu = new_mtu;
2540
2541 return 0;
2542}
2543
2544/**
2545 * \brief Handler for SIOCSHWTSTAMP ioctl
2546 * @param netdev network device
2547 * @param ifr interface request
2548 * @param cmd command
2549 */
2550static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2551{
2552 struct hwtstamp_config conf;
2553 struct lio *lio = GET_LIO(netdev);
2554
2555 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2556 return -EFAULT;
2557
2558 if (conf.flags)
2559 return -EINVAL;
2560
2561 switch (conf.tx_type) {
2562 case HWTSTAMP_TX_ON:
2563 case HWTSTAMP_TX_OFF:
2564 break;
2565 default:
2566 return -ERANGE;
2567 }
2568
2569 switch (conf.rx_filter) {
2570 case HWTSTAMP_FILTER_NONE:
2571 break;
2572 case HWTSTAMP_FILTER_ALL:
2573 case HWTSTAMP_FILTER_SOME:
2574 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2575 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2576 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2577 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2578 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2579 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2580 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2581 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2582 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2583 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2584 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2585 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2586 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2587 break;
2588 default:
2589 return -ERANGE;
2590 }
2591
2592 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2593 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2594
2595 else
2596 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2597
2598 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2599}
2600
2601/**
2602 * \brief ioctl handler
2603 * @param netdev network device
2604 * @param ifr interface request
2605 * @param cmd command
2606 */
2607static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2608{
2609 switch (cmd) {
2610 case SIOCSHWTSTAMP:
2611 return hwtstamp_ioctl(netdev, ifr, cmd);
2612 default:
2613 return -EOPNOTSUPP;
2614 }
2615}
2616
2617/**
2618 * \brief handle a Tx timestamp response
2619 * @param status response status
2620 * @param buf pointer to skb
2621 */
2622static void handle_timestamp(struct octeon_device *oct,
2623 u32 status,
2624 void *buf)
2625{
2626 struct octnet_buf_free_info *finfo;
2627 struct octeon_soft_command *sc;
2628 struct oct_timestamp_resp *resp;
2629 struct lio *lio;
2630 struct sk_buff *skb = (struct sk_buff *)buf;
2631
2632 finfo = (struct octnet_buf_free_info *)skb->cb;
2633 lio = finfo->lio;
2634 sc = finfo->sc;
2635 oct = lio->oct_dev;
2636 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2637
2638 if (status != OCTEON_REQUEST_DONE) {
2639 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2640 CVM_CAST64(status));
2641 resp->timestamp = 0;
2642 }
2643
2644 octeon_swap_8B_data(&resp->timestamp, 1);
2645
Colin Ian King19a6d152016-02-05 16:30:39 +00002646 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002647 struct skb_shared_hwtstamps ts;
2648 u64 ns = resp->timestamp;
2649
2650 netif_info(lio, tx_done, lio->netdev,
2651 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2652 skb, (unsigned long long)ns);
2653 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2654 skb_tstamp_tx(skb, &ts);
2655 }
2656
2657 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002658 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002659}
2660
2661/* \brief Send a data packet that will be timestamped
2662 * @param oct octeon device
2663 * @param ndata pointer to network data
2664 * @param finfo pointer to private network data
2665 */
2666static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2667 struct octnic_data_pkt *ndata,
2668 struct octnet_buf_free_info *finfo,
2669 int xmit_more)
2670{
2671 int retval;
2672 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002673 struct lio *lio;
2674 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002675 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002676
2677 lio = finfo->lio;
2678
2679 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2680 sizeof(struct oct_timestamp_resp));
2681 finfo->sc = sc;
2682
2683 if (!sc) {
2684 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2685 return IQ_SEND_FAILED;
2686 }
2687
2688 if (ndata->reqtype == REQTYPE_NORESP_NET)
2689 ndata->reqtype = REQTYPE_RESP_NET;
2690 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2691 ndata->reqtype = REQTYPE_RESP_NET_SG;
2692
2693 sc->callback = handle_timestamp;
2694 sc->callback_arg = finfo->skb;
2695 sc->iq_no = ndata->q_no;
2696
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002697 len = (u32)((struct octeon_instr_ih2 *)(&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002698
2699 ring_doorbell = !xmit_more;
2700 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002701 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002702
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002703 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002704 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2705 retval);
2706 octeon_free_soft_command(oct, sc);
2707 } else {
2708 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2709 }
2710
2711 return retval;
2712}
2713
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002714/** \brief Transmit networks packets to the Octeon interface
2715 * @param skbuff skbuff struct to be passed to network layer.
2716 * @param netdev pointer to network device
2717 * @returns whether the packet was transmitted to the device okay or not
2718 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2719 */
2720static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2721{
2722 struct lio *lio;
2723 struct octnet_buf_free_info *finfo;
2724 union octnic_cmd_setup cmdsetup;
2725 struct octnic_data_pkt ndata;
2726 struct octeon_device *oct;
2727 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002728 struct octeon_instr_irh *irh;
2729 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002730 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002731 int q_idx = 0, iq_no = 0;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002732 int xmit_more, j;
2733 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002734 u32 tag = 0;
2735
2736 lio = GET_LIO(netdev);
2737 oct = lio->oct_dev;
2738
2739 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002740 q_idx = skb->queue_mapping;
2741 q_idx = (q_idx % (lio->linfo.num_txpciq));
2742 tag = q_idx;
2743 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002744 } else {
2745 iq_no = lio->txq;
2746 }
2747
2748 stats = &oct->instr_queue[iq_no]->stats;
2749
2750 /* Check for all conditions in which the current packet cannot be
2751 * transmitted.
2752 */
2753 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002754 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002755 (skb->len <= 0)) {
2756 netif_info(lio, tx_err, lio->netdev,
2757 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002758 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002759 goto lio_xmit_failed;
2760 }
2761
2762 /* Use space in skb->cb to store info used to unmap and
2763 * free the buffers.
2764 */
2765 finfo = (struct octnet_buf_free_info *)skb->cb;
2766 finfo->lio = lio;
2767 finfo->skb = skb;
2768 finfo->sc = NULL;
2769
2770 /* Prepare the attributes for the data to be passed to OSI. */
2771 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2772
2773 ndata.buf = (void *)finfo;
2774
2775 ndata.q_no = iq_no;
2776
2777 if (netif_is_multiqueue(netdev)) {
2778 if (octnet_iq_is_full(oct, ndata.q_no)) {
2779 /* defer sending if queue is full */
2780 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2781 ndata.q_no);
2782 stats->tx_iq_busy++;
2783 return NETDEV_TX_BUSY;
2784 }
2785 } else {
2786 if (octnet_iq_is_full(oct, lio->txq)) {
2787 /* defer sending if queue is full */
2788 stats->tx_iq_busy++;
2789 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2790 ndata.q_no);
2791 return NETDEV_TX_BUSY;
2792 }
2793 }
2794 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
2795 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no );
2796 */
2797
2798 ndata.datasize = skb->len;
2799
2800 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002801 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002803 if (skb->ip_summed == CHECKSUM_PARTIAL)
2804 cmdsetup.s.transport_csum = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002805
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002806 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2807 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2808 cmdsetup.s.timestamp = 1;
2809 }
2810
2811 if (skb_shinfo(skb)->nr_frags == 0) {
2812 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002813 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002814 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002815 dptr = dma_map_single(&oct->pci_dev->dev,
2816 skb->data,
2817 skb->len,
2818 DMA_TO_DEVICE);
2819 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002820 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2821 __func__);
2822 return NETDEV_TX_BUSY;
2823 }
2824
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002825 ndata.cmd.cmd2.dptr = dptr;
2826 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002827 ndata.reqtype = REQTYPE_NORESP_NET;
2828
2829 } else {
2830 int i, frags;
2831 struct skb_frag_struct *frag;
2832 struct octnic_gather *g;
2833
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002834 spin_lock(&lio->glist_lock[q_idx]);
2835 g = (struct octnic_gather *)
2836 list_delete_head(&lio->glist[q_idx]);
2837 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002838
2839 if (!g) {
2840 netif_info(lio, tx_err, lio->netdev,
2841 "Transmit scatter gather: glist null!\n");
2842 goto lio_xmit_failed;
2843 }
2844
2845 cmdsetup.s.gather = 1;
2846 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002847 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002848
2849 memset(g->sg, 0, g->sg_size);
2850
2851 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2852 skb->data,
2853 (skb->len - skb->data_len),
2854 DMA_TO_DEVICE);
2855 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2856 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2857 __func__);
2858 return NETDEV_TX_BUSY;
2859 }
2860 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2861
2862 frags = skb_shinfo(skb)->nr_frags;
2863 i = 1;
2864 while (frags--) {
2865 frag = &skb_shinfo(skb)->frags[i - 1];
2866
2867 g->sg[(i >> 2)].ptr[(i & 3)] =
2868 dma_map_page(&oct->pci_dev->dev,
2869 frag->page.p,
2870 frag->page_offset,
2871 frag->size,
2872 DMA_TO_DEVICE);
2873
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002874 if (dma_mapping_error(&oct->pci_dev->dev,
2875 g->sg[i >> 2].ptr[i & 3])) {
2876 dma_unmap_single(&oct->pci_dev->dev,
2877 g->sg[0].ptr[0],
2878 skb->len - skb->data_len,
2879 DMA_TO_DEVICE);
2880 for (j = 1; j < i; j++) {
2881 frag = &skb_shinfo(skb)->frags[j - 1];
2882 dma_unmap_page(&oct->pci_dev->dev,
2883 g->sg[j >> 2].ptr[j & 3],
2884 frag->size,
2885 DMA_TO_DEVICE);
2886 }
2887 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2888 __func__);
2889 return NETDEV_TX_BUSY;
2890 }
2891
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002892 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2893 i++;
2894 }
2895
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002896 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
2897 g->sg_size, DMA_TO_DEVICE);
2898 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002899
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002900 ndata.cmd.cmd2.dptr = dptr;
2901 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002902 finfo->g = g;
2903
2904 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2905 }
2906
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002907 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2908 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002909
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002910 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002911 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2912 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
2913 }
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002914 /* HW insert VLAN tag */
2915 if (skb_vlan_tag_present(skb)) {
2916 irh->priority = skb_vlan_tag_get(skb) >> 13;
2917 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2918 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002919
2920 xmit_more = skb->xmit_more;
2921
2922 if (unlikely(cmdsetup.s.timestamp))
2923 status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
2924 else
2925 status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
2926 if (status == IQ_SEND_FAILED)
2927 goto lio_xmit_failed;
2928
2929 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2930
2931 if (status == IQ_SEND_STOP)
2932 stop_q(lio->netdev, q_idx);
2933
Florian Westphal860e9532016-05-03 16:33:13 +02002934 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002935
2936 stats->tx_done++;
2937 stats->tx_tot_bytes += skb->len;
2938
2939 return NETDEV_TX_OK;
2940
2941lio_xmit_failed:
2942 stats->tx_dropped++;
2943 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2944 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002945 if (dptr)
2946 dma_unmap_single(&oct->pci_dev->dev, dptr,
2947 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002948 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002949 return NETDEV_TX_OK;
2950}
2951
2952/** \brief Network device Tx timeout
2953 * @param netdev pointer to network device
2954 */
2955static void liquidio_tx_timeout(struct net_device *netdev)
2956{
2957 struct lio *lio;
2958
2959 lio = GET_LIO(netdev);
2960
2961 netif_info(lio, tx_err, lio->netdev,
2962 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2963 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002964 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002965 txqs_wake(netdev);
2966}
2967
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002968int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002969{
2970 struct lio *lio = GET_LIO(netdev);
2971 struct octeon_device *oct = lio->oct_dev;
2972 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002973 int ret = 0;
2974
2975 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2976
2977 nctrl.ncmd.u64 = 0;
2978 nctrl.ncmd.s.cmd = cmd;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002979 nctrl.ncmd.s.param1 = param1;
2980 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002981 nctrl.wait_time = 100;
2982 nctrl.netpndev = (u64)netdev;
2983 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2984
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002985 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002986 if (ret < 0) {
2987 dev_err(&oct->pci_dev->dev, "Feature change failed in core (ret: 0x%x)\n",
2988 ret);
2989 }
2990 return ret;
2991}
2992
2993/** \brief Net device fix features
2994 * @param netdev pointer to network device
2995 * @param request features requested
2996 * @returns updated features list
2997 */
2998static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2999 netdev_features_t request)
3000{
3001 struct lio *lio = netdev_priv(netdev);
3002
3003 if ((request & NETIF_F_RXCSUM) &&
3004 !(lio->dev_capability & NETIF_F_RXCSUM))
3005 request &= ~NETIF_F_RXCSUM;
3006
3007 if ((request & NETIF_F_HW_CSUM) &&
3008 !(lio->dev_capability & NETIF_F_HW_CSUM))
3009 request &= ~NETIF_F_HW_CSUM;
3010
3011 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3012 request &= ~NETIF_F_TSO;
3013
3014 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3015 request &= ~NETIF_F_TSO6;
3016
3017 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3018 request &= ~NETIF_F_LRO;
3019
3020 /*Disable LRO if RXCSUM is off */
3021 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3022 (lio->dev_capability & NETIF_F_LRO))
3023 request &= ~NETIF_F_LRO;
3024
3025 return request;
3026}
3027
3028/** \brief Net device set features
3029 * @param netdev pointer to network device
3030 * @param features features to enable/disable
3031 */
3032static int liquidio_set_features(struct net_device *netdev,
3033 netdev_features_t features)
3034{
3035 struct lio *lio = netdev_priv(netdev);
3036
3037 if (!((netdev->features ^ features) & NETIF_F_LRO))
3038 return 0;
3039
3040 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003041 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3042 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003043 else if (!(features & NETIF_F_LRO) &&
3044 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003045 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3046 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003047
3048 return 0;
3049}
3050
3051static struct net_device_ops lionetdevops = {
3052 .ndo_open = liquidio_open,
3053 .ndo_stop = liquidio_stop,
3054 .ndo_start_xmit = liquidio_xmit,
3055 .ndo_get_stats = liquidio_get_stats,
3056 .ndo_set_mac_address = liquidio_set_mac,
3057 .ndo_set_rx_mode = liquidio_set_mcast_list,
3058 .ndo_tx_timeout = liquidio_tx_timeout,
3059 .ndo_change_mtu = liquidio_change_mtu,
3060 .ndo_do_ioctl = liquidio_ioctl,
3061 .ndo_fix_features = liquidio_fix_features,
3062 .ndo_set_features = liquidio_set_features,
3063};
3064
3065/** \brief Entry point for the liquidio module
3066 */
3067static int __init liquidio_init(void)
3068{
3069 int i;
3070 struct handshake *hs;
3071
3072 init_completion(&first_stage);
3073
3074 octeon_init_device_list(conf_type);
3075
3076 if (liquidio_init_pci())
3077 return -EINVAL;
3078
3079 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3080
3081 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3082 hs = &handshake[i];
3083 if (hs->pci_dev) {
3084 wait_for_completion(&hs->init);
3085 if (!hs->init_ok) {
3086 /* init handshake failed */
3087 dev_err(&hs->pci_dev->dev,
3088 "Failed to init device\n");
3089 liquidio_deinit_pci();
3090 return -EIO;
3091 }
3092 }
3093 }
3094
3095 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3096 hs = &handshake[i];
3097 if (hs->pci_dev) {
3098 wait_for_completion_timeout(&hs->started,
3099 msecs_to_jiffies(30000));
3100 if (!hs->started_ok) {
3101 /* starter handshake failed */
3102 dev_err(&hs->pci_dev->dev,
3103 "Firmware failed to start\n");
3104 liquidio_deinit_pci();
3105 return -EIO;
3106 }
3107 }
3108 }
3109
3110 return 0;
3111}
3112
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003113static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003114{
3115 struct octeon_device *oct = (struct octeon_device *)buf;
3116 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003117 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003118 union oct_link_status *ls;
3119 int i;
3120
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003121 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003122 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3123 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003124 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003125 goto nic_info_err;
3126 }
3127
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003128 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003129 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3130
3131 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003132 for (i = 0; i < oct->ifcount; i++) {
3133 if (oct->props[i].gmxport == gmxport) {
3134 update_link_status(oct->props[i].netdev, ls);
3135 break;
3136 }
3137 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003138
3139nic_info_err:
3140 for (i = 0; i < recv_pkt->buffer_count; i++)
3141 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3142 octeon_free_recv_info(recv_info);
3143 return 0;
3144}
3145
3146/**
3147 * \brief Setup network interfaces
3148 * @param octeon_dev octeon device
3149 *
3150 * Called during init time for each device. It assumes the NIC
3151 * is already up and running. The link information for each
3152 * interface is passed in link_info.
3153 */
3154static int setup_nic_devices(struct octeon_device *octeon_dev)
3155{
3156 struct lio *lio = NULL;
3157 struct net_device *netdev;
3158 u8 mac[6], i, j;
3159 struct octeon_soft_command *sc;
3160 struct liquidio_if_cfg_context *ctx;
3161 struct liquidio_if_cfg_resp *resp;
3162 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003163 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003164 int num_cpus = num_online_cpus();
3165 union oct_nic_if_cfg if_cfg;
3166 unsigned int base_queue;
3167 unsigned int gmx_port_id;
3168 u32 resp_size, ctx_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003169 u32 ifidx_or_pfnum;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003170
3171 /* This is to handle link status changes */
3172 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3173 OPCODE_NIC_INFO,
3174 lio_nic_info, octeon_dev);
3175
3176 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3177 * They are handled directly.
3178 */
3179 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3180 free_netbuf);
3181
3182 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3183 free_netsgbuf);
3184
3185 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3186 free_netsgbuf_with_resp);
3187
3188 for (i = 0; i < octeon_dev->ifcount; i++) {
3189 resp_size = sizeof(struct liquidio_if_cfg_resp);
3190 ctx_size = sizeof(struct liquidio_if_cfg_context);
3191 sc = (struct octeon_soft_command *)
3192 octeon_alloc_soft_command(octeon_dev, 0,
3193 resp_size, ctx_size);
3194 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3195 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
3196
3197 num_iqueues =
3198 CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3199 num_oqueues =
3200 CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3201 base_queue =
3202 CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
3203 gmx_port_id =
3204 CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003205 ifidx_or_pfnum = i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003206 if (num_iqueues > num_cpus)
3207 num_iqueues = num_cpus;
3208 if (num_oqueues > num_cpus)
3209 num_oqueues = num_cpus;
3210 dev_dbg(&octeon_dev->pci_dev->dev,
3211 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003212 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003213 ACCESS_ONCE(ctx->cond) = 0;
3214 ctx->octeon_id = lio_get_device_id(octeon_dev);
3215 init_waitqueue_head(&ctx->wc);
3216
3217 if_cfg.u64 = 0;
3218 if_cfg.s.num_iqueues = num_iqueues;
3219 if_cfg.s.num_oqueues = num_oqueues;
3220 if_cfg.s.base_queue = base_queue;
3221 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003222
3223 sc->iq_no = 0;
3224
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003225 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003226 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003227 if_cfg.u64, 0);
3228
3229 sc->callback = if_cfg_callback;
3230 sc->callback_arg = sc;
3231 sc->wait_time = 1000;
3232
3233 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003234 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003235 dev_err(&octeon_dev->pci_dev->dev,
3236 "iq/oq config failed status: %x\n",
3237 retval);
3238 /* Soft instr is freed by driver in case of failure. */
3239 goto setup_nic_dev_fail;
3240 }
3241
3242 /* Sleep on a wait queue till the cond flag indicates that the
3243 * response arrived or timed-out.
3244 */
3245 sleep_cond(&ctx->wc, &ctx->cond);
3246 retval = resp->status;
3247 if (retval) {
3248 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3249 goto setup_nic_dev_fail;
3250 }
3251
3252 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3253 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3254
3255 num_iqueues = hweight64(resp->cfg_info.iqmask);
3256 num_oqueues = hweight64(resp->cfg_info.oqmask);
3257
3258 if (!(num_iqueues) || !(num_oqueues)) {
3259 dev_err(&octeon_dev->pci_dev->dev,
3260 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3261 resp->cfg_info.iqmask,
3262 resp->cfg_info.oqmask);
3263 goto setup_nic_dev_fail;
3264 }
3265 dev_dbg(&octeon_dev->pci_dev->dev,
3266 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3267 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3268 num_iqueues, num_oqueues);
3269 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3270
3271 if (!netdev) {
3272 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3273 goto setup_nic_dev_fail;
3274 }
3275
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003276 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003277
3278 if (num_iqueues > 1)
3279 lionetdevops.ndo_select_queue = select_q;
3280
3281 /* Associate the routines that will handle different
3282 * netdev tasks.
3283 */
3284 netdev->netdev_ops = &lionetdevops;
3285
3286 lio = GET_LIO(netdev);
3287
3288 memset(lio, 0, sizeof(struct lio));
3289
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003290 lio->ifidx = ifidx_or_pfnum;
3291
3292 props = &octeon_dev->props[i];
3293 props->gmxport = resp->cfg_info.linfo.gmxport;
3294 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003295
3296 lio->linfo.num_rxpciq = num_oqueues;
3297 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003298 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003299 lio->linfo.rxpciq[j].u64 =
3300 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003301 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003302 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003303 lio->linfo.txpciq[j].u64 =
3304 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003305 }
3306 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3307 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3308 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3309
3310 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3311
3312 lio->dev_capability = NETIF_F_HIGHDMA
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003313 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3314 | NETIF_F_SG | NETIF_F_RXCSUM
3315 | NETIF_F_GRO
3316 | NETIF_F_TSO | NETIF_F_TSO6
3317 | NETIF_F_LRO;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003318 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3319
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003320 netdev->vlan_features = lio->dev_capability;
3321 /* Add any unchangeable hw features */
3322 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_RX |
3323 NETIF_F_HW_VLAN_CTAG_TX;
3324
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003325 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3326
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003327 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003328 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3329 netdev->hw_features = netdev->hw_features &
3330 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003331
3332 /* Point to the properties for octeon device to which this
3333 * interface belongs.
3334 */
3335 lio->oct_dev = octeon_dev;
3336 lio->octprops = props;
3337 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338
3339 dev_dbg(&octeon_dev->pci_dev->dev,
3340 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3341 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3342
3343 /* 64-bit swap required on LE machines */
3344 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3345 for (j = 0; j < 6; j++)
3346 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3347
3348 /* Copy MAC Address to OS network device structure */
3349
3350 ether_addr_copy(netdev->dev_addr, mac);
3351
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003352 /* By default all interfaces on a single Octeon uses the same
3353 * tx and rx queues
3354 */
3355 lio->txq = lio->linfo.txpciq[0].s.q_no;
3356 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003357 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003358 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3359 goto setup_nic_dev_fail;
3360 }
3361
3362 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3363
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003364 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3365 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3366
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003367 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003368 dev_err(&octeon_dev->pci_dev->dev,
3369 "Gather list allocation failed\n");
3370 goto setup_nic_dev_fail;
3371 }
3372
3373 /* Register ethtool support */
3374 liquidio_set_ethtool_ops(netdev);
3375
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003376 if (netdev->features & NETIF_F_LRO)
3377 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3378 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003379
3380 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003381 liquidio_set_feature(netdev, OCTNET_CMD_VERBOSE_ENABLE,
3382 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003383
3384 /* Register the network device with the OS */
3385 if (register_netdev(netdev)) {
3386 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3387 goto setup_nic_dev_fail;
3388 }
3389
3390 dev_dbg(&octeon_dev->pci_dev->dev,
3391 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3392 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3393 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003394 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003395
3396 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3397
3398 dev_dbg(&octeon_dev->pci_dev->dev,
3399 "NIC ifidx:%d Setup successful\n", i);
3400
3401 octeon_free_soft_command(octeon_dev, sc);
3402 }
3403
3404 return 0;
3405
3406setup_nic_dev_fail:
3407
3408 octeon_free_soft_command(octeon_dev, sc);
3409
3410 while (i--) {
3411 dev_err(&octeon_dev->pci_dev->dev,
3412 "NIC ifidx:%d Setup failed\n", i);
3413 liquidio_destroy_nic_device(octeon_dev, i);
3414 }
3415 return -ENODEV;
3416}
3417
3418/**
3419 * \brief initialize the NIC
3420 * @param oct octeon device
3421 *
3422 * This initialization routine is called once the Octeon device application is
3423 * up and running
3424 */
3425static int liquidio_init_nic_module(struct octeon_device *oct)
3426{
3427 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003428 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003429 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3430
3431 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3432
3433 /* only default iq and oq were initialized
3434 * initialize the rest as well
3435 */
3436 /* run port_config command for each port */
3437 oct->ifcount = num_nic_ports;
3438
3439 memset(oct->props, 0,
3440 sizeof(struct octdev_props) * num_nic_ports);
3441
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003442 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3443 oct->props[i].gmxport = -1;
3444
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003445 retval = setup_nic_devices(oct);
3446 if (retval) {
3447 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3448 goto octnet_init_failure;
3449 }
3450
3451 liquidio_ptp_init(oct);
3452
3453 /* Initialize interrupt moderation params */
3454 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
3455 intrmod_cfg->intrmod_enable = 1;
3456 intrmod_cfg->intrmod_check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3457 intrmod_cfg->intrmod_maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3458 intrmod_cfg->intrmod_minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3459 intrmod_cfg->intrmod_maxcnt_trigger = LIO_INTRMOD_MAXCNT_TRIGGER;
3460 intrmod_cfg->intrmod_maxtmr_trigger = LIO_INTRMOD_MAXTMR_TRIGGER;
3461 intrmod_cfg->intrmod_mintmr_trigger = LIO_INTRMOD_MINTMR_TRIGGER;
3462 intrmod_cfg->intrmod_mincnt_trigger = LIO_INTRMOD_MINCNT_TRIGGER;
3463
3464 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3465
3466 return retval;
3467
3468octnet_init_failure:
3469
3470 oct->ifcount = 0;
3471
3472 return retval;
3473}
3474
3475/**
3476 * \brief starter callback that invokes the remaining initialization work after
3477 * the NIC is up and running.
3478 * @param octptr work struct work_struct
3479 */
3480static void nic_starter(struct work_struct *work)
3481{
3482 struct octeon_device *oct;
3483 struct cavium_wk *wk = (struct cavium_wk *)work;
3484
3485 oct = (struct octeon_device *)wk->ctxptr;
3486
3487 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3488 return;
3489
3490 /* If the status of the device is CORE_OK, the core
3491 * application has reported its application type. Call
3492 * any registered handlers now and move to the RUNNING
3493 * state.
3494 */
3495 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3496 schedule_delayed_work(&oct->nic_poll_work.work,
3497 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3498 return;
3499 }
3500
3501 atomic_set(&oct->status, OCT_DEV_RUNNING);
3502
3503 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3504 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3505
3506 if (liquidio_init_nic_module(oct))
3507 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3508 else
3509 handshake[oct->octeon_id].started_ok = 1;
3510 } else {
3511 dev_err(&oct->pci_dev->dev,
3512 "Unexpected application running on NIC (%d). Check firmware.\n",
3513 oct->app_mode);
3514 }
3515
3516 complete(&handshake[oct->octeon_id].started);
3517}
3518
3519/**
3520 * \brief Device initialization for each Octeon device that is probed
3521 * @param octeon_dev octeon device
3522 */
3523static int octeon_device_init(struct octeon_device *octeon_dev)
3524{
3525 int j, ret;
3526 struct octeon_device_priv *oct_priv =
3527 (struct octeon_device_priv *)octeon_dev->priv;
3528 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3529
3530 /* Enable access to the octeon device and make its DMA capability
3531 * known to the OS.
3532 */
3533 if (octeon_pci_os_setup(octeon_dev))
3534 return 1;
3535
3536 /* Identify the Octeon type and map the BAR address space. */
3537 if (octeon_chip_specific_setup(octeon_dev)) {
3538 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3539 return 1;
3540 }
3541
3542 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3543
3544 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3545
3546 /* Do a soft reset of the Octeon device. */
3547 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3548 return 1;
3549
3550 /* Initialize the dispatch mechanism used to push packets arriving on
3551 * Octeon Output queues.
3552 */
3553 if (octeon_init_dispatch_list(octeon_dev))
3554 return 1;
3555
3556 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3557 OPCODE_NIC_CORE_DRV_ACTIVE,
3558 octeon_core_drv_init,
3559 octeon_dev);
3560
3561 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3562 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3563 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3564 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3565
3566 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3567
3568 octeon_set_io_queues_off(octeon_dev);
3569
3570 /* Setup the data structures that manage this Octeon's Input queues. */
3571 if (octeon_setup_instr_queues(octeon_dev)) {
3572 dev_err(&octeon_dev->pci_dev->dev,
3573 "instruction queue initialization failed\n");
3574 /* On error, release any previously allocated queues */
3575 for (j = 0; j < octeon_dev->num_iqs; j++)
3576 octeon_delete_instr_queue(octeon_dev, j);
3577 return 1;
3578 }
3579 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3580
3581 /* Initialize soft command buffer pool
3582 */
3583 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3584 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3585 return 1;
3586 }
3587 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3588
3589 /* Initialize lists to manage the requests of different types that
3590 * arrive from user & kernel applications for this octeon device.
3591 */
3592 if (octeon_setup_response_list(octeon_dev)) {
3593 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3594 return 1;
3595 }
3596 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3597
3598 if (octeon_setup_output_queues(octeon_dev)) {
3599 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
3600 /* Release any previously allocated queues */
3601 for (j = 0; j < octeon_dev->num_oqs; j++)
3602 octeon_delete_droq(octeon_dev, j);
3603 }
3604
3605 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
3606
3607 /* The input and output queue registers were setup earlier (the queues
3608 * were not enabled). Any additional registers that need to be
3609 * programmed should be done now.
3610 */
3611 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3612 if (ret) {
3613 dev_err(&octeon_dev->pci_dev->dev,
3614 "Failed to configure device registers\n");
3615 return ret;
3616 }
3617
3618 /* Initialize the tasklet that handles output queue packet processing.*/
3619 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
3620 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
3621 (unsigned long)octeon_dev);
3622
3623 /* Setup the interrupt handler and record the INT SUM register address
3624 */
3625 octeon_setup_interrupt(octeon_dev);
3626
3627 /* Enable Octeon device interrupts */
3628 octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
3629
3630 /* Enable the input and output queues for this Octeon device */
3631 octeon_dev->fn_list.enable_io_queues(octeon_dev);
3632
3633 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
3634
3635 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
3636
3637 if (ddr_timeout == 0) {
3638 dev_info(&octeon_dev->pci_dev->dev,
3639 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
3640 }
3641
3642 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
3643
3644 /* Wait for the octeon to initialize DDR after the soft-reset. */
3645 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
3646 if (ret) {
3647 dev_err(&octeon_dev->pci_dev->dev,
3648 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
3649 ret);
3650 return 1;
3651 }
3652
3653 if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
3654 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
3655 return 1;
3656 }
3657
3658 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
3659 ret = octeon_init_consoles(octeon_dev);
3660 if (ret) {
3661 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
3662 return 1;
3663 }
3664 ret = octeon_add_console(octeon_dev, 0);
3665 if (ret) {
3666 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
3667 return 1;
3668 }
3669
3670 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
3671
3672 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
3673 ret = load_firmware(octeon_dev);
3674 if (ret) {
3675 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
3676 return 1;
3677 }
3678
3679 handshake[octeon_dev->octeon_id].init_ok = 1;
3680 complete(&handshake[octeon_dev->octeon_id].init);
3681
3682 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
3683
3684 /* Send Credit for Octeon Output queues. Credits are always sent after
3685 * the output queue is enabled.
3686 */
3687 for (j = 0; j < octeon_dev->num_oqs; j++)
3688 writel(octeon_dev->droq[j]->max_count,
3689 octeon_dev->droq[j]->pkts_credit_reg);
3690
3691 /* Packets can start arriving on the output queues from this point. */
3692
3693 return 0;
3694}
3695
3696/**
3697 * \brief Exits the module
3698 */
3699static void __exit liquidio_exit(void)
3700{
3701 liquidio_deinit_pci();
3702
3703 pr_info("LiquidIO network module is now unloaded\n");
3704}
3705
3706module_init(liquidio_init);
3707module_exit(liquidio_exit);