blob: 89d4bbc8170758493de18ed21f59e1f20202d16b [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
Derek Chicklesd3961792017-08-14 12:17:56 -070062static char fw_type[LIO_MAX_FW_TYPE_LEN] = LIO_FW_NAME_TYPE_NIC;
63module_param_string(fw_type, fw_type, sizeof(fw_type), 0444);
64MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\". Use \"none\" to load firmware from flash.");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070065
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
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800178#ifdef CONFIG_PCI_IOV
179static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
180#endif
181
Rick Farringtonda1542b2017-08-11 18:43:14 -0700182static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
183 char *prefix, char *suffix);
184
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700185static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700186static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700187static void liquidio_remove(struct pci_dev *pdev);
188static int liquidio_probe(struct pci_dev *pdev,
189 const struct pci_device_id *ent);
Felix Manlunasbb54be52017-04-04 19:26:57 -0700190static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
191 int linkstate);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700192
193static struct handshake handshake[MAX_OCTEON_DEVICES];
194static struct completion first_stage;
195
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700196static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700197{
198 int q_no;
199 int reschedule = 0;
200 struct octeon_device *oct = (struct octeon_device *)pdev;
201 struct octeon_device_priv *oct_priv =
202 (struct octeon_device_priv *)oct->priv;
203
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700204 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800205 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700206 continue;
207 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
208 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700209 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700210
211 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
212 /* set time and cnt interrupt thresholds for this DROQ
213 * for NAPI
214 */
215 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
216
217 octeon_write_csr64(
218 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
219 0x5700000040ULL);
220 octeon_write_csr64(
221 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
222 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700223 }
224
225 if (reschedule)
226 tasklet_schedule(&oct_priv->droq_tasklet);
227}
228
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700229static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700230{
231 struct octeon_device_priv *oct_priv =
232 (struct octeon_device_priv *)oct->priv;
233 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
234 int i;
235
236 do {
237 pending_pkts = 0;
238
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700239 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800240 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700241 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700242 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700243 }
244 if (pkt_cnt > 0) {
245 pending_pkts += pkt_cnt;
246 tasklet_schedule(&oct_priv->droq_tasklet);
247 }
248 pkt_cnt = 0;
249 schedule_timeout_uninterruptible(1);
250
251 } while (retry-- && pending_pkts);
252
253 return pkt_cnt;
254}
255
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700256/**
257 * \brief Forces all IO queues off on a given device
258 * @param oct Pointer to Octeon device
259 */
260static void force_io_queues_off(struct octeon_device *oct)
261{
262 if ((oct->chip_id == OCTEON_CN66XX) ||
263 (oct->chip_id == OCTEON_CN68XX)) {
264 /* Reset the Enable bits for Input Queues. */
265 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
266
267 /* Reset the Enable bits for Output Queues. */
268 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
269 }
270}
271
272/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700273 * \brief Cause device to go quiet so it can be safely removed/reset/etc
274 * @param oct Pointer to Octeon device
275 */
276static inline void pcierror_quiesce_device(struct octeon_device *oct)
277{
278 int i;
279
280 /* Disable the input and output queues now. No more packets will
281 * arrive from Octeon, but we should wait for all packet processing
282 * to finish.
283 */
284 force_io_queues_off(oct);
285
286 /* To allow for in-flight requests */
287 schedule_timeout_uninterruptible(100);
288
289 if (wait_for_pending_requests(oct))
290 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
291
292 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700293 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700294 struct octeon_instr_queue *iq;
295
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800296 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700297 continue;
298 iq = oct->instr_queue[i];
299
300 if (atomic_read(&iq->instr_pending)) {
301 spin_lock_bh(&iq->lock);
302 iq->fill_cnt = 0;
303 iq->octeon_read_index = iq->host_write_index;
304 iq->stats.instr_processed +=
305 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700306 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700307 spin_unlock_bh(&iq->lock);
308 }
309 }
310
311 /* Force all pending ordered list requests to time out. */
312 lio_process_ordered_list(oct, 1);
313
314 /* We do not need to wait for output queue packets to be processed. */
315}
316
317/**
318 * \brief Cleanup PCI AER uncorrectable error status
319 * @param dev Pointer to PCI device
320 */
321static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
322{
323 int pos = 0x100;
324 u32 status, mask;
325
326 pr_info("%s :\n", __func__);
327
328 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
329 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
330 if (dev->error_state == pci_channel_io_normal)
331 status &= ~mask; /* Clear corresponding nonfatal bits */
332 else
333 status &= mask; /* Clear corresponding fatal bits */
334 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
335}
336
337/**
338 * \brief Stop all PCI IO to a given device
339 * @param dev Pointer to Octeon device
340 */
341static void stop_pci_io(struct octeon_device *oct)
342{
343 /* No more instructions will be forwarded. */
344 atomic_set(&oct->status, OCT_DEV_IN_RESET);
345
346 pci_disable_device(oct->pci_dev);
347
348 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700349 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700350
351 pcierror_quiesce_device(oct);
352
353 /* Release the interrupt line */
354 free_irq(oct->pci_dev->irq, oct);
355
356 if (oct->flags & LIO_FLAG_MSI_ENABLED)
357 pci_disable_msi(oct->pci_dev);
358
359 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
360 lio_get_state_string(&oct->status));
361
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700362 /* making it a common function for all OCTEON models */
363 cleanup_aer_uncorrect_error_status(oct->pci_dev);
364}
365
366/**
367 * \brief called when PCI error is detected
368 * @param pdev Pointer to PCI device
369 * @param state The current pci connection state
370 *
371 * This function is called after a PCI bus error affecting
372 * this device has been detected.
373 */
374static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
375 pci_channel_state_t state)
376{
377 struct octeon_device *oct = pci_get_drvdata(pdev);
378
379 /* Non-correctable Non-fatal errors */
380 if (state == pci_channel_io_normal) {
381 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
382 cleanup_aer_uncorrect_error_status(oct->pci_dev);
383 return PCI_ERS_RESULT_CAN_RECOVER;
384 }
385
386 /* Non-correctable Fatal errors */
387 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
388 stop_pci_io(oct);
389
390 /* Always return a DISCONNECT. There is no support for recovery but only
391 * for a clean shutdown.
392 */
393 return PCI_ERS_RESULT_DISCONNECT;
394}
395
396/**
397 * \brief mmio handler
398 * @param pdev Pointer to PCI device
399 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700400static pci_ers_result_t liquidio_pcie_mmio_enabled(
401 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700402{
403 /* We should never hit this since we never ask for a reset for a Fatal
404 * Error. We always return DISCONNECT in io_error above.
405 * But play safe and return RECOVERED for now.
406 */
407 return PCI_ERS_RESULT_RECOVERED;
408}
409
410/**
411 * \brief called after the pci bus has been reset.
412 * @param pdev Pointer to PCI device
413 *
414 * Restart the card from scratch, as if from a cold-boot. Implementation
415 * resembles the first-half of the octeon_resume routine.
416 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700417static pci_ers_result_t liquidio_pcie_slot_reset(
418 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700419{
420 /* We should never hit this since we never ask for a reset for a Fatal
421 * Error. We always return DISCONNECT in io_error above.
422 * But play safe and return RECOVERED for now.
423 */
424 return PCI_ERS_RESULT_RECOVERED;
425}
426
427/**
428 * \brief called when traffic can start flowing again.
429 * @param pdev Pointer to PCI device
430 *
431 * This callback is called when the error recovery driver tells us that
432 * its OK to resume normal operation. Implementation resembles the
433 * second-half of the octeon_resume routine.
434 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700435static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700436{
437 /* Nothing to be done here. */
438}
439
440#ifdef CONFIG_PM
441/**
442 * \brief called when suspending
443 * @param pdev Pointer to PCI device
444 * @param state state to suspend to
445 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700446static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
447 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700448{
449 return 0;
450}
451
452/**
453 * \brief called when resuming
454 * @param pdev Pointer to PCI device
455 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700456static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700457{
458 return 0;
459}
460#endif
461
462/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100463static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700464 .error_detected = liquidio_pcie_error_detected,
465 .mmio_enabled = liquidio_pcie_mmio_enabled,
466 .slot_reset = liquidio_pcie_slot_reset,
467 .resume = liquidio_pcie_resume,
468};
469
470static const struct pci_device_id liquidio_pci_tbl[] = {
471 { /* 68xx */
472 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
473 },
474 { /* 66xx */
475 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
476 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700477 { /* 23xx pf */
478 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
479 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700480 {
481 0, 0, 0, 0, 0, 0, 0
482 }
483};
484MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
485
486static struct pci_driver liquidio_pci_driver = {
487 .name = "LiquidIO",
488 .id_table = liquidio_pci_tbl,
489 .probe = liquidio_probe,
490 .remove = liquidio_remove,
491 .err_handler = &liquidio_err_handler, /* For AER */
492
493#ifdef CONFIG_PM
494 .suspend = liquidio_suspend,
495 .resume = liquidio_resume,
496#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800497#ifdef CONFIG_PCI_IOV
498 .sriov_configure = liquidio_enable_sriov,
499#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700500};
501
502/**
503 * \brief register PCI driver
504 */
505static int liquidio_init_pci(void)
506{
507 return pci_register_driver(&liquidio_pci_driver);
508}
509
510/**
511 * \brief unregister PCI driver
512 */
513static void liquidio_deinit_pci(void)
514{
515 pci_unregister_driver(&liquidio_pci_driver);
516}
517
518/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700519 * \brief Stop Tx queues
520 * @param netdev network device
521 */
522static inline void txqs_stop(struct net_device *netdev)
523{
524 if (netif_is_multiqueue(netdev)) {
525 int i;
526
527 for (i = 0; i < netdev->num_tx_queues; i++)
528 netif_stop_subqueue(netdev, i);
529 } else {
530 netif_stop_queue(netdev);
531 }
532}
533
534/**
535 * \brief Start Tx queues
536 * @param netdev network device
537 */
538static inline void txqs_start(struct net_device *netdev)
539{
540 if (netif_is_multiqueue(netdev)) {
541 int i;
542
543 for (i = 0; i < netdev->num_tx_queues; i++)
544 netif_start_subqueue(netdev, i);
545 } else {
546 netif_start_queue(netdev);
547 }
548}
549
550/**
551 * \brief Wake Tx queues
552 * @param netdev network device
553 */
554static inline void txqs_wake(struct net_device *netdev)
555{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700556 struct lio *lio = GET_LIO(netdev);
557
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700558 if (netif_is_multiqueue(netdev)) {
559 int i;
560
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700561 for (i = 0; i < netdev->num_tx_queues; i++) {
562 int qno = lio->linfo.txpciq[i %
Intiyaz Bashaa82457f2017-08-15 12:46:18 -0700563 lio->oct_dev->num_iqs].s.q_no;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700564
565 if (__netif_subqueue_stopped(netdev, i)) {
566 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
567 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700568 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700569 }
570 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700571 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700572 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
573 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700574 netif_wake_queue(netdev);
575 }
576}
577
578/**
579 * \brief Stop Tx queue
580 * @param netdev network device
581 */
582static void stop_txq(struct net_device *netdev)
583{
584 txqs_stop(netdev);
585}
586
587/**
588 * \brief Start Tx queue
589 * @param netdev network device
590 */
591static void start_txq(struct net_device *netdev)
592{
593 struct lio *lio = GET_LIO(netdev);
594
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700595 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700596 txqs_start(netdev);
597 return;
598 }
599}
600
601/**
602 * \brief Wake a queue
603 * @param netdev network device
604 * @param q which queue to wake
605 */
606static inline void wake_q(struct net_device *netdev, int q)
607{
608 if (netif_is_multiqueue(netdev))
609 netif_wake_subqueue(netdev, q);
610 else
611 netif_wake_queue(netdev);
612}
613
614/**
615 * \brief Stop a queue
616 * @param netdev network device
617 * @param q which queue to stop
618 */
619static inline void stop_q(struct net_device *netdev, int q)
620{
621 if (netif_is_multiqueue(netdev))
622 netif_stop_subqueue(netdev, q);
623 else
624 netif_stop_queue(netdev);
625}
626
627/**
628 * \brief Check Tx queue status, and take appropriate action
629 * @param lio per-network private data
630 * @returns 0 if full, number of queues woken up otherwise
631 */
632static inline int check_txq_status(struct lio *lio)
633{
634 int ret_val = 0;
635
636 if (netif_is_multiqueue(lio->netdev)) {
637 int numqs = lio->netdev->num_tx_queues;
638 int q, iq = 0;
639
640 /* check each sub-queue state */
641 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700642 iq = lio->linfo.txpciq[q %
Intiyaz Bashaa82457f2017-08-15 12:46:18 -0700643 lio->oct_dev->num_iqs].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700644 if (octnet_iq_is_full(lio->oct_dev, iq))
645 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700646 if (__netif_subqueue_stopped(lio->netdev, q)) {
647 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700648 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
649 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700650 ret_val++;
651 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700652 }
653 } else {
654 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
655 return 0;
656 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700657 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
658 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700659 ret_val = 1;
660 }
661 return ret_val;
662}
663
664/**
665 * Remove the node at the head of the list. The list would be empty at
666 * the end of this call if there are no more nodes in the list.
667 */
668static inline struct list_head *list_delete_head(struct list_head *root)
669{
670 struct list_head *node;
671
672 if ((root->prev == root) && (root->next == root))
673 node = NULL;
674 else
675 node = root->next;
676
677 if (node)
678 list_del(node);
679
680 return node;
681}
682
683/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700684 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700685 * @param lio per-network private data
686 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700687static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700688{
689 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700690 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700691
VSR Burru67e303e2017-03-06 18:45:59 -0800692 kfree(lio->glist_lock);
693 lio->glist_lock = NULL;
694
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700695 if (!lio->glist)
696 return;
697
698 for (i = 0; i < lio->linfo.num_txpciq; i++) {
699 do {
700 g = (struct octnic_gather *)
701 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800702 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700703 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700704 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800705
Felix Manlunas58ad3192017-03-20 19:04:48 -0700706 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
707 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800708 lio_dma_free(lio->oct_dev,
709 lio->glist_entry_size * lio->tx_qsize,
710 lio->glists_virt_base[i],
711 lio->glists_dma_base[i]);
712 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700713 }
714
VSR Burru67e303e2017-03-06 18:45:59 -0800715 kfree(lio->glists_virt_base);
716 lio->glists_virt_base = NULL;
717
718 kfree(lio->glists_dma_base);
719 lio->glists_dma_base = NULL;
720
721 kfree(lio->glist);
722 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700723}
724
725/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700726 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700727 * @param lio per-network private data
728 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700729static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700730{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700731 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700732 struct octnic_gather *g;
733
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700734 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
735 GFP_KERNEL);
736 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800737 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700738
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700739 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
740 GFP_KERNEL);
741 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800742 kfree(lio->glist_lock);
743 lio->glist_lock = NULL;
744 return -ENOMEM;
745 }
746
747 lio->glist_entry_size =
748 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
749
750 /* allocate memory to store virtual and dma base address of
751 * per glist consistent memory
752 */
753 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
754 GFP_KERNEL);
755 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
756 GFP_KERNEL);
757
758 if (!lio->glists_virt_base || !lio->glists_dma_base) {
759 delete_glists(lio);
760 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700761 }
762
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700763 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800764 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700765
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700766 spin_lock_init(&lio->glist_lock[i]);
767
768 INIT_LIST_HEAD(&lio->glist[i]);
769
VSR Burru67e303e2017-03-06 18:45:59 -0800770 lio->glists_virt_base[i] =
771 lio_dma_alloc(oct,
772 lio->glist_entry_size * lio->tx_qsize,
773 &lio->glists_dma_base[i]);
774
775 if (!lio->glists_virt_base[i]) {
776 delete_glists(lio);
777 return -ENOMEM;
778 }
779
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700780 for (j = 0; j < lio->tx_qsize; j++) {
781 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
782 numa_node);
783 if (!g)
784 g = kzalloc(sizeof(*g), GFP_KERNEL);
785 if (!g)
786 break;
787
VSR Burru67e303e2017-03-06 18:45:59 -0800788 g->sg = lio->glists_virt_base[i] +
789 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700790
VSR Burru67e303e2017-03-06 18:45:59 -0800791 g->sg_dma_ptr = lio->glists_dma_base[i] +
792 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700793
794 list_add_tail(&g->list, &lio->glist[i]);
795 }
796
797 if (j != lio->tx_qsize) {
798 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800799 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700800 }
801 }
802
803 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700804}
805
806/**
807 * \brief Print link information
808 * @param netdev network device
809 */
810static void print_link_info(struct net_device *netdev)
811{
812 struct lio *lio = GET_LIO(netdev);
813
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -0700814 if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
815 ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700816 struct oct_link_info *linfo = &lio->linfo;
817
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700818 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700819 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
820 linfo->link.s.speed,
821 (linfo->link.s.duplex) ? "Full" : "Half");
822 } else {
823 netif_info(lio, link, lio->netdev, "Link Down\n");
824 }
825 }
826}
827
828/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700829 * \brief Routine to notify MTU change
830 * @param work work_struct data structure
831 */
832static void octnet_link_status_change(struct work_struct *work)
833{
834 struct cavium_wk *wk = (struct cavium_wk *)work;
835 struct lio *lio = (struct lio *)wk->ctxptr;
836
837 rtnl_lock();
838 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
839 rtnl_unlock();
840}
841
842/**
843 * \brief Sets up the mtu status change work
844 * @param netdev network device
845 */
846static inline int setup_link_status_change_wq(struct net_device *netdev)
847{
848 struct lio *lio = GET_LIO(netdev);
849 struct octeon_device *oct = lio->oct_dev;
850
851 lio->link_status_wq.wq = alloc_workqueue("link-status",
852 WQ_MEM_RECLAIM, 0);
853 if (!lio->link_status_wq.wq) {
854 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
855 return -1;
856 }
857 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
858 octnet_link_status_change);
859 lio->link_status_wq.wk.ctxptr = lio;
860
861 return 0;
862}
863
864static inline void cleanup_link_status_change_wq(struct net_device *netdev)
865{
866 struct lio *lio = GET_LIO(netdev);
867
868 if (lio->link_status_wq.wq) {
869 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
870 destroy_workqueue(lio->link_status_wq.wq);
871 }
872}
873
874/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700875 * \brief Update link status
876 * @param netdev network device
877 * @param ls link status structure
878 *
879 * Called on receipt of a link status response from the core application to
880 * update each interface's link status.
881 */
882static inline void update_link_status(struct net_device *netdev,
883 union oct_link_status *ls)
884{
885 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700886 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700887
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700888 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700889
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700890 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700891 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700892 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700893
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700894 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700895 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700896 txqs_wake(netdev);
897 } else {
898 netif_carrier_off(netdev);
899 stop_txq(netdev);
900 }
901 }
902}
903
Felix Manlunasbb54be52017-04-04 19:26:57 -0700904static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
905{
906 struct octeon_device *other_oct;
907
908 other_oct = lio_get_device(oct->octeon_id + 1);
909
910 if (other_oct && other_oct->pci_dev) {
911 int oct_busnum, other_oct_busnum;
912
913 oct_busnum = oct->pci_dev->bus->number;
914 other_oct_busnum = other_oct->pci_dev->bus->number;
915
916 if (oct_busnum == other_oct_busnum) {
917 int oct_slot, other_oct_slot;
918
919 oct_slot = PCI_SLOT(oct->pci_dev->devfn);
920 other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
921
922 if (oct_slot == other_oct_slot)
923 return other_oct;
924 }
925 }
926
927 return NULL;
928}
929
930static void disable_all_vf_links(struct octeon_device *oct)
931{
932 struct net_device *netdev;
933 int max_vfs, vf, i;
934
935 if (!oct)
936 return;
937
938 max_vfs = oct->sriov_info.max_vfs;
939
940 for (i = 0; i < oct->ifcount; i++) {
941 netdev = oct->props[i].netdev;
942 if (!netdev)
943 continue;
944
945 for (vf = 0; vf < max_vfs; vf++)
946 liquidio_set_vf_link_state(netdev, vf,
947 IFLA_VF_LINK_STATE_DISABLE);
948 }
949}
950
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700951static int liquidio_watchdog(void *param)
952{
Felix Manlunasbb54be52017-04-04 19:26:57 -0700953 bool err_msg_was_printed[LIO_MAX_CORES];
954 u16 mask_of_crashed_or_stuck_cores = 0;
955 bool all_vf_links_are_disabled = false;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700956 struct octeon_device *oct = param;
Felix Manlunasbb54be52017-04-04 19:26:57 -0700957 struct octeon_device *other_oct;
958#ifdef CONFIG_MODULE_UNLOAD
959 long refcount, vfs_referencing_pf;
960 u64 vfs_mask1, vfs_mask2;
961#endif
962 int core;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700963
Felix Manlunasbb54be52017-04-04 19:26:57 -0700964 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700965
966 while (!kthread_should_stop()) {
Felix Manlunasbb54be52017-04-04 19:26:57 -0700967 /* sleep for a couple of seconds so that we don't hog the CPU */
968 set_current_state(TASK_INTERRUPTIBLE);
969 schedule_timeout(msecs_to_jiffies(2000));
970
971 mask_of_crashed_or_stuck_cores =
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700972 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
973
Felix Manlunasbb54be52017-04-04 19:26:57 -0700974 if (!mask_of_crashed_or_stuck_cores)
975 continue;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700976
Felix Manlunasbb54be52017-04-04 19:26:57 -0700977 WRITE_ONCE(oct->cores_crashed, true);
978 other_oct = get_other_octeon_device(oct);
979 if (other_oct)
980 WRITE_ONCE(other_oct->cores_crashed, true);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700981
Felix Manlunasbb54be52017-04-04 19:26:57 -0700982 for (core = 0; core < LIO_MAX_CORES; core++) {
983 bool core_crashed_or_got_stuck;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700984
Felix Manlunasbb54be52017-04-04 19:26:57 -0700985 core_crashed_or_got_stuck =
986 (mask_of_crashed_or_stuck_cores
987 >> core) & 1;
988
989 if (core_crashed_or_got_stuck &&
990 !err_msg_was_printed[core]) {
991 dev_err(&oct->pci_dev->dev,
992 "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
993 core);
994 err_msg_was_printed[core] = true;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -0700995 }
996 }
997
Felix Manlunasbb54be52017-04-04 19:26:57 -0700998 if (all_vf_links_are_disabled)
999 continue;
1000
1001 disable_all_vf_links(oct);
1002 disable_all_vf_links(other_oct);
1003 all_vf_links_are_disabled = true;
1004
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001005#ifdef CONFIG_MODULE_UNLOAD
Felix Manlunasbb54be52017-04-04 19:26:57 -07001006 vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1007 vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001008
Felix Manlunasbb54be52017-04-04 19:26:57 -07001009 vfs_referencing_pf = hweight64(vfs_mask1);
1010 vfs_referencing_pf += hweight64(vfs_mask2);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001011
Felix Manlunasbb54be52017-04-04 19:26:57 -07001012 refcount = module_refcount(THIS_MODULE);
1013 if (refcount >= vfs_referencing_pf) {
1014 while (vfs_referencing_pf) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001015 module_put(THIS_MODULE);
Felix Manlunasbb54be52017-04-04 19:26:57 -07001016 vfs_referencing_pf--;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001017 }
1018 }
1019#endif
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001020 }
1021
1022 return 0;
1023}
1024
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001025/**
1026 * \brief PCI probe handler
1027 * @param pdev PCI device structure
1028 * @param ent unused
1029 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001030static int
1031liquidio_probe(struct pci_dev *pdev,
1032 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001033{
1034 struct octeon_device *oct_dev = NULL;
1035 struct handshake *hs;
1036
1037 oct_dev = octeon_allocate_device(pdev->device,
1038 sizeof(struct octeon_device_priv));
1039 if (!oct_dev) {
1040 dev_err(&pdev->dev, "Unable to allocate device\n");
1041 return -ENOMEM;
1042 }
1043
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001044 if (pdev->device == OCTEON_CN23XX_PF_VID)
1045 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1046
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07001047 /* Enable PTP for 6XXX Device */
1048 if (((pdev->device == OCTEON_CN66XX) ||
1049 (pdev->device == OCTEON_CN68XX)))
1050 oct_dev->ptp_enable = true;
1051 else
1052 oct_dev->ptp_enable = false;
1053
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001054 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1055 (u32)pdev->vendor, (u32)pdev->device);
1056
1057 /* Assign octeon_device for this device to the private data area. */
1058 pci_set_drvdata(pdev, oct_dev);
1059
1060 /* set linux specific device pointer */
1061 oct_dev->pci_dev = (void *)pdev;
1062
1063 hs = &handshake[oct_dev->octeon_id];
1064 init_completion(&hs->init);
1065 init_completion(&hs->started);
1066 hs->pci_dev = pdev;
1067
1068 if (oct_dev->octeon_id == 0)
1069 /* first LiquidIO NIC is detected */
1070 complete(&first_stage);
1071
1072 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001073 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001074 liquidio_remove(pdev);
1075 return -ENOMEM;
1076 }
1077
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001078 if (OCTEON_CN23XX_PF(oct_dev)) {
1079 u64 scratch1;
1080 u8 bus, device, function;
1081
1082 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1083 if (!(scratch1 & 4ULL)) {
1084 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1085 * the lio watchdog kernel thread is running for this
1086 * NIC. Each NIC gets one watchdog kernel thread.
1087 */
1088 scratch1 |= 4ULL;
1089 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1090 scratch1);
1091
1092 bus = pdev->bus->number;
1093 device = PCI_SLOT(pdev->devfn);
1094 function = PCI_FUNC(pdev->devfn);
1095 oct_dev->watchdog_task = kthread_create(
1096 liquidio_watchdog, oct_dev,
1097 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001098 if (!IS_ERR(oct_dev->watchdog_task)) {
1099 wake_up_process(oct_dev->watchdog_task);
1100 } else {
1101 oct_dev->watchdog_task = NULL;
1102 dev_err(&oct_dev->pci_dev->dev,
1103 "failed to create kernel_thread\n");
1104 liquidio_remove(pdev);
1105 return -1;
1106 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001107 }
1108 }
1109
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001110 oct_dev->rx_pause = 1;
1111 oct_dev->tx_pause = 1;
1112
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001113 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1114
1115 return 0;
1116}
1117
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001118static bool fw_type_is_none(void)
1119{
1120 return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1121 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
1122}
1123
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001124/**
Rick Farrington70535352017-08-17 23:11:25 -07001125 * \brief PCI FLR for each Octeon device.
1126 * @param oct octeon device
1127 */
1128static void octeon_pci_flr(struct octeon_device *oct)
1129{
1130 int rc;
1131
1132 pci_save_state(oct->pci_dev);
1133
1134 pci_cfg_access_lock(oct->pci_dev);
1135
1136 /* Quiesce the device completely */
1137 pci_write_config_word(oct->pci_dev, PCI_COMMAND,
1138 PCI_COMMAND_INTX_DISABLE);
1139
1140 rc = __pci_reset_function_locked(oct->pci_dev);
1141
1142 if (rc != 0)
1143 dev_err(&oct->pci_dev->dev, "Error %d resetting PCI function %d\n",
1144 rc, oct->pf_num);
1145
1146 pci_cfg_access_unlock(oct->pci_dev);
1147
1148 pci_restore_state(oct->pci_dev);
1149}
1150
1151/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001152 *\brief Destroy resources associated with octeon device
1153 * @param pdev PCI device structure
1154 * @param ent unused
1155 */
1156static void octeon_destroy_resources(struct octeon_device *oct)
1157{
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001158 int i, refcount;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001159 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001160 struct octeon_device_priv *oct_priv =
1161 (struct octeon_device_priv *)oct->priv;
1162
1163 struct handshake *hs;
1164
1165 switch (atomic_read(&oct->status)) {
1166 case OCT_DEV_RUNNING:
1167 case OCT_DEV_CORE_OK:
1168
1169 /* No more instructions will be forwarded. */
1170 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1171
1172 oct->app_mode = CVM_DRV_INVALID_APP;
1173 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1174 lio_get_state_string(&oct->status));
1175
1176 schedule_timeout_uninterruptible(HZ / 10);
1177
1178 /* fallthrough */
1179 case OCT_DEV_HOST_OK:
1180
1181 /* fallthrough */
1182 case OCT_DEV_CONSOLE_INIT_DONE:
1183 /* Remove any consoles */
1184 octeon_remove_consoles(oct);
1185
1186 /* fallthrough */
1187 case OCT_DEV_IO_QUEUES_DONE:
1188 if (wait_for_pending_requests(oct))
1189 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1190
1191 if (lio_wait_for_instr_fetch(oct))
1192 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1193
1194 /* Disable the input and output queues now. No more packets will
1195 * arrive from Octeon, but we should wait for all packet
1196 * processing to finish.
1197 */
1198 oct->fn_list.disable_io_queues(oct);
1199
1200 if (lio_wait_for_oq_pkts(oct))
1201 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1202
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001203 /* fallthrough */
1204 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001205 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001206 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001207
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001208 if (oct->msix_on) {
1209 msix_entries = (struct msix_entry *)oct->msix_entries;
1210 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001211 if (oct->ioq_vector[i].vector) {
1212 /* clear the affinity_cpumask */
1213 irq_set_affinity_hint(
1214 msix_entries[i].vector,
1215 NULL);
1216 free_irq(msix_entries[i].vector,
1217 &oct->ioq_vector[i]);
1218 oct->ioq_vector[i].vector = 0;
1219 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001220 }
1221 /* non-iov vector's argument is oct struct */
1222 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001223
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001224 pci_disable_msix(oct->pci_dev);
1225 kfree(oct->msix_entries);
1226 oct->msix_entries = NULL;
1227 } else {
1228 /* Release the interrupt line */
1229 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001230
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001231 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1232 pci_disable_msi(oct->pci_dev);
1233 }
1234
Rick Farrington0c88a762017-03-13 12:58:04 -07001235 kfree(oct->irq_name_storage);
1236 oct->irq_name_storage = NULL;
1237
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001238 /* fallthrough */
1239 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001240 if (OCTEON_CN23XX_PF(oct))
1241 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001242
1243 /* fallthrough */
1244 case OCT_DEV_MBOX_SETUP_DONE:
1245 if (OCTEON_CN23XX_PF(oct))
1246 oct->fn_list.free_mbox(oct);
1247
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001248 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001249 case OCT_DEV_IN_RESET:
1250 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001251 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001252 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001253 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001254 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001255 continue;
1256 octeon_delete_droq(oct, i);
1257 }
1258
1259 /* Force any pending handshakes to complete */
1260 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1261 hs = &handshake[i];
1262
1263 if (hs->pci_dev) {
1264 handshake[oct->octeon_id].init_ok = 0;
1265 complete(&handshake[oct->octeon_id].init);
1266 handshake[oct->octeon_id].started_ok = 0;
1267 complete(&handshake[oct->octeon_id].started);
1268 }
1269 }
1270
1271 /* fallthrough */
1272 case OCT_DEV_RESP_LIST_INIT_DONE:
1273 octeon_delete_response_list(oct);
1274
1275 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001276 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001277 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001278 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001279 continue;
1280 octeon_delete_instr_queue(oct, i);
1281 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001282#ifdef CONFIG_PCI_IOV
1283 if (oct->sriov_info.sriov_enabled)
1284 pci_disable_sriov(oct->pci_dev);
1285#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001286 /* fallthrough */
1287 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1288 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001289
1290 /* fallthrough */
1291 case OCT_DEV_DISPATCH_INIT_DONE:
1292 octeon_delete_dispatch_list(oct);
1293 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1294
1295 /* fallthrough */
1296 case OCT_DEV_PCI_MAP_DONE:
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001297 refcount = octeon_deregister_device(oct);
1298
Rick Farrington70535352017-08-17 23:11:25 -07001299 /* Soft reset the octeon device before exiting.
1300 * However, if fw was loaded from card (i.e. autoboot),
1301 * perform an FLR instead.
1302 * Implementation note: only soft-reset the device
1303 * if it is a CN6XXX OR the LAST CN23XX device.
1304 */
1305 if (fw_type_is_none())
1306 octeon_pci_flr(oct);
1307 else if (OCTEON_CN6XXX(oct) || !refcount)
1308 oct->fn_list.soft_reset(oct);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001309
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001310 octeon_unmap_pci_barx(oct, 0);
1311 octeon_unmap_pci_barx(oct, 1);
1312
1313 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001314 case OCT_DEV_PCI_ENABLE_DONE:
1315 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001316 /* Disable the device, releasing the PCI INT */
1317 pci_disable_device(oct->pci_dev);
1318
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001319 /* fallthrough */
1320 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001321 /* Nothing to be done here either */
1322 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001323 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001324
1325 tasklet_kill(&oct_priv->droq_tasklet);
1326}
1327
1328/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001329 * \brief Callback for rx ctrl
1330 * @param status status of request
1331 * @param buf pointer to resp structure
1332 */
1333static void rx_ctl_callback(struct octeon_device *oct,
1334 u32 status,
1335 void *buf)
1336{
1337 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1338 struct liquidio_rx_ctl_context *ctx;
1339
1340 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1341
1342 oct = lio_get_device(ctx->octeon_id);
1343 if (status)
1344 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1345 CVM_CAST64(status));
1346 WRITE_ONCE(ctx->cond, 1);
1347
1348 /* This barrier is required to be sure that the response has been
1349 * written fully before waking up the handler
1350 */
1351 wmb();
1352
1353 wake_up_interruptible(&ctx->wc);
1354}
1355
1356/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001357 * \brief Send Rx control command
1358 * @param lio per-network private data
1359 * @param start_stop whether to start or stop
1360 */
1361static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1362{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001363 struct octeon_soft_command *sc;
1364 struct liquidio_rx_ctl_context *ctx;
1365 union octnet_cmd *ncmd;
1366 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1367 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1368 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001369
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001370 if (oct->props[lio->ifidx].rx_on == start_stop)
1371 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001372
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001373 sc = (struct octeon_soft_command *)
1374 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1375 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001376
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001377 ncmd = (union octnet_cmd *)sc->virtdptr;
1378 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1379
1380 WRITE_ONCE(ctx->cond, 0);
1381 ctx->octeon_id = lio_get_device_id(oct);
1382 init_waitqueue_head(&ctx->wc);
1383
1384 ncmd->u64 = 0;
1385 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1386 ncmd->s.param1 = start_stop;
1387
1388 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1389
1390 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1391
1392 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1393 OPCODE_NIC_CMD, 0, 0, 0);
1394
1395 sc->callback = rx_ctl_callback;
1396 sc->callback_arg = sc;
1397 sc->wait_time = 5000;
1398
1399 retval = octeon_send_soft_command(oct, sc);
1400 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001401 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001402 } else {
1403 /* Sleep on a wait queue till the cond flag indicates that the
1404 * response arrived or timed-out.
1405 */
1406 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1407 return;
1408 oct->props[lio->ifidx].rx_on = start_stop;
1409 }
1410
1411 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001412}
1413
1414/**
1415 * \brief Destroy NIC device interface
1416 * @param oct octeon device
1417 * @param ifidx which interface to destroy
1418 *
1419 * Cleanup associated with each interface for an Octeon device when NIC
1420 * module is being unloaded or if initialization fails during load.
1421 */
1422static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1423{
1424 struct net_device *netdev = oct->props[ifidx].netdev;
1425 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001426 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001427
1428 if (!netdev) {
1429 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1430 __func__, ifidx);
1431 return;
1432 }
1433
1434 lio = GET_LIO(netdev);
1435
1436 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1437
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001438 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001439 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001440
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001441 if (oct->props[lio->ifidx].napi_enabled == 1) {
1442 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1443 napi_disable(napi);
1444
1445 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001446
1447 if (OCTEON_CN23XX_PF(oct))
1448 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001449 }
1450
Intiyaz Basha42013e92017-08-08 19:34:28 -07001451 /* Delete NAPI */
1452 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1453 netif_napi_del(napi);
1454
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001455 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1456 unregister_netdev(netdev);
1457
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001458 cleanup_link_status_change_wq(netdev);
1459
Satanand Burla031d4f12017-03-22 11:31:13 -07001460 cleanup_rx_oom_poll_fn(netdev);
1461
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001462 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001463
1464 free_netdev(netdev);
1465
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001466 oct->props[ifidx].gmxport = -1;
1467
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001468 oct->props[ifidx].netdev = NULL;
1469}
1470
1471/**
1472 * \brief Stop complete NIC functionality
1473 * @param oct octeon device
1474 */
1475static int liquidio_stop_nic_module(struct octeon_device *oct)
1476{
1477 int i, j;
1478 struct lio *lio;
1479
1480 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1481 if (!oct->ifcount) {
1482 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1483 return 1;
1484 }
1485
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001486 spin_lock_bh(&oct->cmd_resp_wqlock);
1487 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1488 spin_unlock_bh(&oct->cmd_resp_wqlock);
1489
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001490 for (i = 0; i < oct->ifcount; i++) {
1491 lio = GET_LIO(oct->props[i].netdev);
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001492 for (j = 0; j < oct->num_oqs; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001493 octeon_unregister_droq_ops(oct,
1494 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001495 }
1496
1497 for (i = 0; i < oct->ifcount; i++)
1498 liquidio_destroy_nic_device(oct, i);
1499
1500 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1501 return 0;
1502}
1503
1504/**
1505 * \brief Cleans up resources at unload time
1506 * @param pdev PCI device structure
1507 */
1508static void liquidio_remove(struct pci_dev *pdev)
1509{
1510 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1511
1512 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1513
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001514 if (oct_dev->watchdog_task)
1515 kthread_stop(oct_dev->watchdog_task);
1516
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001517 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1518 liquidio_stop_nic_module(oct_dev);
1519
1520 /* Reset the octeon device and cleanup all memory allocated for
1521 * the octeon device by driver.
1522 */
1523 octeon_destroy_resources(oct_dev);
1524
1525 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1526
1527 /* This octeon device has been removed. Update the global
1528 * data structure to reflect this. Free the device structure.
1529 */
1530 octeon_free_device_mem(oct_dev);
1531}
1532
1533/**
1534 * \brief Identify the Octeon device and to map the BAR address space
1535 * @param oct octeon device
1536 */
1537static int octeon_chip_specific_setup(struct octeon_device *oct)
1538{
1539 u32 dev_id, rev_id;
1540 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001541 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001542
1543 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1544 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1545 oct->rev_id = rev_id & 0xff;
1546
1547 switch (dev_id) {
1548 case OCTEON_CN68XX_PCIID:
1549 oct->chip_id = OCTEON_CN68XX;
1550 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001551 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001552 break;
1553
1554 case OCTEON_CN66XX_PCIID:
1555 oct->chip_id = OCTEON_CN66XX;
1556 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001557 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001558 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001559
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001560 case OCTEON_CN23XX_PCIID_PF:
1561 oct->chip_id = OCTEON_CN23XX_PF_VID;
1562 ret = setup_cn23xx_octeon_pf_device(oct);
Derek Chicklescf19a8c2017-08-01 15:05:07 -07001563#ifdef CONFIG_PCI_IOV
1564 if (!ret)
1565 pci_sriov_set_totalvfs(oct->pci_dev,
1566 oct->sriov_info.max_vfs);
1567#endif
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001568 s = "CN23XX";
1569 break;
1570
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001571 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001572 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001573 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1574 dev_id);
1575 }
1576
1577 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001578 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001579 OCTEON_MAJOR_REV(oct),
1580 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001581 octeon_get_conf(oct)->card_name,
1582 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001583
1584 return ret;
1585}
1586
1587/**
1588 * \brief PCI initialization for each Octeon device.
1589 * @param oct octeon device
1590 */
1591static int octeon_pci_os_setup(struct octeon_device *oct)
1592{
1593 /* setup PCI stuff first */
1594 if (pci_enable_device(oct->pci_dev)) {
1595 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1596 return 1;
1597 }
1598
1599 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1600 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001601 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001602 return 1;
1603 }
1604
1605 /* Enable PCI DMA Master. */
1606 pci_set_master(oct->pci_dev);
1607
1608 return 0;
1609}
1610
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001611static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1612{
1613 int q = 0;
1614
1615 if (netif_is_multiqueue(lio->netdev))
1616 q = skb->queue_mapping % lio->linfo.num_txpciq;
1617
1618 return q;
1619}
1620
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001621/**
1622 * \brief Check Tx queue state for a given network buffer
1623 * @param lio per-network private data
1624 * @param skb network buffer
1625 */
1626static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1627{
1628 int q = 0, iq = 0;
1629
1630 if (netif_is_multiqueue(lio->netdev)) {
1631 q = skb->queue_mapping;
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07001632 iq = lio->linfo.txpciq[(q % lio->oct_dev->num_iqs)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001633 } else {
1634 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001635 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001636 }
1637
1638 if (octnet_iq_is_full(lio->oct_dev, iq))
1639 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001640
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001641 if (__netif_subqueue_stopped(lio->netdev, q)) {
1642 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001643 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001644 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001645 return 1;
1646}
1647
1648/**
1649 * \brief Unmap and free network buffer
1650 * @param buf buffer
1651 */
1652static void free_netbuf(void *buf)
1653{
1654 struct sk_buff *skb;
1655 struct octnet_buf_free_info *finfo;
1656 struct lio *lio;
1657
1658 finfo = (struct octnet_buf_free_info *)buf;
1659 skb = finfo->skb;
1660 lio = finfo->lio;
1661
1662 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1663 DMA_TO_DEVICE);
1664
1665 check_txq_state(lio, skb);
1666
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001667 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001668}
1669
1670/**
1671 * \brief Unmap and free gather buffer
1672 * @param buf buffer
1673 */
1674static void free_netsgbuf(void *buf)
1675{
1676 struct octnet_buf_free_info *finfo;
1677 struct sk_buff *skb;
1678 struct lio *lio;
1679 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001680 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001681
1682 finfo = (struct octnet_buf_free_info *)buf;
1683 skb = finfo->skb;
1684 lio = finfo->lio;
1685 g = finfo->g;
1686 frags = skb_shinfo(skb)->nr_frags;
1687
1688 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1689 g->sg[0].ptr[0], (skb->len - skb->data_len),
1690 DMA_TO_DEVICE);
1691
1692 i = 1;
1693 while (frags--) {
1694 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1695
1696 pci_unmap_page((lio->oct_dev)->pci_dev,
1697 g->sg[(i >> 2)].ptr[(i & 3)],
1698 frag->size, DMA_TO_DEVICE);
1699 i++;
1700 }
1701
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001702 iq = skb_iq(lio, skb);
1703 spin_lock(&lio->glist_lock[iq]);
1704 list_add_tail(&g->list, &lio->glist[iq]);
1705 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001706
1707 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1708
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001709 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001710}
1711
1712/**
1713 * \brief Unmap and free gather buffer with response
1714 * @param buf buffer
1715 */
1716static void free_netsgbuf_with_resp(void *buf)
1717{
1718 struct octeon_soft_command *sc;
1719 struct octnet_buf_free_info *finfo;
1720 struct sk_buff *skb;
1721 struct lio *lio;
1722 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001723 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001724
1725 sc = (struct octeon_soft_command *)buf;
1726 skb = (struct sk_buff *)sc->callback_arg;
1727 finfo = (struct octnet_buf_free_info *)&skb->cb;
1728
1729 lio = finfo->lio;
1730 g = finfo->g;
1731 frags = skb_shinfo(skb)->nr_frags;
1732
1733 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1734 g->sg[0].ptr[0], (skb->len - skb->data_len),
1735 DMA_TO_DEVICE);
1736
1737 i = 1;
1738 while (frags--) {
1739 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1740
1741 pci_unmap_page((lio->oct_dev)->pci_dev,
1742 g->sg[(i >> 2)].ptr[(i & 3)],
1743 frag->size, DMA_TO_DEVICE);
1744 i++;
1745 }
1746
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001747 iq = skb_iq(lio, skb);
1748
1749 spin_lock(&lio->glist_lock[iq]);
1750 list_add_tail(&g->list, &lio->glist[iq]);
1751 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001752
1753 /* Don't free the skb yet */
1754
1755 check_txq_state(lio, skb);
1756}
1757
1758/**
1759 * \brief Adjust ptp frequency
1760 * @param ptp PTP clock info
1761 * @param ppb how much to adjust by, in parts-per-billion
1762 */
1763static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1764{
1765 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1766 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1767 u64 comp, delta;
1768 unsigned long flags;
1769 bool neg_adj = false;
1770
1771 if (ppb < 0) {
1772 neg_adj = true;
1773 ppb = -ppb;
1774 }
1775
1776 /* The hardware adds the clock compensation value to the
1777 * PTP clock on every coprocessor clock cycle, so we
1778 * compute the delta in terms of coprocessor clocks.
1779 */
1780 delta = (u64)ppb << 32;
1781 do_div(delta, oct->coproc_clock_rate);
1782
1783 spin_lock_irqsave(&lio->ptp_lock, flags);
1784 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1785 if (neg_adj)
1786 comp -= delta;
1787 else
1788 comp += delta;
1789 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1790 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1791
1792 return 0;
1793}
1794
1795/**
1796 * \brief Adjust ptp time
1797 * @param ptp PTP clock info
1798 * @param delta how much to adjust by, in nanosecs
1799 */
1800static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1801{
1802 unsigned long flags;
1803 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1804
1805 spin_lock_irqsave(&lio->ptp_lock, flags);
1806 lio->ptp_adjust += delta;
1807 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1808
1809 return 0;
1810}
1811
1812/**
1813 * \brief Get hardware clock time, including any adjustment
1814 * @param ptp PTP clock info
1815 * @param ts timespec
1816 */
1817static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1818 struct timespec64 *ts)
1819{
1820 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001821 unsigned long flags;
1822 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1823 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1824
1825 spin_lock_irqsave(&lio->ptp_lock, flags);
1826 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1827 ns += lio->ptp_adjust;
1828 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1829
Kefeng Wang286af312016-01-27 17:34:37 +08001830 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001831
1832 return 0;
1833}
1834
1835/**
1836 * \brief Set hardware clock time. Reset adjustment
1837 * @param ptp PTP clock info
1838 * @param ts timespec
1839 */
1840static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1841 const struct timespec64 *ts)
1842{
1843 u64 ns;
1844 unsigned long flags;
1845 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1846 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1847
1848 ns = timespec_to_ns(ts);
1849
1850 spin_lock_irqsave(&lio->ptp_lock, flags);
1851 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1852 lio->ptp_adjust = 0;
1853 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1854
1855 return 0;
1856}
1857
1858/**
1859 * \brief Check if PTP is enabled
1860 * @param ptp PTP clock info
1861 * @param rq request
1862 * @param on is it on
1863 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001864static int
1865liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
1866 struct ptp_clock_request *rq __attribute__((unused)),
1867 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001868{
1869 return -EOPNOTSUPP;
1870}
1871
1872/**
1873 * \brief Open PTP clock source
1874 * @param netdev network device
1875 */
1876static void oct_ptp_open(struct net_device *netdev)
1877{
1878 struct lio *lio = GET_LIO(netdev);
1879 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1880
1881 spin_lock_init(&lio->ptp_lock);
1882
1883 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1884 lio->ptp_info.owner = THIS_MODULE;
1885 lio->ptp_info.max_adj = 250000000;
1886 lio->ptp_info.n_alarm = 0;
1887 lio->ptp_info.n_ext_ts = 0;
1888 lio->ptp_info.n_per_out = 0;
1889 lio->ptp_info.pps = 0;
1890 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1891 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1892 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1893 lio->ptp_info.settime64 = liquidio_ptp_settime;
1894 lio->ptp_info.enable = liquidio_ptp_enable;
1895
1896 lio->ptp_adjust = 0;
1897
1898 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1899 &oct->pci_dev->dev);
1900
1901 if (IS_ERR(lio->ptp_clock))
1902 lio->ptp_clock = NULL;
1903}
1904
1905/**
1906 * \brief Init PTP clock
1907 * @param oct octeon device
1908 */
1909static void liquidio_ptp_init(struct octeon_device *oct)
1910{
1911 u64 clock_comp, cfg;
1912
1913 clock_comp = (u64)NSEC_PER_SEC << 32;
1914 do_div(clock_comp, oct->coproc_clock_rate);
1915 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1916
1917 /* Enable */
1918 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1919 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1920}
1921
1922/**
1923 * \brief Load firmware to device
1924 * @param oct octeon device
1925 *
1926 * Maps device to firmware filename, requests firmware, and downloads it
1927 */
1928static int load_firmware(struct octeon_device *oct)
1929{
1930 int ret = 0;
1931 const struct firmware *fw;
1932 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1933 char *tmp_fw_type;
1934
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001935 if (fw_type[0] == '\0')
1936 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1937 else
1938 tmp_fw_type = fw_type;
1939
1940 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1941 octeon_get_conf(oct)->card_name, tmp_fw_type,
1942 LIO_FW_NAME_SUFFIX);
1943
1944 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1945 if (ret) {
1946 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1947 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001948 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001949 return ret;
1950 }
1951
1952 ret = octeon_download_firmware(oct, fw->data, fw->size);
1953
1954 release_firmware(fw);
1955
1956 return ret;
1957}
1958
1959/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001960 * \brief Callback for getting interface configuration
1961 * @param status status of request
1962 * @param buf pointer to resp structure
1963 */
1964static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001965 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001966 void *buf)
1967{
1968 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1969 struct liquidio_if_cfg_resp *resp;
1970 struct liquidio_if_cfg_context *ctx;
1971
1972 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07001973 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001974
1975 oct = lio_get_device(ctx->octeon_id);
1976 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07001977 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
1978 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001979 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001980
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001981 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
1982 resp->cfg_info.liquidio_firmware_version);
1983
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001984 /* This barrier is required to be sure that the response has been
1985 * written fully before waking up the handler
1986 */
1987 wmb();
1988
1989 wake_up_interruptible(&ctx->wc);
1990}
1991
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001992/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001993 * \brief Poll routine for checking transmit queue status
1994 * @param work work_struct data structure
1995 */
1996static void octnet_poll_check_txq_status(struct work_struct *work)
1997{
1998 struct cavium_wk *wk = (struct cavium_wk *)work;
1999 struct lio *lio = (struct lio *)wk->ctxptr;
2000
2001 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2002 return;
2003
2004 check_txq_status(lio);
2005 queue_delayed_work(lio->txq_status_wq.wq,
2006 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2007}
2008
2009/**
2010 * \brief Sets up the txq poll check
2011 * @param netdev network device
2012 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002013static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002014{
2015 struct lio *lio = GET_LIO(netdev);
2016 struct octeon_device *oct = lio->oct_dev;
2017
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302018 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2019 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002020 if (!lio->txq_status_wq.wq) {
2021 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002022 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002023 }
2024 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2025 octnet_poll_check_txq_status);
2026 lio->txq_status_wq.wk.ctxptr = lio;
2027 queue_delayed_work(lio->txq_status_wq.wq,
2028 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002029 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002030}
2031
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002032static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2033{
2034 struct lio *lio = GET_LIO(netdev);
2035
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002036 if (lio->txq_status_wq.wq) {
2037 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2038 destroy_workqueue(lio->txq_status_wq.wq);
2039 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002040}
2041
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002042/**
2043 * \brief Net device open for LiquidIO
2044 * @param netdev network device
2045 */
2046static int liquidio_open(struct net_device *netdev)
2047{
2048 struct lio *lio = GET_LIO(netdev);
2049 struct octeon_device *oct = lio->oct_dev;
2050 struct napi_struct *napi, *n;
2051
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002052 if (oct->props[lio->ifidx].napi_enabled == 0) {
2053 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2054 napi_enable(napi);
2055
2056 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002057
2058 if (OCTEON_CN23XX_PF(oct))
2059 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002060 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002061
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002062 if (oct->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002063 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002064
2065 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002066
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002067 /* Ready for link status updates */
2068 lio->intf_open = 1;
2069
2070 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2071
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002072 if (OCTEON_CN23XX_PF(oct)) {
2073 if (!oct->msix_on)
2074 if (setup_tx_poll_fn(netdev))
2075 return -1;
2076 } else {
2077 if (setup_tx_poll_fn(netdev))
2078 return -1;
2079 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002080
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002081 start_txq(netdev);
2082
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002083 /* tell Octeon to start forwarding packets to host */
2084 send_rx_ctrl_cmd(lio, 1);
2085
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002086 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2087 netdev->name);
2088
2089 return 0;
2090}
2091
2092/**
2093 * \brief Net device stop for LiquidIO
2094 * @param netdev network device
2095 */
2096static int liquidio_stop(struct net_device *netdev)
2097{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002098 struct lio *lio = GET_LIO(netdev);
2099 struct octeon_device *oct = lio->oct_dev;
Intiyaz Basha42013e92017-08-08 19:34:28 -07002100 struct napi_struct *napi, *n;
2101
2102 if (oct->props[lio->ifidx].napi_enabled) {
2103 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2104 napi_disable(napi);
2105
2106 oct->props[lio->ifidx].napi_enabled = 0;
2107
2108 if (OCTEON_CN23XX_PF(oct))
2109 oct->droq[0]->ops.poll_mode = 0;
2110 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002111
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002112 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2113
2114 netif_tx_disable(netdev);
2115
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002116 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002117 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002118 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002119 lio->linfo.link.s.link_up = 0;
2120 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002121
Felix Manlunascb2336b2017-01-11 17:09:02 -08002122 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002123 send_rx_ctrl_cmd(lio, 0);
2124
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002125 if (OCTEON_CN23XX_PF(oct)) {
2126 if (!oct->msix_on)
2127 cleanup_tx_poll_fn(netdev);
2128 } else {
2129 cleanup_tx_poll_fn(netdev);
2130 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002131
2132 if (lio->ptp_clock) {
2133 ptp_clock_unregister(lio->ptp_clock);
2134 lio->ptp_clock = NULL;
2135 }
2136
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002137 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002138
2139 return 0;
2140}
2141
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002142/**
2143 * \brief Converts a mask based on net device flags
2144 * @param netdev network device
2145 *
2146 * This routine generates a octnet_ifflags mask from the net device flags
2147 * received from the OS.
2148 */
2149static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2150{
2151 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2152
2153 if (netdev->flags & IFF_PROMISC)
2154 f |= OCTNET_IFFLAG_PROMISC;
2155
2156 if (netdev->flags & IFF_ALLMULTI)
2157 f |= OCTNET_IFFLAG_ALLMULTI;
2158
2159 if (netdev->flags & IFF_MULTICAST) {
2160 f |= OCTNET_IFFLAG_MULTICAST;
2161
2162 /* Accept all multicast addresses if there are more than we
2163 * can handle
2164 */
2165 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2166 f |= OCTNET_IFFLAG_ALLMULTI;
2167 }
2168
2169 if (netdev->flags & IFF_BROADCAST)
2170 f |= OCTNET_IFFLAG_BROADCAST;
2171
2172 return f;
2173}
2174
2175/**
2176 * \brief Net device set_multicast_list
2177 * @param netdev network device
2178 */
2179static void liquidio_set_mcast_list(struct net_device *netdev)
2180{
2181 struct lio *lio = GET_LIO(netdev);
2182 struct octeon_device *oct = lio->oct_dev;
2183 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002184 struct netdev_hw_addr *ha;
2185 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002186 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002187 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2188
2189 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2190
2191 /* Create a ctrl pkt command to be sent to core app. */
2192 nctrl.ncmd.u64 = 0;
2193 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002194 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2195 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002196 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002197 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002198 nctrl.netpndev = (u64)netdev;
2199 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2200
2201 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002202 mc = &nctrl.udd[0];
2203 netdev_for_each_mc_addr(ha, netdev) {
2204 *mc = 0;
2205 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2206 /* no need to swap bytes */
2207
2208 if (++mc > &nctrl.udd[mc_count])
2209 break;
2210 }
2211
2212 /* Apparently, any activity in this call from the kernel has to
2213 * be atomic. So we won't wait for response.
2214 */
2215 nctrl.wait_time = 0;
2216
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002217 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002218 if (ret < 0) {
2219 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2220 ret);
2221 }
2222}
2223
2224/**
2225 * \brief Net device set_mac_address
2226 * @param netdev network device
2227 */
2228static int liquidio_set_mac(struct net_device *netdev, void *p)
2229{
2230 int ret = 0;
2231 struct lio *lio = GET_LIO(netdev);
2232 struct octeon_device *oct = lio->oct_dev;
2233 struct sockaddr *addr = (struct sockaddr *)p;
2234 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002235
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002236 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002237 return -EADDRNOTAVAIL;
2238
2239 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2240
2241 nctrl.ncmd.u64 = 0;
2242 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002243 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002244 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002245 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002246 nctrl.netpndev = (u64)netdev;
2247 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2248 nctrl.wait_time = 100;
2249
2250 nctrl.udd[0] = 0;
2251 /* The MAC Address is presented in network byte order. */
2252 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2253
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002254 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002255 if (ret < 0) {
2256 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2257 return -ENOMEM;
2258 }
2259 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2260 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2261
2262 return 0;
2263}
2264
2265/**
2266 * \brief Net device get_stats
2267 * @param netdev network device
2268 */
2269static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2270{
2271 struct lio *lio = GET_LIO(netdev);
2272 struct net_device_stats *stats = &netdev->stats;
2273 struct octeon_device *oct;
2274 u64 pkts = 0, drop = 0, bytes = 0;
2275 struct oct_droq_stats *oq_stats;
2276 struct oct_iq_stats *iq_stats;
2277 int i, iq_no, oq_no;
2278
2279 oct = lio->oct_dev;
2280
Intiyaz Bashad18ca7d2017-08-14 12:01:56 -07002281 if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
2282 return stats;
2283
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07002284 for (i = 0; i < oct->num_iqs; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002285 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002286 iq_stats = &oct->instr_queue[iq_no]->stats;
2287 pkts += iq_stats->tx_done;
2288 drop += iq_stats->tx_dropped;
2289 bytes += iq_stats->tx_tot_bytes;
2290 }
2291
2292 stats->tx_packets = pkts;
2293 stats->tx_bytes = bytes;
2294 stats->tx_dropped = drop;
2295
2296 pkts = 0;
2297 drop = 0;
2298 bytes = 0;
2299
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07002300 for (i = 0; i < oct->num_oqs; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002301 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002302 oq_stats = &oct->droq[oq_no]->stats;
2303 pkts += oq_stats->rx_pkts_received;
2304 drop += (oq_stats->rx_dropped +
2305 oq_stats->dropped_nodispatch +
2306 oq_stats->dropped_toomany +
2307 oq_stats->dropped_nomem);
2308 bytes += oq_stats->rx_bytes_received;
2309 }
2310
2311 stats->rx_bytes = bytes;
2312 stats->rx_packets = pkts;
2313 stats->rx_dropped = drop;
2314
2315 return stats;
2316}
2317
2318/**
2319 * \brief Net device change_mtu
2320 * @param netdev network device
2321 */
2322static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2323{
2324 struct lio *lio = GET_LIO(netdev);
2325 struct octeon_device *oct = lio->oct_dev;
2326 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002327 int ret = 0;
2328
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002329 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2330
2331 nctrl.ncmd.u64 = 0;
2332 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002333 nctrl.ncmd.s.param1 = new_mtu;
2334 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002335 nctrl.wait_time = 100;
2336 nctrl.netpndev = (u64)netdev;
2337 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2338
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002339 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002340 if (ret < 0) {
2341 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2342 return -1;
2343 }
2344
2345 lio->mtu = new_mtu;
2346
2347 return 0;
2348}
2349
2350/**
2351 * \brief Handler for SIOCSHWTSTAMP ioctl
2352 * @param netdev network device
2353 * @param ifr interface request
2354 * @param cmd command
2355 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002356static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002357{
2358 struct hwtstamp_config conf;
2359 struct lio *lio = GET_LIO(netdev);
2360
2361 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2362 return -EFAULT;
2363
2364 if (conf.flags)
2365 return -EINVAL;
2366
2367 switch (conf.tx_type) {
2368 case HWTSTAMP_TX_ON:
2369 case HWTSTAMP_TX_OFF:
2370 break;
2371 default:
2372 return -ERANGE;
2373 }
2374
2375 switch (conf.rx_filter) {
2376 case HWTSTAMP_FILTER_NONE:
2377 break;
2378 case HWTSTAMP_FILTER_ALL:
2379 case HWTSTAMP_FILTER_SOME:
2380 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2381 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2382 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2383 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2384 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2385 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2386 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2387 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2388 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2389 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2390 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2391 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvare3412572017-05-19 17:52:36 +02002392 case HWTSTAMP_FILTER_NTP_ALL:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002393 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2394 break;
2395 default:
2396 return -ERANGE;
2397 }
2398
2399 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2400 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2401
2402 else
2403 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2404
2405 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2406}
2407
2408/**
2409 * \brief ioctl handler
2410 * @param netdev network device
2411 * @param ifr interface request
2412 * @param cmd command
2413 */
2414static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2415{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002416 struct lio *lio = GET_LIO(netdev);
2417
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002418 switch (cmd) {
2419 case SIOCSHWTSTAMP:
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002420 if (lio->oct_dev->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002421 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002422 default:
2423 return -EOPNOTSUPP;
2424 }
2425}
2426
2427/**
2428 * \brief handle a Tx timestamp response
2429 * @param status response status
2430 * @param buf pointer to skb
2431 */
2432static void handle_timestamp(struct octeon_device *oct,
2433 u32 status,
2434 void *buf)
2435{
2436 struct octnet_buf_free_info *finfo;
2437 struct octeon_soft_command *sc;
2438 struct oct_timestamp_resp *resp;
2439 struct lio *lio;
2440 struct sk_buff *skb = (struct sk_buff *)buf;
2441
2442 finfo = (struct octnet_buf_free_info *)skb->cb;
2443 lio = finfo->lio;
2444 sc = finfo->sc;
2445 oct = lio->oct_dev;
2446 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2447
2448 if (status != OCTEON_REQUEST_DONE) {
2449 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2450 CVM_CAST64(status));
2451 resp->timestamp = 0;
2452 }
2453
2454 octeon_swap_8B_data(&resp->timestamp, 1);
2455
Colin Ian King19a6d152016-02-05 16:30:39 +00002456 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002457 struct skb_shared_hwtstamps ts;
2458 u64 ns = resp->timestamp;
2459
2460 netif_info(lio, tx_done, lio->netdev,
2461 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2462 skb, (unsigned long long)ns);
2463 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2464 skb_tstamp_tx(skb, &ts);
2465 }
2466
2467 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002468 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002469}
2470
2471/* \brief Send a data packet that will be timestamped
2472 * @param oct octeon device
2473 * @param ndata pointer to network data
2474 * @param finfo pointer to private network data
2475 */
2476static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2477 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002478 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002479{
2480 int retval;
2481 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002482 struct lio *lio;
2483 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002484 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002485
2486 lio = finfo->lio;
2487
2488 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2489 sizeof(struct oct_timestamp_resp));
2490 finfo->sc = sc;
2491
2492 if (!sc) {
2493 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2494 return IQ_SEND_FAILED;
2495 }
2496
2497 if (ndata->reqtype == REQTYPE_NORESP_NET)
2498 ndata->reqtype = REQTYPE_RESP_NET;
2499 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2500 ndata->reqtype = REQTYPE_RESP_NET_SG;
2501
2502 sc->callback = handle_timestamp;
2503 sc->callback_arg = finfo->skb;
2504 sc->iq_no = ndata->q_no;
2505
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002506 if (OCTEON_CN23XX_PF(oct))
2507 len = (u32)((struct octeon_instr_ih3 *)
2508 (&sc->cmd.cmd3.ih3))->dlengsz;
2509 else
2510 len = (u32)((struct octeon_instr_ih2 *)
2511 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002512
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002513 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002514
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002515 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002516 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002517
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002518 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002519 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2520 retval);
2521 octeon_free_soft_command(oct, sc);
2522 } else {
2523 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2524 }
2525
2526 return retval;
2527}
2528
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002529/** \brief Transmit networks packets to the Octeon interface
2530 * @param skbuff skbuff struct to be passed to network layer.
2531 * @param netdev pointer to network device
2532 * @returns whether the packet was transmitted to the device okay or not
2533 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2534 */
2535static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2536{
2537 struct lio *lio;
2538 struct octnet_buf_free_info *finfo;
2539 union octnic_cmd_setup cmdsetup;
2540 struct octnic_data_pkt ndata;
2541 struct octeon_device *oct;
2542 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002543 struct octeon_instr_irh *irh;
2544 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002545 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002546 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002547 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002548 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002549 u32 tag = 0;
2550
2551 lio = GET_LIO(netdev);
2552 oct = lio->oct_dev;
2553
2554 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002555 q_idx = skb->queue_mapping;
2556 q_idx = (q_idx % (lio->linfo.num_txpciq));
2557 tag = q_idx;
2558 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002559 } else {
2560 iq_no = lio->txq;
2561 }
2562
2563 stats = &oct->instr_queue[iq_no]->stats;
2564
2565 /* Check for all conditions in which the current packet cannot be
2566 * transmitted.
2567 */
2568 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002569 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002570 (skb->len <= 0)) {
2571 netif_info(lio, tx_err, lio->netdev,
2572 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002573 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002574 goto lio_xmit_failed;
2575 }
2576
2577 /* Use space in skb->cb to store info used to unmap and
2578 * free the buffers.
2579 */
2580 finfo = (struct octnet_buf_free_info *)skb->cb;
2581 finfo->lio = lio;
2582 finfo->skb = skb;
2583 finfo->sc = NULL;
2584
2585 /* Prepare the attributes for the data to be passed to OSI. */
2586 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2587
2588 ndata.buf = (void *)finfo;
2589
2590 ndata.q_no = iq_no;
2591
2592 if (netif_is_multiqueue(netdev)) {
2593 if (octnet_iq_is_full(oct, ndata.q_no)) {
2594 /* defer sending if queue is full */
2595 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2596 ndata.q_no);
2597 stats->tx_iq_busy++;
2598 return NETDEV_TX_BUSY;
2599 }
2600 } else {
2601 if (octnet_iq_is_full(oct, lio->txq)) {
2602 /* defer sending if queue is full */
2603 stats->tx_iq_busy++;
2604 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002605 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002606 return NETDEV_TX_BUSY;
2607 }
2608 }
2609 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002610 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002611 */
2612
2613 ndata.datasize = skb->len;
2614
2615 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002616 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002617
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002618 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2619 if (skb->encapsulation) {
2620 cmdsetup.s.tnl_csum = 1;
2621 stats->tx_vxlan++;
2622 } else {
2623 cmdsetup.s.transport_csum = 1;
2624 }
2625 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002626 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2627 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2628 cmdsetup.s.timestamp = 1;
2629 }
2630
2631 if (skb_shinfo(skb)->nr_frags == 0) {
2632 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002633 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002634
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002635 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002636 dptr = dma_map_single(&oct->pci_dev->dev,
2637 skb->data,
2638 skb->len,
2639 DMA_TO_DEVICE);
2640 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002641 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2642 __func__);
2643 return NETDEV_TX_BUSY;
2644 }
2645
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002646 if (OCTEON_CN23XX_PF(oct))
2647 ndata.cmd.cmd3.dptr = dptr;
2648 else
2649 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002650 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002651 ndata.reqtype = REQTYPE_NORESP_NET;
2652
2653 } else {
2654 int i, frags;
2655 struct skb_frag_struct *frag;
2656 struct octnic_gather *g;
2657
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002658 spin_lock(&lio->glist_lock[q_idx]);
2659 g = (struct octnic_gather *)
2660 list_delete_head(&lio->glist[q_idx]);
2661 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002662
2663 if (!g) {
2664 netif_info(lio, tx_err, lio->netdev,
2665 "Transmit scatter gather: glist null!\n");
2666 goto lio_xmit_failed;
2667 }
2668
2669 cmdsetup.s.gather = 1;
2670 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002671 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002672
2673 memset(g->sg, 0, g->sg_size);
2674
2675 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2676 skb->data,
2677 (skb->len - skb->data_len),
2678 DMA_TO_DEVICE);
2679 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2680 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2681 __func__);
2682 return NETDEV_TX_BUSY;
2683 }
2684 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2685
2686 frags = skb_shinfo(skb)->nr_frags;
2687 i = 1;
2688 while (frags--) {
2689 frag = &skb_shinfo(skb)->frags[i - 1];
2690
2691 g->sg[(i >> 2)].ptr[(i & 3)] =
2692 dma_map_page(&oct->pci_dev->dev,
2693 frag->page.p,
2694 frag->page_offset,
2695 frag->size,
2696 DMA_TO_DEVICE);
2697
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002698 if (dma_mapping_error(&oct->pci_dev->dev,
2699 g->sg[i >> 2].ptr[i & 3])) {
2700 dma_unmap_single(&oct->pci_dev->dev,
2701 g->sg[0].ptr[0],
2702 skb->len - skb->data_len,
2703 DMA_TO_DEVICE);
2704 for (j = 1; j < i; j++) {
2705 frag = &skb_shinfo(skb)->frags[j - 1];
2706 dma_unmap_page(&oct->pci_dev->dev,
2707 g->sg[j >> 2].ptr[j & 3],
2708 frag->size,
2709 DMA_TO_DEVICE);
2710 }
2711 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2712 __func__);
2713 return NETDEV_TX_BUSY;
2714 }
2715
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002716 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2717 i++;
2718 }
2719
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002720 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002721
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002722 if (OCTEON_CN23XX_PF(oct))
2723 ndata.cmd.cmd3.dptr = dptr;
2724 else
2725 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002726 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002727 finfo->g = g;
2728
2729 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2730 }
2731
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07002732 if (OCTEON_CN23XX_PF(oct)) {
2733 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
2734 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
2735 } else {
2736 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2737 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
2738 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002739
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002740 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002741 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2742 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002743 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002744 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002745
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002746 /* HW insert VLAN tag */
2747 if (skb_vlan_tag_present(skb)) {
2748 irh->priority = skb_vlan_tag_get(skb) >> 13;
2749 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2750 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002751
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002752 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002753 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002754 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002755 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002756 if (status == IQ_SEND_FAILED)
2757 goto lio_xmit_failed;
2758
2759 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2760
2761 if (status == IQ_SEND_STOP)
2762 stop_q(lio->netdev, q_idx);
2763
Florian Westphal860e9532016-05-03 16:33:13 +02002764 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002765
Satanand Burla80c8eae2017-01-26 11:52:35 -08002766 if (tx_info->s.gso_segs)
2767 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002768 else
2769 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08002770 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002771
2772 return NETDEV_TX_OK;
2773
2774lio_xmit_failed:
2775 stats->tx_dropped++;
2776 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2777 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002778 if (dptr)
2779 dma_unmap_single(&oct->pci_dev->dev, dptr,
2780 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002781 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002782 return NETDEV_TX_OK;
2783}
2784
2785/** \brief Network device Tx timeout
2786 * @param netdev pointer to network device
2787 */
2788static void liquidio_tx_timeout(struct net_device *netdev)
2789{
2790 struct lio *lio;
2791
2792 lio = GET_LIO(netdev);
2793
2794 netif_info(lio, tx_err, lio->netdev,
2795 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2796 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002797 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002798 txqs_wake(netdev);
2799}
2800
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07002801static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
2802 __be16 proto __attribute__((unused)),
2803 u16 vid)
2804{
2805 struct lio *lio = GET_LIO(netdev);
2806 struct octeon_device *oct = lio->oct_dev;
2807 struct octnic_ctrl_pkt nctrl;
2808 int ret = 0;
2809
2810 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2811
2812 nctrl.ncmd.u64 = 0;
2813 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
2814 nctrl.ncmd.s.param1 = vid;
2815 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2816 nctrl.wait_time = 100;
2817 nctrl.netpndev = (u64)netdev;
2818 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2819
2820 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2821 if (ret < 0) {
2822 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2823 ret);
2824 }
2825
2826 return ret;
2827}
2828
2829static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
2830 __be16 proto __attribute__((unused)),
2831 u16 vid)
2832{
2833 struct lio *lio = GET_LIO(netdev);
2834 struct octeon_device *oct = lio->oct_dev;
2835 struct octnic_ctrl_pkt nctrl;
2836 int ret = 0;
2837
2838 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2839
2840 nctrl.ncmd.u64 = 0;
2841 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
2842 nctrl.ncmd.s.param1 = vid;
2843 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2844 nctrl.wait_time = 100;
2845 nctrl.netpndev = (u64)netdev;
2846 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2847
2848 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2849 if (ret < 0) {
2850 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2851 ret);
2852 }
2853 return ret;
2854}
2855
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002856/** Sending command to enable/disable RX checksum offload
2857 * @param netdev pointer to network device
2858 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
2859 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
2860 * OCTNET_CMD_RXCSUM_DISABLE
2861 * @returns SUCCESS or FAILURE
2862 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02002863static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
2864 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002865{
2866 struct lio *lio = GET_LIO(netdev);
2867 struct octeon_device *oct = lio->oct_dev;
2868 struct octnic_ctrl_pkt nctrl;
2869 int ret = 0;
2870
Felix Manlunas0c264582017-04-06 19:22:22 -07002871 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2872
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002873 nctrl.ncmd.u64 = 0;
2874 nctrl.ncmd.s.cmd = command;
2875 nctrl.ncmd.s.param1 = rx_cmd;
2876 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2877 nctrl.wait_time = 100;
2878 nctrl.netpndev = (u64)netdev;
2879 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2880
2881 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2882 if (ret < 0) {
2883 dev_err(&oct->pci_dev->dev,
2884 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
2885 ret);
2886 }
2887 return ret;
2888}
2889
2890/** Sending command to add/delete VxLAN UDP port to firmware
2891 * @param netdev pointer to network device
2892 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
2893 * @param vxlan_port VxLAN port to be added or deleted
2894 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
2895 * OCTNET_CMD_VXLAN_PORT_DEL
2896 * @returns SUCCESS or FAILURE
2897 */
2898static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
2899 u16 vxlan_port, u8 vxlan_cmd_bit)
2900{
2901 struct lio *lio = GET_LIO(netdev);
2902 struct octeon_device *oct = lio->oct_dev;
2903 struct octnic_ctrl_pkt nctrl;
2904 int ret = 0;
2905
Felix Manlunas0c264582017-04-06 19:22:22 -07002906 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2907
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002908 nctrl.ncmd.u64 = 0;
2909 nctrl.ncmd.s.cmd = command;
2910 nctrl.ncmd.s.more = vxlan_cmd_bit;
2911 nctrl.ncmd.s.param1 = vxlan_port;
2912 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2913 nctrl.wait_time = 100;
2914 nctrl.netpndev = (u64)netdev;
2915 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2916
2917 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2918 if (ret < 0) {
2919 dev_err(&oct->pci_dev->dev,
2920 "VxLAN port add/delete failed in core (ret:0x%x)\n",
2921 ret);
2922 }
2923 return ret;
2924}
2925
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002926/** \brief Net device fix features
2927 * @param netdev pointer to network device
2928 * @param request features requested
2929 * @returns updated features list
2930 */
2931static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2932 netdev_features_t request)
2933{
2934 struct lio *lio = netdev_priv(netdev);
2935
2936 if ((request & NETIF_F_RXCSUM) &&
2937 !(lio->dev_capability & NETIF_F_RXCSUM))
2938 request &= ~NETIF_F_RXCSUM;
2939
2940 if ((request & NETIF_F_HW_CSUM) &&
2941 !(lio->dev_capability & NETIF_F_HW_CSUM))
2942 request &= ~NETIF_F_HW_CSUM;
2943
2944 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
2945 request &= ~NETIF_F_TSO;
2946
2947 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
2948 request &= ~NETIF_F_TSO6;
2949
2950 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
2951 request &= ~NETIF_F_LRO;
2952
2953 /*Disable LRO if RXCSUM is off */
2954 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
2955 (lio->dev_capability & NETIF_F_LRO))
2956 request &= ~NETIF_F_LRO;
2957
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07002958 if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2959 !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
2960 request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2961
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002962 return request;
2963}
2964
2965/** \brief Net device set features
2966 * @param netdev pointer to network device
2967 * @param features features to enable/disable
2968 */
2969static int liquidio_set_features(struct net_device *netdev,
2970 netdev_features_t features)
2971{
2972 struct lio *lio = netdev_priv(netdev);
2973
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07002974 if ((features & NETIF_F_LRO) &&
2975 (lio->dev_capability & NETIF_F_LRO) &&
2976 !(netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002977 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
2978 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002979 else if (!(features & NETIF_F_LRO) &&
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07002980 (lio->dev_capability & NETIF_F_LRO) &&
2981 (netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002982 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
2983 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002984
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002985 /* Sending command to firmware to enable/disable RX checksum
2986 * offload settings using ethtool
2987 */
2988 if (!(netdev->features & NETIF_F_RXCSUM) &&
2989 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2990 (features & NETIF_F_RXCSUM))
2991 liquidio_set_rxcsum_command(netdev,
2992 OCTNET_CMD_TNL_RX_CSUM_CTL,
2993 OCTNET_CMD_RXCSUM_ENABLE);
2994 else if ((netdev->features & NETIF_F_RXCSUM) &&
2995 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2996 !(features & NETIF_F_RXCSUM))
2997 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
2998 OCTNET_CMD_RXCSUM_DISABLE);
2999
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003000 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3001 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3002 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3003 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3004 OCTNET_CMD_VLAN_FILTER_ENABLE);
3005 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3006 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3007 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3008 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3009 OCTNET_CMD_VLAN_FILTER_DISABLE);
3010
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003011 return 0;
3012}
3013
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003014static void liquidio_add_vxlan_port(struct net_device *netdev,
3015 struct udp_tunnel_info *ti)
3016{
3017 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3018 return;
3019
3020 liquidio_vxlan_port_command(netdev,
3021 OCTNET_CMD_VXLAN_PORT_CONFIG,
3022 htons(ti->port),
3023 OCTNET_CMD_VXLAN_PORT_ADD);
3024}
3025
3026static void liquidio_del_vxlan_port(struct net_device *netdev,
3027 struct udp_tunnel_info *ti)
3028{
3029 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3030 return;
3031
3032 liquidio_vxlan_port_command(netdev,
3033 OCTNET_CMD_VXLAN_PORT_CONFIG,
3034 htons(ti->port),
3035 OCTNET_CMD_VXLAN_PORT_DEL);
3036}
3037
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003038static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3039 u8 *mac, bool is_admin_assigned)
3040{
3041 struct lio *lio = GET_LIO(netdev);
3042 struct octeon_device *oct = lio->oct_dev;
3043 struct octnic_ctrl_pkt nctrl;
3044
3045 if (!is_valid_ether_addr(mac))
3046 return -EINVAL;
3047
3048 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3049 return -EINVAL;
3050
3051 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3052
3053 nctrl.ncmd.u64 = 0;
3054 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3055 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3056 nctrl.ncmd.s.param1 = vfidx + 1;
3057 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3058 nctrl.ncmd.s.more = 1;
3059 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003060 nctrl.netpndev = (u64)netdev;
3061 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003062 nctrl.wait_time = LIO_CMD_WAIT_TM;
3063
3064 nctrl.udd[0] = 0;
3065 /* The MAC Address is presented in network byte order. */
3066 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3067
3068 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3069
3070 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3071
3072 return 0;
3073}
3074
3075static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3076{
3077 struct lio *lio = GET_LIO(netdev);
3078 struct octeon_device *oct = lio->oct_dev;
3079 int retval;
3080
Felix Manlunas0d9a5992017-05-16 11:28:00 -07003081 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3082 return -EINVAL;
3083
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003084 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3085 if (!retval)
3086 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3087
3088 return retval;
3089}
3090
3091static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3092 u16 vlan, u8 qos, __be16 vlan_proto)
3093{
3094 struct lio *lio = GET_LIO(netdev);
3095 struct octeon_device *oct = lio->oct_dev;
3096 struct octnic_ctrl_pkt nctrl;
3097 u16 vlantci;
3098
3099 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3100 return -EINVAL;
3101
3102 if (vlan_proto != htons(ETH_P_8021Q))
3103 return -EPROTONOSUPPORT;
3104
3105 if (vlan >= VLAN_N_VID || qos > 7)
3106 return -EINVAL;
3107
3108 if (vlan)
3109 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3110 else
3111 vlantci = 0;
3112
3113 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3114 return 0;
3115
3116 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3117
3118 if (vlan)
3119 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3120 else
3121 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3122
3123 nctrl.ncmd.s.param1 = vlantci;
3124 nctrl.ncmd.s.param2 =
3125 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3126 nctrl.ncmd.s.more = 0;
3127 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3128 nctrl.cb_fn = 0;
3129 nctrl.wait_time = LIO_CMD_WAIT_TM;
3130
3131 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3132
3133 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3134
3135 return 0;
3136}
3137
3138static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3139 struct ifla_vf_info *ivi)
3140{
3141 struct lio *lio = GET_LIO(netdev);
3142 struct octeon_device *oct = lio->oct_dev;
3143 u8 *macaddr;
3144
3145 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3146 return -EINVAL;
3147
3148 ivi->vf = vfidx;
3149 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3150 ether_addr_copy(&ivi->mac[0], macaddr);
3151 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3152 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3153 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3154 return 0;
3155}
3156
3157static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3158 int linkstate)
3159{
3160 struct lio *lio = GET_LIO(netdev);
3161 struct octeon_device *oct = lio->oct_dev;
3162 struct octnic_ctrl_pkt nctrl;
3163
3164 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3165 return -EINVAL;
3166
3167 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3168 return 0;
3169
3170 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3171 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3172 nctrl.ncmd.s.param1 =
3173 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3174 nctrl.ncmd.s.param2 = linkstate;
3175 nctrl.ncmd.s.more = 0;
3176 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3177 nctrl.cb_fn = 0;
3178 nctrl.wait_time = LIO_CMD_WAIT_TM;
3179
3180 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3181
3182 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3183
3184 return 0;
3185}
3186
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003187static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003188 .ndo_open = liquidio_open,
3189 .ndo_stop = liquidio_stop,
3190 .ndo_start_xmit = liquidio_xmit,
3191 .ndo_get_stats = liquidio_get_stats,
3192 .ndo_set_mac_address = liquidio_set_mac,
3193 .ndo_set_rx_mode = liquidio_set_mcast_list,
3194 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003195
3196 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3197 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003198 .ndo_change_mtu = liquidio_change_mtu,
3199 .ndo_do_ioctl = liquidio_ioctl,
3200 .ndo_fix_features = liquidio_fix_features,
3201 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003202 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3203 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003204 .ndo_set_vf_mac = liquidio_set_vf_mac,
3205 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3206 .ndo_get_vf_config = liquidio_get_vf_config,
3207 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003208};
3209
3210/** \brief Entry point for the liquidio module
3211 */
3212static int __init liquidio_init(void)
3213{
3214 int i;
3215 struct handshake *hs;
3216
3217 init_completion(&first_stage);
3218
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003219 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003220
3221 if (liquidio_init_pci())
3222 return -EINVAL;
3223
3224 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3225
3226 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3227 hs = &handshake[i];
3228 if (hs->pci_dev) {
3229 wait_for_completion(&hs->init);
3230 if (!hs->init_ok) {
3231 /* init handshake failed */
3232 dev_err(&hs->pci_dev->dev,
3233 "Failed to init device\n");
3234 liquidio_deinit_pci();
3235 return -EIO;
3236 }
3237 }
3238 }
3239
3240 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3241 hs = &handshake[i];
3242 if (hs->pci_dev) {
3243 wait_for_completion_timeout(&hs->started,
3244 msecs_to_jiffies(30000));
3245 if (!hs->started_ok) {
3246 /* starter handshake failed */
3247 dev_err(&hs->pci_dev->dev,
3248 "Firmware failed to start\n");
3249 liquidio_deinit_pci();
3250 return -EIO;
3251 }
3252 }
3253 }
3254
3255 return 0;
3256}
3257
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003258static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003259{
3260 struct octeon_device *oct = (struct octeon_device *)buf;
3261 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003262 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003263 union oct_link_status *ls;
3264 int i;
3265
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003266 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003267 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3268 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003269 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003270 goto nic_info_err;
3271 }
3272
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003273 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003274 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3275 OCT_DROQ_INFO_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003276
3277 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003278 for (i = 0; i < oct->ifcount; i++) {
3279 if (oct->props[i].gmxport == gmxport) {
3280 update_link_status(oct->props[i].netdev, ls);
3281 break;
3282 }
3283 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003284
3285nic_info_err:
3286 for (i = 0; i < recv_pkt->buffer_count; i++)
3287 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3288 octeon_free_recv_info(recv_info);
3289 return 0;
3290}
3291
3292/**
3293 * \brief Setup network interfaces
3294 * @param octeon_dev octeon device
3295 *
3296 * Called during init time for each device. It assumes the NIC
3297 * is already up and running. The link information for each
3298 * interface is passed in link_info.
3299 */
3300static int setup_nic_devices(struct octeon_device *octeon_dev)
3301{
3302 struct lio *lio = NULL;
3303 struct net_device *netdev;
3304 u8 mac[6], i, j;
3305 struct octeon_soft_command *sc;
3306 struct liquidio_if_cfg_context *ctx;
3307 struct liquidio_if_cfg_resp *resp;
3308 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003309 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003310 union oct_nic_if_cfg if_cfg;
3311 unsigned int base_queue;
3312 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003313 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003314 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003315 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003316
3317 /* This is to handle link status changes */
3318 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3319 OPCODE_NIC_INFO,
3320 lio_nic_info, octeon_dev);
3321
3322 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3323 * They are handled directly.
3324 */
3325 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3326 free_netbuf);
3327
3328 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3329 free_netsgbuf);
3330
3331 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3332 free_netsgbuf_with_resp);
3333
3334 for (i = 0; i < octeon_dev->ifcount; i++) {
3335 resp_size = sizeof(struct liquidio_if_cfg_resp);
3336 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003337 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003339 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003340 resp_size, ctx_size);
3341 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3342 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003343 vdata = (struct lio_version *)sc->virtdptr;
3344
3345 *((u64 *)vdata) = 0;
3346 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3347 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3348 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003349
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003350 if (OCTEON_CN23XX_PF(octeon_dev)) {
3351 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3352 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3353 base_queue = octeon_dev->sriov_info.pf_srn;
3354
3355 gmx_port_id = octeon_dev->pf_num;
3356 ifidx_or_pfnum = octeon_dev->pf_num;
3357 } else {
3358 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3359 octeon_get_conf(octeon_dev), i);
3360 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3361 octeon_get_conf(octeon_dev), i);
3362 base_queue = CFG_GET_BASE_QUE_NIC_IF(
3363 octeon_get_conf(octeon_dev), i);
3364 gmx_port_id = CFG_GET_GMXID_NIC_IF(
3365 octeon_get_conf(octeon_dev), i);
3366 ifidx_or_pfnum = i;
3367 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003368
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003369 dev_dbg(&octeon_dev->pci_dev->dev,
3370 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003371 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003372 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003373 ctx->octeon_id = lio_get_device_id(octeon_dev);
3374 init_waitqueue_head(&ctx->wc);
3375
3376 if_cfg.u64 = 0;
3377 if_cfg.s.num_iqueues = num_iqueues;
3378 if_cfg.s.num_oqueues = num_oqueues;
3379 if_cfg.s.base_queue = base_queue;
3380 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003381
3382 sc->iq_no = 0;
3383
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003384 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003385 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003386 if_cfg.u64, 0);
3387
3388 sc->callback = if_cfg_callback;
3389 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003390 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003391
3392 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003393 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003394 dev_err(&octeon_dev->pci_dev->dev,
3395 "iq/oq config failed status: %x\n",
3396 retval);
3397 /* Soft instr is freed by driver in case of failure. */
3398 goto setup_nic_dev_fail;
3399 }
3400
3401 /* Sleep on a wait queue till the cond flag indicates that the
3402 * response arrived or timed-out.
3403 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003404 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3405 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3406 goto setup_nic_wait_intr;
3407 }
3408
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003409 retval = resp->status;
3410 if (retval) {
3411 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3412 goto setup_nic_dev_fail;
3413 }
3414
3415 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3416 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3417
3418 num_iqueues = hweight64(resp->cfg_info.iqmask);
3419 num_oqueues = hweight64(resp->cfg_info.oqmask);
3420
3421 if (!(num_iqueues) || !(num_oqueues)) {
3422 dev_err(&octeon_dev->pci_dev->dev,
3423 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3424 resp->cfg_info.iqmask,
3425 resp->cfg_info.oqmask);
3426 goto setup_nic_dev_fail;
3427 }
3428 dev_dbg(&octeon_dev->pci_dev->dev,
3429 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3430 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3431 num_iqueues, num_oqueues);
3432 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3433
3434 if (!netdev) {
3435 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3436 goto setup_nic_dev_fail;
3437 }
3438
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003439 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003440
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003441 /* Associate the routines that will handle different
3442 * netdev tasks.
3443 */
3444 netdev->netdev_ops = &lionetdevops;
3445
3446 lio = GET_LIO(netdev);
3447
3448 memset(lio, 0, sizeof(struct lio));
3449
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003450 lio->ifidx = ifidx_or_pfnum;
3451
3452 props = &octeon_dev->props[i];
3453 props->gmxport = resp->cfg_info.linfo.gmxport;
3454 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003455
3456 lio->linfo.num_rxpciq = num_oqueues;
3457 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003458 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003459 lio->linfo.rxpciq[j].u64 =
3460 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003461 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003462 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003463 lio->linfo.txpciq[j].u64 =
3464 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003465 }
3466 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3467 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3468 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3469
3470 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3471
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07003472 if (OCTEON_CN23XX_PF(octeon_dev) ||
3473 OCTEON_CN6XXX(octeon_dev)) {
3474 lio->dev_capability = NETIF_F_HIGHDMA
3475 | NETIF_F_IP_CSUM
3476 | NETIF_F_IPV6_CSUM
3477 | NETIF_F_SG | NETIF_F_RXCSUM
3478 | NETIF_F_GRO
3479 | NETIF_F_TSO | NETIF_F_TSO6
3480 | NETIF_F_LRO;
3481 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003482 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3483
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003484 /* Copy of transmit encapsulation capabilities:
3485 * TSO, TSO6, Checksums for this device
3486 */
3487 lio->enc_dev_capability = NETIF_F_IP_CSUM
3488 | NETIF_F_IPV6_CSUM
3489 | NETIF_F_GSO_UDP_TUNNEL
3490 | NETIF_F_HW_CSUM | NETIF_F_SG
3491 | NETIF_F_RXCSUM
3492 | NETIF_F_TSO | NETIF_F_TSO6
3493 | NETIF_F_LRO;
3494
3495 netdev->hw_enc_features = (lio->enc_dev_capability &
3496 ~NETIF_F_LRO);
3497
3498 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3499
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003500 netdev->vlan_features = lio->dev_capability;
3501 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003502 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3503 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003504 NETIF_F_HW_VLAN_CTAG_TX;
3505
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003506 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3507
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003508 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003509 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3510 netdev->hw_features = netdev->hw_features &
3511 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003512
Jarod Wilson109cc162016-10-17 15:54:13 -04003513 /* MTU range: 68 - 16000 */
3514 netdev->min_mtu = LIO_MIN_MTU_SIZE;
3515 netdev->max_mtu = LIO_MAX_MTU_SIZE;
3516
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003517 /* Point to the properties for octeon device to which this
3518 * interface belongs.
3519 */
3520 lio->oct_dev = octeon_dev;
3521 lio->octprops = props;
3522 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003523
3524 dev_dbg(&octeon_dev->pci_dev->dev,
3525 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3526 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3527
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003528 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
3529 u8 vfmac[ETH_ALEN];
3530
3531 random_ether_addr(&vfmac[0]);
3532 if (__liquidio_set_vf_mac(netdev, j,
3533 &vfmac[0], false)) {
3534 dev_err(&octeon_dev->pci_dev->dev,
3535 "Error setting VF%d MAC address\n",
3536 j);
3537 goto setup_nic_dev_fail;
3538 }
3539 }
3540
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003541 /* 64-bit swap required on LE machines */
3542 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3543 for (j = 0; j < 6; j++)
3544 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3545
3546 /* Copy MAC Address to OS network device structure */
3547
3548 ether_addr_copy(netdev->dev_addr, mac);
3549
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003550 /* By default all interfaces on a single Octeon uses the same
3551 * tx and rx queues
3552 */
3553 lio->txq = lio->linfo.txpciq[0].s.q_no;
3554 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07003555 if (liquidio_setup_io_queues(octeon_dev, i,
3556 lio->linfo.num_txpciq,
3557 lio->linfo.num_rxpciq)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003558 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3559 goto setup_nic_dev_fail;
3560 }
3561
3562 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3563
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003564 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3565 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3566
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003567 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003568 dev_err(&octeon_dev->pci_dev->dev,
3569 "Gather list allocation failed\n");
3570 goto setup_nic_dev_fail;
3571 }
3572
3573 /* Register ethtool support */
3574 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003575 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
3576 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
3577 else
3578 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003579
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003580 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003581 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3582 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003583
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003584 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3585 OCTNET_CMD_VLAN_FILTER_ENABLE);
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003586
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003587 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003588 liquidio_set_feature(netdev,
3589 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003590
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07003591 if (setup_link_status_change_wq(netdev))
3592 goto setup_nic_dev_fail;
3593
Satanand Burla031d4f12017-03-22 11:31:13 -07003594 if (setup_rx_oom_poll_fn(netdev))
3595 goto setup_nic_dev_fail;
3596
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003597 /* Register the network device with the OS */
3598 if (register_netdev(netdev)) {
3599 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3600 goto setup_nic_dev_fail;
3601 }
3602
3603 dev_dbg(&octeon_dev->pci_dev->dev,
3604 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3605 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3606 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003607 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003608
3609 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3610
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003611 /* Sending command to firmware to enable Rx checksum offload
3612 * by default at the time of setup of Liquidio driver for
3613 * this device
3614 */
3615 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3616 OCTNET_CMD_RXCSUM_ENABLE);
3617 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3618 OCTNET_CMD_TXCSUM_ENABLE);
3619
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003620 dev_dbg(&octeon_dev->pci_dev->dev,
3621 "NIC ifidx:%d Setup successful\n", i);
3622
3623 octeon_free_soft_command(octeon_dev, sc);
3624 }
3625
3626 return 0;
3627
3628setup_nic_dev_fail:
3629
3630 octeon_free_soft_command(octeon_dev, sc);
3631
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07003632setup_nic_wait_intr:
3633
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003634 while (i--) {
3635 dev_err(&octeon_dev->pci_dev->dev,
3636 "NIC ifidx:%d Setup failed\n", i);
3637 liquidio_destroy_nic_device(octeon_dev, i);
3638 }
3639 return -ENODEV;
3640}
3641
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08003642#ifdef CONFIG_PCI_IOV
3643static int octeon_enable_sriov(struct octeon_device *oct)
3644{
3645 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
3646 struct pci_dev *vfdev;
3647 int err;
3648 u32 u;
3649
3650 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
3651 err = pci_enable_sriov(oct->pci_dev,
3652 oct->sriov_info.num_vfs_alloced);
3653 if (err) {
3654 dev_err(&oct->pci_dev->dev,
3655 "OCTEON: Failed to enable PCI sriov: %d\n",
3656 err);
3657 oct->sriov_info.num_vfs_alloced = 0;
3658 return err;
3659 }
3660 oct->sriov_info.sriov_enabled = 1;
3661
3662 /* init lookup table that maps DPI ring number to VF pci_dev
3663 * struct pointer
3664 */
3665 u = 0;
3666 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3667 OCTEON_CN23XX_VF_VID, NULL);
3668 while (vfdev) {
3669 if (vfdev->is_virtfn &&
3670 (vfdev->physfn == oct->pci_dev)) {
3671 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
3672 vfdev;
3673 u += oct->sriov_info.rings_per_vf;
3674 }
3675 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3676 OCTEON_CN23XX_VF_VID, vfdev);
3677 }
3678 }
3679
3680 return num_vfs_alloced;
3681}
3682
3683static int lio_pci_sriov_disable(struct octeon_device *oct)
3684{
3685 int u;
3686
3687 if (pci_vfs_assigned(oct->pci_dev)) {
3688 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
3689 return -EPERM;
3690 }
3691
3692 pci_disable_sriov(oct->pci_dev);
3693
3694 u = 0;
3695 while (u < MAX_POSSIBLE_VFS) {
3696 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
3697 u += oct->sriov_info.rings_per_vf;
3698 }
3699
3700 oct->sriov_info.num_vfs_alloced = 0;
3701 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
3702 oct->pf_num);
3703
3704 return 0;
3705}
3706
3707static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
3708{
3709 struct octeon_device *oct = pci_get_drvdata(dev);
3710 int ret = 0;
3711
3712 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
3713 (oct->sriov_info.sriov_enabled)) {
3714 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
3715 oct->pf_num, num_vfs);
3716 return 0;
3717 }
3718
3719 if (!num_vfs) {
3720 ret = lio_pci_sriov_disable(oct);
3721 } else if (num_vfs > oct->sriov_info.max_vfs) {
3722 dev_err(&oct->pci_dev->dev,
3723 "OCTEON: Max allowed VFs:%d user requested:%d",
3724 oct->sriov_info.max_vfs, num_vfs);
3725 ret = -EPERM;
3726 } else {
3727 oct->sriov_info.num_vfs_alloced = num_vfs;
3728 ret = octeon_enable_sriov(oct);
3729 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
3730 oct->pf_num, num_vfs);
3731 }
3732
3733 return ret;
3734}
3735#endif
3736
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003737/**
3738 * \brief initialize the NIC
3739 * @param oct octeon device
3740 *
3741 * This initialization routine is called once the Octeon device application is
3742 * up and running
3743 */
3744static int liquidio_init_nic_module(struct octeon_device *oct)
3745{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003746 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003747 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3748
3749 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3750
3751 /* only default iq and oq were initialized
3752 * initialize the rest as well
3753 */
3754 /* run port_config command for each port */
3755 oct->ifcount = num_nic_ports;
3756
Raghu Vatsavayi30136392016-09-01 11:16:11 -07003757 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003758
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003759 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3760 oct->props[i].gmxport = -1;
3761
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003762 retval = setup_nic_devices(oct);
3763 if (retval) {
3764 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3765 goto octnet_init_failure;
3766 }
3767
3768 liquidio_ptp_init(oct);
3769
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003770 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3771
3772 return retval;
3773
3774octnet_init_failure:
3775
3776 oct->ifcount = 0;
3777
3778 return retval;
3779}
3780
3781/**
3782 * \brief starter callback that invokes the remaining initialization work after
3783 * the NIC is up and running.
3784 * @param octptr work struct work_struct
3785 */
3786static void nic_starter(struct work_struct *work)
3787{
3788 struct octeon_device *oct;
3789 struct cavium_wk *wk = (struct cavium_wk *)work;
3790
3791 oct = (struct octeon_device *)wk->ctxptr;
3792
3793 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3794 return;
3795
3796 /* If the status of the device is CORE_OK, the core
3797 * application has reported its application type. Call
3798 * any registered handlers now and move to the RUNNING
3799 * state.
3800 */
3801 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3802 schedule_delayed_work(&oct->nic_poll_work.work,
3803 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3804 return;
3805 }
3806
3807 atomic_set(&oct->status, OCT_DEV_RUNNING);
3808
3809 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3810 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3811
3812 if (liquidio_init_nic_module(oct))
3813 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3814 else
3815 handshake[oct->octeon_id].started_ok = 1;
3816 } else {
3817 dev_err(&oct->pci_dev->dev,
3818 "Unexpected application running on NIC (%d). Check firmware.\n",
3819 oct->app_mode);
3820 }
3821
3822 complete(&handshake[oct->octeon_id].started);
3823}
3824
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003825static int
3826octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
3827{
3828 struct octeon_device *oct = (struct octeon_device *)buf;
3829 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
3830 int i, notice, vf_idx;
Felix Manlunasbb54be52017-04-04 19:26:57 -07003831 bool cores_crashed;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003832 u64 *data, vf_num;
3833
3834 notice = recv_pkt->rh.r.ossp;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003835 data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003836
3837 /* the first 64-bit word of data is the vf_num */
3838 vf_num = data[0];
3839 octeon_swap_8B_data(&vf_num, 1);
3840 vf_idx = (int)vf_num - 1;
3841
Felix Manlunasbb54be52017-04-04 19:26:57 -07003842 cores_crashed = READ_ONCE(oct->cores_crashed);
3843
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003844 if (notice == VF_DRV_LOADED) {
3845 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
3846 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
3847 dev_info(&oct->pci_dev->dev,
3848 "driver for VF%d was loaded\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07003849 if (!cores_crashed)
3850 try_module_get(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003851 }
3852 } else if (notice == VF_DRV_REMOVED) {
3853 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
3854 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
3855 dev_info(&oct->pci_dev->dev,
3856 "driver for VF%d was removed\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07003857 if (!cores_crashed)
3858 module_put(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003859 }
3860 } else if (notice == VF_DRV_MACADDR_CHANGED) {
3861 u8 *b = (u8 *)&data[1];
3862
3863 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
3864 dev_info(&oct->pci_dev->dev,
3865 "VF driver changed VF%d's MAC address to %pM\n",
3866 vf_idx, b + 2);
3867 }
3868
3869 for (i = 0; i < recv_pkt->buffer_count; i++)
3870 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3871 octeon_free_recv_info(recv_info);
3872
3873 return 0;
3874}
3875
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003876/**
3877 * \brief Device initialization for each Octeon device that is probed
3878 * @param octeon_dev octeon device
3879 */
3880static int octeon_device_init(struct octeon_device *octeon_dev)
3881{
3882 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07003883 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07003884 char bootcmd[] = "\n";
Rick Farringtonda1542b2017-08-11 18:43:14 -07003885 char *dbg_enb = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003886 struct octeon_device_priv *oct_priv =
3887 (struct octeon_device_priv *)octeon_dev->priv;
3888 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3889
3890 /* Enable access to the octeon device and make its DMA capability
3891 * known to the OS.
3892 */
3893 if (octeon_pci_os_setup(octeon_dev))
3894 return 1;
3895
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08003896 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
3897
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003898 /* Identify the Octeon type and map the BAR address space. */
3899 if (octeon_chip_specific_setup(octeon_dev)) {
3900 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3901 return 1;
3902 }
3903
3904 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3905
Rick Farringtone1e3ce62017-05-16 11:14:50 -07003906 /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
3907 * since that is what is required for the reference to be removed
3908 * during de-initialization (see 'octeon_destroy_resources').
3909 */
3910 octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
3911 PCI_SLOT(octeon_dev->pci_dev->devfn),
3912 PCI_FUNC(octeon_dev->pci_dev->devfn),
3913 true);
3914
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003915 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3916
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07003917 if (OCTEON_CN23XX_PF(octeon_dev)) {
Rick Farrington70535352017-08-17 23:11:25 -07003918 if (!cn23xx_fw_loaded(octeon_dev) && !fw_type_is_none()) {
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07003919 fw_loaded = 0;
Rick Farrington70535352017-08-17 23:11:25 -07003920 /* Do a soft reset of the Octeon device. */
3921 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3922 return 1;
3923 /* things might have changed */
3924 if (!cn23xx_fw_loaded(octeon_dev))
3925 fw_loaded = 0;
3926 else
3927 fw_loaded = 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07003928 } else {
3929 fw_loaded = 1;
3930 }
3931 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003932 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07003933 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003934
3935 /* Initialize the dispatch mechanism used to push packets arriving on
3936 * Octeon Output queues.
3937 */
3938 if (octeon_init_dispatch_list(octeon_dev))
3939 return 1;
3940
3941 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3942 OPCODE_NIC_CORE_DRV_ACTIVE,
3943 octeon_core_drv_init,
3944 octeon_dev);
3945
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003946 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3947 OPCODE_NIC_VF_DRV_NOTICE,
3948 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003949 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3950 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3951 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3952 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3953
3954 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3955
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08003956 if (octeon_set_io_queues_off(octeon_dev)) {
3957 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
3958 return 1;
3959 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003960
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07003961 if (OCTEON_CN23XX_PF(octeon_dev)) {
3962 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3963 if (ret) {
3964 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
3965 return ret;
3966 }
3967 }
3968
3969 /* Initialize soft command buffer pool
3970 */
3971 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3972 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3973 return 1;
3974 }
3975 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3976
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003977 /* Setup the data structures that manage this Octeon's Input queues. */
3978 if (octeon_setup_instr_queues(octeon_dev)) {
3979 dev_err(&octeon_dev->pci_dev->dev,
3980 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003981 return 1;
3982 }
3983 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3984
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003985 /* Initialize lists to manage the requests of different types that
3986 * arrive from user & kernel applications for this octeon device.
3987 */
3988 if (octeon_setup_response_list(octeon_dev)) {
3989 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3990 return 1;
3991 }
3992 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3993
3994 if (octeon_setup_output_queues(octeon_dev)) {
3995 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07003996 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003997 }
3998
3999 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4000
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004001 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004002 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4003 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4004 return 1;
4005 }
4006 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4007
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004008 if (octeon_allocate_ioq_vector(octeon_dev)) {
4009 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4010 return 1;
4011 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004012 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004013
4014 } else {
4015 /* The input and output queue registers were setup earlier (the
4016 * queues were not enabled). Any additional registers
4017 * that need to be programmed should be done now.
4018 */
4019 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4020 if (ret) {
4021 dev_err(&octeon_dev->pci_dev->dev,
4022 "Failed to configure device registers\n");
4023 return ret;
4024 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004025 }
4026
4027 /* Initialize the tasklet that handles output queue packet processing.*/
4028 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4029 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4030 (unsigned long)octeon_dev);
4031
4032 /* Setup the interrupt handler and record the INT SUM register address
4033 */
Intiyaz Bashaa82457f2017-08-15 12:46:18 -07004034 if (octeon_setup_interrupt(octeon_dev,
4035 octeon_dev->sriov_info.num_pf_rings))
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004036 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004037
4038 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004039 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004040
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004041 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4042
Rick Farrington3c57f612017-08-17 23:11:30 -07004043 /* Send Credit for Octeon Output queues. Credits are always sent BEFORE
4044 * the output queue is enabled.
4045 * This ensures that we'll receive the f/w CORE DRV_ACTIVE message in
4046 * case we've configured CN23XX_SLI_GBL_CONTROL[NOPTR_D] = 0.
4047 * Otherwise, it is possible that the DRV_ACTIVE message will be sent
4048 * before any credits have been issued, causing the ring to be reset
4049 * (and the f/w appear to never have started).
4050 */
4051 for (j = 0; j < octeon_dev->num_oqs; j++)
4052 writel(octeon_dev->droq[j]->max_count,
4053 octeon_dev->droq[j]->pkts_credit_reg);
4054
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004055 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004056 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4057 if (ret) {
4058 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4059 return ret;
4060 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004061
4062 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4063
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004064 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4065 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4066 if (!ddr_timeout) {
4067 dev_info(&octeon_dev->pci_dev->dev,
4068 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4069 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004070
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004071 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004072
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004073 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4074 while (!ddr_timeout) {
4075 set_current_state(TASK_INTERRUPTIBLE);
4076 if (schedule_timeout(HZ / 10)) {
4077 /* user probably pressed Control-C */
4078 return 1;
4079 }
4080 }
4081 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4082 if (ret) {
4083 dev_err(&octeon_dev->pci_dev->dev,
4084 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4085 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004086 return 1;
4087 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004088
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004089 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4090 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4091 return 1;
4092 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004093
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004094 /* Divert uboot to take commands from host instead. */
4095 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004096
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004097 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4098 ret = octeon_init_consoles(octeon_dev);
4099 if (ret) {
4100 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4101 return 1;
4102 }
Rick Farringtonda1542b2017-08-11 18:43:14 -07004103 /* If console debug enabled, specify empty string to use default
4104 * enablement ELSE specify NULL string for 'disabled'.
4105 */
4106 dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
4107 ret = octeon_add_console(octeon_dev, 0, dbg_enb);
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004108 if (ret) {
4109 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4110 return 1;
Rick Farringtonda1542b2017-08-11 18:43:14 -07004111 } else if (octeon_console_debug_enabled(0)) {
4112 /* If console was added AND we're logging console output
4113 * then set our console print function.
4114 */
4115 octeon_dev->console[0].print = octeon_dbg_console_print;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004116 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004117
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004118 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004119
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004120 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4121 ret = load_firmware(octeon_dev);
4122 if (ret) {
4123 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4124 return 1;
4125 }
4126 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4127 * loaded
4128 */
4129 if (OCTEON_CN23XX_PF(octeon_dev))
4130 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4131 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004132 }
4133
4134 handshake[octeon_dev->octeon_id].init_ok = 1;
4135 complete(&handshake[octeon_dev->octeon_id].init);
4136
4137 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4138
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004139 return 0;
4140}
4141
4142/**
Rick Farringtonda1542b2017-08-11 18:43:14 -07004143 * \brief Debug console print function
4144 * @param octeon_dev octeon device
4145 * @param console_num console number
4146 * @param prefix first portion of line to display
4147 * @param suffix second portion of line to display
4148 *
4149 * The OCTEON debug console outputs entire lines (excluding '\n').
4150 * Normally, the line will be passed in the 'prefix' parameter.
4151 * However, due to buffering, it is possible for a line to be split into two
4152 * parts, in which case they will be passed as the 'prefix' parameter and
4153 * 'suffix' parameter.
4154 */
4155static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
4156 char *prefix, char *suffix)
4157{
4158 if (prefix && suffix)
4159 dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
4160 suffix);
4161 else if (prefix)
4162 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
4163 else if (suffix)
4164 dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
4165
4166 return 0;
4167}
4168
4169/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004170 * \brief Exits the module
4171 */
4172static void __exit liquidio_exit(void)
4173{
4174 liquidio_deinit_pci();
4175
4176 pr_info("LiquidIO network module is now unloaded\n");
4177}
4178
4179module_init(liquidio_init);
4180module_exit(liquidio_exit);