blob: 4a5629f8d23881d5609090f21efe971e793ae833 [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>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070040#include <net/vxlan.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070041#include "octeon_config.h"
42#include "liquidio_common.h"
43#include "octeon_droq.h"
44#include "octeon_iq.h"
45#include "response_manager.h"
46#include "octeon_device.h"
47#include "octeon_nic.h"
48#include "octeon_main.h"
49#include "octeon_network.h"
50#include "cn66xx_regs.h"
51#include "cn66xx_device.h"
52#include "cn68xx_regs.h"
53#include "cn68xx_device.h"
54#include "liquidio_image.h"
55
56MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
57MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
58MODULE_LICENSE("GPL");
59MODULE_VERSION(LIQUIDIO_VERSION);
60MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
61MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
62MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
63
64static int ddr_timeout = 10000;
65module_param(ddr_timeout, int, 0644);
66MODULE_PARM_DESC(ddr_timeout,
67 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
68
69static u32 console_bitmask;
70module_param(console_bitmask, int, 0644);
71MODULE_PARM_DESC(console_bitmask,
72 "Bitmask indicating which consoles have debug output redirected to syslog.");
73
74#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
75
Raghu Vatsavayi1f164712016-06-21 22:53:11 -070076#define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \
77 (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count)
78
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070079static int debug = -1;
80module_param(debug, int, 0644);
81MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
82
83static char fw_type[LIO_MAX_FW_TYPE_LEN];
84module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
85MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
86
87static int conf_type;
88module_param(conf_type, int, 0);
89MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
90
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070091static int ptp_enable = 1;
92
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070093/* Bit mask values for lio->ifstate */
94#define LIO_IFSTATE_DROQ_OPS 0x01
95#define LIO_IFSTATE_REGISTERED 0x02
96#define LIO_IFSTATE_RUNNING 0x04
97#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
98
99/* Polling interval for determining when NIC application is alive */
100#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
101
102/* runtime link query interval */
103#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
104
105struct liquidio_if_cfg_context {
106 int octeon_id;
107
108 wait_queue_head_t wc;
109
110 int cond;
111};
112
113struct liquidio_if_cfg_resp {
114 u64 rh;
115 struct liquidio_if_cfg_info cfg_info;
116 u64 status;
117};
118
119struct oct_link_status_resp {
120 u64 rh;
121 struct oct_link_info link_info;
122 u64 status;
123};
124
125struct oct_timestamp_resp {
126 u64 rh;
127 u64 timestamp;
128 u64 status;
129};
130
131#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
132
133union tx_info {
134 u64 u64;
135 struct {
136#ifdef __BIG_ENDIAN_BITFIELD
137 u16 gso_size;
138 u16 gso_segs;
139 u32 reserved;
140#else
141 u32 reserved;
142 u16 gso_segs;
143 u16 gso_size;
144#endif
145 } s;
146};
147
148/** Octeon device properties to be used by the NIC module.
149 * Each octeon device in the system will be represented
150 * by this structure in the NIC module.
151 */
152
153#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
154
155#define OCTNIC_GSO_MAX_HEADER_SIZE 128
156#define OCTNIC_GSO_MAX_SIZE (GSO_MAX_SIZE - OCTNIC_GSO_MAX_HEADER_SIZE)
157
158/** Structure of a node in list of gather components maintained by
159 * NIC driver for each network device.
160 */
161struct octnic_gather {
162 /** List manipulation. Next and prev pointers. */
163 struct list_head list;
164
165 /** Size of the gather component at sg in bytes. */
166 int sg_size;
167
168 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
169 int adjust;
170
171 /** Gather component that can accommodate max sized fragment list
172 * received from the IP layer.
173 */
174 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700175
176 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700177};
178
179/** This structure is used by NIC driver to store information required
180 * to free the sk_buff when the packet has been fetched by Octeon.
181 * Bytes offset below assume worst-case of a 64-bit system.
182 */
183struct octnet_buf_free_info {
184 /** Bytes 1-8. Pointer to network device private structure. */
185 struct lio *lio;
186
187 /** Bytes 9-16. Pointer to sk_buff. */
188 struct sk_buff *skb;
189
190 /** Bytes 17-24. Pointer to gather list. */
191 struct octnic_gather *g;
192
193 /** Bytes 25-32. Physical address of skb->data or gather list. */
194 u64 dptr;
195
196 /** Bytes 33-47. Piggybacked soft command, if any */
197 struct octeon_soft_command *sc;
198};
199
200struct handshake {
201 struct completion init;
202 struct completion started;
203 struct pci_dev *pci_dev;
204 int init_ok;
205 int started_ok;
206};
207
208struct octeon_device_priv {
209 /** Tasklet structures for this device. */
210 struct tasklet_struct droq_tasklet;
211 unsigned long napi_mask;
212};
213
214static int octeon_device_init(struct octeon_device *);
215static void liquidio_remove(struct pci_dev *pdev);
216static int liquidio_probe(struct pci_dev *pdev,
217 const struct pci_device_id *ent);
218
219static struct handshake handshake[MAX_OCTEON_DEVICES];
220static struct completion first_stage;
221
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700222static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700223{
224 int q_no;
225 int reschedule = 0;
226 struct octeon_device *oct = (struct octeon_device *)pdev;
227 struct octeon_device_priv *oct_priv =
228 (struct octeon_device_priv *)oct->priv;
229
230 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700231 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
232 if (!(oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700233 continue;
234 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
235 MAX_PACKET_BUDGET);
236 }
237
238 if (reschedule)
239 tasklet_schedule(&oct_priv->droq_tasklet);
240}
241
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700242static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700243{
244 struct octeon_device_priv *oct_priv =
245 (struct octeon_device_priv *)oct->priv;
246 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
247 int i;
248
249 do {
250 pending_pkts = 0;
251
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700252 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
253 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700254 continue;
255 pkt_cnt += octeon_droq_check_hw_for_pkts(oct,
256 oct->droq[i]);
257 }
258 if (pkt_cnt > 0) {
259 pending_pkts += pkt_cnt;
260 tasklet_schedule(&oct_priv->droq_tasklet);
261 }
262 pkt_cnt = 0;
263 schedule_timeout_uninterruptible(1);
264
265 } while (retry-- && pending_pkts);
266
267 return pkt_cnt;
268}
269
270void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
271 unsigned int bytes_compl)
272{
273 struct netdev_queue *netdev_queue = txq;
274
275 netdev_tx_completed_queue(netdev_queue, pkts_compl, bytes_compl);
276}
277
278void octeon_update_tx_completion_counters(void *buf, int reqtype,
279 unsigned int *pkts_compl,
280 unsigned int *bytes_compl)
281{
282 struct octnet_buf_free_info *finfo;
283 struct sk_buff *skb = NULL;
284 struct octeon_soft_command *sc;
285
286 switch (reqtype) {
287 case REQTYPE_NORESP_NET:
288 case REQTYPE_NORESP_NET_SG:
289 finfo = buf;
290 skb = finfo->skb;
291 break;
292
293 case REQTYPE_RESP_NET_SG:
294 case REQTYPE_RESP_NET:
295 sc = buf;
296 skb = sc->callback_arg;
297 break;
298
299 default:
300 return;
301 }
302
303 (*pkts_compl)++;
304 *bytes_compl += skb->len;
305}
306
307void octeon_report_sent_bytes_to_bql(void *buf, int reqtype)
308{
309 struct octnet_buf_free_info *finfo;
310 struct sk_buff *skb;
311 struct octeon_soft_command *sc;
312 struct netdev_queue *txq;
313
314 switch (reqtype) {
315 case REQTYPE_NORESP_NET:
316 case REQTYPE_NORESP_NET_SG:
317 finfo = buf;
318 skb = finfo->skb;
319 break;
320
321 case REQTYPE_RESP_NET_SG:
322 case REQTYPE_RESP_NET:
323 sc = buf;
324 skb = sc->callback_arg;
325 break;
326
327 default:
328 return;
329 }
330
331 txq = netdev_get_tx_queue(skb->dev, skb_get_queue_mapping(skb));
332 netdev_tx_sent_queue(txq, skb->len);
333}
334
335int octeon_console_debug_enabled(u32 console)
336{
337 return (console_bitmask >> (console)) & 0x1;
338}
339
340/**
341 * \brief Forces all IO queues off on a given device
342 * @param oct Pointer to Octeon device
343 */
344static void force_io_queues_off(struct octeon_device *oct)
345{
346 if ((oct->chip_id == OCTEON_CN66XX) ||
347 (oct->chip_id == OCTEON_CN68XX)) {
348 /* Reset the Enable bits for Input Queues. */
349 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
350
351 /* Reset the Enable bits for Output Queues. */
352 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
353 }
354}
355
356/**
357 * \brief wait for all pending requests to complete
358 * @param oct Pointer to Octeon device
359 *
360 * Called during shutdown sequence
361 */
362static int wait_for_pending_requests(struct octeon_device *oct)
363{
364 int i, pcount = 0;
365
366 for (i = 0; i < 100; i++) {
367 pcount =
368 atomic_read(&oct->response_list
369 [OCTEON_ORDERED_SC_LIST].pending_req_count);
370 if (pcount)
371 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700372 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700373 break;
374 }
375
376 if (pcount)
377 return 1;
378
379 return 0;
380}
381
382/**
383 * \brief Cause device to go quiet so it can be safely removed/reset/etc
384 * @param oct Pointer to Octeon device
385 */
386static inline void pcierror_quiesce_device(struct octeon_device *oct)
387{
388 int i;
389
390 /* Disable the input and output queues now. No more packets will
391 * arrive from Octeon, but we should wait for all packet processing
392 * to finish.
393 */
394 force_io_queues_off(oct);
395
396 /* To allow for in-flight requests */
397 schedule_timeout_uninterruptible(100);
398
399 if (wait_for_pending_requests(oct))
400 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
401
402 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700403 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700404 struct octeon_instr_queue *iq;
405
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700406 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700407 continue;
408 iq = oct->instr_queue[i];
409
410 if (atomic_read(&iq->instr_pending)) {
411 spin_lock_bh(&iq->lock);
412 iq->fill_cnt = 0;
413 iq->octeon_read_index = iq->host_write_index;
414 iq->stats.instr_processed +=
415 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700416 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700417 spin_unlock_bh(&iq->lock);
418 }
419 }
420
421 /* Force all pending ordered list requests to time out. */
422 lio_process_ordered_list(oct, 1);
423
424 /* We do not need to wait for output queue packets to be processed. */
425}
426
427/**
428 * \brief Cleanup PCI AER uncorrectable error status
429 * @param dev Pointer to PCI device
430 */
431static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
432{
433 int pos = 0x100;
434 u32 status, mask;
435
436 pr_info("%s :\n", __func__);
437
438 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
439 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
440 if (dev->error_state == pci_channel_io_normal)
441 status &= ~mask; /* Clear corresponding nonfatal bits */
442 else
443 status &= mask; /* Clear corresponding fatal bits */
444 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
445}
446
447/**
448 * \brief Stop all PCI IO to a given device
449 * @param dev Pointer to Octeon device
450 */
451static void stop_pci_io(struct octeon_device *oct)
452{
453 /* No more instructions will be forwarded. */
454 atomic_set(&oct->status, OCT_DEV_IN_RESET);
455
456 pci_disable_device(oct->pci_dev);
457
458 /* Disable interrupts */
459 oct->fn_list.disable_interrupt(oct->chip);
460
461 pcierror_quiesce_device(oct);
462
463 /* Release the interrupt line */
464 free_irq(oct->pci_dev->irq, oct);
465
466 if (oct->flags & LIO_FLAG_MSI_ENABLED)
467 pci_disable_msi(oct->pci_dev);
468
469 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
470 lio_get_state_string(&oct->status));
471
472 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
473 /* making it a common function for all OCTEON models */
474 cleanup_aer_uncorrect_error_status(oct->pci_dev);
475}
476
477/**
478 * \brief called when PCI error is detected
479 * @param pdev Pointer to PCI device
480 * @param state The current pci connection state
481 *
482 * This function is called after a PCI bus error affecting
483 * this device has been detected.
484 */
485static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
486 pci_channel_state_t state)
487{
488 struct octeon_device *oct = pci_get_drvdata(pdev);
489
490 /* Non-correctable Non-fatal errors */
491 if (state == pci_channel_io_normal) {
492 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
493 cleanup_aer_uncorrect_error_status(oct->pci_dev);
494 return PCI_ERS_RESULT_CAN_RECOVER;
495 }
496
497 /* Non-correctable Fatal errors */
498 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
499 stop_pci_io(oct);
500
501 /* Always return a DISCONNECT. There is no support for recovery but only
502 * for a clean shutdown.
503 */
504 return PCI_ERS_RESULT_DISCONNECT;
505}
506
507/**
508 * \brief mmio handler
509 * @param pdev Pointer to PCI device
510 */
511static pci_ers_result_t liquidio_pcie_mmio_enabled(struct pci_dev *pdev)
512{
513 /* We should never hit this since we never ask for a reset for a Fatal
514 * Error. We always return DISCONNECT in io_error above.
515 * But play safe and return RECOVERED for now.
516 */
517 return PCI_ERS_RESULT_RECOVERED;
518}
519
520/**
521 * \brief called after the pci bus has been reset.
522 * @param pdev Pointer to PCI device
523 *
524 * Restart the card from scratch, as if from a cold-boot. Implementation
525 * resembles the first-half of the octeon_resume routine.
526 */
527static pci_ers_result_t liquidio_pcie_slot_reset(struct pci_dev *pdev)
528{
529 /* We should never hit this since we never ask for a reset for a Fatal
530 * Error. We always return DISCONNECT in io_error above.
531 * But play safe and return RECOVERED for now.
532 */
533 return PCI_ERS_RESULT_RECOVERED;
534}
535
536/**
537 * \brief called when traffic can start flowing again.
538 * @param pdev Pointer to PCI device
539 *
540 * This callback is called when the error recovery driver tells us that
541 * its OK to resume normal operation. Implementation resembles the
542 * second-half of the octeon_resume routine.
543 */
544static void liquidio_pcie_resume(struct pci_dev *pdev)
545{
546 /* Nothing to be done here. */
547}
548
549#ifdef CONFIG_PM
550/**
551 * \brief called when suspending
552 * @param pdev Pointer to PCI device
553 * @param state state to suspend to
554 */
555static int liquidio_suspend(struct pci_dev *pdev, pm_message_t state)
556{
557 return 0;
558}
559
560/**
561 * \brief called when resuming
562 * @param pdev Pointer to PCI device
563 */
564static int liquidio_resume(struct pci_dev *pdev)
565{
566 return 0;
567}
568#endif
569
570/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100571static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700572 .error_detected = liquidio_pcie_error_detected,
573 .mmio_enabled = liquidio_pcie_mmio_enabled,
574 .slot_reset = liquidio_pcie_slot_reset,
575 .resume = liquidio_pcie_resume,
576};
577
578static const struct pci_device_id liquidio_pci_tbl[] = {
579 { /* 68xx */
580 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
581 },
582 { /* 66xx */
583 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
584 },
585 {
586 0, 0, 0, 0, 0, 0, 0
587 }
588};
589MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
590
591static struct pci_driver liquidio_pci_driver = {
592 .name = "LiquidIO",
593 .id_table = liquidio_pci_tbl,
594 .probe = liquidio_probe,
595 .remove = liquidio_remove,
596 .err_handler = &liquidio_err_handler, /* For AER */
597
598#ifdef CONFIG_PM
599 .suspend = liquidio_suspend,
600 .resume = liquidio_resume,
601#endif
602
603};
604
605/**
606 * \brief register PCI driver
607 */
608static int liquidio_init_pci(void)
609{
610 return pci_register_driver(&liquidio_pci_driver);
611}
612
613/**
614 * \brief unregister PCI driver
615 */
616static void liquidio_deinit_pci(void)
617{
618 pci_unregister_driver(&liquidio_pci_driver);
619}
620
621/**
622 * \brief check interface state
623 * @param lio per-network private data
624 * @param state_flag flag state to check
625 */
626static inline int ifstate_check(struct lio *lio, int state_flag)
627{
628 return atomic_read(&lio->ifstate) & state_flag;
629}
630
631/**
632 * \brief set interface state
633 * @param lio per-network private data
634 * @param state_flag flag state to set
635 */
636static inline void ifstate_set(struct lio *lio, int state_flag)
637{
638 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
639}
640
641/**
642 * \brief clear interface state
643 * @param lio per-network private data
644 * @param state_flag flag state to clear
645 */
646static inline void ifstate_reset(struct lio *lio, int state_flag)
647{
648 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
649}
650
651/**
652 * \brief Stop Tx queues
653 * @param netdev network device
654 */
655static inline void txqs_stop(struct net_device *netdev)
656{
657 if (netif_is_multiqueue(netdev)) {
658 int i;
659
660 for (i = 0; i < netdev->num_tx_queues; i++)
661 netif_stop_subqueue(netdev, i);
662 } else {
663 netif_stop_queue(netdev);
664 }
665}
666
667/**
668 * \brief Start Tx queues
669 * @param netdev network device
670 */
671static inline void txqs_start(struct net_device *netdev)
672{
673 if (netif_is_multiqueue(netdev)) {
674 int i;
675
676 for (i = 0; i < netdev->num_tx_queues; i++)
677 netif_start_subqueue(netdev, i);
678 } else {
679 netif_start_queue(netdev);
680 }
681}
682
683/**
684 * \brief Wake Tx queues
685 * @param netdev network device
686 */
687static inline void txqs_wake(struct net_device *netdev)
688{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700689 struct lio *lio = GET_LIO(netdev);
690
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700691 if (netif_is_multiqueue(netdev)) {
692 int i;
693
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700694 for (i = 0; i < netdev->num_tx_queues; i++) {
695 int qno = lio->linfo.txpciq[i %
696 (lio->linfo.num_txpciq)].s.q_no;
697
698 if (__netif_subqueue_stopped(netdev, i)) {
699 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
700 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700701 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700702 }
703 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700704 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700705 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
706 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700707 netif_wake_queue(netdev);
708 }
709}
710
711/**
712 * \brief Stop Tx queue
713 * @param netdev network device
714 */
715static void stop_txq(struct net_device *netdev)
716{
717 txqs_stop(netdev);
718}
719
720/**
721 * \brief Start Tx queue
722 * @param netdev network device
723 */
724static void start_txq(struct net_device *netdev)
725{
726 struct lio *lio = GET_LIO(netdev);
727
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700728 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700729 txqs_start(netdev);
730 return;
731 }
732}
733
734/**
735 * \brief Wake a queue
736 * @param netdev network device
737 * @param q which queue to wake
738 */
739static inline void wake_q(struct net_device *netdev, int q)
740{
741 if (netif_is_multiqueue(netdev))
742 netif_wake_subqueue(netdev, q);
743 else
744 netif_wake_queue(netdev);
745}
746
747/**
748 * \brief Stop a queue
749 * @param netdev network device
750 * @param q which queue to stop
751 */
752static inline void stop_q(struct net_device *netdev, int q)
753{
754 if (netif_is_multiqueue(netdev))
755 netif_stop_subqueue(netdev, q);
756 else
757 netif_stop_queue(netdev);
758}
759
760/**
761 * \brief Check Tx queue status, and take appropriate action
762 * @param lio per-network private data
763 * @returns 0 if full, number of queues woken up otherwise
764 */
765static inline int check_txq_status(struct lio *lio)
766{
767 int ret_val = 0;
768
769 if (netif_is_multiqueue(lio->netdev)) {
770 int numqs = lio->netdev->num_tx_queues;
771 int q, iq = 0;
772
773 /* check each sub-queue state */
774 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700775 iq = lio->linfo.txpciq[q %
776 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700777 if (octnet_iq_is_full(lio->oct_dev, iq))
778 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700779 if (__netif_subqueue_stopped(lio->netdev, q)) {
780 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700781 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
782 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700783 ret_val++;
784 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700785 }
786 } else {
787 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
788 return 0;
789 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700790 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
791 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700792 ret_val = 1;
793 }
794 return ret_val;
795}
796
797/**
798 * Remove the node at the head of the list. The list would be empty at
799 * the end of this call if there are no more nodes in the list.
800 */
801static inline struct list_head *list_delete_head(struct list_head *root)
802{
803 struct list_head *node;
804
805 if ((root->prev == root) && (root->next == root))
806 node = NULL;
807 else
808 node = root->next;
809
810 if (node)
811 list_del(node);
812
813 return node;
814}
815
816/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700817 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700818 * @param lio per-network private data
819 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700820static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700821{
822 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700823 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700824
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700825 if (!lio->glist)
826 return;
827
828 for (i = 0; i < lio->linfo.num_txpciq; i++) {
829 do {
830 g = (struct octnic_gather *)
831 list_delete_head(&lio->glist[i]);
832 if (g) {
833 if (g->sg) {
834 dma_unmap_single(&lio->oct_dev->
835 pci_dev->dev,
836 g->sg_dma_ptr,
837 g->sg_size,
838 DMA_TO_DEVICE);
839 kfree((void *)((unsigned long)g->sg -
840 g->adjust));
841 }
842 kfree(g);
843 }
844 } while (g);
845 }
846
847 kfree((void *)lio->glist);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700848}
849
850/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700851 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700852 * @param lio per-network private data
853 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700854static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700855{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700856 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700857 struct octnic_gather *g;
858
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700859 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
860 GFP_KERNEL);
861 if (!lio->glist_lock)
862 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700863
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700864 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
865 GFP_KERNEL);
866 if (!lio->glist) {
867 kfree((void *)lio->glist_lock);
868 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700869 }
870
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700871 for (i = 0; i < num_iqs; i++) {
872 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700873
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700874 spin_lock_init(&lio->glist_lock[i]);
875
876 INIT_LIST_HEAD(&lio->glist[i]);
877
878 for (j = 0; j < lio->tx_qsize; j++) {
879 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
880 numa_node);
881 if (!g)
882 g = kzalloc(sizeof(*g), GFP_KERNEL);
883 if (!g)
884 break;
885
886 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
887 OCT_SG_ENTRY_SIZE);
888
889 g->sg = kmalloc_node(g->sg_size + 8,
890 GFP_KERNEL, numa_node);
891 if (!g->sg)
892 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
893 if (!g->sg) {
894 kfree(g);
895 break;
896 }
897
898 /* The gather component should be aligned on 64-bit
899 * boundary
900 */
901 if (((unsigned long)g->sg) & 7) {
902 g->adjust = 8 - (((unsigned long)g->sg) & 7);
903 g->sg = (struct octeon_sg_entry *)
904 ((unsigned long)g->sg + g->adjust);
905 }
906 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
907 g->sg, g->sg_size,
908 DMA_TO_DEVICE);
909 if (dma_mapping_error(&oct->pci_dev->dev,
910 g->sg_dma_ptr)) {
911 kfree((void *)((unsigned long)g->sg -
912 g->adjust));
913 kfree(g);
914 break;
915 }
916
917 list_add_tail(&g->list, &lio->glist[i]);
918 }
919
920 if (j != lio->tx_qsize) {
921 delete_glists(lio);
922 return 1;
923 }
924 }
925
926 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700927}
928
929/**
930 * \brief Print link information
931 * @param netdev network device
932 */
933static void print_link_info(struct net_device *netdev)
934{
935 struct lio *lio = GET_LIO(netdev);
936
937 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
938 struct oct_link_info *linfo = &lio->linfo;
939
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700940 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700941 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
942 linfo->link.s.speed,
943 (linfo->link.s.duplex) ? "Full" : "Half");
944 } else {
945 netif_info(lio, link, lio->netdev, "Link Down\n");
946 }
947 }
948}
949
950/**
951 * \brief Update link status
952 * @param netdev network device
953 * @param ls link status structure
954 *
955 * Called on receipt of a link status response from the core application to
956 * update each interface's link status.
957 */
958static inline void update_link_status(struct net_device *netdev,
959 union oct_link_status *ls)
960{
961 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700962 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700963
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700964 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700965
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700966 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700967 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700968 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700969
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700970 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700971 netif_carrier_on(netdev);
972 /* start_txq(netdev); */
973 txqs_wake(netdev);
974 } else {
975 netif_carrier_off(netdev);
976 stop_txq(netdev);
977 }
978 }
979}
980
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700981/* Runs in interrupt context. */
982static void update_txq_status(struct octeon_device *oct, int iq_num)
983{
984 struct net_device *netdev;
985 struct lio *lio;
986 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
987
988 /*octeon_update_iq_read_idx(oct, iq);*/
989
990 netdev = oct->props[iq->ifidx].netdev;
991
992 /* This is needed because the first IQ does not have
993 * a netdev associated with it.
994 */
995 if (!netdev)
996 return;
997
998 lio = GET_LIO(netdev);
999 if (netif_is_multiqueue(netdev)) {
1000 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
1001 lio->linfo.link.s.link_up &&
1002 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001003 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
1004 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001005 netif_wake_subqueue(netdev, iq->q_index);
1006 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001007 if (!octnet_iq_is_full(oct, lio->txq)) {
1008 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
1009 lio->txq,
1010 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001011 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001012 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001013 }
1014 }
1015}
1016
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001017/**
1018 * \brief Droq packet processor sceduler
1019 * @param oct octeon device
1020 */
1021static
1022void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
1023{
1024 struct octeon_device_priv *oct_priv =
1025 (struct octeon_device_priv *)oct->priv;
1026 u64 oq_no;
1027 struct octeon_droq *droq;
1028
1029 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001030 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1031 oq_no++) {
1032 if (!(oct->droq_intr & (1ULL << oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001033 continue;
1034
1035 droq = oct->droq[oq_no];
1036
1037 if (droq->ops.poll_mode) {
1038 droq->ops.napi_fn(droq);
1039 oct_priv->napi_mask |= (1 << oq_no);
1040 } else {
1041 tasklet_schedule(&oct_priv->droq_tasklet);
1042 }
1043 }
1044 }
1045}
1046
1047/**
1048 * \brief Interrupt handler for octeon
1049 * @param irq unused
1050 * @param dev octeon device
1051 */
1052static
1053irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
1054{
1055 struct octeon_device *oct = (struct octeon_device *)dev;
1056 irqreturn_t ret;
1057
1058 /* Disable our interrupts for the duration of ISR */
1059 oct->fn_list.disable_interrupt(oct->chip);
1060
1061 ret = oct->fn_list.process_interrupt_regs(oct);
1062
1063 if (ret == IRQ_HANDLED)
1064 liquidio_schedule_droq_pkt_handlers(oct);
1065
1066 /* Re-enable our interrupts */
1067 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
1068 oct->fn_list.enable_interrupt(oct->chip);
1069
1070 return ret;
1071}
1072
1073/**
1074 * \brief Setup interrupt for octeon device
1075 * @param oct octeon device
1076 *
1077 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1078 */
1079static int octeon_setup_interrupt(struct octeon_device *oct)
1080{
1081 int irqret, err;
1082
1083 err = pci_enable_msi(oct->pci_dev);
1084 if (err)
1085 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1086 err);
1087 else
1088 oct->flags |= LIO_FLAG_MSI_ENABLED;
1089
1090 irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
1091 IRQF_SHARED, "octeon", oct);
1092 if (irqret) {
1093 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1094 pci_disable_msi(oct->pci_dev);
1095 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1096 irqret);
1097 return 1;
1098 }
1099
1100 return 0;
1101}
1102
1103/**
1104 * \brief PCI probe handler
1105 * @param pdev PCI device structure
1106 * @param ent unused
1107 */
1108static int liquidio_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1109{
1110 struct octeon_device *oct_dev = NULL;
1111 struct handshake *hs;
1112
1113 oct_dev = octeon_allocate_device(pdev->device,
1114 sizeof(struct octeon_device_priv));
1115 if (!oct_dev) {
1116 dev_err(&pdev->dev, "Unable to allocate device\n");
1117 return -ENOMEM;
1118 }
1119
1120 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1121 (u32)pdev->vendor, (u32)pdev->device);
1122
1123 /* Assign octeon_device for this device to the private data area. */
1124 pci_set_drvdata(pdev, oct_dev);
1125
1126 /* set linux specific device pointer */
1127 oct_dev->pci_dev = (void *)pdev;
1128
1129 hs = &handshake[oct_dev->octeon_id];
1130 init_completion(&hs->init);
1131 init_completion(&hs->started);
1132 hs->pci_dev = pdev;
1133
1134 if (oct_dev->octeon_id == 0)
1135 /* first LiquidIO NIC is detected */
1136 complete(&first_stage);
1137
1138 if (octeon_device_init(oct_dev)) {
1139 liquidio_remove(pdev);
1140 return -ENOMEM;
1141 }
1142
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001143 oct_dev->rx_pause = 1;
1144 oct_dev->tx_pause = 1;
1145
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001146 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1147
1148 return 0;
1149}
1150
1151/**
1152 *\brief Destroy resources associated with octeon device
1153 * @param pdev PCI device structure
1154 * @param ent unused
1155 */
1156static void octeon_destroy_resources(struct octeon_device *oct)
1157{
1158 int i;
1159 struct octeon_device_priv *oct_priv =
1160 (struct octeon_device_priv *)oct->priv;
1161
1162 struct handshake *hs;
1163
1164 switch (atomic_read(&oct->status)) {
1165 case OCT_DEV_RUNNING:
1166 case OCT_DEV_CORE_OK:
1167
1168 /* No more instructions will be forwarded. */
1169 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1170
1171 oct->app_mode = CVM_DRV_INVALID_APP;
1172 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1173 lio_get_state_string(&oct->status));
1174
1175 schedule_timeout_uninterruptible(HZ / 10);
1176
1177 /* fallthrough */
1178 case OCT_DEV_HOST_OK:
1179
1180 /* fallthrough */
1181 case OCT_DEV_CONSOLE_INIT_DONE:
1182 /* Remove any consoles */
1183 octeon_remove_consoles(oct);
1184
1185 /* fallthrough */
1186 case OCT_DEV_IO_QUEUES_DONE:
1187 if (wait_for_pending_requests(oct))
1188 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1189
1190 if (lio_wait_for_instr_fetch(oct))
1191 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1192
1193 /* Disable the input and output queues now. No more packets will
1194 * arrive from Octeon, but we should wait for all packet
1195 * processing to finish.
1196 */
1197 oct->fn_list.disable_io_queues(oct);
1198
1199 if (lio_wait_for_oq_pkts(oct))
1200 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1201
1202 /* Disable interrupts */
1203 oct->fn_list.disable_interrupt(oct->chip);
1204
1205 /* Release the interrupt line */
1206 free_irq(oct->pci_dev->irq, oct);
1207
1208 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1209 pci_disable_msi(oct->pci_dev);
1210
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001211 /* fallthrough */
1212 case OCT_DEV_IN_RESET:
1213 case OCT_DEV_DROQ_INIT_DONE:
1214 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1215 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001216 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
1217 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001218 continue;
1219 octeon_delete_droq(oct, i);
1220 }
1221
1222 /* Force any pending handshakes to complete */
1223 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1224 hs = &handshake[i];
1225
1226 if (hs->pci_dev) {
1227 handshake[oct->octeon_id].init_ok = 0;
1228 complete(&handshake[oct->octeon_id].init);
1229 handshake[oct->octeon_id].started_ok = 0;
1230 complete(&handshake[oct->octeon_id].started);
1231 }
1232 }
1233
1234 /* fallthrough */
1235 case OCT_DEV_RESP_LIST_INIT_DONE:
1236 octeon_delete_response_list(oct);
1237
1238 /* fallthrough */
1239 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1240 octeon_free_sc_buffer_pool(oct);
1241
1242 /* fallthrough */
1243 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001244 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
1245 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001246 continue;
1247 octeon_delete_instr_queue(oct, i);
1248 }
1249
1250 /* fallthrough */
1251 case OCT_DEV_DISPATCH_INIT_DONE:
1252 octeon_delete_dispatch_list(oct);
1253 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1254
1255 /* fallthrough */
1256 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001257
1258 /* Soft reset the octeon device before exiting */
1259 oct->fn_list.soft_reset(oct);
1260
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001261 octeon_unmap_pci_barx(oct, 0);
1262 octeon_unmap_pci_barx(oct, 1);
1263
1264 /* fallthrough */
1265 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001266 /* Disable the device, releasing the PCI INT */
1267 pci_disable_device(oct->pci_dev);
1268
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001269 /* Nothing to be done here either */
1270 break;
1271 } /* end switch(oct->status) */
1272
1273 tasklet_kill(&oct_priv->droq_tasklet);
1274}
1275
1276/**
1277 * \brief Send Rx control command
1278 * @param lio per-network private data
1279 * @param start_stop whether to start or stop
1280 */
1281static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1282{
1283 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001284
1285 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1286
1287 nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001288 nctrl.ncmd.s.param1 = start_stop;
1289 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001290 nctrl.netpndev = (u64)lio->netdev;
1291
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001292 if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl) < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001293 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1294}
1295
1296/**
1297 * \brief Destroy NIC device interface
1298 * @param oct octeon device
1299 * @param ifidx which interface to destroy
1300 *
1301 * Cleanup associated with each interface for an Octeon device when NIC
1302 * module is being unloaded or if initialization fails during load.
1303 */
1304static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1305{
1306 struct net_device *netdev = oct->props[ifidx].netdev;
1307 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001308 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001309
1310 if (!netdev) {
1311 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1312 __func__, ifidx);
1313 return;
1314 }
1315
1316 lio = GET_LIO(netdev);
1317
1318 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1319
1320 send_rx_ctrl_cmd(lio, 0);
1321
1322 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1323 txqs_stop(netdev);
1324
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001325 if (oct->props[lio->ifidx].napi_enabled == 1) {
1326 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1327 napi_disable(napi);
1328
1329 oct->props[lio->ifidx].napi_enabled = 0;
1330 }
1331
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001332 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1333 unregister_netdev(netdev);
1334
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001335 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001336
1337 free_netdev(netdev);
1338
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001339 oct->props[ifidx].gmxport = -1;
1340
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001341 oct->props[ifidx].netdev = NULL;
1342}
1343
1344/**
1345 * \brief Stop complete NIC functionality
1346 * @param oct octeon device
1347 */
1348static int liquidio_stop_nic_module(struct octeon_device *oct)
1349{
1350 int i, j;
1351 struct lio *lio;
1352
1353 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1354 if (!oct->ifcount) {
1355 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1356 return 1;
1357 }
1358
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001359 spin_lock_bh(&oct->cmd_resp_wqlock);
1360 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1361 spin_unlock_bh(&oct->cmd_resp_wqlock);
1362
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001363 for (i = 0; i < oct->ifcount; i++) {
1364 lio = GET_LIO(oct->props[i].netdev);
1365 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001366 octeon_unregister_droq_ops(oct,
1367 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001368 }
1369
1370 for (i = 0; i < oct->ifcount; i++)
1371 liquidio_destroy_nic_device(oct, i);
1372
1373 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1374 return 0;
1375}
1376
1377/**
1378 * \brief Cleans up resources at unload time
1379 * @param pdev PCI device structure
1380 */
1381static void liquidio_remove(struct pci_dev *pdev)
1382{
1383 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1384
1385 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1386
1387 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1388 liquidio_stop_nic_module(oct_dev);
1389
1390 /* Reset the octeon device and cleanup all memory allocated for
1391 * the octeon device by driver.
1392 */
1393 octeon_destroy_resources(oct_dev);
1394
1395 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1396
1397 /* This octeon device has been removed. Update the global
1398 * data structure to reflect this. Free the device structure.
1399 */
1400 octeon_free_device_mem(oct_dev);
1401}
1402
1403/**
1404 * \brief Identify the Octeon device and to map the BAR address space
1405 * @param oct octeon device
1406 */
1407static int octeon_chip_specific_setup(struct octeon_device *oct)
1408{
1409 u32 dev_id, rev_id;
1410 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001411 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001412
1413 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1414 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1415 oct->rev_id = rev_id & 0xff;
1416
1417 switch (dev_id) {
1418 case OCTEON_CN68XX_PCIID:
1419 oct->chip_id = OCTEON_CN68XX;
1420 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001421 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001422 break;
1423
1424 case OCTEON_CN66XX_PCIID:
1425 oct->chip_id = OCTEON_CN66XX;
1426 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001427 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001428 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001429
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001430 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001431 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001432 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1433 dev_id);
1434 }
1435
1436 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001437 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001438 OCTEON_MAJOR_REV(oct),
1439 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001440 octeon_get_conf(oct)->card_name,
1441 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001442
1443 return ret;
1444}
1445
1446/**
1447 * \brief PCI initialization for each Octeon device.
1448 * @param oct octeon device
1449 */
1450static int octeon_pci_os_setup(struct octeon_device *oct)
1451{
1452 /* setup PCI stuff first */
1453 if (pci_enable_device(oct->pci_dev)) {
1454 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1455 return 1;
1456 }
1457
1458 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1459 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1460 return 1;
1461 }
1462
1463 /* Enable PCI DMA Master. */
1464 pci_set_master(oct->pci_dev);
1465
1466 return 0;
1467}
1468
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001469static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1470{
1471 int q = 0;
1472
1473 if (netif_is_multiqueue(lio->netdev))
1474 q = skb->queue_mapping % lio->linfo.num_txpciq;
1475
1476 return q;
1477}
1478
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001479/**
1480 * \brief Check Tx queue state for a given network buffer
1481 * @param lio per-network private data
1482 * @param skb network buffer
1483 */
1484static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1485{
1486 int q = 0, iq = 0;
1487
1488 if (netif_is_multiqueue(lio->netdev)) {
1489 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001490 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001491 } else {
1492 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001493 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001494 }
1495
1496 if (octnet_iq_is_full(lio->oct_dev, iq))
1497 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001498
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001499 if (__netif_subqueue_stopped(lio->netdev, q)) {
1500 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001501 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001502 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001503 return 1;
1504}
1505
1506/**
1507 * \brief Unmap and free network buffer
1508 * @param buf buffer
1509 */
1510static void free_netbuf(void *buf)
1511{
1512 struct sk_buff *skb;
1513 struct octnet_buf_free_info *finfo;
1514 struct lio *lio;
1515
1516 finfo = (struct octnet_buf_free_info *)buf;
1517 skb = finfo->skb;
1518 lio = finfo->lio;
1519
1520 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1521 DMA_TO_DEVICE);
1522
1523 check_txq_state(lio, skb);
1524
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001525 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001526}
1527
1528/**
1529 * \brief Unmap and free gather buffer
1530 * @param buf buffer
1531 */
1532static void free_netsgbuf(void *buf)
1533{
1534 struct octnet_buf_free_info *finfo;
1535 struct sk_buff *skb;
1536 struct lio *lio;
1537 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001538 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001539
1540 finfo = (struct octnet_buf_free_info *)buf;
1541 skb = finfo->skb;
1542 lio = finfo->lio;
1543 g = finfo->g;
1544 frags = skb_shinfo(skb)->nr_frags;
1545
1546 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1547 g->sg[0].ptr[0], (skb->len - skb->data_len),
1548 DMA_TO_DEVICE);
1549
1550 i = 1;
1551 while (frags--) {
1552 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1553
1554 pci_unmap_page((lio->oct_dev)->pci_dev,
1555 g->sg[(i >> 2)].ptr[(i & 3)],
1556 frag->size, DMA_TO_DEVICE);
1557 i++;
1558 }
1559
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001560 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1561 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001562
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001563 iq = skb_iq(lio, skb);
1564 spin_lock(&lio->glist_lock[iq]);
1565 list_add_tail(&g->list, &lio->glist[iq]);
1566 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001567
1568 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1569
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001570 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001571}
1572
1573/**
1574 * \brief Unmap and free gather buffer with response
1575 * @param buf buffer
1576 */
1577static void free_netsgbuf_with_resp(void *buf)
1578{
1579 struct octeon_soft_command *sc;
1580 struct octnet_buf_free_info *finfo;
1581 struct sk_buff *skb;
1582 struct lio *lio;
1583 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001584 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001585
1586 sc = (struct octeon_soft_command *)buf;
1587 skb = (struct sk_buff *)sc->callback_arg;
1588 finfo = (struct octnet_buf_free_info *)&skb->cb;
1589
1590 lio = finfo->lio;
1591 g = finfo->g;
1592 frags = skb_shinfo(skb)->nr_frags;
1593
1594 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1595 g->sg[0].ptr[0], (skb->len - skb->data_len),
1596 DMA_TO_DEVICE);
1597
1598 i = 1;
1599 while (frags--) {
1600 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1601
1602 pci_unmap_page((lio->oct_dev)->pci_dev,
1603 g->sg[(i >> 2)].ptr[(i & 3)],
1604 frag->size, DMA_TO_DEVICE);
1605 i++;
1606 }
1607
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001608 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1609 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001610
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001611 iq = skb_iq(lio, skb);
1612
1613 spin_lock(&lio->glist_lock[iq]);
1614 list_add_tail(&g->list, &lio->glist[iq]);
1615 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001616
1617 /* Don't free the skb yet */
1618
1619 check_txq_state(lio, skb);
1620}
1621
1622/**
1623 * \brief Adjust ptp frequency
1624 * @param ptp PTP clock info
1625 * @param ppb how much to adjust by, in parts-per-billion
1626 */
1627static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1628{
1629 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1630 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1631 u64 comp, delta;
1632 unsigned long flags;
1633 bool neg_adj = false;
1634
1635 if (ppb < 0) {
1636 neg_adj = true;
1637 ppb = -ppb;
1638 }
1639
1640 /* The hardware adds the clock compensation value to the
1641 * PTP clock on every coprocessor clock cycle, so we
1642 * compute the delta in terms of coprocessor clocks.
1643 */
1644 delta = (u64)ppb << 32;
1645 do_div(delta, oct->coproc_clock_rate);
1646
1647 spin_lock_irqsave(&lio->ptp_lock, flags);
1648 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1649 if (neg_adj)
1650 comp -= delta;
1651 else
1652 comp += delta;
1653 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1654 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1655
1656 return 0;
1657}
1658
1659/**
1660 * \brief Adjust ptp time
1661 * @param ptp PTP clock info
1662 * @param delta how much to adjust by, in nanosecs
1663 */
1664static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1665{
1666 unsigned long flags;
1667 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1668
1669 spin_lock_irqsave(&lio->ptp_lock, flags);
1670 lio->ptp_adjust += delta;
1671 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1672
1673 return 0;
1674}
1675
1676/**
1677 * \brief Get hardware clock time, including any adjustment
1678 * @param ptp PTP clock info
1679 * @param ts timespec
1680 */
1681static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1682 struct timespec64 *ts)
1683{
1684 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001685 unsigned long flags;
1686 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1687 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1688
1689 spin_lock_irqsave(&lio->ptp_lock, flags);
1690 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1691 ns += lio->ptp_adjust;
1692 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1693
Kefeng Wang286af312016-01-27 17:34:37 +08001694 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001695
1696 return 0;
1697}
1698
1699/**
1700 * \brief Set hardware clock time. Reset adjustment
1701 * @param ptp PTP clock info
1702 * @param ts timespec
1703 */
1704static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1705 const struct timespec64 *ts)
1706{
1707 u64 ns;
1708 unsigned long flags;
1709 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1710 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1711
1712 ns = timespec_to_ns(ts);
1713
1714 spin_lock_irqsave(&lio->ptp_lock, flags);
1715 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1716 lio->ptp_adjust = 0;
1717 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1718
1719 return 0;
1720}
1721
1722/**
1723 * \brief Check if PTP is enabled
1724 * @param ptp PTP clock info
1725 * @param rq request
1726 * @param on is it on
1727 */
1728static int liquidio_ptp_enable(struct ptp_clock_info *ptp,
1729 struct ptp_clock_request *rq, int on)
1730{
1731 return -EOPNOTSUPP;
1732}
1733
1734/**
1735 * \brief Open PTP clock source
1736 * @param netdev network device
1737 */
1738static void oct_ptp_open(struct net_device *netdev)
1739{
1740 struct lio *lio = GET_LIO(netdev);
1741 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1742
1743 spin_lock_init(&lio->ptp_lock);
1744
1745 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1746 lio->ptp_info.owner = THIS_MODULE;
1747 lio->ptp_info.max_adj = 250000000;
1748 lio->ptp_info.n_alarm = 0;
1749 lio->ptp_info.n_ext_ts = 0;
1750 lio->ptp_info.n_per_out = 0;
1751 lio->ptp_info.pps = 0;
1752 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1753 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1754 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1755 lio->ptp_info.settime64 = liquidio_ptp_settime;
1756 lio->ptp_info.enable = liquidio_ptp_enable;
1757
1758 lio->ptp_adjust = 0;
1759
1760 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1761 &oct->pci_dev->dev);
1762
1763 if (IS_ERR(lio->ptp_clock))
1764 lio->ptp_clock = NULL;
1765}
1766
1767/**
1768 * \brief Init PTP clock
1769 * @param oct octeon device
1770 */
1771static void liquidio_ptp_init(struct octeon_device *oct)
1772{
1773 u64 clock_comp, cfg;
1774
1775 clock_comp = (u64)NSEC_PER_SEC << 32;
1776 do_div(clock_comp, oct->coproc_clock_rate);
1777 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1778
1779 /* Enable */
1780 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1781 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1782}
1783
1784/**
1785 * \brief Load firmware to device
1786 * @param oct octeon device
1787 *
1788 * Maps device to firmware filename, requests firmware, and downloads it
1789 */
1790static int load_firmware(struct octeon_device *oct)
1791{
1792 int ret = 0;
1793 const struct firmware *fw;
1794 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1795 char *tmp_fw_type;
1796
1797 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1798 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1799 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1800 return ret;
1801 }
1802
1803 if (fw_type[0] == '\0')
1804 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1805 else
1806 tmp_fw_type = fw_type;
1807
1808 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1809 octeon_get_conf(oct)->card_name, tmp_fw_type,
1810 LIO_FW_NAME_SUFFIX);
1811
1812 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1813 if (ret) {
1814 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1815 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001816 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001817 return ret;
1818 }
1819
1820 ret = octeon_download_firmware(oct, fw->data, fw->size);
1821
1822 release_firmware(fw);
1823
1824 return ret;
1825}
1826
1827/**
1828 * \brief Setup output queue
1829 * @param oct octeon device
1830 * @param q_no which queue
1831 * @param num_descs how many descriptors
1832 * @param desc_size size of each descriptor
1833 * @param app_ctx application context
1834 */
1835static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
1836 int desc_size, void *app_ctx)
1837{
1838 int ret_val = 0;
1839
1840 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
1841 /* droq creation and local register settings. */
1842 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301843 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001844 return ret_val;
1845
1846 if (ret_val == 1) {
1847 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
1848 return 0;
1849 }
1850 /* tasklet creation for the droq */
1851
1852 /* Enable the droq queues */
1853 octeon_set_droq_pkt_op(oct, q_no, 1);
1854
1855 /* Send Credit for Octeon Output queues. Credits are always
1856 * sent after the output queue is enabled.
1857 */
1858 writel(oct->droq[q_no]->max_count,
1859 oct->droq[q_no]->pkts_credit_reg);
1860
1861 return ret_val;
1862}
1863
1864/**
1865 * \brief Callback for getting interface configuration
1866 * @param status status of request
1867 * @param buf pointer to resp structure
1868 */
1869static void if_cfg_callback(struct octeon_device *oct,
1870 u32 status,
1871 void *buf)
1872{
1873 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1874 struct liquidio_if_cfg_resp *resp;
1875 struct liquidio_if_cfg_context *ctx;
1876
1877 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1878 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1879
1880 oct = lio_get_device(ctx->octeon_id);
1881 if (resp->status)
1882 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1883 CVM_CAST64(resp->status));
1884 ACCESS_ONCE(ctx->cond) = 1;
1885
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001886 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
1887 resp->cfg_info.liquidio_firmware_version);
1888
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001889 /* This barrier is required to be sure that the response has been
1890 * written fully before waking up the handler
1891 */
1892 wmb();
1893
1894 wake_up_interruptible(&ctx->wc);
1895}
1896
1897/**
1898 * \brief Select queue based on hash
1899 * @param dev Net device
1900 * @param skb sk_buff structure
1901 * @returns selected queue number
1902 */
1903static u16 select_q(struct net_device *dev, struct sk_buff *skb,
1904 void *accel_priv, select_queue_fallback_t fallback)
1905{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001906 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001907 struct lio *lio;
1908
1909 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001910 qindex = skb_tx_hash(dev, skb);
1911
1912 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001913}
1914
1915/** Routine to push packets arriving on Octeon interface upto network layer.
1916 * @param oct_id - octeon device id.
1917 * @param skbuff - skbuff struct to be passed to network layer.
1918 * @param len - size of total data received.
1919 * @param rh - Control header associated with the packet
1920 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001921 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001922 */
1923static void
1924liquidio_push_packet(u32 octeon_id,
1925 void *skbuff,
1926 u32 len,
1927 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001928 void *param,
1929 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001930{
1931 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001932 struct sk_buff *skb = (struct sk_buff *)skbuff;
1933 struct skb_shared_hwtstamps *shhwtstamps;
1934 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07001935 u16 vtag = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001936 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001937 struct octeon_droq *droq = container_of(param, struct octeon_droq,
1938 napi);
1939 if (netdev) {
1940 int packet_was_received;
1941 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001942 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001943
1944 /* Do not proceed if the interface is not in RUNNING state. */
1945 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1946 recv_buffer_free(skb);
1947 droq->stats.rx_dropped++;
1948 return;
1949 }
1950
1951 skb->dev = netdev;
1952
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001953 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001954 if (likely(len > MIN_SKB_SIZE)) {
1955 struct octeon_skb_page_info *pg_info;
1956 unsigned char *va;
1957
1958 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
1959 if (pg_info->page) {
1960 /* For Paged allocation use the frags */
1961 va = page_address(pg_info->page) +
1962 pg_info->page_offset;
1963 memcpy(skb->data, va, MIN_SKB_SIZE);
1964 skb_put(skb, MIN_SKB_SIZE);
1965 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1966 pg_info->page,
1967 pg_info->page_offset +
1968 MIN_SKB_SIZE,
1969 len - MIN_SKB_SIZE,
1970 LIO_RXBUFFER_SZ);
1971 }
1972 } else {
1973 struct octeon_skb_page_info *pg_info =
1974 ((struct octeon_skb_page_info *)(skb->cb));
1975 skb_copy_to_linear_data(skb, page_address(pg_info->page)
1976 + pg_info->page_offset, len);
1977 skb_put(skb, len);
1978 put_page(pg_info->page);
1979 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001980
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001981 if (((oct->chip_id == OCTEON_CN66XX) ||
1982 (oct->chip_id == OCTEON_CN68XX)) &&
1983 ptp_enable) {
1984 if (rh->r_dh.has_hwtstamp) {
1985 /* timestamp is included from the hardware at
1986 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001987 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001988 if (ifstate_check
1989 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
1990 /* Nanoseconds are in the first 64-bits
1991 * of the packet.
1992 */
1993 memcpy(&ns, (skb->data), sizeof(ns));
1994 shhwtstamps = skb_hwtstamps(skb);
1995 shhwtstamps->hwtstamp =
1996 ns_to_ktime(ns +
1997 lio->ptp_adjust);
1998 }
1999 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002000 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002001 }
2002
2003 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002004 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002005 (((rh->r_dh.encap_on) &&
2006 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2007 (!(rh->r_dh.encap_on) &&
2008 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002009 /* checksum has already been verified */
2010 skb->ip_summed = CHECKSUM_UNNECESSARY;
2011 else
2012 skb->ip_summed = CHECKSUM_NONE;
2013
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002014 /* Setting Encapsulation field on basis of status received
2015 * from the firmware
2016 */
2017 if (rh->r_dh.encap_on) {
2018 skb->encapsulation = 1;
2019 skb->csum_level = 1;
2020 droq->stats.rx_vxlan++;
2021 }
2022
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002023 /* inbound VLAN tag */
2024 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2025 (rh->r_dh.vlan != 0)) {
2026 u16 vid = rh->r_dh.vlan;
2027 u16 priority = rh->r_dh.priority;
2028
2029 vtag = priority << 13 | vid;
2030 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2031 }
2032
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002033 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2034
2035 if (packet_was_received) {
2036 droq->stats.rx_bytes_received += len;
2037 droq->stats.rx_pkts_received++;
2038 netdev->last_rx = jiffies;
2039 } else {
2040 droq->stats.rx_dropped++;
2041 netif_info(lio, rx_err, lio->netdev,
2042 "droq:%d error rx_dropped:%llu\n",
2043 droq->q_no, droq->stats.rx_dropped);
2044 }
2045
2046 } else {
2047 recv_buffer_free(skb);
2048 }
2049}
2050
2051/**
2052 * \brief wrapper for calling napi_schedule
2053 * @param param parameters to pass to napi_schedule
2054 *
2055 * Used when scheduling on different CPUs
2056 */
2057static void napi_schedule_wrapper(void *param)
2058{
2059 struct napi_struct *napi = param;
2060
2061 napi_schedule(napi);
2062}
2063
2064/**
2065 * \brief callback when receive interrupt occurs and we are in NAPI mode
2066 * @param arg pointer to octeon output queue
2067 */
2068static void liquidio_napi_drv_callback(void *arg)
2069{
2070 struct octeon_droq *droq = arg;
2071 int this_cpu = smp_processor_id();
2072
2073 if (droq->cpu_id == this_cpu) {
2074 napi_schedule(&droq->napi);
2075 } else {
2076 struct call_single_data *csd = &droq->csd;
2077
2078 csd->func = napi_schedule_wrapper;
2079 csd->info = &droq->napi;
2080 csd->flags = 0;
2081
2082 smp_call_function_single_async(droq->cpu_id, csd);
2083 }
2084}
2085
2086/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002087 * \brief Entry point for NAPI polling
2088 * @param napi NAPI structure
2089 * @param budget maximum number of items to process
2090 */
2091static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2092{
2093 struct octeon_droq *droq;
2094 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002095 int tx_done = 0, iq_no;
2096 struct octeon_instr_queue *iq;
2097 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002098
2099 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002100 oct = droq->oct_dev;
2101 iq_no = droq->q_no;
2102 /* Handle Droq descriptors */
2103 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2104 POLL_EVENT_PROCESS_PKTS,
2105 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002106
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002107 /* Flush the instruction queue */
2108 iq = oct->instr_queue[iq_no];
2109 if (iq) {
2110 /* Process iq buffers with in the budget limits */
2111 tx_done = octeon_flush_iq(oct, iq, 1, budget);
2112 /* Update iq read-index rather than waiting for next interrupt.
2113 * Return back if tx_done is false.
2114 */
2115 update_txq_status(oct, iq_no);
2116 /*tx_done = (iq->flush_index == iq->octeon_read_index);*/
2117 } else {
2118 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2119 __func__, iq_no);
2120 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002121
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002122 if ((work_done < budget) && (tx_done)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002123 napi_complete(napi);
2124 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2125 POLL_EVENT_ENABLE_INTR, 0);
2126 return 0;
2127 }
2128
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002129 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002130}
2131
2132/**
2133 * \brief Setup input and output queues
2134 * @param octeon_dev octeon device
2135 * @param net_device Net device
2136 *
2137 * Note: Queues are with respect to the octeon device. Thus
2138 * an input queue is for egress packets, and output queues
2139 * are for ingress packets.
2140 */
2141static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002142 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002143{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002144 struct octeon_droq_ops droq_ops;
2145 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002146 static int cpu_id;
2147 static int cpu_id_modulus;
2148 struct octeon_droq *droq;
2149 struct napi_struct *napi;
2150 int q, q_no, retval = 0;
2151 struct lio *lio;
2152 int num_tx_descs;
2153
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002154 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002155
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002156 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002157
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002158 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2159
2160 droq_ops.fptr = liquidio_push_packet;
2161 droq_ops.farg = (void *)netdev;
2162
2163 droq_ops.poll_mode = 1;
2164 droq_ops.napi_fn = liquidio_napi_drv_callback;
2165 cpu_id = 0;
2166 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002167
2168 /* set up DROQs. */
2169 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002170 q_no = lio->linfo.rxpciq[q].s.q_no;
2171 dev_dbg(&octeon_dev->pci_dev->dev,
2172 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2173 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002174 retval = octeon_setup_droq(octeon_dev, q_no,
2175 CFG_GET_NUM_RX_DESCS_NIC_IF
2176 (octeon_get_conf(octeon_dev),
2177 lio->ifidx),
2178 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2179 (octeon_get_conf(octeon_dev),
2180 lio->ifidx), NULL);
2181 if (retval) {
2182 dev_err(&octeon_dev->pci_dev->dev,
2183 " %s : Runtime DROQ(RxQ) creation failed.\n",
2184 __func__);
2185 return 1;
2186 }
2187
2188 droq = octeon_dev->droq[q_no];
2189 napi = &droq->napi;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002190 dev_dbg(&octeon_dev->pci_dev->dev,
2191 "netif_napi_add netdev:%llx oct:%llx\n",
2192 (u64)netdev,
2193 (u64)octeon_dev);
2194 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002195
2196 /* designate a CPU for this droq */
2197 droq->cpu_id = cpu_id;
2198 cpu_id++;
2199 if (cpu_id >= cpu_id_modulus)
2200 cpu_id = 0;
2201
2202 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2203 }
2204
2205 /* set up IQs. */
2206 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2207 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2208 (octeon_dev),
2209 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002210 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2211 lio->linfo.txpciq[q], num_tx_descs,
2212 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002213 if (retval) {
2214 dev_err(&octeon_dev->pci_dev->dev,
2215 " %s : Runtime IQ(TxQ) creation failed.\n",
2216 __func__);
2217 return 1;
2218 }
2219 }
2220
2221 return 0;
2222}
2223
2224/**
2225 * \brief Poll routine for checking transmit queue status
2226 * @param work work_struct data structure
2227 */
2228static void octnet_poll_check_txq_status(struct work_struct *work)
2229{
2230 struct cavium_wk *wk = (struct cavium_wk *)work;
2231 struct lio *lio = (struct lio *)wk->ctxptr;
2232
2233 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2234 return;
2235
2236 check_txq_status(lio);
2237 queue_delayed_work(lio->txq_status_wq.wq,
2238 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2239}
2240
2241/**
2242 * \brief Sets up the txq poll check
2243 * @param netdev network device
2244 */
2245static inline void setup_tx_poll_fn(struct net_device *netdev)
2246{
2247 struct lio *lio = GET_LIO(netdev);
2248 struct octeon_device *oct = lio->oct_dev;
2249
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302250 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2251 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002252 if (!lio->txq_status_wq.wq) {
2253 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2254 return;
2255 }
2256 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2257 octnet_poll_check_txq_status);
2258 lio->txq_status_wq.wk.ctxptr = lio;
2259 queue_delayed_work(lio->txq_status_wq.wq,
2260 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2261}
2262
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002263static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2264{
2265 struct lio *lio = GET_LIO(netdev);
2266
2267 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2268 destroy_workqueue(lio->txq_status_wq.wq);
2269}
2270
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002271/**
2272 * \brief Net device open for LiquidIO
2273 * @param netdev network device
2274 */
2275static int liquidio_open(struct net_device *netdev)
2276{
2277 struct lio *lio = GET_LIO(netdev);
2278 struct octeon_device *oct = lio->oct_dev;
2279 struct napi_struct *napi, *n;
2280
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002281 if (oct->props[lio->ifidx].napi_enabled == 0) {
2282 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2283 napi_enable(napi);
2284
2285 oct->props[lio->ifidx].napi_enabled = 1;
2286 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002287
2288 oct_ptp_open(netdev);
2289
2290 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002291
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002292 setup_tx_poll_fn(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002293
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002294 start_txq(netdev);
2295
2296 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002297
2298 /* tell Octeon to start forwarding packets to host */
2299 send_rx_ctrl_cmd(lio, 1);
2300
2301 /* Ready for link status updates */
2302 lio->intf_open = 1;
2303
2304 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2305 netdev->name);
2306
2307 return 0;
2308}
2309
2310/**
2311 * \brief Net device stop for LiquidIO
2312 * @param netdev network device
2313 */
2314static int liquidio_stop(struct net_device *netdev)
2315{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002316 struct lio *lio = GET_LIO(netdev);
2317 struct octeon_device *oct = lio->oct_dev;
2318
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002319 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2320
2321 netif_tx_disable(netdev);
2322
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002323 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002324 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002325 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002326 lio->linfo.link.s.link_up = 0;
2327 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002328
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002329 /* Pause for a moment and wait for Octeon to flush out (to the wire) any
2330 * egress packets that are in-flight.
2331 */
2332 set_current_state(TASK_INTERRUPTIBLE);
2333 schedule_timeout(msecs_to_jiffies(100));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002334
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002335 /* Now it should be safe to tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002336 send_rx_ctrl_cmd(lio, 0);
2337
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002338 cleanup_tx_poll_fn(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002339
2340 if (lio->ptp_clock) {
2341 ptp_clock_unregister(lio->ptp_clock);
2342 lio->ptp_clock = NULL;
2343 }
2344
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002345 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2346 module_put(THIS_MODULE);
2347
2348 return 0;
2349}
2350
2351void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr)
2352{
2353 struct octnic_ctrl_pkt *nctrl = (struct octnic_ctrl_pkt *)nctrl_ptr;
2354 struct net_device *netdev = (struct net_device *)nctrl->netpndev;
2355 struct lio *lio = GET_LIO(netdev);
2356 struct octeon_device *oct = lio->oct_dev;
2357
2358 switch (nctrl->ncmd.s.cmd) {
2359 case OCTNET_CMD_CHANGE_DEVFLAGS:
2360 case OCTNET_CMD_SET_MULTI_LIST:
2361 break;
2362
2363 case OCTNET_CMD_CHANGE_MACADDR:
2364 /* If command is successful, change the MACADDR. */
2365 netif_info(lio, probe, lio->netdev, " MACAddr changed to 0x%llx\n",
2366 CVM_CAST64(nctrl->udd[0]));
2367 dev_info(&oct->pci_dev->dev, "%s MACAddr changed to 0x%llx\n",
2368 netdev->name, CVM_CAST64(nctrl->udd[0]));
2369 memcpy(netdev->dev_addr, ((u8 *)&nctrl->udd[0]) + 2, ETH_ALEN);
2370 break;
2371
2372 case OCTNET_CMD_CHANGE_MTU:
2373 /* If command is successful, change the MTU. */
2374 netif_info(lio, probe, lio->netdev, " MTU Changed from %d to %d\n",
2375 netdev->mtu, nctrl->ncmd.s.param2);
2376 dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
2377 netdev->name, netdev->mtu,
2378 nctrl->ncmd.s.param2);
2379 netdev->mtu = nctrl->ncmd.s.param2;
2380 break;
2381
2382 case OCTNET_CMD_GPIO_ACCESS:
2383 netif_info(lio, probe, lio->netdev, "LED Flashing visual identification\n");
2384
2385 break;
2386
2387 case OCTNET_CMD_LRO_ENABLE:
2388 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2389 break;
2390
2391 case OCTNET_CMD_LRO_DISABLE:
2392 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2393 netdev->name);
2394 break;
2395
2396 case OCTNET_CMD_VERBOSE_ENABLE:
2397 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2398 break;
2399
2400 case OCTNET_CMD_VERBOSE_DISABLE:
2401 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2402 netdev->name);
2403 break;
2404
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07002405 case OCTNET_CMD_ENABLE_VLAN_FILTER:
2406 dev_info(&oct->pci_dev->dev, "%s VLAN filter enabled\n",
2407 netdev->name);
2408 break;
2409
2410 case OCTNET_CMD_ADD_VLAN_FILTER:
2411 dev_info(&oct->pci_dev->dev, "%s VLAN filter %d added\n",
2412 netdev->name, nctrl->ncmd.s.param1);
2413 break;
2414
2415 case OCTNET_CMD_DEL_VLAN_FILTER:
2416 dev_info(&oct->pci_dev->dev, "%s VLAN filter %d removed\n",
2417 netdev->name, nctrl->ncmd.s.param1);
2418 break;
2419
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002420 case OCTNET_CMD_SET_SETTINGS:
2421 dev_info(&oct->pci_dev->dev, "%s settings changed\n",
2422 netdev->name);
2423
2424 break;
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002425 /* Case to handle "OCTNET_CMD_TNL_RX_CSUM_CTL"
2426 * Command passed by NIC driver
2427 */
2428 case OCTNET_CMD_TNL_RX_CSUM_CTL:
2429 if (nctrl->ncmd.s.param1 == OCTNET_CMD_RXCSUM_ENABLE) {
2430 netif_info(lio, probe, lio->netdev,
2431 "%s RX Checksum Offload Enabled\n",
2432 netdev->name);
2433 } else if (nctrl->ncmd.s.param1 ==
2434 OCTNET_CMD_RXCSUM_DISABLE) {
2435 netif_info(lio, probe, lio->netdev,
2436 "%s RX Checksum Offload Disabled\n",
2437 netdev->name);
2438 }
2439 break;
2440
2441 /* Case to handle "OCTNET_CMD_TNL_TX_CSUM_CTL"
2442 * Command passed by NIC driver
2443 */
2444 case OCTNET_CMD_TNL_TX_CSUM_CTL:
2445 if (nctrl->ncmd.s.param1 == OCTNET_CMD_TXCSUM_ENABLE) {
2446 netif_info(lio, probe, lio->netdev,
2447 "%s TX Checksum Offload Enabled\n",
2448 netdev->name);
2449 } else if (nctrl->ncmd.s.param1 ==
2450 OCTNET_CMD_TXCSUM_DISABLE) {
2451 netif_info(lio, probe, lio->netdev,
2452 "%s TX Checksum Offload Disabled\n",
2453 netdev->name);
2454 }
2455 break;
2456
2457 /* Case to handle "OCTNET_CMD_VXLAN_PORT_CONFIG"
2458 * Command passed by NIC driver
2459 */
2460 case OCTNET_CMD_VXLAN_PORT_CONFIG:
2461 if (nctrl->ncmd.s.more == OCTNET_CMD_VXLAN_PORT_ADD) {
2462 netif_info(lio, probe, lio->netdev,
2463 "%s VxLAN Destination UDP PORT:%d ADDED\n",
2464 netdev->name,
2465 nctrl->ncmd.s.param1);
2466 } else if (nctrl->ncmd.s.more ==
2467 OCTNET_CMD_VXLAN_PORT_DEL) {
2468 netif_info(lio, probe, lio->netdev,
2469 "%s VxLAN Destination UDP PORT:%d DELETED\n",
2470 netdev->name,
2471 nctrl->ncmd.s.param1);
2472 }
2473 break;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002474
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002475 case OCTNET_CMD_SET_FLOW_CTL:
2476 netif_info(lio, probe, lio->netdev, "Set RX/TX flow control parameters\n");
2477 break;
2478
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002479 default:
2480 dev_err(&oct->pci_dev->dev, "%s Unknown cmd %d\n", __func__,
2481 nctrl->ncmd.s.cmd);
2482 }
2483}
2484
2485/**
2486 * \brief Converts a mask based on net device flags
2487 * @param netdev network device
2488 *
2489 * This routine generates a octnet_ifflags mask from the net device flags
2490 * received from the OS.
2491 */
2492static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2493{
2494 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2495
2496 if (netdev->flags & IFF_PROMISC)
2497 f |= OCTNET_IFFLAG_PROMISC;
2498
2499 if (netdev->flags & IFF_ALLMULTI)
2500 f |= OCTNET_IFFLAG_ALLMULTI;
2501
2502 if (netdev->flags & IFF_MULTICAST) {
2503 f |= OCTNET_IFFLAG_MULTICAST;
2504
2505 /* Accept all multicast addresses if there are more than we
2506 * can handle
2507 */
2508 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2509 f |= OCTNET_IFFLAG_ALLMULTI;
2510 }
2511
2512 if (netdev->flags & IFF_BROADCAST)
2513 f |= OCTNET_IFFLAG_BROADCAST;
2514
2515 return f;
2516}
2517
2518/**
2519 * \brief Net device set_multicast_list
2520 * @param netdev network device
2521 */
2522static void liquidio_set_mcast_list(struct net_device *netdev)
2523{
2524 struct lio *lio = GET_LIO(netdev);
2525 struct octeon_device *oct = lio->oct_dev;
2526 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002527 struct netdev_hw_addr *ha;
2528 u64 *mc;
2529 int ret, i;
2530 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2531
2532 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2533
2534 /* Create a ctrl pkt command to be sent to core app. */
2535 nctrl.ncmd.u64 = 0;
2536 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002537 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2538 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002539 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002540 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002541 nctrl.netpndev = (u64)netdev;
2542 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2543
2544 /* copy all the addresses into the udd */
2545 i = 0;
2546 mc = &nctrl.udd[0];
2547 netdev_for_each_mc_addr(ha, netdev) {
2548 *mc = 0;
2549 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2550 /* no need to swap bytes */
2551
2552 if (++mc > &nctrl.udd[mc_count])
2553 break;
2554 }
2555
2556 /* Apparently, any activity in this call from the kernel has to
2557 * be atomic. So we won't wait for response.
2558 */
2559 nctrl.wait_time = 0;
2560
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002561 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002562 if (ret < 0) {
2563 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2564 ret);
2565 }
2566}
2567
2568/**
2569 * \brief Net device set_mac_address
2570 * @param netdev network device
2571 */
2572static int liquidio_set_mac(struct net_device *netdev, void *p)
2573{
2574 int ret = 0;
2575 struct lio *lio = GET_LIO(netdev);
2576 struct octeon_device *oct = lio->oct_dev;
2577 struct sockaddr *addr = (struct sockaddr *)p;
2578 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002579
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002580 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002581 return -EADDRNOTAVAIL;
2582
2583 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2584
2585 nctrl.ncmd.u64 = 0;
2586 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002587 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002588 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002589 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002590 nctrl.netpndev = (u64)netdev;
2591 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2592 nctrl.wait_time = 100;
2593
2594 nctrl.udd[0] = 0;
2595 /* The MAC Address is presented in network byte order. */
2596 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2597
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002598 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002599 if (ret < 0) {
2600 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2601 return -ENOMEM;
2602 }
2603 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2604 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2605
2606 return 0;
2607}
2608
2609/**
2610 * \brief Net device get_stats
2611 * @param netdev network device
2612 */
2613static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2614{
2615 struct lio *lio = GET_LIO(netdev);
2616 struct net_device_stats *stats = &netdev->stats;
2617 struct octeon_device *oct;
2618 u64 pkts = 0, drop = 0, bytes = 0;
2619 struct oct_droq_stats *oq_stats;
2620 struct oct_iq_stats *iq_stats;
2621 int i, iq_no, oq_no;
2622
2623 oct = lio->oct_dev;
2624
2625 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002626 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002627 iq_stats = &oct->instr_queue[iq_no]->stats;
2628 pkts += iq_stats->tx_done;
2629 drop += iq_stats->tx_dropped;
2630 bytes += iq_stats->tx_tot_bytes;
2631 }
2632
2633 stats->tx_packets = pkts;
2634 stats->tx_bytes = bytes;
2635 stats->tx_dropped = drop;
2636
2637 pkts = 0;
2638 drop = 0;
2639 bytes = 0;
2640
2641 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002642 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002643 oq_stats = &oct->droq[oq_no]->stats;
2644 pkts += oq_stats->rx_pkts_received;
2645 drop += (oq_stats->rx_dropped +
2646 oq_stats->dropped_nodispatch +
2647 oq_stats->dropped_toomany +
2648 oq_stats->dropped_nomem);
2649 bytes += oq_stats->rx_bytes_received;
2650 }
2651
2652 stats->rx_bytes = bytes;
2653 stats->rx_packets = pkts;
2654 stats->rx_dropped = drop;
2655
2656 return stats;
2657}
2658
2659/**
2660 * \brief Net device change_mtu
2661 * @param netdev network device
2662 */
2663static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2664{
2665 struct lio *lio = GET_LIO(netdev);
2666 struct octeon_device *oct = lio->oct_dev;
2667 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002668 int max_frm_size = new_mtu + OCTNET_FRM_HEADER_SIZE;
2669 int ret = 0;
2670
2671 /* Limit the MTU to make sure the ethernet packets are between 64 bytes
2672 * and 65535 bytes
2673 */
2674 if ((max_frm_size < OCTNET_MIN_FRM_SIZE) ||
2675 (max_frm_size > OCTNET_MAX_FRM_SIZE)) {
2676 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2677 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
2678 (OCTNET_MIN_FRM_SIZE - OCTNET_FRM_HEADER_SIZE),
2679 (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE));
2680 return -EINVAL;
2681 }
2682
2683 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2684
2685 nctrl.ncmd.u64 = 0;
2686 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002687 nctrl.ncmd.s.param1 = new_mtu;
2688 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002689 nctrl.wait_time = 100;
2690 nctrl.netpndev = (u64)netdev;
2691 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2692
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002693 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002694 if (ret < 0) {
2695 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2696 return -1;
2697 }
2698
2699 lio->mtu = new_mtu;
2700
2701 return 0;
2702}
2703
2704/**
2705 * \brief Handler for SIOCSHWTSTAMP ioctl
2706 * @param netdev network device
2707 * @param ifr interface request
2708 * @param cmd command
2709 */
2710static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2711{
2712 struct hwtstamp_config conf;
2713 struct lio *lio = GET_LIO(netdev);
2714
2715 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2716 return -EFAULT;
2717
2718 if (conf.flags)
2719 return -EINVAL;
2720
2721 switch (conf.tx_type) {
2722 case HWTSTAMP_TX_ON:
2723 case HWTSTAMP_TX_OFF:
2724 break;
2725 default:
2726 return -ERANGE;
2727 }
2728
2729 switch (conf.rx_filter) {
2730 case HWTSTAMP_FILTER_NONE:
2731 break;
2732 case HWTSTAMP_FILTER_ALL:
2733 case HWTSTAMP_FILTER_SOME:
2734 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2735 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2736 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2737 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2738 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2739 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2740 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2741 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2742 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2743 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2744 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2745 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2746 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2747 break;
2748 default:
2749 return -ERANGE;
2750 }
2751
2752 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2753 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2754
2755 else
2756 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2757
2758 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2759}
2760
2761/**
2762 * \brief ioctl handler
2763 * @param netdev network device
2764 * @param ifr interface request
2765 * @param cmd command
2766 */
2767static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2768{
2769 switch (cmd) {
2770 case SIOCSHWTSTAMP:
2771 return hwtstamp_ioctl(netdev, ifr, cmd);
2772 default:
2773 return -EOPNOTSUPP;
2774 }
2775}
2776
2777/**
2778 * \brief handle a Tx timestamp response
2779 * @param status response status
2780 * @param buf pointer to skb
2781 */
2782static void handle_timestamp(struct octeon_device *oct,
2783 u32 status,
2784 void *buf)
2785{
2786 struct octnet_buf_free_info *finfo;
2787 struct octeon_soft_command *sc;
2788 struct oct_timestamp_resp *resp;
2789 struct lio *lio;
2790 struct sk_buff *skb = (struct sk_buff *)buf;
2791
2792 finfo = (struct octnet_buf_free_info *)skb->cb;
2793 lio = finfo->lio;
2794 sc = finfo->sc;
2795 oct = lio->oct_dev;
2796 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2797
2798 if (status != OCTEON_REQUEST_DONE) {
2799 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2800 CVM_CAST64(status));
2801 resp->timestamp = 0;
2802 }
2803
2804 octeon_swap_8B_data(&resp->timestamp, 1);
2805
Colin Ian King19a6d152016-02-05 16:30:39 +00002806 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002807 struct skb_shared_hwtstamps ts;
2808 u64 ns = resp->timestamp;
2809
2810 netif_info(lio, tx_done, lio->netdev,
2811 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2812 skb, (unsigned long long)ns);
2813 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2814 skb_tstamp_tx(skb, &ts);
2815 }
2816
2817 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002818 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002819}
2820
2821/* \brief Send a data packet that will be timestamped
2822 * @param oct octeon device
2823 * @param ndata pointer to network data
2824 * @param finfo pointer to private network data
2825 */
2826static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2827 struct octnic_data_pkt *ndata,
2828 struct octnet_buf_free_info *finfo,
2829 int xmit_more)
2830{
2831 int retval;
2832 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002833 struct lio *lio;
2834 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002835 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002836
2837 lio = finfo->lio;
2838
2839 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2840 sizeof(struct oct_timestamp_resp));
2841 finfo->sc = sc;
2842
2843 if (!sc) {
2844 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2845 return IQ_SEND_FAILED;
2846 }
2847
2848 if (ndata->reqtype == REQTYPE_NORESP_NET)
2849 ndata->reqtype = REQTYPE_RESP_NET;
2850 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2851 ndata->reqtype = REQTYPE_RESP_NET_SG;
2852
2853 sc->callback = handle_timestamp;
2854 sc->callback_arg = finfo->skb;
2855 sc->iq_no = ndata->q_no;
2856
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002857 len = (u32)((struct octeon_instr_ih2 *)(&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002858
2859 ring_doorbell = !xmit_more;
2860 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002861 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002862
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002863 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002864 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2865 retval);
2866 octeon_free_soft_command(oct, sc);
2867 } else {
2868 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2869 }
2870
2871 return retval;
2872}
2873
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002874/** \brief Transmit networks packets to the Octeon interface
2875 * @param skbuff skbuff struct to be passed to network layer.
2876 * @param netdev pointer to network device
2877 * @returns whether the packet was transmitted to the device okay or not
2878 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2879 */
2880static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2881{
2882 struct lio *lio;
2883 struct octnet_buf_free_info *finfo;
2884 union octnic_cmd_setup cmdsetup;
2885 struct octnic_data_pkt ndata;
2886 struct octeon_device *oct;
2887 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002888 struct octeon_instr_irh *irh;
2889 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002890 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002891 int q_idx = 0, iq_no = 0;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002892 int xmit_more, j;
2893 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002894 u32 tag = 0;
2895
2896 lio = GET_LIO(netdev);
2897 oct = lio->oct_dev;
2898
2899 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002900 q_idx = skb->queue_mapping;
2901 q_idx = (q_idx % (lio->linfo.num_txpciq));
2902 tag = q_idx;
2903 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002904 } else {
2905 iq_no = lio->txq;
2906 }
2907
2908 stats = &oct->instr_queue[iq_no]->stats;
2909
2910 /* Check for all conditions in which the current packet cannot be
2911 * transmitted.
2912 */
2913 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002914 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002915 (skb->len <= 0)) {
2916 netif_info(lio, tx_err, lio->netdev,
2917 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002918 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002919 goto lio_xmit_failed;
2920 }
2921
2922 /* Use space in skb->cb to store info used to unmap and
2923 * free the buffers.
2924 */
2925 finfo = (struct octnet_buf_free_info *)skb->cb;
2926 finfo->lio = lio;
2927 finfo->skb = skb;
2928 finfo->sc = NULL;
2929
2930 /* Prepare the attributes for the data to be passed to OSI. */
2931 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2932
2933 ndata.buf = (void *)finfo;
2934
2935 ndata.q_no = iq_no;
2936
2937 if (netif_is_multiqueue(netdev)) {
2938 if (octnet_iq_is_full(oct, ndata.q_no)) {
2939 /* defer sending if queue is full */
2940 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2941 ndata.q_no);
2942 stats->tx_iq_busy++;
2943 return NETDEV_TX_BUSY;
2944 }
2945 } else {
2946 if (octnet_iq_is_full(oct, lio->txq)) {
2947 /* defer sending if queue is full */
2948 stats->tx_iq_busy++;
2949 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2950 ndata.q_no);
2951 return NETDEV_TX_BUSY;
2952 }
2953 }
2954 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
2955 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no );
2956 */
2957
2958 ndata.datasize = skb->len;
2959
2960 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002961 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002962
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002963 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2964 if (skb->encapsulation) {
2965 cmdsetup.s.tnl_csum = 1;
2966 stats->tx_vxlan++;
2967 } else {
2968 cmdsetup.s.transport_csum = 1;
2969 }
2970 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002971 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2972 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2973 cmdsetup.s.timestamp = 1;
2974 }
2975
2976 if (skb_shinfo(skb)->nr_frags == 0) {
2977 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002978 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002979 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002980 dptr = dma_map_single(&oct->pci_dev->dev,
2981 skb->data,
2982 skb->len,
2983 DMA_TO_DEVICE);
2984 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002985 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2986 __func__);
2987 return NETDEV_TX_BUSY;
2988 }
2989
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002990 ndata.cmd.cmd2.dptr = dptr;
2991 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002992 ndata.reqtype = REQTYPE_NORESP_NET;
2993
2994 } else {
2995 int i, frags;
2996 struct skb_frag_struct *frag;
2997 struct octnic_gather *g;
2998
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002999 spin_lock(&lio->glist_lock[q_idx]);
3000 g = (struct octnic_gather *)
3001 list_delete_head(&lio->glist[q_idx]);
3002 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003003
3004 if (!g) {
3005 netif_info(lio, tx_err, lio->netdev,
3006 "Transmit scatter gather: glist null!\n");
3007 goto lio_xmit_failed;
3008 }
3009
3010 cmdsetup.s.gather = 1;
3011 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003012 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003013
3014 memset(g->sg, 0, g->sg_size);
3015
3016 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3017 skb->data,
3018 (skb->len - skb->data_len),
3019 DMA_TO_DEVICE);
3020 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3021 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3022 __func__);
3023 return NETDEV_TX_BUSY;
3024 }
3025 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3026
3027 frags = skb_shinfo(skb)->nr_frags;
3028 i = 1;
3029 while (frags--) {
3030 frag = &skb_shinfo(skb)->frags[i - 1];
3031
3032 g->sg[(i >> 2)].ptr[(i & 3)] =
3033 dma_map_page(&oct->pci_dev->dev,
3034 frag->page.p,
3035 frag->page_offset,
3036 frag->size,
3037 DMA_TO_DEVICE);
3038
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003039 if (dma_mapping_error(&oct->pci_dev->dev,
3040 g->sg[i >> 2].ptr[i & 3])) {
3041 dma_unmap_single(&oct->pci_dev->dev,
3042 g->sg[0].ptr[0],
3043 skb->len - skb->data_len,
3044 DMA_TO_DEVICE);
3045 for (j = 1; j < i; j++) {
3046 frag = &skb_shinfo(skb)->frags[j - 1];
3047 dma_unmap_page(&oct->pci_dev->dev,
3048 g->sg[j >> 2].ptr[j & 3],
3049 frag->size,
3050 DMA_TO_DEVICE);
3051 }
3052 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3053 __func__);
3054 return NETDEV_TX_BUSY;
3055 }
3056
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003057 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3058 i++;
3059 }
3060
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003061 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
3062 g->sg_size, DMA_TO_DEVICE);
3063 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003064
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003065 ndata.cmd.cmd2.dptr = dptr;
3066 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003067 finfo->g = g;
3068
3069 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3070 }
3071
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003072 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3073 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003074
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003075 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003076 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3077 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003078 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003079 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003080
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003081 /* HW insert VLAN tag */
3082 if (skb_vlan_tag_present(skb)) {
3083 irh->priority = skb_vlan_tag_get(skb) >> 13;
3084 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3085 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003086
3087 xmit_more = skb->xmit_more;
3088
3089 if (unlikely(cmdsetup.s.timestamp))
3090 status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
3091 else
3092 status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
3093 if (status == IQ_SEND_FAILED)
3094 goto lio_xmit_failed;
3095
3096 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3097
3098 if (status == IQ_SEND_STOP)
3099 stop_q(lio->netdev, q_idx);
3100
Florian Westphal860e9532016-05-03 16:33:13 +02003101 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003102
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003103 if (skb_shinfo(skb)->gso_size)
3104 stats->tx_done += skb_shinfo(skb)->gso_segs;
3105 else
3106 stats->tx_done++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003107 stats->tx_tot_bytes += skb->len;
3108
3109 return NETDEV_TX_OK;
3110
3111lio_xmit_failed:
3112 stats->tx_dropped++;
3113 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3114 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003115 if (dptr)
3116 dma_unmap_single(&oct->pci_dev->dev, dptr,
3117 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003118 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003119 return NETDEV_TX_OK;
3120}
3121
3122/** \brief Network device Tx timeout
3123 * @param netdev pointer to network device
3124 */
3125static void liquidio_tx_timeout(struct net_device *netdev)
3126{
3127 struct lio *lio;
3128
3129 lio = GET_LIO(netdev);
3130
3131 netif_info(lio, tx_err, lio->netdev,
3132 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3133 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003134 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003135 txqs_wake(netdev);
3136}
3137
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003138static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3139 __be16 proto __attribute__((unused)),
3140 u16 vid)
3141{
3142 struct lio *lio = GET_LIO(netdev);
3143 struct octeon_device *oct = lio->oct_dev;
3144 struct octnic_ctrl_pkt nctrl;
3145 int ret = 0;
3146
3147 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3148
3149 nctrl.ncmd.u64 = 0;
3150 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3151 nctrl.ncmd.s.param1 = vid;
3152 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3153 nctrl.wait_time = 100;
3154 nctrl.netpndev = (u64)netdev;
3155 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3156
3157 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3158 if (ret < 0) {
3159 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3160 ret);
3161 }
3162
3163 return ret;
3164}
3165
3166static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3167 __be16 proto __attribute__((unused)),
3168 u16 vid)
3169{
3170 struct lio *lio = GET_LIO(netdev);
3171 struct octeon_device *oct = lio->oct_dev;
3172 struct octnic_ctrl_pkt nctrl;
3173 int ret = 0;
3174
3175 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3176
3177 nctrl.ncmd.u64 = 0;
3178 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3179 nctrl.ncmd.s.param1 = vid;
3180 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3181 nctrl.wait_time = 100;
3182 nctrl.netpndev = (u64)netdev;
3183 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3184
3185 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3186 if (ret < 0) {
3187 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3188 ret);
3189 }
3190 return ret;
3191}
3192
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003193/** Sending command to enable/disable RX checksum offload
3194 * @param netdev pointer to network device
3195 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3196 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3197 * OCTNET_CMD_RXCSUM_DISABLE
3198 * @returns SUCCESS or FAILURE
3199 */
3200int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3201 u8 rx_cmd)
3202{
3203 struct lio *lio = GET_LIO(netdev);
3204 struct octeon_device *oct = lio->oct_dev;
3205 struct octnic_ctrl_pkt nctrl;
3206 int ret = 0;
3207
3208 nctrl.ncmd.u64 = 0;
3209 nctrl.ncmd.s.cmd = command;
3210 nctrl.ncmd.s.param1 = rx_cmd;
3211 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3212 nctrl.wait_time = 100;
3213 nctrl.netpndev = (u64)netdev;
3214 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3215
3216 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3217 if (ret < 0) {
3218 dev_err(&oct->pci_dev->dev,
3219 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3220 ret);
3221 }
3222 return ret;
3223}
3224
3225/** Sending command to add/delete VxLAN UDP port to firmware
3226 * @param netdev pointer to network device
3227 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3228 * @param vxlan_port VxLAN port to be added or deleted
3229 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3230 * OCTNET_CMD_VXLAN_PORT_DEL
3231 * @returns SUCCESS or FAILURE
3232 */
3233static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3234 u16 vxlan_port, u8 vxlan_cmd_bit)
3235{
3236 struct lio *lio = GET_LIO(netdev);
3237 struct octeon_device *oct = lio->oct_dev;
3238 struct octnic_ctrl_pkt nctrl;
3239 int ret = 0;
3240
3241 nctrl.ncmd.u64 = 0;
3242 nctrl.ncmd.s.cmd = command;
3243 nctrl.ncmd.s.more = vxlan_cmd_bit;
3244 nctrl.ncmd.s.param1 = vxlan_port;
3245 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3246 nctrl.wait_time = 100;
3247 nctrl.netpndev = (u64)netdev;
3248 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3249
3250 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3251 if (ret < 0) {
3252 dev_err(&oct->pci_dev->dev,
3253 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3254 ret);
3255 }
3256 return ret;
3257}
3258
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003259int liquidio_set_feature(struct net_device *netdev, int cmd, u16 param1)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003260{
3261 struct lio *lio = GET_LIO(netdev);
3262 struct octeon_device *oct = lio->oct_dev;
3263 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003264 int ret = 0;
3265
3266 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3267
3268 nctrl.ncmd.u64 = 0;
3269 nctrl.ncmd.s.cmd = cmd;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003270 nctrl.ncmd.s.param1 = param1;
3271 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003272 nctrl.wait_time = 100;
3273 nctrl.netpndev = (u64)netdev;
3274 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3275
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003276 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003277 if (ret < 0) {
3278 dev_err(&oct->pci_dev->dev, "Feature change failed in core (ret: 0x%x)\n",
3279 ret);
3280 }
3281 return ret;
3282}
3283
3284/** \brief Net device fix features
3285 * @param netdev pointer to network device
3286 * @param request features requested
3287 * @returns updated features list
3288 */
3289static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3290 netdev_features_t request)
3291{
3292 struct lio *lio = netdev_priv(netdev);
3293
3294 if ((request & NETIF_F_RXCSUM) &&
3295 !(lio->dev_capability & NETIF_F_RXCSUM))
3296 request &= ~NETIF_F_RXCSUM;
3297
3298 if ((request & NETIF_F_HW_CSUM) &&
3299 !(lio->dev_capability & NETIF_F_HW_CSUM))
3300 request &= ~NETIF_F_HW_CSUM;
3301
3302 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3303 request &= ~NETIF_F_TSO;
3304
3305 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3306 request &= ~NETIF_F_TSO6;
3307
3308 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3309 request &= ~NETIF_F_LRO;
3310
3311 /*Disable LRO if RXCSUM is off */
3312 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3313 (lio->dev_capability & NETIF_F_LRO))
3314 request &= ~NETIF_F_LRO;
3315
3316 return request;
3317}
3318
3319/** \brief Net device set features
3320 * @param netdev pointer to network device
3321 * @param features features to enable/disable
3322 */
3323static int liquidio_set_features(struct net_device *netdev,
3324 netdev_features_t features)
3325{
3326 struct lio *lio = netdev_priv(netdev);
3327
3328 if (!((netdev->features ^ features) & NETIF_F_LRO))
3329 return 0;
3330
3331 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003332 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3333 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003334 else if (!(features & NETIF_F_LRO) &&
3335 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003336 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3337 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003339 /* Sending command to firmware to enable/disable RX checksum
3340 * offload settings using ethtool
3341 */
3342 if (!(netdev->features & NETIF_F_RXCSUM) &&
3343 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3344 (features & NETIF_F_RXCSUM))
3345 liquidio_set_rxcsum_command(netdev,
3346 OCTNET_CMD_TNL_RX_CSUM_CTL,
3347 OCTNET_CMD_RXCSUM_ENABLE);
3348 else if ((netdev->features & NETIF_F_RXCSUM) &&
3349 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3350 !(features & NETIF_F_RXCSUM))
3351 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3352 OCTNET_CMD_RXCSUM_DISABLE);
3353
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003354 return 0;
3355}
3356
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003357static void liquidio_add_vxlan_port(struct net_device *netdev,
3358 struct udp_tunnel_info *ti)
3359{
3360 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3361 return;
3362
3363 liquidio_vxlan_port_command(netdev,
3364 OCTNET_CMD_VXLAN_PORT_CONFIG,
3365 htons(ti->port),
3366 OCTNET_CMD_VXLAN_PORT_ADD);
3367}
3368
3369static void liquidio_del_vxlan_port(struct net_device *netdev,
3370 struct udp_tunnel_info *ti)
3371{
3372 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3373 return;
3374
3375 liquidio_vxlan_port_command(netdev,
3376 OCTNET_CMD_VXLAN_PORT_CONFIG,
3377 htons(ti->port),
3378 OCTNET_CMD_VXLAN_PORT_DEL);
3379}
3380
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003381static struct net_device_ops lionetdevops = {
3382 .ndo_open = liquidio_open,
3383 .ndo_stop = liquidio_stop,
3384 .ndo_start_xmit = liquidio_xmit,
3385 .ndo_get_stats = liquidio_get_stats,
3386 .ndo_set_mac_address = liquidio_set_mac,
3387 .ndo_set_rx_mode = liquidio_set_mcast_list,
3388 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003389
3390 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3391 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003392 .ndo_change_mtu = liquidio_change_mtu,
3393 .ndo_do_ioctl = liquidio_ioctl,
3394 .ndo_fix_features = liquidio_fix_features,
3395 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003396 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3397 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003398};
3399
3400/** \brief Entry point for the liquidio module
3401 */
3402static int __init liquidio_init(void)
3403{
3404 int i;
3405 struct handshake *hs;
3406
3407 init_completion(&first_stage);
3408
3409 octeon_init_device_list(conf_type);
3410
3411 if (liquidio_init_pci())
3412 return -EINVAL;
3413
3414 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3415
3416 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3417 hs = &handshake[i];
3418 if (hs->pci_dev) {
3419 wait_for_completion(&hs->init);
3420 if (!hs->init_ok) {
3421 /* init handshake failed */
3422 dev_err(&hs->pci_dev->dev,
3423 "Failed to init device\n");
3424 liquidio_deinit_pci();
3425 return -EIO;
3426 }
3427 }
3428 }
3429
3430 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3431 hs = &handshake[i];
3432 if (hs->pci_dev) {
3433 wait_for_completion_timeout(&hs->started,
3434 msecs_to_jiffies(30000));
3435 if (!hs->started_ok) {
3436 /* starter handshake failed */
3437 dev_err(&hs->pci_dev->dev,
3438 "Firmware failed to start\n");
3439 liquidio_deinit_pci();
3440 return -EIO;
3441 }
3442 }
3443 }
3444
3445 return 0;
3446}
3447
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003448static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003449{
3450 struct octeon_device *oct = (struct octeon_device *)buf;
3451 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003452 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003453 union oct_link_status *ls;
3454 int i;
3455
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003456 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003457 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3458 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003459 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003460 goto nic_info_err;
3461 }
3462
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003463 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003464 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3465
3466 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003467 for (i = 0; i < oct->ifcount; i++) {
3468 if (oct->props[i].gmxport == gmxport) {
3469 update_link_status(oct->props[i].netdev, ls);
3470 break;
3471 }
3472 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003473
3474nic_info_err:
3475 for (i = 0; i < recv_pkt->buffer_count; i++)
3476 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3477 octeon_free_recv_info(recv_info);
3478 return 0;
3479}
3480
3481/**
3482 * \brief Setup network interfaces
3483 * @param octeon_dev octeon device
3484 *
3485 * Called during init time for each device. It assumes the NIC
3486 * is already up and running. The link information for each
3487 * interface is passed in link_info.
3488 */
3489static int setup_nic_devices(struct octeon_device *octeon_dev)
3490{
3491 struct lio *lio = NULL;
3492 struct net_device *netdev;
3493 u8 mac[6], i, j;
3494 struct octeon_soft_command *sc;
3495 struct liquidio_if_cfg_context *ctx;
3496 struct liquidio_if_cfg_resp *resp;
3497 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003498 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003499 int num_cpus = num_online_cpus();
3500 union oct_nic_if_cfg if_cfg;
3501 unsigned int base_queue;
3502 unsigned int gmx_port_id;
3503 u32 resp_size, ctx_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003504 u32 ifidx_or_pfnum;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003505
3506 /* This is to handle link status changes */
3507 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3508 OPCODE_NIC_INFO,
3509 lio_nic_info, octeon_dev);
3510
3511 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3512 * They are handled directly.
3513 */
3514 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3515 free_netbuf);
3516
3517 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3518 free_netsgbuf);
3519
3520 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3521 free_netsgbuf_with_resp);
3522
3523 for (i = 0; i < octeon_dev->ifcount; i++) {
3524 resp_size = sizeof(struct liquidio_if_cfg_resp);
3525 ctx_size = sizeof(struct liquidio_if_cfg_context);
3526 sc = (struct octeon_soft_command *)
3527 octeon_alloc_soft_command(octeon_dev, 0,
3528 resp_size, ctx_size);
3529 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3530 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
3531
3532 num_iqueues =
3533 CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3534 num_oqueues =
3535 CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3536 base_queue =
3537 CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
3538 gmx_port_id =
3539 CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003540 ifidx_or_pfnum = i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003541 if (num_iqueues > num_cpus)
3542 num_iqueues = num_cpus;
3543 if (num_oqueues > num_cpus)
3544 num_oqueues = num_cpus;
3545 dev_dbg(&octeon_dev->pci_dev->dev,
3546 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003547 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003548 ACCESS_ONCE(ctx->cond) = 0;
3549 ctx->octeon_id = lio_get_device_id(octeon_dev);
3550 init_waitqueue_head(&ctx->wc);
3551
3552 if_cfg.u64 = 0;
3553 if_cfg.s.num_iqueues = num_iqueues;
3554 if_cfg.s.num_oqueues = num_oqueues;
3555 if_cfg.s.base_queue = base_queue;
3556 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003557
3558 sc->iq_no = 0;
3559
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003560 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003561 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003562 if_cfg.u64, 0);
3563
3564 sc->callback = if_cfg_callback;
3565 sc->callback_arg = sc;
3566 sc->wait_time = 1000;
3567
3568 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003569 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003570 dev_err(&octeon_dev->pci_dev->dev,
3571 "iq/oq config failed status: %x\n",
3572 retval);
3573 /* Soft instr is freed by driver in case of failure. */
3574 goto setup_nic_dev_fail;
3575 }
3576
3577 /* Sleep on a wait queue till the cond flag indicates that the
3578 * response arrived or timed-out.
3579 */
3580 sleep_cond(&ctx->wc, &ctx->cond);
3581 retval = resp->status;
3582 if (retval) {
3583 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3584 goto setup_nic_dev_fail;
3585 }
3586
3587 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3588 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3589
3590 num_iqueues = hweight64(resp->cfg_info.iqmask);
3591 num_oqueues = hweight64(resp->cfg_info.oqmask);
3592
3593 if (!(num_iqueues) || !(num_oqueues)) {
3594 dev_err(&octeon_dev->pci_dev->dev,
3595 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3596 resp->cfg_info.iqmask,
3597 resp->cfg_info.oqmask);
3598 goto setup_nic_dev_fail;
3599 }
3600 dev_dbg(&octeon_dev->pci_dev->dev,
3601 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3602 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3603 num_iqueues, num_oqueues);
3604 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3605
3606 if (!netdev) {
3607 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3608 goto setup_nic_dev_fail;
3609 }
3610
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003611 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003612
3613 if (num_iqueues > 1)
3614 lionetdevops.ndo_select_queue = select_q;
3615
3616 /* Associate the routines that will handle different
3617 * netdev tasks.
3618 */
3619 netdev->netdev_ops = &lionetdevops;
3620
3621 lio = GET_LIO(netdev);
3622
3623 memset(lio, 0, sizeof(struct lio));
3624
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003625 lio->ifidx = ifidx_or_pfnum;
3626
3627 props = &octeon_dev->props[i];
3628 props->gmxport = resp->cfg_info.linfo.gmxport;
3629 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003630
3631 lio->linfo.num_rxpciq = num_oqueues;
3632 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003633 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003634 lio->linfo.rxpciq[j].u64 =
3635 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003636 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003637 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003638 lio->linfo.txpciq[j].u64 =
3639 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003640 }
3641 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3642 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3643 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3644
3645 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3646
3647 lio->dev_capability = NETIF_F_HIGHDMA
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003648 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3649 | NETIF_F_SG | NETIF_F_RXCSUM
3650 | NETIF_F_GRO
3651 | NETIF_F_TSO | NETIF_F_TSO6
3652 | NETIF_F_LRO;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003653 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3654
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003655 /* Copy of transmit encapsulation capabilities:
3656 * TSO, TSO6, Checksums for this device
3657 */
3658 lio->enc_dev_capability = NETIF_F_IP_CSUM
3659 | NETIF_F_IPV6_CSUM
3660 | NETIF_F_GSO_UDP_TUNNEL
3661 | NETIF_F_HW_CSUM | NETIF_F_SG
3662 | NETIF_F_RXCSUM
3663 | NETIF_F_TSO | NETIF_F_TSO6
3664 | NETIF_F_LRO;
3665
3666 netdev->hw_enc_features = (lio->enc_dev_capability &
3667 ~NETIF_F_LRO);
3668
3669 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3670
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003671 netdev->vlan_features = lio->dev_capability;
3672 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003673 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3674 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003675 NETIF_F_HW_VLAN_CTAG_TX;
3676
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003677 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3678
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003679 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003680 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3681 netdev->hw_features = netdev->hw_features &
3682 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003683
3684 /* Point to the properties for octeon device to which this
3685 * interface belongs.
3686 */
3687 lio->oct_dev = octeon_dev;
3688 lio->octprops = props;
3689 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003690
3691 dev_dbg(&octeon_dev->pci_dev->dev,
3692 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3693 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3694
3695 /* 64-bit swap required on LE machines */
3696 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3697 for (j = 0; j < 6; j++)
3698 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3699
3700 /* Copy MAC Address to OS network device structure */
3701
3702 ether_addr_copy(netdev->dev_addr, mac);
3703
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003704 /* By default all interfaces on a single Octeon uses the same
3705 * tx and rx queues
3706 */
3707 lio->txq = lio->linfo.txpciq[0].s.q_no;
3708 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003709 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003710 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3711 goto setup_nic_dev_fail;
3712 }
3713
3714 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3715
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003716 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3717 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3718
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003719 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003720 dev_err(&octeon_dev->pci_dev->dev,
3721 "Gather list allocation failed\n");
3722 goto setup_nic_dev_fail;
3723 }
3724
3725 /* Register ethtool support */
3726 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayif5a20472016-06-21 22:53:14 -07003727 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003728
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003729 if (netdev->features & NETIF_F_LRO)
3730 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3731 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003732
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003733 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
3734
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003735 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003736 liquidio_set_feature(netdev,
3737 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003738
3739 /* Register the network device with the OS */
3740 if (register_netdev(netdev)) {
3741 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3742 goto setup_nic_dev_fail;
3743 }
3744
3745 dev_dbg(&octeon_dev->pci_dev->dev,
3746 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3747 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3748 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003749 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003750
3751 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3752
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003753 /* Sending command to firmware to enable Rx checksum offload
3754 * by default at the time of setup of Liquidio driver for
3755 * this device
3756 */
3757 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3758 OCTNET_CMD_RXCSUM_ENABLE);
3759 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3760 OCTNET_CMD_TXCSUM_ENABLE);
3761
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003762 dev_dbg(&octeon_dev->pci_dev->dev,
3763 "NIC ifidx:%d Setup successful\n", i);
3764
3765 octeon_free_soft_command(octeon_dev, sc);
3766 }
3767
3768 return 0;
3769
3770setup_nic_dev_fail:
3771
3772 octeon_free_soft_command(octeon_dev, sc);
3773
3774 while (i--) {
3775 dev_err(&octeon_dev->pci_dev->dev,
3776 "NIC ifidx:%d Setup failed\n", i);
3777 liquidio_destroy_nic_device(octeon_dev, i);
3778 }
3779 return -ENODEV;
3780}
3781
3782/**
3783 * \brief initialize the NIC
3784 * @param oct octeon device
3785 *
3786 * This initialization routine is called once the Octeon device application is
3787 * up and running
3788 */
3789static int liquidio_init_nic_module(struct octeon_device *oct)
3790{
3791 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003792 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003793 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3794
3795 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3796
3797 /* only default iq and oq were initialized
3798 * initialize the rest as well
3799 */
3800 /* run port_config command for each port */
3801 oct->ifcount = num_nic_ports;
3802
3803 memset(oct->props, 0,
3804 sizeof(struct octdev_props) * num_nic_ports);
3805
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003806 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3807 oct->props[i].gmxport = -1;
3808
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003809 retval = setup_nic_devices(oct);
3810 if (retval) {
3811 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3812 goto octnet_init_failure;
3813 }
3814
3815 liquidio_ptp_init(oct);
3816
3817 /* Initialize interrupt moderation params */
3818 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07003819 intrmod_cfg->rx_enable = 1;
3820 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3821 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3822 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3823 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
3824 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
3825 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
3826 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
3827 intrmod_cfg->tx_enable = 1;
3828 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
3829 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
3830 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
3831 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003832 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3833
3834 return retval;
3835
3836octnet_init_failure:
3837
3838 oct->ifcount = 0;
3839
3840 return retval;
3841}
3842
3843/**
3844 * \brief starter callback that invokes the remaining initialization work after
3845 * the NIC is up and running.
3846 * @param octptr work struct work_struct
3847 */
3848static void nic_starter(struct work_struct *work)
3849{
3850 struct octeon_device *oct;
3851 struct cavium_wk *wk = (struct cavium_wk *)work;
3852
3853 oct = (struct octeon_device *)wk->ctxptr;
3854
3855 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3856 return;
3857
3858 /* If the status of the device is CORE_OK, the core
3859 * application has reported its application type. Call
3860 * any registered handlers now and move to the RUNNING
3861 * state.
3862 */
3863 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3864 schedule_delayed_work(&oct->nic_poll_work.work,
3865 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3866 return;
3867 }
3868
3869 atomic_set(&oct->status, OCT_DEV_RUNNING);
3870
3871 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3872 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3873
3874 if (liquidio_init_nic_module(oct))
3875 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3876 else
3877 handshake[oct->octeon_id].started_ok = 1;
3878 } else {
3879 dev_err(&oct->pci_dev->dev,
3880 "Unexpected application running on NIC (%d). Check firmware.\n",
3881 oct->app_mode);
3882 }
3883
3884 complete(&handshake[oct->octeon_id].started);
3885}
3886
3887/**
3888 * \brief Device initialization for each Octeon device that is probed
3889 * @param octeon_dev octeon device
3890 */
3891static int octeon_device_init(struct octeon_device *octeon_dev)
3892{
3893 int j, ret;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07003894 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003895 struct octeon_device_priv *oct_priv =
3896 (struct octeon_device_priv *)octeon_dev->priv;
3897 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3898
3899 /* Enable access to the octeon device and make its DMA capability
3900 * known to the OS.
3901 */
3902 if (octeon_pci_os_setup(octeon_dev))
3903 return 1;
3904
3905 /* Identify the Octeon type and map the BAR address space. */
3906 if (octeon_chip_specific_setup(octeon_dev)) {
3907 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3908 return 1;
3909 }
3910
3911 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3912
3913 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3914
3915 /* Do a soft reset of the Octeon device. */
3916 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3917 return 1;
3918
3919 /* Initialize the dispatch mechanism used to push packets arriving on
3920 * Octeon Output queues.
3921 */
3922 if (octeon_init_dispatch_list(octeon_dev))
3923 return 1;
3924
3925 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3926 OPCODE_NIC_CORE_DRV_ACTIVE,
3927 octeon_core_drv_init,
3928 octeon_dev);
3929
3930 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3931 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3932 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3933 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3934
3935 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3936
3937 octeon_set_io_queues_off(octeon_dev);
3938
3939 /* Setup the data structures that manage this Octeon's Input queues. */
3940 if (octeon_setup_instr_queues(octeon_dev)) {
3941 dev_err(&octeon_dev->pci_dev->dev,
3942 "instruction queue initialization failed\n");
3943 /* On error, release any previously allocated queues */
3944 for (j = 0; j < octeon_dev->num_iqs; j++)
3945 octeon_delete_instr_queue(octeon_dev, j);
3946 return 1;
3947 }
3948 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3949
3950 /* Initialize soft command buffer pool
3951 */
3952 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3953 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3954 return 1;
3955 }
3956 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3957
3958 /* Initialize lists to manage the requests of different types that
3959 * arrive from user & kernel applications for this octeon device.
3960 */
3961 if (octeon_setup_response_list(octeon_dev)) {
3962 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3963 return 1;
3964 }
3965 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3966
3967 if (octeon_setup_output_queues(octeon_dev)) {
3968 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
3969 /* Release any previously allocated queues */
3970 for (j = 0; j < octeon_dev->num_oqs; j++)
3971 octeon_delete_droq(octeon_dev, j);
3972 }
3973
3974 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
3975
3976 /* The input and output queue registers were setup earlier (the queues
3977 * were not enabled). Any additional registers that need to be
3978 * programmed should be done now.
3979 */
3980 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3981 if (ret) {
3982 dev_err(&octeon_dev->pci_dev->dev,
3983 "Failed to configure device registers\n");
3984 return ret;
3985 }
3986
3987 /* Initialize the tasklet that handles output queue packet processing.*/
3988 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
3989 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
3990 (unsigned long)octeon_dev);
3991
3992 /* Setup the interrupt handler and record the INT SUM register address
3993 */
3994 octeon_setup_interrupt(octeon_dev);
3995
3996 /* Enable Octeon device interrupts */
3997 octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
3998
3999 /* Enable the input and output queues for this Octeon device */
4000 octeon_dev->fn_list.enable_io_queues(octeon_dev);
4001
4002 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4003
4004 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4005
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004006 if (ddr_timeout == 0)
4007 dev_info(&octeon_dev->pci_dev->dev, "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004008
4009 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
4010
4011 /* Wait for the octeon to initialize DDR after the soft-reset. */
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004012 while (ddr_timeout == 0) {
4013 set_current_state(TASK_INTERRUPTIBLE);
4014 if (schedule_timeout(HZ / 10)) {
4015 /* user probably pressed Control-C */
4016 return 1;
4017 }
4018 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004019 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4020 if (ret) {
4021 dev_err(&octeon_dev->pci_dev->dev,
4022 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4023 ret);
4024 return 1;
4025 }
4026
4027 if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
4028 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4029 return 1;
4030 }
4031
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004032 /* Divert uboot to take commands from host instead. */
4033 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
4034
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004035 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4036 ret = octeon_init_consoles(octeon_dev);
4037 if (ret) {
4038 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4039 return 1;
4040 }
4041 ret = octeon_add_console(octeon_dev, 0);
4042 if (ret) {
4043 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4044 return 1;
4045 }
4046
4047 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
4048
4049 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4050 ret = load_firmware(octeon_dev);
4051 if (ret) {
4052 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4053 return 1;
4054 }
4055
4056 handshake[octeon_dev->octeon_id].init_ok = 1;
4057 complete(&handshake[octeon_dev->octeon_id].init);
4058
4059 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4060
4061 /* Send Credit for Octeon Output queues. Credits are always sent after
4062 * the output queue is enabled.
4063 */
4064 for (j = 0; j < octeon_dev->num_oqs; j++)
4065 writel(octeon_dev->droq[j]->max_count,
4066 octeon_dev->droq[j]->pkts_credit_reg);
4067
4068 /* Packets can start arriving on the output queues from this point. */
4069
4070 return 0;
4071}
4072
4073/**
4074 * \brief Exits the module
4075 */
4076static void __exit liquidio_exit(void)
4077{
4078 liquidio_deinit_pci();
4079
4080 pr_info("LiquidIO network module is now unloaded\n");
4081}
4082
4083module_init(liquidio_init);
4084module_exit(liquidio_exit);