blob: b06be91d9d2e2ac8d2e8e3fcfd3eb45af4f7b8b6 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
Russell Kinge3bfc6e2017-02-07 15:03:03 -080018#include <linux/module.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070019#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070020#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070021#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070022#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include "liquidio_common.h"
24#include "octeon_droq.h"
25#include "octeon_iq.h"
26#include "response_manager.h"
27#include "octeon_device.h"
28#include "octeon_nic.h"
29#include "octeon_main.h"
30#include "octeon_network.h"
31#include "cn66xx_regs.h"
32#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070033#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070034#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070035#include "liquidio_image.h"
36
37MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
38MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
39MODULE_LICENSE("GPL");
40MODULE_VERSION(LIQUIDIO_VERSION);
41MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
42MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
43MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -080044MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070045
46static int ddr_timeout = 10000;
47module_param(ddr_timeout, int, 0644);
48MODULE_PARM_DESC(ddr_timeout,
49 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
50
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070051#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
52
53static int debug = -1;
54module_param(debug, int, 0644);
55MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
56
57static char fw_type[LIO_MAX_FW_TYPE_LEN];
58module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
59MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
60
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070061static int ptp_enable = 1;
62
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070063/* Polling interval for determining when NIC application is alive */
64#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
65
66/* runtime link query interval */
67#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
68
69struct liquidio_if_cfg_context {
70 int octeon_id;
71
72 wait_queue_head_t wc;
73
74 int cond;
75};
76
77struct liquidio_if_cfg_resp {
78 u64 rh;
79 struct liquidio_if_cfg_info cfg_info;
80 u64 status;
81};
82
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -070083struct liquidio_rx_ctl_context {
84 int octeon_id;
85
86 wait_queue_head_t wc;
87
88 int cond;
89};
90
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070091struct oct_link_status_resp {
92 u64 rh;
93 struct oct_link_info link_info;
94 u64 status;
95};
96
97struct oct_timestamp_resp {
98 u64 rh;
99 u64 timestamp;
100 u64 status;
101};
102
103#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
104
105union tx_info {
106 u64 u64;
107 struct {
108#ifdef __BIG_ENDIAN_BITFIELD
109 u16 gso_size;
110 u16 gso_segs;
111 u32 reserved;
112#else
113 u32 reserved;
114 u16 gso_segs;
115 u16 gso_size;
116#endif
117 } s;
118};
119
120/** Octeon device properties to be used by the NIC module.
121 * Each octeon device in the system will be represented
122 * by this structure in the NIC module.
123 */
124
125#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
126
127#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700128#define OCTNIC_GSO_MAX_SIZE \
129 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700130
131/** Structure of a node in list of gather components maintained by
132 * NIC driver for each network device.
133 */
134struct octnic_gather {
135 /** List manipulation. Next and prev pointers. */
136 struct list_head list;
137
138 /** Size of the gather component at sg in bytes. */
139 int sg_size;
140
141 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
142 int adjust;
143
144 /** Gather component that can accommodate max sized fragment list
145 * received from the IP layer.
146 */
147 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700148
VSR Burru67e303e2017-03-06 18:45:59 -0800149 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700150};
151
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700152struct handshake {
153 struct completion init;
154 struct completion started;
155 struct pci_dev *pci_dev;
156 int init_ok;
157 int started_ok;
158};
159
160struct octeon_device_priv {
161 /** Tasklet structures for this device. */
162 struct tasklet_struct droq_tasklet;
163 unsigned long napi_mask;
164};
165
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800166#ifdef CONFIG_PCI_IOV
167static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
168#endif
169
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700170static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700171static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700172static void liquidio_remove(struct pci_dev *pdev);
173static int liquidio_probe(struct pci_dev *pdev,
174 const struct pci_device_id *ent);
175
176static struct handshake handshake[MAX_OCTEON_DEVICES];
177static struct completion first_stage;
178
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700179static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700180{
181 int q_no;
182 int reschedule = 0;
183 struct octeon_device *oct = (struct octeon_device *)pdev;
184 struct octeon_device_priv *oct_priv =
185 (struct octeon_device_priv *)oct->priv;
186
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700187 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800188 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700189 continue;
190 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
191 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700192 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700193
194 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
195 /* set time and cnt interrupt thresholds for this DROQ
196 * for NAPI
197 */
198 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
199
200 octeon_write_csr64(
201 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
202 0x5700000040ULL);
203 octeon_write_csr64(
204 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
205 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700206 }
207
208 if (reschedule)
209 tasklet_schedule(&oct_priv->droq_tasklet);
210}
211
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700212static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700213{
214 struct octeon_device_priv *oct_priv =
215 (struct octeon_device_priv *)oct->priv;
216 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
217 int i;
218
219 do {
220 pending_pkts = 0;
221
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700222 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800223 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700224 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700225 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700226 }
227 if (pkt_cnt > 0) {
228 pending_pkts += pkt_cnt;
229 tasklet_schedule(&oct_priv->droq_tasklet);
230 }
231 pkt_cnt = 0;
232 schedule_timeout_uninterruptible(1);
233
234 } while (retry-- && pending_pkts);
235
236 return pkt_cnt;
237}
238
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700239/**
240 * \brief Forces all IO queues off on a given device
241 * @param oct Pointer to Octeon device
242 */
243static void force_io_queues_off(struct octeon_device *oct)
244{
245 if ((oct->chip_id == OCTEON_CN66XX) ||
246 (oct->chip_id == OCTEON_CN68XX)) {
247 /* Reset the Enable bits for Input Queues. */
248 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
249
250 /* Reset the Enable bits for Output Queues. */
251 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
252 }
253}
254
255/**
256 * \brief wait for all pending requests to complete
257 * @param oct Pointer to Octeon device
258 *
259 * Called during shutdown sequence
260 */
261static int wait_for_pending_requests(struct octeon_device *oct)
262{
263 int i, pcount = 0;
264
265 for (i = 0; i < 100; i++) {
266 pcount =
267 atomic_read(&oct->response_list
268 [OCTEON_ORDERED_SC_LIST].pending_req_count);
269 if (pcount)
270 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700271 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700272 break;
273 }
274
275 if (pcount)
276 return 1;
277
278 return 0;
279}
280
281/**
282 * \brief Cause device to go quiet so it can be safely removed/reset/etc
283 * @param oct Pointer to Octeon device
284 */
285static inline void pcierror_quiesce_device(struct octeon_device *oct)
286{
287 int i;
288
289 /* Disable the input and output queues now. No more packets will
290 * arrive from Octeon, but we should wait for all packet processing
291 * to finish.
292 */
293 force_io_queues_off(oct);
294
295 /* To allow for in-flight requests */
296 schedule_timeout_uninterruptible(100);
297
298 if (wait_for_pending_requests(oct))
299 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
300
301 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700302 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700303 struct octeon_instr_queue *iq;
304
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800305 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700306 continue;
307 iq = oct->instr_queue[i];
308
309 if (atomic_read(&iq->instr_pending)) {
310 spin_lock_bh(&iq->lock);
311 iq->fill_cnt = 0;
312 iq->octeon_read_index = iq->host_write_index;
313 iq->stats.instr_processed +=
314 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700315 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700316 spin_unlock_bh(&iq->lock);
317 }
318 }
319
320 /* Force all pending ordered list requests to time out. */
321 lio_process_ordered_list(oct, 1);
322
323 /* We do not need to wait for output queue packets to be processed. */
324}
325
326/**
327 * \brief Cleanup PCI AER uncorrectable error status
328 * @param dev Pointer to PCI device
329 */
330static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
331{
332 int pos = 0x100;
333 u32 status, mask;
334
335 pr_info("%s :\n", __func__);
336
337 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
338 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
339 if (dev->error_state == pci_channel_io_normal)
340 status &= ~mask; /* Clear corresponding nonfatal bits */
341 else
342 status &= mask; /* Clear corresponding fatal bits */
343 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
344}
345
346/**
347 * \brief Stop all PCI IO to a given device
348 * @param dev Pointer to Octeon device
349 */
350static void stop_pci_io(struct octeon_device *oct)
351{
352 /* No more instructions will be forwarded. */
353 atomic_set(&oct->status, OCT_DEV_IN_RESET);
354
355 pci_disable_device(oct->pci_dev);
356
357 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700358 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700359
360 pcierror_quiesce_device(oct);
361
362 /* Release the interrupt line */
363 free_irq(oct->pci_dev->irq, oct);
364
365 if (oct->flags & LIO_FLAG_MSI_ENABLED)
366 pci_disable_msi(oct->pci_dev);
367
368 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
369 lio_get_state_string(&oct->status));
370
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700371 /* making it a common function for all OCTEON models */
372 cleanup_aer_uncorrect_error_status(oct->pci_dev);
373}
374
375/**
376 * \brief called when PCI error is detected
377 * @param pdev Pointer to PCI device
378 * @param state The current pci connection state
379 *
380 * This function is called after a PCI bus error affecting
381 * this device has been detected.
382 */
383static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
384 pci_channel_state_t state)
385{
386 struct octeon_device *oct = pci_get_drvdata(pdev);
387
388 /* Non-correctable Non-fatal errors */
389 if (state == pci_channel_io_normal) {
390 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
391 cleanup_aer_uncorrect_error_status(oct->pci_dev);
392 return PCI_ERS_RESULT_CAN_RECOVER;
393 }
394
395 /* Non-correctable Fatal errors */
396 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
397 stop_pci_io(oct);
398
399 /* Always return a DISCONNECT. There is no support for recovery but only
400 * for a clean shutdown.
401 */
402 return PCI_ERS_RESULT_DISCONNECT;
403}
404
405/**
406 * \brief mmio handler
407 * @param pdev Pointer to PCI device
408 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700409static pci_ers_result_t liquidio_pcie_mmio_enabled(
410 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700411{
412 /* We should never hit this since we never ask for a reset for a Fatal
413 * Error. We always return DISCONNECT in io_error above.
414 * But play safe and return RECOVERED for now.
415 */
416 return PCI_ERS_RESULT_RECOVERED;
417}
418
419/**
420 * \brief called after the pci bus has been reset.
421 * @param pdev Pointer to PCI device
422 *
423 * Restart the card from scratch, as if from a cold-boot. Implementation
424 * resembles the first-half of the octeon_resume routine.
425 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700426static pci_ers_result_t liquidio_pcie_slot_reset(
427 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700428{
429 /* We should never hit this since we never ask for a reset for a Fatal
430 * Error. We always return DISCONNECT in io_error above.
431 * But play safe and return RECOVERED for now.
432 */
433 return PCI_ERS_RESULT_RECOVERED;
434}
435
436/**
437 * \brief called when traffic can start flowing again.
438 * @param pdev Pointer to PCI device
439 *
440 * This callback is called when the error recovery driver tells us that
441 * its OK to resume normal operation. Implementation resembles the
442 * second-half of the octeon_resume routine.
443 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700444static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700445{
446 /* Nothing to be done here. */
447}
448
449#ifdef CONFIG_PM
450/**
451 * \brief called when suspending
452 * @param pdev Pointer to PCI device
453 * @param state state to suspend to
454 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700455static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
456 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700457{
458 return 0;
459}
460
461/**
462 * \brief called when resuming
463 * @param pdev Pointer to PCI device
464 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700465static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700466{
467 return 0;
468}
469#endif
470
471/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100472static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700473 .error_detected = liquidio_pcie_error_detected,
474 .mmio_enabled = liquidio_pcie_mmio_enabled,
475 .slot_reset = liquidio_pcie_slot_reset,
476 .resume = liquidio_pcie_resume,
477};
478
479static const struct pci_device_id liquidio_pci_tbl[] = {
480 { /* 68xx */
481 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
482 },
483 { /* 66xx */
484 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
485 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700486 { /* 23xx pf */
487 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
488 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700489 {
490 0, 0, 0, 0, 0, 0, 0
491 }
492};
493MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
494
495static struct pci_driver liquidio_pci_driver = {
496 .name = "LiquidIO",
497 .id_table = liquidio_pci_tbl,
498 .probe = liquidio_probe,
499 .remove = liquidio_remove,
500 .err_handler = &liquidio_err_handler, /* For AER */
501
502#ifdef CONFIG_PM
503 .suspend = liquidio_suspend,
504 .resume = liquidio_resume,
505#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800506#ifdef CONFIG_PCI_IOV
507 .sriov_configure = liquidio_enable_sriov,
508#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700509};
510
511/**
512 * \brief register PCI driver
513 */
514static int liquidio_init_pci(void)
515{
516 return pci_register_driver(&liquidio_pci_driver);
517}
518
519/**
520 * \brief unregister PCI driver
521 */
522static void liquidio_deinit_pci(void)
523{
524 pci_unregister_driver(&liquidio_pci_driver);
525}
526
527/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700528 * \brief Stop Tx queues
529 * @param netdev network device
530 */
531static inline void txqs_stop(struct net_device *netdev)
532{
533 if (netif_is_multiqueue(netdev)) {
534 int i;
535
536 for (i = 0; i < netdev->num_tx_queues; i++)
537 netif_stop_subqueue(netdev, i);
538 } else {
539 netif_stop_queue(netdev);
540 }
541}
542
543/**
544 * \brief Start Tx queues
545 * @param netdev network device
546 */
547static inline void txqs_start(struct net_device *netdev)
548{
549 if (netif_is_multiqueue(netdev)) {
550 int i;
551
552 for (i = 0; i < netdev->num_tx_queues; i++)
553 netif_start_subqueue(netdev, i);
554 } else {
555 netif_start_queue(netdev);
556 }
557}
558
559/**
560 * \brief Wake Tx queues
561 * @param netdev network device
562 */
563static inline void txqs_wake(struct net_device *netdev)
564{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700565 struct lio *lio = GET_LIO(netdev);
566
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700567 if (netif_is_multiqueue(netdev)) {
568 int i;
569
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700570 for (i = 0; i < netdev->num_tx_queues; i++) {
571 int qno = lio->linfo.txpciq[i %
572 (lio->linfo.num_txpciq)].s.q_no;
573
574 if (__netif_subqueue_stopped(netdev, i)) {
575 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
576 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700577 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700578 }
579 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700580 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700581 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
582 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700583 netif_wake_queue(netdev);
584 }
585}
586
587/**
588 * \brief Stop Tx queue
589 * @param netdev network device
590 */
591static void stop_txq(struct net_device *netdev)
592{
593 txqs_stop(netdev);
594}
595
596/**
597 * \brief Start Tx queue
598 * @param netdev network device
599 */
600static void start_txq(struct net_device *netdev)
601{
602 struct lio *lio = GET_LIO(netdev);
603
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700604 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700605 txqs_start(netdev);
606 return;
607 }
608}
609
610/**
611 * \brief Wake a queue
612 * @param netdev network device
613 * @param q which queue to wake
614 */
615static inline void wake_q(struct net_device *netdev, int q)
616{
617 if (netif_is_multiqueue(netdev))
618 netif_wake_subqueue(netdev, q);
619 else
620 netif_wake_queue(netdev);
621}
622
623/**
624 * \brief Stop a queue
625 * @param netdev network device
626 * @param q which queue to stop
627 */
628static inline void stop_q(struct net_device *netdev, int q)
629{
630 if (netif_is_multiqueue(netdev))
631 netif_stop_subqueue(netdev, q);
632 else
633 netif_stop_queue(netdev);
634}
635
636/**
637 * \brief Check Tx queue status, and take appropriate action
638 * @param lio per-network private data
639 * @returns 0 if full, number of queues woken up otherwise
640 */
641static inline int check_txq_status(struct lio *lio)
642{
643 int ret_val = 0;
644
645 if (netif_is_multiqueue(lio->netdev)) {
646 int numqs = lio->netdev->num_tx_queues;
647 int q, iq = 0;
648
649 /* check each sub-queue state */
650 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700651 iq = lio->linfo.txpciq[q %
652 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700653 if (octnet_iq_is_full(lio->oct_dev, iq))
654 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700655 if (__netif_subqueue_stopped(lio->netdev, q)) {
656 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700657 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
658 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700659 ret_val++;
660 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700661 }
662 } else {
663 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
664 return 0;
665 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700666 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
667 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700668 ret_val = 1;
669 }
670 return ret_val;
671}
672
673/**
674 * Remove the node at the head of the list. The list would be empty at
675 * the end of this call if there are no more nodes in the list.
676 */
677static inline struct list_head *list_delete_head(struct list_head *root)
678{
679 struct list_head *node;
680
681 if ((root->prev == root) && (root->next == root))
682 node = NULL;
683 else
684 node = root->next;
685
686 if (node)
687 list_del(node);
688
689 return node;
690}
691
692/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700693 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700694 * @param lio per-network private data
695 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700696static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700697{
698 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700699 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700700
VSR Burru67e303e2017-03-06 18:45:59 -0800701 kfree(lio->glist_lock);
702 lio->glist_lock = NULL;
703
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700704 if (!lio->glist)
705 return;
706
707 for (i = 0; i < lio->linfo.num_txpciq; i++) {
708 do {
709 g = (struct octnic_gather *)
710 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800711 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700712 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700713 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800714
Felix Manlunas58ad3192017-03-20 19:04:48 -0700715 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
716 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800717 lio_dma_free(lio->oct_dev,
718 lio->glist_entry_size * lio->tx_qsize,
719 lio->glists_virt_base[i],
720 lio->glists_dma_base[i]);
721 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700722 }
723
VSR Burru67e303e2017-03-06 18:45:59 -0800724 kfree(lio->glists_virt_base);
725 lio->glists_virt_base = NULL;
726
727 kfree(lio->glists_dma_base);
728 lio->glists_dma_base = NULL;
729
730 kfree(lio->glist);
731 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700732}
733
734/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700735 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700736 * @param lio per-network private data
737 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700738static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700739{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700740 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700741 struct octnic_gather *g;
742
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700743 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
744 GFP_KERNEL);
745 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800746 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700747
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700748 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
749 GFP_KERNEL);
750 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800751 kfree(lio->glist_lock);
752 lio->glist_lock = NULL;
753 return -ENOMEM;
754 }
755
756 lio->glist_entry_size =
757 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
758
759 /* allocate memory to store virtual and dma base address of
760 * per glist consistent memory
761 */
762 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
763 GFP_KERNEL);
764 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
765 GFP_KERNEL);
766
767 if (!lio->glists_virt_base || !lio->glists_dma_base) {
768 delete_glists(lio);
769 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700770 }
771
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700772 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800773 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700774
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700775 spin_lock_init(&lio->glist_lock[i]);
776
777 INIT_LIST_HEAD(&lio->glist[i]);
778
VSR Burru67e303e2017-03-06 18:45:59 -0800779 lio->glists_virt_base[i] =
780 lio_dma_alloc(oct,
781 lio->glist_entry_size * lio->tx_qsize,
782 &lio->glists_dma_base[i]);
783
784 if (!lio->glists_virt_base[i]) {
785 delete_glists(lio);
786 return -ENOMEM;
787 }
788
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700789 for (j = 0; j < lio->tx_qsize; j++) {
790 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
791 numa_node);
792 if (!g)
793 g = kzalloc(sizeof(*g), GFP_KERNEL);
794 if (!g)
795 break;
796
VSR Burru67e303e2017-03-06 18:45:59 -0800797 g->sg = lio->glists_virt_base[i] +
798 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700799
VSR Burru67e303e2017-03-06 18:45:59 -0800800 g->sg_dma_ptr = lio->glists_dma_base[i] +
801 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700802
803 list_add_tail(&g->list, &lio->glist[i]);
804 }
805
806 if (j != lio->tx_qsize) {
807 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800808 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700809 }
810 }
811
812 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700813}
814
815/**
816 * \brief Print link information
817 * @param netdev network device
818 */
819static void print_link_info(struct net_device *netdev)
820{
821 struct lio *lio = GET_LIO(netdev);
822
823 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
824 struct oct_link_info *linfo = &lio->linfo;
825
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700826 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700827 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
828 linfo->link.s.speed,
829 (linfo->link.s.duplex) ? "Full" : "Half");
830 } else {
831 netif_info(lio, link, lio->netdev, "Link Down\n");
832 }
833 }
834}
835
836/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700837 * \brief Routine to notify MTU change
838 * @param work work_struct data structure
839 */
840static void octnet_link_status_change(struct work_struct *work)
841{
842 struct cavium_wk *wk = (struct cavium_wk *)work;
843 struct lio *lio = (struct lio *)wk->ctxptr;
844
845 rtnl_lock();
846 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
847 rtnl_unlock();
848}
849
850/**
851 * \brief Sets up the mtu status change work
852 * @param netdev network device
853 */
854static inline int setup_link_status_change_wq(struct net_device *netdev)
855{
856 struct lio *lio = GET_LIO(netdev);
857 struct octeon_device *oct = lio->oct_dev;
858
859 lio->link_status_wq.wq = alloc_workqueue("link-status",
860 WQ_MEM_RECLAIM, 0);
861 if (!lio->link_status_wq.wq) {
862 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
863 return -1;
864 }
865 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
866 octnet_link_status_change);
867 lio->link_status_wq.wk.ctxptr = lio;
868
869 return 0;
870}
871
872static inline void cleanup_link_status_change_wq(struct net_device *netdev)
873{
874 struct lio *lio = GET_LIO(netdev);
875
876 if (lio->link_status_wq.wq) {
877 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
878 destroy_workqueue(lio->link_status_wq.wq);
879 }
880}
881
882/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700883 * \brief Update link status
884 * @param netdev network device
885 * @param ls link status structure
886 *
887 * Called on receipt of a link status response from the core application to
888 * update each interface's link status.
889 */
890static inline void update_link_status(struct net_device *netdev,
891 union oct_link_status *ls)
892{
893 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700894 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700895
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700896 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700897
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700898 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700899 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700900 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700901
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700902 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700903 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700904 txqs_wake(netdev);
905 } else {
906 netif_carrier_off(netdev);
907 stop_txq(netdev);
908 }
909 }
910}
911
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700912/* Runs in interrupt context. */
913static void update_txq_status(struct octeon_device *oct, int iq_num)
914{
915 struct net_device *netdev;
916 struct lio *lio;
917 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
918
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700919 netdev = oct->props[iq->ifidx].netdev;
920
921 /* This is needed because the first IQ does not have
922 * a netdev associated with it.
923 */
924 if (!netdev)
925 return;
926
927 lio = GET_LIO(netdev);
928 if (netif_is_multiqueue(netdev)) {
929 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
930 lio->linfo.link.s.link_up &&
931 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700932 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
933 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700934 netif_wake_subqueue(netdev, iq->q_index);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700935 }
VSR Burru6069f3f2017-03-22 11:54:50 -0700936 } else if (netif_queue_stopped(netdev) &&
937 lio->linfo.link.s.link_up &&
938 (!octnet_iq_is_full(oct, lio->txq))) {
939 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
940 lio->txq, tx_restart, 1);
941 netif_wake_queue(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700942 }
943}
944
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700945static
946int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
947{
948 struct octeon_device *oct = droq->oct_dev;
949 struct octeon_device_priv *oct_priv =
950 (struct octeon_device_priv *)oct->priv;
951
952 if (droq->ops.poll_mode) {
953 droq->ops.napi_fn(droq);
954 } else {
955 if (ret & MSIX_PO_INT) {
956 tasklet_schedule(&oct_priv->droq_tasklet);
957 return 1;
958 }
959 /* this will be flushed periodically by check iq db */
960 if (ret & MSIX_PI_INT)
961 return 0;
962 }
963 return 0;
964}
965
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700966/**
967 * \brief Droq packet processor sceduler
968 * @param oct octeon device
969 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700970static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700971{
972 struct octeon_device_priv *oct_priv =
973 (struct octeon_device_priv *)oct->priv;
974 u64 oq_no;
975 struct octeon_droq *droq;
976
977 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700978 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
979 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800980 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700981 continue;
982
983 droq = oct->droq[oq_no];
984
985 if (droq->ops.poll_mode) {
986 droq->ops.napi_fn(droq);
987 oct_priv->napi_mask |= (1 << oq_no);
988 } else {
989 tasklet_schedule(&oct_priv->droq_tasklet);
990 }
991 }
992 }
993}
994
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700995static irqreturn_t
996liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
997{
998 u64 ret;
999 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1000 struct octeon_device *oct = ioq_vector->oct_dev;
1001 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1002
1003 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1004
1005 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1006 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1007
1008 return IRQ_HANDLED;
1009}
1010
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001011/**
1012 * \brief Interrupt handler for octeon
1013 * @param irq unused
1014 * @param dev octeon device
1015 */
1016static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001017irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1018 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001019{
1020 struct octeon_device *oct = (struct octeon_device *)dev;
1021 irqreturn_t ret;
1022
1023 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001024 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001025
1026 ret = oct->fn_list.process_interrupt_regs(oct);
1027
1028 if (ret == IRQ_HANDLED)
1029 liquidio_schedule_droq_pkt_handlers(oct);
1030
1031 /* Re-enable our interrupts */
1032 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001033 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001034
1035 return ret;
1036}
1037
1038/**
1039 * \brief Setup interrupt for octeon device
1040 * @param oct octeon device
1041 *
1042 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1043 */
1044static int octeon_setup_interrupt(struct octeon_device *oct)
1045{
1046 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001047 struct msix_entry *msix_entries;
1048 int i;
1049 int num_ioq_vectors;
1050 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001051 char *queue_irq_names = NULL;
1052 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001053
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001054 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1055 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1056 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1057 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001058
Rick Farrington0c88a762017-03-13 12:58:04 -07001059 /* allocate storage for the names assigned to each irq */
1060 oct->irq_name_storage =
1061 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1062 GFP_KERNEL);
1063 if (!oct->irq_name_storage) {
1064 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1065 return -ENOMEM;
1066 }
1067
1068 queue_irq_names = oct->irq_name_storage;
1069 aux_irq_name = &queue_irq_names
1070 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1071
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001072 oct->msix_entries = kcalloc(
1073 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001074 if (!oct->msix_entries) {
1075 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1076 kfree(oct->irq_name_storage);
1077 oct->irq_name_storage = NULL;
1078 return -ENOMEM;
1079 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001080
1081 msix_entries = (struct msix_entry *)oct->msix_entries;
1082 /*Assumption is that pf msix vectors start from pf srn to pf to
1083 * trs and not from 0. if not change this code
1084 */
1085 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1086 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1087 msix_entries[oct->num_msix_irqs - 1].entry =
1088 oct->sriov_info.trs;
1089 num_alloc_ioq_vectors = pci_enable_msix_range(
1090 oct->pci_dev, msix_entries,
1091 oct->num_msix_irqs,
1092 oct->num_msix_irqs);
1093 if (num_alloc_ioq_vectors < 0) {
1094 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1095 kfree(oct->msix_entries);
1096 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001097 kfree(oct->irq_name_storage);
1098 oct->irq_name_storage = NULL;
1099 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001100 }
1101 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1102
1103 num_ioq_vectors = oct->num_msix_irqs;
1104
1105 /** For PF, there is one non-ioq interrupt handler */
1106 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001107
1108 snprintf(aux_irq_name, INTRNAMSIZ,
1109 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001110 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001111 liquidio_legacy_intr_handler, 0,
1112 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001113 if (irqret) {
1114 dev_err(&oct->pci_dev->dev,
1115 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1116 irqret);
1117 pci_disable_msix(oct->pci_dev);
1118 kfree(oct->msix_entries);
1119 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001120 kfree(oct->irq_name_storage);
1121 oct->irq_name_storage = NULL;
1122 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001123 }
1124
1125 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001126 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1127 "LiquidIO%u-pf%u-rxtx-%u",
1128 oct->octeon_id, oct->pf_num, i);
1129
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001130 irqret = request_irq(msix_entries[i].vector,
1131 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001132 &queue_irq_names[IRQ_NAME_OFF(i)],
1133 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001134 if (irqret) {
1135 dev_err(&oct->pci_dev->dev,
1136 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1137 irqret);
1138 /** Freeing the non-ioq irq vector here . */
1139 free_irq(msix_entries[num_ioq_vectors].vector,
1140 oct);
1141
1142 while (i) {
1143 i--;
1144 /** clearing affinity mask. */
1145 irq_set_affinity_hint(
1146 msix_entries[i].vector, NULL);
1147 free_irq(msix_entries[i].vector,
1148 &oct->ioq_vector[i]);
1149 }
1150 pci_disable_msix(oct->pci_dev);
1151 kfree(oct->msix_entries);
1152 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001153 kfree(oct->irq_name_storage);
1154 oct->irq_name_storage = NULL;
1155 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001156 }
1157 oct->ioq_vector[i].vector = msix_entries[i].vector;
1158 /* assign the cpu mask for this msix interrupt vector */
1159 irq_set_affinity_hint(
1160 msix_entries[i].vector,
1161 (&oct->ioq_vector[i].affinity_mask));
1162 }
1163 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1164 oct->octeon_id);
1165 } else {
1166 err = pci_enable_msi(oct->pci_dev);
1167 if (err)
1168 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1169 err);
1170 else
1171 oct->flags |= LIO_FLAG_MSI_ENABLED;
1172
Rick Farrington0c88a762017-03-13 12:58:04 -07001173 /* allocate storage for the names assigned to the irq */
1174 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1175 if (!oct->irq_name_storage)
1176 return -ENOMEM;
1177
1178 queue_irq_names = oct->irq_name_storage;
1179
1180 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1181 "LiquidIO%u-pf%u-rxtx-%u",
1182 oct->octeon_id, oct->pf_num, 0);
1183
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001184 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001185 liquidio_legacy_intr_handler,
1186 IRQF_SHARED,
1187 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001188 if (irqret) {
1189 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1190 pci_disable_msi(oct->pci_dev);
1191 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1192 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001193 kfree(oct->irq_name_storage);
1194 oct->irq_name_storage = NULL;
1195 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001196 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001197 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001198 return 0;
1199}
1200
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001201static int liquidio_watchdog(void *param)
1202{
1203 u64 wdog;
1204 u16 mask_of_stuck_cores = 0;
1205 u16 mask_of_crashed_cores = 0;
1206 int core_num;
1207 u8 core_is_stuck[LIO_MAX_CORES];
1208 u8 core_crashed[LIO_MAX_CORES];
1209 struct octeon_device *oct = param;
1210
1211 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1212 memset(core_crashed, 0, sizeof(core_crashed));
1213
1214 while (!kthread_should_stop()) {
1215 mask_of_crashed_cores =
1216 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1217
1218 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1219 if (!core_is_stuck[core_num]) {
1220 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1221
1222 /* look at watchdog state field */
1223 wdog &= CIU3_WDOG_MASK;
1224 if (wdog) {
1225 /* this watchdog timer has expired */
1226 core_is_stuck[core_num] =
1227 LIO_MONITOR_WDOG_EXPIRE;
1228 mask_of_stuck_cores |= (1 << core_num);
1229 }
1230 }
1231
1232 if (!core_crashed[core_num])
1233 core_crashed[core_num] =
1234 (mask_of_crashed_cores >> core_num) & 1;
1235 }
1236
1237 if (mask_of_stuck_cores) {
1238 for (core_num = 0; core_num < LIO_MAX_CORES;
1239 core_num++) {
1240 if (core_is_stuck[core_num] == 1) {
1241 dev_err(&oct->pci_dev->dev,
1242 "ERROR: Octeon core %d is stuck!\n",
1243 core_num);
1244 /* 2 means we have printk'd an error
1245 * so no need to repeat the same printk
1246 */
1247 core_is_stuck[core_num] =
1248 LIO_MONITOR_CORE_STUCK_MSGD;
1249 }
1250 }
1251 }
1252
1253 if (mask_of_crashed_cores) {
1254 for (core_num = 0; core_num < LIO_MAX_CORES;
1255 core_num++) {
1256 if (core_crashed[core_num] == 1) {
1257 dev_err(&oct->pci_dev->dev,
1258 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1259 core_num);
1260 /* 2 means we have printk'd an error
1261 * so no need to repeat the same printk
1262 */
1263 core_crashed[core_num] =
1264 LIO_MONITOR_CORE_STUCK_MSGD;
1265 }
1266 }
1267 }
1268#ifdef CONFIG_MODULE_UNLOAD
1269 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1270 /* make module refcount=0 so that rmmod will work */
1271 long refcount;
1272
1273 refcount = module_refcount(THIS_MODULE);
1274
1275 while (refcount > 0) {
1276 module_put(THIS_MODULE);
1277 refcount = module_refcount(THIS_MODULE);
1278 }
1279
1280 /* compensate for and withstand an unlikely (but still
1281 * possible) race condition
1282 */
1283 while (refcount < 0) {
1284 try_module_get(THIS_MODULE);
1285 refcount = module_refcount(THIS_MODULE);
1286 }
1287 }
1288#endif
1289 /* sleep for two seconds */
1290 set_current_state(TASK_INTERRUPTIBLE);
1291 schedule_timeout(2 * HZ);
1292 }
1293
1294 return 0;
1295}
1296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001297/**
1298 * \brief PCI probe handler
1299 * @param pdev PCI device structure
1300 * @param ent unused
1301 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001302static int
1303liquidio_probe(struct pci_dev *pdev,
1304 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001305{
1306 struct octeon_device *oct_dev = NULL;
1307 struct handshake *hs;
1308
1309 oct_dev = octeon_allocate_device(pdev->device,
1310 sizeof(struct octeon_device_priv));
1311 if (!oct_dev) {
1312 dev_err(&pdev->dev, "Unable to allocate device\n");
1313 return -ENOMEM;
1314 }
1315
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001316 if (pdev->device == OCTEON_CN23XX_PF_VID)
1317 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001319 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1320 (u32)pdev->vendor, (u32)pdev->device);
1321
1322 /* Assign octeon_device for this device to the private data area. */
1323 pci_set_drvdata(pdev, oct_dev);
1324
1325 /* set linux specific device pointer */
1326 oct_dev->pci_dev = (void *)pdev;
1327
1328 hs = &handshake[oct_dev->octeon_id];
1329 init_completion(&hs->init);
1330 init_completion(&hs->started);
1331 hs->pci_dev = pdev;
1332
1333 if (oct_dev->octeon_id == 0)
1334 /* first LiquidIO NIC is detected */
1335 complete(&first_stage);
1336
1337 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001338 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001339 liquidio_remove(pdev);
1340 return -ENOMEM;
1341 }
1342
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001343 if (OCTEON_CN23XX_PF(oct_dev)) {
1344 u64 scratch1;
1345 u8 bus, device, function;
1346
1347 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1348 if (!(scratch1 & 4ULL)) {
1349 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1350 * the lio watchdog kernel thread is running for this
1351 * NIC. Each NIC gets one watchdog kernel thread.
1352 */
1353 scratch1 |= 4ULL;
1354 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1355 scratch1);
1356
1357 bus = pdev->bus->number;
1358 device = PCI_SLOT(pdev->devfn);
1359 function = PCI_FUNC(pdev->devfn);
1360 oct_dev->watchdog_task = kthread_create(
1361 liquidio_watchdog, oct_dev,
1362 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001363 if (!IS_ERR(oct_dev->watchdog_task)) {
1364 wake_up_process(oct_dev->watchdog_task);
1365 } else {
1366 oct_dev->watchdog_task = NULL;
1367 dev_err(&oct_dev->pci_dev->dev,
1368 "failed to create kernel_thread\n");
1369 liquidio_remove(pdev);
1370 return -1;
1371 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001372 }
1373 }
1374
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001375 oct_dev->rx_pause = 1;
1376 oct_dev->tx_pause = 1;
1377
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001378 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1379
1380 return 0;
1381}
1382
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001383static bool fw_type_is_none(void)
1384{
1385 return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1386 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
1387}
1388
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001389/**
1390 *\brief Destroy resources associated with octeon device
1391 * @param pdev PCI device structure
1392 * @param ent unused
1393 */
1394static void octeon_destroy_resources(struct octeon_device *oct)
1395{
1396 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001397 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001398 struct octeon_device_priv *oct_priv =
1399 (struct octeon_device_priv *)oct->priv;
1400
1401 struct handshake *hs;
1402
1403 switch (atomic_read(&oct->status)) {
1404 case OCT_DEV_RUNNING:
1405 case OCT_DEV_CORE_OK:
1406
1407 /* No more instructions will be forwarded. */
1408 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1409
1410 oct->app_mode = CVM_DRV_INVALID_APP;
1411 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1412 lio_get_state_string(&oct->status));
1413
1414 schedule_timeout_uninterruptible(HZ / 10);
1415
1416 /* fallthrough */
1417 case OCT_DEV_HOST_OK:
1418
1419 /* fallthrough */
1420 case OCT_DEV_CONSOLE_INIT_DONE:
1421 /* Remove any consoles */
1422 octeon_remove_consoles(oct);
1423
1424 /* fallthrough */
1425 case OCT_DEV_IO_QUEUES_DONE:
1426 if (wait_for_pending_requests(oct))
1427 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1428
1429 if (lio_wait_for_instr_fetch(oct))
1430 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1431
1432 /* Disable the input and output queues now. No more packets will
1433 * arrive from Octeon, but we should wait for all packet
1434 * processing to finish.
1435 */
1436 oct->fn_list.disable_io_queues(oct);
1437
1438 if (lio_wait_for_oq_pkts(oct))
1439 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1440
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001441 /* fallthrough */
1442 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001443 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001444 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001445
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001446 if (oct->msix_on) {
1447 msix_entries = (struct msix_entry *)oct->msix_entries;
1448 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1449 /* clear the affinity_cpumask */
1450 irq_set_affinity_hint(msix_entries[i].vector,
1451 NULL);
1452 free_irq(msix_entries[i].vector,
1453 &oct->ioq_vector[i]);
1454 }
1455 /* non-iov vector's argument is oct struct */
1456 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001457
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001458 pci_disable_msix(oct->pci_dev);
1459 kfree(oct->msix_entries);
1460 oct->msix_entries = NULL;
1461 } else {
1462 /* Release the interrupt line */
1463 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001464
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001465 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1466 pci_disable_msi(oct->pci_dev);
1467 }
1468
Rick Farrington0c88a762017-03-13 12:58:04 -07001469 kfree(oct->irq_name_storage);
1470 oct->irq_name_storage = NULL;
1471
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001472 /* fallthrough */
1473 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001474 if (OCTEON_CN23XX_PF(oct))
1475 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001476
1477 /* fallthrough */
1478 case OCT_DEV_MBOX_SETUP_DONE:
1479 if (OCTEON_CN23XX_PF(oct))
1480 oct->fn_list.free_mbox(oct);
1481
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001482 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001483 case OCT_DEV_IN_RESET:
1484 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001485 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001486 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001487 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001488 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001489 continue;
1490 octeon_delete_droq(oct, i);
1491 }
1492
1493 /* Force any pending handshakes to complete */
1494 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1495 hs = &handshake[i];
1496
1497 if (hs->pci_dev) {
1498 handshake[oct->octeon_id].init_ok = 0;
1499 complete(&handshake[oct->octeon_id].init);
1500 handshake[oct->octeon_id].started_ok = 0;
1501 complete(&handshake[oct->octeon_id].started);
1502 }
1503 }
1504
1505 /* fallthrough */
1506 case OCT_DEV_RESP_LIST_INIT_DONE:
1507 octeon_delete_response_list(oct);
1508
1509 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001510 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001511 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001512 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001513 continue;
1514 octeon_delete_instr_queue(oct, i);
1515 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001516#ifdef CONFIG_PCI_IOV
1517 if (oct->sriov_info.sriov_enabled)
1518 pci_disable_sriov(oct->pci_dev);
1519#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001520 /* fallthrough */
1521 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1522 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001523
1524 /* fallthrough */
1525 case OCT_DEV_DISPATCH_INIT_DONE:
1526 octeon_delete_dispatch_list(oct);
1527 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1528
1529 /* fallthrough */
1530 case OCT_DEV_PCI_MAP_DONE:
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001531 if (!fw_type_is_none()) {
1532 /* Soft reset the octeon device before exiting */
1533 if (!OCTEON_CN23XX_PF(oct) ||
1534 (OCTEON_CN23XX_PF(oct) && !oct->octeon_id))
1535 oct->fn_list.soft_reset(oct);
1536 }
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001537
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001538 octeon_unmap_pci_barx(oct, 0);
1539 octeon_unmap_pci_barx(oct, 1);
1540
1541 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001542 case OCT_DEV_PCI_ENABLE_DONE:
1543 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001544 /* Disable the device, releasing the PCI INT */
1545 pci_disable_device(oct->pci_dev);
1546
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001547 /* fallthrough */
1548 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001549 /* Nothing to be done here either */
1550 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001551 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001552
1553 tasklet_kill(&oct_priv->droq_tasklet);
1554}
1555
1556/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001557 * \brief Callback for rx ctrl
1558 * @param status status of request
1559 * @param buf pointer to resp structure
1560 */
1561static void rx_ctl_callback(struct octeon_device *oct,
1562 u32 status,
1563 void *buf)
1564{
1565 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1566 struct liquidio_rx_ctl_context *ctx;
1567
1568 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1569
1570 oct = lio_get_device(ctx->octeon_id);
1571 if (status)
1572 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1573 CVM_CAST64(status));
1574 WRITE_ONCE(ctx->cond, 1);
1575
1576 /* This barrier is required to be sure that the response has been
1577 * written fully before waking up the handler
1578 */
1579 wmb();
1580
1581 wake_up_interruptible(&ctx->wc);
1582}
1583
1584/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001585 * \brief Send Rx control command
1586 * @param lio per-network private data
1587 * @param start_stop whether to start or stop
1588 */
1589static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1590{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001591 struct octeon_soft_command *sc;
1592 struct liquidio_rx_ctl_context *ctx;
1593 union octnet_cmd *ncmd;
1594 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1595 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1596 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001597
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001598 if (oct->props[lio->ifidx].rx_on == start_stop)
1599 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001600
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001601 sc = (struct octeon_soft_command *)
1602 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1603 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001604
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001605 ncmd = (union octnet_cmd *)sc->virtdptr;
1606 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1607
1608 WRITE_ONCE(ctx->cond, 0);
1609 ctx->octeon_id = lio_get_device_id(oct);
1610 init_waitqueue_head(&ctx->wc);
1611
1612 ncmd->u64 = 0;
1613 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1614 ncmd->s.param1 = start_stop;
1615
1616 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1617
1618 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1619
1620 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1621 OPCODE_NIC_CMD, 0, 0, 0);
1622
1623 sc->callback = rx_ctl_callback;
1624 sc->callback_arg = sc;
1625 sc->wait_time = 5000;
1626
1627 retval = octeon_send_soft_command(oct, sc);
1628 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001629 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001630 } else {
1631 /* Sleep on a wait queue till the cond flag indicates that the
1632 * response arrived or timed-out.
1633 */
1634 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1635 return;
1636 oct->props[lio->ifidx].rx_on = start_stop;
1637 }
1638
1639 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001640}
1641
1642/**
1643 * \brief Destroy NIC device interface
1644 * @param oct octeon device
1645 * @param ifidx which interface to destroy
1646 *
1647 * Cleanup associated with each interface for an Octeon device when NIC
1648 * module is being unloaded or if initialization fails during load.
1649 */
1650static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1651{
1652 struct net_device *netdev = oct->props[ifidx].netdev;
1653 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001654 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001655
1656 if (!netdev) {
1657 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1658 __func__, ifidx);
1659 return;
1660 }
1661
1662 lio = GET_LIO(netdev);
1663
1664 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1665
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001666 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001667 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001668
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001669 if (fw_type_is_none()) {
1670 struct octnic_ctrl_pkt nctrl;
1671
1672 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1673 nctrl.ncmd.s.cmd = OCTNET_CMD_RESET_PF;
1674 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1675 octnet_send_nic_ctrl_pkt(oct, &nctrl);
1676 }
1677
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001678 if (oct->props[lio->ifidx].napi_enabled == 1) {
1679 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1680 napi_disable(napi);
1681
1682 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001683
1684 if (OCTEON_CN23XX_PF(oct))
1685 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001686 }
1687
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001688 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1689 unregister_netdev(netdev);
1690
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001691 cleanup_link_status_change_wq(netdev);
1692
Satanand Burla031d4f12017-03-22 11:31:13 -07001693 cleanup_rx_oom_poll_fn(netdev);
1694
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001695 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001696
1697 free_netdev(netdev);
1698
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001699 oct->props[ifidx].gmxport = -1;
1700
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001701 oct->props[ifidx].netdev = NULL;
1702}
1703
1704/**
1705 * \brief Stop complete NIC functionality
1706 * @param oct octeon device
1707 */
1708static int liquidio_stop_nic_module(struct octeon_device *oct)
1709{
1710 int i, j;
1711 struct lio *lio;
1712
1713 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1714 if (!oct->ifcount) {
1715 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1716 return 1;
1717 }
1718
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001719 spin_lock_bh(&oct->cmd_resp_wqlock);
1720 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1721 spin_unlock_bh(&oct->cmd_resp_wqlock);
1722
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001723 for (i = 0; i < oct->ifcount; i++) {
1724 lio = GET_LIO(oct->props[i].netdev);
1725 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001726 octeon_unregister_droq_ops(oct,
1727 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001728 }
1729
1730 for (i = 0; i < oct->ifcount; i++)
1731 liquidio_destroy_nic_device(oct, i);
1732
1733 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1734 return 0;
1735}
1736
1737/**
1738 * \brief Cleans up resources at unload time
1739 * @param pdev PCI device structure
1740 */
1741static void liquidio_remove(struct pci_dev *pdev)
1742{
1743 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1744
1745 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1746
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001747 if (oct_dev->watchdog_task)
1748 kthread_stop(oct_dev->watchdog_task);
1749
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001750 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1751 liquidio_stop_nic_module(oct_dev);
1752
1753 /* Reset the octeon device and cleanup all memory allocated for
1754 * the octeon device by driver.
1755 */
1756 octeon_destroy_resources(oct_dev);
1757
1758 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1759
1760 /* This octeon device has been removed. Update the global
1761 * data structure to reflect this. Free the device structure.
1762 */
1763 octeon_free_device_mem(oct_dev);
1764}
1765
1766/**
1767 * \brief Identify the Octeon device and to map the BAR address space
1768 * @param oct octeon device
1769 */
1770static int octeon_chip_specific_setup(struct octeon_device *oct)
1771{
1772 u32 dev_id, rev_id;
1773 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001774 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001775
1776 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1777 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1778 oct->rev_id = rev_id & 0xff;
1779
1780 switch (dev_id) {
1781 case OCTEON_CN68XX_PCIID:
1782 oct->chip_id = OCTEON_CN68XX;
1783 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001784 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001785 break;
1786
1787 case OCTEON_CN66XX_PCIID:
1788 oct->chip_id = OCTEON_CN66XX;
1789 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001790 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001791 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001792
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001793 case OCTEON_CN23XX_PCIID_PF:
1794 oct->chip_id = OCTEON_CN23XX_PF_VID;
1795 ret = setup_cn23xx_octeon_pf_device(oct);
1796 s = "CN23XX";
1797 break;
1798
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001799 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001800 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001801 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1802 dev_id);
1803 }
1804
1805 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001806 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001807 OCTEON_MAJOR_REV(oct),
1808 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001809 octeon_get_conf(oct)->card_name,
1810 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001811
1812 return ret;
1813}
1814
1815/**
1816 * \brief PCI initialization for each Octeon device.
1817 * @param oct octeon device
1818 */
1819static int octeon_pci_os_setup(struct octeon_device *oct)
1820{
1821 /* setup PCI stuff first */
1822 if (pci_enable_device(oct->pci_dev)) {
1823 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1824 return 1;
1825 }
1826
1827 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1828 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001829 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001830 return 1;
1831 }
1832
1833 /* Enable PCI DMA Master. */
1834 pci_set_master(oct->pci_dev);
1835
1836 return 0;
1837}
1838
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001839static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1840{
1841 int q = 0;
1842
1843 if (netif_is_multiqueue(lio->netdev))
1844 q = skb->queue_mapping % lio->linfo.num_txpciq;
1845
1846 return q;
1847}
1848
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001849/**
1850 * \brief Check Tx queue state for a given network buffer
1851 * @param lio per-network private data
1852 * @param skb network buffer
1853 */
1854static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1855{
1856 int q = 0, iq = 0;
1857
1858 if (netif_is_multiqueue(lio->netdev)) {
1859 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001860 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001861 } else {
1862 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001863 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001864 }
1865
1866 if (octnet_iq_is_full(lio->oct_dev, iq))
1867 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001868
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001869 if (__netif_subqueue_stopped(lio->netdev, q)) {
1870 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001871 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001872 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001873 return 1;
1874}
1875
1876/**
1877 * \brief Unmap and free network buffer
1878 * @param buf buffer
1879 */
1880static void free_netbuf(void *buf)
1881{
1882 struct sk_buff *skb;
1883 struct octnet_buf_free_info *finfo;
1884 struct lio *lio;
1885
1886 finfo = (struct octnet_buf_free_info *)buf;
1887 skb = finfo->skb;
1888 lio = finfo->lio;
1889
1890 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1891 DMA_TO_DEVICE);
1892
1893 check_txq_state(lio, skb);
1894
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001895 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001896}
1897
1898/**
1899 * \brief Unmap and free gather buffer
1900 * @param buf buffer
1901 */
1902static void free_netsgbuf(void *buf)
1903{
1904 struct octnet_buf_free_info *finfo;
1905 struct sk_buff *skb;
1906 struct lio *lio;
1907 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001908 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001909
1910 finfo = (struct octnet_buf_free_info *)buf;
1911 skb = finfo->skb;
1912 lio = finfo->lio;
1913 g = finfo->g;
1914 frags = skb_shinfo(skb)->nr_frags;
1915
1916 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1917 g->sg[0].ptr[0], (skb->len - skb->data_len),
1918 DMA_TO_DEVICE);
1919
1920 i = 1;
1921 while (frags--) {
1922 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1923
1924 pci_unmap_page((lio->oct_dev)->pci_dev,
1925 g->sg[(i >> 2)].ptr[(i & 3)],
1926 frag->size, DMA_TO_DEVICE);
1927 i++;
1928 }
1929
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001930 iq = skb_iq(lio, skb);
1931 spin_lock(&lio->glist_lock[iq]);
1932 list_add_tail(&g->list, &lio->glist[iq]);
1933 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001934
1935 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1936
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001937 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001938}
1939
1940/**
1941 * \brief Unmap and free gather buffer with response
1942 * @param buf buffer
1943 */
1944static void free_netsgbuf_with_resp(void *buf)
1945{
1946 struct octeon_soft_command *sc;
1947 struct octnet_buf_free_info *finfo;
1948 struct sk_buff *skb;
1949 struct lio *lio;
1950 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001951 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001952
1953 sc = (struct octeon_soft_command *)buf;
1954 skb = (struct sk_buff *)sc->callback_arg;
1955 finfo = (struct octnet_buf_free_info *)&skb->cb;
1956
1957 lio = finfo->lio;
1958 g = finfo->g;
1959 frags = skb_shinfo(skb)->nr_frags;
1960
1961 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1962 g->sg[0].ptr[0], (skb->len - skb->data_len),
1963 DMA_TO_DEVICE);
1964
1965 i = 1;
1966 while (frags--) {
1967 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1968
1969 pci_unmap_page((lio->oct_dev)->pci_dev,
1970 g->sg[(i >> 2)].ptr[(i & 3)],
1971 frag->size, DMA_TO_DEVICE);
1972 i++;
1973 }
1974
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001975 iq = skb_iq(lio, skb);
1976
1977 spin_lock(&lio->glist_lock[iq]);
1978 list_add_tail(&g->list, &lio->glist[iq]);
1979 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001980
1981 /* Don't free the skb yet */
1982
1983 check_txq_state(lio, skb);
1984}
1985
1986/**
1987 * \brief Adjust ptp frequency
1988 * @param ptp PTP clock info
1989 * @param ppb how much to adjust by, in parts-per-billion
1990 */
1991static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1992{
1993 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1994 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1995 u64 comp, delta;
1996 unsigned long flags;
1997 bool neg_adj = false;
1998
1999 if (ppb < 0) {
2000 neg_adj = true;
2001 ppb = -ppb;
2002 }
2003
2004 /* The hardware adds the clock compensation value to the
2005 * PTP clock on every coprocessor clock cycle, so we
2006 * compute the delta in terms of coprocessor clocks.
2007 */
2008 delta = (u64)ppb << 32;
2009 do_div(delta, oct->coproc_clock_rate);
2010
2011 spin_lock_irqsave(&lio->ptp_lock, flags);
2012 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
2013 if (neg_adj)
2014 comp -= delta;
2015 else
2016 comp += delta;
2017 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2018 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2019
2020 return 0;
2021}
2022
2023/**
2024 * \brief Adjust ptp time
2025 * @param ptp PTP clock info
2026 * @param delta how much to adjust by, in nanosecs
2027 */
2028static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2029{
2030 unsigned long flags;
2031 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2032
2033 spin_lock_irqsave(&lio->ptp_lock, flags);
2034 lio->ptp_adjust += delta;
2035 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2036
2037 return 0;
2038}
2039
2040/**
2041 * \brief Get hardware clock time, including any adjustment
2042 * @param ptp PTP clock info
2043 * @param ts timespec
2044 */
2045static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2046 struct timespec64 *ts)
2047{
2048 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002049 unsigned long flags;
2050 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2051 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2052
2053 spin_lock_irqsave(&lio->ptp_lock, flags);
2054 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2055 ns += lio->ptp_adjust;
2056 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2057
Kefeng Wang286af312016-01-27 17:34:37 +08002058 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002059
2060 return 0;
2061}
2062
2063/**
2064 * \brief Set hardware clock time. Reset adjustment
2065 * @param ptp PTP clock info
2066 * @param ts timespec
2067 */
2068static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2069 const struct timespec64 *ts)
2070{
2071 u64 ns;
2072 unsigned long flags;
2073 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2074 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2075
2076 ns = timespec_to_ns(ts);
2077
2078 spin_lock_irqsave(&lio->ptp_lock, flags);
2079 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2080 lio->ptp_adjust = 0;
2081 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2082
2083 return 0;
2084}
2085
2086/**
2087 * \brief Check if PTP is enabled
2088 * @param ptp PTP clock info
2089 * @param rq request
2090 * @param on is it on
2091 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002092static int
2093liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2094 struct ptp_clock_request *rq __attribute__((unused)),
2095 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002096{
2097 return -EOPNOTSUPP;
2098}
2099
2100/**
2101 * \brief Open PTP clock source
2102 * @param netdev network device
2103 */
2104static void oct_ptp_open(struct net_device *netdev)
2105{
2106 struct lio *lio = GET_LIO(netdev);
2107 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2108
2109 spin_lock_init(&lio->ptp_lock);
2110
2111 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2112 lio->ptp_info.owner = THIS_MODULE;
2113 lio->ptp_info.max_adj = 250000000;
2114 lio->ptp_info.n_alarm = 0;
2115 lio->ptp_info.n_ext_ts = 0;
2116 lio->ptp_info.n_per_out = 0;
2117 lio->ptp_info.pps = 0;
2118 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2119 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2120 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2121 lio->ptp_info.settime64 = liquidio_ptp_settime;
2122 lio->ptp_info.enable = liquidio_ptp_enable;
2123
2124 lio->ptp_adjust = 0;
2125
2126 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2127 &oct->pci_dev->dev);
2128
2129 if (IS_ERR(lio->ptp_clock))
2130 lio->ptp_clock = NULL;
2131}
2132
2133/**
2134 * \brief Init PTP clock
2135 * @param oct octeon device
2136 */
2137static void liquidio_ptp_init(struct octeon_device *oct)
2138{
2139 u64 clock_comp, cfg;
2140
2141 clock_comp = (u64)NSEC_PER_SEC << 32;
2142 do_div(clock_comp, oct->coproc_clock_rate);
2143 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2144
2145 /* Enable */
2146 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2147 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2148}
2149
2150/**
2151 * \brief Load firmware to device
2152 * @param oct octeon device
2153 *
2154 * Maps device to firmware filename, requests firmware, and downloads it
2155 */
2156static int load_firmware(struct octeon_device *oct)
2157{
2158 int ret = 0;
2159 const struct firmware *fw;
2160 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2161 char *tmp_fw_type;
2162
Felix Manlunas7cc61db2017-03-23 13:26:28 -07002163 if (fw_type_is_none()) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002164 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2165 return ret;
2166 }
2167
2168 if (fw_type[0] == '\0')
2169 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2170 else
2171 tmp_fw_type = fw_type;
2172
2173 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2174 octeon_get_conf(oct)->card_name, tmp_fw_type,
2175 LIO_FW_NAME_SUFFIX);
2176
2177 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2178 if (ret) {
2179 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2180 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002181 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002182 return ret;
2183 }
2184
2185 ret = octeon_download_firmware(oct, fw->data, fw->size);
2186
2187 release_firmware(fw);
2188
2189 return ret;
2190}
2191
2192/**
2193 * \brief Setup output queue
2194 * @param oct octeon device
2195 * @param q_no which queue
2196 * @param num_descs how many descriptors
2197 * @param desc_size size of each descriptor
2198 * @param app_ctx application context
2199 */
2200static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2201 int desc_size, void *app_ctx)
2202{
2203 int ret_val = 0;
2204
2205 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2206 /* droq creation and local register settings. */
2207 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302208 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002209 return ret_val;
2210
2211 if (ret_val == 1) {
2212 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2213 return 0;
2214 }
2215 /* tasklet creation for the droq */
2216
2217 /* Enable the droq queues */
2218 octeon_set_droq_pkt_op(oct, q_no, 1);
2219
2220 /* Send Credit for Octeon Output queues. Credits are always
2221 * sent after the output queue is enabled.
2222 */
2223 writel(oct->droq[q_no]->max_count,
2224 oct->droq[q_no]->pkts_credit_reg);
2225
2226 return ret_val;
2227}
2228
2229/**
2230 * \brief Callback for getting interface configuration
2231 * @param status status of request
2232 * @param buf pointer to resp structure
2233 */
2234static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002235 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002236 void *buf)
2237{
2238 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2239 struct liquidio_if_cfg_resp *resp;
2240 struct liquidio_if_cfg_context *ctx;
2241
2242 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002243 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002244
2245 oct = lio_get_device(ctx->octeon_id);
2246 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002247 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2248 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002249 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002250
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002251 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2252 resp->cfg_info.liquidio_firmware_version);
2253
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002254 /* This barrier is required to be sure that the response has been
2255 * written fully before waking up the handler
2256 */
2257 wmb();
2258
2259 wake_up_interruptible(&ctx->wc);
2260}
2261
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002262/** Routine to push packets arriving on Octeon interface upto network layer.
2263 * @param oct_id - octeon device id.
2264 * @param skbuff - skbuff struct to be passed to network layer.
2265 * @param len - size of total data received.
2266 * @param rh - Control header associated with the packet
2267 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002268 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002269 */
2270static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002271liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002272 void *skbuff,
2273 u32 len,
2274 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002275 void *param,
2276 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002277{
2278 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002279 struct sk_buff *skb = (struct sk_buff *)skbuff;
2280 struct skb_shared_hwtstamps *shhwtstamps;
2281 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002282 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002283 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002284 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002285 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2286 napi);
2287 if (netdev) {
2288 int packet_was_received;
2289 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002290 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002291
2292 /* Do not proceed if the interface is not in RUNNING state. */
2293 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2294 recv_buffer_free(skb);
2295 droq->stats.rx_dropped++;
2296 return;
2297 }
2298
2299 skb->dev = netdev;
2300
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002301 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002302 if (likely(len > MIN_SKB_SIZE)) {
2303 struct octeon_skb_page_info *pg_info;
2304 unsigned char *va;
2305
2306 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2307 if (pg_info->page) {
2308 /* For Paged allocation use the frags */
2309 va = page_address(pg_info->page) +
2310 pg_info->page_offset;
2311 memcpy(skb->data, va, MIN_SKB_SIZE);
2312 skb_put(skb, MIN_SKB_SIZE);
2313 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2314 pg_info->page,
2315 pg_info->page_offset +
2316 MIN_SKB_SIZE,
2317 len - MIN_SKB_SIZE,
2318 LIO_RXBUFFER_SZ);
2319 }
2320 } else {
2321 struct octeon_skb_page_info *pg_info =
2322 ((struct octeon_skb_page_info *)(skb->cb));
2323 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2324 + pg_info->page_offset, len);
2325 skb_put(skb, len);
2326 put_page(pg_info->page);
2327 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002328
Prasad Kannegantide28c992017-01-09 14:42:40 -08002329 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2330
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002331 if (((oct->chip_id == OCTEON_CN66XX) ||
2332 (oct->chip_id == OCTEON_CN68XX)) &&
2333 ptp_enable) {
2334 if (rh->r_dh.has_hwtstamp) {
2335 /* timestamp is included from the hardware at
2336 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002337 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002338 if (ifstate_check
2339 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2340 /* Nanoseconds are in the first 64-bits
2341 * of the packet.
2342 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002343 memcpy(&ns, (skb->data + r_dh_off),
2344 sizeof(ns));
2345 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002346 shhwtstamps = skb_hwtstamps(skb);
2347 shhwtstamps->hwtstamp =
2348 ns_to_ktime(ns +
2349 lio->ptp_adjust);
2350 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002351 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002352 }
2353
Prasad Kannegantide28c992017-01-09 14:42:40 -08002354 if (rh->r_dh.has_hash) {
2355 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2356 u32 hash = be32_to_cpu(*hash_be);
2357
2358 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2359 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2360 }
2361
2362 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2363
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002364 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002365 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002366 (((rh->r_dh.encap_on) &&
2367 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2368 (!(rh->r_dh.encap_on) &&
2369 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002370 /* checksum has already been verified */
2371 skb->ip_summed = CHECKSUM_UNNECESSARY;
2372 else
2373 skb->ip_summed = CHECKSUM_NONE;
2374
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002375 /* Setting Encapsulation field on basis of status received
2376 * from the firmware
2377 */
2378 if (rh->r_dh.encap_on) {
2379 skb->encapsulation = 1;
2380 skb->csum_level = 1;
2381 droq->stats.rx_vxlan++;
2382 }
2383
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002384 /* inbound VLAN tag */
2385 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2386 (rh->r_dh.vlan != 0)) {
2387 u16 vid = rh->r_dh.vlan;
2388 u16 priority = rh->r_dh.priority;
2389
2390 vtag = priority << 13 | vid;
2391 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2392 }
2393
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002394 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2395
2396 if (packet_was_received) {
2397 droq->stats.rx_bytes_received += len;
2398 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002399 } else {
2400 droq->stats.rx_dropped++;
2401 netif_info(lio, rx_err, lio->netdev,
2402 "droq:%d error rx_dropped:%llu\n",
2403 droq->q_no, droq->stats.rx_dropped);
2404 }
2405
2406 } else {
2407 recv_buffer_free(skb);
2408 }
2409}
2410
2411/**
2412 * \brief wrapper for calling napi_schedule
2413 * @param param parameters to pass to napi_schedule
2414 *
2415 * Used when scheduling on different CPUs
2416 */
2417static void napi_schedule_wrapper(void *param)
2418{
2419 struct napi_struct *napi = param;
2420
2421 napi_schedule(napi);
2422}
2423
2424/**
2425 * \brief callback when receive interrupt occurs and we are in NAPI mode
2426 * @param arg pointer to octeon output queue
2427 */
2428static void liquidio_napi_drv_callback(void *arg)
2429{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002430 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002431 struct octeon_droq *droq = arg;
2432 int this_cpu = smp_processor_id();
2433
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002434 oct = droq->oct_dev;
2435
2436 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2437 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002438 } else {
2439 struct call_single_data *csd = &droq->csd;
2440
2441 csd->func = napi_schedule_wrapper;
2442 csd->info = &droq->napi;
2443 csd->flags = 0;
2444
2445 smp_call_function_single_async(droq->cpu_id, csd);
2446 }
2447}
2448
2449/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002450 * \brief Entry point for NAPI polling
2451 * @param napi NAPI structure
2452 * @param budget maximum number of items to process
2453 */
2454static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2455{
2456 struct octeon_droq *droq;
2457 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002458 int tx_done = 0, iq_no;
2459 struct octeon_instr_queue *iq;
2460 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002461
2462 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002463 oct = droq->oct_dev;
2464 iq_no = droq->q_no;
2465 /* Handle Droq descriptors */
2466 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2467 POLL_EVENT_PROCESS_PKTS,
2468 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002469
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002470 /* Flush the instruction queue */
2471 iq = oct->instr_queue[iq_no];
2472 if (iq) {
VSR Burru6069f3f2017-03-22 11:54:50 -07002473 if (atomic_read(&iq->instr_pending))
2474 /* Process iq buffers with in the budget limits */
2475 tx_done = octeon_flush_iq(oct, iq, budget);
2476 else
2477 tx_done = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002478 /* Update iq read-index rather than waiting for next interrupt.
2479 * Return back if tx_done is false.
2480 */
2481 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002482 } else {
2483 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2484 __func__, iq_no);
2485 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002486
Satanand Burlacdb478e2017-01-31 13:04:42 -08002487 /* force enable interrupt if reg cnts are high to avoid wraparound */
2488 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002489 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002490 (droq->pkt_count >= MAX_REG_CNT)) {
2491 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002492 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002493 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2494 POLL_EVENT_ENABLE_INTR, 0);
2495 return 0;
2496 }
2497
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002498 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002499}
2500
2501/**
2502 * \brief Setup input and output queues
2503 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002504 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002505 *
2506 * Note: Queues are with respect to the octeon device. Thus
2507 * an input queue is for egress packets, and output queues
2508 * are for ingress packets.
2509 */
2510static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002511 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002512{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002513 struct octeon_droq_ops droq_ops;
2514 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002515 static int cpu_id;
2516 static int cpu_id_modulus;
2517 struct octeon_droq *droq;
2518 struct napi_struct *napi;
2519 int q, q_no, retval = 0;
2520 struct lio *lio;
2521 int num_tx_descs;
2522
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002523 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002524
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002525 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002526
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002527 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2528
2529 droq_ops.fptr = liquidio_push_packet;
2530 droq_ops.farg = (void *)netdev;
2531
2532 droq_ops.poll_mode = 1;
2533 droq_ops.napi_fn = liquidio_napi_drv_callback;
2534 cpu_id = 0;
2535 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002536
2537 /* set up DROQs. */
2538 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002539 q_no = lio->linfo.rxpciq[q].s.q_no;
2540 dev_dbg(&octeon_dev->pci_dev->dev,
2541 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2542 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002543 retval = octeon_setup_droq(octeon_dev, q_no,
2544 CFG_GET_NUM_RX_DESCS_NIC_IF
2545 (octeon_get_conf(octeon_dev),
2546 lio->ifidx),
2547 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2548 (octeon_get_conf(octeon_dev),
2549 lio->ifidx), NULL);
2550 if (retval) {
2551 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002552 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002553 __func__);
2554 return 1;
2555 }
2556
2557 droq = octeon_dev->droq[q_no];
2558 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002559 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2560 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002561 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002562
2563 /* designate a CPU for this droq */
2564 droq->cpu_id = cpu_id;
2565 cpu_id++;
2566 if (cpu_id >= cpu_id_modulus)
2567 cpu_id = 0;
2568
2569 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2570 }
2571
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002572 if (OCTEON_CN23XX_PF(octeon_dev)) {
2573 /* 23XX PF can receive control messages (via the first PF-owned
2574 * droq) from the firmware even if the ethX interface is down,
2575 * so that's why poll_mode must be off for the first droq.
2576 */
2577 octeon_dev->droq[0]->ops.poll_mode = 0;
2578 }
2579
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002580 /* set up IQs. */
2581 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2582 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2583 (octeon_dev),
2584 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002585 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2586 lio->linfo.txpciq[q], num_tx_descs,
2587 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002588 if (retval) {
2589 dev_err(&octeon_dev->pci_dev->dev,
2590 " %s : Runtime IQ(TxQ) creation failed.\n",
2591 __func__);
2592 return 1;
2593 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002594
2595 if (octeon_dev->ioq_vector) {
2596 struct octeon_ioq_vector *ioq_vector;
2597
2598 ioq_vector = &octeon_dev->ioq_vector[q];
2599 netif_set_xps_queue(netdev,
2600 &ioq_vector->affinity_mask,
2601 ioq_vector->iq_index);
2602 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002603 }
2604
2605 return 0;
2606}
2607
2608/**
2609 * \brief Poll routine for checking transmit queue status
2610 * @param work work_struct data structure
2611 */
2612static void octnet_poll_check_txq_status(struct work_struct *work)
2613{
2614 struct cavium_wk *wk = (struct cavium_wk *)work;
2615 struct lio *lio = (struct lio *)wk->ctxptr;
2616
2617 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2618 return;
2619
2620 check_txq_status(lio);
2621 queue_delayed_work(lio->txq_status_wq.wq,
2622 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2623}
2624
2625/**
2626 * \brief Sets up the txq poll check
2627 * @param netdev network device
2628 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002629static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002630{
2631 struct lio *lio = GET_LIO(netdev);
2632 struct octeon_device *oct = lio->oct_dev;
2633
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302634 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2635 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002636 if (!lio->txq_status_wq.wq) {
2637 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002638 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002639 }
2640 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2641 octnet_poll_check_txq_status);
2642 lio->txq_status_wq.wk.ctxptr = lio;
2643 queue_delayed_work(lio->txq_status_wq.wq,
2644 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002645 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002646}
2647
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002648static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2649{
2650 struct lio *lio = GET_LIO(netdev);
2651
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002652 if (lio->txq_status_wq.wq) {
2653 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2654 destroy_workqueue(lio->txq_status_wq.wq);
2655 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002656}
2657
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002658/**
2659 * \brief Net device open for LiquidIO
2660 * @param netdev network device
2661 */
2662static int liquidio_open(struct net_device *netdev)
2663{
2664 struct lio *lio = GET_LIO(netdev);
2665 struct octeon_device *oct = lio->oct_dev;
2666 struct napi_struct *napi, *n;
2667
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002668 if (oct->props[lio->ifidx].napi_enabled == 0) {
2669 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2670 napi_enable(napi);
2671
2672 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002673
2674 if (OCTEON_CN23XX_PF(oct))
2675 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002676 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002677
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002678 if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
2679 ptp_enable)
2680 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002681
2682 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002683
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002684 /* Ready for link status updates */
2685 lio->intf_open = 1;
2686
2687 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2688
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002689 if (OCTEON_CN23XX_PF(oct)) {
2690 if (!oct->msix_on)
2691 if (setup_tx_poll_fn(netdev))
2692 return -1;
2693 } else {
2694 if (setup_tx_poll_fn(netdev))
2695 return -1;
2696 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002697
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002698 start_txq(netdev);
2699
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002700 /* tell Octeon to start forwarding packets to host */
2701 send_rx_ctrl_cmd(lio, 1);
2702
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002703 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2704 netdev->name);
2705
2706 return 0;
2707}
2708
2709/**
2710 * \brief Net device stop for LiquidIO
2711 * @param netdev network device
2712 */
2713static int liquidio_stop(struct net_device *netdev)
2714{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002715 struct lio *lio = GET_LIO(netdev);
2716 struct octeon_device *oct = lio->oct_dev;
2717
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002718 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2719
2720 netif_tx_disable(netdev);
2721
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002722 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002723 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002724 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002725 lio->linfo.link.s.link_up = 0;
2726 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002727
Felix Manlunascb2336b2017-01-11 17:09:02 -08002728 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002729 send_rx_ctrl_cmd(lio, 0);
2730
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002731 if (OCTEON_CN23XX_PF(oct)) {
2732 if (!oct->msix_on)
2733 cleanup_tx_poll_fn(netdev);
2734 } else {
2735 cleanup_tx_poll_fn(netdev);
2736 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002737
2738 if (lio->ptp_clock) {
2739 ptp_clock_unregister(lio->ptp_clock);
2740 lio->ptp_clock = NULL;
2741 }
2742
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002743 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002744
2745 return 0;
2746}
2747
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002748/**
2749 * \brief Converts a mask based on net device flags
2750 * @param netdev network device
2751 *
2752 * This routine generates a octnet_ifflags mask from the net device flags
2753 * received from the OS.
2754 */
2755static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2756{
2757 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2758
2759 if (netdev->flags & IFF_PROMISC)
2760 f |= OCTNET_IFFLAG_PROMISC;
2761
2762 if (netdev->flags & IFF_ALLMULTI)
2763 f |= OCTNET_IFFLAG_ALLMULTI;
2764
2765 if (netdev->flags & IFF_MULTICAST) {
2766 f |= OCTNET_IFFLAG_MULTICAST;
2767
2768 /* Accept all multicast addresses if there are more than we
2769 * can handle
2770 */
2771 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2772 f |= OCTNET_IFFLAG_ALLMULTI;
2773 }
2774
2775 if (netdev->flags & IFF_BROADCAST)
2776 f |= OCTNET_IFFLAG_BROADCAST;
2777
2778 return f;
2779}
2780
2781/**
2782 * \brief Net device set_multicast_list
2783 * @param netdev network device
2784 */
2785static void liquidio_set_mcast_list(struct net_device *netdev)
2786{
2787 struct lio *lio = GET_LIO(netdev);
2788 struct octeon_device *oct = lio->oct_dev;
2789 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002790 struct netdev_hw_addr *ha;
2791 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002792 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002793 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2794
2795 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2796
2797 /* Create a ctrl pkt command to be sent to core app. */
2798 nctrl.ncmd.u64 = 0;
2799 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002800 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2801 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002803 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002804 nctrl.netpndev = (u64)netdev;
2805 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2806
2807 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002808 mc = &nctrl.udd[0];
2809 netdev_for_each_mc_addr(ha, netdev) {
2810 *mc = 0;
2811 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2812 /* no need to swap bytes */
2813
2814 if (++mc > &nctrl.udd[mc_count])
2815 break;
2816 }
2817
2818 /* Apparently, any activity in this call from the kernel has to
2819 * be atomic. So we won't wait for response.
2820 */
2821 nctrl.wait_time = 0;
2822
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002823 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002824 if (ret < 0) {
2825 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2826 ret);
2827 }
2828}
2829
2830/**
2831 * \brief Net device set_mac_address
2832 * @param netdev network device
2833 */
2834static int liquidio_set_mac(struct net_device *netdev, void *p)
2835{
2836 int ret = 0;
2837 struct lio *lio = GET_LIO(netdev);
2838 struct octeon_device *oct = lio->oct_dev;
2839 struct sockaddr *addr = (struct sockaddr *)p;
2840 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002841
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002842 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002843 return -EADDRNOTAVAIL;
2844
2845 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2846
2847 nctrl.ncmd.u64 = 0;
2848 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002849 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002850 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002851 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002852 nctrl.netpndev = (u64)netdev;
2853 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2854 nctrl.wait_time = 100;
2855
2856 nctrl.udd[0] = 0;
2857 /* The MAC Address is presented in network byte order. */
2858 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2859
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002860 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002861 if (ret < 0) {
2862 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2863 return -ENOMEM;
2864 }
2865 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2866 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2867
2868 return 0;
2869}
2870
2871/**
2872 * \brief Net device get_stats
2873 * @param netdev network device
2874 */
2875static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2876{
2877 struct lio *lio = GET_LIO(netdev);
2878 struct net_device_stats *stats = &netdev->stats;
2879 struct octeon_device *oct;
2880 u64 pkts = 0, drop = 0, bytes = 0;
2881 struct oct_droq_stats *oq_stats;
2882 struct oct_iq_stats *iq_stats;
2883 int i, iq_no, oq_no;
2884
2885 oct = lio->oct_dev;
2886
2887 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002888 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002889 iq_stats = &oct->instr_queue[iq_no]->stats;
2890 pkts += iq_stats->tx_done;
2891 drop += iq_stats->tx_dropped;
2892 bytes += iq_stats->tx_tot_bytes;
2893 }
2894
2895 stats->tx_packets = pkts;
2896 stats->tx_bytes = bytes;
2897 stats->tx_dropped = drop;
2898
2899 pkts = 0;
2900 drop = 0;
2901 bytes = 0;
2902
2903 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002904 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002905 oq_stats = &oct->droq[oq_no]->stats;
2906 pkts += oq_stats->rx_pkts_received;
2907 drop += (oq_stats->rx_dropped +
2908 oq_stats->dropped_nodispatch +
2909 oq_stats->dropped_toomany +
2910 oq_stats->dropped_nomem);
2911 bytes += oq_stats->rx_bytes_received;
2912 }
2913
2914 stats->rx_bytes = bytes;
2915 stats->rx_packets = pkts;
2916 stats->rx_dropped = drop;
2917
2918 return stats;
2919}
2920
2921/**
2922 * \brief Net device change_mtu
2923 * @param netdev network device
2924 */
2925static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2926{
2927 struct lio *lio = GET_LIO(netdev);
2928 struct octeon_device *oct = lio->oct_dev;
2929 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002930 int ret = 0;
2931
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002932 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2933
2934 nctrl.ncmd.u64 = 0;
2935 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002936 nctrl.ncmd.s.param1 = new_mtu;
2937 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002938 nctrl.wait_time = 100;
2939 nctrl.netpndev = (u64)netdev;
2940 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2941
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002942 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002943 if (ret < 0) {
2944 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2945 return -1;
2946 }
2947
2948 lio->mtu = new_mtu;
2949
2950 return 0;
2951}
2952
2953/**
2954 * \brief Handler for SIOCSHWTSTAMP ioctl
2955 * @param netdev network device
2956 * @param ifr interface request
2957 * @param cmd command
2958 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002959static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002960{
2961 struct hwtstamp_config conf;
2962 struct lio *lio = GET_LIO(netdev);
2963
2964 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2965 return -EFAULT;
2966
2967 if (conf.flags)
2968 return -EINVAL;
2969
2970 switch (conf.tx_type) {
2971 case HWTSTAMP_TX_ON:
2972 case HWTSTAMP_TX_OFF:
2973 break;
2974 default:
2975 return -ERANGE;
2976 }
2977
2978 switch (conf.rx_filter) {
2979 case HWTSTAMP_FILTER_NONE:
2980 break;
2981 case HWTSTAMP_FILTER_ALL:
2982 case HWTSTAMP_FILTER_SOME:
2983 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2984 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2985 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2986 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2987 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2988 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2989 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2990 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2991 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2992 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2993 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2994 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2995 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2996 break;
2997 default:
2998 return -ERANGE;
2999 }
3000
3001 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
3002 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3003
3004 else
3005 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3006
3007 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
3008}
3009
3010/**
3011 * \brief ioctl handler
3012 * @param netdev network device
3013 * @param ifr interface request
3014 * @param cmd command
3015 */
3016static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3017{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003018 struct lio *lio = GET_LIO(netdev);
3019
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003020 switch (cmd) {
3021 case SIOCSHWTSTAMP:
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003022 if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
3023 lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
3024 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003025 default:
3026 return -EOPNOTSUPP;
3027 }
3028}
3029
3030/**
3031 * \brief handle a Tx timestamp response
3032 * @param status response status
3033 * @param buf pointer to skb
3034 */
3035static void handle_timestamp(struct octeon_device *oct,
3036 u32 status,
3037 void *buf)
3038{
3039 struct octnet_buf_free_info *finfo;
3040 struct octeon_soft_command *sc;
3041 struct oct_timestamp_resp *resp;
3042 struct lio *lio;
3043 struct sk_buff *skb = (struct sk_buff *)buf;
3044
3045 finfo = (struct octnet_buf_free_info *)skb->cb;
3046 lio = finfo->lio;
3047 sc = finfo->sc;
3048 oct = lio->oct_dev;
3049 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3050
3051 if (status != OCTEON_REQUEST_DONE) {
3052 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3053 CVM_CAST64(status));
3054 resp->timestamp = 0;
3055 }
3056
3057 octeon_swap_8B_data(&resp->timestamp, 1);
3058
Colin Ian King19a6d152016-02-05 16:30:39 +00003059 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003060 struct skb_shared_hwtstamps ts;
3061 u64 ns = resp->timestamp;
3062
3063 netif_info(lio, tx_done, lio->netdev,
3064 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3065 skb, (unsigned long long)ns);
3066 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3067 skb_tstamp_tx(skb, &ts);
3068 }
3069
3070 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003071 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003072}
3073
3074/* \brief Send a data packet that will be timestamped
3075 * @param oct octeon device
3076 * @param ndata pointer to network data
3077 * @param finfo pointer to private network data
3078 */
3079static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3080 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003081 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003082{
3083 int retval;
3084 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003085 struct lio *lio;
3086 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003087 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003088
3089 lio = finfo->lio;
3090
3091 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3092 sizeof(struct oct_timestamp_resp));
3093 finfo->sc = sc;
3094
3095 if (!sc) {
3096 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3097 return IQ_SEND_FAILED;
3098 }
3099
3100 if (ndata->reqtype == REQTYPE_NORESP_NET)
3101 ndata->reqtype = REQTYPE_RESP_NET;
3102 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3103 ndata->reqtype = REQTYPE_RESP_NET_SG;
3104
3105 sc->callback = handle_timestamp;
3106 sc->callback_arg = finfo->skb;
3107 sc->iq_no = ndata->q_no;
3108
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003109 if (OCTEON_CN23XX_PF(oct))
3110 len = (u32)((struct octeon_instr_ih3 *)
3111 (&sc->cmd.cmd3.ih3))->dlengsz;
3112 else
3113 len = (u32)((struct octeon_instr_ih2 *)
3114 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003115
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003116 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003117
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003118 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003119 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003120
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003121 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003122 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3123 retval);
3124 octeon_free_soft_command(oct, sc);
3125 } else {
3126 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3127 }
3128
3129 return retval;
3130}
3131
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003132/** \brief Transmit networks packets to the Octeon interface
3133 * @param skbuff skbuff struct to be passed to network layer.
3134 * @param netdev pointer to network device
3135 * @returns whether the packet was transmitted to the device okay or not
3136 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3137 */
3138static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3139{
3140 struct lio *lio;
3141 struct octnet_buf_free_info *finfo;
3142 union octnic_cmd_setup cmdsetup;
3143 struct octnic_data_pkt ndata;
3144 struct octeon_device *oct;
3145 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003146 struct octeon_instr_irh *irh;
3147 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003148 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003149 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003150 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003151 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003152 u32 tag = 0;
3153
3154 lio = GET_LIO(netdev);
3155 oct = lio->oct_dev;
3156
3157 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003158 q_idx = skb->queue_mapping;
3159 q_idx = (q_idx % (lio->linfo.num_txpciq));
3160 tag = q_idx;
3161 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003162 } else {
3163 iq_no = lio->txq;
3164 }
3165
3166 stats = &oct->instr_queue[iq_no]->stats;
3167
3168 /* Check for all conditions in which the current packet cannot be
3169 * transmitted.
3170 */
3171 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003172 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003173 (skb->len <= 0)) {
3174 netif_info(lio, tx_err, lio->netdev,
3175 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003176 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003177 goto lio_xmit_failed;
3178 }
3179
3180 /* Use space in skb->cb to store info used to unmap and
3181 * free the buffers.
3182 */
3183 finfo = (struct octnet_buf_free_info *)skb->cb;
3184 finfo->lio = lio;
3185 finfo->skb = skb;
3186 finfo->sc = NULL;
3187
3188 /* Prepare the attributes for the data to be passed to OSI. */
3189 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3190
3191 ndata.buf = (void *)finfo;
3192
3193 ndata.q_no = iq_no;
3194
3195 if (netif_is_multiqueue(netdev)) {
3196 if (octnet_iq_is_full(oct, ndata.q_no)) {
3197 /* defer sending if queue is full */
3198 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3199 ndata.q_no);
3200 stats->tx_iq_busy++;
3201 return NETDEV_TX_BUSY;
3202 }
3203 } else {
3204 if (octnet_iq_is_full(oct, lio->txq)) {
3205 /* defer sending if queue is full */
3206 stats->tx_iq_busy++;
3207 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003208 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003209 return NETDEV_TX_BUSY;
3210 }
3211 }
3212 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003213 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003214 */
3215
3216 ndata.datasize = skb->len;
3217
3218 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003219 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003220
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003221 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3222 if (skb->encapsulation) {
3223 cmdsetup.s.tnl_csum = 1;
3224 stats->tx_vxlan++;
3225 } else {
3226 cmdsetup.s.transport_csum = 1;
3227 }
3228 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003229 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3230 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3231 cmdsetup.s.timestamp = 1;
3232 }
3233
3234 if (skb_shinfo(skb)->nr_frags == 0) {
3235 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003236 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003237
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003238 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003239 dptr = dma_map_single(&oct->pci_dev->dev,
3240 skb->data,
3241 skb->len,
3242 DMA_TO_DEVICE);
3243 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003244 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3245 __func__);
3246 return NETDEV_TX_BUSY;
3247 }
3248
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003249 if (OCTEON_CN23XX_PF(oct))
3250 ndata.cmd.cmd3.dptr = dptr;
3251 else
3252 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003253 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003254 ndata.reqtype = REQTYPE_NORESP_NET;
3255
3256 } else {
3257 int i, frags;
3258 struct skb_frag_struct *frag;
3259 struct octnic_gather *g;
3260
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003261 spin_lock(&lio->glist_lock[q_idx]);
3262 g = (struct octnic_gather *)
3263 list_delete_head(&lio->glist[q_idx]);
3264 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003265
3266 if (!g) {
3267 netif_info(lio, tx_err, lio->netdev,
3268 "Transmit scatter gather: glist null!\n");
3269 goto lio_xmit_failed;
3270 }
3271
3272 cmdsetup.s.gather = 1;
3273 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003274 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003275
3276 memset(g->sg, 0, g->sg_size);
3277
3278 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3279 skb->data,
3280 (skb->len - skb->data_len),
3281 DMA_TO_DEVICE);
3282 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3283 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3284 __func__);
3285 return NETDEV_TX_BUSY;
3286 }
3287 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3288
3289 frags = skb_shinfo(skb)->nr_frags;
3290 i = 1;
3291 while (frags--) {
3292 frag = &skb_shinfo(skb)->frags[i - 1];
3293
3294 g->sg[(i >> 2)].ptr[(i & 3)] =
3295 dma_map_page(&oct->pci_dev->dev,
3296 frag->page.p,
3297 frag->page_offset,
3298 frag->size,
3299 DMA_TO_DEVICE);
3300
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003301 if (dma_mapping_error(&oct->pci_dev->dev,
3302 g->sg[i >> 2].ptr[i & 3])) {
3303 dma_unmap_single(&oct->pci_dev->dev,
3304 g->sg[0].ptr[0],
3305 skb->len - skb->data_len,
3306 DMA_TO_DEVICE);
3307 for (j = 1; j < i; j++) {
3308 frag = &skb_shinfo(skb)->frags[j - 1];
3309 dma_unmap_page(&oct->pci_dev->dev,
3310 g->sg[j >> 2].ptr[j & 3],
3311 frag->size,
3312 DMA_TO_DEVICE);
3313 }
3314 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3315 __func__);
3316 return NETDEV_TX_BUSY;
3317 }
3318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003319 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3320 i++;
3321 }
3322
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003323 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003324
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003325 if (OCTEON_CN23XX_PF(oct))
3326 ndata.cmd.cmd3.dptr = dptr;
3327 else
3328 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003329 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003330 finfo->g = g;
3331
3332 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3333 }
3334
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003335 if (OCTEON_CN23XX_PF(oct)) {
3336 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3337 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3338 } else {
3339 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3340 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3341 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003342
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003343 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003344 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3345 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003346 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003347 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003348
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003349 /* HW insert VLAN tag */
3350 if (skb_vlan_tag_present(skb)) {
3351 irh->priority = skb_vlan_tag_get(skb) >> 13;
3352 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3353 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003354
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003355 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003356 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003357 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003358 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003359 if (status == IQ_SEND_FAILED)
3360 goto lio_xmit_failed;
3361
3362 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3363
3364 if (status == IQ_SEND_STOP)
3365 stop_q(lio->netdev, q_idx);
3366
Florian Westphal860e9532016-05-03 16:33:13 +02003367 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003368
Satanand Burla80c8eae2017-01-26 11:52:35 -08003369 if (tx_info->s.gso_segs)
3370 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003371 else
3372 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003373 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003374
3375 return NETDEV_TX_OK;
3376
3377lio_xmit_failed:
3378 stats->tx_dropped++;
3379 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3380 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003381 if (dptr)
3382 dma_unmap_single(&oct->pci_dev->dev, dptr,
3383 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003384 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003385 return NETDEV_TX_OK;
3386}
3387
3388/** \brief Network device Tx timeout
3389 * @param netdev pointer to network device
3390 */
3391static void liquidio_tx_timeout(struct net_device *netdev)
3392{
3393 struct lio *lio;
3394
3395 lio = GET_LIO(netdev);
3396
3397 netif_info(lio, tx_err, lio->netdev,
3398 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3399 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003400 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003401 txqs_wake(netdev);
3402}
3403
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003404static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3405 __be16 proto __attribute__((unused)),
3406 u16 vid)
3407{
3408 struct lio *lio = GET_LIO(netdev);
3409 struct octeon_device *oct = lio->oct_dev;
3410 struct octnic_ctrl_pkt nctrl;
3411 int ret = 0;
3412
3413 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3414
3415 nctrl.ncmd.u64 = 0;
3416 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3417 nctrl.ncmd.s.param1 = vid;
3418 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3419 nctrl.wait_time = 100;
3420 nctrl.netpndev = (u64)netdev;
3421 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3422
3423 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3424 if (ret < 0) {
3425 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3426 ret);
3427 }
3428
3429 return ret;
3430}
3431
3432static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3433 __be16 proto __attribute__((unused)),
3434 u16 vid)
3435{
3436 struct lio *lio = GET_LIO(netdev);
3437 struct octeon_device *oct = lio->oct_dev;
3438 struct octnic_ctrl_pkt nctrl;
3439 int ret = 0;
3440
3441 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3442
3443 nctrl.ncmd.u64 = 0;
3444 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3445 nctrl.ncmd.s.param1 = vid;
3446 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3447 nctrl.wait_time = 100;
3448 nctrl.netpndev = (u64)netdev;
3449 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3450
3451 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3452 if (ret < 0) {
3453 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3454 ret);
3455 }
3456 return ret;
3457}
3458
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003459/** Sending command to enable/disable RX checksum offload
3460 * @param netdev pointer to network device
3461 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3462 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3463 * OCTNET_CMD_RXCSUM_DISABLE
3464 * @returns SUCCESS or FAILURE
3465 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003466static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3467 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003468{
3469 struct lio *lio = GET_LIO(netdev);
3470 struct octeon_device *oct = lio->oct_dev;
3471 struct octnic_ctrl_pkt nctrl;
3472 int ret = 0;
3473
3474 nctrl.ncmd.u64 = 0;
3475 nctrl.ncmd.s.cmd = command;
3476 nctrl.ncmd.s.param1 = rx_cmd;
3477 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3478 nctrl.wait_time = 100;
3479 nctrl.netpndev = (u64)netdev;
3480 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3481
3482 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3483 if (ret < 0) {
3484 dev_err(&oct->pci_dev->dev,
3485 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3486 ret);
3487 }
3488 return ret;
3489}
3490
3491/** Sending command to add/delete VxLAN UDP port to firmware
3492 * @param netdev pointer to network device
3493 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3494 * @param vxlan_port VxLAN port to be added or deleted
3495 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3496 * OCTNET_CMD_VXLAN_PORT_DEL
3497 * @returns SUCCESS or FAILURE
3498 */
3499static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3500 u16 vxlan_port, u8 vxlan_cmd_bit)
3501{
3502 struct lio *lio = GET_LIO(netdev);
3503 struct octeon_device *oct = lio->oct_dev;
3504 struct octnic_ctrl_pkt nctrl;
3505 int ret = 0;
3506
3507 nctrl.ncmd.u64 = 0;
3508 nctrl.ncmd.s.cmd = command;
3509 nctrl.ncmd.s.more = vxlan_cmd_bit;
3510 nctrl.ncmd.s.param1 = vxlan_port;
3511 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3512 nctrl.wait_time = 100;
3513 nctrl.netpndev = (u64)netdev;
3514 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3515
3516 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3517 if (ret < 0) {
3518 dev_err(&oct->pci_dev->dev,
3519 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3520 ret);
3521 }
3522 return ret;
3523}
3524
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003525/** \brief Net device fix features
3526 * @param netdev pointer to network device
3527 * @param request features requested
3528 * @returns updated features list
3529 */
3530static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3531 netdev_features_t request)
3532{
3533 struct lio *lio = netdev_priv(netdev);
3534
3535 if ((request & NETIF_F_RXCSUM) &&
3536 !(lio->dev_capability & NETIF_F_RXCSUM))
3537 request &= ~NETIF_F_RXCSUM;
3538
3539 if ((request & NETIF_F_HW_CSUM) &&
3540 !(lio->dev_capability & NETIF_F_HW_CSUM))
3541 request &= ~NETIF_F_HW_CSUM;
3542
3543 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3544 request &= ~NETIF_F_TSO;
3545
3546 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3547 request &= ~NETIF_F_TSO6;
3548
3549 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3550 request &= ~NETIF_F_LRO;
3551
3552 /*Disable LRO if RXCSUM is off */
3553 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3554 (lio->dev_capability & NETIF_F_LRO))
3555 request &= ~NETIF_F_LRO;
3556
3557 return request;
3558}
3559
3560/** \brief Net device set features
3561 * @param netdev pointer to network device
3562 * @param features features to enable/disable
3563 */
3564static int liquidio_set_features(struct net_device *netdev,
3565 netdev_features_t features)
3566{
3567 struct lio *lio = netdev_priv(netdev);
3568
3569 if (!((netdev->features ^ features) & NETIF_F_LRO))
3570 return 0;
3571
3572 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003573 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3574 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003575 else if (!(features & NETIF_F_LRO) &&
3576 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003577 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3578 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003579
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003580 /* Sending command to firmware to enable/disable RX checksum
3581 * offload settings using ethtool
3582 */
3583 if (!(netdev->features & NETIF_F_RXCSUM) &&
3584 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3585 (features & NETIF_F_RXCSUM))
3586 liquidio_set_rxcsum_command(netdev,
3587 OCTNET_CMD_TNL_RX_CSUM_CTL,
3588 OCTNET_CMD_RXCSUM_ENABLE);
3589 else if ((netdev->features & NETIF_F_RXCSUM) &&
3590 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3591 !(features & NETIF_F_RXCSUM))
3592 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3593 OCTNET_CMD_RXCSUM_DISABLE);
3594
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003595 return 0;
3596}
3597
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003598static void liquidio_add_vxlan_port(struct net_device *netdev,
3599 struct udp_tunnel_info *ti)
3600{
3601 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3602 return;
3603
3604 liquidio_vxlan_port_command(netdev,
3605 OCTNET_CMD_VXLAN_PORT_CONFIG,
3606 htons(ti->port),
3607 OCTNET_CMD_VXLAN_PORT_ADD);
3608}
3609
3610static void liquidio_del_vxlan_port(struct net_device *netdev,
3611 struct udp_tunnel_info *ti)
3612{
3613 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3614 return;
3615
3616 liquidio_vxlan_port_command(netdev,
3617 OCTNET_CMD_VXLAN_PORT_CONFIG,
3618 htons(ti->port),
3619 OCTNET_CMD_VXLAN_PORT_DEL);
3620}
3621
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003622static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3623 u8 *mac, bool is_admin_assigned)
3624{
3625 struct lio *lio = GET_LIO(netdev);
3626 struct octeon_device *oct = lio->oct_dev;
3627 struct octnic_ctrl_pkt nctrl;
3628
3629 if (!is_valid_ether_addr(mac))
3630 return -EINVAL;
3631
3632 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3633 return -EINVAL;
3634
3635 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3636
3637 nctrl.ncmd.u64 = 0;
3638 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3639 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3640 nctrl.ncmd.s.param1 = vfidx + 1;
3641 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3642 nctrl.ncmd.s.more = 1;
3643 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003644 nctrl.netpndev = (u64)netdev;
3645 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003646 nctrl.wait_time = LIO_CMD_WAIT_TM;
3647
3648 nctrl.udd[0] = 0;
3649 /* The MAC Address is presented in network byte order. */
3650 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3651
3652 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3653
3654 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3655
3656 return 0;
3657}
3658
3659static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3660{
3661 struct lio *lio = GET_LIO(netdev);
3662 struct octeon_device *oct = lio->oct_dev;
3663 int retval;
3664
3665 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3666 if (!retval)
3667 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3668
3669 return retval;
3670}
3671
3672static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3673 u16 vlan, u8 qos, __be16 vlan_proto)
3674{
3675 struct lio *lio = GET_LIO(netdev);
3676 struct octeon_device *oct = lio->oct_dev;
3677 struct octnic_ctrl_pkt nctrl;
3678 u16 vlantci;
3679
3680 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3681 return -EINVAL;
3682
3683 if (vlan_proto != htons(ETH_P_8021Q))
3684 return -EPROTONOSUPPORT;
3685
3686 if (vlan >= VLAN_N_VID || qos > 7)
3687 return -EINVAL;
3688
3689 if (vlan)
3690 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3691 else
3692 vlantci = 0;
3693
3694 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3695 return 0;
3696
3697 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3698
3699 if (vlan)
3700 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3701 else
3702 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3703
3704 nctrl.ncmd.s.param1 = vlantci;
3705 nctrl.ncmd.s.param2 =
3706 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3707 nctrl.ncmd.s.more = 0;
3708 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3709 nctrl.cb_fn = 0;
3710 nctrl.wait_time = LIO_CMD_WAIT_TM;
3711
3712 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3713
3714 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3715
3716 return 0;
3717}
3718
3719static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3720 struct ifla_vf_info *ivi)
3721{
3722 struct lio *lio = GET_LIO(netdev);
3723 struct octeon_device *oct = lio->oct_dev;
3724 u8 *macaddr;
3725
3726 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3727 return -EINVAL;
3728
3729 ivi->vf = vfidx;
3730 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3731 ether_addr_copy(&ivi->mac[0], macaddr);
3732 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3733 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3734 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3735 return 0;
3736}
3737
3738static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3739 int linkstate)
3740{
3741 struct lio *lio = GET_LIO(netdev);
3742 struct octeon_device *oct = lio->oct_dev;
3743 struct octnic_ctrl_pkt nctrl;
3744
3745 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3746 return -EINVAL;
3747
3748 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3749 return 0;
3750
3751 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3752 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3753 nctrl.ncmd.s.param1 =
3754 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3755 nctrl.ncmd.s.param2 = linkstate;
3756 nctrl.ncmd.s.more = 0;
3757 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3758 nctrl.cb_fn = 0;
3759 nctrl.wait_time = LIO_CMD_WAIT_TM;
3760
3761 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3762
3763 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3764
3765 return 0;
3766}
3767
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003768static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003769 .ndo_open = liquidio_open,
3770 .ndo_stop = liquidio_stop,
3771 .ndo_start_xmit = liquidio_xmit,
3772 .ndo_get_stats = liquidio_get_stats,
3773 .ndo_set_mac_address = liquidio_set_mac,
3774 .ndo_set_rx_mode = liquidio_set_mcast_list,
3775 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003776
3777 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3778 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003779 .ndo_change_mtu = liquidio_change_mtu,
3780 .ndo_do_ioctl = liquidio_ioctl,
3781 .ndo_fix_features = liquidio_fix_features,
3782 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003783 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3784 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003785 .ndo_set_vf_mac = liquidio_set_vf_mac,
3786 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3787 .ndo_get_vf_config = liquidio_get_vf_config,
3788 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003789};
3790
3791/** \brief Entry point for the liquidio module
3792 */
3793static int __init liquidio_init(void)
3794{
3795 int i;
3796 struct handshake *hs;
3797
3798 init_completion(&first_stage);
3799
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003800 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003801
3802 if (liquidio_init_pci())
3803 return -EINVAL;
3804
3805 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3806
3807 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3808 hs = &handshake[i];
3809 if (hs->pci_dev) {
3810 wait_for_completion(&hs->init);
3811 if (!hs->init_ok) {
3812 /* init handshake failed */
3813 dev_err(&hs->pci_dev->dev,
3814 "Failed to init device\n");
3815 liquidio_deinit_pci();
3816 return -EIO;
3817 }
3818 }
3819 }
3820
3821 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3822 hs = &handshake[i];
3823 if (hs->pci_dev) {
3824 wait_for_completion_timeout(&hs->started,
3825 msecs_to_jiffies(30000));
3826 if (!hs->started_ok) {
3827 /* starter handshake failed */
3828 dev_err(&hs->pci_dev->dev,
3829 "Firmware failed to start\n");
3830 liquidio_deinit_pci();
3831 return -EIO;
3832 }
3833 }
3834 }
3835
3836 return 0;
3837}
3838
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003839static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003840{
3841 struct octeon_device *oct = (struct octeon_device *)buf;
3842 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003843 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003844 union oct_link_status *ls;
3845 int i;
3846
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003847 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003848 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3849 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003850 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003851 goto nic_info_err;
3852 }
3853
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003854 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003855 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3856
3857 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003858 for (i = 0; i < oct->ifcount; i++) {
3859 if (oct->props[i].gmxport == gmxport) {
3860 update_link_status(oct->props[i].netdev, ls);
3861 break;
3862 }
3863 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003864
3865nic_info_err:
3866 for (i = 0; i < recv_pkt->buffer_count; i++)
3867 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3868 octeon_free_recv_info(recv_info);
3869 return 0;
3870}
3871
3872/**
3873 * \brief Setup network interfaces
3874 * @param octeon_dev octeon device
3875 *
3876 * Called during init time for each device. It assumes the NIC
3877 * is already up and running. The link information for each
3878 * interface is passed in link_info.
3879 */
3880static int setup_nic_devices(struct octeon_device *octeon_dev)
3881{
3882 struct lio *lio = NULL;
3883 struct net_device *netdev;
3884 u8 mac[6], i, j;
3885 struct octeon_soft_command *sc;
3886 struct liquidio_if_cfg_context *ctx;
3887 struct liquidio_if_cfg_resp *resp;
3888 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003889 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003890 union oct_nic_if_cfg if_cfg;
3891 unsigned int base_queue;
3892 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003893 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003894 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003895 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003896
3897 /* This is to handle link status changes */
3898 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3899 OPCODE_NIC_INFO,
3900 lio_nic_info, octeon_dev);
3901
3902 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3903 * They are handled directly.
3904 */
3905 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3906 free_netbuf);
3907
3908 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3909 free_netsgbuf);
3910
3911 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3912 free_netsgbuf_with_resp);
3913
3914 for (i = 0; i < octeon_dev->ifcount; i++) {
3915 resp_size = sizeof(struct liquidio_if_cfg_resp);
3916 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003917 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003918 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003919 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003920 resp_size, ctx_size);
3921 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3922 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003923 vdata = (struct lio_version *)sc->virtdptr;
3924
3925 *((u64 *)vdata) = 0;
3926 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3927 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3928 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003929
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003930 if (OCTEON_CN23XX_PF(octeon_dev)) {
3931 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3932 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3933 base_queue = octeon_dev->sriov_info.pf_srn;
3934
3935 gmx_port_id = octeon_dev->pf_num;
3936 ifidx_or_pfnum = octeon_dev->pf_num;
3937 } else {
3938 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3939 octeon_get_conf(octeon_dev), i);
3940 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3941 octeon_get_conf(octeon_dev), i);
3942 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3943 octeon_get_conf(octeon_dev), i);
3944 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3945 octeon_get_conf(octeon_dev), i);
3946 ifidx_or_pfnum = i;
3947 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003948
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003949 dev_dbg(&octeon_dev->pci_dev->dev,
3950 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003951 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003952 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003953 ctx->octeon_id = lio_get_device_id(octeon_dev);
3954 init_waitqueue_head(&ctx->wc);
3955
3956 if_cfg.u64 = 0;
3957 if_cfg.s.num_iqueues = num_iqueues;
3958 if_cfg.s.num_oqueues = num_oqueues;
3959 if_cfg.s.base_queue = base_queue;
3960 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003961
3962 sc->iq_no = 0;
3963
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003964 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003965 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003966 if_cfg.u64, 0);
3967
3968 sc->callback = if_cfg_callback;
3969 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003970 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003971
3972 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003973 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003974 dev_err(&octeon_dev->pci_dev->dev,
3975 "iq/oq config failed status: %x\n",
3976 retval);
3977 /* Soft instr is freed by driver in case of failure. */
3978 goto setup_nic_dev_fail;
3979 }
3980
3981 /* Sleep on a wait queue till the cond flag indicates that the
3982 * response arrived or timed-out.
3983 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003984 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3985 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3986 goto setup_nic_wait_intr;
3987 }
3988
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003989 retval = resp->status;
3990 if (retval) {
3991 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3992 goto setup_nic_dev_fail;
3993 }
3994
3995 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3996 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3997
3998 num_iqueues = hweight64(resp->cfg_info.iqmask);
3999 num_oqueues = hweight64(resp->cfg_info.oqmask);
4000
4001 if (!(num_iqueues) || !(num_oqueues)) {
4002 dev_err(&octeon_dev->pci_dev->dev,
4003 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
4004 resp->cfg_info.iqmask,
4005 resp->cfg_info.oqmask);
4006 goto setup_nic_dev_fail;
4007 }
4008 dev_dbg(&octeon_dev->pci_dev->dev,
4009 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
4010 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
4011 num_iqueues, num_oqueues);
4012 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
4013
4014 if (!netdev) {
4015 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
4016 goto setup_nic_dev_fail;
4017 }
4018
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004019 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004020
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004021 /* Associate the routines that will handle different
4022 * netdev tasks.
4023 */
4024 netdev->netdev_ops = &lionetdevops;
4025
4026 lio = GET_LIO(netdev);
4027
4028 memset(lio, 0, sizeof(struct lio));
4029
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004030 lio->ifidx = ifidx_or_pfnum;
4031
4032 props = &octeon_dev->props[i];
4033 props->gmxport = resp->cfg_info.linfo.gmxport;
4034 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004035
4036 lio->linfo.num_rxpciq = num_oqueues;
4037 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004038 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004039 lio->linfo.rxpciq[j].u64 =
4040 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004041 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004042 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004043 lio->linfo.txpciq[j].u64 =
4044 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004045 }
4046 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4047 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4048 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4049
4050 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4051
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004052 if (OCTEON_CN23XX_PF(octeon_dev) ||
4053 OCTEON_CN6XXX(octeon_dev)) {
4054 lio->dev_capability = NETIF_F_HIGHDMA
4055 | NETIF_F_IP_CSUM
4056 | NETIF_F_IPV6_CSUM
4057 | NETIF_F_SG | NETIF_F_RXCSUM
4058 | NETIF_F_GRO
4059 | NETIF_F_TSO | NETIF_F_TSO6
4060 | NETIF_F_LRO;
4061 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004062 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4063
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004064 /* Copy of transmit encapsulation capabilities:
4065 * TSO, TSO6, Checksums for this device
4066 */
4067 lio->enc_dev_capability = NETIF_F_IP_CSUM
4068 | NETIF_F_IPV6_CSUM
4069 | NETIF_F_GSO_UDP_TUNNEL
4070 | NETIF_F_HW_CSUM | NETIF_F_SG
4071 | NETIF_F_RXCSUM
4072 | NETIF_F_TSO | NETIF_F_TSO6
4073 | NETIF_F_LRO;
4074
4075 netdev->hw_enc_features = (lio->enc_dev_capability &
4076 ~NETIF_F_LRO);
4077
4078 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4079
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004080 netdev->vlan_features = lio->dev_capability;
4081 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004082 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4083 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004084 NETIF_F_HW_VLAN_CTAG_TX;
4085
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004086 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4087
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004088 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004089 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4090 netdev->hw_features = netdev->hw_features &
4091 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004092
Jarod Wilson109cc162016-10-17 15:54:13 -04004093 /* MTU range: 68 - 16000 */
4094 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4095 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4096
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004097 /* Point to the properties for octeon device to which this
4098 * interface belongs.
4099 */
4100 lio->oct_dev = octeon_dev;
4101 lio->octprops = props;
4102 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004103
4104 dev_dbg(&octeon_dev->pci_dev->dev,
4105 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4106 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4107
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004108 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4109 u8 vfmac[ETH_ALEN];
4110
4111 random_ether_addr(&vfmac[0]);
4112 if (__liquidio_set_vf_mac(netdev, j,
4113 &vfmac[0], false)) {
4114 dev_err(&octeon_dev->pci_dev->dev,
4115 "Error setting VF%d MAC address\n",
4116 j);
4117 goto setup_nic_dev_fail;
4118 }
4119 }
4120
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004121 /* 64-bit swap required on LE machines */
4122 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4123 for (j = 0; j < 6; j++)
4124 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4125
4126 /* Copy MAC Address to OS network device structure */
4127
4128 ether_addr_copy(netdev->dev_addr, mac);
4129
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004130 /* By default all interfaces on a single Octeon uses the same
4131 * tx and rx queues
4132 */
4133 lio->txq = lio->linfo.txpciq[0].s.q_no;
4134 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004135 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004136 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4137 goto setup_nic_dev_fail;
4138 }
4139
4140 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4141
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004142 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4143 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4144
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004145 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004146 dev_err(&octeon_dev->pci_dev->dev,
4147 "Gather list allocation failed\n");
4148 goto setup_nic_dev_fail;
4149 }
4150
4151 /* Register ethtool support */
4152 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004153 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4154 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4155 else
4156 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004157
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004158 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004159 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4160 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004161
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004162 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4163
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004164 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004165 liquidio_set_feature(netdev,
4166 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004167
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004168 if (setup_link_status_change_wq(netdev))
4169 goto setup_nic_dev_fail;
4170
Satanand Burla031d4f12017-03-22 11:31:13 -07004171 if (setup_rx_oom_poll_fn(netdev))
4172 goto setup_nic_dev_fail;
4173
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004174 /* Register the network device with the OS */
4175 if (register_netdev(netdev)) {
4176 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4177 goto setup_nic_dev_fail;
4178 }
4179
4180 dev_dbg(&octeon_dev->pci_dev->dev,
4181 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4182 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4183 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004184 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004185
4186 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4187
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004188 /* Sending command to firmware to enable Rx checksum offload
4189 * by default at the time of setup of Liquidio driver for
4190 * this device
4191 */
4192 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4193 OCTNET_CMD_RXCSUM_ENABLE);
4194 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4195 OCTNET_CMD_TXCSUM_ENABLE);
4196
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004197 dev_dbg(&octeon_dev->pci_dev->dev,
4198 "NIC ifidx:%d Setup successful\n", i);
4199
4200 octeon_free_soft_command(octeon_dev, sc);
4201 }
4202
4203 return 0;
4204
4205setup_nic_dev_fail:
4206
4207 octeon_free_soft_command(octeon_dev, sc);
4208
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004209setup_nic_wait_intr:
4210
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004211 while (i--) {
4212 dev_err(&octeon_dev->pci_dev->dev,
4213 "NIC ifidx:%d Setup failed\n", i);
4214 liquidio_destroy_nic_device(octeon_dev, i);
4215 }
4216 return -ENODEV;
4217}
4218
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004219#ifdef CONFIG_PCI_IOV
4220static int octeon_enable_sriov(struct octeon_device *oct)
4221{
4222 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4223 struct pci_dev *vfdev;
4224 int err;
4225 u32 u;
4226
4227 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4228 err = pci_enable_sriov(oct->pci_dev,
4229 oct->sriov_info.num_vfs_alloced);
4230 if (err) {
4231 dev_err(&oct->pci_dev->dev,
4232 "OCTEON: Failed to enable PCI sriov: %d\n",
4233 err);
4234 oct->sriov_info.num_vfs_alloced = 0;
4235 return err;
4236 }
4237 oct->sriov_info.sriov_enabled = 1;
4238
4239 /* init lookup table that maps DPI ring number to VF pci_dev
4240 * struct pointer
4241 */
4242 u = 0;
4243 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4244 OCTEON_CN23XX_VF_VID, NULL);
4245 while (vfdev) {
4246 if (vfdev->is_virtfn &&
4247 (vfdev->physfn == oct->pci_dev)) {
4248 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4249 vfdev;
4250 u += oct->sriov_info.rings_per_vf;
4251 }
4252 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4253 OCTEON_CN23XX_VF_VID, vfdev);
4254 }
4255 }
4256
4257 return num_vfs_alloced;
4258}
4259
4260static int lio_pci_sriov_disable(struct octeon_device *oct)
4261{
4262 int u;
4263
4264 if (pci_vfs_assigned(oct->pci_dev)) {
4265 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4266 return -EPERM;
4267 }
4268
4269 pci_disable_sriov(oct->pci_dev);
4270
4271 u = 0;
4272 while (u < MAX_POSSIBLE_VFS) {
4273 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4274 u += oct->sriov_info.rings_per_vf;
4275 }
4276
4277 oct->sriov_info.num_vfs_alloced = 0;
4278 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4279 oct->pf_num);
4280
4281 return 0;
4282}
4283
4284static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4285{
4286 struct octeon_device *oct = pci_get_drvdata(dev);
4287 int ret = 0;
4288
4289 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4290 (oct->sriov_info.sriov_enabled)) {
4291 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4292 oct->pf_num, num_vfs);
4293 return 0;
4294 }
4295
4296 if (!num_vfs) {
4297 ret = lio_pci_sriov_disable(oct);
4298 } else if (num_vfs > oct->sriov_info.max_vfs) {
4299 dev_err(&oct->pci_dev->dev,
4300 "OCTEON: Max allowed VFs:%d user requested:%d",
4301 oct->sriov_info.max_vfs, num_vfs);
4302 ret = -EPERM;
4303 } else {
4304 oct->sriov_info.num_vfs_alloced = num_vfs;
4305 ret = octeon_enable_sriov(oct);
4306 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4307 oct->pf_num, num_vfs);
4308 }
4309
4310 return ret;
4311}
4312#endif
4313
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004314/**
4315 * \brief initialize the NIC
4316 * @param oct octeon device
4317 *
4318 * This initialization routine is called once the Octeon device application is
4319 * up and running
4320 */
4321static int liquidio_init_nic_module(struct octeon_device *oct)
4322{
4323 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004324 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004325 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4326
4327 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4328
4329 /* only default iq and oq were initialized
4330 * initialize the rest as well
4331 */
4332 /* run port_config command for each port */
4333 oct->ifcount = num_nic_ports;
4334
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004335 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004336
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004337 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4338 oct->props[i].gmxport = -1;
4339
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004340 retval = setup_nic_devices(oct);
4341 if (retval) {
4342 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4343 goto octnet_init_failure;
4344 }
4345
4346 liquidio_ptp_init(oct);
4347
4348 /* Initialize interrupt moderation params */
4349 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004350 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004351 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004352 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4353 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4354 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4355 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4356 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4357 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4358 intrmod_cfg->tx_enable = 1;
4359 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4360 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4361 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4362 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004363 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004364 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4365
4366 return retval;
4367
4368octnet_init_failure:
4369
4370 oct->ifcount = 0;
4371
4372 return retval;
4373}
4374
4375/**
4376 * \brief starter callback that invokes the remaining initialization work after
4377 * the NIC is up and running.
4378 * @param octptr work struct work_struct
4379 */
4380static void nic_starter(struct work_struct *work)
4381{
4382 struct octeon_device *oct;
4383 struct cavium_wk *wk = (struct cavium_wk *)work;
4384
4385 oct = (struct octeon_device *)wk->ctxptr;
4386
4387 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4388 return;
4389
4390 /* If the status of the device is CORE_OK, the core
4391 * application has reported its application type. Call
4392 * any registered handlers now and move to the RUNNING
4393 * state.
4394 */
4395 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4396 schedule_delayed_work(&oct->nic_poll_work.work,
4397 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4398 return;
4399 }
4400
4401 atomic_set(&oct->status, OCT_DEV_RUNNING);
4402
4403 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4404 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4405
4406 if (liquidio_init_nic_module(oct))
4407 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4408 else
4409 handshake[oct->octeon_id].started_ok = 1;
4410 } else {
4411 dev_err(&oct->pci_dev->dev,
4412 "Unexpected application running on NIC (%d). Check firmware.\n",
4413 oct->app_mode);
4414 }
4415
4416 complete(&handshake[oct->octeon_id].started);
4417}
4418
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004419static int
4420octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4421{
4422 struct octeon_device *oct = (struct octeon_device *)buf;
4423 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4424 int i, notice, vf_idx;
4425 u64 *data, vf_num;
4426
4427 notice = recv_pkt->rh.r.ossp;
4428 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4429
4430 /* the first 64-bit word of data is the vf_num */
4431 vf_num = data[0];
4432 octeon_swap_8B_data(&vf_num, 1);
4433 vf_idx = (int)vf_num - 1;
4434
4435 if (notice == VF_DRV_LOADED) {
4436 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4437 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4438 dev_info(&oct->pci_dev->dev,
4439 "driver for VF%d was loaded\n", vf_idx);
4440 try_module_get(THIS_MODULE);
4441 }
4442 } else if (notice == VF_DRV_REMOVED) {
4443 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4444 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4445 dev_info(&oct->pci_dev->dev,
4446 "driver for VF%d was removed\n", vf_idx);
4447 module_put(THIS_MODULE);
4448 }
4449 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4450 u8 *b = (u8 *)&data[1];
4451
4452 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4453 dev_info(&oct->pci_dev->dev,
4454 "VF driver changed VF%d's MAC address to %pM\n",
4455 vf_idx, b + 2);
4456 }
4457
4458 for (i = 0; i < recv_pkt->buffer_count; i++)
4459 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4460 octeon_free_recv_info(recv_info);
4461
4462 return 0;
4463}
4464
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004465/**
4466 * \brief Device initialization for each Octeon device that is probed
4467 * @param octeon_dev octeon device
4468 */
4469static int octeon_device_init(struct octeon_device *octeon_dev)
4470{
4471 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004472 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004473 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004474 struct octeon_device_priv *oct_priv =
4475 (struct octeon_device_priv *)octeon_dev->priv;
4476 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4477
4478 /* Enable access to the octeon device and make its DMA capability
4479 * known to the OS.
4480 */
4481 if (octeon_pci_os_setup(octeon_dev))
4482 return 1;
4483
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004484 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4485
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004486 /* Identify the Octeon type and map the BAR address space. */
4487 if (octeon_chip_specific_setup(octeon_dev)) {
4488 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4489 return 1;
4490 }
4491
4492 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4493
4494 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4495
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004496 if (OCTEON_CN23XX_PF(octeon_dev)) {
4497 if (!cn23xx_fw_loaded(octeon_dev)) {
4498 fw_loaded = 0;
Felix Manlunas7cc61db2017-03-23 13:26:28 -07004499 if (!fw_type_is_none()) {
4500 /* Do a soft reset of the Octeon device. */
4501 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4502 return 1;
4503 /* things might have changed */
4504 if (!cn23xx_fw_loaded(octeon_dev))
4505 fw_loaded = 0;
4506 else
4507 fw_loaded = 1;
4508 }
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004509 } else {
4510 fw_loaded = 1;
4511 }
4512 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004513 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004514 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004515
4516 /* Initialize the dispatch mechanism used to push packets arriving on
4517 * Octeon Output queues.
4518 */
4519 if (octeon_init_dispatch_list(octeon_dev))
4520 return 1;
4521
4522 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4523 OPCODE_NIC_CORE_DRV_ACTIVE,
4524 octeon_core_drv_init,
4525 octeon_dev);
4526
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004527 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4528 OPCODE_NIC_VF_DRV_NOTICE,
4529 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004530 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4531 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4532 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4533 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4534
4535 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4536
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004537 if (octeon_set_io_queues_off(octeon_dev)) {
4538 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4539 return 1;
4540 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004541
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004542 if (OCTEON_CN23XX_PF(octeon_dev)) {
4543 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4544 if (ret) {
4545 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4546 return ret;
4547 }
4548 }
4549
4550 /* Initialize soft command buffer pool
4551 */
4552 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4553 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4554 return 1;
4555 }
4556 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4557
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004558 /* Setup the data structures that manage this Octeon's Input queues. */
4559 if (octeon_setup_instr_queues(octeon_dev)) {
4560 dev_err(&octeon_dev->pci_dev->dev,
4561 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004562 return 1;
4563 }
4564 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4565
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004566 /* Initialize lists to manage the requests of different types that
4567 * arrive from user & kernel applications for this octeon device.
4568 */
4569 if (octeon_setup_response_list(octeon_dev)) {
4570 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4571 return 1;
4572 }
4573 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4574
4575 if (octeon_setup_output_queues(octeon_dev)) {
4576 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004577 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004578 }
4579
4580 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4581
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004582 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004583 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4584 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4585 return 1;
4586 }
4587 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4588
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004589 if (octeon_allocate_ioq_vector(octeon_dev)) {
4590 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4591 return 1;
4592 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004593 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004594
4595 } else {
4596 /* The input and output queue registers were setup earlier (the
4597 * queues were not enabled). Any additional registers
4598 * that need to be programmed should be done now.
4599 */
4600 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4601 if (ret) {
4602 dev_err(&octeon_dev->pci_dev->dev,
4603 "Failed to configure device registers\n");
4604 return ret;
4605 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004606 }
4607
4608 /* Initialize the tasklet that handles output queue packet processing.*/
4609 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4610 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4611 (unsigned long)octeon_dev);
4612
4613 /* Setup the interrupt handler and record the INT SUM register address
4614 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004615 if (octeon_setup_interrupt(octeon_dev))
4616 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004617
4618 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004619 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004620
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004621 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4622
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004623 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004624 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4625 if (ret) {
4626 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4627 return ret;
4628 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004629
4630 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4631
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004632 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4633 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4634 if (!ddr_timeout) {
4635 dev_info(&octeon_dev->pci_dev->dev,
4636 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4637 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004638
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004639 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004640
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004641 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4642 while (!ddr_timeout) {
4643 set_current_state(TASK_INTERRUPTIBLE);
4644 if (schedule_timeout(HZ / 10)) {
4645 /* user probably pressed Control-C */
4646 return 1;
4647 }
4648 }
4649 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4650 if (ret) {
4651 dev_err(&octeon_dev->pci_dev->dev,
4652 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4653 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004654 return 1;
4655 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004656
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004657 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4658 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4659 return 1;
4660 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004661
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004662 /* Divert uboot to take commands from host instead. */
4663 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004664
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004665 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4666 ret = octeon_init_consoles(octeon_dev);
4667 if (ret) {
4668 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4669 return 1;
4670 }
4671 ret = octeon_add_console(octeon_dev, 0);
4672 if (ret) {
4673 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4674 return 1;
4675 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004676
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004677 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004678
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004679 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4680 ret = load_firmware(octeon_dev);
4681 if (ret) {
4682 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4683 return 1;
4684 }
4685 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4686 * loaded
4687 */
4688 if (OCTEON_CN23XX_PF(octeon_dev))
4689 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4690 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004691 }
4692
4693 handshake[octeon_dev->octeon_id].init_ok = 1;
4694 complete(&handshake[octeon_dev->octeon_id].init);
4695
4696 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4697
4698 /* Send Credit for Octeon Output queues. Credits are always sent after
4699 * the output queue is enabled.
4700 */
4701 for (j = 0; j < octeon_dev->num_oqs; j++)
4702 writel(octeon_dev->droq[j]->max_count,
4703 octeon_dev->droq[j]->pkts_credit_reg);
4704
4705 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004706 return 0;
4707}
4708
4709/**
4710 * \brief Exits the module
4711 */
4712static void __exit liquidio_exit(void)
4713{
4714 liquidio_deinit_pci();
4715
4716 pr_info("LiquidIO network module is now unloaded\n");
4717}
4718
4719module_init(liquidio_init);
4720module_exit(liquidio_exit);