blob: afa173de1042bf1f0213bb0d8fd6078f7c6b8062 [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
715 if (lio->glists_virt_base && lio->glists_virt_base[i]) {
716 lio_dma_free(lio->oct_dev,
717 lio->glist_entry_size * lio->tx_qsize,
718 lio->glists_virt_base[i],
719 lio->glists_dma_base[i]);
720 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700721 }
722
VSR Burru67e303e2017-03-06 18:45:59 -0800723 kfree(lio->glists_virt_base);
724 lio->glists_virt_base = NULL;
725
726 kfree(lio->glists_dma_base);
727 lio->glists_dma_base = NULL;
728
729 kfree(lio->glist);
730 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700731}
732
733/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700734 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700735 * @param lio per-network private data
736 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700737static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700738{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700739 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700740 struct octnic_gather *g;
741
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700742 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
743 GFP_KERNEL);
744 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800745 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700746
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700747 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
748 GFP_KERNEL);
749 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800750 kfree(lio->glist_lock);
751 lio->glist_lock = NULL;
752 return -ENOMEM;
753 }
754
755 lio->glist_entry_size =
756 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
757
758 /* allocate memory to store virtual and dma base address of
759 * per glist consistent memory
760 */
761 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
762 GFP_KERNEL);
763 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
764 GFP_KERNEL);
765
766 if (!lio->glists_virt_base || !lio->glists_dma_base) {
767 delete_glists(lio);
768 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700769 }
770
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700771 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800772 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700773
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700774 spin_lock_init(&lio->glist_lock[i]);
775
776 INIT_LIST_HEAD(&lio->glist[i]);
777
VSR Burru67e303e2017-03-06 18:45:59 -0800778 lio->glists_virt_base[i] =
779 lio_dma_alloc(oct,
780 lio->glist_entry_size * lio->tx_qsize,
781 &lio->glists_dma_base[i]);
782
783 if (!lio->glists_virt_base[i]) {
784 delete_glists(lio);
785 return -ENOMEM;
786 }
787
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700788 for (j = 0; j < lio->tx_qsize; j++) {
789 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
790 numa_node);
791 if (!g)
792 g = kzalloc(sizeof(*g), GFP_KERNEL);
793 if (!g)
794 break;
795
VSR Burru67e303e2017-03-06 18:45:59 -0800796 g->sg = lio->glists_virt_base[i] +
797 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700798
VSR Burru67e303e2017-03-06 18:45:59 -0800799 g->sg_dma_ptr = lio->glists_dma_base[i] +
800 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700801
802 list_add_tail(&g->list, &lio->glist[i]);
803 }
804
805 if (j != lio->tx_qsize) {
806 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800807 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700808 }
809 }
810
811 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700812}
813
814/**
815 * \brief Print link information
816 * @param netdev network device
817 */
818static void print_link_info(struct net_device *netdev)
819{
820 struct lio *lio = GET_LIO(netdev);
821
822 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
823 struct oct_link_info *linfo = &lio->linfo;
824
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700825 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700826 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
827 linfo->link.s.speed,
828 (linfo->link.s.duplex) ? "Full" : "Half");
829 } else {
830 netif_info(lio, link, lio->netdev, "Link Down\n");
831 }
832 }
833}
834
835/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700836 * \brief Routine to notify MTU change
837 * @param work work_struct data structure
838 */
839static void octnet_link_status_change(struct work_struct *work)
840{
841 struct cavium_wk *wk = (struct cavium_wk *)work;
842 struct lio *lio = (struct lio *)wk->ctxptr;
843
844 rtnl_lock();
845 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
846 rtnl_unlock();
847}
848
849/**
850 * \brief Sets up the mtu status change work
851 * @param netdev network device
852 */
853static inline int setup_link_status_change_wq(struct net_device *netdev)
854{
855 struct lio *lio = GET_LIO(netdev);
856 struct octeon_device *oct = lio->oct_dev;
857
858 lio->link_status_wq.wq = alloc_workqueue("link-status",
859 WQ_MEM_RECLAIM, 0);
860 if (!lio->link_status_wq.wq) {
861 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
862 return -1;
863 }
864 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
865 octnet_link_status_change);
866 lio->link_status_wq.wk.ctxptr = lio;
867
868 return 0;
869}
870
871static inline void cleanup_link_status_change_wq(struct net_device *netdev)
872{
873 struct lio *lio = GET_LIO(netdev);
874
875 if (lio->link_status_wq.wq) {
876 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
877 destroy_workqueue(lio->link_status_wq.wq);
878 }
879}
880
881/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700882 * \brief Update link status
883 * @param netdev network device
884 * @param ls link status structure
885 *
886 * Called on receipt of a link status response from the core application to
887 * update each interface's link status.
888 */
889static inline void update_link_status(struct net_device *netdev,
890 union oct_link_status *ls)
891{
892 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700893 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700894
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700895 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700896
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700897 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700898 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700899 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700900
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700901 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700902 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700903 txqs_wake(netdev);
904 } else {
905 netif_carrier_off(netdev);
906 stop_txq(netdev);
907 }
908 }
909}
910
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700911/* Runs in interrupt context. */
912static void update_txq_status(struct octeon_device *oct, int iq_num)
913{
914 struct net_device *netdev;
915 struct lio *lio;
916 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
917
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700918 netdev = oct->props[iq->ifidx].netdev;
919
920 /* This is needed because the first IQ does not have
921 * a netdev associated with it.
922 */
923 if (!netdev)
924 return;
925
926 lio = GET_LIO(netdev);
927 if (netif_is_multiqueue(netdev)) {
928 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
929 lio->linfo.link.s.link_up &&
930 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700931 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
932 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700933 netif_wake_subqueue(netdev, iq->q_index);
934 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700935 if (!octnet_iq_is_full(oct, lio->txq)) {
936 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
937 lio->txq,
938 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700939 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700940 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700941 }
942 }
943}
944
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700945static
946int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
947{
948 struct octeon_device *oct = droq->oct_dev;
949 struct octeon_device_priv *oct_priv =
950 (struct octeon_device_priv *)oct->priv;
951
952 if (droq->ops.poll_mode) {
953 droq->ops.napi_fn(droq);
954 } else {
955 if (ret & MSIX_PO_INT) {
956 tasklet_schedule(&oct_priv->droq_tasklet);
957 return 1;
958 }
959 /* this will be flushed periodically by check iq db */
960 if (ret & MSIX_PI_INT)
961 return 0;
962 }
963 return 0;
964}
965
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700966/**
967 * \brief Droq packet processor sceduler
968 * @param oct octeon device
969 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700970static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700971{
972 struct octeon_device_priv *oct_priv =
973 (struct octeon_device_priv *)oct->priv;
974 u64 oq_no;
975 struct octeon_droq *droq;
976
977 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700978 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
979 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800980 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700981 continue;
982
983 droq = oct->droq[oq_no];
984
985 if (droq->ops.poll_mode) {
986 droq->ops.napi_fn(droq);
987 oct_priv->napi_mask |= (1 << oq_no);
988 } else {
989 tasklet_schedule(&oct_priv->droq_tasklet);
990 }
991 }
992 }
993}
994
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700995static irqreturn_t
996liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
997{
998 u64 ret;
999 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1000 struct octeon_device *oct = ioq_vector->oct_dev;
1001 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1002
1003 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1004
1005 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1006 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1007
1008 return IRQ_HANDLED;
1009}
1010
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001011/**
1012 * \brief Interrupt handler for octeon
1013 * @param irq unused
1014 * @param dev octeon device
1015 */
1016static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001017irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1018 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001019{
1020 struct octeon_device *oct = (struct octeon_device *)dev;
1021 irqreturn_t ret;
1022
1023 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001024 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001025
1026 ret = oct->fn_list.process_interrupt_regs(oct);
1027
1028 if (ret == IRQ_HANDLED)
1029 liquidio_schedule_droq_pkt_handlers(oct);
1030
1031 /* Re-enable our interrupts */
1032 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001033 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001034
1035 return ret;
1036}
1037
1038/**
1039 * \brief Setup interrupt for octeon device
1040 * @param oct octeon device
1041 *
1042 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1043 */
1044static int octeon_setup_interrupt(struct octeon_device *oct)
1045{
1046 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001047 struct msix_entry *msix_entries;
1048 int i;
1049 int num_ioq_vectors;
1050 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001051 char *queue_irq_names = NULL;
1052 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001053
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001054 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1055 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1056 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1057 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001058
Rick Farrington0c88a762017-03-13 12:58:04 -07001059 /* allocate storage for the names assigned to each irq */
1060 oct->irq_name_storage =
1061 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1062 GFP_KERNEL);
1063 if (!oct->irq_name_storage) {
1064 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1065 return -ENOMEM;
1066 }
1067
1068 queue_irq_names = oct->irq_name_storage;
1069 aux_irq_name = &queue_irq_names
1070 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1071
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001072 oct->msix_entries = kcalloc(
1073 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001074 if (!oct->msix_entries) {
1075 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1076 kfree(oct->irq_name_storage);
1077 oct->irq_name_storage = NULL;
1078 return -ENOMEM;
1079 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001080
1081 msix_entries = (struct msix_entry *)oct->msix_entries;
1082 /*Assumption is that pf msix vectors start from pf srn to pf to
1083 * trs and not from 0. if not change this code
1084 */
1085 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1086 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1087 msix_entries[oct->num_msix_irqs - 1].entry =
1088 oct->sriov_info.trs;
1089 num_alloc_ioq_vectors = pci_enable_msix_range(
1090 oct->pci_dev, msix_entries,
1091 oct->num_msix_irqs,
1092 oct->num_msix_irqs);
1093 if (num_alloc_ioq_vectors < 0) {
1094 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1095 kfree(oct->msix_entries);
1096 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001097 kfree(oct->irq_name_storage);
1098 oct->irq_name_storage = NULL;
1099 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001100 }
1101 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1102
1103 num_ioq_vectors = oct->num_msix_irqs;
1104
1105 /** For PF, there is one non-ioq interrupt handler */
1106 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001107
1108 snprintf(aux_irq_name, INTRNAMSIZ,
1109 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001110 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001111 liquidio_legacy_intr_handler, 0,
1112 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001113 if (irqret) {
1114 dev_err(&oct->pci_dev->dev,
1115 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1116 irqret);
1117 pci_disable_msix(oct->pci_dev);
1118 kfree(oct->msix_entries);
1119 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001120 kfree(oct->irq_name_storage);
1121 oct->irq_name_storage = NULL;
1122 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001123 }
1124
1125 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001126 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1127 "LiquidIO%u-pf%u-rxtx-%u",
1128 oct->octeon_id, oct->pf_num, i);
1129
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001130 irqret = request_irq(msix_entries[i].vector,
1131 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001132 &queue_irq_names[IRQ_NAME_OFF(i)],
1133 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001134 if (irqret) {
1135 dev_err(&oct->pci_dev->dev,
1136 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1137 irqret);
1138 /** Freeing the non-ioq irq vector here . */
1139 free_irq(msix_entries[num_ioq_vectors].vector,
1140 oct);
1141
1142 while (i) {
1143 i--;
1144 /** clearing affinity mask. */
1145 irq_set_affinity_hint(
1146 msix_entries[i].vector, NULL);
1147 free_irq(msix_entries[i].vector,
1148 &oct->ioq_vector[i]);
1149 }
1150 pci_disable_msix(oct->pci_dev);
1151 kfree(oct->msix_entries);
1152 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001153 kfree(oct->irq_name_storage);
1154 oct->irq_name_storage = NULL;
1155 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001156 }
1157 oct->ioq_vector[i].vector = msix_entries[i].vector;
1158 /* assign the cpu mask for this msix interrupt vector */
1159 irq_set_affinity_hint(
1160 msix_entries[i].vector,
1161 (&oct->ioq_vector[i].affinity_mask));
1162 }
1163 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1164 oct->octeon_id);
1165 } else {
1166 err = pci_enable_msi(oct->pci_dev);
1167 if (err)
1168 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1169 err);
1170 else
1171 oct->flags |= LIO_FLAG_MSI_ENABLED;
1172
Rick Farrington0c88a762017-03-13 12:58:04 -07001173 /* allocate storage for the names assigned to the irq */
1174 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1175 if (!oct->irq_name_storage)
1176 return -ENOMEM;
1177
1178 queue_irq_names = oct->irq_name_storage;
1179
1180 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1181 "LiquidIO%u-pf%u-rxtx-%u",
1182 oct->octeon_id, oct->pf_num, 0);
1183
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001184 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001185 liquidio_legacy_intr_handler,
1186 IRQF_SHARED,
1187 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001188 if (irqret) {
1189 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1190 pci_disable_msi(oct->pci_dev);
1191 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1192 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001193 kfree(oct->irq_name_storage);
1194 oct->irq_name_storage = NULL;
1195 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001196 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001197 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001198 return 0;
1199}
1200
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001201static int liquidio_watchdog(void *param)
1202{
1203 u64 wdog;
1204 u16 mask_of_stuck_cores = 0;
1205 u16 mask_of_crashed_cores = 0;
1206 int core_num;
1207 u8 core_is_stuck[LIO_MAX_CORES];
1208 u8 core_crashed[LIO_MAX_CORES];
1209 struct octeon_device *oct = param;
1210
1211 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1212 memset(core_crashed, 0, sizeof(core_crashed));
1213
1214 while (!kthread_should_stop()) {
1215 mask_of_crashed_cores =
1216 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1217
1218 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1219 if (!core_is_stuck[core_num]) {
1220 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1221
1222 /* look at watchdog state field */
1223 wdog &= CIU3_WDOG_MASK;
1224 if (wdog) {
1225 /* this watchdog timer has expired */
1226 core_is_stuck[core_num] =
1227 LIO_MONITOR_WDOG_EXPIRE;
1228 mask_of_stuck_cores |= (1 << core_num);
1229 }
1230 }
1231
1232 if (!core_crashed[core_num])
1233 core_crashed[core_num] =
1234 (mask_of_crashed_cores >> core_num) & 1;
1235 }
1236
1237 if (mask_of_stuck_cores) {
1238 for (core_num = 0; core_num < LIO_MAX_CORES;
1239 core_num++) {
1240 if (core_is_stuck[core_num] == 1) {
1241 dev_err(&oct->pci_dev->dev,
1242 "ERROR: Octeon core %d is stuck!\n",
1243 core_num);
1244 /* 2 means we have printk'd an error
1245 * so no need to repeat the same printk
1246 */
1247 core_is_stuck[core_num] =
1248 LIO_MONITOR_CORE_STUCK_MSGD;
1249 }
1250 }
1251 }
1252
1253 if (mask_of_crashed_cores) {
1254 for (core_num = 0; core_num < LIO_MAX_CORES;
1255 core_num++) {
1256 if (core_crashed[core_num] == 1) {
1257 dev_err(&oct->pci_dev->dev,
1258 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1259 core_num);
1260 /* 2 means we have printk'd an error
1261 * so no need to repeat the same printk
1262 */
1263 core_crashed[core_num] =
1264 LIO_MONITOR_CORE_STUCK_MSGD;
1265 }
1266 }
1267 }
1268#ifdef CONFIG_MODULE_UNLOAD
1269 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1270 /* make module refcount=0 so that rmmod will work */
1271 long refcount;
1272
1273 refcount = module_refcount(THIS_MODULE);
1274
1275 while (refcount > 0) {
1276 module_put(THIS_MODULE);
1277 refcount = module_refcount(THIS_MODULE);
1278 }
1279
1280 /* compensate for and withstand an unlikely (but still
1281 * possible) race condition
1282 */
1283 while (refcount < 0) {
1284 try_module_get(THIS_MODULE);
1285 refcount = module_refcount(THIS_MODULE);
1286 }
1287 }
1288#endif
1289 /* sleep for two seconds */
1290 set_current_state(TASK_INTERRUPTIBLE);
1291 schedule_timeout(2 * HZ);
1292 }
1293
1294 return 0;
1295}
1296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001297/**
1298 * \brief PCI probe handler
1299 * @param pdev PCI device structure
1300 * @param ent unused
1301 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001302static int
1303liquidio_probe(struct pci_dev *pdev,
1304 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001305{
1306 struct octeon_device *oct_dev = NULL;
1307 struct handshake *hs;
1308
1309 oct_dev = octeon_allocate_device(pdev->device,
1310 sizeof(struct octeon_device_priv));
1311 if (!oct_dev) {
1312 dev_err(&pdev->dev, "Unable to allocate device\n");
1313 return -ENOMEM;
1314 }
1315
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001316 if (pdev->device == OCTEON_CN23XX_PF_VID)
1317 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001319 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1320 (u32)pdev->vendor, (u32)pdev->device);
1321
1322 /* Assign octeon_device for this device to the private data area. */
1323 pci_set_drvdata(pdev, oct_dev);
1324
1325 /* set linux specific device pointer */
1326 oct_dev->pci_dev = (void *)pdev;
1327
1328 hs = &handshake[oct_dev->octeon_id];
1329 init_completion(&hs->init);
1330 init_completion(&hs->started);
1331 hs->pci_dev = pdev;
1332
1333 if (oct_dev->octeon_id == 0)
1334 /* first LiquidIO NIC is detected */
1335 complete(&first_stage);
1336
1337 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001338 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001339 liquidio_remove(pdev);
1340 return -ENOMEM;
1341 }
1342
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001343 if (OCTEON_CN23XX_PF(oct_dev)) {
1344 u64 scratch1;
1345 u8 bus, device, function;
1346
1347 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1348 if (!(scratch1 & 4ULL)) {
1349 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1350 * the lio watchdog kernel thread is running for this
1351 * NIC. Each NIC gets one watchdog kernel thread.
1352 */
1353 scratch1 |= 4ULL;
1354 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1355 scratch1);
1356
1357 bus = pdev->bus->number;
1358 device = PCI_SLOT(pdev->devfn);
1359 function = PCI_FUNC(pdev->devfn);
1360 oct_dev->watchdog_task = kthread_create(
1361 liquidio_watchdog, oct_dev,
1362 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001363 if (!IS_ERR(oct_dev->watchdog_task)) {
1364 wake_up_process(oct_dev->watchdog_task);
1365 } else {
1366 oct_dev->watchdog_task = NULL;
1367 dev_err(&oct_dev->pci_dev->dev,
1368 "failed to create kernel_thread\n");
1369 liquidio_remove(pdev);
1370 return -1;
1371 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001372 }
1373 }
1374
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001375 oct_dev->rx_pause = 1;
1376 oct_dev->tx_pause = 1;
1377
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001378 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1379
1380 return 0;
1381}
1382
1383/**
1384 *\brief Destroy resources associated with octeon device
1385 * @param pdev PCI device structure
1386 * @param ent unused
1387 */
1388static void octeon_destroy_resources(struct octeon_device *oct)
1389{
1390 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001391 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001392 struct octeon_device_priv *oct_priv =
1393 (struct octeon_device_priv *)oct->priv;
1394
1395 struct handshake *hs;
1396
1397 switch (atomic_read(&oct->status)) {
1398 case OCT_DEV_RUNNING:
1399 case OCT_DEV_CORE_OK:
1400
1401 /* No more instructions will be forwarded. */
1402 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1403
1404 oct->app_mode = CVM_DRV_INVALID_APP;
1405 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1406 lio_get_state_string(&oct->status));
1407
1408 schedule_timeout_uninterruptible(HZ / 10);
1409
1410 /* fallthrough */
1411 case OCT_DEV_HOST_OK:
1412
1413 /* fallthrough */
1414 case OCT_DEV_CONSOLE_INIT_DONE:
1415 /* Remove any consoles */
1416 octeon_remove_consoles(oct);
1417
1418 /* fallthrough */
1419 case OCT_DEV_IO_QUEUES_DONE:
1420 if (wait_for_pending_requests(oct))
1421 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1422
1423 if (lio_wait_for_instr_fetch(oct))
1424 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1425
1426 /* Disable the input and output queues now. No more packets will
1427 * arrive from Octeon, but we should wait for all packet
1428 * processing to finish.
1429 */
1430 oct->fn_list.disable_io_queues(oct);
1431
1432 if (lio_wait_for_oq_pkts(oct))
1433 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1434
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001435 /* fallthrough */
1436 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001437 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001438 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001439
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001440 if (oct->msix_on) {
1441 msix_entries = (struct msix_entry *)oct->msix_entries;
1442 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1443 /* clear the affinity_cpumask */
1444 irq_set_affinity_hint(msix_entries[i].vector,
1445 NULL);
1446 free_irq(msix_entries[i].vector,
1447 &oct->ioq_vector[i]);
1448 }
1449 /* non-iov vector's argument is oct struct */
1450 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001451
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001452 pci_disable_msix(oct->pci_dev);
1453 kfree(oct->msix_entries);
1454 oct->msix_entries = NULL;
1455 } else {
1456 /* Release the interrupt line */
1457 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001458
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001459 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1460 pci_disable_msi(oct->pci_dev);
1461 }
1462
Rick Farrington0c88a762017-03-13 12:58:04 -07001463 kfree(oct->irq_name_storage);
1464 oct->irq_name_storage = NULL;
1465
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001466 /* fallthrough */
1467 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001468 if (OCTEON_CN23XX_PF(oct))
1469 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001470
1471 /* fallthrough */
1472 case OCT_DEV_MBOX_SETUP_DONE:
1473 if (OCTEON_CN23XX_PF(oct))
1474 oct->fn_list.free_mbox(oct);
1475
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001476 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001477 case OCT_DEV_IN_RESET:
1478 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001479 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001480 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001481 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001482 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001483 continue;
1484 octeon_delete_droq(oct, i);
1485 }
1486
1487 /* Force any pending handshakes to complete */
1488 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1489 hs = &handshake[i];
1490
1491 if (hs->pci_dev) {
1492 handshake[oct->octeon_id].init_ok = 0;
1493 complete(&handshake[oct->octeon_id].init);
1494 handshake[oct->octeon_id].started_ok = 0;
1495 complete(&handshake[oct->octeon_id].started);
1496 }
1497 }
1498
1499 /* fallthrough */
1500 case OCT_DEV_RESP_LIST_INIT_DONE:
1501 octeon_delete_response_list(oct);
1502
1503 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001504 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001505 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001506 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001507 continue;
1508 octeon_delete_instr_queue(oct, i);
1509 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001510#ifdef CONFIG_PCI_IOV
1511 if (oct->sriov_info.sriov_enabled)
1512 pci_disable_sriov(oct->pci_dev);
1513#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001514 /* fallthrough */
1515 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1516 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001517
1518 /* fallthrough */
1519 case OCT_DEV_DISPATCH_INIT_DONE:
1520 octeon_delete_dispatch_list(oct);
1521 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1522
1523 /* fallthrough */
1524 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001525 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001526 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1527 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001528
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001529 octeon_unmap_pci_barx(oct, 0);
1530 octeon_unmap_pci_barx(oct, 1);
1531
1532 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001533 case OCT_DEV_PCI_ENABLE_DONE:
1534 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001535 /* Disable the device, releasing the PCI INT */
1536 pci_disable_device(oct->pci_dev);
1537
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001538 /* fallthrough */
1539 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001540 /* Nothing to be done here either */
1541 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001542 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001543
1544 tasklet_kill(&oct_priv->droq_tasklet);
1545}
1546
1547/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001548 * \brief Callback for rx ctrl
1549 * @param status status of request
1550 * @param buf pointer to resp structure
1551 */
1552static void rx_ctl_callback(struct octeon_device *oct,
1553 u32 status,
1554 void *buf)
1555{
1556 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1557 struct liquidio_rx_ctl_context *ctx;
1558
1559 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1560
1561 oct = lio_get_device(ctx->octeon_id);
1562 if (status)
1563 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1564 CVM_CAST64(status));
1565 WRITE_ONCE(ctx->cond, 1);
1566
1567 /* This barrier is required to be sure that the response has been
1568 * written fully before waking up the handler
1569 */
1570 wmb();
1571
1572 wake_up_interruptible(&ctx->wc);
1573}
1574
1575/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001576 * \brief Send Rx control command
1577 * @param lio per-network private data
1578 * @param start_stop whether to start or stop
1579 */
1580static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1581{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001582 struct octeon_soft_command *sc;
1583 struct liquidio_rx_ctl_context *ctx;
1584 union octnet_cmd *ncmd;
1585 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1586 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1587 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001588
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001589 if (oct->props[lio->ifidx].rx_on == start_stop)
1590 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001591
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001592 sc = (struct octeon_soft_command *)
1593 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1594 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001595
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001596 ncmd = (union octnet_cmd *)sc->virtdptr;
1597 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1598
1599 WRITE_ONCE(ctx->cond, 0);
1600 ctx->octeon_id = lio_get_device_id(oct);
1601 init_waitqueue_head(&ctx->wc);
1602
1603 ncmd->u64 = 0;
1604 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1605 ncmd->s.param1 = start_stop;
1606
1607 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1608
1609 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1610
1611 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1612 OPCODE_NIC_CMD, 0, 0, 0);
1613
1614 sc->callback = rx_ctl_callback;
1615 sc->callback_arg = sc;
1616 sc->wait_time = 5000;
1617
1618 retval = octeon_send_soft_command(oct, sc);
1619 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001620 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001621 } else {
1622 /* Sleep on a wait queue till the cond flag indicates that the
1623 * response arrived or timed-out.
1624 */
1625 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1626 return;
1627 oct->props[lio->ifidx].rx_on = start_stop;
1628 }
1629
1630 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001631}
1632
1633/**
1634 * \brief Destroy NIC device interface
1635 * @param oct octeon device
1636 * @param ifidx which interface to destroy
1637 *
1638 * Cleanup associated with each interface for an Octeon device when NIC
1639 * module is being unloaded or if initialization fails during load.
1640 */
1641static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1642{
1643 struct net_device *netdev = oct->props[ifidx].netdev;
1644 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001645 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001646
1647 if (!netdev) {
1648 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1649 __func__, ifidx);
1650 return;
1651 }
1652
1653 lio = GET_LIO(netdev);
1654
1655 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1656
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001657 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001658 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001659
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001660 if (oct->props[lio->ifidx].napi_enabled == 1) {
1661 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1662 napi_disable(napi);
1663
1664 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001665
1666 if (OCTEON_CN23XX_PF(oct))
1667 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001668 }
1669
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001670 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1671 unregister_netdev(netdev);
1672
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001673 cleanup_link_status_change_wq(netdev);
1674
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001675 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001676
1677 free_netdev(netdev);
1678
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001679 oct->props[ifidx].gmxport = -1;
1680
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001681 oct->props[ifidx].netdev = NULL;
1682}
1683
1684/**
1685 * \brief Stop complete NIC functionality
1686 * @param oct octeon device
1687 */
1688static int liquidio_stop_nic_module(struct octeon_device *oct)
1689{
1690 int i, j;
1691 struct lio *lio;
1692
1693 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1694 if (!oct->ifcount) {
1695 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1696 return 1;
1697 }
1698
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001699 spin_lock_bh(&oct->cmd_resp_wqlock);
1700 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1701 spin_unlock_bh(&oct->cmd_resp_wqlock);
1702
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001703 for (i = 0; i < oct->ifcount; i++) {
1704 lio = GET_LIO(oct->props[i].netdev);
1705 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001706 octeon_unregister_droq_ops(oct,
1707 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001708 }
1709
1710 for (i = 0; i < oct->ifcount; i++)
1711 liquidio_destroy_nic_device(oct, i);
1712
1713 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1714 return 0;
1715}
1716
1717/**
1718 * \brief Cleans up resources at unload time
1719 * @param pdev PCI device structure
1720 */
1721static void liquidio_remove(struct pci_dev *pdev)
1722{
1723 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1724
1725 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1726
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001727 if (oct_dev->watchdog_task)
1728 kthread_stop(oct_dev->watchdog_task);
1729
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001730 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1731 liquidio_stop_nic_module(oct_dev);
1732
1733 /* Reset the octeon device and cleanup all memory allocated for
1734 * the octeon device by driver.
1735 */
1736 octeon_destroy_resources(oct_dev);
1737
1738 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1739
1740 /* This octeon device has been removed. Update the global
1741 * data structure to reflect this. Free the device structure.
1742 */
1743 octeon_free_device_mem(oct_dev);
1744}
1745
1746/**
1747 * \brief Identify the Octeon device and to map the BAR address space
1748 * @param oct octeon device
1749 */
1750static int octeon_chip_specific_setup(struct octeon_device *oct)
1751{
1752 u32 dev_id, rev_id;
1753 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001754 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001755
1756 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1757 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1758 oct->rev_id = rev_id & 0xff;
1759
1760 switch (dev_id) {
1761 case OCTEON_CN68XX_PCIID:
1762 oct->chip_id = OCTEON_CN68XX;
1763 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001764 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001765 break;
1766
1767 case OCTEON_CN66XX_PCIID:
1768 oct->chip_id = OCTEON_CN66XX;
1769 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001770 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001771 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001772
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001773 case OCTEON_CN23XX_PCIID_PF:
1774 oct->chip_id = OCTEON_CN23XX_PF_VID;
1775 ret = setup_cn23xx_octeon_pf_device(oct);
1776 s = "CN23XX";
1777 break;
1778
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001779 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001780 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001781 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1782 dev_id);
1783 }
1784
1785 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001786 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001787 OCTEON_MAJOR_REV(oct),
1788 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001789 octeon_get_conf(oct)->card_name,
1790 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001791
1792 return ret;
1793}
1794
1795/**
1796 * \brief PCI initialization for each Octeon device.
1797 * @param oct octeon device
1798 */
1799static int octeon_pci_os_setup(struct octeon_device *oct)
1800{
1801 /* setup PCI stuff first */
1802 if (pci_enable_device(oct->pci_dev)) {
1803 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1804 return 1;
1805 }
1806
1807 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1808 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001809 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001810 return 1;
1811 }
1812
1813 /* Enable PCI DMA Master. */
1814 pci_set_master(oct->pci_dev);
1815
1816 return 0;
1817}
1818
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001819static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1820{
1821 int q = 0;
1822
1823 if (netif_is_multiqueue(lio->netdev))
1824 q = skb->queue_mapping % lio->linfo.num_txpciq;
1825
1826 return q;
1827}
1828
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001829/**
1830 * \brief Check Tx queue state for a given network buffer
1831 * @param lio per-network private data
1832 * @param skb network buffer
1833 */
1834static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1835{
1836 int q = 0, iq = 0;
1837
1838 if (netif_is_multiqueue(lio->netdev)) {
1839 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001840 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001841 } else {
1842 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001843 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001844 }
1845
1846 if (octnet_iq_is_full(lio->oct_dev, iq))
1847 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001848
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001849 if (__netif_subqueue_stopped(lio->netdev, q)) {
1850 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001851 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001852 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001853 return 1;
1854}
1855
1856/**
1857 * \brief Unmap and free network buffer
1858 * @param buf buffer
1859 */
1860static void free_netbuf(void *buf)
1861{
1862 struct sk_buff *skb;
1863 struct octnet_buf_free_info *finfo;
1864 struct lio *lio;
1865
1866 finfo = (struct octnet_buf_free_info *)buf;
1867 skb = finfo->skb;
1868 lio = finfo->lio;
1869
1870 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1871 DMA_TO_DEVICE);
1872
1873 check_txq_state(lio, skb);
1874
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001875 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001876}
1877
1878/**
1879 * \brief Unmap and free gather buffer
1880 * @param buf buffer
1881 */
1882static void free_netsgbuf(void *buf)
1883{
1884 struct octnet_buf_free_info *finfo;
1885 struct sk_buff *skb;
1886 struct lio *lio;
1887 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001888 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001889
1890 finfo = (struct octnet_buf_free_info *)buf;
1891 skb = finfo->skb;
1892 lio = finfo->lio;
1893 g = finfo->g;
1894 frags = skb_shinfo(skb)->nr_frags;
1895
1896 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1897 g->sg[0].ptr[0], (skb->len - skb->data_len),
1898 DMA_TO_DEVICE);
1899
1900 i = 1;
1901 while (frags--) {
1902 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1903
1904 pci_unmap_page((lio->oct_dev)->pci_dev,
1905 g->sg[(i >> 2)].ptr[(i & 3)],
1906 frag->size, DMA_TO_DEVICE);
1907 i++;
1908 }
1909
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001910 iq = skb_iq(lio, skb);
1911 spin_lock(&lio->glist_lock[iq]);
1912 list_add_tail(&g->list, &lio->glist[iq]);
1913 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001914
1915 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1916
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001917 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001918}
1919
1920/**
1921 * \brief Unmap and free gather buffer with response
1922 * @param buf buffer
1923 */
1924static void free_netsgbuf_with_resp(void *buf)
1925{
1926 struct octeon_soft_command *sc;
1927 struct octnet_buf_free_info *finfo;
1928 struct sk_buff *skb;
1929 struct lio *lio;
1930 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001931 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001932
1933 sc = (struct octeon_soft_command *)buf;
1934 skb = (struct sk_buff *)sc->callback_arg;
1935 finfo = (struct octnet_buf_free_info *)&skb->cb;
1936
1937 lio = finfo->lio;
1938 g = finfo->g;
1939 frags = skb_shinfo(skb)->nr_frags;
1940
1941 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1942 g->sg[0].ptr[0], (skb->len - skb->data_len),
1943 DMA_TO_DEVICE);
1944
1945 i = 1;
1946 while (frags--) {
1947 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1948
1949 pci_unmap_page((lio->oct_dev)->pci_dev,
1950 g->sg[(i >> 2)].ptr[(i & 3)],
1951 frag->size, DMA_TO_DEVICE);
1952 i++;
1953 }
1954
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001955 iq = skb_iq(lio, skb);
1956
1957 spin_lock(&lio->glist_lock[iq]);
1958 list_add_tail(&g->list, &lio->glist[iq]);
1959 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001960
1961 /* Don't free the skb yet */
1962
1963 check_txq_state(lio, skb);
1964}
1965
1966/**
1967 * \brief Adjust ptp frequency
1968 * @param ptp PTP clock info
1969 * @param ppb how much to adjust by, in parts-per-billion
1970 */
1971static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1972{
1973 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1974 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1975 u64 comp, delta;
1976 unsigned long flags;
1977 bool neg_adj = false;
1978
1979 if (ppb < 0) {
1980 neg_adj = true;
1981 ppb = -ppb;
1982 }
1983
1984 /* The hardware adds the clock compensation value to the
1985 * PTP clock on every coprocessor clock cycle, so we
1986 * compute the delta in terms of coprocessor clocks.
1987 */
1988 delta = (u64)ppb << 32;
1989 do_div(delta, oct->coproc_clock_rate);
1990
1991 spin_lock_irqsave(&lio->ptp_lock, flags);
1992 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1993 if (neg_adj)
1994 comp -= delta;
1995 else
1996 comp += delta;
1997 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1998 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1999
2000 return 0;
2001}
2002
2003/**
2004 * \brief Adjust ptp time
2005 * @param ptp PTP clock info
2006 * @param delta how much to adjust by, in nanosecs
2007 */
2008static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2009{
2010 unsigned long flags;
2011 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2012
2013 spin_lock_irqsave(&lio->ptp_lock, flags);
2014 lio->ptp_adjust += delta;
2015 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2016
2017 return 0;
2018}
2019
2020/**
2021 * \brief Get hardware clock time, including any adjustment
2022 * @param ptp PTP clock info
2023 * @param ts timespec
2024 */
2025static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2026 struct timespec64 *ts)
2027{
2028 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002029 unsigned long flags;
2030 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2031 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2032
2033 spin_lock_irqsave(&lio->ptp_lock, flags);
2034 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2035 ns += lio->ptp_adjust;
2036 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2037
Kefeng Wang286af312016-01-27 17:34:37 +08002038 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002039
2040 return 0;
2041}
2042
2043/**
2044 * \brief Set hardware clock time. Reset adjustment
2045 * @param ptp PTP clock info
2046 * @param ts timespec
2047 */
2048static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2049 const struct timespec64 *ts)
2050{
2051 u64 ns;
2052 unsigned long flags;
2053 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2054 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2055
2056 ns = timespec_to_ns(ts);
2057
2058 spin_lock_irqsave(&lio->ptp_lock, flags);
2059 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2060 lio->ptp_adjust = 0;
2061 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2062
2063 return 0;
2064}
2065
2066/**
2067 * \brief Check if PTP is enabled
2068 * @param ptp PTP clock info
2069 * @param rq request
2070 * @param on is it on
2071 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002072static int
2073liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2074 struct ptp_clock_request *rq __attribute__((unused)),
2075 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002076{
2077 return -EOPNOTSUPP;
2078}
2079
2080/**
2081 * \brief Open PTP clock source
2082 * @param netdev network device
2083 */
2084static void oct_ptp_open(struct net_device *netdev)
2085{
2086 struct lio *lio = GET_LIO(netdev);
2087 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2088
2089 spin_lock_init(&lio->ptp_lock);
2090
2091 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2092 lio->ptp_info.owner = THIS_MODULE;
2093 lio->ptp_info.max_adj = 250000000;
2094 lio->ptp_info.n_alarm = 0;
2095 lio->ptp_info.n_ext_ts = 0;
2096 lio->ptp_info.n_per_out = 0;
2097 lio->ptp_info.pps = 0;
2098 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2099 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2100 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2101 lio->ptp_info.settime64 = liquidio_ptp_settime;
2102 lio->ptp_info.enable = liquidio_ptp_enable;
2103
2104 lio->ptp_adjust = 0;
2105
2106 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2107 &oct->pci_dev->dev);
2108
2109 if (IS_ERR(lio->ptp_clock))
2110 lio->ptp_clock = NULL;
2111}
2112
2113/**
2114 * \brief Init PTP clock
2115 * @param oct octeon device
2116 */
2117static void liquidio_ptp_init(struct octeon_device *oct)
2118{
2119 u64 clock_comp, cfg;
2120
2121 clock_comp = (u64)NSEC_PER_SEC << 32;
2122 do_div(clock_comp, oct->coproc_clock_rate);
2123 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2124
2125 /* Enable */
2126 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2127 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2128}
2129
2130/**
2131 * \brief Load firmware to device
2132 * @param oct octeon device
2133 *
2134 * Maps device to firmware filename, requests firmware, and downloads it
2135 */
2136static int load_firmware(struct octeon_device *oct)
2137{
2138 int ret = 0;
2139 const struct firmware *fw;
2140 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2141 char *tmp_fw_type;
2142
2143 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2144 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2145 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2146 return ret;
2147 }
2148
2149 if (fw_type[0] == '\0')
2150 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2151 else
2152 tmp_fw_type = fw_type;
2153
2154 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2155 octeon_get_conf(oct)->card_name, tmp_fw_type,
2156 LIO_FW_NAME_SUFFIX);
2157
2158 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2159 if (ret) {
2160 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2161 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002162 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002163 return ret;
2164 }
2165
2166 ret = octeon_download_firmware(oct, fw->data, fw->size);
2167
2168 release_firmware(fw);
2169
2170 return ret;
2171}
2172
2173/**
2174 * \brief Setup output queue
2175 * @param oct octeon device
2176 * @param q_no which queue
2177 * @param num_descs how many descriptors
2178 * @param desc_size size of each descriptor
2179 * @param app_ctx application context
2180 */
2181static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2182 int desc_size, void *app_ctx)
2183{
2184 int ret_val = 0;
2185
2186 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2187 /* droq creation and local register settings. */
2188 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302189 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002190 return ret_val;
2191
2192 if (ret_val == 1) {
2193 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2194 return 0;
2195 }
2196 /* tasklet creation for the droq */
2197
2198 /* Enable the droq queues */
2199 octeon_set_droq_pkt_op(oct, q_no, 1);
2200
2201 /* Send Credit for Octeon Output queues. Credits are always
2202 * sent after the output queue is enabled.
2203 */
2204 writel(oct->droq[q_no]->max_count,
2205 oct->droq[q_no]->pkts_credit_reg);
2206
2207 return ret_val;
2208}
2209
2210/**
2211 * \brief Callback for getting interface configuration
2212 * @param status status of request
2213 * @param buf pointer to resp structure
2214 */
2215static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002216 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002217 void *buf)
2218{
2219 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2220 struct liquidio_if_cfg_resp *resp;
2221 struct liquidio_if_cfg_context *ctx;
2222
2223 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002224 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002225
2226 oct = lio_get_device(ctx->octeon_id);
2227 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002228 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2229 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002230 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002231
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002232 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2233 resp->cfg_info.liquidio_firmware_version);
2234
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002235 /* This barrier is required to be sure that the response has been
2236 * written fully before waking up the handler
2237 */
2238 wmb();
2239
2240 wake_up_interruptible(&ctx->wc);
2241}
2242
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002243/** Routine to push packets arriving on Octeon interface upto network layer.
2244 * @param oct_id - octeon device id.
2245 * @param skbuff - skbuff struct to be passed to network layer.
2246 * @param len - size of total data received.
2247 * @param rh - Control header associated with the packet
2248 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002249 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002250 */
2251static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002252liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002253 void *skbuff,
2254 u32 len,
2255 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002256 void *param,
2257 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002258{
2259 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002260 struct sk_buff *skb = (struct sk_buff *)skbuff;
2261 struct skb_shared_hwtstamps *shhwtstamps;
2262 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002263 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002264 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002265 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002266 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2267 napi);
2268 if (netdev) {
2269 int packet_was_received;
2270 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002271 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002272
2273 /* Do not proceed if the interface is not in RUNNING state. */
2274 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2275 recv_buffer_free(skb);
2276 droq->stats.rx_dropped++;
2277 return;
2278 }
2279
2280 skb->dev = netdev;
2281
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002282 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002283 if (likely(len > MIN_SKB_SIZE)) {
2284 struct octeon_skb_page_info *pg_info;
2285 unsigned char *va;
2286
2287 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2288 if (pg_info->page) {
2289 /* For Paged allocation use the frags */
2290 va = page_address(pg_info->page) +
2291 pg_info->page_offset;
2292 memcpy(skb->data, va, MIN_SKB_SIZE);
2293 skb_put(skb, MIN_SKB_SIZE);
2294 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2295 pg_info->page,
2296 pg_info->page_offset +
2297 MIN_SKB_SIZE,
2298 len - MIN_SKB_SIZE,
2299 LIO_RXBUFFER_SZ);
2300 }
2301 } else {
2302 struct octeon_skb_page_info *pg_info =
2303 ((struct octeon_skb_page_info *)(skb->cb));
2304 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2305 + pg_info->page_offset, len);
2306 skb_put(skb, len);
2307 put_page(pg_info->page);
2308 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002309
Prasad Kannegantide28c992017-01-09 14:42:40 -08002310 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2311
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002312 if (((oct->chip_id == OCTEON_CN66XX) ||
2313 (oct->chip_id == OCTEON_CN68XX)) &&
2314 ptp_enable) {
2315 if (rh->r_dh.has_hwtstamp) {
2316 /* timestamp is included from the hardware at
2317 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002318 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002319 if (ifstate_check
2320 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2321 /* Nanoseconds are in the first 64-bits
2322 * of the packet.
2323 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002324 memcpy(&ns, (skb->data + r_dh_off),
2325 sizeof(ns));
2326 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002327 shhwtstamps = skb_hwtstamps(skb);
2328 shhwtstamps->hwtstamp =
2329 ns_to_ktime(ns +
2330 lio->ptp_adjust);
2331 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002332 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002333 }
2334
Prasad Kannegantide28c992017-01-09 14:42:40 -08002335 if (rh->r_dh.has_hash) {
2336 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2337 u32 hash = be32_to_cpu(*hash_be);
2338
2339 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2340 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2341 }
2342
2343 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2344
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002345 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002346 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002347 (((rh->r_dh.encap_on) &&
2348 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2349 (!(rh->r_dh.encap_on) &&
2350 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002351 /* checksum has already been verified */
2352 skb->ip_summed = CHECKSUM_UNNECESSARY;
2353 else
2354 skb->ip_summed = CHECKSUM_NONE;
2355
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002356 /* Setting Encapsulation field on basis of status received
2357 * from the firmware
2358 */
2359 if (rh->r_dh.encap_on) {
2360 skb->encapsulation = 1;
2361 skb->csum_level = 1;
2362 droq->stats.rx_vxlan++;
2363 }
2364
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002365 /* inbound VLAN tag */
2366 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2367 (rh->r_dh.vlan != 0)) {
2368 u16 vid = rh->r_dh.vlan;
2369 u16 priority = rh->r_dh.priority;
2370
2371 vtag = priority << 13 | vid;
2372 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2373 }
2374
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002375 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2376
2377 if (packet_was_received) {
2378 droq->stats.rx_bytes_received += len;
2379 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002380 } else {
2381 droq->stats.rx_dropped++;
2382 netif_info(lio, rx_err, lio->netdev,
2383 "droq:%d error rx_dropped:%llu\n",
2384 droq->q_no, droq->stats.rx_dropped);
2385 }
2386
2387 } else {
2388 recv_buffer_free(skb);
2389 }
2390}
2391
2392/**
2393 * \brief wrapper for calling napi_schedule
2394 * @param param parameters to pass to napi_schedule
2395 *
2396 * Used when scheduling on different CPUs
2397 */
2398static void napi_schedule_wrapper(void *param)
2399{
2400 struct napi_struct *napi = param;
2401
2402 napi_schedule(napi);
2403}
2404
2405/**
2406 * \brief callback when receive interrupt occurs and we are in NAPI mode
2407 * @param arg pointer to octeon output queue
2408 */
2409static void liquidio_napi_drv_callback(void *arg)
2410{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002411 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002412 struct octeon_droq *droq = arg;
2413 int this_cpu = smp_processor_id();
2414
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002415 oct = droq->oct_dev;
2416
2417 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2418 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002419 } else {
2420 struct call_single_data *csd = &droq->csd;
2421
2422 csd->func = napi_schedule_wrapper;
2423 csd->info = &droq->napi;
2424 csd->flags = 0;
2425
2426 smp_call_function_single_async(droq->cpu_id, csd);
2427 }
2428}
2429
2430/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002431 * \brief Entry point for NAPI polling
2432 * @param napi NAPI structure
2433 * @param budget maximum number of items to process
2434 */
2435static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2436{
2437 struct octeon_droq *droq;
2438 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002439 int tx_done = 0, iq_no;
2440 struct octeon_instr_queue *iq;
2441 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002442
2443 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002444 oct = droq->oct_dev;
2445 iq_no = droq->q_no;
2446 /* Handle Droq descriptors */
2447 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2448 POLL_EVENT_PROCESS_PKTS,
2449 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002450
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002451 /* Flush the instruction queue */
2452 iq = oct->instr_queue[iq_no];
2453 if (iq) {
2454 /* Process iq buffers with in the budget limits */
Derek Chickles60889862017-01-06 17:16:12 -08002455 tx_done = octeon_flush_iq(oct, iq, budget);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002456 /* Update iq read-index rather than waiting for next interrupt.
2457 * Return back if tx_done is false.
2458 */
2459 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002460 } else {
2461 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2462 __func__, iq_no);
2463 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002464
Satanand Burlacdb478e2017-01-31 13:04:42 -08002465 /* force enable interrupt if reg cnts are high to avoid wraparound */
2466 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002467 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002468 (droq->pkt_count >= MAX_REG_CNT)) {
2469 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002470 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002471 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2472 POLL_EVENT_ENABLE_INTR, 0);
2473 return 0;
2474 }
2475
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002476 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002477}
2478
2479/**
2480 * \brief Setup input and output queues
2481 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002482 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002483 *
2484 * Note: Queues are with respect to the octeon device. Thus
2485 * an input queue is for egress packets, and output queues
2486 * are for ingress packets.
2487 */
2488static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002489 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002490{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002491 struct octeon_droq_ops droq_ops;
2492 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002493 static int cpu_id;
2494 static int cpu_id_modulus;
2495 struct octeon_droq *droq;
2496 struct napi_struct *napi;
2497 int q, q_no, retval = 0;
2498 struct lio *lio;
2499 int num_tx_descs;
2500
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002501 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002502
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002503 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002504
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002505 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2506
2507 droq_ops.fptr = liquidio_push_packet;
2508 droq_ops.farg = (void *)netdev;
2509
2510 droq_ops.poll_mode = 1;
2511 droq_ops.napi_fn = liquidio_napi_drv_callback;
2512 cpu_id = 0;
2513 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002514
2515 /* set up DROQs. */
2516 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002517 q_no = lio->linfo.rxpciq[q].s.q_no;
2518 dev_dbg(&octeon_dev->pci_dev->dev,
2519 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2520 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002521 retval = octeon_setup_droq(octeon_dev, q_no,
2522 CFG_GET_NUM_RX_DESCS_NIC_IF
2523 (octeon_get_conf(octeon_dev),
2524 lio->ifidx),
2525 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2526 (octeon_get_conf(octeon_dev),
2527 lio->ifidx), NULL);
2528 if (retval) {
2529 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002530 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002531 __func__);
2532 return 1;
2533 }
2534
2535 droq = octeon_dev->droq[q_no];
2536 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002537 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2538 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002539 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002540
2541 /* designate a CPU for this droq */
2542 droq->cpu_id = cpu_id;
2543 cpu_id++;
2544 if (cpu_id >= cpu_id_modulus)
2545 cpu_id = 0;
2546
2547 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2548 }
2549
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002550 if (OCTEON_CN23XX_PF(octeon_dev)) {
2551 /* 23XX PF can receive control messages (via the first PF-owned
2552 * droq) from the firmware even if the ethX interface is down,
2553 * so that's why poll_mode must be off for the first droq.
2554 */
2555 octeon_dev->droq[0]->ops.poll_mode = 0;
2556 }
2557
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002558 /* set up IQs. */
2559 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2560 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2561 (octeon_dev),
2562 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002563 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2564 lio->linfo.txpciq[q], num_tx_descs,
2565 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002566 if (retval) {
2567 dev_err(&octeon_dev->pci_dev->dev,
2568 " %s : Runtime IQ(TxQ) creation failed.\n",
2569 __func__);
2570 return 1;
2571 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002572
2573 if (octeon_dev->ioq_vector) {
2574 struct octeon_ioq_vector *ioq_vector;
2575
2576 ioq_vector = &octeon_dev->ioq_vector[q];
2577 netif_set_xps_queue(netdev,
2578 &ioq_vector->affinity_mask,
2579 ioq_vector->iq_index);
2580 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002581 }
2582
2583 return 0;
2584}
2585
2586/**
2587 * \brief Poll routine for checking transmit queue status
2588 * @param work work_struct data structure
2589 */
2590static void octnet_poll_check_txq_status(struct work_struct *work)
2591{
2592 struct cavium_wk *wk = (struct cavium_wk *)work;
2593 struct lio *lio = (struct lio *)wk->ctxptr;
2594
2595 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2596 return;
2597
2598 check_txq_status(lio);
2599 queue_delayed_work(lio->txq_status_wq.wq,
2600 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2601}
2602
2603/**
2604 * \brief Sets up the txq poll check
2605 * @param netdev network device
2606 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002607static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002608{
2609 struct lio *lio = GET_LIO(netdev);
2610 struct octeon_device *oct = lio->oct_dev;
2611
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302612 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2613 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002614 if (!lio->txq_status_wq.wq) {
2615 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002616 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002617 }
2618 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2619 octnet_poll_check_txq_status);
2620 lio->txq_status_wq.wk.ctxptr = lio;
2621 queue_delayed_work(lio->txq_status_wq.wq,
2622 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002623 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002624}
2625
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002626static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2627{
2628 struct lio *lio = GET_LIO(netdev);
2629
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002630 if (lio->txq_status_wq.wq) {
2631 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2632 destroy_workqueue(lio->txq_status_wq.wq);
2633 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002634}
2635
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002636/**
2637 * \brief Net device open for LiquidIO
2638 * @param netdev network device
2639 */
2640static int liquidio_open(struct net_device *netdev)
2641{
2642 struct lio *lio = GET_LIO(netdev);
2643 struct octeon_device *oct = lio->oct_dev;
2644 struct napi_struct *napi, *n;
2645
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002646 if (oct->props[lio->ifidx].napi_enabled == 0) {
2647 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2648 napi_enable(napi);
2649
2650 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002651
2652 if (OCTEON_CN23XX_PF(oct))
2653 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002654 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002655
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002656 if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
2657 ptp_enable)
2658 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002659
2660 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002661
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002662 /* Ready for link status updates */
2663 lio->intf_open = 1;
2664
2665 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2666
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002667 if (OCTEON_CN23XX_PF(oct)) {
2668 if (!oct->msix_on)
2669 if (setup_tx_poll_fn(netdev))
2670 return -1;
2671 } else {
2672 if (setup_tx_poll_fn(netdev))
2673 return -1;
2674 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002675
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002676 start_txq(netdev);
2677
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002678 /* tell Octeon to start forwarding packets to host */
2679 send_rx_ctrl_cmd(lio, 1);
2680
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002681 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2682 netdev->name);
2683
2684 return 0;
2685}
2686
2687/**
2688 * \brief Net device stop for LiquidIO
2689 * @param netdev network device
2690 */
2691static int liquidio_stop(struct net_device *netdev)
2692{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002693 struct lio *lio = GET_LIO(netdev);
2694 struct octeon_device *oct = lio->oct_dev;
2695
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002696 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2697
2698 netif_tx_disable(netdev);
2699
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002700 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002701 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002702 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002703 lio->linfo.link.s.link_up = 0;
2704 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705
Felix Manlunascb2336b2017-01-11 17:09:02 -08002706 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002707 send_rx_ctrl_cmd(lio, 0);
2708
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002709 if (OCTEON_CN23XX_PF(oct)) {
2710 if (!oct->msix_on)
2711 cleanup_tx_poll_fn(netdev);
2712 } else {
2713 cleanup_tx_poll_fn(netdev);
2714 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002715
2716 if (lio->ptp_clock) {
2717 ptp_clock_unregister(lio->ptp_clock);
2718 lio->ptp_clock = NULL;
2719 }
2720
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002721 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002722
2723 return 0;
2724}
2725
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002726/**
2727 * \brief Converts a mask based on net device flags
2728 * @param netdev network device
2729 *
2730 * This routine generates a octnet_ifflags mask from the net device flags
2731 * received from the OS.
2732 */
2733static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2734{
2735 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2736
2737 if (netdev->flags & IFF_PROMISC)
2738 f |= OCTNET_IFFLAG_PROMISC;
2739
2740 if (netdev->flags & IFF_ALLMULTI)
2741 f |= OCTNET_IFFLAG_ALLMULTI;
2742
2743 if (netdev->flags & IFF_MULTICAST) {
2744 f |= OCTNET_IFFLAG_MULTICAST;
2745
2746 /* Accept all multicast addresses if there are more than we
2747 * can handle
2748 */
2749 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2750 f |= OCTNET_IFFLAG_ALLMULTI;
2751 }
2752
2753 if (netdev->flags & IFF_BROADCAST)
2754 f |= OCTNET_IFFLAG_BROADCAST;
2755
2756 return f;
2757}
2758
2759/**
2760 * \brief Net device set_multicast_list
2761 * @param netdev network device
2762 */
2763static void liquidio_set_mcast_list(struct net_device *netdev)
2764{
2765 struct lio *lio = GET_LIO(netdev);
2766 struct octeon_device *oct = lio->oct_dev;
2767 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002768 struct netdev_hw_addr *ha;
2769 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002770 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002771 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2772
2773 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2774
2775 /* Create a ctrl pkt command to be sent to core app. */
2776 nctrl.ncmd.u64 = 0;
2777 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002778 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2779 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002780 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002781 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002782 nctrl.netpndev = (u64)netdev;
2783 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2784
2785 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002786 mc = &nctrl.udd[0];
2787 netdev_for_each_mc_addr(ha, netdev) {
2788 *mc = 0;
2789 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2790 /* no need to swap bytes */
2791
2792 if (++mc > &nctrl.udd[mc_count])
2793 break;
2794 }
2795
2796 /* Apparently, any activity in this call from the kernel has to
2797 * be atomic. So we won't wait for response.
2798 */
2799 nctrl.wait_time = 0;
2800
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002801 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 if (ret < 0) {
2803 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2804 ret);
2805 }
2806}
2807
2808/**
2809 * \brief Net device set_mac_address
2810 * @param netdev network device
2811 */
2812static int liquidio_set_mac(struct net_device *netdev, void *p)
2813{
2814 int ret = 0;
2815 struct lio *lio = GET_LIO(netdev);
2816 struct octeon_device *oct = lio->oct_dev;
2817 struct sockaddr *addr = (struct sockaddr *)p;
2818 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002819
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002820 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002821 return -EADDRNOTAVAIL;
2822
2823 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2824
2825 nctrl.ncmd.u64 = 0;
2826 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002827 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002828 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002829 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002830 nctrl.netpndev = (u64)netdev;
2831 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2832 nctrl.wait_time = 100;
2833
2834 nctrl.udd[0] = 0;
2835 /* The MAC Address is presented in network byte order. */
2836 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2837
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002838 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002839 if (ret < 0) {
2840 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2841 return -ENOMEM;
2842 }
2843 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2844 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2845
2846 return 0;
2847}
2848
2849/**
2850 * \brief Net device get_stats
2851 * @param netdev network device
2852 */
2853static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2854{
2855 struct lio *lio = GET_LIO(netdev);
2856 struct net_device_stats *stats = &netdev->stats;
2857 struct octeon_device *oct;
2858 u64 pkts = 0, drop = 0, bytes = 0;
2859 struct oct_droq_stats *oq_stats;
2860 struct oct_iq_stats *iq_stats;
2861 int i, iq_no, oq_no;
2862
2863 oct = lio->oct_dev;
2864
2865 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002866 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002867 iq_stats = &oct->instr_queue[iq_no]->stats;
2868 pkts += iq_stats->tx_done;
2869 drop += iq_stats->tx_dropped;
2870 bytes += iq_stats->tx_tot_bytes;
2871 }
2872
2873 stats->tx_packets = pkts;
2874 stats->tx_bytes = bytes;
2875 stats->tx_dropped = drop;
2876
2877 pkts = 0;
2878 drop = 0;
2879 bytes = 0;
2880
2881 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002882 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002883 oq_stats = &oct->droq[oq_no]->stats;
2884 pkts += oq_stats->rx_pkts_received;
2885 drop += (oq_stats->rx_dropped +
2886 oq_stats->dropped_nodispatch +
2887 oq_stats->dropped_toomany +
2888 oq_stats->dropped_nomem);
2889 bytes += oq_stats->rx_bytes_received;
2890 }
2891
2892 stats->rx_bytes = bytes;
2893 stats->rx_packets = pkts;
2894 stats->rx_dropped = drop;
2895
2896 return stats;
2897}
2898
2899/**
2900 * \brief Net device change_mtu
2901 * @param netdev network device
2902 */
2903static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2904{
2905 struct lio *lio = GET_LIO(netdev);
2906 struct octeon_device *oct = lio->oct_dev;
2907 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002908 int ret = 0;
2909
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002910 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2911
2912 nctrl.ncmd.u64 = 0;
2913 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002914 nctrl.ncmd.s.param1 = new_mtu;
2915 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002916 nctrl.wait_time = 100;
2917 nctrl.netpndev = (u64)netdev;
2918 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2919
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002920 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002921 if (ret < 0) {
2922 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2923 return -1;
2924 }
2925
2926 lio->mtu = new_mtu;
2927
2928 return 0;
2929}
2930
2931/**
2932 * \brief Handler for SIOCSHWTSTAMP ioctl
2933 * @param netdev network device
2934 * @param ifr interface request
2935 * @param cmd command
2936 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002937static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002938{
2939 struct hwtstamp_config conf;
2940 struct lio *lio = GET_LIO(netdev);
2941
2942 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2943 return -EFAULT;
2944
2945 if (conf.flags)
2946 return -EINVAL;
2947
2948 switch (conf.tx_type) {
2949 case HWTSTAMP_TX_ON:
2950 case HWTSTAMP_TX_OFF:
2951 break;
2952 default:
2953 return -ERANGE;
2954 }
2955
2956 switch (conf.rx_filter) {
2957 case HWTSTAMP_FILTER_NONE:
2958 break;
2959 case HWTSTAMP_FILTER_ALL:
2960 case HWTSTAMP_FILTER_SOME:
2961 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2962 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2963 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2964 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2965 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2966 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2967 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2968 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2969 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2970 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2971 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2972 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2973 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2974 break;
2975 default:
2976 return -ERANGE;
2977 }
2978
2979 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2980 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2981
2982 else
2983 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2984
2985 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2986}
2987
2988/**
2989 * \brief ioctl handler
2990 * @param netdev network device
2991 * @param ifr interface request
2992 * @param cmd command
2993 */
2994static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2995{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002996 struct lio *lio = GET_LIO(netdev);
2997
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002998 switch (cmd) {
2999 case SIOCSHWTSTAMP:
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003000 if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
3001 lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
3002 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003003 default:
3004 return -EOPNOTSUPP;
3005 }
3006}
3007
3008/**
3009 * \brief handle a Tx timestamp response
3010 * @param status response status
3011 * @param buf pointer to skb
3012 */
3013static void handle_timestamp(struct octeon_device *oct,
3014 u32 status,
3015 void *buf)
3016{
3017 struct octnet_buf_free_info *finfo;
3018 struct octeon_soft_command *sc;
3019 struct oct_timestamp_resp *resp;
3020 struct lio *lio;
3021 struct sk_buff *skb = (struct sk_buff *)buf;
3022
3023 finfo = (struct octnet_buf_free_info *)skb->cb;
3024 lio = finfo->lio;
3025 sc = finfo->sc;
3026 oct = lio->oct_dev;
3027 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3028
3029 if (status != OCTEON_REQUEST_DONE) {
3030 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3031 CVM_CAST64(status));
3032 resp->timestamp = 0;
3033 }
3034
3035 octeon_swap_8B_data(&resp->timestamp, 1);
3036
Colin Ian King19a6d152016-02-05 16:30:39 +00003037 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003038 struct skb_shared_hwtstamps ts;
3039 u64 ns = resp->timestamp;
3040
3041 netif_info(lio, tx_done, lio->netdev,
3042 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3043 skb, (unsigned long long)ns);
3044 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3045 skb_tstamp_tx(skb, &ts);
3046 }
3047
3048 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003049 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003050}
3051
3052/* \brief Send a data packet that will be timestamped
3053 * @param oct octeon device
3054 * @param ndata pointer to network data
3055 * @param finfo pointer to private network data
3056 */
3057static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3058 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003059 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003060{
3061 int retval;
3062 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003063 struct lio *lio;
3064 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003065 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003066
3067 lio = finfo->lio;
3068
3069 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3070 sizeof(struct oct_timestamp_resp));
3071 finfo->sc = sc;
3072
3073 if (!sc) {
3074 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3075 return IQ_SEND_FAILED;
3076 }
3077
3078 if (ndata->reqtype == REQTYPE_NORESP_NET)
3079 ndata->reqtype = REQTYPE_RESP_NET;
3080 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3081 ndata->reqtype = REQTYPE_RESP_NET_SG;
3082
3083 sc->callback = handle_timestamp;
3084 sc->callback_arg = finfo->skb;
3085 sc->iq_no = ndata->q_no;
3086
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003087 if (OCTEON_CN23XX_PF(oct))
3088 len = (u32)((struct octeon_instr_ih3 *)
3089 (&sc->cmd.cmd3.ih3))->dlengsz;
3090 else
3091 len = (u32)((struct octeon_instr_ih2 *)
3092 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003093
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003094 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003095
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003096 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003097 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003098
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003099 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003100 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3101 retval);
3102 octeon_free_soft_command(oct, sc);
3103 } else {
3104 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3105 }
3106
3107 return retval;
3108}
3109
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003110/** \brief Transmit networks packets to the Octeon interface
3111 * @param skbuff skbuff struct to be passed to network layer.
3112 * @param netdev pointer to network device
3113 * @returns whether the packet was transmitted to the device okay or not
3114 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3115 */
3116static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3117{
3118 struct lio *lio;
3119 struct octnet_buf_free_info *finfo;
3120 union octnic_cmd_setup cmdsetup;
3121 struct octnic_data_pkt ndata;
3122 struct octeon_device *oct;
3123 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003124 struct octeon_instr_irh *irh;
3125 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003126 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003127 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003128 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003129 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003130 u32 tag = 0;
3131
3132 lio = GET_LIO(netdev);
3133 oct = lio->oct_dev;
3134
3135 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003136 q_idx = skb->queue_mapping;
3137 q_idx = (q_idx % (lio->linfo.num_txpciq));
3138 tag = q_idx;
3139 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003140 } else {
3141 iq_no = lio->txq;
3142 }
3143
3144 stats = &oct->instr_queue[iq_no]->stats;
3145
3146 /* Check for all conditions in which the current packet cannot be
3147 * transmitted.
3148 */
3149 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003150 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003151 (skb->len <= 0)) {
3152 netif_info(lio, tx_err, lio->netdev,
3153 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003154 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003155 goto lio_xmit_failed;
3156 }
3157
3158 /* Use space in skb->cb to store info used to unmap and
3159 * free the buffers.
3160 */
3161 finfo = (struct octnet_buf_free_info *)skb->cb;
3162 finfo->lio = lio;
3163 finfo->skb = skb;
3164 finfo->sc = NULL;
3165
3166 /* Prepare the attributes for the data to be passed to OSI. */
3167 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3168
3169 ndata.buf = (void *)finfo;
3170
3171 ndata.q_no = iq_no;
3172
3173 if (netif_is_multiqueue(netdev)) {
3174 if (octnet_iq_is_full(oct, ndata.q_no)) {
3175 /* defer sending if queue is full */
3176 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3177 ndata.q_no);
3178 stats->tx_iq_busy++;
3179 return NETDEV_TX_BUSY;
3180 }
3181 } else {
3182 if (octnet_iq_is_full(oct, lio->txq)) {
3183 /* defer sending if queue is full */
3184 stats->tx_iq_busy++;
3185 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003186 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003187 return NETDEV_TX_BUSY;
3188 }
3189 }
3190 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003191 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003192 */
3193
3194 ndata.datasize = skb->len;
3195
3196 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003197 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003198
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003199 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3200 if (skb->encapsulation) {
3201 cmdsetup.s.tnl_csum = 1;
3202 stats->tx_vxlan++;
3203 } else {
3204 cmdsetup.s.transport_csum = 1;
3205 }
3206 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003207 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3208 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3209 cmdsetup.s.timestamp = 1;
3210 }
3211
3212 if (skb_shinfo(skb)->nr_frags == 0) {
3213 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003214 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003215
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003216 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003217 dptr = dma_map_single(&oct->pci_dev->dev,
3218 skb->data,
3219 skb->len,
3220 DMA_TO_DEVICE);
3221 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003222 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3223 __func__);
3224 return NETDEV_TX_BUSY;
3225 }
3226
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003227 if (OCTEON_CN23XX_PF(oct))
3228 ndata.cmd.cmd3.dptr = dptr;
3229 else
3230 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003231 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003232 ndata.reqtype = REQTYPE_NORESP_NET;
3233
3234 } else {
3235 int i, frags;
3236 struct skb_frag_struct *frag;
3237 struct octnic_gather *g;
3238
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003239 spin_lock(&lio->glist_lock[q_idx]);
3240 g = (struct octnic_gather *)
3241 list_delete_head(&lio->glist[q_idx]);
3242 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003243
3244 if (!g) {
3245 netif_info(lio, tx_err, lio->netdev,
3246 "Transmit scatter gather: glist null!\n");
3247 goto lio_xmit_failed;
3248 }
3249
3250 cmdsetup.s.gather = 1;
3251 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003252 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003253
3254 memset(g->sg, 0, g->sg_size);
3255
3256 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3257 skb->data,
3258 (skb->len - skb->data_len),
3259 DMA_TO_DEVICE);
3260 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3261 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3262 __func__);
3263 return NETDEV_TX_BUSY;
3264 }
3265 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3266
3267 frags = skb_shinfo(skb)->nr_frags;
3268 i = 1;
3269 while (frags--) {
3270 frag = &skb_shinfo(skb)->frags[i - 1];
3271
3272 g->sg[(i >> 2)].ptr[(i & 3)] =
3273 dma_map_page(&oct->pci_dev->dev,
3274 frag->page.p,
3275 frag->page_offset,
3276 frag->size,
3277 DMA_TO_DEVICE);
3278
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003279 if (dma_mapping_error(&oct->pci_dev->dev,
3280 g->sg[i >> 2].ptr[i & 3])) {
3281 dma_unmap_single(&oct->pci_dev->dev,
3282 g->sg[0].ptr[0],
3283 skb->len - skb->data_len,
3284 DMA_TO_DEVICE);
3285 for (j = 1; j < i; j++) {
3286 frag = &skb_shinfo(skb)->frags[j - 1];
3287 dma_unmap_page(&oct->pci_dev->dev,
3288 g->sg[j >> 2].ptr[j & 3],
3289 frag->size,
3290 DMA_TO_DEVICE);
3291 }
3292 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3293 __func__);
3294 return NETDEV_TX_BUSY;
3295 }
3296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003297 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3298 i++;
3299 }
3300
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003301 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003302
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003303 if (OCTEON_CN23XX_PF(oct))
3304 ndata.cmd.cmd3.dptr = dptr;
3305 else
3306 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003307 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003308 finfo->g = g;
3309
3310 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3311 }
3312
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003313 if (OCTEON_CN23XX_PF(oct)) {
3314 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3315 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3316 } else {
3317 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3318 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3319 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003320
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003321 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003322 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3323 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003324 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003325 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003326
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003327 /* HW insert VLAN tag */
3328 if (skb_vlan_tag_present(skb)) {
3329 irh->priority = skb_vlan_tag_get(skb) >> 13;
3330 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3331 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003332
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003333 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003334 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003335 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003336 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003337 if (status == IQ_SEND_FAILED)
3338 goto lio_xmit_failed;
3339
3340 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3341
3342 if (status == IQ_SEND_STOP)
3343 stop_q(lio->netdev, q_idx);
3344
Florian Westphal860e9532016-05-03 16:33:13 +02003345 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003346
Satanand Burla80c8eae2017-01-26 11:52:35 -08003347 if (tx_info->s.gso_segs)
3348 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003349 else
3350 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003351 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003352
3353 return NETDEV_TX_OK;
3354
3355lio_xmit_failed:
3356 stats->tx_dropped++;
3357 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3358 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003359 if (dptr)
3360 dma_unmap_single(&oct->pci_dev->dev, dptr,
3361 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003362 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003363 return NETDEV_TX_OK;
3364}
3365
3366/** \brief Network device Tx timeout
3367 * @param netdev pointer to network device
3368 */
3369static void liquidio_tx_timeout(struct net_device *netdev)
3370{
3371 struct lio *lio;
3372
3373 lio = GET_LIO(netdev);
3374
3375 netif_info(lio, tx_err, lio->netdev,
3376 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3377 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003378 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003379 txqs_wake(netdev);
3380}
3381
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003382static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3383 __be16 proto __attribute__((unused)),
3384 u16 vid)
3385{
3386 struct lio *lio = GET_LIO(netdev);
3387 struct octeon_device *oct = lio->oct_dev;
3388 struct octnic_ctrl_pkt nctrl;
3389 int ret = 0;
3390
3391 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3392
3393 nctrl.ncmd.u64 = 0;
3394 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3395 nctrl.ncmd.s.param1 = vid;
3396 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3397 nctrl.wait_time = 100;
3398 nctrl.netpndev = (u64)netdev;
3399 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3400
3401 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3402 if (ret < 0) {
3403 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3404 ret);
3405 }
3406
3407 return ret;
3408}
3409
3410static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3411 __be16 proto __attribute__((unused)),
3412 u16 vid)
3413{
3414 struct lio *lio = GET_LIO(netdev);
3415 struct octeon_device *oct = lio->oct_dev;
3416 struct octnic_ctrl_pkt nctrl;
3417 int ret = 0;
3418
3419 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3420
3421 nctrl.ncmd.u64 = 0;
3422 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3423 nctrl.ncmd.s.param1 = vid;
3424 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3425 nctrl.wait_time = 100;
3426 nctrl.netpndev = (u64)netdev;
3427 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3428
3429 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3430 if (ret < 0) {
3431 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3432 ret);
3433 }
3434 return ret;
3435}
3436
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003437/** Sending command to enable/disable RX checksum offload
3438 * @param netdev pointer to network device
3439 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3440 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3441 * OCTNET_CMD_RXCSUM_DISABLE
3442 * @returns SUCCESS or FAILURE
3443 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003444static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3445 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003446{
3447 struct lio *lio = GET_LIO(netdev);
3448 struct octeon_device *oct = lio->oct_dev;
3449 struct octnic_ctrl_pkt nctrl;
3450 int ret = 0;
3451
3452 nctrl.ncmd.u64 = 0;
3453 nctrl.ncmd.s.cmd = command;
3454 nctrl.ncmd.s.param1 = rx_cmd;
3455 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3456 nctrl.wait_time = 100;
3457 nctrl.netpndev = (u64)netdev;
3458 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3459
3460 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3461 if (ret < 0) {
3462 dev_err(&oct->pci_dev->dev,
3463 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3464 ret);
3465 }
3466 return ret;
3467}
3468
3469/** Sending command to add/delete VxLAN UDP port to firmware
3470 * @param netdev pointer to network device
3471 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3472 * @param vxlan_port VxLAN port to be added or deleted
3473 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3474 * OCTNET_CMD_VXLAN_PORT_DEL
3475 * @returns SUCCESS or FAILURE
3476 */
3477static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3478 u16 vxlan_port, u8 vxlan_cmd_bit)
3479{
3480 struct lio *lio = GET_LIO(netdev);
3481 struct octeon_device *oct = lio->oct_dev;
3482 struct octnic_ctrl_pkt nctrl;
3483 int ret = 0;
3484
3485 nctrl.ncmd.u64 = 0;
3486 nctrl.ncmd.s.cmd = command;
3487 nctrl.ncmd.s.more = vxlan_cmd_bit;
3488 nctrl.ncmd.s.param1 = vxlan_port;
3489 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3490 nctrl.wait_time = 100;
3491 nctrl.netpndev = (u64)netdev;
3492 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3493
3494 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3495 if (ret < 0) {
3496 dev_err(&oct->pci_dev->dev,
3497 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3498 ret);
3499 }
3500 return ret;
3501}
3502
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003503/** \brief Net device fix features
3504 * @param netdev pointer to network device
3505 * @param request features requested
3506 * @returns updated features list
3507 */
3508static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3509 netdev_features_t request)
3510{
3511 struct lio *lio = netdev_priv(netdev);
3512
3513 if ((request & NETIF_F_RXCSUM) &&
3514 !(lio->dev_capability & NETIF_F_RXCSUM))
3515 request &= ~NETIF_F_RXCSUM;
3516
3517 if ((request & NETIF_F_HW_CSUM) &&
3518 !(lio->dev_capability & NETIF_F_HW_CSUM))
3519 request &= ~NETIF_F_HW_CSUM;
3520
3521 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3522 request &= ~NETIF_F_TSO;
3523
3524 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3525 request &= ~NETIF_F_TSO6;
3526
3527 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3528 request &= ~NETIF_F_LRO;
3529
3530 /*Disable LRO if RXCSUM is off */
3531 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3532 (lio->dev_capability & NETIF_F_LRO))
3533 request &= ~NETIF_F_LRO;
3534
3535 return request;
3536}
3537
3538/** \brief Net device set features
3539 * @param netdev pointer to network device
3540 * @param features features to enable/disable
3541 */
3542static int liquidio_set_features(struct net_device *netdev,
3543 netdev_features_t features)
3544{
3545 struct lio *lio = netdev_priv(netdev);
3546
3547 if (!((netdev->features ^ features) & NETIF_F_LRO))
3548 return 0;
3549
3550 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003551 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3552 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003553 else if (!(features & NETIF_F_LRO) &&
3554 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003555 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3556 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003557
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003558 /* Sending command to firmware to enable/disable RX checksum
3559 * offload settings using ethtool
3560 */
3561 if (!(netdev->features & NETIF_F_RXCSUM) &&
3562 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3563 (features & NETIF_F_RXCSUM))
3564 liquidio_set_rxcsum_command(netdev,
3565 OCTNET_CMD_TNL_RX_CSUM_CTL,
3566 OCTNET_CMD_RXCSUM_ENABLE);
3567 else if ((netdev->features & NETIF_F_RXCSUM) &&
3568 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3569 !(features & NETIF_F_RXCSUM))
3570 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3571 OCTNET_CMD_RXCSUM_DISABLE);
3572
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003573 return 0;
3574}
3575
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003576static void liquidio_add_vxlan_port(struct net_device *netdev,
3577 struct udp_tunnel_info *ti)
3578{
3579 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3580 return;
3581
3582 liquidio_vxlan_port_command(netdev,
3583 OCTNET_CMD_VXLAN_PORT_CONFIG,
3584 htons(ti->port),
3585 OCTNET_CMD_VXLAN_PORT_ADD);
3586}
3587
3588static void liquidio_del_vxlan_port(struct net_device *netdev,
3589 struct udp_tunnel_info *ti)
3590{
3591 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3592 return;
3593
3594 liquidio_vxlan_port_command(netdev,
3595 OCTNET_CMD_VXLAN_PORT_CONFIG,
3596 htons(ti->port),
3597 OCTNET_CMD_VXLAN_PORT_DEL);
3598}
3599
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003600static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3601 u8 *mac, bool is_admin_assigned)
3602{
3603 struct lio *lio = GET_LIO(netdev);
3604 struct octeon_device *oct = lio->oct_dev;
3605 struct octnic_ctrl_pkt nctrl;
3606
3607 if (!is_valid_ether_addr(mac))
3608 return -EINVAL;
3609
3610 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3611 return -EINVAL;
3612
3613 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3614
3615 nctrl.ncmd.u64 = 0;
3616 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3617 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3618 nctrl.ncmd.s.param1 = vfidx + 1;
3619 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3620 nctrl.ncmd.s.more = 1;
3621 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3622 nctrl.cb_fn = 0;
3623 nctrl.wait_time = LIO_CMD_WAIT_TM;
3624
3625 nctrl.udd[0] = 0;
3626 /* The MAC Address is presented in network byte order. */
3627 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3628
3629 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3630
3631 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3632
3633 return 0;
3634}
3635
3636static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3637{
3638 struct lio *lio = GET_LIO(netdev);
3639 struct octeon_device *oct = lio->oct_dev;
3640 int retval;
3641
3642 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3643 if (!retval)
3644 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3645
3646 return retval;
3647}
3648
3649static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3650 u16 vlan, u8 qos, __be16 vlan_proto)
3651{
3652 struct lio *lio = GET_LIO(netdev);
3653 struct octeon_device *oct = lio->oct_dev;
3654 struct octnic_ctrl_pkt nctrl;
3655 u16 vlantci;
3656
3657 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3658 return -EINVAL;
3659
3660 if (vlan_proto != htons(ETH_P_8021Q))
3661 return -EPROTONOSUPPORT;
3662
3663 if (vlan >= VLAN_N_VID || qos > 7)
3664 return -EINVAL;
3665
3666 if (vlan)
3667 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3668 else
3669 vlantci = 0;
3670
3671 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3672 return 0;
3673
3674 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3675
3676 if (vlan)
3677 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3678 else
3679 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3680
3681 nctrl.ncmd.s.param1 = vlantci;
3682 nctrl.ncmd.s.param2 =
3683 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3684 nctrl.ncmd.s.more = 0;
3685 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3686 nctrl.cb_fn = 0;
3687 nctrl.wait_time = LIO_CMD_WAIT_TM;
3688
3689 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3690
3691 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3692
3693 return 0;
3694}
3695
3696static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3697 struct ifla_vf_info *ivi)
3698{
3699 struct lio *lio = GET_LIO(netdev);
3700 struct octeon_device *oct = lio->oct_dev;
3701 u8 *macaddr;
3702
3703 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3704 return -EINVAL;
3705
3706 ivi->vf = vfidx;
3707 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3708 ether_addr_copy(&ivi->mac[0], macaddr);
3709 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3710 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3711 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3712 return 0;
3713}
3714
3715static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3716 int linkstate)
3717{
3718 struct lio *lio = GET_LIO(netdev);
3719 struct octeon_device *oct = lio->oct_dev;
3720 struct octnic_ctrl_pkt nctrl;
3721
3722 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3723 return -EINVAL;
3724
3725 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3726 return 0;
3727
3728 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3729 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3730 nctrl.ncmd.s.param1 =
3731 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3732 nctrl.ncmd.s.param2 = linkstate;
3733 nctrl.ncmd.s.more = 0;
3734 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3735 nctrl.cb_fn = 0;
3736 nctrl.wait_time = LIO_CMD_WAIT_TM;
3737
3738 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3739
3740 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3741
3742 return 0;
3743}
3744
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003745static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003746 .ndo_open = liquidio_open,
3747 .ndo_stop = liquidio_stop,
3748 .ndo_start_xmit = liquidio_xmit,
3749 .ndo_get_stats = liquidio_get_stats,
3750 .ndo_set_mac_address = liquidio_set_mac,
3751 .ndo_set_rx_mode = liquidio_set_mcast_list,
3752 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003753
3754 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3755 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003756 .ndo_change_mtu = liquidio_change_mtu,
3757 .ndo_do_ioctl = liquidio_ioctl,
3758 .ndo_fix_features = liquidio_fix_features,
3759 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003760 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3761 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003762 .ndo_set_vf_mac = liquidio_set_vf_mac,
3763 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3764 .ndo_get_vf_config = liquidio_get_vf_config,
3765 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003766};
3767
3768/** \brief Entry point for the liquidio module
3769 */
3770static int __init liquidio_init(void)
3771{
3772 int i;
3773 struct handshake *hs;
3774
3775 init_completion(&first_stage);
3776
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003777 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003778
3779 if (liquidio_init_pci())
3780 return -EINVAL;
3781
3782 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3783
3784 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3785 hs = &handshake[i];
3786 if (hs->pci_dev) {
3787 wait_for_completion(&hs->init);
3788 if (!hs->init_ok) {
3789 /* init handshake failed */
3790 dev_err(&hs->pci_dev->dev,
3791 "Failed to init device\n");
3792 liquidio_deinit_pci();
3793 return -EIO;
3794 }
3795 }
3796 }
3797
3798 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3799 hs = &handshake[i];
3800 if (hs->pci_dev) {
3801 wait_for_completion_timeout(&hs->started,
3802 msecs_to_jiffies(30000));
3803 if (!hs->started_ok) {
3804 /* starter handshake failed */
3805 dev_err(&hs->pci_dev->dev,
3806 "Firmware failed to start\n");
3807 liquidio_deinit_pci();
3808 return -EIO;
3809 }
3810 }
3811 }
3812
3813 return 0;
3814}
3815
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003816static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003817{
3818 struct octeon_device *oct = (struct octeon_device *)buf;
3819 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003820 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003821 union oct_link_status *ls;
3822 int i;
3823
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003824 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003825 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3826 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003827 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003828 goto nic_info_err;
3829 }
3830
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003831 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003832 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3833
3834 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003835 for (i = 0; i < oct->ifcount; i++) {
3836 if (oct->props[i].gmxport == gmxport) {
3837 update_link_status(oct->props[i].netdev, ls);
3838 break;
3839 }
3840 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003841
3842nic_info_err:
3843 for (i = 0; i < recv_pkt->buffer_count; i++)
3844 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3845 octeon_free_recv_info(recv_info);
3846 return 0;
3847}
3848
3849/**
3850 * \brief Setup network interfaces
3851 * @param octeon_dev octeon device
3852 *
3853 * Called during init time for each device. It assumes the NIC
3854 * is already up and running. The link information for each
3855 * interface is passed in link_info.
3856 */
3857static int setup_nic_devices(struct octeon_device *octeon_dev)
3858{
3859 struct lio *lio = NULL;
3860 struct net_device *netdev;
3861 u8 mac[6], i, j;
3862 struct octeon_soft_command *sc;
3863 struct liquidio_if_cfg_context *ctx;
3864 struct liquidio_if_cfg_resp *resp;
3865 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003866 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003867 union oct_nic_if_cfg if_cfg;
3868 unsigned int base_queue;
3869 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003870 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003871 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003872 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003873
3874 /* This is to handle link status changes */
3875 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3876 OPCODE_NIC_INFO,
3877 lio_nic_info, octeon_dev);
3878
3879 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3880 * They are handled directly.
3881 */
3882 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3883 free_netbuf);
3884
3885 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3886 free_netsgbuf);
3887
3888 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3889 free_netsgbuf_with_resp);
3890
3891 for (i = 0; i < octeon_dev->ifcount; i++) {
3892 resp_size = sizeof(struct liquidio_if_cfg_resp);
3893 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003894 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003895 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003896 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003897 resp_size, ctx_size);
3898 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3899 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003900 vdata = (struct lio_version *)sc->virtdptr;
3901
3902 *((u64 *)vdata) = 0;
3903 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3904 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3905 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003906
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003907 if (OCTEON_CN23XX_PF(octeon_dev)) {
3908 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3909 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3910 base_queue = octeon_dev->sriov_info.pf_srn;
3911
3912 gmx_port_id = octeon_dev->pf_num;
3913 ifidx_or_pfnum = octeon_dev->pf_num;
3914 } else {
3915 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3916 octeon_get_conf(octeon_dev), i);
3917 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3918 octeon_get_conf(octeon_dev), i);
3919 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3920 octeon_get_conf(octeon_dev), i);
3921 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3922 octeon_get_conf(octeon_dev), i);
3923 ifidx_or_pfnum = i;
3924 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003925
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003926 dev_dbg(&octeon_dev->pci_dev->dev,
3927 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003928 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003929 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003930 ctx->octeon_id = lio_get_device_id(octeon_dev);
3931 init_waitqueue_head(&ctx->wc);
3932
3933 if_cfg.u64 = 0;
3934 if_cfg.s.num_iqueues = num_iqueues;
3935 if_cfg.s.num_oqueues = num_oqueues;
3936 if_cfg.s.base_queue = base_queue;
3937 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003938
3939 sc->iq_no = 0;
3940
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003941 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003942 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003943 if_cfg.u64, 0);
3944
3945 sc->callback = if_cfg_callback;
3946 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003947 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003948
3949 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003950 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003951 dev_err(&octeon_dev->pci_dev->dev,
3952 "iq/oq config failed status: %x\n",
3953 retval);
3954 /* Soft instr is freed by driver in case of failure. */
3955 goto setup_nic_dev_fail;
3956 }
3957
3958 /* Sleep on a wait queue till the cond flag indicates that the
3959 * response arrived or timed-out.
3960 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003961 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3962 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3963 goto setup_nic_wait_intr;
3964 }
3965
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003966 retval = resp->status;
3967 if (retval) {
3968 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3969 goto setup_nic_dev_fail;
3970 }
3971
3972 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3973 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3974
3975 num_iqueues = hweight64(resp->cfg_info.iqmask);
3976 num_oqueues = hweight64(resp->cfg_info.oqmask);
3977
3978 if (!(num_iqueues) || !(num_oqueues)) {
3979 dev_err(&octeon_dev->pci_dev->dev,
3980 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3981 resp->cfg_info.iqmask,
3982 resp->cfg_info.oqmask);
3983 goto setup_nic_dev_fail;
3984 }
3985 dev_dbg(&octeon_dev->pci_dev->dev,
3986 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3987 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3988 num_iqueues, num_oqueues);
3989 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3990
3991 if (!netdev) {
3992 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3993 goto setup_nic_dev_fail;
3994 }
3995
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003996 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003997
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003998 /* Associate the routines that will handle different
3999 * netdev tasks.
4000 */
4001 netdev->netdev_ops = &lionetdevops;
4002
4003 lio = GET_LIO(netdev);
4004
4005 memset(lio, 0, sizeof(struct lio));
4006
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004007 lio->ifidx = ifidx_or_pfnum;
4008
4009 props = &octeon_dev->props[i];
4010 props->gmxport = resp->cfg_info.linfo.gmxport;
4011 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004012
4013 lio->linfo.num_rxpciq = num_oqueues;
4014 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004015 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004016 lio->linfo.rxpciq[j].u64 =
4017 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004018 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004019 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004020 lio->linfo.txpciq[j].u64 =
4021 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004022 }
4023 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4024 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4025 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4026
4027 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4028
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004029 if (OCTEON_CN23XX_PF(octeon_dev) ||
4030 OCTEON_CN6XXX(octeon_dev)) {
4031 lio->dev_capability = NETIF_F_HIGHDMA
4032 | NETIF_F_IP_CSUM
4033 | NETIF_F_IPV6_CSUM
4034 | NETIF_F_SG | NETIF_F_RXCSUM
4035 | NETIF_F_GRO
4036 | NETIF_F_TSO | NETIF_F_TSO6
4037 | NETIF_F_LRO;
4038 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004039 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4040
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004041 /* Copy of transmit encapsulation capabilities:
4042 * TSO, TSO6, Checksums for this device
4043 */
4044 lio->enc_dev_capability = NETIF_F_IP_CSUM
4045 | NETIF_F_IPV6_CSUM
4046 | NETIF_F_GSO_UDP_TUNNEL
4047 | NETIF_F_HW_CSUM | NETIF_F_SG
4048 | NETIF_F_RXCSUM
4049 | NETIF_F_TSO | NETIF_F_TSO6
4050 | NETIF_F_LRO;
4051
4052 netdev->hw_enc_features = (lio->enc_dev_capability &
4053 ~NETIF_F_LRO);
4054
4055 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4056
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004057 netdev->vlan_features = lio->dev_capability;
4058 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004059 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4060 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004061 NETIF_F_HW_VLAN_CTAG_TX;
4062
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004063 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4064
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004065 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004066 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4067 netdev->hw_features = netdev->hw_features &
4068 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004069
Jarod Wilson109cc162016-10-17 15:54:13 -04004070 /* MTU range: 68 - 16000 */
4071 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4072 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4073
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004074 /* Point to the properties for octeon device to which this
4075 * interface belongs.
4076 */
4077 lio->oct_dev = octeon_dev;
4078 lio->octprops = props;
4079 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004080
4081 dev_dbg(&octeon_dev->pci_dev->dev,
4082 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4083 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4084
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004085 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4086 u8 vfmac[ETH_ALEN];
4087
4088 random_ether_addr(&vfmac[0]);
4089 if (__liquidio_set_vf_mac(netdev, j,
4090 &vfmac[0], false)) {
4091 dev_err(&octeon_dev->pci_dev->dev,
4092 "Error setting VF%d MAC address\n",
4093 j);
4094 goto setup_nic_dev_fail;
4095 }
4096 }
4097
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004098 /* 64-bit swap required on LE machines */
4099 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4100 for (j = 0; j < 6; j++)
4101 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4102
4103 /* Copy MAC Address to OS network device structure */
4104
4105 ether_addr_copy(netdev->dev_addr, mac);
4106
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004107 /* By default all interfaces on a single Octeon uses the same
4108 * tx and rx queues
4109 */
4110 lio->txq = lio->linfo.txpciq[0].s.q_no;
4111 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004112 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004113 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4114 goto setup_nic_dev_fail;
4115 }
4116
4117 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4118
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004119 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4120 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4121
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004122 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004123 dev_err(&octeon_dev->pci_dev->dev,
4124 "Gather list allocation failed\n");
4125 goto setup_nic_dev_fail;
4126 }
4127
4128 /* Register ethtool support */
4129 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004130 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4131 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4132 else
4133 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004134
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004135 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004136 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4137 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004138
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004139 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4140
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004141 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004142 liquidio_set_feature(netdev,
4143 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004144
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004145 if (setup_link_status_change_wq(netdev))
4146 goto setup_nic_dev_fail;
4147
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004148 /* Register the network device with the OS */
4149 if (register_netdev(netdev)) {
4150 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4151 goto setup_nic_dev_fail;
4152 }
4153
4154 dev_dbg(&octeon_dev->pci_dev->dev,
4155 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4156 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4157 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004158 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004159
4160 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4161
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004162 /* Sending command to firmware to enable Rx checksum offload
4163 * by default at the time of setup of Liquidio driver for
4164 * this device
4165 */
4166 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4167 OCTNET_CMD_RXCSUM_ENABLE);
4168 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4169 OCTNET_CMD_TXCSUM_ENABLE);
4170
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004171 dev_dbg(&octeon_dev->pci_dev->dev,
4172 "NIC ifidx:%d Setup successful\n", i);
4173
4174 octeon_free_soft_command(octeon_dev, sc);
4175 }
4176
4177 return 0;
4178
4179setup_nic_dev_fail:
4180
4181 octeon_free_soft_command(octeon_dev, sc);
4182
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004183setup_nic_wait_intr:
4184
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004185 while (i--) {
4186 dev_err(&octeon_dev->pci_dev->dev,
4187 "NIC ifidx:%d Setup failed\n", i);
4188 liquidio_destroy_nic_device(octeon_dev, i);
4189 }
4190 return -ENODEV;
4191}
4192
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004193#ifdef CONFIG_PCI_IOV
4194static int octeon_enable_sriov(struct octeon_device *oct)
4195{
4196 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4197 struct pci_dev *vfdev;
4198 int err;
4199 u32 u;
4200
4201 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4202 err = pci_enable_sriov(oct->pci_dev,
4203 oct->sriov_info.num_vfs_alloced);
4204 if (err) {
4205 dev_err(&oct->pci_dev->dev,
4206 "OCTEON: Failed to enable PCI sriov: %d\n",
4207 err);
4208 oct->sriov_info.num_vfs_alloced = 0;
4209 return err;
4210 }
4211 oct->sriov_info.sriov_enabled = 1;
4212
4213 /* init lookup table that maps DPI ring number to VF pci_dev
4214 * struct pointer
4215 */
4216 u = 0;
4217 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4218 OCTEON_CN23XX_VF_VID, NULL);
4219 while (vfdev) {
4220 if (vfdev->is_virtfn &&
4221 (vfdev->physfn == oct->pci_dev)) {
4222 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4223 vfdev;
4224 u += oct->sriov_info.rings_per_vf;
4225 }
4226 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4227 OCTEON_CN23XX_VF_VID, vfdev);
4228 }
4229 }
4230
4231 return num_vfs_alloced;
4232}
4233
4234static int lio_pci_sriov_disable(struct octeon_device *oct)
4235{
4236 int u;
4237
4238 if (pci_vfs_assigned(oct->pci_dev)) {
4239 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4240 return -EPERM;
4241 }
4242
4243 pci_disable_sriov(oct->pci_dev);
4244
4245 u = 0;
4246 while (u < MAX_POSSIBLE_VFS) {
4247 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4248 u += oct->sriov_info.rings_per_vf;
4249 }
4250
4251 oct->sriov_info.num_vfs_alloced = 0;
4252 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4253 oct->pf_num);
4254
4255 return 0;
4256}
4257
4258static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4259{
4260 struct octeon_device *oct = pci_get_drvdata(dev);
4261 int ret = 0;
4262
4263 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4264 (oct->sriov_info.sriov_enabled)) {
4265 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4266 oct->pf_num, num_vfs);
4267 return 0;
4268 }
4269
4270 if (!num_vfs) {
4271 ret = lio_pci_sriov_disable(oct);
4272 } else if (num_vfs > oct->sriov_info.max_vfs) {
4273 dev_err(&oct->pci_dev->dev,
4274 "OCTEON: Max allowed VFs:%d user requested:%d",
4275 oct->sriov_info.max_vfs, num_vfs);
4276 ret = -EPERM;
4277 } else {
4278 oct->sriov_info.num_vfs_alloced = num_vfs;
4279 ret = octeon_enable_sriov(oct);
4280 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4281 oct->pf_num, num_vfs);
4282 }
4283
4284 return ret;
4285}
4286#endif
4287
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004288/**
4289 * \brief initialize the NIC
4290 * @param oct octeon device
4291 *
4292 * This initialization routine is called once the Octeon device application is
4293 * up and running
4294 */
4295static int liquidio_init_nic_module(struct octeon_device *oct)
4296{
4297 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004298 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004299 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4300
4301 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4302
4303 /* only default iq and oq were initialized
4304 * initialize the rest as well
4305 */
4306 /* run port_config command for each port */
4307 oct->ifcount = num_nic_ports;
4308
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004309 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004310
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004311 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4312 oct->props[i].gmxport = -1;
4313
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004314 retval = setup_nic_devices(oct);
4315 if (retval) {
4316 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4317 goto octnet_init_failure;
4318 }
4319
4320 liquidio_ptp_init(oct);
4321
4322 /* Initialize interrupt moderation params */
4323 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004324 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004325 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004326 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4327 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4328 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4329 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4330 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4331 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4332 intrmod_cfg->tx_enable = 1;
4333 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4334 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4335 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4336 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004337 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004338 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4339
4340 return retval;
4341
4342octnet_init_failure:
4343
4344 oct->ifcount = 0;
4345
4346 return retval;
4347}
4348
4349/**
4350 * \brief starter callback that invokes the remaining initialization work after
4351 * the NIC is up and running.
4352 * @param octptr work struct work_struct
4353 */
4354static void nic_starter(struct work_struct *work)
4355{
4356 struct octeon_device *oct;
4357 struct cavium_wk *wk = (struct cavium_wk *)work;
4358
4359 oct = (struct octeon_device *)wk->ctxptr;
4360
4361 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4362 return;
4363
4364 /* If the status of the device is CORE_OK, the core
4365 * application has reported its application type. Call
4366 * any registered handlers now and move to the RUNNING
4367 * state.
4368 */
4369 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4370 schedule_delayed_work(&oct->nic_poll_work.work,
4371 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4372 return;
4373 }
4374
4375 atomic_set(&oct->status, OCT_DEV_RUNNING);
4376
4377 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4378 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4379
4380 if (liquidio_init_nic_module(oct))
4381 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4382 else
4383 handshake[oct->octeon_id].started_ok = 1;
4384 } else {
4385 dev_err(&oct->pci_dev->dev,
4386 "Unexpected application running on NIC (%d). Check firmware.\n",
4387 oct->app_mode);
4388 }
4389
4390 complete(&handshake[oct->octeon_id].started);
4391}
4392
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004393static int
4394octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4395{
4396 struct octeon_device *oct = (struct octeon_device *)buf;
4397 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4398 int i, notice, vf_idx;
4399 u64 *data, vf_num;
4400
4401 notice = recv_pkt->rh.r.ossp;
4402 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4403
4404 /* the first 64-bit word of data is the vf_num */
4405 vf_num = data[0];
4406 octeon_swap_8B_data(&vf_num, 1);
4407 vf_idx = (int)vf_num - 1;
4408
4409 if (notice == VF_DRV_LOADED) {
4410 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4411 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4412 dev_info(&oct->pci_dev->dev,
4413 "driver for VF%d was loaded\n", vf_idx);
4414 try_module_get(THIS_MODULE);
4415 }
4416 } else if (notice == VF_DRV_REMOVED) {
4417 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4418 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4419 dev_info(&oct->pci_dev->dev,
4420 "driver for VF%d was removed\n", vf_idx);
4421 module_put(THIS_MODULE);
4422 }
4423 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4424 u8 *b = (u8 *)&data[1];
4425
4426 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4427 dev_info(&oct->pci_dev->dev,
4428 "VF driver changed VF%d's MAC address to %pM\n",
4429 vf_idx, b + 2);
4430 }
4431
4432 for (i = 0; i < recv_pkt->buffer_count; i++)
4433 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4434 octeon_free_recv_info(recv_info);
4435
4436 return 0;
4437}
4438
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004439/**
4440 * \brief Device initialization for each Octeon device that is probed
4441 * @param octeon_dev octeon device
4442 */
4443static int octeon_device_init(struct octeon_device *octeon_dev)
4444{
4445 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004446 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004447 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004448 struct octeon_device_priv *oct_priv =
4449 (struct octeon_device_priv *)octeon_dev->priv;
4450 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4451
4452 /* Enable access to the octeon device and make its DMA capability
4453 * known to the OS.
4454 */
4455 if (octeon_pci_os_setup(octeon_dev))
4456 return 1;
4457
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004458 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4459
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004460 /* Identify the Octeon type and map the BAR address space. */
4461 if (octeon_chip_specific_setup(octeon_dev)) {
4462 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4463 return 1;
4464 }
4465
4466 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4467
4468 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4469
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004470 if (OCTEON_CN23XX_PF(octeon_dev)) {
4471 if (!cn23xx_fw_loaded(octeon_dev)) {
4472 fw_loaded = 0;
4473 /* Do a soft reset of the Octeon device. */
4474 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4475 return 1;
4476 /* things might have changed */
4477 if (!cn23xx_fw_loaded(octeon_dev))
4478 fw_loaded = 0;
4479 else
4480 fw_loaded = 1;
4481 } else {
4482 fw_loaded = 1;
4483 }
4484 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004485 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004486 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004487
4488 /* Initialize the dispatch mechanism used to push packets arriving on
4489 * Octeon Output queues.
4490 */
4491 if (octeon_init_dispatch_list(octeon_dev))
4492 return 1;
4493
4494 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4495 OPCODE_NIC_CORE_DRV_ACTIVE,
4496 octeon_core_drv_init,
4497 octeon_dev);
4498
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004499 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4500 OPCODE_NIC_VF_DRV_NOTICE,
4501 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004502 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4503 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4504 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4505 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4506
4507 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4508
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004509 if (octeon_set_io_queues_off(octeon_dev)) {
4510 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4511 return 1;
4512 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004513
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004514 if (OCTEON_CN23XX_PF(octeon_dev)) {
4515 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4516 if (ret) {
4517 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4518 return ret;
4519 }
4520 }
4521
4522 /* Initialize soft command buffer pool
4523 */
4524 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4525 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4526 return 1;
4527 }
4528 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4529
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004530 /* Setup the data structures that manage this Octeon's Input queues. */
4531 if (octeon_setup_instr_queues(octeon_dev)) {
4532 dev_err(&octeon_dev->pci_dev->dev,
4533 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004534 return 1;
4535 }
4536 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4537
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004538 /* Initialize lists to manage the requests of different types that
4539 * arrive from user & kernel applications for this octeon device.
4540 */
4541 if (octeon_setup_response_list(octeon_dev)) {
4542 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4543 return 1;
4544 }
4545 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4546
4547 if (octeon_setup_output_queues(octeon_dev)) {
4548 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004549 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004550 }
4551
4552 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4553
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004554 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004555 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4556 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4557 return 1;
4558 }
4559 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4560
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004561 if (octeon_allocate_ioq_vector(octeon_dev)) {
4562 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4563 return 1;
4564 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004565 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004566
4567 } else {
4568 /* The input and output queue registers were setup earlier (the
4569 * queues were not enabled). Any additional registers
4570 * that need to be programmed should be done now.
4571 */
4572 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4573 if (ret) {
4574 dev_err(&octeon_dev->pci_dev->dev,
4575 "Failed to configure device registers\n");
4576 return ret;
4577 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004578 }
4579
4580 /* Initialize the tasklet that handles output queue packet processing.*/
4581 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4582 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4583 (unsigned long)octeon_dev);
4584
4585 /* Setup the interrupt handler and record the INT SUM register address
4586 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004587 if (octeon_setup_interrupt(octeon_dev))
4588 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004589
4590 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004591 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004592
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004593 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4594
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004595 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004596 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4597 if (ret) {
4598 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4599 return ret;
4600 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004601
4602 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4603
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004604 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4605 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4606 if (!ddr_timeout) {
4607 dev_info(&octeon_dev->pci_dev->dev,
4608 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4609 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004610
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004611 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004612
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004613 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4614 while (!ddr_timeout) {
4615 set_current_state(TASK_INTERRUPTIBLE);
4616 if (schedule_timeout(HZ / 10)) {
4617 /* user probably pressed Control-C */
4618 return 1;
4619 }
4620 }
4621 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4622 if (ret) {
4623 dev_err(&octeon_dev->pci_dev->dev,
4624 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4625 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004626 return 1;
4627 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004628
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004629 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4630 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4631 return 1;
4632 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004633
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004634 /* Divert uboot to take commands from host instead. */
4635 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004636
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004637 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4638 ret = octeon_init_consoles(octeon_dev);
4639 if (ret) {
4640 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4641 return 1;
4642 }
4643 ret = octeon_add_console(octeon_dev, 0);
4644 if (ret) {
4645 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4646 return 1;
4647 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004648
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004649 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004650
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004651 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4652 ret = load_firmware(octeon_dev);
4653 if (ret) {
4654 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4655 return 1;
4656 }
4657 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4658 * loaded
4659 */
4660 if (OCTEON_CN23XX_PF(octeon_dev))
4661 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4662 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004663 }
4664
4665 handshake[octeon_dev->octeon_id].init_ok = 1;
4666 complete(&handshake[octeon_dev->octeon_id].init);
4667
4668 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4669
4670 /* Send Credit for Octeon Output queues. Credits are always sent after
4671 * the output queue is enabled.
4672 */
4673 for (j = 0; j < octeon_dev->num_oqs; j++)
4674 writel(octeon_dev->droq[j]->max_count,
4675 octeon_dev->droq[j]->pkts_credit_reg);
4676
4677 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004678 return 0;
4679}
4680
4681/**
4682 * \brief Exits the module
4683 */
4684static void __exit liquidio_exit(void)
4685{
4686 liquidio_deinit_pci();
4687
4688 pr_info("LiquidIO network module is now unloaded\n");
4689}
4690
4691module_init(liquidio_init);
4692module_exit(liquidio_exit);