blob: dffed432d58e7ad48cd2616e41d1fcb7bed89be4 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 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 details.
17 ***********************************************************************/
Russell Kinge3bfc6e2017-02-07 15:03:03 -080018#include <linux/module.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070019#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070020#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070021#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070022#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include "liquidio_common.h"
24#include "octeon_droq.h"
25#include "octeon_iq.h"
26#include "response_manager.h"
27#include "octeon_device.h"
28#include "octeon_nic.h"
29#include "octeon_main.h"
30#include "octeon_network.h"
31#include "cn66xx_regs.h"
32#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070033#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070034#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070035#include "liquidio_image.h"
36
37MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
38MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
39MODULE_LICENSE("GPL");
40MODULE_VERSION(LIQUIDIO_VERSION);
41MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
42MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
43MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -080044MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070045
46static int ddr_timeout = 10000;
47module_param(ddr_timeout, int, 0644);
48MODULE_PARM_DESC(ddr_timeout,
49 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
50
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070051#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
52
53static int debug = -1;
54module_param(debug, int, 0644);
55MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
56
57static char fw_type[LIO_MAX_FW_TYPE_LEN];
58module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
59MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
60
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070061static int ptp_enable = 1;
62
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070063/* Bit mask values for lio->ifstate */
64#define LIO_IFSTATE_DROQ_OPS 0x01
65#define LIO_IFSTATE_REGISTERED 0x02
66#define LIO_IFSTATE_RUNNING 0x04
67#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
68
69/* Polling interval for determining when NIC application is alive */
70#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
71
72/* runtime link query interval */
73#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
74
75struct liquidio_if_cfg_context {
76 int octeon_id;
77
78 wait_queue_head_t wc;
79
80 int cond;
81};
82
83struct liquidio_if_cfg_resp {
84 u64 rh;
85 struct liquidio_if_cfg_info cfg_info;
86 u64 status;
87};
88
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -070089struct liquidio_rx_ctl_context {
90 int octeon_id;
91
92 wait_queue_head_t wc;
93
94 int cond;
95};
96
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070097struct oct_link_status_resp {
98 u64 rh;
99 struct oct_link_info link_info;
100 u64 status;
101};
102
103struct oct_timestamp_resp {
104 u64 rh;
105 u64 timestamp;
106 u64 status;
107};
108
109#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
110
111union tx_info {
112 u64 u64;
113 struct {
114#ifdef __BIG_ENDIAN_BITFIELD
115 u16 gso_size;
116 u16 gso_segs;
117 u32 reserved;
118#else
119 u32 reserved;
120 u16 gso_segs;
121 u16 gso_size;
122#endif
123 } s;
124};
125
126/** Octeon device properties to be used by the NIC module.
127 * Each octeon device in the system will be represented
128 * by this structure in the NIC module.
129 */
130
131#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
132
133#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700134#define OCTNIC_GSO_MAX_SIZE \
135 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700136
137/** Structure of a node in list of gather components maintained by
138 * NIC driver for each network device.
139 */
140struct octnic_gather {
141 /** List manipulation. Next and prev pointers. */
142 struct list_head list;
143
144 /** Size of the gather component at sg in bytes. */
145 int sg_size;
146
147 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
148 int adjust;
149
150 /** Gather component that can accommodate max sized fragment list
151 * received from the IP layer.
152 */
153 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700154
155 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700156};
157
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700158struct handshake {
159 struct completion init;
160 struct completion started;
161 struct pci_dev *pci_dev;
162 int init_ok;
163 int started_ok;
164};
165
166struct octeon_device_priv {
167 /** Tasklet structures for this device. */
168 struct tasklet_struct droq_tasklet;
169 unsigned long napi_mask;
170};
171
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800172#ifdef CONFIG_PCI_IOV
173static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
174#endif
175
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700176static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700177static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700178static void liquidio_remove(struct pci_dev *pdev);
179static int liquidio_probe(struct pci_dev *pdev,
180 const struct pci_device_id *ent);
181
182static struct handshake handshake[MAX_OCTEON_DEVICES];
183static struct completion first_stage;
184
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700185static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700186{
187 int q_no;
188 int reschedule = 0;
189 struct octeon_device *oct = (struct octeon_device *)pdev;
190 struct octeon_device_priv *oct_priv =
191 (struct octeon_device_priv *)oct->priv;
192
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700193 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800194 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700195 continue;
196 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
197 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700198 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700199
200 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
201 /* set time and cnt interrupt thresholds for this DROQ
202 * for NAPI
203 */
204 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
205
206 octeon_write_csr64(
207 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
208 0x5700000040ULL);
209 octeon_write_csr64(
210 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
211 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700212 }
213
214 if (reschedule)
215 tasklet_schedule(&oct_priv->droq_tasklet);
216}
217
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700218static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700219{
220 struct octeon_device_priv *oct_priv =
221 (struct octeon_device_priv *)oct->priv;
222 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
223 int i;
224
225 do {
226 pending_pkts = 0;
227
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700228 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800229 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700230 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700231 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700232 }
233 if (pkt_cnt > 0) {
234 pending_pkts += pkt_cnt;
235 tasklet_schedule(&oct_priv->droq_tasklet);
236 }
237 pkt_cnt = 0;
238 schedule_timeout_uninterruptible(1);
239
240 } while (retry-- && pending_pkts);
241
242 return pkt_cnt;
243}
244
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700245/**
246 * \brief Forces all IO queues off on a given device
247 * @param oct Pointer to Octeon device
248 */
249static void force_io_queues_off(struct octeon_device *oct)
250{
251 if ((oct->chip_id == OCTEON_CN66XX) ||
252 (oct->chip_id == OCTEON_CN68XX)) {
253 /* Reset the Enable bits for Input Queues. */
254 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
255
256 /* Reset the Enable bits for Output Queues. */
257 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
258 }
259}
260
261/**
262 * \brief wait for all pending requests to complete
263 * @param oct Pointer to Octeon device
264 *
265 * Called during shutdown sequence
266 */
267static int wait_for_pending_requests(struct octeon_device *oct)
268{
269 int i, pcount = 0;
270
271 for (i = 0; i < 100; i++) {
272 pcount =
273 atomic_read(&oct->response_list
274 [OCTEON_ORDERED_SC_LIST].pending_req_count);
275 if (pcount)
276 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700277 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700278 break;
279 }
280
281 if (pcount)
282 return 1;
283
284 return 0;
285}
286
287/**
288 * \brief Cause device to go quiet so it can be safely removed/reset/etc
289 * @param oct Pointer to Octeon device
290 */
291static inline void pcierror_quiesce_device(struct octeon_device *oct)
292{
293 int i;
294
295 /* Disable the input and output queues now. No more packets will
296 * arrive from Octeon, but we should wait for all packet processing
297 * to finish.
298 */
299 force_io_queues_off(oct);
300
301 /* To allow for in-flight requests */
302 schedule_timeout_uninterruptible(100);
303
304 if (wait_for_pending_requests(oct))
305 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
306
307 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700308 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700309 struct octeon_instr_queue *iq;
310
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800311 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700312 continue;
313 iq = oct->instr_queue[i];
314
315 if (atomic_read(&iq->instr_pending)) {
316 spin_lock_bh(&iq->lock);
317 iq->fill_cnt = 0;
318 iq->octeon_read_index = iq->host_write_index;
319 iq->stats.instr_processed +=
320 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700321 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700322 spin_unlock_bh(&iq->lock);
323 }
324 }
325
326 /* Force all pending ordered list requests to time out. */
327 lio_process_ordered_list(oct, 1);
328
329 /* We do not need to wait for output queue packets to be processed. */
330}
331
332/**
333 * \brief Cleanup PCI AER uncorrectable error status
334 * @param dev Pointer to PCI device
335 */
336static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
337{
338 int pos = 0x100;
339 u32 status, mask;
340
341 pr_info("%s :\n", __func__);
342
343 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
344 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
345 if (dev->error_state == pci_channel_io_normal)
346 status &= ~mask; /* Clear corresponding nonfatal bits */
347 else
348 status &= mask; /* Clear corresponding fatal bits */
349 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
350}
351
352/**
353 * \brief Stop all PCI IO to a given device
354 * @param dev Pointer to Octeon device
355 */
356static void stop_pci_io(struct octeon_device *oct)
357{
358 /* No more instructions will be forwarded. */
359 atomic_set(&oct->status, OCT_DEV_IN_RESET);
360
361 pci_disable_device(oct->pci_dev);
362
363 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700364 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700365
366 pcierror_quiesce_device(oct);
367
368 /* Release the interrupt line */
369 free_irq(oct->pci_dev->irq, oct);
370
371 if (oct->flags & LIO_FLAG_MSI_ENABLED)
372 pci_disable_msi(oct->pci_dev);
373
374 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
375 lio_get_state_string(&oct->status));
376
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700377 /* making it a common function for all OCTEON models */
378 cleanup_aer_uncorrect_error_status(oct->pci_dev);
379}
380
381/**
382 * \brief called when PCI error is detected
383 * @param pdev Pointer to PCI device
384 * @param state The current pci connection state
385 *
386 * This function is called after a PCI bus error affecting
387 * this device has been detected.
388 */
389static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
390 pci_channel_state_t state)
391{
392 struct octeon_device *oct = pci_get_drvdata(pdev);
393
394 /* Non-correctable Non-fatal errors */
395 if (state == pci_channel_io_normal) {
396 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
397 cleanup_aer_uncorrect_error_status(oct->pci_dev);
398 return PCI_ERS_RESULT_CAN_RECOVER;
399 }
400
401 /* Non-correctable Fatal errors */
402 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
403 stop_pci_io(oct);
404
405 /* Always return a DISCONNECT. There is no support for recovery but only
406 * for a clean shutdown.
407 */
408 return PCI_ERS_RESULT_DISCONNECT;
409}
410
411/**
412 * \brief mmio handler
413 * @param pdev Pointer to PCI device
414 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700415static pci_ers_result_t liquidio_pcie_mmio_enabled(
416 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700417{
418 /* We should never hit this since we never ask for a reset for a Fatal
419 * Error. We always return DISCONNECT in io_error above.
420 * But play safe and return RECOVERED for now.
421 */
422 return PCI_ERS_RESULT_RECOVERED;
423}
424
425/**
426 * \brief called after the pci bus has been reset.
427 * @param pdev Pointer to PCI device
428 *
429 * Restart the card from scratch, as if from a cold-boot. Implementation
430 * resembles the first-half of the octeon_resume routine.
431 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700432static pci_ers_result_t liquidio_pcie_slot_reset(
433 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700434{
435 /* We should never hit this since we never ask for a reset for a Fatal
436 * Error. We always return DISCONNECT in io_error above.
437 * But play safe and return RECOVERED for now.
438 */
439 return PCI_ERS_RESULT_RECOVERED;
440}
441
442/**
443 * \brief called when traffic can start flowing again.
444 * @param pdev Pointer to PCI device
445 *
446 * This callback is called when the error recovery driver tells us that
447 * its OK to resume normal operation. Implementation resembles the
448 * second-half of the octeon_resume routine.
449 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700450static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700451{
452 /* Nothing to be done here. */
453}
454
455#ifdef CONFIG_PM
456/**
457 * \brief called when suspending
458 * @param pdev Pointer to PCI device
459 * @param state state to suspend to
460 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700461static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
462 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700463{
464 return 0;
465}
466
467/**
468 * \brief called when resuming
469 * @param pdev Pointer to PCI device
470 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700471static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700472{
473 return 0;
474}
475#endif
476
477/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100478static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700479 .error_detected = liquidio_pcie_error_detected,
480 .mmio_enabled = liquidio_pcie_mmio_enabled,
481 .slot_reset = liquidio_pcie_slot_reset,
482 .resume = liquidio_pcie_resume,
483};
484
485static const struct pci_device_id liquidio_pci_tbl[] = {
486 { /* 68xx */
487 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
488 },
489 { /* 66xx */
490 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
491 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700492 { /* 23xx pf */
493 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
494 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700495 {
496 0, 0, 0, 0, 0, 0, 0
497 }
498};
499MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
500
501static struct pci_driver liquidio_pci_driver = {
502 .name = "LiquidIO",
503 .id_table = liquidio_pci_tbl,
504 .probe = liquidio_probe,
505 .remove = liquidio_remove,
506 .err_handler = &liquidio_err_handler, /* For AER */
507
508#ifdef CONFIG_PM
509 .suspend = liquidio_suspend,
510 .resume = liquidio_resume,
511#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800512#ifdef CONFIG_PCI_IOV
513 .sriov_configure = liquidio_enable_sriov,
514#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700515};
516
517/**
518 * \brief register PCI driver
519 */
520static int liquidio_init_pci(void)
521{
522 return pci_register_driver(&liquidio_pci_driver);
523}
524
525/**
526 * \brief unregister PCI driver
527 */
528static void liquidio_deinit_pci(void)
529{
530 pci_unregister_driver(&liquidio_pci_driver);
531}
532
533/**
534 * \brief check interface state
535 * @param lio per-network private data
536 * @param state_flag flag state to check
537 */
538static inline int ifstate_check(struct lio *lio, int state_flag)
539{
540 return atomic_read(&lio->ifstate) & state_flag;
541}
542
543/**
544 * \brief set interface state
545 * @param lio per-network private data
546 * @param state_flag flag state to set
547 */
548static inline void ifstate_set(struct lio *lio, int state_flag)
549{
550 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
551}
552
553/**
554 * \brief clear interface state
555 * @param lio per-network private data
556 * @param state_flag flag state to clear
557 */
558static inline void ifstate_reset(struct lio *lio, int state_flag)
559{
560 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
561}
562
563/**
564 * \brief Stop Tx queues
565 * @param netdev network device
566 */
567static inline void txqs_stop(struct net_device *netdev)
568{
569 if (netif_is_multiqueue(netdev)) {
570 int i;
571
572 for (i = 0; i < netdev->num_tx_queues; i++)
573 netif_stop_subqueue(netdev, i);
574 } else {
575 netif_stop_queue(netdev);
576 }
577}
578
579/**
580 * \brief Start Tx queues
581 * @param netdev network device
582 */
583static inline void txqs_start(struct net_device *netdev)
584{
585 if (netif_is_multiqueue(netdev)) {
586 int i;
587
588 for (i = 0; i < netdev->num_tx_queues; i++)
589 netif_start_subqueue(netdev, i);
590 } else {
591 netif_start_queue(netdev);
592 }
593}
594
595/**
596 * \brief Wake Tx queues
597 * @param netdev network device
598 */
599static inline void txqs_wake(struct net_device *netdev)
600{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700601 struct lio *lio = GET_LIO(netdev);
602
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700603 if (netif_is_multiqueue(netdev)) {
604 int i;
605
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700606 for (i = 0; i < netdev->num_tx_queues; i++) {
607 int qno = lio->linfo.txpciq[i %
608 (lio->linfo.num_txpciq)].s.q_no;
609
610 if (__netif_subqueue_stopped(netdev, i)) {
611 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
612 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700613 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700614 }
615 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700616 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700617 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
618 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700619 netif_wake_queue(netdev);
620 }
621}
622
623/**
624 * \brief Stop Tx queue
625 * @param netdev network device
626 */
627static void stop_txq(struct net_device *netdev)
628{
629 txqs_stop(netdev);
630}
631
632/**
633 * \brief Start Tx queue
634 * @param netdev network device
635 */
636static void start_txq(struct net_device *netdev)
637{
638 struct lio *lio = GET_LIO(netdev);
639
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700640 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700641 txqs_start(netdev);
642 return;
643 }
644}
645
646/**
647 * \brief Wake a queue
648 * @param netdev network device
649 * @param q which queue to wake
650 */
651static inline void wake_q(struct net_device *netdev, int q)
652{
653 if (netif_is_multiqueue(netdev))
654 netif_wake_subqueue(netdev, q);
655 else
656 netif_wake_queue(netdev);
657}
658
659/**
660 * \brief Stop a queue
661 * @param netdev network device
662 * @param q which queue to stop
663 */
664static inline void stop_q(struct net_device *netdev, int q)
665{
666 if (netif_is_multiqueue(netdev))
667 netif_stop_subqueue(netdev, q);
668 else
669 netif_stop_queue(netdev);
670}
671
672/**
673 * \brief Check Tx queue status, and take appropriate action
674 * @param lio per-network private data
675 * @returns 0 if full, number of queues woken up otherwise
676 */
677static inline int check_txq_status(struct lio *lio)
678{
679 int ret_val = 0;
680
681 if (netif_is_multiqueue(lio->netdev)) {
682 int numqs = lio->netdev->num_tx_queues;
683 int q, iq = 0;
684
685 /* check each sub-queue state */
686 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700687 iq = lio->linfo.txpciq[q %
688 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700689 if (octnet_iq_is_full(lio->oct_dev, iq))
690 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700691 if (__netif_subqueue_stopped(lio->netdev, q)) {
692 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700693 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
694 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700695 ret_val++;
696 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700697 }
698 } else {
699 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
700 return 0;
701 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700702 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
703 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700704 ret_val = 1;
705 }
706 return ret_val;
707}
708
709/**
710 * Remove the node at the head of the list. The list would be empty at
711 * the end of this call if there are no more nodes in the list.
712 */
713static inline struct list_head *list_delete_head(struct list_head *root)
714{
715 struct list_head *node;
716
717 if ((root->prev == root) && (root->next == root))
718 node = NULL;
719 else
720 node = root->next;
721
722 if (node)
723 list_del(node);
724
725 return node;
726}
727
728/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700729 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700730 * @param lio per-network private data
731 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700732static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700733{
734 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700735 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700736
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700737 if (!lio->glist)
738 return;
739
740 for (i = 0; i < lio->linfo.num_txpciq; i++) {
741 do {
742 g = (struct octnic_gather *)
743 list_delete_head(&lio->glist[i]);
744 if (g) {
745 if (g->sg) {
746 dma_unmap_single(&lio->oct_dev->
747 pci_dev->dev,
748 g->sg_dma_ptr,
749 g->sg_size,
750 DMA_TO_DEVICE);
751 kfree((void *)((unsigned long)g->sg -
752 g->adjust));
753 }
754 kfree(g);
755 }
756 } while (g);
757 }
758
759 kfree((void *)lio->glist);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -0800760 kfree((void *)lio->glist_lock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700761}
762
763/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700764 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700765 * @param lio per-network private data
766 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700767static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700768{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700769 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700770 struct octnic_gather *g;
771
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700772 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
773 GFP_KERNEL);
774 if (!lio->glist_lock)
775 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700776
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700777 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
778 GFP_KERNEL);
779 if (!lio->glist) {
780 kfree((void *)lio->glist_lock);
781 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700782 }
783
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700784 for (i = 0; i < num_iqs; i++) {
785 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700786
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700787 spin_lock_init(&lio->glist_lock[i]);
788
789 INIT_LIST_HEAD(&lio->glist[i]);
790
791 for (j = 0; j < lio->tx_qsize; j++) {
792 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
793 numa_node);
794 if (!g)
795 g = kzalloc(sizeof(*g), GFP_KERNEL);
796 if (!g)
797 break;
798
799 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
800 OCT_SG_ENTRY_SIZE);
801
802 g->sg = kmalloc_node(g->sg_size + 8,
803 GFP_KERNEL, numa_node);
804 if (!g->sg)
805 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
806 if (!g->sg) {
807 kfree(g);
808 break;
809 }
810
811 /* The gather component should be aligned on 64-bit
812 * boundary
813 */
814 if (((unsigned long)g->sg) & 7) {
815 g->adjust = 8 - (((unsigned long)g->sg) & 7);
816 g->sg = (struct octeon_sg_entry *)
817 ((unsigned long)g->sg + g->adjust);
818 }
819 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
820 g->sg, g->sg_size,
821 DMA_TO_DEVICE);
822 if (dma_mapping_error(&oct->pci_dev->dev,
823 g->sg_dma_ptr)) {
824 kfree((void *)((unsigned long)g->sg -
825 g->adjust));
826 kfree(g);
827 break;
828 }
829
830 list_add_tail(&g->list, &lio->glist[i]);
831 }
832
833 if (j != lio->tx_qsize) {
834 delete_glists(lio);
835 return 1;
836 }
837 }
838
839 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700840}
841
842/**
843 * \brief Print link information
844 * @param netdev network device
845 */
846static void print_link_info(struct net_device *netdev)
847{
848 struct lio *lio = GET_LIO(netdev);
849
850 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
851 struct oct_link_info *linfo = &lio->linfo;
852
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700853 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700854 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
855 linfo->link.s.speed,
856 (linfo->link.s.duplex) ? "Full" : "Half");
857 } else {
858 netif_info(lio, link, lio->netdev, "Link Down\n");
859 }
860 }
861}
862
863/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700864 * \brief Routine to notify MTU change
865 * @param work work_struct data structure
866 */
867static void octnet_link_status_change(struct work_struct *work)
868{
869 struct cavium_wk *wk = (struct cavium_wk *)work;
870 struct lio *lio = (struct lio *)wk->ctxptr;
871
872 rtnl_lock();
873 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
874 rtnl_unlock();
875}
876
877/**
878 * \brief Sets up the mtu status change work
879 * @param netdev network device
880 */
881static inline int setup_link_status_change_wq(struct net_device *netdev)
882{
883 struct lio *lio = GET_LIO(netdev);
884 struct octeon_device *oct = lio->oct_dev;
885
886 lio->link_status_wq.wq = alloc_workqueue("link-status",
887 WQ_MEM_RECLAIM, 0);
888 if (!lio->link_status_wq.wq) {
889 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
890 return -1;
891 }
892 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
893 octnet_link_status_change);
894 lio->link_status_wq.wk.ctxptr = lio;
895
896 return 0;
897}
898
899static inline void cleanup_link_status_change_wq(struct net_device *netdev)
900{
901 struct lio *lio = GET_LIO(netdev);
902
903 if (lio->link_status_wq.wq) {
904 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
905 destroy_workqueue(lio->link_status_wq.wq);
906 }
907}
908
909/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700910 * \brief Update link status
911 * @param netdev network device
912 * @param ls link status structure
913 *
914 * Called on receipt of a link status response from the core application to
915 * update each interface's link status.
916 */
917static inline void update_link_status(struct net_device *netdev,
918 union oct_link_status *ls)
919{
920 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700921 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700922
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700923 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700924
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700925 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700926 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700927 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700928
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700929 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700930 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700931 txqs_wake(netdev);
932 } else {
933 netif_carrier_off(netdev);
934 stop_txq(netdev);
935 }
936 }
937}
938
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700939/* Runs in interrupt context. */
940static void update_txq_status(struct octeon_device *oct, int iq_num)
941{
942 struct net_device *netdev;
943 struct lio *lio;
944 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
945
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700946 netdev = oct->props[iq->ifidx].netdev;
947
948 /* This is needed because the first IQ does not have
949 * a netdev associated with it.
950 */
951 if (!netdev)
952 return;
953
954 lio = GET_LIO(netdev);
955 if (netif_is_multiqueue(netdev)) {
956 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
957 lio->linfo.link.s.link_up &&
958 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700959 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
960 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700961 netif_wake_subqueue(netdev, iq->q_index);
962 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700963 if (!octnet_iq_is_full(oct, lio->txq)) {
964 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
965 lio->txq,
966 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700967 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700968 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700969 }
970 }
971}
972
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700973static
974int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
975{
976 struct octeon_device *oct = droq->oct_dev;
977 struct octeon_device_priv *oct_priv =
978 (struct octeon_device_priv *)oct->priv;
979
980 if (droq->ops.poll_mode) {
981 droq->ops.napi_fn(droq);
982 } else {
983 if (ret & MSIX_PO_INT) {
984 tasklet_schedule(&oct_priv->droq_tasklet);
985 return 1;
986 }
987 /* this will be flushed periodically by check iq db */
988 if (ret & MSIX_PI_INT)
989 return 0;
990 }
991 return 0;
992}
993
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700994/**
995 * \brief Droq packet processor sceduler
996 * @param oct octeon device
997 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700998static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700999{
1000 struct octeon_device_priv *oct_priv =
1001 (struct octeon_device_priv *)oct->priv;
1002 u64 oq_no;
1003 struct octeon_droq *droq;
1004
1005 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001006 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1007 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001008 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001009 continue;
1010
1011 droq = oct->droq[oq_no];
1012
1013 if (droq->ops.poll_mode) {
1014 droq->ops.napi_fn(droq);
1015 oct_priv->napi_mask |= (1 << oq_no);
1016 } else {
1017 tasklet_schedule(&oct_priv->droq_tasklet);
1018 }
1019 }
1020 }
1021}
1022
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001023static irqreturn_t
1024liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1025{
1026 u64 ret;
1027 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1028 struct octeon_device *oct = ioq_vector->oct_dev;
1029 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1030
1031 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1032
1033 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1034 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1035
1036 return IRQ_HANDLED;
1037}
1038
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001039/**
1040 * \brief Interrupt handler for octeon
1041 * @param irq unused
1042 * @param dev octeon device
1043 */
1044static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001045irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1046 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001047{
1048 struct octeon_device *oct = (struct octeon_device *)dev;
1049 irqreturn_t ret;
1050
1051 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001052 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001053
1054 ret = oct->fn_list.process_interrupt_regs(oct);
1055
1056 if (ret == IRQ_HANDLED)
1057 liquidio_schedule_droq_pkt_handlers(oct);
1058
1059 /* Re-enable our interrupts */
1060 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001061 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001062
1063 return ret;
1064}
1065
1066/**
1067 * \brief Setup interrupt for octeon device
1068 * @param oct octeon device
1069 *
1070 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1071 */
1072static int octeon_setup_interrupt(struct octeon_device *oct)
1073{
1074 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001075 struct msix_entry *msix_entries;
1076 int i;
1077 int num_ioq_vectors;
1078 int num_alloc_ioq_vectors;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001079
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001080 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1081 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1082 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1083 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001084
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001085 oct->msix_entries = kcalloc(
1086 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
1087 if (!oct->msix_entries)
1088 return 1;
1089
1090 msix_entries = (struct msix_entry *)oct->msix_entries;
1091 /*Assumption is that pf msix vectors start from pf srn to pf to
1092 * trs and not from 0. if not change this code
1093 */
1094 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1095 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1096 msix_entries[oct->num_msix_irqs - 1].entry =
1097 oct->sriov_info.trs;
1098 num_alloc_ioq_vectors = pci_enable_msix_range(
1099 oct->pci_dev, msix_entries,
1100 oct->num_msix_irqs,
1101 oct->num_msix_irqs);
1102 if (num_alloc_ioq_vectors < 0) {
1103 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1104 kfree(oct->msix_entries);
1105 oct->msix_entries = NULL;
1106 return 1;
1107 }
1108 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1109
1110 num_ioq_vectors = oct->num_msix_irqs;
1111
1112 /** For PF, there is one non-ioq interrupt handler */
1113 num_ioq_vectors -= 1;
1114 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
1115 liquidio_legacy_intr_handler, 0, "octeon",
1116 oct);
1117 if (irqret) {
1118 dev_err(&oct->pci_dev->dev,
1119 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1120 irqret);
1121 pci_disable_msix(oct->pci_dev);
1122 kfree(oct->msix_entries);
1123 oct->msix_entries = NULL;
1124 return 1;
1125 }
1126
1127 for (i = 0; i < num_ioq_vectors; i++) {
1128 irqret = request_irq(msix_entries[i].vector,
1129 liquidio_msix_intr_handler, 0,
1130 "octeon", &oct->ioq_vector[i]);
1131 if (irqret) {
1132 dev_err(&oct->pci_dev->dev,
1133 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1134 irqret);
1135 /** Freeing the non-ioq irq vector here . */
1136 free_irq(msix_entries[num_ioq_vectors].vector,
1137 oct);
1138
1139 while (i) {
1140 i--;
1141 /** clearing affinity mask. */
1142 irq_set_affinity_hint(
1143 msix_entries[i].vector, NULL);
1144 free_irq(msix_entries[i].vector,
1145 &oct->ioq_vector[i]);
1146 }
1147 pci_disable_msix(oct->pci_dev);
1148 kfree(oct->msix_entries);
1149 oct->msix_entries = NULL;
1150 return 1;
1151 }
1152 oct->ioq_vector[i].vector = msix_entries[i].vector;
1153 /* assign the cpu mask for this msix interrupt vector */
1154 irq_set_affinity_hint(
1155 msix_entries[i].vector,
1156 (&oct->ioq_vector[i].affinity_mask));
1157 }
1158 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1159 oct->octeon_id);
1160 } else {
1161 err = pci_enable_msi(oct->pci_dev);
1162 if (err)
1163 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1164 err);
1165 else
1166 oct->flags |= LIO_FLAG_MSI_ENABLED;
1167
1168 irqret = request_irq(oct->pci_dev->irq,
1169 liquidio_legacy_intr_handler, IRQF_SHARED,
1170 "octeon", oct);
1171 if (irqret) {
1172 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1173 pci_disable_msi(oct->pci_dev);
1174 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1175 irqret);
1176 return 1;
1177 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001178 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001179 return 0;
1180}
1181
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001182static int liquidio_watchdog(void *param)
1183{
1184 u64 wdog;
1185 u16 mask_of_stuck_cores = 0;
1186 u16 mask_of_crashed_cores = 0;
1187 int core_num;
1188 u8 core_is_stuck[LIO_MAX_CORES];
1189 u8 core_crashed[LIO_MAX_CORES];
1190 struct octeon_device *oct = param;
1191
1192 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1193 memset(core_crashed, 0, sizeof(core_crashed));
1194
1195 while (!kthread_should_stop()) {
1196 mask_of_crashed_cores =
1197 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1198
1199 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1200 if (!core_is_stuck[core_num]) {
1201 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1202
1203 /* look at watchdog state field */
1204 wdog &= CIU3_WDOG_MASK;
1205 if (wdog) {
1206 /* this watchdog timer has expired */
1207 core_is_stuck[core_num] =
1208 LIO_MONITOR_WDOG_EXPIRE;
1209 mask_of_stuck_cores |= (1 << core_num);
1210 }
1211 }
1212
1213 if (!core_crashed[core_num])
1214 core_crashed[core_num] =
1215 (mask_of_crashed_cores >> core_num) & 1;
1216 }
1217
1218 if (mask_of_stuck_cores) {
1219 for (core_num = 0; core_num < LIO_MAX_CORES;
1220 core_num++) {
1221 if (core_is_stuck[core_num] == 1) {
1222 dev_err(&oct->pci_dev->dev,
1223 "ERROR: Octeon core %d is stuck!\n",
1224 core_num);
1225 /* 2 means we have printk'd an error
1226 * so no need to repeat the same printk
1227 */
1228 core_is_stuck[core_num] =
1229 LIO_MONITOR_CORE_STUCK_MSGD;
1230 }
1231 }
1232 }
1233
1234 if (mask_of_crashed_cores) {
1235 for (core_num = 0; core_num < LIO_MAX_CORES;
1236 core_num++) {
1237 if (core_crashed[core_num] == 1) {
1238 dev_err(&oct->pci_dev->dev,
1239 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1240 core_num);
1241 /* 2 means we have printk'd an error
1242 * so no need to repeat the same printk
1243 */
1244 core_crashed[core_num] =
1245 LIO_MONITOR_CORE_STUCK_MSGD;
1246 }
1247 }
1248 }
1249#ifdef CONFIG_MODULE_UNLOAD
1250 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1251 /* make module refcount=0 so that rmmod will work */
1252 long refcount;
1253
1254 refcount = module_refcount(THIS_MODULE);
1255
1256 while (refcount > 0) {
1257 module_put(THIS_MODULE);
1258 refcount = module_refcount(THIS_MODULE);
1259 }
1260
1261 /* compensate for and withstand an unlikely (but still
1262 * possible) race condition
1263 */
1264 while (refcount < 0) {
1265 try_module_get(THIS_MODULE);
1266 refcount = module_refcount(THIS_MODULE);
1267 }
1268 }
1269#endif
1270 /* sleep for two seconds */
1271 set_current_state(TASK_INTERRUPTIBLE);
1272 schedule_timeout(2 * HZ);
1273 }
1274
1275 return 0;
1276}
1277
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001278/**
1279 * \brief PCI probe handler
1280 * @param pdev PCI device structure
1281 * @param ent unused
1282 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001283static int
1284liquidio_probe(struct pci_dev *pdev,
1285 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001286{
1287 struct octeon_device *oct_dev = NULL;
1288 struct handshake *hs;
1289
1290 oct_dev = octeon_allocate_device(pdev->device,
1291 sizeof(struct octeon_device_priv));
1292 if (!oct_dev) {
1293 dev_err(&pdev->dev, "Unable to allocate device\n");
1294 return -ENOMEM;
1295 }
1296
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001297 if (pdev->device == OCTEON_CN23XX_PF_VID)
1298 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1299
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001300 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1301 (u32)pdev->vendor, (u32)pdev->device);
1302
1303 /* Assign octeon_device for this device to the private data area. */
1304 pci_set_drvdata(pdev, oct_dev);
1305
1306 /* set linux specific device pointer */
1307 oct_dev->pci_dev = (void *)pdev;
1308
1309 hs = &handshake[oct_dev->octeon_id];
1310 init_completion(&hs->init);
1311 init_completion(&hs->started);
1312 hs->pci_dev = pdev;
1313
1314 if (oct_dev->octeon_id == 0)
1315 /* first LiquidIO NIC is detected */
1316 complete(&first_stage);
1317
1318 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001319 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001320 liquidio_remove(pdev);
1321 return -ENOMEM;
1322 }
1323
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001324 if (OCTEON_CN23XX_PF(oct_dev)) {
1325 u64 scratch1;
1326 u8 bus, device, function;
1327
1328 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1329 if (!(scratch1 & 4ULL)) {
1330 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1331 * the lio watchdog kernel thread is running for this
1332 * NIC. Each NIC gets one watchdog kernel thread.
1333 */
1334 scratch1 |= 4ULL;
1335 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1336 scratch1);
1337
1338 bus = pdev->bus->number;
1339 device = PCI_SLOT(pdev->devfn);
1340 function = PCI_FUNC(pdev->devfn);
1341 oct_dev->watchdog_task = kthread_create(
1342 liquidio_watchdog, oct_dev,
1343 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001344 if (!IS_ERR(oct_dev->watchdog_task)) {
1345 wake_up_process(oct_dev->watchdog_task);
1346 } else {
1347 oct_dev->watchdog_task = NULL;
1348 dev_err(&oct_dev->pci_dev->dev,
1349 "failed to create kernel_thread\n");
1350 liquidio_remove(pdev);
1351 return -1;
1352 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001353 }
1354 }
1355
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001356 oct_dev->rx_pause = 1;
1357 oct_dev->tx_pause = 1;
1358
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001359 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1360
1361 return 0;
1362}
1363
1364/**
1365 *\brief Destroy resources associated with octeon device
1366 * @param pdev PCI device structure
1367 * @param ent unused
1368 */
1369static void octeon_destroy_resources(struct octeon_device *oct)
1370{
1371 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001372 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001373 struct octeon_device_priv *oct_priv =
1374 (struct octeon_device_priv *)oct->priv;
1375
1376 struct handshake *hs;
1377
1378 switch (atomic_read(&oct->status)) {
1379 case OCT_DEV_RUNNING:
1380 case OCT_DEV_CORE_OK:
1381
1382 /* No more instructions will be forwarded. */
1383 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1384
1385 oct->app_mode = CVM_DRV_INVALID_APP;
1386 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1387 lio_get_state_string(&oct->status));
1388
1389 schedule_timeout_uninterruptible(HZ / 10);
1390
1391 /* fallthrough */
1392 case OCT_DEV_HOST_OK:
1393
1394 /* fallthrough */
1395 case OCT_DEV_CONSOLE_INIT_DONE:
1396 /* Remove any consoles */
1397 octeon_remove_consoles(oct);
1398
1399 /* fallthrough */
1400 case OCT_DEV_IO_QUEUES_DONE:
1401 if (wait_for_pending_requests(oct))
1402 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1403
1404 if (lio_wait_for_instr_fetch(oct))
1405 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1406
1407 /* Disable the input and output queues now. No more packets will
1408 * arrive from Octeon, but we should wait for all packet
1409 * processing to finish.
1410 */
1411 oct->fn_list.disable_io_queues(oct);
1412
1413 if (lio_wait_for_oq_pkts(oct))
1414 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1415
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001416 /* fallthrough */
1417 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001418 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001419 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001420
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001421 if (oct->msix_on) {
1422 msix_entries = (struct msix_entry *)oct->msix_entries;
1423 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1424 /* clear the affinity_cpumask */
1425 irq_set_affinity_hint(msix_entries[i].vector,
1426 NULL);
1427 free_irq(msix_entries[i].vector,
1428 &oct->ioq_vector[i]);
1429 }
1430 /* non-iov vector's argument is oct struct */
1431 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001432
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001433 pci_disable_msix(oct->pci_dev);
1434 kfree(oct->msix_entries);
1435 oct->msix_entries = NULL;
1436 } else {
1437 /* Release the interrupt line */
1438 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001439
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001440 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1441 pci_disable_msi(oct->pci_dev);
1442 }
1443
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001444 /* fallthrough */
1445 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001446 if (OCTEON_CN23XX_PF(oct))
1447 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001448
1449 /* fallthrough */
1450 case OCT_DEV_MBOX_SETUP_DONE:
1451 if (OCTEON_CN23XX_PF(oct))
1452 oct->fn_list.free_mbox(oct);
1453
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001454 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001455 case OCT_DEV_IN_RESET:
1456 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001457 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001458 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001459 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001460 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001461 continue;
1462 octeon_delete_droq(oct, i);
1463 }
1464
1465 /* Force any pending handshakes to complete */
1466 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1467 hs = &handshake[i];
1468
1469 if (hs->pci_dev) {
1470 handshake[oct->octeon_id].init_ok = 0;
1471 complete(&handshake[oct->octeon_id].init);
1472 handshake[oct->octeon_id].started_ok = 0;
1473 complete(&handshake[oct->octeon_id].started);
1474 }
1475 }
1476
1477 /* fallthrough */
1478 case OCT_DEV_RESP_LIST_INIT_DONE:
1479 octeon_delete_response_list(oct);
1480
1481 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001482 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001483 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001484 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001485 continue;
1486 octeon_delete_instr_queue(oct, i);
1487 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001488#ifdef CONFIG_PCI_IOV
1489 if (oct->sriov_info.sriov_enabled)
1490 pci_disable_sriov(oct->pci_dev);
1491#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001492 /* fallthrough */
1493 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1494 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001495
1496 /* fallthrough */
1497 case OCT_DEV_DISPATCH_INIT_DONE:
1498 octeon_delete_dispatch_list(oct);
1499 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1500
1501 /* fallthrough */
1502 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001503 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001504 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1505 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001506
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001507 octeon_unmap_pci_barx(oct, 0);
1508 octeon_unmap_pci_barx(oct, 1);
1509
1510 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001511 case OCT_DEV_PCI_ENABLE_DONE:
1512 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001513 /* Disable the device, releasing the PCI INT */
1514 pci_disable_device(oct->pci_dev);
1515
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001516 /* fallthrough */
1517 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001518 /* Nothing to be done here either */
1519 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001520 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001521
1522 tasklet_kill(&oct_priv->droq_tasklet);
1523}
1524
1525/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001526 * \brief Callback for rx ctrl
1527 * @param status status of request
1528 * @param buf pointer to resp structure
1529 */
1530static void rx_ctl_callback(struct octeon_device *oct,
1531 u32 status,
1532 void *buf)
1533{
1534 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1535 struct liquidio_rx_ctl_context *ctx;
1536
1537 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1538
1539 oct = lio_get_device(ctx->octeon_id);
1540 if (status)
1541 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1542 CVM_CAST64(status));
1543 WRITE_ONCE(ctx->cond, 1);
1544
1545 /* This barrier is required to be sure that the response has been
1546 * written fully before waking up the handler
1547 */
1548 wmb();
1549
1550 wake_up_interruptible(&ctx->wc);
1551}
1552
1553/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001554 * \brief Send Rx control command
1555 * @param lio per-network private data
1556 * @param start_stop whether to start or stop
1557 */
1558static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1559{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001560 struct octeon_soft_command *sc;
1561 struct liquidio_rx_ctl_context *ctx;
1562 union octnet_cmd *ncmd;
1563 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1564 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1565 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001566
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001567 if (oct->props[lio->ifidx].rx_on == start_stop)
1568 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001569
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001570 sc = (struct octeon_soft_command *)
1571 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1572 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001573
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001574 ncmd = (union octnet_cmd *)sc->virtdptr;
1575 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1576
1577 WRITE_ONCE(ctx->cond, 0);
1578 ctx->octeon_id = lio_get_device_id(oct);
1579 init_waitqueue_head(&ctx->wc);
1580
1581 ncmd->u64 = 0;
1582 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1583 ncmd->s.param1 = start_stop;
1584
1585 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1586
1587 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1588
1589 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1590 OPCODE_NIC_CMD, 0, 0, 0);
1591
1592 sc->callback = rx_ctl_callback;
1593 sc->callback_arg = sc;
1594 sc->wait_time = 5000;
1595
1596 retval = octeon_send_soft_command(oct, sc);
1597 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001598 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001599 } else {
1600 /* Sleep on a wait queue till the cond flag indicates that the
1601 * response arrived or timed-out.
1602 */
1603 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1604 return;
1605 oct->props[lio->ifidx].rx_on = start_stop;
1606 }
1607
1608 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001609}
1610
1611/**
1612 * \brief Destroy NIC device interface
1613 * @param oct octeon device
1614 * @param ifidx which interface to destroy
1615 *
1616 * Cleanup associated with each interface for an Octeon device when NIC
1617 * module is being unloaded or if initialization fails during load.
1618 */
1619static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1620{
1621 struct net_device *netdev = oct->props[ifidx].netdev;
1622 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001623 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001624
1625 if (!netdev) {
1626 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1627 __func__, ifidx);
1628 return;
1629 }
1630
1631 lio = GET_LIO(netdev);
1632
1633 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1634
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001635 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001636 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001637
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001638 if (oct->props[lio->ifidx].napi_enabled == 1) {
1639 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1640 napi_disable(napi);
1641
1642 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001643
1644 if (OCTEON_CN23XX_PF(oct))
1645 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001646 }
1647
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001648 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1649 unregister_netdev(netdev);
1650
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001651 cleanup_link_status_change_wq(netdev);
1652
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001653 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001654
1655 free_netdev(netdev);
1656
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001657 oct->props[ifidx].gmxport = -1;
1658
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001659 oct->props[ifidx].netdev = NULL;
1660}
1661
1662/**
1663 * \brief Stop complete NIC functionality
1664 * @param oct octeon device
1665 */
1666static int liquidio_stop_nic_module(struct octeon_device *oct)
1667{
1668 int i, j;
1669 struct lio *lio;
1670
1671 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1672 if (!oct->ifcount) {
1673 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1674 return 1;
1675 }
1676
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001677 spin_lock_bh(&oct->cmd_resp_wqlock);
1678 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1679 spin_unlock_bh(&oct->cmd_resp_wqlock);
1680
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001681 for (i = 0; i < oct->ifcount; i++) {
1682 lio = GET_LIO(oct->props[i].netdev);
1683 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001684 octeon_unregister_droq_ops(oct,
1685 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001686 }
1687
1688 for (i = 0; i < oct->ifcount; i++)
1689 liquidio_destroy_nic_device(oct, i);
1690
1691 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1692 return 0;
1693}
1694
1695/**
1696 * \brief Cleans up resources at unload time
1697 * @param pdev PCI device structure
1698 */
1699static void liquidio_remove(struct pci_dev *pdev)
1700{
1701 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1702
1703 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1704
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001705 if (oct_dev->watchdog_task)
1706 kthread_stop(oct_dev->watchdog_task);
1707
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001708 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1709 liquidio_stop_nic_module(oct_dev);
1710
1711 /* Reset the octeon device and cleanup all memory allocated for
1712 * the octeon device by driver.
1713 */
1714 octeon_destroy_resources(oct_dev);
1715
1716 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1717
1718 /* This octeon device has been removed. Update the global
1719 * data structure to reflect this. Free the device structure.
1720 */
1721 octeon_free_device_mem(oct_dev);
1722}
1723
1724/**
1725 * \brief Identify the Octeon device and to map the BAR address space
1726 * @param oct octeon device
1727 */
1728static int octeon_chip_specific_setup(struct octeon_device *oct)
1729{
1730 u32 dev_id, rev_id;
1731 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001732 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001733
1734 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1735 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1736 oct->rev_id = rev_id & 0xff;
1737
1738 switch (dev_id) {
1739 case OCTEON_CN68XX_PCIID:
1740 oct->chip_id = OCTEON_CN68XX;
1741 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001742 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001743 break;
1744
1745 case OCTEON_CN66XX_PCIID:
1746 oct->chip_id = OCTEON_CN66XX;
1747 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001748 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001749 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001750
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001751 case OCTEON_CN23XX_PCIID_PF:
1752 oct->chip_id = OCTEON_CN23XX_PF_VID;
1753 ret = setup_cn23xx_octeon_pf_device(oct);
1754 s = "CN23XX";
1755 break;
1756
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001757 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001758 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001759 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1760 dev_id);
1761 }
1762
1763 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001764 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001765 OCTEON_MAJOR_REV(oct),
1766 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001767 octeon_get_conf(oct)->card_name,
1768 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001769
1770 return ret;
1771}
1772
1773/**
1774 * \brief PCI initialization for each Octeon device.
1775 * @param oct octeon device
1776 */
1777static int octeon_pci_os_setup(struct octeon_device *oct)
1778{
1779 /* setup PCI stuff first */
1780 if (pci_enable_device(oct->pci_dev)) {
1781 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1782 return 1;
1783 }
1784
1785 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1786 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001787 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001788 return 1;
1789 }
1790
1791 /* Enable PCI DMA Master. */
1792 pci_set_master(oct->pci_dev);
1793
1794 return 0;
1795}
1796
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001797static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1798{
1799 int q = 0;
1800
1801 if (netif_is_multiqueue(lio->netdev))
1802 q = skb->queue_mapping % lio->linfo.num_txpciq;
1803
1804 return q;
1805}
1806
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001807/**
1808 * \brief Check Tx queue state for a given network buffer
1809 * @param lio per-network private data
1810 * @param skb network buffer
1811 */
1812static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1813{
1814 int q = 0, iq = 0;
1815
1816 if (netif_is_multiqueue(lio->netdev)) {
1817 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001818 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001819 } else {
1820 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001821 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001822 }
1823
1824 if (octnet_iq_is_full(lio->oct_dev, iq))
1825 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001826
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001827 if (__netif_subqueue_stopped(lio->netdev, q)) {
1828 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001829 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001830 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001831 return 1;
1832}
1833
1834/**
1835 * \brief Unmap and free network buffer
1836 * @param buf buffer
1837 */
1838static void free_netbuf(void *buf)
1839{
1840 struct sk_buff *skb;
1841 struct octnet_buf_free_info *finfo;
1842 struct lio *lio;
1843
1844 finfo = (struct octnet_buf_free_info *)buf;
1845 skb = finfo->skb;
1846 lio = finfo->lio;
1847
1848 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1849 DMA_TO_DEVICE);
1850
1851 check_txq_state(lio, skb);
1852
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001853 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001854}
1855
1856/**
1857 * \brief Unmap and free gather buffer
1858 * @param buf buffer
1859 */
1860static void free_netsgbuf(void *buf)
1861{
1862 struct octnet_buf_free_info *finfo;
1863 struct sk_buff *skb;
1864 struct lio *lio;
1865 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001866 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001867
1868 finfo = (struct octnet_buf_free_info *)buf;
1869 skb = finfo->skb;
1870 lio = finfo->lio;
1871 g = finfo->g;
1872 frags = skb_shinfo(skb)->nr_frags;
1873
1874 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1875 g->sg[0].ptr[0], (skb->len - skb->data_len),
1876 DMA_TO_DEVICE);
1877
1878 i = 1;
1879 while (frags--) {
1880 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1881
1882 pci_unmap_page((lio->oct_dev)->pci_dev,
1883 g->sg[(i >> 2)].ptr[(i & 3)],
1884 frag->size, DMA_TO_DEVICE);
1885 i++;
1886 }
1887
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001888 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1889 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001890
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001891 iq = skb_iq(lio, skb);
1892 spin_lock(&lio->glist_lock[iq]);
1893 list_add_tail(&g->list, &lio->glist[iq]);
1894 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001895
1896 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1897
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001898 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001899}
1900
1901/**
1902 * \brief Unmap and free gather buffer with response
1903 * @param buf buffer
1904 */
1905static void free_netsgbuf_with_resp(void *buf)
1906{
1907 struct octeon_soft_command *sc;
1908 struct octnet_buf_free_info *finfo;
1909 struct sk_buff *skb;
1910 struct lio *lio;
1911 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001912 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001913
1914 sc = (struct octeon_soft_command *)buf;
1915 skb = (struct sk_buff *)sc->callback_arg;
1916 finfo = (struct octnet_buf_free_info *)&skb->cb;
1917
1918 lio = finfo->lio;
1919 g = finfo->g;
1920 frags = skb_shinfo(skb)->nr_frags;
1921
1922 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1923 g->sg[0].ptr[0], (skb->len - skb->data_len),
1924 DMA_TO_DEVICE);
1925
1926 i = 1;
1927 while (frags--) {
1928 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1929
1930 pci_unmap_page((lio->oct_dev)->pci_dev,
1931 g->sg[(i >> 2)].ptr[(i & 3)],
1932 frag->size, DMA_TO_DEVICE);
1933 i++;
1934 }
1935
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001936 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1937 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001938
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001939 iq = skb_iq(lio, skb);
1940
1941 spin_lock(&lio->glist_lock[iq]);
1942 list_add_tail(&g->list, &lio->glist[iq]);
1943 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001944
1945 /* Don't free the skb yet */
1946
1947 check_txq_state(lio, skb);
1948}
1949
1950/**
1951 * \brief Adjust ptp frequency
1952 * @param ptp PTP clock info
1953 * @param ppb how much to adjust by, in parts-per-billion
1954 */
1955static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1956{
1957 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1958 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1959 u64 comp, delta;
1960 unsigned long flags;
1961 bool neg_adj = false;
1962
1963 if (ppb < 0) {
1964 neg_adj = true;
1965 ppb = -ppb;
1966 }
1967
1968 /* The hardware adds the clock compensation value to the
1969 * PTP clock on every coprocessor clock cycle, so we
1970 * compute the delta in terms of coprocessor clocks.
1971 */
1972 delta = (u64)ppb << 32;
1973 do_div(delta, oct->coproc_clock_rate);
1974
1975 spin_lock_irqsave(&lio->ptp_lock, flags);
1976 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1977 if (neg_adj)
1978 comp -= delta;
1979 else
1980 comp += delta;
1981 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1982 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1983
1984 return 0;
1985}
1986
1987/**
1988 * \brief Adjust ptp time
1989 * @param ptp PTP clock info
1990 * @param delta how much to adjust by, in nanosecs
1991 */
1992static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1993{
1994 unsigned long flags;
1995 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1996
1997 spin_lock_irqsave(&lio->ptp_lock, flags);
1998 lio->ptp_adjust += delta;
1999 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2000
2001 return 0;
2002}
2003
2004/**
2005 * \brief Get hardware clock time, including any adjustment
2006 * @param ptp PTP clock info
2007 * @param ts timespec
2008 */
2009static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2010 struct timespec64 *ts)
2011{
2012 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002013 unsigned long flags;
2014 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2015 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2016
2017 spin_lock_irqsave(&lio->ptp_lock, flags);
2018 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2019 ns += lio->ptp_adjust;
2020 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2021
Kefeng Wang286af312016-01-27 17:34:37 +08002022 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002023
2024 return 0;
2025}
2026
2027/**
2028 * \brief Set hardware clock time. Reset adjustment
2029 * @param ptp PTP clock info
2030 * @param ts timespec
2031 */
2032static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2033 const struct timespec64 *ts)
2034{
2035 u64 ns;
2036 unsigned long flags;
2037 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2038 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2039
2040 ns = timespec_to_ns(ts);
2041
2042 spin_lock_irqsave(&lio->ptp_lock, flags);
2043 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2044 lio->ptp_adjust = 0;
2045 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2046
2047 return 0;
2048}
2049
2050/**
2051 * \brief Check if PTP is enabled
2052 * @param ptp PTP clock info
2053 * @param rq request
2054 * @param on is it on
2055 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002056static int
2057liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2058 struct ptp_clock_request *rq __attribute__((unused)),
2059 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002060{
2061 return -EOPNOTSUPP;
2062}
2063
2064/**
2065 * \brief Open PTP clock source
2066 * @param netdev network device
2067 */
2068static void oct_ptp_open(struct net_device *netdev)
2069{
2070 struct lio *lio = GET_LIO(netdev);
2071 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2072
2073 spin_lock_init(&lio->ptp_lock);
2074
2075 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2076 lio->ptp_info.owner = THIS_MODULE;
2077 lio->ptp_info.max_adj = 250000000;
2078 lio->ptp_info.n_alarm = 0;
2079 lio->ptp_info.n_ext_ts = 0;
2080 lio->ptp_info.n_per_out = 0;
2081 lio->ptp_info.pps = 0;
2082 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2083 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2084 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2085 lio->ptp_info.settime64 = liquidio_ptp_settime;
2086 lio->ptp_info.enable = liquidio_ptp_enable;
2087
2088 lio->ptp_adjust = 0;
2089
2090 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2091 &oct->pci_dev->dev);
2092
2093 if (IS_ERR(lio->ptp_clock))
2094 lio->ptp_clock = NULL;
2095}
2096
2097/**
2098 * \brief Init PTP clock
2099 * @param oct octeon device
2100 */
2101static void liquidio_ptp_init(struct octeon_device *oct)
2102{
2103 u64 clock_comp, cfg;
2104
2105 clock_comp = (u64)NSEC_PER_SEC << 32;
2106 do_div(clock_comp, oct->coproc_clock_rate);
2107 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2108
2109 /* Enable */
2110 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2111 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2112}
2113
2114/**
2115 * \brief Load firmware to device
2116 * @param oct octeon device
2117 *
2118 * Maps device to firmware filename, requests firmware, and downloads it
2119 */
2120static int load_firmware(struct octeon_device *oct)
2121{
2122 int ret = 0;
2123 const struct firmware *fw;
2124 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2125 char *tmp_fw_type;
2126
2127 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2128 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2129 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2130 return ret;
2131 }
2132
2133 if (fw_type[0] == '\0')
2134 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2135 else
2136 tmp_fw_type = fw_type;
2137
2138 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2139 octeon_get_conf(oct)->card_name, tmp_fw_type,
2140 LIO_FW_NAME_SUFFIX);
2141
2142 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2143 if (ret) {
2144 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2145 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002146 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002147 return ret;
2148 }
2149
2150 ret = octeon_download_firmware(oct, fw->data, fw->size);
2151
2152 release_firmware(fw);
2153
2154 return ret;
2155}
2156
2157/**
2158 * \brief Setup output queue
2159 * @param oct octeon device
2160 * @param q_no which queue
2161 * @param num_descs how many descriptors
2162 * @param desc_size size of each descriptor
2163 * @param app_ctx application context
2164 */
2165static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2166 int desc_size, void *app_ctx)
2167{
2168 int ret_val = 0;
2169
2170 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2171 /* droq creation and local register settings. */
2172 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302173 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002174 return ret_val;
2175
2176 if (ret_val == 1) {
2177 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2178 return 0;
2179 }
2180 /* tasklet creation for the droq */
2181
2182 /* Enable the droq queues */
2183 octeon_set_droq_pkt_op(oct, q_no, 1);
2184
2185 /* Send Credit for Octeon Output queues. Credits are always
2186 * sent after the output queue is enabled.
2187 */
2188 writel(oct->droq[q_no]->max_count,
2189 oct->droq[q_no]->pkts_credit_reg);
2190
2191 return ret_val;
2192}
2193
2194/**
2195 * \brief Callback for getting interface configuration
2196 * @param status status of request
2197 * @param buf pointer to resp structure
2198 */
2199static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002200 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002201 void *buf)
2202{
2203 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2204 struct liquidio_if_cfg_resp *resp;
2205 struct liquidio_if_cfg_context *ctx;
2206
2207 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002208 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002209
2210 oct = lio_get_device(ctx->octeon_id);
2211 if (resp->status)
2212 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
2213 CVM_CAST64(resp->status));
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002214 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002215
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002216 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2217 resp->cfg_info.liquidio_firmware_version);
2218
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002219 /* This barrier is required to be sure that the response has been
2220 * written fully before waking up the handler
2221 */
2222 wmb();
2223
2224 wake_up_interruptible(&ctx->wc);
2225}
2226
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002227/** Routine to push packets arriving on Octeon interface upto network layer.
2228 * @param oct_id - octeon device id.
2229 * @param skbuff - skbuff struct to be passed to network layer.
2230 * @param len - size of total data received.
2231 * @param rh - Control header associated with the packet
2232 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002233 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002234 */
2235static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002236liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002237 void *skbuff,
2238 u32 len,
2239 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002240 void *param,
2241 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002242{
2243 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002244 struct sk_buff *skb = (struct sk_buff *)skbuff;
2245 struct skb_shared_hwtstamps *shhwtstamps;
2246 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002247 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002248 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002249 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002250 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2251 napi);
2252 if (netdev) {
2253 int packet_was_received;
2254 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002255 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002256
2257 /* Do not proceed if the interface is not in RUNNING state. */
2258 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2259 recv_buffer_free(skb);
2260 droq->stats.rx_dropped++;
2261 return;
2262 }
2263
2264 skb->dev = netdev;
2265
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002266 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002267 if (likely(len > MIN_SKB_SIZE)) {
2268 struct octeon_skb_page_info *pg_info;
2269 unsigned char *va;
2270
2271 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2272 if (pg_info->page) {
2273 /* For Paged allocation use the frags */
2274 va = page_address(pg_info->page) +
2275 pg_info->page_offset;
2276 memcpy(skb->data, va, MIN_SKB_SIZE);
2277 skb_put(skb, MIN_SKB_SIZE);
2278 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2279 pg_info->page,
2280 pg_info->page_offset +
2281 MIN_SKB_SIZE,
2282 len - MIN_SKB_SIZE,
2283 LIO_RXBUFFER_SZ);
2284 }
2285 } else {
2286 struct octeon_skb_page_info *pg_info =
2287 ((struct octeon_skb_page_info *)(skb->cb));
2288 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2289 + pg_info->page_offset, len);
2290 skb_put(skb, len);
2291 put_page(pg_info->page);
2292 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002293
Prasad Kannegantide28c992017-01-09 14:42:40 -08002294 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2295
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002296 if (((oct->chip_id == OCTEON_CN66XX) ||
2297 (oct->chip_id == OCTEON_CN68XX)) &&
2298 ptp_enable) {
2299 if (rh->r_dh.has_hwtstamp) {
2300 /* timestamp is included from the hardware at
2301 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002302 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002303 if (ifstate_check
2304 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2305 /* Nanoseconds are in the first 64-bits
2306 * of the packet.
2307 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002308 memcpy(&ns, (skb->data + r_dh_off),
2309 sizeof(ns));
2310 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002311 shhwtstamps = skb_hwtstamps(skb);
2312 shhwtstamps->hwtstamp =
2313 ns_to_ktime(ns +
2314 lio->ptp_adjust);
2315 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002316 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002317 }
2318
Prasad Kannegantide28c992017-01-09 14:42:40 -08002319 if (rh->r_dh.has_hash) {
2320 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2321 u32 hash = be32_to_cpu(*hash_be);
2322
2323 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2324 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2325 }
2326
2327 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2328
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002329 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002330 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002331 (((rh->r_dh.encap_on) &&
2332 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2333 (!(rh->r_dh.encap_on) &&
2334 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002335 /* checksum has already been verified */
2336 skb->ip_summed = CHECKSUM_UNNECESSARY;
2337 else
2338 skb->ip_summed = CHECKSUM_NONE;
2339
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002340 /* Setting Encapsulation field on basis of status received
2341 * from the firmware
2342 */
2343 if (rh->r_dh.encap_on) {
2344 skb->encapsulation = 1;
2345 skb->csum_level = 1;
2346 droq->stats.rx_vxlan++;
2347 }
2348
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002349 /* inbound VLAN tag */
2350 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2351 (rh->r_dh.vlan != 0)) {
2352 u16 vid = rh->r_dh.vlan;
2353 u16 priority = rh->r_dh.priority;
2354
2355 vtag = priority << 13 | vid;
2356 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2357 }
2358
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002359 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2360
2361 if (packet_was_received) {
2362 droq->stats.rx_bytes_received += len;
2363 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002364 } else {
2365 droq->stats.rx_dropped++;
2366 netif_info(lio, rx_err, lio->netdev,
2367 "droq:%d error rx_dropped:%llu\n",
2368 droq->q_no, droq->stats.rx_dropped);
2369 }
2370
2371 } else {
2372 recv_buffer_free(skb);
2373 }
2374}
2375
2376/**
2377 * \brief wrapper for calling napi_schedule
2378 * @param param parameters to pass to napi_schedule
2379 *
2380 * Used when scheduling on different CPUs
2381 */
2382static void napi_schedule_wrapper(void *param)
2383{
2384 struct napi_struct *napi = param;
2385
2386 napi_schedule(napi);
2387}
2388
2389/**
2390 * \brief callback when receive interrupt occurs and we are in NAPI mode
2391 * @param arg pointer to octeon output queue
2392 */
2393static void liquidio_napi_drv_callback(void *arg)
2394{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002395 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002396 struct octeon_droq *droq = arg;
2397 int this_cpu = smp_processor_id();
2398
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002399 oct = droq->oct_dev;
2400
2401 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2402 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002403 } else {
2404 struct call_single_data *csd = &droq->csd;
2405
2406 csd->func = napi_schedule_wrapper;
2407 csd->info = &droq->napi;
2408 csd->flags = 0;
2409
2410 smp_call_function_single_async(droq->cpu_id, csd);
2411 }
2412}
2413
2414/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002415 * \brief Entry point for NAPI polling
2416 * @param napi NAPI structure
2417 * @param budget maximum number of items to process
2418 */
2419static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2420{
2421 struct octeon_droq *droq;
2422 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002423 int tx_done = 0, iq_no;
2424 struct octeon_instr_queue *iq;
2425 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002426
2427 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002428 oct = droq->oct_dev;
2429 iq_no = droq->q_no;
2430 /* Handle Droq descriptors */
2431 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2432 POLL_EVENT_PROCESS_PKTS,
2433 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002434
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002435 /* Flush the instruction queue */
2436 iq = oct->instr_queue[iq_no];
2437 if (iq) {
2438 /* Process iq buffers with in the budget limits */
Derek Chickles60889862017-01-06 17:16:12 -08002439 tx_done = octeon_flush_iq(oct, iq, budget);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002440 /* Update iq read-index rather than waiting for next interrupt.
2441 * Return back if tx_done is false.
2442 */
2443 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002444 } else {
2445 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2446 __func__, iq_no);
2447 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002448
Satanand Burlacdb478e2017-01-31 13:04:42 -08002449 /* force enable interrupt if reg cnts are high to avoid wraparound */
2450 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002451 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002452 (droq->pkt_count >= MAX_REG_CNT)) {
2453 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002454 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002455 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2456 POLL_EVENT_ENABLE_INTR, 0);
2457 return 0;
2458 }
2459
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002460 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002461}
2462
2463/**
2464 * \brief Setup input and output queues
2465 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002466 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002467 *
2468 * Note: Queues are with respect to the octeon device. Thus
2469 * an input queue is for egress packets, and output queues
2470 * are for ingress packets.
2471 */
2472static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002473 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002474{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002475 struct octeon_droq_ops droq_ops;
2476 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002477 static int cpu_id;
2478 static int cpu_id_modulus;
2479 struct octeon_droq *droq;
2480 struct napi_struct *napi;
2481 int q, q_no, retval = 0;
2482 struct lio *lio;
2483 int num_tx_descs;
2484
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002485 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002486
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002487 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002488
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002489 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2490
2491 droq_ops.fptr = liquidio_push_packet;
2492 droq_ops.farg = (void *)netdev;
2493
2494 droq_ops.poll_mode = 1;
2495 droq_ops.napi_fn = liquidio_napi_drv_callback;
2496 cpu_id = 0;
2497 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002498
2499 /* set up DROQs. */
2500 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002501 q_no = lio->linfo.rxpciq[q].s.q_no;
2502 dev_dbg(&octeon_dev->pci_dev->dev,
2503 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2504 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002505 retval = octeon_setup_droq(octeon_dev, q_no,
2506 CFG_GET_NUM_RX_DESCS_NIC_IF
2507 (octeon_get_conf(octeon_dev),
2508 lio->ifidx),
2509 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2510 (octeon_get_conf(octeon_dev),
2511 lio->ifidx), NULL);
2512 if (retval) {
2513 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002514 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002515 __func__);
2516 return 1;
2517 }
2518
2519 droq = octeon_dev->droq[q_no];
2520 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002521 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2522 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002523 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002524
2525 /* designate a CPU for this droq */
2526 droq->cpu_id = cpu_id;
2527 cpu_id++;
2528 if (cpu_id >= cpu_id_modulus)
2529 cpu_id = 0;
2530
2531 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2532 }
2533
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002534 if (OCTEON_CN23XX_PF(octeon_dev)) {
2535 /* 23XX PF can receive control messages (via the first PF-owned
2536 * droq) from the firmware even if the ethX interface is down,
2537 * so that's why poll_mode must be off for the first droq.
2538 */
2539 octeon_dev->droq[0]->ops.poll_mode = 0;
2540 }
2541
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002542 /* set up IQs. */
2543 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2544 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2545 (octeon_dev),
2546 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002547 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2548 lio->linfo.txpciq[q], num_tx_descs,
2549 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002550 if (retval) {
2551 dev_err(&octeon_dev->pci_dev->dev,
2552 " %s : Runtime IQ(TxQ) creation failed.\n",
2553 __func__);
2554 return 1;
2555 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002556
2557 if (octeon_dev->ioq_vector) {
2558 struct octeon_ioq_vector *ioq_vector;
2559
2560 ioq_vector = &octeon_dev->ioq_vector[q];
2561 netif_set_xps_queue(netdev,
2562 &ioq_vector->affinity_mask,
2563 ioq_vector->iq_index);
2564 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002565 }
2566
2567 return 0;
2568}
2569
2570/**
2571 * \brief Poll routine for checking transmit queue status
2572 * @param work work_struct data structure
2573 */
2574static void octnet_poll_check_txq_status(struct work_struct *work)
2575{
2576 struct cavium_wk *wk = (struct cavium_wk *)work;
2577 struct lio *lio = (struct lio *)wk->ctxptr;
2578
2579 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2580 return;
2581
2582 check_txq_status(lio);
2583 queue_delayed_work(lio->txq_status_wq.wq,
2584 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2585}
2586
2587/**
2588 * \brief Sets up the txq poll check
2589 * @param netdev network device
2590 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002591static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002592{
2593 struct lio *lio = GET_LIO(netdev);
2594 struct octeon_device *oct = lio->oct_dev;
2595
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302596 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2597 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002598 if (!lio->txq_status_wq.wq) {
2599 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002600 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002601 }
2602 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2603 octnet_poll_check_txq_status);
2604 lio->txq_status_wq.wk.ctxptr = lio;
2605 queue_delayed_work(lio->txq_status_wq.wq,
2606 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002607 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002608}
2609
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002610static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2611{
2612 struct lio *lio = GET_LIO(netdev);
2613
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002614 if (lio->txq_status_wq.wq) {
2615 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2616 destroy_workqueue(lio->txq_status_wq.wq);
2617 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002618}
2619
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002620/**
2621 * \brief Net device open for LiquidIO
2622 * @param netdev network device
2623 */
2624static int liquidio_open(struct net_device *netdev)
2625{
2626 struct lio *lio = GET_LIO(netdev);
2627 struct octeon_device *oct = lio->oct_dev;
2628 struct napi_struct *napi, *n;
2629
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002630 if (oct->props[lio->ifidx].napi_enabled == 0) {
2631 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2632 napi_enable(napi);
2633
2634 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002635
2636 if (OCTEON_CN23XX_PF(oct))
2637 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002638 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002639
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002640 if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
2641 ptp_enable)
2642 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002643
2644 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002645
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002646 /* Ready for link status updates */
2647 lio->intf_open = 1;
2648
2649 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2650
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002651 if (OCTEON_CN23XX_PF(oct)) {
2652 if (!oct->msix_on)
2653 if (setup_tx_poll_fn(netdev))
2654 return -1;
2655 } else {
2656 if (setup_tx_poll_fn(netdev))
2657 return -1;
2658 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002659
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002660 start_txq(netdev);
2661
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002662 /* tell Octeon to start forwarding packets to host */
2663 send_rx_ctrl_cmd(lio, 1);
2664
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002665 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2666 netdev->name);
2667
2668 return 0;
2669}
2670
2671/**
2672 * \brief Net device stop for LiquidIO
2673 * @param netdev network device
2674 */
2675static int liquidio_stop(struct net_device *netdev)
2676{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002677 struct lio *lio = GET_LIO(netdev);
2678 struct octeon_device *oct = lio->oct_dev;
2679
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002680 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2681
2682 netif_tx_disable(netdev);
2683
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002684 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002685 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002686 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002687 lio->linfo.link.s.link_up = 0;
2688 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002689
Felix Manlunascb2336b2017-01-11 17:09:02 -08002690 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002691 send_rx_ctrl_cmd(lio, 0);
2692
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002693 if (OCTEON_CN23XX_PF(oct)) {
2694 if (!oct->msix_on)
2695 cleanup_tx_poll_fn(netdev);
2696 } else {
2697 cleanup_tx_poll_fn(netdev);
2698 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002699
2700 if (lio->ptp_clock) {
2701 ptp_clock_unregister(lio->ptp_clock);
2702 lio->ptp_clock = NULL;
2703 }
2704
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002706
2707 return 0;
2708}
2709
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002710/**
2711 * \brief Converts a mask based on net device flags
2712 * @param netdev network device
2713 *
2714 * This routine generates a octnet_ifflags mask from the net device flags
2715 * received from the OS.
2716 */
2717static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2718{
2719 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2720
2721 if (netdev->flags & IFF_PROMISC)
2722 f |= OCTNET_IFFLAG_PROMISC;
2723
2724 if (netdev->flags & IFF_ALLMULTI)
2725 f |= OCTNET_IFFLAG_ALLMULTI;
2726
2727 if (netdev->flags & IFF_MULTICAST) {
2728 f |= OCTNET_IFFLAG_MULTICAST;
2729
2730 /* Accept all multicast addresses if there are more than we
2731 * can handle
2732 */
2733 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2734 f |= OCTNET_IFFLAG_ALLMULTI;
2735 }
2736
2737 if (netdev->flags & IFF_BROADCAST)
2738 f |= OCTNET_IFFLAG_BROADCAST;
2739
2740 return f;
2741}
2742
2743/**
2744 * \brief Net device set_multicast_list
2745 * @param netdev network device
2746 */
2747static void liquidio_set_mcast_list(struct net_device *netdev)
2748{
2749 struct lio *lio = GET_LIO(netdev);
2750 struct octeon_device *oct = lio->oct_dev;
2751 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002752 struct netdev_hw_addr *ha;
2753 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002754 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002755 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2756
2757 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2758
2759 /* Create a ctrl pkt command to be sent to core app. */
2760 nctrl.ncmd.u64 = 0;
2761 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002762 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2763 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002764 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002765 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002766 nctrl.netpndev = (u64)netdev;
2767 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2768
2769 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002770 mc = &nctrl.udd[0];
2771 netdev_for_each_mc_addr(ha, netdev) {
2772 *mc = 0;
2773 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2774 /* no need to swap bytes */
2775
2776 if (++mc > &nctrl.udd[mc_count])
2777 break;
2778 }
2779
2780 /* Apparently, any activity in this call from the kernel has to
2781 * be atomic. So we won't wait for response.
2782 */
2783 nctrl.wait_time = 0;
2784
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002785 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002786 if (ret < 0) {
2787 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2788 ret);
2789 }
2790}
2791
2792/**
2793 * \brief Net device set_mac_address
2794 * @param netdev network device
2795 */
2796static int liquidio_set_mac(struct net_device *netdev, void *p)
2797{
2798 int ret = 0;
2799 struct lio *lio = GET_LIO(netdev);
2800 struct octeon_device *oct = lio->oct_dev;
2801 struct sockaddr *addr = (struct sockaddr *)p;
2802 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002803
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002804 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002805 return -EADDRNOTAVAIL;
2806
2807 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2808
2809 nctrl.ncmd.u64 = 0;
2810 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002811 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002812 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002813 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002814 nctrl.netpndev = (u64)netdev;
2815 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2816 nctrl.wait_time = 100;
2817
2818 nctrl.udd[0] = 0;
2819 /* The MAC Address is presented in network byte order. */
2820 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2821
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002822 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002823 if (ret < 0) {
2824 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2825 return -ENOMEM;
2826 }
2827 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2828 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2829
2830 return 0;
2831}
2832
2833/**
2834 * \brief Net device get_stats
2835 * @param netdev network device
2836 */
2837static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2838{
2839 struct lio *lio = GET_LIO(netdev);
2840 struct net_device_stats *stats = &netdev->stats;
2841 struct octeon_device *oct;
2842 u64 pkts = 0, drop = 0, bytes = 0;
2843 struct oct_droq_stats *oq_stats;
2844 struct oct_iq_stats *iq_stats;
2845 int i, iq_no, oq_no;
2846
2847 oct = lio->oct_dev;
2848
2849 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002850 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002851 iq_stats = &oct->instr_queue[iq_no]->stats;
2852 pkts += iq_stats->tx_done;
2853 drop += iq_stats->tx_dropped;
2854 bytes += iq_stats->tx_tot_bytes;
2855 }
2856
2857 stats->tx_packets = pkts;
2858 stats->tx_bytes = bytes;
2859 stats->tx_dropped = drop;
2860
2861 pkts = 0;
2862 drop = 0;
2863 bytes = 0;
2864
2865 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002866 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002867 oq_stats = &oct->droq[oq_no]->stats;
2868 pkts += oq_stats->rx_pkts_received;
2869 drop += (oq_stats->rx_dropped +
2870 oq_stats->dropped_nodispatch +
2871 oq_stats->dropped_toomany +
2872 oq_stats->dropped_nomem);
2873 bytes += oq_stats->rx_bytes_received;
2874 }
2875
2876 stats->rx_bytes = bytes;
2877 stats->rx_packets = pkts;
2878 stats->rx_dropped = drop;
2879
2880 return stats;
2881}
2882
2883/**
2884 * \brief Net device change_mtu
2885 * @param netdev network device
2886 */
2887static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2888{
2889 struct lio *lio = GET_LIO(netdev);
2890 struct octeon_device *oct = lio->oct_dev;
2891 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002892 int ret = 0;
2893
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002894 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2895
2896 nctrl.ncmd.u64 = 0;
2897 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002898 nctrl.ncmd.s.param1 = new_mtu;
2899 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002900 nctrl.wait_time = 100;
2901 nctrl.netpndev = (u64)netdev;
2902 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2903
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002904 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002905 if (ret < 0) {
2906 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2907 return -1;
2908 }
2909
2910 lio->mtu = new_mtu;
2911
2912 return 0;
2913}
2914
2915/**
2916 * \brief Handler for SIOCSHWTSTAMP ioctl
2917 * @param netdev network device
2918 * @param ifr interface request
2919 * @param cmd command
2920 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002921static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002922{
2923 struct hwtstamp_config conf;
2924 struct lio *lio = GET_LIO(netdev);
2925
2926 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2927 return -EFAULT;
2928
2929 if (conf.flags)
2930 return -EINVAL;
2931
2932 switch (conf.tx_type) {
2933 case HWTSTAMP_TX_ON:
2934 case HWTSTAMP_TX_OFF:
2935 break;
2936 default:
2937 return -ERANGE;
2938 }
2939
2940 switch (conf.rx_filter) {
2941 case HWTSTAMP_FILTER_NONE:
2942 break;
2943 case HWTSTAMP_FILTER_ALL:
2944 case HWTSTAMP_FILTER_SOME:
2945 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2946 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2947 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2948 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2949 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2950 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2951 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2952 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2953 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2954 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2955 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2956 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2957 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2958 break;
2959 default:
2960 return -ERANGE;
2961 }
2962
2963 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2964 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2965
2966 else
2967 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2968
2969 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2970}
2971
2972/**
2973 * \brief ioctl handler
2974 * @param netdev network device
2975 * @param ifr interface request
2976 * @param cmd command
2977 */
2978static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2979{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002980 struct lio *lio = GET_LIO(netdev);
2981
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002982 switch (cmd) {
2983 case SIOCSHWTSTAMP:
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002984 if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
2985 lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
2986 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002987 default:
2988 return -EOPNOTSUPP;
2989 }
2990}
2991
2992/**
2993 * \brief handle a Tx timestamp response
2994 * @param status response status
2995 * @param buf pointer to skb
2996 */
2997static void handle_timestamp(struct octeon_device *oct,
2998 u32 status,
2999 void *buf)
3000{
3001 struct octnet_buf_free_info *finfo;
3002 struct octeon_soft_command *sc;
3003 struct oct_timestamp_resp *resp;
3004 struct lio *lio;
3005 struct sk_buff *skb = (struct sk_buff *)buf;
3006
3007 finfo = (struct octnet_buf_free_info *)skb->cb;
3008 lio = finfo->lio;
3009 sc = finfo->sc;
3010 oct = lio->oct_dev;
3011 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3012
3013 if (status != OCTEON_REQUEST_DONE) {
3014 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3015 CVM_CAST64(status));
3016 resp->timestamp = 0;
3017 }
3018
3019 octeon_swap_8B_data(&resp->timestamp, 1);
3020
Colin Ian King19a6d152016-02-05 16:30:39 +00003021 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003022 struct skb_shared_hwtstamps ts;
3023 u64 ns = resp->timestamp;
3024
3025 netif_info(lio, tx_done, lio->netdev,
3026 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3027 skb, (unsigned long long)ns);
3028 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3029 skb_tstamp_tx(skb, &ts);
3030 }
3031
3032 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003033 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003034}
3035
3036/* \brief Send a data packet that will be timestamped
3037 * @param oct octeon device
3038 * @param ndata pointer to network data
3039 * @param finfo pointer to private network data
3040 */
3041static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3042 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003043 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003044{
3045 int retval;
3046 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003047 struct lio *lio;
3048 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003049 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003050
3051 lio = finfo->lio;
3052
3053 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3054 sizeof(struct oct_timestamp_resp));
3055 finfo->sc = sc;
3056
3057 if (!sc) {
3058 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3059 return IQ_SEND_FAILED;
3060 }
3061
3062 if (ndata->reqtype == REQTYPE_NORESP_NET)
3063 ndata->reqtype = REQTYPE_RESP_NET;
3064 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3065 ndata->reqtype = REQTYPE_RESP_NET_SG;
3066
3067 sc->callback = handle_timestamp;
3068 sc->callback_arg = finfo->skb;
3069 sc->iq_no = ndata->q_no;
3070
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003071 if (OCTEON_CN23XX_PF(oct))
3072 len = (u32)((struct octeon_instr_ih3 *)
3073 (&sc->cmd.cmd3.ih3))->dlengsz;
3074 else
3075 len = (u32)((struct octeon_instr_ih2 *)
3076 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003077
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003078 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003079
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003080 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003081 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003082
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003083 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003084 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3085 retval);
3086 octeon_free_soft_command(oct, sc);
3087 } else {
3088 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3089 }
3090
3091 return retval;
3092}
3093
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003094/** \brief Transmit networks packets to the Octeon interface
3095 * @param skbuff skbuff struct to be passed to network layer.
3096 * @param netdev pointer to network device
3097 * @returns whether the packet was transmitted to the device okay or not
3098 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3099 */
3100static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3101{
3102 struct lio *lio;
3103 struct octnet_buf_free_info *finfo;
3104 union octnic_cmd_setup cmdsetup;
3105 struct octnic_data_pkt ndata;
3106 struct octeon_device *oct;
3107 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003108 struct octeon_instr_irh *irh;
3109 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003110 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003111 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003112 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003113 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003114 u32 tag = 0;
3115
3116 lio = GET_LIO(netdev);
3117 oct = lio->oct_dev;
3118
3119 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003120 q_idx = skb->queue_mapping;
3121 q_idx = (q_idx % (lio->linfo.num_txpciq));
3122 tag = q_idx;
3123 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003124 } else {
3125 iq_no = lio->txq;
3126 }
3127
3128 stats = &oct->instr_queue[iq_no]->stats;
3129
3130 /* Check for all conditions in which the current packet cannot be
3131 * transmitted.
3132 */
3133 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003134 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003135 (skb->len <= 0)) {
3136 netif_info(lio, tx_err, lio->netdev,
3137 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003138 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003139 goto lio_xmit_failed;
3140 }
3141
3142 /* Use space in skb->cb to store info used to unmap and
3143 * free the buffers.
3144 */
3145 finfo = (struct octnet_buf_free_info *)skb->cb;
3146 finfo->lio = lio;
3147 finfo->skb = skb;
3148 finfo->sc = NULL;
3149
3150 /* Prepare the attributes for the data to be passed to OSI. */
3151 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3152
3153 ndata.buf = (void *)finfo;
3154
3155 ndata.q_no = iq_no;
3156
3157 if (netif_is_multiqueue(netdev)) {
3158 if (octnet_iq_is_full(oct, ndata.q_no)) {
3159 /* defer sending if queue is full */
3160 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3161 ndata.q_no);
3162 stats->tx_iq_busy++;
3163 return NETDEV_TX_BUSY;
3164 }
3165 } else {
3166 if (octnet_iq_is_full(oct, lio->txq)) {
3167 /* defer sending if queue is full */
3168 stats->tx_iq_busy++;
3169 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003170 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003171 return NETDEV_TX_BUSY;
3172 }
3173 }
3174 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003175 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003176 */
3177
3178 ndata.datasize = skb->len;
3179
3180 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003181 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003182
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003183 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3184 if (skb->encapsulation) {
3185 cmdsetup.s.tnl_csum = 1;
3186 stats->tx_vxlan++;
3187 } else {
3188 cmdsetup.s.transport_csum = 1;
3189 }
3190 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003191 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3192 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3193 cmdsetup.s.timestamp = 1;
3194 }
3195
3196 if (skb_shinfo(skb)->nr_frags == 0) {
3197 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003198 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003199
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003200 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003201 dptr = dma_map_single(&oct->pci_dev->dev,
3202 skb->data,
3203 skb->len,
3204 DMA_TO_DEVICE);
3205 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003206 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3207 __func__);
3208 return NETDEV_TX_BUSY;
3209 }
3210
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003211 if (OCTEON_CN23XX_PF(oct))
3212 ndata.cmd.cmd3.dptr = dptr;
3213 else
3214 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003215 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003216 ndata.reqtype = REQTYPE_NORESP_NET;
3217
3218 } else {
3219 int i, frags;
3220 struct skb_frag_struct *frag;
3221 struct octnic_gather *g;
3222
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003223 spin_lock(&lio->glist_lock[q_idx]);
3224 g = (struct octnic_gather *)
3225 list_delete_head(&lio->glist[q_idx]);
3226 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003227
3228 if (!g) {
3229 netif_info(lio, tx_err, lio->netdev,
3230 "Transmit scatter gather: glist null!\n");
3231 goto lio_xmit_failed;
3232 }
3233
3234 cmdsetup.s.gather = 1;
3235 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003236 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003237
3238 memset(g->sg, 0, g->sg_size);
3239
3240 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3241 skb->data,
3242 (skb->len - skb->data_len),
3243 DMA_TO_DEVICE);
3244 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3245 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3246 __func__);
3247 return NETDEV_TX_BUSY;
3248 }
3249 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3250
3251 frags = skb_shinfo(skb)->nr_frags;
3252 i = 1;
3253 while (frags--) {
3254 frag = &skb_shinfo(skb)->frags[i - 1];
3255
3256 g->sg[(i >> 2)].ptr[(i & 3)] =
3257 dma_map_page(&oct->pci_dev->dev,
3258 frag->page.p,
3259 frag->page_offset,
3260 frag->size,
3261 DMA_TO_DEVICE);
3262
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003263 if (dma_mapping_error(&oct->pci_dev->dev,
3264 g->sg[i >> 2].ptr[i & 3])) {
3265 dma_unmap_single(&oct->pci_dev->dev,
3266 g->sg[0].ptr[0],
3267 skb->len - skb->data_len,
3268 DMA_TO_DEVICE);
3269 for (j = 1; j < i; j++) {
3270 frag = &skb_shinfo(skb)->frags[j - 1];
3271 dma_unmap_page(&oct->pci_dev->dev,
3272 g->sg[j >> 2].ptr[j & 3],
3273 frag->size,
3274 DMA_TO_DEVICE);
3275 }
3276 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3277 __func__);
3278 return NETDEV_TX_BUSY;
3279 }
3280
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003281 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3282 i++;
3283 }
3284
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003285 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
3286 g->sg_size, DMA_TO_DEVICE);
3287 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003288
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003289 if (OCTEON_CN23XX_PF(oct))
3290 ndata.cmd.cmd3.dptr = dptr;
3291 else
3292 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003293 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003294 finfo->g = g;
3295
3296 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3297 }
3298
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003299 if (OCTEON_CN23XX_PF(oct)) {
3300 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3301 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3302 } else {
3303 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3304 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3305 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003306
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003307 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003308 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3309 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003310 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003311 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003312
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003313 /* HW insert VLAN tag */
3314 if (skb_vlan_tag_present(skb)) {
3315 irh->priority = skb_vlan_tag_get(skb) >> 13;
3316 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3317 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003319 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003320 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003321 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003322 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003323 if (status == IQ_SEND_FAILED)
3324 goto lio_xmit_failed;
3325
3326 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3327
3328 if (status == IQ_SEND_STOP)
3329 stop_q(lio->netdev, q_idx);
3330
Florian Westphal860e9532016-05-03 16:33:13 +02003331 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003332
Satanand Burla80c8eae2017-01-26 11:52:35 -08003333 if (tx_info->s.gso_segs)
3334 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003335 else
3336 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003337 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338
3339 return NETDEV_TX_OK;
3340
3341lio_xmit_failed:
3342 stats->tx_dropped++;
3343 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3344 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003345 if (dptr)
3346 dma_unmap_single(&oct->pci_dev->dev, dptr,
3347 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003348 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003349 return NETDEV_TX_OK;
3350}
3351
3352/** \brief Network device Tx timeout
3353 * @param netdev pointer to network device
3354 */
3355static void liquidio_tx_timeout(struct net_device *netdev)
3356{
3357 struct lio *lio;
3358
3359 lio = GET_LIO(netdev);
3360
3361 netif_info(lio, tx_err, lio->netdev,
3362 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3363 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003364 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003365 txqs_wake(netdev);
3366}
3367
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003368static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3369 __be16 proto __attribute__((unused)),
3370 u16 vid)
3371{
3372 struct lio *lio = GET_LIO(netdev);
3373 struct octeon_device *oct = lio->oct_dev;
3374 struct octnic_ctrl_pkt nctrl;
3375 int ret = 0;
3376
3377 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3378
3379 nctrl.ncmd.u64 = 0;
3380 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3381 nctrl.ncmd.s.param1 = vid;
3382 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3383 nctrl.wait_time = 100;
3384 nctrl.netpndev = (u64)netdev;
3385 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3386
3387 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3388 if (ret < 0) {
3389 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3390 ret);
3391 }
3392
3393 return ret;
3394}
3395
3396static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3397 __be16 proto __attribute__((unused)),
3398 u16 vid)
3399{
3400 struct lio *lio = GET_LIO(netdev);
3401 struct octeon_device *oct = lio->oct_dev;
3402 struct octnic_ctrl_pkt nctrl;
3403 int ret = 0;
3404
3405 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3406
3407 nctrl.ncmd.u64 = 0;
3408 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3409 nctrl.ncmd.s.param1 = vid;
3410 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3411 nctrl.wait_time = 100;
3412 nctrl.netpndev = (u64)netdev;
3413 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3414
3415 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3416 if (ret < 0) {
3417 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3418 ret);
3419 }
3420 return ret;
3421}
3422
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003423/** Sending command to enable/disable RX checksum offload
3424 * @param netdev pointer to network device
3425 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3426 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3427 * OCTNET_CMD_RXCSUM_DISABLE
3428 * @returns SUCCESS or FAILURE
3429 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003430static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3431 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003432{
3433 struct lio *lio = GET_LIO(netdev);
3434 struct octeon_device *oct = lio->oct_dev;
3435 struct octnic_ctrl_pkt nctrl;
3436 int ret = 0;
3437
3438 nctrl.ncmd.u64 = 0;
3439 nctrl.ncmd.s.cmd = command;
3440 nctrl.ncmd.s.param1 = rx_cmd;
3441 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3442 nctrl.wait_time = 100;
3443 nctrl.netpndev = (u64)netdev;
3444 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3445
3446 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3447 if (ret < 0) {
3448 dev_err(&oct->pci_dev->dev,
3449 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3450 ret);
3451 }
3452 return ret;
3453}
3454
3455/** Sending command to add/delete VxLAN UDP port to firmware
3456 * @param netdev pointer to network device
3457 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3458 * @param vxlan_port VxLAN port to be added or deleted
3459 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3460 * OCTNET_CMD_VXLAN_PORT_DEL
3461 * @returns SUCCESS or FAILURE
3462 */
3463static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3464 u16 vxlan_port, u8 vxlan_cmd_bit)
3465{
3466 struct lio *lio = GET_LIO(netdev);
3467 struct octeon_device *oct = lio->oct_dev;
3468 struct octnic_ctrl_pkt nctrl;
3469 int ret = 0;
3470
3471 nctrl.ncmd.u64 = 0;
3472 nctrl.ncmd.s.cmd = command;
3473 nctrl.ncmd.s.more = vxlan_cmd_bit;
3474 nctrl.ncmd.s.param1 = vxlan_port;
3475 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3476 nctrl.wait_time = 100;
3477 nctrl.netpndev = (u64)netdev;
3478 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3479
3480 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3481 if (ret < 0) {
3482 dev_err(&oct->pci_dev->dev,
3483 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3484 ret);
3485 }
3486 return ret;
3487}
3488
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003489/** \brief Net device fix features
3490 * @param netdev pointer to network device
3491 * @param request features requested
3492 * @returns updated features list
3493 */
3494static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3495 netdev_features_t request)
3496{
3497 struct lio *lio = netdev_priv(netdev);
3498
3499 if ((request & NETIF_F_RXCSUM) &&
3500 !(lio->dev_capability & NETIF_F_RXCSUM))
3501 request &= ~NETIF_F_RXCSUM;
3502
3503 if ((request & NETIF_F_HW_CSUM) &&
3504 !(lio->dev_capability & NETIF_F_HW_CSUM))
3505 request &= ~NETIF_F_HW_CSUM;
3506
3507 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3508 request &= ~NETIF_F_TSO;
3509
3510 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3511 request &= ~NETIF_F_TSO6;
3512
3513 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3514 request &= ~NETIF_F_LRO;
3515
3516 /*Disable LRO if RXCSUM is off */
3517 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3518 (lio->dev_capability & NETIF_F_LRO))
3519 request &= ~NETIF_F_LRO;
3520
3521 return request;
3522}
3523
3524/** \brief Net device set features
3525 * @param netdev pointer to network device
3526 * @param features features to enable/disable
3527 */
3528static int liquidio_set_features(struct net_device *netdev,
3529 netdev_features_t features)
3530{
3531 struct lio *lio = netdev_priv(netdev);
3532
3533 if (!((netdev->features ^ features) & NETIF_F_LRO))
3534 return 0;
3535
3536 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003537 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3538 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003539 else if (!(features & NETIF_F_LRO) &&
3540 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003541 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3542 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003543
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003544 /* Sending command to firmware to enable/disable RX checksum
3545 * offload settings using ethtool
3546 */
3547 if (!(netdev->features & NETIF_F_RXCSUM) &&
3548 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3549 (features & NETIF_F_RXCSUM))
3550 liquidio_set_rxcsum_command(netdev,
3551 OCTNET_CMD_TNL_RX_CSUM_CTL,
3552 OCTNET_CMD_RXCSUM_ENABLE);
3553 else if ((netdev->features & NETIF_F_RXCSUM) &&
3554 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3555 !(features & NETIF_F_RXCSUM))
3556 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3557 OCTNET_CMD_RXCSUM_DISABLE);
3558
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003559 return 0;
3560}
3561
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003562static void liquidio_add_vxlan_port(struct net_device *netdev,
3563 struct udp_tunnel_info *ti)
3564{
3565 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3566 return;
3567
3568 liquidio_vxlan_port_command(netdev,
3569 OCTNET_CMD_VXLAN_PORT_CONFIG,
3570 htons(ti->port),
3571 OCTNET_CMD_VXLAN_PORT_ADD);
3572}
3573
3574static void liquidio_del_vxlan_port(struct net_device *netdev,
3575 struct udp_tunnel_info *ti)
3576{
3577 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3578 return;
3579
3580 liquidio_vxlan_port_command(netdev,
3581 OCTNET_CMD_VXLAN_PORT_CONFIG,
3582 htons(ti->port),
3583 OCTNET_CMD_VXLAN_PORT_DEL);
3584}
3585
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003586static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3587 u8 *mac, bool is_admin_assigned)
3588{
3589 struct lio *lio = GET_LIO(netdev);
3590 struct octeon_device *oct = lio->oct_dev;
3591 struct octnic_ctrl_pkt nctrl;
3592
3593 if (!is_valid_ether_addr(mac))
3594 return -EINVAL;
3595
3596 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3597 return -EINVAL;
3598
3599 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3600
3601 nctrl.ncmd.u64 = 0;
3602 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3603 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3604 nctrl.ncmd.s.param1 = vfidx + 1;
3605 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3606 nctrl.ncmd.s.more = 1;
3607 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3608 nctrl.cb_fn = 0;
3609 nctrl.wait_time = LIO_CMD_WAIT_TM;
3610
3611 nctrl.udd[0] = 0;
3612 /* The MAC Address is presented in network byte order. */
3613 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3614
3615 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3616
3617 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3618
3619 return 0;
3620}
3621
3622static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3623{
3624 struct lio *lio = GET_LIO(netdev);
3625 struct octeon_device *oct = lio->oct_dev;
3626 int retval;
3627
3628 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3629 if (!retval)
3630 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3631
3632 return retval;
3633}
3634
3635static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3636 u16 vlan, u8 qos, __be16 vlan_proto)
3637{
3638 struct lio *lio = GET_LIO(netdev);
3639 struct octeon_device *oct = lio->oct_dev;
3640 struct octnic_ctrl_pkt nctrl;
3641 u16 vlantci;
3642
3643 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3644 return -EINVAL;
3645
3646 if (vlan_proto != htons(ETH_P_8021Q))
3647 return -EPROTONOSUPPORT;
3648
3649 if (vlan >= VLAN_N_VID || qos > 7)
3650 return -EINVAL;
3651
3652 if (vlan)
3653 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3654 else
3655 vlantci = 0;
3656
3657 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3658 return 0;
3659
3660 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3661
3662 if (vlan)
3663 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3664 else
3665 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3666
3667 nctrl.ncmd.s.param1 = vlantci;
3668 nctrl.ncmd.s.param2 =
3669 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3670 nctrl.ncmd.s.more = 0;
3671 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3672 nctrl.cb_fn = 0;
3673 nctrl.wait_time = LIO_CMD_WAIT_TM;
3674
3675 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3676
3677 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3678
3679 return 0;
3680}
3681
3682static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3683 struct ifla_vf_info *ivi)
3684{
3685 struct lio *lio = GET_LIO(netdev);
3686 struct octeon_device *oct = lio->oct_dev;
3687 u8 *macaddr;
3688
3689 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3690 return -EINVAL;
3691
3692 ivi->vf = vfidx;
3693 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3694 ether_addr_copy(&ivi->mac[0], macaddr);
3695 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3696 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3697 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3698 return 0;
3699}
3700
3701static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3702 int linkstate)
3703{
3704 struct lio *lio = GET_LIO(netdev);
3705 struct octeon_device *oct = lio->oct_dev;
3706 struct octnic_ctrl_pkt nctrl;
3707
3708 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3709 return -EINVAL;
3710
3711 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3712 return 0;
3713
3714 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3715 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3716 nctrl.ncmd.s.param1 =
3717 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3718 nctrl.ncmd.s.param2 = linkstate;
3719 nctrl.ncmd.s.more = 0;
3720 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3721 nctrl.cb_fn = 0;
3722 nctrl.wait_time = LIO_CMD_WAIT_TM;
3723
3724 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3725
3726 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3727
3728 return 0;
3729}
3730
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003731static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003732 .ndo_open = liquidio_open,
3733 .ndo_stop = liquidio_stop,
3734 .ndo_start_xmit = liquidio_xmit,
3735 .ndo_get_stats = liquidio_get_stats,
3736 .ndo_set_mac_address = liquidio_set_mac,
3737 .ndo_set_rx_mode = liquidio_set_mcast_list,
3738 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003739
3740 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3741 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003742 .ndo_change_mtu = liquidio_change_mtu,
3743 .ndo_do_ioctl = liquidio_ioctl,
3744 .ndo_fix_features = liquidio_fix_features,
3745 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003746 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3747 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003748 .ndo_set_vf_mac = liquidio_set_vf_mac,
3749 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3750 .ndo_get_vf_config = liquidio_get_vf_config,
3751 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003752};
3753
3754/** \brief Entry point for the liquidio module
3755 */
3756static int __init liquidio_init(void)
3757{
3758 int i;
3759 struct handshake *hs;
3760
3761 init_completion(&first_stage);
3762
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003763 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003764
3765 if (liquidio_init_pci())
3766 return -EINVAL;
3767
3768 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3769
3770 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3771 hs = &handshake[i];
3772 if (hs->pci_dev) {
3773 wait_for_completion(&hs->init);
3774 if (!hs->init_ok) {
3775 /* init handshake failed */
3776 dev_err(&hs->pci_dev->dev,
3777 "Failed to init device\n");
3778 liquidio_deinit_pci();
3779 return -EIO;
3780 }
3781 }
3782 }
3783
3784 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3785 hs = &handshake[i];
3786 if (hs->pci_dev) {
3787 wait_for_completion_timeout(&hs->started,
3788 msecs_to_jiffies(30000));
3789 if (!hs->started_ok) {
3790 /* starter handshake failed */
3791 dev_err(&hs->pci_dev->dev,
3792 "Firmware failed to start\n");
3793 liquidio_deinit_pci();
3794 return -EIO;
3795 }
3796 }
3797 }
3798
3799 return 0;
3800}
3801
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003802static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003803{
3804 struct octeon_device *oct = (struct octeon_device *)buf;
3805 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003806 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003807 union oct_link_status *ls;
3808 int i;
3809
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003810 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003811 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3812 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003813 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003814 goto nic_info_err;
3815 }
3816
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003817 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003818 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3819
3820 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003821 for (i = 0; i < oct->ifcount; i++) {
3822 if (oct->props[i].gmxport == gmxport) {
3823 update_link_status(oct->props[i].netdev, ls);
3824 break;
3825 }
3826 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003827
3828nic_info_err:
3829 for (i = 0; i < recv_pkt->buffer_count; i++)
3830 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3831 octeon_free_recv_info(recv_info);
3832 return 0;
3833}
3834
3835/**
3836 * \brief Setup network interfaces
3837 * @param octeon_dev octeon device
3838 *
3839 * Called during init time for each device. It assumes the NIC
3840 * is already up and running. The link information for each
3841 * interface is passed in link_info.
3842 */
3843static int setup_nic_devices(struct octeon_device *octeon_dev)
3844{
3845 struct lio *lio = NULL;
3846 struct net_device *netdev;
3847 u8 mac[6], i, j;
3848 struct octeon_soft_command *sc;
3849 struct liquidio_if_cfg_context *ctx;
3850 struct liquidio_if_cfg_resp *resp;
3851 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003852 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003853 union oct_nic_if_cfg if_cfg;
3854 unsigned int base_queue;
3855 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003856 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003857 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003858 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003859
3860 /* This is to handle link status changes */
3861 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3862 OPCODE_NIC_INFO,
3863 lio_nic_info, octeon_dev);
3864
3865 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3866 * They are handled directly.
3867 */
3868 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3869 free_netbuf);
3870
3871 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3872 free_netsgbuf);
3873
3874 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3875 free_netsgbuf_with_resp);
3876
3877 for (i = 0; i < octeon_dev->ifcount; i++) {
3878 resp_size = sizeof(struct liquidio_if_cfg_resp);
3879 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003880 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003881 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003882 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003883 resp_size, ctx_size);
3884 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3885 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003886 vdata = (struct lio_version *)sc->virtdptr;
3887
3888 *((u64 *)vdata) = 0;
3889 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3890 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3891 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003892
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003893 if (OCTEON_CN23XX_PF(octeon_dev)) {
3894 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3895 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3896 base_queue = octeon_dev->sriov_info.pf_srn;
3897
3898 gmx_port_id = octeon_dev->pf_num;
3899 ifidx_or_pfnum = octeon_dev->pf_num;
3900 } else {
3901 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3902 octeon_get_conf(octeon_dev), i);
3903 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3904 octeon_get_conf(octeon_dev), i);
3905 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3906 octeon_get_conf(octeon_dev), i);
3907 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3908 octeon_get_conf(octeon_dev), i);
3909 ifidx_or_pfnum = i;
3910 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003911
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003912 dev_dbg(&octeon_dev->pci_dev->dev,
3913 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003914 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003915 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003916 ctx->octeon_id = lio_get_device_id(octeon_dev);
3917 init_waitqueue_head(&ctx->wc);
3918
3919 if_cfg.u64 = 0;
3920 if_cfg.s.num_iqueues = num_iqueues;
3921 if_cfg.s.num_oqueues = num_oqueues;
3922 if_cfg.s.base_queue = base_queue;
3923 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003924
3925 sc->iq_no = 0;
3926
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003927 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003928 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003929 if_cfg.u64, 0);
3930
3931 sc->callback = if_cfg_callback;
3932 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003933 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003934
3935 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003936 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003937 dev_err(&octeon_dev->pci_dev->dev,
3938 "iq/oq config failed status: %x\n",
3939 retval);
3940 /* Soft instr is freed by driver in case of failure. */
3941 goto setup_nic_dev_fail;
3942 }
3943
3944 /* Sleep on a wait queue till the cond flag indicates that the
3945 * response arrived or timed-out.
3946 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003947 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3948 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3949 goto setup_nic_wait_intr;
3950 }
3951
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003952 retval = resp->status;
3953 if (retval) {
3954 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3955 goto setup_nic_dev_fail;
3956 }
3957
3958 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3959 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3960
3961 num_iqueues = hweight64(resp->cfg_info.iqmask);
3962 num_oqueues = hweight64(resp->cfg_info.oqmask);
3963
3964 if (!(num_iqueues) || !(num_oqueues)) {
3965 dev_err(&octeon_dev->pci_dev->dev,
3966 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3967 resp->cfg_info.iqmask,
3968 resp->cfg_info.oqmask);
3969 goto setup_nic_dev_fail;
3970 }
3971 dev_dbg(&octeon_dev->pci_dev->dev,
3972 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3973 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3974 num_iqueues, num_oqueues);
3975 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3976
3977 if (!netdev) {
3978 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3979 goto setup_nic_dev_fail;
3980 }
3981
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003982 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003983
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003984 /* Associate the routines that will handle different
3985 * netdev tasks.
3986 */
3987 netdev->netdev_ops = &lionetdevops;
3988
3989 lio = GET_LIO(netdev);
3990
3991 memset(lio, 0, sizeof(struct lio));
3992
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003993 lio->ifidx = ifidx_or_pfnum;
3994
3995 props = &octeon_dev->props[i];
3996 props->gmxport = resp->cfg_info.linfo.gmxport;
3997 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003998
3999 lio->linfo.num_rxpciq = num_oqueues;
4000 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004001 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004002 lio->linfo.rxpciq[j].u64 =
4003 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004004 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004005 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004006 lio->linfo.txpciq[j].u64 =
4007 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004008 }
4009 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4010 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4011 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4012
4013 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4014
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004015 if (OCTEON_CN23XX_PF(octeon_dev) ||
4016 OCTEON_CN6XXX(octeon_dev)) {
4017 lio->dev_capability = NETIF_F_HIGHDMA
4018 | NETIF_F_IP_CSUM
4019 | NETIF_F_IPV6_CSUM
4020 | NETIF_F_SG | NETIF_F_RXCSUM
4021 | NETIF_F_GRO
4022 | NETIF_F_TSO | NETIF_F_TSO6
4023 | NETIF_F_LRO;
4024 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004025 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4026
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004027 /* Copy of transmit encapsulation capabilities:
4028 * TSO, TSO6, Checksums for this device
4029 */
4030 lio->enc_dev_capability = NETIF_F_IP_CSUM
4031 | NETIF_F_IPV6_CSUM
4032 | NETIF_F_GSO_UDP_TUNNEL
4033 | NETIF_F_HW_CSUM | NETIF_F_SG
4034 | NETIF_F_RXCSUM
4035 | NETIF_F_TSO | NETIF_F_TSO6
4036 | NETIF_F_LRO;
4037
4038 netdev->hw_enc_features = (lio->enc_dev_capability &
4039 ~NETIF_F_LRO);
4040
4041 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4042
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004043 netdev->vlan_features = lio->dev_capability;
4044 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004045 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4046 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004047 NETIF_F_HW_VLAN_CTAG_TX;
4048
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004049 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4050
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004051 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004052 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4053 netdev->hw_features = netdev->hw_features &
4054 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004055
Jarod Wilson109cc162016-10-17 15:54:13 -04004056 /* MTU range: 68 - 16000 */
4057 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4058 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4059
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004060 /* Point to the properties for octeon device to which this
4061 * interface belongs.
4062 */
4063 lio->oct_dev = octeon_dev;
4064 lio->octprops = props;
4065 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004066
4067 dev_dbg(&octeon_dev->pci_dev->dev,
4068 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4069 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4070
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004071 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4072 u8 vfmac[ETH_ALEN];
4073
4074 random_ether_addr(&vfmac[0]);
4075 if (__liquidio_set_vf_mac(netdev, j,
4076 &vfmac[0], false)) {
4077 dev_err(&octeon_dev->pci_dev->dev,
4078 "Error setting VF%d MAC address\n",
4079 j);
4080 goto setup_nic_dev_fail;
4081 }
4082 }
4083
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004084 /* 64-bit swap required on LE machines */
4085 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4086 for (j = 0; j < 6; j++)
4087 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4088
4089 /* Copy MAC Address to OS network device structure */
4090
4091 ether_addr_copy(netdev->dev_addr, mac);
4092
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004093 /* By default all interfaces on a single Octeon uses the same
4094 * tx and rx queues
4095 */
4096 lio->txq = lio->linfo.txpciq[0].s.q_no;
4097 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004098 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004099 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4100 goto setup_nic_dev_fail;
4101 }
4102
4103 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4104
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004105 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4106 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4107
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004108 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004109 dev_err(&octeon_dev->pci_dev->dev,
4110 "Gather list allocation failed\n");
4111 goto setup_nic_dev_fail;
4112 }
4113
4114 /* Register ethtool support */
4115 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004116 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4117 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4118 else
4119 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004120
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004121 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004122 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4123 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004124
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004125 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4126
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004127 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004128 liquidio_set_feature(netdev,
4129 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004130
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004131 if (setup_link_status_change_wq(netdev))
4132 goto setup_nic_dev_fail;
4133
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004134 /* Register the network device with the OS */
4135 if (register_netdev(netdev)) {
4136 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4137 goto setup_nic_dev_fail;
4138 }
4139
4140 dev_dbg(&octeon_dev->pci_dev->dev,
4141 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4142 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4143 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004144 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004145
4146 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4147
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004148 /* Sending command to firmware to enable Rx checksum offload
4149 * by default at the time of setup of Liquidio driver for
4150 * this device
4151 */
4152 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4153 OCTNET_CMD_RXCSUM_ENABLE);
4154 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4155 OCTNET_CMD_TXCSUM_ENABLE);
4156
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004157 dev_dbg(&octeon_dev->pci_dev->dev,
4158 "NIC ifidx:%d Setup successful\n", i);
4159
4160 octeon_free_soft_command(octeon_dev, sc);
4161 }
4162
4163 return 0;
4164
4165setup_nic_dev_fail:
4166
4167 octeon_free_soft_command(octeon_dev, sc);
4168
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004169setup_nic_wait_intr:
4170
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004171 while (i--) {
4172 dev_err(&octeon_dev->pci_dev->dev,
4173 "NIC ifidx:%d Setup failed\n", i);
4174 liquidio_destroy_nic_device(octeon_dev, i);
4175 }
4176 return -ENODEV;
4177}
4178
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004179#ifdef CONFIG_PCI_IOV
4180static int octeon_enable_sriov(struct octeon_device *oct)
4181{
4182 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4183 struct pci_dev *vfdev;
4184 int err;
4185 u32 u;
4186
4187 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4188 err = pci_enable_sriov(oct->pci_dev,
4189 oct->sriov_info.num_vfs_alloced);
4190 if (err) {
4191 dev_err(&oct->pci_dev->dev,
4192 "OCTEON: Failed to enable PCI sriov: %d\n",
4193 err);
4194 oct->sriov_info.num_vfs_alloced = 0;
4195 return err;
4196 }
4197 oct->sriov_info.sriov_enabled = 1;
4198
4199 /* init lookup table that maps DPI ring number to VF pci_dev
4200 * struct pointer
4201 */
4202 u = 0;
4203 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4204 OCTEON_CN23XX_VF_VID, NULL);
4205 while (vfdev) {
4206 if (vfdev->is_virtfn &&
4207 (vfdev->physfn == oct->pci_dev)) {
4208 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4209 vfdev;
4210 u += oct->sriov_info.rings_per_vf;
4211 }
4212 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4213 OCTEON_CN23XX_VF_VID, vfdev);
4214 }
4215 }
4216
4217 return num_vfs_alloced;
4218}
4219
4220static int lio_pci_sriov_disable(struct octeon_device *oct)
4221{
4222 int u;
4223
4224 if (pci_vfs_assigned(oct->pci_dev)) {
4225 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4226 return -EPERM;
4227 }
4228
4229 pci_disable_sriov(oct->pci_dev);
4230
4231 u = 0;
4232 while (u < MAX_POSSIBLE_VFS) {
4233 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4234 u += oct->sriov_info.rings_per_vf;
4235 }
4236
4237 oct->sriov_info.num_vfs_alloced = 0;
4238 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4239 oct->pf_num);
4240
4241 return 0;
4242}
4243
4244static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4245{
4246 struct octeon_device *oct = pci_get_drvdata(dev);
4247 int ret = 0;
4248
4249 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4250 (oct->sriov_info.sriov_enabled)) {
4251 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4252 oct->pf_num, num_vfs);
4253 return 0;
4254 }
4255
4256 if (!num_vfs) {
4257 ret = lio_pci_sriov_disable(oct);
4258 } else if (num_vfs > oct->sriov_info.max_vfs) {
4259 dev_err(&oct->pci_dev->dev,
4260 "OCTEON: Max allowed VFs:%d user requested:%d",
4261 oct->sriov_info.max_vfs, num_vfs);
4262 ret = -EPERM;
4263 } else {
4264 oct->sriov_info.num_vfs_alloced = num_vfs;
4265 ret = octeon_enable_sriov(oct);
4266 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4267 oct->pf_num, num_vfs);
4268 }
4269
4270 return ret;
4271}
4272#endif
4273
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004274/**
4275 * \brief initialize the NIC
4276 * @param oct octeon device
4277 *
4278 * This initialization routine is called once the Octeon device application is
4279 * up and running
4280 */
4281static int liquidio_init_nic_module(struct octeon_device *oct)
4282{
4283 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004284 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004285 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4286
4287 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4288
4289 /* only default iq and oq were initialized
4290 * initialize the rest as well
4291 */
4292 /* run port_config command for each port */
4293 oct->ifcount = num_nic_ports;
4294
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004295 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004296
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004297 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4298 oct->props[i].gmxport = -1;
4299
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004300 retval = setup_nic_devices(oct);
4301 if (retval) {
4302 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4303 goto octnet_init_failure;
4304 }
4305
4306 liquidio_ptp_init(oct);
4307
4308 /* Initialize interrupt moderation params */
4309 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004310 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004311 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004312 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4313 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4314 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4315 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4316 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4317 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4318 intrmod_cfg->tx_enable = 1;
4319 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4320 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4321 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4322 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004323 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004324 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4325
4326 return retval;
4327
4328octnet_init_failure:
4329
4330 oct->ifcount = 0;
4331
4332 return retval;
4333}
4334
4335/**
4336 * \brief starter callback that invokes the remaining initialization work after
4337 * the NIC is up and running.
4338 * @param octptr work struct work_struct
4339 */
4340static void nic_starter(struct work_struct *work)
4341{
4342 struct octeon_device *oct;
4343 struct cavium_wk *wk = (struct cavium_wk *)work;
4344
4345 oct = (struct octeon_device *)wk->ctxptr;
4346
4347 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4348 return;
4349
4350 /* If the status of the device is CORE_OK, the core
4351 * application has reported its application type. Call
4352 * any registered handlers now and move to the RUNNING
4353 * state.
4354 */
4355 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4356 schedule_delayed_work(&oct->nic_poll_work.work,
4357 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4358 return;
4359 }
4360
4361 atomic_set(&oct->status, OCT_DEV_RUNNING);
4362
4363 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4364 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4365
4366 if (liquidio_init_nic_module(oct))
4367 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4368 else
4369 handshake[oct->octeon_id].started_ok = 1;
4370 } else {
4371 dev_err(&oct->pci_dev->dev,
4372 "Unexpected application running on NIC (%d). Check firmware.\n",
4373 oct->app_mode);
4374 }
4375
4376 complete(&handshake[oct->octeon_id].started);
4377}
4378
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004379static int
4380octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4381{
4382 struct octeon_device *oct = (struct octeon_device *)buf;
4383 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4384 int i, notice, vf_idx;
4385 u64 *data, vf_num;
4386
4387 notice = recv_pkt->rh.r.ossp;
4388 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4389
4390 /* the first 64-bit word of data is the vf_num */
4391 vf_num = data[0];
4392 octeon_swap_8B_data(&vf_num, 1);
4393 vf_idx = (int)vf_num - 1;
4394
4395 if (notice == VF_DRV_LOADED) {
4396 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4397 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4398 dev_info(&oct->pci_dev->dev,
4399 "driver for VF%d was loaded\n", vf_idx);
4400 try_module_get(THIS_MODULE);
4401 }
4402 } else if (notice == VF_DRV_REMOVED) {
4403 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4404 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4405 dev_info(&oct->pci_dev->dev,
4406 "driver for VF%d was removed\n", vf_idx);
4407 module_put(THIS_MODULE);
4408 }
4409 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4410 u8 *b = (u8 *)&data[1];
4411
4412 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4413 dev_info(&oct->pci_dev->dev,
4414 "VF driver changed VF%d's MAC address to %pM\n",
4415 vf_idx, b + 2);
4416 }
4417
4418 for (i = 0; i < recv_pkt->buffer_count; i++)
4419 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4420 octeon_free_recv_info(recv_info);
4421
4422 return 0;
4423}
4424
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004425/**
4426 * \brief Device initialization for each Octeon device that is probed
4427 * @param octeon_dev octeon device
4428 */
4429static int octeon_device_init(struct octeon_device *octeon_dev)
4430{
4431 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004432 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004433 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004434 struct octeon_device_priv *oct_priv =
4435 (struct octeon_device_priv *)octeon_dev->priv;
4436 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4437
4438 /* Enable access to the octeon device and make its DMA capability
4439 * known to the OS.
4440 */
4441 if (octeon_pci_os_setup(octeon_dev))
4442 return 1;
4443
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004444 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4445
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004446 /* Identify the Octeon type and map the BAR address space. */
4447 if (octeon_chip_specific_setup(octeon_dev)) {
4448 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4449 return 1;
4450 }
4451
4452 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4453
4454 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4455
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004456 if (OCTEON_CN23XX_PF(octeon_dev)) {
4457 if (!cn23xx_fw_loaded(octeon_dev)) {
4458 fw_loaded = 0;
4459 /* Do a soft reset of the Octeon device. */
4460 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4461 return 1;
4462 /* things might have changed */
4463 if (!cn23xx_fw_loaded(octeon_dev))
4464 fw_loaded = 0;
4465 else
4466 fw_loaded = 1;
4467 } else {
4468 fw_loaded = 1;
4469 }
4470 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004471 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004472 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004473
4474 /* Initialize the dispatch mechanism used to push packets arriving on
4475 * Octeon Output queues.
4476 */
4477 if (octeon_init_dispatch_list(octeon_dev))
4478 return 1;
4479
4480 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4481 OPCODE_NIC_CORE_DRV_ACTIVE,
4482 octeon_core_drv_init,
4483 octeon_dev);
4484
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004485 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4486 OPCODE_NIC_VF_DRV_NOTICE,
4487 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004488 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4489 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4490 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4491 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4492
4493 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4494
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004495 if (octeon_set_io_queues_off(octeon_dev)) {
4496 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4497 return 1;
4498 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004499
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004500 if (OCTEON_CN23XX_PF(octeon_dev)) {
4501 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4502 if (ret) {
4503 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4504 return ret;
4505 }
4506 }
4507
4508 /* Initialize soft command buffer pool
4509 */
4510 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4511 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4512 return 1;
4513 }
4514 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4515
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004516 /* Setup the data structures that manage this Octeon's Input queues. */
4517 if (octeon_setup_instr_queues(octeon_dev)) {
4518 dev_err(&octeon_dev->pci_dev->dev,
4519 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004520 return 1;
4521 }
4522 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4523
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004524 /* Initialize lists to manage the requests of different types that
4525 * arrive from user & kernel applications for this octeon device.
4526 */
4527 if (octeon_setup_response_list(octeon_dev)) {
4528 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4529 return 1;
4530 }
4531 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4532
4533 if (octeon_setup_output_queues(octeon_dev)) {
4534 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004535 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004536 }
4537
4538 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4539
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004540 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004541 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4542 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4543 return 1;
4544 }
4545 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4546
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004547 if (octeon_allocate_ioq_vector(octeon_dev)) {
4548 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4549 return 1;
4550 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004551 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004552
4553 } else {
4554 /* The input and output queue registers were setup earlier (the
4555 * queues were not enabled). Any additional registers
4556 * that need to be programmed should be done now.
4557 */
4558 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4559 if (ret) {
4560 dev_err(&octeon_dev->pci_dev->dev,
4561 "Failed to configure device registers\n");
4562 return ret;
4563 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004564 }
4565
4566 /* Initialize the tasklet that handles output queue packet processing.*/
4567 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4568 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4569 (unsigned long)octeon_dev);
4570
4571 /* Setup the interrupt handler and record the INT SUM register address
4572 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004573 if (octeon_setup_interrupt(octeon_dev))
4574 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004575
4576 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004577 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004578
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004579 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4580
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004581 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004582 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4583 if (ret) {
4584 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4585 return ret;
4586 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004587
4588 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4589
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004590 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4591 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4592 if (!ddr_timeout) {
4593 dev_info(&octeon_dev->pci_dev->dev,
4594 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4595 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004596
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004597 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004598
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004599 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4600 while (!ddr_timeout) {
4601 set_current_state(TASK_INTERRUPTIBLE);
4602 if (schedule_timeout(HZ / 10)) {
4603 /* user probably pressed Control-C */
4604 return 1;
4605 }
4606 }
4607 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4608 if (ret) {
4609 dev_err(&octeon_dev->pci_dev->dev,
4610 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4611 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004612 return 1;
4613 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004614
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004615 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4616 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4617 return 1;
4618 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004619
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004620 /* Divert uboot to take commands from host instead. */
4621 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004622
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004623 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4624 ret = octeon_init_consoles(octeon_dev);
4625 if (ret) {
4626 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4627 return 1;
4628 }
4629 ret = octeon_add_console(octeon_dev, 0);
4630 if (ret) {
4631 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4632 return 1;
4633 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004634
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004635 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004636
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004637 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4638 ret = load_firmware(octeon_dev);
4639 if (ret) {
4640 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4641 return 1;
4642 }
4643 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4644 * loaded
4645 */
4646 if (OCTEON_CN23XX_PF(octeon_dev))
4647 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4648 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004649 }
4650
4651 handshake[octeon_dev->octeon_id].init_ok = 1;
4652 complete(&handshake[octeon_dev->octeon_id].init);
4653
4654 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4655
4656 /* Send Credit for Octeon Output queues. Credits are always sent after
4657 * the output queue is enabled.
4658 */
4659 for (j = 0; j < octeon_dev->num_oqs; j++)
4660 writel(octeon_dev->droq[j]->max_count,
4661 octeon_dev->droq[j]->pkts_credit_reg);
4662
4663 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004664 return 0;
4665}
4666
4667/**
4668 * \brief Exits the module
4669 */
4670static void __exit liquidio_exit(void)
4671{
4672 liquidio_deinit_pci();
4673
4674 pr_info("LiquidIO network module is now unloaded\n");
4675}
4676
4677module_init(liquidio_init);
4678module_exit(liquidio_exit);