blob: 86ea86cfc1336664e7ecd8480be74b2af099596c [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);
935 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700936 if (!octnet_iq_is_full(oct, lio->txq)) {
937 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
938 lio->txq,
939 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700940 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700941 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700942 }
943 }
944}
945
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700946static
947int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
948{
949 struct octeon_device *oct = droq->oct_dev;
950 struct octeon_device_priv *oct_priv =
951 (struct octeon_device_priv *)oct->priv;
952
953 if (droq->ops.poll_mode) {
954 droq->ops.napi_fn(droq);
955 } else {
956 if (ret & MSIX_PO_INT) {
957 tasklet_schedule(&oct_priv->droq_tasklet);
958 return 1;
959 }
960 /* this will be flushed periodically by check iq db */
961 if (ret & MSIX_PI_INT)
962 return 0;
963 }
964 return 0;
965}
966
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700967/**
968 * \brief Droq packet processor sceduler
969 * @param oct octeon device
970 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700971static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700972{
973 struct octeon_device_priv *oct_priv =
974 (struct octeon_device_priv *)oct->priv;
975 u64 oq_no;
976 struct octeon_droq *droq;
977
978 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700979 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
980 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800981 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700982 continue;
983
984 droq = oct->droq[oq_no];
985
986 if (droq->ops.poll_mode) {
987 droq->ops.napi_fn(droq);
988 oct_priv->napi_mask |= (1 << oq_no);
989 } else {
990 tasklet_schedule(&oct_priv->droq_tasklet);
991 }
992 }
993 }
994}
995
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700996static irqreturn_t
997liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
998{
999 u64 ret;
1000 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1001 struct octeon_device *oct = ioq_vector->oct_dev;
1002 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1003
1004 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1005
1006 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1007 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1008
1009 return IRQ_HANDLED;
1010}
1011
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001012/**
1013 * \brief Interrupt handler for octeon
1014 * @param irq unused
1015 * @param dev octeon device
1016 */
1017static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001018irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1019 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001020{
1021 struct octeon_device *oct = (struct octeon_device *)dev;
1022 irqreturn_t ret;
1023
1024 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001025 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001026
1027 ret = oct->fn_list.process_interrupt_regs(oct);
1028
1029 if (ret == IRQ_HANDLED)
1030 liquidio_schedule_droq_pkt_handlers(oct);
1031
1032 /* Re-enable our interrupts */
1033 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001034 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001035
1036 return ret;
1037}
1038
1039/**
1040 * \brief Setup interrupt for octeon device
1041 * @param oct octeon device
1042 *
1043 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1044 */
1045static int octeon_setup_interrupt(struct octeon_device *oct)
1046{
1047 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001048 struct msix_entry *msix_entries;
1049 int i;
1050 int num_ioq_vectors;
1051 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001052 char *queue_irq_names = NULL;
1053 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001054
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001055 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1056 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1057 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1058 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001059
Rick Farrington0c88a762017-03-13 12:58:04 -07001060 /* allocate storage for the names assigned to each irq */
1061 oct->irq_name_storage =
1062 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1063 GFP_KERNEL);
1064 if (!oct->irq_name_storage) {
1065 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1066 return -ENOMEM;
1067 }
1068
1069 queue_irq_names = oct->irq_name_storage;
1070 aux_irq_name = &queue_irq_names
1071 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1072
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001073 oct->msix_entries = kcalloc(
1074 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001075 if (!oct->msix_entries) {
1076 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1077 kfree(oct->irq_name_storage);
1078 oct->irq_name_storage = NULL;
1079 return -ENOMEM;
1080 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001081
1082 msix_entries = (struct msix_entry *)oct->msix_entries;
1083 /*Assumption is that pf msix vectors start from pf srn to pf to
1084 * trs and not from 0. if not change this code
1085 */
1086 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1087 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1088 msix_entries[oct->num_msix_irqs - 1].entry =
1089 oct->sriov_info.trs;
1090 num_alloc_ioq_vectors = pci_enable_msix_range(
1091 oct->pci_dev, msix_entries,
1092 oct->num_msix_irqs,
1093 oct->num_msix_irqs);
1094 if (num_alloc_ioq_vectors < 0) {
1095 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1096 kfree(oct->msix_entries);
1097 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001098 kfree(oct->irq_name_storage);
1099 oct->irq_name_storage = NULL;
1100 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001101 }
1102 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1103
1104 num_ioq_vectors = oct->num_msix_irqs;
1105
1106 /** For PF, there is one non-ioq interrupt handler */
1107 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001108
1109 snprintf(aux_irq_name, INTRNAMSIZ,
1110 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001111 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001112 liquidio_legacy_intr_handler, 0,
1113 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001114 if (irqret) {
1115 dev_err(&oct->pci_dev->dev,
1116 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1117 irqret);
1118 pci_disable_msix(oct->pci_dev);
1119 kfree(oct->msix_entries);
1120 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001121 kfree(oct->irq_name_storage);
1122 oct->irq_name_storage = NULL;
1123 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001124 }
1125
1126 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001127 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1128 "LiquidIO%u-pf%u-rxtx-%u",
1129 oct->octeon_id, oct->pf_num, i);
1130
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001131 irqret = request_irq(msix_entries[i].vector,
1132 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001133 &queue_irq_names[IRQ_NAME_OFF(i)],
1134 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001135 if (irqret) {
1136 dev_err(&oct->pci_dev->dev,
1137 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1138 irqret);
1139 /** Freeing the non-ioq irq vector here . */
1140 free_irq(msix_entries[num_ioq_vectors].vector,
1141 oct);
1142
1143 while (i) {
1144 i--;
1145 /** clearing affinity mask. */
1146 irq_set_affinity_hint(
1147 msix_entries[i].vector, NULL);
1148 free_irq(msix_entries[i].vector,
1149 &oct->ioq_vector[i]);
1150 }
1151 pci_disable_msix(oct->pci_dev);
1152 kfree(oct->msix_entries);
1153 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001154 kfree(oct->irq_name_storage);
1155 oct->irq_name_storage = NULL;
1156 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001157 }
1158 oct->ioq_vector[i].vector = msix_entries[i].vector;
1159 /* assign the cpu mask for this msix interrupt vector */
1160 irq_set_affinity_hint(
1161 msix_entries[i].vector,
1162 (&oct->ioq_vector[i].affinity_mask));
1163 }
1164 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1165 oct->octeon_id);
1166 } else {
1167 err = pci_enable_msi(oct->pci_dev);
1168 if (err)
1169 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1170 err);
1171 else
1172 oct->flags |= LIO_FLAG_MSI_ENABLED;
1173
Rick Farrington0c88a762017-03-13 12:58:04 -07001174 /* allocate storage for the names assigned to the irq */
1175 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1176 if (!oct->irq_name_storage)
1177 return -ENOMEM;
1178
1179 queue_irq_names = oct->irq_name_storage;
1180
1181 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1182 "LiquidIO%u-pf%u-rxtx-%u",
1183 oct->octeon_id, oct->pf_num, 0);
1184
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001185 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001186 liquidio_legacy_intr_handler,
1187 IRQF_SHARED,
1188 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001189 if (irqret) {
1190 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1191 pci_disable_msi(oct->pci_dev);
1192 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1193 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001194 kfree(oct->irq_name_storage);
1195 oct->irq_name_storage = NULL;
1196 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001197 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001198 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001199 return 0;
1200}
1201
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001202static int liquidio_watchdog(void *param)
1203{
1204 u64 wdog;
1205 u16 mask_of_stuck_cores = 0;
1206 u16 mask_of_crashed_cores = 0;
1207 int core_num;
1208 u8 core_is_stuck[LIO_MAX_CORES];
1209 u8 core_crashed[LIO_MAX_CORES];
1210 struct octeon_device *oct = param;
1211
1212 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1213 memset(core_crashed, 0, sizeof(core_crashed));
1214
1215 while (!kthread_should_stop()) {
1216 mask_of_crashed_cores =
1217 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1218
1219 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1220 if (!core_is_stuck[core_num]) {
1221 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1222
1223 /* look at watchdog state field */
1224 wdog &= CIU3_WDOG_MASK;
1225 if (wdog) {
1226 /* this watchdog timer has expired */
1227 core_is_stuck[core_num] =
1228 LIO_MONITOR_WDOG_EXPIRE;
1229 mask_of_stuck_cores |= (1 << core_num);
1230 }
1231 }
1232
1233 if (!core_crashed[core_num])
1234 core_crashed[core_num] =
1235 (mask_of_crashed_cores >> core_num) & 1;
1236 }
1237
1238 if (mask_of_stuck_cores) {
1239 for (core_num = 0; core_num < LIO_MAX_CORES;
1240 core_num++) {
1241 if (core_is_stuck[core_num] == 1) {
1242 dev_err(&oct->pci_dev->dev,
1243 "ERROR: Octeon core %d is stuck!\n",
1244 core_num);
1245 /* 2 means we have printk'd an error
1246 * so no need to repeat the same printk
1247 */
1248 core_is_stuck[core_num] =
1249 LIO_MONITOR_CORE_STUCK_MSGD;
1250 }
1251 }
1252 }
1253
1254 if (mask_of_crashed_cores) {
1255 for (core_num = 0; core_num < LIO_MAX_CORES;
1256 core_num++) {
1257 if (core_crashed[core_num] == 1) {
1258 dev_err(&oct->pci_dev->dev,
1259 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1260 core_num);
1261 /* 2 means we have printk'd an error
1262 * so no need to repeat the same printk
1263 */
1264 core_crashed[core_num] =
1265 LIO_MONITOR_CORE_STUCK_MSGD;
1266 }
1267 }
1268 }
1269#ifdef CONFIG_MODULE_UNLOAD
1270 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1271 /* make module refcount=0 so that rmmod will work */
1272 long refcount;
1273
1274 refcount = module_refcount(THIS_MODULE);
1275
1276 while (refcount > 0) {
1277 module_put(THIS_MODULE);
1278 refcount = module_refcount(THIS_MODULE);
1279 }
1280
1281 /* compensate for and withstand an unlikely (but still
1282 * possible) race condition
1283 */
1284 while (refcount < 0) {
1285 try_module_get(THIS_MODULE);
1286 refcount = module_refcount(THIS_MODULE);
1287 }
1288 }
1289#endif
1290 /* sleep for two seconds */
1291 set_current_state(TASK_INTERRUPTIBLE);
1292 schedule_timeout(2 * HZ);
1293 }
1294
1295 return 0;
1296}
1297
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001298/**
1299 * \brief PCI probe handler
1300 * @param pdev PCI device structure
1301 * @param ent unused
1302 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001303static int
1304liquidio_probe(struct pci_dev *pdev,
1305 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001306{
1307 struct octeon_device *oct_dev = NULL;
1308 struct handshake *hs;
1309
1310 oct_dev = octeon_allocate_device(pdev->device,
1311 sizeof(struct octeon_device_priv));
1312 if (!oct_dev) {
1313 dev_err(&pdev->dev, "Unable to allocate device\n");
1314 return -ENOMEM;
1315 }
1316
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001317 if (pdev->device == OCTEON_CN23XX_PF_VID)
1318 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1319
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001320 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1321 (u32)pdev->vendor, (u32)pdev->device);
1322
1323 /* Assign octeon_device for this device to the private data area. */
1324 pci_set_drvdata(pdev, oct_dev);
1325
1326 /* set linux specific device pointer */
1327 oct_dev->pci_dev = (void *)pdev;
1328
1329 hs = &handshake[oct_dev->octeon_id];
1330 init_completion(&hs->init);
1331 init_completion(&hs->started);
1332 hs->pci_dev = pdev;
1333
1334 if (oct_dev->octeon_id == 0)
1335 /* first LiquidIO NIC is detected */
1336 complete(&first_stage);
1337
1338 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001339 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001340 liquidio_remove(pdev);
1341 return -ENOMEM;
1342 }
1343
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001344 if (OCTEON_CN23XX_PF(oct_dev)) {
1345 u64 scratch1;
1346 u8 bus, device, function;
1347
1348 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1349 if (!(scratch1 & 4ULL)) {
1350 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1351 * the lio watchdog kernel thread is running for this
1352 * NIC. Each NIC gets one watchdog kernel thread.
1353 */
1354 scratch1 |= 4ULL;
1355 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1356 scratch1);
1357
1358 bus = pdev->bus->number;
1359 device = PCI_SLOT(pdev->devfn);
1360 function = PCI_FUNC(pdev->devfn);
1361 oct_dev->watchdog_task = kthread_create(
1362 liquidio_watchdog, oct_dev,
1363 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001364 if (!IS_ERR(oct_dev->watchdog_task)) {
1365 wake_up_process(oct_dev->watchdog_task);
1366 } else {
1367 oct_dev->watchdog_task = NULL;
1368 dev_err(&oct_dev->pci_dev->dev,
1369 "failed to create kernel_thread\n");
1370 liquidio_remove(pdev);
1371 return -1;
1372 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001373 }
1374 }
1375
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001376 oct_dev->rx_pause = 1;
1377 oct_dev->tx_pause = 1;
1378
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001379 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1380
1381 return 0;
1382}
1383
1384/**
1385 *\brief Destroy resources associated with octeon device
1386 * @param pdev PCI device structure
1387 * @param ent unused
1388 */
1389static void octeon_destroy_resources(struct octeon_device *oct)
1390{
1391 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001392 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001393 struct octeon_device_priv *oct_priv =
1394 (struct octeon_device_priv *)oct->priv;
1395
1396 struct handshake *hs;
1397
1398 switch (atomic_read(&oct->status)) {
1399 case OCT_DEV_RUNNING:
1400 case OCT_DEV_CORE_OK:
1401
1402 /* No more instructions will be forwarded. */
1403 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1404
1405 oct->app_mode = CVM_DRV_INVALID_APP;
1406 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1407 lio_get_state_string(&oct->status));
1408
1409 schedule_timeout_uninterruptible(HZ / 10);
1410
1411 /* fallthrough */
1412 case OCT_DEV_HOST_OK:
1413
1414 /* fallthrough */
1415 case OCT_DEV_CONSOLE_INIT_DONE:
1416 /* Remove any consoles */
1417 octeon_remove_consoles(oct);
1418
1419 /* fallthrough */
1420 case OCT_DEV_IO_QUEUES_DONE:
1421 if (wait_for_pending_requests(oct))
1422 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1423
1424 if (lio_wait_for_instr_fetch(oct))
1425 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1426
1427 /* Disable the input and output queues now. No more packets will
1428 * arrive from Octeon, but we should wait for all packet
1429 * processing to finish.
1430 */
1431 oct->fn_list.disable_io_queues(oct);
1432
1433 if (lio_wait_for_oq_pkts(oct))
1434 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1435
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001436 /* fallthrough */
1437 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001438 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001439 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001440
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001441 if (oct->msix_on) {
1442 msix_entries = (struct msix_entry *)oct->msix_entries;
1443 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1444 /* clear the affinity_cpumask */
1445 irq_set_affinity_hint(msix_entries[i].vector,
1446 NULL);
1447 free_irq(msix_entries[i].vector,
1448 &oct->ioq_vector[i]);
1449 }
1450 /* non-iov vector's argument is oct struct */
1451 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001452
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001453 pci_disable_msix(oct->pci_dev);
1454 kfree(oct->msix_entries);
1455 oct->msix_entries = NULL;
1456 } else {
1457 /* Release the interrupt line */
1458 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001459
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001460 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1461 pci_disable_msi(oct->pci_dev);
1462 }
1463
Rick Farrington0c88a762017-03-13 12:58:04 -07001464 kfree(oct->irq_name_storage);
1465 oct->irq_name_storage = NULL;
1466
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001467 /* fallthrough */
1468 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001469 if (OCTEON_CN23XX_PF(oct))
1470 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001471
1472 /* fallthrough */
1473 case OCT_DEV_MBOX_SETUP_DONE:
1474 if (OCTEON_CN23XX_PF(oct))
1475 oct->fn_list.free_mbox(oct);
1476
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001477 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001478 case OCT_DEV_IN_RESET:
1479 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001480 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001481 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001482 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001483 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001484 continue;
1485 octeon_delete_droq(oct, i);
1486 }
1487
1488 /* Force any pending handshakes to complete */
1489 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1490 hs = &handshake[i];
1491
1492 if (hs->pci_dev) {
1493 handshake[oct->octeon_id].init_ok = 0;
1494 complete(&handshake[oct->octeon_id].init);
1495 handshake[oct->octeon_id].started_ok = 0;
1496 complete(&handshake[oct->octeon_id].started);
1497 }
1498 }
1499
1500 /* fallthrough */
1501 case OCT_DEV_RESP_LIST_INIT_DONE:
1502 octeon_delete_response_list(oct);
1503
1504 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001505 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001506 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001507 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001508 continue;
1509 octeon_delete_instr_queue(oct, i);
1510 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001511#ifdef CONFIG_PCI_IOV
1512 if (oct->sriov_info.sriov_enabled)
1513 pci_disable_sriov(oct->pci_dev);
1514#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001515 /* fallthrough */
1516 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1517 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001518
1519 /* fallthrough */
1520 case OCT_DEV_DISPATCH_INIT_DONE:
1521 octeon_delete_dispatch_list(oct);
1522 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1523
1524 /* fallthrough */
1525 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001526 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001527 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1528 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001529
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001530 octeon_unmap_pci_barx(oct, 0);
1531 octeon_unmap_pci_barx(oct, 1);
1532
1533 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001534 case OCT_DEV_PCI_ENABLE_DONE:
1535 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001536 /* Disable the device, releasing the PCI INT */
1537 pci_disable_device(oct->pci_dev);
1538
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001539 /* fallthrough */
1540 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001541 /* Nothing to be done here either */
1542 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001543 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001544
1545 tasklet_kill(&oct_priv->droq_tasklet);
1546}
1547
1548/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001549 * \brief Callback for rx ctrl
1550 * @param status status of request
1551 * @param buf pointer to resp structure
1552 */
1553static void rx_ctl_callback(struct octeon_device *oct,
1554 u32 status,
1555 void *buf)
1556{
1557 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1558 struct liquidio_rx_ctl_context *ctx;
1559
1560 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1561
1562 oct = lio_get_device(ctx->octeon_id);
1563 if (status)
1564 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1565 CVM_CAST64(status));
1566 WRITE_ONCE(ctx->cond, 1);
1567
1568 /* This barrier is required to be sure that the response has been
1569 * written fully before waking up the handler
1570 */
1571 wmb();
1572
1573 wake_up_interruptible(&ctx->wc);
1574}
1575
1576/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001577 * \brief Send Rx control command
1578 * @param lio per-network private data
1579 * @param start_stop whether to start or stop
1580 */
1581static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1582{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001583 struct octeon_soft_command *sc;
1584 struct liquidio_rx_ctl_context *ctx;
1585 union octnet_cmd *ncmd;
1586 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1587 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1588 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001589
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001590 if (oct->props[lio->ifidx].rx_on == start_stop)
1591 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001592
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001593 sc = (struct octeon_soft_command *)
1594 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1595 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001596
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001597 ncmd = (union octnet_cmd *)sc->virtdptr;
1598 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1599
1600 WRITE_ONCE(ctx->cond, 0);
1601 ctx->octeon_id = lio_get_device_id(oct);
1602 init_waitqueue_head(&ctx->wc);
1603
1604 ncmd->u64 = 0;
1605 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1606 ncmd->s.param1 = start_stop;
1607
1608 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1609
1610 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1611
1612 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1613 OPCODE_NIC_CMD, 0, 0, 0);
1614
1615 sc->callback = rx_ctl_callback;
1616 sc->callback_arg = sc;
1617 sc->wait_time = 5000;
1618
1619 retval = octeon_send_soft_command(oct, sc);
1620 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001621 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001622 } else {
1623 /* Sleep on a wait queue till the cond flag indicates that the
1624 * response arrived or timed-out.
1625 */
1626 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1627 return;
1628 oct->props[lio->ifidx].rx_on = start_stop;
1629 }
1630
1631 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001632}
1633
1634/**
1635 * \brief Destroy NIC device interface
1636 * @param oct octeon device
1637 * @param ifidx which interface to destroy
1638 *
1639 * Cleanup associated with each interface for an Octeon device when NIC
1640 * module is being unloaded or if initialization fails during load.
1641 */
1642static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1643{
1644 struct net_device *netdev = oct->props[ifidx].netdev;
1645 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001646 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001647
1648 if (!netdev) {
1649 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1650 __func__, ifidx);
1651 return;
1652 }
1653
1654 lio = GET_LIO(netdev);
1655
1656 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1657
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001658 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001659 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001660
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001661 if (oct->props[lio->ifidx].napi_enabled == 1) {
1662 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1663 napi_disable(napi);
1664
1665 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001666
1667 if (OCTEON_CN23XX_PF(oct))
1668 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001669 }
1670
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001671 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1672 unregister_netdev(netdev);
1673
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001674 cleanup_link_status_change_wq(netdev);
1675
Satanand Burla031d4f12017-03-22 11:31:13 -07001676 cleanup_rx_oom_poll_fn(netdev);
1677
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001678 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001679
1680 free_netdev(netdev);
1681
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001682 oct->props[ifidx].gmxport = -1;
1683
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001684 oct->props[ifidx].netdev = NULL;
1685}
1686
1687/**
1688 * \brief Stop complete NIC functionality
1689 * @param oct octeon device
1690 */
1691static int liquidio_stop_nic_module(struct octeon_device *oct)
1692{
1693 int i, j;
1694 struct lio *lio;
1695
1696 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1697 if (!oct->ifcount) {
1698 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1699 return 1;
1700 }
1701
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001702 spin_lock_bh(&oct->cmd_resp_wqlock);
1703 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1704 spin_unlock_bh(&oct->cmd_resp_wqlock);
1705
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001706 for (i = 0; i < oct->ifcount; i++) {
1707 lio = GET_LIO(oct->props[i].netdev);
1708 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001709 octeon_unregister_droq_ops(oct,
1710 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001711 }
1712
1713 for (i = 0; i < oct->ifcount; i++)
1714 liquidio_destroy_nic_device(oct, i);
1715
1716 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1717 return 0;
1718}
1719
1720/**
1721 * \brief Cleans up resources at unload time
1722 * @param pdev PCI device structure
1723 */
1724static void liquidio_remove(struct pci_dev *pdev)
1725{
1726 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1727
1728 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1729
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001730 if (oct_dev->watchdog_task)
1731 kthread_stop(oct_dev->watchdog_task);
1732
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001733 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1734 liquidio_stop_nic_module(oct_dev);
1735
1736 /* Reset the octeon device and cleanup all memory allocated for
1737 * the octeon device by driver.
1738 */
1739 octeon_destroy_resources(oct_dev);
1740
1741 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1742
1743 /* This octeon device has been removed. Update the global
1744 * data structure to reflect this. Free the device structure.
1745 */
1746 octeon_free_device_mem(oct_dev);
1747}
1748
1749/**
1750 * \brief Identify the Octeon device and to map the BAR address space
1751 * @param oct octeon device
1752 */
1753static int octeon_chip_specific_setup(struct octeon_device *oct)
1754{
1755 u32 dev_id, rev_id;
1756 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001757 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001758
1759 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1760 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1761 oct->rev_id = rev_id & 0xff;
1762
1763 switch (dev_id) {
1764 case OCTEON_CN68XX_PCIID:
1765 oct->chip_id = OCTEON_CN68XX;
1766 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001767 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001768 break;
1769
1770 case OCTEON_CN66XX_PCIID:
1771 oct->chip_id = OCTEON_CN66XX;
1772 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001773 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001774 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001775
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001776 case OCTEON_CN23XX_PCIID_PF:
1777 oct->chip_id = OCTEON_CN23XX_PF_VID;
1778 ret = setup_cn23xx_octeon_pf_device(oct);
1779 s = "CN23XX";
1780 break;
1781
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001782 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001783 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001784 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1785 dev_id);
1786 }
1787
1788 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001789 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001790 OCTEON_MAJOR_REV(oct),
1791 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001792 octeon_get_conf(oct)->card_name,
1793 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001794
1795 return ret;
1796}
1797
1798/**
1799 * \brief PCI initialization for each Octeon device.
1800 * @param oct octeon device
1801 */
1802static int octeon_pci_os_setup(struct octeon_device *oct)
1803{
1804 /* setup PCI stuff first */
1805 if (pci_enable_device(oct->pci_dev)) {
1806 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1807 return 1;
1808 }
1809
1810 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1811 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001812 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001813 return 1;
1814 }
1815
1816 /* Enable PCI DMA Master. */
1817 pci_set_master(oct->pci_dev);
1818
1819 return 0;
1820}
1821
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001822static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1823{
1824 int q = 0;
1825
1826 if (netif_is_multiqueue(lio->netdev))
1827 q = skb->queue_mapping % lio->linfo.num_txpciq;
1828
1829 return q;
1830}
1831
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001832/**
1833 * \brief Check Tx queue state for a given network buffer
1834 * @param lio per-network private data
1835 * @param skb network buffer
1836 */
1837static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1838{
1839 int q = 0, iq = 0;
1840
1841 if (netif_is_multiqueue(lio->netdev)) {
1842 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001843 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001844 } else {
1845 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001846 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001847 }
1848
1849 if (octnet_iq_is_full(lio->oct_dev, iq))
1850 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001851
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001852 if (__netif_subqueue_stopped(lio->netdev, q)) {
1853 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001854 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001855 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001856 return 1;
1857}
1858
1859/**
1860 * \brief Unmap and free network buffer
1861 * @param buf buffer
1862 */
1863static void free_netbuf(void *buf)
1864{
1865 struct sk_buff *skb;
1866 struct octnet_buf_free_info *finfo;
1867 struct lio *lio;
1868
1869 finfo = (struct octnet_buf_free_info *)buf;
1870 skb = finfo->skb;
1871 lio = finfo->lio;
1872
1873 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1874 DMA_TO_DEVICE);
1875
1876 check_txq_state(lio, skb);
1877
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001878 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001879}
1880
1881/**
1882 * \brief Unmap and free gather buffer
1883 * @param buf buffer
1884 */
1885static void free_netsgbuf(void *buf)
1886{
1887 struct octnet_buf_free_info *finfo;
1888 struct sk_buff *skb;
1889 struct lio *lio;
1890 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001891 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001892
1893 finfo = (struct octnet_buf_free_info *)buf;
1894 skb = finfo->skb;
1895 lio = finfo->lio;
1896 g = finfo->g;
1897 frags = skb_shinfo(skb)->nr_frags;
1898
1899 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1900 g->sg[0].ptr[0], (skb->len - skb->data_len),
1901 DMA_TO_DEVICE);
1902
1903 i = 1;
1904 while (frags--) {
1905 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1906
1907 pci_unmap_page((lio->oct_dev)->pci_dev,
1908 g->sg[(i >> 2)].ptr[(i & 3)],
1909 frag->size, DMA_TO_DEVICE);
1910 i++;
1911 }
1912
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001913 iq = skb_iq(lio, skb);
1914 spin_lock(&lio->glist_lock[iq]);
1915 list_add_tail(&g->list, &lio->glist[iq]);
1916 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001917
1918 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1919
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001920 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001921}
1922
1923/**
1924 * \brief Unmap and free gather buffer with response
1925 * @param buf buffer
1926 */
1927static void free_netsgbuf_with_resp(void *buf)
1928{
1929 struct octeon_soft_command *sc;
1930 struct octnet_buf_free_info *finfo;
1931 struct sk_buff *skb;
1932 struct lio *lio;
1933 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001934 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001935
1936 sc = (struct octeon_soft_command *)buf;
1937 skb = (struct sk_buff *)sc->callback_arg;
1938 finfo = (struct octnet_buf_free_info *)&skb->cb;
1939
1940 lio = finfo->lio;
1941 g = finfo->g;
1942 frags = skb_shinfo(skb)->nr_frags;
1943
1944 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1945 g->sg[0].ptr[0], (skb->len - skb->data_len),
1946 DMA_TO_DEVICE);
1947
1948 i = 1;
1949 while (frags--) {
1950 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1951
1952 pci_unmap_page((lio->oct_dev)->pci_dev,
1953 g->sg[(i >> 2)].ptr[(i & 3)],
1954 frag->size, DMA_TO_DEVICE);
1955 i++;
1956 }
1957
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001958 iq = skb_iq(lio, skb);
1959
1960 spin_lock(&lio->glist_lock[iq]);
1961 list_add_tail(&g->list, &lio->glist[iq]);
1962 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001963
1964 /* Don't free the skb yet */
1965
1966 check_txq_state(lio, skb);
1967}
1968
1969/**
1970 * \brief Adjust ptp frequency
1971 * @param ptp PTP clock info
1972 * @param ppb how much to adjust by, in parts-per-billion
1973 */
1974static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1975{
1976 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1977 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1978 u64 comp, delta;
1979 unsigned long flags;
1980 bool neg_adj = false;
1981
1982 if (ppb < 0) {
1983 neg_adj = true;
1984 ppb = -ppb;
1985 }
1986
1987 /* The hardware adds the clock compensation value to the
1988 * PTP clock on every coprocessor clock cycle, so we
1989 * compute the delta in terms of coprocessor clocks.
1990 */
1991 delta = (u64)ppb << 32;
1992 do_div(delta, oct->coproc_clock_rate);
1993
1994 spin_lock_irqsave(&lio->ptp_lock, flags);
1995 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1996 if (neg_adj)
1997 comp -= delta;
1998 else
1999 comp += delta;
2000 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2001 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2002
2003 return 0;
2004}
2005
2006/**
2007 * \brief Adjust ptp time
2008 * @param ptp PTP clock info
2009 * @param delta how much to adjust by, in nanosecs
2010 */
2011static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2012{
2013 unsigned long flags;
2014 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2015
2016 spin_lock_irqsave(&lio->ptp_lock, flags);
2017 lio->ptp_adjust += delta;
2018 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2019
2020 return 0;
2021}
2022
2023/**
2024 * \brief Get hardware clock time, including any adjustment
2025 * @param ptp PTP clock info
2026 * @param ts timespec
2027 */
2028static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2029 struct timespec64 *ts)
2030{
2031 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002032 unsigned long flags;
2033 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2034 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2035
2036 spin_lock_irqsave(&lio->ptp_lock, flags);
2037 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2038 ns += lio->ptp_adjust;
2039 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2040
Kefeng Wang286af312016-01-27 17:34:37 +08002041 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002042
2043 return 0;
2044}
2045
2046/**
2047 * \brief Set hardware clock time. Reset adjustment
2048 * @param ptp PTP clock info
2049 * @param ts timespec
2050 */
2051static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2052 const struct timespec64 *ts)
2053{
2054 u64 ns;
2055 unsigned long flags;
2056 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2057 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2058
2059 ns = timespec_to_ns(ts);
2060
2061 spin_lock_irqsave(&lio->ptp_lock, flags);
2062 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2063 lio->ptp_adjust = 0;
2064 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2065
2066 return 0;
2067}
2068
2069/**
2070 * \brief Check if PTP is enabled
2071 * @param ptp PTP clock info
2072 * @param rq request
2073 * @param on is it on
2074 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002075static int
2076liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2077 struct ptp_clock_request *rq __attribute__((unused)),
2078 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002079{
2080 return -EOPNOTSUPP;
2081}
2082
2083/**
2084 * \brief Open PTP clock source
2085 * @param netdev network device
2086 */
2087static void oct_ptp_open(struct net_device *netdev)
2088{
2089 struct lio *lio = GET_LIO(netdev);
2090 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2091
2092 spin_lock_init(&lio->ptp_lock);
2093
2094 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2095 lio->ptp_info.owner = THIS_MODULE;
2096 lio->ptp_info.max_adj = 250000000;
2097 lio->ptp_info.n_alarm = 0;
2098 lio->ptp_info.n_ext_ts = 0;
2099 lio->ptp_info.n_per_out = 0;
2100 lio->ptp_info.pps = 0;
2101 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2102 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2103 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2104 lio->ptp_info.settime64 = liquidio_ptp_settime;
2105 lio->ptp_info.enable = liquidio_ptp_enable;
2106
2107 lio->ptp_adjust = 0;
2108
2109 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2110 &oct->pci_dev->dev);
2111
2112 if (IS_ERR(lio->ptp_clock))
2113 lio->ptp_clock = NULL;
2114}
2115
2116/**
2117 * \brief Init PTP clock
2118 * @param oct octeon device
2119 */
2120static void liquidio_ptp_init(struct octeon_device *oct)
2121{
2122 u64 clock_comp, cfg;
2123
2124 clock_comp = (u64)NSEC_PER_SEC << 32;
2125 do_div(clock_comp, oct->coproc_clock_rate);
2126 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2127
2128 /* Enable */
2129 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2130 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2131}
2132
2133/**
2134 * \brief Load firmware to device
2135 * @param oct octeon device
2136 *
2137 * Maps device to firmware filename, requests firmware, and downloads it
2138 */
2139static int load_firmware(struct octeon_device *oct)
2140{
2141 int ret = 0;
2142 const struct firmware *fw;
2143 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2144 char *tmp_fw_type;
2145
2146 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2147 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2148 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2149 return ret;
2150 }
2151
2152 if (fw_type[0] == '\0')
2153 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2154 else
2155 tmp_fw_type = fw_type;
2156
2157 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2158 octeon_get_conf(oct)->card_name, tmp_fw_type,
2159 LIO_FW_NAME_SUFFIX);
2160
2161 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2162 if (ret) {
2163 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2164 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002165 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002166 return ret;
2167 }
2168
2169 ret = octeon_download_firmware(oct, fw->data, fw->size);
2170
2171 release_firmware(fw);
2172
2173 return ret;
2174}
2175
2176/**
2177 * \brief Setup output queue
2178 * @param oct octeon device
2179 * @param q_no which queue
2180 * @param num_descs how many descriptors
2181 * @param desc_size size of each descriptor
2182 * @param app_ctx application context
2183 */
2184static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2185 int desc_size, void *app_ctx)
2186{
2187 int ret_val = 0;
2188
2189 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2190 /* droq creation and local register settings. */
2191 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302192 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002193 return ret_val;
2194
2195 if (ret_val == 1) {
2196 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2197 return 0;
2198 }
2199 /* tasklet creation for the droq */
2200
2201 /* Enable the droq queues */
2202 octeon_set_droq_pkt_op(oct, q_no, 1);
2203
2204 /* Send Credit for Octeon Output queues. Credits are always
2205 * sent after the output queue is enabled.
2206 */
2207 writel(oct->droq[q_no]->max_count,
2208 oct->droq[q_no]->pkts_credit_reg);
2209
2210 return ret_val;
2211}
2212
2213/**
2214 * \brief Callback for getting interface configuration
2215 * @param status status of request
2216 * @param buf pointer to resp structure
2217 */
2218static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002219 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002220 void *buf)
2221{
2222 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2223 struct liquidio_if_cfg_resp *resp;
2224 struct liquidio_if_cfg_context *ctx;
2225
2226 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002227 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002228
2229 oct = lio_get_device(ctx->octeon_id);
2230 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002231 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2232 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002233 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002234
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002235 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2236 resp->cfg_info.liquidio_firmware_version);
2237
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002238 /* This barrier is required to be sure that the response has been
2239 * written fully before waking up the handler
2240 */
2241 wmb();
2242
2243 wake_up_interruptible(&ctx->wc);
2244}
2245
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002246/** Routine to push packets arriving on Octeon interface upto network layer.
2247 * @param oct_id - octeon device id.
2248 * @param skbuff - skbuff struct to be passed to network layer.
2249 * @param len - size of total data received.
2250 * @param rh - Control header associated with the packet
2251 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002252 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002253 */
2254static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002255liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002256 void *skbuff,
2257 u32 len,
2258 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002259 void *param,
2260 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002261{
2262 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002263 struct sk_buff *skb = (struct sk_buff *)skbuff;
2264 struct skb_shared_hwtstamps *shhwtstamps;
2265 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002266 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002267 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002268 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002269 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2270 napi);
2271 if (netdev) {
2272 int packet_was_received;
2273 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002274 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002275
2276 /* Do not proceed if the interface is not in RUNNING state. */
2277 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2278 recv_buffer_free(skb);
2279 droq->stats.rx_dropped++;
2280 return;
2281 }
2282
2283 skb->dev = netdev;
2284
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002285 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002286 if (likely(len > MIN_SKB_SIZE)) {
2287 struct octeon_skb_page_info *pg_info;
2288 unsigned char *va;
2289
2290 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2291 if (pg_info->page) {
2292 /* For Paged allocation use the frags */
2293 va = page_address(pg_info->page) +
2294 pg_info->page_offset;
2295 memcpy(skb->data, va, MIN_SKB_SIZE);
2296 skb_put(skb, MIN_SKB_SIZE);
2297 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2298 pg_info->page,
2299 pg_info->page_offset +
2300 MIN_SKB_SIZE,
2301 len - MIN_SKB_SIZE,
2302 LIO_RXBUFFER_SZ);
2303 }
2304 } else {
2305 struct octeon_skb_page_info *pg_info =
2306 ((struct octeon_skb_page_info *)(skb->cb));
2307 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2308 + pg_info->page_offset, len);
2309 skb_put(skb, len);
2310 put_page(pg_info->page);
2311 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002312
Prasad Kannegantide28c992017-01-09 14:42:40 -08002313 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2314
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002315 if (((oct->chip_id == OCTEON_CN66XX) ||
2316 (oct->chip_id == OCTEON_CN68XX)) &&
2317 ptp_enable) {
2318 if (rh->r_dh.has_hwtstamp) {
2319 /* timestamp is included from the hardware at
2320 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002321 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002322 if (ifstate_check
2323 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2324 /* Nanoseconds are in the first 64-bits
2325 * of the packet.
2326 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002327 memcpy(&ns, (skb->data + r_dh_off),
2328 sizeof(ns));
2329 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002330 shhwtstamps = skb_hwtstamps(skb);
2331 shhwtstamps->hwtstamp =
2332 ns_to_ktime(ns +
2333 lio->ptp_adjust);
2334 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002335 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002336 }
2337
Prasad Kannegantide28c992017-01-09 14:42:40 -08002338 if (rh->r_dh.has_hash) {
2339 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2340 u32 hash = be32_to_cpu(*hash_be);
2341
2342 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2343 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2344 }
2345
2346 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2347
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002348 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002349 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002350 (((rh->r_dh.encap_on) &&
2351 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2352 (!(rh->r_dh.encap_on) &&
2353 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002354 /* checksum has already been verified */
2355 skb->ip_summed = CHECKSUM_UNNECESSARY;
2356 else
2357 skb->ip_summed = CHECKSUM_NONE;
2358
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002359 /* Setting Encapsulation field on basis of status received
2360 * from the firmware
2361 */
2362 if (rh->r_dh.encap_on) {
2363 skb->encapsulation = 1;
2364 skb->csum_level = 1;
2365 droq->stats.rx_vxlan++;
2366 }
2367
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002368 /* inbound VLAN tag */
2369 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2370 (rh->r_dh.vlan != 0)) {
2371 u16 vid = rh->r_dh.vlan;
2372 u16 priority = rh->r_dh.priority;
2373
2374 vtag = priority << 13 | vid;
2375 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2376 }
2377
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002378 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2379
2380 if (packet_was_received) {
2381 droq->stats.rx_bytes_received += len;
2382 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002383 } else {
2384 droq->stats.rx_dropped++;
2385 netif_info(lio, rx_err, lio->netdev,
2386 "droq:%d error rx_dropped:%llu\n",
2387 droq->q_no, droq->stats.rx_dropped);
2388 }
2389
2390 } else {
2391 recv_buffer_free(skb);
2392 }
2393}
2394
2395/**
2396 * \brief wrapper for calling napi_schedule
2397 * @param param parameters to pass to napi_schedule
2398 *
2399 * Used when scheduling on different CPUs
2400 */
2401static void napi_schedule_wrapper(void *param)
2402{
2403 struct napi_struct *napi = param;
2404
2405 napi_schedule(napi);
2406}
2407
2408/**
2409 * \brief callback when receive interrupt occurs and we are in NAPI mode
2410 * @param arg pointer to octeon output queue
2411 */
2412static void liquidio_napi_drv_callback(void *arg)
2413{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002414 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002415 struct octeon_droq *droq = arg;
2416 int this_cpu = smp_processor_id();
2417
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002418 oct = droq->oct_dev;
2419
2420 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2421 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002422 } else {
2423 struct call_single_data *csd = &droq->csd;
2424
2425 csd->func = napi_schedule_wrapper;
2426 csd->info = &droq->napi;
2427 csd->flags = 0;
2428
2429 smp_call_function_single_async(droq->cpu_id, csd);
2430 }
2431}
2432
2433/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002434 * \brief Entry point for NAPI polling
2435 * @param napi NAPI structure
2436 * @param budget maximum number of items to process
2437 */
2438static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2439{
2440 struct octeon_droq *droq;
2441 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002442 int tx_done = 0, iq_no;
2443 struct octeon_instr_queue *iq;
2444 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002445
2446 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002447 oct = droq->oct_dev;
2448 iq_no = droq->q_no;
2449 /* Handle Droq descriptors */
2450 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2451 POLL_EVENT_PROCESS_PKTS,
2452 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002453
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002454 /* Flush the instruction queue */
2455 iq = oct->instr_queue[iq_no];
2456 if (iq) {
2457 /* Process iq buffers with in the budget limits */
Derek Chickles60889862017-01-06 17:16:12 -08002458 tx_done = octeon_flush_iq(oct, iq, budget);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002459 /* Update iq read-index rather than waiting for next interrupt.
2460 * Return back if tx_done is false.
2461 */
2462 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002463 } else {
2464 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2465 __func__, iq_no);
2466 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002467
Satanand Burlacdb478e2017-01-31 13:04:42 -08002468 /* force enable interrupt if reg cnts are high to avoid wraparound */
2469 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002470 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002471 (droq->pkt_count >= MAX_REG_CNT)) {
2472 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002473 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002474 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2475 POLL_EVENT_ENABLE_INTR, 0);
2476 return 0;
2477 }
2478
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002479 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002480}
2481
2482/**
2483 * \brief Setup input and output queues
2484 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002485 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002486 *
2487 * Note: Queues are with respect to the octeon device. Thus
2488 * an input queue is for egress packets, and output queues
2489 * are for ingress packets.
2490 */
2491static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002492 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002493{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002494 struct octeon_droq_ops droq_ops;
2495 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002496 static int cpu_id;
2497 static int cpu_id_modulus;
2498 struct octeon_droq *droq;
2499 struct napi_struct *napi;
2500 int q, q_no, retval = 0;
2501 struct lio *lio;
2502 int num_tx_descs;
2503
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002504 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002505
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002506 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002507
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002508 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2509
2510 droq_ops.fptr = liquidio_push_packet;
2511 droq_ops.farg = (void *)netdev;
2512
2513 droq_ops.poll_mode = 1;
2514 droq_ops.napi_fn = liquidio_napi_drv_callback;
2515 cpu_id = 0;
2516 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002517
2518 /* set up DROQs. */
2519 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002520 q_no = lio->linfo.rxpciq[q].s.q_no;
2521 dev_dbg(&octeon_dev->pci_dev->dev,
2522 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2523 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002524 retval = octeon_setup_droq(octeon_dev, q_no,
2525 CFG_GET_NUM_RX_DESCS_NIC_IF
2526 (octeon_get_conf(octeon_dev),
2527 lio->ifidx),
2528 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2529 (octeon_get_conf(octeon_dev),
2530 lio->ifidx), NULL);
2531 if (retval) {
2532 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002533 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002534 __func__);
2535 return 1;
2536 }
2537
2538 droq = octeon_dev->droq[q_no];
2539 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002540 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2541 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002542 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002543
2544 /* designate a CPU for this droq */
2545 droq->cpu_id = cpu_id;
2546 cpu_id++;
2547 if (cpu_id >= cpu_id_modulus)
2548 cpu_id = 0;
2549
2550 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2551 }
2552
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002553 if (OCTEON_CN23XX_PF(octeon_dev)) {
2554 /* 23XX PF can receive control messages (via the first PF-owned
2555 * droq) from the firmware even if the ethX interface is down,
2556 * so that's why poll_mode must be off for the first droq.
2557 */
2558 octeon_dev->droq[0]->ops.poll_mode = 0;
2559 }
2560
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002561 /* set up IQs. */
2562 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2563 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2564 (octeon_dev),
2565 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002566 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2567 lio->linfo.txpciq[q], num_tx_descs,
2568 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002569 if (retval) {
2570 dev_err(&octeon_dev->pci_dev->dev,
2571 " %s : Runtime IQ(TxQ) creation failed.\n",
2572 __func__);
2573 return 1;
2574 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002575
2576 if (octeon_dev->ioq_vector) {
2577 struct octeon_ioq_vector *ioq_vector;
2578
2579 ioq_vector = &octeon_dev->ioq_vector[q];
2580 netif_set_xps_queue(netdev,
2581 &ioq_vector->affinity_mask,
2582 ioq_vector->iq_index);
2583 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002584 }
2585
2586 return 0;
2587}
2588
2589/**
2590 * \brief Poll routine for checking transmit queue status
2591 * @param work work_struct data structure
2592 */
2593static void octnet_poll_check_txq_status(struct work_struct *work)
2594{
2595 struct cavium_wk *wk = (struct cavium_wk *)work;
2596 struct lio *lio = (struct lio *)wk->ctxptr;
2597
2598 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2599 return;
2600
2601 check_txq_status(lio);
2602 queue_delayed_work(lio->txq_status_wq.wq,
2603 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2604}
2605
2606/**
2607 * \brief Sets up the txq poll check
2608 * @param netdev network device
2609 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002610static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002611{
2612 struct lio *lio = GET_LIO(netdev);
2613 struct octeon_device *oct = lio->oct_dev;
2614
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302615 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2616 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002617 if (!lio->txq_status_wq.wq) {
2618 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002619 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002620 }
2621 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2622 octnet_poll_check_txq_status);
2623 lio->txq_status_wq.wk.ctxptr = lio;
2624 queue_delayed_work(lio->txq_status_wq.wq,
2625 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002626 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002627}
2628
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002629static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2630{
2631 struct lio *lio = GET_LIO(netdev);
2632
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002633 if (lio->txq_status_wq.wq) {
2634 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2635 destroy_workqueue(lio->txq_status_wq.wq);
2636 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002637}
2638
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002639/**
2640 * \brief Net device open for LiquidIO
2641 * @param netdev network device
2642 */
2643static int liquidio_open(struct net_device *netdev)
2644{
2645 struct lio *lio = GET_LIO(netdev);
2646 struct octeon_device *oct = lio->oct_dev;
2647 struct napi_struct *napi, *n;
2648
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002649 if (oct->props[lio->ifidx].napi_enabled == 0) {
2650 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2651 napi_enable(napi);
2652
2653 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002654
2655 if (OCTEON_CN23XX_PF(oct))
2656 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002657 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002658
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002659 if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
2660 ptp_enable)
2661 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002662
2663 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002664
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002665 /* Ready for link status updates */
2666 lio->intf_open = 1;
2667
2668 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2669
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002670 if (OCTEON_CN23XX_PF(oct)) {
2671 if (!oct->msix_on)
2672 if (setup_tx_poll_fn(netdev))
2673 return -1;
2674 } else {
2675 if (setup_tx_poll_fn(netdev))
2676 return -1;
2677 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002678
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002679 start_txq(netdev);
2680
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002681 /* tell Octeon to start forwarding packets to host */
2682 send_rx_ctrl_cmd(lio, 1);
2683
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002684 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2685 netdev->name);
2686
2687 return 0;
2688}
2689
2690/**
2691 * \brief Net device stop for LiquidIO
2692 * @param netdev network device
2693 */
2694static int liquidio_stop(struct net_device *netdev)
2695{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002696 struct lio *lio = GET_LIO(netdev);
2697 struct octeon_device *oct = lio->oct_dev;
2698
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002699 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2700
2701 netif_tx_disable(netdev);
2702
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002703 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002704 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002706 lio->linfo.link.s.link_up = 0;
2707 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002708
Felix Manlunascb2336b2017-01-11 17:09:02 -08002709 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002710 send_rx_ctrl_cmd(lio, 0);
2711
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002712 if (OCTEON_CN23XX_PF(oct)) {
2713 if (!oct->msix_on)
2714 cleanup_tx_poll_fn(netdev);
2715 } else {
2716 cleanup_tx_poll_fn(netdev);
2717 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002718
2719 if (lio->ptp_clock) {
2720 ptp_clock_unregister(lio->ptp_clock);
2721 lio->ptp_clock = NULL;
2722 }
2723
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002724 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002725
2726 return 0;
2727}
2728
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002729/**
2730 * \brief Converts a mask based on net device flags
2731 * @param netdev network device
2732 *
2733 * This routine generates a octnet_ifflags mask from the net device flags
2734 * received from the OS.
2735 */
2736static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2737{
2738 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2739
2740 if (netdev->flags & IFF_PROMISC)
2741 f |= OCTNET_IFFLAG_PROMISC;
2742
2743 if (netdev->flags & IFF_ALLMULTI)
2744 f |= OCTNET_IFFLAG_ALLMULTI;
2745
2746 if (netdev->flags & IFF_MULTICAST) {
2747 f |= OCTNET_IFFLAG_MULTICAST;
2748
2749 /* Accept all multicast addresses if there are more than we
2750 * can handle
2751 */
2752 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2753 f |= OCTNET_IFFLAG_ALLMULTI;
2754 }
2755
2756 if (netdev->flags & IFF_BROADCAST)
2757 f |= OCTNET_IFFLAG_BROADCAST;
2758
2759 return f;
2760}
2761
2762/**
2763 * \brief Net device set_multicast_list
2764 * @param netdev network device
2765 */
2766static void liquidio_set_mcast_list(struct net_device *netdev)
2767{
2768 struct lio *lio = GET_LIO(netdev);
2769 struct octeon_device *oct = lio->oct_dev;
2770 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002771 struct netdev_hw_addr *ha;
2772 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002773 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002774 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2775
2776 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2777
2778 /* Create a ctrl pkt command to be sent to core app. */
2779 nctrl.ncmd.u64 = 0;
2780 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002781 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2782 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002783 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002784 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002785 nctrl.netpndev = (u64)netdev;
2786 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2787
2788 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002789 mc = &nctrl.udd[0];
2790 netdev_for_each_mc_addr(ha, netdev) {
2791 *mc = 0;
2792 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2793 /* no need to swap bytes */
2794
2795 if (++mc > &nctrl.udd[mc_count])
2796 break;
2797 }
2798
2799 /* Apparently, any activity in this call from the kernel has to
2800 * be atomic. So we won't wait for response.
2801 */
2802 nctrl.wait_time = 0;
2803
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002804 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002805 if (ret < 0) {
2806 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2807 ret);
2808 }
2809}
2810
2811/**
2812 * \brief Net device set_mac_address
2813 * @param netdev network device
2814 */
2815static int liquidio_set_mac(struct net_device *netdev, void *p)
2816{
2817 int ret = 0;
2818 struct lio *lio = GET_LIO(netdev);
2819 struct octeon_device *oct = lio->oct_dev;
2820 struct sockaddr *addr = (struct sockaddr *)p;
2821 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002822
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002823 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002824 return -EADDRNOTAVAIL;
2825
2826 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2827
2828 nctrl.ncmd.u64 = 0;
2829 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002830 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002831 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002832 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002833 nctrl.netpndev = (u64)netdev;
2834 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2835 nctrl.wait_time = 100;
2836
2837 nctrl.udd[0] = 0;
2838 /* The MAC Address is presented in network byte order. */
2839 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2840
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002841 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002842 if (ret < 0) {
2843 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2844 return -ENOMEM;
2845 }
2846 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2847 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2848
2849 return 0;
2850}
2851
2852/**
2853 * \brief Net device get_stats
2854 * @param netdev network device
2855 */
2856static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2857{
2858 struct lio *lio = GET_LIO(netdev);
2859 struct net_device_stats *stats = &netdev->stats;
2860 struct octeon_device *oct;
2861 u64 pkts = 0, drop = 0, bytes = 0;
2862 struct oct_droq_stats *oq_stats;
2863 struct oct_iq_stats *iq_stats;
2864 int i, iq_no, oq_no;
2865
2866 oct = lio->oct_dev;
2867
2868 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002869 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002870 iq_stats = &oct->instr_queue[iq_no]->stats;
2871 pkts += iq_stats->tx_done;
2872 drop += iq_stats->tx_dropped;
2873 bytes += iq_stats->tx_tot_bytes;
2874 }
2875
2876 stats->tx_packets = pkts;
2877 stats->tx_bytes = bytes;
2878 stats->tx_dropped = drop;
2879
2880 pkts = 0;
2881 drop = 0;
2882 bytes = 0;
2883
2884 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002885 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002886 oq_stats = &oct->droq[oq_no]->stats;
2887 pkts += oq_stats->rx_pkts_received;
2888 drop += (oq_stats->rx_dropped +
2889 oq_stats->dropped_nodispatch +
2890 oq_stats->dropped_toomany +
2891 oq_stats->dropped_nomem);
2892 bytes += oq_stats->rx_bytes_received;
2893 }
2894
2895 stats->rx_bytes = bytes;
2896 stats->rx_packets = pkts;
2897 stats->rx_dropped = drop;
2898
2899 return stats;
2900}
2901
2902/**
2903 * \brief Net device change_mtu
2904 * @param netdev network device
2905 */
2906static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2907{
2908 struct lio *lio = GET_LIO(netdev);
2909 struct octeon_device *oct = lio->oct_dev;
2910 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002911 int ret = 0;
2912
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002913 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2914
2915 nctrl.ncmd.u64 = 0;
2916 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002917 nctrl.ncmd.s.param1 = new_mtu;
2918 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002919 nctrl.wait_time = 100;
2920 nctrl.netpndev = (u64)netdev;
2921 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2922
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002923 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002924 if (ret < 0) {
2925 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2926 return -1;
2927 }
2928
2929 lio->mtu = new_mtu;
2930
2931 return 0;
2932}
2933
2934/**
2935 * \brief Handler for SIOCSHWTSTAMP ioctl
2936 * @param netdev network device
2937 * @param ifr interface request
2938 * @param cmd command
2939 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002940static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002941{
2942 struct hwtstamp_config conf;
2943 struct lio *lio = GET_LIO(netdev);
2944
2945 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2946 return -EFAULT;
2947
2948 if (conf.flags)
2949 return -EINVAL;
2950
2951 switch (conf.tx_type) {
2952 case HWTSTAMP_TX_ON:
2953 case HWTSTAMP_TX_OFF:
2954 break;
2955 default:
2956 return -ERANGE;
2957 }
2958
2959 switch (conf.rx_filter) {
2960 case HWTSTAMP_FILTER_NONE:
2961 break;
2962 case HWTSTAMP_FILTER_ALL:
2963 case HWTSTAMP_FILTER_SOME:
2964 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2965 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2966 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2967 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2968 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2969 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2970 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2971 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2972 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2973 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2974 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2975 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2976 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2977 break;
2978 default:
2979 return -ERANGE;
2980 }
2981
2982 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2983 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2984
2985 else
2986 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2987
2988 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2989}
2990
2991/**
2992 * \brief ioctl handler
2993 * @param netdev network device
2994 * @param ifr interface request
2995 * @param cmd command
2996 */
2997static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2998{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002999 struct lio *lio = GET_LIO(netdev);
3000
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003001 switch (cmd) {
3002 case SIOCSHWTSTAMP:
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003003 if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
3004 lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
3005 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003006 default:
3007 return -EOPNOTSUPP;
3008 }
3009}
3010
3011/**
3012 * \brief handle a Tx timestamp response
3013 * @param status response status
3014 * @param buf pointer to skb
3015 */
3016static void handle_timestamp(struct octeon_device *oct,
3017 u32 status,
3018 void *buf)
3019{
3020 struct octnet_buf_free_info *finfo;
3021 struct octeon_soft_command *sc;
3022 struct oct_timestamp_resp *resp;
3023 struct lio *lio;
3024 struct sk_buff *skb = (struct sk_buff *)buf;
3025
3026 finfo = (struct octnet_buf_free_info *)skb->cb;
3027 lio = finfo->lio;
3028 sc = finfo->sc;
3029 oct = lio->oct_dev;
3030 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3031
3032 if (status != OCTEON_REQUEST_DONE) {
3033 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3034 CVM_CAST64(status));
3035 resp->timestamp = 0;
3036 }
3037
3038 octeon_swap_8B_data(&resp->timestamp, 1);
3039
Colin Ian King19a6d152016-02-05 16:30:39 +00003040 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003041 struct skb_shared_hwtstamps ts;
3042 u64 ns = resp->timestamp;
3043
3044 netif_info(lio, tx_done, lio->netdev,
3045 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3046 skb, (unsigned long long)ns);
3047 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3048 skb_tstamp_tx(skb, &ts);
3049 }
3050
3051 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003052 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003053}
3054
3055/* \brief Send a data packet that will be timestamped
3056 * @param oct octeon device
3057 * @param ndata pointer to network data
3058 * @param finfo pointer to private network data
3059 */
3060static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3061 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003062 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003063{
3064 int retval;
3065 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003066 struct lio *lio;
3067 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003068 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003069
3070 lio = finfo->lio;
3071
3072 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3073 sizeof(struct oct_timestamp_resp));
3074 finfo->sc = sc;
3075
3076 if (!sc) {
3077 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3078 return IQ_SEND_FAILED;
3079 }
3080
3081 if (ndata->reqtype == REQTYPE_NORESP_NET)
3082 ndata->reqtype = REQTYPE_RESP_NET;
3083 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3084 ndata->reqtype = REQTYPE_RESP_NET_SG;
3085
3086 sc->callback = handle_timestamp;
3087 sc->callback_arg = finfo->skb;
3088 sc->iq_no = ndata->q_no;
3089
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003090 if (OCTEON_CN23XX_PF(oct))
3091 len = (u32)((struct octeon_instr_ih3 *)
3092 (&sc->cmd.cmd3.ih3))->dlengsz;
3093 else
3094 len = (u32)((struct octeon_instr_ih2 *)
3095 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003096
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003097 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003098
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003099 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003100 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003101
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003102 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003103 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3104 retval);
3105 octeon_free_soft_command(oct, sc);
3106 } else {
3107 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3108 }
3109
3110 return retval;
3111}
3112
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003113/** \brief Transmit networks packets to the Octeon interface
3114 * @param skbuff skbuff struct to be passed to network layer.
3115 * @param netdev pointer to network device
3116 * @returns whether the packet was transmitted to the device okay or not
3117 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3118 */
3119static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3120{
3121 struct lio *lio;
3122 struct octnet_buf_free_info *finfo;
3123 union octnic_cmd_setup cmdsetup;
3124 struct octnic_data_pkt ndata;
3125 struct octeon_device *oct;
3126 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003127 struct octeon_instr_irh *irh;
3128 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003129 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003130 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003131 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003132 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003133 u32 tag = 0;
3134
3135 lio = GET_LIO(netdev);
3136 oct = lio->oct_dev;
3137
3138 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003139 q_idx = skb->queue_mapping;
3140 q_idx = (q_idx % (lio->linfo.num_txpciq));
3141 tag = q_idx;
3142 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003143 } else {
3144 iq_no = lio->txq;
3145 }
3146
3147 stats = &oct->instr_queue[iq_no]->stats;
3148
3149 /* Check for all conditions in which the current packet cannot be
3150 * transmitted.
3151 */
3152 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003153 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003154 (skb->len <= 0)) {
3155 netif_info(lio, tx_err, lio->netdev,
3156 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003157 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003158 goto lio_xmit_failed;
3159 }
3160
3161 /* Use space in skb->cb to store info used to unmap and
3162 * free the buffers.
3163 */
3164 finfo = (struct octnet_buf_free_info *)skb->cb;
3165 finfo->lio = lio;
3166 finfo->skb = skb;
3167 finfo->sc = NULL;
3168
3169 /* Prepare the attributes for the data to be passed to OSI. */
3170 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3171
3172 ndata.buf = (void *)finfo;
3173
3174 ndata.q_no = iq_no;
3175
3176 if (netif_is_multiqueue(netdev)) {
3177 if (octnet_iq_is_full(oct, ndata.q_no)) {
3178 /* defer sending if queue is full */
3179 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3180 ndata.q_no);
3181 stats->tx_iq_busy++;
3182 return NETDEV_TX_BUSY;
3183 }
3184 } else {
3185 if (octnet_iq_is_full(oct, lio->txq)) {
3186 /* defer sending if queue is full */
3187 stats->tx_iq_busy++;
3188 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003189 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003190 return NETDEV_TX_BUSY;
3191 }
3192 }
3193 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003194 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003195 */
3196
3197 ndata.datasize = skb->len;
3198
3199 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003200 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003201
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003202 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3203 if (skb->encapsulation) {
3204 cmdsetup.s.tnl_csum = 1;
3205 stats->tx_vxlan++;
3206 } else {
3207 cmdsetup.s.transport_csum = 1;
3208 }
3209 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003210 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3211 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3212 cmdsetup.s.timestamp = 1;
3213 }
3214
3215 if (skb_shinfo(skb)->nr_frags == 0) {
3216 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003217 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003218
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003219 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003220 dptr = dma_map_single(&oct->pci_dev->dev,
3221 skb->data,
3222 skb->len,
3223 DMA_TO_DEVICE);
3224 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003225 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3226 __func__);
3227 return NETDEV_TX_BUSY;
3228 }
3229
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003230 if (OCTEON_CN23XX_PF(oct))
3231 ndata.cmd.cmd3.dptr = dptr;
3232 else
3233 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003234 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003235 ndata.reqtype = REQTYPE_NORESP_NET;
3236
3237 } else {
3238 int i, frags;
3239 struct skb_frag_struct *frag;
3240 struct octnic_gather *g;
3241
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003242 spin_lock(&lio->glist_lock[q_idx]);
3243 g = (struct octnic_gather *)
3244 list_delete_head(&lio->glist[q_idx]);
3245 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003246
3247 if (!g) {
3248 netif_info(lio, tx_err, lio->netdev,
3249 "Transmit scatter gather: glist null!\n");
3250 goto lio_xmit_failed;
3251 }
3252
3253 cmdsetup.s.gather = 1;
3254 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003255 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003256
3257 memset(g->sg, 0, g->sg_size);
3258
3259 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3260 skb->data,
3261 (skb->len - skb->data_len),
3262 DMA_TO_DEVICE);
3263 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3264 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3265 __func__);
3266 return NETDEV_TX_BUSY;
3267 }
3268 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3269
3270 frags = skb_shinfo(skb)->nr_frags;
3271 i = 1;
3272 while (frags--) {
3273 frag = &skb_shinfo(skb)->frags[i - 1];
3274
3275 g->sg[(i >> 2)].ptr[(i & 3)] =
3276 dma_map_page(&oct->pci_dev->dev,
3277 frag->page.p,
3278 frag->page_offset,
3279 frag->size,
3280 DMA_TO_DEVICE);
3281
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003282 if (dma_mapping_error(&oct->pci_dev->dev,
3283 g->sg[i >> 2].ptr[i & 3])) {
3284 dma_unmap_single(&oct->pci_dev->dev,
3285 g->sg[0].ptr[0],
3286 skb->len - skb->data_len,
3287 DMA_TO_DEVICE);
3288 for (j = 1; j < i; j++) {
3289 frag = &skb_shinfo(skb)->frags[j - 1];
3290 dma_unmap_page(&oct->pci_dev->dev,
3291 g->sg[j >> 2].ptr[j & 3],
3292 frag->size,
3293 DMA_TO_DEVICE);
3294 }
3295 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3296 __func__);
3297 return NETDEV_TX_BUSY;
3298 }
3299
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003300 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3301 i++;
3302 }
3303
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003304 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003305
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003306 if (OCTEON_CN23XX_PF(oct))
3307 ndata.cmd.cmd3.dptr = dptr;
3308 else
3309 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003310 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003311 finfo->g = g;
3312
3313 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3314 }
3315
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003316 if (OCTEON_CN23XX_PF(oct)) {
3317 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3318 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3319 } else {
3320 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3321 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3322 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003323
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003324 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003325 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3326 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003327 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003328 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003329
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003330 /* HW insert VLAN tag */
3331 if (skb_vlan_tag_present(skb)) {
3332 irh->priority = skb_vlan_tag_get(skb) >> 13;
3333 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3334 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003335
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003336 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003337 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003339 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003340 if (status == IQ_SEND_FAILED)
3341 goto lio_xmit_failed;
3342
3343 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3344
3345 if (status == IQ_SEND_STOP)
3346 stop_q(lio->netdev, q_idx);
3347
Florian Westphal860e9532016-05-03 16:33:13 +02003348 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003349
Satanand Burla80c8eae2017-01-26 11:52:35 -08003350 if (tx_info->s.gso_segs)
3351 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003352 else
3353 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003354 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003355
3356 return NETDEV_TX_OK;
3357
3358lio_xmit_failed:
3359 stats->tx_dropped++;
3360 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3361 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003362 if (dptr)
3363 dma_unmap_single(&oct->pci_dev->dev, dptr,
3364 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003365 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003366 return NETDEV_TX_OK;
3367}
3368
3369/** \brief Network device Tx timeout
3370 * @param netdev pointer to network device
3371 */
3372static void liquidio_tx_timeout(struct net_device *netdev)
3373{
3374 struct lio *lio;
3375
3376 lio = GET_LIO(netdev);
3377
3378 netif_info(lio, tx_err, lio->netdev,
3379 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3380 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003381 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003382 txqs_wake(netdev);
3383}
3384
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003385static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3386 __be16 proto __attribute__((unused)),
3387 u16 vid)
3388{
3389 struct lio *lio = GET_LIO(netdev);
3390 struct octeon_device *oct = lio->oct_dev;
3391 struct octnic_ctrl_pkt nctrl;
3392 int ret = 0;
3393
3394 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3395
3396 nctrl.ncmd.u64 = 0;
3397 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3398 nctrl.ncmd.s.param1 = vid;
3399 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3400 nctrl.wait_time = 100;
3401 nctrl.netpndev = (u64)netdev;
3402 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3403
3404 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3405 if (ret < 0) {
3406 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3407 ret);
3408 }
3409
3410 return ret;
3411}
3412
3413static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3414 __be16 proto __attribute__((unused)),
3415 u16 vid)
3416{
3417 struct lio *lio = GET_LIO(netdev);
3418 struct octeon_device *oct = lio->oct_dev;
3419 struct octnic_ctrl_pkt nctrl;
3420 int ret = 0;
3421
3422 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3423
3424 nctrl.ncmd.u64 = 0;
3425 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3426 nctrl.ncmd.s.param1 = vid;
3427 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3428 nctrl.wait_time = 100;
3429 nctrl.netpndev = (u64)netdev;
3430 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3431
3432 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3433 if (ret < 0) {
3434 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3435 ret);
3436 }
3437 return ret;
3438}
3439
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003440/** Sending command to enable/disable RX checksum offload
3441 * @param netdev pointer to network device
3442 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3443 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3444 * OCTNET_CMD_RXCSUM_DISABLE
3445 * @returns SUCCESS or FAILURE
3446 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003447static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3448 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003449{
3450 struct lio *lio = GET_LIO(netdev);
3451 struct octeon_device *oct = lio->oct_dev;
3452 struct octnic_ctrl_pkt nctrl;
3453 int ret = 0;
3454
3455 nctrl.ncmd.u64 = 0;
3456 nctrl.ncmd.s.cmd = command;
3457 nctrl.ncmd.s.param1 = rx_cmd;
3458 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3459 nctrl.wait_time = 100;
3460 nctrl.netpndev = (u64)netdev;
3461 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3462
3463 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3464 if (ret < 0) {
3465 dev_err(&oct->pci_dev->dev,
3466 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3467 ret);
3468 }
3469 return ret;
3470}
3471
3472/** Sending command to add/delete VxLAN UDP port to firmware
3473 * @param netdev pointer to network device
3474 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3475 * @param vxlan_port VxLAN port to be added or deleted
3476 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3477 * OCTNET_CMD_VXLAN_PORT_DEL
3478 * @returns SUCCESS or FAILURE
3479 */
3480static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3481 u16 vxlan_port, u8 vxlan_cmd_bit)
3482{
3483 struct lio *lio = GET_LIO(netdev);
3484 struct octeon_device *oct = lio->oct_dev;
3485 struct octnic_ctrl_pkt nctrl;
3486 int ret = 0;
3487
3488 nctrl.ncmd.u64 = 0;
3489 nctrl.ncmd.s.cmd = command;
3490 nctrl.ncmd.s.more = vxlan_cmd_bit;
3491 nctrl.ncmd.s.param1 = vxlan_port;
3492 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3493 nctrl.wait_time = 100;
3494 nctrl.netpndev = (u64)netdev;
3495 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3496
3497 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3498 if (ret < 0) {
3499 dev_err(&oct->pci_dev->dev,
3500 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3501 ret);
3502 }
3503 return ret;
3504}
3505
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003506/** \brief Net device fix features
3507 * @param netdev pointer to network device
3508 * @param request features requested
3509 * @returns updated features list
3510 */
3511static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3512 netdev_features_t request)
3513{
3514 struct lio *lio = netdev_priv(netdev);
3515
3516 if ((request & NETIF_F_RXCSUM) &&
3517 !(lio->dev_capability & NETIF_F_RXCSUM))
3518 request &= ~NETIF_F_RXCSUM;
3519
3520 if ((request & NETIF_F_HW_CSUM) &&
3521 !(lio->dev_capability & NETIF_F_HW_CSUM))
3522 request &= ~NETIF_F_HW_CSUM;
3523
3524 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3525 request &= ~NETIF_F_TSO;
3526
3527 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3528 request &= ~NETIF_F_TSO6;
3529
3530 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3531 request &= ~NETIF_F_LRO;
3532
3533 /*Disable LRO if RXCSUM is off */
3534 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3535 (lio->dev_capability & NETIF_F_LRO))
3536 request &= ~NETIF_F_LRO;
3537
3538 return request;
3539}
3540
3541/** \brief Net device set features
3542 * @param netdev pointer to network device
3543 * @param features features to enable/disable
3544 */
3545static int liquidio_set_features(struct net_device *netdev,
3546 netdev_features_t features)
3547{
3548 struct lio *lio = netdev_priv(netdev);
3549
3550 if (!((netdev->features ^ features) & NETIF_F_LRO))
3551 return 0;
3552
3553 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003554 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3555 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003556 else if (!(features & NETIF_F_LRO) &&
3557 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003558 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3559 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003560
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003561 /* Sending command to firmware to enable/disable RX checksum
3562 * offload settings using ethtool
3563 */
3564 if (!(netdev->features & NETIF_F_RXCSUM) &&
3565 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3566 (features & NETIF_F_RXCSUM))
3567 liquidio_set_rxcsum_command(netdev,
3568 OCTNET_CMD_TNL_RX_CSUM_CTL,
3569 OCTNET_CMD_RXCSUM_ENABLE);
3570 else if ((netdev->features & NETIF_F_RXCSUM) &&
3571 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3572 !(features & NETIF_F_RXCSUM))
3573 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3574 OCTNET_CMD_RXCSUM_DISABLE);
3575
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003576 return 0;
3577}
3578
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003579static void liquidio_add_vxlan_port(struct net_device *netdev,
3580 struct udp_tunnel_info *ti)
3581{
3582 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3583 return;
3584
3585 liquidio_vxlan_port_command(netdev,
3586 OCTNET_CMD_VXLAN_PORT_CONFIG,
3587 htons(ti->port),
3588 OCTNET_CMD_VXLAN_PORT_ADD);
3589}
3590
3591static void liquidio_del_vxlan_port(struct net_device *netdev,
3592 struct udp_tunnel_info *ti)
3593{
3594 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3595 return;
3596
3597 liquidio_vxlan_port_command(netdev,
3598 OCTNET_CMD_VXLAN_PORT_CONFIG,
3599 htons(ti->port),
3600 OCTNET_CMD_VXLAN_PORT_DEL);
3601}
3602
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003603static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3604 u8 *mac, bool is_admin_assigned)
3605{
3606 struct lio *lio = GET_LIO(netdev);
3607 struct octeon_device *oct = lio->oct_dev;
3608 struct octnic_ctrl_pkt nctrl;
3609
3610 if (!is_valid_ether_addr(mac))
3611 return -EINVAL;
3612
3613 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3614 return -EINVAL;
3615
3616 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3617
3618 nctrl.ncmd.u64 = 0;
3619 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3620 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3621 nctrl.ncmd.s.param1 = vfidx + 1;
3622 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3623 nctrl.ncmd.s.more = 1;
3624 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003625 nctrl.netpndev = (u64)netdev;
3626 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003627 nctrl.wait_time = LIO_CMD_WAIT_TM;
3628
3629 nctrl.udd[0] = 0;
3630 /* The MAC Address is presented in network byte order. */
3631 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3632
3633 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3634
3635 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3636
3637 return 0;
3638}
3639
3640static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3641{
3642 struct lio *lio = GET_LIO(netdev);
3643 struct octeon_device *oct = lio->oct_dev;
3644 int retval;
3645
3646 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3647 if (!retval)
3648 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3649
3650 return retval;
3651}
3652
3653static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3654 u16 vlan, u8 qos, __be16 vlan_proto)
3655{
3656 struct lio *lio = GET_LIO(netdev);
3657 struct octeon_device *oct = lio->oct_dev;
3658 struct octnic_ctrl_pkt nctrl;
3659 u16 vlantci;
3660
3661 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3662 return -EINVAL;
3663
3664 if (vlan_proto != htons(ETH_P_8021Q))
3665 return -EPROTONOSUPPORT;
3666
3667 if (vlan >= VLAN_N_VID || qos > 7)
3668 return -EINVAL;
3669
3670 if (vlan)
3671 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3672 else
3673 vlantci = 0;
3674
3675 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3676 return 0;
3677
3678 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3679
3680 if (vlan)
3681 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3682 else
3683 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3684
3685 nctrl.ncmd.s.param1 = vlantci;
3686 nctrl.ncmd.s.param2 =
3687 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3688 nctrl.ncmd.s.more = 0;
3689 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3690 nctrl.cb_fn = 0;
3691 nctrl.wait_time = LIO_CMD_WAIT_TM;
3692
3693 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3694
3695 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3696
3697 return 0;
3698}
3699
3700static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3701 struct ifla_vf_info *ivi)
3702{
3703 struct lio *lio = GET_LIO(netdev);
3704 struct octeon_device *oct = lio->oct_dev;
3705 u8 *macaddr;
3706
3707 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3708 return -EINVAL;
3709
3710 ivi->vf = vfidx;
3711 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3712 ether_addr_copy(&ivi->mac[0], macaddr);
3713 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3714 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3715 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3716 return 0;
3717}
3718
3719static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3720 int linkstate)
3721{
3722 struct lio *lio = GET_LIO(netdev);
3723 struct octeon_device *oct = lio->oct_dev;
3724 struct octnic_ctrl_pkt nctrl;
3725
3726 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3727 return -EINVAL;
3728
3729 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3730 return 0;
3731
3732 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3733 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3734 nctrl.ncmd.s.param1 =
3735 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3736 nctrl.ncmd.s.param2 = linkstate;
3737 nctrl.ncmd.s.more = 0;
3738 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3739 nctrl.cb_fn = 0;
3740 nctrl.wait_time = LIO_CMD_WAIT_TM;
3741
3742 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3743
3744 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3745
3746 return 0;
3747}
3748
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003749static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003750 .ndo_open = liquidio_open,
3751 .ndo_stop = liquidio_stop,
3752 .ndo_start_xmit = liquidio_xmit,
3753 .ndo_get_stats = liquidio_get_stats,
3754 .ndo_set_mac_address = liquidio_set_mac,
3755 .ndo_set_rx_mode = liquidio_set_mcast_list,
3756 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003757
3758 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3759 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003760 .ndo_change_mtu = liquidio_change_mtu,
3761 .ndo_do_ioctl = liquidio_ioctl,
3762 .ndo_fix_features = liquidio_fix_features,
3763 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003764 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3765 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003766 .ndo_set_vf_mac = liquidio_set_vf_mac,
3767 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3768 .ndo_get_vf_config = liquidio_get_vf_config,
3769 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003770};
3771
3772/** \brief Entry point for the liquidio module
3773 */
3774static int __init liquidio_init(void)
3775{
3776 int i;
3777 struct handshake *hs;
3778
3779 init_completion(&first_stage);
3780
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003781 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003782
3783 if (liquidio_init_pci())
3784 return -EINVAL;
3785
3786 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3787
3788 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3789 hs = &handshake[i];
3790 if (hs->pci_dev) {
3791 wait_for_completion(&hs->init);
3792 if (!hs->init_ok) {
3793 /* init handshake failed */
3794 dev_err(&hs->pci_dev->dev,
3795 "Failed to init device\n");
3796 liquidio_deinit_pci();
3797 return -EIO;
3798 }
3799 }
3800 }
3801
3802 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3803 hs = &handshake[i];
3804 if (hs->pci_dev) {
3805 wait_for_completion_timeout(&hs->started,
3806 msecs_to_jiffies(30000));
3807 if (!hs->started_ok) {
3808 /* starter handshake failed */
3809 dev_err(&hs->pci_dev->dev,
3810 "Firmware failed to start\n");
3811 liquidio_deinit_pci();
3812 return -EIO;
3813 }
3814 }
3815 }
3816
3817 return 0;
3818}
3819
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003820static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003821{
3822 struct octeon_device *oct = (struct octeon_device *)buf;
3823 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003824 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003825 union oct_link_status *ls;
3826 int i;
3827
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003828 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003829 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3830 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003831 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003832 goto nic_info_err;
3833 }
3834
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003835 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003836 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3837
3838 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003839 for (i = 0; i < oct->ifcount; i++) {
3840 if (oct->props[i].gmxport == gmxport) {
3841 update_link_status(oct->props[i].netdev, ls);
3842 break;
3843 }
3844 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003845
3846nic_info_err:
3847 for (i = 0; i < recv_pkt->buffer_count; i++)
3848 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3849 octeon_free_recv_info(recv_info);
3850 return 0;
3851}
3852
3853/**
3854 * \brief Setup network interfaces
3855 * @param octeon_dev octeon device
3856 *
3857 * Called during init time for each device. It assumes the NIC
3858 * is already up and running. The link information for each
3859 * interface is passed in link_info.
3860 */
3861static int setup_nic_devices(struct octeon_device *octeon_dev)
3862{
3863 struct lio *lio = NULL;
3864 struct net_device *netdev;
3865 u8 mac[6], i, j;
3866 struct octeon_soft_command *sc;
3867 struct liquidio_if_cfg_context *ctx;
3868 struct liquidio_if_cfg_resp *resp;
3869 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003870 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003871 union oct_nic_if_cfg if_cfg;
3872 unsigned int base_queue;
3873 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003874 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003875 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003876 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003877
3878 /* This is to handle link status changes */
3879 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3880 OPCODE_NIC_INFO,
3881 lio_nic_info, octeon_dev);
3882
3883 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3884 * They are handled directly.
3885 */
3886 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3887 free_netbuf);
3888
3889 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3890 free_netsgbuf);
3891
3892 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3893 free_netsgbuf_with_resp);
3894
3895 for (i = 0; i < octeon_dev->ifcount; i++) {
3896 resp_size = sizeof(struct liquidio_if_cfg_resp);
3897 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003898 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003899 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003900 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003901 resp_size, ctx_size);
3902 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3903 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003904 vdata = (struct lio_version *)sc->virtdptr;
3905
3906 *((u64 *)vdata) = 0;
3907 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3908 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3909 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003910
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003911 if (OCTEON_CN23XX_PF(octeon_dev)) {
3912 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3913 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3914 base_queue = octeon_dev->sriov_info.pf_srn;
3915
3916 gmx_port_id = octeon_dev->pf_num;
3917 ifidx_or_pfnum = octeon_dev->pf_num;
3918 } else {
3919 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3920 octeon_get_conf(octeon_dev), i);
3921 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3922 octeon_get_conf(octeon_dev), i);
3923 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3924 octeon_get_conf(octeon_dev), i);
3925 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3926 octeon_get_conf(octeon_dev), i);
3927 ifidx_or_pfnum = i;
3928 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003929
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003930 dev_dbg(&octeon_dev->pci_dev->dev,
3931 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003932 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003933 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003934 ctx->octeon_id = lio_get_device_id(octeon_dev);
3935 init_waitqueue_head(&ctx->wc);
3936
3937 if_cfg.u64 = 0;
3938 if_cfg.s.num_iqueues = num_iqueues;
3939 if_cfg.s.num_oqueues = num_oqueues;
3940 if_cfg.s.base_queue = base_queue;
3941 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003942
3943 sc->iq_no = 0;
3944
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003945 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003946 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003947 if_cfg.u64, 0);
3948
3949 sc->callback = if_cfg_callback;
3950 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003951 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003952
3953 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003954 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003955 dev_err(&octeon_dev->pci_dev->dev,
3956 "iq/oq config failed status: %x\n",
3957 retval);
3958 /* Soft instr is freed by driver in case of failure. */
3959 goto setup_nic_dev_fail;
3960 }
3961
3962 /* Sleep on a wait queue till the cond flag indicates that the
3963 * response arrived or timed-out.
3964 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003965 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3966 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3967 goto setup_nic_wait_intr;
3968 }
3969
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003970 retval = resp->status;
3971 if (retval) {
3972 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3973 goto setup_nic_dev_fail;
3974 }
3975
3976 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3977 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3978
3979 num_iqueues = hweight64(resp->cfg_info.iqmask);
3980 num_oqueues = hweight64(resp->cfg_info.oqmask);
3981
3982 if (!(num_iqueues) || !(num_oqueues)) {
3983 dev_err(&octeon_dev->pci_dev->dev,
3984 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3985 resp->cfg_info.iqmask,
3986 resp->cfg_info.oqmask);
3987 goto setup_nic_dev_fail;
3988 }
3989 dev_dbg(&octeon_dev->pci_dev->dev,
3990 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3991 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3992 num_iqueues, num_oqueues);
3993 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3994
3995 if (!netdev) {
3996 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3997 goto setup_nic_dev_fail;
3998 }
3999
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004000 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004001
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004002 /* Associate the routines that will handle different
4003 * netdev tasks.
4004 */
4005 netdev->netdev_ops = &lionetdevops;
4006
4007 lio = GET_LIO(netdev);
4008
4009 memset(lio, 0, sizeof(struct lio));
4010
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004011 lio->ifidx = ifidx_or_pfnum;
4012
4013 props = &octeon_dev->props[i];
4014 props->gmxport = resp->cfg_info.linfo.gmxport;
4015 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004016
4017 lio->linfo.num_rxpciq = num_oqueues;
4018 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004019 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004020 lio->linfo.rxpciq[j].u64 =
4021 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004022 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004023 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004024 lio->linfo.txpciq[j].u64 =
4025 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004026 }
4027 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4028 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4029 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4030
4031 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4032
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004033 if (OCTEON_CN23XX_PF(octeon_dev) ||
4034 OCTEON_CN6XXX(octeon_dev)) {
4035 lio->dev_capability = NETIF_F_HIGHDMA
4036 | NETIF_F_IP_CSUM
4037 | NETIF_F_IPV6_CSUM
4038 | NETIF_F_SG | NETIF_F_RXCSUM
4039 | NETIF_F_GRO
4040 | NETIF_F_TSO | NETIF_F_TSO6
4041 | NETIF_F_LRO;
4042 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004043 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4044
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004045 /* Copy of transmit encapsulation capabilities:
4046 * TSO, TSO6, Checksums for this device
4047 */
4048 lio->enc_dev_capability = NETIF_F_IP_CSUM
4049 | NETIF_F_IPV6_CSUM
4050 | NETIF_F_GSO_UDP_TUNNEL
4051 | NETIF_F_HW_CSUM | NETIF_F_SG
4052 | NETIF_F_RXCSUM
4053 | NETIF_F_TSO | NETIF_F_TSO6
4054 | NETIF_F_LRO;
4055
4056 netdev->hw_enc_features = (lio->enc_dev_capability &
4057 ~NETIF_F_LRO);
4058
4059 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4060
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004061 netdev->vlan_features = lio->dev_capability;
4062 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004063 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4064 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004065 NETIF_F_HW_VLAN_CTAG_TX;
4066
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004067 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4068
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004069 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004070 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4071 netdev->hw_features = netdev->hw_features &
4072 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004073
Jarod Wilson109cc162016-10-17 15:54:13 -04004074 /* MTU range: 68 - 16000 */
4075 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4076 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4077
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004078 /* Point to the properties for octeon device to which this
4079 * interface belongs.
4080 */
4081 lio->oct_dev = octeon_dev;
4082 lio->octprops = props;
4083 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004084
4085 dev_dbg(&octeon_dev->pci_dev->dev,
4086 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4087 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4088
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004089 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4090 u8 vfmac[ETH_ALEN];
4091
4092 random_ether_addr(&vfmac[0]);
4093 if (__liquidio_set_vf_mac(netdev, j,
4094 &vfmac[0], false)) {
4095 dev_err(&octeon_dev->pci_dev->dev,
4096 "Error setting VF%d MAC address\n",
4097 j);
4098 goto setup_nic_dev_fail;
4099 }
4100 }
4101
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004102 /* 64-bit swap required on LE machines */
4103 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4104 for (j = 0; j < 6; j++)
4105 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4106
4107 /* Copy MAC Address to OS network device structure */
4108
4109 ether_addr_copy(netdev->dev_addr, mac);
4110
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004111 /* By default all interfaces on a single Octeon uses the same
4112 * tx and rx queues
4113 */
4114 lio->txq = lio->linfo.txpciq[0].s.q_no;
4115 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004116 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004117 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4118 goto setup_nic_dev_fail;
4119 }
4120
4121 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4122
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004123 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4124 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4125
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004126 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004127 dev_err(&octeon_dev->pci_dev->dev,
4128 "Gather list allocation failed\n");
4129 goto setup_nic_dev_fail;
4130 }
4131
4132 /* Register ethtool support */
4133 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004134 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4135 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4136 else
4137 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004138
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004139 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004140 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4141 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004142
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004143 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4144
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004145 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004146 liquidio_set_feature(netdev,
4147 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004148
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004149 if (setup_link_status_change_wq(netdev))
4150 goto setup_nic_dev_fail;
4151
Satanand Burla031d4f12017-03-22 11:31:13 -07004152 if (setup_rx_oom_poll_fn(netdev))
4153 goto setup_nic_dev_fail;
4154
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004155 /* Register the network device with the OS */
4156 if (register_netdev(netdev)) {
4157 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4158 goto setup_nic_dev_fail;
4159 }
4160
4161 dev_dbg(&octeon_dev->pci_dev->dev,
4162 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4163 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4164 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004165 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004166
4167 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4168
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004169 /* Sending command to firmware to enable Rx checksum offload
4170 * by default at the time of setup of Liquidio driver for
4171 * this device
4172 */
4173 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4174 OCTNET_CMD_RXCSUM_ENABLE);
4175 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4176 OCTNET_CMD_TXCSUM_ENABLE);
4177
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004178 dev_dbg(&octeon_dev->pci_dev->dev,
4179 "NIC ifidx:%d Setup successful\n", i);
4180
4181 octeon_free_soft_command(octeon_dev, sc);
4182 }
4183
4184 return 0;
4185
4186setup_nic_dev_fail:
4187
4188 octeon_free_soft_command(octeon_dev, sc);
4189
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004190setup_nic_wait_intr:
4191
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004192 while (i--) {
4193 dev_err(&octeon_dev->pci_dev->dev,
4194 "NIC ifidx:%d Setup failed\n", i);
4195 liquidio_destroy_nic_device(octeon_dev, i);
4196 }
4197 return -ENODEV;
4198}
4199
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004200#ifdef CONFIG_PCI_IOV
4201static int octeon_enable_sriov(struct octeon_device *oct)
4202{
4203 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4204 struct pci_dev *vfdev;
4205 int err;
4206 u32 u;
4207
4208 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4209 err = pci_enable_sriov(oct->pci_dev,
4210 oct->sriov_info.num_vfs_alloced);
4211 if (err) {
4212 dev_err(&oct->pci_dev->dev,
4213 "OCTEON: Failed to enable PCI sriov: %d\n",
4214 err);
4215 oct->sriov_info.num_vfs_alloced = 0;
4216 return err;
4217 }
4218 oct->sriov_info.sriov_enabled = 1;
4219
4220 /* init lookup table that maps DPI ring number to VF pci_dev
4221 * struct pointer
4222 */
4223 u = 0;
4224 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4225 OCTEON_CN23XX_VF_VID, NULL);
4226 while (vfdev) {
4227 if (vfdev->is_virtfn &&
4228 (vfdev->physfn == oct->pci_dev)) {
4229 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4230 vfdev;
4231 u += oct->sriov_info.rings_per_vf;
4232 }
4233 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4234 OCTEON_CN23XX_VF_VID, vfdev);
4235 }
4236 }
4237
4238 return num_vfs_alloced;
4239}
4240
4241static int lio_pci_sriov_disable(struct octeon_device *oct)
4242{
4243 int u;
4244
4245 if (pci_vfs_assigned(oct->pci_dev)) {
4246 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4247 return -EPERM;
4248 }
4249
4250 pci_disable_sriov(oct->pci_dev);
4251
4252 u = 0;
4253 while (u < MAX_POSSIBLE_VFS) {
4254 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4255 u += oct->sriov_info.rings_per_vf;
4256 }
4257
4258 oct->sriov_info.num_vfs_alloced = 0;
4259 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4260 oct->pf_num);
4261
4262 return 0;
4263}
4264
4265static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4266{
4267 struct octeon_device *oct = pci_get_drvdata(dev);
4268 int ret = 0;
4269
4270 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4271 (oct->sriov_info.sriov_enabled)) {
4272 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4273 oct->pf_num, num_vfs);
4274 return 0;
4275 }
4276
4277 if (!num_vfs) {
4278 ret = lio_pci_sriov_disable(oct);
4279 } else if (num_vfs > oct->sriov_info.max_vfs) {
4280 dev_err(&oct->pci_dev->dev,
4281 "OCTEON: Max allowed VFs:%d user requested:%d",
4282 oct->sriov_info.max_vfs, num_vfs);
4283 ret = -EPERM;
4284 } else {
4285 oct->sriov_info.num_vfs_alloced = num_vfs;
4286 ret = octeon_enable_sriov(oct);
4287 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4288 oct->pf_num, num_vfs);
4289 }
4290
4291 return ret;
4292}
4293#endif
4294
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004295/**
4296 * \brief initialize the NIC
4297 * @param oct octeon device
4298 *
4299 * This initialization routine is called once the Octeon device application is
4300 * up and running
4301 */
4302static int liquidio_init_nic_module(struct octeon_device *oct)
4303{
4304 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004305 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004306 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4307
4308 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4309
4310 /* only default iq and oq were initialized
4311 * initialize the rest as well
4312 */
4313 /* run port_config command for each port */
4314 oct->ifcount = num_nic_ports;
4315
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004316 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004317
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004318 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4319 oct->props[i].gmxport = -1;
4320
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004321 retval = setup_nic_devices(oct);
4322 if (retval) {
4323 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4324 goto octnet_init_failure;
4325 }
4326
4327 liquidio_ptp_init(oct);
4328
4329 /* Initialize interrupt moderation params */
4330 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004331 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004332 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004333 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4334 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4335 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4336 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4337 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4338 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4339 intrmod_cfg->tx_enable = 1;
4340 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4341 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4342 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4343 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004344 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004345 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4346
4347 return retval;
4348
4349octnet_init_failure:
4350
4351 oct->ifcount = 0;
4352
4353 return retval;
4354}
4355
4356/**
4357 * \brief starter callback that invokes the remaining initialization work after
4358 * the NIC is up and running.
4359 * @param octptr work struct work_struct
4360 */
4361static void nic_starter(struct work_struct *work)
4362{
4363 struct octeon_device *oct;
4364 struct cavium_wk *wk = (struct cavium_wk *)work;
4365
4366 oct = (struct octeon_device *)wk->ctxptr;
4367
4368 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4369 return;
4370
4371 /* If the status of the device is CORE_OK, the core
4372 * application has reported its application type. Call
4373 * any registered handlers now and move to the RUNNING
4374 * state.
4375 */
4376 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4377 schedule_delayed_work(&oct->nic_poll_work.work,
4378 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4379 return;
4380 }
4381
4382 atomic_set(&oct->status, OCT_DEV_RUNNING);
4383
4384 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4385 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4386
4387 if (liquidio_init_nic_module(oct))
4388 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4389 else
4390 handshake[oct->octeon_id].started_ok = 1;
4391 } else {
4392 dev_err(&oct->pci_dev->dev,
4393 "Unexpected application running on NIC (%d). Check firmware.\n",
4394 oct->app_mode);
4395 }
4396
4397 complete(&handshake[oct->octeon_id].started);
4398}
4399
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004400static int
4401octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4402{
4403 struct octeon_device *oct = (struct octeon_device *)buf;
4404 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4405 int i, notice, vf_idx;
4406 u64 *data, vf_num;
4407
4408 notice = recv_pkt->rh.r.ossp;
4409 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4410
4411 /* the first 64-bit word of data is the vf_num */
4412 vf_num = data[0];
4413 octeon_swap_8B_data(&vf_num, 1);
4414 vf_idx = (int)vf_num - 1;
4415
4416 if (notice == VF_DRV_LOADED) {
4417 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4418 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4419 dev_info(&oct->pci_dev->dev,
4420 "driver for VF%d was loaded\n", vf_idx);
4421 try_module_get(THIS_MODULE);
4422 }
4423 } else if (notice == VF_DRV_REMOVED) {
4424 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4425 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4426 dev_info(&oct->pci_dev->dev,
4427 "driver for VF%d was removed\n", vf_idx);
4428 module_put(THIS_MODULE);
4429 }
4430 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4431 u8 *b = (u8 *)&data[1];
4432
4433 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4434 dev_info(&oct->pci_dev->dev,
4435 "VF driver changed VF%d's MAC address to %pM\n",
4436 vf_idx, b + 2);
4437 }
4438
4439 for (i = 0; i < recv_pkt->buffer_count; i++)
4440 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4441 octeon_free_recv_info(recv_info);
4442
4443 return 0;
4444}
4445
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004446/**
4447 * \brief Device initialization for each Octeon device that is probed
4448 * @param octeon_dev octeon device
4449 */
4450static int octeon_device_init(struct octeon_device *octeon_dev)
4451{
4452 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004453 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004454 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004455 struct octeon_device_priv *oct_priv =
4456 (struct octeon_device_priv *)octeon_dev->priv;
4457 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4458
4459 /* Enable access to the octeon device and make its DMA capability
4460 * known to the OS.
4461 */
4462 if (octeon_pci_os_setup(octeon_dev))
4463 return 1;
4464
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004465 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4466
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004467 /* Identify the Octeon type and map the BAR address space. */
4468 if (octeon_chip_specific_setup(octeon_dev)) {
4469 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4470 return 1;
4471 }
4472
4473 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4474
4475 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4476
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004477 if (OCTEON_CN23XX_PF(octeon_dev)) {
4478 if (!cn23xx_fw_loaded(octeon_dev)) {
4479 fw_loaded = 0;
4480 /* Do a soft reset of the Octeon device. */
4481 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4482 return 1;
4483 /* things might have changed */
4484 if (!cn23xx_fw_loaded(octeon_dev))
4485 fw_loaded = 0;
4486 else
4487 fw_loaded = 1;
4488 } else {
4489 fw_loaded = 1;
4490 }
4491 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004492 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004493 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004494
4495 /* Initialize the dispatch mechanism used to push packets arriving on
4496 * Octeon Output queues.
4497 */
4498 if (octeon_init_dispatch_list(octeon_dev))
4499 return 1;
4500
4501 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4502 OPCODE_NIC_CORE_DRV_ACTIVE,
4503 octeon_core_drv_init,
4504 octeon_dev);
4505
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004506 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4507 OPCODE_NIC_VF_DRV_NOTICE,
4508 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004509 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4510 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4511 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4512 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4513
4514 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4515
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004516 if (octeon_set_io_queues_off(octeon_dev)) {
4517 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4518 return 1;
4519 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004520
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004521 if (OCTEON_CN23XX_PF(octeon_dev)) {
4522 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4523 if (ret) {
4524 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4525 return ret;
4526 }
4527 }
4528
4529 /* Initialize soft command buffer pool
4530 */
4531 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4532 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4533 return 1;
4534 }
4535 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4536
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004537 /* Setup the data structures that manage this Octeon's Input queues. */
4538 if (octeon_setup_instr_queues(octeon_dev)) {
4539 dev_err(&octeon_dev->pci_dev->dev,
4540 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004541 return 1;
4542 }
4543 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4544
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004545 /* Initialize lists to manage the requests of different types that
4546 * arrive from user & kernel applications for this octeon device.
4547 */
4548 if (octeon_setup_response_list(octeon_dev)) {
4549 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4550 return 1;
4551 }
4552 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4553
4554 if (octeon_setup_output_queues(octeon_dev)) {
4555 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004556 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004557 }
4558
4559 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4560
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004561 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004562 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4563 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4564 return 1;
4565 }
4566 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4567
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004568 if (octeon_allocate_ioq_vector(octeon_dev)) {
4569 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4570 return 1;
4571 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004572 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004573
4574 } else {
4575 /* The input and output queue registers were setup earlier (the
4576 * queues were not enabled). Any additional registers
4577 * that need to be programmed should be done now.
4578 */
4579 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4580 if (ret) {
4581 dev_err(&octeon_dev->pci_dev->dev,
4582 "Failed to configure device registers\n");
4583 return ret;
4584 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004585 }
4586
4587 /* Initialize the tasklet that handles output queue packet processing.*/
4588 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4589 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4590 (unsigned long)octeon_dev);
4591
4592 /* Setup the interrupt handler and record the INT SUM register address
4593 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004594 if (octeon_setup_interrupt(octeon_dev))
4595 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004596
4597 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004598 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004599
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004600 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4601
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004602 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004603 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4604 if (ret) {
4605 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4606 return ret;
4607 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004608
4609 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4610
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004611 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4612 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4613 if (!ddr_timeout) {
4614 dev_info(&octeon_dev->pci_dev->dev,
4615 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4616 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004617
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004618 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004619
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004620 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4621 while (!ddr_timeout) {
4622 set_current_state(TASK_INTERRUPTIBLE);
4623 if (schedule_timeout(HZ / 10)) {
4624 /* user probably pressed Control-C */
4625 return 1;
4626 }
4627 }
4628 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4629 if (ret) {
4630 dev_err(&octeon_dev->pci_dev->dev,
4631 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4632 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004633 return 1;
4634 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004635
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004636 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4637 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4638 return 1;
4639 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004640
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004641 /* Divert uboot to take commands from host instead. */
4642 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004643
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004644 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4645 ret = octeon_init_consoles(octeon_dev);
4646 if (ret) {
4647 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4648 return 1;
4649 }
4650 ret = octeon_add_console(octeon_dev, 0);
4651 if (ret) {
4652 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4653 return 1;
4654 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004655
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004656 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004657
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004658 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4659 ret = load_firmware(octeon_dev);
4660 if (ret) {
4661 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4662 return 1;
4663 }
4664 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4665 * loaded
4666 */
4667 if (OCTEON_CN23XX_PF(octeon_dev))
4668 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4669 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004670 }
4671
4672 handshake[octeon_dev->octeon_id].init_ok = 1;
4673 complete(&handshake[octeon_dev->octeon_id].init);
4674
4675 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4676
4677 /* Send Credit for Octeon Output queues. Credits are always sent after
4678 * the output queue is enabled.
4679 */
4680 for (j = 0; j < octeon_dev->num_oqs; j++)
4681 writel(octeon_dev->droq[j]->max_count,
4682 octeon_dev->droq[j]->pkts_credit_reg);
4683
4684 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004685 return 0;
4686}
4687
4688/**
4689 * \brief Exits the module
4690 */
4691static void __exit liquidio_exit(void)
4692{
4693 liquidio_deinit_pci();
4694
4695 pr_info("LiquidIO network module is now unloaded\n");
4696}
4697
4698module_init(liquidio_init);
4699module_exit(liquidio_exit);