blob: 8ec0b6d978d2c896d9dbb67a34cb0e0d39b9476e [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
Russell Kinge3bfc6e2017-02-07 15:03:03 -080018#include <linux/module.h>
Florian Westphal282ccf62017-03-29 17:17:31 +020019#include <linux/interrupt.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070020#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070021#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070022#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070023#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include "liquidio_common.h"
25#include "octeon_droq.h"
26#include "octeon_iq.h"
27#include "response_manager.h"
28#include "octeon_device.h"
29#include "octeon_nic.h"
30#include "octeon_main.h"
31#include "octeon_network.h"
32#include "cn66xx_regs.h"
33#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070034#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070035#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070036#include "liquidio_image.h"
37
38MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
39MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
40MODULE_LICENSE("GPL");
41MODULE_VERSION(LIQUIDIO_VERSION);
Derek Chicklesea6404c82017-08-07 12:22:15 -070042MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
43 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
44MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
45 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
47 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
48MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
49 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070050
51static int ddr_timeout = 10000;
52module_param(ddr_timeout, int, 0644);
53MODULE_PARM_DESC(ddr_timeout,
54 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
55
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070056#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
57
58static int debug = -1;
59module_param(debug, int, 0644);
60MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
61
62static char fw_type[LIO_MAX_FW_TYPE_LEN];
63module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
64MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
65
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070066static u32 console_bitmask;
67module_param(console_bitmask, int, 0644);
68MODULE_PARM_DESC(console_bitmask,
69 "Bitmask indicating which consoles have debug output redirected to syslog.");
70
71/**
72 * \brief determines if a given console has debug enabled.
73 * @param console console to check
74 * @returns 1 = enabled. 0 otherwise
75 */
Rick Farringtonda1542b2017-08-11 18:43:14 -070076static int octeon_console_debug_enabled(u32 console)
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070077{
78 return (console_bitmask >> (console)) & 0x1;
79}
80
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070081/* Polling interval for determining when NIC application is alive */
82#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
83
84/* runtime link query interval */
85#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
86
87struct liquidio_if_cfg_context {
88 int octeon_id;
89
90 wait_queue_head_t wc;
91
92 int cond;
93};
94
95struct liquidio_if_cfg_resp {
96 u64 rh;
97 struct liquidio_if_cfg_info cfg_info;
98 u64 status;
99};
100
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -0700101struct liquidio_rx_ctl_context {
102 int octeon_id;
103
104 wait_queue_head_t wc;
105
106 int cond;
107};
108
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700109struct oct_link_status_resp {
110 u64 rh;
111 struct oct_link_info link_info;
112 u64 status;
113};
114
115struct oct_timestamp_resp {
116 u64 rh;
117 u64 timestamp;
118 u64 status;
119};
120
121#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
122
123union tx_info {
124 u64 u64;
125 struct {
126#ifdef __BIG_ENDIAN_BITFIELD
127 u16 gso_size;
128 u16 gso_segs;
129 u32 reserved;
130#else
131 u32 reserved;
132 u16 gso_segs;
133 u16 gso_size;
134#endif
135 } s;
136};
137
138/** Octeon device properties to be used by the NIC module.
139 * Each octeon device in the system will be represented
140 * by this structure in the NIC module.
141 */
142
143#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
144
145#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700146#define OCTNIC_GSO_MAX_SIZE \
147 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700148
149/** Structure of a node in list of gather components maintained by
150 * NIC driver for each network device.
151 */
152struct octnic_gather {
153 /** List manipulation. Next and prev pointers. */
154 struct list_head list;
155
156 /** Size of the gather component at sg in bytes. */
157 int sg_size;
158
159 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
160 int adjust;
161
162 /** Gather component that can accommodate max sized fragment list
163 * received from the IP layer.
164 */
165 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700166
VSR Burru67e303e2017-03-06 18:45:59 -0800167 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700168};
169
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700170struct handshake {
171 struct completion init;
172 struct completion started;
173 struct pci_dev *pci_dev;
174 int init_ok;
175 int started_ok;
176};
177
178struct octeon_device_priv {
179 /** Tasklet structures for this device. */
180 struct tasklet_struct droq_tasklet;
181 unsigned long napi_mask;
182};
183
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800184#ifdef CONFIG_PCI_IOV
185static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
186#endif
187
Rick Farringtonda1542b2017-08-11 18:43:14 -0700188static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
189 char *prefix, char *suffix);
190
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700191static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700192static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700193static void liquidio_remove(struct pci_dev *pdev);
194static int liquidio_probe(struct pci_dev *pdev,
195 const struct pci_device_id *ent);
Felix Manlunasbb54be52017-04-04 19:26:57 -0700196static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
197 int linkstate);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700198
199static struct handshake handshake[MAX_OCTEON_DEVICES];
200static struct completion first_stage;
201
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700202static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700203{
204 int q_no;
205 int reschedule = 0;
206 struct octeon_device *oct = (struct octeon_device *)pdev;
207 struct octeon_device_priv *oct_priv =
208 (struct octeon_device_priv *)oct->priv;
209
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700210 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800211 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700212 continue;
213 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
214 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700215 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700216
217 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
218 /* set time and cnt interrupt thresholds for this DROQ
219 * for NAPI
220 */
221 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
222
223 octeon_write_csr64(
224 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
225 0x5700000040ULL);
226 octeon_write_csr64(
227 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
228 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700229 }
230
231 if (reschedule)
232 tasklet_schedule(&oct_priv->droq_tasklet);
233}
234
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700235static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700236{
237 struct octeon_device_priv *oct_priv =
238 (struct octeon_device_priv *)oct->priv;
239 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
240 int i;
241
242 do {
243 pending_pkts = 0;
244
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700245 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800246 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700247 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700248 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700249 }
250 if (pkt_cnt > 0) {
251 pending_pkts += pkt_cnt;
252 tasklet_schedule(&oct_priv->droq_tasklet);
253 }
254 pkt_cnt = 0;
255 schedule_timeout_uninterruptible(1);
256
257 } while (retry-- && pending_pkts);
258
259 return pkt_cnt;
260}
261
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700262/**
263 * \brief Forces all IO queues off on a given device
264 * @param oct Pointer to Octeon device
265 */
266static void force_io_queues_off(struct octeon_device *oct)
267{
268 if ((oct->chip_id == OCTEON_CN66XX) ||
269 (oct->chip_id == OCTEON_CN68XX)) {
270 /* Reset the Enable bits for Input Queues. */
271 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
272
273 /* Reset the Enable bits for Output Queues. */
274 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
275 }
276}
277
278/**
279 * \brief wait for all pending requests to complete
280 * @param oct Pointer to Octeon device
281 *
282 * Called during shutdown sequence
283 */
284static int wait_for_pending_requests(struct octeon_device *oct)
285{
286 int i, pcount = 0;
287
288 for (i = 0; i < 100; i++) {
289 pcount =
290 atomic_read(&oct->response_list
291 [OCTEON_ORDERED_SC_LIST].pending_req_count);
292 if (pcount)
293 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700294 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700295 break;
296 }
297
298 if (pcount)
299 return 1;
300
301 return 0;
302}
303
304/**
305 * \brief Cause device to go quiet so it can be safely removed/reset/etc
306 * @param oct Pointer to Octeon device
307 */
308static inline void pcierror_quiesce_device(struct octeon_device *oct)
309{
310 int i;
311
312 /* Disable the input and output queues now. No more packets will
313 * arrive from Octeon, but we should wait for all packet processing
314 * to finish.
315 */
316 force_io_queues_off(oct);
317
318 /* To allow for in-flight requests */
319 schedule_timeout_uninterruptible(100);
320
321 if (wait_for_pending_requests(oct))
322 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
323
324 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700325 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700326 struct octeon_instr_queue *iq;
327
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800328 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700329 continue;
330 iq = oct->instr_queue[i];
331
332 if (atomic_read(&iq->instr_pending)) {
333 spin_lock_bh(&iq->lock);
334 iq->fill_cnt = 0;
335 iq->octeon_read_index = iq->host_write_index;
336 iq->stats.instr_processed +=
337 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700338 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700339 spin_unlock_bh(&iq->lock);
340 }
341 }
342
343 /* Force all pending ordered list requests to time out. */
344 lio_process_ordered_list(oct, 1);
345
346 /* We do not need to wait for output queue packets to be processed. */
347}
348
349/**
350 * \brief Cleanup PCI AER uncorrectable error status
351 * @param dev Pointer to PCI device
352 */
353static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
354{
355 int pos = 0x100;
356 u32 status, mask;
357
358 pr_info("%s :\n", __func__);
359
360 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
361 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
362 if (dev->error_state == pci_channel_io_normal)
363 status &= ~mask; /* Clear corresponding nonfatal bits */
364 else
365 status &= mask; /* Clear corresponding fatal bits */
366 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
367}
368
369/**
370 * \brief Stop all PCI IO to a given device
371 * @param dev Pointer to Octeon device
372 */
373static void stop_pci_io(struct octeon_device *oct)
374{
375 /* No more instructions will be forwarded. */
376 atomic_set(&oct->status, OCT_DEV_IN_RESET);
377
378 pci_disable_device(oct->pci_dev);
379
380 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700381 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700382
383 pcierror_quiesce_device(oct);
384
385 /* Release the interrupt line */
386 free_irq(oct->pci_dev->irq, oct);
387
388 if (oct->flags & LIO_FLAG_MSI_ENABLED)
389 pci_disable_msi(oct->pci_dev);
390
391 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
392 lio_get_state_string(&oct->status));
393
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700394 /* making it a common function for all OCTEON models */
395 cleanup_aer_uncorrect_error_status(oct->pci_dev);
396}
397
398/**
399 * \brief called when PCI error is detected
400 * @param pdev Pointer to PCI device
401 * @param state The current pci connection state
402 *
403 * This function is called after a PCI bus error affecting
404 * this device has been detected.
405 */
406static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
407 pci_channel_state_t state)
408{
409 struct octeon_device *oct = pci_get_drvdata(pdev);
410
411 /* Non-correctable Non-fatal errors */
412 if (state == pci_channel_io_normal) {
413 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
414 cleanup_aer_uncorrect_error_status(oct->pci_dev);
415 return PCI_ERS_RESULT_CAN_RECOVER;
416 }
417
418 /* Non-correctable Fatal errors */
419 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
420 stop_pci_io(oct);
421
422 /* Always return a DISCONNECT. There is no support for recovery but only
423 * for a clean shutdown.
424 */
425 return PCI_ERS_RESULT_DISCONNECT;
426}
427
428/**
429 * \brief mmio handler
430 * @param pdev Pointer to PCI device
431 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700432static pci_ers_result_t liquidio_pcie_mmio_enabled(
433 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700434{
435 /* We should never hit this since we never ask for a reset for a Fatal
436 * Error. We always return DISCONNECT in io_error above.
437 * But play safe and return RECOVERED for now.
438 */
439 return PCI_ERS_RESULT_RECOVERED;
440}
441
442/**
443 * \brief called after the pci bus has been reset.
444 * @param pdev Pointer to PCI device
445 *
446 * Restart the card from scratch, as if from a cold-boot. Implementation
447 * resembles the first-half of the octeon_resume routine.
448 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700449static pci_ers_result_t liquidio_pcie_slot_reset(
450 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700451{
452 /* We should never hit this since we never ask for a reset for a Fatal
453 * Error. We always return DISCONNECT in io_error above.
454 * But play safe and return RECOVERED for now.
455 */
456 return PCI_ERS_RESULT_RECOVERED;
457}
458
459/**
460 * \brief called when traffic can start flowing again.
461 * @param pdev Pointer to PCI device
462 *
463 * This callback is called when the error recovery driver tells us that
464 * its OK to resume normal operation. Implementation resembles the
465 * second-half of the octeon_resume routine.
466 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700467static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700468{
469 /* Nothing to be done here. */
470}
471
472#ifdef CONFIG_PM
473/**
474 * \brief called when suspending
475 * @param pdev Pointer to PCI device
476 * @param state state to suspend to
477 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700478static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
479 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700480{
481 return 0;
482}
483
484/**
485 * \brief called when resuming
486 * @param pdev Pointer to PCI device
487 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700488static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700489{
490 return 0;
491}
492#endif
493
494/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100495static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700496 .error_detected = liquidio_pcie_error_detected,
497 .mmio_enabled = liquidio_pcie_mmio_enabled,
498 .slot_reset = liquidio_pcie_slot_reset,
499 .resume = liquidio_pcie_resume,
500};
501
502static const struct pci_device_id liquidio_pci_tbl[] = {
503 { /* 68xx */
504 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
505 },
506 { /* 66xx */
507 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
508 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700509 { /* 23xx pf */
510 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
511 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700512 {
513 0, 0, 0, 0, 0, 0, 0
514 }
515};
516MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
517
518static struct pci_driver liquidio_pci_driver = {
519 .name = "LiquidIO",
520 .id_table = liquidio_pci_tbl,
521 .probe = liquidio_probe,
522 .remove = liquidio_remove,
523 .err_handler = &liquidio_err_handler, /* For AER */
524
525#ifdef CONFIG_PM
526 .suspend = liquidio_suspend,
527 .resume = liquidio_resume,
528#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800529#ifdef CONFIG_PCI_IOV
530 .sriov_configure = liquidio_enable_sriov,
531#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700532};
533
534/**
535 * \brief register PCI driver
536 */
537static int liquidio_init_pci(void)
538{
539 return pci_register_driver(&liquidio_pci_driver);
540}
541
542/**
543 * \brief unregister PCI driver
544 */
545static void liquidio_deinit_pci(void)
546{
547 pci_unregister_driver(&liquidio_pci_driver);
548}
549
550/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700551 * \brief Stop Tx queues
552 * @param netdev network device
553 */
554static inline void txqs_stop(struct net_device *netdev)
555{
556 if (netif_is_multiqueue(netdev)) {
557 int i;
558
559 for (i = 0; i < netdev->num_tx_queues; i++)
560 netif_stop_subqueue(netdev, i);
561 } else {
562 netif_stop_queue(netdev);
563 }
564}
565
566/**
567 * \brief Start Tx queues
568 * @param netdev network device
569 */
570static inline void txqs_start(struct net_device *netdev)
571{
572 if (netif_is_multiqueue(netdev)) {
573 int i;
574
575 for (i = 0; i < netdev->num_tx_queues; i++)
576 netif_start_subqueue(netdev, i);
577 } else {
578 netif_start_queue(netdev);
579 }
580}
581
582/**
583 * \brief Wake Tx queues
584 * @param netdev network device
585 */
586static inline void txqs_wake(struct net_device *netdev)
587{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700588 struct lio *lio = GET_LIO(netdev);
589
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700590 if (netif_is_multiqueue(netdev)) {
591 int i;
592
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700593 for (i = 0; i < netdev->num_tx_queues; i++) {
594 int qno = lio->linfo.txpciq[i %
595 (lio->linfo.num_txpciq)].s.q_no;
596
597 if (__netif_subqueue_stopped(netdev, i)) {
598 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
599 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700600 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700601 }
602 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700603 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700604 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
605 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700606 netif_wake_queue(netdev);
607 }
608}
609
610/**
611 * \brief Stop Tx queue
612 * @param netdev network device
613 */
614static void stop_txq(struct net_device *netdev)
615{
616 txqs_stop(netdev);
617}
618
619/**
620 * \brief Start Tx queue
621 * @param netdev network device
622 */
623static void start_txq(struct net_device *netdev)
624{
625 struct lio *lio = GET_LIO(netdev);
626
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700627 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700628 txqs_start(netdev);
629 return;
630 }
631}
632
633/**
634 * \brief Wake a queue
635 * @param netdev network device
636 * @param q which queue to wake
637 */
638static inline void wake_q(struct net_device *netdev, int q)
639{
640 if (netif_is_multiqueue(netdev))
641 netif_wake_subqueue(netdev, q);
642 else
643 netif_wake_queue(netdev);
644}
645
646/**
647 * \brief Stop a queue
648 * @param netdev network device
649 * @param q which queue to stop
650 */
651static inline void stop_q(struct net_device *netdev, int q)
652{
653 if (netif_is_multiqueue(netdev))
654 netif_stop_subqueue(netdev, q);
655 else
656 netif_stop_queue(netdev);
657}
658
659/**
660 * \brief Check Tx queue status, and take appropriate action
661 * @param lio per-network private data
662 * @returns 0 if full, number of queues woken up otherwise
663 */
664static inline int check_txq_status(struct lio *lio)
665{
666 int ret_val = 0;
667
668 if (netif_is_multiqueue(lio->netdev)) {
669 int numqs = lio->netdev->num_tx_queues;
670 int q, iq = 0;
671
672 /* check each sub-queue state */
673 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700674 iq = lio->linfo.txpciq[q %
675 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700676 if (octnet_iq_is_full(lio->oct_dev, iq))
677 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700678 if (__netif_subqueue_stopped(lio->netdev, q)) {
679 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700680 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
681 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700682 ret_val++;
683 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700684 }
685 } else {
686 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
687 return 0;
688 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700689 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
690 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700691 ret_val = 1;
692 }
693 return ret_val;
694}
695
696/**
697 * Remove the node at the head of the list. The list would be empty at
698 * the end of this call if there are no more nodes in the list.
699 */
700static inline struct list_head *list_delete_head(struct list_head *root)
701{
702 struct list_head *node;
703
704 if ((root->prev == root) && (root->next == root))
705 node = NULL;
706 else
707 node = root->next;
708
709 if (node)
710 list_del(node);
711
712 return node;
713}
714
715/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700716 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700717 * @param lio per-network private data
718 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700719static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700720{
721 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700722 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700723
VSR Burru67e303e2017-03-06 18:45:59 -0800724 kfree(lio->glist_lock);
725 lio->glist_lock = NULL;
726
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700727 if (!lio->glist)
728 return;
729
730 for (i = 0; i < lio->linfo.num_txpciq; i++) {
731 do {
732 g = (struct octnic_gather *)
733 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800734 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700735 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700736 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800737
Felix Manlunas58ad3192017-03-20 19:04:48 -0700738 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
739 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800740 lio_dma_free(lio->oct_dev,
741 lio->glist_entry_size * lio->tx_qsize,
742 lio->glists_virt_base[i],
743 lio->glists_dma_base[i]);
744 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700745 }
746
VSR Burru67e303e2017-03-06 18:45:59 -0800747 kfree(lio->glists_virt_base);
748 lio->glists_virt_base = NULL;
749
750 kfree(lio->glists_dma_base);
751 lio->glists_dma_base = NULL;
752
753 kfree(lio->glist);
754 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700755}
756
757/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700758 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700759 * @param lio per-network private data
760 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700761static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700762{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700763 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700764 struct octnic_gather *g;
765
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700766 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
767 GFP_KERNEL);
768 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800769 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700770
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700771 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
772 GFP_KERNEL);
773 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800774 kfree(lio->glist_lock);
775 lio->glist_lock = NULL;
776 return -ENOMEM;
777 }
778
779 lio->glist_entry_size =
780 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
781
782 /* allocate memory to store virtual and dma base address of
783 * per glist consistent memory
784 */
785 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
786 GFP_KERNEL);
787 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
788 GFP_KERNEL);
789
790 if (!lio->glists_virt_base || !lio->glists_dma_base) {
791 delete_glists(lio);
792 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700793 }
794
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700795 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800796 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700797
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700798 spin_lock_init(&lio->glist_lock[i]);
799
800 INIT_LIST_HEAD(&lio->glist[i]);
801
VSR Burru67e303e2017-03-06 18:45:59 -0800802 lio->glists_virt_base[i] =
803 lio_dma_alloc(oct,
804 lio->glist_entry_size * lio->tx_qsize,
805 &lio->glists_dma_base[i]);
806
807 if (!lio->glists_virt_base[i]) {
808 delete_glists(lio);
809 return -ENOMEM;
810 }
811
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700812 for (j = 0; j < lio->tx_qsize; j++) {
813 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
814 numa_node);
815 if (!g)
816 g = kzalloc(sizeof(*g), GFP_KERNEL);
817 if (!g)
818 break;
819
VSR Burru67e303e2017-03-06 18:45:59 -0800820 g->sg = lio->glists_virt_base[i] +
821 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700822
VSR Burru67e303e2017-03-06 18:45:59 -0800823 g->sg_dma_ptr = lio->glists_dma_base[i] +
824 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700825
826 list_add_tail(&g->list, &lio->glist[i]);
827 }
828
829 if (j != lio->tx_qsize) {
830 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800831 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700832 }
833 }
834
835 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700836}
837
838/**
839 * \brief Print link information
840 * @param netdev network device
841 */
842static void print_link_info(struct net_device *netdev)
843{
844 struct lio *lio = GET_LIO(netdev);
845
846 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
847 struct oct_link_info *linfo = &lio->linfo;
848
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700849 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700850 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
851 linfo->link.s.speed,
852 (linfo->link.s.duplex) ? "Full" : "Half");
853 } else {
854 netif_info(lio, link, lio->netdev, "Link Down\n");
855 }
856 }
857}
858
859/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700860 * \brief Routine to notify MTU change
861 * @param work work_struct data structure
862 */
863static void octnet_link_status_change(struct work_struct *work)
864{
865 struct cavium_wk *wk = (struct cavium_wk *)work;
866 struct lio *lio = (struct lio *)wk->ctxptr;
867
868 rtnl_lock();
869 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
870 rtnl_unlock();
871}
872
873/**
874 * \brief Sets up the mtu status change work
875 * @param netdev network device
876 */
877static inline int setup_link_status_change_wq(struct net_device *netdev)
878{
879 struct lio *lio = GET_LIO(netdev);
880 struct octeon_device *oct = lio->oct_dev;
881
882 lio->link_status_wq.wq = alloc_workqueue("link-status",
883 WQ_MEM_RECLAIM, 0);
884 if (!lio->link_status_wq.wq) {
885 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
886 return -1;
887 }
888 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
889 octnet_link_status_change);
890 lio->link_status_wq.wk.ctxptr = lio;
891
892 return 0;
893}
894
895static inline void cleanup_link_status_change_wq(struct net_device *netdev)
896{
897 struct lio *lio = GET_LIO(netdev);
898
899 if (lio->link_status_wq.wq) {
900 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
901 destroy_workqueue(lio->link_status_wq.wq);
902 }
903}
904
905/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700906 * \brief Update link status
907 * @param netdev network device
908 * @param ls link status structure
909 *
910 * Called on receipt of a link status response from the core application to
911 * update each interface's link status.
912 */
913static inline void update_link_status(struct net_device *netdev,
914 union oct_link_status *ls)
915{
916 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700917 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700918
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700919 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700920
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700921 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700922 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700923 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700924
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700925 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700926 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700927 txqs_wake(netdev);
928 } else {
929 netif_carrier_off(netdev);
930 stop_txq(netdev);
931 }
932 }
933}
934
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700935/* Runs in interrupt context. */
936static void update_txq_status(struct octeon_device *oct, int iq_num)
937{
938 struct net_device *netdev;
939 struct lio *lio;
940 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
941
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700942 netdev = oct->props[iq->ifidx].netdev;
943
944 /* This is needed because the first IQ does not have
945 * a netdev associated with it.
946 */
947 if (!netdev)
948 return;
949
950 lio = GET_LIO(netdev);
951 if (netif_is_multiqueue(netdev)) {
952 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
953 lio->linfo.link.s.link_up &&
954 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700955 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
956 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700957 netif_wake_subqueue(netdev, iq->q_index);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700958 }
VSR Burru6069f3f2017-03-22 11:54:50 -0700959 } else if (netif_queue_stopped(netdev) &&
960 lio->linfo.link.s.link_up &&
961 (!octnet_iq_is_full(oct, lio->txq))) {
962 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
963 lio->txq, tx_restart, 1);
964 netif_wake_queue(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700965 }
966}
967
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700968static
969int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
970{
971 struct octeon_device *oct = droq->oct_dev;
972 struct octeon_device_priv *oct_priv =
973 (struct octeon_device_priv *)oct->priv;
974
975 if (droq->ops.poll_mode) {
976 droq->ops.napi_fn(droq);
977 } else {
978 if (ret & MSIX_PO_INT) {
979 tasklet_schedule(&oct_priv->droq_tasklet);
980 return 1;
981 }
982 /* this will be flushed periodically by check iq db */
983 if (ret & MSIX_PI_INT)
984 return 0;
985 }
986 return 0;
987}
988
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700989/**
990 * \brief Droq packet processor sceduler
991 * @param oct octeon device
992 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700993static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700994{
995 struct octeon_device_priv *oct_priv =
996 (struct octeon_device_priv *)oct->priv;
997 u64 oq_no;
998 struct octeon_droq *droq;
999
1000 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001001 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
1002 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001003 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001004 continue;
1005
1006 droq = oct->droq[oq_no];
1007
1008 if (droq->ops.poll_mode) {
1009 droq->ops.napi_fn(droq);
1010 oct_priv->napi_mask |= (1 << oq_no);
1011 } else {
1012 tasklet_schedule(&oct_priv->droq_tasklet);
1013 }
1014 }
1015 }
1016}
1017
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001018static irqreturn_t
1019liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1020{
1021 u64 ret;
1022 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1023 struct octeon_device *oct = ioq_vector->oct_dev;
1024 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1025
1026 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1027
1028 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1029 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1030
1031 return IRQ_HANDLED;
1032}
1033
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001034/**
1035 * \brief Interrupt handler for octeon
1036 * @param irq unused
1037 * @param dev octeon device
1038 */
1039static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001040irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1041 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001042{
1043 struct octeon_device *oct = (struct octeon_device *)dev;
1044 irqreturn_t ret;
1045
1046 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001047 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001048
1049 ret = oct->fn_list.process_interrupt_regs(oct);
1050
1051 if (ret == IRQ_HANDLED)
1052 liquidio_schedule_droq_pkt_handlers(oct);
1053
1054 /* Re-enable our interrupts */
1055 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001056 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001057
1058 return ret;
1059}
1060
1061/**
1062 * \brief Setup interrupt for octeon device
1063 * @param oct octeon device
1064 *
1065 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1066 */
1067static int octeon_setup_interrupt(struct octeon_device *oct)
1068{
1069 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001070 struct msix_entry *msix_entries;
1071 int i;
1072 int num_ioq_vectors;
1073 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001074 char *queue_irq_names = NULL;
1075 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001076
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001077 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1078 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1079 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1080 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001081
Rick Farrington0c88a762017-03-13 12:58:04 -07001082 /* allocate storage for the names assigned to each irq */
1083 oct->irq_name_storage =
1084 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1085 GFP_KERNEL);
1086 if (!oct->irq_name_storage) {
1087 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1088 return -ENOMEM;
1089 }
1090
1091 queue_irq_names = oct->irq_name_storage;
1092 aux_irq_name = &queue_irq_names
1093 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1094
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001095 oct->msix_entries = kcalloc(
1096 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001097 if (!oct->msix_entries) {
1098 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1099 kfree(oct->irq_name_storage);
1100 oct->irq_name_storage = NULL;
1101 return -ENOMEM;
1102 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001103
1104 msix_entries = (struct msix_entry *)oct->msix_entries;
1105 /*Assumption is that pf msix vectors start from pf srn to pf to
1106 * trs and not from 0. if not change this code
1107 */
1108 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1109 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1110 msix_entries[oct->num_msix_irqs - 1].entry =
1111 oct->sriov_info.trs;
1112 num_alloc_ioq_vectors = pci_enable_msix_range(
1113 oct->pci_dev, msix_entries,
1114 oct->num_msix_irqs,
1115 oct->num_msix_irqs);
1116 if (num_alloc_ioq_vectors < 0) {
1117 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1118 kfree(oct->msix_entries);
1119 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001120 kfree(oct->irq_name_storage);
1121 oct->irq_name_storage = NULL;
1122 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001123 }
1124 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1125
1126 num_ioq_vectors = oct->num_msix_irqs;
1127
1128 /** For PF, there is one non-ioq interrupt handler */
1129 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001130
1131 snprintf(aux_irq_name, INTRNAMSIZ,
1132 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001133 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001134 liquidio_legacy_intr_handler, 0,
1135 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001136 if (irqret) {
1137 dev_err(&oct->pci_dev->dev,
1138 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1139 irqret);
1140 pci_disable_msix(oct->pci_dev);
1141 kfree(oct->msix_entries);
1142 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001143 kfree(oct->irq_name_storage);
1144 oct->irq_name_storage = NULL;
1145 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001146 }
1147
1148 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001149 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1150 "LiquidIO%u-pf%u-rxtx-%u",
1151 oct->octeon_id, oct->pf_num, i);
1152
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001153 irqret = request_irq(msix_entries[i].vector,
1154 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001155 &queue_irq_names[IRQ_NAME_OFF(i)],
1156 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001157 if (irqret) {
1158 dev_err(&oct->pci_dev->dev,
1159 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1160 irqret);
1161 /** Freeing the non-ioq irq vector here . */
1162 free_irq(msix_entries[num_ioq_vectors].vector,
1163 oct);
1164
1165 while (i) {
1166 i--;
1167 /** clearing affinity mask. */
1168 irq_set_affinity_hint(
1169 msix_entries[i].vector, NULL);
1170 free_irq(msix_entries[i].vector,
1171 &oct->ioq_vector[i]);
1172 }
1173 pci_disable_msix(oct->pci_dev);
1174 kfree(oct->msix_entries);
1175 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001176 kfree(oct->irq_name_storage);
1177 oct->irq_name_storage = NULL;
1178 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001179 }
1180 oct->ioq_vector[i].vector = msix_entries[i].vector;
1181 /* assign the cpu mask for this msix interrupt vector */
1182 irq_set_affinity_hint(
1183 msix_entries[i].vector,
1184 (&oct->ioq_vector[i].affinity_mask));
1185 }
1186 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1187 oct->octeon_id);
1188 } else {
1189 err = pci_enable_msi(oct->pci_dev);
1190 if (err)
1191 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1192 err);
1193 else
1194 oct->flags |= LIO_FLAG_MSI_ENABLED;
1195
Rick Farrington0c88a762017-03-13 12:58:04 -07001196 /* allocate storage for the names assigned to the irq */
1197 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1198 if (!oct->irq_name_storage)
1199 return -ENOMEM;
1200
1201 queue_irq_names = oct->irq_name_storage;
1202
1203 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1204 "LiquidIO%u-pf%u-rxtx-%u",
1205 oct->octeon_id, oct->pf_num, 0);
1206
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001207 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001208 liquidio_legacy_intr_handler,
1209 IRQF_SHARED,
1210 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001211 if (irqret) {
1212 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1213 pci_disable_msi(oct->pci_dev);
1214 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1215 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001216 kfree(oct->irq_name_storage);
1217 oct->irq_name_storage = NULL;
1218 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001219 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001220 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001221 return 0;
1222}
1223
Felix Manlunasbb54be52017-04-04 19:26:57 -07001224static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
1225{
1226 struct octeon_device *other_oct;
1227
1228 other_oct = lio_get_device(oct->octeon_id + 1);
1229
1230 if (other_oct && other_oct->pci_dev) {
1231 int oct_busnum, other_oct_busnum;
1232
1233 oct_busnum = oct->pci_dev->bus->number;
1234 other_oct_busnum = other_oct->pci_dev->bus->number;
1235
1236 if (oct_busnum == other_oct_busnum) {
1237 int oct_slot, other_oct_slot;
1238
1239 oct_slot = PCI_SLOT(oct->pci_dev->devfn);
1240 other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
1241
1242 if (oct_slot == other_oct_slot)
1243 return other_oct;
1244 }
1245 }
1246
1247 return NULL;
1248}
1249
1250static void disable_all_vf_links(struct octeon_device *oct)
1251{
1252 struct net_device *netdev;
1253 int max_vfs, vf, i;
1254
1255 if (!oct)
1256 return;
1257
1258 max_vfs = oct->sriov_info.max_vfs;
1259
1260 for (i = 0; i < oct->ifcount; i++) {
1261 netdev = oct->props[i].netdev;
1262 if (!netdev)
1263 continue;
1264
1265 for (vf = 0; vf < max_vfs; vf++)
1266 liquidio_set_vf_link_state(netdev, vf,
1267 IFLA_VF_LINK_STATE_DISABLE);
1268 }
1269}
1270
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001271static int liquidio_watchdog(void *param)
1272{
Felix Manlunasbb54be52017-04-04 19:26:57 -07001273 bool err_msg_was_printed[LIO_MAX_CORES];
1274 u16 mask_of_crashed_or_stuck_cores = 0;
1275 bool all_vf_links_are_disabled = false;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001276 struct octeon_device *oct = param;
Felix Manlunasbb54be52017-04-04 19:26:57 -07001277 struct octeon_device *other_oct;
1278#ifdef CONFIG_MODULE_UNLOAD
1279 long refcount, vfs_referencing_pf;
1280 u64 vfs_mask1, vfs_mask2;
1281#endif
1282 int core;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001283
Felix Manlunasbb54be52017-04-04 19:26:57 -07001284 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001285
1286 while (!kthread_should_stop()) {
Felix Manlunasbb54be52017-04-04 19:26:57 -07001287 /* sleep for a couple of seconds so that we don't hog the CPU */
1288 set_current_state(TASK_INTERRUPTIBLE);
1289 schedule_timeout(msecs_to_jiffies(2000));
1290
1291 mask_of_crashed_or_stuck_cores =
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001292 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1293
Felix Manlunasbb54be52017-04-04 19:26:57 -07001294 if (!mask_of_crashed_or_stuck_cores)
1295 continue;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001296
Felix Manlunasbb54be52017-04-04 19:26:57 -07001297 WRITE_ONCE(oct->cores_crashed, true);
1298 other_oct = get_other_octeon_device(oct);
1299 if (other_oct)
1300 WRITE_ONCE(other_oct->cores_crashed, true);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001301
Felix Manlunasbb54be52017-04-04 19:26:57 -07001302 for (core = 0; core < LIO_MAX_CORES; core++) {
1303 bool core_crashed_or_got_stuck;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001304
Felix Manlunasbb54be52017-04-04 19:26:57 -07001305 core_crashed_or_got_stuck =
1306 (mask_of_crashed_or_stuck_cores
1307 >> core) & 1;
1308
1309 if (core_crashed_or_got_stuck &&
1310 !err_msg_was_printed[core]) {
1311 dev_err(&oct->pci_dev->dev,
1312 "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
1313 core);
1314 err_msg_was_printed[core] = true;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001315 }
1316 }
1317
Felix Manlunasbb54be52017-04-04 19:26:57 -07001318 if (all_vf_links_are_disabled)
1319 continue;
1320
1321 disable_all_vf_links(oct);
1322 disable_all_vf_links(other_oct);
1323 all_vf_links_are_disabled = true;
1324
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001325#ifdef CONFIG_MODULE_UNLOAD
Felix Manlunasbb54be52017-04-04 19:26:57 -07001326 vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1327 vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001328
Felix Manlunasbb54be52017-04-04 19:26:57 -07001329 vfs_referencing_pf = hweight64(vfs_mask1);
1330 vfs_referencing_pf += hweight64(vfs_mask2);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001331
Felix Manlunasbb54be52017-04-04 19:26:57 -07001332 refcount = module_refcount(THIS_MODULE);
1333 if (refcount >= vfs_referencing_pf) {
1334 while (vfs_referencing_pf) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001335 module_put(THIS_MODULE);
Felix Manlunasbb54be52017-04-04 19:26:57 -07001336 vfs_referencing_pf--;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001337 }
1338 }
1339#endif
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001340 }
1341
1342 return 0;
1343}
1344
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001345/**
1346 * \brief PCI probe handler
1347 * @param pdev PCI device structure
1348 * @param ent unused
1349 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001350static int
1351liquidio_probe(struct pci_dev *pdev,
1352 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001353{
1354 struct octeon_device *oct_dev = NULL;
1355 struct handshake *hs;
1356
1357 oct_dev = octeon_allocate_device(pdev->device,
1358 sizeof(struct octeon_device_priv));
1359 if (!oct_dev) {
1360 dev_err(&pdev->dev, "Unable to allocate device\n");
1361 return -ENOMEM;
1362 }
1363
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001364 if (pdev->device == OCTEON_CN23XX_PF_VID)
1365 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1366
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07001367 /* Enable PTP for 6XXX Device */
1368 if (((pdev->device == OCTEON_CN66XX) ||
1369 (pdev->device == OCTEON_CN68XX)))
1370 oct_dev->ptp_enable = true;
1371 else
1372 oct_dev->ptp_enable = false;
1373
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001374 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1375 (u32)pdev->vendor, (u32)pdev->device);
1376
1377 /* Assign octeon_device for this device to the private data area. */
1378 pci_set_drvdata(pdev, oct_dev);
1379
1380 /* set linux specific device pointer */
1381 oct_dev->pci_dev = (void *)pdev;
1382
1383 hs = &handshake[oct_dev->octeon_id];
1384 init_completion(&hs->init);
1385 init_completion(&hs->started);
1386 hs->pci_dev = pdev;
1387
1388 if (oct_dev->octeon_id == 0)
1389 /* first LiquidIO NIC is detected */
1390 complete(&first_stage);
1391
1392 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001393 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001394 liquidio_remove(pdev);
1395 return -ENOMEM;
1396 }
1397
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001398 if (OCTEON_CN23XX_PF(oct_dev)) {
1399 u64 scratch1;
1400 u8 bus, device, function;
1401
1402 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1403 if (!(scratch1 & 4ULL)) {
1404 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1405 * the lio watchdog kernel thread is running for this
1406 * NIC. Each NIC gets one watchdog kernel thread.
1407 */
1408 scratch1 |= 4ULL;
1409 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1410 scratch1);
1411
1412 bus = pdev->bus->number;
1413 device = PCI_SLOT(pdev->devfn);
1414 function = PCI_FUNC(pdev->devfn);
1415 oct_dev->watchdog_task = kthread_create(
1416 liquidio_watchdog, oct_dev,
1417 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001418 if (!IS_ERR(oct_dev->watchdog_task)) {
1419 wake_up_process(oct_dev->watchdog_task);
1420 } else {
1421 oct_dev->watchdog_task = NULL;
1422 dev_err(&oct_dev->pci_dev->dev,
1423 "failed to create kernel_thread\n");
1424 liquidio_remove(pdev);
1425 return -1;
1426 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001427 }
1428 }
1429
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001430 oct_dev->rx_pause = 1;
1431 oct_dev->tx_pause = 1;
1432
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001433 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1434
1435 return 0;
1436}
1437
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001438static bool fw_type_is_none(void)
1439{
1440 return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1441 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
1442}
1443
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001444/**
1445 *\brief Destroy resources associated with octeon device
1446 * @param pdev PCI device structure
1447 * @param ent unused
1448 */
1449static void octeon_destroy_resources(struct octeon_device *oct)
1450{
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001451 int i, refcount;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001452 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001453 struct octeon_device_priv *oct_priv =
1454 (struct octeon_device_priv *)oct->priv;
1455
1456 struct handshake *hs;
1457
1458 switch (atomic_read(&oct->status)) {
1459 case OCT_DEV_RUNNING:
1460 case OCT_DEV_CORE_OK:
1461
1462 /* No more instructions will be forwarded. */
1463 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1464
1465 oct->app_mode = CVM_DRV_INVALID_APP;
1466 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1467 lio_get_state_string(&oct->status));
1468
1469 schedule_timeout_uninterruptible(HZ / 10);
1470
1471 /* fallthrough */
1472 case OCT_DEV_HOST_OK:
1473
1474 /* fallthrough */
1475 case OCT_DEV_CONSOLE_INIT_DONE:
1476 /* Remove any consoles */
1477 octeon_remove_consoles(oct);
1478
1479 /* fallthrough */
1480 case OCT_DEV_IO_QUEUES_DONE:
1481 if (wait_for_pending_requests(oct))
1482 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1483
1484 if (lio_wait_for_instr_fetch(oct))
1485 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1486
1487 /* Disable the input and output queues now. No more packets will
1488 * arrive from Octeon, but we should wait for all packet
1489 * processing to finish.
1490 */
1491 oct->fn_list.disable_io_queues(oct);
1492
1493 if (lio_wait_for_oq_pkts(oct))
1494 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1495
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001496 /* fallthrough */
1497 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001498 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001499 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001500
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001501 if (oct->msix_on) {
1502 msix_entries = (struct msix_entry *)oct->msix_entries;
1503 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1504 /* clear the affinity_cpumask */
1505 irq_set_affinity_hint(msix_entries[i].vector,
1506 NULL);
1507 free_irq(msix_entries[i].vector,
1508 &oct->ioq_vector[i]);
1509 }
1510 /* non-iov vector's argument is oct struct */
1511 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001512
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001513 pci_disable_msix(oct->pci_dev);
1514 kfree(oct->msix_entries);
1515 oct->msix_entries = NULL;
1516 } else {
1517 /* Release the interrupt line */
1518 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001519
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001520 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1521 pci_disable_msi(oct->pci_dev);
1522 }
1523
Rick Farrington0c88a762017-03-13 12:58:04 -07001524 kfree(oct->irq_name_storage);
1525 oct->irq_name_storage = NULL;
1526
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001527 /* fallthrough */
1528 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001529 if (OCTEON_CN23XX_PF(oct))
1530 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001531
1532 /* fallthrough */
1533 case OCT_DEV_MBOX_SETUP_DONE:
1534 if (OCTEON_CN23XX_PF(oct))
1535 oct->fn_list.free_mbox(oct);
1536
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001537 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001538 case OCT_DEV_IN_RESET:
1539 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001540 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001541 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001542 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001543 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001544 continue;
1545 octeon_delete_droq(oct, i);
1546 }
1547
1548 /* Force any pending handshakes to complete */
1549 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1550 hs = &handshake[i];
1551
1552 if (hs->pci_dev) {
1553 handshake[oct->octeon_id].init_ok = 0;
1554 complete(&handshake[oct->octeon_id].init);
1555 handshake[oct->octeon_id].started_ok = 0;
1556 complete(&handshake[oct->octeon_id].started);
1557 }
1558 }
1559
1560 /* fallthrough */
1561 case OCT_DEV_RESP_LIST_INIT_DONE:
1562 octeon_delete_response_list(oct);
1563
1564 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001565 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001566 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001567 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001568 continue;
1569 octeon_delete_instr_queue(oct, i);
1570 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001571#ifdef CONFIG_PCI_IOV
1572 if (oct->sriov_info.sriov_enabled)
1573 pci_disable_sriov(oct->pci_dev);
1574#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001575 /* fallthrough */
1576 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1577 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001578
1579 /* fallthrough */
1580 case OCT_DEV_DISPATCH_INIT_DONE:
1581 octeon_delete_dispatch_list(oct);
1582 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1583
1584 /* fallthrough */
1585 case OCT_DEV_PCI_MAP_DONE:
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001586 refcount = octeon_deregister_device(oct);
1587
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001588 if (!fw_type_is_none()) {
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001589 /* Soft reset the octeon device before exiting.
1590 * Implementation note: here, we reset the device
1591 * if it is a CN6XXX OR the last CN23XX device.
1592 */
1593 if (OCTEON_CN6XXX(oct) || !refcount)
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001594 oct->fn_list.soft_reset(oct);
1595 }
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001596
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001597 octeon_unmap_pci_barx(oct, 0);
1598 octeon_unmap_pci_barx(oct, 1);
1599
1600 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001601 case OCT_DEV_PCI_ENABLE_DONE:
1602 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001603 /* Disable the device, releasing the PCI INT */
1604 pci_disable_device(oct->pci_dev);
1605
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001606 /* fallthrough */
1607 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001608 /* Nothing to be done here either */
1609 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001610 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001611
1612 tasklet_kill(&oct_priv->droq_tasklet);
1613}
1614
1615/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001616 * \brief Callback for rx ctrl
1617 * @param status status of request
1618 * @param buf pointer to resp structure
1619 */
1620static void rx_ctl_callback(struct octeon_device *oct,
1621 u32 status,
1622 void *buf)
1623{
1624 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1625 struct liquidio_rx_ctl_context *ctx;
1626
1627 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1628
1629 oct = lio_get_device(ctx->octeon_id);
1630 if (status)
1631 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1632 CVM_CAST64(status));
1633 WRITE_ONCE(ctx->cond, 1);
1634
1635 /* This barrier is required to be sure that the response has been
1636 * written fully before waking up the handler
1637 */
1638 wmb();
1639
1640 wake_up_interruptible(&ctx->wc);
1641}
1642
1643/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001644 * \brief Send Rx control command
1645 * @param lio per-network private data
1646 * @param start_stop whether to start or stop
1647 */
1648static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1649{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001650 struct octeon_soft_command *sc;
1651 struct liquidio_rx_ctl_context *ctx;
1652 union octnet_cmd *ncmd;
1653 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1654 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1655 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001656
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001657 if (oct->props[lio->ifidx].rx_on == start_stop)
1658 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001659
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001660 sc = (struct octeon_soft_command *)
1661 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1662 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001663
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001664 ncmd = (union octnet_cmd *)sc->virtdptr;
1665 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1666
1667 WRITE_ONCE(ctx->cond, 0);
1668 ctx->octeon_id = lio_get_device_id(oct);
1669 init_waitqueue_head(&ctx->wc);
1670
1671 ncmd->u64 = 0;
1672 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1673 ncmd->s.param1 = start_stop;
1674
1675 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1676
1677 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1678
1679 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1680 OPCODE_NIC_CMD, 0, 0, 0);
1681
1682 sc->callback = rx_ctl_callback;
1683 sc->callback_arg = sc;
1684 sc->wait_time = 5000;
1685
1686 retval = octeon_send_soft_command(oct, sc);
1687 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001688 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001689 } else {
1690 /* Sleep on a wait queue till the cond flag indicates that the
1691 * response arrived or timed-out.
1692 */
1693 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1694 return;
1695 oct->props[lio->ifidx].rx_on = start_stop;
1696 }
1697
1698 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001699}
1700
1701/**
1702 * \brief Destroy NIC device interface
1703 * @param oct octeon device
1704 * @param ifidx which interface to destroy
1705 *
1706 * Cleanup associated with each interface for an Octeon device when NIC
1707 * module is being unloaded or if initialization fails during load.
1708 */
1709static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1710{
1711 struct net_device *netdev = oct->props[ifidx].netdev;
1712 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001713 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001714
1715 if (!netdev) {
1716 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1717 __func__, ifidx);
1718 return;
1719 }
1720
1721 lio = GET_LIO(netdev);
1722
1723 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1724
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001725 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001726 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001727
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001728 if (fw_type_is_none()) {
1729 struct octnic_ctrl_pkt nctrl;
1730
1731 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1732 nctrl.ncmd.s.cmd = OCTNET_CMD_RESET_PF;
1733 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1734 octnet_send_nic_ctrl_pkt(oct, &nctrl);
1735 }
1736
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001737 if (oct->props[lio->ifidx].napi_enabled == 1) {
1738 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1739 napi_disable(napi);
1740
1741 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001742
1743 if (OCTEON_CN23XX_PF(oct))
1744 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001745 }
1746
Intiyaz Basha42013e92017-08-08 19:34:28 -07001747 /* Delete NAPI */
1748 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1749 netif_napi_del(napi);
1750
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001751 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1752 unregister_netdev(netdev);
1753
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001754 cleanup_link_status_change_wq(netdev);
1755
Satanand Burla031d4f12017-03-22 11:31:13 -07001756 cleanup_rx_oom_poll_fn(netdev);
1757
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001758 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001759
1760 free_netdev(netdev);
1761
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001762 oct->props[ifidx].gmxport = -1;
1763
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001764 oct->props[ifidx].netdev = NULL;
1765}
1766
1767/**
1768 * \brief Stop complete NIC functionality
1769 * @param oct octeon device
1770 */
1771static int liquidio_stop_nic_module(struct octeon_device *oct)
1772{
1773 int i, j;
1774 struct lio *lio;
1775
1776 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1777 if (!oct->ifcount) {
1778 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1779 return 1;
1780 }
1781
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001782 spin_lock_bh(&oct->cmd_resp_wqlock);
1783 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1784 spin_unlock_bh(&oct->cmd_resp_wqlock);
1785
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001786 for (i = 0; i < oct->ifcount; i++) {
1787 lio = GET_LIO(oct->props[i].netdev);
1788 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001789 octeon_unregister_droq_ops(oct,
1790 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001791 }
1792
1793 for (i = 0; i < oct->ifcount; i++)
1794 liquidio_destroy_nic_device(oct, i);
1795
1796 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1797 return 0;
1798}
1799
1800/**
1801 * \brief Cleans up resources at unload time
1802 * @param pdev PCI device structure
1803 */
1804static void liquidio_remove(struct pci_dev *pdev)
1805{
1806 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1807
1808 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1809
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001810 if (oct_dev->watchdog_task)
1811 kthread_stop(oct_dev->watchdog_task);
1812
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001813 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1814 liquidio_stop_nic_module(oct_dev);
1815
1816 /* Reset the octeon device and cleanup all memory allocated for
1817 * the octeon device by driver.
1818 */
1819 octeon_destroy_resources(oct_dev);
1820
1821 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1822
1823 /* This octeon device has been removed. Update the global
1824 * data structure to reflect this. Free the device structure.
1825 */
1826 octeon_free_device_mem(oct_dev);
1827}
1828
1829/**
1830 * \brief Identify the Octeon device and to map the BAR address space
1831 * @param oct octeon device
1832 */
1833static int octeon_chip_specific_setup(struct octeon_device *oct)
1834{
1835 u32 dev_id, rev_id;
1836 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001837 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001838
1839 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1840 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1841 oct->rev_id = rev_id & 0xff;
1842
1843 switch (dev_id) {
1844 case OCTEON_CN68XX_PCIID:
1845 oct->chip_id = OCTEON_CN68XX;
1846 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001847 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001848 break;
1849
1850 case OCTEON_CN66XX_PCIID:
1851 oct->chip_id = OCTEON_CN66XX;
1852 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001853 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001854 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001855
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001856 case OCTEON_CN23XX_PCIID_PF:
1857 oct->chip_id = OCTEON_CN23XX_PF_VID;
1858 ret = setup_cn23xx_octeon_pf_device(oct);
Derek Chicklescf19a8c2017-08-01 15:05:07 -07001859#ifdef CONFIG_PCI_IOV
1860 if (!ret)
1861 pci_sriov_set_totalvfs(oct->pci_dev,
1862 oct->sriov_info.max_vfs);
1863#endif
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001864 s = "CN23XX";
1865 break;
1866
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001867 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001868 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001869 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1870 dev_id);
1871 }
1872
1873 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001874 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001875 OCTEON_MAJOR_REV(oct),
1876 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001877 octeon_get_conf(oct)->card_name,
1878 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001879
1880 return ret;
1881}
1882
1883/**
1884 * \brief PCI initialization for each Octeon device.
1885 * @param oct octeon device
1886 */
1887static int octeon_pci_os_setup(struct octeon_device *oct)
1888{
1889 /* setup PCI stuff first */
1890 if (pci_enable_device(oct->pci_dev)) {
1891 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1892 return 1;
1893 }
1894
1895 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1896 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001897 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001898 return 1;
1899 }
1900
1901 /* Enable PCI DMA Master. */
1902 pci_set_master(oct->pci_dev);
1903
1904 return 0;
1905}
1906
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001907static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1908{
1909 int q = 0;
1910
1911 if (netif_is_multiqueue(lio->netdev))
1912 q = skb->queue_mapping % lio->linfo.num_txpciq;
1913
1914 return q;
1915}
1916
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001917/**
1918 * \brief Check Tx queue state for a given network buffer
1919 * @param lio per-network private data
1920 * @param skb network buffer
1921 */
1922static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1923{
1924 int q = 0, iq = 0;
1925
1926 if (netif_is_multiqueue(lio->netdev)) {
1927 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001928 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001929 } else {
1930 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001931 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001932 }
1933
1934 if (octnet_iq_is_full(lio->oct_dev, iq))
1935 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001936
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001937 if (__netif_subqueue_stopped(lio->netdev, q)) {
1938 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001939 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001940 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001941 return 1;
1942}
1943
1944/**
1945 * \brief Unmap and free network buffer
1946 * @param buf buffer
1947 */
1948static void free_netbuf(void *buf)
1949{
1950 struct sk_buff *skb;
1951 struct octnet_buf_free_info *finfo;
1952 struct lio *lio;
1953
1954 finfo = (struct octnet_buf_free_info *)buf;
1955 skb = finfo->skb;
1956 lio = finfo->lio;
1957
1958 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1959 DMA_TO_DEVICE);
1960
1961 check_txq_state(lio, skb);
1962
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001963 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001964}
1965
1966/**
1967 * \brief Unmap and free gather buffer
1968 * @param buf buffer
1969 */
1970static void free_netsgbuf(void *buf)
1971{
1972 struct octnet_buf_free_info *finfo;
1973 struct sk_buff *skb;
1974 struct lio *lio;
1975 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001976 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001977
1978 finfo = (struct octnet_buf_free_info *)buf;
1979 skb = finfo->skb;
1980 lio = finfo->lio;
1981 g = finfo->g;
1982 frags = skb_shinfo(skb)->nr_frags;
1983
1984 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1985 g->sg[0].ptr[0], (skb->len - skb->data_len),
1986 DMA_TO_DEVICE);
1987
1988 i = 1;
1989 while (frags--) {
1990 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1991
1992 pci_unmap_page((lio->oct_dev)->pci_dev,
1993 g->sg[(i >> 2)].ptr[(i & 3)],
1994 frag->size, DMA_TO_DEVICE);
1995 i++;
1996 }
1997
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001998 iq = skb_iq(lio, skb);
1999 spin_lock(&lio->glist_lock[iq]);
2000 list_add_tail(&g->list, &lio->glist[iq]);
2001 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002002
2003 check_txq_state(lio, skb); /* mq support: sub-queue state check */
2004
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002005 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002006}
2007
2008/**
2009 * \brief Unmap and free gather buffer with response
2010 * @param buf buffer
2011 */
2012static void free_netsgbuf_with_resp(void *buf)
2013{
2014 struct octeon_soft_command *sc;
2015 struct octnet_buf_free_info *finfo;
2016 struct sk_buff *skb;
2017 struct lio *lio;
2018 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002019 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002020
2021 sc = (struct octeon_soft_command *)buf;
2022 skb = (struct sk_buff *)sc->callback_arg;
2023 finfo = (struct octnet_buf_free_info *)&skb->cb;
2024
2025 lio = finfo->lio;
2026 g = finfo->g;
2027 frags = skb_shinfo(skb)->nr_frags;
2028
2029 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
2030 g->sg[0].ptr[0], (skb->len - skb->data_len),
2031 DMA_TO_DEVICE);
2032
2033 i = 1;
2034 while (frags--) {
2035 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
2036
2037 pci_unmap_page((lio->oct_dev)->pci_dev,
2038 g->sg[(i >> 2)].ptr[(i & 3)],
2039 frag->size, DMA_TO_DEVICE);
2040 i++;
2041 }
2042
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002043 iq = skb_iq(lio, skb);
2044
2045 spin_lock(&lio->glist_lock[iq]);
2046 list_add_tail(&g->list, &lio->glist[iq]);
2047 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002048
2049 /* Don't free the skb yet */
2050
2051 check_txq_state(lio, skb);
2052}
2053
2054/**
2055 * \brief Adjust ptp frequency
2056 * @param ptp PTP clock info
2057 * @param ppb how much to adjust by, in parts-per-billion
2058 */
2059static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
2060{
2061 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2062 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2063 u64 comp, delta;
2064 unsigned long flags;
2065 bool neg_adj = false;
2066
2067 if (ppb < 0) {
2068 neg_adj = true;
2069 ppb = -ppb;
2070 }
2071
2072 /* The hardware adds the clock compensation value to the
2073 * PTP clock on every coprocessor clock cycle, so we
2074 * compute the delta in terms of coprocessor clocks.
2075 */
2076 delta = (u64)ppb << 32;
2077 do_div(delta, oct->coproc_clock_rate);
2078
2079 spin_lock_irqsave(&lio->ptp_lock, flags);
2080 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
2081 if (neg_adj)
2082 comp -= delta;
2083 else
2084 comp += delta;
2085 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2086 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2087
2088 return 0;
2089}
2090
2091/**
2092 * \brief Adjust ptp time
2093 * @param ptp PTP clock info
2094 * @param delta how much to adjust by, in nanosecs
2095 */
2096static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2097{
2098 unsigned long flags;
2099 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2100
2101 spin_lock_irqsave(&lio->ptp_lock, flags);
2102 lio->ptp_adjust += delta;
2103 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2104
2105 return 0;
2106}
2107
2108/**
2109 * \brief Get hardware clock time, including any adjustment
2110 * @param ptp PTP clock info
2111 * @param ts timespec
2112 */
2113static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2114 struct timespec64 *ts)
2115{
2116 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002117 unsigned long flags;
2118 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2119 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2120
2121 spin_lock_irqsave(&lio->ptp_lock, flags);
2122 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2123 ns += lio->ptp_adjust;
2124 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2125
Kefeng Wang286af312016-01-27 17:34:37 +08002126 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002127
2128 return 0;
2129}
2130
2131/**
2132 * \brief Set hardware clock time. Reset adjustment
2133 * @param ptp PTP clock info
2134 * @param ts timespec
2135 */
2136static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2137 const struct timespec64 *ts)
2138{
2139 u64 ns;
2140 unsigned long flags;
2141 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2142 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2143
2144 ns = timespec_to_ns(ts);
2145
2146 spin_lock_irqsave(&lio->ptp_lock, flags);
2147 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2148 lio->ptp_adjust = 0;
2149 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2150
2151 return 0;
2152}
2153
2154/**
2155 * \brief Check if PTP is enabled
2156 * @param ptp PTP clock info
2157 * @param rq request
2158 * @param on is it on
2159 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002160static int
2161liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2162 struct ptp_clock_request *rq __attribute__((unused)),
2163 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002164{
2165 return -EOPNOTSUPP;
2166}
2167
2168/**
2169 * \brief Open PTP clock source
2170 * @param netdev network device
2171 */
2172static void oct_ptp_open(struct net_device *netdev)
2173{
2174 struct lio *lio = GET_LIO(netdev);
2175 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2176
2177 spin_lock_init(&lio->ptp_lock);
2178
2179 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2180 lio->ptp_info.owner = THIS_MODULE;
2181 lio->ptp_info.max_adj = 250000000;
2182 lio->ptp_info.n_alarm = 0;
2183 lio->ptp_info.n_ext_ts = 0;
2184 lio->ptp_info.n_per_out = 0;
2185 lio->ptp_info.pps = 0;
2186 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2187 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2188 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2189 lio->ptp_info.settime64 = liquidio_ptp_settime;
2190 lio->ptp_info.enable = liquidio_ptp_enable;
2191
2192 lio->ptp_adjust = 0;
2193
2194 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2195 &oct->pci_dev->dev);
2196
2197 if (IS_ERR(lio->ptp_clock))
2198 lio->ptp_clock = NULL;
2199}
2200
2201/**
2202 * \brief Init PTP clock
2203 * @param oct octeon device
2204 */
2205static void liquidio_ptp_init(struct octeon_device *oct)
2206{
2207 u64 clock_comp, cfg;
2208
2209 clock_comp = (u64)NSEC_PER_SEC << 32;
2210 do_div(clock_comp, oct->coproc_clock_rate);
2211 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2212
2213 /* Enable */
2214 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2215 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2216}
2217
2218/**
2219 * \brief Load firmware to device
2220 * @param oct octeon device
2221 *
2222 * Maps device to firmware filename, requests firmware, and downloads it
2223 */
2224static int load_firmware(struct octeon_device *oct)
2225{
2226 int ret = 0;
2227 const struct firmware *fw;
2228 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2229 char *tmp_fw_type;
2230
Felix Manlunas7cc61db2017-03-23 13:26:28 -07002231 if (fw_type_is_none()) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002232 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2233 return ret;
2234 }
2235
2236 if (fw_type[0] == '\0')
2237 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2238 else
2239 tmp_fw_type = fw_type;
2240
2241 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2242 octeon_get_conf(oct)->card_name, tmp_fw_type,
2243 LIO_FW_NAME_SUFFIX);
2244
2245 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2246 if (ret) {
2247 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2248 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002249 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002250 return ret;
2251 }
2252
2253 ret = octeon_download_firmware(oct, fw->data, fw->size);
2254
2255 release_firmware(fw);
2256
2257 return ret;
2258}
2259
2260/**
2261 * \brief Setup output queue
2262 * @param oct octeon device
2263 * @param q_no which queue
2264 * @param num_descs how many descriptors
2265 * @param desc_size size of each descriptor
2266 * @param app_ctx application context
2267 */
2268static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2269 int desc_size, void *app_ctx)
2270{
2271 int ret_val = 0;
2272
2273 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2274 /* droq creation and local register settings. */
2275 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302276 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002277 return ret_val;
2278
2279 if (ret_val == 1) {
2280 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2281 return 0;
2282 }
2283 /* tasklet creation for the droq */
2284
2285 /* Enable the droq queues */
2286 octeon_set_droq_pkt_op(oct, q_no, 1);
2287
2288 /* Send Credit for Octeon Output queues. Credits are always
2289 * sent after the output queue is enabled.
2290 */
2291 writel(oct->droq[q_no]->max_count,
2292 oct->droq[q_no]->pkts_credit_reg);
2293
2294 return ret_val;
2295}
2296
2297/**
2298 * \brief Callback for getting interface configuration
2299 * @param status status of request
2300 * @param buf pointer to resp structure
2301 */
2302static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002303 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002304 void *buf)
2305{
2306 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2307 struct liquidio_if_cfg_resp *resp;
2308 struct liquidio_if_cfg_context *ctx;
2309
2310 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002311 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002312
2313 oct = lio_get_device(ctx->octeon_id);
2314 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002315 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2316 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002317 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002318
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002319 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2320 resp->cfg_info.liquidio_firmware_version);
2321
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002322 /* This barrier is required to be sure that the response has been
2323 * written fully before waking up the handler
2324 */
2325 wmb();
2326
2327 wake_up_interruptible(&ctx->wc);
2328}
2329
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002330/** Routine to push packets arriving on Octeon interface upto network layer.
2331 * @param oct_id - octeon device id.
2332 * @param skbuff - skbuff struct to be passed to network layer.
2333 * @param len - size of total data received.
2334 * @param rh - Control header associated with the packet
2335 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002336 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002337 */
2338static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002339liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002340 void *skbuff,
2341 u32 len,
2342 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002343 void *param,
2344 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002345{
2346 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002347 struct sk_buff *skb = (struct sk_buff *)skbuff;
2348 struct skb_shared_hwtstamps *shhwtstamps;
2349 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002350 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002351 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002352 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002353 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2354 napi);
2355 if (netdev) {
2356 int packet_was_received;
2357 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002358 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002359
2360 /* Do not proceed if the interface is not in RUNNING state. */
2361 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2362 recv_buffer_free(skb);
2363 droq->stats.rx_dropped++;
2364 return;
2365 }
2366
2367 skb->dev = netdev;
2368
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002369 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002370 if (likely(len > MIN_SKB_SIZE)) {
2371 struct octeon_skb_page_info *pg_info;
2372 unsigned char *va;
2373
2374 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2375 if (pg_info->page) {
2376 /* For Paged allocation use the frags */
2377 va = page_address(pg_info->page) +
2378 pg_info->page_offset;
2379 memcpy(skb->data, va, MIN_SKB_SIZE);
2380 skb_put(skb, MIN_SKB_SIZE);
2381 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2382 pg_info->page,
2383 pg_info->page_offset +
2384 MIN_SKB_SIZE,
2385 len - MIN_SKB_SIZE,
2386 LIO_RXBUFFER_SZ);
2387 }
2388 } else {
2389 struct octeon_skb_page_info *pg_info =
2390 ((struct octeon_skb_page_info *)(skb->cb));
2391 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2392 + pg_info->page_offset, len);
2393 skb_put(skb, len);
2394 put_page(pg_info->page);
2395 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002396
Prasad Kannegantide28c992017-01-09 14:42:40 -08002397 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2398
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002399 if (oct->ptp_enable) {
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002400 if (rh->r_dh.has_hwtstamp) {
2401 /* timestamp is included from the hardware at
2402 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002403 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002404 if (ifstate_check
2405 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2406 /* Nanoseconds are in the first 64-bits
2407 * of the packet.
2408 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002409 memcpy(&ns, (skb->data + r_dh_off),
2410 sizeof(ns));
2411 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002412 shhwtstamps = skb_hwtstamps(skb);
2413 shhwtstamps->hwtstamp =
2414 ns_to_ktime(ns +
2415 lio->ptp_adjust);
2416 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002417 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002418 }
2419
Prasad Kannegantide28c992017-01-09 14:42:40 -08002420 if (rh->r_dh.has_hash) {
2421 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2422 u32 hash = be32_to_cpu(*hash_be);
2423
2424 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2425 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2426 }
2427
2428 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2429
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002430 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002431 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002432 (((rh->r_dh.encap_on) &&
2433 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2434 (!(rh->r_dh.encap_on) &&
2435 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002436 /* checksum has already been verified */
2437 skb->ip_summed = CHECKSUM_UNNECESSARY;
2438 else
2439 skb->ip_summed = CHECKSUM_NONE;
2440
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002441 /* Setting Encapsulation field on basis of status received
2442 * from the firmware
2443 */
2444 if (rh->r_dh.encap_on) {
2445 skb->encapsulation = 1;
2446 skb->csum_level = 1;
2447 droq->stats.rx_vxlan++;
2448 }
2449
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002450 /* inbound VLAN tag */
2451 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2452 (rh->r_dh.vlan != 0)) {
2453 u16 vid = rh->r_dh.vlan;
2454 u16 priority = rh->r_dh.priority;
2455
2456 vtag = priority << 13 | vid;
2457 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2458 }
2459
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002460 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2461
2462 if (packet_was_received) {
2463 droq->stats.rx_bytes_received += len;
2464 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002465 } else {
2466 droq->stats.rx_dropped++;
2467 netif_info(lio, rx_err, lio->netdev,
2468 "droq:%d error rx_dropped:%llu\n",
2469 droq->q_no, droq->stats.rx_dropped);
2470 }
2471
2472 } else {
2473 recv_buffer_free(skb);
2474 }
2475}
2476
2477/**
2478 * \brief wrapper for calling napi_schedule
2479 * @param param parameters to pass to napi_schedule
2480 *
2481 * Used when scheduling on different CPUs
2482 */
2483static void napi_schedule_wrapper(void *param)
2484{
2485 struct napi_struct *napi = param;
2486
2487 napi_schedule(napi);
2488}
2489
2490/**
2491 * \brief callback when receive interrupt occurs and we are in NAPI mode
2492 * @param arg pointer to octeon output queue
2493 */
2494static void liquidio_napi_drv_callback(void *arg)
2495{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002496 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002497 struct octeon_droq *droq = arg;
2498 int this_cpu = smp_processor_id();
2499
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002500 oct = droq->oct_dev;
2501
2502 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2503 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002504 } else {
2505 struct call_single_data *csd = &droq->csd;
2506
2507 csd->func = napi_schedule_wrapper;
2508 csd->info = &droq->napi;
2509 csd->flags = 0;
2510
2511 smp_call_function_single_async(droq->cpu_id, csd);
2512 }
2513}
2514
2515/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002516 * \brief Entry point for NAPI polling
2517 * @param napi NAPI structure
2518 * @param budget maximum number of items to process
2519 */
2520static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2521{
2522 struct octeon_droq *droq;
2523 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002524 int tx_done = 0, iq_no;
2525 struct octeon_instr_queue *iq;
2526 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002527
2528 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002529 oct = droq->oct_dev;
2530 iq_no = droq->q_no;
2531 /* Handle Droq descriptors */
2532 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2533 POLL_EVENT_PROCESS_PKTS,
2534 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002535
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002536 /* Flush the instruction queue */
2537 iq = oct->instr_queue[iq_no];
2538 if (iq) {
VSR Burru6069f3f2017-03-22 11:54:50 -07002539 if (atomic_read(&iq->instr_pending))
2540 /* Process iq buffers with in the budget limits */
2541 tx_done = octeon_flush_iq(oct, iq, budget);
2542 else
2543 tx_done = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002544 /* Update iq read-index rather than waiting for next interrupt.
2545 * Return back if tx_done is false.
2546 */
2547 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002548 } else {
2549 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2550 __func__, iq_no);
2551 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002552
Satanand Burlacdb478e2017-01-31 13:04:42 -08002553 /* force enable interrupt if reg cnts are high to avoid wraparound */
2554 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002555 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002556 (droq->pkt_count >= MAX_REG_CNT)) {
2557 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002558 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002559 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2560 POLL_EVENT_ENABLE_INTR, 0);
2561 return 0;
2562 }
2563
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002564 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002565}
2566
2567/**
2568 * \brief Setup input and output queues
2569 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002570 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002571 *
2572 * Note: Queues are with respect to the octeon device. Thus
2573 * an input queue is for egress packets, and output queues
2574 * are for ingress packets.
2575 */
2576static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002577 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002578{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002579 struct octeon_droq_ops droq_ops;
2580 struct net_device *netdev;
Gustavo A. R. Silvaeb2b9872017-07-18 15:53:48 -05002581 int cpu_id;
2582 int cpu_id_modulus;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002583 struct octeon_droq *droq;
2584 struct napi_struct *napi;
2585 int q, q_no, retval = 0;
2586 struct lio *lio;
2587 int num_tx_descs;
2588
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002589 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002590
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002591 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002592
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002593 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2594
2595 droq_ops.fptr = liquidio_push_packet;
2596 droq_ops.farg = (void *)netdev;
2597
2598 droq_ops.poll_mode = 1;
2599 droq_ops.napi_fn = liquidio_napi_drv_callback;
2600 cpu_id = 0;
2601 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002602
2603 /* set up DROQs. */
2604 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002605 q_no = lio->linfo.rxpciq[q].s.q_no;
2606 dev_dbg(&octeon_dev->pci_dev->dev,
2607 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2608 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002609 retval = octeon_setup_droq(octeon_dev, q_no,
2610 CFG_GET_NUM_RX_DESCS_NIC_IF
2611 (octeon_get_conf(octeon_dev),
2612 lio->ifidx),
2613 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2614 (octeon_get_conf(octeon_dev),
2615 lio->ifidx), NULL);
2616 if (retval) {
2617 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002618 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002619 __func__);
2620 return 1;
2621 }
2622
2623 droq = octeon_dev->droq[q_no];
2624 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002625 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2626 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002627 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002628
2629 /* designate a CPU for this droq */
2630 droq->cpu_id = cpu_id;
2631 cpu_id++;
2632 if (cpu_id >= cpu_id_modulus)
2633 cpu_id = 0;
2634
2635 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2636 }
2637
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002638 if (OCTEON_CN23XX_PF(octeon_dev)) {
2639 /* 23XX PF can receive control messages (via the first PF-owned
2640 * droq) from the firmware even if the ethX interface is down,
2641 * so that's why poll_mode must be off for the first droq.
2642 */
2643 octeon_dev->droq[0]->ops.poll_mode = 0;
2644 }
2645
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002646 /* set up IQs. */
2647 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2648 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2649 (octeon_dev),
2650 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002651 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2652 lio->linfo.txpciq[q], num_tx_descs,
2653 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002654 if (retval) {
2655 dev_err(&octeon_dev->pci_dev->dev,
2656 " %s : Runtime IQ(TxQ) creation failed.\n",
2657 __func__);
2658 return 1;
2659 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002660
2661 if (octeon_dev->ioq_vector) {
2662 struct octeon_ioq_vector *ioq_vector;
2663
2664 ioq_vector = &octeon_dev->ioq_vector[q];
2665 netif_set_xps_queue(netdev,
2666 &ioq_vector->affinity_mask,
2667 ioq_vector->iq_index);
2668 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002669 }
2670
2671 return 0;
2672}
2673
2674/**
2675 * \brief Poll routine for checking transmit queue status
2676 * @param work work_struct data structure
2677 */
2678static void octnet_poll_check_txq_status(struct work_struct *work)
2679{
2680 struct cavium_wk *wk = (struct cavium_wk *)work;
2681 struct lio *lio = (struct lio *)wk->ctxptr;
2682
2683 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2684 return;
2685
2686 check_txq_status(lio);
2687 queue_delayed_work(lio->txq_status_wq.wq,
2688 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2689}
2690
2691/**
2692 * \brief Sets up the txq poll check
2693 * @param netdev network device
2694 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002695static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002696{
2697 struct lio *lio = GET_LIO(netdev);
2698 struct octeon_device *oct = lio->oct_dev;
2699
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302700 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2701 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002702 if (!lio->txq_status_wq.wq) {
2703 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002704 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002705 }
2706 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2707 octnet_poll_check_txq_status);
2708 lio->txq_status_wq.wk.ctxptr = lio;
2709 queue_delayed_work(lio->txq_status_wq.wq,
2710 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002711 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002712}
2713
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002714static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2715{
2716 struct lio *lio = GET_LIO(netdev);
2717
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002718 if (lio->txq_status_wq.wq) {
2719 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2720 destroy_workqueue(lio->txq_status_wq.wq);
2721 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002722}
2723
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002724/**
2725 * \brief Net device open for LiquidIO
2726 * @param netdev network device
2727 */
2728static int liquidio_open(struct net_device *netdev)
2729{
2730 struct lio *lio = GET_LIO(netdev);
2731 struct octeon_device *oct = lio->oct_dev;
2732 struct napi_struct *napi, *n;
2733
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002734 if (oct->props[lio->ifidx].napi_enabled == 0) {
2735 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2736 napi_enable(napi);
2737
2738 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002739
2740 if (OCTEON_CN23XX_PF(oct))
2741 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002742 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002743
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002744 if (oct->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002745 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002746
2747 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002748
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002749 /* Ready for link status updates */
2750 lio->intf_open = 1;
2751
2752 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2753
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002754 if (OCTEON_CN23XX_PF(oct)) {
2755 if (!oct->msix_on)
2756 if (setup_tx_poll_fn(netdev))
2757 return -1;
2758 } else {
2759 if (setup_tx_poll_fn(netdev))
2760 return -1;
2761 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002762
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002763 start_txq(netdev);
2764
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002765 /* tell Octeon to start forwarding packets to host */
2766 send_rx_ctrl_cmd(lio, 1);
2767
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002768 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2769 netdev->name);
2770
2771 return 0;
2772}
2773
2774/**
2775 * \brief Net device stop for LiquidIO
2776 * @param netdev network device
2777 */
2778static int liquidio_stop(struct net_device *netdev)
2779{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002780 struct lio *lio = GET_LIO(netdev);
2781 struct octeon_device *oct = lio->oct_dev;
Intiyaz Basha42013e92017-08-08 19:34:28 -07002782 struct napi_struct *napi, *n;
2783
2784 if (oct->props[lio->ifidx].napi_enabled) {
2785 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2786 napi_disable(napi);
2787
2788 oct->props[lio->ifidx].napi_enabled = 0;
2789
2790 if (OCTEON_CN23XX_PF(oct))
2791 oct->droq[0]->ops.poll_mode = 0;
2792 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002793
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002794 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2795
2796 netif_tx_disable(netdev);
2797
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002798 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002799 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002800 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002801 lio->linfo.link.s.link_up = 0;
2802 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002803
Felix Manlunascb2336b2017-01-11 17:09:02 -08002804 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002805 send_rx_ctrl_cmd(lio, 0);
2806
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002807 if (OCTEON_CN23XX_PF(oct)) {
2808 if (!oct->msix_on)
2809 cleanup_tx_poll_fn(netdev);
2810 } else {
2811 cleanup_tx_poll_fn(netdev);
2812 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002813
2814 if (lio->ptp_clock) {
2815 ptp_clock_unregister(lio->ptp_clock);
2816 lio->ptp_clock = NULL;
2817 }
2818
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002819 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002820
2821 return 0;
2822}
2823
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002824/**
2825 * \brief Converts a mask based on net device flags
2826 * @param netdev network device
2827 *
2828 * This routine generates a octnet_ifflags mask from the net device flags
2829 * received from the OS.
2830 */
2831static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2832{
2833 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2834
2835 if (netdev->flags & IFF_PROMISC)
2836 f |= OCTNET_IFFLAG_PROMISC;
2837
2838 if (netdev->flags & IFF_ALLMULTI)
2839 f |= OCTNET_IFFLAG_ALLMULTI;
2840
2841 if (netdev->flags & IFF_MULTICAST) {
2842 f |= OCTNET_IFFLAG_MULTICAST;
2843
2844 /* Accept all multicast addresses if there are more than we
2845 * can handle
2846 */
2847 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2848 f |= OCTNET_IFFLAG_ALLMULTI;
2849 }
2850
2851 if (netdev->flags & IFF_BROADCAST)
2852 f |= OCTNET_IFFLAG_BROADCAST;
2853
2854 return f;
2855}
2856
2857/**
2858 * \brief Net device set_multicast_list
2859 * @param netdev network device
2860 */
2861static void liquidio_set_mcast_list(struct net_device *netdev)
2862{
2863 struct lio *lio = GET_LIO(netdev);
2864 struct octeon_device *oct = lio->oct_dev;
2865 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002866 struct netdev_hw_addr *ha;
2867 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002868 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002869 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2870
2871 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2872
2873 /* Create a ctrl pkt command to be sent to core app. */
2874 nctrl.ncmd.u64 = 0;
2875 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002876 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2877 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002878 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002879 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002880 nctrl.netpndev = (u64)netdev;
2881 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2882
2883 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002884 mc = &nctrl.udd[0];
2885 netdev_for_each_mc_addr(ha, netdev) {
2886 *mc = 0;
2887 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2888 /* no need to swap bytes */
2889
2890 if (++mc > &nctrl.udd[mc_count])
2891 break;
2892 }
2893
2894 /* Apparently, any activity in this call from the kernel has to
2895 * be atomic. So we won't wait for response.
2896 */
2897 nctrl.wait_time = 0;
2898
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002899 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002900 if (ret < 0) {
2901 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2902 ret);
2903 }
2904}
2905
2906/**
2907 * \brief Net device set_mac_address
2908 * @param netdev network device
2909 */
2910static int liquidio_set_mac(struct net_device *netdev, void *p)
2911{
2912 int ret = 0;
2913 struct lio *lio = GET_LIO(netdev);
2914 struct octeon_device *oct = lio->oct_dev;
2915 struct sockaddr *addr = (struct sockaddr *)p;
2916 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002917
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002918 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002919 return -EADDRNOTAVAIL;
2920
2921 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2922
2923 nctrl.ncmd.u64 = 0;
2924 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002925 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002926 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002927 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002928 nctrl.netpndev = (u64)netdev;
2929 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2930 nctrl.wait_time = 100;
2931
2932 nctrl.udd[0] = 0;
2933 /* The MAC Address is presented in network byte order. */
2934 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2935
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002936 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002937 if (ret < 0) {
2938 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2939 return -ENOMEM;
2940 }
2941 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2942 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2943
2944 return 0;
2945}
2946
2947/**
2948 * \brief Net device get_stats
2949 * @param netdev network device
2950 */
2951static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2952{
2953 struct lio *lio = GET_LIO(netdev);
2954 struct net_device_stats *stats = &netdev->stats;
2955 struct octeon_device *oct;
2956 u64 pkts = 0, drop = 0, bytes = 0;
2957 struct oct_droq_stats *oq_stats;
2958 struct oct_iq_stats *iq_stats;
2959 int i, iq_no, oq_no;
2960
2961 oct = lio->oct_dev;
2962
2963 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002964 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002965 iq_stats = &oct->instr_queue[iq_no]->stats;
2966 pkts += iq_stats->tx_done;
2967 drop += iq_stats->tx_dropped;
2968 bytes += iq_stats->tx_tot_bytes;
2969 }
2970
2971 stats->tx_packets = pkts;
2972 stats->tx_bytes = bytes;
2973 stats->tx_dropped = drop;
2974
2975 pkts = 0;
2976 drop = 0;
2977 bytes = 0;
2978
2979 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002980 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002981 oq_stats = &oct->droq[oq_no]->stats;
2982 pkts += oq_stats->rx_pkts_received;
2983 drop += (oq_stats->rx_dropped +
2984 oq_stats->dropped_nodispatch +
2985 oq_stats->dropped_toomany +
2986 oq_stats->dropped_nomem);
2987 bytes += oq_stats->rx_bytes_received;
2988 }
2989
2990 stats->rx_bytes = bytes;
2991 stats->rx_packets = pkts;
2992 stats->rx_dropped = drop;
2993
2994 return stats;
2995}
2996
2997/**
2998 * \brief Net device change_mtu
2999 * @param netdev network device
3000 */
3001static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
3002{
3003 struct lio *lio = GET_LIO(netdev);
3004 struct octeon_device *oct = lio->oct_dev;
3005 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003006 int ret = 0;
3007
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003008 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3009
3010 nctrl.ncmd.u64 = 0;
3011 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003012 nctrl.ncmd.s.param1 = new_mtu;
3013 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003014 nctrl.wait_time = 100;
3015 nctrl.netpndev = (u64)netdev;
3016 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3017
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003018 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003019 if (ret < 0) {
3020 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
3021 return -1;
3022 }
3023
3024 lio->mtu = new_mtu;
3025
3026 return 0;
3027}
3028
3029/**
3030 * \brief Handler for SIOCSHWTSTAMP ioctl
3031 * @param netdev network device
3032 * @param ifr interface request
3033 * @param cmd command
3034 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003035static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003036{
3037 struct hwtstamp_config conf;
3038 struct lio *lio = GET_LIO(netdev);
3039
3040 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
3041 return -EFAULT;
3042
3043 if (conf.flags)
3044 return -EINVAL;
3045
3046 switch (conf.tx_type) {
3047 case HWTSTAMP_TX_ON:
3048 case HWTSTAMP_TX_OFF:
3049 break;
3050 default:
3051 return -ERANGE;
3052 }
3053
3054 switch (conf.rx_filter) {
3055 case HWTSTAMP_FILTER_NONE:
3056 break;
3057 case HWTSTAMP_FILTER_ALL:
3058 case HWTSTAMP_FILTER_SOME:
3059 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3060 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3061 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3062 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3063 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3064 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3065 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3066 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3067 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3068 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3069 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3070 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvare3412572017-05-19 17:52:36 +02003071 case HWTSTAMP_FILTER_NTP_ALL:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003072 conf.rx_filter = HWTSTAMP_FILTER_ALL;
3073 break;
3074 default:
3075 return -ERANGE;
3076 }
3077
3078 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
3079 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3080
3081 else
3082 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3083
3084 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
3085}
3086
3087/**
3088 * \brief ioctl handler
3089 * @param netdev network device
3090 * @param ifr interface request
3091 * @param cmd command
3092 */
3093static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3094{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003095 struct lio *lio = GET_LIO(netdev);
3096
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003097 switch (cmd) {
3098 case SIOCSHWTSTAMP:
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07003099 if (lio->oct_dev->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003100 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003101 default:
3102 return -EOPNOTSUPP;
3103 }
3104}
3105
3106/**
3107 * \brief handle a Tx timestamp response
3108 * @param status response status
3109 * @param buf pointer to skb
3110 */
3111static void handle_timestamp(struct octeon_device *oct,
3112 u32 status,
3113 void *buf)
3114{
3115 struct octnet_buf_free_info *finfo;
3116 struct octeon_soft_command *sc;
3117 struct oct_timestamp_resp *resp;
3118 struct lio *lio;
3119 struct sk_buff *skb = (struct sk_buff *)buf;
3120
3121 finfo = (struct octnet_buf_free_info *)skb->cb;
3122 lio = finfo->lio;
3123 sc = finfo->sc;
3124 oct = lio->oct_dev;
3125 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3126
3127 if (status != OCTEON_REQUEST_DONE) {
3128 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3129 CVM_CAST64(status));
3130 resp->timestamp = 0;
3131 }
3132
3133 octeon_swap_8B_data(&resp->timestamp, 1);
3134
Colin Ian King19a6d152016-02-05 16:30:39 +00003135 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003136 struct skb_shared_hwtstamps ts;
3137 u64 ns = resp->timestamp;
3138
3139 netif_info(lio, tx_done, lio->netdev,
3140 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3141 skb, (unsigned long long)ns);
3142 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3143 skb_tstamp_tx(skb, &ts);
3144 }
3145
3146 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003147 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003148}
3149
3150/* \brief Send a data packet that will be timestamped
3151 * @param oct octeon device
3152 * @param ndata pointer to network data
3153 * @param finfo pointer to private network data
3154 */
3155static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3156 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003157 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003158{
3159 int retval;
3160 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003161 struct lio *lio;
3162 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003163 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003164
3165 lio = finfo->lio;
3166
3167 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3168 sizeof(struct oct_timestamp_resp));
3169 finfo->sc = sc;
3170
3171 if (!sc) {
3172 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3173 return IQ_SEND_FAILED;
3174 }
3175
3176 if (ndata->reqtype == REQTYPE_NORESP_NET)
3177 ndata->reqtype = REQTYPE_RESP_NET;
3178 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3179 ndata->reqtype = REQTYPE_RESP_NET_SG;
3180
3181 sc->callback = handle_timestamp;
3182 sc->callback_arg = finfo->skb;
3183 sc->iq_no = ndata->q_no;
3184
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003185 if (OCTEON_CN23XX_PF(oct))
3186 len = (u32)((struct octeon_instr_ih3 *)
3187 (&sc->cmd.cmd3.ih3))->dlengsz;
3188 else
3189 len = (u32)((struct octeon_instr_ih2 *)
3190 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003191
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003192 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003193
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003194 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003195 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003196
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003197 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003198 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3199 retval);
3200 octeon_free_soft_command(oct, sc);
3201 } else {
3202 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3203 }
3204
3205 return retval;
3206}
3207
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003208/** \brief Transmit networks packets to the Octeon interface
3209 * @param skbuff skbuff struct to be passed to network layer.
3210 * @param netdev pointer to network device
3211 * @returns whether the packet was transmitted to the device okay or not
3212 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3213 */
3214static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3215{
3216 struct lio *lio;
3217 struct octnet_buf_free_info *finfo;
3218 union octnic_cmd_setup cmdsetup;
3219 struct octnic_data_pkt ndata;
3220 struct octeon_device *oct;
3221 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003222 struct octeon_instr_irh *irh;
3223 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003224 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003225 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003226 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003227 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003228 u32 tag = 0;
3229
3230 lio = GET_LIO(netdev);
3231 oct = lio->oct_dev;
3232
3233 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003234 q_idx = skb->queue_mapping;
3235 q_idx = (q_idx % (lio->linfo.num_txpciq));
3236 tag = q_idx;
3237 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003238 } else {
3239 iq_no = lio->txq;
3240 }
3241
3242 stats = &oct->instr_queue[iq_no]->stats;
3243
3244 /* Check for all conditions in which the current packet cannot be
3245 * transmitted.
3246 */
3247 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003248 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003249 (skb->len <= 0)) {
3250 netif_info(lio, tx_err, lio->netdev,
3251 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003252 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003253 goto lio_xmit_failed;
3254 }
3255
3256 /* Use space in skb->cb to store info used to unmap and
3257 * free the buffers.
3258 */
3259 finfo = (struct octnet_buf_free_info *)skb->cb;
3260 finfo->lio = lio;
3261 finfo->skb = skb;
3262 finfo->sc = NULL;
3263
3264 /* Prepare the attributes for the data to be passed to OSI. */
3265 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3266
3267 ndata.buf = (void *)finfo;
3268
3269 ndata.q_no = iq_no;
3270
3271 if (netif_is_multiqueue(netdev)) {
3272 if (octnet_iq_is_full(oct, ndata.q_no)) {
3273 /* defer sending if queue is full */
3274 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3275 ndata.q_no);
3276 stats->tx_iq_busy++;
3277 return NETDEV_TX_BUSY;
3278 }
3279 } else {
3280 if (octnet_iq_is_full(oct, lio->txq)) {
3281 /* defer sending if queue is full */
3282 stats->tx_iq_busy++;
3283 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003284 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003285 return NETDEV_TX_BUSY;
3286 }
3287 }
3288 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003289 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003290 */
3291
3292 ndata.datasize = skb->len;
3293
3294 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003295 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003296
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003297 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3298 if (skb->encapsulation) {
3299 cmdsetup.s.tnl_csum = 1;
3300 stats->tx_vxlan++;
3301 } else {
3302 cmdsetup.s.transport_csum = 1;
3303 }
3304 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003305 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3306 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3307 cmdsetup.s.timestamp = 1;
3308 }
3309
3310 if (skb_shinfo(skb)->nr_frags == 0) {
3311 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003312 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003313
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003314 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003315 dptr = dma_map_single(&oct->pci_dev->dev,
3316 skb->data,
3317 skb->len,
3318 DMA_TO_DEVICE);
3319 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003320 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3321 __func__);
3322 return NETDEV_TX_BUSY;
3323 }
3324
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003325 if (OCTEON_CN23XX_PF(oct))
3326 ndata.cmd.cmd3.dptr = dptr;
3327 else
3328 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003329 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003330 ndata.reqtype = REQTYPE_NORESP_NET;
3331
3332 } else {
3333 int i, frags;
3334 struct skb_frag_struct *frag;
3335 struct octnic_gather *g;
3336
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003337 spin_lock(&lio->glist_lock[q_idx]);
3338 g = (struct octnic_gather *)
3339 list_delete_head(&lio->glist[q_idx]);
3340 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003341
3342 if (!g) {
3343 netif_info(lio, tx_err, lio->netdev,
3344 "Transmit scatter gather: glist null!\n");
3345 goto lio_xmit_failed;
3346 }
3347
3348 cmdsetup.s.gather = 1;
3349 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003350 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003351
3352 memset(g->sg, 0, g->sg_size);
3353
3354 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3355 skb->data,
3356 (skb->len - skb->data_len),
3357 DMA_TO_DEVICE);
3358 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3359 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3360 __func__);
3361 return NETDEV_TX_BUSY;
3362 }
3363 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3364
3365 frags = skb_shinfo(skb)->nr_frags;
3366 i = 1;
3367 while (frags--) {
3368 frag = &skb_shinfo(skb)->frags[i - 1];
3369
3370 g->sg[(i >> 2)].ptr[(i & 3)] =
3371 dma_map_page(&oct->pci_dev->dev,
3372 frag->page.p,
3373 frag->page_offset,
3374 frag->size,
3375 DMA_TO_DEVICE);
3376
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003377 if (dma_mapping_error(&oct->pci_dev->dev,
3378 g->sg[i >> 2].ptr[i & 3])) {
3379 dma_unmap_single(&oct->pci_dev->dev,
3380 g->sg[0].ptr[0],
3381 skb->len - skb->data_len,
3382 DMA_TO_DEVICE);
3383 for (j = 1; j < i; j++) {
3384 frag = &skb_shinfo(skb)->frags[j - 1];
3385 dma_unmap_page(&oct->pci_dev->dev,
3386 g->sg[j >> 2].ptr[j & 3],
3387 frag->size,
3388 DMA_TO_DEVICE);
3389 }
3390 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3391 __func__);
3392 return NETDEV_TX_BUSY;
3393 }
3394
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003395 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3396 i++;
3397 }
3398
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003399 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003400
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003401 if (OCTEON_CN23XX_PF(oct))
3402 ndata.cmd.cmd3.dptr = dptr;
3403 else
3404 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003405 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003406 finfo->g = g;
3407
3408 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3409 }
3410
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003411 if (OCTEON_CN23XX_PF(oct)) {
3412 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3413 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3414 } else {
3415 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3416 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3417 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003418
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003419 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003420 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3421 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003422 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003423 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003424
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003425 /* HW insert VLAN tag */
3426 if (skb_vlan_tag_present(skb)) {
3427 irh->priority = skb_vlan_tag_get(skb) >> 13;
3428 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3429 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003430
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003431 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003432 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003433 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003434 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003435 if (status == IQ_SEND_FAILED)
3436 goto lio_xmit_failed;
3437
3438 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3439
3440 if (status == IQ_SEND_STOP)
3441 stop_q(lio->netdev, q_idx);
3442
Florian Westphal860e9532016-05-03 16:33:13 +02003443 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003444
Satanand Burla80c8eae2017-01-26 11:52:35 -08003445 if (tx_info->s.gso_segs)
3446 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003447 else
3448 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003449 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003450
3451 return NETDEV_TX_OK;
3452
3453lio_xmit_failed:
3454 stats->tx_dropped++;
3455 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3456 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003457 if (dptr)
3458 dma_unmap_single(&oct->pci_dev->dev, dptr,
3459 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003460 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003461 return NETDEV_TX_OK;
3462}
3463
3464/** \brief Network device Tx timeout
3465 * @param netdev pointer to network device
3466 */
3467static void liquidio_tx_timeout(struct net_device *netdev)
3468{
3469 struct lio *lio;
3470
3471 lio = GET_LIO(netdev);
3472
3473 netif_info(lio, tx_err, lio->netdev,
3474 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3475 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003476 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003477 txqs_wake(netdev);
3478}
3479
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003480static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3481 __be16 proto __attribute__((unused)),
3482 u16 vid)
3483{
3484 struct lio *lio = GET_LIO(netdev);
3485 struct octeon_device *oct = lio->oct_dev;
3486 struct octnic_ctrl_pkt nctrl;
3487 int ret = 0;
3488
3489 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3490
3491 nctrl.ncmd.u64 = 0;
3492 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3493 nctrl.ncmd.s.param1 = vid;
3494 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3495 nctrl.wait_time = 100;
3496 nctrl.netpndev = (u64)netdev;
3497 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3498
3499 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3500 if (ret < 0) {
3501 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3502 ret);
3503 }
3504
3505 return ret;
3506}
3507
3508static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3509 __be16 proto __attribute__((unused)),
3510 u16 vid)
3511{
3512 struct lio *lio = GET_LIO(netdev);
3513 struct octeon_device *oct = lio->oct_dev;
3514 struct octnic_ctrl_pkt nctrl;
3515 int ret = 0;
3516
3517 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3518
3519 nctrl.ncmd.u64 = 0;
3520 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3521 nctrl.ncmd.s.param1 = vid;
3522 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3523 nctrl.wait_time = 100;
3524 nctrl.netpndev = (u64)netdev;
3525 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3526
3527 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3528 if (ret < 0) {
3529 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3530 ret);
3531 }
3532 return ret;
3533}
3534
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003535/** Sending command to enable/disable RX checksum offload
3536 * @param netdev pointer to network device
3537 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3538 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3539 * OCTNET_CMD_RXCSUM_DISABLE
3540 * @returns SUCCESS or FAILURE
3541 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003542static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3543 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003544{
3545 struct lio *lio = GET_LIO(netdev);
3546 struct octeon_device *oct = lio->oct_dev;
3547 struct octnic_ctrl_pkt nctrl;
3548 int ret = 0;
3549
Felix Manlunas0c264582017-04-06 19:22:22 -07003550 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3551
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003552 nctrl.ncmd.u64 = 0;
3553 nctrl.ncmd.s.cmd = command;
3554 nctrl.ncmd.s.param1 = rx_cmd;
3555 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3556 nctrl.wait_time = 100;
3557 nctrl.netpndev = (u64)netdev;
3558 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3559
3560 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3561 if (ret < 0) {
3562 dev_err(&oct->pci_dev->dev,
3563 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3564 ret);
3565 }
3566 return ret;
3567}
3568
3569/** Sending command to add/delete VxLAN UDP port to firmware
3570 * @param netdev pointer to network device
3571 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3572 * @param vxlan_port VxLAN port to be added or deleted
3573 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3574 * OCTNET_CMD_VXLAN_PORT_DEL
3575 * @returns SUCCESS or FAILURE
3576 */
3577static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3578 u16 vxlan_port, u8 vxlan_cmd_bit)
3579{
3580 struct lio *lio = GET_LIO(netdev);
3581 struct octeon_device *oct = lio->oct_dev;
3582 struct octnic_ctrl_pkt nctrl;
3583 int ret = 0;
3584
Felix Manlunas0c264582017-04-06 19:22:22 -07003585 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3586
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003587 nctrl.ncmd.u64 = 0;
3588 nctrl.ncmd.s.cmd = command;
3589 nctrl.ncmd.s.more = vxlan_cmd_bit;
3590 nctrl.ncmd.s.param1 = vxlan_port;
3591 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3592 nctrl.wait_time = 100;
3593 nctrl.netpndev = (u64)netdev;
3594 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3595
3596 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3597 if (ret < 0) {
3598 dev_err(&oct->pci_dev->dev,
3599 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3600 ret);
3601 }
3602 return ret;
3603}
3604
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003605/** \brief Net device fix features
3606 * @param netdev pointer to network device
3607 * @param request features requested
3608 * @returns updated features list
3609 */
3610static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3611 netdev_features_t request)
3612{
3613 struct lio *lio = netdev_priv(netdev);
3614
3615 if ((request & NETIF_F_RXCSUM) &&
3616 !(lio->dev_capability & NETIF_F_RXCSUM))
3617 request &= ~NETIF_F_RXCSUM;
3618
3619 if ((request & NETIF_F_HW_CSUM) &&
3620 !(lio->dev_capability & NETIF_F_HW_CSUM))
3621 request &= ~NETIF_F_HW_CSUM;
3622
3623 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3624 request &= ~NETIF_F_TSO;
3625
3626 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3627 request &= ~NETIF_F_TSO6;
3628
3629 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3630 request &= ~NETIF_F_LRO;
3631
3632 /*Disable LRO if RXCSUM is off */
3633 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3634 (lio->dev_capability & NETIF_F_LRO))
3635 request &= ~NETIF_F_LRO;
3636
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003637 if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3638 !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
3639 request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3640
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003641 return request;
3642}
3643
3644/** \brief Net device set features
3645 * @param netdev pointer to network device
3646 * @param features features to enable/disable
3647 */
3648static int liquidio_set_features(struct net_device *netdev,
3649 netdev_features_t features)
3650{
3651 struct lio *lio = netdev_priv(netdev);
3652
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003653 if ((features & NETIF_F_LRO) &&
3654 (lio->dev_capability & NETIF_F_LRO) &&
3655 !(netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003656 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3657 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003658 else if (!(features & NETIF_F_LRO) &&
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003659 (lio->dev_capability & NETIF_F_LRO) &&
3660 (netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003661 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3662 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003663
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003664 /* Sending command to firmware to enable/disable RX checksum
3665 * offload settings using ethtool
3666 */
3667 if (!(netdev->features & NETIF_F_RXCSUM) &&
3668 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3669 (features & NETIF_F_RXCSUM))
3670 liquidio_set_rxcsum_command(netdev,
3671 OCTNET_CMD_TNL_RX_CSUM_CTL,
3672 OCTNET_CMD_RXCSUM_ENABLE);
3673 else if ((netdev->features & NETIF_F_RXCSUM) &&
3674 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3675 !(features & NETIF_F_RXCSUM))
3676 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3677 OCTNET_CMD_RXCSUM_DISABLE);
3678
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003679 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3680 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3681 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3682 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3683 OCTNET_CMD_VLAN_FILTER_ENABLE);
3684 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3685 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3686 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3687 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3688 OCTNET_CMD_VLAN_FILTER_DISABLE);
3689
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003690 return 0;
3691}
3692
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003693static void liquidio_add_vxlan_port(struct net_device *netdev,
3694 struct udp_tunnel_info *ti)
3695{
3696 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3697 return;
3698
3699 liquidio_vxlan_port_command(netdev,
3700 OCTNET_CMD_VXLAN_PORT_CONFIG,
3701 htons(ti->port),
3702 OCTNET_CMD_VXLAN_PORT_ADD);
3703}
3704
3705static void liquidio_del_vxlan_port(struct net_device *netdev,
3706 struct udp_tunnel_info *ti)
3707{
3708 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3709 return;
3710
3711 liquidio_vxlan_port_command(netdev,
3712 OCTNET_CMD_VXLAN_PORT_CONFIG,
3713 htons(ti->port),
3714 OCTNET_CMD_VXLAN_PORT_DEL);
3715}
3716
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003717static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3718 u8 *mac, bool is_admin_assigned)
3719{
3720 struct lio *lio = GET_LIO(netdev);
3721 struct octeon_device *oct = lio->oct_dev;
3722 struct octnic_ctrl_pkt nctrl;
3723
3724 if (!is_valid_ether_addr(mac))
3725 return -EINVAL;
3726
3727 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3728 return -EINVAL;
3729
3730 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3731
3732 nctrl.ncmd.u64 = 0;
3733 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3734 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3735 nctrl.ncmd.s.param1 = vfidx + 1;
3736 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3737 nctrl.ncmd.s.more = 1;
3738 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003739 nctrl.netpndev = (u64)netdev;
3740 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003741 nctrl.wait_time = LIO_CMD_WAIT_TM;
3742
3743 nctrl.udd[0] = 0;
3744 /* The MAC Address is presented in network byte order. */
3745 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3746
3747 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3748
3749 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3750
3751 return 0;
3752}
3753
3754static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3755{
3756 struct lio *lio = GET_LIO(netdev);
3757 struct octeon_device *oct = lio->oct_dev;
3758 int retval;
3759
Felix Manlunas0d9a5992017-05-16 11:28:00 -07003760 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3761 return -EINVAL;
3762
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003763 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3764 if (!retval)
3765 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3766
3767 return retval;
3768}
3769
3770static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3771 u16 vlan, u8 qos, __be16 vlan_proto)
3772{
3773 struct lio *lio = GET_LIO(netdev);
3774 struct octeon_device *oct = lio->oct_dev;
3775 struct octnic_ctrl_pkt nctrl;
3776 u16 vlantci;
3777
3778 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3779 return -EINVAL;
3780
3781 if (vlan_proto != htons(ETH_P_8021Q))
3782 return -EPROTONOSUPPORT;
3783
3784 if (vlan >= VLAN_N_VID || qos > 7)
3785 return -EINVAL;
3786
3787 if (vlan)
3788 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3789 else
3790 vlantci = 0;
3791
3792 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3793 return 0;
3794
3795 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3796
3797 if (vlan)
3798 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3799 else
3800 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3801
3802 nctrl.ncmd.s.param1 = vlantci;
3803 nctrl.ncmd.s.param2 =
3804 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3805 nctrl.ncmd.s.more = 0;
3806 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3807 nctrl.cb_fn = 0;
3808 nctrl.wait_time = LIO_CMD_WAIT_TM;
3809
3810 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3811
3812 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3813
3814 return 0;
3815}
3816
3817static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3818 struct ifla_vf_info *ivi)
3819{
3820 struct lio *lio = GET_LIO(netdev);
3821 struct octeon_device *oct = lio->oct_dev;
3822 u8 *macaddr;
3823
3824 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3825 return -EINVAL;
3826
3827 ivi->vf = vfidx;
3828 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3829 ether_addr_copy(&ivi->mac[0], macaddr);
3830 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3831 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3832 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3833 return 0;
3834}
3835
3836static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3837 int linkstate)
3838{
3839 struct lio *lio = GET_LIO(netdev);
3840 struct octeon_device *oct = lio->oct_dev;
3841 struct octnic_ctrl_pkt nctrl;
3842
3843 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3844 return -EINVAL;
3845
3846 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3847 return 0;
3848
3849 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3850 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3851 nctrl.ncmd.s.param1 =
3852 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3853 nctrl.ncmd.s.param2 = linkstate;
3854 nctrl.ncmd.s.more = 0;
3855 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3856 nctrl.cb_fn = 0;
3857 nctrl.wait_time = LIO_CMD_WAIT_TM;
3858
3859 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3860
3861 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3862
3863 return 0;
3864}
3865
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003866static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003867 .ndo_open = liquidio_open,
3868 .ndo_stop = liquidio_stop,
3869 .ndo_start_xmit = liquidio_xmit,
3870 .ndo_get_stats = liquidio_get_stats,
3871 .ndo_set_mac_address = liquidio_set_mac,
3872 .ndo_set_rx_mode = liquidio_set_mcast_list,
3873 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003874
3875 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3876 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003877 .ndo_change_mtu = liquidio_change_mtu,
3878 .ndo_do_ioctl = liquidio_ioctl,
3879 .ndo_fix_features = liquidio_fix_features,
3880 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003881 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3882 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003883 .ndo_set_vf_mac = liquidio_set_vf_mac,
3884 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3885 .ndo_get_vf_config = liquidio_get_vf_config,
3886 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003887};
3888
3889/** \brief Entry point for the liquidio module
3890 */
3891static int __init liquidio_init(void)
3892{
3893 int i;
3894 struct handshake *hs;
3895
3896 init_completion(&first_stage);
3897
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003898 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003899
3900 if (liquidio_init_pci())
3901 return -EINVAL;
3902
3903 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3904
3905 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3906 hs = &handshake[i];
3907 if (hs->pci_dev) {
3908 wait_for_completion(&hs->init);
3909 if (!hs->init_ok) {
3910 /* init handshake failed */
3911 dev_err(&hs->pci_dev->dev,
3912 "Failed to init device\n");
3913 liquidio_deinit_pci();
3914 return -EIO;
3915 }
3916 }
3917 }
3918
3919 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3920 hs = &handshake[i];
3921 if (hs->pci_dev) {
3922 wait_for_completion_timeout(&hs->started,
3923 msecs_to_jiffies(30000));
3924 if (!hs->started_ok) {
3925 /* starter handshake failed */
3926 dev_err(&hs->pci_dev->dev,
3927 "Firmware failed to start\n");
3928 liquidio_deinit_pci();
3929 return -EIO;
3930 }
3931 }
3932 }
3933
3934 return 0;
3935}
3936
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003937static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003938{
3939 struct octeon_device *oct = (struct octeon_device *)buf;
3940 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003941 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003942 union oct_link_status *ls;
3943 int i;
3944
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003945 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003946 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3947 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003948 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003949 goto nic_info_err;
3950 }
3951
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003952 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003953 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3954 OCT_DROQ_INFO_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003955
3956 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003957 for (i = 0; i < oct->ifcount; i++) {
3958 if (oct->props[i].gmxport == gmxport) {
3959 update_link_status(oct->props[i].netdev, ls);
3960 break;
3961 }
3962 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003963
3964nic_info_err:
3965 for (i = 0; i < recv_pkt->buffer_count; i++)
3966 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3967 octeon_free_recv_info(recv_info);
3968 return 0;
3969}
3970
3971/**
3972 * \brief Setup network interfaces
3973 * @param octeon_dev octeon device
3974 *
3975 * Called during init time for each device. It assumes the NIC
3976 * is already up and running. The link information for each
3977 * interface is passed in link_info.
3978 */
3979static int setup_nic_devices(struct octeon_device *octeon_dev)
3980{
3981 struct lio *lio = NULL;
3982 struct net_device *netdev;
3983 u8 mac[6], i, j;
3984 struct octeon_soft_command *sc;
3985 struct liquidio_if_cfg_context *ctx;
3986 struct liquidio_if_cfg_resp *resp;
3987 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003988 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003989 union oct_nic_if_cfg if_cfg;
3990 unsigned int base_queue;
3991 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003992 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003993 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003994 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003995
3996 /* This is to handle link status changes */
3997 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3998 OPCODE_NIC_INFO,
3999 lio_nic_info, octeon_dev);
4000
4001 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
4002 * They are handled directly.
4003 */
4004 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
4005 free_netbuf);
4006
4007 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
4008 free_netsgbuf);
4009
4010 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
4011 free_netsgbuf_with_resp);
4012
4013 for (i = 0; i < octeon_dev->ifcount; i++) {
4014 resp_size = sizeof(struct liquidio_if_cfg_resp);
4015 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004016 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004017 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004018 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004019 resp_size, ctx_size);
4020 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
4021 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004022 vdata = (struct lio_version *)sc->virtdptr;
4023
4024 *((u64 *)vdata) = 0;
4025 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
4026 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
4027 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004028
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004029 if (OCTEON_CN23XX_PF(octeon_dev)) {
4030 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
4031 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
4032 base_queue = octeon_dev->sriov_info.pf_srn;
4033
4034 gmx_port_id = octeon_dev->pf_num;
4035 ifidx_or_pfnum = octeon_dev->pf_num;
4036 } else {
4037 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
4038 octeon_get_conf(octeon_dev), i);
4039 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
4040 octeon_get_conf(octeon_dev), i);
4041 base_queue = CFG_GET_BASE_QUE_NIC_IF(
4042 octeon_get_conf(octeon_dev), i);
4043 gmx_port_id = CFG_GET_GMXID_NIC_IF(
4044 octeon_get_conf(octeon_dev), i);
4045 ifidx_or_pfnum = i;
4046 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07004047
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004048 dev_dbg(&octeon_dev->pci_dev->dev,
4049 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004050 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07004051 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004052 ctx->octeon_id = lio_get_device_id(octeon_dev);
4053 init_waitqueue_head(&ctx->wc);
4054
4055 if_cfg.u64 = 0;
4056 if_cfg.s.num_iqueues = num_iqueues;
4057 if_cfg.s.num_oqueues = num_oqueues;
4058 if_cfg.s.base_queue = base_queue;
4059 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004060
4061 sc->iq_no = 0;
4062
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004063 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004064 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004065 if_cfg.u64, 0);
4066
4067 sc->callback = if_cfg_callback;
4068 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07004069 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004070
4071 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07004072 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004073 dev_err(&octeon_dev->pci_dev->dev,
4074 "iq/oq config failed status: %x\n",
4075 retval);
4076 /* Soft instr is freed by driver in case of failure. */
4077 goto setup_nic_dev_fail;
4078 }
4079
4080 /* Sleep on a wait queue till the cond flag indicates that the
4081 * response arrived or timed-out.
4082 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004083 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
4084 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
4085 goto setup_nic_wait_intr;
4086 }
4087
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004088 retval = resp->status;
4089 if (retval) {
4090 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
4091 goto setup_nic_dev_fail;
4092 }
4093
4094 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
4095 (sizeof(struct liquidio_if_cfg_info)) >> 3);
4096
4097 num_iqueues = hweight64(resp->cfg_info.iqmask);
4098 num_oqueues = hweight64(resp->cfg_info.oqmask);
4099
4100 if (!(num_iqueues) || !(num_oqueues)) {
4101 dev_err(&octeon_dev->pci_dev->dev,
4102 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
4103 resp->cfg_info.iqmask,
4104 resp->cfg_info.oqmask);
4105 goto setup_nic_dev_fail;
4106 }
4107 dev_dbg(&octeon_dev->pci_dev->dev,
4108 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
4109 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
4110 num_iqueues, num_oqueues);
4111 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
4112
4113 if (!netdev) {
4114 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
4115 goto setup_nic_dev_fail;
4116 }
4117
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004118 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004119
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004120 /* Associate the routines that will handle different
4121 * netdev tasks.
4122 */
4123 netdev->netdev_ops = &lionetdevops;
4124
4125 lio = GET_LIO(netdev);
4126
4127 memset(lio, 0, sizeof(struct lio));
4128
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004129 lio->ifidx = ifidx_or_pfnum;
4130
4131 props = &octeon_dev->props[i];
4132 props->gmxport = resp->cfg_info.linfo.gmxport;
4133 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004134
4135 lio->linfo.num_rxpciq = num_oqueues;
4136 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004137 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004138 lio->linfo.rxpciq[j].u64 =
4139 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004140 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004141 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004142 lio->linfo.txpciq[j].u64 =
4143 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004144 }
4145 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4146 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4147 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4148
4149 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4150
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004151 if (OCTEON_CN23XX_PF(octeon_dev) ||
4152 OCTEON_CN6XXX(octeon_dev)) {
4153 lio->dev_capability = NETIF_F_HIGHDMA
4154 | NETIF_F_IP_CSUM
4155 | NETIF_F_IPV6_CSUM
4156 | NETIF_F_SG | NETIF_F_RXCSUM
4157 | NETIF_F_GRO
4158 | NETIF_F_TSO | NETIF_F_TSO6
4159 | NETIF_F_LRO;
4160 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004161 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4162
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004163 /* Copy of transmit encapsulation capabilities:
4164 * TSO, TSO6, Checksums for this device
4165 */
4166 lio->enc_dev_capability = NETIF_F_IP_CSUM
4167 | NETIF_F_IPV6_CSUM
4168 | NETIF_F_GSO_UDP_TUNNEL
4169 | NETIF_F_HW_CSUM | NETIF_F_SG
4170 | NETIF_F_RXCSUM
4171 | NETIF_F_TSO | NETIF_F_TSO6
4172 | NETIF_F_LRO;
4173
4174 netdev->hw_enc_features = (lio->enc_dev_capability &
4175 ~NETIF_F_LRO);
4176
4177 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4178
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004179 netdev->vlan_features = lio->dev_capability;
4180 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004181 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4182 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004183 NETIF_F_HW_VLAN_CTAG_TX;
4184
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004185 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4186
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004187 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004188 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4189 netdev->hw_features = netdev->hw_features &
4190 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004191
Jarod Wilson109cc162016-10-17 15:54:13 -04004192 /* MTU range: 68 - 16000 */
4193 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4194 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4195
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004196 /* Point to the properties for octeon device to which this
4197 * interface belongs.
4198 */
4199 lio->oct_dev = octeon_dev;
4200 lio->octprops = props;
4201 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004202
4203 dev_dbg(&octeon_dev->pci_dev->dev,
4204 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4205 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4206
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004207 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4208 u8 vfmac[ETH_ALEN];
4209
4210 random_ether_addr(&vfmac[0]);
4211 if (__liquidio_set_vf_mac(netdev, j,
4212 &vfmac[0], false)) {
4213 dev_err(&octeon_dev->pci_dev->dev,
4214 "Error setting VF%d MAC address\n",
4215 j);
4216 goto setup_nic_dev_fail;
4217 }
4218 }
4219
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004220 /* 64-bit swap required on LE machines */
4221 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4222 for (j = 0; j < 6; j++)
4223 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4224
4225 /* Copy MAC Address to OS network device structure */
4226
4227 ether_addr_copy(netdev->dev_addr, mac);
4228
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004229 /* By default all interfaces on a single Octeon uses the same
4230 * tx and rx queues
4231 */
4232 lio->txq = lio->linfo.txpciq[0].s.q_no;
4233 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004234 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004235 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4236 goto setup_nic_dev_fail;
4237 }
4238
4239 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4240
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004241 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4242 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4243
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004244 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004245 dev_err(&octeon_dev->pci_dev->dev,
4246 "Gather list allocation failed\n");
4247 goto setup_nic_dev_fail;
4248 }
4249
4250 /* Register ethtool support */
4251 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004252 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4253 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4254 else
4255 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004256
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004257 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004258 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4259 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004260
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07004261 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
4262 OCTNET_CMD_VLAN_FILTER_ENABLE);
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004263
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004264 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004265 liquidio_set_feature(netdev,
4266 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004267
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004268 if (setup_link_status_change_wq(netdev))
4269 goto setup_nic_dev_fail;
4270
Satanand Burla031d4f12017-03-22 11:31:13 -07004271 if (setup_rx_oom_poll_fn(netdev))
4272 goto setup_nic_dev_fail;
4273
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004274 /* Register the network device with the OS */
4275 if (register_netdev(netdev)) {
4276 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4277 goto setup_nic_dev_fail;
4278 }
4279
4280 dev_dbg(&octeon_dev->pci_dev->dev,
4281 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4282 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4283 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004284 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004285
4286 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4287
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004288 /* Sending command to firmware to enable Rx checksum offload
4289 * by default at the time of setup of Liquidio driver for
4290 * this device
4291 */
4292 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4293 OCTNET_CMD_RXCSUM_ENABLE);
4294 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4295 OCTNET_CMD_TXCSUM_ENABLE);
4296
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004297 dev_dbg(&octeon_dev->pci_dev->dev,
4298 "NIC ifidx:%d Setup successful\n", i);
4299
4300 octeon_free_soft_command(octeon_dev, sc);
4301 }
4302
4303 return 0;
4304
4305setup_nic_dev_fail:
4306
4307 octeon_free_soft_command(octeon_dev, sc);
4308
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004309setup_nic_wait_intr:
4310
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004311 while (i--) {
4312 dev_err(&octeon_dev->pci_dev->dev,
4313 "NIC ifidx:%d Setup failed\n", i);
4314 liquidio_destroy_nic_device(octeon_dev, i);
4315 }
4316 return -ENODEV;
4317}
4318
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004319#ifdef CONFIG_PCI_IOV
4320static int octeon_enable_sriov(struct octeon_device *oct)
4321{
4322 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4323 struct pci_dev *vfdev;
4324 int err;
4325 u32 u;
4326
4327 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4328 err = pci_enable_sriov(oct->pci_dev,
4329 oct->sriov_info.num_vfs_alloced);
4330 if (err) {
4331 dev_err(&oct->pci_dev->dev,
4332 "OCTEON: Failed to enable PCI sriov: %d\n",
4333 err);
4334 oct->sriov_info.num_vfs_alloced = 0;
4335 return err;
4336 }
4337 oct->sriov_info.sriov_enabled = 1;
4338
4339 /* init lookup table that maps DPI ring number to VF pci_dev
4340 * struct pointer
4341 */
4342 u = 0;
4343 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4344 OCTEON_CN23XX_VF_VID, NULL);
4345 while (vfdev) {
4346 if (vfdev->is_virtfn &&
4347 (vfdev->physfn == oct->pci_dev)) {
4348 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4349 vfdev;
4350 u += oct->sriov_info.rings_per_vf;
4351 }
4352 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4353 OCTEON_CN23XX_VF_VID, vfdev);
4354 }
4355 }
4356
4357 return num_vfs_alloced;
4358}
4359
4360static int lio_pci_sriov_disable(struct octeon_device *oct)
4361{
4362 int u;
4363
4364 if (pci_vfs_assigned(oct->pci_dev)) {
4365 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4366 return -EPERM;
4367 }
4368
4369 pci_disable_sriov(oct->pci_dev);
4370
4371 u = 0;
4372 while (u < MAX_POSSIBLE_VFS) {
4373 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4374 u += oct->sriov_info.rings_per_vf;
4375 }
4376
4377 oct->sriov_info.num_vfs_alloced = 0;
4378 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4379 oct->pf_num);
4380
4381 return 0;
4382}
4383
4384static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4385{
4386 struct octeon_device *oct = pci_get_drvdata(dev);
4387 int ret = 0;
4388
4389 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4390 (oct->sriov_info.sriov_enabled)) {
4391 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4392 oct->pf_num, num_vfs);
4393 return 0;
4394 }
4395
4396 if (!num_vfs) {
4397 ret = lio_pci_sriov_disable(oct);
4398 } else if (num_vfs > oct->sriov_info.max_vfs) {
4399 dev_err(&oct->pci_dev->dev,
4400 "OCTEON: Max allowed VFs:%d user requested:%d",
4401 oct->sriov_info.max_vfs, num_vfs);
4402 ret = -EPERM;
4403 } else {
4404 oct->sriov_info.num_vfs_alloced = num_vfs;
4405 ret = octeon_enable_sriov(oct);
4406 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4407 oct->pf_num, num_vfs);
4408 }
4409
4410 return ret;
4411}
4412#endif
4413
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004414/**
4415 * \brief initialize the NIC
4416 * @param oct octeon device
4417 *
4418 * This initialization routine is called once the Octeon device application is
4419 * up and running
4420 */
4421static int liquidio_init_nic_module(struct octeon_device *oct)
4422{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004423 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004424 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4425
4426 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4427
4428 /* only default iq and oq were initialized
4429 * initialize the rest as well
4430 */
4431 /* run port_config command for each port */
4432 oct->ifcount = num_nic_ports;
4433
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004434 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004435
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004436 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4437 oct->props[i].gmxport = -1;
4438
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004439 retval = setup_nic_devices(oct);
4440 if (retval) {
4441 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4442 goto octnet_init_failure;
4443 }
4444
4445 liquidio_ptp_init(oct);
4446
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004447 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4448
4449 return retval;
4450
4451octnet_init_failure:
4452
4453 oct->ifcount = 0;
4454
4455 return retval;
4456}
4457
4458/**
4459 * \brief starter callback that invokes the remaining initialization work after
4460 * the NIC is up and running.
4461 * @param octptr work struct work_struct
4462 */
4463static void nic_starter(struct work_struct *work)
4464{
4465 struct octeon_device *oct;
4466 struct cavium_wk *wk = (struct cavium_wk *)work;
4467
4468 oct = (struct octeon_device *)wk->ctxptr;
4469
4470 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4471 return;
4472
4473 /* If the status of the device is CORE_OK, the core
4474 * application has reported its application type. Call
4475 * any registered handlers now and move to the RUNNING
4476 * state.
4477 */
4478 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4479 schedule_delayed_work(&oct->nic_poll_work.work,
4480 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4481 return;
4482 }
4483
4484 atomic_set(&oct->status, OCT_DEV_RUNNING);
4485
4486 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4487 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4488
4489 if (liquidio_init_nic_module(oct))
4490 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4491 else
4492 handshake[oct->octeon_id].started_ok = 1;
4493 } else {
4494 dev_err(&oct->pci_dev->dev,
4495 "Unexpected application running on NIC (%d). Check firmware.\n",
4496 oct->app_mode);
4497 }
4498
4499 complete(&handshake[oct->octeon_id].started);
4500}
4501
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004502static int
4503octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4504{
4505 struct octeon_device *oct = (struct octeon_device *)buf;
4506 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4507 int i, notice, vf_idx;
Felix Manlunasbb54be52017-04-04 19:26:57 -07004508 bool cores_crashed;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004509 u64 *data, vf_num;
4510
4511 notice = recv_pkt->rh.r.ossp;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07004512 data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004513
4514 /* the first 64-bit word of data is the vf_num */
4515 vf_num = data[0];
4516 octeon_swap_8B_data(&vf_num, 1);
4517 vf_idx = (int)vf_num - 1;
4518
Felix Manlunasbb54be52017-04-04 19:26:57 -07004519 cores_crashed = READ_ONCE(oct->cores_crashed);
4520
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004521 if (notice == VF_DRV_LOADED) {
4522 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4523 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4524 dev_info(&oct->pci_dev->dev,
4525 "driver for VF%d was loaded\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004526 if (!cores_crashed)
4527 try_module_get(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004528 }
4529 } else if (notice == VF_DRV_REMOVED) {
4530 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4531 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4532 dev_info(&oct->pci_dev->dev,
4533 "driver for VF%d was removed\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004534 if (!cores_crashed)
4535 module_put(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004536 }
4537 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4538 u8 *b = (u8 *)&data[1];
4539
4540 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4541 dev_info(&oct->pci_dev->dev,
4542 "VF driver changed VF%d's MAC address to %pM\n",
4543 vf_idx, b + 2);
4544 }
4545
4546 for (i = 0; i < recv_pkt->buffer_count; i++)
4547 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4548 octeon_free_recv_info(recv_info);
4549
4550 return 0;
4551}
4552
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004553/**
4554 * \brief Device initialization for each Octeon device that is probed
4555 * @param octeon_dev octeon device
4556 */
4557static int octeon_device_init(struct octeon_device *octeon_dev)
4558{
4559 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004560 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004561 char bootcmd[] = "\n";
Rick Farringtonda1542b2017-08-11 18:43:14 -07004562 char *dbg_enb = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004563 struct octeon_device_priv *oct_priv =
4564 (struct octeon_device_priv *)octeon_dev->priv;
4565 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4566
4567 /* Enable access to the octeon device and make its DMA capability
4568 * known to the OS.
4569 */
4570 if (octeon_pci_os_setup(octeon_dev))
4571 return 1;
4572
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004573 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4574
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004575 /* Identify the Octeon type and map the BAR address space. */
4576 if (octeon_chip_specific_setup(octeon_dev)) {
4577 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4578 return 1;
4579 }
4580
4581 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4582
Rick Farringtone1e3ce62017-05-16 11:14:50 -07004583 /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
4584 * since that is what is required for the reference to be removed
4585 * during de-initialization (see 'octeon_destroy_resources').
4586 */
4587 octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
4588 PCI_SLOT(octeon_dev->pci_dev->devfn),
4589 PCI_FUNC(octeon_dev->pci_dev->devfn),
4590 true);
4591
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004592 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4593
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004594 if (OCTEON_CN23XX_PF(octeon_dev)) {
4595 if (!cn23xx_fw_loaded(octeon_dev)) {
4596 fw_loaded = 0;
Felix Manlunas7cc61db2017-03-23 13:26:28 -07004597 if (!fw_type_is_none()) {
4598 /* Do a soft reset of the Octeon device. */
4599 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4600 return 1;
4601 /* things might have changed */
4602 if (!cn23xx_fw_loaded(octeon_dev))
4603 fw_loaded = 0;
4604 else
4605 fw_loaded = 1;
4606 }
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004607 } else {
4608 fw_loaded = 1;
4609 }
4610 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004611 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004612 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004613
4614 /* Initialize the dispatch mechanism used to push packets arriving on
4615 * Octeon Output queues.
4616 */
4617 if (octeon_init_dispatch_list(octeon_dev))
4618 return 1;
4619
4620 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4621 OPCODE_NIC_CORE_DRV_ACTIVE,
4622 octeon_core_drv_init,
4623 octeon_dev);
4624
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004625 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4626 OPCODE_NIC_VF_DRV_NOTICE,
4627 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004628 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4629 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4630 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4631 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4632
4633 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4634
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004635 if (octeon_set_io_queues_off(octeon_dev)) {
4636 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4637 return 1;
4638 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004639
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004640 if (OCTEON_CN23XX_PF(octeon_dev)) {
4641 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4642 if (ret) {
4643 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4644 return ret;
4645 }
4646 }
4647
4648 /* Initialize soft command buffer pool
4649 */
4650 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4651 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4652 return 1;
4653 }
4654 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4655
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004656 /* Setup the data structures that manage this Octeon's Input queues. */
4657 if (octeon_setup_instr_queues(octeon_dev)) {
4658 dev_err(&octeon_dev->pci_dev->dev,
4659 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004660 return 1;
4661 }
4662 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4663
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004664 /* Initialize lists to manage the requests of different types that
4665 * arrive from user & kernel applications for this octeon device.
4666 */
4667 if (octeon_setup_response_list(octeon_dev)) {
4668 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4669 return 1;
4670 }
4671 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4672
4673 if (octeon_setup_output_queues(octeon_dev)) {
4674 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004675 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004676 }
4677
4678 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4679
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004680 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004681 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4682 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4683 return 1;
4684 }
4685 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4686
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004687 if (octeon_allocate_ioq_vector(octeon_dev)) {
4688 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4689 return 1;
4690 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004691 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004692
4693 } else {
4694 /* The input and output queue registers were setup earlier (the
4695 * queues were not enabled). Any additional registers
4696 * that need to be programmed should be done now.
4697 */
4698 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4699 if (ret) {
4700 dev_err(&octeon_dev->pci_dev->dev,
4701 "Failed to configure device registers\n");
4702 return ret;
4703 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004704 }
4705
4706 /* Initialize the tasklet that handles output queue packet processing.*/
4707 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4708 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4709 (unsigned long)octeon_dev);
4710
4711 /* Setup the interrupt handler and record the INT SUM register address
4712 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004713 if (octeon_setup_interrupt(octeon_dev))
4714 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004715
4716 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004717 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004718
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004719 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4720
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004721 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004722 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4723 if (ret) {
4724 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4725 return ret;
4726 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004727
4728 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4729
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004730 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4731 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4732 if (!ddr_timeout) {
4733 dev_info(&octeon_dev->pci_dev->dev,
4734 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4735 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004736
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004737 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004738
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004739 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4740 while (!ddr_timeout) {
4741 set_current_state(TASK_INTERRUPTIBLE);
4742 if (schedule_timeout(HZ / 10)) {
4743 /* user probably pressed Control-C */
4744 return 1;
4745 }
4746 }
4747 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4748 if (ret) {
4749 dev_err(&octeon_dev->pci_dev->dev,
4750 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4751 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004752 return 1;
4753 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004754
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004755 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4756 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4757 return 1;
4758 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004759
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004760 /* Divert uboot to take commands from host instead. */
4761 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004762
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004763 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4764 ret = octeon_init_consoles(octeon_dev);
4765 if (ret) {
4766 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4767 return 1;
4768 }
Rick Farringtonda1542b2017-08-11 18:43:14 -07004769 /* If console debug enabled, specify empty string to use default
4770 * enablement ELSE specify NULL string for 'disabled'.
4771 */
4772 dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
4773 ret = octeon_add_console(octeon_dev, 0, dbg_enb);
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004774 if (ret) {
4775 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4776 return 1;
Rick Farringtonda1542b2017-08-11 18:43:14 -07004777 } else if (octeon_console_debug_enabled(0)) {
4778 /* If console was added AND we're logging console output
4779 * then set our console print function.
4780 */
4781 octeon_dev->console[0].print = octeon_dbg_console_print;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004782 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004783
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004784 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004785
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004786 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4787 ret = load_firmware(octeon_dev);
4788 if (ret) {
4789 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4790 return 1;
4791 }
4792 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4793 * loaded
4794 */
4795 if (OCTEON_CN23XX_PF(octeon_dev))
4796 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4797 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004798 }
4799
4800 handshake[octeon_dev->octeon_id].init_ok = 1;
4801 complete(&handshake[octeon_dev->octeon_id].init);
4802
4803 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4804
4805 /* Send Credit for Octeon Output queues. Credits are always sent after
4806 * the output queue is enabled.
4807 */
4808 for (j = 0; j < octeon_dev->num_oqs; j++)
4809 writel(octeon_dev->droq[j]->max_count,
4810 octeon_dev->droq[j]->pkts_credit_reg);
4811
4812 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004813 return 0;
4814}
4815
4816/**
Rick Farringtonda1542b2017-08-11 18:43:14 -07004817 * \brief Debug console print function
4818 * @param octeon_dev octeon device
4819 * @param console_num console number
4820 * @param prefix first portion of line to display
4821 * @param suffix second portion of line to display
4822 *
4823 * The OCTEON debug console outputs entire lines (excluding '\n').
4824 * Normally, the line will be passed in the 'prefix' parameter.
4825 * However, due to buffering, it is possible for a line to be split into two
4826 * parts, in which case they will be passed as the 'prefix' parameter and
4827 * 'suffix' parameter.
4828 */
4829static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
4830 char *prefix, char *suffix)
4831{
4832 if (prefix && suffix)
4833 dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
4834 suffix);
4835 else if (prefix)
4836 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
4837 else if (suffix)
4838 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
4839
4840 return 0;
4841}
4842
4843/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004844 * \brief Exits the module
4845 */
4846static void __exit liquidio_exit(void)
4847{
4848 liquidio_deinit_pci();
4849
4850 pr_info("LiquidIO network module is now unloaded\n");
4851}
4852
4853module_init(liquidio_init);
4854module_exit(liquidio_exit);