blob: 38b7ea591d04bc9a41c743165ec1eefb06da8de8 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
Russell Kinge3bfc6e2017-02-07 15:03:03 -080018#include <linux/module.h>
Florian Westphal282ccf62017-03-29 17:17:31 +020019#include <linux/interrupt.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070020#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070021#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070022#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070023#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include "liquidio_common.h"
25#include "octeon_droq.h"
26#include "octeon_iq.h"
27#include "response_manager.h"
28#include "octeon_device.h"
29#include "octeon_nic.h"
30#include "octeon_main.h"
31#include "octeon_network.h"
32#include "cn66xx_regs.h"
33#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070034#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070035#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070036#include "liquidio_image.h"
37
38MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
39MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
40MODULE_LICENSE("GPL");
41MODULE_VERSION(LIQUIDIO_VERSION);
Derek Chicklesea6404c82017-08-07 12:22:15 -070042MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
43 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
44MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
45 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
47 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
48MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
49 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070050
51static int ddr_timeout = 10000;
52module_param(ddr_timeout, int, 0644);
53MODULE_PARM_DESC(ddr_timeout,
54 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
55
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070056#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
57
58static 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
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070066static u32 console_bitmask;
67module_param(console_bitmask, int, 0644);
68MODULE_PARM_DESC(console_bitmask,
69 "Bitmask indicating which consoles have debug output redirected to syslog.");
70
71/**
72 * \brief determines if a given console has debug enabled.
73 * @param console console to check
74 * @returns 1 = enabled. 0 otherwise
75 */
Rick Farringtonda1542b2017-08-11 18:43:14 -070076static int octeon_console_debug_enabled(u32 console)
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070077{
78 return (console_bitmask >> (console)) & 0x1;
79}
80
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070081/* Polling interval for determining when NIC application is alive */
82#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
83
84/* runtime link query interval */
85#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
86
87struct liquidio_if_cfg_context {
88 int octeon_id;
89
90 wait_queue_head_t wc;
91
92 int cond;
93};
94
95struct liquidio_if_cfg_resp {
96 u64 rh;
97 struct liquidio_if_cfg_info cfg_info;
98 u64 status;
99};
100
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -0700101struct liquidio_rx_ctl_context {
102 int octeon_id;
103
104 wait_queue_head_t wc;
105
106 int cond;
107};
108
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700109struct oct_link_status_resp {
110 u64 rh;
111 struct oct_link_info link_info;
112 u64 status;
113};
114
115struct oct_timestamp_resp {
116 u64 rh;
117 u64 timestamp;
118 u64 status;
119};
120
121#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
122
123union tx_info {
124 u64 u64;
125 struct {
126#ifdef __BIG_ENDIAN_BITFIELD
127 u16 gso_size;
128 u16 gso_segs;
129 u32 reserved;
130#else
131 u32 reserved;
132 u16 gso_segs;
133 u16 gso_size;
134#endif
135 } s;
136};
137
138/** Octeon device properties to be used by the NIC module.
139 * Each octeon device in the system will be represented
140 * by this structure in the NIC module.
141 */
142
143#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
144
145#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700146#define OCTNIC_GSO_MAX_SIZE \
147 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700148
149/** Structure of a node in list of gather components maintained by
150 * NIC driver for each network device.
151 */
152struct octnic_gather {
153 /** List manipulation. Next and prev pointers. */
154 struct list_head list;
155
156 /** Size of the gather component at sg in bytes. */
157 int sg_size;
158
159 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
160 int adjust;
161
162 /** Gather component that can accommodate max sized fragment list
163 * received from the IP layer.
164 */
165 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700166
VSR Burru67e303e2017-03-06 18:45:59 -0800167 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700168};
169
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700170struct handshake {
171 struct completion init;
172 struct completion started;
173 struct pci_dev *pci_dev;
174 int init_ok;
175 int started_ok;
176};
177
178struct octeon_device_priv {
179 /** Tasklet structures for this device. */
180 struct tasklet_struct droq_tasklet;
181 unsigned long napi_mask;
182};
183
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800184#ifdef CONFIG_PCI_IOV
185static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
186#endif
187
Rick Farringtonda1542b2017-08-11 18:43:14 -0700188static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
189 char *prefix, char *suffix);
190
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700191static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700192static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700193static void liquidio_remove(struct pci_dev *pdev);
194static int liquidio_probe(struct pci_dev *pdev,
195 const struct pci_device_id *ent);
Felix Manlunasbb54be52017-04-04 19:26:57 -0700196static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
197 int linkstate);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700198
199static struct handshake handshake[MAX_OCTEON_DEVICES];
200static struct completion first_stage;
201
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700202static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700203{
204 int q_no;
205 int reschedule = 0;
206 struct octeon_device *oct = (struct octeon_device *)pdev;
207 struct octeon_device_priv *oct_priv =
208 (struct octeon_device_priv *)oct->priv;
209
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700210 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800211 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700212 continue;
213 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
214 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700215 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700216
217 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
218 /* set time and cnt interrupt thresholds for this DROQ
219 * for NAPI
220 */
221 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
222
223 octeon_write_csr64(
224 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
225 0x5700000040ULL);
226 octeon_write_csr64(
227 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
228 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700229 }
230
231 if (reschedule)
232 tasklet_schedule(&oct_priv->droq_tasklet);
233}
234
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700235static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700236{
237 struct octeon_device_priv *oct_priv =
238 (struct octeon_device_priv *)oct->priv;
239 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
240 int i;
241
242 do {
243 pending_pkts = 0;
244
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700245 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800246 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700247 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700248 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700249 }
250 if (pkt_cnt > 0) {
251 pending_pkts += pkt_cnt;
252 tasklet_schedule(&oct_priv->droq_tasklet);
253 }
254 pkt_cnt = 0;
255 schedule_timeout_uninterruptible(1);
256
257 } while (retry-- && pending_pkts);
258
259 return pkt_cnt;
260}
261
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700262/**
263 * \brief Forces all IO queues off on a given device
264 * @param oct Pointer to Octeon device
265 */
266static void force_io_queues_off(struct octeon_device *oct)
267{
268 if ((oct->chip_id == OCTEON_CN66XX) ||
269 (oct->chip_id == OCTEON_CN68XX)) {
270 /* Reset the Enable bits for Input Queues. */
271 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
272
273 /* Reset the Enable bits for Output Queues. */
274 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
275 }
276}
277
278/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700279 * \brief Cause device to go quiet so it can be safely removed/reset/etc
280 * @param oct Pointer to Octeon device
281 */
282static inline void pcierror_quiesce_device(struct octeon_device *oct)
283{
284 int i;
285
286 /* Disable the input and output queues now. No more packets will
287 * arrive from Octeon, but we should wait for all packet processing
288 * to finish.
289 */
290 force_io_queues_off(oct);
291
292 /* To allow for in-flight requests */
293 schedule_timeout_uninterruptible(100);
294
295 if (wait_for_pending_requests(oct))
296 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
297
298 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700299 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700300 struct octeon_instr_queue *iq;
301
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800302 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700303 continue;
304 iq = oct->instr_queue[i];
305
306 if (atomic_read(&iq->instr_pending)) {
307 spin_lock_bh(&iq->lock);
308 iq->fill_cnt = 0;
309 iq->octeon_read_index = iq->host_write_index;
310 iq->stats.instr_processed +=
311 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700312 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700313 spin_unlock_bh(&iq->lock);
314 }
315 }
316
317 /* Force all pending ordered list requests to time out. */
318 lio_process_ordered_list(oct, 1);
319
320 /* We do not need to wait for output queue packets to be processed. */
321}
322
323/**
324 * \brief Cleanup PCI AER uncorrectable error status
325 * @param dev Pointer to PCI device
326 */
327static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
328{
329 int pos = 0x100;
330 u32 status, mask;
331
332 pr_info("%s :\n", __func__);
333
334 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
335 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
336 if (dev->error_state == pci_channel_io_normal)
337 status &= ~mask; /* Clear corresponding nonfatal bits */
338 else
339 status &= mask; /* Clear corresponding fatal bits */
340 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
341}
342
343/**
344 * \brief Stop all PCI IO to a given device
345 * @param dev Pointer to Octeon device
346 */
347static void stop_pci_io(struct octeon_device *oct)
348{
349 /* No more instructions will be forwarded. */
350 atomic_set(&oct->status, OCT_DEV_IN_RESET);
351
352 pci_disable_device(oct->pci_dev);
353
354 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700355 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700356
357 pcierror_quiesce_device(oct);
358
359 /* Release the interrupt line */
360 free_irq(oct->pci_dev->irq, oct);
361
362 if (oct->flags & LIO_FLAG_MSI_ENABLED)
363 pci_disable_msi(oct->pci_dev);
364
365 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
366 lio_get_state_string(&oct->status));
367
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700368 /* making it a common function for all OCTEON models */
369 cleanup_aer_uncorrect_error_status(oct->pci_dev);
370}
371
372/**
373 * \brief called when PCI error is detected
374 * @param pdev Pointer to PCI device
375 * @param state The current pci connection state
376 *
377 * This function is called after a PCI bus error affecting
378 * this device has been detected.
379 */
380static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
381 pci_channel_state_t state)
382{
383 struct octeon_device *oct = pci_get_drvdata(pdev);
384
385 /* Non-correctable Non-fatal errors */
386 if (state == pci_channel_io_normal) {
387 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
388 cleanup_aer_uncorrect_error_status(oct->pci_dev);
389 return PCI_ERS_RESULT_CAN_RECOVER;
390 }
391
392 /* Non-correctable Fatal errors */
393 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
394 stop_pci_io(oct);
395
396 /* Always return a DISCONNECT. There is no support for recovery but only
397 * for a clean shutdown.
398 */
399 return PCI_ERS_RESULT_DISCONNECT;
400}
401
402/**
403 * \brief mmio handler
404 * @param pdev Pointer to PCI device
405 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700406static pci_ers_result_t liquidio_pcie_mmio_enabled(
407 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700408{
409 /* We should never hit this since we never ask for a reset for a Fatal
410 * Error. We always return DISCONNECT in io_error above.
411 * But play safe and return RECOVERED for now.
412 */
413 return PCI_ERS_RESULT_RECOVERED;
414}
415
416/**
417 * \brief called after the pci bus has been reset.
418 * @param pdev Pointer to PCI device
419 *
420 * Restart the card from scratch, as if from a cold-boot. Implementation
421 * resembles the first-half of the octeon_resume routine.
422 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700423static pci_ers_result_t liquidio_pcie_slot_reset(
424 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700425{
426 /* We should never hit this since we never ask for a reset for a Fatal
427 * Error. We always return DISCONNECT in io_error above.
428 * But play safe and return RECOVERED for now.
429 */
430 return PCI_ERS_RESULT_RECOVERED;
431}
432
433/**
434 * \brief called when traffic can start flowing again.
435 * @param pdev Pointer to PCI device
436 *
437 * This callback is called when the error recovery driver tells us that
438 * its OK to resume normal operation. Implementation resembles the
439 * second-half of the octeon_resume routine.
440 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700441static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700442{
443 /* Nothing to be done here. */
444}
445
446#ifdef CONFIG_PM
447/**
448 * \brief called when suspending
449 * @param pdev Pointer to PCI device
450 * @param state state to suspend to
451 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700452static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
453 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700454{
455 return 0;
456}
457
458/**
459 * \brief called when resuming
460 * @param pdev Pointer to PCI device
461 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700462static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700463{
464 return 0;
465}
466#endif
467
468/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100469static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700470 .error_detected = liquidio_pcie_error_detected,
471 .mmio_enabled = liquidio_pcie_mmio_enabled,
472 .slot_reset = liquidio_pcie_slot_reset,
473 .resume = liquidio_pcie_resume,
474};
475
476static const struct pci_device_id liquidio_pci_tbl[] = {
477 { /* 68xx */
478 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
479 },
480 { /* 66xx */
481 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
482 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700483 { /* 23xx pf */
484 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
485 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700486 {
487 0, 0, 0, 0, 0, 0, 0
488 }
489};
490MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
491
492static struct pci_driver liquidio_pci_driver = {
493 .name = "LiquidIO",
494 .id_table = liquidio_pci_tbl,
495 .probe = liquidio_probe,
496 .remove = liquidio_remove,
497 .err_handler = &liquidio_err_handler, /* For AER */
498
499#ifdef CONFIG_PM
500 .suspend = liquidio_suspend,
501 .resume = liquidio_resume,
502#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800503#ifdef CONFIG_PCI_IOV
504 .sriov_configure = liquidio_enable_sriov,
505#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700506};
507
508/**
509 * \brief register PCI driver
510 */
511static int liquidio_init_pci(void)
512{
513 return pci_register_driver(&liquidio_pci_driver);
514}
515
516/**
517 * \brief unregister PCI driver
518 */
519static void liquidio_deinit_pci(void)
520{
521 pci_unregister_driver(&liquidio_pci_driver);
522}
523
524/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700525 * \brief Stop Tx queues
526 * @param netdev network device
527 */
528static inline void txqs_stop(struct net_device *netdev)
529{
530 if (netif_is_multiqueue(netdev)) {
531 int i;
532
533 for (i = 0; i < netdev->num_tx_queues; i++)
534 netif_stop_subqueue(netdev, i);
535 } else {
536 netif_stop_queue(netdev);
537 }
538}
539
540/**
541 * \brief Start Tx queues
542 * @param netdev network device
543 */
544static inline void txqs_start(struct net_device *netdev)
545{
546 if (netif_is_multiqueue(netdev)) {
547 int i;
548
549 for (i = 0; i < netdev->num_tx_queues; i++)
550 netif_start_subqueue(netdev, i);
551 } else {
552 netif_start_queue(netdev);
553 }
554}
555
556/**
557 * \brief Wake Tx queues
558 * @param netdev network device
559 */
560static inline void txqs_wake(struct net_device *netdev)
561{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700562 struct lio *lio = GET_LIO(netdev);
563
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700564 if (netif_is_multiqueue(netdev)) {
565 int i;
566
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700567 for (i = 0; i < netdev->num_tx_queues; i++) {
568 int qno = lio->linfo.txpciq[i %
569 (lio->linfo.num_txpciq)].s.q_no;
570
571 if (__netif_subqueue_stopped(netdev, i)) {
572 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
573 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700574 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700575 }
576 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700577 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700578 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
579 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700580 netif_wake_queue(netdev);
581 }
582}
583
584/**
585 * \brief Stop Tx queue
586 * @param netdev network device
587 */
588static void stop_txq(struct net_device *netdev)
589{
590 txqs_stop(netdev);
591}
592
593/**
594 * \brief Start Tx queue
595 * @param netdev network device
596 */
597static void start_txq(struct net_device *netdev)
598{
599 struct lio *lio = GET_LIO(netdev);
600
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700601 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700602 txqs_start(netdev);
603 return;
604 }
605}
606
607/**
608 * \brief Wake a queue
609 * @param netdev network device
610 * @param q which queue to wake
611 */
612static inline void wake_q(struct net_device *netdev, int q)
613{
614 if (netif_is_multiqueue(netdev))
615 netif_wake_subqueue(netdev, q);
616 else
617 netif_wake_queue(netdev);
618}
619
620/**
621 * \brief Stop a queue
622 * @param netdev network device
623 * @param q which queue to stop
624 */
625static inline void stop_q(struct net_device *netdev, int q)
626{
627 if (netif_is_multiqueue(netdev))
628 netif_stop_subqueue(netdev, q);
629 else
630 netif_stop_queue(netdev);
631}
632
633/**
634 * \brief Check Tx queue status, and take appropriate action
635 * @param lio per-network private data
636 * @returns 0 if full, number of queues woken up otherwise
637 */
638static inline int check_txq_status(struct lio *lio)
639{
640 int ret_val = 0;
641
642 if (netif_is_multiqueue(lio->netdev)) {
643 int numqs = lio->netdev->num_tx_queues;
644 int q, iq = 0;
645
646 /* check each sub-queue state */
647 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700648 iq = lio->linfo.txpciq[q %
649 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700650 if (octnet_iq_is_full(lio->oct_dev, iq))
651 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700652 if (__netif_subqueue_stopped(lio->netdev, q)) {
653 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700654 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
655 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700656 ret_val++;
657 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700658 }
659 } else {
660 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
661 return 0;
662 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700663 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
664 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700665 ret_val = 1;
666 }
667 return ret_val;
668}
669
670/**
671 * Remove the node at the head of the list. The list would be empty at
672 * the end of this call if there are no more nodes in the list.
673 */
674static inline struct list_head *list_delete_head(struct list_head *root)
675{
676 struct list_head *node;
677
678 if ((root->prev == root) && (root->next == root))
679 node = NULL;
680 else
681 node = root->next;
682
683 if (node)
684 list_del(node);
685
686 return node;
687}
688
689/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700690 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700691 * @param lio per-network private data
692 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700693static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700694{
695 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700696 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700697
VSR Burru67e303e2017-03-06 18:45:59 -0800698 kfree(lio->glist_lock);
699 lio->glist_lock = NULL;
700
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700701 if (!lio->glist)
702 return;
703
704 for (i = 0; i < lio->linfo.num_txpciq; i++) {
705 do {
706 g = (struct octnic_gather *)
707 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800708 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700709 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700710 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800711
Felix Manlunas58ad3192017-03-20 19:04:48 -0700712 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
713 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800714 lio_dma_free(lio->oct_dev,
715 lio->glist_entry_size * lio->tx_qsize,
716 lio->glists_virt_base[i],
717 lio->glists_dma_base[i]);
718 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700719 }
720
VSR Burru67e303e2017-03-06 18:45:59 -0800721 kfree(lio->glists_virt_base);
722 lio->glists_virt_base = NULL;
723
724 kfree(lio->glists_dma_base);
725 lio->glists_dma_base = NULL;
726
727 kfree(lio->glist);
728 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700729}
730
731/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700732 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700733 * @param lio per-network private data
734 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700735static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700736{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700737 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700738 struct octnic_gather *g;
739
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700740 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
741 GFP_KERNEL);
742 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800743 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700744
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700745 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
746 GFP_KERNEL);
747 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800748 kfree(lio->glist_lock);
749 lio->glist_lock = NULL;
750 return -ENOMEM;
751 }
752
753 lio->glist_entry_size =
754 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
755
756 /* allocate memory to store virtual and dma base address of
757 * per glist consistent memory
758 */
759 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
760 GFP_KERNEL);
761 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
762 GFP_KERNEL);
763
764 if (!lio->glists_virt_base || !lio->glists_dma_base) {
765 delete_glists(lio);
766 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700767 }
768
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700769 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800770 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700771
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700772 spin_lock_init(&lio->glist_lock[i]);
773
774 INIT_LIST_HEAD(&lio->glist[i]);
775
VSR Burru67e303e2017-03-06 18:45:59 -0800776 lio->glists_virt_base[i] =
777 lio_dma_alloc(oct,
778 lio->glist_entry_size * lio->tx_qsize,
779 &lio->glists_dma_base[i]);
780
781 if (!lio->glists_virt_base[i]) {
782 delete_glists(lio);
783 return -ENOMEM;
784 }
785
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700786 for (j = 0; j < lio->tx_qsize; j++) {
787 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
788 numa_node);
789 if (!g)
790 g = kzalloc(sizeof(*g), GFP_KERNEL);
791 if (!g)
792 break;
793
VSR Burru67e303e2017-03-06 18:45:59 -0800794 g->sg = lio->glists_virt_base[i] +
795 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700796
VSR Burru67e303e2017-03-06 18:45:59 -0800797 g->sg_dma_ptr = lio->glists_dma_base[i] +
798 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700799
800 list_add_tail(&g->list, &lio->glist[i]);
801 }
802
803 if (j != lio->tx_qsize) {
804 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800805 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700806 }
807 }
808
809 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700810}
811
812/**
813 * \brief Print link information
814 * @param netdev network device
815 */
816static void print_link_info(struct net_device *netdev)
817{
818 struct lio *lio = GET_LIO(netdev);
819
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -0700820 if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
821 ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700822 struct oct_link_info *linfo = &lio->linfo;
823
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700824 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700825 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
826 linfo->link.s.speed,
827 (linfo->link.s.duplex) ? "Full" : "Half");
828 } else {
829 netif_info(lio, link, lio->netdev, "Link Down\n");
830 }
831 }
832}
833
834/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700835 * \brief Routine to notify MTU change
836 * @param work work_struct data structure
837 */
838static void octnet_link_status_change(struct work_struct *work)
839{
840 struct cavium_wk *wk = (struct cavium_wk *)work;
841 struct lio *lio = (struct lio *)wk->ctxptr;
842
843 rtnl_lock();
844 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
845 rtnl_unlock();
846}
847
848/**
849 * \brief Sets up the mtu status change work
850 * @param netdev network device
851 */
852static inline int setup_link_status_change_wq(struct net_device *netdev)
853{
854 struct lio *lio = GET_LIO(netdev);
855 struct octeon_device *oct = lio->oct_dev;
856
857 lio->link_status_wq.wq = alloc_workqueue("link-status",
858 WQ_MEM_RECLAIM, 0);
859 if (!lio->link_status_wq.wq) {
860 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
861 return -1;
862 }
863 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
864 octnet_link_status_change);
865 lio->link_status_wq.wk.ctxptr = lio;
866
867 return 0;
868}
869
870static inline void cleanup_link_status_change_wq(struct net_device *netdev)
871{
872 struct lio *lio = GET_LIO(netdev);
873
874 if (lio->link_status_wq.wq) {
875 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
876 destroy_workqueue(lio->link_status_wq.wq);
877 }
878}
879
880/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700881 * \brief Update link status
882 * @param netdev network device
883 * @param ls link status structure
884 *
885 * Called on receipt of a link status response from the core application to
886 * update each interface's link status.
887 */
888static inline void update_link_status(struct net_device *netdev,
889 union oct_link_status *ls)
890{
891 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700892 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700893
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700894 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700895
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700896 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700897 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700898 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700899
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700900 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700901 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700902 txqs_wake(netdev);
903 } else {
904 netif_carrier_off(netdev);
905 stop_txq(netdev);
906 }
907 }
908}
909
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700910static
911int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
912{
913 struct octeon_device *oct = droq->oct_dev;
914 struct octeon_device_priv *oct_priv =
915 (struct octeon_device_priv *)oct->priv;
916
917 if (droq->ops.poll_mode) {
918 droq->ops.napi_fn(droq);
919 } else {
920 if (ret & MSIX_PO_INT) {
921 tasklet_schedule(&oct_priv->droq_tasklet);
922 return 1;
923 }
924 /* this will be flushed periodically by check iq db */
925 if (ret & MSIX_PI_INT)
926 return 0;
927 }
928 return 0;
929}
930
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700931/**
932 * \brief Droq packet processor sceduler
933 * @param oct octeon device
934 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700935static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700936{
937 struct octeon_device_priv *oct_priv =
938 (struct octeon_device_priv *)oct->priv;
939 u64 oq_no;
940 struct octeon_droq *droq;
941
942 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700943 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
944 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800945 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700946 continue;
947
948 droq = oct->droq[oq_no];
949
950 if (droq->ops.poll_mode) {
951 droq->ops.napi_fn(droq);
952 oct_priv->napi_mask |= (1 << oq_no);
953 } else {
954 tasklet_schedule(&oct_priv->droq_tasklet);
955 }
956 }
957 }
958}
959
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700960static irqreturn_t
961liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
962{
963 u64 ret;
964 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
965 struct octeon_device *oct = ioq_vector->oct_dev;
966 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
967
968 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
969
970 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
971 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
972
973 return IRQ_HANDLED;
974}
975
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700976/**
977 * \brief Interrupt handler for octeon
978 * @param irq unused
979 * @param dev octeon device
980 */
981static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700982irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
983 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700984{
985 struct octeon_device *oct = (struct octeon_device *)dev;
986 irqreturn_t ret;
987
988 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700989 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700990
991 ret = oct->fn_list.process_interrupt_regs(oct);
992
993 if (ret == IRQ_HANDLED)
994 liquidio_schedule_droq_pkt_handlers(oct);
995
996 /* Re-enable our interrupts */
997 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700998 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700999
1000 return ret;
1001}
1002
1003/**
1004 * \brief Setup interrupt for octeon device
1005 * @param oct octeon device
1006 *
1007 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1008 */
1009static int octeon_setup_interrupt(struct octeon_device *oct)
1010{
1011 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001012 struct msix_entry *msix_entries;
1013 int i;
1014 int num_ioq_vectors;
1015 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001016 char *queue_irq_names = NULL;
1017 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001018
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001019 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1020 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1021 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1022 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001023
Rick Farrington0c88a762017-03-13 12:58:04 -07001024 /* allocate storage for the names assigned to each irq */
1025 oct->irq_name_storage =
1026 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1027 GFP_KERNEL);
1028 if (!oct->irq_name_storage) {
1029 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1030 return -ENOMEM;
1031 }
1032
1033 queue_irq_names = oct->irq_name_storage;
1034 aux_irq_name = &queue_irq_names
1035 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1036
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001037 oct->msix_entries = kcalloc(
1038 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001039 if (!oct->msix_entries) {
1040 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1041 kfree(oct->irq_name_storage);
1042 oct->irq_name_storage = NULL;
1043 return -ENOMEM;
1044 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001045
1046 msix_entries = (struct msix_entry *)oct->msix_entries;
1047 /*Assumption is that pf msix vectors start from pf srn to pf to
1048 * trs and not from 0. if not change this code
1049 */
1050 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1051 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1052 msix_entries[oct->num_msix_irqs - 1].entry =
1053 oct->sriov_info.trs;
1054 num_alloc_ioq_vectors = pci_enable_msix_range(
1055 oct->pci_dev, msix_entries,
1056 oct->num_msix_irqs,
1057 oct->num_msix_irqs);
1058 if (num_alloc_ioq_vectors < 0) {
1059 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1060 kfree(oct->msix_entries);
1061 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001062 kfree(oct->irq_name_storage);
1063 oct->irq_name_storage = NULL;
1064 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001065 }
1066 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1067
1068 num_ioq_vectors = oct->num_msix_irqs;
1069
1070 /** For PF, there is one non-ioq interrupt handler */
1071 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001072
1073 snprintf(aux_irq_name, INTRNAMSIZ,
1074 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001075 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001076 liquidio_legacy_intr_handler, 0,
1077 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001078 if (irqret) {
1079 dev_err(&oct->pci_dev->dev,
1080 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1081 irqret);
1082 pci_disable_msix(oct->pci_dev);
1083 kfree(oct->msix_entries);
1084 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001085 kfree(oct->irq_name_storage);
1086 oct->irq_name_storage = NULL;
1087 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001088 }
1089
1090 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001091 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1092 "LiquidIO%u-pf%u-rxtx-%u",
1093 oct->octeon_id, oct->pf_num, i);
1094
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001095 irqret = request_irq(msix_entries[i].vector,
1096 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001097 &queue_irq_names[IRQ_NAME_OFF(i)],
1098 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001099 if (irqret) {
1100 dev_err(&oct->pci_dev->dev,
1101 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1102 irqret);
1103 /** Freeing the non-ioq irq vector here . */
1104 free_irq(msix_entries[num_ioq_vectors].vector,
1105 oct);
1106
1107 while (i) {
1108 i--;
1109 /** clearing affinity mask. */
1110 irq_set_affinity_hint(
1111 msix_entries[i].vector, NULL);
1112 free_irq(msix_entries[i].vector,
1113 &oct->ioq_vector[i]);
1114 }
1115 pci_disable_msix(oct->pci_dev);
1116 kfree(oct->msix_entries);
1117 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001118 kfree(oct->irq_name_storage);
1119 oct->irq_name_storage = NULL;
1120 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001121 }
1122 oct->ioq_vector[i].vector = msix_entries[i].vector;
1123 /* assign the cpu mask for this msix interrupt vector */
1124 irq_set_affinity_hint(
1125 msix_entries[i].vector,
1126 (&oct->ioq_vector[i].affinity_mask));
1127 }
1128 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1129 oct->octeon_id);
1130 } else {
1131 err = pci_enable_msi(oct->pci_dev);
1132 if (err)
1133 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1134 err);
1135 else
1136 oct->flags |= LIO_FLAG_MSI_ENABLED;
1137
Rick Farrington0c88a762017-03-13 12:58:04 -07001138 /* allocate storage for the names assigned to the irq */
1139 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1140 if (!oct->irq_name_storage)
1141 return -ENOMEM;
1142
1143 queue_irq_names = oct->irq_name_storage;
1144
1145 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1146 "LiquidIO%u-pf%u-rxtx-%u",
1147 oct->octeon_id, oct->pf_num, 0);
1148
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001149 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001150 liquidio_legacy_intr_handler,
1151 IRQF_SHARED,
1152 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001153 if (irqret) {
1154 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1155 pci_disable_msi(oct->pci_dev);
1156 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1157 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001158 kfree(oct->irq_name_storage);
1159 oct->irq_name_storage = NULL;
1160 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001161 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001162 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001163 return 0;
1164}
1165
Felix Manlunasbb54be52017-04-04 19:26:57 -07001166static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
1167{
1168 struct octeon_device *other_oct;
1169
1170 other_oct = lio_get_device(oct->octeon_id + 1);
1171
1172 if (other_oct && other_oct->pci_dev) {
1173 int oct_busnum, other_oct_busnum;
1174
1175 oct_busnum = oct->pci_dev->bus->number;
1176 other_oct_busnum = other_oct->pci_dev->bus->number;
1177
1178 if (oct_busnum == other_oct_busnum) {
1179 int oct_slot, other_oct_slot;
1180
1181 oct_slot = PCI_SLOT(oct->pci_dev->devfn);
1182 other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
1183
1184 if (oct_slot == other_oct_slot)
1185 return other_oct;
1186 }
1187 }
1188
1189 return NULL;
1190}
1191
1192static void disable_all_vf_links(struct octeon_device *oct)
1193{
1194 struct net_device *netdev;
1195 int max_vfs, vf, i;
1196
1197 if (!oct)
1198 return;
1199
1200 max_vfs = oct->sriov_info.max_vfs;
1201
1202 for (i = 0; i < oct->ifcount; i++) {
1203 netdev = oct->props[i].netdev;
1204 if (!netdev)
1205 continue;
1206
1207 for (vf = 0; vf < max_vfs; vf++)
1208 liquidio_set_vf_link_state(netdev, vf,
1209 IFLA_VF_LINK_STATE_DISABLE);
1210 }
1211}
1212
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001213static int liquidio_watchdog(void *param)
1214{
Felix Manlunasbb54be52017-04-04 19:26:57 -07001215 bool err_msg_was_printed[LIO_MAX_CORES];
1216 u16 mask_of_crashed_or_stuck_cores = 0;
1217 bool all_vf_links_are_disabled = false;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001218 struct octeon_device *oct = param;
Felix Manlunasbb54be52017-04-04 19:26:57 -07001219 struct octeon_device *other_oct;
1220#ifdef CONFIG_MODULE_UNLOAD
1221 long refcount, vfs_referencing_pf;
1222 u64 vfs_mask1, vfs_mask2;
1223#endif
1224 int core;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001225
Felix Manlunasbb54be52017-04-04 19:26:57 -07001226 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001227
1228 while (!kthread_should_stop()) {
Felix Manlunasbb54be52017-04-04 19:26:57 -07001229 /* sleep for a couple of seconds so that we don't hog the CPU */
1230 set_current_state(TASK_INTERRUPTIBLE);
1231 schedule_timeout(msecs_to_jiffies(2000));
1232
1233 mask_of_crashed_or_stuck_cores =
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001234 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1235
Felix Manlunasbb54be52017-04-04 19:26:57 -07001236 if (!mask_of_crashed_or_stuck_cores)
1237 continue;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001238
Felix Manlunasbb54be52017-04-04 19:26:57 -07001239 WRITE_ONCE(oct->cores_crashed, true);
1240 other_oct = get_other_octeon_device(oct);
1241 if (other_oct)
1242 WRITE_ONCE(other_oct->cores_crashed, true);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001243
Felix Manlunasbb54be52017-04-04 19:26:57 -07001244 for (core = 0; core < LIO_MAX_CORES; core++) {
1245 bool core_crashed_or_got_stuck;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001246
Felix Manlunasbb54be52017-04-04 19:26:57 -07001247 core_crashed_or_got_stuck =
1248 (mask_of_crashed_or_stuck_cores
1249 >> core) & 1;
1250
1251 if (core_crashed_or_got_stuck &&
1252 !err_msg_was_printed[core]) {
1253 dev_err(&oct->pci_dev->dev,
1254 "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
1255 core);
1256 err_msg_was_printed[core] = true;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001257 }
1258 }
1259
Felix Manlunasbb54be52017-04-04 19:26:57 -07001260 if (all_vf_links_are_disabled)
1261 continue;
1262
1263 disable_all_vf_links(oct);
1264 disable_all_vf_links(other_oct);
1265 all_vf_links_are_disabled = true;
1266
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001267#ifdef CONFIG_MODULE_UNLOAD
Felix Manlunasbb54be52017-04-04 19:26:57 -07001268 vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1269 vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001270
Felix Manlunasbb54be52017-04-04 19:26:57 -07001271 vfs_referencing_pf = hweight64(vfs_mask1);
1272 vfs_referencing_pf += hweight64(vfs_mask2);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001273
Felix Manlunasbb54be52017-04-04 19:26:57 -07001274 refcount = module_refcount(THIS_MODULE);
1275 if (refcount >= vfs_referencing_pf) {
1276 while (vfs_referencing_pf) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001277 module_put(THIS_MODULE);
Felix Manlunasbb54be52017-04-04 19:26:57 -07001278 vfs_referencing_pf--;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001279 }
1280 }
1281#endif
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001282 }
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
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07001309 /* Enable PTP for 6XXX Device */
1310 if (((pdev->device == OCTEON_CN66XX) ||
1311 (pdev->device == OCTEON_CN68XX)))
1312 oct_dev->ptp_enable = true;
1313 else
1314 oct_dev->ptp_enable = false;
1315
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001316 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1317 (u32)pdev->vendor, (u32)pdev->device);
1318
1319 /* Assign octeon_device for this device to the private data area. */
1320 pci_set_drvdata(pdev, oct_dev);
1321
1322 /* set linux specific device pointer */
1323 oct_dev->pci_dev = (void *)pdev;
1324
1325 hs = &handshake[oct_dev->octeon_id];
1326 init_completion(&hs->init);
1327 init_completion(&hs->started);
1328 hs->pci_dev = pdev;
1329
1330 if (oct_dev->octeon_id == 0)
1331 /* first LiquidIO NIC is detected */
1332 complete(&first_stage);
1333
1334 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001335 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001336 liquidio_remove(pdev);
1337 return -ENOMEM;
1338 }
1339
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001340 if (OCTEON_CN23XX_PF(oct_dev)) {
1341 u64 scratch1;
1342 u8 bus, device, function;
1343
1344 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1345 if (!(scratch1 & 4ULL)) {
1346 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1347 * the lio watchdog kernel thread is running for this
1348 * NIC. Each NIC gets one watchdog kernel thread.
1349 */
1350 scratch1 |= 4ULL;
1351 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1352 scratch1);
1353
1354 bus = pdev->bus->number;
1355 device = PCI_SLOT(pdev->devfn);
1356 function = PCI_FUNC(pdev->devfn);
1357 oct_dev->watchdog_task = kthread_create(
1358 liquidio_watchdog, oct_dev,
1359 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001360 if (!IS_ERR(oct_dev->watchdog_task)) {
1361 wake_up_process(oct_dev->watchdog_task);
1362 } else {
1363 oct_dev->watchdog_task = NULL;
1364 dev_err(&oct_dev->pci_dev->dev,
1365 "failed to create kernel_thread\n");
1366 liquidio_remove(pdev);
1367 return -1;
1368 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001369 }
1370 }
1371
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001372 oct_dev->rx_pause = 1;
1373 oct_dev->tx_pause = 1;
1374
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001375 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1376
1377 return 0;
1378}
1379
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001380static bool fw_type_is_none(void)
1381{
1382 return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1383 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
1384}
1385
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001386/**
1387 *\brief Destroy resources associated with octeon device
1388 * @param pdev PCI device structure
1389 * @param ent unused
1390 */
1391static void octeon_destroy_resources(struct octeon_device *oct)
1392{
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001393 int i, refcount;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001394 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001395 struct octeon_device_priv *oct_priv =
1396 (struct octeon_device_priv *)oct->priv;
1397
1398 struct handshake *hs;
1399
1400 switch (atomic_read(&oct->status)) {
1401 case OCT_DEV_RUNNING:
1402 case OCT_DEV_CORE_OK:
1403
1404 /* No more instructions will be forwarded. */
1405 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1406
1407 oct->app_mode = CVM_DRV_INVALID_APP;
1408 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1409 lio_get_state_string(&oct->status));
1410
1411 schedule_timeout_uninterruptible(HZ / 10);
1412
1413 /* fallthrough */
1414 case OCT_DEV_HOST_OK:
1415
1416 /* fallthrough */
1417 case OCT_DEV_CONSOLE_INIT_DONE:
1418 /* Remove any consoles */
1419 octeon_remove_consoles(oct);
1420
1421 /* fallthrough */
1422 case OCT_DEV_IO_QUEUES_DONE:
1423 if (wait_for_pending_requests(oct))
1424 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1425
1426 if (lio_wait_for_instr_fetch(oct))
1427 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1428
1429 /* Disable the input and output queues now. No more packets will
1430 * arrive from Octeon, but we should wait for all packet
1431 * processing to finish.
1432 */
1433 oct->fn_list.disable_io_queues(oct);
1434
1435 if (lio_wait_for_oq_pkts(oct))
1436 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1437
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001438 /* fallthrough */
1439 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001440 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001441 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001442
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001443 if (oct->msix_on) {
1444 msix_entries = (struct msix_entry *)oct->msix_entries;
1445 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1446 /* clear the affinity_cpumask */
1447 irq_set_affinity_hint(msix_entries[i].vector,
1448 NULL);
1449 free_irq(msix_entries[i].vector,
1450 &oct->ioq_vector[i]);
1451 }
1452 /* non-iov vector's argument is oct struct */
1453 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001454
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001455 pci_disable_msix(oct->pci_dev);
1456 kfree(oct->msix_entries);
1457 oct->msix_entries = NULL;
1458 } else {
1459 /* Release the interrupt line */
1460 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001461
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001462 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1463 pci_disable_msi(oct->pci_dev);
1464 }
1465
Rick Farrington0c88a762017-03-13 12:58:04 -07001466 kfree(oct->irq_name_storage);
1467 oct->irq_name_storage = NULL;
1468
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001469 /* fallthrough */
1470 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001471 if (OCTEON_CN23XX_PF(oct))
1472 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001473
1474 /* fallthrough */
1475 case OCT_DEV_MBOX_SETUP_DONE:
1476 if (OCTEON_CN23XX_PF(oct))
1477 oct->fn_list.free_mbox(oct);
1478
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001479 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001480 case OCT_DEV_IN_RESET:
1481 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001482 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001483 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001484 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001485 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001486 continue;
1487 octeon_delete_droq(oct, i);
1488 }
1489
1490 /* Force any pending handshakes to complete */
1491 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1492 hs = &handshake[i];
1493
1494 if (hs->pci_dev) {
1495 handshake[oct->octeon_id].init_ok = 0;
1496 complete(&handshake[oct->octeon_id].init);
1497 handshake[oct->octeon_id].started_ok = 0;
1498 complete(&handshake[oct->octeon_id].started);
1499 }
1500 }
1501
1502 /* fallthrough */
1503 case OCT_DEV_RESP_LIST_INIT_DONE:
1504 octeon_delete_response_list(oct);
1505
1506 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001507 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001508 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001509 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001510 continue;
1511 octeon_delete_instr_queue(oct, i);
1512 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001513#ifdef CONFIG_PCI_IOV
1514 if (oct->sriov_info.sriov_enabled)
1515 pci_disable_sriov(oct->pci_dev);
1516#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001517 /* fallthrough */
1518 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1519 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001520
1521 /* fallthrough */
1522 case OCT_DEV_DISPATCH_INIT_DONE:
1523 octeon_delete_dispatch_list(oct);
1524 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1525
1526 /* fallthrough */
1527 case OCT_DEV_PCI_MAP_DONE:
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001528 refcount = octeon_deregister_device(oct);
1529
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001530 if (!fw_type_is_none()) {
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001531 /* Soft reset the octeon device before exiting.
1532 * Implementation note: here, we reset the device
1533 * if it is a CN6XXX OR the last CN23XX device.
1534 */
1535 if (OCTEON_CN6XXX(oct) || !refcount)
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001536 oct->fn_list.soft_reset(oct);
1537 }
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001538
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001539 octeon_unmap_pci_barx(oct, 0);
1540 octeon_unmap_pci_barx(oct, 1);
1541
1542 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001543 case OCT_DEV_PCI_ENABLE_DONE:
1544 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001545 /* Disable the device, releasing the PCI INT */
1546 pci_disable_device(oct->pci_dev);
1547
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001548 /* fallthrough */
1549 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001550 /* Nothing to be done here either */
1551 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001552 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001553
1554 tasklet_kill(&oct_priv->droq_tasklet);
1555}
1556
1557/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001558 * \brief Callback for rx ctrl
1559 * @param status status of request
1560 * @param buf pointer to resp structure
1561 */
1562static void rx_ctl_callback(struct octeon_device *oct,
1563 u32 status,
1564 void *buf)
1565{
1566 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1567 struct liquidio_rx_ctl_context *ctx;
1568
1569 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1570
1571 oct = lio_get_device(ctx->octeon_id);
1572 if (status)
1573 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1574 CVM_CAST64(status));
1575 WRITE_ONCE(ctx->cond, 1);
1576
1577 /* This barrier is required to be sure that the response has been
1578 * written fully before waking up the handler
1579 */
1580 wmb();
1581
1582 wake_up_interruptible(&ctx->wc);
1583}
1584
1585/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001586 * \brief Send Rx control command
1587 * @param lio per-network private data
1588 * @param start_stop whether to start or stop
1589 */
1590static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1591{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001592 struct octeon_soft_command *sc;
1593 struct liquidio_rx_ctl_context *ctx;
1594 union octnet_cmd *ncmd;
1595 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1596 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1597 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001598
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001599 if (oct->props[lio->ifidx].rx_on == start_stop)
1600 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001601
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001602 sc = (struct octeon_soft_command *)
1603 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1604 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001605
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001606 ncmd = (union octnet_cmd *)sc->virtdptr;
1607 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1608
1609 WRITE_ONCE(ctx->cond, 0);
1610 ctx->octeon_id = lio_get_device_id(oct);
1611 init_waitqueue_head(&ctx->wc);
1612
1613 ncmd->u64 = 0;
1614 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1615 ncmd->s.param1 = start_stop;
1616
1617 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1618
1619 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1620
1621 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1622 OPCODE_NIC_CMD, 0, 0, 0);
1623
1624 sc->callback = rx_ctl_callback;
1625 sc->callback_arg = sc;
1626 sc->wait_time = 5000;
1627
1628 retval = octeon_send_soft_command(oct, sc);
1629 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001630 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001631 } else {
1632 /* Sleep on a wait queue till the cond flag indicates that the
1633 * response arrived or timed-out.
1634 */
1635 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1636 return;
1637 oct->props[lio->ifidx].rx_on = start_stop;
1638 }
1639
1640 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001641}
1642
1643/**
1644 * \brief Destroy NIC device interface
1645 * @param oct octeon device
1646 * @param ifidx which interface to destroy
1647 *
1648 * Cleanup associated with each interface for an Octeon device when NIC
1649 * module is being unloaded or if initialization fails during load.
1650 */
1651static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1652{
1653 struct net_device *netdev = oct->props[ifidx].netdev;
1654 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001655 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001656
1657 if (!netdev) {
1658 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1659 __func__, ifidx);
1660 return;
1661 }
1662
1663 lio = GET_LIO(netdev);
1664
1665 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1666
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001667 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001668 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001669
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001670 if (fw_type_is_none()) {
1671 struct octnic_ctrl_pkt nctrl;
1672
1673 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1674 nctrl.ncmd.s.cmd = OCTNET_CMD_RESET_PF;
1675 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1676 octnet_send_nic_ctrl_pkt(oct, &nctrl);
1677 }
1678
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001679 if (oct->props[lio->ifidx].napi_enabled == 1) {
1680 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1681 napi_disable(napi);
1682
1683 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001684
1685 if (OCTEON_CN23XX_PF(oct))
1686 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001687 }
1688
Intiyaz Basha42013e92017-08-08 19:34:28 -07001689 /* Delete NAPI */
1690 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1691 netif_napi_del(napi);
1692
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001693 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1694 unregister_netdev(netdev);
1695
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001696 cleanup_link_status_change_wq(netdev);
1697
Satanand Burla031d4f12017-03-22 11:31:13 -07001698 cleanup_rx_oom_poll_fn(netdev);
1699
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001700 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001701
1702 free_netdev(netdev);
1703
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001704 oct->props[ifidx].gmxport = -1;
1705
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001706 oct->props[ifidx].netdev = NULL;
1707}
1708
1709/**
1710 * \brief Stop complete NIC functionality
1711 * @param oct octeon device
1712 */
1713static int liquidio_stop_nic_module(struct octeon_device *oct)
1714{
1715 int i, j;
1716 struct lio *lio;
1717
1718 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1719 if (!oct->ifcount) {
1720 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1721 return 1;
1722 }
1723
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001724 spin_lock_bh(&oct->cmd_resp_wqlock);
1725 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1726 spin_unlock_bh(&oct->cmd_resp_wqlock);
1727
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001728 for (i = 0; i < oct->ifcount; i++) {
1729 lio = GET_LIO(oct->props[i].netdev);
1730 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001731 octeon_unregister_droq_ops(oct,
1732 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001733 }
1734
1735 for (i = 0; i < oct->ifcount; i++)
1736 liquidio_destroy_nic_device(oct, i);
1737
1738 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1739 return 0;
1740}
1741
1742/**
1743 * \brief Cleans up resources at unload time
1744 * @param pdev PCI device structure
1745 */
1746static void liquidio_remove(struct pci_dev *pdev)
1747{
1748 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1749
1750 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1751
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001752 if (oct_dev->watchdog_task)
1753 kthread_stop(oct_dev->watchdog_task);
1754
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001755 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1756 liquidio_stop_nic_module(oct_dev);
1757
1758 /* Reset the octeon device and cleanup all memory allocated for
1759 * the octeon device by driver.
1760 */
1761 octeon_destroy_resources(oct_dev);
1762
1763 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1764
1765 /* This octeon device has been removed. Update the global
1766 * data structure to reflect this. Free the device structure.
1767 */
1768 octeon_free_device_mem(oct_dev);
1769}
1770
1771/**
1772 * \brief Identify the Octeon device and to map the BAR address space
1773 * @param oct octeon device
1774 */
1775static int octeon_chip_specific_setup(struct octeon_device *oct)
1776{
1777 u32 dev_id, rev_id;
1778 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001779 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001780
1781 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1782 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1783 oct->rev_id = rev_id & 0xff;
1784
1785 switch (dev_id) {
1786 case OCTEON_CN68XX_PCIID:
1787 oct->chip_id = OCTEON_CN68XX;
1788 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001789 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001790 break;
1791
1792 case OCTEON_CN66XX_PCIID:
1793 oct->chip_id = OCTEON_CN66XX;
1794 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001795 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001796 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001797
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001798 case OCTEON_CN23XX_PCIID_PF:
1799 oct->chip_id = OCTEON_CN23XX_PF_VID;
1800 ret = setup_cn23xx_octeon_pf_device(oct);
Derek Chicklescf19a8c2017-08-01 15:05:07 -07001801#ifdef CONFIG_PCI_IOV
1802 if (!ret)
1803 pci_sriov_set_totalvfs(oct->pci_dev,
1804 oct->sriov_info.max_vfs);
1805#endif
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001806 s = "CN23XX";
1807 break;
1808
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001809 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001810 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001811 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1812 dev_id);
1813 }
1814
1815 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001816 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001817 OCTEON_MAJOR_REV(oct),
1818 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001819 octeon_get_conf(oct)->card_name,
1820 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001821
1822 return ret;
1823}
1824
1825/**
1826 * \brief PCI initialization for each Octeon device.
1827 * @param oct octeon device
1828 */
1829static int octeon_pci_os_setup(struct octeon_device *oct)
1830{
1831 /* setup PCI stuff first */
1832 if (pci_enable_device(oct->pci_dev)) {
1833 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1834 return 1;
1835 }
1836
1837 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1838 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001839 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001840 return 1;
1841 }
1842
1843 /* Enable PCI DMA Master. */
1844 pci_set_master(oct->pci_dev);
1845
1846 return 0;
1847}
1848
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001849static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1850{
1851 int q = 0;
1852
1853 if (netif_is_multiqueue(lio->netdev))
1854 q = skb->queue_mapping % lio->linfo.num_txpciq;
1855
1856 return q;
1857}
1858
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001859/**
1860 * \brief Check Tx queue state for a given network buffer
1861 * @param lio per-network private data
1862 * @param skb network buffer
1863 */
1864static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1865{
1866 int q = 0, iq = 0;
1867
1868 if (netif_is_multiqueue(lio->netdev)) {
1869 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001870 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001871 } else {
1872 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001873 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001874 }
1875
1876 if (octnet_iq_is_full(lio->oct_dev, iq))
1877 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001878
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001879 if (__netif_subqueue_stopped(lio->netdev, q)) {
1880 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001881 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001882 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001883 return 1;
1884}
1885
1886/**
1887 * \brief Unmap and free network buffer
1888 * @param buf buffer
1889 */
1890static void free_netbuf(void *buf)
1891{
1892 struct sk_buff *skb;
1893 struct octnet_buf_free_info *finfo;
1894 struct lio *lio;
1895
1896 finfo = (struct octnet_buf_free_info *)buf;
1897 skb = finfo->skb;
1898 lio = finfo->lio;
1899
1900 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1901 DMA_TO_DEVICE);
1902
1903 check_txq_state(lio, skb);
1904
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001905 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001906}
1907
1908/**
1909 * \brief Unmap and free gather buffer
1910 * @param buf buffer
1911 */
1912static void free_netsgbuf(void *buf)
1913{
1914 struct octnet_buf_free_info *finfo;
1915 struct sk_buff *skb;
1916 struct lio *lio;
1917 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001918 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001919
1920 finfo = (struct octnet_buf_free_info *)buf;
1921 skb = finfo->skb;
1922 lio = finfo->lio;
1923 g = finfo->g;
1924 frags = skb_shinfo(skb)->nr_frags;
1925
1926 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1927 g->sg[0].ptr[0], (skb->len - skb->data_len),
1928 DMA_TO_DEVICE);
1929
1930 i = 1;
1931 while (frags--) {
1932 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1933
1934 pci_unmap_page((lio->oct_dev)->pci_dev,
1935 g->sg[(i >> 2)].ptr[(i & 3)],
1936 frag->size, DMA_TO_DEVICE);
1937 i++;
1938 }
1939
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001940 iq = skb_iq(lio, skb);
1941 spin_lock(&lio->glist_lock[iq]);
1942 list_add_tail(&g->list, &lio->glist[iq]);
1943 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001944
1945 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1946
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001947 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001948}
1949
1950/**
1951 * \brief Unmap and free gather buffer with response
1952 * @param buf buffer
1953 */
1954static void free_netsgbuf_with_resp(void *buf)
1955{
1956 struct octeon_soft_command *sc;
1957 struct octnet_buf_free_info *finfo;
1958 struct sk_buff *skb;
1959 struct lio *lio;
1960 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001961 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001962
1963 sc = (struct octeon_soft_command *)buf;
1964 skb = (struct sk_buff *)sc->callback_arg;
1965 finfo = (struct octnet_buf_free_info *)&skb->cb;
1966
1967 lio = finfo->lio;
1968 g = finfo->g;
1969 frags = skb_shinfo(skb)->nr_frags;
1970
1971 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1972 g->sg[0].ptr[0], (skb->len - skb->data_len),
1973 DMA_TO_DEVICE);
1974
1975 i = 1;
1976 while (frags--) {
1977 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1978
1979 pci_unmap_page((lio->oct_dev)->pci_dev,
1980 g->sg[(i >> 2)].ptr[(i & 3)],
1981 frag->size, DMA_TO_DEVICE);
1982 i++;
1983 }
1984
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001985 iq = skb_iq(lio, skb);
1986
1987 spin_lock(&lio->glist_lock[iq]);
1988 list_add_tail(&g->list, &lio->glist[iq]);
1989 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001990
1991 /* Don't free the skb yet */
1992
1993 check_txq_state(lio, skb);
1994}
1995
1996/**
1997 * \brief Adjust ptp frequency
1998 * @param ptp PTP clock info
1999 * @param ppb how much to adjust by, in parts-per-billion
2000 */
2001static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
2002{
2003 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2004 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2005 u64 comp, delta;
2006 unsigned long flags;
2007 bool neg_adj = false;
2008
2009 if (ppb < 0) {
2010 neg_adj = true;
2011 ppb = -ppb;
2012 }
2013
2014 /* The hardware adds the clock compensation value to the
2015 * PTP clock on every coprocessor clock cycle, so we
2016 * compute the delta in terms of coprocessor clocks.
2017 */
2018 delta = (u64)ppb << 32;
2019 do_div(delta, oct->coproc_clock_rate);
2020
2021 spin_lock_irqsave(&lio->ptp_lock, flags);
2022 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
2023 if (neg_adj)
2024 comp -= delta;
2025 else
2026 comp += delta;
2027 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2028 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2029
2030 return 0;
2031}
2032
2033/**
2034 * \brief Adjust ptp time
2035 * @param ptp PTP clock info
2036 * @param delta how much to adjust by, in nanosecs
2037 */
2038static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2039{
2040 unsigned long flags;
2041 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2042
2043 spin_lock_irqsave(&lio->ptp_lock, flags);
2044 lio->ptp_adjust += delta;
2045 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2046
2047 return 0;
2048}
2049
2050/**
2051 * \brief Get hardware clock time, including any adjustment
2052 * @param ptp PTP clock info
2053 * @param ts timespec
2054 */
2055static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2056 struct timespec64 *ts)
2057{
2058 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002059 unsigned long flags;
2060 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2061 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2062
2063 spin_lock_irqsave(&lio->ptp_lock, flags);
2064 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2065 ns += lio->ptp_adjust;
2066 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2067
Kefeng Wang286af312016-01-27 17:34:37 +08002068 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002069
2070 return 0;
2071}
2072
2073/**
2074 * \brief Set hardware clock time. Reset adjustment
2075 * @param ptp PTP clock info
2076 * @param ts timespec
2077 */
2078static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2079 const struct timespec64 *ts)
2080{
2081 u64 ns;
2082 unsigned long flags;
2083 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2084 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2085
2086 ns = timespec_to_ns(ts);
2087
2088 spin_lock_irqsave(&lio->ptp_lock, flags);
2089 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2090 lio->ptp_adjust = 0;
2091 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2092
2093 return 0;
2094}
2095
2096/**
2097 * \brief Check if PTP is enabled
2098 * @param ptp PTP clock info
2099 * @param rq request
2100 * @param on is it on
2101 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002102static int
2103liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2104 struct ptp_clock_request *rq __attribute__((unused)),
2105 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002106{
2107 return -EOPNOTSUPP;
2108}
2109
2110/**
2111 * \brief Open PTP clock source
2112 * @param netdev network device
2113 */
2114static void oct_ptp_open(struct net_device *netdev)
2115{
2116 struct lio *lio = GET_LIO(netdev);
2117 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2118
2119 spin_lock_init(&lio->ptp_lock);
2120
2121 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2122 lio->ptp_info.owner = THIS_MODULE;
2123 lio->ptp_info.max_adj = 250000000;
2124 lio->ptp_info.n_alarm = 0;
2125 lio->ptp_info.n_ext_ts = 0;
2126 lio->ptp_info.n_per_out = 0;
2127 lio->ptp_info.pps = 0;
2128 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2129 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2130 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2131 lio->ptp_info.settime64 = liquidio_ptp_settime;
2132 lio->ptp_info.enable = liquidio_ptp_enable;
2133
2134 lio->ptp_adjust = 0;
2135
2136 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2137 &oct->pci_dev->dev);
2138
2139 if (IS_ERR(lio->ptp_clock))
2140 lio->ptp_clock = NULL;
2141}
2142
2143/**
2144 * \brief Init PTP clock
2145 * @param oct octeon device
2146 */
2147static void liquidio_ptp_init(struct octeon_device *oct)
2148{
2149 u64 clock_comp, cfg;
2150
2151 clock_comp = (u64)NSEC_PER_SEC << 32;
2152 do_div(clock_comp, oct->coproc_clock_rate);
2153 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2154
2155 /* Enable */
2156 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2157 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2158}
2159
2160/**
2161 * \brief Load firmware to device
2162 * @param oct octeon device
2163 *
2164 * Maps device to firmware filename, requests firmware, and downloads it
2165 */
2166static int load_firmware(struct octeon_device *oct)
2167{
2168 int ret = 0;
2169 const struct firmware *fw;
2170 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2171 char *tmp_fw_type;
2172
Felix Manlunas7cc61db2017-03-23 13:26:28 -07002173 if (fw_type_is_none()) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002174 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2175 return ret;
2176 }
2177
2178 if (fw_type[0] == '\0')
2179 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2180 else
2181 tmp_fw_type = fw_type;
2182
2183 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2184 octeon_get_conf(oct)->card_name, tmp_fw_type,
2185 LIO_FW_NAME_SUFFIX);
2186
2187 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2188 if (ret) {
2189 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2190 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002191 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002192 return ret;
2193 }
2194
2195 ret = octeon_download_firmware(oct, fw->data, fw->size);
2196
2197 release_firmware(fw);
2198
2199 return ret;
2200}
2201
2202/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002203 * \brief Callback for getting interface configuration
2204 * @param status status of request
2205 * @param buf pointer to resp structure
2206 */
2207static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002208 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002209 void *buf)
2210{
2211 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2212 struct liquidio_if_cfg_resp *resp;
2213 struct liquidio_if_cfg_context *ctx;
2214
2215 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002216 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002217
2218 oct = lio_get_device(ctx->octeon_id);
2219 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002220 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2221 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002222 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002223
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002224 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2225 resp->cfg_info.liquidio_firmware_version);
2226
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002227 /* This barrier is required to be sure that the response has been
2228 * written fully before waking up the handler
2229 */
2230 wmb();
2231
2232 wake_up_interruptible(&ctx->wc);
2233}
2234
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002235/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002236 * \brief Poll routine for checking transmit queue status
2237 * @param work work_struct data structure
2238 */
2239static void octnet_poll_check_txq_status(struct work_struct *work)
2240{
2241 struct cavium_wk *wk = (struct cavium_wk *)work;
2242 struct lio *lio = (struct lio *)wk->ctxptr;
2243
2244 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2245 return;
2246
2247 check_txq_status(lio);
2248 queue_delayed_work(lio->txq_status_wq.wq,
2249 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2250}
2251
2252/**
2253 * \brief Sets up the txq poll check
2254 * @param netdev network device
2255 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002256static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002257{
2258 struct lio *lio = GET_LIO(netdev);
2259 struct octeon_device *oct = lio->oct_dev;
2260
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302261 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2262 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002263 if (!lio->txq_status_wq.wq) {
2264 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002265 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002266 }
2267 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2268 octnet_poll_check_txq_status);
2269 lio->txq_status_wq.wk.ctxptr = lio;
2270 queue_delayed_work(lio->txq_status_wq.wq,
2271 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002272 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002273}
2274
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002275static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2276{
2277 struct lio *lio = GET_LIO(netdev);
2278
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002279 if (lio->txq_status_wq.wq) {
2280 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2281 destroy_workqueue(lio->txq_status_wq.wq);
2282 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002283}
2284
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002285/**
2286 * \brief Net device open for LiquidIO
2287 * @param netdev network device
2288 */
2289static int liquidio_open(struct net_device *netdev)
2290{
2291 struct lio *lio = GET_LIO(netdev);
2292 struct octeon_device *oct = lio->oct_dev;
2293 struct napi_struct *napi, *n;
2294
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002295 if (oct->props[lio->ifidx].napi_enabled == 0) {
2296 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2297 napi_enable(napi);
2298
2299 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002300
2301 if (OCTEON_CN23XX_PF(oct))
2302 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002303 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002304
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002305 if (oct->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002306 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002307
2308 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002309
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002310 /* Ready for link status updates */
2311 lio->intf_open = 1;
2312
2313 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2314
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002315 if (OCTEON_CN23XX_PF(oct)) {
2316 if (!oct->msix_on)
2317 if (setup_tx_poll_fn(netdev))
2318 return -1;
2319 } else {
2320 if (setup_tx_poll_fn(netdev))
2321 return -1;
2322 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002323
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002324 start_txq(netdev);
2325
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002326 /* tell Octeon to start forwarding packets to host */
2327 send_rx_ctrl_cmd(lio, 1);
2328
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002329 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2330 netdev->name);
2331
2332 return 0;
2333}
2334
2335/**
2336 * \brief Net device stop for LiquidIO
2337 * @param netdev network device
2338 */
2339static int liquidio_stop(struct net_device *netdev)
2340{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002341 struct lio *lio = GET_LIO(netdev);
2342 struct octeon_device *oct = lio->oct_dev;
Intiyaz Basha42013e92017-08-08 19:34:28 -07002343 struct napi_struct *napi, *n;
2344
2345 if (oct->props[lio->ifidx].napi_enabled) {
2346 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2347 napi_disable(napi);
2348
2349 oct->props[lio->ifidx].napi_enabled = 0;
2350
2351 if (OCTEON_CN23XX_PF(oct))
2352 oct->droq[0]->ops.poll_mode = 0;
2353 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002354
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002355 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2356
2357 netif_tx_disable(netdev);
2358
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002359 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002360 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002361 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002362 lio->linfo.link.s.link_up = 0;
2363 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002364
Felix Manlunascb2336b2017-01-11 17:09:02 -08002365 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002366 send_rx_ctrl_cmd(lio, 0);
2367
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002368 if (OCTEON_CN23XX_PF(oct)) {
2369 if (!oct->msix_on)
2370 cleanup_tx_poll_fn(netdev);
2371 } else {
2372 cleanup_tx_poll_fn(netdev);
2373 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002374
2375 if (lio->ptp_clock) {
2376 ptp_clock_unregister(lio->ptp_clock);
2377 lio->ptp_clock = NULL;
2378 }
2379
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002380 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002381
2382 return 0;
2383}
2384
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002385/**
2386 * \brief Converts a mask based on net device flags
2387 * @param netdev network device
2388 *
2389 * This routine generates a octnet_ifflags mask from the net device flags
2390 * received from the OS.
2391 */
2392static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2393{
2394 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2395
2396 if (netdev->flags & IFF_PROMISC)
2397 f |= OCTNET_IFFLAG_PROMISC;
2398
2399 if (netdev->flags & IFF_ALLMULTI)
2400 f |= OCTNET_IFFLAG_ALLMULTI;
2401
2402 if (netdev->flags & IFF_MULTICAST) {
2403 f |= OCTNET_IFFLAG_MULTICAST;
2404
2405 /* Accept all multicast addresses if there are more than we
2406 * can handle
2407 */
2408 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2409 f |= OCTNET_IFFLAG_ALLMULTI;
2410 }
2411
2412 if (netdev->flags & IFF_BROADCAST)
2413 f |= OCTNET_IFFLAG_BROADCAST;
2414
2415 return f;
2416}
2417
2418/**
2419 * \brief Net device set_multicast_list
2420 * @param netdev network device
2421 */
2422static void liquidio_set_mcast_list(struct net_device *netdev)
2423{
2424 struct lio *lio = GET_LIO(netdev);
2425 struct octeon_device *oct = lio->oct_dev;
2426 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002427 struct netdev_hw_addr *ha;
2428 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002429 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002430 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2431
2432 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2433
2434 /* Create a ctrl pkt command to be sent to core app. */
2435 nctrl.ncmd.u64 = 0;
2436 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002437 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2438 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002439 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002440 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002441 nctrl.netpndev = (u64)netdev;
2442 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2443
2444 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002445 mc = &nctrl.udd[0];
2446 netdev_for_each_mc_addr(ha, netdev) {
2447 *mc = 0;
2448 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2449 /* no need to swap bytes */
2450
2451 if (++mc > &nctrl.udd[mc_count])
2452 break;
2453 }
2454
2455 /* Apparently, any activity in this call from the kernel has to
2456 * be atomic. So we won't wait for response.
2457 */
2458 nctrl.wait_time = 0;
2459
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002460 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002461 if (ret < 0) {
2462 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2463 ret);
2464 }
2465}
2466
2467/**
2468 * \brief Net device set_mac_address
2469 * @param netdev network device
2470 */
2471static int liquidio_set_mac(struct net_device *netdev, void *p)
2472{
2473 int ret = 0;
2474 struct lio *lio = GET_LIO(netdev);
2475 struct octeon_device *oct = lio->oct_dev;
2476 struct sockaddr *addr = (struct sockaddr *)p;
2477 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002478
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002479 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002480 return -EADDRNOTAVAIL;
2481
2482 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2483
2484 nctrl.ncmd.u64 = 0;
2485 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002486 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002487 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002488 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002489 nctrl.netpndev = (u64)netdev;
2490 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2491 nctrl.wait_time = 100;
2492
2493 nctrl.udd[0] = 0;
2494 /* The MAC Address is presented in network byte order. */
2495 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2496
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002497 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002498 if (ret < 0) {
2499 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2500 return -ENOMEM;
2501 }
2502 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2503 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2504
2505 return 0;
2506}
2507
2508/**
2509 * \brief Net device get_stats
2510 * @param netdev network device
2511 */
2512static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2513{
2514 struct lio *lio = GET_LIO(netdev);
2515 struct net_device_stats *stats = &netdev->stats;
2516 struct octeon_device *oct;
2517 u64 pkts = 0, drop = 0, bytes = 0;
2518 struct oct_droq_stats *oq_stats;
2519 struct oct_iq_stats *iq_stats;
2520 int i, iq_no, oq_no;
2521
2522 oct = lio->oct_dev;
2523
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -07002524 if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
2525 return stats;
2526
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002527 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002528 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002529 iq_stats = &oct->instr_queue[iq_no]->stats;
2530 pkts += iq_stats->tx_done;
2531 drop += iq_stats->tx_dropped;
2532 bytes += iq_stats->tx_tot_bytes;
2533 }
2534
2535 stats->tx_packets = pkts;
2536 stats->tx_bytes = bytes;
2537 stats->tx_dropped = drop;
2538
2539 pkts = 0;
2540 drop = 0;
2541 bytes = 0;
2542
2543 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002544 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002545 oq_stats = &oct->droq[oq_no]->stats;
2546 pkts += oq_stats->rx_pkts_received;
2547 drop += (oq_stats->rx_dropped +
2548 oq_stats->dropped_nodispatch +
2549 oq_stats->dropped_toomany +
2550 oq_stats->dropped_nomem);
2551 bytes += oq_stats->rx_bytes_received;
2552 }
2553
2554 stats->rx_bytes = bytes;
2555 stats->rx_packets = pkts;
2556 stats->rx_dropped = drop;
2557
2558 return stats;
2559}
2560
2561/**
2562 * \brief Net device change_mtu
2563 * @param netdev network device
2564 */
2565static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2566{
2567 struct lio *lio = GET_LIO(netdev);
2568 struct octeon_device *oct = lio->oct_dev;
2569 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002570 int ret = 0;
2571
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002572 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2573
2574 nctrl.ncmd.u64 = 0;
2575 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002576 nctrl.ncmd.s.param1 = new_mtu;
2577 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002578 nctrl.wait_time = 100;
2579 nctrl.netpndev = (u64)netdev;
2580 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2581
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002582 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002583 if (ret < 0) {
2584 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2585 return -1;
2586 }
2587
2588 lio->mtu = new_mtu;
2589
2590 return 0;
2591}
2592
2593/**
2594 * \brief Handler for SIOCSHWTSTAMP ioctl
2595 * @param netdev network device
2596 * @param ifr interface request
2597 * @param cmd command
2598 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002599static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002600{
2601 struct hwtstamp_config conf;
2602 struct lio *lio = GET_LIO(netdev);
2603
2604 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2605 return -EFAULT;
2606
2607 if (conf.flags)
2608 return -EINVAL;
2609
2610 switch (conf.tx_type) {
2611 case HWTSTAMP_TX_ON:
2612 case HWTSTAMP_TX_OFF:
2613 break;
2614 default:
2615 return -ERANGE;
2616 }
2617
2618 switch (conf.rx_filter) {
2619 case HWTSTAMP_FILTER_NONE:
2620 break;
2621 case HWTSTAMP_FILTER_ALL:
2622 case HWTSTAMP_FILTER_SOME:
2623 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2624 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2625 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2626 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2627 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2628 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2629 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2630 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2631 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2632 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2633 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2634 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvare3412572017-05-19 17:52:36 +02002635 case HWTSTAMP_FILTER_NTP_ALL:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002636 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2637 break;
2638 default:
2639 return -ERANGE;
2640 }
2641
2642 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2643 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2644
2645 else
2646 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2647
2648 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2649}
2650
2651/**
2652 * \brief ioctl handler
2653 * @param netdev network device
2654 * @param ifr interface request
2655 * @param cmd command
2656 */
2657static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2658{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002659 struct lio *lio = GET_LIO(netdev);
2660
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002661 switch (cmd) {
2662 case SIOCSHWTSTAMP:
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002663 if (lio->oct_dev->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002664 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002665 default:
2666 return -EOPNOTSUPP;
2667 }
2668}
2669
2670/**
2671 * \brief handle a Tx timestamp response
2672 * @param status response status
2673 * @param buf pointer to skb
2674 */
2675static void handle_timestamp(struct octeon_device *oct,
2676 u32 status,
2677 void *buf)
2678{
2679 struct octnet_buf_free_info *finfo;
2680 struct octeon_soft_command *sc;
2681 struct oct_timestamp_resp *resp;
2682 struct lio *lio;
2683 struct sk_buff *skb = (struct sk_buff *)buf;
2684
2685 finfo = (struct octnet_buf_free_info *)skb->cb;
2686 lio = finfo->lio;
2687 sc = finfo->sc;
2688 oct = lio->oct_dev;
2689 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2690
2691 if (status != OCTEON_REQUEST_DONE) {
2692 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2693 CVM_CAST64(status));
2694 resp->timestamp = 0;
2695 }
2696
2697 octeon_swap_8B_data(&resp->timestamp, 1);
2698
Colin Ian King19a6d152016-02-05 16:30:39 +00002699 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002700 struct skb_shared_hwtstamps ts;
2701 u64 ns = resp->timestamp;
2702
2703 netif_info(lio, tx_done, lio->netdev,
2704 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2705 skb, (unsigned long long)ns);
2706 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2707 skb_tstamp_tx(skb, &ts);
2708 }
2709
2710 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002711 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002712}
2713
2714/* \brief Send a data packet that will be timestamped
2715 * @param oct octeon device
2716 * @param ndata pointer to network data
2717 * @param finfo pointer to private network data
2718 */
2719static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2720 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002721 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002722{
2723 int retval;
2724 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002725 struct lio *lio;
2726 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002727 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002728
2729 lio = finfo->lio;
2730
2731 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2732 sizeof(struct oct_timestamp_resp));
2733 finfo->sc = sc;
2734
2735 if (!sc) {
2736 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2737 return IQ_SEND_FAILED;
2738 }
2739
2740 if (ndata->reqtype == REQTYPE_NORESP_NET)
2741 ndata->reqtype = REQTYPE_RESP_NET;
2742 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2743 ndata->reqtype = REQTYPE_RESP_NET_SG;
2744
2745 sc->callback = handle_timestamp;
2746 sc->callback_arg = finfo->skb;
2747 sc->iq_no = ndata->q_no;
2748
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002749 if (OCTEON_CN23XX_PF(oct))
2750 len = (u32)((struct octeon_instr_ih3 *)
2751 (&sc->cmd.cmd3.ih3))->dlengsz;
2752 else
2753 len = (u32)((struct octeon_instr_ih2 *)
2754 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002755
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002756 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002757
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002758 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002759 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002760
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002761 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002762 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2763 retval);
2764 octeon_free_soft_command(oct, sc);
2765 } else {
2766 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2767 }
2768
2769 return retval;
2770}
2771
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002772/** \brief Transmit networks packets to the Octeon interface
2773 * @param skbuff skbuff struct to be passed to network layer.
2774 * @param netdev pointer to network device
2775 * @returns whether the packet was transmitted to the device okay or not
2776 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2777 */
2778static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2779{
2780 struct lio *lio;
2781 struct octnet_buf_free_info *finfo;
2782 union octnic_cmd_setup cmdsetup;
2783 struct octnic_data_pkt ndata;
2784 struct octeon_device *oct;
2785 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002786 struct octeon_instr_irh *irh;
2787 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002788 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002789 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002790 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002791 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002792 u32 tag = 0;
2793
2794 lio = GET_LIO(netdev);
2795 oct = lio->oct_dev;
2796
2797 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002798 q_idx = skb->queue_mapping;
2799 q_idx = (q_idx % (lio->linfo.num_txpciq));
2800 tag = q_idx;
2801 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 } else {
2803 iq_no = lio->txq;
2804 }
2805
2806 stats = &oct->instr_queue[iq_no]->stats;
2807
2808 /* Check for all conditions in which the current packet cannot be
2809 * transmitted.
2810 */
2811 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002812 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002813 (skb->len <= 0)) {
2814 netif_info(lio, tx_err, lio->netdev,
2815 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002816 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002817 goto lio_xmit_failed;
2818 }
2819
2820 /* Use space in skb->cb to store info used to unmap and
2821 * free the buffers.
2822 */
2823 finfo = (struct octnet_buf_free_info *)skb->cb;
2824 finfo->lio = lio;
2825 finfo->skb = skb;
2826 finfo->sc = NULL;
2827
2828 /* Prepare the attributes for the data to be passed to OSI. */
2829 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2830
2831 ndata.buf = (void *)finfo;
2832
2833 ndata.q_no = iq_no;
2834
2835 if (netif_is_multiqueue(netdev)) {
2836 if (octnet_iq_is_full(oct, ndata.q_no)) {
2837 /* defer sending if queue is full */
2838 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2839 ndata.q_no);
2840 stats->tx_iq_busy++;
2841 return NETDEV_TX_BUSY;
2842 }
2843 } else {
2844 if (octnet_iq_is_full(oct, lio->txq)) {
2845 /* defer sending if queue is full */
2846 stats->tx_iq_busy++;
2847 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002848 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002849 return NETDEV_TX_BUSY;
2850 }
2851 }
2852 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002853 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002854 */
2855
2856 ndata.datasize = skb->len;
2857
2858 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002859 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002860
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002861 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2862 if (skb->encapsulation) {
2863 cmdsetup.s.tnl_csum = 1;
2864 stats->tx_vxlan++;
2865 } else {
2866 cmdsetup.s.transport_csum = 1;
2867 }
2868 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002869 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2870 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2871 cmdsetup.s.timestamp = 1;
2872 }
2873
2874 if (skb_shinfo(skb)->nr_frags == 0) {
2875 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002876 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002877
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002878 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002879 dptr = dma_map_single(&oct->pci_dev->dev,
2880 skb->data,
2881 skb->len,
2882 DMA_TO_DEVICE);
2883 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002884 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2885 __func__);
2886 return NETDEV_TX_BUSY;
2887 }
2888
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002889 if (OCTEON_CN23XX_PF(oct))
2890 ndata.cmd.cmd3.dptr = dptr;
2891 else
2892 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002893 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002894 ndata.reqtype = REQTYPE_NORESP_NET;
2895
2896 } else {
2897 int i, frags;
2898 struct skb_frag_struct *frag;
2899 struct octnic_gather *g;
2900
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002901 spin_lock(&lio->glist_lock[q_idx]);
2902 g = (struct octnic_gather *)
2903 list_delete_head(&lio->glist[q_idx]);
2904 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002905
2906 if (!g) {
2907 netif_info(lio, tx_err, lio->netdev,
2908 "Transmit scatter gather: glist null!\n");
2909 goto lio_xmit_failed;
2910 }
2911
2912 cmdsetup.s.gather = 1;
2913 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002914 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002915
2916 memset(g->sg, 0, g->sg_size);
2917
2918 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2919 skb->data,
2920 (skb->len - skb->data_len),
2921 DMA_TO_DEVICE);
2922 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2923 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2924 __func__);
2925 return NETDEV_TX_BUSY;
2926 }
2927 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2928
2929 frags = skb_shinfo(skb)->nr_frags;
2930 i = 1;
2931 while (frags--) {
2932 frag = &skb_shinfo(skb)->frags[i - 1];
2933
2934 g->sg[(i >> 2)].ptr[(i & 3)] =
2935 dma_map_page(&oct->pci_dev->dev,
2936 frag->page.p,
2937 frag->page_offset,
2938 frag->size,
2939 DMA_TO_DEVICE);
2940
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002941 if (dma_mapping_error(&oct->pci_dev->dev,
2942 g->sg[i >> 2].ptr[i & 3])) {
2943 dma_unmap_single(&oct->pci_dev->dev,
2944 g->sg[0].ptr[0],
2945 skb->len - skb->data_len,
2946 DMA_TO_DEVICE);
2947 for (j = 1; j < i; j++) {
2948 frag = &skb_shinfo(skb)->frags[j - 1];
2949 dma_unmap_page(&oct->pci_dev->dev,
2950 g->sg[j >> 2].ptr[j & 3],
2951 frag->size,
2952 DMA_TO_DEVICE);
2953 }
2954 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2955 __func__);
2956 return NETDEV_TX_BUSY;
2957 }
2958
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002959 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2960 i++;
2961 }
2962
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002963 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002964
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002965 if (OCTEON_CN23XX_PF(oct))
2966 ndata.cmd.cmd3.dptr = dptr;
2967 else
2968 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002969 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002970 finfo->g = g;
2971
2972 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2973 }
2974
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002975 if (OCTEON_CN23XX_PF(oct)) {
2976 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
2977 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
2978 } else {
2979 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2980 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
2981 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002982
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002983 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002984 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2985 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002986 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002987 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002988
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002989 /* HW insert VLAN tag */
2990 if (skb_vlan_tag_present(skb)) {
2991 irh->priority = skb_vlan_tag_get(skb) >> 13;
2992 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2993 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002994
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002995 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002996 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002997 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002998 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002999 if (status == IQ_SEND_FAILED)
3000 goto lio_xmit_failed;
3001
3002 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3003
3004 if (status == IQ_SEND_STOP)
3005 stop_q(lio->netdev, q_idx);
3006
Florian Westphal860e9532016-05-03 16:33:13 +02003007 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003008
Satanand Burla80c8eae2017-01-26 11:52:35 -08003009 if (tx_info->s.gso_segs)
3010 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003011 else
3012 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003013 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003014
3015 return NETDEV_TX_OK;
3016
3017lio_xmit_failed:
3018 stats->tx_dropped++;
3019 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3020 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003021 if (dptr)
3022 dma_unmap_single(&oct->pci_dev->dev, dptr,
3023 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003024 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003025 return NETDEV_TX_OK;
3026}
3027
3028/** \brief Network device Tx timeout
3029 * @param netdev pointer to network device
3030 */
3031static void liquidio_tx_timeout(struct net_device *netdev)
3032{
3033 struct lio *lio;
3034
3035 lio = GET_LIO(netdev);
3036
3037 netif_info(lio, tx_err, lio->netdev,
3038 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3039 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003040 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003041 txqs_wake(netdev);
3042}
3043
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003044static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3045 __be16 proto __attribute__((unused)),
3046 u16 vid)
3047{
3048 struct lio *lio = GET_LIO(netdev);
3049 struct octeon_device *oct = lio->oct_dev;
3050 struct octnic_ctrl_pkt nctrl;
3051 int ret = 0;
3052
3053 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3054
3055 nctrl.ncmd.u64 = 0;
3056 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3057 nctrl.ncmd.s.param1 = vid;
3058 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3059 nctrl.wait_time = 100;
3060 nctrl.netpndev = (u64)netdev;
3061 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3062
3063 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3064 if (ret < 0) {
3065 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3066 ret);
3067 }
3068
3069 return ret;
3070}
3071
3072static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3073 __be16 proto __attribute__((unused)),
3074 u16 vid)
3075{
3076 struct lio *lio = GET_LIO(netdev);
3077 struct octeon_device *oct = lio->oct_dev;
3078 struct octnic_ctrl_pkt nctrl;
3079 int ret = 0;
3080
3081 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3082
3083 nctrl.ncmd.u64 = 0;
3084 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3085 nctrl.ncmd.s.param1 = vid;
3086 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3087 nctrl.wait_time = 100;
3088 nctrl.netpndev = (u64)netdev;
3089 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3090
3091 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3092 if (ret < 0) {
3093 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3094 ret);
3095 }
3096 return ret;
3097}
3098
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003099/** Sending command to enable/disable RX checksum offload
3100 * @param netdev pointer to network device
3101 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3102 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3103 * OCTNET_CMD_RXCSUM_DISABLE
3104 * @returns SUCCESS or FAILURE
3105 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003106static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3107 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003108{
3109 struct lio *lio = GET_LIO(netdev);
3110 struct octeon_device *oct = lio->oct_dev;
3111 struct octnic_ctrl_pkt nctrl;
3112 int ret = 0;
3113
Felix Manlunas0c264582017-04-06 19:22:22 -07003114 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3115
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003116 nctrl.ncmd.u64 = 0;
3117 nctrl.ncmd.s.cmd = command;
3118 nctrl.ncmd.s.param1 = rx_cmd;
3119 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3120 nctrl.wait_time = 100;
3121 nctrl.netpndev = (u64)netdev;
3122 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3123
3124 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3125 if (ret < 0) {
3126 dev_err(&oct->pci_dev->dev,
3127 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3128 ret);
3129 }
3130 return ret;
3131}
3132
3133/** Sending command to add/delete VxLAN UDP port to firmware
3134 * @param netdev pointer to network device
3135 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3136 * @param vxlan_port VxLAN port to be added or deleted
3137 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3138 * OCTNET_CMD_VXLAN_PORT_DEL
3139 * @returns SUCCESS or FAILURE
3140 */
3141static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3142 u16 vxlan_port, u8 vxlan_cmd_bit)
3143{
3144 struct lio *lio = GET_LIO(netdev);
3145 struct octeon_device *oct = lio->oct_dev;
3146 struct octnic_ctrl_pkt nctrl;
3147 int ret = 0;
3148
Felix Manlunas0c264582017-04-06 19:22:22 -07003149 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3150
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003151 nctrl.ncmd.u64 = 0;
3152 nctrl.ncmd.s.cmd = command;
3153 nctrl.ncmd.s.more = vxlan_cmd_bit;
3154 nctrl.ncmd.s.param1 = vxlan_port;
3155 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3156 nctrl.wait_time = 100;
3157 nctrl.netpndev = (u64)netdev;
3158 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3159
3160 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3161 if (ret < 0) {
3162 dev_err(&oct->pci_dev->dev,
3163 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3164 ret);
3165 }
3166 return ret;
3167}
3168
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003169/** \brief Net device fix features
3170 * @param netdev pointer to network device
3171 * @param request features requested
3172 * @returns updated features list
3173 */
3174static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3175 netdev_features_t request)
3176{
3177 struct lio *lio = netdev_priv(netdev);
3178
3179 if ((request & NETIF_F_RXCSUM) &&
3180 !(lio->dev_capability & NETIF_F_RXCSUM))
3181 request &= ~NETIF_F_RXCSUM;
3182
3183 if ((request & NETIF_F_HW_CSUM) &&
3184 !(lio->dev_capability & NETIF_F_HW_CSUM))
3185 request &= ~NETIF_F_HW_CSUM;
3186
3187 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3188 request &= ~NETIF_F_TSO;
3189
3190 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3191 request &= ~NETIF_F_TSO6;
3192
3193 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3194 request &= ~NETIF_F_LRO;
3195
3196 /*Disable LRO if RXCSUM is off */
3197 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3198 (lio->dev_capability & NETIF_F_LRO))
3199 request &= ~NETIF_F_LRO;
3200
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003201 if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3202 !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
3203 request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3204
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003205 return request;
3206}
3207
3208/** \brief Net device set features
3209 * @param netdev pointer to network device
3210 * @param features features to enable/disable
3211 */
3212static int liquidio_set_features(struct net_device *netdev,
3213 netdev_features_t features)
3214{
3215 struct lio *lio = netdev_priv(netdev);
3216
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003217 if ((features & NETIF_F_LRO) &&
3218 (lio->dev_capability & NETIF_F_LRO) &&
3219 !(netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003220 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3221 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003222 else if (!(features & NETIF_F_LRO) &&
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003223 (lio->dev_capability & NETIF_F_LRO) &&
3224 (netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003225 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3226 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003227
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003228 /* Sending command to firmware to enable/disable RX checksum
3229 * offload settings using ethtool
3230 */
3231 if (!(netdev->features & NETIF_F_RXCSUM) &&
3232 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3233 (features & NETIF_F_RXCSUM))
3234 liquidio_set_rxcsum_command(netdev,
3235 OCTNET_CMD_TNL_RX_CSUM_CTL,
3236 OCTNET_CMD_RXCSUM_ENABLE);
3237 else if ((netdev->features & NETIF_F_RXCSUM) &&
3238 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3239 !(features & NETIF_F_RXCSUM))
3240 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3241 OCTNET_CMD_RXCSUM_DISABLE);
3242
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003243 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3244 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3245 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3246 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3247 OCTNET_CMD_VLAN_FILTER_ENABLE);
3248 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3249 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3250 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3251 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3252 OCTNET_CMD_VLAN_FILTER_DISABLE);
3253
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003254 return 0;
3255}
3256
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003257static void liquidio_add_vxlan_port(struct net_device *netdev,
3258 struct udp_tunnel_info *ti)
3259{
3260 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3261 return;
3262
3263 liquidio_vxlan_port_command(netdev,
3264 OCTNET_CMD_VXLAN_PORT_CONFIG,
3265 htons(ti->port),
3266 OCTNET_CMD_VXLAN_PORT_ADD);
3267}
3268
3269static void liquidio_del_vxlan_port(struct net_device *netdev,
3270 struct udp_tunnel_info *ti)
3271{
3272 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3273 return;
3274
3275 liquidio_vxlan_port_command(netdev,
3276 OCTNET_CMD_VXLAN_PORT_CONFIG,
3277 htons(ti->port),
3278 OCTNET_CMD_VXLAN_PORT_DEL);
3279}
3280
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003281static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3282 u8 *mac, bool is_admin_assigned)
3283{
3284 struct lio *lio = GET_LIO(netdev);
3285 struct octeon_device *oct = lio->oct_dev;
3286 struct octnic_ctrl_pkt nctrl;
3287
3288 if (!is_valid_ether_addr(mac))
3289 return -EINVAL;
3290
3291 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3292 return -EINVAL;
3293
3294 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3295
3296 nctrl.ncmd.u64 = 0;
3297 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3298 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3299 nctrl.ncmd.s.param1 = vfidx + 1;
3300 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3301 nctrl.ncmd.s.more = 1;
3302 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003303 nctrl.netpndev = (u64)netdev;
3304 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003305 nctrl.wait_time = LIO_CMD_WAIT_TM;
3306
3307 nctrl.udd[0] = 0;
3308 /* The MAC Address is presented in network byte order. */
3309 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3310
3311 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3312
3313 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3314
3315 return 0;
3316}
3317
3318static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3319{
3320 struct lio *lio = GET_LIO(netdev);
3321 struct octeon_device *oct = lio->oct_dev;
3322 int retval;
3323
Felix Manlunas0d9a5992017-05-16 11:28:00 -07003324 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3325 return -EINVAL;
3326
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003327 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3328 if (!retval)
3329 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3330
3331 return retval;
3332}
3333
3334static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3335 u16 vlan, u8 qos, __be16 vlan_proto)
3336{
3337 struct lio *lio = GET_LIO(netdev);
3338 struct octeon_device *oct = lio->oct_dev;
3339 struct octnic_ctrl_pkt nctrl;
3340 u16 vlantci;
3341
3342 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3343 return -EINVAL;
3344
3345 if (vlan_proto != htons(ETH_P_8021Q))
3346 return -EPROTONOSUPPORT;
3347
3348 if (vlan >= VLAN_N_VID || qos > 7)
3349 return -EINVAL;
3350
3351 if (vlan)
3352 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3353 else
3354 vlantci = 0;
3355
3356 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3357 return 0;
3358
3359 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3360
3361 if (vlan)
3362 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3363 else
3364 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3365
3366 nctrl.ncmd.s.param1 = vlantci;
3367 nctrl.ncmd.s.param2 =
3368 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3369 nctrl.ncmd.s.more = 0;
3370 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3371 nctrl.cb_fn = 0;
3372 nctrl.wait_time = LIO_CMD_WAIT_TM;
3373
3374 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3375
3376 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3377
3378 return 0;
3379}
3380
3381static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3382 struct ifla_vf_info *ivi)
3383{
3384 struct lio *lio = GET_LIO(netdev);
3385 struct octeon_device *oct = lio->oct_dev;
3386 u8 *macaddr;
3387
3388 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3389 return -EINVAL;
3390
3391 ivi->vf = vfidx;
3392 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3393 ether_addr_copy(&ivi->mac[0], macaddr);
3394 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3395 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3396 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3397 return 0;
3398}
3399
3400static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3401 int linkstate)
3402{
3403 struct lio *lio = GET_LIO(netdev);
3404 struct octeon_device *oct = lio->oct_dev;
3405 struct octnic_ctrl_pkt nctrl;
3406
3407 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3408 return -EINVAL;
3409
3410 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3411 return 0;
3412
3413 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3414 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3415 nctrl.ncmd.s.param1 =
3416 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3417 nctrl.ncmd.s.param2 = linkstate;
3418 nctrl.ncmd.s.more = 0;
3419 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3420 nctrl.cb_fn = 0;
3421 nctrl.wait_time = LIO_CMD_WAIT_TM;
3422
3423 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3424
3425 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3426
3427 return 0;
3428}
3429
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003430static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003431 .ndo_open = liquidio_open,
3432 .ndo_stop = liquidio_stop,
3433 .ndo_start_xmit = liquidio_xmit,
3434 .ndo_get_stats = liquidio_get_stats,
3435 .ndo_set_mac_address = liquidio_set_mac,
3436 .ndo_set_rx_mode = liquidio_set_mcast_list,
3437 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003438
3439 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3440 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003441 .ndo_change_mtu = liquidio_change_mtu,
3442 .ndo_do_ioctl = liquidio_ioctl,
3443 .ndo_fix_features = liquidio_fix_features,
3444 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003445 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3446 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003447 .ndo_set_vf_mac = liquidio_set_vf_mac,
3448 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3449 .ndo_get_vf_config = liquidio_get_vf_config,
3450 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003451};
3452
3453/** \brief Entry point for the liquidio module
3454 */
3455static int __init liquidio_init(void)
3456{
3457 int i;
3458 struct handshake *hs;
3459
3460 init_completion(&first_stage);
3461
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003462 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003463
3464 if (liquidio_init_pci())
3465 return -EINVAL;
3466
3467 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3468
3469 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3470 hs = &handshake[i];
3471 if (hs->pci_dev) {
3472 wait_for_completion(&hs->init);
3473 if (!hs->init_ok) {
3474 /* init handshake failed */
3475 dev_err(&hs->pci_dev->dev,
3476 "Failed to init device\n");
3477 liquidio_deinit_pci();
3478 return -EIO;
3479 }
3480 }
3481 }
3482
3483 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3484 hs = &handshake[i];
3485 if (hs->pci_dev) {
3486 wait_for_completion_timeout(&hs->started,
3487 msecs_to_jiffies(30000));
3488 if (!hs->started_ok) {
3489 /* starter handshake failed */
3490 dev_err(&hs->pci_dev->dev,
3491 "Firmware failed to start\n");
3492 liquidio_deinit_pci();
3493 return -EIO;
3494 }
3495 }
3496 }
3497
3498 return 0;
3499}
3500
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003501static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003502{
3503 struct octeon_device *oct = (struct octeon_device *)buf;
3504 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003505 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003506 union oct_link_status *ls;
3507 int i;
3508
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003509 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003510 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3511 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003512 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003513 goto nic_info_err;
3514 }
3515
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003516 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003517 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3518 OCT_DROQ_INFO_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003519
3520 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003521 for (i = 0; i < oct->ifcount; i++) {
3522 if (oct->props[i].gmxport == gmxport) {
3523 update_link_status(oct->props[i].netdev, ls);
3524 break;
3525 }
3526 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003527
3528nic_info_err:
3529 for (i = 0; i < recv_pkt->buffer_count; i++)
3530 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3531 octeon_free_recv_info(recv_info);
3532 return 0;
3533}
3534
3535/**
3536 * \brief Setup network interfaces
3537 * @param octeon_dev octeon device
3538 *
3539 * Called during init time for each device. It assumes the NIC
3540 * is already up and running. The link information for each
3541 * interface is passed in link_info.
3542 */
3543static int setup_nic_devices(struct octeon_device *octeon_dev)
3544{
3545 struct lio *lio = NULL;
3546 struct net_device *netdev;
3547 u8 mac[6], i, j;
3548 struct octeon_soft_command *sc;
3549 struct liquidio_if_cfg_context *ctx;
3550 struct liquidio_if_cfg_resp *resp;
3551 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003552 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003553 union oct_nic_if_cfg if_cfg;
3554 unsigned int base_queue;
3555 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003556 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003557 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003558 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003559
3560 /* This is to handle link status changes */
3561 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3562 OPCODE_NIC_INFO,
3563 lio_nic_info, octeon_dev);
3564
3565 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3566 * They are handled directly.
3567 */
3568 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3569 free_netbuf);
3570
3571 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3572 free_netsgbuf);
3573
3574 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3575 free_netsgbuf_with_resp);
3576
3577 for (i = 0; i < octeon_dev->ifcount; i++) {
3578 resp_size = sizeof(struct liquidio_if_cfg_resp);
3579 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003580 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003581 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003582 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003583 resp_size, ctx_size);
3584 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3585 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003586 vdata = (struct lio_version *)sc->virtdptr;
3587
3588 *((u64 *)vdata) = 0;
3589 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3590 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3591 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003592
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003593 if (OCTEON_CN23XX_PF(octeon_dev)) {
3594 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3595 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3596 base_queue = octeon_dev->sriov_info.pf_srn;
3597
3598 gmx_port_id = octeon_dev->pf_num;
3599 ifidx_or_pfnum = octeon_dev->pf_num;
3600 } else {
3601 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3602 octeon_get_conf(octeon_dev), i);
3603 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3604 octeon_get_conf(octeon_dev), i);
3605 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3606 octeon_get_conf(octeon_dev), i);
3607 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3608 octeon_get_conf(octeon_dev), i);
3609 ifidx_or_pfnum = i;
3610 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003611
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003612 dev_dbg(&octeon_dev->pci_dev->dev,
3613 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003614 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003615 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003616 ctx->octeon_id = lio_get_device_id(octeon_dev);
3617 init_waitqueue_head(&ctx->wc);
3618
3619 if_cfg.u64 = 0;
3620 if_cfg.s.num_iqueues = num_iqueues;
3621 if_cfg.s.num_oqueues = num_oqueues;
3622 if_cfg.s.base_queue = base_queue;
3623 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003624
3625 sc->iq_no = 0;
3626
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003627 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003628 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003629 if_cfg.u64, 0);
3630
3631 sc->callback = if_cfg_callback;
3632 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003633 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003634
3635 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003636 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003637 dev_err(&octeon_dev->pci_dev->dev,
3638 "iq/oq config failed status: %x\n",
3639 retval);
3640 /* Soft instr is freed by driver in case of failure. */
3641 goto setup_nic_dev_fail;
3642 }
3643
3644 /* Sleep on a wait queue till the cond flag indicates that the
3645 * response arrived or timed-out.
3646 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003647 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3648 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3649 goto setup_nic_wait_intr;
3650 }
3651
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003652 retval = resp->status;
3653 if (retval) {
3654 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3655 goto setup_nic_dev_fail;
3656 }
3657
3658 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3659 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3660
3661 num_iqueues = hweight64(resp->cfg_info.iqmask);
3662 num_oqueues = hweight64(resp->cfg_info.oqmask);
3663
3664 if (!(num_iqueues) || !(num_oqueues)) {
3665 dev_err(&octeon_dev->pci_dev->dev,
3666 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3667 resp->cfg_info.iqmask,
3668 resp->cfg_info.oqmask);
3669 goto setup_nic_dev_fail;
3670 }
3671 dev_dbg(&octeon_dev->pci_dev->dev,
3672 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3673 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3674 num_iqueues, num_oqueues);
3675 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3676
3677 if (!netdev) {
3678 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3679 goto setup_nic_dev_fail;
3680 }
3681
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003682 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003683
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003684 /* Associate the routines that will handle different
3685 * netdev tasks.
3686 */
3687 netdev->netdev_ops = &lionetdevops;
3688
3689 lio = GET_LIO(netdev);
3690
3691 memset(lio, 0, sizeof(struct lio));
3692
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003693 lio->ifidx = ifidx_or_pfnum;
3694
3695 props = &octeon_dev->props[i];
3696 props->gmxport = resp->cfg_info.linfo.gmxport;
3697 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003698
3699 lio->linfo.num_rxpciq = num_oqueues;
3700 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003701 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003702 lio->linfo.rxpciq[j].u64 =
3703 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003704 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003705 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003706 lio->linfo.txpciq[j].u64 =
3707 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003708 }
3709 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3710 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3711 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3712
3713 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3714
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003715 if (OCTEON_CN23XX_PF(octeon_dev) ||
3716 OCTEON_CN6XXX(octeon_dev)) {
3717 lio->dev_capability = NETIF_F_HIGHDMA
3718 | NETIF_F_IP_CSUM
3719 | NETIF_F_IPV6_CSUM
3720 | NETIF_F_SG | NETIF_F_RXCSUM
3721 | NETIF_F_GRO
3722 | NETIF_F_TSO | NETIF_F_TSO6
3723 | NETIF_F_LRO;
3724 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003725 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3726
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003727 /* Copy of transmit encapsulation capabilities:
3728 * TSO, TSO6, Checksums for this device
3729 */
3730 lio->enc_dev_capability = NETIF_F_IP_CSUM
3731 | NETIF_F_IPV6_CSUM
3732 | NETIF_F_GSO_UDP_TUNNEL
3733 | NETIF_F_HW_CSUM | NETIF_F_SG
3734 | NETIF_F_RXCSUM
3735 | NETIF_F_TSO | NETIF_F_TSO6
3736 | NETIF_F_LRO;
3737
3738 netdev->hw_enc_features = (lio->enc_dev_capability &
3739 ~NETIF_F_LRO);
3740
3741 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3742
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003743 netdev->vlan_features = lio->dev_capability;
3744 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003745 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3746 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003747 NETIF_F_HW_VLAN_CTAG_TX;
3748
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003749 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3750
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003751 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003752 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3753 netdev->hw_features = netdev->hw_features &
3754 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003755
Jarod Wilson109cc162016-10-17 15:54:13 -04003756 /* MTU range: 68 - 16000 */
3757 netdev->min_mtu = LIO_MIN_MTU_SIZE;
3758 netdev->max_mtu = LIO_MAX_MTU_SIZE;
3759
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003760 /* Point to the properties for octeon device to which this
3761 * interface belongs.
3762 */
3763 lio->oct_dev = octeon_dev;
3764 lio->octprops = props;
3765 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003766
3767 dev_dbg(&octeon_dev->pci_dev->dev,
3768 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3769 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3770
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003771 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
3772 u8 vfmac[ETH_ALEN];
3773
3774 random_ether_addr(&vfmac[0]);
3775 if (__liquidio_set_vf_mac(netdev, j,
3776 &vfmac[0], false)) {
3777 dev_err(&octeon_dev->pci_dev->dev,
3778 "Error setting VF%d MAC address\n",
3779 j);
3780 goto setup_nic_dev_fail;
3781 }
3782 }
3783
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003784 /* 64-bit swap required on LE machines */
3785 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3786 for (j = 0; j < 6; j++)
3787 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3788
3789 /* Copy MAC Address to OS network device structure */
3790
3791 ether_addr_copy(netdev->dev_addr, mac);
3792
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003793 /* By default all interfaces on a single Octeon uses the same
3794 * tx and rx queues
3795 */
3796 lio->txq = lio->linfo.txpciq[0].s.q_no;
3797 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Intiyaz Basha8974de12017-08-14 12:01:53 -07003798 if (liquidio_setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003799 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3800 goto setup_nic_dev_fail;
3801 }
3802
3803 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3804
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003805 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3806 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3807
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003808 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003809 dev_err(&octeon_dev->pci_dev->dev,
3810 "Gather list allocation failed\n");
3811 goto setup_nic_dev_fail;
3812 }
3813
3814 /* Register ethtool support */
3815 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003816 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
3817 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
3818 else
3819 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003820
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003821 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003822 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3823 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003824
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003825 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3826 OCTNET_CMD_VLAN_FILTER_ENABLE);
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003827
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003828 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003829 liquidio_set_feature(netdev,
3830 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003831
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07003832 if (setup_link_status_change_wq(netdev))
3833 goto setup_nic_dev_fail;
3834
Satanand Burla031d4f12017-03-22 11:31:13 -07003835 if (setup_rx_oom_poll_fn(netdev))
3836 goto setup_nic_dev_fail;
3837
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003838 /* Register the network device with the OS */
3839 if (register_netdev(netdev)) {
3840 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3841 goto setup_nic_dev_fail;
3842 }
3843
3844 dev_dbg(&octeon_dev->pci_dev->dev,
3845 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3846 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3847 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003848 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003849
3850 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3851
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003852 /* Sending command to firmware to enable Rx checksum offload
3853 * by default at the time of setup of Liquidio driver for
3854 * this device
3855 */
3856 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3857 OCTNET_CMD_RXCSUM_ENABLE);
3858 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3859 OCTNET_CMD_TXCSUM_ENABLE);
3860
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003861 dev_dbg(&octeon_dev->pci_dev->dev,
3862 "NIC ifidx:%d Setup successful\n", i);
3863
3864 octeon_free_soft_command(octeon_dev, sc);
3865 }
3866
3867 return 0;
3868
3869setup_nic_dev_fail:
3870
3871 octeon_free_soft_command(octeon_dev, sc);
3872
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003873setup_nic_wait_intr:
3874
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003875 while (i--) {
3876 dev_err(&octeon_dev->pci_dev->dev,
3877 "NIC ifidx:%d Setup failed\n", i);
3878 liquidio_destroy_nic_device(octeon_dev, i);
3879 }
3880 return -ENODEV;
3881}
3882
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08003883#ifdef CONFIG_PCI_IOV
3884static int octeon_enable_sriov(struct octeon_device *oct)
3885{
3886 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
3887 struct pci_dev *vfdev;
3888 int err;
3889 u32 u;
3890
3891 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
3892 err = pci_enable_sriov(oct->pci_dev,
3893 oct->sriov_info.num_vfs_alloced);
3894 if (err) {
3895 dev_err(&oct->pci_dev->dev,
3896 "OCTEON: Failed to enable PCI sriov: %d\n",
3897 err);
3898 oct->sriov_info.num_vfs_alloced = 0;
3899 return err;
3900 }
3901 oct->sriov_info.sriov_enabled = 1;
3902
3903 /* init lookup table that maps DPI ring number to VF pci_dev
3904 * struct pointer
3905 */
3906 u = 0;
3907 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3908 OCTEON_CN23XX_VF_VID, NULL);
3909 while (vfdev) {
3910 if (vfdev->is_virtfn &&
3911 (vfdev->physfn == oct->pci_dev)) {
3912 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
3913 vfdev;
3914 u += oct->sriov_info.rings_per_vf;
3915 }
3916 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3917 OCTEON_CN23XX_VF_VID, vfdev);
3918 }
3919 }
3920
3921 return num_vfs_alloced;
3922}
3923
3924static int lio_pci_sriov_disable(struct octeon_device *oct)
3925{
3926 int u;
3927
3928 if (pci_vfs_assigned(oct->pci_dev)) {
3929 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
3930 return -EPERM;
3931 }
3932
3933 pci_disable_sriov(oct->pci_dev);
3934
3935 u = 0;
3936 while (u < MAX_POSSIBLE_VFS) {
3937 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
3938 u += oct->sriov_info.rings_per_vf;
3939 }
3940
3941 oct->sriov_info.num_vfs_alloced = 0;
3942 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
3943 oct->pf_num);
3944
3945 return 0;
3946}
3947
3948static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
3949{
3950 struct octeon_device *oct = pci_get_drvdata(dev);
3951 int ret = 0;
3952
3953 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
3954 (oct->sriov_info.sriov_enabled)) {
3955 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
3956 oct->pf_num, num_vfs);
3957 return 0;
3958 }
3959
3960 if (!num_vfs) {
3961 ret = lio_pci_sriov_disable(oct);
3962 } else if (num_vfs > oct->sriov_info.max_vfs) {
3963 dev_err(&oct->pci_dev->dev,
3964 "OCTEON: Max allowed VFs:%d user requested:%d",
3965 oct->sriov_info.max_vfs, num_vfs);
3966 ret = -EPERM;
3967 } else {
3968 oct->sriov_info.num_vfs_alloced = num_vfs;
3969 ret = octeon_enable_sriov(oct);
3970 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
3971 oct->pf_num, num_vfs);
3972 }
3973
3974 return ret;
3975}
3976#endif
3977
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003978/**
3979 * \brief initialize the NIC
3980 * @param oct octeon device
3981 *
3982 * This initialization routine is called once the Octeon device application is
3983 * up and running
3984 */
3985static int liquidio_init_nic_module(struct octeon_device *oct)
3986{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003987 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003988 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3989
3990 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3991
3992 /* only default iq and oq were initialized
3993 * initialize the rest as well
3994 */
3995 /* run port_config command for each port */
3996 oct->ifcount = num_nic_ports;
3997
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003998 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003999
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004000 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4001 oct->props[i].gmxport = -1;
4002
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004003 retval = setup_nic_devices(oct);
4004 if (retval) {
4005 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4006 goto octnet_init_failure;
4007 }
4008
4009 liquidio_ptp_init(oct);
4010
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004011 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4012
4013 return retval;
4014
4015octnet_init_failure:
4016
4017 oct->ifcount = 0;
4018
4019 return retval;
4020}
4021
4022/**
4023 * \brief starter callback that invokes the remaining initialization work after
4024 * the NIC is up and running.
4025 * @param octptr work struct work_struct
4026 */
4027static void nic_starter(struct work_struct *work)
4028{
4029 struct octeon_device *oct;
4030 struct cavium_wk *wk = (struct cavium_wk *)work;
4031
4032 oct = (struct octeon_device *)wk->ctxptr;
4033
4034 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4035 return;
4036
4037 /* If the status of the device is CORE_OK, the core
4038 * application has reported its application type. Call
4039 * any registered handlers now and move to the RUNNING
4040 * state.
4041 */
4042 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4043 schedule_delayed_work(&oct->nic_poll_work.work,
4044 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4045 return;
4046 }
4047
4048 atomic_set(&oct->status, OCT_DEV_RUNNING);
4049
4050 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4051 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4052
4053 if (liquidio_init_nic_module(oct))
4054 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4055 else
4056 handshake[oct->octeon_id].started_ok = 1;
4057 } else {
4058 dev_err(&oct->pci_dev->dev,
4059 "Unexpected application running on NIC (%d). Check firmware.\n",
4060 oct->app_mode);
4061 }
4062
4063 complete(&handshake[oct->octeon_id].started);
4064}
4065
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004066static int
4067octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4068{
4069 struct octeon_device *oct = (struct octeon_device *)buf;
4070 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4071 int i, notice, vf_idx;
Felix Manlunasbb54be52017-04-04 19:26:57 -07004072 bool cores_crashed;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004073 u64 *data, vf_num;
4074
4075 notice = recv_pkt->rh.r.ossp;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07004076 data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004077
4078 /* the first 64-bit word of data is the vf_num */
4079 vf_num = data[0];
4080 octeon_swap_8B_data(&vf_num, 1);
4081 vf_idx = (int)vf_num - 1;
4082
Felix Manlunasbb54be52017-04-04 19:26:57 -07004083 cores_crashed = READ_ONCE(oct->cores_crashed);
4084
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004085 if (notice == VF_DRV_LOADED) {
4086 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4087 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4088 dev_info(&oct->pci_dev->dev,
4089 "driver for VF%d was loaded\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004090 if (!cores_crashed)
4091 try_module_get(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004092 }
4093 } else if (notice == VF_DRV_REMOVED) {
4094 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4095 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4096 dev_info(&oct->pci_dev->dev,
4097 "driver for VF%d was removed\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004098 if (!cores_crashed)
4099 module_put(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004100 }
4101 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4102 u8 *b = (u8 *)&data[1];
4103
4104 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4105 dev_info(&oct->pci_dev->dev,
4106 "VF driver changed VF%d's MAC address to %pM\n",
4107 vf_idx, b + 2);
4108 }
4109
4110 for (i = 0; i < recv_pkt->buffer_count; i++)
4111 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4112 octeon_free_recv_info(recv_info);
4113
4114 return 0;
4115}
4116
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004117/**
4118 * \brief Device initialization for each Octeon device that is probed
4119 * @param octeon_dev octeon device
4120 */
4121static int octeon_device_init(struct octeon_device *octeon_dev)
4122{
4123 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004124 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004125 char bootcmd[] = "\n";
Rick Farringtonda1542b2017-08-11 18:43:14 -07004126 char *dbg_enb = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004127 struct octeon_device_priv *oct_priv =
4128 (struct octeon_device_priv *)octeon_dev->priv;
4129 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4130
4131 /* Enable access to the octeon device and make its DMA capability
4132 * known to the OS.
4133 */
4134 if (octeon_pci_os_setup(octeon_dev))
4135 return 1;
4136
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004137 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4138
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004139 /* Identify the Octeon type and map the BAR address space. */
4140 if (octeon_chip_specific_setup(octeon_dev)) {
4141 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4142 return 1;
4143 }
4144
4145 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4146
Rick Farringtone1e3ce62017-05-16 11:14:50 -07004147 /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
4148 * since that is what is required for the reference to be removed
4149 * during de-initialization (see 'octeon_destroy_resources').
4150 */
4151 octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
4152 PCI_SLOT(octeon_dev->pci_dev->devfn),
4153 PCI_FUNC(octeon_dev->pci_dev->devfn),
4154 true);
4155
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004156 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4157
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004158 if (OCTEON_CN23XX_PF(octeon_dev)) {
4159 if (!cn23xx_fw_loaded(octeon_dev)) {
4160 fw_loaded = 0;
Felix Manlunas7cc61db2017-03-23 13:26:28 -07004161 if (!fw_type_is_none()) {
4162 /* Do a soft reset of the Octeon device. */
4163 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4164 return 1;
4165 /* things might have changed */
4166 if (!cn23xx_fw_loaded(octeon_dev))
4167 fw_loaded = 0;
4168 else
4169 fw_loaded = 1;
4170 }
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004171 } else {
4172 fw_loaded = 1;
4173 }
4174 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004175 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004176 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004177
4178 /* Initialize the dispatch mechanism used to push packets arriving on
4179 * Octeon Output queues.
4180 */
4181 if (octeon_init_dispatch_list(octeon_dev))
4182 return 1;
4183
4184 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4185 OPCODE_NIC_CORE_DRV_ACTIVE,
4186 octeon_core_drv_init,
4187 octeon_dev);
4188
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004189 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4190 OPCODE_NIC_VF_DRV_NOTICE,
4191 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004192 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4193 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4194 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4195 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4196
4197 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4198
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004199 if (octeon_set_io_queues_off(octeon_dev)) {
4200 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4201 return 1;
4202 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004203
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004204 if (OCTEON_CN23XX_PF(octeon_dev)) {
4205 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4206 if (ret) {
4207 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4208 return ret;
4209 }
4210 }
4211
4212 /* Initialize soft command buffer pool
4213 */
4214 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4215 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4216 return 1;
4217 }
4218 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4219
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004220 /* Setup the data structures that manage this Octeon's Input queues. */
4221 if (octeon_setup_instr_queues(octeon_dev)) {
4222 dev_err(&octeon_dev->pci_dev->dev,
4223 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004224 return 1;
4225 }
4226 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4227
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004228 /* Initialize lists to manage the requests of different types that
4229 * arrive from user & kernel applications for this octeon device.
4230 */
4231 if (octeon_setup_response_list(octeon_dev)) {
4232 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4233 return 1;
4234 }
4235 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4236
4237 if (octeon_setup_output_queues(octeon_dev)) {
4238 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004239 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004240 }
4241
4242 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4243
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004244 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004245 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4246 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4247 return 1;
4248 }
4249 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4250
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004251 if (octeon_allocate_ioq_vector(octeon_dev)) {
4252 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4253 return 1;
4254 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004255 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004256
4257 } else {
4258 /* The input and output queue registers were setup earlier (the
4259 * queues were not enabled). Any additional registers
4260 * that need to be programmed should be done now.
4261 */
4262 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4263 if (ret) {
4264 dev_err(&octeon_dev->pci_dev->dev,
4265 "Failed to configure device registers\n");
4266 return ret;
4267 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004268 }
4269
4270 /* Initialize the tasklet that handles output queue packet processing.*/
4271 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4272 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4273 (unsigned long)octeon_dev);
4274
4275 /* Setup the interrupt handler and record the INT SUM register address
4276 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004277 if (octeon_setup_interrupt(octeon_dev))
4278 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004279
4280 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004281 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004282
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004283 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4284
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004285 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004286 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4287 if (ret) {
4288 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4289 return ret;
4290 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004291
4292 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4293
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004294 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4295 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4296 if (!ddr_timeout) {
4297 dev_info(&octeon_dev->pci_dev->dev,
4298 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4299 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004300
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004301 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004302
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004303 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4304 while (!ddr_timeout) {
4305 set_current_state(TASK_INTERRUPTIBLE);
4306 if (schedule_timeout(HZ / 10)) {
4307 /* user probably pressed Control-C */
4308 return 1;
4309 }
4310 }
4311 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4312 if (ret) {
4313 dev_err(&octeon_dev->pci_dev->dev,
4314 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4315 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004316 return 1;
4317 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004318
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004319 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4320 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4321 return 1;
4322 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004323
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004324 /* Divert uboot to take commands from host instead. */
4325 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004326
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004327 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4328 ret = octeon_init_consoles(octeon_dev);
4329 if (ret) {
4330 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4331 return 1;
4332 }
Rick Farringtonda1542b2017-08-11 18:43:14 -07004333 /* If console debug enabled, specify empty string to use default
4334 * enablement ELSE specify NULL string for 'disabled'.
4335 */
4336 dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
4337 ret = octeon_add_console(octeon_dev, 0, dbg_enb);
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004338 if (ret) {
4339 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4340 return 1;
Rick Farringtonda1542b2017-08-11 18:43:14 -07004341 } else if (octeon_console_debug_enabled(0)) {
4342 /* If console was added AND we're logging console output
4343 * then set our console print function.
4344 */
4345 octeon_dev->console[0].print = octeon_dbg_console_print;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004346 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004347
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004348 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004349
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004350 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4351 ret = load_firmware(octeon_dev);
4352 if (ret) {
4353 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4354 return 1;
4355 }
4356 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4357 * loaded
4358 */
4359 if (OCTEON_CN23XX_PF(octeon_dev))
4360 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4361 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004362 }
4363
4364 handshake[octeon_dev->octeon_id].init_ok = 1;
4365 complete(&handshake[octeon_dev->octeon_id].init);
4366
4367 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4368
4369 /* Send Credit for Octeon Output queues. Credits are always sent after
4370 * the output queue is enabled.
4371 */
4372 for (j = 0; j < octeon_dev->num_oqs; j++)
4373 writel(octeon_dev->droq[j]->max_count,
4374 octeon_dev->droq[j]->pkts_credit_reg);
4375
4376 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004377 return 0;
4378}
4379
4380/**
Rick Farringtonda1542b2017-08-11 18:43:14 -07004381 * \brief Debug console print function
4382 * @param octeon_dev octeon device
4383 * @param console_num console number
4384 * @param prefix first portion of line to display
4385 * @param suffix second portion of line to display
4386 *
4387 * The OCTEON debug console outputs entire lines (excluding '\n').
4388 * Normally, the line will be passed in the 'prefix' parameter.
4389 * However, due to buffering, it is possible for a line to be split into two
4390 * parts, in which case they will be passed as the 'prefix' parameter and
4391 * 'suffix' parameter.
4392 */
4393static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
4394 char *prefix, char *suffix)
4395{
4396 if (prefix && suffix)
4397 dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
4398 suffix);
4399 else if (prefix)
4400 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
4401 else if (suffix)
4402 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
4403
4404 return 0;
4405}
4406
4407/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004408 * \brief Exits the module
4409 */
4410static void __exit liquidio_exit(void)
4411{
4412 liquidio_deinit_pci();
4413
4414 pr_info("LiquidIO network module is now unloaded\n");
4415}
4416
4417module_init(liquidio_init);
4418module_exit(liquidio_exit);