blob: afce65976de3a197e50458155e6f976f4b7fd1f1 [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>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include <linux/firmware.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070025#include <linux/ptp_clock_kernel.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070026#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070027#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070028#include "liquidio_common.h"
29#include "octeon_droq.h"
30#include "octeon_iq.h"
31#include "response_manager.h"
32#include "octeon_device.h"
33#include "octeon_nic.h"
34#include "octeon_main.h"
35#include "octeon_network.h"
36#include "cn66xx_regs.h"
37#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070038#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070039#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070040#include "liquidio_image.h"
41
42MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
43MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
44MODULE_LICENSE("GPL");
45MODULE_VERSION(LIQUIDIO_VERSION);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
47MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
48MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
49
50static int ddr_timeout = 10000;
51module_param(ddr_timeout, int, 0644);
52MODULE_PARM_DESC(ddr_timeout,
53 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
54
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070055#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
56
Raghu Vatsavayi1f164712016-06-21 22:53:11 -070057#define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \
58 (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count)
59
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070060static int debug = -1;
61module_param(debug, int, 0644);
62MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
63
64static char fw_type[LIO_MAX_FW_TYPE_LEN];
65module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
66MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
67
68static int conf_type;
69module_param(conf_type, int, 0);
70MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
71
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070072static int ptp_enable = 1;
73
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070074/* Bit mask values for lio->ifstate */
75#define LIO_IFSTATE_DROQ_OPS 0x01
76#define LIO_IFSTATE_REGISTERED 0x02
77#define LIO_IFSTATE_RUNNING 0x04
78#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
79
80/* Polling interval for determining when NIC application is alive */
81#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
82
83/* runtime link query interval */
84#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
85
86struct liquidio_if_cfg_context {
87 int octeon_id;
88
89 wait_queue_head_t wc;
90
91 int cond;
92};
93
94struct liquidio_if_cfg_resp {
95 u64 rh;
96 struct liquidio_if_cfg_info cfg_info;
97 u64 status;
98};
99
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -0700100struct liquidio_rx_ctl_context {
101 int octeon_id;
102
103 wait_queue_head_t wc;
104
105 int cond;
106};
107
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700108struct oct_link_status_resp {
109 u64 rh;
110 struct oct_link_info link_info;
111 u64 status;
112};
113
114struct oct_timestamp_resp {
115 u64 rh;
116 u64 timestamp;
117 u64 status;
118};
119
120#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
121
122union tx_info {
123 u64 u64;
124 struct {
125#ifdef __BIG_ENDIAN_BITFIELD
126 u16 gso_size;
127 u16 gso_segs;
128 u32 reserved;
129#else
130 u32 reserved;
131 u16 gso_segs;
132 u16 gso_size;
133#endif
134 } s;
135};
136
137/** Octeon device properties to be used by the NIC module.
138 * Each octeon device in the system will be represented
139 * by this structure in the NIC module.
140 */
141
142#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
143
144#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700145#define OCTNIC_GSO_MAX_SIZE \
146 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700147
148/** Structure of a node in list of gather components maintained by
149 * NIC driver for each network device.
150 */
151struct octnic_gather {
152 /** List manipulation. Next and prev pointers. */
153 struct list_head list;
154
155 /** Size of the gather component at sg in bytes. */
156 int sg_size;
157
158 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
159 int adjust;
160
161 /** Gather component that can accommodate max sized fragment list
162 * received from the IP layer.
163 */
164 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700165
166 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700167};
168
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700169struct handshake {
170 struct completion init;
171 struct completion started;
172 struct pci_dev *pci_dev;
173 int init_ok;
174 int started_ok;
175};
176
177struct octeon_device_priv {
178 /** Tasklet structures for this device. */
179 struct tasklet_struct droq_tasklet;
180 unsigned long napi_mask;
181};
182
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800183#ifdef CONFIG_PCI_IOV
184static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
185#endif
186
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700187static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700188static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700189static void liquidio_remove(struct pci_dev *pdev);
190static int liquidio_probe(struct pci_dev *pdev,
191 const struct pci_device_id *ent);
192
193static struct handshake handshake[MAX_OCTEON_DEVICES];
194static struct completion first_stage;
195
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700196static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700197{
198 int q_no;
199 int reschedule = 0;
200 struct octeon_device *oct = (struct octeon_device *)pdev;
201 struct octeon_device_priv *oct_priv =
202 (struct octeon_device_priv *)oct->priv;
203
204 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700205 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
206 if (!(oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700207 continue;
208 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
209 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700210 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700211
212 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
213 /* set time and cnt interrupt thresholds for this DROQ
214 * for NAPI
215 */
216 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
217
218 octeon_write_csr64(
219 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
220 0x5700000040ULL);
221 octeon_write_csr64(
222 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
223 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700224 }
225
226 if (reschedule)
227 tasklet_schedule(&oct_priv->droq_tasklet);
228}
229
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700230static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700231{
232 struct octeon_device_priv *oct_priv =
233 (struct octeon_device_priv *)oct->priv;
234 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
235 int i;
236
237 do {
238 pending_pkts = 0;
239
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700240 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
241 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700242 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700243 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700244 }
245 if (pkt_cnt > 0) {
246 pending_pkts += pkt_cnt;
247 tasklet_schedule(&oct_priv->droq_tasklet);
248 }
249 pkt_cnt = 0;
250 schedule_timeout_uninterruptible(1);
251
252 } while (retry-- && pending_pkts);
253
254 return pkt_cnt;
255}
256
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700257/**
258 * \brief Forces all IO queues off on a given device
259 * @param oct Pointer to Octeon device
260 */
261static void force_io_queues_off(struct octeon_device *oct)
262{
263 if ((oct->chip_id == OCTEON_CN66XX) ||
264 (oct->chip_id == OCTEON_CN68XX)) {
265 /* Reset the Enable bits for Input Queues. */
266 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
267
268 /* Reset the Enable bits for Output Queues. */
269 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
270 }
271}
272
273/**
274 * \brief wait for all pending requests to complete
275 * @param oct Pointer to Octeon device
276 *
277 * Called during shutdown sequence
278 */
279static int wait_for_pending_requests(struct octeon_device *oct)
280{
281 int i, pcount = 0;
282
283 for (i = 0; i < 100; i++) {
284 pcount =
285 atomic_read(&oct->response_list
286 [OCTEON_ORDERED_SC_LIST].pending_req_count);
287 if (pcount)
288 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700289 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700290 break;
291 }
292
293 if (pcount)
294 return 1;
295
296 return 0;
297}
298
299/**
300 * \brief Cause device to go quiet so it can be safely removed/reset/etc
301 * @param oct Pointer to Octeon device
302 */
303static inline void pcierror_quiesce_device(struct octeon_device *oct)
304{
305 int i;
306
307 /* Disable the input and output queues now. No more packets will
308 * arrive from Octeon, but we should wait for all packet processing
309 * to finish.
310 */
311 force_io_queues_off(oct);
312
313 /* To allow for in-flight requests */
314 schedule_timeout_uninterruptible(100);
315
316 if (wait_for_pending_requests(oct))
317 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
318
319 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700320 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700321 struct octeon_instr_queue *iq;
322
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700323 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700324 continue;
325 iq = oct->instr_queue[i];
326
327 if (atomic_read(&iq->instr_pending)) {
328 spin_lock_bh(&iq->lock);
329 iq->fill_cnt = 0;
330 iq->octeon_read_index = iq->host_write_index;
331 iq->stats.instr_processed +=
332 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700333 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700334 spin_unlock_bh(&iq->lock);
335 }
336 }
337
338 /* Force all pending ordered list requests to time out. */
339 lio_process_ordered_list(oct, 1);
340
341 /* We do not need to wait for output queue packets to be processed. */
342}
343
344/**
345 * \brief Cleanup PCI AER uncorrectable error status
346 * @param dev Pointer to PCI device
347 */
348static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
349{
350 int pos = 0x100;
351 u32 status, mask;
352
353 pr_info("%s :\n", __func__);
354
355 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
356 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
357 if (dev->error_state == pci_channel_io_normal)
358 status &= ~mask; /* Clear corresponding nonfatal bits */
359 else
360 status &= mask; /* Clear corresponding fatal bits */
361 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
362}
363
364/**
365 * \brief Stop all PCI IO to a given device
366 * @param dev Pointer to Octeon device
367 */
368static void stop_pci_io(struct octeon_device *oct)
369{
370 /* No more instructions will be forwarded. */
371 atomic_set(&oct->status, OCT_DEV_IN_RESET);
372
373 pci_disable_device(oct->pci_dev);
374
375 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700376 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700377
378 pcierror_quiesce_device(oct);
379
380 /* Release the interrupt line */
381 free_irq(oct->pci_dev->irq, oct);
382
383 if (oct->flags & LIO_FLAG_MSI_ENABLED)
384 pci_disable_msi(oct->pci_dev);
385
386 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
387 lio_get_state_string(&oct->status));
388
389 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
390 /* making it a common function for all OCTEON models */
391 cleanup_aer_uncorrect_error_status(oct->pci_dev);
392}
393
394/**
395 * \brief called when PCI error is detected
396 * @param pdev Pointer to PCI device
397 * @param state The current pci connection state
398 *
399 * This function is called after a PCI bus error affecting
400 * this device has been detected.
401 */
402static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
403 pci_channel_state_t state)
404{
405 struct octeon_device *oct = pci_get_drvdata(pdev);
406
407 /* Non-correctable Non-fatal errors */
408 if (state == pci_channel_io_normal) {
409 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
410 cleanup_aer_uncorrect_error_status(oct->pci_dev);
411 return PCI_ERS_RESULT_CAN_RECOVER;
412 }
413
414 /* Non-correctable Fatal errors */
415 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
416 stop_pci_io(oct);
417
418 /* Always return a DISCONNECT. There is no support for recovery but only
419 * for a clean shutdown.
420 */
421 return PCI_ERS_RESULT_DISCONNECT;
422}
423
424/**
425 * \brief mmio handler
426 * @param pdev Pointer to PCI device
427 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700428static pci_ers_result_t liquidio_pcie_mmio_enabled(
429 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700430{
431 /* We should never hit this since we never ask for a reset for a Fatal
432 * Error. We always return DISCONNECT in io_error above.
433 * But play safe and return RECOVERED for now.
434 */
435 return PCI_ERS_RESULT_RECOVERED;
436}
437
438/**
439 * \brief called after the pci bus has been reset.
440 * @param pdev Pointer to PCI device
441 *
442 * Restart the card from scratch, as if from a cold-boot. Implementation
443 * resembles the first-half of the octeon_resume routine.
444 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700445static pci_ers_result_t liquidio_pcie_slot_reset(
446 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700447{
448 /* We should never hit this since we never ask for a reset for a Fatal
449 * Error. We always return DISCONNECT in io_error above.
450 * But play safe and return RECOVERED for now.
451 */
452 return PCI_ERS_RESULT_RECOVERED;
453}
454
455/**
456 * \brief called when traffic can start flowing again.
457 * @param pdev Pointer to PCI device
458 *
459 * This callback is called when the error recovery driver tells us that
460 * its OK to resume normal operation. Implementation resembles the
461 * second-half of the octeon_resume routine.
462 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700463static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700464{
465 /* Nothing to be done here. */
466}
467
468#ifdef CONFIG_PM
469/**
470 * \brief called when suspending
471 * @param pdev Pointer to PCI device
472 * @param state state to suspend to
473 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700474static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
475 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700476{
477 return 0;
478}
479
480/**
481 * \brief called when resuming
482 * @param pdev Pointer to PCI device
483 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700484static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700485{
486 return 0;
487}
488#endif
489
490/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100491static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700492 .error_detected = liquidio_pcie_error_detected,
493 .mmio_enabled = liquidio_pcie_mmio_enabled,
494 .slot_reset = liquidio_pcie_slot_reset,
495 .resume = liquidio_pcie_resume,
496};
497
498static const struct pci_device_id liquidio_pci_tbl[] = {
499 { /* 68xx */
500 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
501 },
502 { /* 66xx */
503 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
504 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700505 { /* 23xx pf */
506 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
507 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700508 {
509 0, 0, 0, 0, 0, 0, 0
510 }
511};
512MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
513
514static struct pci_driver liquidio_pci_driver = {
515 .name = "LiquidIO",
516 .id_table = liquidio_pci_tbl,
517 .probe = liquidio_probe,
518 .remove = liquidio_remove,
519 .err_handler = &liquidio_err_handler, /* For AER */
520
521#ifdef CONFIG_PM
522 .suspend = liquidio_suspend,
523 .resume = liquidio_resume,
524#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800525#ifdef CONFIG_PCI_IOV
526 .sriov_configure = liquidio_enable_sriov,
527#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700528};
529
530/**
531 * \brief register PCI driver
532 */
533static int liquidio_init_pci(void)
534{
535 return pci_register_driver(&liquidio_pci_driver);
536}
537
538/**
539 * \brief unregister PCI driver
540 */
541static void liquidio_deinit_pci(void)
542{
543 pci_unregister_driver(&liquidio_pci_driver);
544}
545
546/**
547 * \brief check interface state
548 * @param lio per-network private data
549 * @param state_flag flag state to check
550 */
551static inline int ifstate_check(struct lio *lio, int state_flag)
552{
553 return atomic_read(&lio->ifstate) & state_flag;
554}
555
556/**
557 * \brief set interface state
558 * @param lio per-network private data
559 * @param state_flag flag state to set
560 */
561static inline void ifstate_set(struct lio *lio, int state_flag)
562{
563 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
564}
565
566/**
567 * \brief clear interface state
568 * @param lio per-network private data
569 * @param state_flag flag state to clear
570 */
571static inline void ifstate_reset(struct lio *lio, int state_flag)
572{
573 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
574}
575
576/**
577 * \brief Stop Tx queues
578 * @param netdev network device
579 */
580static inline void txqs_stop(struct net_device *netdev)
581{
582 if (netif_is_multiqueue(netdev)) {
583 int i;
584
585 for (i = 0; i < netdev->num_tx_queues; i++)
586 netif_stop_subqueue(netdev, i);
587 } else {
588 netif_stop_queue(netdev);
589 }
590}
591
592/**
593 * \brief Start Tx queues
594 * @param netdev network device
595 */
596static inline void txqs_start(struct net_device *netdev)
597{
598 if (netif_is_multiqueue(netdev)) {
599 int i;
600
601 for (i = 0; i < netdev->num_tx_queues; i++)
602 netif_start_subqueue(netdev, i);
603 } else {
604 netif_start_queue(netdev);
605 }
606}
607
608/**
609 * \brief Wake Tx queues
610 * @param netdev network device
611 */
612static inline void txqs_wake(struct net_device *netdev)
613{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700614 struct lio *lio = GET_LIO(netdev);
615
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700616 if (netif_is_multiqueue(netdev)) {
617 int i;
618
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700619 for (i = 0; i < netdev->num_tx_queues; i++) {
620 int qno = lio->linfo.txpciq[i %
621 (lio->linfo.num_txpciq)].s.q_no;
622
623 if (__netif_subqueue_stopped(netdev, i)) {
624 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
625 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700626 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700627 }
628 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700629 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700630 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
631 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700632 netif_wake_queue(netdev);
633 }
634}
635
636/**
637 * \brief Stop Tx queue
638 * @param netdev network device
639 */
640static void stop_txq(struct net_device *netdev)
641{
642 txqs_stop(netdev);
643}
644
645/**
646 * \brief Start Tx queue
647 * @param netdev network device
648 */
649static void start_txq(struct net_device *netdev)
650{
651 struct lio *lio = GET_LIO(netdev);
652
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700653 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700654 txqs_start(netdev);
655 return;
656 }
657}
658
659/**
660 * \brief Wake a queue
661 * @param netdev network device
662 * @param q which queue to wake
663 */
664static inline void wake_q(struct net_device *netdev, int q)
665{
666 if (netif_is_multiqueue(netdev))
667 netif_wake_subqueue(netdev, q);
668 else
669 netif_wake_queue(netdev);
670}
671
672/**
673 * \brief Stop a queue
674 * @param netdev network device
675 * @param q which queue to stop
676 */
677static inline void stop_q(struct net_device *netdev, int q)
678{
679 if (netif_is_multiqueue(netdev))
680 netif_stop_subqueue(netdev, q);
681 else
682 netif_stop_queue(netdev);
683}
684
685/**
686 * \brief Check Tx queue status, and take appropriate action
687 * @param lio per-network private data
688 * @returns 0 if full, number of queues woken up otherwise
689 */
690static inline int check_txq_status(struct lio *lio)
691{
692 int ret_val = 0;
693
694 if (netif_is_multiqueue(lio->netdev)) {
695 int numqs = lio->netdev->num_tx_queues;
696 int q, iq = 0;
697
698 /* check each sub-queue state */
699 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700700 iq = lio->linfo.txpciq[q %
701 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700702 if (octnet_iq_is_full(lio->oct_dev, iq))
703 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700704 if (__netif_subqueue_stopped(lio->netdev, q)) {
705 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700706 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
707 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700708 ret_val++;
709 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700710 }
711 } else {
712 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
713 return 0;
714 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700715 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
716 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700717 ret_val = 1;
718 }
719 return ret_val;
720}
721
722/**
723 * Remove the node at the head of the list. The list would be empty at
724 * the end of this call if there are no more nodes in the list.
725 */
726static inline struct list_head *list_delete_head(struct list_head *root)
727{
728 struct list_head *node;
729
730 if ((root->prev == root) && (root->next == root))
731 node = NULL;
732 else
733 node = root->next;
734
735 if (node)
736 list_del(node);
737
738 return node;
739}
740
741/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700742 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700743 * @param lio per-network private data
744 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700745static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700746{
747 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700748 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700749
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700750 if (!lio->glist)
751 return;
752
753 for (i = 0; i < lio->linfo.num_txpciq; i++) {
754 do {
755 g = (struct octnic_gather *)
756 list_delete_head(&lio->glist[i]);
757 if (g) {
758 if (g->sg) {
759 dma_unmap_single(&lio->oct_dev->
760 pci_dev->dev,
761 g->sg_dma_ptr,
762 g->sg_size,
763 DMA_TO_DEVICE);
764 kfree((void *)((unsigned long)g->sg -
765 g->adjust));
766 }
767 kfree(g);
768 }
769 } while (g);
770 }
771
772 kfree((void *)lio->glist);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700773}
774
775/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700776 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700777 * @param lio per-network private data
778 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700779static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700780{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700781 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700782 struct octnic_gather *g;
783
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700784 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
785 GFP_KERNEL);
786 if (!lio->glist_lock)
787 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700788
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700789 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
790 GFP_KERNEL);
791 if (!lio->glist) {
792 kfree((void *)lio->glist_lock);
793 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700794 }
795
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700796 for (i = 0; i < num_iqs; i++) {
797 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700798
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700799 spin_lock_init(&lio->glist_lock[i]);
800
801 INIT_LIST_HEAD(&lio->glist[i]);
802
803 for (j = 0; j < lio->tx_qsize; j++) {
804 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
805 numa_node);
806 if (!g)
807 g = kzalloc(sizeof(*g), GFP_KERNEL);
808 if (!g)
809 break;
810
811 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
812 OCT_SG_ENTRY_SIZE);
813
814 g->sg = kmalloc_node(g->sg_size + 8,
815 GFP_KERNEL, numa_node);
816 if (!g->sg)
817 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
818 if (!g->sg) {
819 kfree(g);
820 break;
821 }
822
823 /* The gather component should be aligned on 64-bit
824 * boundary
825 */
826 if (((unsigned long)g->sg) & 7) {
827 g->adjust = 8 - (((unsigned long)g->sg) & 7);
828 g->sg = (struct octeon_sg_entry *)
829 ((unsigned long)g->sg + g->adjust);
830 }
831 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
832 g->sg, g->sg_size,
833 DMA_TO_DEVICE);
834 if (dma_mapping_error(&oct->pci_dev->dev,
835 g->sg_dma_ptr)) {
836 kfree((void *)((unsigned long)g->sg -
837 g->adjust));
838 kfree(g);
839 break;
840 }
841
842 list_add_tail(&g->list, &lio->glist[i]);
843 }
844
845 if (j != lio->tx_qsize) {
846 delete_glists(lio);
847 return 1;
848 }
849 }
850
851 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700852}
853
854/**
855 * \brief Print link information
856 * @param netdev network device
857 */
858static void print_link_info(struct net_device *netdev)
859{
860 struct lio *lio = GET_LIO(netdev);
861
862 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
863 struct oct_link_info *linfo = &lio->linfo;
864
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700865 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700866 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
867 linfo->link.s.speed,
868 (linfo->link.s.duplex) ? "Full" : "Half");
869 } else {
870 netif_info(lio, link, lio->netdev, "Link Down\n");
871 }
872 }
873}
874
875/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700876 * \brief Routine to notify MTU change
877 * @param work work_struct data structure
878 */
879static void octnet_link_status_change(struct work_struct *work)
880{
881 struct cavium_wk *wk = (struct cavium_wk *)work;
882 struct lio *lio = (struct lio *)wk->ctxptr;
883
884 rtnl_lock();
885 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
886 rtnl_unlock();
887}
888
889/**
890 * \brief Sets up the mtu status change work
891 * @param netdev network device
892 */
893static inline int setup_link_status_change_wq(struct net_device *netdev)
894{
895 struct lio *lio = GET_LIO(netdev);
896 struct octeon_device *oct = lio->oct_dev;
897
898 lio->link_status_wq.wq = alloc_workqueue("link-status",
899 WQ_MEM_RECLAIM, 0);
900 if (!lio->link_status_wq.wq) {
901 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
902 return -1;
903 }
904 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
905 octnet_link_status_change);
906 lio->link_status_wq.wk.ctxptr = lio;
907
908 return 0;
909}
910
911static inline void cleanup_link_status_change_wq(struct net_device *netdev)
912{
913 struct lio *lio = GET_LIO(netdev);
914
915 if (lio->link_status_wq.wq) {
916 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
917 destroy_workqueue(lio->link_status_wq.wq);
918 }
919}
920
921/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700922 * \brief Update link status
923 * @param netdev network device
924 * @param ls link status structure
925 *
926 * Called on receipt of a link status response from the core application to
927 * update each interface's link status.
928 */
929static inline void update_link_status(struct net_device *netdev,
930 union oct_link_status *ls)
931{
932 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700933 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700934
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700935 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700936
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700937 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700938 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700939 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700940
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700941 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700942 netif_carrier_on(netdev);
943 /* start_txq(netdev); */
944 txqs_wake(netdev);
945 } else {
946 netif_carrier_off(netdev);
947 stop_txq(netdev);
948 }
949 }
950}
951
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700952/* Runs in interrupt context. */
953static void update_txq_status(struct octeon_device *oct, int iq_num)
954{
955 struct net_device *netdev;
956 struct lio *lio;
957 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
958
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700959 netdev = oct->props[iq->ifidx].netdev;
960
961 /* This is needed because the first IQ does not have
962 * a netdev associated with it.
963 */
964 if (!netdev)
965 return;
966
967 lio = GET_LIO(netdev);
968 if (netif_is_multiqueue(netdev)) {
969 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
970 lio->linfo.link.s.link_up &&
971 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700972 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
973 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700974 netif_wake_subqueue(netdev, iq->q_index);
975 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700976 if (!octnet_iq_is_full(oct, lio->txq)) {
977 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
978 lio->txq,
979 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700980 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700981 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700982 }
983 }
984}
985
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700986static
987int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
988{
989 struct octeon_device *oct = droq->oct_dev;
990 struct octeon_device_priv *oct_priv =
991 (struct octeon_device_priv *)oct->priv;
992
993 if (droq->ops.poll_mode) {
994 droq->ops.napi_fn(droq);
995 } else {
996 if (ret & MSIX_PO_INT) {
997 tasklet_schedule(&oct_priv->droq_tasklet);
998 return 1;
999 }
1000 /* this will be flushed periodically by check iq db */
1001 if (ret & MSIX_PI_INT)
1002 return 0;
1003 }
1004 return 0;
1005}
1006
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001007/**
1008 * \brief Droq packet processor sceduler
1009 * @param oct octeon device
1010 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07001011static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001012{
1013 struct octeon_device_priv *oct_priv =
1014 (struct octeon_device_priv *)oct->priv;
1015 u64 oq_no;
1016 struct octeon_droq *droq;
1017
1018 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001019 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1020 oq_no++) {
1021 if (!(oct->droq_intr & (1ULL << oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001022 continue;
1023
1024 droq = oct->droq[oq_no];
1025
1026 if (droq->ops.poll_mode) {
1027 droq->ops.napi_fn(droq);
1028 oct_priv->napi_mask |= (1 << oq_no);
1029 } else {
1030 tasklet_schedule(&oct_priv->droq_tasklet);
1031 }
1032 }
1033 }
1034}
1035
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001036static irqreturn_t
1037liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1038{
1039 u64 ret;
1040 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1041 struct octeon_device *oct = ioq_vector->oct_dev;
1042 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1043
1044 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1045
1046 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1047 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1048
1049 return IRQ_HANDLED;
1050}
1051
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001052/**
1053 * \brief Interrupt handler for octeon
1054 * @param irq unused
1055 * @param dev octeon device
1056 */
1057static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001058irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1059 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001060{
1061 struct octeon_device *oct = (struct octeon_device *)dev;
1062 irqreturn_t ret;
1063
1064 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001065 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001066
1067 ret = oct->fn_list.process_interrupt_regs(oct);
1068
1069 if (ret == IRQ_HANDLED)
1070 liquidio_schedule_droq_pkt_handlers(oct);
1071
1072 /* Re-enable our interrupts */
1073 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001074 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001075
1076 return ret;
1077}
1078
1079/**
1080 * \brief Setup interrupt for octeon device
1081 * @param oct octeon device
1082 *
1083 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1084 */
1085static int octeon_setup_interrupt(struct octeon_device *oct)
1086{
1087 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001088 struct msix_entry *msix_entries;
1089 int i;
1090 int num_ioq_vectors;
1091 int num_alloc_ioq_vectors;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001092
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001093 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1094 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1095 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1096 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001097
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001098 oct->msix_entries = kcalloc(
1099 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
1100 if (!oct->msix_entries)
1101 return 1;
1102
1103 msix_entries = (struct msix_entry *)oct->msix_entries;
1104 /*Assumption is that pf msix vectors start from pf srn to pf to
1105 * trs and not from 0. if not change this code
1106 */
1107 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1108 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1109 msix_entries[oct->num_msix_irqs - 1].entry =
1110 oct->sriov_info.trs;
1111 num_alloc_ioq_vectors = pci_enable_msix_range(
1112 oct->pci_dev, msix_entries,
1113 oct->num_msix_irqs,
1114 oct->num_msix_irqs);
1115 if (num_alloc_ioq_vectors < 0) {
1116 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1117 kfree(oct->msix_entries);
1118 oct->msix_entries = NULL;
1119 return 1;
1120 }
1121 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1122
1123 num_ioq_vectors = oct->num_msix_irqs;
1124
1125 /** For PF, there is one non-ioq interrupt handler */
1126 num_ioq_vectors -= 1;
1127 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
1128 liquidio_legacy_intr_handler, 0, "octeon",
1129 oct);
1130 if (irqret) {
1131 dev_err(&oct->pci_dev->dev,
1132 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1133 irqret);
1134 pci_disable_msix(oct->pci_dev);
1135 kfree(oct->msix_entries);
1136 oct->msix_entries = NULL;
1137 return 1;
1138 }
1139
1140 for (i = 0; i < num_ioq_vectors; i++) {
1141 irqret = request_irq(msix_entries[i].vector,
1142 liquidio_msix_intr_handler, 0,
1143 "octeon", &oct->ioq_vector[i]);
1144 if (irqret) {
1145 dev_err(&oct->pci_dev->dev,
1146 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1147 irqret);
1148 /** Freeing the non-ioq irq vector here . */
1149 free_irq(msix_entries[num_ioq_vectors].vector,
1150 oct);
1151
1152 while (i) {
1153 i--;
1154 /** clearing affinity mask. */
1155 irq_set_affinity_hint(
1156 msix_entries[i].vector, NULL);
1157 free_irq(msix_entries[i].vector,
1158 &oct->ioq_vector[i]);
1159 }
1160 pci_disable_msix(oct->pci_dev);
1161 kfree(oct->msix_entries);
1162 oct->msix_entries = NULL;
1163 return 1;
1164 }
1165 oct->ioq_vector[i].vector = msix_entries[i].vector;
1166 /* assign the cpu mask for this msix interrupt vector */
1167 irq_set_affinity_hint(
1168 msix_entries[i].vector,
1169 (&oct->ioq_vector[i].affinity_mask));
1170 }
1171 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1172 oct->octeon_id);
1173 } else {
1174 err = pci_enable_msi(oct->pci_dev);
1175 if (err)
1176 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1177 err);
1178 else
1179 oct->flags |= LIO_FLAG_MSI_ENABLED;
1180
1181 irqret = request_irq(oct->pci_dev->irq,
1182 liquidio_legacy_intr_handler, IRQF_SHARED,
1183 "octeon", oct);
1184 if (irqret) {
1185 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1186 pci_disable_msi(oct->pci_dev);
1187 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1188 irqret);
1189 return 1;
1190 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001191 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001192 return 0;
1193}
1194
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001195static int liquidio_watchdog(void *param)
1196{
1197 u64 wdog;
1198 u16 mask_of_stuck_cores = 0;
1199 u16 mask_of_crashed_cores = 0;
1200 int core_num;
1201 u8 core_is_stuck[LIO_MAX_CORES];
1202 u8 core_crashed[LIO_MAX_CORES];
1203 struct octeon_device *oct = param;
1204
1205 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1206 memset(core_crashed, 0, sizeof(core_crashed));
1207
1208 while (!kthread_should_stop()) {
1209 mask_of_crashed_cores =
1210 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1211
1212 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1213 if (!core_is_stuck[core_num]) {
1214 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1215
1216 /* look at watchdog state field */
1217 wdog &= CIU3_WDOG_MASK;
1218 if (wdog) {
1219 /* this watchdog timer has expired */
1220 core_is_stuck[core_num] =
1221 LIO_MONITOR_WDOG_EXPIRE;
1222 mask_of_stuck_cores |= (1 << core_num);
1223 }
1224 }
1225
1226 if (!core_crashed[core_num])
1227 core_crashed[core_num] =
1228 (mask_of_crashed_cores >> core_num) & 1;
1229 }
1230
1231 if (mask_of_stuck_cores) {
1232 for (core_num = 0; core_num < LIO_MAX_CORES;
1233 core_num++) {
1234 if (core_is_stuck[core_num] == 1) {
1235 dev_err(&oct->pci_dev->dev,
1236 "ERROR: Octeon core %d is stuck!\n",
1237 core_num);
1238 /* 2 means we have printk'd an error
1239 * so no need to repeat the same printk
1240 */
1241 core_is_stuck[core_num] =
1242 LIO_MONITOR_CORE_STUCK_MSGD;
1243 }
1244 }
1245 }
1246
1247 if (mask_of_crashed_cores) {
1248 for (core_num = 0; core_num < LIO_MAX_CORES;
1249 core_num++) {
1250 if (core_crashed[core_num] == 1) {
1251 dev_err(&oct->pci_dev->dev,
1252 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1253 core_num);
1254 /* 2 means we have printk'd an error
1255 * so no need to repeat the same printk
1256 */
1257 core_crashed[core_num] =
1258 LIO_MONITOR_CORE_STUCK_MSGD;
1259 }
1260 }
1261 }
1262#ifdef CONFIG_MODULE_UNLOAD
1263 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1264 /* make module refcount=0 so that rmmod will work */
1265 long refcount;
1266
1267 refcount = module_refcount(THIS_MODULE);
1268
1269 while (refcount > 0) {
1270 module_put(THIS_MODULE);
1271 refcount = module_refcount(THIS_MODULE);
1272 }
1273
1274 /* compensate for and withstand an unlikely (but still
1275 * possible) race condition
1276 */
1277 while (refcount < 0) {
1278 try_module_get(THIS_MODULE);
1279 refcount = module_refcount(THIS_MODULE);
1280 }
1281 }
1282#endif
1283 /* sleep for two seconds */
1284 set_current_state(TASK_INTERRUPTIBLE);
1285 schedule_timeout(2 * HZ);
1286 }
1287
1288 return 0;
1289}
1290
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001291/**
1292 * \brief PCI probe handler
1293 * @param pdev PCI device structure
1294 * @param ent unused
1295 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001296static int
1297liquidio_probe(struct pci_dev *pdev,
1298 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001299{
1300 struct octeon_device *oct_dev = NULL;
1301 struct handshake *hs;
1302
1303 oct_dev = octeon_allocate_device(pdev->device,
1304 sizeof(struct octeon_device_priv));
1305 if (!oct_dev) {
1306 dev_err(&pdev->dev, "Unable to allocate device\n");
1307 return -ENOMEM;
1308 }
1309
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001310 if (pdev->device == OCTEON_CN23XX_PF_VID)
1311 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1312
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001313 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1314 (u32)pdev->vendor, (u32)pdev->device);
1315
1316 /* Assign octeon_device for this device to the private data area. */
1317 pci_set_drvdata(pdev, oct_dev);
1318
1319 /* set linux specific device pointer */
1320 oct_dev->pci_dev = (void *)pdev;
1321
1322 hs = &handshake[oct_dev->octeon_id];
1323 init_completion(&hs->init);
1324 init_completion(&hs->started);
1325 hs->pci_dev = pdev;
1326
1327 if (oct_dev->octeon_id == 0)
1328 /* first LiquidIO NIC is detected */
1329 complete(&first_stage);
1330
1331 if (octeon_device_init(oct_dev)) {
1332 liquidio_remove(pdev);
1333 return -ENOMEM;
1334 }
1335
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001336 if (OCTEON_CN23XX_PF(oct_dev)) {
1337 u64 scratch1;
1338 u8 bus, device, function;
1339
1340 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1341 if (!(scratch1 & 4ULL)) {
1342 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1343 * the lio watchdog kernel thread is running for this
1344 * NIC. Each NIC gets one watchdog kernel thread.
1345 */
1346 scratch1 |= 4ULL;
1347 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1348 scratch1);
1349
1350 bus = pdev->bus->number;
1351 device = PCI_SLOT(pdev->devfn);
1352 function = PCI_FUNC(pdev->devfn);
1353 oct_dev->watchdog_task = kthread_create(
1354 liquidio_watchdog, oct_dev,
1355 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
1356 wake_up_process(oct_dev->watchdog_task);
1357 }
1358 }
1359
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001360 oct_dev->rx_pause = 1;
1361 oct_dev->tx_pause = 1;
1362
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001363 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1364
1365 return 0;
1366}
1367
1368/**
1369 *\brief Destroy resources associated with octeon device
1370 * @param pdev PCI device structure
1371 * @param ent unused
1372 */
1373static void octeon_destroy_resources(struct octeon_device *oct)
1374{
1375 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001376 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001377 struct octeon_device_priv *oct_priv =
1378 (struct octeon_device_priv *)oct->priv;
1379
1380 struct handshake *hs;
1381
1382 switch (atomic_read(&oct->status)) {
1383 case OCT_DEV_RUNNING:
1384 case OCT_DEV_CORE_OK:
1385
1386 /* No more instructions will be forwarded. */
1387 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1388
1389 oct->app_mode = CVM_DRV_INVALID_APP;
1390 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1391 lio_get_state_string(&oct->status));
1392
1393 schedule_timeout_uninterruptible(HZ / 10);
1394
1395 /* fallthrough */
1396 case OCT_DEV_HOST_OK:
1397
1398 /* fallthrough */
1399 case OCT_DEV_CONSOLE_INIT_DONE:
1400 /* Remove any consoles */
1401 octeon_remove_consoles(oct);
1402
1403 /* fallthrough */
1404 case OCT_DEV_IO_QUEUES_DONE:
1405 if (wait_for_pending_requests(oct))
1406 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1407
1408 if (lio_wait_for_instr_fetch(oct))
1409 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1410
1411 /* Disable the input and output queues now. No more packets will
1412 * arrive from Octeon, but we should wait for all packet
1413 * processing to finish.
1414 */
1415 oct->fn_list.disable_io_queues(oct);
1416
1417 if (lio_wait_for_oq_pkts(oct))
1418 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1419
1420 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001421 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001422
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001423 if (oct->msix_on) {
1424 msix_entries = (struct msix_entry *)oct->msix_entries;
1425 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1426 /* clear the affinity_cpumask */
1427 irq_set_affinity_hint(msix_entries[i].vector,
1428 NULL);
1429 free_irq(msix_entries[i].vector,
1430 &oct->ioq_vector[i]);
1431 }
1432 /* non-iov vector's argument is oct struct */
1433 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001434
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001435 pci_disable_msix(oct->pci_dev);
1436 kfree(oct->msix_entries);
1437 oct->msix_entries = NULL;
1438 } else {
1439 /* Release the interrupt line */
1440 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001441
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001442 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1443 pci_disable_msi(oct->pci_dev);
1444 }
1445
1446 if (OCTEON_CN23XX_PF(oct))
1447 octeon_free_ioq_vector(oct);
1448 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001449 case OCT_DEV_IN_RESET:
1450 case OCT_DEV_DROQ_INIT_DONE:
1451 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1452 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001453 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001454 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001455 continue;
1456 octeon_delete_droq(oct, i);
1457 }
1458
1459 /* Force any pending handshakes to complete */
1460 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1461 hs = &handshake[i];
1462
1463 if (hs->pci_dev) {
1464 handshake[oct->octeon_id].init_ok = 0;
1465 complete(&handshake[oct->octeon_id].init);
1466 handshake[oct->octeon_id].started_ok = 0;
1467 complete(&handshake[oct->octeon_id].started);
1468 }
1469 }
1470
1471 /* fallthrough */
1472 case OCT_DEV_RESP_LIST_INIT_DONE:
1473 octeon_delete_response_list(oct);
1474
1475 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001476 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001477 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001478 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001479 continue;
1480 octeon_delete_instr_queue(oct, i);
1481 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001482#ifdef CONFIG_PCI_IOV
1483 if (oct->sriov_info.sriov_enabled)
1484 pci_disable_sriov(oct->pci_dev);
1485#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001486 /* fallthrough */
1487 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1488 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001489
1490 /* fallthrough */
1491 case OCT_DEV_DISPATCH_INIT_DONE:
1492 octeon_delete_dispatch_list(oct);
1493 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1494
1495 /* fallthrough */
1496 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001497 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001498 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1499 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001500
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001501 octeon_unmap_pci_barx(oct, 0);
1502 octeon_unmap_pci_barx(oct, 1);
1503
1504 /* fallthrough */
1505 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001506 /* Disable the device, releasing the PCI INT */
1507 pci_disable_device(oct->pci_dev);
1508
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001509 /* Nothing to be done here either */
1510 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001511 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001512
1513 tasklet_kill(&oct_priv->droq_tasklet);
1514}
1515
1516/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001517 * \brief Callback for rx ctrl
1518 * @param status status of request
1519 * @param buf pointer to resp structure
1520 */
1521static void rx_ctl_callback(struct octeon_device *oct,
1522 u32 status,
1523 void *buf)
1524{
1525 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1526 struct liquidio_rx_ctl_context *ctx;
1527
1528 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1529
1530 oct = lio_get_device(ctx->octeon_id);
1531 if (status)
1532 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1533 CVM_CAST64(status));
1534 WRITE_ONCE(ctx->cond, 1);
1535
1536 /* This barrier is required to be sure that the response has been
1537 * written fully before waking up the handler
1538 */
1539 wmb();
1540
1541 wake_up_interruptible(&ctx->wc);
1542}
1543
1544/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001545 * \brief Send Rx control command
1546 * @param lio per-network private data
1547 * @param start_stop whether to start or stop
1548 */
1549static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1550{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001551 struct octeon_soft_command *sc;
1552 struct liquidio_rx_ctl_context *ctx;
1553 union octnet_cmd *ncmd;
1554 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1555 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1556 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001557
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001558 if (oct->props[lio->ifidx].rx_on == start_stop)
1559 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001560
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001561 sc = (struct octeon_soft_command *)
1562 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1563 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001564
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001565 ncmd = (union octnet_cmd *)sc->virtdptr;
1566 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1567
1568 WRITE_ONCE(ctx->cond, 0);
1569 ctx->octeon_id = lio_get_device_id(oct);
1570 init_waitqueue_head(&ctx->wc);
1571
1572 ncmd->u64 = 0;
1573 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1574 ncmd->s.param1 = start_stop;
1575
1576 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1577
1578 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1579
1580 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1581 OPCODE_NIC_CMD, 0, 0, 0);
1582
1583 sc->callback = rx_ctl_callback;
1584 sc->callback_arg = sc;
1585 sc->wait_time = 5000;
1586
1587 retval = octeon_send_soft_command(oct, sc);
1588 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001589 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001590 } else {
1591 /* Sleep on a wait queue till the cond flag indicates that the
1592 * response arrived or timed-out.
1593 */
1594 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1595 return;
1596 oct->props[lio->ifidx].rx_on = start_stop;
1597 }
1598
1599 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001600}
1601
1602/**
1603 * \brief Destroy NIC device interface
1604 * @param oct octeon device
1605 * @param ifidx which interface to destroy
1606 *
1607 * Cleanup associated with each interface for an Octeon device when NIC
1608 * module is being unloaded or if initialization fails during load.
1609 */
1610static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1611{
1612 struct net_device *netdev = oct->props[ifidx].netdev;
1613 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001614 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001615
1616 if (!netdev) {
1617 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1618 __func__, ifidx);
1619 return;
1620 }
1621
1622 lio = GET_LIO(netdev);
1623
1624 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1625
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001626 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001627 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001628
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001629 if (oct->props[lio->ifidx].napi_enabled == 1) {
1630 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1631 napi_disable(napi);
1632
1633 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001634
1635 if (OCTEON_CN23XX_PF(oct))
1636 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001637 }
1638
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001639 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1640 unregister_netdev(netdev);
1641
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001642 cleanup_link_status_change_wq(netdev);
1643
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001644 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001645
1646 free_netdev(netdev);
1647
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001648 oct->props[ifidx].gmxport = -1;
1649
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001650 oct->props[ifidx].netdev = NULL;
1651}
1652
1653/**
1654 * \brief Stop complete NIC functionality
1655 * @param oct octeon device
1656 */
1657static int liquidio_stop_nic_module(struct octeon_device *oct)
1658{
1659 int i, j;
1660 struct lio *lio;
1661
1662 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1663 if (!oct->ifcount) {
1664 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1665 return 1;
1666 }
1667
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001668 spin_lock_bh(&oct->cmd_resp_wqlock);
1669 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1670 spin_unlock_bh(&oct->cmd_resp_wqlock);
1671
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001672 for (i = 0; i < oct->ifcount; i++) {
1673 lio = GET_LIO(oct->props[i].netdev);
1674 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001675 octeon_unregister_droq_ops(oct,
1676 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001677 }
1678
1679 for (i = 0; i < oct->ifcount; i++)
1680 liquidio_destroy_nic_device(oct, i);
1681
1682 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1683 return 0;
1684}
1685
1686/**
1687 * \brief Cleans up resources at unload time
1688 * @param pdev PCI device structure
1689 */
1690static void liquidio_remove(struct pci_dev *pdev)
1691{
1692 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1693
1694 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1695
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001696 if (oct_dev->watchdog_task)
1697 kthread_stop(oct_dev->watchdog_task);
1698
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001699 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1700 liquidio_stop_nic_module(oct_dev);
1701
1702 /* Reset the octeon device and cleanup all memory allocated for
1703 * the octeon device by driver.
1704 */
1705 octeon_destroy_resources(oct_dev);
1706
1707 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1708
1709 /* This octeon device has been removed. Update the global
1710 * data structure to reflect this. Free the device structure.
1711 */
1712 octeon_free_device_mem(oct_dev);
1713}
1714
1715/**
1716 * \brief Identify the Octeon device and to map the BAR address space
1717 * @param oct octeon device
1718 */
1719static int octeon_chip_specific_setup(struct octeon_device *oct)
1720{
1721 u32 dev_id, rev_id;
1722 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001723 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001724
1725 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1726 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1727 oct->rev_id = rev_id & 0xff;
1728
1729 switch (dev_id) {
1730 case OCTEON_CN68XX_PCIID:
1731 oct->chip_id = OCTEON_CN68XX;
1732 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001733 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001734 break;
1735
1736 case OCTEON_CN66XX_PCIID:
1737 oct->chip_id = OCTEON_CN66XX;
1738 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001739 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001740 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001741
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001742 case OCTEON_CN23XX_PCIID_PF:
1743 oct->chip_id = OCTEON_CN23XX_PF_VID;
1744 ret = setup_cn23xx_octeon_pf_device(oct);
1745 s = "CN23XX";
1746 break;
1747
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001748 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001749 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001750 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1751 dev_id);
1752 }
1753
1754 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001755 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001756 OCTEON_MAJOR_REV(oct),
1757 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001758 octeon_get_conf(oct)->card_name,
1759 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001760
1761 return ret;
1762}
1763
1764/**
1765 * \brief PCI initialization for each Octeon device.
1766 * @param oct octeon device
1767 */
1768static int octeon_pci_os_setup(struct octeon_device *oct)
1769{
1770 /* setup PCI stuff first */
1771 if (pci_enable_device(oct->pci_dev)) {
1772 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1773 return 1;
1774 }
1775
1776 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1777 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1778 return 1;
1779 }
1780
1781 /* Enable PCI DMA Master. */
1782 pci_set_master(oct->pci_dev);
1783
1784 return 0;
1785}
1786
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001787static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1788{
1789 int q = 0;
1790
1791 if (netif_is_multiqueue(lio->netdev))
1792 q = skb->queue_mapping % lio->linfo.num_txpciq;
1793
1794 return q;
1795}
1796
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001797/**
1798 * \brief Check Tx queue state for a given network buffer
1799 * @param lio per-network private data
1800 * @param skb network buffer
1801 */
1802static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1803{
1804 int q = 0, iq = 0;
1805
1806 if (netif_is_multiqueue(lio->netdev)) {
1807 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001808 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001809 } else {
1810 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001811 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001812 }
1813
1814 if (octnet_iq_is_full(lio->oct_dev, iq))
1815 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001816
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001817 if (__netif_subqueue_stopped(lio->netdev, q)) {
1818 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001819 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001820 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001821 return 1;
1822}
1823
1824/**
1825 * \brief Unmap and free network buffer
1826 * @param buf buffer
1827 */
1828static void free_netbuf(void *buf)
1829{
1830 struct sk_buff *skb;
1831 struct octnet_buf_free_info *finfo;
1832 struct lio *lio;
1833
1834 finfo = (struct octnet_buf_free_info *)buf;
1835 skb = finfo->skb;
1836 lio = finfo->lio;
1837
1838 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1839 DMA_TO_DEVICE);
1840
1841 check_txq_state(lio, skb);
1842
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001843 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001844}
1845
1846/**
1847 * \brief Unmap and free gather buffer
1848 * @param buf buffer
1849 */
1850static void free_netsgbuf(void *buf)
1851{
1852 struct octnet_buf_free_info *finfo;
1853 struct sk_buff *skb;
1854 struct lio *lio;
1855 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001856 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001857
1858 finfo = (struct octnet_buf_free_info *)buf;
1859 skb = finfo->skb;
1860 lio = finfo->lio;
1861 g = finfo->g;
1862 frags = skb_shinfo(skb)->nr_frags;
1863
1864 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1865 g->sg[0].ptr[0], (skb->len - skb->data_len),
1866 DMA_TO_DEVICE);
1867
1868 i = 1;
1869 while (frags--) {
1870 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1871
1872 pci_unmap_page((lio->oct_dev)->pci_dev,
1873 g->sg[(i >> 2)].ptr[(i & 3)],
1874 frag->size, DMA_TO_DEVICE);
1875 i++;
1876 }
1877
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001878 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1879 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001880
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001881 iq = skb_iq(lio, skb);
1882 spin_lock(&lio->glist_lock[iq]);
1883 list_add_tail(&g->list, &lio->glist[iq]);
1884 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001885
1886 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1887
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001888 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001889}
1890
1891/**
1892 * \brief Unmap and free gather buffer with response
1893 * @param buf buffer
1894 */
1895static void free_netsgbuf_with_resp(void *buf)
1896{
1897 struct octeon_soft_command *sc;
1898 struct octnet_buf_free_info *finfo;
1899 struct sk_buff *skb;
1900 struct lio *lio;
1901 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001902 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001903
1904 sc = (struct octeon_soft_command *)buf;
1905 skb = (struct sk_buff *)sc->callback_arg;
1906 finfo = (struct octnet_buf_free_info *)&skb->cb;
1907
1908 lio = finfo->lio;
1909 g = finfo->g;
1910 frags = skb_shinfo(skb)->nr_frags;
1911
1912 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1913 g->sg[0].ptr[0], (skb->len - skb->data_len),
1914 DMA_TO_DEVICE);
1915
1916 i = 1;
1917 while (frags--) {
1918 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1919
1920 pci_unmap_page((lio->oct_dev)->pci_dev,
1921 g->sg[(i >> 2)].ptr[(i & 3)],
1922 frag->size, DMA_TO_DEVICE);
1923 i++;
1924 }
1925
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001926 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1927 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001928
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001929 iq = skb_iq(lio, skb);
1930
1931 spin_lock(&lio->glist_lock[iq]);
1932 list_add_tail(&g->list, &lio->glist[iq]);
1933 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001934
1935 /* Don't free the skb yet */
1936
1937 check_txq_state(lio, skb);
1938}
1939
1940/**
1941 * \brief Adjust ptp frequency
1942 * @param ptp PTP clock info
1943 * @param ppb how much to adjust by, in parts-per-billion
1944 */
1945static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1946{
1947 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1948 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1949 u64 comp, delta;
1950 unsigned long flags;
1951 bool neg_adj = false;
1952
1953 if (ppb < 0) {
1954 neg_adj = true;
1955 ppb = -ppb;
1956 }
1957
1958 /* The hardware adds the clock compensation value to the
1959 * PTP clock on every coprocessor clock cycle, so we
1960 * compute the delta in terms of coprocessor clocks.
1961 */
1962 delta = (u64)ppb << 32;
1963 do_div(delta, oct->coproc_clock_rate);
1964
1965 spin_lock_irqsave(&lio->ptp_lock, flags);
1966 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1967 if (neg_adj)
1968 comp -= delta;
1969 else
1970 comp += delta;
1971 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1972 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1973
1974 return 0;
1975}
1976
1977/**
1978 * \brief Adjust ptp time
1979 * @param ptp PTP clock info
1980 * @param delta how much to adjust by, in nanosecs
1981 */
1982static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1983{
1984 unsigned long flags;
1985 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1986
1987 spin_lock_irqsave(&lio->ptp_lock, flags);
1988 lio->ptp_adjust += delta;
1989 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1990
1991 return 0;
1992}
1993
1994/**
1995 * \brief Get hardware clock time, including any adjustment
1996 * @param ptp PTP clock info
1997 * @param ts timespec
1998 */
1999static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2000 struct timespec64 *ts)
2001{
2002 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002003 unsigned long flags;
2004 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2005 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2006
2007 spin_lock_irqsave(&lio->ptp_lock, flags);
2008 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2009 ns += lio->ptp_adjust;
2010 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2011
Kefeng Wang286af312016-01-27 17:34:37 +08002012 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002013
2014 return 0;
2015}
2016
2017/**
2018 * \brief Set hardware clock time. Reset adjustment
2019 * @param ptp PTP clock info
2020 * @param ts timespec
2021 */
2022static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2023 const struct timespec64 *ts)
2024{
2025 u64 ns;
2026 unsigned long flags;
2027 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2028 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2029
2030 ns = timespec_to_ns(ts);
2031
2032 spin_lock_irqsave(&lio->ptp_lock, flags);
2033 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2034 lio->ptp_adjust = 0;
2035 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2036
2037 return 0;
2038}
2039
2040/**
2041 * \brief Check if PTP is enabled
2042 * @param ptp PTP clock info
2043 * @param rq request
2044 * @param on is it on
2045 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002046static int
2047liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2048 struct ptp_clock_request *rq __attribute__((unused)),
2049 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002050{
2051 return -EOPNOTSUPP;
2052}
2053
2054/**
2055 * \brief Open PTP clock source
2056 * @param netdev network device
2057 */
2058static void oct_ptp_open(struct net_device *netdev)
2059{
2060 struct lio *lio = GET_LIO(netdev);
2061 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2062
2063 spin_lock_init(&lio->ptp_lock);
2064
2065 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2066 lio->ptp_info.owner = THIS_MODULE;
2067 lio->ptp_info.max_adj = 250000000;
2068 lio->ptp_info.n_alarm = 0;
2069 lio->ptp_info.n_ext_ts = 0;
2070 lio->ptp_info.n_per_out = 0;
2071 lio->ptp_info.pps = 0;
2072 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2073 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2074 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2075 lio->ptp_info.settime64 = liquidio_ptp_settime;
2076 lio->ptp_info.enable = liquidio_ptp_enable;
2077
2078 lio->ptp_adjust = 0;
2079
2080 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2081 &oct->pci_dev->dev);
2082
2083 if (IS_ERR(lio->ptp_clock))
2084 lio->ptp_clock = NULL;
2085}
2086
2087/**
2088 * \brief Init PTP clock
2089 * @param oct octeon device
2090 */
2091static void liquidio_ptp_init(struct octeon_device *oct)
2092{
2093 u64 clock_comp, cfg;
2094
2095 clock_comp = (u64)NSEC_PER_SEC << 32;
2096 do_div(clock_comp, oct->coproc_clock_rate);
2097 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2098
2099 /* Enable */
2100 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2101 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2102}
2103
2104/**
2105 * \brief Load firmware to device
2106 * @param oct octeon device
2107 *
2108 * Maps device to firmware filename, requests firmware, and downloads it
2109 */
2110static int load_firmware(struct octeon_device *oct)
2111{
2112 int ret = 0;
2113 const struct firmware *fw;
2114 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2115 char *tmp_fw_type;
2116
2117 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2118 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2119 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2120 return ret;
2121 }
2122
2123 if (fw_type[0] == '\0')
2124 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2125 else
2126 tmp_fw_type = fw_type;
2127
2128 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2129 octeon_get_conf(oct)->card_name, tmp_fw_type,
2130 LIO_FW_NAME_SUFFIX);
2131
2132 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2133 if (ret) {
2134 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2135 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002136 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002137 return ret;
2138 }
2139
2140 ret = octeon_download_firmware(oct, fw->data, fw->size);
2141
2142 release_firmware(fw);
2143
2144 return ret;
2145}
2146
2147/**
2148 * \brief Setup output queue
2149 * @param oct octeon device
2150 * @param q_no which queue
2151 * @param num_descs how many descriptors
2152 * @param desc_size size of each descriptor
2153 * @param app_ctx application context
2154 */
2155static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2156 int desc_size, void *app_ctx)
2157{
2158 int ret_val = 0;
2159
2160 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2161 /* droq creation and local register settings. */
2162 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302163 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002164 return ret_val;
2165
2166 if (ret_val == 1) {
2167 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2168 return 0;
2169 }
2170 /* tasklet creation for the droq */
2171
2172 /* Enable the droq queues */
2173 octeon_set_droq_pkt_op(oct, q_no, 1);
2174
2175 /* Send Credit for Octeon Output queues. Credits are always
2176 * sent after the output queue is enabled.
2177 */
2178 writel(oct->droq[q_no]->max_count,
2179 oct->droq[q_no]->pkts_credit_reg);
2180
2181 return ret_val;
2182}
2183
2184/**
2185 * \brief Callback for getting interface configuration
2186 * @param status status of request
2187 * @param buf pointer to resp structure
2188 */
2189static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002190 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002191 void *buf)
2192{
2193 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2194 struct liquidio_if_cfg_resp *resp;
2195 struct liquidio_if_cfg_context *ctx;
2196
2197 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002198 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002199
2200 oct = lio_get_device(ctx->octeon_id);
2201 if (resp->status)
2202 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
2203 CVM_CAST64(resp->status));
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002204 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002205
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002206 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2207 resp->cfg_info.liquidio_firmware_version);
2208
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002209 /* This barrier is required to be sure that the response has been
2210 * written fully before waking up the handler
2211 */
2212 wmb();
2213
2214 wake_up_interruptible(&ctx->wc);
2215}
2216
2217/**
2218 * \brief Select queue based on hash
2219 * @param dev Net device
2220 * @param skb sk_buff structure
2221 * @returns selected queue number
2222 */
2223static u16 select_q(struct net_device *dev, struct sk_buff *skb,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002224 void *accel_priv __attribute__((unused)),
2225 select_queue_fallback_t fallback __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002226{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002227 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002228 struct lio *lio;
2229
2230 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002231 qindex = skb_tx_hash(dev, skb);
2232
2233 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002234}
2235
2236/** Routine to push packets arriving on Octeon interface upto network layer.
2237 * @param oct_id - octeon device id.
2238 * @param skbuff - skbuff struct to be passed to network layer.
2239 * @param len - size of total data received.
2240 * @param rh - Control header associated with the packet
2241 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002242 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002243 */
2244static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002245liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002246 void *skbuff,
2247 u32 len,
2248 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002249 void *param,
2250 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002251{
2252 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002253 struct sk_buff *skb = (struct sk_buff *)skbuff;
2254 struct skb_shared_hwtstamps *shhwtstamps;
2255 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002256 u16 vtag = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002257 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002258 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2259 napi);
2260 if (netdev) {
2261 int packet_was_received;
2262 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002263 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002264
2265 /* Do not proceed if the interface is not in RUNNING state. */
2266 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2267 recv_buffer_free(skb);
2268 droq->stats.rx_dropped++;
2269 return;
2270 }
2271
2272 skb->dev = netdev;
2273
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002274 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002275 if (likely(len > MIN_SKB_SIZE)) {
2276 struct octeon_skb_page_info *pg_info;
2277 unsigned char *va;
2278
2279 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2280 if (pg_info->page) {
2281 /* For Paged allocation use the frags */
2282 va = page_address(pg_info->page) +
2283 pg_info->page_offset;
2284 memcpy(skb->data, va, MIN_SKB_SIZE);
2285 skb_put(skb, MIN_SKB_SIZE);
2286 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2287 pg_info->page,
2288 pg_info->page_offset +
2289 MIN_SKB_SIZE,
2290 len - MIN_SKB_SIZE,
2291 LIO_RXBUFFER_SZ);
2292 }
2293 } else {
2294 struct octeon_skb_page_info *pg_info =
2295 ((struct octeon_skb_page_info *)(skb->cb));
2296 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2297 + pg_info->page_offset, len);
2298 skb_put(skb, len);
2299 put_page(pg_info->page);
2300 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002301
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002302 if (((oct->chip_id == OCTEON_CN66XX) ||
2303 (oct->chip_id == OCTEON_CN68XX)) &&
2304 ptp_enable) {
2305 if (rh->r_dh.has_hwtstamp) {
2306 /* timestamp is included from the hardware at
2307 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002308 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002309 if (ifstate_check
2310 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2311 /* Nanoseconds are in the first 64-bits
2312 * of the packet.
2313 */
2314 memcpy(&ns, (skb->data), sizeof(ns));
2315 shhwtstamps = skb_hwtstamps(skb);
2316 shhwtstamps->hwtstamp =
2317 ns_to_ktime(ns +
2318 lio->ptp_adjust);
2319 }
2320 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002321 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002322 }
2323
2324 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002325 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002326 (((rh->r_dh.encap_on) &&
2327 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2328 (!(rh->r_dh.encap_on) &&
2329 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002330 /* checksum has already been verified */
2331 skb->ip_summed = CHECKSUM_UNNECESSARY;
2332 else
2333 skb->ip_summed = CHECKSUM_NONE;
2334
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002335 /* Setting Encapsulation field on basis of status received
2336 * from the firmware
2337 */
2338 if (rh->r_dh.encap_on) {
2339 skb->encapsulation = 1;
2340 skb->csum_level = 1;
2341 droq->stats.rx_vxlan++;
2342 }
2343
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002344 /* inbound VLAN tag */
2345 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2346 (rh->r_dh.vlan != 0)) {
2347 u16 vid = rh->r_dh.vlan;
2348 u16 priority = rh->r_dh.priority;
2349
2350 vtag = priority << 13 | vid;
2351 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2352 }
2353
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002354 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2355
2356 if (packet_was_received) {
2357 droq->stats.rx_bytes_received += len;
2358 droq->stats.rx_pkts_received++;
2359 netdev->last_rx = jiffies;
2360 } else {
2361 droq->stats.rx_dropped++;
2362 netif_info(lio, rx_err, lio->netdev,
2363 "droq:%d error rx_dropped:%llu\n",
2364 droq->q_no, droq->stats.rx_dropped);
2365 }
2366
2367 } else {
2368 recv_buffer_free(skb);
2369 }
2370}
2371
2372/**
2373 * \brief wrapper for calling napi_schedule
2374 * @param param parameters to pass to napi_schedule
2375 *
2376 * Used when scheduling on different CPUs
2377 */
2378static void napi_schedule_wrapper(void *param)
2379{
2380 struct napi_struct *napi = param;
2381
2382 napi_schedule(napi);
2383}
2384
2385/**
2386 * \brief callback when receive interrupt occurs and we are in NAPI mode
2387 * @param arg pointer to octeon output queue
2388 */
2389static void liquidio_napi_drv_callback(void *arg)
2390{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002391 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002392 struct octeon_droq *droq = arg;
2393 int this_cpu = smp_processor_id();
2394
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002395 oct = droq->oct_dev;
2396
2397 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2398 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002399 } else {
2400 struct call_single_data *csd = &droq->csd;
2401
2402 csd->func = napi_schedule_wrapper;
2403 csd->info = &droq->napi;
2404 csd->flags = 0;
2405
2406 smp_call_function_single_async(droq->cpu_id, csd);
2407 }
2408}
2409
2410/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002411 * \brief Entry point for NAPI polling
2412 * @param napi NAPI structure
2413 * @param budget maximum number of items to process
2414 */
2415static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2416{
2417 struct octeon_droq *droq;
2418 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002419 int tx_done = 0, iq_no;
2420 struct octeon_instr_queue *iq;
2421 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002422
2423 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002424 oct = droq->oct_dev;
2425 iq_no = droq->q_no;
2426 /* Handle Droq descriptors */
2427 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2428 POLL_EVENT_PROCESS_PKTS,
2429 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002430
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002431 /* Flush the instruction queue */
2432 iq = oct->instr_queue[iq_no];
2433 if (iq) {
2434 /* Process iq buffers with in the budget limits */
2435 tx_done = octeon_flush_iq(oct, iq, 1, budget);
2436 /* Update iq read-index rather than waiting for next interrupt.
2437 * Return back if tx_done is false.
2438 */
2439 update_txq_status(oct, iq_no);
2440 /*tx_done = (iq->flush_index == iq->octeon_read_index);*/
2441 } else {
2442 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2443 __func__, iq_no);
2444 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002445
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002446 if ((work_done < budget) && (tx_done)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002447 napi_complete(napi);
2448 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2449 POLL_EVENT_ENABLE_INTR, 0);
2450 return 0;
2451 }
2452
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002453 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002454}
2455
2456/**
2457 * \brief Setup input and output queues
2458 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002459 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002460 *
2461 * Note: Queues are with respect to the octeon device. Thus
2462 * an input queue is for egress packets, and output queues
2463 * are for ingress packets.
2464 */
2465static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002466 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002467{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002468 struct octeon_droq_ops droq_ops;
2469 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002470 static int cpu_id;
2471 static int cpu_id_modulus;
2472 struct octeon_droq *droq;
2473 struct napi_struct *napi;
2474 int q, q_no, retval = 0;
2475 struct lio *lio;
2476 int num_tx_descs;
2477
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002478 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002479
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002480 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002481
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002482 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2483
2484 droq_ops.fptr = liquidio_push_packet;
2485 droq_ops.farg = (void *)netdev;
2486
2487 droq_ops.poll_mode = 1;
2488 droq_ops.napi_fn = liquidio_napi_drv_callback;
2489 cpu_id = 0;
2490 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002491
2492 /* set up DROQs. */
2493 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002494 q_no = lio->linfo.rxpciq[q].s.q_no;
2495 dev_dbg(&octeon_dev->pci_dev->dev,
2496 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2497 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002498 retval = octeon_setup_droq(octeon_dev, q_no,
2499 CFG_GET_NUM_RX_DESCS_NIC_IF
2500 (octeon_get_conf(octeon_dev),
2501 lio->ifidx),
2502 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2503 (octeon_get_conf(octeon_dev),
2504 lio->ifidx), NULL);
2505 if (retval) {
2506 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002507 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002508 __func__);
2509 return 1;
2510 }
2511
2512 droq = octeon_dev->droq[q_no];
2513 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002514 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2515 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002516 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002517
2518 /* designate a CPU for this droq */
2519 droq->cpu_id = cpu_id;
2520 cpu_id++;
2521 if (cpu_id >= cpu_id_modulus)
2522 cpu_id = 0;
2523
2524 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2525 }
2526
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002527 if (OCTEON_CN23XX_PF(octeon_dev)) {
2528 /* 23XX PF can receive control messages (via the first PF-owned
2529 * droq) from the firmware even if the ethX interface is down,
2530 * so that's why poll_mode must be off for the first droq.
2531 */
2532 octeon_dev->droq[0]->ops.poll_mode = 0;
2533 }
2534
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002535 /* set up IQs. */
2536 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2537 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2538 (octeon_dev),
2539 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002540 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2541 lio->linfo.txpciq[q], num_tx_descs,
2542 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002543 if (retval) {
2544 dev_err(&octeon_dev->pci_dev->dev,
2545 " %s : Runtime IQ(TxQ) creation failed.\n",
2546 __func__);
2547 return 1;
2548 }
2549 }
2550
2551 return 0;
2552}
2553
2554/**
2555 * \brief Poll routine for checking transmit queue status
2556 * @param work work_struct data structure
2557 */
2558static void octnet_poll_check_txq_status(struct work_struct *work)
2559{
2560 struct cavium_wk *wk = (struct cavium_wk *)work;
2561 struct lio *lio = (struct lio *)wk->ctxptr;
2562
2563 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2564 return;
2565
2566 check_txq_status(lio);
2567 queue_delayed_work(lio->txq_status_wq.wq,
2568 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2569}
2570
2571/**
2572 * \brief Sets up the txq poll check
2573 * @param netdev network device
2574 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002575static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002576{
2577 struct lio *lio = GET_LIO(netdev);
2578 struct octeon_device *oct = lio->oct_dev;
2579
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302580 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2581 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002582 if (!lio->txq_status_wq.wq) {
2583 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002584 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002585 }
2586 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2587 octnet_poll_check_txq_status);
2588 lio->txq_status_wq.wk.ctxptr = lio;
2589 queue_delayed_work(lio->txq_status_wq.wq,
2590 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002591 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002592}
2593
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002594static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2595{
2596 struct lio *lio = GET_LIO(netdev);
2597
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002598 if (lio->txq_status_wq.wq) {
2599 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2600 destroy_workqueue(lio->txq_status_wq.wq);
2601 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002602}
2603
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002604/**
2605 * \brief Net device open for LiquidIO
2606 * @param netdev network device
2607 */
2608static int liquidio_open(struct net_device *netdev)
2609{
2610 struct lio *lio = GET_LIO(netdev);
2611 struct octeon_device *oct = lio->oct_dev;
2612 struct napi_struct *napi, *n;
2613
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002614 if (oct->props[lio->ifidx].napi_enabled == 0) {
2615 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2616 napi_enable(napi);
2617
2618 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002619
2620 if (OCTEON_CN23XX_PF(oct))
2621 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002622 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002623
2624 oct_ptp_open(netdev);
2625
2626 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002627
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002628 /* Ready for link status updates */
2629 lio->intf_open = 1;
2630
2631 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2632
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002633 if (OCTEON_CN23XX_PF(oct)) {
2634 if (!oct->msix_on)
2635 if (setup_tx_poll_fn(netdev))
2636 return -1;
2637 } else {
2638 if (setup_tx_poll_fn(netdev))
2639 return -1;
2640 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002641
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002642 start_txq(netdev);
2643
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002644 /* tell Octeon to start forwarding packets to host */
2645 send_rx_ctrl_cmd(lio, 1);
2646
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002647 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2648 netdev->name);
2649
2650 return 0;
2651}
2652
2653/**
2654 * \brief Net device stop for LiquidIO
2655 * @param netdev network device
2656 */
2657static int liquidio_stop(struct net_device *netdev)
2658{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002659 struct lio *lio = GET_LIO(netdev);
2660 struct octeon_device *oct = lio->oct_dev;
2661
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002662 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2663
2664 netif_tx_disable(netdev);
2665
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002666 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002667 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002668 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002669 lio->linfo.link.s.link_up = 0;
2670 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002671
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002672 /* Pause for a moment and wait for Octeon to flush out (to the wire) any
2673 * egress packets that are in-flight.
2674 */
2675 set_current_state(TASK_INTERRUPTIBLE);
2676 schedule_timeout(msecs_to_jiffies(100));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002677
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002678 /* Now it should be safe to tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002679 send_rx_ctrl_cmd(lio, 0);
2680
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002681 if (OCTEON_CN23XX_PF(oct)) {
2682 if (!oct->msix_on)
2683 cleanup_tx_poll_fn(netdev);
2684 } else {
2685 cleanup_tx_poll_fn(netdev);
2686 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002687
2688 if (lio->ptp_clock) {
2689 ptp_clock_unregister(lio->ptp_clock);
2690 lio->ptp_clock = NULL;
2691 }
2692
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002693 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002694
2695 return 0;
2696}
2697
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002698/**
2699 * \brief Converts a mask based on net device flags
2700 * @param netdev network device
2701 *
2702 * This routine generates a octnet_ifflags mask from the net device flags
2703 * received from the OS.
2704 */
2705static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2706{
2707 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2708
2709 if (netdev->flags & IFF_PROMISC)
2710 f |= OCTNET_IFFLAG_PROMISC;
2711
2712 if (netdev->flags & IFF_ALLMULTI)
2713 f |= OCTNET_IFFLAG_ALLMULTI;
2714
2715 if (netdev->flags & IFF_MULTICAST) {
2716 f |= OCTNET_IFFLAG_MULTICAST;
2717
2718 /* Accept all multicast addresses if there are more than we
2719 * can handle
2720 */
2721 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2722 f |= OCTNET_IFFLAG_ALLMULTI;
2723 }
2724
2725 if (netdev->flags & IFF_BROADCAST)
2726 f |= OCTNET_IFFLAG_BROADCAST;
2727
2728 return f;
2729}
2730
2731/**
2732 * \brief Net device set_multicast_list
2733 * @param netdev network device
2734 */
2735static void liquidio_set_mcast_list(struct net_device *netdev)
2736{
2737 struct lio *lio = GET_LIO(netdev);
2738 struct octeon_device *oct = lio->oct_dev;
2739 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002740 struct netdev_hw_addr *ha;
2741 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002742 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002743 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2744
2745 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2746
2747 /* Create a ctrl pkt command to be sent to core app. */
2748 nctrl.ncmd.u64 = 0;
2749 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002750 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2751 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002752 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002753 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002754 nctrl.netpndev = (u64)netdev;
2755 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2756
2757 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002758 mc = &nctrl.udd[0];
2759 netdev_for_each_mc_addr(ha, netdev) {
2760 *mc = 0;
2761 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2762 /* no need to swap bytes */
2763
2764 if (++mc > &nctrl.udd[mc_count])
2765 break;
2766 }
2767
2768 /* Apparently, any activity in this call from the kernel has to
2769 * be atomic. So we won't wait for response.
2770 */
2771 nctrl.wait_time = 0;
2772
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002773 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002774 if (ret < 0) {
2775 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2776 ret);
2777 }
2778}
2779
2780/**
2781 * \brief Net device set_mac_address
2782 * @param netdev network device
2783 */
2784static int liquidio_set_mac(struct net_device *netdev, void *p)
2785{
2786 int ret = 0;
2787 struct lio *lio = GET_LIO(netdev);
2788 struct octeon_device *oct = lio->oct_dev;
2789 struct sockaddr *addr = (struct sockaddr *)p;
2790 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002791
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002792 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002793 return -EADDRNOTAVAIL;
2794
2795 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2796
2797 nctrl.ncmd.u64 = 0;
2798 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002799 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002800 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002801 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 nctrl.netpndev = (u64)netdev;
2803 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2804 nctrl.wait_time = 100;
2805
2806 nctrl.udd[0] = 0;
2807 /* The MAC Address is presented in network byte order. */
2808 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2809
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002810 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002811 if (ret < 0) {
2812 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2813 return -ENOMEM;
2814 }
2815 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2816 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2817
2818 return 0;
2819}
2820
2821/**
2822 * \brief Net device get_stats
2823 * @param netdev network device
2824 */
2825static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2826{
2827 struct lio *lio = GET_LIO(netdev);
2828 struct net_device_stats *stats = &netdev->stats;
2829 struct octeon_device *oct;
2830 u64 pkts = 0, drop = 0, bytes = 0;
2831 struct oct_droq_stats *oq_stats;
2832 struct oct_iq_stats *iq_stats;
2833 int i, iq_no, oq_no;
2834
2835 oct = lio->oct_dev;
2836
2837 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002838 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002839 iq_stats = &oct->instr_queue[iq_no]->stats;
2840 pkts += iq_stats->tx_done;
2841 drop += iq_stats->tx_dropped;
2842 bytes += iq_stats->tx_tot_bytes;
2843 }
2844
2845 stats->tx_packets = pkts;
2846 stats->tx_bytes = bytes;
2847 stats->tx_dropped = drop;
2848
2849 pkts = 0;
2850 drop = 0;
2851 bytes = 0;
2852
2853 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002854 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002855 oq_stats = &oct->droq[oq_no]->stats;
2856 pkts += oq_stats->rx_pkts_received;
2857 drop += (oq_stats->rx_dropped +
2858 oq_stats->dropped_nodispatch +
2859 oq_stats->dropped_toomany +
2860 oq_stats->dropped_nomem);
2861 bytes += oq_stats->rx_bytes_received;
2862 }
2863
2864 stats->rx_bytes = bytes;
2865 stats->rx_packets = pkts;
2866 stats->rx_dropped = drop;
2867
2868 return stats;
2869}
2870
2871/**
2872 * \brief Net device change_mtu
2873 * @param netdev network device
2874 */
2875static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2876{
2877 struct lio *lio = GET_LIO(netdev);
2878 struct octeon_device *oct = lio->oct_dev;
2879 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002880 int ret = 0;
2881
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002882 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2883
2884 nctrl.ncmd.u64 = 0;
2885 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002886 nctrl.ncmd.s.param1 = new_mtu;
2887 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002888 nctrl.wait_time = 100;
2889 nctrl.netpndev = (u64)netdev;
2890 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2891
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002892 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002893 if (ret < 0) {
2894 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2895 return -1;
2896 }
2897
2898 lio->mtu = new_mtu;
2899
2900 return 0;
2901}
2902
2903/**
2904 * \brief Handler for SIOCSHWTSTAMP ioctl
2905 * @param netdev network device
2906 * @param ifr interface request
2907 * @param cmd command
2908 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002909static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002910{
2911 struct hwtstamp_config conf;
2912 struct lio *lio = GET_LIO(netdev);
2913
2914 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2915 return -EFAULT;
2916
2917 if (conf.flags)
2918 return -EINVAL;
2919
2920 switch (conf.tx_type) {
2921 case HWTSTAMP_TX_ON:
2922 case HWTSTAMP_TX_OFF:
2923 break;
2924 default:
2925 return -ERANGE;
2926 }
2927
2928 switch (conf.rx_filter) {
2929 case HWTSTAMP_FILTER_NONE:
2930 break;
2931 case HWTSTAMP_FILTER_ALL:
2932 case HWTSTAMP_FILTER_SOME:
2933 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2934 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2935 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2936 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2937 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2938 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2939 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2940 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2941 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2942 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2943 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2944 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2945 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2946 break;
2947 default:
2948 return -ERANGE;
2949 }
2950
2951 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2952 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2953
2954 else
2955 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2956
2957 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2958}
2959
2960/**
2961 * \brief ioctl handler
2962 * @param netdev network device
2963 * @param ifr interface request
2964 * @param cmd command
2965 */
2966static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2967{
2968 switch (cmd) {
2969 case SIOCSHWTSTAMP:
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002970 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002971 default:
2972 return -EOPNOTSUPP;
2973 }
2974}
2975
2976/**
2977 * \brief handle a Tx timestamp response
2978 * @param status response status
2979 * @param buf pointer to skb
2980 */
2981static void handle_timestamp(struct octeon_device *oct,
2982 u32 status,
2983 void *buf)
2984{
2985 struct octnet_buf_free_info *finfo;
2986 struct octeon_soft_command *sc;
2987 struct oct_timestamp_resp *resp;
2988 struct lio *lio;
2989 struct sk_buff *skb = (struct sk_buff *)buf;
2990
2991 finfo = (struct octnet_buf_free_info *)skb->cb;
2992 lio = finfo->lio;
2993 sc = finfo->sc;
2994 oct = lio->oct_dev;
2995 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2996
2997 if (status != OCTEON_REQUEST_DONE) {
2998 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2999 CVM_CAST64(status));
3000 resp->timestamp = 0;
3001 }
3002
3003 octeon_swap_8B_data(&resp->timestamp, 1);
3004
Colin Ian King19a6d152016-02-05 16:30:39 +00003005 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003006 struct skb_shared_hwtstamps ts;
3007 u64 ns = resp->timestamp;
3008
3009 netif_info(lio, tx_done, lio->netdev,
3010 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3011 skb, (unsigned long long)ns);
3012 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3013 skb_tstamp_tx(skb, &ts);
3014 }
3015
3016 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003017 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003018}
3019
3020/* \brief Send a data packet that will be timestamped
3021 * @param oct octeon device
3022 * @param ndata pointer to network data
3023 * @param finfo pointer to private network data
3024 */
3025static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3026 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003027 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003028{
3029 int retval;
3030 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003031 struct lio *lio;
3032 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003033 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003034
3035 lio = finfo->lio;
3036
3037 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3038 sizeof(struct oct_timestamp_resp));
3039 finfo->sc = sc;
3040
3041 if (!sc) {
3042 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3043 return IQ_SEND_FAILED;
3044 }
3045
3046 if (ndata->reqtype == REQTYPE_NORESP_NET)
3047 ndata->reqtype = REQTYPE_RESP_NET;
3048 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3049 ndata->reqtype = REQTYPE_RESP_NET_SG;
3050
3051 sc->callback = handle_timestamp;
3052 sc->callback_arg = finfo->skb;
3053 sc->iq_no = ndata->q_no;
3054
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003055 if (OCTEON_CN23XX_PF(oct))
3056 len = (u32)((struct octeon_instr_ih3 *)
3057 (&sc->cmd.cmd3.ih3))->dlengsz;
3058 else
3059 len = (u32)((struct octeon_instr_ih2 *)
3060 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003061
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003062 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003063
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003064 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003065 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003066
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003067 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003068 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3069 retval);
3070 octeon_free_soft_command(oct, sc);
3071 } else {
3072 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3073 }
3074
3075 return retval;
3076}
3077
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003078/** \brief Transmit networks packets to the Octeon interface
3079 * @param skbuff skbuff struct to be passed to network layer.
3080 * @param netdev pointer to network device
3081 * @returns whether the packet was transmitted to the device okay or not
3082 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3083 */
3084static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3085{
3086 struct lio *lio;
3087 struct octnet_buf_free_info *finfo;
3088 union octnic_cmd_setup cmdsetup;
3089 struct octnic_data_pkt ndata;
3090 struct octeon_device *oct;
3091 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003092 struct octeon_instr_irh *irh;
3093 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003094 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003095 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003096 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003097 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003098 u32 tag = 0;
3099
3100 lio = GET_LIO(netdev);
3101 oct = lio->oct_dev;
3102
3103 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003104 q_idx = skb->queue_mapping;
3105 q_idx = (q_idx % (lio->linfo.num_txpciq));
3106 tag = q_idx;
3107 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003108 } else {
3109 iq_no = lio->txq;
3110 }
3111
3112 stats = &oct->instr_queue[iq_no]->stats;
3113
3114 /* Check for all conditions in which the current packet cannot be
3115 * transmitted.
3116 */
3117 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003118 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003119 (skb->len <= 0)) {
3120 netif_info(lio, tx_err, lio->netdev,
3121 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003122 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003123 goto lio_xmit_failed;
3124 }
3125
3126 /* Use space in skb->cb to store info used to unmap and
3127 * free the buffers.
3128 */
3129 finfo = (struct octnet_buf_free_info *)skb->cb;
3130 finfo->lio = lio;
3131 finfo->skb = skb;
3132 finfo->sc = NULL;
3133
3134 /* Prepare the attributes for the data to be passed to OSI. */
3135 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3136
3137 ndata.buf = (void *)finfo;
3138
3139 ndata.q_no = iq_no;
3140
3141 if (netif_is_multiqueue(netdev)) {
3142 if (octnet_iq_is_full(oct, ndata.q_no)) {
3143 /* defer sending if queue is full */
3144 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3145 ndata.q_no);
3146 stats->tx_iq_busy++;
3147 return NETDEV_TX_BUSY;
3148 }
3149 } else {
3150 if (octnet_iq_is_full(oct, lio->txq)) {
3151 /* defer sending if queue is full */
3152 stats->tx_iq_busy++;
3153 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003154 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003155 return NETDEV_TX_BUSY;
3156 }
3157 }
3158 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003159 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003160 */
3161
3162 ndata.datasize = skb->len;
3163
3164 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003165 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003166
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003167 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3168 if (skb->encapsulation) {
3169 cmdsetup.s.tnl_csum = 1;
3170 stats->tx_vxlan++;
3171 } else {
3172 cmdsetup.s.transport_csum = 1;
3173 }
3174 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003175 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3176 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3177 cmdsetup.s.timestamp = 1;
3178 }
3179
3180 if (skb_shinfo(skb)->nr_frags == 0) {
3181 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003182 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003183
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003184 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003185 dptr = dma_map_single(&oct->pci_dev->dev,
3186 skb->data,
3187 skb->len,
3188 DMA_TO_DEVICE);
3189 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003190 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3191 __func__);
3192 return NETDEV_TX_BUSY;
3193 }
3194
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003195 if (OCTEON_CN23XX_PF(oct))
3196 ndata.cmd.cmd3.dptr = dptr;
3197 else
3198 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003199 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003200 ndata.reqtype = REQTYPE_NORESP_NET;
3201
3202 } else {
3203 int i, frags;
3204 struct skb_frag_struct *frag;
3205 struct octnic_gather *g;
3206
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003207 spin_lock(&lio->glist_lock[q_idx]);
3208 g = (struct octnic_gather *)
3209 list_delete_head(&lio->glist[q_idx]);
3210 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003211
3212 if (!g) {
3213 netif_info(lio, tx_err, lio->netdev,
3214 "Transmit scatter gather: glist null!\n");
3215 goto lio_xmit_failed;
3216 }
3217
3218 cmdsetup.s.gather = 1;
3219 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003220 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003221
3222 memset(g->sg, 0, g->sg_size);
3223
3224 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3225 skb->data,
3226 (skb->len - skb->data_len),
3227 DMA_TO_DEVICE);
3228 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3229 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3230 __func__);
3231 return NETDEV_TX_BUSY;
3232 }
3233 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3234
3235 frags = skb_shinfo(skb)->nr_frags;
3236 i = 1;
3237 while (frags--) {
3238 frag = &skb_shinfo(skb)->frags[i - 1];
3239
3240 g->sg[(i >> 2)].ptr[(i & 3)] =
3241 dma_map_page(&oct->pci_dev->dev,
3242 frag->page.p,
3243 frag->page_offset,
3244 frag->size,
3245 DMA_TO_DEVICE);
3246
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003247 if (dma_mapping_error(&oct->pci_dev->dev,
3248 g->sg[i >> 2].ptr[i & 3])) {
3249 dma_unmap_single(&oct->pci_dev->dev,
3250 g->sg[0].ptr[0],
3251 skb->len - skb->data_len,
3252 DMA_TO_DEVICE);
3253 for (j = 1; j < i; j++) {
3254 frag = &skb_shinfo(skb)->frags[j - 1];
3255 dma_unmap_page(&oct->pci_dev->dev,
3256 g->sg[j >> 2].ptr[j & 3],
3257 frag->size,
3258 DMA_TO_DEVICE);
3259 }
3260 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3261 __func__);
3262 return NETDEV_TX_BUSY;
3263 }
3264
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003265 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3266 i++;
3267 }
3268
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003269 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
3270 g->sg_size, DMA_TO_DEVICE);
3271 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003272
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003273 if (OCTEON_CN23XX_PF(oct))
3274 ndata.cmd.cmd3.dptr = dptr;
3275 else
3276 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003277 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003278 finfo->g = g;
3279
3280 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3281 }
3282
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003283 if (OCTEON_CN23XX_PF(oct)) {
3284 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3285 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3286 } else {
3287 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3288 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3289 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003290
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003291 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003292 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3293 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003294 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003295 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003296
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003297 /* HW insert VLAN tag */
3298 if (skb_vlan_tag_present(skb)) {
3299 irh->priority = skb_vlan_tag_get(skb) >> 13;
3300 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3301 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003302
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003303 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003304 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003305 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003306 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003307 if (status == IQ_SEND_FAILED)
3308 goto lio_xmit_failed;
3309
3310 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3311
3312 if (status == IQ_SEND_STOP)
3313 stop_q(lio->netdev, q_idx);
3314
Florian Westphal860e9532016-05-03 16:33:13 +02003315 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003316
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003317 if (skb_shinfo(skb)->gso_size)
3318 stats->tx_done += skb_shinfo(skb)->gso_segs;
3319 else
3320 stats->tx_done++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003321 stats->tx_tot_bytes += skb->len;
3322
3323 return NETDEV_TX_OK;
3324
3325lio_xmit_failed:
3326 stats->tx_dropped++;
3327 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3328 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003329 if (dptr)
3330 dma_unmap_single(&oct->pci_dev->dev, dptr,
3331 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003332 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003333 return NETDEV_TX_OK;
3334}
3335
3336/** \brief Network device Tx timeout
3337 * @param netdev pointer to network device
3338 */
3339static void liquidio_tx_timeout(struct net_device *netdev)
3340{
3341 struct lio *lio;
3342
3343 lio = GET_LIO(netdev);
3344
3345 netif_info(lio, tx_err, lio->netdev,
3346 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3347 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003348 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003349 txqs_wake(netdev);
3350}
3351
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003352static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3353 __be16 proto __attribute__((unused)),
3354 u16 vid)
3355{
3356 struct lio *lio = GET_LIO(netdev);
3357 struct octeon_device *oct = lio->oct_dev;
3358 struct octnic_ctrl_pkt nctrl;
3359 int ret = 0;
3360
3361 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3362
3363 nctrl.ncmd.u64 = 0;
3364 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3365 nctrl.ncmd.s.param1 = vid;
3366 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3367 nctrl.wait_time = 100;
3368 nctrl.netpndev = (u64)netdev;
3369 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3370
3371 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3372 if (ret < 0) {
3373 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3374 ret);
3375 }
3376
3377 return ret;
3378}
3379
3380static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3381 __be16 proto __attribute__((unused)),
3382 u16 vid)
3383{
3384 struct lio *lio = GET_LIO(netdev);
3385 struct octeon_device *oct = lio->oct_dev;
3386 struct octnic_ctrl_pkt nctrl;
3387 int ret = 0;
3388
3389 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3390
3391 nctrl.ncmd.u64 = 0;
3392 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3393 nctrl.ncmd.s.param1 = vid;
3394 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3395 nctrl.wait_time = 100;
3396 nctrl.netpndev = (u64)netdev;
3397 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3398
3399 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3400 if (ret < 0) {
3401 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3402 ret);
3403 }
3404 return ret;
3405}
3406
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003407/** Sending command to enable/disable RX checksum offload
3408 * @param netdev pointer to network device
3409 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3410 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3411 * OCTNET_CMD_RXCSUM_DISABLE
3412 * @returns SUCCESS or FAILURE
3413 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003414static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3415 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003416{
3417 struct lio *lio = GET_LIO(netdev);
3418 struct octeon_device *oct = lio->oct_dev;
3419 struct octnic_ctrl_pkt nctrl;
3420 int ret = 0;
3421
3422 nctrl.ncmd.u64 = 0;
3423 nctrl.ncmd.s.cmd = command;
3424 nctrl.ncmd.s.param1 = rx_cmd;
3425 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3426 nctrl.wait_time = 100;
3427 nctrl.netpndev = (u64)netdev;
3428 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3429
3430 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3431 if (ret < 0) {
3432 dev_err(&oct->pci_dev->dev,
3433 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3434 ret);
3435 }
3436 return ret;
3437}
3438
3439/** Sending command to add/delete VxLAN UDP port to firmware
3440 * @param netdev pointer to network device
3441 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3442 * @param vxlan_port VxLAN port to be added or deleted
3443 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3444 * OCTNET_CMD_VXLAN_PORT_DEL
3445 * @returns SUCCESS or FAILURE
3446 */
3447static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3448 u16 vxlan_port, u8 vxlan_cmd_bit)
3449{
3450 struct lio *lio = GET_LIO(netdev);
3451 struct octeon_device *oct = lio->oct_dev;
3452 struct octnic_ctrl_pkt nctrl;
3453 int ret = 0;
3454
3455 nctrl.ncmd.u64 = 0;
3456 nctrl.ncmd.s.cmd = command;
3457 nctrl.ncmd.s.more = vxlan_cmd_bit;
3458 nctrl.ncmd.s.param1 = vxlan_port;
3459 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3460 nctrl.wait_time = 100;
3461 nctrl.netpndev = (u64)netdev;
3462 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3463
3464 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3465 if (ret < 0) {
3466 dev_err(&oct->pci_dev->dev,
3467 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3468 ret);
3469 }
3470 return ret;
3471}
3472
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003473/** \brief Net device fix features
3474 * @param netdev pointer to network device
3475 * @param request features requested
3476 * @returns updated features list
3477 */
3478static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3479 netdev_features_t request)
3480{
3481 struct lio *lio = netdev_priv(netdev);
3482
3483 if ((request & NETIF_F_RXCSUM) &&
3484 !(lio->dev_capability & NETIF_F_RXCSUM))
3485 request &= ~NETIF_F_RXCSUM;
3486
3487 if ((request & NETIF_F_HW_CSUM) &&
3488 !(lio->dev_capability & NETIF_F_HW_CSUM))
3489 request &= ~NETIF_F_HW_CSUM;
3490
3491 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3492 request &= ~NETIF_F_TSO;
3493
3494 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3495 request &= ~NETIF_F_TSO6;
3496
3497 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3498 request &= ~NETIF_F_LRO;
3499
3500 /*Disable LRO if RXCSUM is off */
3501 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3502 (lio->dev_capability & NETIF_F_LRO))
3503 request &= ~NETIF_F_LRO;
3504
3505 return request;
3506}
3507
3508/** \brief Net device set features
3509 * @param netdev pointer to network device
3510 * @param features features to enable/disable
3511 */
3512static int liquidio_set_features(struct net_device *netdev,
3513 netdev_features_t features)
3514{
3515 struct lio *lio = netdev_priv(netdev);
3516
3517 if (!((netdev->features ^ features) & NETIF_F_LRO))
3518 return 0;
3519
3520 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003521 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3522 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003523 else if (!(features & NETIF_F_LRO) &&
3524 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003525 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3526 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003527
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003528 /* Sending command to firmware to enable/disable RX checksum
3529 * offload settings using ethtool
3530 */
3531 if (!(netdev->features & NETIF_F_RXCSUM) &&
3532 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3533 (features & NETIF_F_RXCSUM))
3534 liquidio_set_rxcsum_command(netdev,
3535 OCTNET_CMD_TNL_RX_CSUM_CTL,
3536 OCTNET_CMD_RXCSUM_ENABLE);
3537 else if ((netdev->features & NETIF_F_RXCSUM) &&
3538 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3539 !(features & NETIF_F_RXCSUM))
3540 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3541 OCTNET_CMD_RXCSUM_DISABLE);
3542
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003543 return 0;
3544}
3545
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003546static void liquidio_add_vxlan_port(struct net_device *netdev,
3547 struct udp_tunnel_info *ti)
3548{
3549 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3550 return;
3551
3552 liquidio_vxlan_port_command(netdev,
3553 OCTNET_CMD_VXLAN_PORT_CONFIG,
3554 htons(ti->port),
3555 OCTNET_CMD_VXLAN_PORT_ADD);
3556}
3557
3558static void liquidio_del_vxlan_port(struct net_device *netdev,
3559 struct udp_tunnel_info *ti)
3560{
3561 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3562 return;
3563
3564 liquidio_vxlan_port_command(netdev,
3565 OCTNET_CMD_VXLAN_PORT_CONFIG,
3566 htons(ti->port),
3567 OCTNET_CMD_VXLAN_PORT_DEL);
3568}
3569
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003570static struct net_device_ops lionetdevops = {
3571 .ndo_open = liquidio_open,
3572 .ndo_stop = liquidio_stop,
3573 .ndo_start_xmit = liquidio_xmit,
3574 .ndo_get_stats = liquidio_get_stats,
3575 .ndo_set_mac_address = liquidio_set_mac,
3576 .ndo_set_rx_mode = liquidio_set_mcast_list,
3577 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003578
3579 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3580 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003581 .ndo_change_mtu = liquidio_change_mtu,
3582 .ndo_do_ioctl = liquidio_ioctl,
3583 .ndo_fix_features = liquidio_fix_features,
3584 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003585 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3586 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003587};
3588
3589/** \brief Entry point for the liquidio module
3590 */
3591static int __init liquidio_init(void)
3592{
3593 int i;
3594 struct handshake *hs;
3595
3596 init_completion(&first_stage);
3597
3598 octeon_init_device_list(conf_type);
3599
3600 if (liquidio_init_pci())
3601 return -EINVAL;
3602
3603 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3604
3605 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3606 hs = &handshake[i];
3607 if (hs->pci_dev) {
3608 wait_for_completion(&hs->init);
3609 if (!hs->init_ok) {
3610 /* init handshake failed */
3611 dev_err(&hs->pci_dev->dev,
3612 "Failed to init device\n");
3613 liquidio_deinit_pci();
3614 return -EIO;
3615 }
3616 }
3617 }
3618
3619 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3620 hs = &handshake[i];
3621 if (hs->pci_dev) {
3622 wait_for_completion_timeout(&hs->started,
3623 msecs_to_jiffies(30000));
3624 if (!hs->started_ok) {
3625 /* starter handshake failed */
3626 dev_err(&hs->pci_dev->dev,
3627 "Firmware failed to start\n");
3628 liquidio_deinit_pci();
3629 return -EIO;
3630 }
3631 }
3632 }
3633
3634 return 0;
3635}
3636
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003637static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003638{
3639 struct octeon_device *oct = (struct octeon_device *)buf;
3640 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003641 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003642 union oct_link_status *ls;
3643 int i;
3644
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003645 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003646 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3647 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003648 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003649 goto nic_info_err;
3650 }
3651
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003652 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003653 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3654
3655 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003656 for (i = 0; i < oct->ifcount; i++) {
3657 if (oct->props[i].gmxport == gmxport) {
3658 update_link_status(oct->props[i].netdev, ls);
3659 break;
3660 }
3661 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003662
3663nic_info_err:
3664 for (i = 0; i < recv_pkt->buffer_count; i++)
3665 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3666 octeon_free_recv_info(recv_info);
3667 return 0;
3668}
3669
3670/**
3671 * \brief Setup network interfaces
3672 * @param octeon_dev octeon device
3673 *
3674 * Called during init time for each device. It assumes the NIC
3675 * is already up and running. The link information for each
3676 * interface is passed in link_info.
3677 */
3678static int setup_nic_devices(struct octeon_device *octeon_dev)
3679{
3680 struct lio *lio = NULL;
3681 struct net_device *netdev;
3682 u8 mac[6], i, j;
3683 struct octeon_soft_command *sc;
3684 struct liquidio_if_cfg_context *ctx;
3685 struct liquidio_if_cfg_resp *resp;
3686 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003687 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003688 union oct_nic_if_cfg if_cfg;
3689 unsigned int base_queue;
3690 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003691 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003692 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003693 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003694
3695 /* This is to handle link status changes */
3696 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3697 OPCODE_NIC_INFO,
3698 lio_nic_info, octeon_dev);
3699
3700 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3701 * They are handled directly.
3702 */
3703 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3704 free_netbuf);
3705
3706 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3707 free_netsgbuf);
3708
3709 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3710 free_netsgbuf_with_resp);
3711
3712 for (i = 0; i < octeon_dev->ifcount; i++) {
3713 resp_size = sizeof(struct liquidio_if_cfg_resp);
3714 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003715 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003716 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003717 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003718 resp_size, ctx_size);
3719 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3720 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003721 vdata = (struct lio_version *)sc->virtdptr;
3722
3723 *((u64 *)vdata) = 0;
3724 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3725 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3726 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003727
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003728 if (OCTEON_CN23XX_PF(octeon_dev)) {
3729 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3730 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3731 base_queue = octeon_dev->sriov_info.pf_srn;
3732
3733 gmx_port_id = octeon_dev->pf_num;
3734 ifidx_or_pfnum = octeon_dev->pf_num;
3735 } else {
3736 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3737 octeon_get_conf(octeon_dev), i);
3738 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3739 octeon_get_conf(octeon_dev), i);
3740 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3741 octeon_get_conf(octeon_dev), i);
3742 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3743 octeon_get_conf(octeon_dev), i);
3744 ifidx_or_pfnum = i;
3745 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003746
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003747 dev_dbg(&octeon_dev->pci_dev->dev,
3748 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003749 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003750 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003751 ctx->octeon_id = lio_get_device_id(octeon_dev);
3752 init_waitqueue_head(&ctx->wc);
3753
3754 if_cfg.u64 = 0;
3755 if_cfg.s.num_iqueues = num_iqueues;
3756 if_cfg.s.num_oqueues = num_oqueues;
3757 if_cfg.s.base_queue = base_queue;
3758 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003759
3760 sc->iq_no = 0;
3761
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003762 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003763 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003764 if_cfg.u64, 0);
3765
3766 sc->callback = if_cfg_callback;
3767 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003768 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003769
3770 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003771 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003772 dev_err(&octeon_dev->pci_dev->dev,
3773 "iq/oq config failed status: %x\n",
3774 retval);
3775 /* Soft instr is freed by driver in case of failure. */
3776 goto setup_nic_dev_fail;
3777 }
3778
3779 /* Sleep on a wait queue till the cond flag indicates that the
3780 * response arrived or timed-out.
3781 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003782 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3783 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3784 goto setup_nic_wait_intr;
3785 }
3786
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003787 retval = resp->status;
3788 if (retval) {
3789 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3790 goto setup_nic_dev_fail;
3791 }
3792
3793 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3794 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3795
3796 num_iqueues = hweight64(resp->cfg_info.iqmask);
3797 num_oqueues = hweight64(resp->cfg_info.oqmask);
3798
3799 if (!(num_iqueues) || !(num_oqueues)) {
3800 dev_err(&octeon_dev->pci_dev->dev,
3801 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3802 resp->cfg_info.iqmask,
3803 resp->cfg_info.oqmask);
3804 goto setup_nic_dev_fail;
3805 }
3806 dev_dbg(&octeon_dev->pci_dev->dev,
3807 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3808 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3809 num_iqueues, num_oqueues);
3810 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3811
3812 if (!netdev) {
3813 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3814 goto setup_nic_dev_fail;
3815 }
3816
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003817 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003818
3819 if (num_iqueues > 1)
3820 lionetdevops.ndo_select_queue = select_q;
3821
3822 /* Associate the routines that will handle different
3823 * netdev tasks.
3824 */
3825 netdev->netdev_ops = &lionetdevops;
3826
3827 lio = GET_LIO(netdev);
3828
3829 memset(lio, 0, sizeof(struct lio));
3830
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003831 lio->ifidx = ifidx_or_pfnum;
3832
3833 props = &octeon_dev->props[i];
3834 props->gmxport = resp->cfg_info.linfo.gmxport;
3835 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003836
3837 lio->linfo.num_rxpciq = num_oqueues;
3838 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003839 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003840 lio->linfo.rxpciq[j].u64 =
3841 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003842 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003843 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003844 lio->linfo.txpciq[j].u64 =
3845 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003846 }
3847 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3848 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3849 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3850
3851 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3852
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003853 if (OCTEON_CN23XX_PF(octeon_dev) ||
3854 OCTEON_CN6XXX(octeon_dev)) {
3855 lio->dev_capability = NETIF_F_HIGHDMA
3856 | NETIF_F_IP_CSUM
3857 | NETIF_F_IPV6_CSUM
3858 | NETIF_F_SG | NETIF_F_RXCSUM
3859 | NETIF_F_GRO
3860 | NETIF_F_TSO | NETIF_F_TSO6
3861 | NETIF_F_LRO;
3862 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003863 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3864
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003865 /* Copy of transmit encapsulation capabilities:
3866 * TSO, TSO6, Checksums for this device
3867 */
3868 lio->enc_dev_capability = NETIF_F_IP_CSUM
3869 | NETIF_F_IPV6_CSUM
3870 | NETIF_F_GSO_UDP_TUNNEL
3871 | NETIF_F_HW_CSUM | NETIF_F_SG
3872 | NETIF_F_RXCSUM
3873 | NETIF_F_TSO | NETIF_F_TSO6
3874 | NETIF_F_LRO;
3875
3876 netdev->hw_enc_features = (lio->enc_dev_capability &
3877 ~NETIF_F_LRO);
3878
3879 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3880
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003881 netdev->vlan_features = lio->dev_capability;
3882 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003883 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3884 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003885 NETIF_F_HW_VLAN_CTAG_TX;
3886
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003887 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3888
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003889 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003890 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3891 netdev->hw_features = netdev->hw_features &
3892 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003893
Jarod Wilson109cc162016-10-17 15:54:13 -04003894 /* MTU range: 68 - 16000 */
3895 netdev->min_mtu = LIO_MIN_MTU_SIZE;
3896 netdev->max_mtu = LIO_MAX_MTU_SIZE;
3897
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003898 /* Point to the properties for octeon device to which this
3899 * interface belongs.
3900 */
3901 lio->oct_dev = octeon_dev;
3902 lio->octprops = props;
3903 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003904
3905 dev_dbg(&octeon_dev->pci_dev->dev,
3906 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3907 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3908
3909 /* 64-bit swap required on LE machines */
3910 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3911 for (j = 0; j < 6; j++)
3912 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3913
3914 /* Copy MAC Address to OS network device structure */
3915
3916 ether_addr_copy(netdev->dev_addr, mac);
3917
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003918 /* By default all interfaces on a single Octeon uses the same
3919 * tx and rx queues
3920 */
3921 lio->txq = lio->linfo.txpciq[0].s.q_no;
3922 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003923 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003924 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3925 goto setup_nic_dev_fail;
3926 }
3927
3928 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3929
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003930 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3931 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3932
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003933 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003934 dev_err(&octeon_dev->pci_dev->dev,
3935 "Gather list allocation failed\n");
3936 goto setup_nic_dev_fail;
3937 }
3938
3939 /* Register ethtool support */
3940 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003941 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
3942 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
3943 else
3944 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003945
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003946 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003947 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3948 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003949
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003950 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
3951
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003952 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003953 liquidio_set_feature(netdev,
3954 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003955
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07003956 if (setup_link_status_change_wq(netdev))
3957 goto setup_nic_dev_fail;
3958
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003959 /* Register the network device with the OS */
3960 if (register_netdev(netdev)) {
3961 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3962 goto setup_nic_dev_fail;
3963 }
3964
3965 dev_dbg(&octeon_dev->pci_dev->dev,
3966 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3967 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3968 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003969 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003970
3971 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3972
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003973 /* Sending command to firmware to enable Rx checksum offload
3974 * by default at the time of setup of Liquidio driver for
3975 * this device
3976 */
3977 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3978 OCTNET_CMD_RXCSUM_ENABLE);
3979 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3980 OCTNET_CMD_TXCSUM_ENABLE);
3981
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003982 dev_dbg(&octeon_dev->pci_dev->dev,
3983 "NIC ifidx:%d Setup successful\n", i);
3984
3985 octeon_free_soft_command(octeon_dev, sc);
3986 }
3987
3988 return 0;
3989
3990setup_nic_dev_fail:
3991
3992 octeon_free_soft_command(octeon_dev, sc);
3993
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003994setup_nic_wait_intr:
3995
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003996 while (i--) {
3997 dev_err(&octeon_dev->pci_dev->dev,
3998 "NIC ifidx:%d Setup failed\n", i);
3999 liquidio_destroy_nic_device(octeon_dev, i);
4000 }
4001 return -ENODEV;
4002}
4003
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004004#ifdef CONFIG_PCI_IOV
4005static int octeon_enable_sriov(struct octeon_device *oct)
4006{
4007 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4008 struct pci_dev *vfdev;
4009 int err;
4010 u32 u;
4011
4012 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4013 err = pci_enable_sriov(oct->pci_dev,
4014 oct->sriov_info.num_vfs_alloced);
4015 if (err) {
4016 dev_err(&oct->pci_dev->dev,
4017 "OCTEON: Failed to enable PCI sriov: %d\n",
4018 err);
4019 oct->sriov_info.num_vfs_alloced = 0;
4020 return err;
4021 }
4022 oct->sriov_info.sriov_enabled = 1;
4023
4024 /* init lookup table that maps DPI ring number to VF pci_dev
4025 * struct pointer
4026 */
4027 u = 0;
4028 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4029 OCTEON_CN23XX_VF_VID, NULL);
4030 while (vfdev) {
4031 if (vfdev->is_virtfn &&
4032 (vfdev->physfn == oct->pci_dev)) {
4033 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4034 vfdev;
4035 u += oct->sriov_info.rings_per_vf;
4036 }
4037 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4038 OCTEON_CN23XX_VF_VID, vfdev);
4039 }
4040 }
4041
4042 return num_vfs_alloced;
4043}
4044
4045static int lio_pci_sriov_disable(struct octeon_device *oct)
4046{
4047 int u;
4048
4049 if (pci_vfs_assigned(oct->pci_dev)) {
4050 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4051 return -EPERM;
4052 }
4053
4054 pci_disable_sriov(oct->pci_dev);
4055
4056 u = 0;
4057 while (u < MAX_POSSIBLE_VFS) {
4058 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4059 u += oct->sriov_info.rings_per_vf;
4060 }
4061
4062 oct->sriov_info.num_vfs_alloced = 0;
4063 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4064 oct->pf_num);
4065
4066 return 0;
4067}
4068
4069static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4070{
4071 struct octeon_device *oct = pci_get_drvdata(dev);
4072 int ret = 0;
4073
4074 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4075 (oct->sriov_info.sriov_enabled)) {
4076 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4077 oct->pf_num, num_vfs);
4078 return 0;
4079 }
4080
4081 if (!num_vfs) {
4082 ret = lio_pci_sriov_disable(oct);
4083 } else if (num_vfs > oct->sriov_info.max_vfs) {
4084 dev_err(&oct->pci_dev->dev,
4085 "OCTEON: Max allowed VFs:%d user requested:%d",
4086 oct->sriov_info.max_vfs, num_vfs);
4087 ret = -EPERM;
4088 } else {
4089 oct->sriov_info.num_vfs_alloced = num_vfs;
4090 ret = octeon_enable_sriov(oct);
4091 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4092 oct->pf_num, num_vfs);
4093 }
4094
4095 return ret;
4096}
4097#endif
4098
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004099/**
4100 * \brief initialize the NIC
4101 * @param oct octeon device
4102 *
4103 * This initialization routine is called once the Octeon device application is
4104 * up and running
4105 */
4106static int liquidio_init_nic_module(struct octeon_device *oct)
4107{
4108 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004109 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004110 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4111
4112 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4113
4114 /* only default iq and oq were initialized
4115 * initialize the rest as well
4116 */
4117 /* run port_config command for each port */
4118 oct->ifcount = num_nic_ports;
4119
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004120 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004121
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004122 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4123 oct->props[i].gmxport = -1;
4124
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004125 retval = setup_nic_devices(oct);
4126 if (retval) {
4127 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4128 goto octnet_init_failure;
4129 }
4130
4131 liquidio_ptp_init(oct);
4132
4133 /* Initialize interrupt moderation params */
4134 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004135 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004136 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004137 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4138 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4139 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4140 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4141 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4142 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4143 intrmod_cfg->tx_enable = 1;
4144 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4145 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4146 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4147 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004148 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004149 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4150
4151 return retval;
4152
4153octnet_init_failure:
4154
4155 oct->ifcount = 0;
4156
4157 return retval;
4158}
4159
4160/**
4161 * \brief starter callback that invokes the remaining initialization work after
4162 * the NIC is up and running.
4163 * @param octptr work struct work_struct
4164 */
4165static void nic_starter(struct work_struct *work)
4166{
4167 struct octeon_device *oct;
4168 struct cavium_wk *wk = (struct cavium_wk *)work;
4169
4170 oct = (struct octeon_device *)wk->ctxptr;
4171
4172 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4173 return;
4174
4175 /* If the status of the device is CORE_OK, the core
4176 * application has reported its application type. Call
4177 * any registered handlers now and move to the RUNNING
4178 * state.
4179 */
4180 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4181 schedule_delayed_work(&oct->nic_poll_work.work,
4182 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4183 return;
4184 }
4185
4186 atomic_set(&oct->status, OCT_DEV_RUNNING);
4187
4188 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4189 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4190
4191 if (liquidio_init_nic_module(oct))
4192 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4193 else
4194 handshake[oct->octeon_id].started_ok = 1;
4195 } else {
4196 dev_err(&oct->pci_dev->dev,
4197 "Unexpected application running on NIC (%d). Check firmware.\n",
4198 oct->app_mode);
4199 }
4200
4201 complete(&handshake[oct->octeon_id].started);
4202}
4203
4204/**
4205 * \brief Device initialization for each Octeon device that is probed
4206 * @param octeon_dev octeon device
4207 */
4208static int octeon_device_init(struct octeon_device *octeon_dev)
4209{
4210 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004211 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004212 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004213 struct octeon_device_priv *oct_priv =
4214 (struct octeon_device_priv *)octeon_dev->priv;
4215 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4216
4217 /* Enable access to the octeon device and make its DMA capability
4218 * known to the OS.
4219 */
4220 if (octeon_pci_os_setup(octeon_dev))
4221 return 1;
4222
4223 /* Identify the Octeon type and map the BAR address space. */
4224 if (octeon_chip_specific_setup(octeon_dev)) {
4225 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4226 return 1;
4227 }
4228
4229 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4230
4231 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4232
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004233 if (OCTEON_CN23XX_PF(octeon_dev)) {
4234 if (!cn23xx_fw_loaded(octeon_dev)) {
4235 fw_loaded = 0;
4236 /* Do a soft reset of the Octeon device. */
4237 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4238 return 1;
4239 /* things might have changed */
4240 if (!cn23xx_fw_loaded(octeon_dev))
4241 fw_loaded = 0;
4242 else
4243 fw_loaded = 1;
4244 } else {
4245 fw_loaded = 1;
4246 }
4247 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004248 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004249 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004250
4251 /* Initialize the dispatch mechanism used to push packets arriving on
4252 * Octeon Output queues.
4253 */
4254 if (octeon_init_dispatch_list(octeon_dev))
4255 return 1;
4256
4257 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4258 OPCODE_NIC_CORE_DRV_ACTIVE,
4259 octeon_core_drv_init,
4260 octeon_dev);
4261
4262 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4263 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4264 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4265 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4266
4267 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4268
4269 octeon_set_io_queues_off(octeon_dev);
4270
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004271 if (OCTEON_CN23XX_PF(octeon_dev)) {
4272 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4273 if (ret) {
4274 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4275 return ret;
4276 }
4277 }
4278
4279 /* Initialize soft command buffer pool
4280 */
4281 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4282 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4283 return 1;
4284 }
4285 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4286
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004287 /* Setup the data structures that manage this Octeon's Input queues. */
4288 if (octeon_setup_instr_queues(octeon_dev)) {
4289 dev_err(&octeon_dev->pci_dev->dev,
4290 "instruction queue initialization failed\n");
4291 /* On error, release any previously allocated queues */
4292 for (j = 0; j < octeon_dev->num_iqs; j++)
4293 octeon_delete_instr_queue(octeon_dev, j);
4294 return 1;
4295 }
4296 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4297
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004298 /* Initialize lists to manage the requests of different types that
4299 * arrive from user & kernel applications for this octeon device.
4300 */
4301 if (octeon_setup_response_list(octeon_dev)) {
4302 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4303 return 1;
4304 }
4305 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4306
4307 if (octeon_setup_output_queues(octeon_dev)) {
4308 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
4309 /* Release any previously allocated queues */
4310 for (j = 0; j < octeon_dev->num_oqs; j++)
4311 octeon_delete_droq(octeon_dev, j);
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004312 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004313 }
4314
4315 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4316
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004317 if (OCTEON_CN23XX_PF(octeon_dev)) {
4318 if (octeon_allocate_ioq_vector(octeon_dev)) {
4319 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4320 return 1;
4321 }
4322
4323 } else {
4324 /* The input and output queue registers were setup earlier (the
4325 * queues were not enabled). Any additional registers
4326 * that need to be programmed should be done now.
4327 */
4328 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4329 if (ret) {
4330 dev_err(&octeon_dev->pci_dev->dev,
4331 "Failed to configure device registers\n");
4332 return ret;
4333 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004334 }
4335
4336 /* Initialize the tasklet that handles output queue packet processing.*/
4337 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4338 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4339 (unsigned long)octeon_dev);
4340
4341 /* Setup the interrupt handler and record the INT SUM register address
4342 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004343 if (octeon_setup_interrupt(octeon_dev))
4344 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004345
4346 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004347 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004348
4349 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004350 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4351 if (ret) {
4352 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4353 return ret;
4354 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004355
4356 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4357
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004358 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4359 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4360 if (!ddr_timeout) {
4361 dev_info(&octeon_dev->pci_dev->dev,
4362 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4363 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004364
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004365 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004366
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004367 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4368 while (!ddr_timeout) {
4369 set_current_state(TASK_INTERRUPTIBLE);
4370 if (schedule_timeout(HZ / 10)) {
4371 /* user probably pressed Control-C */
4372 return 1;
4373 }
4374 }
4375 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4376 if (ret) {
4377 dev_err(&octeon_dev->pci_dev->dev,
4378 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4379 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004380 return 1;
4381 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004382
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004383 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4384 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4385 return 1;
4386 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004387
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004388 /* Divert uboot to take commands from host instead. */
4389 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004390
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004391 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4392 ret = octeon_init_consoles(octeon_dev);
4393 if (ret) {
4394 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4395 return 1;
4396 }
4397 ret = octeon_add_console(octeon_dev, 0);
4398 if (ret) {
4399 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4400 return 1;
4401 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004402
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004403 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004404
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004405 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4406 ret = load_firmware(octeon_dev);
4407 if (ret) {
4408 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4409 return 1;
4410 }
4411 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4412 * loaded
4413 */
4414 if (OCTEON_CN23XX_PF(octeon_dev))
4415 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4416 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004417 }
4418
4419 handshake[octeon_dev->octeon_id].init_ok = 1;
4420 complete(&handshake[octeon_dev->octeon_id].init);
4421
4422 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4423
4424 /* Send Credit for Octeon Output queues. Credits are always sent after
4425 * the output queue is enabled.
4426 */
4427 for (j = 0; j < octeon_dev->num_oqs; j++)
4428 writel(octeon_dev->droq[j]->max_count,
4429 octeon_dev->droq[j]->pkts_credit_reg);
4430
4431 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004432 return 0;
4433}
4434
4435/**
4436 * \brief Exits the module
4437 */
4438static void __exit liquidio_exit(void)
4439{
4440 liquidio_deinit_pci();
4441
4442 pr_info("LiquidIO network module is now unloaded\n");
4443}
4444
4445module_init(liquidio_init);
4446module_exit(liquidio_exit);