blob: 10732e0e48cfb08aeec04e9b7b7eac7cbd09d0a0 [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/* Polling interval for determining when NIC application is alive */
64#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
65
66/* runtime link query interval */
67#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
68
69struct liquidio_if_cfg_context {
70 int octeon_id;
71
72 wait_queue_head_t wc;
73
74 int cond;
75};
76
77struct liquidio_if_cfg_resp {
78 u64 rh;
79 struct liquidio_if_cfg_info cfg_info;
80 u64 status;
81};
82
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -070083struct liquidio_rx_ctl_context {
84 int octeon_id;
85
86 wait_queue_head_t wc;
87
88 int cond;
89};
90
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070091struct oct_link_status_resp {
92 u64 rh;
93 struct oct_link_info link_info;
94 u64 status;
95};
96
97struct oct_timestamp_resp {
98 u64 rh;
99 u64 timestamp;
100 u64 status;
101};
102
103#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
104
105union tx_info {
106 u64 u64;
107 struct {
108#ifdef __BIG_ENDIAN_BITFIELD
109 u16 gso_size;
110 u16 gso_segs;
111 u32 reserved;
112#else
113 u32 reserved;
114 u16 gso_segs;
115 u16 gso_size;
116#endif
117 } s;
118};
119
120/** Octeon device properties to be used by the NIC module.
121 * Each octeon device in the system will be represented
122 * by this structure in the NIC module.
123 */
124
125#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
126
127#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700128#define OCTNIC_GSO_MAX_SIZE \
129 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700130
131/** Structure of a node in list of gather components maintained by
132 * NIC driver for each network device.
133 */
134struct octnic_gather {
135 /** List manipulation. Next and prev pointers. */
136 struct list_head list;
137
138 /** Size of the gather component at sg in bytes. */
139 int sg_size;
140
141 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
142 int adjust;
143
144 /** Gather component that can accommodate max sized fragment list
145 * received from the IP layer.
146 */
147 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700148
VSR Burru67e303e2017-03-06 18:45:59 -0800149 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700150};
151
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700152struct handshake {
153 struct completion init;
154 struct completion started;
155 struct pci_dev *pci_dev;
156 int init_ok;
157 int started_ok;
158};
159
160struct octeon_device_priv {
161 /** Tasklet structures for this device. */
162 struct tasklet_struct droq_tasklet;
163 unsigned long napi_mask;
164};
165
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800166#ifdef CONFIG_PCI_IOV
167static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
168#endif
169
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700170static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700171static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700172static void liquidio_remove(struct pci_dev *pdev);
173static int liquidio_probe(struct pci_dev *pdev,
174 const struct pci_device_id *ent);
175
176static struct handshake handshake[MAX_OCTEON_DEVICES];
177static struct completion first_stage;
178
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700179static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700180{
181 int q_no;
182 int reschedule = 0;
183 struct octeon_device *oct = (struct octeon_device *)pdev;
184 struct octeon_device_priv *oct_priv =
185 (struct octeon_device_priv *)oct->priv;
186
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700187 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800188 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700189 continue;
190 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
191 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700192 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700193
194 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
195 /* set time and cnt interrupt thresholds for this DROQ
196 * for NAPI
197 */
198 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
199
200 octeon_write_csr64(
201 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
202 0x5700000040ULL);
203 octeon_write_csr64(
204 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
205 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700206 }
207
208 if (reschedule)
209 tasklet_schedule(&oct_priv->droq_tasklet);
210}
211
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700212static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700213{
214 struct octeon_device_priv *oct_priv =
215 (struct octeon_device_priv *)oct->priv;
216 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
217 int i;
218
219 do {
220 pending_pkts = 0;
221
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700222 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800223 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700224 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700225 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700226 }
227 if (pkt_cnt > 0) {
228 pending_pkts += pkt_cnt;
229 tasklet_schedule(&oct_priv->droq_tasklet);
230 }
231 pkt_cnt = 0;
232 schedule_timeout_uninterruptible(1);
233
234 } while (retry-- && pending_pkts);
235
236 return pkt_cnt;
237}
238
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700239/**
240 * \brief Forces all IO queues off on a given device
241 * @param oct Pointer to Octeon device
242 */
243static void force_io_queues_off(struct octeon_device *oct)
244{
245 if ((oct->chip_id == OCTEON_CN66XX) ||
246 (oct->chip_id == OCTEON_CN68XX)) {
247 /* Reset the Enable bits for Input Queues. */
248 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
249
250 /* Reset the Enable bits for Output Queues. */
251 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
252 }
253}
254
255/**
256 * \brief wait for all pending requests to complete
257 * @param oct Pointer to Octeon device
258 *
259 * Called during shutdown sequence
260 */
261static int wait_for_pending_requests(struct octeon_device *oct)
262{
263 int i, pcount = 0;
264
265 for (i = 0; i < 100; i++) {
266 pcount =
267 atomic_read(&oct->response_list
268 [OCTEON_ORDERED_SC_LIST].pending_req_count);
269 if (pcount)
270 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700271 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700272 break;
273 }
274
275 if (pcount)
276 return 1;
277
278 return 0;
279}
280
281/**
282 * \brief Cause device to go quiet so it can be safely removed/reset/etc
283 * @param oct Pointer to Octeon device
284 */
285static inline void pcierror_quiesce_device(struct octeon_device *oct)
286{
287 int i;
288
289 /* Disable the input and output queues now. No more packets will
290 * arrive from Octeon, but we should wait for all packet processing
291 * to finish.
292 */
293 force_io_queues_off(oct);
294
295 /* To allow for in-flight requests */
296 schedule_timeout_uninterruptible(100);
297
298 if (wait_for_pending_requests(oct))
299 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
300
301 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700302 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700303 struct octeon_instr_queue *iq;
304
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800305 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700306 continue;
307 iq = oct->instr_queue[i];
308
309 if (atomic_read(&iq->instr_pending)) {
310 spin_lock_bh(&iq->lock);
311 iq->fill_cnt = 0;
312 iq->octeon_read_index = iq->host_write_index;
313 iq->stats.instr_processed +=
314 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700315 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700316 spin_unlock_bh(&iq->lock);
317 }
318 }
319
320 /* Force all pending ordered list requests to time out. */
321 lio_process_ordered_list(oct, 1);
322
323 /* We do not need to wait for output queue packets to be processed. */
324}
325
326/**
327 * \brief Cleanup PCI AER uncorrectable error status
328 * @param dev Pointer to PCI device
329 */
330static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
331{
332 int pos = 0x100;
333 u32 status, mask;
334
335 pr_info("%s :\n", __func__);
336
337 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
338 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
339 if (dev->error_state == pci_channel_io_normal)
340 status &= ~mask; /* Clear corresponding nonfatal bits */
341 else
342 status &= mask; /* Clear corresponding fatal bits */
343 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
344}
345
346/**
347 * \brief Stop all PCI IO to a given device
348 * @param dev Pointer to Octeon device
349 */
350static void stop_pci_io(struct octeon_device *oct)
351{
352 /* No more instructions will be forwarded. */
353 atomic_set(&oct->status, OCT_DEV_IN_RESET);
354
355 pci_disable_device(oct->pci_dev);
356
357 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700358 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700359
360 pcierror_quiesce_device(oct);
361
362 /* Release the interrupt line */
363 free_irq(oct->pci_dev->irq, oct);
364
365 if (oct->flags & LIO_FLAG_MSI_ENABLED)
366 pci_disable_msi(oct->pci_dev);
367
368 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
369 lio_get_state_string(&oct->status));
370
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700371 /* making it a common function for all OCTEON models */
372 cleanup_aer_uncorrect_error_status(oct->pci_dev);
373}
374
375/**
376 * \brief called when PCI error is detected
377 * @param pdev Pointer to PCI device
378 * @param state The current pci connection state
379 *
380 * This function is called after a PCI bus error affecting
381 * this device has been detected.
382 */
383static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
384 pci_channel_state_t state)
385{
386 struct octeon_device *oct = pci_get_drvdata(pdev);
387
388 /* Non-correctable Non-fatal errors */
389 if (state == pci_channel_io_normal) {
390 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
391 cleanup_aer_uncorrect_error_status(oct->pci_dev);
392 return PCI_ERS_RESULT_CAN_RECOVER;
393 }
394
395 /* Non-correctable Fatal errors */
396 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
397 stop_pci_io(oct);
398
399 /* Always return a DISCONNECT. There is no support for recovery but only
400 * for a clean shutdown.
401 */
402 return PCI_ERS_RESULT_DISCONNECT;
403}
404
405/**
406 * \brief mmio handler
407 * @param pdev Pointer to PCI device
408 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700409static pci_ers_result_t liquidio_pcie_mmio_enabled(
410 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700411{
412 /* We should never hit this since we never ask for a reset for a Fatal
413 * Error. We always return DISCONNECT in io_error above.
414 * But play safe and return RECOVERED for now.
415 */
416 return PCI_ERS_RESULT_RECOVERED;
417}
418
419/**
420 * \brief called after the pci bus has been reset.
421 * @param pdev Pointer to PCI device
422 *
423 * Restart the card from scratch, as if from a cold-boot. Implementation
424 * resembles the first-half of the octeon_resume routine.
425 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700426static pci_ers_result_t liquidio_pcie_slot_reset(
427 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700428{
429 /* We should never hit this since we never ask for a reset for a Fatal
430 * Error. We always return DISCONNECT in io_error above.
431 * But play safe and return RECOVERED for now.
432 */
433 return PCI_ERS_RESULT_RECOVERED;
434}
435
436/**
437 * \brief called when traffic can start flowing again.
438 * @param pdev Pointer to PCI device
439 *
440 * This callback is called when the error recovery driver tells us that
441 * its OK to resume normal operation. Implementation resembles the
442 * second-half of the octeon_resume routine.
443 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700444static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700445{
446 /* Nothing to be done here. */
447}
448
449#ifdef CONFIG_PM
450/**
451 * \brief called when suspending
452 * @param pdev Pointer to PCI device
453 * @param state state to suspend to
454 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700455static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
456 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700457{
458 return 0;
459}
460
461/**
462 * \brief called when resuming
463 * @param pdev Pointer to PCI device
464 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700465static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700466{
467 return 0;
468}
469#endif
470
471/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100472static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700473 .error_detected = liquidio_pcie_error_detected,
474 .mmio_enabled = liquidio_pcie_mmio_enabled,
475 .slot_reset = liquidio_pcie_slot_reset,
476 .resume = liquidio_pcie_resume,
477};
478
479static const struct pci_device_id liquidio_pci_tbl[] = {
480 { /* 68xx */
481 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
482 },
483 { /* 66xx */
484 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
485 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700486 { /* 23xx pf */
487 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
488 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700489 {
490 0, 0, 0, 0, 0, 0, 0
491 }
492};
493MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
494
495static struct pci_driver liquidio_pci_driver = {
496 .name = "LiquidIO",
497 .id_table = liquidio_pci_tbl,
498 .probe = liquidio_probe,
499 .remove = liquidio_remove,
500 .err_handler = &liquidio_err_handler, /* For AER */
501
502#ifdef CONFIG_PM
503 .suspend = liquidio_suspend,
504 .resume = liquidio_resume,
505#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800506#ifdef CONFIG_PCI_IOV
507 .sriov_configure = liquidio_enable_sriov,
508#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700509};
510
511/**
512 * \brief register PCI driver
513 */
514static int liquidio_init_pci(void)
515{
516 return pci_register_driver(&liquidio_pci_driver);
517}
518
519/**
520 * \brief unregister PCI driver
521 */
522static void liquidio_deinit_pci(void)
523{
524 pci_unregister_driver(&liquidio_pci_driver);
525}
526
527/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700528 * \brief Stop Tx queues
529 * @param netdev network device
530 */
531static inline void txqs_stop(struct net_device *netdev)
532{
533 if (netif_is_multiqueue(netdev)) {
534 int i;
535
536 for (i = 0; i < netdev->num_tx_queues; i++)
537 netif_stop_subqueue(netdev, i);
538 } else {
539 netif_stop_queue(netdev);
540 }
541}
542
543/**
544 * \brief Start Tx queues
545 * @param netdev network device
546 */
547static inline void txqs_start(struct net_device *netdev)
548{
549 if (netif_is_multiqueue(netdev)) {
550 int i;
551
552 for (i = 0; i < netdev->num_tx_queues; i++)
553 netif_start_subqueue(netdev, i);
554 } else {
555 netif_start_queue(netdev);
556 }
557}
558
559/**
560 * \brief Wake Tx queues
561 * @param netdev network device
562 */
563static inline void txqs_wake(struct net_device *netdev)
564{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700565 struct lio *lio = GET_LIO(netdev);
566
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700567 if (netif_is_multiqueue(netdev)) {
568 int i;
569
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700570 for (i = 0; i < netdev->num_tx_queues; i++) {
571 int qno = lio->linfo.txpciq[i %
572 (lio->linfo.num_txpciq)].s.q_no;
573
574 if (__netif_subqueue_stopped(netdev, i)) {
575 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
576 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700577 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700578 }
579 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700580 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700581 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
582 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700583 netif_wake_queue(netdev);
584 }
585}
586
587/**
588 * \brief Stop Tx queue
589 * @param netdev network device
590 */
591static void stop_txq(struct net_device *netdev)
592{
593 txqs_stop(netdev);
594}
595
596/**
597 * \brief Start Tx queue
598 * @param netdev network device
599 */
600static void start_txq(struct net_device *netdev)
601{
602 struct lio *lio = GET_LIO(netdev);
603
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700604 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700605 txqs_start(netdev);
606 return;
607 }
608}
609
610/**
611 * \brief Wake a queue
612 * @param netdev network device
613 * @param q which queue to wake
614 */
615static inline void wake_q(struct net_device *netdev, int q)
616{
617 if (netif_is_multiqueue(netdev))
618 netif_wake_subqueue(netdev, q);
619 else
620 netif_wake_queue(netdev);
621}
622
623/**
624 * \brief Stop a queue
625 * @param netdev network device
626 * @param q which queue to stop
627 */
628static inline void stop_q(struct net_device *netdev, int q)
629{
630 if (netif_is_multiqueue(netdev))
631 netif_stop_subqueue(netdev, q);
632 else
633 netif_stop_queue(netdev);
634}
635
636/**
637 * \brief Check Tx queue status, and take appropriate action
638 * @param lio per-network private data
639 * @returns 0 if full, number of queues woken up otherwise
640 */
641static inline int check_txq_status(struct lio *lio)
642{
643 int ret_val = 0;
644
645 if (netif_is_multiqueue(lio->netdev)) {
646 int numqs = lio->netdev->num_tx_queues;
647 int q, iq = 0;
648
649 /* check each sub-queue state */
650 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700651 iq = lio->linfo.txpciq[q %
652 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700653 if (octnet_iq_is_full(lio->oct_dev, iq))
654 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700655 if (__netif_subqueue_stopped(lio->netdev, q)) {
656 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700657 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
658 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700659 ret_val++;
660 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700661 }
662 } else {
663 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
664 return 0;
665 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700666 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
667 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700668 ret_val = 1;
669 }
670 return ret_val;
671}
672
673/**
674 * Remove the node at the head of the list. The list would be empty at
675 * the end of this call if there are no more nodes in the list.
676 */
677static inline struct list_head *list_delete_head(struct list_head *root)
678{
679 struct list_head *node;
680
681 if ((root->prev == root) && (root->next == root))
682 node = NULL;
683 else
684 node = root->next;
685
686 if (node)
687 list_del(node);
688
689 return node;
690}
691
692/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700693 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700694 * @param lio per-network private data
695 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700696static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700697{
698 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700699 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700700
VSR Burru67e303e2017-03-06 18:45:59 -0800701 kfree(lio->glist_lock);
702 lio->glist_lock = NULL;
703
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700704 if (!lio->glist)
705 return;
706
707 for (i = 0; i < lio->linfo.num_txpciq; i++) {
708 do {
709 g = (struct octnic_gather *)
710 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800711 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700712 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700713 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800714
Felix Manlunas58ad3192017-03-20 19:04:48 -0700715 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
716 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800717 lio_dma_free(lio->oct_dev,
718 lio->glist_entry_size * lio->tx_qsize,
719 lio->glists_virt_base[i],
720 lio->glists_dma_base[i]);
721 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700722 }
723
VSR Burru67e303e2017-03-06 18:45:59 -0800724 kfree(lio->glists_virt_base);
725 lio->glists_virt_base = NULL;
726
727 kfree(lio->glists_dma_base);
728 lio->glists_dma_base = NULL;
729
730 kfree(lio->glist);
731 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700732}
733
734/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700735 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700736 * @param lio per-network private data
737 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700738static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700739{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700740 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700741 struct octnic_gather *g;
742
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700743 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
744 GFP_KERNEL);
745 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800746 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700747
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700748 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
749 GFP_KERNEL);
750 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800751 kfree(lio->glist_lock);
752 lio->glist_lock = NULL;
753 return -ENOMEM;
754 }
755
756 lio->glist_entry_size =
757 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
758
759 /* allocate memory to store virtual and dma base address of
760 * per glist consistent memory
761 */
762 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
763 GFP_KERNEL);
764 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
765 GFP_KERNEL);
766
767 if (!lio->glists_virt_base || !lio->glists_dma_base) {
768 delete_glists(lio);
769 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700770 }
771
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700772 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800773 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700774
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700775 spin_lock_init(&lio->glist_lock[i]);
776
777 INIT_LIST_HEAD(&lio->glist[i]);
778
VSR Burru67e303e2017-03-06 18:45:59 -0800779 lio->glists_virt_base[i] =
780 lio_dma_alloc(oct,
781 lio->glist_entry_size * lio->tx_qsize,
782 &lio->glists_dma_base[i]);
783
784 if (!lio->glists_virt_base[i]) {
785 delete_glists(lio);
786 return -ENOMEM;
787 }
788
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700789 for (j = 0; j < lio->tx_qsize; j++) {
790 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
791 numa_node);
792 if (!g)
793 g = kzalloc(sizeof(*g), GFP_KERNEL);
794 if (!g)
795 break;
796
VSR Burru67e303e2017-03-06 18:45:59 -0800797 g->sg = lio->glists_virt_base[i] +
798 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700799
VSR Burru67e303e2017-03-06 18:45:59 -0800800 g->sg_dma_ptr = lio->glists_dma_base[i] +
801 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700802
803 list_add_tail(&g->list, &lio->glist[i]);
804 }
805
806 if (j != lio->tx_qsize) {
807 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800808 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700809 }
810 }
811
812 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700813}
814
815/**
816 * \brief Print link information
817 * @param netdev network device
818 */
819static void print_link_info(struct net_device *netdev)
820{
821 struct lio *lio = GET_LIO(netdev);
822
823 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
824 struct oct_link_info *linfo = &lio->linfo;
825
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700826 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700827 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
828 linfo->link.s.speed,
829 (linfo->link.s.duplex) ? "Full" : "Half");
830 } else {
831 netif_info(lio, link, lio->netdev, "Link Down\n");
832 }
833 }
834}
835
836/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700837 * \brief Routine to notify MTU change
838 * @param work work_struct data structure
839 */
840static void octnet_link_status_change(struct work_struct *work)
841{
842 struct cavium_wk *wk = (struct cavium_wk *)work;
843 struct lio *lio = (struct lio *)wk->ctxptr;
844
845 rtnl_lock();
846 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
847 rtnl_unlock();
848}
849
850/**
851 * \brief Sets up the mtu status change work
852 * @param netdev network device
853 */
854static inline int setup_link_status_change_wq(struct net_device *netdev)
855{
856 struct lio *lio = GET_LIO(netdev);
857 struct octeon_device *oct = lio->oct_dev;
858
859 lio->link_status_wq.wq = alloc_workqueue("link-status",
860 WQ_MEM_RECLAIM, 0);
861 if (!lio->link_status_wq.wq) {
862 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
863 return -1;
864 }
865 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
866 octnet_link_status_change);
867 lio->link_status_wq.wk.ctxptr = lio;
868
869 return 0;
870}
871
872static inline void cleanup_link_status_change_wq(struct net_device *netdev)
873{
874 struct lio *lio = GET_LIO(netdev);
875
876 if (lio->link_status_wq.wq) {
877 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
878 destroy_workqueue(lio->link_status_wq.wq);
879 }
880}
881
882/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700883 * \brief Update link status
884 * @param netdev network device
885 * @param ls link status structure
886 *
887 * Called on receipt of a link status response from the core application to
888 * update each interface's link status.
889 */
890static inline void update_link_status(struct net_device *netdev,
891 union oct_link_status *ls)
892{
893 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700894 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700895
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700896 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700897
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700898 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700899 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700900 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700901
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700902 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700903 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700904 txqs_wake(netdev);
905 } else {
906 netif_carrier_off(netdev);
907 stop_txq(netdev);
908 }
909 }
910}
911
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700912/* Runs in interrupt context. */
913static void update_txq_status(struct octeon_device *oct, int iq_num)
914{
915 struct net_device *netdev;
916 struct lio *lio;
917 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
918
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700919 netdev = oct->props[iq->ifidx].netdev;
920
921 /* This is needed because the first IQ does not have
922 * a netdev associated with it.
923 */
924 if (!netdev)
925 return;
926
927 lio = GET_LIO(netdev);
928 if (netif_is_multiqueue(netdev)) {
929 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
930 lio->linfo.link.s.link_up &&
931 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700932 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
933 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700934 netif_wake_subqueue(netdev, iq->q_index);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700935 }
VSR Burru6069f3f2017-03-22 11:54:50 -0700936 } else if (netif_queue_stopped(netdev) &&
937 lio->linfo.link.s.link_up &&
938 (!octnet_iq_is_full(oct, lio->txq))) {
939 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
940 lio->txq, tx_restart, 1);
941 netif_wake_queue(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700942 }
943}
944
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700945static
946int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
947{
948 struct octeon_device *oct = droq->oct_dev;
949 struct octeon_device_priv *oct_priv =
950 (struct octeon_device_priv *)oct->priv;
951
952 if (droq->ops.poll_mode) {
953 droq->ops.napi_fn(droq);
954 } else {
955 if (ret & MSIX_PO_INT) {
956 tasklet_schedule(&oct_priv->droq_tasklet);
957 return 1;
958 }
959 /* this will be flushed periodically by check iq db */
960 if (ret & MSIX_PI_INT)
961 return 0;
962 }
963 return 0;
964}
965
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700966/**
967 * \brief Droq packet processor sceduler
968 * @param oct octeon device
969 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700970static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700971{
972 struct octeon_device_priv *oct_priv =
973 (struct octeon_device_priv *)oct->priv;
974 u64 oq_no;
975 struct octeon_droq *droq;
976
977 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700978 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
979 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800980 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700981 continue;
982
983 droq = oct->droq[oq_no];
984
985 if (droq->ops.poll_mode) {
986 droq->ops.napi_fn(droq);
987 oct_priv->napi_mask |= (1 << oq_no);
988 } else {
989 tasklet_schedule(&oct_priv->droq_tasklet);
990 }
991 }
992 }
993}
994
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700995static irqreturn_t
996liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
997{
998 u64 ret;
999 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1000 struct octeon_device *oct = ioq_vector->oct_dev;
1001 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1002
1003 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1004
1005 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1006 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1007
1008 return IRQ_HANDLED;
1009}
1010
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001011/**
1012 * \brief Interrupt handler for octeon
1013 * @param irq unused
1014 * @param dev octeon device
1015 */
1016static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001017irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1018 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001019{
1020 struct octeon_device *oct = (struct octeon_device *)dev;
1021 irqreturn_t ret;
1022
1023 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001024 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001025
1026 ret = oct->fn_list.process_interrupt_regs(oct);
1027
1028 if (ret == IRQ_HANDLED)
1029 liquidio_schedule_droq_pkt_handlers(oct);
1030
1031 /* Re-enable our interrupts */
1032 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001033 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001034
1035 return ret;
1036}
1037
1038/**
1039 * \brief Setup interrupt for octeon device
1040 * @param oct octeon device
1041 *
1042 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1043 */
1044static int octeon_setup_interrupt(struct octeon_device *oct)
1045{
1046 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001047 struct msix_entry *msix_entries;
1048 int i;
1049 int num_ioq_vectors;
1050 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001051 char *queue_irq_names = NULL;
1052 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001053
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001054 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1055 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1056 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1057 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001058
Rick Farrington0c88a762017-03-13 12:58:04 -07001059 /* allocate storage for the names assigned to each irq */
1060 oct->irq_name_storage =
1061 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1062 GFP_KERNEL);
1063 if (!oct->irq_name_storage) {
1064 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1065 return -ENOMEM;
1066 }
1067
1068 queue_irq_names = oct->irq_name_storage;
1069 aux_irq_name = &queue_irq_names
1070 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1071
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001072 oct->msix_entries = kcalloc(
1073 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001074 if (!oct->msix_entries) {
1075 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1076 kfree(oct->irq_name_storage);
1077 oct->irq_name_storage = NULL;
1078 return -ENOMEM;
1079 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001080
1081 msix_entries = (struct msix_entry *)oct->msix_entries;
1082 /*Assumption is that pf msix vectors start from pf srn to pf to
1083 * trs and not from 0. if not change this code
1084 */
1085 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1086 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1087 msix_entries[oct->num_msix_irqs - 1].entry =
1088 oct->sriov_info.trs;
1089 num_alloc_ioq_vectors = pci_enable_msix_range(
1090 oct->pci_dev, msix_entries,
1091 oct->num_msix_irqs,
1092 oct->num_msix_irqs);
1093 if (num_alloc_ioq_vectors < 0) {
1094 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1095 kfree(oct->msix_entries);
1096 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001097 kfree(oct->irq_name_storage);
1098 oct->irq_name_storage = NULL;
1099 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001100 }
1101 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1102
1103 num_ioq_vectors = oct->num_msix_irqs;
1104
1105 /** For PF, there is one non-ioq interrupt handler */
1106 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001107
1108 snprintf(aux_irq_name, INTRNAMSIZ,
1109 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001110 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001111 liquidio_legacy_intr_handler, 0,
1112 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001113 if (irqret) {
1114 dev_err(&oct->pci_dev->dev,
1115 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1116 irqret);
1117 pci_disable_msix(oct->pci_dev);
1118 kfree(oct->msix_entries);
1119 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001120 kfree(oct->irq_name_storage);
1121 oct->irq_name_storage = NULL;
1122 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001123 }
1124
1125 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001126 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1127 "LiquidIO%u-pf%u-rxtx-%u",
1128 oct->octeon_id, oct->pf_num, i);
1129
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001130 irqret = request_irq(msix_entries[i].vector,
1131 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001132 &queue_irq_names[IRQ_NAME_OFF(i)],
1133 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001134 if (irqret) {
1135 dev_err(&oct->pci_dev->dev,
1136 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1137 irqret);
1138 /** Freeing the non-ioq irq vector here . */
1139 free_irq(msix_entries[num_ioq_vectors].vector,
1140 oct);
1141
1142 while (i) {
1143 i--;
1144 /** clearing affinity mask. */
1145 irq_set_affinity_hint(
1146 msix_entries[i].vector, NULL);
1147 free_irq(msix_entries[i].vector,
1148 &oct->ioq_vector[i]);
1149 }
1150 pci_disable_msix(oct->pci_dev);
1151 kfree(oct->msix_entries);
1152 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001153 kfree(oct->irq_name_storage);
1154 oct->irq_name_storage = NULL;
1155 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001156 }
1157 oct->ioq_vector[i].vector = msix_entries[i].vector;
1158 /* assign the cpu mask for this msix interrupt vector */
1159 irq_set_affinity_hint(
1160 msix_entries[i].vector,
1161 (&oct->ioq_vector[i].affinity_mask));
1162 }
1163 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1164 oct->octeon_id);
1165 } else {
1166 err = pci_enable_msi(oct->pci_dev);
1167 if (err)
1168 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1169 err);
1170 else
1171 oct->flags |= LIO_FLAG_MSI_ENABLED;
1172
Rick Farrington0c88a762017-03-13 12:58:04 -07001173 /* allocate storage for the names assigned to the irq */
1174 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1175 if (!oct->irq_name_storage)
1176 return -ENOMEM;
1177
1178 queue_irq_names = oct->irq_name_storage;
1179
1180 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1181 "LiquidIO%u-pf%u-rxtx-%u",
1182 oct->octeon_id, oct->pf_num, 0);
1183
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001184 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001185 liquidio_legacy_intr_handler,
1186 IRQF_SHARED,
1187 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001188 if (irqret) {
1189 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1190 pci_disable_msi(oct->pci_dev);
1191 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1192 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001193 kfree(oct->irq_name_storage);
1194 oct->irq_name_storage = NULL;
1195 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001196 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001197 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001198 return 0;
1199}
1200
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001201static int liquidio_watchdog(void *param)
1202{
1203 u64 wdog;
1204 u16 mask_of_stuck_cores = 0;
1205 u16 mask_of_crashed_cores = 0;
1206 int core_num;
1207 u8 core_is_stuck[LIO_MAX_CORES];
1208 u8 core_crashed[LIO_MAX_CORES];
1209 struct octeon_device *oct = param;
1210
1211 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1212 memset(core_crashed, 0, sizeof(core_crashed));
1213
1214 while (!kthread_should_stop()) {
1215 mask_of_crashed_cores =
1216 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1217
1218 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1219 if (!core_is_stuck[core_num]) {
1220 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1221
1222 /* look at watchdog state field */
1223 wdog &= CIU3_WDOG_MASK;
1224 if (wdog) {
1225 /* this watchdog timer has expired */
1226 core_is_stuck[core_num] =
1227 LIO_MONITOR_WDOG_EXPIRE;
1228 mask_of_stuck_cores |= (1 << core_num);
1229 }
1230 }
1231
1232 if (!core_crashed[core_num])
1233 core_crashed[core_num] =
1234 (mask_of_crashed_cores >> core_num) & 1;
1235 }
1236
1237 if (mask_of_stuck_cores) {
1238 for (core_num = 0; core_num < LIO_MAX_CORES;
1239 core_num++) {
1240 if (core_is_stuck[core_num] == 1) {
1241 dev_err(&oct->pci_dev->dev,
1242 "ERROR: Octeon core %d is stuck!\n",
1243 core_num);
1244 /* 2 means we have printk'd an error
1245 * so no need to repeat the same printk
1246 */
1247 core_is_stuck[core_num] =
1248 LIO_MONITOR_CORE_STUCK_MSGD;
1249 }
1250 }
1251 }
1252
1253 if (mask_of_crashed_cores) {
1254 for (core_num = 0; core_num < LIO_MAX_CORES;
1255 core_num++) {
1256 if (core_crashed[core_num] == 1) {
1257 dev_err(&oct->pci_dev->dev,
1258 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1259 core_num);
1260 /* 2 means we have printk'd an error
1261 * so no need to repeat the same printk
1262 */
1263 core_crashed[core_num] =
1264 LIO_MONITOR_CORE_STUCK_MSGD;
1265 }
1266 }
1267 }
1268#ifdef CONFIG_MODULE_UNLOAD
1269 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1270 /* make module refcount=0 so that rmmod will work */
1271 long refcount;
1272
1273 refcount = module_refcount(THIS_MODULE);
1274
1275 while (refcount > 0) {
1276 module_put(THIS_MODULE);
1277 refcount = module_refcount(THIS_MODULE);
1278 }
1279
1280 /* compensate for and withstand an unlikely (but still
1281 * possible) race condition
1282 */
1283 while (refcount < 0) {
1284 try_module_get(THIS_MODULE);
1285 refcount = module_refcount(THIS_MODULE);
1286 }
1287 }
1288#endif
1289 /* sleep for two seconds */
1290 set_current_state(TASK_INTERRUPTIBLE);
1291 schedule_timeout(2 * HZ);
1292 }
1293
1294 return 0;
1295}
1296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001297/**
1298 * \brief PCI probe handler
1299 * @param pdev PCI device structure
1300 * @param ent unused
1301 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001302static int
1303liquidio_probe(struct pci_dev *pdev,
1304 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001305{
1306 struct octeon_device *oct_dev = NULL;
1307 struct handshake *hs;
1308
1309 oct_dev = octeon_allocate_device(pdev->device,
1310 sizeof(struct octeon_device_priv));
1311 if (!oct_dev) {
1312 dev_err(&pdev->dev, "Unable to allocate device\n");
1313 return -ENOMEM;
1314 }
1315
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001316 if (pdev->device == OCTEON_CN23XX_PF_VID)
1317 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001319 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1320 (u32)pdev->vendor, (u32)pdev->device);
1321
1322 /* Assign octeon_device for this device to the private data area. */
1323 pci_set_drvdata(pdev, oct_dev);
1324
1325 /* set linux specific device pointer */
1326 oct_dev->pci_dev = (void *)pdev;
1327
1328 hs = &handshake[oct_dev->octeon_id];
1329 init_completion(&hs->init);
1330 init_completion(&hs->started);
1331 hs->pci_dev = pdev;
1332
1333 if (oct_dev->octeon_id == 0)
1334 /* first LiquidIO NIC is detected */
1335 complete(&first_stage);
1336
1337 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001338 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001339 liquidio_remove(pdev);
1340 return -ENOMEM;
1341 }
1342
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001343 if (OCTEON_CN23XX_PF(oct_dev)) {
1344 u64 scratch1;
1345 u8 bus, device, function;
1346
1347 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1348 if (!(scratch1 & 4ULL)) {
1349 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1350 * the lio watchdog kernel thread is running for this
1351 * NIC. Each NIC gets one watchdog kernel thread.
1352 */
1353 scratch1 |= 4ULL;
1354 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1355 scratch1);
1356
1357 bus = pdev->bus->number;
1358 device = PCI_SLOT(pdev->devfn);
1359 function = PCI_FUNC(pdev->devfn);
1360 oct_dev->watchdog_task = kthread_create(
1361 liquidio_watchdog, oct_dev,
1362 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001363 if (!IS_ERR(oct_dev->watchdog_task)) {
1364 wake_up_process(oct_dev->watchdog_task);
1365 } else {
1366 oct_dev->watchdog_task = NULL;
1367 dev_err(&oct_dev->pci_dev->dev,
1368 "failed to create kernel_thread\n");
1369 liquidio_remove(pdev);
1370 return -1;
1371 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001372 }
1373 }
1374
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001375 oct_dev->rx_pause = 1;
1376 oct_dev->tx_pause = 1;
1377
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001378 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1379
1380 return 0;
1381}
1382
1383/**
1384 *\brief Destroy resources associated with octeon device
1385 * @param pdev PCI device structure
1386 * @param ent unused
1387 */
1388static void octeon_destroy_resources(struct octeon_device *oct)
1389{
1390 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001391 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001392 struct octeon_device_priv *oct_priv =
1393 (struct octeon_device_priv *)oct->priv;
1394
1395 struct handshake *hs;
1396
1397 switch (atomic_read(&oct->status)) {
1398 case OCT_DEV_RUNNING:
1399 case OCT_DEV_CORE_OK:
1400
1401 /* No more instructions will be forwarded. */
1402 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1403
1404 oct->app_mode = CVM_DRV_INVALID_APP;
1405 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1406 lio_get_state_string(&oct->status));
1407
1408 schedule_timeout_uninterruptible(HZ / 10);
1409
1410 /* fallthrough */
1411 case OCT_DEV_HOST_OK:
1412
1413 /* fallthrough */
1414 case OCT_DEV_CONSOLE_INIT_DONE:
1415 /* Remove any consoles */
1416 octeon_remove_consoles(oct);
1417
1418 /* fallthrough */
1419 case OCT_DEV_IO_QUEUES_DONE:
1420 if (wait_for_pending_requests(oct))
1421 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1422
1423 if (lio_wait_for_instr_fetch(oct))
1424 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1425
1426 /* Disable the input and output queues now. No more packets will
1427 * arrive from Octeon, but we should wait for all packet
1428 * processing to finish.
1429 */
1430 oct->fn_list.disable_io_queues(oct);
1431
1432 if (lio_wait_for_oq_pkts(oct))
1433 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1434
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001435 /* fallthrough */
1436 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001437 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001438 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001439
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001440 if (oct->msix_on) {
1441 msix_entries = (struct msix_entry *)oct->msix_entries;
1442 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1443 /* clear the affinity_cpumask */
1444 irq_set_affinity_hint(msix_entries[i].vector,
1445 NULL);
1446 free_irq(msix_entries[i].vector,
1447 &oct->ioq_vector[i]);
1448 }
1449 /* non-iov vector's argument is oct struct */
1450 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001451
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001452 pci_disable_msix(oct->pci_dev);
1453 kfree(oct->msix_entries);
1454 oct->msix_entries = NULL;
1455 } else {
1456 /* Release the interrupt line */
1457 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001458
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001459 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1460 pci_disable_msi(oct->pci_dev);
1461 }
1462
Rick Farrington0c88a762017-03-13 12:58:04 -07001463 kfree(oct->irq_name_storage);
1464 oct->irq_name_storage = NULL;
1465
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001466 /* fallthrough */
1467 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001468 if (OCTEON_CN23XX_PF(oct))
1469 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001470
1471 /* fallthrough */
1472 case OCT_DEV_MBOX_SETUP_DONE:
1473 if (OCTEON_CN23XX_PF(oct))
1474 oct->fn_list.free_mbox(oct);
1475
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001476 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001477 case OCT_DEV_IN_RESET:
1478 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001479 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001480 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001481 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001482 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001483 continue;
1484 octeon_delete_droq(oct, i);
1485 }
1486
1487 /* Force any pending handshakes to complete */
1488 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1489 hs = &handshake[i];
1490
1491 if (hs->pci_dev) {
1492 handshake[oct->octeon_id].init_ok = 0;
1493 complete(&handshake[oct->octeon_id].init);
1494 handshake[oct->octeon_id].started_ok = 0;
1495 complete(&handshake[oct->octeon_id].started);
1496 }
1497 }
1498
1499 /* fallthrough */
1500 case OCT_DEV_RESP_LIST_INIT_DONE:
1501 octeon_delete_response_list(oct);
1502
1503 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001504 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001505 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001506 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001507 continue;
1508 octeon_delete_instr_queue(oct, i);
1509 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001510#ifdef CONFIG_PCI_IOV
1511 if (oct->sriov_info.sriov_enabled)
1512 pci_disable_sriov(oct->pci_dev);
1513#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001514 /* fallthrough */
1515 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1516 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001517
1518 /* fallthrough */
1519 case OCT_DEV_DISPATCH_INIT_DONE:
1520 octeon_delete_dispatch_list(oct);
1521 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1522
1523 /* fallthrough */
1524 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001525 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001526 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1527 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001528
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001529 octeon_unmap_pci_barx(oct, 0);
1530 octeon_unmap_pci_barx(oct, 1);
1531
1532 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001533 case OCT_DEV_PCI_ENABLE_DONE:
1534 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001535 /* Disable the device, releasing the PCI INT */
1536 pci_disable_device(oct->pci_dev);
1537
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001538 /* fallthrough */
1539 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001540 /* Nothing to be done here either */
1541 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001542 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001543
1544 tasklet_kill(&oct_priv->droq_tasklet);
1545}
1546
1547/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001548 * \brief Callback for rx ctrl
1549 * @param status status of request
1550 * @param buf pointer to resp structure
1551 */
1552static void rx_ctl_callback(struct octeon_device *oct,
1553 u32 status,
1554 void *buf)
1555{
1556 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1557 struct liquidio_rx_ctl_context *ctx;
1558
1559 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1560
1561 oct = lio_get_device(ctx->octeon_id);
1562 if (status)
1563 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1564 CVM_CAST64(status));
1565 WRITE_ONCE(ctx->cond, 1);
1566
1567 /* This barrier is required to be sure that the response has been
1568 * written fully before waking up the handler
1569 */
1570 wmb();
1571
1572 wake_up_interruptible(&ctx->wc);
1573}
1574
1575/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001576 * \brief Send Rx control command
1577 * @param lio per-network private data
1578 * @param start_stop whether to start or stop
1579 */
1580static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1581{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001582 struct octeon_soft_command *sc;
1583 struct liquidio_rx_ctl_context *ctx;
1584 union octnet_cmd *ncmd;
1585 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1586 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1587 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001588
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001589 if (oct->props[lio->ifidx].rx_on == start_stop)
1590 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001591
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001592 sc = (struct octeon_soft_command *)
1593 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1594 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001595
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001596 ncmd = (union octnet_cmd *)sc->virtdptr;
1597 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1598
1599 WRITE_ONCE(ctx->cond, 0);
1600 ctx->octeon_id = lio_get_device_id(oct);
1601 init_waitqueue_head(&ctx->wc);
1602
1603 ncmd->u64 = 0;
1604 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1605 ncmd->s.param1 = start_stop;
1606
1607 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1608
1609 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1610
1611 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1612 OPCODE_NIC_CMD, 0, 0, 0);
1613
1614 sc->callback = rx_ctl_callback;
1615 sc->callback_arg = sc;
1616 sc->wait_time = 5000;
1617
1618 retval = octeon_send_soft_command(oct, sc);
1619 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001620 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001621 } else {
1622 /* Sleep on a wait queue till the cond flag indicates that the
1623 * response arrived or timed-out.
1624 */
1625 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1626 return;
1627 oct->props[lio->ifidx].rx_on = start_stop;
1628 }
1629
1630 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001631}
1632
1633/**
1634 * \brief Destroy NIC device interface
1635 * @param oct octeon device
1636 * @param ifidx which interface to destroy
1637 *
1638 * Cleanup associated with each interface for an Octeon device when NIC
1639 * module is being unloaded or if initialization fails during load.
1640 */
1641static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1642{
1643 struct net_device *netdev = oct->props[ifidx].netdev;
1644 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001645 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001646
1647 if (!netdev) {
1648 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1649 __func__, ifidx);
1650 return;
1651 }
1652
1653 lio = GET_LIO(netdev);
1654
1655 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1656
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001657 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001658 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001659
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001660 if (oct->props[lio->ifidx].napi_enabled == 1) {
1661 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1662 napi_disable(napi);
1663
1664 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001665
1666 if (OCTEON_CN23XX_PF(oct))
1667 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001668 }
1669
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001670 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1671 unregister_netdev(netdev);
1672
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001673 cleanup_link_status_change_wq(netdev);
1674
Satanand Burla031d4f12017-03-22 11:31:13 -07001675 cleanup_rx_oom_poll_fn(netdev);
1676
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001677 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001678
1679 free_netdev(netdev);
1680
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001681 oct->props[ifidx].gmxport = -1;
1682
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001683 oct->props[ifidx].netdev = NULL;
1684}
1685
1686/**
1687 * \brief Stop complete NIC functionality
1688 * @param oct octeon device
1689 */
1690static int liquidio_stop_nic_module(struct octeon_device *oct)
1691{
1692 int i, j;
1693 struct lio *lio;
1694
1695 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1696 if (!oct->ifcount) {
1697 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1698 return 1;
1699 }
1700
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001701 spin_lock_bh(&oct->cmd_resp_wqlock);
1702 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1703 spin_unlock_bh(&oct->cmd_resp_wqlock);
1704
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001705 for (i = 0; i < oct->ifcount; i++) {
1706 lio = GET_LIO(oct->props[i].netdev);
1707 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001708 octeon_unregister_droq_ops(oct,
1709 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001710 }
1711
1712 for (i = 0; i < oct->ifcount; i++)
1713 liquidio_destroy_nic_device(oct, i);
1714
1715 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1716 return 0;
1717}
1718
1719/**
1720 * \brief Cleans up resources at unload time
1721 * @param pdev PCI device structure
1722 */
1723static void liquidio_remove(struct pci_dev *pdev)
1724{
1725 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1726
1727 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1728
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001729 if (oct_dev->watchdog_task)
1730 kthread_stop(oct_dev->watchdog_task);
1731
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001732 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1733 liquidio_stop_nic_module(oct_dev);
1734
1735 /* Reset the octeon device and cleanup all memory allocated for
1736 * the octeon device by driver.
1737 */
1738 octeon_destroy_resources(oct_dev);
1739
1740 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1741
1742 /* This octeon device has been removed. Update the global
1743 * data structure to reflect this. Free the device structure.
1744 */
1745 octeon_free_device_mem(oct_dev);
1746}
1747
1748/**
1749 * \brief Identify the Octeon device and to map the BAR address space
1750 * @param oct octeon device
1751 */
1752static int octeon_chip_specific_setup(struct octeon_device *oct)
1753{
1754 u32 dev_id, rev_id;
1755 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001756 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001757
1758 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1759 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1760 oct->rev_id = rev_id & 0xff;
1761
1762 switch (dev_id) {
1763 case OCTEON_CN68XX_PCIID:
1764 oct->chip_id = OCTEON_CN68XX;
1765 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001766 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001767 break;
1768
1769 case OCTEON_CN66XX_PCIID:
1770 oct->chip_id = OCTEON_CN66XX;
1771 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001772 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001773 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001774
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001775 case OCTEON_CN23XX_PCIID_PF:
1776 oct->chip_id = OCTEON_CN23XX_PF_VID;
1777 ret = setup_cn23xx_octeon_pf_device(oct);
1778 s = "CN23XX";
1779 break;
1780
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001781 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001782 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001783 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1784 dev_id);
1785 }
1786
1787 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001788 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001789 OCTEON_MAJOR_REV(oct),
1790 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001791 octeon_get_conf(oct)->card_name,
1792 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001793
1794 return ret;
1795}
1796
1797/**
1798 * \brief PCI initialization for each Octeon device.
1799 * @param oct octeon device
1800 */
1801static int octeon_pci_os_setup(struct octeon_device *oct)
1802{
1803 /* setup PCI stuff first */
1804 if (pci_enable_device(oct->pci_dev)) {
1805 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1806 return 1;
1807 }
1808
1809 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1810 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001811 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001812 return 1;
1813 }
1814
1815 /* Enable PCI DMA Master. */
1816 pci_set_master(oct->pci_dev);
1817
1818 return 0;
1819}
1820
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001821static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1822{
1823 int q = 0;
1824
1825 if (netif_is_multiqueue(lio->netdev))
1826 q = skb->queue_mapping % lio->linfo.num_txpciq;
1827
1828 return q;
1829}
1830
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001831/**
1832 * \brief Check Tx queue state for a given network buffer
1833 * @param lio per-network private data
1834 * @param skb network buffer
1835 */
1836static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1837{
1838 int q = 0, iq = 0;
1839
1840 if (netif_is_multiqueue(lio->netdev)) {
1841 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001842 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001843 } else {
1844 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001845 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001846 }
1847
1848 if (octnet_iq_is_full(lio->oct_dev, iq))
1849 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001850
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001851 if (__netif_subqueue_stopped(lio->netdev, q)) {
1852 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001853 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001854 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001855 return 1;
1856}
1857
1858/**
1859 * \brief Unmap and free network buffer
1860 * @param buf buffer
1861 */
1862static void free_netbuf(void *buf)
1863{
1864 struct sk_buff *skb;
1865 struct octnet_buf_free_info *finfo;
1866 struct lio *lio;
1867
1868 finfo = (struct octnet_buf_free_info *)buf;
1869 skb = finfo->skb;
1870 lio = finfo->lio;
1871
1872 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1873 DMA_TO_DEVICE);
1874
1875 check_txq_state(lio, skb);
1876
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001877 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001878}
1879
1880/**
1881 * \brief Unmap and free gather buffer
1882 * @param buf buffer
1883 */
1884static void free_netsgbuf(void *buf)
1885{
1886 struct octnet_buf_free_info *finfo;
1887 struct sk_buff *skb;
1888 struct lio *lio;
1889 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001890 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001891
1892 finfo = (struct octnet_buf_free_info *)buf;
1893 skb = finfo->skb;
1894 lio = finfo->lio;
1895 g = finfo->g;
1896 frags = skb_shinfo(skb)->nr_frags;
1897
1898 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1899 g->sg[0].ptr[0], (skb->len - skb->data_len),
1900 DMA_TO_DEVICE);
1901
1902 i = 1;
1903 while (frags--) {
1904 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1905
1906 pci_unmap_page((lio->oct_dev)->pci_dev,
1907 g->sg[(i >> 2)].ptr[(i & 3)],
1908 frag->size, DMA_TO_DEVICE);
1909 i++;
1910 }
1911
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001912 iq = skb_iq(lio, skb);
1913 spin_lock(&lio->glist_lock[iq]);
1914 list_add_tail(&g->list, &lio->glist[iq]);
1915 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001916
1917 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1918
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001919 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001920}
1921
1922/**
1923 * \brief Unmap and free gather buffer with response
1924 * @param buf buffer
1925 */
1926static void free_netsgbuf_with_resp(void *buf)
1927{
1928 struct octeon_soft_command *sc;
1929 struct octnet_buf_free_info *finfo;
1930 struct sk_buff *skb;
1931 struct lio *lio;
1932 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001933 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001934
1935 sc = (struct octeon_soft_command *)buf;
1936 skb = (struct sk_buff *)sc->callback_arg;
1937 finfo = (struct octnet_buf_free_info *)&skb->cb;
1938
1939 lio = finfo->lio;
1940 g = finfo->g;
1941 frags = skb_shinfo(skb)->nr_frags;
1942
1943 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1944 g->sg[0].ptr[0], (skb->len - skb->data_len),
1945 DMA_TO_DEVICE);
1946
1947 i = 1;
1948 while (frags--) {
1949 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1950
1951 pci_unmap_page((lio->oct_dev)->pci_dev,
1952 g->sg[(i >> 2)].ptr[(i & 3)],
1953 frag->size, DMA_TO_DEVICE);
1954 i++;
1955 }
1956
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001957 iq = skb_iq(lio, skb);
1958
1959 spin_lock(&lio->glist_lock[iq]);
1960 list_add_tail(&g->list, &lio->glist[iq]);
1961 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001962
1963 /* Don't free the skb yet */
1964
1965 check_txq_state(lio, skb);
1966}
1967
1968/**
1969 * \brief Adjust ptp frequency
1970 * @param ptp PTP clock info
1971 * @param ppb how much to adjust by, in parts-per-billion
1972 */
1973static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1974{
1975 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1976 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1977 u64 comp, delta;
1978 unsigned long flags;
1979 bool neg_adj = false;
1980
1981 if (ppb < 0) {
1982 neg_adj = true;
1983 ppb = -ppb;
1984 }
1985
1986 /* The hardware adds the clock compensation value to the
1987 * PTP clock on every coprocessor clock cycle, so we
1988 * compute the delta in terms of coprocessor clocks.
1989 */
1990 delta = (u64)ppb << 32;
1991 do_div(delta, oct->coproc_clock_rate);
1992
1993 spin_lock_irqsave(&lio->ptp_lock, flags);
1994 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1995 if (neg_adj)
1996 comp -= delta;
1997 else
1998 comp += delta;
1999 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2000 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2001
2002 return 0;
2003}
2004
2005/**
2006 * \brief Adjust ptp time
2007 * @param ptp PTP clock info
2008 * @param delta how much to adjust by, in nanosecs
2009 */
2010static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2011{
2012 unsigned long flags;
2013 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2014
2015 spin_lock_irqsave(&lio->ptp_lock, flags);
2016 lio->ptp_adjust += delta;
2017 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2018
2019 return 0;
2020}
2021
2022/**
2023 * \brief Get hardware clock time, including any adjustment
2024 * @param ptp PTP clock info
2025 * @param ts timespec
2026 */
2027static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2028 struct timespec64 *ts)
2029{
2030 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002031 unsigned long flags;
2032 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2033 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2034
2035 spin_lock_irqsave(&lio->ptp_lock, flags);
2036 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2037 ns += lio->ptp_adjust;
2038 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2039
Kefeng Wang286af312016-01-27 17:34:37 +08002040 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002041
2042 return 0;
2043}
2044
2045/**
2046 * \brief Set hardware clock time. Reset adjustment
2047 * @param ptp PTP clock info
2048 * @param ts timespec
2049 */
2050static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2051 const struct timespec64 *ts)
2052{
2053 u64 ns;
2054 unsigned long flags;
2055 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2056 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2057
2058 ns = timespec_to_ns(ts);
2059
2060 spin_lock_irqsave(&lio->ptp_lock, flags);
2061 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2062 lio->ptp_adjust = 0;
2063 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2064
2065 return 0;
2066}
2067
2068/**
2069 * \brief Check if PTP is enabled
2070 * @param ptp PTP clock info
2071 * @param rq request
2072 * @param on is it on
2073 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002074static int
2075liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2076 struct ptp_clock_request *rq __attribute__((unused)),
2077 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002078{
2079 return -EOPNOTSUPP;
2080}
2081
2082/**
2083 * \brief Open PTP clock source
2084 * @param netdev network device
2085 */
2086static void oct_ptp_open(struct net_device *netdev)
2087{
2088 struct lio *lio = GET_LIO(netdev);
2089 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2090
2091 spin_lock_init(&lio->ptp_lock);
2092
2093 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2094 lio->ptp_info.owner = THIS_MODULE;
2095 lio->ptp_info.max_adj = 250000000;
2096 lio->ptp_info.n_alarm = 0;
2097 lio->ptp_info.n_ext_ts = 0;
2098 lio->ptp_info.n_per_out = 0;
2099 lio->ptp_info.pps = 0;
2100 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2101 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2102 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2103 lio->ptp_info.settime64 = liquidio_ptp_settime;
2104 lio->ptp_info.enable = liquidio_ptp_enable;
2105
2106 lio->ptp_adjust = 0;
2107
2108 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2109 &oct->pci_dev->dev);
2110
2111 if (IS_ERR(lio->ptp_clock))
2112 lio->ptp_clock = NULL;
2113}
2114
2115/**
2116 * \brief Init PTP clock
2117 * @param oct octeon device
2118 */
2119static void liquidio_ptp_init(struct octeon_device *oct)
2120{
2121 u64 clock_comp, cfg;
2122
2123 clock_comp = (u64)NSEC_PER_SEC << 32;
2124 do_div(clock_comp, oct->coproc_clock_rate);
2125 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2126
2127 /* Enable */
2128 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2129 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2130}
2131
2132/**
2133 * \brief Load firmware to device
2134 * @param oct octeon device
2135 *
2136 * Maps device to firmware filename, requests firmware, and downloads it
2137 */
2138static int load_firmware(struct octeon_device *oct)
2139{
2140 int ret = 0;
2141 const struct firmware *fw;
2142 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2143 char *tmp_fw_type;
2144
2145 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2146 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2147 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2148 return ret;
2149 }
2150
2151 if (fw_type[0] == '\0')
2152 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2153 else
2154 tmp_fw_type = fw_type;
2155
2156 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2157 octeon_get_conf(oct)->card_name, tmp_fw_type,
2158 LIO_FW_NAME_SUFFIX);
2159
2160 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2161 if (ret) {
2162 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2163 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002164 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002165 return ret;
2166 }
2167
2168 ret = octeon_download_firmware(oct, fw->data, fw->size);
2169
2170 release_firmware(fw);
2171
2172 return ret;
2173}
2174
2175/**
2176 * \brief Setup output queue
2177 * @param oct octeon device
2178 * @param q_no which queue
2179 * @param num_descs how many descriptors
2180 * @param desc_size size of each descriptor
2181 * @param app_ctx application context
2182 */
2183static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2184 int desc_size, void *app_ctx)
2185{
2186 int ret_val = 0;
2187
2188 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2189 /* droq creation and local register settings. */
2190 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302191 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002192 return ret_val;
2193
2194 if (ret_val == 1) {
2195 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2196 return 0;
2197 }
2198 /* tasklet creation for the droq */
2199
2200 /* Enable the droq queues */
2201 octeon_set_droq_pkt_op(oct, q_no, 1);
2202
2203 /* Send Credit for Octeon Output queues. Credits are always
2204 * sent after the output queue is enabled.
2205 */
2206 writel(oct->droq[q_no]->max_count,
2207 oct->droq[q_no]->pkts_credit_reg);
2208
2209 return ret_val;
2210}
2211
2212/**
2213 * \brief Callback for getting interface configuration
2214 * @param status status of request
2215 * @param buf pointer to resp structure
2216 */
2217static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002218 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002219 void *buf)
2220{
2221 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2222 struct liquidio_if_cfg_resp *resp;
2223 struct liquidio_if_cfg_context *ctx;
2224
2225 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002226 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002227
2228 oct = lio_get_device(ctx->octeon_id);
2229 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002230 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2231 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002232 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002233
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002234 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2235 resp->cfg_info.liquidio_firmware_version);
2236
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002237 /* This barrier is required to be sure that the response has been
2238 * written fully before waking up the handler
2239 */
2240 wmb();
2241
2242 wake_up_interruptible(&ctx->wc);
2243}
2244
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002245/** Routine to push packets arriving on Octeon interface upto network layer.
2246 * @param oct_id - octeon device id.
2247 * @param skbuff - skbuff struct to be passed to network layer.
2248 * @param len - size of total data received.
2249 * @param rh - Control header associated with the packet
2250 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002251 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002252 */
2253static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002254liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002255 void *skbuff,
2256 u32 len,
2257 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002258 void *param,
2259 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002260{
2261 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002262 struct sk_buff *skb = (struct sk_buff *)skbuff;
2263 struct skb_shared_hwtstamps *shhwtstamps;
2264 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002265 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002266 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002267 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002268 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2269 napi);
2270 if (netdev) {
2271 int packet_was_received;
2272 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002273 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002274
2275 /* Do not proceed if the interface is not in RUNNING state. */
2276 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2277 recv_buffer_free(skb);
2278 droq->stats.rx_dropped++;
2279 return;
2280 }
2281
2282 skb->dev = netdev;
2283
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002284 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002285 if (likely(len > MIN_SKB_SIZE)) {
2286 struct octeon_skb_page_info *pg_info;
2287 unsigned char *va;
2288
2289 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2290 if (pg_info->page) {
2291 /* For Paged allocation use the frags */
2292 va = page_address(pg_info->page) +
2293 pg_info->page_offset;
2294 memcpy(skb->data, va, MIN_SKB_SIZE);
2295 skb_put(skb, MIN_SKB_SIZE);
2296 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2297 pg_info->page,
2298 pg_info->page_offset +
2299 MIN_SKB_SIZE,
2300 len - MIN_SKB_SIZE,
2301 LIO_RXBUFFER_SZ);
2302 }
2303 } else {
2304 struct octeon_skb_page_info *pg_info =
2305 ((struct octeon_skb_page_info *)(skb->cb));
2306 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2307 + pg_info->page_offset, len);
2308 skb_put(skb, len);
2309 put_page(pg_info->page);
2310 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002311
Prasad Kannegantide28c992017-01-09 14:42:40 -08002312 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2313
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002314 if (((oct->chip_id == OCTEON_CN66XX) ||
2315 (oct->chip_id == OCTEON_CN68XX)) &&
2316 ptp_enable) {
2317 if (rh->r_dh.has_hwtstamp) {
2318 /* timestamp is included from the hardware at
2319 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002320 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002321 if (ifstate_check
2322 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2323 /* Nanoseconds are in the first 64-bits
2324 * of the packet.
2325 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002326 memcpy(&ns, (skb->data + r_dh_off),
2327 sizeof(ns));
2328 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002329 shhwtstamps = skb_hwtstamps(skb);
2330 shhwtstamps->hwtstamp =
2331 ns_to_ktime(ns +
2332 lio->ptp_adjust);
2333 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002334 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002335 }
2336
Prasad Kannegantide28c992017-01-09 14:42:40 -08002337 if (rh->r_dh.has_hash) {
2338 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2339 u32 hash = be32_to_cpu(*hash_be);
2340
2341 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2342 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2343 }
2344
2345 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2346
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002347 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002348 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002349 (((rh->r_dh.encap_on) &&
2350 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2351 (!(rh->r_dh.encap_on) &&
2352 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002353 /* checksum has already been verified */
2354 skb->ip_summed = CHECKSUM_UNNECESSARY;
2355 else
2356 skb->ip_summed = CHECKSUM_NONE;
2357
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002358 /* Setting Encapsulation field on basis of status received
2359 * from the firmware
2360 */
2361 if (rh->r_dh.encap_on) {
2362 skb->encapsulation = 1;
2363 skb->csum_level = 1;
2364 droq->stats.rx_vxlan++;
2365 }
2366
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002367 /* inbound VLAN tag */
2368 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2369 (rh->r_dh.vlan != 0)) {
2370 u16 vid = rh->r_dh.vlan;
2371 u16 priority = rh->r_dh.priority;
2372
2373 vtag = priority << 13 | vid;
2374 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2375 }
2376
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002377 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2378
2379 if (packet_was_received) {
2380 droq->stats.rx_bytes_received += len;
2381 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002382 } else {
2383 droq->stats.rx_dropped++;
2384 netif_info(lio, rx_err, lio->netdev,
2385 "droq:%d error rx_dropped:%llu\n",
2386 droq->q_no, droq->stats.rx_dropped);
2387 }
2388
2389 } else {
2390 recv_buffer_free(skb);
2391 }
2392}
2393
2394/**
2395 * \brief wrapper for calling napi_schedule
2396 * @param param parameters to pass to napi_schedule
2397 *
2398 * Used when scheduling on different CPUs
2399 */
2400static void napi_schedule_wrapper(void *param)
2401{
2402 struct napi_struct *napi = param;
2403
2404 napi_schedule(napi);
2405}
2406
2407/**
2408 * \brief callback when receive interrupt occurs and we are in NAPI mode
2409 * @param arg pointer to octeon output queue
2410 */
2411static void liquidio_napi_drv_callback(void *arg)
2412{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002413 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002414 struct octeon_droq *droq = arg;
2415 int this_cpu = smp_processor_id();
2416
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002417 oct = droq->oct_dev;
2418
2419 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2420 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002421 } else {
2422 struct call_single_data *csd = &droq->csd;
2423
2424 csd->func = napi_schedule_wrapper;
2425 csd->info = &droq->napi;
2426 csd->flags = 0;
2427
2428 smp_call_function_single_async(droq->cpu_id, csd);
2429 }
2430}
2431
2432/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002433 * \brief Entry point for NAPI polling
2434 * @param napi NAPI structure
2435 * @param budget maximum number of items to process
2436 */
2437static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2438{
2439 struct octeon_droq *droq;
2440 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002441 int tx_done = 0, iq_no;
2442 struct octeon_instr_queue *iq;
2443 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002444
2445 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002446 oct = droq->oct_dev;
2447 iq_no = droq->q_no;
2448 /* Handle Droq descriptors */
2449 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2450 POLL_EVENT_PROCESS_PKTS,
2451 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002452
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002453 /* Flush the instruction queue */
2454 iq = oct->instr_queue[iq_no];
2455 if (iq) {
VSR Burru6069f3f2017-03-22 11:54:50 -07002456 if (atomic_read(&iq->instr_pending))
2457 /* Process iq buffers with in the budget limits */
2458 tx_done = octeon_flush_iq(oct, iq, budget);
2459 else
2460 tx_done = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002461 /* Update iq read-index rather than waiting for next interrupt.
2462 * Return back if tx_done is false.
2463 */
2464 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002465 } else {
2466 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2467 __func__, iq_no);
2468 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002469
Satanand Burlacdb478e2017-01-31 13:04:42 -08002470 /* force enable interrupt if reg cnts are high to avoid wraparound */
2471 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002472 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002473 (droq->pkt_count >= MAX_REG_CNT)) {
2474 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002475 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002476 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2477 POLL_EVENT_ENABLE_INTR, 0);
2478 return 0;
2479 }
2480
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002481 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002482}
2483
2484/**
2485 * \brief Setup input and output queues
2486 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002487 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002488 *
2489 * Note: Queues are with respect to the octeon device. Thus
2490 * an input queue is for egress packets, and output queues
2491 * are for ingress packets.
2492 */
2493static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002494 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002495{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002496 struct octeon_droq_ops droq_ops;
2497 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002498 static int cpu_id;
2499 static int cpu_id_modulus;
2500 struct octeon_droq *droq;
2501 struct napi_struct *napi;
2502 int q, q_no, retval = 0;
2503 struct lio *lio;
2504 int num_tx_descs;
2505
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002506 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002507
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002508 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002509
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002510 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2511
2512 droq_ops.fptr = liquidio_push_packet;
2513 droq_ops.farg = (void *)netdev;
2514
2515 droq_ops.poll_mode = 1;
2516 droq_ops.napi_fn = liquidio_napi_drv_callback;
2517 cpu_id = 0;
2518 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002519
2520 /* set up DROQs. */
2521 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002522 q_no = lio->linfo.rxpciq[q].s.q_no;
2523 dev_dbg(&octeon_dev->pci_dev->dev,
2524 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2525 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002526 retval = octeon_setup_droq(octeon_dev, q_no,
2527 CFG_GET_NUM_RX_DESCS_NIC_IF
2528 (octeon_get_conf(octeon_dev),
2529 lio->ifidx),
2530 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2531 (octeon_get_conf(octeon_dev),
2532 lio->ifidx), NULL);
2533 if (retval) {
2534 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002535 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002536 __func__);
2537 return 1;
2538 }
2539
2540 droq = octeon_dev->droq[q_no];
2541 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002542 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2543 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002544 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002545
2546 /* designate a CPU for this droq */
2547 droq->cpu_id = cpu_id;
2548 cpu_id++;
2549 if (cpu_id >= cpu_id_modulus)
2550 cpu_id = 0;
2551
2552 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2553 }
2554
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002555 if (OCTEON_CN23XX_PF(octeon_dev)) {
2556 /* 23XX PF can receive control messages (via the first PF-owned
2557 * droq) from the firmware even if the ethX interface is down,
2558 * so that's why poll_mode must be off for the first droq.
2559 */
2560 octeon_dev->droq[0]->ops.poll_mode = 0;
2561 }
2562
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002563 /* set up IQs. */
2564 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2565 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2566 (octeon_dev),
2567 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002568 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2569 lio->linfo.txpciq[q], num_tx_descs,
2570 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002571 if (retval) {
2572 dev_err(&octeon_dev->pci_dev->dev,
2573 " %s : Runtime IQ(TxQ) creation failed.\n",
2574 __func__);
2575 return 1;
2576 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002577
2578 if (octeon_dev->ioq_vector) {
2579 struct octeon_ioq_vector *ioq_vector;
2580
2581 ioq_vector = &octeon_dev->ioq_vector[q];
2582 netif_set_xps_queue(netdev,
2583 &ioq_vector->affinity_mask,
2584 ioq_vector->iq_index);
2585 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002586 }
2587
2588 return 0;
2589}
2590
2591/**
2592 * \brief Poll routine for checking transmit queue status
2593 * @param work work_struct data structure
2594 */
2595static void octnet_poll_check_txq_status(struct work_struct *work)
2596{
2597 struct cavium_wk *wk = (struct cavium_wk *)work;
2598 struct lio *lio = (struct lio *)wk->ctxptr;
2599
2600 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2601 return;
2602
2603 check_txq_status(lio);
2604 queue_delayed_work(lio->txq_status_wq.wq,
2605 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2606}
2607
2608/**
2609 * \brief Sets up the txq poll check
2610 * @param netdev network device
2611 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002612static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002613{
2614 struct lio *lio = GET_LIO(netdev);
2615 struct octeon_device *oct = lio->oct_dev;
2616
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302617 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2618 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002619 if (!lio->txq_status_wq.wq) {
2620 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002621 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002622 }
2623 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2624 octnet_poll_check_txq_status);
2625 lio->txq_status_wq.wk.ctxptr = lio;
2626 queue_delayed_work(lio->txq_status_wq.wq,
2627 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002628 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002629}
2630
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002631static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2632{
2633 struct lio *lio = GET_LIO(netdev);
2634
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002635 if (lio->txq_status_wq.wq) {
2636 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2637 destroy_workqueue(lio->txq_status_wq.wq);
2638 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002639}
2640
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002641/**
2642 * \brief Net device open for LiquidIO
2643 * @param netdev network device
2644 */
2645static int liquidio_open(struct net_device *netdev)
2646{
2647 struct lio *lio = GET_LIO(netdev);
2648 struct octeon_device *oct = lio->oct_dev;
2649 struct napi_struct *napi, *n;
2650
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002651 if (oct->props[lio->ifidx].napi_enabled == 0) {
2652 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2653 napi_enable(napi);
2654
2655 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002656
2657 if (OCTEON_CN23XX_PF(oct))
2658 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002659 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002660
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002661 if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
2662 ptp_enable)
2663 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002664
2665 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002666
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002667 /* Ready for link status updates */
2668 lio->intf_open = 1;
2669
2670 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2671
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002672 if (OCTEON_CN23XX_PF(oct)) {
2673 if (!oct->msix_on)
2674 if (setup_tx_poll_fn(netdev))
2675 return -1;
2676 } else {
2677 if (setup_tx_poll_fn(netdev))
2678 return -1;
2679 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002680
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002681 start_txq(netdev);
2682
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002683 /* tell Octeon to start forwarding packets to host */
2684 send_rx_ctrl_cmd(lio, 1);
2685
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002686 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2687 netdev->name);
2688
2689 return 0;
2690}
2691
2692/**
2693 * \brief Net device stop for LiquidIO
2694 * @param netdev network device
2695 */
2696static int liquidio_stop(struct net_device *netdev)
2697{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002698 struct lio *lio = GET_LIO(netdev);
2699 struct octeon_device *oct = lio->oct_dev;
2700
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002701 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2702
2703 netif_tx_disable(netdev);
2704
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002706 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002707 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002708 lio->linfo.link.s.link_up = 0;
2709 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002710
Felix Manlunascb2336b2017-01-11 17:09:02 -08002711 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002712 send_rx_ctrl_cmd(lio, 0);
2713
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002714 if (OCTEON_CN23XX_PF(oct)) {
2715 if (!oct->msix_on)
2716 cleanup_tx_poll_fn(netdev);
2717 } else {
2718 cleanup_tx_poll_fn(netdev);
2719 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002720
2721 if (lio->ptp_clock) {
2722 ptp_clock_unregister(lio->ptp_clock);
2723 lio->ptp_clock = NULL;
2724 }
2725
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002726 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002727
2728 return 0;
2729}
2730
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002731/**
2732 * \brief Converts a mask based on net device flags
2733 * @param netdev network device
2734 *
2735 * This routine generates a octnet_ifflags mask from the net device flags
2736 * received from the OS.
2737 */
2738static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2739{
2740 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2741
2742 if (netdev->flags & IFF_PROMISC)
2743 f |= OCTNET_IFFLAG_PROMISC;
2744
2745 if (netdev->flags & IFF_ALLMULTI)
2746 f |= OCTNET_IFFLAG_ALLMULTI;
2747
2748 if (netdev->flags & IFF_MULTICAST) {
2749 f |= OCTNET_IFFLAG_MULTICAST;
2750
2751 /* Accept all multicast addresses if there are more than we
2752 * can handle
2753 */
2754 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2755 f |= OCTNET_IFFLAG_ALLMULTI;
2756 }
2757
2758 if (netdev->flags & IFF_BROADCAST)
2759 f |= OCTNET_IFFLAG_BROADCAST;
2760
2761 return f;
2762}
2763
2764/**
2765 * \brief Net device set_multicast_list
2766 * @param netdev network device
2767 */
2768static void liquidio_set_mcast_list(struct net_device *netdev)
2769{
2770 struct lio *lio = GET_LIO(netdev);
2771 struct octeon_device *oct = lio->oct_dev;
2772 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002773 struct netdev_hw_addr *ha;
2774 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002775 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002776 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2777
2778 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2779
2780 /* Create a ctrl pkt command to be sent to core app. */
2781 nctrl.ncmd.u64 = 0;
2782 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002783 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2784 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002785 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002786 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002787 nctrl.netpndev = (u64)netdev;
2788 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2789
2790 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002791 mc = &nctrl.udd[0];
2792 netdev_for_each_mc_addr(ha, netdev) {
2793 *mc = 0;
2794 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2795 /* no need to swap bytes */
2796
2797 if (++mc > &nctrl.udd[mc_count])
2798 break;
2799 }
2800
2801 /* Apparently, any activity in this call from the kernel has to
2802 * be atomic. So we won't wait for response.
2803 */
2804 nctrl.wait_time = 0;
2805
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002806 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002807 if (ret < 0) {
2808 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2809 ret);
2810 }
2811}
2812
2813/**
2814 * \brief Net device set_mac_address
2815 * @param netdev network device
2816 */
2817static int liquidio_set_mac(struct net_device *netdev, void *p)
2818{
2819 int ret = 0;
2820 struct lio *lio = GET_LIO(netdev);
2821 struct octeon_device *oct = lio->oct_dev;
2822 struct sockaddr *addr = (struct sockaddr *)p;
2823 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002824
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002825 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002826 return -EADDRNOTAVAIL;
2827
2828 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2829
2830 nctrl.ncmd.u64 = 0;
2831 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002832 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002833 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002834 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002835 nctrl.netpndev = (u64)netdev;
2836 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2837 nctrl.wait_time = 100;
2838
2839 nctrl.udd[0] = 0;
2840 /* The MAC Address is presented in network byte order. */
2841 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2842
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002843 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002844 if (ret < 0) {
2845 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2846 return -ENOMEM;
2847 }
2848 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2849 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2850
2851 return 0;
2852}
2853
2854/**
2855 * \brief Net device get_stats
2856 * @param netdev network device
2857 */
2858static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2859{
2860 struct lio *lio = GET_LIO(netdev);
2861 struct net_device_stats *stats = &netdev->stats;
2862 struct octeon_device *oct;
2863 u64 pkts = 0, drop = 0, bytes = 0;
2864 struct oct_droq_stats *oq_stats;
2865 struct oct_iq_stats *iq_stats;
2866 int i, iq_no, oq_no;
2867
2868 oct = lio->oct_dev;
2869
2870 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002871 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002872 iq_stats = &oct->instr_queue[iq_no]->stats;
2873 pkts += iq_stats->tx_done;
2874 drop += iq_stats->tx_dropped;
2875 bytes += iq_stats->tx_tot_bytes;
2876 }
2877
2878 stats->tx_packets = pkts;
2879 stats->tx_bytes = bytes;
2880 stats->tx_dropped = drop;
2881
2882 pkts = 0;
2883 drop = 0;
2884 bytes = 0;
2885
2886 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002887 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002888 oq_stats = &oct->droq[oq_no]->stats;
2889 pkts += oq_stats->rx_pkts_received;
2890 drop += (oq_stats->rx_dropped +
2891 oq_stats->dropped_nodispatch +
2892 oq_stats->dropped_toomany +
2893 oq_stats->dropped_nomem);
2894 bytes += oq_stats->rx_bytes_received;
2895 }
2896
2897 stats->rx_bytes = bytes;
2898 stats->rx_packets = pkts;
2899 stats->rx_dropped = drop;
2900
2901 return stats;
2902}
2903
2904/**
2905 * \brief Net device change_mtu
2906 * @param netdev network device
2907 */
2908static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2909{
2910 struct lio *lio = GET_LIO(netdev);
2911 struct octeon_device *oct = lio->oct_dev;
2912 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002913 int ret = 0;
2914
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002915 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2916
2917 nctrl.ncmd.u64 = 0;
2918 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002919 nctrl.ncmd.s.param1 = new_mtu;
2920 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002921 nctrl.wait_time = 100;
2922 nctrl.netpndev = (u64)netdev;
2923 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2924
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002925 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002926 if (ret < 0) {
2927 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2928 return -1;
2929 }
2930
2931 lio->mtu = new_mtu;
2932
2933 return 0;
2934}
2935
2936/**
2937 * \brief Handler for SIOCSHWTSTAMP ioctl
2938 * @param netdev network device
2939 * @param ifr interface request
2940 * @param cmd command
2941 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002942static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002943{
2944 struct hwtstamp_config conf;
2945 struct lio *lio = GET_LIO(netdev);
2946
2947 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2948 return -EFAULT;
2949
2950 if (conf.flags)
2951 return -EINVAL;
2952
2953 switch (conf.tx_type) {
2954 case HWTSTAMP_TX_ON:
2955 case HWTSTAMP_TX_OFF:
2956 break;
2957 default:
2958 return -ERANGE;
2959 }
2960
2961 switch (conf.rx_filter) {
2962 case HWTSTAMP_FILTER_NONE:
2963 break;
2964 case HWTSTAMP_FILTER_ALL:
2965 case HWTSTAMP_FILTER_SOME:
2966 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2967 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2968 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2969 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2970 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2971 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2972 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2973 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2974 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2975 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2976 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2977 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2978 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2979 break;
2980 default:
2981 return -ERANGE;
2982 }
2983
2984 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2985 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2986
2987 else
2988 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2989
2990 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2991}
2992
2993/**
2994 * \brief ioctl handler
2995 * @param netdev network device
2996 * @param ifr interface request
2997 * @param cmd command
2998 */
2999static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3000{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003001 struct lio *lio = GET_LIO(netdev);
3002
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003003 switch (cmd) {
3004 case SIOCSHWTSTAMP:
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003005 if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
3006 lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
3007 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003008 default:
3009 return -EOPNOTSUPP;
3010 }
3011}
3012
3013/**
3014 * \brief handle a Tx timestamp response
3015 * @param status response status
3016 * @param buf pointer to skb
3017 */
3018static void handle_timestamp(struct octeon_device *oct,
3019 u32 status,
3020 void *buf)
3021{
3022 struct octnet_buf_free_info *finfo;
3023 struct octeon_soft_command *sc;
3024 struct oct_timestamp_resp *resp;
3025 struct lio *lio;
3026 struct sk_buff *skb = (struct sk_buff *)buf;
3027
3028 finfo = (struct octnet_buf_free_info *)skb->cb;
3029 lio = finfo->lio;
3030 sc = finfo->sc;
3031 oct = lio->oct_dev;
3032 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3033
3034 if (status != OCTEON_REQUEST_DONE) {
3035 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3036 CVM_CAST64(status));
3037 resp->timestamp = 0;
3038 }
3039
3040 octeon_swap_8B_data(&resp->timestamp, 1);
3041
Colin Ian King19a6d152016-02-05 16:30:39 +00003042 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003043 struct skb_shared_hwtstamps ts;
3044 u64 ns = resp->timestamp;
3045
3046 netif_info(lio, tx_done, lio->netdev,
3047 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3048 skb, (unsigned long long)ns);
3049 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3050 skb_tstamp_tx(skb, &ts);
3051 }
3052
3053 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003054 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003055}
3056
3057/* \brief Send a data packet that will be timestamped
3058 * @param oct octeon device
3059 * @param ndata pointer to network data
3060 * @param finfo pointer to private network data
3061 */
3062static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3063 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003064 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003065{
3066 int retval;
3067 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003068 struct lio *lio;
3069 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003070 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003071
3072 lio = finfo->lio;
3073
3074 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3075 sizeof(struct oct_timestamp_resp));
3076 finfo->sc = sc;
3077
3078 if (!sc) {
3079 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3080 return IQ_SEND_FAILED;
3081 }
3082
3083 if (ndata->reqtype == REQTYPE_NORESP_NET)
3084 ndata->reqtype = REQTYPE_RESP_NET;
3085 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3086 ndata->reqtype = REQTYPE_RESP_NET_SG;
3087
3088 sc->callback = handle_timestamp;
3089 sc->callback_arg = finfo->skb;
3090 sc->iq_no = ndata->q_no;
3091
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003092 if (OCTEON_CN23XX_PF(oct))
3093 len = (u32)((struct octeon_instr_ih3 *)
3094 (&sc->cmd.cmd3.ih3))->dlengsz;
3095 else
3096 len = (u32)((struct octeon_instr_ih2 *)
3097 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003098
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003099 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003100
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003101 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003102 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003103
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003104 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003105 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3106 retval);
3107 octeon_free_soft_command(oct, sc);
3108 } else {
3109 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3110 }
3111
3112 return retval;
3113}
3114
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003115/** \brief Transmit networks packets to the Octeon interface
3116 * @param skbuff skbuff struct to be passed to network layer.
3117 * @param netdev pointer to network device
3118 * @returns whether the packet was transmitted to the device okay or not
3119 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3120 */
3121static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3122{
3123 struct lio *lio;
3124 struct octnet_buf_free_info *finfo;
3125 union octnic_cmd_setup cmdsetup;
3126 struct octnic_data_pkt ndata;
3127 struct octeon_device *oct;
3128 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003129 struct octeon_instr_irh *irh;
3130 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003131 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003132 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003133 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003134 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003135 u32 tag = 0;
3136
3137 lio = GET_LIO(netdev);
3138 oct = lio->oct_dev;
3139
3140 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003141 q_idx = skb->queue_mapping;
3142 q_idx = (q_idx % (lio->linfo.num_txpciq));
3143 tag = q_idx;
3144 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003145 } else {
3146 iq_no = lio->txq;
3147 }
3148
3149 stats = &oct->instr_queue[iq_no]->stats;
3150
3151 /* Check for all conditions in which the current packet cannot be
3152 * transmitted.
3153 */
3154 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003155 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003156 (skb->len <= 0)) {
3157 netif_info(lio, tx_err, lio->netdev,
3158 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003159 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003160 goto lio_xmit_failed;
3161 }
3162
3163 /* Use space in skb->cb to store info used to unmap and
3164 * free the buffers.
3165 */
3166 finfo = (struct octnet_buf_free_info *)skb->cb;
3167 finfo->lio = lio;
3168 finfo->skb = skb;
3169 finfo->sc = NULL;
3170
3171 /* Prepare the attributes for the data to be passed to OSI. */
3172 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3173
3174 ndata.buf = (void *)finfo;
3175
3176 ndata.q_no = iq_no;
3177
3178 if (netif_is_multiqueue(netdev)) {
3179 if (octnet_iq_is_full(oct, ndata.q_no)) {
3180 /* defer sending if queue is full */
3181 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3182 ndata.q_no);
3183 stats->tx_iq_busy++;
3184 return NETDEV_TX_BUSY;
3185 }
3186 } else {
3187 if (octnet_iq_is_full(oct, lio->txq)) {
3188 /* defer sending if queue is full */
3189 stats->tx_iq_busy++;
3190 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003191 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003192 return NETDEV_TX_BUSY;
3193 }
3194 }
3195 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003196 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003197 */
3198
3199 ndata.datasize = skb->len;
3200
3201 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003202 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003203
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003204 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3205 if (skb->encapsulation) {
3206 cmdsetup.s.tnl_csum = 1;
3207 stats->tx_vxlan++;
3208 } else {
3209 cmdsetup.s.transport_csum = 1;
3210 }
3211 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003212 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3213 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3214 cmdsetup.s.timestamp = 1;
3215 }
3216
3217 if (skb_shinfo(skb)->nr_frags == 0) {
3218 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003219 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003220
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003221 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003222 dptr = dma_map_single(&oct->pci_dev->dev,
3223 skb->data,
3224 skb->len,
3225 DMA_TO_DEVICE);
3226 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003227 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3228 __func__);
3229 return NETDEV_TX_BUSY;
3230 }
3231
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003232 if (OCTEON_CN23XX_PF(oct))
3233 ndata.cmd.cmd3.dptr = dptr;
3234 else
3235 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003236 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003237 ndata.reqtype = REQTYPE_NORESP_NET;
3238
3239 } else {
3240 int i, frags;
3241 struct skb_frag_struct *frag;
3242 struct octnic_gather *g;
3243
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003244 spin_lock(&lio->glist_lock[q_idx]);
3245 g = (struct octnic_gather *)
3246 list_delete_head(&lio->glist[q_idx]);
3247 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003248
3249 if (!g) {
3250 netif_info(lio, tx_err, lio->netdev,
3251 "Transmit scatter gather: glist null!\n");
3252 goto lio_xmit_failed;
3253 }
3254
3255 cmdsetup.s.gather = 1;
3256 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003257 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003258
3259 memset(g->sg, 0, g->sg_size);
3260
3261 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3262 skb->data,
3263 (skb->len - skb->data_len),
3264 DMA_TO_DEVICE);
3265 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3266 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3267 __func__);
3268 return NETDEV_TX_BUSY;
3269 }
3270 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3271
3272 frags = skb_shinfo(skb)->nr_frags;
3273 i = 1;
3274 while (frags--) {
3275 frag = &skb_shinfo(skb)->frags[i - 1];
3276
3277 g->sg[(i >> 2)].ptr[(i & 3)] =
3278 dma_map_page(&oct->pci_dev->dev,
3279 frag->page.p,
3280 frag->page_offset,
3281 frag->size,
3282 DMA_TO_DEVICE);
3283
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003284 if (dma_mapping_error(&oct->pci_dev->dev,
3285 g->sg[i >> 2].ptr[i & 3])) {
3286 dma_unmap_single(&oct->pci_dev->dev,
3287 g->sg[0].ptr[0],
3288 skb->len - skb->data_len,
3289 DMA_TO_DEVICE);
3290 for (j = 1; j < i; j++) {
3291 frag = &skb_shinfo(skb)->frags[j - 1];
3292 dma_unmap_page(&oct->pci_dev->dev,
3293 g->sg[j >> 2].ptr[j & 3],
3294 frag->size,
3295 DMA_TO_DEVICE);
3296 }
3297 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3298 __func__);
3299 return NETDEV_TX_BUSY;
3300 }
3301
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003302 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3303 i++;
3304 }
3305
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003306 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003307
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003308 if (OCTEON_CN23XX_PF(oct))
3309 ndata.cmd.cmd3.dptr = dptr;
3310 else
3311 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003312 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003313 finfo->g = g;
3314
3315 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3316 }
3317
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003318 if (OCTEON_CN23XX_PF(oct)) {
3319 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3320 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3321 } else {
3322 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3323 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3324 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003325
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003326 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003327 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3328 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003329 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003330 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003331
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003332 /* HW insert VLAN tag */
3333 if (skb_vlan_tag_present(skb)) {
3334 irh->priority = skb_vlan_tag_get(skb) >> 13;
3335 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3336 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003337
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003339 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003340 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003341 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003342 if (status == IQ_SEND_FAILED)
3343 goto lio_xmit_failed;
3344
3345 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3346
3347 if (status == IQ_SEND_STOP)
3348 stop_q(lio->netdev, q_idx);
3349
Florian Westphal860e9532016-05-03 16:33:13 +02003350 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003351
Satanand Burla80c8eae2017-01-26 11:52:35 -08003352 if (tx_info->s.gso_segs)
3353 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003354 else
3355 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003356 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003357
3358 return NETDEV_TX_OK;
3359
3360lio_xmit_failed:
3361 stats->tx_dropped++;
3362 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3363 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003364 if (dptr)
3365 dma_unmap_single(&oct->pci_dev->dev, dptr,
3366 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003367 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003368 return NETDEV_TX_OK;
3369}
3370
3371/** \brief Network device Tx timeout
3372 * @param netdev pointer to network device
3373 */
3374static void liquidio_tx_timeout(struct net_device *netdev)
3375{
3376 struct lio *lio;
3377
3378 lio = GET_LIO(netdev);
3379
3380 netif_info(lio, tx_err, lio->netdev,
3381 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3382 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003383 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003384 txqs_wake(netdev);
3385}
3386
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003387static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3388 __be16 proto __attribute__((unused)),
3389 u16 vid)
3390{
3391 struct lio *lio = GET_LIO(netdev);
3392 struct octeon_device *oct = lio->oct_dev;
3393 struct octnic_ctrl_pkt nctrl;
3394 int ret = 0;
3395
3396 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3397
3398 nctrl.ncmd.u64 = 0;
3399 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3400 nctrl.ncmd.s.param1 = vid;
3401 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3402 nctrl.wait_time = 100;
3403 nctrl.netpndev = (u64)netdev;
3404 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3405
3406 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3407 if (ret < 0) {
3408 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3409 ret);
3410 }
3411
3412 return ret;
3413}
3414
3415static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3416 __be16 proto __attribute__((unused)),
3417 u16 vid)
3418{
3419 struct lio *lio = GET_LIO(netdev);
3420 struct octeon_device *oct = lio->oct_dev;
3421 struct octnic_ctrl_pkt nctrl;
3422 int ret = 0;
3423
3424 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3425
3426 nctrl.ncmd.u64 = 0;
3427 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3428 nctrl.ncmd.s.param1 = vid;
3429 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3430 nctrl.wait_time = 100;
3431 nctrl.netpndev = (u64)netdev;
3432 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3433
3434 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3435 if (ret < 0) {
3436 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3437 ret);
3438 }
3439 return ret;
3440}
3441
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003442/** Sending command to enable/disable RX checksum offload
3443 * @param netdev pointer to network device
3444 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3445 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3446 * OCTNET_CMD_RXCSUM_DISABLE
3447 * @returns SUCCESS or FAILURE
3448 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003449static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3450 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003451{
3452 struct lio *lio = GET_LIO(netdev);
3453 struct octeon_device *oct = lio->oct_dev;
3454 struct octnic_ctrl_pkt nctrl;
3455 int ret = 0;
3456
3457 nctrl.ncmd.u64 = 0;
3458 nctrl.ncmd.s.cmd = command;
3459 nctrl.ncmd.s.param1 = rx_cmd;
3460 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3461 nctrl.wait_time = 100;
3462 nctrl.netpndev = (u64)netdev;
3463 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3464
3465 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3466 if (ret < 0) {
3467 dev_err(&oct->pci_dev->dev,
3468 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3469 ret);
3470 }
3471 return ret;
3472}
3473
3474/** Sending command to add/delete VxLAN UDP port to firmware
3475 * @param netdev pointer to network device
3476 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3477 * @param vxlan_port VxLAN port to be added or deleted
3478 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3479 * OCTNET_CMD_VXLAN_PORT_DEL
3480 * @returns SUCCESS or FAILURE
3481 */
3482static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3483 u16 vxlan_port, u8 vxlan_cmd_bit)
3484{
3485 struct lio *lio = GET_LIO(netdev);
3486 struct octeon_device *oct = lio->oct_dev;
3487 struct octnic_ctrl_pkt nctrl;
3488 int ret = 0;
3489
3490 nctrl.ncmd.u64 = 0;
3491 nctrl.ncmd.s.cmd = command;
3492 nctrl.ncmd.s.more = vxlan_cmd_bit;
3493 nctrl.ncmd.s.param1 = vxlan_port;
3494 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3495 nctrl.wait_time = 100;
3496 nctrl.netpndev = (u64)netdev;
3497 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3498
3499 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3500 if (ret < 0) {
3501 dev_err(&oct->pci_dev->dev,
3502 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3503 ret);
3504 }
3505 return ret;
3506}
3507
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003508/** \brief Net device fix features
3509 * @param netdev pointer to network device
3510 * @param request features requested
3511 * @returns updated features list
3512 */
3513static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3514 netdev_features_t request)
3515{
3516 struct lio *lio = netdev_priv(netdev);
3517
3518 if ((request & NETIF_F_RXCSUM) &&
3519 !(lio->dev_capability & NETIF_F_RXCSUM))
3520 request &= ~NETIF_F_RXCSUM;
3521
3522 if ((request & NETIF_F_HW_CSUM) &&
3523 !(lio->dev_capability & NETIF_F_HW_CSUM))
3524 request &= ~NETIF_F_HW_CSUM;
3525
3526 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3527 request &= ~NETIF_F_TSO;
3528
3529 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3530 request &= ~NETIF_F_TSO6;
3531
3532 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3533 request &= ~NETIF_F_LRO;
3534
3535 /*Disable LRO if RXCSUM is off */
3536 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3537 (lio->dev_capability & NETIF_F_LRO))
3538 request &= ~NETIF_F_LRO;
3539
3540 return request;
3541}
3542
3543/** \brief Net device set features
3544 * @param netdev pointer to network device
3545 * @param features features to enable/disable
3546 */
3547static int liquidio_set_features(struct net_device *netdev,
3548 netdev_features_t features)
3549{
3550 struct lio *lio = netdev_priv(netdev);
3551
3552 if (!((netdev->features ^ features) & NETIF_F_LRO))
3553 return 0;
3554
3555 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003556 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3557 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003558 else if (!(features & NETIF_F_LRO) &&
3559 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003560 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3561 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003562
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003563 /* Sending command to firmware to enable/disable RX checksum
3564 * offload settings using ethtool
3565 */
3566 if (!(netdev->features & NETIF_F_RXCSUM) &&
3567 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3568 (features & NETIF_F_RXCSUM))
3569 liquidio_set_rxcsum_command(netdev,
3570 OCTNET_CMD_TNL_RX_CSUM_CTL,
3571 OCTNET_CMD_RXCSUM_ENABLE);
3572 else if ((netdev->features & NETIF_F_RXCSUM) &&
3573 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3574 !(features & NETIF_F_RXCSUM))
3575 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3576 OCTNET_CMD_RXCSUM_DISABLE);
3577
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003578 return 0;
3579}
3580
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003581static void liquidio_add_vxlan_port(struct net_device *netdev,
3582 struct udp_tunnel_info *ti)
3583{
3584 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3585 return;
3586
3587 liquidio_vxlan_port_command(netdev,
3588 OCTNET_CMD_VXLAN_PORT_CONFIG,
3589 htons(ti->port),
3590 OCTNET_CMD_VXLAN_PORT_ADD);
3591}
3592
3593static void liquidio_del_vxlan_port(struct net_device *netdev,
3594 struct udp_tunnel_info *ti)
3595{
3596 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3597 return;
3598
3599 liquidio_vxlan_port_command(netdev,
3600 OCTNET_CMD_VXLAN_PORT_CONFIG,
3601 htons(ti->port),
3602 OCTNET_CMD_VXLAN_PORT_DEL);
3603}
3604
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003605static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3606 u8 *mac, bool is_admin_assigned)
3607{
3608 struct lio *lio = GET_LIO(netdev);
3609 struct octeon_device *oct = lio->oct_dev;
3610 struct octnic_ctrl_pkt nctrl;
3611
3612 if (!is_valid_ether_addr(mac))
3613 return -EINVAL;
3614
3615 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3616 return -EINVAL;
3617
3618 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3619
3620 nctrl.ncmd.u64 = 0;
3621 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3622 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3623 nctrl.ncmd.s.param1 = vfidx + 1;
3624 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3625 nctrl.ncmd.s.more = 1;
3626 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003627 nctrl.netpndev = (u64)netdev;
3628 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003629 nctrl.wait_time = LIO_CMD_WAIT_TM;
3630
3631 nctrl.udd[0] = 0;
3632 /* The MAC Address is presented in network byte order. */
3633 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3634
3635 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3636
3637 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3638
3639 return 0;
3640}
3641
3642static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3643{
3644 struct lio *lio = GET_LIO(netdev);
3645 struct octeon_device *oct = lio->oct_dev;
3646 int retval;
3647
3648 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3649 if (!retval)
3650 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3651
3652 return retval;
3653}
3654
3655static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3656 u16 vlan, u8 qos, __be16 vlan_proto)
3657{
3658 struct lio *lio = GET_LIO(netdev);
3659 struct octeon_device *oct = lio->oct_dev;
3660 struct octnic_ctrl_pkt nctrl;
3661 u16 vlantci;
3662
3663 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3664 return -EINVAL;
3665
3666 if (vlan_proto != htons(ETH_P_8021Q))
3667 return -EPROTONOSUPPORT;
3668
3669 if (vlan >= VLAN_N_VID || qos > 7)
3670 return -EINVAL;
3671
3672 if (vlan)
3673 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3674 else
3675 vlantci = 0;
3676
3677 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3678 return 0;
3679
3680 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3681
3682 if (vlan)
3683 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3684 else
3685 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3686
3687 nctrl.ncmd.s.param1 = vlantci;
3688 nctrl.ncmd.s.param2 =
3689 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3690 nctrl.ncmd.s.more = 0;
3691 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3692 nctrl.cb_fn = 0;
3693 nctrl.wait_time = LIO_CMD_WAIT_TM;
3694
3695 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3696
3697 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3698
3699 return 0;
3700}
3701
3702static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3703 struct ifla_vf_info *ivi)
3704{
3705 struct lio *lio = GET_LIO(netdev);
3706 struct octeon_device *oct = lio->oct_dev;
3707 u8 *macaddr;
3708
3709 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3710 return -EINVAL;
3711
3712 ivi->vf = vfidx;
3713 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3714 ether_addr_copy(&ivi->mac[0], macaddr);
3715 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3716 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3717 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3718 return 0;
3719}
3720
3721static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3722 int linkstate)
3723{
3724 struct lio *lio = GET_LIO(netdev);
3725 struct octeon_device *oct = lio->oct_dev;
3726 struct octnic_ctrl_pkt nctrl;
3727
3728 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3729 return -EINVAL;
3730
3731 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3732 return 0;
3733
3734 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3735 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3736 nctrl.ncmd.s.param1 =
3737 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3738 nctrl.ncmd.s.param2 = linkstate;
3739 nctrl.ncmd.s.more = 0;
3740 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3741 nctrl.cb_fn = 0;
3742 nctrl.wait_time = LIO_CMD_WAIT_TM;
3743
3744 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3745
3746 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3747
3748 return 0;
3749}
3750
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003751static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003752 .ndo_open = liquidio_open,
3753 .ndo_stop = liquidio_stop,
3754 .ndo_start_xmit = liquidio_xmit,
3755 .ndo_get_stats = liquidio_get_stats,
3756 .ndo_set_mac_address = liquidio_set_mac,
3757 .ndo_set_rx_mode = liquidio_set_mcast_list,
3758 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003759
3760 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3761 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003762 .ndo_change_mtu = liquidio_change_mtu,
3763 .ndo_do_ioctl = liquidio_ioctl,
3764 .ndo_fix_features = liquidio_fix_features,
3765 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003766 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3767 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003768 .ndo_set_vf_mac = liquidio_set_vf_mac,
3769 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3770 .ndo_get_vf_config = liquidio_get_vf_config,
3771 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003772};
3773
3774/** \brief Entry point for the liquidio module
3775 */
3776static int __init liquidio_init(void)
3777{
3778 int i;
3779 struct handshake *hs;
3780
3781 init_completion(&first_stage);
3782
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003783 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003784
3785 if (liquidio_init_pci())
3786 return -EINVAL;
3787
3788 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3789
3790 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3791 hs = &handshake[i];
3792 if (hs->pci_dev) {
3793 wait_for_completion(&hs->init);
3794 if (!hs->init_ok) {
3795 /* init handshake failed */
3796 dev_err(&hs->pci_dev->dev,
3797 "Failed to init device\n");
3798 liquidio_deinit_pci();
3799 return -EIO;
3800 }
3801 }
3802 }
3803
3804 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3805 hs = &handshake[i];
3806 if (hs->pci_dev) {
3807 wait_for_completion_timeout(&hs->started,
3808 msecs_to_jiffies(30000));
3809 if (!hs->started_ok) {
3810 /* starter handshake failed */
3811 dev_err(&hs->pci_dev->dev,
3812 "Firmware failed to start\n");
3813 liquidio_deinit_pci();
3814 return -EIO;
3815 }
3816 }
3817 }
3818
3819 return 0;
3820}
3821
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003822static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003823{
3824 struct octeon_device *oct = (struct octeon_device *)buf;
3825 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003826 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003827 union oct_link_status *ls;
3828 int i;
3829
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003830 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003831 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3832 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003833 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003834 goto nic_info_err;
3835 }
3836
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003837 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003838 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3839
3840 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003841 for (i = 0; i < oct->ifcount; i++) {
3842 if (oct->props[i].gmxport == gmxport) {
3843 update_link_status(oct->props[i].netdev, ls);
3844 break;
3845 }
3846 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003847
3848nic_info_err:
3849 for (i = 0; i < recv_pkt->buffer_count; i++)
3850 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3851 octeon_free_recv_info(recv_info);
3852 return 0;
3853}
3854
3855/**
3856 * \brief Setup network interfaces
3857 * @param octeon_dev octeon device
3858 *
3859 * Called during init time for each device. It assumes the NIC
3860 * is already up and running. The link information for each
3861 * interface is passed in link_info.
3862 */
3863static int setup_nic_devices(struct octeon_device *octeon_dev)
3864{
3865 struct lio *lio = NULL;
3866 struct net_device *netdev;
3867 u8 mac[6], i, j;
3868 struct octeon_soft_command *sc;
3869 struct liquidio_if_cfg_context *ctx;
3870 struct liquidio_if_cfg_resp *resp;
3871 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003872 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003873 union oct_nic_if_cfg if_cfg;
3874 unsigned int base_queue;
3875 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003876 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003877 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003878 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003879
3880 /* This is to handle link status changes */
3881 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3882 OPCODE_NIC_INFO,
3883 lio_nic_info, octeon_dev);
3884
3885 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3886 * They are handled directly.
3887 */
3888 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3889 free_netbuf);
3890
3891 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3892 free_netsgbuf);
3893
3894 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3895 free_netsgbuf_with_resp);
3896
3897 for (i = 0; i < octeon_dev->ifcount; i++) {
3898 resp_size = sizeof(struct liquidio_if_cfg_resp);
3899 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003900 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003901 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003902 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003903 resp_size, ctx_size);
3904 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3905 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003906 vdata = (struct lio_version *)sc->virtdptr;
3907
3908 *((u64 *)vdata) = 0;
3909 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3910 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3911 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003912
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003913 if (OCTEON_CN23XX_PF(octeon_dev)) {
3914 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3915 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3916 base_queue = octeon_dev->sriov_info.pf_srn;
3917
3918 gmx_port_id = octeon_dev->pf_num;
3919 ifidx_or_pfnum = octeon_dev->pf_num;
3920 } else {
3921 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3922 octeon_get_conf(octeon_dev), i);
3923 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3924 octeon_get_conf(octeon_dev), i);
3925 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3926 octeon_get_conf(octeon_dev), i);
3927 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3928 octeon_get_conf(octeon_dev), i);
3929 ifidx_or_pfnum = i;
3930 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003931
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003932 dev_dbg(&octeon_dev->pci_dev->dev,
3933 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003934 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003935 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003936 ctx->octeon_id = lio_get_device_id(octeon_dev);
3937 init_waitqueue_head(&ctx->wc);
3938
3939 if_cfg.u64 = 0;
3940 if_cfg.s.num_iqueues = num_iqueues;
3941 if_cfg.s.num_oqueues = num_oqueues;
3942 if_cfg.s.base_queue = base_queue;
3943 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003944
3945 sc->iq_no = 0;
3946
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003947 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003948 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003949 if_cfg.u64, 0);
3950
3951 sc->callback = if_cfg_callback;
3952 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003953 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003954
3955 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003956 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003957 dev_err(&octeon_dev->pci_dev->dev,
3958 "iq/oq config failed status: %x\n",
3959 retval);
3960 /* Soft instr is freed by driver in case of failure. */
3961 goto setup_nic_dev_fail;
3962 }
3963
3964 /* Sleep on a wait queue till the cond flag indicates that the
3965 * response arrived or timed-out.
3966 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003967 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3968 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3969 goto setup_nic_wait_intr;
3970 }
3971
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003972 retval = resp->status;
3973 if (retval) {
3974 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3975 goto setup_nic_dev_fail;
3976 }
3977
3978 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3979 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3980
3981 num_iqueues = hweight64(resp->cfg_info.iqmask);
3982 num_oqueues = hweight64(resp->cfg_info.oqmask);
3983
3984 if (!(num_iqueues) || !(num_oqueues)) {
3985 dev_err(&octeon_dev->pci_dev->dev,
3986 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3987 resp->cfg_info.iqmask,
3988 resp->cfg_info.oqmask);
3989 goto setup_nic_dev_fail;
3990 }
3991 dev_dbg(&octeon_dev->pci_dev->dev,
3992 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3993 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3994 num_iqueues, num_oqueues);
3995 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3996
3997 if (!netdev) {
3998 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3999 goto setup_nic_dev_fail;
4000 }
4001
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004002 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004003
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004004 /* Associate the routines that will handle different
4005 * netdev tasks.
4006 */
4007 netdev->netdev_ops = &lionetdevops;
4008
4009 lio = GET_LIO(netdev);
4010
4011 memset(lio, 0, sizeof(struct lio));
4012
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004013 lio->ifidx = ifidx_or_pfnum;
4014
4015 props = &octeon_dev->props[i];
4016 props->gmxport = resp->cfg_info.linfo.gmxport;
4017 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004018
4019 lio->linfo.num_rxpciq = num_oqueues;
4020 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004021 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004022 lio->linfo.rxpciq[j].u64 =
4023 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004024 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004025 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004026 lio->linfo.txpciq[j].u64 =
4027 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004028 }
4029 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4030 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4031 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4032
4033 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4034
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004035 if (OCTEON_CN23XX_PF(octeon_dev) ||
4036 OCTEON_CN6XXX(octeon_dev)) {
4037 lio->dev_capability = NETIF_F_HIGHDMA
4038 | NETIF_F_IP_CSUM
4039 | NETIF_F_IPV6_CSUM
4040 | NETIF_F_SG | NETIF_F_RXCSUM
4041 | NETIF_F_GRO
4042 | NETIF_F_TSO | NETIF_F_TSO6
4043 | NETIF_F_LRO;
4044 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004045 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4046
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004047 /* Copy of transmit encapsulation capabilities:
4048 * TSO, TSO6, Checksums for this device
4049 */
4050 lio->enc_dev_capability = NETIF_F_IP_CSUM
4051 | NETIF_F_IPV6_CSUM
4052 | NETIF_F_GSO_UDP_TUNNEL
4053 | NETIF_F_HW_CSUM | NETIF_F_SG
4054 | NETIF_F_RXCSUM
4055 | NETIF_F_TSO | NETIF_F_TSO6
4056 | NETIF_F_LRO;
4057
4058 netdev->hw_enc_features = (lio->enc_dev_capability &
4059 ~NETIF_F_LRO);
4060
4061 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4062
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004063 netdev->vlan_features = lio->dev_capability;
4064 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004065 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4066 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004067 NETIF_F_HW_VLAN_CTAG_TX;
4068
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004069 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4070
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004071 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004072 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4073 netdev->hw_features = netdev->hw_features &
4074 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004075
Jarod Wilson109cc162016-10-17 15:54:13 -04004076 /* MTU range: 68 - 16000 */
4077 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4078 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4079
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004080 /* Point to the properties for octeon device to which this
4081 * interface belongs.
4082 */
4083 lio->oct_dev = octeon_dev;
4084 lio->octprops = props;
4085 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004086
4087 dev_dbg(&octeon_dev->pci_dev->dev,
4088 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4089 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4090
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004091 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4092 u8 vfmac[ETH_ALEN];
4093
4094 random_ether_addr(&vfmac[0]);
4095 if (__liquidio_set_vf_mac(netdev, j,
4096 &vfmac[0], false)) {
4097 dev_err(&octeon_dev->pci_dev->dev,
4098 "Error setting VF%d MAC address\n",
4099 j);
4100 goto setup_nic_dev_fail;
4101 }
4102 }
4103
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004104 /* 64-bit swap required on LE machines */
4105 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4106 for (j = 0; j < 6; j++)
4107 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4108
4109 /* Copy MAC Address to OS network device structure */
4110
4111 ether_addr_copy(netdev->dev_addr, mac);
4112
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004113 /* By default all interfaces on a single Octeon uses the same
4114 * tx and rx queues
4115 */
4116 lio->txq = lio->linfo.txpciq[0].s.q_no;
4117 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004118 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004119 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4120 goto setup_nic_dev_fail;
4121 }
4122
4123 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4124
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004125 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4126 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4127
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004128 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004129 dev_err(&octeon_dev->pci_dev->dev,
4130 "Gather list allocation failed\n");
4131 goto setup_nic_dev_fail;
4132 }
4133
4134 /* Register ethtool support */
4135 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004136 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4137 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4138 else
4139 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004140
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004141 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004142 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4143 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004144
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004145 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4146
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004147 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004148 liquidio_set_feature(netdev,
4149 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004150
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004151 if (setup_link_status_change_wq(netdev))
4152 goto setup_nic_dev_fail;
4153
Satanand Burla031d4f12017-03-22 11:31:13 -07004154 if (setup_rx_oom_poll_fn(netdev))
4155 goto setup_nic_dev_fail;
4156
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004157 /* Register the network device with the OS */
4158 if (register_netdev(netdev)) {
4159 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4160 goto setup_nic_dev_fail;
4161 }
4162
4163 dev_dbg(&octeon_dev->pci_dev->dev,
4164 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4165 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4166 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004167 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004168
4169 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4170
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004171 /* Sending command to firmware to enable Rx checksum offload
4172 * by default at the time of setup of Liquidio driver for
4173 * this device
4174 */
4175 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4176 OCTNET_CMD_RXCSUM_ENABLE);
4177 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4178 OCTNET_CMD_TXCSUM_ENABLE);
4179
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004180 dev_dbg(&octeon_dev->pci_dev->dev,
4181 "NIC ifidx:%d Setup successful\n", i);
4182
4183 octeon_free_soft_command(octeon_dev, sc);
4184 }
4185
4186 return 0;
4187
4188setup_nic_dev_fail:
4189
4190 octeon_free_soft_command(octeon_dev, sc);
4191
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004192setup_nic_wait_intr:
4193
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004194 while (i--) {
4195 dev_err(&octeon_dev->pci_dev->dev,
4196 "NIC ifidx:%d Setup failed\n", i);
4197 liquidio_destroy_nic_device(octeon_dev, i);
4198 }
4199 return -ENODEV;
4200}
4201
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004202#ifdef CONFIG_PCI_IOV
4203static int octeon_enable_sriov(struct octeon_device *oct)
4204{
4205 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4206 struct pci_dev *vfdev;
4207 int err;
4208 u32 u;
4209
4210 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4211 err = pci_enable_sriov(oct->pci_dev,
4212 oct->sriov_info.num_vfs_alloced);
4213 if (err) {
4214 dev_err(&oct->pci_dev->dev,
4215 "OCTEON: Failed to enable PCI sriov: %d\n",
4216 err);
4217 oct->sriov_info.num_vfs_alloced = 0;
4218 return err;
4219 }
4220 oct->sriov_info.sriov_enabled = 1;
4221
4222 /* init lookup table that maps DPI ring number to VF pci_dev
4223 * struct pointer
4224 */
4225 u = 0;
4226 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4227 OCTEON_CN23XX_VF_VID, NULL);
4228 while (vfdev) {
4229 if (vfdev->is_virtfn &&
4230 (vfdev->physfn == oct->pci_dev)) {
4231 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4232 vfdev;
4233 u += oct->sriov_info.rings_per_vf;
4234 }
4235 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4236 OCTEON_CN23XX_VF_VID, vfdev);
4237 }
4238 }
4239
4240 return num_vfs_alloced;
4241}
4242
4243static int lio_pci_sriov_disable(struct octeon_device *oct)
4244{
4245 int u;
4246
4247 if (pci_vfs_assigned(oct->pci_dev)) {
4248 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4249 return -EPERM;
4250 }
4251
4252 pci_disable_sriov(oct->pci_dev);
4253
4254 u = 0;
4255 while (u < MAX_POSSIBLE_VFS) {
4256 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4257 u += oct->sriov_info.rings_per_vf;
4258 }
4259
4260 oct->sriov_info.num_vfs_alloced = 0;
4261 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4262 oct->pf_num);
4263
4264 return 0;
4265}
4266
4267static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4268{
4269 struct octeon_device *oct = pci_get_drvdata(dev);
4270 int ret = 0;
4271
4272 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4273 (oct->sriov_info.sriov_enabled)) {
4274 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4275 oct->pf_num, num_vfs);
4276 return 0;
4277 }
4278
4279 if (!num_vfs) {
4280 ret = lio_pci_sriov_disable(oct);
4281 } else if (num_vfs > oct->sriov_info.max_vfs) {
4282 dev_err(&oct->pci_dev->dev,
4283 "OCTEON: Max allowed VFs:%d user requested:%d",
4284 oct->sriov_info.max_vfs, num_vfs);
4285 ret = -EPERM;
4286 } else {
4287 oct->sriov_info.num_vfs_alloced = num_vfs;
4288 ret = octeon_enable_sriov(oct);
4289 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4290 oct->pf_num, num_vfs);
4291 }
4292
4293 return ret;
4294}
4295#endif
4296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004297/**
4298 * \brief initialize the NIC
4299 * @param oct octeon device
4300 *
4301 * This initialization routine is called once the Octeon device application is
4302 * up and running
4303 */
4304static int liquidio_init_nic_module(struct octeon_device *oct)
4305{
4306 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004307 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004308 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4309
4310 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4311
4312 /* only default iq and oq were initialized
4313 * initialize the rest as well
4314 */
4315 /* run port_config command for each port */
4316 oct->ifcount = num_nic_ports;
4317
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004318 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004319
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004320 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4321 oct->props[i].gmxport = -1;
4322
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004323 retval = setup_nic_devices(oct);
4324 if (retval) {
4325 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4326 goto octnet_init_failure;
4327 }
4328
4329 liquidio_ptp_init(oct);
4330
4331 /* Initialize interrupt moderation params */
4332 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004333 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004334 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004335 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4336 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4337 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4338 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4339 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4340 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4341 intrmod_cfg->tx_enable = 1;
4342 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4343 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4344 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4345 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004346 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004347 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4348
4349 return retval;
4350
4351octnet_init_failure:
4352
4353 oct->ifcount = 0;
4354
4355 return retval;
4356}
4357
4358/**
4359 * \brief starter callback that invokes the remaining initialization work after
4360 * the NIC is up and running.
4361 * @param octptr work struct work_struct
4362 */
4363static void nic_starter(struct work_struct *work)
4364{
4365 struct octeon_device *oct;
4366 struct cavium_wk *wk = (struct cavium_wk *)work;
4367
4368 oct = (struct octeon_device *)wk->ctxptr;
4369
4370 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4371 return;
4372
4373 /* If the status of the device is CORE_OK, the core
4374 * application has reported its application type. Call
4375 * any registered handlers now and move to the RUNNING
4376 * state.
4377 */
4378 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4379 schedule_delayed_work(&oct->nic_poll_work.work,
4380 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4381 return;
4382 }
4383
4384 atomic_set(&oct->status, OCT_DEV_RUNNING);
4385
4386 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4387 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4388
4389 if (liquidio_init_nic_module(oct))
4390 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4391 else
4392 handshake[oct->octeon_id].started_ok = 1;
4393 } else {
4394 dev_err(&oct->pci_dev->dev,
4395 "Unexpected application running on NIC (%d). Check firmware.\n",
4396 oct->app_mode);
4397 }
4398
4399 complete(&handshake[oct->octeon_id].started);
4400}
4401
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004402static int
4403octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4404{
4405 struct octeon_device *oct = (struct octeon_device *)buf;
4406 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4407 int i, notice, vf_idx;
4408 u64 *data, vf_num;
4409
4410 notice = recv_pkt->rh.r.ossp;
4411 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4412
4413 /* the first 64-bit word of data is the vf_num */
4414 vf_num = data[0];
4415 octeon_swap_8B_data(&vf_num, 1);
4416 vf_idx = (int)vf_num - 1;
4417
4418 if (notice == VF_DRV_LOADED) {
4419 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4420 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4421 dev_info(&oct->pci_dev->dev,
4422 "driver for VF%d was loaded\n", vf_idx);
4423 try_module_get(THIS_MODULE);
4424 }
4425 } else if (notice == VF_DRV_REMOVED) {
4426 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4427 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4428 dev_info(&oct->pci_dev->dev,
4429 "driver for VF%d was removed\n", vf_idx);
4430 module_put(THIS_MODULE);
4431 }
4432 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4433 u8 *b = (u8 *)&data[1];
4434
4435 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4436 dev_info(&oct->pci_dev->dev,
4437 "VF driver changed VF%d's MAC address to %pM\n",
4438 vf_idx, b + 2);
4439 }
4440
4441 for (i = 0; i < recv_pkt->buffer_count; i++)
4442 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4443 octeon_free_recv_info(recv_info);
4444
4445 return 0;
4446}
4447
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004448/**
4449 * \brief Device initialization for each Octeon device that is probed
4450 * @param octeon_dev octeon device
4451 */
4452static int octeon_device_init(struct octeon_device *octeon_dev)
4453{
4454 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004455 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004456 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004457 struct octeon_device_priv *oct_priv =
4458 (struct octeon_device_priv *)octeon_dev->priv;
4459 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4460
4461 /* Enable access to the octeon device and make its DMA capability
4462 * known to the OS.
4463 */
4464 if (octeon_pci_os_setup(octeon_dev))
4465 return 1;
4466
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004467 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4468
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004469 /* Identify the Octeon type and map the BAR address space. */
4470 if (octeon_chip_specific_setup(octeon_dev)) {
4471 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4472 return 1;
4473 }
4474
4475 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4476
4477 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4478
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004479 if (OCTEON_CN23XX_PF(octeon_dev)) {
4480 if (!cn23xx_fw_loaded(octeon_dev)) {
4481 fw_loaded = 0;
4482 /* Do a soft reset of the Octeon device. */
4483 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4484 return 1;
4485 /* things might have changed */
4486 if (!cn23xx_fw_loaded(octeon_dev))
4487 fw_loaded = 0;
4488 else
4489 fw_loaded = 1;
4490 } else {
4491 fw_loaded = 1;
4492 }
4493 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004494 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004495 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004496
4497 /* Initialize the dispatch mechanism used to push packets arriving on
4498 * Octeon Output queues.
4499 */
4500 if (octeon_init_dispatch_list(octeon_dev))
4501 return 1;
4502
4503 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4504 OPCODE_NIC_CORE_DRV_ACTIVE,
4505 octeon_core_drv_init,
4506 octeon_dev);
4507
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004508 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4509 OPCODE_NIC_VF_DRV_NOTICE,
4510 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004511 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4512 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4513 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4514 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4515
4516 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4517
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004518 if (octeon_set_io_queues_off(octeon_dev)) {
4519 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4520 return 1;
4521 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004522
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004523 if (OCTEON_CN23XX_PF(octeon_dev)) {
4524 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4525 if (ret) {
4526 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4527 return ret;
4528 }
4529 }
4530
4531 /* Initialize soft command buffer pool
4532 */
4533 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4534 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4535 return 1;
4536 }
4537 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4538
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004539 /* Setup the data structures that manage this Octeon's Input queues. */
4540 if (octeon_setup_instr_queues(octeon_dev)) {
4541 dev_err(&octeon_dev->pci_dev->dev,
4542 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004543 return 1;
4544 }
4545 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4546
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004547 /* Initialize lists to manage the requests of different types that
4548 * arrive from user & kernel applications for this octeon device.
4549 */
4550 if (octeon_setup_response_list(octeon_dev)) {
4551 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4552 return 1;
4553 }
4554 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4555
4556 if (octeon_setup_output_queues(octeon_dev)) {
4557 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004558 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004559 }
4560
4561 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4562
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004563 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004564 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4565 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4566 return 1;
4567 }
4568 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4569
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004570 if (octeon_allocate_ioq_vector(octeon_dev)) {
4571 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4572 return 1;
4573 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004574 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004575
4576 } else {
4577 /* The input and output queue registers were setup earlier (the
4578 * queues were not enabled). Any additional registers
4579 * that need to be programmed should be done now.
4580 */
4581 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4582 if (ret) {
4583 dev_err(&octeon_dev->pci_dev->dev,
4584 "Failed to configure device registers\n");
4585 return ret;
4586 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004587 }
4588
4589 /* Initialize the tasklet that handles output queue packet processing.*/
4590 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4591 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4592 (unsigned long)octeon_dev);
4593
4594 /* Setup the interrupt handler and record the INT SUM register address
4595 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004596 if (octeon_setup_interrupt(octeon_dev))
4597 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004598
4599 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004600 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004601
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004602 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4603
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004604 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004605 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4606 if (ret) {
4607 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4608 return ret;
4609 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004610
4611 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4612
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004613 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4614 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4615 if (!ddr_timeout) {
4616 dev_info(&octeon_dev->pci_dev->dev,
4617 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4618 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004619
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004620 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004621
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004622 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4623 while (!ddr_timeout) {
4624 set_current_state(TASK_INTERRUPTIBLE);
4625 if (schedule_timeout(HZ / 10)) {
4626 /* user probably pressed Control-C */
4627 return 1;
4628 }
4629 }
4630 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4631 if (ret) {
4632 dev_err(&octeon_dev->pci_dev->dev,
4633 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4634 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004635 return 1;
4636 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004637
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004638 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4639 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4640 return 1;
4641 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004642
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004643 /* Divert uboot to take commands from host instead. */
4644 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004645
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004646 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4647 ret = octeon_init_consoles(octeon_dev);
4648 if (ret) {
4649 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4650 return 1;
4651 }
4652 ret = octeon_add_console(octeon_dev, 0);
4653 if (ret) {
4654 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4655 return 1;
4656 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004657
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004658 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004659
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004660 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4661 ret = load_firmware(octeon_dev);
4662 if (ret) {
4663 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4664 return 1;
4665 }
4666 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4667 * loaded
4668 */
4669 if (OCTEON_CN23XX_PF(octeon_dev))
4670 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4671 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004672 }
4673
4674 handshake[octeon_dev->octeon_id].init_ok = 1;
4675 complete(&handshake[octeon_dev->octeon_id].init);
4676
4677 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4678
4679 /* Send Credit for Octeon Output queues. Credits are always sent after
4680 * the output queue is enabled.
4681 */
4682 for (j = 0; j < octeon_dev->num_oqs; j++)
4683 writel(octeon_dev->droq[j]->max_count,
4684 octeon_dev->droq[j]->pkts_credit_reg);
4685
4686 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004687 return 0;
4688}
4689
4690/**
4691 * \brief Exits the module
4692 */
4693static void __exit liquidio_exit(void)
4694{
4695 liquidio_deinit_pci();
4696
4697 pr_info("LiquidIO network module is now unloaded\n");
4698}
4699
4700module_init(liquidio_init);
4701module_exit(liquidio_exit);