blob: e20632360a064e4f8dddab8180c224d6698f7258 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2* Author: Cavium, Inc.
3*
4* Contact: support@cavium.com
5* Please include "LiquidIO" in the subject.
6*
7* Copyright (c) 2003-2015 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
17* details.
18*
19* This file may also be available under a different license from Cavium.
20* Contact Cavium, Inc. for more information
21**********************************************************************/
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070022#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070024#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070025#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070026#include "liquidio_common.h"
27#include "octeon_droq.h"
28#include "octeon_iq.h"
29#include "response_manager.h"
30#include "octeon_device.h"
31#include "octeon_nic.h"
32#include "octeon_main.h"
33#include "octeon_network.h"
34#include "cn66xx_regs.h"
35#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070036#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070037#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070038#include "liquidio_image.h"
39
40MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
41MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
42MODULE_LICENSE("GPL");
43MODULE_VERSION(LIQUIDIO_VERSION);
44MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
45MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
47
48static int ddr_timeout = 10000;
49module_param(ddr_timeout, int, 0644);
50MODULE_PARM_DESC(ddr_timeout,
51 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
52
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070053#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
54
Raghu Vatsavayi1f164712016-06-21 22:53:11 -070055#define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \
56 (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count)
57
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070058static int debug = -1;
59module_param(debug, int, 0644);
60MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
61
62static char fw_type[LIO_MAX_FW_TYPE_LEN];
63module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
64MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
65
66static int conf_type;
67module_param(conf_type, int, 0);
68MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
69
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070070static int ptp_enable = 1;
71
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070072/* Bit mask values for lio->ifstate */
73#define LIO_IFSTATE_DROQ_OPS 0x01
74#define LIO_IFSTATE_REGISTERED 0x02
75#define LIO_IFSTATE_RUNNING 0x04
76#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
77
78/* Polling interval for determining when NIC application is alive */
79#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
80
81/* runtime link query interval */
82#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
83
84struct liquidio_if_cfg_context {
85 int octeon_id;
86
87 wait_queue_head_t wc;
88
89 int cond;
90};
91
92struct liquidio_if_cfg_resp {
93 u64 rh;
94 struct liquidio_if_cfg_info cfg_info;
95 u64 status;
96};
97
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -070098struct liquidio_rx_ctl_context {
99 int octeon_id;
100
101 wait_queue_head_t wc;
102
103 int cond;
104};
105
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700106struct oct_link_status_resp {
107 u64 rh;
108 struct oct_link_info link_info;
109 u64 status;
110};
111
112struct oct_timestamp_resp {
113 u64 rh;
114 u64 timestamp;
115 u64 status;
116};
117
118#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
119
120union tx_info {
121 u64 u64;
122 struct {
123#ifdef __BIG_ENDIAN_BITFIELD
124 u16 gso_size;
125 u16 gso_segs;
126 u32 reserved;
127#else
128 u32 reserved;
129 u16 gso_segs;
130 u16 gso_size;
131#endif
132 } s;
133};
134
135/** Octeon device properties to be used by the NIC module.
136 * Each octeon device in the system will be represented
137 * by this structure in the NIC module.
138 */
139
140#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
141
142#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700143#define OCTNIC_GSO_MAX_SIZE \
144 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700145
146/** Structure of a node in list of gather components maintained by
147 * NIC driver for each network device.
148 */
149struct octnic_gather {
150 /** List manipulation. Next and prev pointers. */
151 struct list_head list;
152
153 /** Size of the gather component at sg in bytes. */
154 int sg_size;
155
156 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
157 int adjust;
158
159 /** Gather component that can accommodate max sized fragment list
160 * received from the IP layer.
161 */
162 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700163
164 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700165};
166
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700167struct handshake {
168 struct completion init;
169 struct completion started;
170 struct pci_dev *pci_dev;
171 int init_ok;
172 int started_ok;
173};
174
175struct octeon_device_priv {
176 /** Tasklet structures for this device. */
177 struct tasklet_struct droq_tasklet;
178 unsigned long napi_mask;
179};
180
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800181#ifdef CONFIG_PCI_IOV
182static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
183#endif
184
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700185static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700186static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700187static void liquidio_remove(struct pci_dev *pdev);
188static int liquidio_probe(struct pci_dev *pdev,
189 const struct pci_device_id *ent);
190
191static struct handshake handshake[MAX_OCTEON_DEVICES];
192static struct completion first_stage;
193
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700194static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700195{
196 int q_no;
197 int reschedule = 0;
198 struct octeon_device *oct = (struct octeon_device *)pdev;
199 struct octeon_device_priv *oct_priv =
200 (struct octeon_device_priv *)oct->priv;
201
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700202 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800203 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700204 continue;
205 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
206 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700207 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700208
209 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
210 /* set time and cnt interrupt thresholds for this DROQ
211 * for NAPI
212 */
213 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
214
215 octeon_write_csr64(
216 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
217 0x5700000040ULL);
218 octeon_write_csr64(
219 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
220 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700221 }
222
223 if (reschedule)
224 tasklet_schedule(&oct_priv->droq_tasklet);
225}
226
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700227static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700228{
229 struct octeon_device_priv *oct_priv =
230 (struct octeon_device_priv *)oct->priv;
231 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
232 int i;
233
234 do {
235 pending_pkts = 0;
236
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700237 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800238 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700239 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700240 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700241 }
242 if (pkt_cnt > 0) {
243 pending_pkts += pkt_cnt;
244 tasklet_schedule(&oct_priv->droq_tasklet);
245 }
246 pkt_cnt = 0;
247 schedule_timeout_uninterruptible(1);
248
249 } while (retry-- && pending_pkts);
250
251 return pkt_cnt;
252}
253
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700254/**
255 * \brief Forces all IO queues off on a given device
256 * @param oct Pointer to Octeon device
257 */
258static void force_io_queues_off(struct octeon_device *oct)
259{
260 if ((oct->chip_id == OCTEON_CN66XX) ||
261 (oct->chip_id == OCTEON_CN68XX)) {
262 /* Reset the Enable bits for Input Queues. */
263 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
264
265 /* Reset the Enable bits for Output Queues. */
266 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
267 }
268}
269
270/**
271 * \brief wait for all pending requests to complete
272 * @param oct Pointer to Octeon device
273 *
274 * Called during shutdown sequence
275 */
276static int wait_for_pending_requests(struct octeon_device *oct)
277{
278 int i, pcount = 0;
279
280 for (i = 0; i < 100; i++) {
281 pcount =
282 atomic_read(&oct->response_list
283 [OCTEON_ORDERED_SC_LIST].pending_req_count);
284 if (pcount)
285 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700286 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700287 break;
288 }
289
290 if (pcount)
291 return 1;
292
293 return 0;
294}
295
296/**
297 * \brief Cause device to go quiet so it can be safely removed/reset/etc
298 * @param oct Pointer to Octeon device
299 */
300static inline void pcierror_quiesce_device(struct octeon_device *oct)
301{
302 int i;
303
304 /* Disable the input and output queues now. No more packets will
305 * arrive from Octeon, but we should wait for all packet processing
306 * to finish.
307 */
308 force_io_queues_off(oct);
309
310 /* To allow for in-flight requests */
311 schedule_timeout_uninterruptible(100);
312
313 if (wait_for_pending_requests(oct))
314 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
315
316 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700317 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700318 struct octeon_instr_queue *iq;
319
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800320 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700321 continue;
322 iq = oct->instr_queue[i];
323
324 if (atomic_read(&iq->instr_pending)) {
325 spin_lock_bh(&iq->lock);
326 iq->fill_cnt = 0;
327 iq->octeon_read_index = iq->host_write_index;
328 iq->stats.instr_processed +=
329 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700330 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700331 spin_unlock_bh(&iq->lock);
332 }
333 }
334
335 /* Force all pending ordered list requests to time out. */
336 lio_process_ordered_list(oct, 1);
337
338 /* We do not need to wait for output queue packets to be processed. */
339}
340
341/**
342 * \brief Cleanup PCI AER uncorrectable error status
343 * @param dev Pointer to PCI device
344 */
345static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
346{
347 int pos = 0x100;
348 u32 status, mask;
349
350 pr_info("%s :\n", __func__);
351
352 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
353 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
354 if (dev->error_state == pci_channel_io_normal)
355 status &= ~mask; /* Clear corresponding nonfatal bits */
356 else
357 status &= mask; /* Clear corresponding fatal bits */
358 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
359}
360
361/**
362 * \brief Stop all PCI IO to a given device
363 * @param dev Pointer to Octeon device
364 */
365static void stop_pci_io(struct octeon_device *oct)
366{
367 /* No more instructions will be forwarded. */
368 atomic_set(&oct->status, OCT_DEV_IN_RESET);
369
370 pci_disable_device(oct->pci_dev);
371
372 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700373 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700374
375 pcierror_quiesce_device(oct);
376
377 /* Release the interrupt line */
378 free_irq(oct->pci_dev->irq, oct);
379
380 if (oct->flags & LIO_FLAG_MSI_ENABLED)
381 pci_disable_msi(oct->pci_dev);
382
383 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
384 lio_get_state_string(&oct->status));
385
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700386 /* making it a common function for all OCTEON models */
387 cleanup_aer_uncorrect_error_status(oct->pci_dev);
388}
389
390/**
391 * \brief called when PCI error is detected
392 * @param pdev Pointer to PCI device
393 * @param state The current pci connection state
394 *
395 * This function is called after a PCI bus error affecting
396 * this device has been detected.
397 */
398static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
399 pci_channel_state_t state)
400{
401 struct octeon_device *oct = pci_get_drvdata(pdev);
402
403 /* Non-correctable Non-fatal errors */
404 if (state == pci_channel_io_normal) {
405 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
406 cleanup_aer_uncorrect_error_status(oct->pci_dev);
407 return PCI_ERS_RESULT_CAN_RECOVER;
408 }
409
410 /* Non-correctable Fatal errors */
411 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
412 stop_pci_io(oct);
413
414 /* Always return a DISCONNECT. There is no support for recovery but only
415 * for a clean shutdown.
416 */
417 return PCI_ERS_RESULT_DISCONNECT;
418}
419
420/**
421 * \brief mmio handler
422 * @param pdev Pointer to PCI device
423 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700424static pci_ers_result_t liquidio_pcie_mmio_enabled(
425 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700426{
427 /* We should never hit this since we never ask for a reset for a Fatal
428 * Error. We always return DISCONNECT in io_error above.
429 * But play safe and return RECOVERED for now.
430 */
431 return PCI_ERS_RESULT_RECOVERED;
432}
433
434/**
435 * \brief called after the pci bus has been reset.
436 * @param pdev Pointer to PCI device
437 *
438 * Restart the card from scratch, as if from a cold-boot. Implementation
439 * resembles the first-half of the octeon_resume routine.
440 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700441static pci_ers_result_t liquidio_pcie_slot_reset(
442 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700443{
444 /* We should never hit this since we never ask for a reset for a Fatal
445 * Error. We always return DISCONNECT in io_error above.
446 * But play safe and return RECOVERED for now.
447 */
448 return PCI_ERS_RESULT_RECOVERED;
449}
450
451/**
452 * \brief called when traffic can start flowing again.
453 * @param pdev Pointer to PCI device
454 *
455 * This callback is called when the error recovery driver tells us that
456 * its OK to resume normal operation. Implementation resembles the
457 * second-half of the octeon_resume routine.
458 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700459static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700460{
461 /* Nothing to be done here. */
462}
463
464#ifdef CONFIG_PM
465/**
466 * \brief called when suspending
467 * @param pdev Pointer to PCI device
468 * @param state state to suspend to
469 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700470static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
471 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700472{
473 return 0;
474}
475
476/**
477 * \brief called when resuming
478 * @param pdev Pointer to PCI device
479 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700480static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700481{
482 return 0;
483}
484#endif
485
486/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100487static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700488 .error_detected = liquidio_pcie_error_detected,
489 .mmio_enabled = liquidio_pcie_mmio_enabled,
490 .slot_reset = liquidio_pcie_slot_reset,
491 .resume = liquidio_pcie_resume,
492};
493
494static const struct pci_device_id liquidio_pci_tbl[] = {
495 { /* 68xx */
496 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
497 },
498 { /* 66xx */
499 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
500 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700501 { /* 23xx pf */
502 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
503 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700504 {
505 0, 0, 0, 0, 0, 0, 0
506 }
507};
508MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
509
510static struct pci_driver liquidio_pci_driver = {
511 .name = "LiquidIO",
512 .id_table = liquidio_pci_tbl,
513 .probe = liquidio_probe,
514 .remove = liquidio_remove,
515 .err_handler = &liquidio_err_handler, /* For AER */
516
517#ifdef CONFIG_PM
518 .suspend = liquidio_suspend,
519 .resume = liquidio_resume,
520#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800521#ifdef CONFIG_PCI_IOV
522 .sriov_configure = liquidio_enable_sriov,
523#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700524};
525
526/**
527 * \brief register PCI driver
528 */
529static int liquidio_init_pci(void)
530{
531 return pci_register_driver(&liquidio_pci_driver);
532}
533
534/**
535 * \brief unregister PCI driver
536 */
537static void liquidio_deinit_pci(void)
538{
539 pci_unregister_driver(&liquidio_pci_driver);
540}
541
542/**
543 * \brief check interface state
544 * @param lio per-network private data
545 * @param state_flag flag state to check
546 */
547static inline int ifstate_check(struct lio *lio, int state_flag)
548{
549 return atomic_read(&lio->ifstate) & state_flag;
550}
551
552/**
553 * \brief set interface state
554 * @param lio per-network private data
555 * @param state_flag flag state to set
556 */
557static inline void ifstate_set(struct lio *lio, int state_flag)
558{
559 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
560}
561
562/**
563 * \brief clear interface state
564 * @param lio per-network private data
565 * @param state_flag flag state to clear
566 */
567static inline void ifstate_reset(struct lio *lio, int state_flag)
568{
569 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
570}
571
572/**
573 * \brief Stop Tx queues
574 * @param netdev network device
575 */
576static inline void txqs_stop(struct net_device *netdev)
577{
578 if (netif_is_multiqueue(netdev)) {
579 int i;
580
581 for (i = 0; i < netdev->num_tx_queues; i++)
582 netif_stop_subqueue(netdev, i);
583 } else {
584 netif_stop_queue(netdev);
585 }
586}
587
588/**
589 * \brief Start Tx queues
590 * @param netdev network device
591 */
592static inline void txqs_start(struct net_device *netdev)
593{
594 if (netif_is_multiqueue(netdev)) {
595 int i;
596
597 for (i = 0; i < netdev->num_tx_queues; i++)
598 netif_start_subqueue(netdev, i);
599 } else {
600 netif_start_queue(netdev);
601 }
602}
603
604/**
605 * \brief Wake Tx queues
606 * @param netdev network device
607 */
608static inline void txqs_wake(struct net_device *netdev)
609{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700610 struct lio *lio = GET_LIO(netdev);
611
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700612 if (netif_is_multiqueue(netdev)) {
613 int i;
614
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700615 for (i = 0; i < netdev->num_tx_queues; i++) {
616 int qno = lio->linfo.txpciq[i %
617 (lio->linfo.num_txpciq)].s.q_no;
618
619 if (__netif_subqueue_stopped(netdev, i)) {
620 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
621 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700622 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700623 }
624 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700625 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700626 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
627 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700628 netif_wake_queue(netdev);
629 }
630}
631
632/**
633 * \brief Stop Tx queue
634 * @param netdev network device
635 */
636static void stop_txq(struct net_device *netdev)
637{
638 txqs_stop(netdev);
639}
640
641/**
642 * \brief Start Tx queue
643 * @param netdev network device
644 */
645static void start_txq(struct net_device *netdev)
646{
647 struct lio *lio = GET_LIO(netdev);
648
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700649 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700650 txqs_start(netdev);
651 return;
652 }
653}
654
655/**
656 * \brief Wake a queue
657 * @param netdev network device
658 * @param q which queue to wake
659 */
660static inline void wake_q(struct net_device *netdev, int q)
661{
662 if (netif_is_multiqueue(netdev))
663 netif_wake_subqueue(netdev, q);
664 else
665 netif_wake_queue(netdev);
666}
667
668/**
669 * \brief Stop a queue
670 * @param netdev network device
671 * @param q which queue to stop
672 */
673static inline void stop_q(struct net_device *netdev, int q)
674{
675 if (netif_is_multiqueue(netdev))
676 netif_stop_subqueue(netdev, q);
677 else
678 netif_stop_queue(netdev);
679}
680
681/**
682 * \brief Check Tx queue status, and take appropriate action
683 * @param lio per-network private data
684 * @returns 0 if full, number of queues woken up otherwise
685 */
686static inline int check_txq_status(struct lio *lio)
687{
688 int ret_val = 0;
689
690 if (netif_is_multiqueue(lio->netdev)) {
691 int numqs = lio->netdev->num_tx_queues;
692 int q, iq = 0;
693
694 /* check each sub-queue state */
695 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700696 iq = lio->linfo.txpciq[q %
697 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700698 if (octnet_iq_is_full(lio->oct_dev, iq))
699 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700700 if (__netif_subqueue_stopped(lio->netdev, q)) {
701 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700702 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
703 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700704 ret_val++;
705 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700706 }
707 } else {
708 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
709 return 0;
710 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700711 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
712 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700713 ret_val = 1;
714 }
715 return ret_val;
716}
717
718/**
719 * Remove the node at the head of the list. The list would be empty at
720 * the end of this call if there are no more nodes in the list.
721 */
722static inline struct list_head *list_delete_head(struct list_head *root)
723{
724 struct list_head *node;
725
726 if ((root->prev == root) && (root->next == root))
727 node = NULL;
728 else
729 node = root->next;
730
731 if (node)
732 list_del(node);
733
734 return node;
735}
736
737/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700738 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700739 * @param lio per-network private data
740 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700741static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700742{
743 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700744 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700745
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700746 if (!lio->glist)
747 return;
748
749 for (i = 0; i < lio->linfo.num_txpciq; i++) {
750 do {
751 g = (struct octnic_gather *)
752 list_delete_head(&lio->glist[i]);
753 if (g) {
754 if (g->sg) {
755 dma_unmap_single(&lio->oct_dev->
756 pci_dev->dev,
757 g->sg_dma_ptr,
758 g->sg_size,
759 DMA_TO_DEVICE);
760 kfree((void *)((unsigned long)g->sg -
761 g->adjust));
762 }
763 kfree(g);
764 }
765 } while (g);
766 }
767
768 kfree((void *)lio->glist);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -0800769 kfree((void *)lio->glist_lock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700770}
771
772/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700773 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700774 * @param lio per-network private data
775 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700776static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700777{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700778 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700779 struct octnic_gather *g;
780
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700781 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
782 GFP_KERNEL);
783 if (!lio->glist_lock)
784 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700785
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700786 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
787 GFP_KERNEL);
788 if (!lio->glist) {
789 kfree((void *)lio->glist_lock);
790 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700791 }
792
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700793 for (i = 0; i < num_iqs; i++) {
794 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700795
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700796 spin_lock_init(&lio->glist_lock[i]);
797
798 INIT_LIST_HEAD(&lio->glist[i]);
799
800 for (j = 0; j < lio->tx_qsize; j++) {
801 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
802 numa_node);
803 if (!g)
804 g = kzalloc(sizeof(*g), GFP_KERNEL);
805 if (!g)
806 break;
807
808 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
809 OCT_SG_ENTRY_SIZE);
810
811 g->sg = kmalloc_node(g->sg_size + 8,
812 GFP_KERNEL, numa_node);
813 if (!g->sg)
814 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
815 if (!g->sg) {
816 kfree(g);
817 break;
818 }
819
820 /* The gather component should be aligned on 64-bit
821 * boundary
822 */
823 if (((unsigned long)g->sg) & 7) {
824 g->adjust = 8 - (((unsigned long)g->sg) & 7);
825 g->sg = (struct octeon_sg_entry *)
826 ((unsigned long)g->sg + g->adjust);
827 }
828 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
829 g->sg, g->sg_size,
830 DMA_TO_DEVICE);
831 if (dma_mapping_error(&oct->pci_dev->dev,
832 g->sg_dma_ptr)) {
833 kfree((void *)((unsigned long)g->sg -
834 g->adjust));
835 kfree(g);
836 break;
837 }
838
839 list_add_tail(&g->list, &lio->glist[i]);
840 }
841
842 if (j != lio->tx_qsize) {
843 delete_glists(lio);
844 return 1;
845 }
846 }
847
848 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700849}
850
851/**
852 * \brief Print link information
853 * @param netdev network device
854 */
855static void print_link_info(struct net_device *netdev)
856{
857 struct lio *lio = GET_LIO(netdev);
858
859 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
860 struct oct_link_info *linfo = &lio->linfo;
861
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700862 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700863 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
864 linfo->link.s.speed,
865 (linfo->link.s.duplex) ? "Full" : "Half");
866 } else {
867 netif_info(lio, link, lio->netdev, "Link Down\n");
868 }
869 }
870}
871
872/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700873 * \brief Routine to notify MTU change
874 * @param work work_struct data structure
875 */
876static void octnet_link_status_change(struct work_struct *work)
877{
878 struct cavium_wk *wk = (struct cavium_wk *)work;
879 struct lio *lio = (struct lio *)wk->ctxptr;
880
881 rtnl_lock();
882 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
883 rtnl_unlock();
884}
885
886/**
887 * \brief Sets up the mtu status change work
888 * @param netdev network device
889 */
890static inline int setup_link_status_change_wq(struct net_device *netdev)
891{
892 struct lio *lio = GET_LIO(netdev);
893 struct octeon_device *oct = lio->oct_dev;
894
895 lio->link_status_wq.wq = alloc_workqueue("link-status",
896 WQ_MEM_RECLAIM, 0);
897 if (!lio->link_status_wq.wq) {
898 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
899 return -1;
900 }
901 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
902 octnet_link_status_change);
903 lio->link_status_wq.wk.ctxptr = lio;
904
905 return 0;
906}
907
908static inline void cleanup_link_status_change_wq(struct net_device *netdev)
909{
910 struct lio *lio = GET_LIO(netdev);
911
912 if (lio->link_status_wq.wq) {
913 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
914 destroy_workqueue(lio->link_status_wq.wq);
915 }
916}
917
918/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700919 * \brief Update link status
920 * @param netdev network device
921 * @param ls link status structure
922 *
923 * Called on receipt of a link status response from the core application to
924 * update each interface's link status.
925 */
926static inline void update_link_status(struct net_device *netdev,
927 union oct_link_status *ls)
928{
929 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700930 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700931
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700932 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700933
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700934 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700935 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700936 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700937
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700938 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700939 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700940 txqs_wake(netdev);
941 } else {
942 netif_carrier_off(netdev);
943 stop_txq(netdev);
944 }
945 }
946}
947
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700948/* Runs in interrupt context. */
949static void update_txq_status(struct octeon_device *oct, int iq_num)
950{
951 struct net_device *netdev;
952 struct lio *lio;
953 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
954
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700955 netdev = oct->props[iq->ifidx].netdev;
956
957 /* This is needed because the first IQ does not have
958 * a netdev associated with it.
959 */
960 if (!netdev)
961 return;
962
963 lio = GET_LIO(netdev);
964 if (netif_is_multiqueue(netdev)) {
965 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
966 lio->linfo.link.s.link_up &&
967 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700968 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
969 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700970 netif_wake_subqueue(netdev, iq->q_index);
971 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700972 if (!octnet_iq_is_full(oct, lio->txq)) {
973 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
974 lio->txq,
975 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700976 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700977 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700978 }
979 }
980}
981
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700982static
983int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
984{
985 struct octeon_device *oct = droq->oct_dev;
986 struct octeon_device_priv *oct_priv =
987 (struct octeon_device_priv *)oct->priv;
988
989 if (droq->ops.poll_mode) {
990 droq->ops.napi_fn(droq);
991 } else {
992 if (ret & MSIX_PO_INT) {
993 tasklet_schedule(&oct_priv->droq_tasklet);
994 return 1;
995 }
996 /* this will be flushed periodically by check iq db */
997 if (ret & MSIX_PI_INT)
998 return 0;
999 }
1000 return 0;
1001}
1002
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001003/**
1004 * \brief Droq packet processor sceduler
1005 * @param oct octeon device
1006 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07001007static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001008{
1009 struct octeon_device_priv *oct_priv =
1010 (struct octeon_device_priv *)oct->priv;
1011 u64 oq_no;
1012 struct octeon_droq *droq;
1013
1014 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001015 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1016 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001017 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001018 continue;
1019
1020 droq = oct->droq[oq_no];
1021
1022 if (droq->ops.poll_mode) {
1023 droq->ops.napi_fn(droq);
1024 oct_priv->napi_mask |= (1 << oq_no);
1025 } else {
1026 tasklet_schedule(&oct_priv->droq_tasklet);
1027 }
1028 }
1029 }
1030}
1031
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001032static irqreturn_t
1033liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1034{
1035 u64 ret;
1036 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1037 struct octeon_device *oct = ioq_vector->oct_dev;
1038 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1039
1040 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1041
1042 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1043 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1044
1045 return IRQ_HANDLED;
1046}
1047
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001048/**
1049 * \brief Interrupt handler for octeon
1050 * @param irq unused
1051 * @param dev octeon device
1052 */
1053static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001054irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1055 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001056{
1057 struct octeon_device *oct = (struct octeon_device *)dev;
1058 irqreturn_t ret;
1059
1060 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001061 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001062
1063 ret = oct->fn_list.process_interrupt_regs(oct);
1064
1065 if (ret == IRQ_HANDLED)
1066 liquidio_schedule_droq_pkt_handlers(oct);
1067
1068 /* Re-enable our interrupts */
1069 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001070 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001071
1072 return ret;
1073}
1074
1075/**
1076 * \brief Setup interrupt for octeon device
1077 * @param oct octeon device
1078 *
1079 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1080 */
1081static int octeon_setup_interrupt(struct octeon_device *oct)
1082{
1083 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001084 struct msix_entry *msix_entries;
1085 int i;
1086 int num_ioq_vectors;
1087 int num_alloc_ioq_vectors;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001088
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001089 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1090 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1091 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1092 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001093
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001094 oct->msix_entries = kcalloc(
1095 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
1096 if (!oct->msix_entries)
1097 return 1;
1098
1099 msix_entries = (struct msix_entry *)oct->msix_entries;
1100 /*Assumption is that pf msix vectors start from pf srn to pf to
1101 * trs and not from 0. if not change this code
1102 */
1103 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1104 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1105 msix_entries[oct->num_msix_irqs - 1].entry =
1106 oct->sriov_info.trs;
1107 num_alloc_ioq_vectors = pci_enable_msix_range(
1108 oct->pci_dev, msix_entries,
1109 oct->num_msix_irqs,
1110 oct->num_msix_irqs);
1111 if (num_alloc_ioq_vectors < 0) {
1112 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1113 kfree(oct->msix_entries);
1114 oct->msix_entries = NULL;
1115 return 1;
1116 }
1117 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1118
1119 num_ioq_vectors = oct->num_msix_irqs;
1120
1121 /** For PF, there is one non-ioq interrupt handler */
1122 num_ioq_vectors -= 1;
1123 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
1124 liquidio_legacy_intr_handler, 0, "octeon",
1125 oct);
1126 if (irqret) {
1127 dev_err(&oct->pci_dev->dev,
1128 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1129 irqret);
1130 pci_disable_msix(oct->pci_dev);
1131 kfree(oct->msix_entries);
1132 oct->msix_entries = NULL;
1133 return 1;
1134 }
1135
1136 for (i = 0; i < num_ioq_vectors; i++) {
1137 irqret = request_irq(msix_entries[i].vector,
1138 liquidio_msix_intr_handler, 0,
1139 "octeon", &oct->ioq_vector[i]);
1140 if (irqret) {
1141 dev_err(&oct->pci_dev->dev,
1142 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1143 irqret);
1144 /** Freeing the non-ioq irq vector here . */
1145 free_irq(msix_entries[num_ioq_vectors].vector,
1146 oct);
1147
1148 while (i) {
1149 i--;
1150 /** clearing affinity mask. */
1151 irq_set_affinity_hint(
1152 msix_entries[i].vector, NULL);
1153 free_irq(msix_entries[i].vector,
1154 &oct->ioq_vector[i]);
1155 }
1156 pci_disable_msix(oct->pci_dev);
1157 kfree(oct->msix_entries);
1158 oct->msix_entries = NULL;
1159 return 1;
1160 }
1161 oct->ioq_vector[i].vector = msix_entries[i].vector;
1162 /* assign the cpu mask for this msix interrupt vector */
1163 irq_set_affinity_hint(
1164 msix_entries[i].vector,
1165 (&oct->ioq_vector[i].affinity_mask));
1166 }
1167 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1168 oct->octeon_id);
1169 } else {
1170 err = pci_enable_msi(oct->pci_dev);
1171 if (err)
1172 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1173 err);
1174 else
1175 oct->flags |= LIO_FLAG_MSI_ENABLED;
1176
1177 irqret = request_irq(oct->pci_dev->irq,
1178 liquidio_legacy_intr_handler, IRQF_SHARED,
1179 "octeon", oct);
1180 if (irqret) {
1181 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1182 pci_disable_msi(oct->pci_dev);
1183 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1184 irqret);
1185 return 1;
1186 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001187 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001188 return 0;
1189}
1190
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001191static int liquidio_watchdog(void *param)
1192{
1193 u64 wdog;
1194 u16 mask_of_stuck_cores = 0;
1195 u16 mask_of_crashed_cores = 0;
1196 int core_num;
1197 u8 core_is_stuck[LIO_MAX_CORES];
1198 u8 core_crashed[LIO_MAX_CORES];
1199 struct octeon_device *oct = param;
1200
1201 memset(core_is_stuck, 0, sizeof(core_is_stuck));
1202 memset(core_crashed, 0, sizeof(core_crashed));
1203
1204 while (!kthread_should_stop()) {
1205 mask_of_crashed_cores =
1206 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1207
1208 for (core_num = 0; core_num < LIO_MAX_CORES; core_num++) {
1209 if (!core_is_stuck[core_num]) {
1210 wdog = lio_pci_readq(oct, CIU3_WDOG(core_num));
1211
1212 /* look at watchdog state field */
1213 wdog &= CIU3_WDOG_MASK;
1214 if (wdog) {
1215 /* this watchdog timer has expired */
1216 core_is_stuck[core_num] =
1217 LIO_MONITOR_WDOG_EXPIRE;
1218 mask_of_stuck_cores |= (1 << core_num);
1219 }
1220 }
1221
1222 if (!core_crashed[core_num])
1223 core_crashed[core_num] =
1224 (mask_of_crashed_cores >> core_num) & 1;
1225 }
1226
1227 if (mask_of_stuck_cores) {
1228 for (core_num = 0; core_num < LIO_MAX_CORES;
1229 core_num++) {
1230 if (core_is_stuck[core_num] == 1) {
1231 dev_err(&oct->pci_dev->dev,
1232 "ERROR: Octeon core %d is stuck!\n",
1233 core_num);
1234 /* 2 means we have printk'd an error
1235 * so no need to repeat the same printk
1236 */
1237 core_is_stuck[core_num] =
1238 LIO_MONITOR_CORE_STUCK_MSGD;
1239 }
1240 }
1241 }
1242
1243 if (mask_of_crashed_cores) {
1244 for (core_num = 0; core_num < LIO_MAX_CORES;
1245 core_num++) {
1246 if (core_crashed[core_num] == 1) {
1247 dev_err(&oct->pci_dev->dev,
1248 "ERROR: Octeon core %d crashed! See oct-fwdump for details.\n",
1249 core_num);
1250 /* 2 means we have printk'd an error
1251 * so no need to repeat the same printk
1252 */
1253 core_crashed[core_num] =
1254 LIO_MONITOR_CORE_STUCK_MSGD;
1255 }
1256 }
1257 }
1258#ifdef CONFIG_MODULE_UNLOAD
1259 if (mask_of_stuck_cores || mask_of_crashed_cores) {
1260 /* make module refcount=0 so that rmmod will work */
1261 long refcount;
1262
1263 refcount = module_refcount(THIS_MODULE);
1264
1265 while (refcount > 0) {
1266 module_put(THIS_MODULE);
1267 refcount = module_refcount(THIS_MODULE);
1268 }
1269
1270 /* compensate for and withstand an unlikely (but still
1271 * possible) race condition
1272 */
1273 while (refcount < 0) {
1274 try_module_get(THIS_MODULE);
1275 refcount = module_refcount(THIS_MODULE);
1276 }
1277 }
1278#endif
1279 /* sleep for two seconds */
1280 set_current_state(TASK_INTERRUPTIBLE);
1281 schedule_timeout(2 * HZ);
1282 }
1283
1284 return 0;
1285}
1286
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001287/**
1288 * \brief PCI probe handler
1289 * @param pdev PCI device structure
1290 * @param ent unused
1291 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001292static int
1293liquidio_probe(struct pci_dev *pdev,
1294 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001295{
1296 struct octeon_device *oct_dev = NULL;
1297 struct handshake *hs;
1298
1299 oct_dev = octeon_allocate_device(pdev->device,
1300 sizeof(struct octeon_device_priv));
1301 if (!oct_dev) {
1302 dev_err(&pdev->dev, "Unable to allocate device\n");
1303 return -ENOMEM;
1304 }
1305
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001306 if (pdev->device == OCTEON_CN23XX_PF_VID)
1307 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1308
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001309 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1310 (u32)pdev->vendor, (u32)pdev->device);
1311
1312 /* Assign octeon_device for this device to the private data area. */
1313 pci_set_drvdata(pdev, oct_dev);
1314
1315 /* set linux specific device pointer */
1316 oct_dev->pci_dev = (void *)pdev;
1317
1318 hs = &handshake[oct_dev->octeon_id];
1319 init_completion(&hs->init);
1320 init_completion(&hs->started);
1321 hs->pci_dev = pdev;
1322
1323 if (oct_dev->octeon_id == 0)
1324 /* first LiquidIO NIC is detected */
1325 complete(&first_stage);
1326
1327 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001328 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001329 liquidio_remove(pdev);
1330 return -ENOMEM;
1331 }
1332
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001333 if (OCTEON_CN23XX_PF(oct_dev)) {
1334 u64 scratch1;
1335 u8 bus, device, function;
1336
1337 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1338 if (!(scratch1 & 4ULL)) {
1339 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1340 * the lio watchdog kernel thread is running for this
1341 * NIC. Each NIC gets one watchdog kernel thread.
1342 */
1343 scratch1 |= 4ULL;
1344 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1345 scratch1);
1346
1347 bus = pdev->bus->number;
1348 device = PCI_SLOT(pdev->devfn);
1349 function = PCI_FUNC(pdev->devfn);
1350 oct_dev->watchdog_task = kthread_create(
1351 liquidio_watchdog, oct_dev,
1352 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001353 if (!IS_ERR(oct_dev->watchdog_task)) {
1354 wake_up_process(oct_dev->watchdog_task);
1355 } else {
1356 oct_dev->watchdog_task = NULL;
1357 dev_err(&oct_dev->pci_dev->dev,
1358 "failed to create kernel_thread\n");
1359 liquidio_remove(pdev);
1360 return -1;
1361 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001362 }
1363 }
1364
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001365 oct_dev->rx_pause = 1;
1366 oct_dev->tx_pause = 1;
1367
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001368 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1369
1370 return 0;
1371}
1372
1373/**
1374 *\brief Destroy resources associated with octeon device
1375 * @param pdev PCI device structure
1376 * @param ent unused
1377 */
1378static void octeon_destroy_resources(struct octeon_device *oct)
1379{
1380 int i;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001381 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001382 struct octeon_device_priv *oct_priv =
1383 (struct octeon_device_priv *)oct->priv;
1384
1385 struct handshake *hs;
1386
1387 switch (atomic_read(&oct->status)) {
1388 case OCT_DEV_RUNNING:
1389 case OCT_DEV_CORE_OK:
1390
1391 /* No more instructions will be forwarded. */
1392 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1393
1394 oct->app_mode = CVM_DRV_INVALID_APP;
1395 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1396 lio_get_state_string(&oct->status));
1397
1398 schedule_timeout_uninterruptible(HZ / 10);
1399
1400 /* fallthrough */
1401 case OCT_DEV_HOST_OK:
1402
1403 /* fallthrough */
1404 case OCT_DEV_CONSOLE_INIT_DONE:
1405 /* Remove any consoles */
1406 octeon_remove_consoles(oct);
1407
1408 /* fallthrough */
1409 case OCT_DEV_IO_QUEUES_DONE:
1410 if (wait_for_pending_requests(oct))
1411 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1412
1413 if (lio_wait_for_instr_fetch(oct))
1414 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1415
1416 /* Disable the input and output queues now. No more packets will
1417 * arrive from Octeon, but we should wait for all packet
1418 * processing to finish.
1419 */
1420 oct->fn_list.disable_io_queues(oct);
1421
1422 if (lio_wait_for_oq_pkts(oct))
1423 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1424
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001425 /* fallthrough */
1426 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001427 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001428 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001429
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001430 if (oct->msix_on) {
1431 msix_entries = (struct msix_entry *)oct->msix_entries;
1432 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1433 /* clear the affinity_cpumask */
1434 irq_set_affinity_hint(msix_entries[i].vector,
1435 NULL);
1436 free_irq(msix_entries[i].vector,
1437 &oct->ioq_vector[i]);
1438 }
1439 /* non-iov vector's argument is oct struct */
1440 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001441
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001442 pci_disable_msix(oct->pci_dev);
1443 kfree(oct->msix_entries);
1444 oct->msix_entries = NULL;
1445 } else {
1446 /* Release the interrupt line */
1447 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001448
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001449 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1450 pci_disable_msi(oct->pci_dev);
1451 }
1452
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001453 /* fallthrough */
1454 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001455 if (OCTEON_CN23XX_PF(oct))
1456 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001457
1458 /* fallthrough */
1459 case OCT_DEV_MBOX_SETUP_DONE:
1460 if (OCTEON_CN23XX_PF(oct))
1461 oct->fn_list.free_mbox(oct);
1462
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001463 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001464 case OCT_DEV_IN_RESET:
1465 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001466 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001467 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001468 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001469 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001470 continue;
1471 octeon_delete_droq(oct, i);
1472 }
1473
1474 /* Force any pending handshakes to complete */
1475 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1476 hs = &handshake[i];
1477
1478 if (hs->pci_dev) {
1479 handshake[oct->octeon_id].init_ok = 0;
1480 complete(&handshake[oct->octeon_id].init);
1481 handshake[oct->octeon_id].started_ok = 0;
1482 complete(&handshake[oct->octeon_id].started);
1483 }
1484 }
1485
1486 /* fallthrough */
1487 case OCT_DEV_RESP_LIST_INIT_DONE:
1488 octeon_delete_response_list(oct);
1489
1490 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001491 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001492 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001493 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001494 continue;
1495 octeon_delete_instr_queue(oct, i);
1496 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001497#ifdef CONFIG_PCI_IOV
1498 if (oct->sriov_info.sriov_enabled)
1499 pci_disable_sriov(oct->pci_dev);
1500#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001501 /* fallthrough */
1502 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1503 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001504
1505 /* fallthrough */
1506 case OCT_DEV_DISPATCH_INIT_DONE:
1507 octeon_delete_dispatch_list(oct);
1508 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1509
1510 /* fallthrough */
1511 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001512 /* Soft reset the octeon device before exiting */
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07001513 if ((!OCTEON_CN23XX_PF(oct)) || !oct->octeon_id)
1514 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001515
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001516 octeon_unmap_pci_barx(oct, 0);
1517 octeon_unmap_pci_barx(oct, 1);
1518
1519 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001520 case OCT_DEV_PCI_ENABLE_DONE:
1521 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001522 /* Disable the device, releasing the PCI INT */
1523 pci_disable_device(oct->pci_dev);
1524
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001525 /* fallthrough */
1526 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001527 /* Nothing to be done here either */
1528 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001529 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001530
1531 tasklet_kill(&oct_priv->droq_tasklet);
1532}
1533
1534/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001535 * \brief Callback for rx ctrl
1536 * @param status status of request
1537 * @param buf pointer to resp structure
1538 */
1539static void rx_ctl_callback(struct octeon_device *oct,
1540 u32 status,
1541 void *buf)
1542{
1543 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1544 struct liquidio_rx_ctl_context *ctx;
1545
1546 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1547
1548 oct = lio_get_device(ctx->octeon_id);
1549 if (status)
1550 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1551 CVM_CAST64(status));
1552 WRITE_ONCE(ctx->cond, 1);
1553
1554 /* This barrier is required to be sure that the response has been
1555 * written fully before waking up the handler
1556 */
1557 wmb();
1558
1559 wake_up_interruptible(&ctx->wc);
1560}
1561
1562/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001563 * \brief Send Rx control command
1564 * @param lio per-network private data
1565 * @param start_stop whether to start or stop
1566 */
1567static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1568{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001569 struct octeon_soft_command *sc;
1570 struct liquidio_rx_ctl_context *ctx;
1571 union octnet_cmd *ncmd;
1572 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1573 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1574 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001575
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001576 if (oct->props[lio->ifidx].rx_on == start_stop)
1577 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001578
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001579 sc = (struct octeon_soft_command *)
1580 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1581 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001582
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001583 ncmd = (union octnet_cmd *)sc->virtdptr;
1584 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1585
1586 WRITE_ONCE(ctx->cond, 0);
1587 ctx->octeon_id = lio_get_device_id(oct);
1588 init_waitqueue_head(&ctx->wc);
1589
1590 ncmd->u64 = 0;
1591 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1592 ncmd->s.param1 = start_stop;
1593
1594 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1595
1596 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1597
1598 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1599 OPCODE_NIC_CMD, 0, 0, 0);
1600
1601 sc->callback = rx_ctl_callback;
1602 sc->callback_arg = sc;
1603 sc->wait_time = 5000;
1604
1605 retval = octeon_send_soft_command(oct, sc);
1606 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001607 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001608 } else {
1609 /* Sleep on a wait queue till the cond flag indicates that the
1610 * response arrived or timed-out.
1611 */
1612 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1613 return;
1614 oct->props[lio->ifidx].rx_on = start_stop;
1615 }
1616
1617 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001618}
1619
1620/**
1621 * \brief Destroy NIC device interface
1622 * @param oct octeon device
1623 * @param ifidx which interface to destroy
1624 *
1625 * Cleanup associated with each interface for an Octeon device when NIC
1626 * module is being unloaded or if initialization fails during load.
1627 */
1628static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1629{
1630 struct net_device *netdev = oct->props[ifidx].netdev;
1631 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001632 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001633
1634 if (!netdev) {
1635 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1636 __func__, ifidx);
1637 return;
1638 }
1639
1640 lio = GET_LIO(netdev);
1641
1642 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1643
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001644 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001645 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001646
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001647 if (oct->props[lio->ifidx].napi_enabled == 1) {
1648 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1649 napi_disable(napi);
1650
1651 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001652
1653 if (OCTEON_CN23XX_PF(oct))
1654 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001655 }
1656
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001657 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1658 unregister_netdev(netdev);
1659
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001660 cleanup_link_status_change_wq(netdev);
1661
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001662 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001663
1664 free_netdev(netdev);
1665
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001666 oct->props[ifidx].gmxport = -1;
1667
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001668 oct->props[ifidx].netdev = NULL;
1669}
1670
1671/**
1672 * \brief Stop complete NIC functionality
1673 * @param oct octeon device
1674 */
1675static int liquidio_stop_nic_module(struct octeon_device *oct)
1676{
1677 int i, j;
1678 struct lio *lio;
1679
1680 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1681 if (!oct->ifcount) {
1682 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1683 return 1;
1684 }
1685
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001686 spin_lock_bh(&oct->cmd_resp_wqlock);
1687 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1688 spin_unlock_bh(&oct->cmd_resp_wqlock);
1689
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001690 for (i = 0; i < oct->ifcount; i++) {
1691 lio = GET_LIO(oct->props[i].netdev);
1692 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001693 octeon_unregister_droq_ops(oct,
1694 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001695 }
1696
1697 for (i = 0; i < oct->ifcount; i++)
1698 liquidio_destroy_nic_device(oct, i);
1699
1700 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1701 return 0;
1702}
1703
1704/**
1705 * \brief Cleans up resources at unload time
1706 * @param pdev PCI device structure
1707 */
1708static void liquidio_remove(struct pci_dev *pdev)
1709{
1710 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1711
1712 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1713
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001714 if (oct_dev->watchdog_task)
1715 kthread_stop(oct_dev->watchdog_task);
1716
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001717 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1718 liquidio_stop_nic_module(oct_dev);
1719
1720 /* Reset the octeon device and cleanup all memory allocated for
1721 * the octeon device by driver.
1722 */
1723 octeon_destroy_resources(oct_dev);
1724
1725 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1726
1727 /* This octeon device has been removed. Update the global
1728 * data structure to reflect this. Free the device structure.
1729 */
1730 octeon_free_device_mem(oct_dev);
1731}
1732
1733/**
1734 * \brief Identify the Octeon device and to map the BAR address space
1735 * @param oct octeon device
1736 */
1737static int octeon_chip_specific_setup(struct octeon_device *oct)
1738{
1739 u32 dev_id, rev_id;
1740 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001741 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001742
1743 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1744 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1745 oct->rev_id = rev_id & 0xff;
1746
1747 switch (dev_id) {
1748 case OCTEON_CN68XX_PCIID:
1749 oct->chip_id = OCTEON_CN68XX;
1750 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001751 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001752 break;
1753
1754 case OCTEON_CN66XX_PCIID:
1755 oct->chip_id = OCTEON_CN66XX;
1756 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001757 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001758 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001759
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001760 case OCTEON_CN23XX_PCIID_PF:
1761 oct->chip_id = OCTEON_CN23XX_PF_VID;
1762 ret = setup_cn23xx_octeon_pf_device(oct);
1763 s = "CN23XX";
1764 break;
1765
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001766 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001767 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001768 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1769 dev_id);
1770 }
1771
1772 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001773 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001774 OCTEON_MAJOR_REV(oct),
1775 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001776 octeon_get_conf(oct)->card_name,
1777 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001778
1779 return ret;
1780}
1781
1782/**
1783 * \brief PCI initialization for each Octeon device.
1784 * @param oct octeon device
1785 */
1786static int octeon_pci_os_setup(struct octeon_device *oct)
1787{
1788 /* setup PCI stuff first */
1789 if (pci_enable_device(oct->pci_dev)) {
1790 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1791 return 1;
1792 }
1793
1794 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1795 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001796 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001797 return 1;
1798 }
1799
1800 /* Enable PCI DMA Master. */
1801 pci_set_master(oct->pci_dev);
1802
1803 return 0;
1804}
1805
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001806static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1807{
1808 int q = 0;
1809
1810 if (netif_is_multiqueue(lio->netdev))
1811 q = skb->queue_mapping % lio->linfo.num_txpciq;
1812
1813 return q;
1814}
1815
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001816/**
1817 * \brief Check Tx queue state for a given network buffer
1818 * @param lio per-network private data
1819 * @param skb network buffer
1820 */
1821static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1822{
1823 int q = 0, iq = 0;
1824
1825 if (netif_is_multiqueue(lio->netdev)) {
1826 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001827 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001828 } else {
1829 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001830 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001831 }
1832
1833 if (octnet_iq_is_full(lio->oct_dev, iq))
1834 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001835
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001836 if (__netif_subqueue_stopped(lio->netdev, q)) {
1837 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001838 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001839 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001840 return 1;
1841}
1842
1843/**
1844 * \brief Unmap and free network buffer
1845 * @param buf buffer
1846 */
1847static void free_netbuf(void *buf)
1848{
1849 struct sk_buff *skb;
1850 struct octnet_buf_free_info *finfo;
1851 struct lio *lio;
1852
1853 finfo = (struct octnet_buf_free_info *)buf;
1854 skb = finfo->skb;
1855 lio = finfo->lio;
1856
1857 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1858 DMA_TO_DEVICE);
1859
1860 check_txq_state(lio, skb);
1861
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001862 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001863}
1864
1865/**
1866 * \brief Unmap and free gather buffer
1867 * @param buf buffer
1868 */
1869static void free_netsgbuf(void *buf)
1870{
1871 struct octnet_buf_free_info *finfo;
1872 struct sk_buff *skb;
1873 struct lio *lio;
1874 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001875 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001876
1877 finfo = (struct octnet_buf_free_info *)buf;
1878 skb = finfo->skb;
1879 lio = finfo->lio;
1880 g = finfo->g;
1881 frags = skb_shinfo(skb)->nr_frags;
1882
1883 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1884 g->sg[0].ptr[0], (skb->len - skb->data_len),
1885 DMA_TO_DEVICE);
1886
1887 i = 1;
1888 while (frags--) {
1889 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1890
1891 pci_unmap_page((lio->oct_dev)->pci_dev,
1892 g->sg[(i >> 2)].ptr[(i & 3)],
1893 frag->size, DMA_TO_DEVICE);
1894 i++;
1895 }
1896
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001897 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1898 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001899
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001900 iq = skb_iq(lio, skb);
1901 spin_lock(&lio->glist_lock[iq]);
1902 list_add_tail(&g->list, &lio->glist[iq]);
1903 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001904
1905 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1906
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001907 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001908}
1909
1910/**
1911 * \brief Unmap and free gather buffer with response
1912 * @param buf buffer
1913 */
1914static void free_netsgbuf_with_resp(void *buf)
1915{
1916 struct octeon_soft_command *sc;
1917 struct octnet_buf_free_info *finfo;
1918 struct sk_buff *skb;
1919 struct lio *lio;
1920 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001921 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001922
1923 sc = (struct octeon_soft_command *)buf;
1924 skb = (struct sk_buff *)sc->callback_arg;
1925 finfo = (struct octnet_buf_free_info *)&skb->cb;
1926
1927 lio = finfo->lio;
1928 g = finfo->g;
1929 frags = skb_shinfo(skb)->nr_frags;
1930
1931 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1932 g->sg[0].ptr[0], (skb->len - skb->data_len),
1933 DMA_TO_DEVICE);
1934
1935 i = 1;
1936 while (frags--) {
1937 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1938
1939 pci_unmap_page((lio->oct_dev)->pci_dev,
1940 g->sg[(i >> 2)].ptr[(i & 3)],
1941 frag->size, DMA_TO_DEVICE);
1942 i++;
1943 }
1944
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001945 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1946 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001947
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001948 iq = skb_iq(lio, skb);
1949
1950 spin_lock(&lio->glist_lock[iq]);
1951 list_add_tail(&g->list, &lio->glist[iq]);
1952 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001953
1954 /* Don't free the skb yet */
1955
1956 check_txq_state(lio, skb);
1957}
1958
1959/**
1960 * \brief Adjust ptp frequency
1961 * @param ptp PTP clock info
1962 * @param ppb how much to adjust by, in parts-per-billion
1963 */
1964static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1965{
1966 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1967 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1968 u64 comp, delta;
1969 unsigned long flags;
1970 bool neg_adj = false;
1971
1972 if (ppb < 0) {
1973 neg_adj = true;
1974 ppb = -ppb;
1975 }
1976
1977 /* The hardware adds the clock compensation value to the
1978 * PTP clock on every coprocessor clock cycle, so we
1979 * compute the delta in terms of coprocessor clocks.
1980 */
1981 delta = (u64)ppb << 32;
1982 do_div(delta, oct->coproc_clock_rate);
1983
1984 spin_lock_irqsave(&lio->ptp_lock, flags);
1985 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1986 if (neg_adj)
1987 comp -= delta;
1988 else
1989 comp += delta;
1990 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1991 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1992
1993 return 0;
1994}
1995
1996/**
1997 * \brief Adjust ptp time
1998 * @param ptp PTP clock info
1999 * @param delta how much to adjust by, in nanosecs
2000 */
2001static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2002{
2003 unsigned long flags;
2004 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2005
2006 spin_lock_irqsave(&lio->ptp_lock, flags);
2007 lio->ptp_adjust += delta;
2008 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2009
2010 return 0;
2011}
2012
2013/**
2014 * \brief Get hardware clock time, including any adjustment
2015 * @param ptp PTP clock info
2016 * @param ts timespec
2017 */
2018static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2019 struct timespec64 *ts)
2020{
2021 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002022 unsigned long flags;
2023 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2024 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2025
2026 spin_lock_irqsave(&lio->ptp_lock, flags);
2027 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2028 ns += lio->ptp_adjust;
2029 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2030
Kefeng Wang286af312016-01-27 17:34:37 +08002031 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002032
2033 return 0;
2034}
2035
2036/**
2037 * \brief Set hardware clock time. Reset adjustment
2038 * @param ptp PTP clock info
2039 * @param ts timespec
2040 */
2041static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2042 const struct timespec64 *ts)
2043{
2044 u64 ns;
2045 unsigned long flags;
2046 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2047 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2048
2049 ns = timespec_to_ns(ts);
2050
2051 spin_lock_irqsave(&lio->ptp_lock, flags);
2052 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2053 lio->ptp_adjust = 0;
2054 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2055
2056 return 0;
2057}
2058
2059/**
2060 * \brief Check if PTP is enabled
2061 * @param ptp PTP clock info
2062 * @param rq request
2063 * @param on is it on
2064 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002065static int
2066liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2067 struct ptp_clock_request *rq __attribute__((unused)),
2068 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002069{
2070 return -EOPNOTSUPP;
2071}
2072
2073/**
2074 * \brief Open PTP clock source
2075 * @param netdev network device
2076 */
2077static void oct_ptp_open(struct net_device *netdev)
2078{
2079 struct lio *lio = GET_LIO(netdev);
2080 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2081
2082 spin_lock_init(&lio->ptp_lock);
2083
2084 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2085 lio->ptp_info.owner = THIS_MODULE;
2086 lio->ptp_info.max_adj = 250000000;
2087 lio->ptp_info.n_alarm = 0;
2088 lio->ptp_info.n_ext_ts = 0;
2089 lio->ptp_info.n_per_out = 0;
2090 lio->ptp_info.pps = 0;
2091 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2092 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2093 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2094 lio->ptp_info.settime64 = liquidio_ptp_settime;
2095 lio->ptp_info.enable = liquidio_ptp_enable;
2096
2097 lio->ptp_adjust = 0;
2098
2099 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2100 &oct->pci_dev->dev);
2101
2102 if (IS_ERR(lio->ptp_clock))
2103 lio->ptp_clock = NULL;
2104}
2105
2106/**
2107 * \brief Init PTP clock
2108 * @param oct octeon device
2109 */
2110static void liquidio_ptp_init(struct octeon_device *oct)
2111{
2112 u64 clock_comp, cfg;
2113
2114 clock_comp = (u64)NSEC_PER_SEC << 32;
2115 do_div(clock_comp, oct->coproc_clock_rate);
2116 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2117
2118 /* Enable */
2119 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2120 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2121}
2122
2123/**
2124 * \brief Load firmware to device
2125 * @param oct octeon device
2126 *
2127 * Maps device to firmware filename, requests firmware, and downloads it
2128 */
2129static int load_firmware(struct octeon_device *oct)
2130{
2131 int ret = 0;
2132 const struct firmware *fw;
2133 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2134 char *tmp_fw_type;
2135
2136 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
2137 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
2138 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2139 return ret;
2140 }
2141
2142 if (fw_type[0] == '\0')
2143 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2144 else
2145 tmp_fw_type = fw_type;
2146
2147 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2148 octeon_get_conf(oct)->card_name, tmp_fw_type,
2149 LIO_FW_NAME_SUFFIX);
2150
2151 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2152 if (ret) {
2153 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2154 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002155 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002156 return ret;
2157 }
2158
2159 ret = octeon_download_firmware(oct, fw->data, fw->size);
2160
2161 release_firmware(fw);
2162
2163 return ret;
2164}
2165
2166/**
2167 * \brief Setup output queue
2168 * @param oct octeon device
2169 * @param q_no which queue
2170 * @param num_descs how many descriptors
2171 * @param desc_size size of each descriptor
2172 * @param app_ctx application context
2173 */
2174static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2175 int desc_size, void *app_ctx)
2176{
2177 int ret_val = 0;
2178
2179 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2180 /* droq creation and local register settings. */
2181 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302182 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002183 return ret_val;
2184
2185 if (ret_val == 1) {
2186 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2187 return 0;
2188 }
2189 /* tasklet creation for the droq */
2190
2191 /* Enable the droq queues */
2192 octeon_set_droq_pkt_op(oct, q_no, 1);
2193
2194 /* Send Credit for Octeon Output queues. Credits are always
2195 * sent after the output queue is enabled.
2196 */
2197 writel(oct->droq[q_no]->max_count,
2198 oct->droq[q_no]->pkts_credit_reg);
2199
2200 return ret_val;
2201}
2202
2203/**
2204 * \brief Callback for getting interface configuration
2205 * @param status status of request
2206 * @param buf pointer to resp structure
2207 */
2208static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002209 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002210 void *buf)
2211{
2212 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2213 struct liquidio_if_cfg_resp *resp;
2214 struct liquidio_if_cfg_context *ctx;
2215
2216 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002217 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002218
2219 oct = lio_get_device(ctx->octeon_id);
2220 if (resp->status)
2221 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
2222 CVM_CAST64(resp->status));
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002223 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002224
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002225 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2226 resp->cfg_info.liquidio_firmware_version);
2227
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002228 /* This barrier is required to be sure that the response has been
2229 * written fully before waking up the handler
2230 */
2231 wmb();
2232
2233 wake_up_interruptible(&ctx->wc);
2234}
2235
2236/**
2237 * \brief Select queue based on hash
2238 * @param dev Net device
2239 * @param skb sk_buff structure
2240 * @returns selected queue number
2241 */
2242static u16 select_q(struct net_device *dev, struct sk_buff *skb,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002243 void *accel_priv __attribute__((unused)),
2244 select_queue_fallback_t fallback __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002245{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002246 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002247 struct lio *lio;
2248
2249 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002250 qindex = skb_tx_hash(dev, skb);
2251
2252 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002253}
2254
2255/** Routine to push packets arriving on Octeon interface upto network layer.
2256 * @param oct_id - octeon device id.
2257 * @param skbuff - skbuff struct to be passed to network layer.
2258 * @param len - size of total data received.
2259 * @param rh - Control header associated with the packet
2260 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002261 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002262 */
2263static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002264liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002265 void *skbuff,
2266 u32 len,
2267 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002268 void *param,
2269 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002270{
2271 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002272 struct sk_buff *skb = (struct sk_buff *)skbuff;
2273 struct skb_shared_hwtstamps *shhwtstamps;
2274 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002275 u16 vtag = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002276 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002277 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2278 napi);
2279 if (netdev) {
2280 int packet_was_received;
2281 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002282 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002283
2284 /* Do not proceed if the interface is not in RUNNING state. */
2285 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2286 recv_buffer_free(skb);
2287 droq->stats.rx_dropped++;
2288 return;
2289 }
2290
2291 skb->dev = netdev;
2292
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002293 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002294 if (likely(len > MIN_SKB_SIZE)) {
2295 struct octeon_skb_page_info *pg_info;
2296 unsigned char *va;
2297
2298 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2299 if (pg_info->page) {
2300 /* For Paged allocation use the frags */
2301 va = page_address(pg_info->page) +
2302 pg_info->page_offset;
2303 memcpy(skb->data, va, MIN_SKB_SIZE);
2304 skb_put(skb, MIN_SKB_SIZE);
2305 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2306 pg_info->page,
2307 pg_info->page_offset +
2308 MIN_SKB_SIZE,
2309 len - MIN_SKB_SIZE,
2310 LIO_RXBUFFER_SZ);
2311 }
2312 } else {
2313 struct octeon_skb_page_info *pg_info =
2314 ((struct octeon_skb_page_info *)(skb->cb));
2315 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2316 + pg_info->page_offset, len);
2317 skb_put(skb, len);
2318 put_page(pg_info->page);
2319 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002320
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002321 if (((oct->chip_id == OCTEON_CN66XX) ||
2322 (oct->chip_id == OCTEON_CN68XX)) &&
2323 ptp_enable) {
2324 if (rh->r_dh.has_hwtstamp) {
2325 /* timestamp is included from the hardware at
2326 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002327 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002328 if (ifstate_check
2329 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2330 /* Nanoseconds are in the first 64-bits
2331 * of the packet.
2332 */
2333 memcpy(&ns, (skb->data), sizeof(ns));
2334 shhwtstamps = skb_hwtstamps(skb);
2335 shhwtstamps->hwtstamp =
2336 ns_to_ktime(ns +
2337 lio->ptp_adjust);
2338 }
2339 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002340 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002341 }
2342
2343 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002344 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002345 (((rh->r_dh.encap_on) &&
2346 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2347 (!(rh->r_dh.encap_on) &&
2348 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002349 /* checksum has already been verified */
2350 skb->ip_summed = CHECKSUM_UNNECESSARY;
2351 else
2352 skb->ip_summed = CHECKSUM_NONE;
2353
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002354 /* Setting Encapsulation field on basis of status received
2355 * from the firmware
2356 */
2357 if (rh->r_dh.encap_on) {
2358 skb->encapsulation = 1;
2359 skb->csum_level = 1;
2360 droq->stats.rx_vxlan++;
2361 }
2362
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002363 /* inbound VLAN tag */
2364 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2365 (rh->r_dh.vlan != 0)) {
2366 u16 vid = rh->r_dh.vlan;
2367 u16 priority = rh->r_dh.priority;
2368
2369 vtag = priority << 13 | vid;
2370 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2371 }
2372
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002373 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2374
2375 if (packet_was_received) {
2376 droq->stats.rx_bytes_received += len;
2377 droq->stats.rx_pkts_received++;
2378 netdev->last_rx = jiffies;
2379 } else {
2380 droq->stats.rx_dropped++;
2381 netif_info(lio, rx_err, lio->netdev,
2382 "droq:%d error rx_dropped:%llu\n",
2383 droq->q_no, droq->stats.rx_dropped);
2384 }
2385
2386 } else {
2387 recv_buffer_free(skb);
2388 }
2389}
2390
2391/**
2392 * \brief wrapper for calling napi_schedule
2393 * @param param parameters to pass to napi_schedule
2394 *
2395 * Used when scheduling on different CPUs
2396 */
2397static void napi_schedule_wrapper(void *param)
2398{
2399 struct napi_struct *napi = param;
2400
2401 napi_schedule(napi);
2402}
2403
2404/**
2405 * \brief callback when receive interrupt occurs and we are in NAPI mode
2406 * @param arg pointer to octeon output queue
2407 */
2408static void liquidio_napi_drv_callback(void *arg)
2409{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002410 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002411 struct octeon_droq *droq = arg;
2412 int this_cpu = smp_processor_id();
2413
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002414 oct = droq->oct_dev;
2415
2416 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2417 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002418 } else {
2419 struct call_single_data *csd = &droq->csd;
2420
2421 csd->func = napi_schedule_wrapper;
2422 csd->info = &droq->napi;
2423 csd->flags = 0;
2424
2425 smp_call_function_single_async(droq->cpu_id, csd);
2426 }
2427}
2428
2429/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002430 * \brief Entry point for NAPI polling
2431 * @param napi NAPI structure
2432 * @param budget maximum number of items to process
2433 */
2434static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2435{
2436 struct octeon_droq *droq;
2437 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002438 int tx_done = 0, iq_no;
2439 struct octeon_instr_queue *iq;
2440 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002441
2442 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002443 oct = droq->oct_dev;
2444 iq_no = droq->q_no;
2445 /* Handle Droq descriptors */
2446 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2447 POLL_EVENT_PROCESS_PKTS,
2448 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002449
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002450 /* Flush the instruction queue */
2451 iq = oct->instr_queue[iq_no];
2452 if (iq) {
2453 /* Process iq buffers with in the budget limits */
2454 tx_done = octeon_flush_iq(oct, iq, 1, budget);
2455 /* Update iq read-index rather than waiting for next interrupt.
2456 * Return back if tx_done is false.
2457 */
2458 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002459 } else {
2460 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2461 __func__, iq_no);
2462 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002463
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002464 if ((work_done < budget) && (tx_done)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002465 napi_complete(napi);
2466 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2467 POLL_EVENT_ENABLE_INTR, 0);
2468 return 0;
2469 }
2470
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002471 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002472}
2473
2474/**
2475 * \brief Setup input and output queues
2476 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002477 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002478 *
2479 * Note: Queues are with respect to the octeon device. Thus
2480 * an input queue is for egress packets, and output queues
2481 * are for ingress packets.
2482 */
2483static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002484 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002485{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002486 struct octeon_droq_ops droq_ops;
2487 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002488 static int cpu_id;
2489 static int cpu_id_modulus;
2490 struct octeon_droq *droq;
2491 struct napi_struct *napi;
2492 int q, q_no, retval = 0;
2493 struct lio *lio;
2494 int num_tx_descs;
2495
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002496 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002497
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002498 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002499
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002500 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2501
2502 droq_ops.fptr = liquidio_push_packet;
2503 droq_ops.farg = (void *)netdev;
2504
2505 droq_ops.poll_mode = 1;
2506 droq_ops.napi_fn = liquidio_napi_drv_callback;
2507 cpu_id = 0;
2508 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002509
2510 /* set up DROQs. */
2511 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002512 q_no = lio->linfo.rxpciq[q].s.q_no;
2513 dev_dbg(&octeon_dev->pci_dev->dev,
2514 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2515 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002516 retval = octeon_setup_droq(octeon_dev, q_no,
2517 CFG_GET_NUM_RX_DESCS_NIC_IF
2518 (octeon_get_conf(octeon_dev),
2519 lio->ifidx),
2520 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2521 (octeon_get_conf(octeon_dev),
2522 lio->ifidx), NULL);
2523 if (retval) {
2524 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002525 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002526 __func__);
2527 return 1;
2528 }
2529
2530 droq = octeon_dev->droq[q_no];
2531 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002532 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2533 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002534 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002535
2536 /* designate a CPU for this droq */
2537 droq->cpu_id = cpu_id;
2538 cpu_id++;
2539 if (cpu_id >= cpu_id_modulus)
2540 cpu_id = 0;
2541
2542 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2543 }
2544
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002545 if (OCTEON_CN23XX_PF(octeon_dev)) {
2546 /* 23XX PF can receive control messages (via the first PF-owned
2547 * droq) from the firmware even if the ethX interface is down,
2548 * so that's why poll_mode must be off for the first droq.
2549 */
2550 octeon_dev->droq[0]->ops.poll_mode = 0;
2551 }
2552
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002553 /* set up IQs. */
2554 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2555 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2556 (octeon_dev),
2557 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002558 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2559 lio->linfo.txpciq[q], num_tx_descs,
2560 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002561 if (retval) {
2562 dev_err(&octeon_dev->pci_dev->dev,
2563 " %s : Runtime IQ(TxQ) creation failed.\n",
2564 __func__);
2565 return 1;
2566 }
2567 }
2568
2569 return 0;
2570}
2571
2572/**
2573 * \brief Poll routine for checking transmit queue status
2574 * @param work work_struct data structure
2575 */
2576static void octnet_poll_check_txq_status(struct work_struct *work)
2577{
2578 struct cavium_wk *wk = (struct cavium_wk *)work;
2579 struct lio *lio = (struct lio *)wk->ctxptr;
2580
2581 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2582 return;
2583
2584 check_txq_status(lio);
2585 queue_delayed_work(lio->txq_status_wq.wq,
2586 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2587}
2588
2589/**
2590 * \brief Sets up the txq poll check
2591 * @param netdev network device
2592 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002593static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002594{
2595 struct lio *lio = GET_LIO(netdev);
2596 struct octeon_device *oct = lio->oct_dev;
2597
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302598 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2599 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002600 if (!lio->txq_status_wq.wq) {
2601 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002602 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002603 }
2604 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2605 octnet_poll_check_txq_status);
2606 lio->txq_status_wq.wk.ctxptr = lio;
2607 queue_delayed_work(lio->txq_status_wq.wq,
2608 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002609 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002610}
2611
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002612static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2613{
2614 struct lio *lio = GET_LIO(netdev);
2615
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002616 if (lio->txq_status_wq.wq) {
2617 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2618 destroy_workqueue(lio->txq_status_wq.wq);
2619 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002620}
2621
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002622/**
2623 * \brief Net device open for LiquidIO
2624 * @param netdev network device
2625 */
2626static int liquidio_open(struct net_device *netdev)
2627{
2628 struct lio *lio = GET_LIO(netdev);
2629 struct octeon_device *oct = lio->oct_dev;
2630 struct napi_struct *napi, *n;
2631
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002632 if (oct->props[lio->ifidx].napi_enabled == 0) {
2633 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2634 napi_enable(napi);
2635
2636 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002637
2638 if (OCTEON_CN23XX_PF(oct))
2639 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002640 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002641
2642 oct_ptp_open(netdev);
2643
2644 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002645
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002646 /* Ready for link status updates */
2647 lio->intf_open = 1;
2648
2649 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2650
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002651 if (OCTEON_CN23XX_PF(oct)) {
2652 if (!oct->msix_on)
2653 if (setup_tx_poll_fn(netdev))
2654 return -1;
2655 } else {
2656 if (setup_tx_poll_fn(netdev))
2657 return -1;
2658 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002659
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002660 start_txq(netdev);
2661
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002662 /* tell Octeon to start forwarding packets to host */
2663 send_rx_ctrl_cmd(lio, 1);
2664
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002665 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2666 netdev->name);
2667
2668 return 0;
2669}
2670
2671/**
2672 * \brief Net device stop for LiquidIO
2673 * @param netdev network device
2674 */
2675static int liquidio_stop(struct net_device *netdev)
2676{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002677 struct lio *lio = GET_LIO(netdev);
2678 struct octeon_device *oct = lio->oct_dev;
2679
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002680 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2681
2682 netif_tx_disable(netdev);
2683
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002684 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002685 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002686 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002687 lio->linfo.link.s.link_up = 0;
2688 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002689
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002690 /* Pause for a moment and wait for Octeon to flush out (to the wire) any
2691 * egress packets that are in-flight.
2692 */
2693 set_current_state(TASK_INTERRUPTIBLE);
2694 schedule_timeout(msecs_to_jiffies(100));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002695
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002696 /* Now it should be safe to tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002697 send_rx_ctrl_cmd(lio, 0);
2698
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002699 if (OCTEON_CN23XX_PF(oct)) {
2700 if (!oct->msix_on)
2701 cleanup_tx_poll_fn(netdev);
2702 } else {
2703 cleanup_tx_poll_fn(netdev);
2704 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705
2706 if (lio->ptp_clock) {
2707 ptp_clock_unregister(lio->ptp_clock);
2708 lio->ptp_clock = NULL;
2709 }
2710
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002711 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002712
2713 return 0;
2714}
2715
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002716/**
2717 * \brief Converts a mask based on net device flags
2718 * @param netdev network device
2719 *
2720 * This routine generates a octnet_ifflags mask from the net device flags
2721 * received from the OS.
2722 */
2723static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2724{
2725 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2726
2727 if (netdev->flags & IFF_PROMISC)
2728 f |= OCTNET_IFFLAG_PROMISC;
2729
2730 if (netdev->flags & IFF_ALLMULTI)
2731 f |= OCTNET_IFFLAG_ALLMULTI;
2732
2733 if (netdev->flags & IFF_MULTICAST) {
2734 f |= OCTNET_IFFLAG_MULTICAST;
2735
2736 /* Accept all multicast addresses if there are more than we
2737 * can handle
2738 */
2739 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2740 f |= OCTNET_IFFLAG_ALLMULTI;
2741 }
2742
2743 if (netdev->flags & IFF_BROADCAST)
2744 f |= OCTNET_IFFLAG_BROADCAST;
2745
2746 return f;
2747}
2748
2749/**
2750 * \brief Net device set_multicast_list
2751 * @param netdev network device
2752 */
2753static void liquidio_set_mcast_list(struct net_device *netdev)
2754{
2755 struct lio *lio = GET_LIO(netdev);
2756 struct octeon_device *oct = lio->oct_dev;
2757 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002758 struct netdev_hw_addr *ha;
2759 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002760 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002761 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2762
2763 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2764
2765 /* Create a ctrl pkt command to be sent to core app. */
2766 nctrl.ncmd.u64 = 0;
2767 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002768 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2769 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002770 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002771 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002772 nctrl.netpndev = (u64)netdev;
2773 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2774
2775 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002776 mc = &nctrl.udd[0];
2777 netdev_for_each_mc_addr(ha, netdev) {
2778 *mc = 0;
2779 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2780 /* no need to swap bytes */
2781
2782 if (++mc > &nctrl.udd[mc_count])
2783 break;
2784 }
2785
2786 /* Apparently, any activity in this call from the kernel has to
2787 * be atomic. So we won't wait for response.
2788 */
2789 nctrl.wait_time = 0;
2790
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002791 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002792 if (ret < 0) {
2793 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2794 ret);
2795 }
2796}
2797
2798/**
2799 * \brief Net device set_mac_address
2800 * @param netdev network device
2801 */
2802static int liquidio_set_mac(struct net_device *netdev, void *p)
2803{
2804 int ret = 0;
2805 struct lio *lio = GET_LIO(netdev);
2806 struct octeon_device *oct = lio->oct_dev;
2807 struct sockaddr *addr = (struct sockaddr *)p;
2808 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002809
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002810 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002811 return -EADDRNOTAVAIL;
2812
2813 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2814
2815 nctrl.ncmd.u64 = 0;
2816 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002817 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002818 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002819 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002820 nctrl.netpndev = (u64)netdev;
2821 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2822 nctrl.wait_time = 100;
2823
2824 nctrl.udd[0] = 0;
2825 /* The MAC Address is presented in network byte order. */
2826 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2827
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002828 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002829 if (ret < 0) {
2830 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2831 return -ENOMEM;
2832 }
2833 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2834 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2835
2836 return 0;
2837}
2838
2839/**
2840 * \brief Net device get_stats
2841 * @param netdev network device
2842 */
2843static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2844{
2845 struct lio *lio = GET_LIO(netdev);
2846 struct net_device_stats *stats = &netdev->stats;
2847 struct octeon_device *oct;
2848 u64 pkts = 0, drop = 0, bytes = 0;
2849 struct oct_droq_stats *oq_stats;
2850 struct oct_iq_stats *iq_stats;
2851 int i, iq_no, oq_no;
2852
2853 oct = lio->oct_dev;
2854
2855 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002856 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002857 iq_stats = &oct->instr_queue[iq_no]->stats;
2858 pkts += iq_stats->tx_done;
2859 drop += iq_stats->tx_dropped;
2860 bytes += iq_stats->tx_tot_bytes;
2861 }
2862
2863 stats->tx_packets = pkts;
2864 stats->tx_bytes = bytes;
2865 stats->tx_dropped = drop;
2866
2867 pkts = 0;
2868 drop = 0;
2869 bytes = 0;
2870
2871 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002872 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002873 oq_stats = &oct->droq[oq_no]->stats;
2874 pkts += oq_stats->rx_pkts_received;
2875 drop += (oq_stats->rx_dropped +
2876 oq_stats->dropped_nodispatch +
2877 oq_stats->dropped_toomany +
2878 oq_stats->dropped_nomem);
2879 bytes += oq_stats->rx_bytes_received;
2880 }
2881
2882 stats->rx_bytes = bytes;
2883 stats->rx_packets = pkts;
2884 stats->rx_dropped = drop;
2885
2886 return stats;
2887}
2888
2889/**
2890 * \brief Net device change_mtu
2891 * @param netdev network device
2892 */
2893static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2894{
2895 struct lio *lio = GET_LIO(netdev);
2896 struct octeon_device *oct = lio->oct_dev;
2897 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002898 int ret = 0;
2899
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002900 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2901
2902 nctrl.ncmd.u64 = 0;
2903 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002904 nctrl.ncmd.s.param1 = new_mtu;
2905 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002906 nctrl.wait_time = 100;
2907 nctrl.netpndev = (u64)netdev;
2908 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2909
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002910 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002911 if (ret < 0) {
2912 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2913 return -1;
2914 }
2915
2916 lio->mtu = new_mtu;
2917
2918 return 0;
2919}
2920
2921/**
2922 * \brief Handler for SIOCSHWTSTAMP ioctl
2923 * @param netdev network device
2924 * @param ifr interface request
2925 * @param cmd command
2926 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002927static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002928{
2929 struct hwtstamp_config conf;
2930 struct lio *lio = GET_LIO(netdev);
2931
2932 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2933 return -EFAULT;
2934
2935 if (conf.flags)
2936 return -EINVAL;
2937
2938 switch (conf.tx_type) {
2939 case HWTSTAMP_TX_ON:
2940 case HWTSTAMP_TX_OFF:
2941 break;
2942 default:
2943 return -ERANGE;
2944 }
2945
2946 switch (conf.rx_filter) {
2947 case HWTSTAMP_FILTER_NONE:
2948 break;
2949 case HWTSTAMP_FILTER_ALL:
2950 case HWTSTAMP_FILTER_SOME:
2951 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2952 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2953 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2954 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2955 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2956 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2957 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2958 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2959 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2960 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2961 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2962 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2963 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2964 break;
2965 default:
2966 return -ERANGE;
2967 }
2968
2969 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2970 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2971
2972 else
2973 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2974
2975 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2976}
2977
2978/**
2979 * \brief ioctl handler
2980 * @param netdev network device
2981 * @param ifr interface request
2982 * @param cmd command
2983 */
2984static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2985{
2986 switch (cmd) {
2987 case SIOCSHWTSTAMP:
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002988 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002989 default:
2990 return -EOPNOTSUPP;
2991 }
2992}
2993
2994/**
2995 * \brief handle a Tx timestamp response
2996 * @param status response status
2997 * @param buf pointer to skb
2998 */
2999static void handle_timestamp(struct octeon_device *oct,
3000 u32 status,
3001 void *buf)
3002{
3003 struct octnet_buf_free_info *finfo;
3004 struct octeon_soft_command *sc;
3005 struct oct_timestamp_resp *resp;
3006 struct lio *lio;
3007 struct sk_buff *skb = (struct sk_buff *)buf;
3008
3009 finfo = (struct octnet_buf_free_info *)skb->cb;
3010 lio = finfo->lio;
3011 sc = finfo->sc;
3012 oct = lio->oct_dev;
3013 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3014
3015 if (status != OCTEON_REQUEST_DONE) {
3016 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3017 CVM_CAST64(status));
3018 resp->timestamp = 0;
3019 }
3020
3021 octeon_swap_8B_data(&resp->timestamp, 1);
3022
Colin Ian King19a6d152016-02-05 16:30:39 +00003023 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003024 struct skb_shared_hwtstamps ts;
3025 u64 ns = resp->timestamp;
3026
3027 netif_info(lio, tx_done, lio->netdev,
3028 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3029 skb, (unsigned long long)ns);
3030 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3031 skb_tstamp_tx(skb, &ts);
3032 }
3033
3034 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003035 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003036}
3037
3038/* \brief Send a data packet that will be timestamped
3039 * @param oct octeon device
3040 * @param ndata pointer to network data
3041 * @param finfo pointer to private network data
3042 */
3043static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3044 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003045 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003046{
3047 int retval;
3048 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003049 struct lio *lio;
3050 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003051 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003052
3053 lio = finfo->lio;
3054
3055 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3056 sizeof(struct oct_timestamp_resp));
3057 finfo->sc = sc;
3058
3059 if (!sc) {
3060 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3061 return IQ_SEND_FAILED;
3062 }
3063
3064 if (ndata->reqtype == REQTYPE_NORESP_NET)
3065 ndata->reqtype = REQTYPE_RESP_NET;
3066 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3067 ndata->reqtype = REQTYPE_RESP_NET_SG;
3068
3069 sc->callback = handle_timestamp;
3070 sc->callback_arg = finfo->skb;
3071 sc->iq_no = ndata->q_no;
3072
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003073 if (OCTEON_CN23XX_PF(oct))
3074 len = (u32)((struct octeon_instr_ih3 *)
3075 (&sc->cmd.cmd3.ih3))->dlengsz;
3076 else
3077 len = (u32)((struct octeon_instr_ih2 *)
3078 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003079
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003080 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003081
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003082 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003083 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003084
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003085 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003086 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3087 retval);
3088 octeon_free_soft_command(oct, sc);
3089 } else {
3090 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3091 }
3092
3093 return retval;
3094}
3095
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003096/** \brief Transmit networks packets to the Octeon interface
3097 * @param skbuff skbuff struct to be passed to network layer.
3098 * @param netdev pointer to network device
3099 * @returns whether the packet was transmitted to the device okay or not
3100 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3101 */
3102static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3103{
3104 struct lio *lio;
3105 struct octnet_buf_free_info *finfo;
3106 union octnic_cmd_setup cmdsetup;
3107 struct octnic_data_pkt ndata;
3108 struct octeon_device *oct;
3109 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003110 struct octeon_instr_irh *irh;
3111 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003112 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003113 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003114 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003115 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003116 u32 tag = 0;
3117
3118 lio = GET_LIO(netdev);
3119 oct = lio->oct_dev;
3120
3121 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003122 q_idx = skb->queue_mapping;
3123 q_idx = (q_idx % (lio->linfo.num_txpciq));
3124 tag = q_idx;
3125 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003126 } else {
3127 iq_no = lio->txq;
3128 }
3129
3130 stats = &oct->instr_queue[iq_no]->stats;
3131
3132 /* Check for all conditions in which the current packet cannot be
3133 * transmitted.
3134 */
3135 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003136 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003137 (skb->len <= 0)) {
3138 netif_info(lio, tx_err, lio->netdev,
3139 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003140 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003141 goto lio_xmit_failed;
3142 }
3143
3144 /* Use space in skb->cb to store info used to unmap and
3145 * free the buffers.
3146 */
3147 finfo = (struct octnet_buf_free_info *)skb->cb;
3148 finfo->lio = lio;
3149 finfo->skb = skb;
3150 finfo->sc = NULL;
3151
3152 /* Prepare the attributes for the data to be passed to OSI. */
3153 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3154
3155 ndata.buf = (void *)finfo;
3156
3157 ndata.q_no = iq_no;
3158
3159 if (netif_is_multiqueue(netdev)) {
3160 if (octnet_iq_is_full(oct, ndata.q_no)) {
3161 /* defer sending if queue is full */
3162 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3163 ndata.q_no);
3164 stats->tx_iq_busy++;
3165 return NETDEV_TX_BUSY;
3166 }
3167 } else {
3168 if (octnet_iq_is_full(oct, lio->txq)) {
3169 /* defer sending if queue is full */
3170 stats->tx_iq_busy++;
3171 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003172 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003173 return NETDEV_TX_BUSY;
3174 }
3175 }
3176 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003177 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003178 */
3179
3180 ndata.datasize = skb->len;
3181
3182 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003183 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003184
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003185 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3186 if (skb->encapsulation) {
3187 cmdsetup.s.tnl_csum = 1;
3188 stats->tx_vxlan++;
3189 } else {
3190 cmdsetup.s.transport_csum = 1;
3191 }
3192 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003193 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3194 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3195 cmdsetup.s.timestamp = 1;
3196 }
3197
3198 if (skb_shinfo(skb)->nr_frags == 0) {
3199 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003200 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003201
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003202 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003203 dptr = dma_map_single(&oct->pci_dev->dev,
3204 skb->data,
3205 skb->len,
3206 DMA_TO_DEVICE);
3207 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003208 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3209 __func__);
3210 return NETDEV_TX_BUSY;
3211 }
3212
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003213 if (OCTEON_CN23XX_PF(oct))
3214 ndata.cmd.cmd3.dptr = dptr;
3215 else
3216 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003217 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003218 ndata.reqtype = REQTYPE_NORESP_NET;
3219
3220 } else {
3221 int i, frags;
3222 struct skb_frag_struct *frag;
3223 struct octnic_gather *g;
3224
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003225 spin_lock(&lio->glist_lock[q_idx]);
3226 g = (struct octnic_gather *)
3227 list_delete_head(&lio->glist[q_idx]);
3228 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003229
3230 if (!g) {
3231 netif_info(lio, tx_err, lio->netdev,
3232 "Transmit scatter gather: glist null!\n");
3233 goto lio_xmit_failed;
3234 }
3235
3236 cmdsetup.s.gather = 1;
3237 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003238 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003239
3240 memset(g->sg, 0, g->sg_size);
3241
3242 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3243 skb->data,
3244 (skb->len - skb->data_len),
3245 DMA_TO_DEVICE);
3246 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3247 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3248 __func__);
3249 return NETDEV_TX_BUSY;
3250 }
3251 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3252
3253 frags = skb_shinfo(skb)->nr_frags;
3254 i = 1;
3255 while (frags--) {
3256 frag = &skb_shinfo(skb)->frags[i - 1];
3257
3258 g->sg[(i >> 2)].ptr[(i & 3)] =
3259 dma_map_page(&oct->pci_dev->dev,
3260 frag->page.p,
3261 frag->page_offset,
3262 frag->size,
3263 DMA_TO_DEVICE);
3264
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003265 if (dma_mapping_error(&oct->pci_dev->dev,
3266 g->sg[i >> 2].ptr[i & 3])) {
3267 dma_unmap_single(&oct->pci_dev->dev,
3268 g->sg[0].ptr[0],
3269 skb->len - skb->data_len,
3270 DMA_TO_DEVICE);
3271 for (j = 1; j < i; j++) {
3272 frag = &skb_shinfo(skb)->frags[j - 1];
3273 dma_unmap_page(&oct->pci_dev->dev,
3274 g->sg[j >> 2].ptr[j & 3],
3275 frag->size,
3276 DMA_TO_DEVICE);
3277 }
3278 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3279 __func__);
3280 return NETDEV_TX_BUSY;
3281 }
3282
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003283 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3284 i++;
3285 }
3286
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003287 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
3288 g->sg_size, DMA_TO_DEVICE);
3289 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003290
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003291 if (OCTEON_CN23XX_PF(oct))
3292 ndata.cmd.cmd3.dptr = dptr;
3293 else
3294 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003295 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003296 finfo->g = g;
3297
3298 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3299 }
3300
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003301 if (OCTEON_CN23XX_PF(oct)) {
3302 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3303 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3304 } else {
3305 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3306 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3307 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003308
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003309 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003310 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3311 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003312 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003313 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003314
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003315 /* HW insert VLAN tag */
3316 if (skb_vlan_tag_present(skb)) {
3317 irh->priority = skb_vlan_tag_get(skb) >> 13;
3318 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3319 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003320
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003321 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003322 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003323 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003324 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003325 if (status == IQ_SEND_FAILED)
3326 goto lio_xmit_failed;
3327
3328 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3329
3330 if (status == IQ_SEND_STOP)
3331 stop_q(lio->netdev, q_idx);
3332
Florian Westphal860e9532016-05-03 16:33:13 +02003333 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003334
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003335 if (skb_shinfo(skb)->gso_size)
3336 stats->tx_done += skb_shinfo(skb)->gso_segs;
3337 else
3338 stats->tx_done++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003339 stats->tx_tot_bytes += skb->len;
3340
3341 return NETDEV_TX_OK;
3342
3343lio_xmit_failed:
3344 stats->tx_dropped++;
3345 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3346 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003347 if (dptr)
3348 dma_unmap_single(&oct->pci_dev->dev, dptr,
3349 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003350 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003351 return NETDEV_TX_OK;
3352}
3353
3354/** \brief Network device Tx timeout
3355 * @param netdev pointer to network device
3356 */
3357static void liquidio_tx_timeout(struct net_device *netdev)
3358{
3359 struct lio *lio;
3360
3361 lio = GET_LIO(netdev);
3362
3363 netif_info(lio, tx_err, lio->netdev,
3364 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3365 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003366 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003367 txqs_wake(netdev);
3368}
3369
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003370static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3371 __be16 proto __attribute__((unused)),
3372 u16 vid)
3373{
3374 struct lio *lio = GET_LIO(netdev);
3375 struct octeon_device *oct = lio->oct_dev;
3376 struct octnic_ctrl_pkt nctrl;
3377 int ret = 0;
3378
3379 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3380
3381 nctrl.ncmd.u64 = 0;
3382 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3383 nctrl.ncmd.s.param1 = vid;
3384 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3385 nctrl.wait_time = 100;
3386 nctrl.netpndev = (u64)netdev;
3387 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3388
3389 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3390 if (ret < 0) {
3391 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3392 ret);
3393 }
3394
3395 return ret;
3396}
3397
3398static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3399 __be16 proto __attribute__((unused)),
3400 u16 vid)
3401{
3402 struct lio *lio = GET_LIO(netdev);
3403 struct octeon_device *oct = lio->oct_dev;
3404 struct octnic_ctrl_pkt nctrl;
3405 int ret = 0;
3406
3407 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3408
3409 nctrl.ncmd.u64 = 0;
3410 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3411 nctrl.ncmd.s.param1 = vid;
3412 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3413 nctrl.wait_time = 100;
3414 nctrl.netpndev = (u64)netdev;
3415 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3416
3417 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3418 if (ret < 0) {
3419 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3420 ret);
3421 }
3422 return ret;
3423}
3424
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003425/** Sending command to enable/disable RX checksum offload
3426 * @param netdev pointer to network device
3427 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3428 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3429 * OCTNET_CMD_RXCSUM_DISABLE
3430 * @returns SUCCESS or FAILURE
3431 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003432static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3433 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003434{
3435 struct lio *lio = GET_LIO(netdev);
3436 struct octeon_device *oct = lio->oct_dev;
3437 struct octnic_ctrl_pkt nctrl;
3438 int ret = 0;
3439
3440 nctrl.ncmd.u64 = 0;
3441 nctrl.ncmd.s.cmd = command;
3442 nctrl.ncmd.s.param1 = rx_cmd;
3443 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3444 nctrl.wait_time = 100;
3445 nctrl.netpndev = (u64)netdev;
3446 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3447
3448 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3449 if (ret < 0) {
3450 dev_err(&oct->pci_dev->dev,
3451 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3452 ret);
3453 }
3454 return ret;
3455}
3456
3457/** Sending command to add/delete VxLAN UDP port to firmware
3458 * @param netdev pointer to network device
3459 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3460 * @param vxlan_port VxLAN port to be added or deleted
3461 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3462 * OCTNET_CMD_VXLAN_PORT_DEL
3463 * @returns SUCCESS or FAILURE
3464 */
3465static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3466 u16 vxlan_port, u8 vxlan_cmd_bit)
3467{
3468 struct lio *lio = GET_LIO(netdev);
3469 struct octeon_device *oct = lio->oct_dev;
3470 struct octnic_ctrl_pkt nctrl;
3471 int ret = 0;
3472
3473 nctrl.ncmd.u64 = 0;
3474 nctrl.ncmd.s.cmd = command;
3475 nctrl.ncmd.s.more = vxlan_cmd_bit;
3476 nctrl.ncmd.s.param1 = vxlan_port;
3477 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3478 nctrl.wait_time = 100;
3479 nctrl.netpndev = (u64)netdev;
3480 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3481
3482 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3483 if (ret < 0) {
3484 dev_err(&oct->pci_dev->dev,
3485 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3486 ret);
3487 }
3488 return ret;
3489}
3490
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003491/** \brief Net device fix features
3492 * @param netdev pointer to network device
3493 * @param request features requested
3494 * @returns updated features list
3495 */
3496static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3497 netdev_features_t request)
3498{
3499 struct lio *lio = netdev_priv(netdev);
3500
3501 if ((request & NETIF_F_RXCSUM) &&
3502 !(lio->dev_capability & NETIF_F_RXCSUM))
3503 request &= ~NETIF_F_RXCSUM;
3504
3505 if ((request & NETIF_F_HW_CSUM) &&
3506 !(lio->dev_capability & NETIF_F_HW_CSUM))
3507 request &= ~NETIF_F_HW_CSUM;
3508
3509 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3510 request &= ~NETIF_F_TSO;
3511
3512 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3513 request &= ~NETIF_F_TSO6;
3514
3515 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3516 request &= ~NETIF_F_LRO;
3517
3518 /*Disable LRO if RXCSUM is off */
3519 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3520 (lio->dev_capability & NETIF_F_LRO))
3521 request &= ~NETIF_F_LRO;
3522
3523 return request;
3524}
3525
3526/** \brief Net device set features
3527 * @param netdev pointer to network device
3528 * @param features features to enable/disable
3529 */
3530static int liquidio_set_features(struct net_device *netdev,
3531 netdev_features_t features)
3532{
3533 struct lio *lio = netdev_priv(netdev);
3534
3535 if (!((netdev->features ^ features) & NETIF_F_LRO))
3536 return 0;
3537
3538 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003539 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3540 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003541 else if (!(features & NETIF_F_LRO) &&
3542 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003543 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3544 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003545
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003546 /* Sending command to firmware to enable/disable RX checksum
3547 * offload settings using ethtool
3548 */
3549 if (!(netdev->features & NETIF_F_RXCSUM) &&
3550 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3551 (features & NETIF_F_RXCSUM))
3552 liquidio_set_rxcsum_command(netdev,
3553 OCTNET_CMD_TNL_RX_CSUM_CTL,
3554 OCTNET_CMD_RXCSUM_ENABLE);
3555 else if ((netdev->features & NETIF_F_RXCSUM) &&
3556 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3557 !(features & NETIF_F_RXCSUM))
3558 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3559 OCTNET_CMD_RXCSUM_DISABLE);
3560
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003561 return 0;
3562}
3563
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003564static void liquidio_add_vxlan_port(struct net_device *netdev,
3565 struct udp_tunnel_info *ti)
3566{
3567 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3568 return;
3569
3570 liquidio_vxlan_port_command(netdev,
3571 OCTNET_CMD_VXLAN_PORT_CONFIG,
3572 htons(ti->port),
3573 OCTNET_CMD_VXLAN_PORT_ADD);
3574}
3575
3576static void liquidio_del_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_DEL);
3586}
3587
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003588static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3589 u8 *mac, bool is_admin_assigned)
3590{
3591 struct lio *lio = GET_LIO(netdev);
3592 struct octeon_device *oct = lio->oct_dev;
3593 struct octnic_ctrl_pkt nctrl;
3594
3595 if (!is_valid_ether_addr(mac))
3596 return -EINVAL;
3597
3598 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3599 return -EINVAL;
3600
3601 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3602
3603 nctrl.ncmd.u64 = 0;
3604 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3605 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3606 nctrl.ncmd.s.param1 = vfidx + 1;
3607 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3608 nctrl.ncmd.s.more = 1;
3609 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3610 nctrl.cb_fn = 0;
3611 nctrl.wait_time = LIO_CMD_WAIT_TM;
3612
3613 nctrl.udd[0] = 0;
3614 /* The MAC Address is presented in network byte order. */
3615 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3616
3617 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3618
3619 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3620
3621 return 0;
3622}
3623
3624static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3625{
3626 struct lio *lio = GET_LIO(netdev);
3627 struct octeon_device *oct = lio->oct_dev;
3628 int retval;
3629
3630 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3631 if (!retval)
3632 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3633
3634 return retval;
3635}
3636
3637static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3638 u16 vlan, u8 qos, __be16 vlan_proto)
3639{
3640 struct lio *lio = GET_LIO(netdev);
3641 struct octeon_device *oct = lio->oct_dev;
3642 struct octnic_ctrl_pkt nctrl;
3643 u16 vlantci;
3644
3645 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3646 return -EINVAL;
3647
3648 if (vlan_proto != htons(ETH_P_8021Q))
3649 return -EPROTONOSUPPORT;
3650
3651 if (vlan >= VLAN_N_VID || qos > 7)
3652 return -EINVAL;
3653
3654 if (vlan)
3655 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3656 else
3657 vlantci = 0;
3658
3659 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3660 return 0;
3661
3662 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3663
3664 if (vlan)
3665 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3666 else
3667 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3668
3669 nctrl.ncmd.s.param1 = vlantci;
3670 nctrl.ncmd.s.param2 =
3671 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3672 nctrl.ncmd.s.more = 0;
3673 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3674 nctrl.cb_fn = 0;
3675 nctrl.wait_time = LIO_CMD_WAIT_TM;
3676
3677 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3678
3679 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3680
3681 return 0;
3682}
3683
3684static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3685 struct ifla_vf_info *ivi)
3686{
3687 struct lio *lio = GET_LIO(netdev);
3688 struct octeon_device *oct = lio->oct_dev;
3689 u8 *macaddr;
3690
3691 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3692 return -EINVAL;
3693
3694 ivi->vf = vfidx;
3695 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3696 ether_addr_copy(&ivi->mac[0], macaddr);
3697 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3698 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3699 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3700 return 0;
3701}
3702
3703static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3704 int linkstate)
3705{
3706 struct lio *lio = GET_LIO(netdev);
3707 struct octeon_device *oct = lio->oct_dev;
3708 struct octnic_ctrl_pkt nctrl;
3709
3710 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3711 return -EINVAL;
3712
3713 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3714 return 0;
3715
3716 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3717 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3718 nctrl.ncmd.s.param1 =
3719 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3720 nctrl.ncmd.s.param2 = linkstate;
3721 nctrl.ncmd.s.more = 0;
3722 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3723 nctrl.cb_fn = 0;
3724 nctrl.wait_time = LIO_CMD_WAIT_TM;
3725
3726 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3727
3728 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3729
3730 return 0;
3731}
3732
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003733static struct net_device_ops lionetdevops = {
3734 .ndo_open = liquidio_open,
3735 .ndo_stop = liquidio_stop,
3736 .ndo_start_xmit = liquidio_xmit,
3737 .ndo_get_stats = liquidio_get_stats,
3738 .ndo_set_mac_address = liquidio_set_mac,
3739 .ndo_set_rx_mode = liquidio_set_mcast_list,
3740 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003741
3742 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3743 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003744 .ndo_change_mtu = liquidio_change_mtu,
3745 .ndo_do_ioctl = liquidio_ioctl,
3746 .ndo_fix_features = liquidio_fix_features,
3747 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003748 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3749 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003750 .ndo_set_vf_mac = liquidio_set_vf_mac,
3751 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3752 .ndo_get_vf_config = liquidio_get_vf_config,
3753 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003754};
3755
3756/** \brief Entry point for the liquidio module
3757 */
3758static int __init liquidio_init(void)
3759{
3760 int i;
3761 struct handshake *hs;
3762
3763 init_completion(&first_stage);
3764
3765 octeon_init_device_list(conf_type);
3766
3767 if (liquidio_init_pci())
3768 return -EINVAL;
3769
3770 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3771
3772 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3773 hs = &handshake[i];
3774 if (hs->pci_dev) {
3775 wait_for_completion(&hs->init);
3776 if (!hs->init_ok) {
3777 /* init handshake failed */
3778 dev_err(&hs->pci_dev->dev,
3779 "Failed to init device\n");
3780 liquidio_deinit_pci();
3781 return -EIO;
3782 }
3783 }
3784 }
3785
3786 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3787 hs = &handshake[i];
3788 if (hs->pci_dev) {
3789 wait_for_completion_timeout(&hs->started,
3790 msecs_to_jiffies(30000));
3791 if (!hs->started_ok) {
3792 /* starter handshake failed */
3793 dev_err(&hs->pci_dev->dev,
3794 "Firmware failed to start\n");
3795 liquidio_deinit_pci();
3796 return -EIO;
3797 }
3798 }
3799 }
3800
3801 return 0;
3802}
3803
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003804static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003805{
3806 struct octeon_device *oct = (struct octeon_device *)buf;
3807 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003808 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003809 union oct_link_status *ls;
3810 int i;
3811
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003812 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003813 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3814 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003815 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003816 goto nic_info_err;
3817 }
3818
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003819 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003820 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3821
3822 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003823 for (i = 0; i < oct->ifcount; i++) {
3824 if (oct->props[i].gmxport == gmxport) {
3825 update_link_status(oct->props[i].netdev, ls);
3826 break;
3827 }
3828 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003829
3830nic_info_err:
3831 for (i = 0; i < recv_pkt->buffer_count; i++)
3832 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3833 octeon_free_recv_info(recv_info);
3834 return 0;
3835}
3836
3837/**
3838 * \brief Setup network interfaces
3839 * @param octeon_dev octeon device
3840 *
3841 * Called during init time for each device. It assumes the NIC
3842 * is already up and running. The link information for each
3843 * interface is passed in link_info.
3844 */
3845static int setup_nic_devices(struct octeon_device *octeon_dev)
3846{
3847 struct lio *lio = NULL;
3848 struct net_device *netdev;
3849 u8 mac[6], i, j;
3850 struct octeon_soft_command *sc;
3851 struct liquidio_if_cfg_context *ctx;
3852 struct liquidio_if_cfg_resp *resp;
3853 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003854 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003855 union oct_nic_if_cfg if_cfg;
3856 unsigned int base_queue;
3857 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003858 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003859 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003860 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003861
3862 /* This is to handle link status changes */
3863 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3864 OPCODE_NIC_INFO,
3865 lio_nic_info, octeon_dev);
3866
3867 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3868 * They are handled directly.
3869 */
3870 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3871 free_netbuf);
3872
3873 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3874 free_netsgbuf);
3875
3876 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3877 free_netsgbuf_with_resp);
3878
3879 for (i = 0; i < octeon_dev->ifcount; i++) {
3880 resp_size = sizeof(struct liquidio_if_cfg_resp);
3881 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003882 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003883 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003884 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003885 resp_size, ctx_size);
3886 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3887 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003888 vdata = (struct lio_version *)sc->virtdptr;
3889
3890 *((u64 *)vdata) = 0;
3891 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3892 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3893 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003894
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003895 if (OCTEON_CN23XX_PF(octeon_dev)) {
3896 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3897 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3898 base_queue = octeon_dev->sriov_info.pf_srn;
3899
3900 gmx_port_id = octeon_dev->pf_num;
3901 ifidx_or_pfnum = octeon_dev->pf_num;
3902 } else {
3903 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3904 octeon_get_conf(octeon_dev), i);
3905 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3906 octeon_get_conf(octeon_dev), i);
3907 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3908 octeon_get_conf(octeon_dev), i);
3909 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3910 octeon_get_conf(octeon_dev), i);
3911 ifidx_or_pfnum = i;
3912 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003913
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003914 dev_dbg(&octeon_dev->pci_dev->dev,
3915 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003916 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003917 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003918 ctx->octeon_id = lio_get_device_id(octeon_dev);
3919 init_waitqueue_head(&ctx->wc);
3920
3921 if_cfg.u64 = 0;
3922 if_cfg.s.num_iqueues = num_iqueues;
3923 if_cfg.s.num_oqueues = num_oqueues;
3924 if_cfg.s.base_queue = base_queue;
3925 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003926
3927 sc->iq_no = 0;
3928
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003929 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003930 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003931 if_cfg.u64, 0);
3932
3933 sc->callback = if_cfg_callback;
3934 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003935 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003936
3937 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003938 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003939 dev_err(&octeon_dev->pci_dev->dev,
3940 "iq/oq config failed status: %x\n",
3941 retval);
3942 /* Soft instr is freed by driver in case of failure. */
3943 goto setup_nic_dev_fail;
3944 }
3945
3946 /* Sleep on a wait queue till the cond flag indicates that the
3947 * response arrived or timed-out.
3948 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003949 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3950 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3951 goto setup_nic_wait_intr;
3952 }
3953
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003954 retval = resp->status;
3955 if (retval) {
3956 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3957 goto setup_nic_dev_fail;
3958 }
3959
3960 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3961 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3962
3963 num_iqueues = hweight64(resp->cfg_info.iqmask);
3964 num_oqueues = hweight64(resp->cfg_info.oqmask);
3965
3966 if (!(num_iqueues) || !(num_oqueues)) {
3967 dev_err(&octeon_dev->pci_dev->dev,
3968 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3969 resp->cfg_info.iqmask,
3970 resp->cfg_info.oqmask);
3971 goto setup_nic_dev_fail;
3972 }
3973 dev_dbg(&octeon_dev->pci_dev->dev,
3974 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3975 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3976 num_iqueues, num_oqueues);
3977 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3978
3979 if (!netdev) {
3980 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3981 goto setup_nic_dev_fail;
3982 }
3983
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003984 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003985
3986 if (num_iqueues > 1)
3987 lionetdevops.ndo_select_queue = select_q;
3988
3989 /* Associate the routines that will handle different
3990 * netdev tasks.
3991 */
3992 netdev->netdev_ops = &lionetdevops;
3993
3994 lio = GET_LIO(netdev);
3995
3996 memset(lio, 0, sizeof(struct lio));
3997
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003998 lio->ifidx = ifidx_or_pfnum;
3999
4000 props = &octeon_dev->props[i];
4001 props->gmxport = resp->cfg_info.linfo.gmxport;
4002 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004003
4004 lio->linfo.num_rxpciq = num_oqueues;
4005 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004006 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004007 lio->linfo.rxpciq[j].u64 =
4008 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004009 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004010 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004011 lio->linfo.txpciq[j].u64 =
4012 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004013 }
4014 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4015 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4016 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4017
4018 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4019
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004020 if (OCTEON_CN23XX_PF(octeon_dev) ||
4021 OCTEON_CN6XXX(octeon_dev)) {
4022 lio->dev_capability = NETIF_F_HIGHDMA
4023 | NETIF_F_IP_CSUM
4024 | NETIF_F_IPV6_CSUM
4025 | NETIF_F_SG | NETIF_F_RXCSUM
4026 | NETIF_F_GRO
4027 | NETIF_F_TSO | NETIF_F_TSO6
4028 | NETIF_F_LRO;
4029 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004030 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4031
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004032 /* Copy of transmit encapsulation capabilities:
4033 * TSO, TSO6, Checksums for this device
4034 */
4035 lio->enc_dev_capability = NETIF_F_IP_CSUM
4036 | NETIF_F_IPV6_CSUM
4037 | NETIF_F_GSO_UDP_TUNNEL
4038 | NETIF_F_HW_CSUM | NETIF_F_SG
4039 | NETIF_F_RXCSUM
4040 | NETIF_F_TSO | NETIF_F_TSO6
4041 | NETIF_F_LRO;
4042
4043 netdev->hw_enc_features = (lio->enc_dev_capability &
4044 ~NETIF_F_LRO);
4045
4046 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4047
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004048 netdev->vlan_features = lio->dev_capability;
4049 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004050 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4051 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004052 NETIF_F_HW_VLAN_CTAG_TX;
4053
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004054 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4055
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004056 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004057 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4058 netdev->hw_features = netdev->hw_features &
4059 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004060
Jarod Wilson109cc162016-10-17 15:54:13 -04004061 /* MTU range: 68 - 16000 */
4062 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4063 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4064
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004065 /* Point to the properties for octeon device to which this
4066 * interface belongs.
4067 */
4068 lio->oct_dev = octeon_dev;
4069 lio->octprops = props;
4070 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004071
4072 dev_dbg(&octeon_dev->pci_dev->dev,
4073 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4074 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4075
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004076 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4077 u8 vfmac[ETH_ALEN];
4078
4079 random_ether_addr(&vfmac[0]);
4080 if (__liquidio_set_vf_mac(netdev, j,
4081 &vfmac[0], false)) {
4082 dev_err(&octeon_dev->pci_dev->dev,
4083 "Error setting VF%d MAC address\n",
4084 j);
4085 goto setup_nic_dev_fail;
4086 }
4087 }
4088
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004089 /* 64-bit swap required on LE machines */
4090 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4091 for (j = 0; j < 6; j++)
4092 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4093
4094 /* Copy MAC Address to OS network device structure */
4095
4096 ether_addr_copy(netdev->dev_addr, mac);
4097
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004098 /* By default all interfaces on a single Octeon uses the same
4099 * tx and rx queues
4100 */
4101 lio->txq = lio->linfo.txpciq[0].s.q_no;
4102 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004103 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004104 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4105 goto setup_nic_dev_fail;
4106 }
4107
4108 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4109
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004110 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4111 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4112
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004113 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004114 dev_err(&octeon_dev->pci_dev->dev,
4115 "Gather list allocation failed\n");
4116 goto setup_nic_dev_fail;
4117 }
4118
4119 /* Register ethtool support */
4120 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004121 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4122 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4123 else
4124 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004125
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004126 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004127 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4128 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004129
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004130 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
4131
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004132 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004133 liquidio_set_feature(netdev,
4134 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004135
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004136 if (setup_link_status_change_wq(netdev))
4137 goto setup_nic_dev_fail;
4138
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004139 /* Register the network device with the OS */
4140 if (register_netdev(netdev)) {
4141 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4142 goto setup_nic_dev_fail;
4143 }
4144
4145 dev_dbg(&octeon_dev->pci_dev->dev,
4146 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4147 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4148 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004149 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004150
4151 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4152
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004153 /* Sending command to firmware to enable Rx checksum offload
4154 * by default at the time of setup of Liquidio driver for
4155 * this device
4156 */
4157 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4158 OCTNET_CMD_RXCSUM_ENABLE);
4159 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4160 OCTNET_CMD_TXCSUM_ENABLE);
4161
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004162 dev_dbg(&octeon_dev->pci_dev->dev,
4163 "NIC ifidx:%d Setup successful\n", i);
4164
4165 octeon_free_soft_command(octeon_dev, sc);
4166 }
4167
4168 return 0;
4169
4170setup_nic_dev_fail:
4171
4172 octeon_free_soft_command(octeon_dev, sc);
4173
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004174setup_nic_wait_intr:
4175
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004176 while (i--) {
4177 dev_err(&octeon_dev->pci_dev->dev,
4178 "NIC ifidx:%d Setup failed\n", i);
4179 liquidio_destroy_nic_device(octeon_dev, i);
4180 }
4181 return -ENODEV;
4182}
4183
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004184#ifdef CONFIG_PCI_IOV
4185static int octeon_enable_sriov(struct octeon_device *oct)
4186{
4187 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4188 struct pci_dev *vfdev;
4189 int err;
4190 u32 u;
4191
4192 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4193 err = pci_enable_sriov(oct->pci_dev,
4194 oct->sriov_info.num_vfs_alloced);
4195 if (err) {
4196 dev_err(&oct->pci_dev->dev,
4197 "OCTEON: Failed to enable PCI sriov: %d\n",
4198 err);
4199 oct->sriov_info.num_vfs_alloced = 0;
4200 return err;
4201 }
4202 oct->sriov_info.sriov_enabled = 1;
4203
4204 /* init lookup table that maps DPI ring number to VF pci_dev
4205 * struct pointer
4206 */
4207 u = 0;
4208 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4209 OCTEON_CN23XX_VF_VID, NULL);
4210 while (vfdev) {
4211 if (vfdev->is_virtfn &&
4212 (vfdev->physfn == oct->pci_dev)) {
4213 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4214 vfdev;
4215 u += oct->sriov_info.rings_per_vf;
4216 }
4217 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4218 OCTEON_CN23XX_VF_VID, vfdev);
4219 }
4220 }
4221
4222 return num_vfs_alloced;
4223}
4224
4225static int lio_pci_sriov_disable(struct octeon_device *oct)
4226{
4227 int u;
4228
4229 if (pci_vfs_assigned(oct->pci_dev)) {
4230 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4231 return -EPERM;
4232 }
4233
4234 pci_disable_sriov(oct->pci_dev);
4235
4236 u = 0;
4237 while (u < MAX_POSSIBLE_VFS) {
4238 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4239 u += oct->sriov_info.rings_per_vf;
4240 }
4241
4242 oct->sriov_info.num_vfs_alloced = 0;
4243 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4244 oct->pf_num);
4245
4246 return 0;
4247}
4248
4249static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4250{
4251 struct octeon_device *oct = pci_get_drvdata(dev);
4252 int ret = 0;
4253
4254 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4255 (oct->sriov_info.sriov_enabled)) {
4256 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4257 oct->pf_num, num_vfs);
4258 return 0;
4259 }
4260
4261 if (!num_vfs) {
4262 ret = lio_pci_sriov_disable(oct);
4263 } else if (num_vfs > oct->sriov_info.max_vfs) {
4264 dev_err(&oct->pci_dev->dev,
4265 "OCTEON: Max allowed VFs:%d user requested:%d",
4266 oct->sriov_info.max_vfs, num_vfs);
4267 ret = -EPERM;
4268 } else {
4269 oct->sriov_info.num_vfs_alloced = num_vfs;
4270 ret = octeon_enable_sriov(oct);
4271 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4272 oct->pf_num, num_vfs);
4273 }
4274
4275 return ret;
4276}
4277#endif
4278
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004279/**
4280 * \brief initialize the NIC
4281 * @param oct octeon device
4282 *
4283 * This initialization routine is called once the Octeon device application is
4284 * up and running
4285 */
4286static int liquidio_init_nic_module(struct octeon_device *oct)
4287{
4288 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004289 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004290 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4291
4292 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4293
4294 /* only default iq and oq were initialized
4295 * initialize the rest as well
4296 */
4297 /* run port_config command for each port */
4298 oct->ifcount = num_nic_ports;
4299
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004300 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004301
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004302 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4303 oct->props[i].gmxport = -1;
4304
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004305 retval = setup_nic_devices(oct);
4306 if (retval) {
4307 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4308 goto octnet_init_failure;
4309 }
4310
4311 liquidio_ptp_init(oct);
4312
4313 /* Initialize interrupt moderation params */
4314 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004315 intrmod_cfg->rx_enable = 1;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004316 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07004317 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
4318 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
4319 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
4320 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
4321 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
4322 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
4323 intrmod_cfg->tx_enable = 1;
4324 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
4325 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
4326 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
4327 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07004328 intrmod_cfg->tx_frames = CFG_GET_IQ_INTR_PKT(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004329 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4330
4331 return retval;
4332
4333octnet_init_failure:
4334
4335 oct->ifcount = 0;
4336
4337 return retval;
4338}
4339
4340/**
4341 * \brief starter callback that invokes the remaining initialization work after
4342 * the NIC is up and running.
4343 * @param octptr work struct work_struct
4344 */
4345static void nic_starter(struct work_struct *work)
4346{
4347 struct octeon_device *oct;
4348 struct cavium_wk *wk = (struct cavium_wk *)work;
4349
4350 oct = (struct octeon_device *)wk->ctxptr;
4351
4352 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4353 return;
4354
4355 /* If the status of the device is CORE_OK, the core
4356 * application has reported its application type. Call
4357 * any registered handlers now and move to the RUNNING
4358 * state.
4359 */
4360 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4361 schedule_delayed_work(&oct->nic_poll_work.work,
4362 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4363 return;
4364 }
4365
4366 atomic_set(&oct->status, OCT_DEV_RUNNING);
4367
4368 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4369 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4370
4371 if (liquidio_init_nic_module(oct))
4372 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4373 else
4374 handshake[oct->octeon_id].started_ok = 1;
4375 } else {
4376 dev_err(&oct->pci_dev->dev,
4377 "Unexpected application running on NIC (%d). Check firmware.\n",
4378 oct->app_mode);
4379 }
4380
4381 complete(&handshake[oct->octeon_id].started);
4382}
4383
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004384static int
4385octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4386{
4387 struct octeon_device *oct = (struct octeon_device *)buf;
4388 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4389 int i, notice, vf_idx;
4390 u64 *data, vf_num;
4391
4392 notice = recv_pkt->rh.r.ossp;
4393 data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
4394
4395 /* the first 64-bit word of data is the vf_num */
4396 vf_num = data[0];
4397 octeon_swap_8B_data(&vf_num, 1);
4398 vf_idx = (int)vf_num - 1;
4399
4400 if (notice == VF_DRV_LOADED) {
4401 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4402 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4403 dev_info(&oct->pci_dev->dev,
4404 "driver for VF%d was loaded\n", vf_idx);
4405 try_module_get(THIS_MODULE);
4406 }
4407 } else if (notice == VF_DRV_REMOVED) {
4408 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4409 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4410 dev_info(&oct->pci_dev->dev,
4411 "driver for VF%d was removed\n", vf_idx);
4412 module_put(THIS_MODULE);
4413 }
4414 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4415 u8 *b = (u8 *)&data[1];
4416
4417 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4418 dev_info(&oct->pci_dev->dev,
4419 "VF driver changed VF%d's MAC address to %pM\n",
4420 vf_idx, b + 2);
4421 }
4422
4423 for (i = 0; i < recv_pkt->buffer_count; i++)
4424 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4425 octeon_free_recv_info(recv_info);
4426
4427 return 0;
4428}
4429
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004430/**
4431 * \brief Device initialization for each Octeon device that is probed
4432 * @param octeon_dev octeon device
4433 */
4434static int octeon_device_init(struct octeon_device *octeon_dev)
4435{
4436 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004437 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004438 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004439 struct octeon_device_priv *oct_priv =
4440 (struct octeon_device_priv *)octeon_dev->priv;
4441 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4442
4443 /* Enable access to the octeon device and make its DMA capability
4444 * known to the OS.
4445 */
4446 if (octeon_pci_os_setup(octeon_dev))
4447 return 1;
4448
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004449 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4450
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004451 /* Identify the Octeon type and map the BAR address space. */
4452 if (octeon_chip_specific_setup(octeon_dev)) {
4453 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4454 return 1;
4455 }
4456
4457 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4458
4459 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4460
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004461 if (OCTEON_CN23XX_PF(octeon_dev)) {
4462 if (!cn23xx_fw_loaded(octeon_dev)) {
4463 fw_loaded = 0;
4464 /* Do a soft reset of the Octeon device. */
4465 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4466 return 1;
4467 /* things might have changed */
4468 if (!cn23xx_fw_loaded(octeon_dev))
4469 fw_loaded = 0;
4470 else
4471 fw_loaded = 1;
4472 } else {
4473 fw_loaded = 1;
4474 }
4475 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004476 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004477 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004478
4479 /* Initialize the dispatch mechanism used to push packets arriving on
4480 * Octeon Output queues.
4481 */
4482 if (octeon_init_dispatch_list(octeon_dev))
4483 return 1;
4484
4485 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4486 OPCODE_NIC_CORE_DRV_ACTIVE,
4487 octeon_core_drv_init,
4488 octeon_dev);
4489
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004490 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4491 OPCODE_NIC_VF_DRV_NOTICE,
4492 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004493 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4494 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4495 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4496 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4497
4498 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4499
4500 octeon_set_io_queues_off(octeon_dev);
4501
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004502 if (OCTEON_CN23XX_PF(octeon_dev)) {
4503 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4504 if (ret) {
4505 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4506 return ret;
4507 }
4508 }
4509
4510 /* Initialize soft command buffer pool
4511 */
4512 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4513 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4514 return 1;
4515 }
4516 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4517
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004518 /* Setup the data structures that manage this Octeon's Input queues. */
4519 if (octeon_setup_instr_queues(octeon_dev)) {
4520 dev_err(&octeon_dev->pci_dev->dev,
4521 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004522 return 1;
4523 }
4524 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4525
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004526 /* Initialize lists to manage the requests of different types that
4527 * arrive from user & kernel applications for this octeon device.
4528 */
4529 if (octeon_setup_response_list(octeon_dev)) {
4530 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4531 return 1;
4532 }
4533 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4534
4535 if (octeon_setup_output_queues(octeon_dev)) {
4536 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004537 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004538 }
4539
4540 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4541
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004542 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004543 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4544 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4545 return 1;
4546 }
4547 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4548
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004549 if (octeon_allocate_ioq_vector(octeon_dev)) {
4550 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4551 return 1;
4552 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004553 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004554
4555 } else {
4556 /* The input and output queue registers were setup earlier (the
4557 * queues were not enabled). Any additional registers
4558 * that need to be programmed should be done now.
4559 */
4560 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4561 if (ret) {
4562 dev_err(&octeon_dev->pci_dev->dev,
4563 "Failed to configure device registers\n");
4564 return ret;
4565 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004566 }
4567
4568 /* Initialize the tasklet that handles output queue packet processing.*/
4569 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4570 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4571 (unsigned long)octeon_dev);
4572
4573 /* Setup the interrupt handler and record the INT SUM register address
4574 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004575 if (octeon_setup_interrupt(octeon_dev))
4576 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004577
4578 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004579 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004580
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004581 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4582
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004583 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004584 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4585 if (ret) {
4586 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4587 return ret;
4588 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004589
4590 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4591
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004592 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4593 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4594 if (!ddr_timeout) {
4595 dev_info(&octeon_dev->pci_dev->dev,
4596 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4597 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004598
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004599 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004600
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004601 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4602 while (!ddr_timeout) {
4603 set_current_state(TASK_INTERRUPTIBLE);
4604 if (schedule_timeout(HZ / 10)) {
4605 /* user probably pressed Control-C */
4606 return 1;
4607 }
4608 }
4609 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4610 if (ret) {
4611 dev_err(&octeon_dev->pci_dev->dev,
4612 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4613 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004614 return 1;
4615 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004616
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004617 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4618 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4619 return 1;
4620 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004621
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004622 /* Divert uboot to take commands from host instead. */
4623 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004624
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004625 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4626 ret = octeon_init_consoles(octeon_dev);
4627 if (ret) {
4628 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4629 return 1;
4630 }
4631 ret = octeon_add_console(octeon_dev, 0);
4632 if (ret) {
4633 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4634 return 1;
4635 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004636
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004637 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004638
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004639 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4640 ret = load_firmware(octeon_dev);
4641 if (ret) {
4642 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4643 return 1;
4644 }
4645 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4646 * loaded
4647 */
4648 if (OCTEON_CN23XX_PF(octeon_dev))
4649 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4650 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004651 }
4652
4653 handshake[octeon_dev->octeon_id].init_ok = 1;
4654 complete(&handshake[octeon_dev->octeon_id].init);
4655
4656 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4657
4658 /* Send Credit for Octeon Output queues. Credits are always sent after
4659 * the output queue is enabled.
4660 */
4661 for (j = 0; j < octeon_dev->num_oqs; j++)
4662 writel(octeon_dev->droq[j]->max_count,
4663 octeon_dev->droq[j]->pkts_credit_reg);
4664
4665 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004666 return 0;
4667}
4668
4669/**
4670 * \brief Exits the module
4671 */
4672static void __exit liquidio_exit(void)
4673{
4674 liquidio_deinit_pci();
4675
4676 pr_info("LiquidIO network module is now unloaded\n");
4677}
4678
4679module_init(liquidio_init);
4680module_exit(liquidio_exit);