blob: f27f0afd0ecf1a7786ad396e3348e97bae7d879d [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>
Vijaya Mohan Guvva1f233f32017-10-31 16:04:53 -070024#include <net/switchdev.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070025#include "liquidio_common.h"
26#include "octeon_droq.h"
27#include "octeon_iq.h"
28#include "response_manager.h"
29#include "octeon_device.h"
30#include "octeon_nic.h"
31#include "octeon_main.h"
32#include "octeon_network.h"
33#include "cn66xx_regs.h"
34#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070035#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070036#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070037#include "liquidio_image.h"
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -070038#include "lio_vf_rep.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070039
40MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
41MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
42MODULE_LICENSE("GPL");
43MODULE_VERSION(LIQUIDIO_VERSION);
Derek Chicklesea6404c82017-08-07 12:22:15 -070044MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
45 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
47 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
48MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
49 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
50MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
51 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070052
53static int ddr_timeout = 10000;
54module_param(ddr_timeout, int, 0644);
55MODULE_PARM_DESC(ddr_timeout,
56 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
57
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070058#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
59
60static int debug = -1;
61module_param(debug, int, 0644);
62MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
63
Rick Farrington088b8742017-09-22 17:12:43 -070064static char fw_type[LIO_MAX_FW_TYPE_LEN] = LIO_FW_NAME_TYPE_AUTO;
Derek Chicklesd3961792017-08-14 12:17:56 -070065module_param_string(fw_type, fw_type, sizeof(fw_type), 0444);
Rick Farrington088b8742017-09-22 17:12:43 -070066MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded (default is \"auto\"), which uses firmware in flash, if present, else loads \"nic\".");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070067
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070068static u32 console_bitmask;
69module_param(console_bitmask, int, 0644);
70MODULE_PARM_DESC(console_bitmask,
71 "Bitmask indicating which consoles have debug output redirected to syslog.");
72
73/**
74 * \brief determines if a given console has debug enabled.
75 * @param console console to check
76 * @returns 1 = enabled. 0 otherwise
77 */
Rick Farringtonda1542b2017-08-11 18:43:14 -070078static int octeon_console_debug_enabled(u32 console)
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070079{
80 return (console_bitmask >> (console)) & 0x1;
81}
82
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070083/* Polling interval for determining when NIC application is alive */
84#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
85
86/* runtime link query interval */
87#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
Veerasenareddy Burru907aaa62017-10-23 20:33:25 -070088/* update localtime to octeon firmware every 60 seconds.
89 * make firmware to use same time reference, so that it will be easy to
90 * correlate firmware logged events/errors with host events, for debugging.
91 */
92#define LIO_SYNC_OCTEON_TIME_INTERVAL_MS 60000
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070093
94struct liquidio_if_cfg_context {
95 int octeon_id;
96
97 wait_queue_head_t wc;
98
99 int cond;
100};
101
102struct liquidio_if_cfg_resp {
103 u64 rh;
104 struct liquidio_if_cfg_info cfg_info;
105 u64 status;
106};
107
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -0700108struct liquidio_rx_ctl_context {
109 int octeon_id;
110
111 wait_queue_head_t wc;
112
113 int cond;
114};
115
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700116struct oct_link_status_resp {
117 u64 rh;
118 struct oct_link_info link_info;
119 u64 status;
120};
121
122struct oct_timestamp_resp {
123 u64 rh;
124 u64 timestamp;
125 u64 status;
126};
127
128#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
129
130union tx_info {
131 u64 u64;
132 struct {
133#ifdef __BIG_ENDIAN_BITFIELD
134 u16 gso_size;
135 u16 gso_segs;
136 u32 reserved;
137#else
138 u32 reserved;
139 u16 gso_segs;
140 u16 gso_size;
141#endif
142 } s;
143};
144
145/** Octeon device properties to be used by the NIC module.
146 * Each octeon device in the system will be represented
147 * by this structure in the NIC module.
148 */
149
150#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
151
152#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700153#define OCTNIC_GSO_MAX_SIZE \
154 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700155
156/** Structure of a node in list of gather components maintained by
157 * NIC driver for each network device.
158 */
159struct octnic_gather {
160 /** List manipulation. Next and prev pointers. */
161 struct list_head list;
162
163 /** Size of the gather component at sg in bytes. */
164 int sg_size;
165
166 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
167 int adjust;
168
169 /** Gather component that can accommodate max sized fragment list
170 * received from the IP layer.
171 */
172 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700173
VSR Burru67e303e2017-03-06 18:45:59 -0800174 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700175};
176
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700177struct handshake {
178 struct completion init;
179 struct completion started;
180 struct pci_dev *pci_dev;
181 int init_ok;
182 int started_ok;
183};
184
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800185#ifdef CONFIG_PCI_IOV
186static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
187#endif
188
Rick Farringtonda1542b2017-08-11 18:43:14 -0700189static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
190 char *prefix, char *suffix);
191
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700192static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700193static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700194static void liquidio_remove(struct pci_dev *pdev);
195static int liquidio_probe(struct pci_dev *pdev,
196 const struct pci_device_id *ent);
Felix Manlunasbb54be52017-04-04 19:26:57 -0700197static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
198 int linkstate);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700199
200static struct handshake handshake[MAX_OCTEON_DEVICES];
201static struct completion first_stage;
202
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700203static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700204{
205 int q_no;
206 int reschedule = 0;
207 struct octeon_device *oct = (struct octeon_device *)pdev;
208 struct octeon_device_priv *oct_priv =
209 (struct octeon_device_priv *)oct->priv;
210
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700211 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800212 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700213 continue;
214 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
215 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700216 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700217
218 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
219 /* set time and cnt interrupt thresholds for this DROQ
220 * for NAPI
221 */
222 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
223
224 octeon_write_csr64(
225 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
226 0x5700000040ULL);
227 octeon_write_csr64(
228 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
229 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700230 }
231
232 if (reschedule)
233 tasklet_schedule(&oct_priv->droq_tasklet);
234}
235
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700236static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700237{
238 struct octeon_device_priv *oct_priv =
239 (struct octeon_device_priv *)oct->priv;
240 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
241 int i;
242
243 do {
244 pending_pkts = 0;
245
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700246 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800247 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700248 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700249 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700250 }
251 if (pkt_cnt > 0) {
252 pending_pkts += pkt_cnt;
253 tasklet_schedule(&oct_priv->droq_tasklet);
254 }
255 pkt_cnt = 0;
256 schedule_timeout_uninterruptible(1);
257
258 } while (retry-- && pending_pkts);
259
260 return pkt_cnt;
261}
262
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700263/**
264 * \brief Forces all IO queues off on a given device
265 * @param oct Pointer to Octeon device
266 */
267static void force_io_queues_off(struct octeon_device *oct)
268{
269 if ((oct->chip_id == OCTEON_CN66XX) ||
270 (oct->chip_id == OCTEON_CN68XX)) {
271 /* Reset the Enable bits for Input Queues. */
272 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
273
274 /* Reset the Enable bits for Output Queues. */
275 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
276 }
277}
278
279/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700280 * \brief Cause device to go quiet so it can be safely removed/reset/etc
281 * @param oct Pointer to Octeon device
282 */
283static inline void pcierror_quiesce_device(struct octeon_device *oct)
284{
285 int i;
286
287 /* Disable the input and output queues now. No more packets will
288 * arrive from Octeon, but we should wait for all packet processing
289 * to finish.
290 */
291 force_io_queues_off(oct);
292
293 /* To allow for in-flight requests */
294 schedule_timeout_uninterruptible(100);
295
296 if (wait_for_pending_requests(oct))
297 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
298
299 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700300 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700301 struct octeon_instr_queue *iq;
302
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800303 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700304 continue;
305 iq = oct->instr_queue[i];
306
307 if (atomic_read(&iq->instr_pending)) {
308 spin_lock_bh(&iq->lock);
309 iq->fill_cnt = 0;
310 iq->octeon_read_index = iq->host_write_index;
311 iq->stats.instr_processed +=
312 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700313 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700314 spin_unlock_bh(&iq->lock);
315 }
316 }
317
318 /* Force all pending ordered list requests to time out. */
319 lio_process_ordered_list(oct, 1);
320
321 /* We do not need to wait for output queue packets to be processed. */
322}
323
324/**
325 * \brief Cleanup PCI AER uncorrectable error status
326 * @param dev Pointer to PCI device
327 */
328static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
329{
330 int pos = 0x100;
331 u32 status, mask;
332
333 pr_info("%s :\n", __func__);
334
335 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
336 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
337 if (dev->error_state == pci_channel_io_normal)
338 status &= ~mask; /* Clear corresponding nonfatal bits */
339 else
340 status &= mask; /* Clear corresponding fatal bits */
341 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
342}
343
344/**
345 * \brief Stop all PCI IO to a given device
346 * @param dev Pointer to Octeon device
347 */
348static void stop_pci_io(struct octeon_device *oct)
349{
350 /* No more instructions will be forwarded. */
351 atomic_set(&oct->status, OCT_DEV_IN_RESET);
352
353 pci_disable_device(oct->pci_dev);
354
355 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700356 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700357
358 pcierror_quiesce_device(oct);
359
360 /* Release the interrupt line */
361 free_irq(oct->pci_dev->irq, oct);
362
363 if (oct->flags & LIO_FLAG_MSI_ENABLED)
364 pci_disable_msi(oct->pci_dev);
365
366 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
367 lio_get_state_string(&oct->status));
368
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700369 /* making it a common function for all OCTEON models */
370 cleanup_aer_uncorrect_error_status(oct->pci_dev);
371}
372
373/**
374 * \brief called when PCI error is detected
375 * @param pdev Pointer to PCI device
376 * @param state The current pci connection state
377 *
378 * This function is called after a PCI bus error affecting
379 * this device has been detected.
380 */
381static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
382 pci_channel_state_t state)
383{
384 struct octeon_device *oct = pci_get_drvdata(pdev);
385
386 /* Non-correctable Non-fatal errors */
387 if (state == pci_channel_io_normal) {
388 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
389 cleanup_aer_uncorrect_error_status(oct->pci_dev);
390 return PCI_ERS_RESULT_CAN_RECOVER;
391 }
392
393 /* Non-correctable Fatal errors */
394 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
395 stop_pci_io(oct);
396
397 /* Always return a DISCONNECT. There is no support for recovery but only
398 * for a clean shutdown.
399 */
400 return PCI_ERS_RESULT_DISCONNECT;
401}
402
403/**
404 * \brief mmio handler
405 * @param pdev Pointer to PCI device
406 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700407static pci_ers_result_t liquidio_pcie_mmio_enabled(
408 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700409{
410 /* We should never hit this since we never ask for a reset for a Fatal
411 * Error. We always return DISCONNECT in io_error above.
412 * But play safe and return RECOVERED for now.
413 */
414 return PCI_ERS_RESULT_RECOVERED;
415}
416
417/**
418 * \brief called after the pci bus has been reset.
419 * @param pdev Pointer to PCI device
420 *
421 * Restart the card from scratch, as if from a cold-boot. Implementation
422 * resembles the first-half of the octeon_resume routine.
423 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700424static pci_ers_result_t liquidio_pcie_slot_reset(
425 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700426{
427 /* We should never hit this since we never ask for a reset for a Fatal
428 * Error. We always return DISCONNECT in io_error above.
429 * But play safe and return RECOVERED for now.
430 */
431 return PCI_ERS_RESULT_RECOVERED;
432}
433
434/**
435 * \brief called when traffic can start flowing again.
436 * @param pdev Pointer to PCI device
437 *
438 * This callback is called when the error recovery driver tells us that
439 * its OK to resume normal operation. Implementation resembles the
440 * second-half of the octeon_resume routine.
441 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700442static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700443{
444 /* Nothing to be done here. */
445}
446
447#ifdef CONFIG_PM
448/**
449 * \brief called when suspending
450 * @param pdev Pointer to PCI device
451 * @param state state to suspend to
452 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700453static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
454 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700455{
456 return 0;
457}
458
459/**
460 * \brief called when resuming
461 * @param pdev Pointer to PCI device
462 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700463static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700464{
465 return 0;
466}
467#endif
468
469/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100470static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700471 .error_detected = liquidio_pcie_error_detected,
472 .mmio_enabled = liquidio_pcie_mmio_enabled,
473 .slot_reset = liquidio_pcie_slot_reset,
474 .resume = liquidio_pcie_resume,
475};
476
477static const struct pci_device_id liquidio_pci_tbl[] = {
478 { /* 68xx */
479 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
480 },
481 { /* 66xx */
482 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
483 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700484 { /* 23xx pf */
485 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
486 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700487 {
488 0, 0, 0, 0, 0, 0, 0
489 }
490};
491MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
492
493static struct pci_driver liquidio_pci_driver = {
494 .name = "LiquidIO",
495 .id_table = liquidio_pci_tbl,
496 .probe = liquidio_probe,
497 .remove = liquidio_remove,
498 .err_handler = &liquidio_err_handler, /* For AER */
499
500#ifdef CONFIG_PM
501 .suspend = liquidio_suspend,
502 .resume = liquidio_resume,
503#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800504#ifdef CONFIG_PCI_IOV
505 .sriov_configure = liquidio_enable_sriov,
506#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700507};
508
509/**
510 * \brief register PCI driver
511 */
512static int liquidio_init_pci(void)
513{
514 return pci_register_driver(&liquidio_pci_driver);
515}
516
517/**
518 * \brief unregister PCI driver
519 */
520static void liquidio_deinit_pci(void)
521{
522 pci_unregister_driver(&liquidio_pci_driver);
523}
524
525/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700526 * \brief Stop Tx queues
527 * @param netdev network device
528 */
529static inline void txqs_stop(struct net_device *netdev)
530{
531 if (netif_is_multiqueue(netdev)) {
532 int i;
533
534 for (i = 0; i < netdev->num_tx_queues; i++)
535 netif_stop_subqueue(netdev, i);
536 } else {
537 netif_stop_queue(netdev);
538 }
539}
540
541/**
542 * \brief Start Tx queues
543 * @param netdev network device
544 */
545static inline void txqs_start(struct net_device *netdev)
546{
547 if (netif_is_multiqueue(netdev)) {
548 int i;
549
550 for (i = 0; i < netdev->num_tx_queues; i++)
551 netif_start_subqueue(netdev, i);
552 } else {
553 netif_start_queue(netdev);
554 }
555}
556
557/**
558 * \brief Wake Tx queues
559 * @param netdev network device
560 */
561static inline void txqs_wake(struct net_device *netdev)
562{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700563 struct lio *lio = GET_LIO(netdev);
564
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700565 if (netif_is_multiqueue(netdev)) {
566 int i;
567
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700568 for (i = 0; i < netdev->num_tx_queues; i++) {
569 int qno = lio->linfo.txpciq[i %
Intiyaz Bashaa82457f2017-08-15 12:46:18 -0700570 lio->oct_dev->num_iqs].s.q_no;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700571
572 if (__netif_subqueue_stopped(netdev, i)) {
573 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
574 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700575 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700576 }
577 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700578 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700579 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
580 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700581 netif_wake_queue(netdev);
582 }
583}
584
585/**
586 * \brief Stop Tx queue
587 * @param netdev network device
588 */
589static void stop_txq(struct net_device *netdev)
590{
591 txqs_stop(netdev);
592}
593
594/**
595 * \brief Start Tx queue
596 * @param netdev network device
597 */
598static void start_txq(struct net_device *netdev)
599{
600 struct lio *lio = GET_LIO(netdev);
601
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700602 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700603 txqs_start(netdev);
604 return;
605 }
606}
607
608/**
609 * \brief Wake a queue
610 * @param netdev network device
611 * @param q which queue to wake
612 */
613static inline void wake_q(struct net_device *netdev, int q)
614{
615 if (netif_is_multiqueue(netdev))
616 netif_wake_subqueue(netdev, q);
617 else
618 netif_wake_queue(netdev);
619}
620
621/**
622 * \brief Stop a queue
623 * @param netdev network device
624 * @param q which queue to stop
625 */
626static inline void stop_q(struct net_device *netdev, int q)
627{
628 if (netif_is_multiqueue(netdev))
629 netif_stop_subqueue(netdev, q);
630 else
631 netif_stop_queue(netdev);
632}
633
634/**
635 * \brief Check Tx queue status, and take appropriate action
636 * @param lio per-network private data
637 * @returns 0 if full, number of queues woken up otherwise
638 */
639static inline int check_txq_status(struct lio *lio)
640{
641 int ret_val = 0;
642
643 if (netif_is_multiqueue(lio->netdev)) {
644 int numqs = lio->netdev->num_tx_queues;
645 int q, iq = 0;
646
647 /* check each sub-queue state */
648 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700649 iq = lio->linfo.txpciq[q %
Intiyaz Bashaa82457f2017-08-15 12:46:18 -0700650 lio->oct_dev->num_iqs].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700651 if (octnet_iq_is_full(lio->oct_dev, iq))
652 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700653 if (__netif_subqueue_stopped(lio->netdev, q)) {
654 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700655 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
656 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700657 ret_val++;
658 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700659 }
660 } else {
661 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
662 return 0;
663 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700664 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
665 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700666 ret_val = 1;
667 }
668 return ret_val;
669}
670
671/**
672 * Remove the node at the head of the list. The list would be empty at
673 * the end of this call if there are no more nodes in the list.
674 */
675static inline struct list_head *list_delete_head(struct list_head *root)
676{
677 struct list_head *node;
678
679 if ((root->prev == root) && (root->next == root))
680 node = NULL;
681 else
682 node = root->next;
683
684 if (node)
685 list_del(node);
686
687 return node;
688}
689
690/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700691 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700692 * @param lio per-network private data
693 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700694static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700695{
696 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700697 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700698
VSR Burru67e303e2017-03-06 18:45:59 -0800699 kfree(lio->glist_lock);
700 lio->glist_lock = NULL;
701
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700702 if (!lio->glist)
703 return;
704
705 for (i = 0; i < lio->linfo.num_txpciq; i++) {
706 do {
707 g = (struct octnic_gather *)
708 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800709 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700710 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700711 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800712
Felix Manlunas58ad3192017-03-20 19:04:48 -0700713 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
714 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800715 lio_dma_free(lio->oct_dev,
716 lio->glist_entry_size * lio->tx_qsize,
717 lio->glists_virt_base[i],
718 lio->glists_dma_base[i]);
719 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700720 }
721
VSR Burru67e303e2017-03-06 18:45:59 -0800722 kfree(lio->glists_virt_base);
723 lio->glists_virt_base = NULL;
724
725 kfree(lio->glists_dma_base);
726 lio->glists_dma_base = NULL;
727
728 kfree(lio->glist);
729 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700730}
731
732/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700733 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700734 * @param lio per-network private data
735 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700736static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700737{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700738 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700739 struct octnic_gather *g;
740
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700741 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
742 GFP_KERNEL);
743 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800744 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700745
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700746 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
747 GFP_KERNEL);
748 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800749 kfree(lio->glist_lock);
750 lio->glist_lock = NULL;
751 return -ENOMEM;
752 }
753
754 lio->glist_entry_size =
755 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
756
757 /* allocate memory to store virtual and dma base address of
758 * per glist consistent memory
759 */
760 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
761 GFP_KERNEL);
762 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
763 GFP_KERNEL);
764
765 if (!lio->glists_virt_base || !lio->glists_dma_base) {
766 delete_glists(lio);
767 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700768 }
769
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700770 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800771 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700772
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700773 spin_lock_init(&lio->glist_lock[i]);
774
775 INIT_LIST_HEAD(&lio->glist[i]);
776
VSR Burru67e303e2017-03-06 18:45:59 -0800777 lio->glists_virt_base[i] =
778 lio_dma_alloc(oct,
779 lio->glist_entry_size * lio->tx_qsize,
780 &lio->glists_dma_base[i]);
781
782 if (!lio->glists_virt_base[i]) {
783 delete_glists(lio);
784 return -ENOMEM;
785 }
786
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700787 for (j = 0; j < lio->tx_qsize; j++) {
788 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
789 numa_node);
790 if (!g)
791 g = kzalloc(sizeof(*g), GFP_KERNEL);
792 if (!g)
793 break;
794
VSR Burru67e303e2017-03-06 18:45:59 -0800795 g->sg = lio->glists_virt_base[i] +
796 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700797
VSR Burru67e303e2017-03-06 18:45:59 -0800798 g->sg_dma_ptr = lio->glists_dma_base[i] +
799 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700800
801 list_add_tail(&g->list, &lio->glist[i]);
802 }
803
804 if (j != lio->tx_qsize) {
805 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800806 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700807 }
808 }
809
810 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700811}
812
813/**
814 * \brief Print link information
815 * @param netdev network device
816 */
817static void print_link_info(struct net_device *netdev)
818{
819 struct lio *lio = GET_LIO(netdev);
820
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -0700821 if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
822 ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700823 struct oct_link_info *linfo = &lio->linfo;
824
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700825 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700826 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
827 linfo->link.s.speed,
828 (linfo->link.s.duplex) ? "Full" : "Half");
829 } else {
830 netif_info(lio, link, lio->netdev, "Link Down\n");
831 }
832 }
833}
834
835/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700836 * \brief Routine to notify MTU change
837 * @param work work_struct data structure
838 */
839static void octnet_link_status_change(struct work_struct *work)
840{
841 struct cavium_wk *wk = (struct cavium_wk *)work;
842 struct lio *lio = (struct lio *)wk->ctxptr;
843
844 rtnl_lock();
845 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
846 rtnl_unlock();
847}
848
849/**
850 * \brief Sets up the mtu status change work
851 * @param netdev network device
852 */
853static inline int setup_link_status_change_wq(struct net_device *netdev)
854{
855 struct lio *lio = GET_LIO(netdev);
856 struct octeon_device *oct = lio->oct_dev;
857
858 lio->link_status_wq.wq = alloc_workqueue("link-status",
859 WQ_MEM_RECLAIM, 0);
860 if (!lio->link_status_wq.wq) {
861 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
862 return -1;
863 }
864 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
865 octnet_link_status_change);
866 lio->link_status_wq.wk.ctxptr = lio;
867
868 return 0;
869}
870
871static inline void cleanup_link_status_change_wq(struct net_device *netdev)
872{
873 struct lio *lio = GET_LIO(netdev);
874
875 if (lio->link_status_wq.wq) {
876 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
877 destroy_workqueue(lio->link_status_wq.wq);
878 }
879}
880
881/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700882 * \brief Update link status
883 * @param netdev network device
884 * @param ls link status structure
885 *
886 * Called on receipt of a link status response from the core application to
887 * update each interface's link status.
888 */
889static inline void update_link_status(struct net_device *netdev,
890 union oct_link_status *ls)
891{
892 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700893 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700894
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700895 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700896
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700897 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700898 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700899 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700900
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700901 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700902 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700903 txqs_wake(netdev);
904 } else {
905 netif_carrier_off(netdev);
906 stop_txq(netdev);
907 }
908 }
909}
910
Veerasenareddy Burru907aaa62017-10-23 20:33:25 -0700911/**
912 * lio_sync_octeon_time_cb - callback that is invoked when soft command
913 * sent by lio_sync_octeon_time() has completed successfully or failed
914 *
915 * @oct - octeon device structure
916 * @status - indicates success or failure
917 * @buf - pointer to the command that was sent to firmware
918 **/
919static void lio_sync_octeon_time_cb(struct octeon_device *oct,
920 u32 status, void *buf)
921{
922 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
923
924 if (status)
925 dev_err(&oct->pci_dev->dev,
926 "Failed to sync time to octeon; error=%d\n", status);
927
928 octeon_free_soft_command(oct, sc);
929}
930
931/**
932 * lio_sync_octeon_time - send latest localtime to octeon firmware so that
933 * firmware will correct it's time, in case there is a time skew
934 *
935 * @work: work scheduled to send time update to octeon firmware
936 **/
937static void lio_sync_octeon_time(struct work_struct *work)
938{
939 struct cavium_wk *wk = (struct cavium_wk *)work;
940 struct lio *lio = (struct lio *)wk->ctxptr;
941 struct octeon_device *oct = lio->oct_dev;
942 struct octeon_soft_command *sc;
943 struct timespec64 ts;
944 struct lio_time *lt;
945 int ret;
946
947 sc = octeon_alloc_soft_command(oct, sizeof(struct lio_time), 0, 0);
948 if (!sc) {
949 dev_err(&oct->pci_dev->dev,
950 "Failed to sync time to octeon: soft command allocation failed\n");
951 return;
952 }
953
954 lt = (struct lio_time *)sc->virtdptr;
955
956 /* Get time of the day */
957 getnstimeofday64(&ts);
958 lt->sec = ts.tv_sec;
959 lt->nsec = ts.tv_nsec;
960 octeon_swap_8B_data((u64 *)lt, (sizeof(struct lio_time)) / 8);
961
962 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
963 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
964 OPCODE_NIC_SYNC_OCTEON_TIME, 0, 0, 0);
965
966 sc->callback = lio_sync_octeon_time_cb;
967 sc->callback_arg = sc;
968 sc->wait_time = 1000;
969
970 ret = octeon_send_soft_command(oct, sc);
971 if (ret == IQ_SEND_FAILED) {
972 dev_err(&oct->pci_dev->dev,
973 "Failed to sync time to octeon: failed to send soft command\n");
974 octeon_free_soft_command(oct, sc);
975 }
976
977 queue_delayed_work(lio->sync_octeon_time_wq.wq,
978 &lio->sync_octeon_time_wq.wk.work,
979 msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
980}
981
982/**
983 * setup_sync_octeon_time_wq - Sets up the work to periodically update
984 * local time to octeon firmware
985 *
986 * @netdev - network device which should send time update to firmware
987 **/
988static inline int setup_sync_octeon_time_wq(struct net_device *netdev)
989{
990 struct lio *lio = GET_LIO(netdev);
991 struct octeon_device *oct = lio->oct_dev;
992
993 lio->sync_octeon_time_wq.wq =
994 alloc_workqueue("update-octeon-time", WQ_MEM_RECLAIM, 0);
995 if (!lio->sync_octeon_time_wq.wq) {
996 dev_err(&oct->pci_dev->dev, "Unable to create wq to update octeon time\n");
997 return -1;
998 }
999 INIT_DELAYED_WORK(&lio->sync_octeon_time_wq.wk.work,
1000 lio_sync_octeon_time);
1001 lio->sync_octeon_time_wq.wk.ctxptr = lio;
1002 queue_delayed_work(lio->sync_octeon_time_wq.wq,
1003 &lio->sync_octeon_time_wq.wk.work,
1004 msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
1005
1006 return 0;
1007}
1008
1009/**
1010 * cleanup_sync_octeon_time_wq - stop scheduling and destroy the work created
1011 * to periodically update local time to octeon firmware
1012 *
1013 * @netdev - network device which should send time update to firmware
1014 **/
1015static inline void cleanup_sync_octeon_time_wq(struct net_device *netdev)
1016{
1017 struct lio *lio = GET_LIO(netdev);
1018 struct cavium_wq *time_wq = &lio->sync_octeon_time_wq;
1019
1020 if (time_wq->wq) {
1021 cancel_delayed_work_sync(&time_wq->wk.work);
1022 destroy_workqueue(time_wq->wq);
1023 }
1024}
1025
Felix Manlunasbb54be52017-04-04 19:26:57 -07001026static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
1027{
1028 struct octeon_device *other_oct;
1029
1030 other_oct = lio_get_device(oct->octeon_id + 1);
1031
1032 if (other_oct && other_oct->pci_dev) {
1033 int oct_busnum, other_oct_busnum;
1034
1035 oct_busnum = oct->pci_dev->bus->number;
1036 other_oct_busnum = other_oct->pci_dev->bus->number;
1037
1038 if (oct_busnum == other_oct_busnum) {
1039 int oct_slot, other_oct_slot;
1040
1041 oct_slot = PCI_SLOT(oct->pci_dev->devfn);
1042 other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
1043
1044 if (oct_slot == other_oct_slot)
1045 return other_oct;
1046 }
1047 }
1048
1049 return NULL;
1050}
1051
1052static void disable_all_vf_links(struct octeon_device *oct)
1053{
1054 struct net_device *netdev;
1055 int max_vfs, vf, i;
1056
1057 if (!oct)
1058 return;
1059
1060 max_vfs = oct->sriov_info.max_vfs;
1061
1062 for (i = 0; i < oct->ifcount; i++) {
1063 netdev = oct->props[i].netdev;
1064 if (!netdev)
1065 continue;
1066
1067 for (vf = 0; vf < max_vfs; vf++)
1068 liquidio_set_vf_link_state(netdev, vf,
1069 IFLA_VF_LINK_STATE_DISABLE);
1070 }
1071}
1072
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001073static int liquidio_watchdog(void *param)
1074{
Felix Manlunasbb54be52017-04-04 19:26:57 -07001075 bool err_msg_was_printed[LIO_MAX_CORES];
1076 u16 mask_of_crashed_or_stuck_cores = 0;
1077 bool all_vf_links_are_disabled = false;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001078 struct octeon_device *oct = param;
Felix Manlunasbb54be52017-04-04 19:26:57 -07001079 struct octeon_device *other_oct;
1080#ifdef CONFIG_MODULE_UNLOAD
1081 long refcount, vfs_referencing_pf;
1082 u64 vfs_mask1, vfs_mask2;
1083#endif
1084 int core;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001085
Felix Manlunasbb54be52017-04-04 19:26:57 -07001086 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001087
1088 while (!kthread_should_stop()) {
Felix Manlunasbb54be52017-04-04 19:26:57 -07001089 /* sleep for a couple of seconds so that we don't hog the CPU */
1090 set_current_state(TASK_INTERRUPTIBLE);
1091 schedule_timeout(msecs_to_jiffies(2000));
1092
1093 mask_of_crashed_or_stuck_cores =
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001094 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1095
Felix Manlunasbb54be52017-04-04 19:26:57 -07001096 if (!mask_of_crashed_or_stuck_cores)
1097 continue;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001098
Felix Manlunasbb54be52017-04-04 19:26:57 -07001099 WRITE_ONCE(oct->cores_crashed, true);
1100 other_oct = get_other_octeon_device(oct);
1101 if (other_oct)
1102 WRITE_ONCE(other_oct->cores_crashed, true);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001103
Felix Manlunasbb54be52017-04-04 19:26:57 -07001104 for (core = 0; core < LIO_MAX_CORES; core++) {
1105 bool core_crashed_or_got_stuck;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001106
Felix Manlunasbb54be52017-04-04 19:26:57 -07001107 core_crashed_or_got_stuck =
1108 (mask_of_crashed_or_stuck_cores
1109 >> core) & 1;
1110
1111 if (core_crashed_or_got_stuck &&
1112 !err_msg_was_printed[core]) {
1113 dev_err(&oct->pci_dev->dev,
1114 "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
1115 core);
1116 err_msg_was_printed[core] = true;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001117 }
1118 }
1119
Felix Manlunasbb54be52017-04-04 19:26:57 -07001120 if (all_vf_links_are_disabled)
1121 continue;
1122
1123 disable_all_vf_links(oct);
1124 disable_all_vf_links(other_oct);
1125 all_vf_links_are_disabled = true;
1126
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001127#ifdef CONFIG_MODULE_UNLOAD
Felix Manlunasbb54be52017-04-04 19:26:57 -07001128 vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1129 vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001130
Felix Manlunasbb54be52017-04-04 19:26:57 -07001131 vfs_referencing_pf = hweight64(vfs_mask1);
1132 vfs_referencing_pf += hweight64(vfs_mask2);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001133
Felix Manlunasbb54be52017-04-04 19:26:57 -07001134 refcount = module_refcount(THIS_MODULE);
1135 if (refcount >= vfs_referencing_pf) {
1136 while (vfs_referencing_pf) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001137 module_put(THIS_MODULE);
Felix Manlunasbb54be52017-04-04 19:26:57 -07001138 vfs_referencing_pf--;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001139 }
1140 }
1141#endif
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001142 }
1143
1144 return 0;
1145}
1146
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001147/**
1148 * \brief PCI probe handler
1149 * @param pdev PCI device structure
1150 * @param ent unused
1151 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001152static int
1153liquidio_probe(struct pci_dev *pdev,
1154 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001155{
1156 struct octeon_device *oct_dev = NULL;
1157 struct handshake *hs;
1158
1159 oct_dev = octeon_allocate_device(pdev->device,
1160 sizeof(struct octeon_device_priv));
1161 if (!oct_dev) {
1162 dev_err(&pdev->dev, "Unable to allocate device\n");
1163 return -ENOMEM;
1164 }
1165
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001166 if (pdev->device == OCTEON_CN23XX_PF_VID)
1167 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1168
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07001169 /* Enable PTP for 6XXX Device */
1170 if (((pdev->device == OCTEON_CN66XX) ||
1171 (pdev->device == OCTEON_CN68XX)))
1172 oct_dev->ptp_enable = true;
1173 else
1174 oct_dev->ptp_enable = false;
1175
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001176 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1177 (u32)pdev->vendor, (u32)pdev->device);
1178
1179 /* Assign octeon_device for this device to the private data area. */
1180 pci_set_drvdata(pdev, oct_dev);
1181
1182 /* set linux specific device pointer */
1183 oct_dev->pci_dev = (void *)pdev;
1184
1185 hs = &handshake[oct_dev->octeon_id];
1186 init_completion(&hs->init);
1187 init_completion(&hs->started);
1188 hs->pci_dev = pdev;
1189
1190 if (oct_dev->octeon_id == 0)
1191 /* first LiquidIO NIC is detected */
1192 complete(&first_stage);
1193
1194 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001195 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001196 liquidio_remove(pdev);
1197 return -ENOMEM;
1198 }
1199
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001200 if (OCTEON_CN23XX_PF(oct_dev)) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001201 u8 bus, device, function;
1202
Felix Manlunas392209f2017-10-25 18:04:56 -07001203 if (atomic_read(oct_dev->adapter_refcount) == 1) {
1204 /* Each NIC gets one watchdog kernel thread. The first
1205 * PF (of each NIC) that gets pci_driver->probe()'d
1206 * creates that thread.
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001207 */
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001208 bus = pdev->bus->number;
1209 device = PCI_SLOT(pdev->devfn);
1210 function = PCI_FUNC(pdev->devfn);
1211 oct_dev->watchdog_task = kthread_create(
1212 liquidio_watchdog, oct_dev,
1213 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001214 if (!IS_ERR(oct_dev->watchdog_task)) {
1215 wake_up_process(oct_dev->watchdog_task);
1216 } else {
1217 oct_dev->watchdog_task = NULL;
1218 dev_err(&oct_dev->pci_dev->dev,
1219 "failed to create kernel_thread\n");
1220 liquidio_remove(pdev);
1221 return -1;
1222 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001223 }
1224 }
1225
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001226 oct_dev->rx_pause = 1;
1227 oct_dev->tx_pause = 1;
1228
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001229 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1230
1231 return 0;
1232}
1233
Rick Farrington088b8742017-09-22 17:12:43 -07001234static bool fw_type_is_auto(void)
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001235{
Rick Farrington088b8742017-09-22 17:12:43 -07001236 return strncmp(fw_type, LIO_FW_NAME_TYPE_AUTO,
1237 sizeof(LIO_FW_NAME_TYPE_AUTO)) == 0;
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001238}
1239
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001240/**
Rick Farrington70535352017-08-17 23:11:25 -07001241 * \brief PCI FLR for each Octeon device.
1242 * @param oct octeon device
1243 */
1244static void octeon_pci_flr(struct octeon_device *oct)
1245{
1246 int rc;
1247
1248 pci_save_state(oct->pci_dev);
1249
1250 pci_cfg_access_lock(oct->pci_dev);
1251
1252 /* Quiesce the device completely */
1253 pci_write_config_word(oct->pci_dev, PCI_COMMAND,
1254 PCI_COMMAND_INTX_DISABLE);
1255
1256 rc = __pci_reset_function_locked(oct->pci_dev);
1257
1258 if (rc != 0)
1259 dev_err(&oct->pci_dev->dev, "Error %d resetting PCI function %d\n",
1260 rc, oct->pf_num);
1261
1262 pci_cfg_access_unlock(oct->pci_dev);
1263
1264 pci_restore_state(oct->pci_dev);
1265}
1266
1267/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001268 *\brief Destroy resources associated with octeon device
1269 * @param pdev PCI device structure
1270 * @param ent unused
1271 */
1272static void octeon_destroy_resources(struct octeon_device *oct)
1273{
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001274 int i, refcount;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001275 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001276 struct octeon_device_priv *oct_priv =
1277 (struct octeon_device_priv *)oct->priv;
1278
1279 struct handshake *hs;
1280
1281 switch (atomic_read(&oct->status)) {
1282 case OCT_DEV_RUNNING:
1283 case OCT_DEV_CORE_OK:
1284
1285 /* No more instructions will be forwarded. */
1286 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1287
1288 oct->app_mode = CVM_DRV_INVALID_APP;
1289 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1290 lio_get_state_string(&oct->status));
1291
1292 schedule_timeout_uninterruptible(HZ / 10);
1293
1294 /* fallthrough */
1295 case OCT_DEV_HOST_OK:
1296
1297 /* fallthrough */
1298 case OCT_DEV_CONSOLE_INIT_DONE:
1299 /* Remove any consoles */
1300 octeon_remove_consoles(oct);
1301
1302 /* fallthrough */
1303 case OCT_DEV_IO_QUEUES_DONE:
1304 if (wait_for_pending_requests(oct))
1305 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1306
1307 if (lio_wait_for_instr_fetch(oct))
1308 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1309
1310 /* Disable the input and output queues now. No more packets will
1311 * arrive from Octeon, but we should wait for all packet
1312 * processing to finish.
1313 */
1314 oct->fn_list.disable_io_queues(oct);
1315
1316 if (lio_wait_for_oq_pkts(oct))
1317 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1318
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001319 /* fallthrough */
1320 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001321 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001322 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001323
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001324 if (oct->msix_on) {
1325 msix_entries = (struct msix_entry *)oct->msix_entries;
1326 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001327 if (oct->ioq_vector[i].vector) {
1328 /* clear the affinity_cpumask */
1329 irq_set_affinity_hint(
1330 msix_entries[i].vector,
1331 NULL);
1332 free_irq(msix_entries[i].vector,
1333 &oct->ioq_vector[i]);
1334 oct->ioq_vector[i].vector = 0;
1335 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001336 }
1337 /* non-iov vector's argument is oct struct */
1338 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001339
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001340 pci_disable_msix(oct->pci_dev);
1341 kfree(oct->msix_entries);
1342 oct->msix_entries = NULL;
1343 } else {
1344 /* Release the interrupt line */
1345 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001346
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001347 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1348 pci_disable_msi(oct->pci_dev);
1349 }
1350
Rick Farrington0c88a762017-03-13 12:58:04 -07001351 kfree(oct->irq_name_storage);
1352 oct->irq_name_storage = NULL;
1353
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001354 /* fallthrough */
1355 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001356 if (OCTEON_CN23XX_PF(oct))
1357 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001358
1359 /* fallthrough */
1360 case OCT_DEV_MBOX_SETUP_DONE:
1361 if (OCTEON_CN23XX_PF(oct))
1362 oct->fn_list.free_mbox(oct);
1363
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001364 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001365 case OCT_DEV_IN_RESET:
1366 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001367 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001368 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001369 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001370 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001371 continue;
1372 octeon_delete_droq(oct, i);
1373 }
1374
1375 /* Force any pending handshakes to complete */
1376 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1377 hs = &handshake[i];
1378
1379 if (hs->pci_dev) {
1380 handshake[oct->octeon_id].init_ok = 0;
1381 complete(&handshake[oct->octeon_id].init);
1382 handshake[oct->octeon_id].started_ok = 0;
1383 complete(&handshake[oct->octeon_id].started);
1384 }
1385 }
1386
1387 /* fallthrough */
1388 case OCT_DEV_RESP_LIST_INIT_DONE:
1389 octeon_delete_response_list(oct);
1390
1391 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001392 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001393 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001394 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001395 continue;
1396 octeon_delete_instr_queue(oct, i);
1397 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001398#ifdef CONFIG_PCI_IOV
1399 if (oct->sriov_info.sriov_enabled)
1400 pci_disable_sriov(oct->pci_dev);
1401#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001402 /* fallthrough */
1403 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1404 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001405
1406 /* fallthrough */
1407 case OCT_DEV_DISPATCH_INIT_DONE:
1408 octeon_delete_dispatch_list(oct);
1409 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1410
1411 /* fallthrough */
1412 case OCT_DEV_PCI_MAP_DONE:
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001413 refcount = octeon_deregister_device(oct);
1414
Rick Farrington70535352017-08-17 23:11:25 -07001415 /* Soft reset the octeon device before exiting.
1416 * However, if fw was loaded from card (i.e. autoboot),
1417 * perform an FLR instead.
1418 * Implementation note: only soft-reset the device
1419 * if it is a CN6XXX OR the LAST CN23XX device.
1420 */
Rick Farrington088b8742017-09-22 17:12:43 -07001421 if (atomic_read(oct->adapter_fw_state) == FW_IS_PRELOADED)
Rick Farrington70535352017-08-17 23:11:25 -07001422 octeon_pci_flr(oct);
1423 else if (OCTEON_CN6XXX(oct) || !refcount)
1424 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001425
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001426 octeon_unmap_pci_barx(oct, 0);
1427 octeon_unmap_pci_barx(oct, 1);
1428
1429 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001430 case OCT_DEV_PCI_ENABLE_DONE:
1431 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001432 /* Disable the device, releasing the PCI INT */
1433 pci_disable_device(oct->pci_dev);
1434
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001435 /* fallthrough */
1436 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001437 /* Nothing to be done here either */
1438 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001439 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001440
1441 tasklet_kill(&oct_priv->droq_tasklet);
1442}
1443
1444/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001445 * \brief Callback for rx ctrl
1446 * @param status status of request
1447 * @param buf pointer to resp structure
1448 */
1449static void rx_ctl_callback(struct octeon_device *oct,
1450 u32 status,
1451 void *buf)
1452{
1453 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1454 struct liquidio_rx_ctl_context *ctx;
1455
1456 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1457
1458 oct = lio_get_device(ctx->octeon_id);
1459 if (status)
1460 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1461 CVM_CAST64(status));
1462 WRITE_ONCE(ctx->cond, 1);
1463
1464 /* This barrier is required to be sure that the response has been
1465 * written fully before waking up the handler
1466 */
1467 wmb();
1468
1469 wake_up_interruptible(&ctx->wc);
1470}
1471
1472/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001473 * \brief Send Rx control command
1474 * @param lio per-network private data
1475 * @param start_stop whether to start or stop
1476 */
1477static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1478{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001479 struct octeon_soft_command *sc;
1480 struct liquidio_rx_ctl_context *ctx;
1481 union octnet_cmd *ncmd;
1482 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1483 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1484 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001485
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001486 if (oct->props[lio->ifidx].rx_on == start_stop)
1487 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001488
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001489 sc = (struct octeon_soft_command *)
1490 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1491 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001492
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001493 ncmd = (union octnet_cmd *)sc->virtdptr;
1494 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1495
1496 WRITE_ONCE(ctx->cond, 0);
1497 ctx->octeon_id = lio_get_device_id(oct);
1498 init_waitqueue_head(&ctx->wc);
1499
1500 ncmd->u64 = 0;
1501 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1502 ncmd->s.param1 = start_stop;
1503
1504 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1505
1506 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1507
1508 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1509 OPCODE_NIC_CMD, 0, 0, 0);
1510
1511 sc->callback = rx_ctl_callback;
1512 sc->callback_arg = sc;
1513 sc->wait_time = 5000;
1514
1515 retval = octeon_send_soft_command(oct, sc);
1516 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001517 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001518 } else {
1519 /* Sleep on a wait queue till the cond flag indicates that the
1520 * response arrived or timed-out.
1521 */
1522 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1523 return;
1524 oct->props[lio->ifidx].rx_on = start_stop;
1525 }
1526
1527 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001528}
1529
1530/**
1531 * \brief Destroy NIC device interface
1532 * @param oct octeon device
1533 * @param ifidx which interface to destroy
1534 *
1535 * Cleanup associated with each interface for an Octeon device when NIC
1536 * module is being unloaded or if initialization fails during load.
1537 */
1538static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1539{
1540 struct net_device *netdev = oct->props[ifidx].netdev;
1541 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001542 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001543
1544 if (!netdev) {
1545 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1546 __func__, ifidx);
1547 return;
1548 }
1549
1550 lio = GET_LIO(netdev);
1551
1552 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1553
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001554 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001555 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001556
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001557 if (oct->props[lio->ifidx].napi_enabled == 1) {
1558 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1559 napi_disable(napi);
1560
1561 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001562
1563 if (OCTEON_CN23XX_PF(oct))
1564 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001565 }
1566
Intiyaz Basha42013e92017-08-08 19:34:28 -07001567 /* Delete NAPI */
1568 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1569 netif_napi_del(napi);
1570
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001571 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1572 unregister_netdev(netdev);
1573
Veerasenareddy Burru907aaa62017-10-23 20:33:25 -07001574 cleanup_sync_octeon_time_wq(netdev);
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001575 cleanup_link_status_change_wq(netdev);
1576
Satanand Burla031d4f12017-03-22 11:31:13 -07001577 cleanup_rx_oom_poll_fn(netdev);
1578
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001579 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001580
1581 free_netdev(netdev);
1582
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001583 oct->props[ifidx].gmxport = -1;
1584
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001585 oct->props[ifidx].netdev = NULL;
1586}
1587
1588/**
1589 * \brief Stop complete NIC functionality
1590 * @param oct octeon device
1591 */
1592static int liquidio_stop_nic_module(struct octeon_device *oct)
1593{
1594 int i, j;
1595 struct lio *lio;
1596
1597 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1598 if (!oct->ifcount) {
1599 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1600 return 1;
1601 }
1602
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001603 spin_lock_bh(&oct->cmd_resp_wqlock);
1604 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1605 spin_unlock_bh(&oct->cmd_resp_wqlock);
1606
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07001607 lio_vf_rep_destroy(oct);
1608
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001609 for (i = 0; i < oct->ifcount; i++) {
1610 lio = GET_LIO(oct->props[i].netdev);
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001611 for (j = 0; j < oct->num_oqs; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001612 octeon_unregister_droq_ops(oct,
1613 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001614 }
1615
1616 for (i = 0; i < oct->ifcount; i++)
1617 liquidio_destroy_nic_device(oct, i);
1618
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07001619 if (oct->devlink) {
1620 devlink_unregister(oct->devlink);
1621 devlink_free(oct->devlink);
1622 oct->devlink = NULL;
1623 }
1624
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001625 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1626 return 0;
1627}
1628
1629/**
1630 * \brief Cleans up resources at unload time
1631 * @param pdev PCI device structure
1632 */
1633static void liquidio_remove(struct pci_dev *pdev)
1634{
1635 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1636
1637 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1638
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001639 if (oct_dev->watchdog_task)
1640 kthread_stop(oct_dev->watchdog_task);
1641
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001642 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1643 liquidio_stop_nic_module(oct_dev);
1644
1645 /* Reset the octeon device and cleanup all memory allocated for
1646 * the octeon device by driver.
1647 */
1648 octeon_destroy_resources(oct_dev);
1649
1650 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1651
1652 /* This octeon device has been removed. Update the global
1653 * data structure to reflect this. Free the device structure.
1654 */
1655 octeon_free_device_mem(oct_dev);
1656}
1657
1658/**
1659 * \brief Identify the Octeon device and to map the BAR address space
1660 * @param oct octeon device
1661 */
1662static int octeon_chip_specific_setup(struct octeon_device *oct)
1663{
1664 u32 dev_id, rev_id;
1665 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001666 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001667
1668 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1669 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1670 oct->rev_id = rev_id & 0xff;
1671
1672 switch (dev_id) {
1673 case OCTEON_CN68XX_PCIID:
1674 oct->chip_id = OCTEON_CN68XX;
1675 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001676 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001677 break;
1678
1679 case OCTEON_CN66XX_PCIID:
1680 oct->chip_id = OCTEON_CN66XX;
1681 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001682 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001683 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001684
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001685 case OCTEON_CN23XX_PCIID_PF:
1686 oct->chip_id = OCTEON_CN23XX_PF_VID;
1687 ret = setup_cn23xx_octeon_pf_device(oct);
Rick Farrington0c45d7f2017-08-18 18:21:49 -07001688 if (ret)
1689 break;
Derek Chicklescf19a8c2017-08-01 15:05:07 -07001690#ifdef CONFIG_PCI_IOV
1691 if (!ret)
1692 pci_sriov_set_totalvfs(oct->pci_dev,
1693 oct->sriov_info.max_vfs);
1694#endif
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001695 s = "CN23XX";
1696 break;
1697
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001698 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001699 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001700 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1701 dev_id);
1702 }
1703
1704 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001705 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001706 OCTEON_MAJOR_REV(oct),
1707 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001708 octeon_get_conf(oct)->card_name,
1709 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001710
1711 return ret;
1712}
1713
1714/**
1715 * \brief PCI initialization for each Octeon device.
1716 * @param oct octeon device
1717 */
1718static int octeon_pci_os_setup(struct octeon_device *oct)
1719{
1720 /* setup PCI stuff first */
1721 if (pci_enable_device(oct->pci_dev)) {
1722 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1723 return 1;
1724 }
1725
1726 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1727 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001728 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001729 return 1;
1730 }
1731
1732 /* Enable PCI DMA Master. */
1733 pci_set_master(oct->pci_dev);
1734
1735 return 0;
1736}
1737
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001738static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1739{
1740 int q = 0;
1741
1742 if (netif_is_multiqueue(lio->netdev))
1743 q = skb->queue_mapping % lio->linfo.num_txpciq;
1744
1745 return q;
1746}
1747
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001748/**
1749 * \brief Check Tx queue state for a given network buffer
1750 * @param lio per-network private data
1751 * @param skb network buffer
1752 */
1753static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1754{
1755 int q = 0, iq = 0;
1756
1757 if (netif_is_multiqueue(lio->netdev)) {
1758 q = skb->queue_mapping;
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001759 iq = lio->linfo.txpciq[(q % lio->oct_dev->num_iqs)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001760 } else {
1761 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001762 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001763 }
1764
1765 if (octnet_iq_is_full(lio->oct_dev, iq))
1766 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001767
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001768 if (__netif_subqueue_stopped(lio->netdev, q)) {
1769 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001770 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001771 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001772 return 1;
1773}
1774
1775/**
1776 * \brief Unmap and free network buffer
1777 * @param buf buffer
1778 */
1779static void free_netbuf(void *buf)
1780{
1781 struct sk_buff *skb;
1782 struct octnet_buf_free_info *finfo;
1783 struct lio *lio;
1784
1785 finfo = (struct octnet_buf_free_info *)buf;
1786 skb = finfo->skb;
1787 lio = finfo->lio;
1788
1789 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1790 DMA_TO_DEVICE);
1791
1792 check_txq_state(lio, skb);
1793
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001794 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001795}
1796
1797/**
1798 * \brief Unmap and free gather buffer
1799 * @param buf buffer
1800 */
1801static void free_netsgbuf(void *buf)
1802{
1803 struct octnet_buf_free_info *finfo;
1804 struct sk_buff *skb;
1805 struct lio *lio;
1806 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001807 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001808
1809 finfo = (struct octnet_buf_free_info *)buf;
1810 skb = finfo->skb;
1811 lio = finfo->lio;
1812 g = finfo->g;
1813 frags = skb_shinfo(skb)->nr_frags;
1814
1815 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1816 g->sg[0].ptr[0], (skb->len - skb->data_len),
1817 DMA_TO_DEVICE);
1818
1819 i = 1;
1820 while (frags--) {
1821 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1822
1823 pci_unmap_page((lio->oct_dev)->pci_dev,
1824 g->sg[(i >> 2)].ptr[(i & 3)],
1825 frag->size, DMA_TO_DEVICE);
1826 i++;
1827 }
1828
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001829 iq = skb_iq(lio, skb);
1830 spin_lock(&lio->glist_lock[iq]);
1831 list_add_tail(&g->list, &lio->glist[iq]);
1832 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001833
1834 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1835
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001836 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001837}
1838
1839/**
1840 * \brief Unmap and free gather buffer with response
1841 * @param buf buffer
1842 */
1843static void free_netsgbuf_with_resp(void *buf)
1844{
1845 struct octeon_soft_command *sc;
1846 struct octnet_buf_free_info *finfo;
1847 struct sk_buff *skb;
1848 struct lio *lio;
1849 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001850 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001851
1852 sc = (struct octeon_soft_command *)buf;
1853 skb = (struct sk_buff *)sc->callback_arg;
1854 finfo = (struct octnet_buf_free_info *)&skb->cb;
1855
1856 lio = finfo->lio;
1857 g = finfo->g;
1858 frags = skb_shinfo(skb)->nr_frags;
1859
1860 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1861 g->sg[0].ptr[0], (skb->len - skb->data_len),
1862 DMA_TO_DEVICE);
1863
1864 i = 1;
1865 while (frags--) {
1866 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1867
1868 pci_unmap_page((lio->oct_dev)->pci_dev,
1869 g->sg[(i >> 2)].ptr[(i & 3)],
1870 frag->size, DMA_TO_DEVICE);
1871 i++;
1872 }
1873
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001874 iq = skb_iq(lio, skb);
1875
1876 spin_lock(&lio->glist_lock[iq]);
1877 list_add_tail(&g->list, &lio->glist[iq]);
1878 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001879
1880 /* Don't free the skb yet */
1881
1882 check_txq_state(lio, skb);
1883}
1884
1885/**
1886 * \brief Adjust ptp frequency
1887 * @param ptp PTP clock info
1888 * @param ppb how much to adjust by, in parts-per-billion
1889 */
1890static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1891{
1892 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1893 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1894 u64 comp, delta;
1895 unsigned long flags;
1896 bool neg_adj = false;
1897
1898 if (ppb < 0) {
1899 neg_adj = true;
1900 ppb = -ppb;
1901 }
1902
1903 /* The hardware adds the clock compensation value to the
1904 * PTP clock on every coprocessor clock cycle, so we
1905 * compute the delta in terms of coprocessor clocks.
1906 */
1907 delta = (u64)ppb << 32;
1908 do_div(delta, oct->coproc_clock_rate);
1909
1910 spin_lock_irqsave(&lio->ptp_lock, flags);
1911 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1912 if (neg_adj)
1913 comp -= delta;
1914 else
1915 comp += delta;
1916 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1917 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1918
1919 return 0;
1920}
1921
1922/**
1923 * \brief Adjust ptp time
1924 * @param ptp PTP clock info
1925 * @param delta how much to adjust by, in nanosecs
1926 */
1927static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1928{
1929 unsigned long flags;
1930 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1931
1932 spin_lock_irqsave(&lio->ptp_lock, flags);
1933 lio->ptp_adjust += delta;
1934 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1935
1936 return 0;
1937}
1938
1939/**
1940 * \brief Get hardware clock time, including any adjustment
1941 * @param ptp PTP clock info
1942 * @param ts timespec
1943 */
1944static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1945 struct timespec64 *ts)
1946{
1947 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001948 unsigned long flags;
1949 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1950 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1951
1952 spin_lock_irqsave(&lio->ptp_lock, flags);
1953 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1954 ns += lio->ptp_adjust;
1955 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1956
Kefeng Wang286af312016-01-27 17:34:37 +08001957 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001958
1959 return 0;
1960}
1961
1962/**
1963 * \brief Set hardware clock time. Reset adjustment
1964 * @param ptp PTP clock info
1965 * @param ts timespec
1966 */
1967static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1968 const struct timespec64 *ts)
1969{
1970 u64 ns;
1971 unsigned long flags;
1972 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1973 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1974
Arnd Bergmanne7ad9792017-10-12 11:48:31 +02001975 ns = timespec64_to_ns(ts);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001976
1977 spin_lock_irqsave(&lio->ptp_lock, flags);
1978 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1979 lio->ptp_adjust = 0;
1980 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1981
1982 return 0;
1983}
1984
1985/**
1986 * \brief Check if PTP is enabled
1987 * @param ptp PTP clock info
1988 * @param rq request
1989 * @param on is it on
1990 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001991static int
1992liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
1993 struct ptp_clock_request *rq __attribute__((unused)),
1994 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001995{
1996 return -EOPNOTSUPP;
1997}
1998
1999/**
2000 * \brief Open PTP clock source
2001 * @param netdev network device
2002 */
2003static void oct_ptp_open(struct net_device *netdev)
2004{
2005 struct lio *lio = GET_LIO(netdev);
2006 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2007
2008 spin_lock_init(&lio->ptp_lock);
2009
2010 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2011 lio->ptp_info.owner = THIS_MODULE;
2012 lio->ptp_info.max_adj = 250000000;
2013 lio->ptp_info.n_alarm = 0;
2014 lio->ptp_info.n_ext_ts = 0;
2015 lio->ptp_info.n_per_out = 0;
2016 lio->ptp_info.pps = 0;
2017 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2018 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2019 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2020 lio->ptp_info.settime64 = liquidio_ptp_settime;
2021 lio->ptp_info.enable = liquidio_ptp_enable;
2022
2023 lio->ptp_adjust = 0;
2024
2025 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2026 &oct->pci_dev->dev);
2027
2028 if (IS_ERR(lio->ptp_clock))
2029 lio->ptp_clock = NULL;
2030}
2031
2032/**
2033 * \brief Init PTP clock
2034 * @param oct octeon device
2035 */
2036static void liquidio_ptp_init(struct octeon_device *oct)
2037{
2038 u64 clock_comp, cfg;
2039
2040 clock_comp = (u64)NSEC_PER_SEC << 32;
2041 do_div(clock_comp, oct->coproc_clock_rate);
2042 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2043
2044 /* Enable */
2045 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2046 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2047}
2048
2049/**
2050 * \brief Load firmware to device
2051 * @param oct octeon device
2052 *
2053 * Maps device to firmware filename, requests firmware, and downloads it
2054 */
2055static int load_firmware(struct octeon_device *oct)
2056{
2057 int ret = 0;
2058 const struct firmware *fw;
2059 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2060 char *tmp_fw_type;
2061
Rick Farrington429cbf62017-09-22 17:12:51 -07002062 if (fw_type_is_auto()) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002063 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
Rick Farrington429cbf62017-09-22 17:12:51 -07002064 strncpy(fw_type, tmp_fw_type, sizeof(fw_type));
2065 } else {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002066 tmp_fw_type = fw_type;
Rick Farrington429cbf62017-09-22 17:12:51 -07002067 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002068
2069 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2070 octeon_get_conf(oct)->card_name, tmp_fw_type,
2071 LIO_FW_NAME_SUFFIX);
2072
2073 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2074 if (ret) {
2075 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2076 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002077 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002078 return ret;
2079 }
2080
2081 ret = octeon_download_firmware(oct, fw->data, fw->size);
2082
2083 release_firmware(fw);
2084
2085 return ret;
2086}
2087
2088/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002089 * \brief Callback for getting interface configuration
2090 * @param status status of request
2091 * @param buf pointer to resp structure
2092 */
2093static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002094 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002095 void *buf)
2096{
2097 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2098 struct liquidio_if_cfg_resp *resp;
2099 struct liquidio_if_cfg_context *ctx;
2100
2101 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002102 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002103
2104 oct = lio_get_device(ctx->octeon_id);
2105 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002106 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2107 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002108 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002109
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002110 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2111 resp->cfg_info.liquidio_firmware_version);
2112
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002113 /* This barrier is required to be sure that the response has been
2114 * written fully before waking up the handler
2115 */
2116 wmb();
2117
2118 wake_up_interruptible(&ctx->wc);
2119}
2120
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002121/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002122 * \brief Poll routine for checking transmit queue status
2123 * @param work work_struct data structure
2124 */
2125static void octnet_poll_check_txq_status(struct work_struct *work)
2126{
2127 struct cavium_wk *wk = (struct cavium_wk *)work;
2128 struct lio *lio = (struct lio *)wk->ctxptr;
2129
2130 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2131 return;
2132
2133 check_txq_status(lio);
2134 queue_delayed_work(lio->txq_status_wq.wq,
2135 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2136}
2137
2138/**
2139 * \brief Sets up the txq poll check
2140 * @param netdev network device
2141 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002142static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002143{
2144 struct lio *lio = GET_LIO(netdev);
2145 struct octeon_device *oct = lio->oct_dev;
2146
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302147 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2148 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002149 if (!lio->txq_status_wq.wq) {
2150 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002151 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002152 }
2153 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2154 octnet_poll_check_txq_status);
2155 lio->txq_status_wq.wk.ctxptr = lio;
2156 queue_delayed_work(lio->txq_status_wq.wq,
2157 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002158 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002159}
2160
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002161static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2162{
2163 struct lio *lio = GET_LIO(netdev);
2164
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002165 if (lio->txq_status_wq.wq) {
2166 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2167 destroy_workqueue(lio->txq_status_wq.wq);
2168 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002169}
2170
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002171/**
2172 * \brief Net device open for LiquidIO
2173 * @param netdev network device
2174 */
2175static int liquidio_open(struct net_device *netdev)
2176{
2177 struct lio *lio = GET_LIO(netdev);
2178 struct octeon_device *oct = lio->oct_dev;
2179 struct napi_struct *napi, *n;
2180
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002181 if (oct->props[lio->ifidx].napi_enabled == 0) {
2182 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2183 napi_enable(napi);
2184
2185 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002186
2187 if (OCTEON_CN23XX_PF(oct))
2188 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002189 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002190
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002191 if (oct->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002192 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002193
2194 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002195
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002196 /* Ready for link status updates */
2197 lio->intf_open = 1;
2198
2199 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2200
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002201 if (OCTEON_CN23XX_PF(oct)) {
2202 if (!oct->msix_on)
2203 if (setup_tx_poll_fn(netdev))
2204 return -1;
2205 } else {
2206 if (setup_tx_poll_fn(netdev))
2207 return -1;
2208 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002209
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002210 start_txq(netdev);
2211
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002212 /* tell Octeon to start forwarding packets to host */
2213 send_rx_ctrl_cmd(lio, 1);
2214
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002215 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2216 netdev->name);
2217
2218 return 0;
2219}
2220
2221/**
2222 * \brief Net device stop for LiquidIO
2223 * @param netdev network device
2224 */
2225static int liquidio_stop(struct net_device *netdev)
2226{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002227 struct lio *lio = GET_LIO(netdev);
2228 struct octeon_device *oct = lio->oct_dev;
Intiyaz Basha42013e92017-08-08 19:34:28 -07002229 struct napi_struct *napi, *n;
2230
2231 if (oct->props[lio->ifidx].napi_enabled) {
2232 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2233 napi_disable(napi);
2234
2235 oct->props[lio->ifidx].napi_enabled = 0;
2236
2237 if (OCTEON_CN23XX_PF(oct))
2238 oct->droq[0]->ops.poll_mode = 0;
2239 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002240
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002241 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2242
2243 netif_tx_disable(netdev);
2244
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002245 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002246 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002247 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002248 lio->linfo.link.s.link_up = 0;
2249 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002250
Felix Manlunascb2336b2017-01-11 17:09:02 -08002251 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002252 send_rx_ctrl_cmd(lio, 0);
2253
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002254 if (OCTEON_CN23XX_PF(oct)) {
2255 if (!oct->msix_on)
2256 cleanup_tx_poll_fn(netdev);
2257 } else {
2258 cleanup_tx_poll_fn(netdev);
2259 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002260
2261 if (lio->ptp_clock) {
2262 ptp_clock_unregister(lio->ptp_clock);
2263 lio->ptp_clock = NULL;
2264 }
2265
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002266 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002267
2268 return 0;
2269}
2270
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002271/**
2272 * \brief Converts a mask based on net device flags
2273 * @param netdev network device
2274 *
2275 * This routine generates a octnet_ifflags mask from the net device flags
2276 * received from the OS.
2277 */
2278static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2279{
2280 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2281
2282 if (netdev->flags & IFF_PROMISC)
2283 f |= OCTNET_IFFLAG_PROMISC;
2284
2285 if (netdev->flags & IFF_ALLMULTI)
2286 f |= OCTNET_IFFLAG_ALLMULTI;
2287
2288 if (netdev->flags & IFF_MULTICAST) {
2289 f |= OCTNET_IFFLAG_MULTICAST;
2290
2291 /* Accept all multicast addresses if there are more than we
2292 * can handle
2293 */
2294 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2295 f |= OCTNET_IFFLAG_ALLMULTI;
2296 }
2297
2298 if (netdev->flags & IFF_BROADCAST)
2299 f |= OCTNET_IFFLAG_BROADCAST;
2300
2301 return f;
2302}
2303
2304/**
2305 * \brief Net device set_multicast_list
2306 * @param netdev network device
2307 */
2308static void liquidio_set_mcast_list(struct net_device *netdev)
2309{
2310 struct lio *lio = GET_LIO(netdev);
2311 struct octeon_device *oct = lio->oct_dev;
2312 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002313 struct netdev_hw_addr *ha;
2314 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002315 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002316 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2317
2318 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2319
2320 /* Create a ctrl pkt command to be sent to core app. */
2321 nctrl.ncmd.u64 = 0;
2322 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002323 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2324 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002325 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002326 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002327 nctrl.netpndev = (u64)netdev;
2328 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2329
2330 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002331 mc = &nctrl.udd[0];
2332 netdev_for_each_mc_addr(ha, netdev) {
2333 *mc = 0;
2334 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2335 /* no need to swap bytes */
2336
2337 if (++mc > &nctrl.udd[mc_count])
2338 break;
2339 }
2340
2341 /* Apparently, any activity in this call from the kernel has to
2342 * be atomic. So we won't wait for response.
2343 */
2344 nctrl.wait_time = 0;
2345
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002346 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002347 if (ret < 0) {
2348 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2349 ret);
2350 }
2351}
2352
2353/**
2354 * \brief Net device set_mac_address
2355 * @param netdev network device
2356 */
2357static int liquidio_set_mac(struct net_device *netdev, void *p)
2358{
2359 int ret = 0;
2360 struct lio *lio = GET_LIO(netdev);
2361 struct octeon_device *oct = lio->oct_dev;
2362 struct sockaddr *addr = (struct sockaddr *)p;
2363 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002364
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002365 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002366 return -EADDRNOTAVAIL;
2367
2368 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2369
2370 nctrl.ncmd.u64 = 0;
2371 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002372 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002373 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002374 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002375 nctrl.netpndev = (u64)netdev;
2376 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2377 nctrl.wait_time = 100;
2378
2379 nctrl.udd[0] = 0;
2380 /* The MAC Address is presented in network byte order. */
2381 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2382
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002383 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002384 if (ret < 0) {
2385 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2386 return -ENOMEM;
2387 }
2388 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2389 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2390
2391 return 0;
2392}
2393
2394/**
2395 * \brief Net device get_stats
2396 * @param netdev network device
2397 */
2398static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2399{
2400 struct lio *lio = GET_LIO(netdev);
2401 struct net_device_stats *stats = &netdev->stats;
2402 struct octeon_device *oct;
2403 u64 pkts = 0, drop = 0, bytes = 0;
2404 struct oct_droq_stats *oq_stats;
2405 struct oct_iq_stats *iq_stats;
2406 int i, iq_no, oq_no;
2407
2408 oct = lio->oct_dev;
2409
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -07002410 if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
2411 return stats;
2412
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07002413 for (i = 0; i < oct->num_iqs; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002414 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002415 iq_stats = &oct->instr_queue[iq_no]->stats;
2416 pkts += iq_stats->tx_done;
2417 drop += iq_stats->tx_dropped;
2418 bytes += iq_stats->tx_tot_bytes;
2419 }
2420
2421 stats->tx_packets = pkts;
2422 stats->tx_bytes = bytes;
2423 stats->tx_dropped = drop;
2424
2425 pkts = 0;
2426 drop = 0;
2427 bytes = 0;
2428
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07002429 for (i = 0; i < oct->num_oqs; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002430 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002431 oq_stats = &oct->droq[oq_no]->stats;
2432 pkts += oq_stats->rx_pkts_received;
2433 drop += (oq_stats->rx_dropped +
2434 oq_stats->dropped_nodispatch +
2435 oq_stats->dropped_toomany +
2436 oq_stats->dropped_nomem);
2437 bytes += oq_stats->rx_bytes_received;
2438 }
2439
2440 stats->rx_bytes = bytes;
2441 stats->rx_packets = pkts;
2442 stats->rx_dropped = drop;
2443
2444 return stats;
2445}
2446
2447/**
2448 * \brief Net device change_mtu
2449 * @param netdev network device
2450 */
2451static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2452{
2453 struct lio *lio = GET_LIO(netdev);
2454 struct octeon_device *oct = lio->oct_dev;
2455 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002456 int ret = 0;
2457
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002458 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2459
2460 nctrl.ncmd.u64 = 0;
2461 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002462 nctrl.ncmd.s.param1 = new_mtu;
2463 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002464 nctrl.wait_time = 100;
2465 nctrl.netpndev = (u64)netdev;
2466 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2467
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002468 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002469 if (ret < 0) {
2470 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2471 return -1;
2472 }
2473
2474 lio->mtu = new_mtu;
2475
2476 return 0;
2477}
2478
2479/**
2480 * \brief Handler for SIOCSHWTSTAMP ioctl
2481 * @param netdev network device
2482 * @param ifr interface request
2483 * @param cmd command
2484 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002485static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002486{
2487 struct hwtstamp_config conf;
2488 struct lio *lio = GET_LIO(netdev);
2489
2490 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2491 return -EFAULT;
2492
2493 if (conf.flags)
2494 return -EINVAL;
2495
2496 switch (conf.tx_type) {
2497 case HWTSTAMP_TX_ON:
2498 case HWTSTAMP_TX_OFF:
2499 break;
2500 default:
2501 return -ERANGE;
2502 }
2503
2504 switch (conf.rx_filter) {
2505 case HWTSTAMP_FILTER_NONE:
2506 break;
2507 case HWTSTAMP_FILTER_ALL:
2508 case HWTSTAMP_FILTER_SOME:
2509 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2510 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2511 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2512 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2513 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2514 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2515 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2516 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2517 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2518 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2519 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2520 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvare3412572017-05-19 17:52:36 +02002521 case HWTSTAMP_FILTER_NTP_ALL:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002522 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2523 break;
2524 default:
2525 return -ERANGE;
2526 }
2527
2528 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2529 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2530
2531 else
2532 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2533
2534 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2535}
2536
2537/**
2538 * \brief ioctl handler
2539 * @param netdev network device
2540 * @param ifr interface request
2541 * @param cmd command
2542 */
2543static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2544{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002545 struct lio *lio = GET_LIO(netdev);
2546
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002547 switch (cmd) {
2548 case SIOCSHWTSTAMP:
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002549 if (lio->oct_dev->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002550 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002551 default:
2552 return -EOPNOTSUPP;
2553 }
2554}
2555
2556/**
2557 * \brief handle a Tx timestamp response
2558 * @param status response status
2559 * @param buf pointer to skb
2560 */
2561static void handle_timestamp(struct octeon_device *oct,
2562 u32 status,
2563 void *buf)
2564{
2565 struct octnet_buf_free_info *finfo;
2566 struct octeon_soft_command *sc;
2567 struct oct_timestamp_resp *resp;
2568 struct lio *lio;
2569 struct sk_buff *skb = (struct sk_buff *)buf;
2570
2571 finfo = (struct octnet_buf_free_info *)skb->cb;
2572 lio = finfo->lio;
2573 sc = finfo->sc;
2574 oct = lio->oct_dev;
2575 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2576
2577 if (status != OCTEON_REQUEST_DONE) {
2578 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2579 CVM_CAST64(status));
2580 resp->timestamp = 0;
2581 }
2582
2583 octeon_swap_8B_data(&resp->timestamp, 1);
2584
Colin Ian King19a6d152016-02-05 16:30:39 +00002585 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002586 struct skb_shared_hwtstamps ts;
2587 u64 ns = resp->timestamp;
2588
2589 netif_info(lio, tx_done, lio->netdev,
2590 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2591 skb, (unsigned long long)ns);
2592 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2593 skb_tstamp_tx(skb, &ts);
2594 }
2595
2596 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002597 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002598}
2599
2600/* \brief Send a data packet that will be timestamped
2601 * @param oct octeon device
2602 * @param ndata pointer to network data
2603 * @param finfo pointer to private network data
2604 */
2605static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2606 struct octnic_data_pkt *ndata,
Intiyaz Bashac859e212017-10-26 16:18:20 -07002607 struct octnet_buf_free_info *finfo,
2608 int xmit_more)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002609{
2610 int retval;
2611 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002612 struct lio *lio;
2613 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002614 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002615
2616 lio = finfo->lio;
2617
2618 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2619 sizeof(struct oct_timestamp_resp));
2620 finfo->sc = sc;
2621
2622 if (!sc) {
2623 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2624 return IQ_SEND_FAILED;
2625 }
2626
2627 if (ndata->reqtype == REQTYPE_NORESP_NET)
2628 ndata->reqtype = REQTYPE_RESP_NET;
2629 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2630 ndata->reqtype = REQTYPE_RESP_NET_SG;
2631
2632 sc->callback = handle_timestamp;
2633 sc->callback_arg = finfo->skb;
2634 sc->iq_no = ndata->q_no;
2635
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002636 if (OCTEON_CN23XX_PF(oct))
2637 len = (u32)((struct octeon_instr_ih3 *)
2638 (&sc->cmd.cmd3.ih3))->dlengsz;
2639 else
2640 len = (u32)((struct octeon_instr_ih2 *)
2641 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002642
Intiyaz Bashac859e212017-10-26 16:18:20 -07002643 ring_doorbell = !xmit_more;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002644
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002645 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002646 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002647
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002648 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002649 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2650 retval);
2651 octeon_free_soft_command(oct, sc);
2652 } else {
2653 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2654 }
2655
2656 return retval;
2657}
2658
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002659/** \brief Transmit networks packets to the Octeon interface
2660 * @param skbuff skbuff struct to be passed to network layer.
2661 * @param netdev pointer to network device
2662 * @returns whether the packet was transmitted to the device okay or not
2663 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2664 */
2665static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2666{
2667 struct lio *lio;
2668 struct octnet_buf_free_info *finfo;
2669 union octnic_cmd_setup cmdsetup;
2670 struct octnic_data_pkt ndata;
2671 struct octeon_device *oct;
2672 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002673 struct octeon_instr_irh *irh;
2674 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002675 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002676 int q_idx = 0, iq_no = 0;
Intiyaz Bashac859e212017-10-26 16:18:20 -07002677 int j, xmit_more = 0;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002678 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002679 u32 tag = 0;
2680
2681 lio = GET_LIO(netdev);
2682 oct = lio->oct_dev;
2683
2684 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002685 q_idx = skb->queue_mapping;
2686 q_idx = (q_idx % (lio->linfo.num_txpciq));
2687 tag = q_idx;
2688 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002689 } else {
2690 iq_no = lio->txq;
2691 }
2692
2693 stats = &oct->instr_queue[iq_no]->stats;
2694
2695 /* Check for all conditions in which the current packet cannot be
2696 * transmitted.
2697 */
2698 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002699 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002700 (skb->len <= 0)) {
2701 netif_info(lio, tx_err, lio->netdev,
2702 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002703 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002704 goto lio_xmit_failed;
2705 }
2706
2707 /* Use space in skb->cb to store info used to unmap and
2708 * free the buffers.
2709 */
2710 finfo = (struct octnet_buf_free_info *)skb->cb;
2711 finfo->lio = lio;
2712 finfo->skb = skb;
2713 finfo->sc = NULL;
2714
2715 /* Prepare the attributes for the data to be passed to OSI. */
2716 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2717
2718 ndata.buf = (void *)finfo;
2719
2720 ndata.q_no = iq_no;
2721
2722 if (netif_is_multiqueue(netdev)) {
2723 if (octnet_iq_is_full(oct, ndata.q_no)) {
2724 /* defer sending if queue is full */
2725 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2726 ndata.q_no);
2727 stats->tx_iq_busy++;
2728 return NETDEV_TX_BUSY;
2729 }
2730 } else {
2731 if (octnet_iq_is_full(oct, lio->txq)) {
2732 /* defer sending if queue is full */
2733 stats->tx_iq_busy++;
2734 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002735 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002736 return NETDEV_TX_BUSY;
2737 }
2738 }
2739 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002740 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002741 */
2742
2743 ndata.datasize = skb->len;
2744
2745 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002746 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002747
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002748 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2749 if (skb->encapsulation) {
2750 cmdsetup.s.tnl_csum = 1;
2751 stats->tx_vxlan++;
2752 } else {
2753 cmdsetup.s.transport_csum = 1;
2754 }
2755 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002756 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2757 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2758 cmdsetup.s.timestamp = 1;
2759 }
2760
2761 if (skb_shinfo(skb)->nr_frags == 0) {
2762 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002763 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002764
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002765 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002766 dptr = dma_map_single(&oct->pci_dev->dev,
2767 skb->data,
2768 skb->len,
2769 DMA_TO_DEVICE);
2770 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002771 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2772 __func__);
2773 return NETDEV_TX_BUSY;
2774 }
2775
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002776 if (OCTEON_CN23XX_PF(oct))
2777 ndata.cmd.cmd3.dptr = dptr;
2778 else
2779 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002780 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002781 ndata.reqtype = REQTYPE_NORESP_NET;
2782
2783 } else {
2784 int i, frags;
2785 struct skb_frag_struct *frag;
2786 struct octnic_gather *g;
2787
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002788 spin_lock(&lio->glist_lock[q_idx]);
2789 g = (struct octnic_gather *)
2790 list_delete_head(&lio->glist[q_idx]);
2791 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002792
2793 if (!g) {
2794 netif_info(lio, tx_err, lio->netdev,
2795 "Transmit scatter gather: glist null!\n");
2796 goto lio_xmit_failed;
2797 }
2798
2799 cmdsetup.s.gather = 1;
2800 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002801 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802
2803 memset(g->sg, 0, g->sg_size);
2804
2805 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2806 skb->data,
2807 (skb->len - skb->data_len),
2808 DMA_TO_DEVICE);
2809 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2810 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2811 __func__);
2812 return NETDEV_TX_BUSY;
2813 }
2814 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2815
2816 frags = skb_shinfo(skb)->nr_frags;
2817 i = 1;
2818 while (frags--) {
2819 frag = &skb_shinfo(skb)->frags[i - 1];
2820
2821 g->sg[(i >> 2)].ptr[(i & 3)] =
2822 dma_map_page(&oct->pci_dev->dev,
2823 frag->page.p,
2824 frag->page_offset,
2825 frag->size,
2826 DMA_TO_DEVICE);
2827
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002828 if (dma_mapping_error(&oct->pci_dev->dev,
2829 g->sg[i >> 2].ptr[i & 3])) {
2830 dma_unmap_single(&oct->pci_dev->dev,
2831 g->sg[0].ptr[0],
2832 skb->len - skb->data_len,
2833 DMA_TO_DEVICE);
2834 for (j = 1; j < i; j++) {
2835 frag = &skb_shinfo(skb)->frags[j - 1];
2836 dma_unmap_page(&oct->pci_dev->dev,
2837 g->sg[j >> 2].ptr[j & 3],
2838 frag->size,
2839 DMA_TO_DEVICE);
2840 }
2841 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2842 __func__);
2843 return NETDEV_TX_BUSY;
2844 }
2845
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002846 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2847 i++;
2848 }
2849
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002850 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002851
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002852 if (OCTEON_CN23XX_PF(oct))
2853 ndata.cmd.cmd3.dptr = dptr;
2854 else
2855 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002856 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002857 finfo->g = g;
2858
2859 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2860 }
2861
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002862 if (OCTEON_CN23XX_PF(oct)) {
2863 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
2864 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
2865 } else {
2866 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2867 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
2868 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002869
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002870 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002871 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2872 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002873 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002874 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002875
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002876 /* HW insert VLAN tag */
2877 if (skb_vlan_tag_present(skb)) {
2878 irh->priority = skb_vlan_tag_get(skb) >> 13;
2879 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2880 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002881
Intiyaz Bashac859e212017-10-26 16:18:20 -07002882 xmit_more = skb->xmit_more;
2883
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002884 if (unlikely(cmdsetup.s.timestamp))
Intiyaz Bashac859e212017-10-26 16:18:20 -07002885 status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002886 else
Intiyaz Bashac859e212017-10-26 16:18:20 -07002887 status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002888 if (status == IQ_SEND_FAILED)
2889 goto lio_xmit_failed;
2890
2891 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2892
2893 if (status == IQ_SEND_STOP)
Intiyaz Bashac859e212017-10-26 16:18:20 -07002894 stop_q(netdev, q_idx);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002895
Florian Westphal860e9532016-05-03 16:33:13 +02002896 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002897
Satanand Burla80c8eae2017-01-26 11:52:35 -08002898 if (tx_info->s.gso_segs)
2899 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002900 else
2901 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08002902 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002903
2904 return NETDEV_TX_OK;
2905
2906lio_xmit_failed:
2907 stats->tx_dropped++;
2908 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2909 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002910 if (dptr)
2911 dma_unmap_single(&oct->pci_dev->dev, dptr,
2912 ndata.datasize, DMA_TO_DEVICE);
Intiyaz Bashac859e212017-10-26 16:18:20 -07002913
2914 octeon_ring_doorbell_locked(oct, iq_no);
2915
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002916 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002917 return NETDEV_TX_OK;
2918}
2919
2920/** \brief Network device Tx timeout
2921 * @param netdev pointer to network device
2922 */
2923static void liquidio_tx_timeout(struct net_device *netdev)
2924{
2925 struct lio *lio;
2926
2927 lio = GET_LIO(netdev);
2928
2929 netif_info(lio, tx_err, lio->netdev,
2930 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2931 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002932 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002933 txqs_wake(netdev);
2934}
2935
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07002936static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
2937 __be16 proto __attribute__((unused)),
2938 u16 vid)
2939{
2940 struct lio *lio = GET_LIO(netdev);
2941 struct octeon_device *oct = lio->oct_dev;
2942 struct octnic_ctrl_pkt nctrl;
2943 int ret = 0;
2944
2945 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2946
2947 nctrl.ncmd.u64 = 0;
2948 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
2949 nctrl.ncmd.s.param1 = vid;
2950 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2951 nctrl.wait_time = 100;
2952 nctrl.netpndev = (u64)netdev;
2953 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2954
2955 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2956 if (ret < 0) {
2957 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2958 ret);
2959 }
2960
2961 return ret;
2962}
2963
2964static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
2965 __be16 proto __attribute__((unused)),
2966 u16 vid)
2967{
2968 struct lio *lio = GET_LIO(netdev);
2969 struct octeon_device *oct = lio->oct_dev;
2970 struct octnic_ctrl_pkt nctrl;
2971 int ret = 0;
2972
2973 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2974
2975 nctrl.ncmd.u64 = 0;
2976 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
2977 nctrl.ncmd.s.param1 = vid;
2978 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2979 nctrl.wait_time = 100;
2980 nctrl.netpndev = (u64)netdev;
2981 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2982
2983 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2984 if (ret < 0) {
2985 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2986 ret);
2987 }
2988 return ret;
2989}
2990
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002991/** Sending command to enable/disable RX checksum offload
2992 * @param netdev pointer to network device
2993 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
2994 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
2995 * OCTNET_CMD_RXCSUM_DISABLE
2996 * @returns SUCCESS or FAILURE
2997 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02002998static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
2999 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003000{
3001 struct lio *lio = GET_LIO(netdev);
3002 struct octeon_device *oct = lio->oct_dev;
3003 struct octnic_ctrl_pkt nctrl;
3004 int ret = 0;
3005
Felix Manlunas0c264582017-04-06 19:22:22 -07003006 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3007
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003008 nctrl.ncmd.u64 = 0;
3009 nctrl.ncmd.s.cmd = command;
3010 nctrl.ncmd.s.param1 = rx_cmd;
3011 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3012 nctrl.wait_time = 100;
3013 nctrl.netpndev = (u64)netdev;
3014 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3015
3016 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3017 if (ret < 0) {
3018 dev_err(&oct->pci_dev->dev,
3019 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3020 ret);
3021 }
3022 return ret;
3023}
3024
3025/** Sending command to add/delete VxLAN UDP port to firmware
3026 * @param netdev pointer to network device
3027 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3028 * @param vxlan_port VxLAN port to be added or deleted
3029 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3030 * OCTNET_CMD_VXLAN_PORT_DEL
3031 * @returns SUCCESS or FAILURE
3032 */
3033static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3034 u16 vxlan_port, u8 vxlan_cmd_bit)
3035{
3036 struct lio *lio = GET_LIO(netdev);
3037 struct octeon_device *oct = lio->oct_dev;
3038 struct octnic_ctrl_pkt nctrl;
3039 int ret = 0;
3040
Felix Manlunas0c264582017-04-06 19:22:22 -07003041 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3042
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003043 nctrl.ncmd.u64 = 0;
3044 nctrl.ncmd.s.cmd = command;
3045 nctrl.ncmd.s.more = vxlan_cmd_bit;
3046 nctrl.ncmd.s.param1 = vxlan_port;
3047 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3048 nctrl.wait_time = 100;
3049 nctrl.netpndev = (u64)netdev;
3050 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3051
3052 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3053 if (ret < 0) {
3054 dev_err(&oct->pci_dev->dev,
3055 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3056 ret);
3057 }
3058 return ret;
3059}
3060
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003061/** \brief Net device fix features
3062 * @param netdev pointer to network device
3063 * @param request features requested
3064 * @returns updated features list
3065 */
3066static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3067 netdev_features_t request)
3068{
3069 struct lio *lio = netdev_priv(netdev);
3070
3071 if ((request & NETIF_F_RXCSUM) &&
3072 !(lio->dev_capability & NETIF_F_RXCSUM))
3073 request &= ~NETIF_F_RXCSUM;
3074
3075 if ((request & NETIF_F_HW_CSUM) &&
3076 !(lio->dev_capability & NETIF_F_HW_CSUM))
3077 request &= ~NETIF_F_HW_CSUM;
3078
3079 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3080 request &= ~NETIF_F_TSO;
3081
3082 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3083 request &= ~NETIF_F_TSO6;
3084
3085 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3086 request &= ~NETIF_F_LRO;
3087
3088 /*Disable LRO if RXCSUM is off */
3089 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3090 (lio->dev_capability & NETIF_F_LRO))
3091 request &= ~NETIF_F_LRO;
3092
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003093 if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3094 !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
3095 request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3096
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003097 return request;
3098}
3099
3100/** \brief Net device set features
3101 * @param netdev pointer to network device
3102 * @param features features to enable/disable
3103 */
3104static int liquidio_set_features(struct net_device *netdev,
3105 netdev_features_t features)
3106{
3107 struct lio *lio = netdev_priv(netdev);
3108
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003109 if ((features & NETIF_F_LRO) &&
3110 (lio->dev_capability & NETIF_F_LRO) &&
3111 !(netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003112 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3113 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003114 else if (!(features & NETIF_F_LRO) &&
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003115 (lio->dev_capability & NETIF_F_LRO) &&
3116 (netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003117 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3118 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003119
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003120 /* Sending command to firmware to enable/disable RX checksum
3121 * offload settings using ethtool
3122 */
3123 if (!(netdev->features & NETIF_F_RXCSUM) &&
3124 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3125 (features & NETIF_F_RXCSUM))
3126 liquidio_set_rxcsum_command(netdev,
3127 OCTNET_CMD_TNL_RX_CSUM_CTL,
3128 OCTNET_CMD_RXCSUM_ENABLE);
3129 else if ((netdev->features & NETIF_F_RXCSUM) &&
3130 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3131 !(features & NETIF_F_RXCSUM))
3132 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3133 OCTNET_CMD_RXCSUM_DISABLE);
3134
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003135 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3136 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3137 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3138 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3139 OCTNET_CMD_VLAN_FILTER_ENABLE);
3140 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3141 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3142 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3143 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3144 OCTNET_CMD_VLAN_FILTER_DISABLE);
3145
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003146 return 0;
3147}
3148
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003149static void liquidio_add_vxlan_port(struct net_device *netdev,
3150 struct udp_tunnel_info *ti)
3151{
3152 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3153 return;
3154
3155 liquidio_vxlan_port_command(netdev,
3156 OCTNET_CMD_VXLAN_PORT_CONFIG,
3157 htons(ti->port),
3158 OCTNET_CMD_VXLAN_PORT_ADD);
3159}
3160
3161static void liquidio_del_vxlan_port(struct net_device *netdev,
3162 struct udp_tunnel_info *ti)
3163{
3164 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3165 return;
3166
3167 liquidio_vxlan_port_command(netdev,
3168 OCTNET_CMD_VXLAN_PORT_CONFIG,
3169 htons(ti->port),
3170 OCTNET_CMD_VXLAN_PORT_DEL);
3171}
3172
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003173static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3174 u8 *mac, bool is_admin_assigned)
3175{
3176 struct lio *lio = GET_LIO(netdev);
3177 struct octeon_device *oct = lio->oct_dev;
3178 struct octnic_ctrl_pkt nctrl;
3179
3180 if (!is_valid_ether_addr(mac))
3181 return -EINVAL;
3182
3183 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3184 return -EINVAL;
3185
3186 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3187
3188 nctrl.ncmd.u64 = 0;
3189 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3190 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3191 nctrl.ncmd.s.param1 = vfidx + 1;
3192 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3193 nctrl.ncmd.s.more = 1;
3194 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003195 nctrl.netpndev = (u64)netdev;
3196 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003197 nctrl.wait_time = LIO_CMD_WAIT_TM;
3198
3199 nctrl.udd[0] = 0;
3200 /* The MAC Address is presented in network byte order. */
3201 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3202
3203 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3204
3205 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3206
3207 return 0;
3208}
3209
3210static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3211{
3212 struct lio *lio = GET_LIO(netdev);
3213 struct octeon_device *oct = lio->oct_dev;
3214 int retval;
3215
Felix Manlunas0d9a5992017-05-16 11:28:00 -07003216 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3217 return -EINVAL;
3218
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003219 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3220 if (!retval)
3221 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3222
3223 return retval;
3224}
3225
3226static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3227 u16 vlan, u8 qos, __be16 vlan_proto)
3228{
3229 struct lio *lio = GET_LIO(netdev);
3230 struct octeon_device *oct = lio->oct_dev;
3231 struct octnic_ctrl_pkt nctrl;
3232 u16 vlantci;
3233
3234 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3235 return -EINVAL;
3236
3237 if (vlan_proto != htons(ETH_P_8021Q))
3238 return -EPROTONOSUPPORT;
3239
3240 if (vlan >= VLAN_N_VID || qos > 7)
3241 return -EINVAL;
3242
3243 if (vlan)
3244 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3245 else
3246 vlantci = 0;
3247
3248 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3249 return 0;
3250
3251 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3252
3253 if (vlan)
3254 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3255 else
3256 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3257
3258 nctrl.ncmd.s.param1 = vlantci;
3259 nctrl.ncmd.s.param2 =
3260 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3261 nctrl.ncmd.s.more = 0;
3262 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3263 nctrl.cb_fn = 0;
3264 nctrl.wait_time = LIO_CMD_WAIT_TM;
3265
3266 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3267
3268 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3269
3270 return 0;
3271}
3272
3273static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3274 struct ifla_vf_info *ivi)
3275{
3276 struct lio *lio = GET_LIO(netdev);
3277 struct octeon_device *oct = lio->oct_dev;
3278 u8 *macaddr;
3279
3280 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3281 return -EINVAL;
3282
3283 ivi->vf = vfidx;
3284 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3285 ether_addr_copy(&ivi->mac[0], macaddr);
3286 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3287 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3288 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3289 return 0;
3290}
3291
3292static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3293 int linkstate)
3294{
3295 struct lio *lio = GET_LIO(netdev);
3296 struct octeon_device *oct = lio->oct_dev;
3297 struct octnic_ctrl_pkt nctrl;
3298
3299 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3300 return -EINVAL;
3301
3302 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3303 return 0;
3304
3305 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3306 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3307 nctrl.ncmd.s.param1 =
3308 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3309 nctrl.ncmd.s.param2 = linkstate;
3310 nctrl.ncmd.s.more = 0;
3311 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3312 nctrl.cb_fn = 0;
3313 nctrl.wait_time = LIO_CMD_WAIT_TM;
3314
3315 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3316
3317 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3318
3319 return 0;
3320}
3321
Vijaya Mohan Guvva1f233f32017-10-31 16:04:53 -07003322static int
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003323liquidio_eswitch_mode_get(struct devlink *devlink, u16 *mode)
3324{
3325 struct lio_devlink_priv *priv;
3326 struct octeon_device *oct;
3327
3328 priv = devlink_priv(devlink);
3329 oct = priv->oct;
3330
3331 *mode = oct->eswitch_mode;
3332
3333 return 0;
3334}
3335
3336static int
3337liquidio_eswitch_mode_set(struct devlink *devlink, u16 mode)
3338{
3339 struct lio_devlink_priv *priv;
3340 struct octeon_device *oct;
3341 int ret = 0;
3342
3343 priv = devlink_priv(devlink);
3344 oct = priv->oct;
3345
3346 if (!(oct->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP))
3347 return -EINVAL;
3348
3349 if (oct->eswitch_mode == mode)
3350 return 0;
3351
3352 switch (mode) {
3353 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
3354 oct->eswitch_mode = mode;
3355 ret = lio_vf_rep_create(oct);
3356 break;
3357
3358 case DEVLINK_ESWITCH_MODE_LEGACY:
3359 lio_vf_rep_destroy(oct);
3360 oct->eswitch_mode = mode;
3361 break;
3362
3363 default:
3364 ret = -EINVAL;
3365 }
3366
3367 return ret;
3368}
3369
3370static const struct devlink_ops liquidio_devlink_ops = {
3371 .eswitch_mode_get = liquidio_eswitch_mode_get,
3372 .eswitch_mode_set = liquidio_eswitch_mode_set,
3373};
3374
3375static int
Vijaya Mohan Guvva1f233f32017-10-31 16:04:53 -07003376lio_pf_switchdev_attr_get(struct net_device *dev, struct switchdev_attr *attr)
3377{
3378 struct lio *lio = GET_LIO(dev);
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003379 struct octeon_device *oct = lio->oct_dev;
3380
3381 if (oct->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
3382 return -EOPNOTSUPP;
Vijaya Mohan Guvva1f233f32017-10-31 16:04:53 -07003383
3384 switch (attr->id) {
3385 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
3386 attr->u.ppid.id_len = ETH_ALEN;
3387 ether_addr_copy(attr->u.ppid.id,
3388 (void *)&lio->linfo.hw_addr + 2);
3389 break;
3390
3391 default:
3392 return -EOPNOTSUPP;
3393 }
3394
3395 return 0;
3396}
3397
3398static const struct switchdev_ops lio_pf_switchdev_ops = {
3399 .switchdev_port_attr_get = lio_pf_switchdev_attr_get,
3400};
3401
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003402static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003403 .ndo_open = liquidio_open,
3404 .ndo_stop = liquidio_stop,
3405 .ndo_start_xmit = liquidio_xmit,
3406 .ndo_get_stats = liquidio_get_stats,
3407 .ndo_set_mac_address = liquidio_set_mac,
3408 .ndo_set_rx_mode = liquidio_set_mcast_list,
3409 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003410
3411 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3412 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003413 .ndo_change_mtu = liquidio_change_mtu,
3414 .ndo_do_ioctl = liquidio_ioctl,
3415 .ndo_fix_features = liquidio_fix_features,
3416 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003417 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3418 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003419 .ndo_set_vf_mac = liquidio_set_vf_mac,
3420 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3421 .ndo_get_vf_config = liquidio_get_vf_config,
3422 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003423};
3424
3425/** \brief Entry point for the liquidio module
3426 */
3427static int __init liquidio_init(void)
3428{
3429 int i;
3430 struct handshake *hs;
3431
3432 init_completion(&first_stage);
3433
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003434 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003435
3436 if (liquidio_init_pci())
3437 return -EINVAL;
3438
3439 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3440
3441 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3442 hs = &handshake[i];
3443 if (hs->pci_dev) {
3444 wait_for_completion(&hs->init);
3445 if (!hs->init_ok) {
3446 /* init handshake failed */
3447 dev_err(&hs->pci_dev->dev,
3448 "Failed to init device\n");
3449 liquidio_deinit_pci();
3450 return -EIO;
3451 }
3452 }
3453 }
3454
3455 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3456 hs = &handshake[i];
3457 if (hs->pci_dev) {
3458 wait_for_completion_timeout(&hs->started,
3459 msecs_to_jiffies(30000));
3460 if (!hs->started_ok) {
3461 /* starter handshake failed */
3462 dev_err(&hs->pci_dev->dev,
3463 "Firmware failed to start\n");
3464 liquidio_deinit_pci();
3465 return -EIO;
3466 }
3467 }
3468 }
3469
3470 return 0;
3471}
3472
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003473static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003474{
3475 struct octeon_device *oct = (struct octeon_device *)buf;
3476 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003477 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003478 union oct_link_status *ls;
3479 int i;
3480
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003481 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003482 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3483 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003484 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003485 goto nic_info_err;
3486 }
3487
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003488 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003489 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3490 OCT_DROQ_INFO_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003491
3492 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003493 for (i = 0; i < oct->ifcount; i++) {
3494 if (oct->props[i].gmxport == gmxport) {
3495 update_link_status(oct->props[i].netdev, ls);
3496 break;
3497 }
3498 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003499
3500nic_info_err:
3501 for (i = 0; i < recv_pkt->buffer_count; i++)
3502 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3503 octeon_free_recv_info(recv_info);
3504 return 0;
3505}
3506
3507/**
3508 * \brief Setup network interfaces
3509 * @param octeon_dev octeon device
3510 *
3511 * Called during init time for each device. It assumes the NIC
3512 * is already up and running. The link information for each
3513 * interface is passed in link_info.
3514 */
3515static int setup_nic_devices(struct octeon_device *octeon_dev)
3516{
3517 struct lio *lio = NULL;
3518 struct net_device *netdev;
Rick Farringtonb36e4822017-09-22 17:12:47 -07003519 u8 mac[6], i, j, *fw_ver;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003520 struct octeon_soft_command *sc;
3521 struct liquidio_if_cfg_context *ctx;
3522 struct liquidio_if_cfg_resp *resp;
3523 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003524 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003525 union oct_nic_if_cfg if_cfg;
3526 unsigned int base_queue;
3527 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003528 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003529 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003530 struct lio_version *vdata;
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003531 struct devlink *devlink;
3532 struct lio_devlink_priv *lio_devlink;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003533
3534 /* This is to handle link status changes */
3535 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3536 OPCODE_NIC_INFO,
3537 lio_nic_info, octeon_dev);
3538
3539 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3540 * They are handled directly.
3541 */
3542 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3543 free_netbuf);
3544
3545 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3546 free_netsgbuf);
3547
3548 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3549 free_netsgbuf_with_resp);
3550
3551 for (i = 0; i < octeon_dev->ifcount; i++) {
3552 resp_size = sizeof(struct liquidio_if_cfg_resp);
3553 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003554 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003555 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003556 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003557 resp_size, ctx_size);
3558 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3559 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003560 vdata = (struct lio_version *)sc->virtdptr;
3561
3562 *((u64 *)vdata) = 0;
3563 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3564 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3565 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003566
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003567 if (OCTEON_CN23XX_PF(octeon_dev)) {
3568 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3569 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3570 base_queue = octeon_dev->sriov_info.pf_srn;
3571
3572 gmx_port_id = octeon_dev->pf_num;
3573 ifidx_or_pfnum = octeon_dev->pf_num;
3574 } else {
3575 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3576 octeon_get_conf(octeon_dev), i);
3577 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3578 octeon_get_conf(octeon_dev), i);
3579 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3580 octeon_get_conf(octeon_dev), i);
3581 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3582 octeon_get_conf(octeon_dev), i);
3583 ifidx_or_pfnum = i;
3584 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003585
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003586 dev_dbg(&octeon_dev->pci_dev->dev,
3587 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003588 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003589 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003590 ctx->octeon_id = lio_get_device_id(octeon_dev);
3591 init_waitqueue_head(&ctx->wc);
3592
3593 if_cfg.u64 = 0;
3594 if_cfg.s.num_iqueues = num_iqueues;
3595 if_cfg.s.num_oqueues = num_oqueues;
3596 if_cfg.s.base_queue = base_queue;
3597 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003598
3599 sc->iq_no = 0;
3600
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003601 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003602 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003603 if_cfg.u64, 0);
3604
3605 sc->callback = if_cfg_callback;
3606 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003607 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003608
3609 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003610 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003611 dev_err(&octeon_dev->pci_dev->dev,
3612 "iq/oq config failed status: %x\n",
3613 retval);
3614 /* Soft instr is freed by driver in case of failure. */
3615 goto setup_nic_dev_fail;
3616 }
3617
3618 /* Sleep on a wait queue till the cond flag indicates that the
3619 * response arrived or timed-out.
3620 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003621 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3622 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3623 goto setup_nic_wait_intr;
3624 }
3625
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003626 retval = resp->status;
3627 if (retval) {
3628 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3629 goto setup_nic_dev_fail;
3630 }
3631
Rick Farringtonb36e4822017-09-22 17:12:47 -07003632 /* Verify f/w version (in case of 'auto' loading from flash) */
3633 fw_ver = octeon_dev->fw_info.liquidio_firmware_version;
3634 if (memcmp(LIQUIDIO_BASE_VERSION,
3635 fw_ver,
3636 strlen(LIQUIDIO_BASE_VERSION))) {
3637 dev_err(&octeon_dev->pci_dev->dev,
3638 "Unmatched firmware version. Expected %s.x, got %s.\n",
3639 LIQUIDIO_BASE_VERSION, fw_ver);
3640 goto setup_nic_dev_fail;
3641 } else if (atomic_read(octeon_dev->adapter_fw_state) ==
3642 FW_IS_PRELOADED) {
3643 dev_info(&octeon_dev->pci_dev->dev,
3644 "Using auto-loaded firmware version %s.\n",
3645 fw_ver);
3646 }
3647
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003648 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3649 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3650
3651 num_iqueues = hweight64(resp->cfg_info.iqmask);
3652 num_oqueues = hweight64(resp->cfg_info.oqmask);
3653
3654 if (!(num_iqueues) || !(num_oqueues)) {
3655 dev_err(&octeon_dev->pci_dev->dev,
3656 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3657 resp->cfg_info.iqmask,
3658 resp->cfg_info.oqmask);
3659 goto setup_nic_dev_fail;
3660 }
3661 dev_dbg(&octeon_dev->pci_dev->dev,
3662 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3663 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3664 num_iqueues, num_oqueues);
3665 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3666
3667 if (!netdev) {
3668 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3669 goto setup_nic_dev_fail;
3670 }
3671
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003672 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003673
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003674 /* Associate the routines that will handle different
3675 * netdev tasks.
3676 */
3677 netdev->netdev_ops = &lionetdevops;
Vijaya Mohan Guvva1f233f32017-10-31 16:04:53 -07003678 SWITCHDEV_SET_OPS(netdev, &lio_pf_switchdev_ops);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003679
3680 lio = GET_LIO(netdev);
3681
3682 memset(lio, 0, sizeof(struct lio));
3683
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003684 lio->ifidx = ifidx_or_pfnum;
3685
3686 props = &octeon_dev->props[i];
3687 props->gmxport = resp->cfg_info.linfo.gmxport;
3688 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003689
3690 lio->linfo.num_rxpciq = num_oqueues;
3691 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003692 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003693 lio->linfo.rxpciq[j].u64 =
3694 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003695 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003696 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003697 lio->linfo.txpciq[j].u64 =
3698 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003699 }
3700 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3701 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3702 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3703
3704 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3705
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003706 if (OCTEON_CN23XX_PF(octeon_dev) ||
3707 OCTEON_CN6XXX(octeon_dev)) {
3708 lio->dev_capability = NETIF_F_HIGHDMA
3709 | NETIF_F_IP_CSUM
3710 | NETIF_F_IPV6_CSUM
3711 | NETIF_F_SG | NETIF_F_RXCSUM
3712 | NETIF_F_GRO
3713 | NETIF_F_TSO | NETIF_F_TSO6
3714 | NETIF_F_LRO;
3715 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003716 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3717
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003718 /* Copy of transmit encapsulation capabilities:
3719 * TSO, TSO6, Checksums for this device
3720 */
3721 lio->enc_dev_capability = NETIF_F_IP_CSUM
3722 | NETIF_F_IPV6_CSUM
3723 | NETIF_F_GSO_UDP_TUNNEL
3724 | NETIF_F_HW_CSUM | NETIF_F_SG
3725 | NETIF_F_RXCSUM
3726 | NETIF_F_TSO | NETIF_F_TSO6
3727 | NETIF_F_LRO;
3728
3729 netdev->hw_enc_features = (lio->enc_dev_capability &
3730 ~NETIF_F_LRO);
3731
3732 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3733
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003734 netdev->vlan_features = lio->dev_capability;
3735 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003736 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3737 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003738 NETIF_F_HW_VLAN_CTAG_TX;
3739
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003740 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3741
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003742 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003743 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3744 netdev->hw_features = netdev->hw_features &
3745 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003746
Jarod Wilson109cc162016-10-17 15:54:13 -04003747 /* MTU range: 68 - 16000 */
3748 netdev->min_mtu = LIO_MIN_MTU_SIZE;
3749 netdev->max_mtu = LIO_MAX_MTU_SIZE;
3750
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003751 /* Point to the properties for octeon device to which this
3752 * interface belongs.
3753 */
3754 lio->oct_dev = octeon_dev;
3755 lio->octprops = props;
3756 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003757
3758 dev_dbg(&octeon_dev->pci_dev->dev,
3759 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3760 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3761
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003762 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
3763 u8 vfmac[ETH_ALEN];
3764
3765 random_ether_addr(&vfmac[0]);
3766 if (__liquidio_set_vf_mac(netdev, j,
3767 &vfmac[0], false)) {
3768 dev_err(&octeon_dev->pci_dev->dev,
3769 "Error setting VF%d MAC address\n",
3770 j);
3771 goto setup_nic_dev_fail;
3772 }
3773 }
3774
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003775 /* 64-bit swap required on LE machines */
3776 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3777 for (j = 0; j < 6; j++)
3778 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3779
3780 /* Copy MAC Address to OS network device structure */
3781
3782 ether_addr_copy(netdev->dev_addr, mac);
3783
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003784 /* By default all interfaces on a single Octeon uses the same
3785 * tx and rx queues
3786 */
3787 lio->txq = lio->linfo.txpciq[0].s.q_no;
3788 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07003789 if (liquidio_setup_io_queues(octeon_dev, i,
3790 lio->linfo.num_txpciq,
3791 lio->linfo.num_rxpciq)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003792 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3793 goto setup_nic_dev_fail;
3794 }
3795
3796 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3797
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003798 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3799 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3800
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003801 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003802 dev_err(&octeon_dev->pci_dev->dev,
3803 "Gather list allocation failed\n");
3804 goto setup_nic_dev_fail;
3805 }
3806
3807 /* Register ethtool support */
3808 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003809 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
3810 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
3811 else
3812 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003813
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003814 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003815 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3816 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003817
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003818 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3819 OCTNET_CMD_VLAN_FILTER_ENABLE);
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003820
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003821 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003822 liquidio_set_feature(netdev,
3823 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003824
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07003825 if (setup_link_status_change_wq(netdev))
3826 goto setup_nic_dev_fail;
3827
Veerasenareddy Burru907aaa62017-10-23 20:33:25 -07003828 if ((octeon_dev->fw_info.app_cap_flags &
3829 LIQUIDIO_TIME_SYNC_CAP) &&
3830 setup_sync_octeon_time_wq(netdev))
3831 goto setup_nic_dev_fail;
3832
Satanand Burla031d4f12017-03-22 11:31:13 -07003833 if (setup_rx_oom_poll_fn(netdev))
3834 goto setup_nic_dev_fail;
3835
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003836 /* Register the network device with the OS */
3837 if (register_netdev(netdev)) {
3838 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3839 goto setup_nic_dev_fail;
3840 }
3841
3842 dev_dbg(&octeon_dev->pci_dev->dev,
3843 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3844 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3845 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003846 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003847
3848 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3849
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003850 /* Sending command to firmware to enable Rx checksum offload
3851 * by default at the time of setup of Liquidio driver for
3852 * this device
3853 */
3854 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3855 OCTNET_CMD_RXCSUM_ENABLE);
3856 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3857 OCTNET_CMD_TXCSUM_ENABLE);
3858
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003859 dev_dbg(&octeon_dev->pci_dev->dev,
3860 "NIC ifidx:%d Setup successful\n", i);
3861
3862 octeon_free_soft_command(octeon_dev, sc);
3863 }
3864
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003865 devlink = devlink_alloc(&liquidio_devlink_ops,
3866 sizeof(struct lio_devlink_priv));
3867 if (!devlink) {
3868 dev_err(&octeon_dev->pci_dev->dev, "devlink alloc failed\n");
3869 goto setup_nic_wait_intr;
3870 }
3871
3872 lio_devlink = devlink_priv(devlink);
3873 lio_devlink->oct = octeon_dev;
3874
3875 if (devlink_register(devlink, &octeon_dev->pci_dev->dev)) {
3876 devlink_free(devlink);
3877 dev_err(&octeon_dev->pci_dev->dev,
3878 "devlink registration failed\n");
3879 goto setup_nic_wait_intr;
3880 }
3881
3882 octeon_dev->devlink = devlink;
3883 octeon_dev->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
3884
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003885 return 0;
3886
3887setup_nic_dev_fail:
3888
3889 octeon_free_soft_command(octeon_dev, sc);
3890
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003891setup_nic_wait_intr:
3892
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003893 while (i--) {
3894 dev_err(&octeon_dev->pci_dev->dev,
3895 "NIC ifidx:%d Setup failed\n", i);
3896 liquidio_destroy_nic_device(octeon_dev, i);
3897 }
3898 return -ENODEV;
3899}
3900
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08003901#ifdef CONFIG_PCI_IOV
3902static int octeon_enable_sriov(struct octeon_device *oct)
3903{
3904 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
3905 struct pci_dev *vfdev;
3906 int err;
3907 u32 u;
3908
3909 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
3910 err = pci_enable_sriov(oct->pci_dev,
3911 oct->sriov_info.num_vfs_alloced);
3912 if (err) {
3913 dev_err(&oct->pci_dev->dev,
3914 "OCTEON: Failed to enable PCI sriov: %d\n",
3915 err);
3916 oct->sriov_info.num_vfs_alloced = 0;
3917 return err;
3918 }
3919 oct->sriov_info.sriov_enabled = 1;
3920
3921 /* init lookup table that maps DPI ring number to VF pci_dev
3922 * struct pointer
3923 */
3924 u = 0;
3925 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3926 OCTEON_CN23XX_VF_VID, NULL);
3927 while (vfdev) {
3928 if (vfdev->is_virtfn &&
3929 (vfdev->physfn == oct->pci_dev)) {
3930 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
3931 vfdev;
3932 u += oct->sriov_info.rings_per_vf;
3933 }
3934 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3935 OCTEON_CN23XX_VF_VID, vfdev);
3936 }
3937 }
3938
3939 return num_vfs_alloced;
3940}
3941
3942static int lio_pci_sriov_disable(struct octeon_device *oct)
3943{
3944 int u;
3945
3946 if (pci_vfs_assigned(oct->pci_dev)) {
3947 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
3948 return -EPERM;
3949 }
3950
3951 pci_disable_sriov(oct->pci_dev);
3952
3953 u = 0;
3954 while (u < MAX_POSSIBLE_VFS) {
3955 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
3956 u += oct->sriov_info.rings_per_vf;
3957 }
3958
3959 oct->sriov_info.num_vfs_alloced = 0;
3960 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
3961 oct->pf_num);
3962
3963 return 0;
3964}
3965
3966static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
3967{
3968 struct octeon_device *oct = pci_get_drvdata(dev);
3969 int ret = 0;
3970
3971 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
3972 (oct->sriov_info.sriov_enabled)) {
3973 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
3974 oct->pf_num, num_vfs);
3975 return 0;
3976 }
3977
3978 if (!num_vfs) {
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003979 lio_vf_rep_destroy(oct);
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08003980 ret = lio_pci_sriov_disable(oct);
3981 } else if (num_vfs > oct->sriov_info.max_vfs) {
3982 dev_err(&oct->pci_dev->dev,
3983 "OCTEON: Max allowed VFs:%d user requested:%d",
3984 oct->sriov_info.max_vfs, num_vfs);
3985 ret = -EPERM;
3986 } else {
3987 oct->sriov_info.num_vfs_alloced = num_vfs;
3988 ret = octeon_enable_sriov(oct);
3989 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
3990 oct->pf_num, num_vfs);
Vijaya Mohan Guvvad4be8eb2017-10-31 16:04:57 -07003991 ret = lio_vf_rep_create(oct);
3992 if (ret)
3993 dev_info(&oct->pci_dev->dev,
3994 "vf representor create failed");
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08003995 }
3996
3997 return ret;
3998}
3999#endif
4000
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004001/**
4002 * \brief initialize the NIC
4003 * @param oct octeon device
4004 *
4005 * This initialization routine is called once the Octeon device application is
4006 * up and running
4007 */
4008static int liquidio_init_nic_module(struct octeon_device *oct)
4009{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004010 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004011 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4012
4013 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4014
4015 /* only default iq and oq were initialized
4016 * initialize the rest as well
4017 */
4018 /* run port_config command for each port */
4019 oct->ifcount = num_nic_ports;
4020
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004021 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004022
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004023 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4024 oct->props[i].gmxport = -1;
4025
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004026 retval = setup_nic_devices(oct);
4027 if (retval) {
4028 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4029 goto octnet_init_failure;
4030 }
4031
4032 liquidio_ptp_init(oct);
4033
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004034 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4035
4036 return retval;
4037
4038octnet_init_failure:
4039
4040 oct->ifcount = 0;
4041
4042 return retval;
4043}
4044
4045/**
4046 * \brief starter callback that invokes the remaining initialization work after
4047 * the NIC is up and running.
4048 * @param octptr work struct work_struct
4049 */
4050static void nic_starter(struct work_struct *work)
4051{
4052 struct octeon_device *oct;
4053 struct cavium_wk *wk = (struct cavium_wk *)work;
4054
4055 oct = (struct octeon_device *)wk->ctxptr;
4056
4057 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4058 return;
4059
4060 /* If the status of the device is CORE_OK, the core
4061 * application has reported its application type. Call
4062 * any registered handlers now and move to the RUNNING
4063 * state.
4064 */
4065 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4066 schedule_delayed_work(&oct->nic_poll_work.work,
4067 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4068 return;
4069 }
4070
4071 atomic_set(&oct->status, OCT_DEV_RUNNING);
4072
4073 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4074 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4075
4076 if (liquidio_init_nic_module(oct))
4077 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4078 else
4079 handshake[oct->octeon_id].started_ok = 1;
4080 } else {
4081 dev_err(&oct->pci_dev->dev,
4082 "Unexpected application running on NIC (%d). Check firmware.\n",
4083 oct->app_mode);
4084 }
4085
4086 complete(&handshake[oct->octeon_id].started);
4087}
4088
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004089static int
4090octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4091{
4092 struct octeon_device *oct = (struct octeon_device *)buf;
4093 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4094 int i, notice, vf_idx;
Felix Manlunasbb54be52017-04-04 19:26:57 -07004095 bool cores_crashed;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004096 u64 *data, vf_num;
4097
4098 notice = recv_pkt->rh.r.ossp;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07004099 data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004100
4101 /* the first 64-bit word of data is the vf_num */
4102 vf_num = data[0];
4103 octeon_swap_8B_data(&vf_num, 1);
4104 vf_idx = (int)vf_num - 1;
4105
Felix Manlunasbb54be52017-04-04 19:26:57 -07004106 cores_crashed = READ_ONCE(oct->cores_crashed);
4107
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004108 if (notice == VF_DRV_LOADED) {
4109 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4110 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4111 dev_info(&oct->pci_dev->dev,
4112 "driver for VF%d was loaded\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004113 if (!cores_crashed)
4114 try_module_get(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004115 }
4116 } else if (notice == VF_DRV_REMOVED) {
4117 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4118 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4119 dev_info(&oct->pci_dev->dev,
4120 "driver for VF%d was removed\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004121 if (!cores_crashed)
4122 module_put(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004123 }
4124 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4125 u8 *b = (u8 *)&data[1];
4126
4127 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4128 dev_info(&oct->pci_dev->dev,
4129 "VF driver changed VF%d's MAC address to %pM\n",
4130 vf_idx, b + 2);
4131 }
4132
4133 for (i = 0; i < recv_pkt->buffer_count; i++)
4134 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4135 octeon_free_recv_info(recv_info);
4136
4137 return 0;
4138}
4139
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004140/**
4141 * \brief Device initialization for each Octeon device that is probed
4142 * @param octeon_dev octeon device
4143 */
4144static int octeon_device_init(struct octeon_device *octeon_dev)
4145{
4146 int j, ret;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004147 char bootcmd[] = "\n";
Rick Farringtonda1542b2017-08-11 18:43:14 -07004148 char *dbg_enb = NULL;
Rick Farrington088b8742017-09-22 17:12:43 -07004149 enum lio_fw_state fw_state;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004150 struct octeon_device_priv *oct_priv =
4151 (struct octeon_device_priv *)octeon_dev->priv;
4152 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4153
4154 /* Enable access to the octeon device and make its DMA capability
4155 * known to the OS.
4156 */
4157 if (octeon_pci_os_setup(octeon_dev))
4158 return 1;
4159
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004160 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4161
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004162 /* Identify the Octeon type and map the BAR address space. */
4163 if (octeon_chip_specific_setup(octeon_dev)) {
4164 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4165 return 1;
4166 }
4167
4168 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4169
Rick Farringtone1e3ce62017-05-16 11:14:50 -07004170 /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
4171 * since that is what is required for the reference to be removed
4172 * during de-initialization (see 'octeon_destroy_resources').
4173 */
4174 octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
4175 PCI_SLOT(octeon_dev->pci_dev->devfn),
4176 PCI_FUNC(octeon_dev->pci_dev->devfn),
4177 true);
4178
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004179 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4180
Rick Farrington088b8742017-09-22 17:12:43 -07004181 /* CN23XX supports preloaded firmware if the following is true:
4182 *
4183 * The adapter indicates that firmware is currently running AND
4184 * 'fw_type' is 'auto'.
4185 *
4186 * (default state is NEEDS_TO_BE_LOADED, override it if appropriate).
4187 */
4188 if (OCTEON_CN23XX_PF(octeon_dev) &&
4189 cn23xx_fw_loaded(octeon_dev) && fw_type_is_auto()) {
4190 atomic_cmpxchg(octeon_dev->adapter_fw_state,
4191 FW_NEEDS_TO_BE_LOADED, FW_IS_PRELOADED);
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004192 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004193
Rick Farrington088b8742017-09-22 17:12:43 -07004194 /* If loading firmware, only first device of adapter needs to do so. */
4195 fw_state = atomic_cmpxchg(octeon_dev->adapter_fw_state,
4196 FW_NEEDS_TO_BE_LOADED,
4197 FW_IS_BEING_LOADED);
4198
4199 /* Here, [local variable] 'fw_state' is set to one of:
4200 *
4201 * FW_IS_PRELOADED: No firmware is to be loaded (see above)
4202 * FW_NEEDS_TO_BE_LOADED: The driver's first instance will load
4203 * firmware to the adapter.
4204 * FW_IS_BEING_LOADED: The driver's second instance will not load
4205 * firmware to the adapter.
4206 */
4207
4208 /* Prior to f/w load, perform a soft reset of the Octeon device;
4209 * if error resetting, return w/error.
4210 */
4211 if (fw_state == FW_NEEDS_TO_BE_LOADED)
4212 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4213 return 1;
4214
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004215 /* Initialize the dispatch mechanism used to push packets arriving on
4216 * Octeon Output queues.
4217 */
4218 if (octeon_init_dispatch_list(octeon_dev))
4219 return 1;
4220
4221 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4222 OPCODE_NIC_CORE_DRV_ACTIVE,
4223 octeon_core_drv_init,
4224 octeon_dev);
4225
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004226 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4227 OPCODE_NIC_VF_DRV_NOTICE,
4228 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004229 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4230 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4231 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4232 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4233
4234 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4235
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004236 if (octeon_set_io_queues_off(octeon_dev)) {
4237 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4238 return 1;
4239 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004240
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004241 if (OCTEON_CN23XX_PF(octeon_dev)) {
4242 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4243 if (ret) {
4244 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4245 return ret;
4246 }
4247 }
4248
4249 /* Initialize soft command buffer pool
4250 */
4251 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4252 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4253 return 1;
4254 }
4255 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4256
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004257 /* Setup the data structures that manage this Octeon's Input queues. */
4258 if (octeon_setup_instr_queues(octeon_dev)) {
4259 dev_err(&octeon_dev->pci_dev->dev,
4260 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004261 return 1;
4262 }
4263 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4264
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004265 /* Initialize lists to manage the requests of different types that
4266 * arrive from user & kernel applications for this octeon device.
4267 */
4268 if (octeon_setup_response_list(octeon_dev)) {
4269 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4270 return 1;
4271 }
4272 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4273
4274 if (octeon_setup_output_queues(octeon_dev)) {
4275 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004276 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004277 }
4278
4279 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4280
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004281 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004282 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4283 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4284 return 1;
4285 }
4286 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4287
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004288 if (octeon_allocate_ioq_vector(octeon_dev)) {
4289 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4290 return 1;
4291 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004292 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004293
4294 } else {
4295 /* The input and output queue registers were setup earlier (the
4296 * queues were not enabled). Any additional registers
4297 * that need to be programmed should be done now.
4298 */
4299 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4300 if (ret) {
4301 dev_err(&octeon_dev->pci_dev->dev,
4302 "Failed to configure device registers\n");
4303 return ret;
4304 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004305 }
4306
4307 /* Initialize the tasklet that handles output queue packet processing.*/
4308 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4309 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4310 (unsigned long)octeon_dev);
4311
4312 /* Setup the interrupt handler and record the INT SUM register address
4313 */
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07004314 if (octeon_setup_interrupt(octeon_dev,
4315 octeon_dev->sriov_info.num_pf_rings))
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004316 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004317
4318 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004319 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004320
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004321 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4322
Rick Farrington3c57f612017-08-17 23:11:30 -07004323 /* Send Credit for Octeon Output queues. Credits are always sent BEFORE
4324 * the output queue is enabled.
4325 * This ensures that we'll receive the f/w CORE DRV_ACTIVE message in
4326 * case we've configured CN23XX_SLI_GBL_CONTROL[NOPTR_D] = 0.
4327 * Otherwise, it is possible that the DRV_ACTIVE message will be sent
4328 * before any credits have been issued, causing the ring to be reset
4329 * (and the f/w appear to never have started).
4330 */
4331 for (j = 0; j < octeon_dev->num_oqs; j++)
4332 writel(octeon_dev->droq[j]->max_count,
4333 octeon_dev->droq[j]->pkts_credit_reg);
4334
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004335 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004336 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4337 if (ret) {
4338 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4339 return ret;
4340 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004341
4342 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4343
Rick Farrington088b8742017-09-22 17:12:43 -07004344 if (fw_state == FW_NEEDS_TO_BE_LOADED) {
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004345 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4346 if (!ddr_timeout) {
4347 dev_info(&octeon_dev->pci_dev->dev,
4348 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4349 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004350
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004351 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004352
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004353 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4354 while (!ddr_timeout) {
4355 set_current_state(TASK_INTERRUPTIBLE);
4356 if (schedule_timeout(HZ / 10)) {
4357 /* user probably pressed Control-C */
4358 return 1;
4359 }
4360 }
4361 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4362 if (ret) {
4363 dev_err(&octeon_dev->pci_dev->dev,
4364 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4365 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004366 return 1;
4367 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004368
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004369 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4370 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4371 return 1;
4372 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004373
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004374 /* Divert uboot to take commands from host instead. */
4375 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004376
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004377 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4378 ret = octeon_init_consoles(octeon_dev);
4379 if (ret) {
4380 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4381 return 1;
4382 }
Rick Farringtonda1542b2017-08-11 18:43:14 -07004383 /* If console debug enabled, specify empty string to use default
4384 * enablement ELSE specify NULL string for 'disabled'.
4385 */
4386 dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
4387 ret = octeon_add_console(octeon_dev, 0, dbg_enb);
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004388 if (ret) {
4389 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4390 return 1;
Rick Farringtonda1542b2017-08-11 18:43:14 -07004391 } else if (octeon_console_debug_enabled(0)) {
4392 /* If console was added AND we're logging console output
4393 * then set our console print function.
4394 */
4395 octeon_dev->console[0].print = octeon_dbg_console_print;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004396 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004397
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004398 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004399
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004400 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4401 ret = load_firmware(octeon_dev);
4402 if (ret) {
4403 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4404 return 1;
4405 }
Rick Farrington088b8742017-09-22 17:12:43 -07004406
4407 atomic_set(octeon_dev->adapter_fw_state, FW_HAS_BEEN_LOADED);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004408 }
4409
4410 handshake[octeon_dev->octeon_id].init_ok = 1;
4411 complete(&handshake[octeon_dev->octeon_id].init);
4412
4413 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4414
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004415 return 0;
4416}
4417
4418/**
Rick Farringtonda1542b2017-08-11 18:43:14 -07004419 * \brief Debug console print function
4420 * @param octeon_dev octeon device
4421 * @param console_num console number
4422 * @param prefix first portion of line to display
4423 * @param suffix second portion of line to display
4424 *
4425 * The OCTEON debug console outputs entire lines (excluding '\n').
4426 * Normally, the line will be passed in the 'prefix' parameter.
4427 * However, due to buffering, it is possible for a line to be split into two
4428 * parts, in which case they will be passed as the 'prefix' parameter and
4429 * 'suffix' parameter.
4430 */
4431static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
4432 char *prefix, char *suffix)
4433{
4434 if (prefix && suffix)
4435 dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
4436 suffix);
4437 else if (prefix)
4438 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
4439 else if (suffix)
4440 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
4441
4442 return 0;
4443}
4444
4445/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004446 * \brief Exits the module
4447 */
4448static void __exit liquidio_exit(void)
4449{
4450 liquidio_deinit_pci();
4451
4452 pr_info("LiquidIO network module is now unloaded\n");
4453}
4454
4455module_init(liquidio_init);
4456module_exit(liquidio_exit);