blob: 8bf6dfcf5881a92d71024e212444b9412e2b3b6a [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
Russell Kinge3bfc6e2017-02-07 15:03:03 -080018#include <linux/module.h>
Florian Westphal282ccf62017-03-29 17:17:31 +020019#include <linux/interrupt.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070020#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070021#include <linux/firmware.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070022#include <net/vxlan.h>
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -070023#include <linux/kthread.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include "liquidio_common.h"
25#include "octeon_droq.h"
26#include "octeon_iq.h"
27#include "response_manager.h"
28#include "octeon_device.h"
29#include "octeon_nic.h"
30#include "octeon_main.h"
31#include "octeon_network.h"
32#include "cn66xx_regs.h"
33#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070034#include "cn68xx_device.h"
Raghu Vatsavayi72c00912016-08-31 11:03:25 -070035#include "cn23xx_pf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070036#include "liquidio_image.h"
37
38MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
39MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
40MODULE_LICENSE("GPL");
41MODULE_VERSION(LIQUIDIO_VERSION);
Derek Chicklesea6404c82017-08-07 12:22:15 -070042MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
43 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
44MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
45 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
47 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
48MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
49 "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070050
51static int ddr_timeout = 10000;
52module_param(ddr_timeout, int, 0644);
53MODULE_PARM_DESC(ddr_timeout,
54 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
55
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070056#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
57
58static int debug = -1;
59module_param(debug, int, 0644);
60MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
61
62static char fw_type[LIO_MAX_FW_TYPE_LEN];
63module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
64MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
65
Intiyaz Basha2470f3a2017-08-03 15:10:17 -070066static u32 console_bitmask;
67module_param(console_bitmask, int, 0644);
68MODULE_PARM_DESC(console_bitmask,
69 "Bitmask indicating which consoles have debug output redirected to syslog.");
70
71/**
72 * \brief determines if a given console has debug enabled.
73 * @param console console to check
74 * @returns 1 = enabled. 0 otherwise
75 */
76int octeon_console_debug_enabled(u32 console)
77{
78 return (console_bitmask >> (console)) & 0x1;
79}
80
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070081/* Polling interval for determining when NIC application is alive */
82#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
83
84/* runtime link query interval */
85#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
86
87struct liquidio_if_cfg_context {
88 int octeon_id;
89
90 wait_queue_head_t wc;
91
92 int cond;
93};
94
95struct liquidio_if_cfg_resp {
96 u64 rh;
97 struct liquidio_if_cfg_info cfg_info;
98 u64 status;
99};
100
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -0700101struct liquidio_rx_ctl_context {
102 int octeon_id;
103
104 wait_queue_head_t wc;
105
106 int cond;
107};
108
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700109struct oct_link_status_resp {
110 u64 rh;
111 struct oct_link_info link_info;
112 u64 status;
113};
114
115struct oct_timestamp_resp {
116 u64 rh;
117 u64 timestamp;
118 u64 status;
119};
120
121#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
122
123union tx_info {
124 u64 u64;
125 struct {
126#ifdef __BIG_ENDIAN_BITFIELD
127 u16 gso_size;
128 u16 gso_segs;
129 u32 reserved;
130#else
131 u32 reserved;
132 u16 gso_segs;
133 u16 gso_size;
134#endif
135 } s;
136};
137
138/** Octeon device properties to be used by the NIC module.
139 * Each octeon device in the system will be represented
140 * by this structure in the NIC module.
141 */
142
143#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
144
145#define OCTNIC_GSO_MAX_HEADER_SIZE 128
Raghu Vatsavayi72c00912016-08-31 11:03:25 -0700146#define OCTNIC_GSO_MAX_SIZE \
147 (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700148
149/** Structure of a node in list of gather components maintained by
150 * NIC driver for each network device.
151 */
152struct octnic_gather {
153 /** List manipulation. Next and prev pointers. */
154 struct list_head list;
155
156 /** Size of the gather component at sg in bytes. */
157 int sg_size;
158
159 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
160 int adjust;
161
162 /** Gather component that can accommodate max sized fragment list
163 * received from the IP layer.
164 */
165 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700166
VSR Burru67e303e2017-03-06 18:45:59 -0800167 dma_addr_t sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700168};
169
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700170struct handshake {
171 struct completion init;
172 struct completion started;
173 struct pci_dev *pci_dev;
174 int init_ok;
175 int started_ok;
176};
177
178struct octeon_device_priv {
179 /** Tasklet structures for this device. */
180 struct tasklet_struct droq_tasklet;
181 unsigned long napi_mask;
182};
183
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800184#ifdef CONFIG_PCI_IOV
185static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
186#endif
187
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700188static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700189static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700190static void liquidio_remove(struct pci_dev *pdev);
191static int liquidio_probe(struct pci_dev *pdev,
192 const struct pci_device_id *ent);
Felix Manlunasbb54be52017-04-04 19:26:57 -0700193static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
194 int linkstate);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700195
196static struct handshake handshake[MAX_OCTEON_DEVICES];
197static struct completion first_stage;
198
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700199static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700200{
201 int q_no;
202 int reschedule = 0;
203 struct octeon_device *oct = (struct octeon_device *)pdev;
204 struct octeon_device_priv *oct_priv =
205 (struct octeon_device_priv *)oct->priv;
206
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700207 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800208 if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700209 continue;
210 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
211 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700212 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700213
214 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
215 /* set time and cnt interrupt thresholds for this DROQ
216 * for NAPI
217 */
218 int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
219
220 octeon_write_csr64(
221 oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
222 0x5700000040ULL);
223 octeon_write_csr64(
224 oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
225 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700226 }
227
228 if (reschedule)
229 tasklet_schedule(&oct_priv->droq_tasklet);
230}
231
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700232static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700233{
234 struct octeon_device_priv *oct_priv =
235 (struct octeon_device_priv *)oct->priv;
236 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
237 int i;
238
239 do {
240 pending_pkts = 0;
241
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700242 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800243 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700244 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700245 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700246 }
247 if (pkt_cnt > 0) {
248 pending_pkts += pkt_cnt;
249 tasklet_schedule(&oct_priv->droq_tasklet);
250 }
251 pkt_cnt = 0;
252 schedule_timeout_uninterruptible(1);
253
254 } while (retry-- && pending_pkts);
255
256 return pkt_cnt;
257}
258
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700259/**
260 * \brief Forces all IO queues off on a given device
261 * @param oct Pointer to Octeon device
262 */
263static void force_io_queues_off(struct octeon_device *oct)
264{
265 if ((oct->chip_id == OCTEON_CN66XX) ||
266 (oct->chip_id == OCTEON_CN68XX)) {
267 /* Reset the Enable bits for Input Queues. */
268 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
269
270 /* Reset the Enable bits for Output Queues. */
271 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
272 }
273}
274
275/**
276 * \brief wait for all pending requests to complete
277 * @param oct Pointer to Octeon device
278 *
279 * Called during shutdown sequence
280 */
281static int wait_for_pending_requests(struct octeon_device *oct)
282{
283 int i, pcount = 0;
284
285 for (i = 0; i < 100; i++) {
286 pcount =
287 atomic_read(&oct->response_list
288 [OCTEON_ORDERED_SC_LIST].pending_req_count);
289 if (pcount)
290 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700291 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700292 break;
293 }
294
295 if (pcount)
296 return 1;
297
298 return 0;
299}
300
301/**
302 * \brief Cause device to go quiet so it can be safely removed/reset/etc
303 * @param oct Pointer to Octeon device
304 */
305static inline void pcierror_quiesce_device(struct octeon_device *oct)
306{
307 int i;
308
309 /* Disable the input and output queues now. No more packets will
310 * arrive from Octeon, but we should wait for all packet processing
311 * to finish.
312 */
313 force_io_queues_off(oct);
314
315 /* To allow for in-flight requests */
316 schedule_timeout_uninterruptible(100);
317
318 if (wait_for_pending_requests(oct))
319 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
320
321 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700322 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700323 struct octeon_instr_queue *iq;
324
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800325 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700326 continue;
327 iq = oct->instr_queue[i];
328
329 if (atomic_read(&iq->instr_pending)) {
330 spin_lock_bh(&iq->lock);
331 iq->fill_cnt = 0;
332 iq->octeon_read_index = iq->host_write_index;
333 iq->stats.instr_processed +=
334 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700335 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700336 spin_unlock_bh(&iq->lock);
337 }
338 }
339
340 /* Force all pending ordered list requests to time out. */
341 lio_process_ordered_list(oct, 1);
342
343 /* We do not need to wait for output queue packets to be processed. */
344}
345
346/**
347 * \brief Cleanup PCI AER uncorrectable error status
348 * @param dev Pointer to PCI device
349 */
350static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
351{
352 int pos = 0x100;
353 u32 status, mask;
354
355 pr_info("%s :\n", __func__);
356
357 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
358 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
359 if (dev->error_state == pci_channel_io_normal)
360 status &= ~mask; /* Clear corresponding nonfatal bits */
361 else
362 status &= mask; /* Clear corresponding fatal bits */
363 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
364}
365
366/**
367 * \brief Stop all PCI IO to a given device
368 * @param dev Pointer to Octeon device
369 */
370static void stop_pci_io(struct octeon_device *oct)
371{
372 /* No more instructions will be forwarded. */
373 atomic_set(&oct->status, OCT_DEV_IN_RESET);
374
375 pci_disable_device(oct->pci_dev);
376
377 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700378 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700379
380 pcierror_quiesce_device(oct);
381
382 /* Release the interrupt line */
383 free_irq(oct->pci_dev->irq, oct);
384
385 if (oct->flags & LIO_FLAG_MSI_ENABLED)
386 pci_disable_msi(oct->pci_dev);
387
388 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
389 lio_get_state_string(&oct->status));
390
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700391 /* making it a common function for all OCTEON models */
392 cleanup_aer_uncorrect_error_status(oct->pci_dev);
393}
394
395/**
396 * \brief called when PCI error is detected
397 * @param pdev Pointer to PCI device
398 * @param state The current pci connection state
399 *
400 * This function is called after a PCI bus error affecting
401 * this device has been detected.
402 */
403static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
404 pci_channel_state_t state)
405{
406 struct octeon_device *oct = pci_get_drvdata(pdev);
407
408 /* Non-correctable Non-fatal errors */
409 if (state == pci_channel_io_normal) {
410 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
411 cleanup_aer_uncorrect_error_status(oct->pci_dev);
412 return PCI_ERS_RESULT_CAN_RECOVER;
413 }
414
415 /* Non-correctable Fatal errors */
416 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
417 stop_pci_io(oct);
418
419 /* Always return a DISCONNECT. There is no support for recovery but only
420 * for a clean shutdown.
421 */
422 return PCI_ERS_RESULT_DISCONNECT;
423}
424
425/**
426 * \brief mmio handler
427 * @param pdev Pointer to PCI device
428 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700429static pci_ers_result_t liquidio_pcie_mmio_enabled(
430 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700431{
432 /* We should never hit this since we never ask for a reset for a Fatal
433 * Error. We always return DISCONNECT in io_error above.
434 * But play safe and return RECOVERED for now.
435 */
436 return PCI_ERS_RESULT_RECOVERED;
437}
438
439/**
440 * \brief called after the pci bus has been reset.
441 * @param pdev Pointer to PCI device
442 *
443 * Restart the card from scratch, as if from a cold-boot. Implementation
444 * resembles the first-half of the octeon_resume routine.
445 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700446static pci_ers_result_t liquidio_pcie_slot_reset(
447 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700448{
449 /* We should never hit this since we never ask for a reset for a Fatal
450 * Error. We always return DISCONNECT in io_error above.
451 * But play safe and return RECOVERED for now.
452 */
453 return PCI_ERS_RESULT_RECOVERED;
454}
455
456/**
457 * \brief called when traffic can start flowing again.
458 * @param pdev Pointer to PCI device
459 *
460 * This callback is called when the error recovery driver tells us that
461 * its OK to resume normal operation. Implementation resembles the
462 * second-half of the octeon_resume routine.
463 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700464static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700465{
466 /* Nothing to be done here. */
467}
468
469#ifdef CONFIG_PM
470/**
471 * \brief called when suspending
472 * @param pdev Pointer to PCI device
473 * @param state state to suspend to
474 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700475static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
476 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700477{
478 return 0;
479}
480
481/**
482 * \brief called when resuming
483 * @param pdev Pointer to PCI device
484 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700485static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700486{
487 return 0;
488}
489#endif
490
491/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100492static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700493 .error_detected = liquidio_pcie_error_detected,
494 .mmio_enabled = liquidio_pcie_mmio_enabled,
495 .slot_reset = liquidio_pcie_slot_reset,
496 .resume = liquidio_pcie_resume,
497};
498
499static const struct pci_device_id liquidio_pci_tbl[] = {
500 { /* 68xx */
501 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
502 },
503 { /* 66xx */
504 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
505 },
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -0700506 { /* 23xx pf */
507 PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
508 },
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700509 {
510 0, 0, 0, 0, 0, 0, 0
511 }
512};
513MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
514
515static struct pci_driver liquidio_pci_driver = {
516 .name = "LiquidIO",
517 .id_table = liquidio_pci_tbl,
518 .probe = liquidio_probe,
519 .remove = liquidio_remove,
520 .err_handler = &liquidio_err_handler, /* For AER */
521
522#ifdef CONFIG_PM
523 .suspend = liquidio_suspend,
524 .resume = liquidio_resume,
525#endif
Raghu Vatsavayica6139f2016-11-14 15:54:40 -0800526#ifdef CONFIG_PCI_IOV
527 .sriov_configure = liquidio_enable_sriov,
528#endif
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700529};
530
531/**
532 * \brief register PCI driver
533 */
534static int liquidio_init_pci(void)
535{
536 return pci_register_driver(&liquidio_pci_driver);
537}
538
539/**
540 * \brief unregister PCI driver
541 */
542static void liquidio_deinit_pci(void)
543{
544 pci_unregister_driver(&liquidio_pci_driver);
545}
546
547/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700548 * \brief Stop Tx queues
549 * @param netdev network device
550 */
551static inline void txqs_stop(struct net_device *netdev)
552{
553 if (netif_is_multiqueue(netdev)) {
554 int i;
555
556 for (i = 0; i < netdev->num_tx_queues; i++)
557 netif_stop_subqueue(netdev, i);
558 } else {
559 netif_stop_queue(netdev);
560 }
561}
562
563/**
564 * \brief Start Tx queues
565 * @param netdev network device
566 */
567static inline void txqs_start(struct net_device *netdev)
568{
569 if (netif_is_multiqueue(netdev)) {
570 int i;
571
572 for (i = 0; i < netdev->num_tx_queues; i++)
573 netif_start_subqueue(netdev, i);
574 } else {
575 netif_start_queue(netdev);
576 }
577}
578
579/**
580 * \brief Wake Tx queues
581 * @param netdev network device
582 */
583static inline void txqs_wake(struct net_device *netdev)
584{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700585 struct lio *lio = GET_LIO(netdev);
586
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700587 if (netif_is_multiqueue(netdev)) {
588 int i;
589
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700590 for (i = 0; i < netdev->num_tx_queues; i++) {
591 int qno = lio->linfo.txpciq[i %
592 (lio->linfo.num_txpciq)].s.q_no;
593
594 if (__netif_subqueue_stopped(netdev, i)) {
595 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
596 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700597 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700598 }
599 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700600 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700601 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
602 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700603 netif_wake_queue(netdev);
604 }
605}
606
607/**
608 * \brief Stop Tx queue
609 * @param netdev network device
610 */
611static void stop_txq(struct net_device *netdev)
612{
613 txqs_stop(netdev);
614}
615
616/**
617 * \brief Start Tx queue
618 * @param netdev network device
619 */
620static void start_txq(struct net_device *netdev)
621{
622 struct lio *lio = GET_LIO(netdev);
623
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700624 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700625 txqs_start(netdev);
626 return;
627 }
628}
629
630/**
631 * \brief Wake a queue
632 * @param netdev network device
633 * @param q which queue to wake
634 */
635static inline void wake_q(struct net_device *netdev, int q)
636{
637 if (netif_is_multiqueue(netdev))
638 netif_wake_subqueue(netdev, q);
639 else
640 netif_wake_queue(netdev);
641}
642
643/**
644 * \brief Stop a queue
645 * @param netdev network device
646 * @param q which queue to stop
647 */
648static inline void stop_q(struct net_device *netdev, int q)
649{
650 if (netif_is_multiqueue(netdev))
651 netif_stop_subqueue(netdev, q);
652 else
653 netif_stop_queue(netdev);
654}
655
656/**
657 * \brief Check Tx queue status, and take appropriate action
658 * @param lio per-network private data
659 * @returns 0 if full, number of queues woken up otherwise
660 */
661static inline int check_txq_status(struct lio *lio)
662{
663 int ret_val = 0;
664
665 if (netif_is_multiqueue(lio->netdev)) {
666 int numqs = lio->netdev->num_tx_queues;
667 int q, iq = 0;
668
669 /* check each sub-queue state */
670 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700671 iq = lio->linfo.txpciq[q %
672 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700673 if (octnet_iq_is_full(lio->oct_dev, iq))
674 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700675 if (__netif_subqueue_stopped(lio->netdev, q)) {
676 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700677 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
678 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700679 ret_val++;
680 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700681 }
682 } else {
683 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
684 return 0;
685 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700686 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
687 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700688 ret_val = 1;
689 }
690 return ret_val;
691}
692
693/**
694 * Remove the node at the head of the list. The list would be empty at
695 * the end of this call if there are no more nodes in the list.
696 */
697static inline struct list_head *list_delete_head(struct list_head *root)
698{
699 struct list_head *node;
700
701 if ((root->prev == root) && (root->next == root))
702 node = NULL;
703 else
704 node = root->next;
705
706 if (node)
707 list_del(node);
708
709 return node;
710}
711
712/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700713 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700714 * @param lio per-network private data
715 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700716static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700717{
718 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700719 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700720
VSR Burru67e303e2017-03-06 18:45:59 -0800721 kfree(lio->glist_lock);
722 lio->glist_lock = NULL;
723
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700724 if (!lio->glist)
725 return;
726
727 for (i = 0; i < lio->linfo.num_txpciq; i++) {
728 do {
729 g = (struct octnic_gather *)
730 list_delete_head(&lio->glist[i]);
VSR Burru67e303e2017-03-06 18:45:59 -0800731 if (g)
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700732 kfree(g);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700733 } while (g);
VSR Burru67e303e2017-03-06 18:45:59 -0800734
Felix Manlunas58ad3192017-03-20 19:04:48 -0700735 if (lio->glists_virt_base && lio->glists_virt_base[i] &&
736 lio->glists_dma_base && lio->glists_dma_base[i]) {
VSR Burru67e303e2017-03-06 18:45:59 -0800737 lio_dma_free(lio->oct_dev,
738 lio->glist_entry_size * lio->tx_qsize,
739 lio->glists_virt_base[i],
740 lio->glists_dma_base[i]);
741 }
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700742 }
743
VSR Burru67e303e2017-03-06 18:45:59 -0800744 kfree(lio->glists_virt_base);
745 lio->glists_virt_base = NULL;
746
747 kfree(lio->glists_dma_base);
748 lio->glists_dma_base = NULL;
749
750 kfree(lio->glist);
751 lio->glist = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700752}
753
754/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700755 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700756 * @param lio per-network private data
757 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700758static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700759{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700760 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700761 struct octnic_gather *g;
762
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700763 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
764 GFP_KERNEL);
765 if (!lio->glist_lock)
VSR Burru67e303e2017-03-06 18:45:59 -0800766 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700767
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700768 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
769 GFP_KERNEL);
770 if (!lio->glist) {
VSR Burru67e303e2017-03-06 18:45:59 -0800771 kfree(lio->glist_lock);
772 lio->glist_lock = NULL;
773 return -ENOMEM;
774 }
775
776 lio->glist_entry_size =
777 ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
778
779 /* allocate memory to store virtual and dma base address of
780 * per glist consistent memory
781 */
782 lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
783 GFP_KERNEL);
784 lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
785 GFP_KERNEL);
786
787 if (!lio->glists_virt_base || !lio->glists_dma_base) {
788 delete_glists(lio);
789 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700790 }
791
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700792 for (i = 0; i < num_iqs; i++) {
VSR Burrub3ca9af2017-03-09 17:03:24 -0800793 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700794
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700795 spin_lock_init(&lio->glist_lock[i]);
796
797 INIT_LIST_HEAD(&lio->glist[i]);
798
VSR Burru67e303e2017-03-06 18:45:59 -0800799 lio->glists_virt_base[i] =
800 lio_dma_alloc(oct,
801 lio->glist_entry_size * lio->tx_qsize,
802 &lio->glists_dma_base[i]);
803
804 if (!lio->glists_virt_base[i]) {
805 delete_glists(lio);
806 return -ENOMEM;
807 }
808
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700809 for (j = 0; j < lio->tx_qsize; j++) {
810 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
811 numa_node);
812 if (!g)
813 g = kzalloc(sizeof(*g), GFP_KERNEL);
814 if (!g)
815 break;
816
VSR Burru67e303e2017-03-06 18:45:59 -0800817 g->sg = lio->glists_virt_base[i] +
818 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700819
VSR Burru67e303e2017-03-06 18:45:59 -0800820 g->sg_dma_ptr = lio->glists_dma_base[i] +
821 (j * lio->glist_entry_size);
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700822
823 list_add_tail(&g->list, &lio->glist[i]);
824 }
825
826 if (j != lio->tx_qsize) {
827 delete_glists(lio);
VSR Burru67e303e2017-03-06 18:45:59 -0800828 return -ENOMEM;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700829 }
830 }
831
832 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700833}
834
835/**
836 * \brief Print link information
837 * @param netdev network device
838 */
839static void print_link_info(struct net_device *netdev)
840{
841 struct lio *lio = GET_LIO(netdev);
842
843 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
844 struct oct_link_info *linfo = &lio->linfo;
845
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700846 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700847 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
848 linfo->link.s.speed,
849 (linfo->link.s.duplex) ? "Full" : "Half");
850 } else {
851 netif_info(lio, link, lio->netdev, "Link Down\n");
852 }
853 }
854}
855
856/**
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -0700857 * \brief Routine to notify MTU change
858 * @param work work_struct data structure
859 */
860static void octnet_link_status_change(struct work_struct *work)
861{
862 struct cavium_wk *wk = (struct cavium_wk *)work;
863 struct lio *lio = (struct lio *)wk->ctxptr;
864
865 rtnl_lock();
866 call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
867 rtnl_unlock();
868}
869
870/**
871 * \brief Sets up the mtu status change work
872 * @param netdev network device
873 */
874static inline int setup_link_status_change_wq(struct net_device *netdev)
875{
876 struct lio *lio = GET_LIO(netdev);
877 struct octeon_device *oct = lio->oct_dev;
878
879 lio->link_status_wq.wq = alloc_workqueue("link-status",
880 WQ_MEM_RECLAIM, 0);
881 if (!lio->link_status_wq.wq) {
882 dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
883 return -1;
884 }
885 INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
886 octnet_link_status_change);
887 lio->link_status_wq.wk.ctxptr = lio;
888
889 return 0;
890}
891
892static inline void cleanup_link_status_change_wq(struct net_device *netdev)
893{
894 struct lio *lio = GET_LIO(netdev);
895
896 if (lio->link_status_wq.wq) {
897 cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
898 destroy_workqueue(lio->link_status_wq.wq);
899 }
900}
901
902/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700903 * \brief Update link status
904 * @param netdev network device
905 * @param ls link status structure
906 *
907 * Called on receipt of a link status response from the core application to
908 * update each interface's link status.
909 */
910static inline void update_link_status(struct net_device *netdev,
911 union oct_link_status *ls)
912{
913 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700914 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700915
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700916 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700917
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700918 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700919 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700920 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700921
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700922 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700923 netif_carrier_on(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700924 txqs_wake(netdev);
925 } else {
926 netif_carrier_off(netdev);
927 stop_txq(netdev);
928 }
929 }
930}
931
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700932/* Runs in interrupt context. */
933static void update_txq_status(struct octeon_device *oct, int iq_num)
934{
935 struct net_device *netdev;
936 struct lio *lio;
937 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
938
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700939 netdev = oct->props[iq->ifidx].netdev;
940
941 /* This is needed because the first IQ does not have
942 * a netdev associated with it.
943 */
944 if (!netdev)
945 return;
946
947 lio = GET_LIO(netdev);
948 if (netif_is_multiqueue(netdev)) {
949 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
950 lio->linfo.link.s.link_up &&
951 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700952 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
953 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700954 netif_wake_subqueue(netdev, iq->q_index);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700955 }
VSR Burru6069f3f2017-03-22 11:54:50 -0700956 } else if (netif_queue_stopped(netdev) &&
957 lio->linfo.link.s.link_up &&
958 (!octnet_iq_is_full(oct, lio->txq))) {
959 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
960 lio->txq, tx_restart, 1);
961 netif_wake_queue(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700962 }
963}
964
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -0700965static
966int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
967{
968 struct octeon_device *oct = droq->oct_dev;
969 struct octeon_device_priv *oct_priv =
970 (struct octeon_device_priv *)oct->priv;
971
972 if (droq->ops.poll_mode) {
973 droq->ops.napi_fn(droq);
974 } else {
975 if (ret & MSIX_PO_INT) {
976 tasklet_schedule(&oct_priv->droq_tasklet);
977 return 1;
978 }
979 /* this will be flushed periodically by check iq db */
980 if (ret & MSIX_PI_INT)
981 return 0;
982 }
983 return 0;
984}
985
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700986/**
987 * \brief Droq packet processor sceduler
988 * @param oct octeon device
989 */
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700990static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700991{
992 struct octeon_device_priv *oct_priv =
993 (struct octeon_device_priv *)oct->priv;
994 u64 oq_no;
995 struct octeon_droq *droq;
996
997 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700998 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
999 oq_no++) {
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001000 if (!(oct->droq_intr & BIT_ULL(oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001001 continue;
1002
1003 droq = oct->droq[oq_no];
1004
1005 if (droq->ops.poll_mode) {
1006 droq->ops.napi_fn(droq);
1007 oct_priv->napi_mask |= (1 << oq_no);
1008 } else {
1009 tasklet_schedule(&oct_priv->droq_tasklet);
1010 }
1011 }
1012 }
1013}
1014
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001015static irqreturn_t
1016liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
1017{
1018 u64 ret;
1019 struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
1020 struct octeon_device *oct = ioq_vector->oct_dev;
1021 struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
1022
1023 ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
1024
1025 if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
1026 liquidio_schedule_msix_droq_pkt_handler(droq, ret);
1027
1028 return IRQ_HANDLED;
1029}
1030
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001031/**
1032 * \brief Interrupt handler for octeon
1033 * @param irq unused
1034 * @param dev octeon device
1035 */
1036static
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001037irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
1038 void *dev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001039{
1040 struct octeon_device *oct = (struct octeon_device *)dev;
1041 irqreturn_t ret;
1042
1043 /* Disable our interrupts for the duration of ISR */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001044 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001045
1046 ret = oct->fn_list.process_interrupt_regs(oct);
1047
1048 if (ret == IRQ_HANDLED)
1049 liquidio_schedule_droq_pkt_handlers(oct);
1050
1051 /* Re-enable our interrupts */
1052 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001053 oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001054
1055 return ret;
1056}
1057
1058/**
1059 * \brief Setup interrupt for octeon device
1060 * @param oct octeon device
1061 *
1062 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
1063 */
1064static int octeon_setup_interrupt(struct octeon_device *oct)
1065{
1066 int irqret, err;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001067 struct msix_entry *msix_entries;
1068 int i;
1069 int num_ioq_vectors;
1070 int num_alloc_ioq_vectors;
Rick Farrington0c88a762017-03-13 12:58:04 -07001071 char *queue_irq_names = NULL;
1072 char *aux_irq_name = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001073
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001074 if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
1075 oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
1076 /* one non ioq interrupt for handling sli_mac_pf_int_sum */
1077 oct->num_msix_irqs += 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001078
Rick Farrington0c88a762017-03-13 12:58:04 -07001079 /* allocate storage for the names assigned to each irq */
1080 oct->irq_name_storage =
1081 kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
1082 GFP_KERNEL);
1083 if (!oct->irq_name_storage) {
1084 dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
1085 return -ENOMEM;
1086 }
1087
1088 queue_irq_names = oct->irq_name_storage;
1089 aux_irq_name = &queue_irq_names
1090 [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
1091
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001092 oct->msix_entries = kcalloc(
1093 oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
Rick Farrington0c88a762017-03-13 12:58:04 -07001094 if (!oct->msix_entries) {
1095 dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
1096 kfree(oct->irq_name_storage);
1097 oct->irq_name_storage = NULL;
1098 return -ENOMEM;
1099 }
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001100
1101 msix_entries = (struct msix_entry *)oct->msix_entries;
1102 /*Assumption is that pf msix vectors start from pf srn to pf to
1103 * trs and not from 0. if not change this code
1104 */
1105 for (i = 0; i < oct->num_msix_irqs - 1; i++)
1106 msix_entries[i].entry = oct->sriov_info.pf_srn + i;
1107 msix_entries[oct->num_msix_irqs - 1].entry =
1108 oct->sriov_info.trs;
1109 num_alloc_ioq_vectors = pci_enable_msix_range(
1110 oct->pci_dev, msix_entries,
1111 oct->num_msix_irqs,
1112 oct->num_msix_irqs);
1113 if (num_alloc_ioq_vectors < 0) {
1114 dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
1115 kfree(oct->msix_entries);
1116 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001117 kfree(oct->irq_name_storage);
1118 oct->irq_name_storage = NULL;
1119 return num_alloc_ioq_vectors;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001120 }
1121 dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
1122
1123 num_ioq_vectors = oct->num_msix_irqs;
1124
1125 /** For PF, there is one non-ioq interrupt handler */
1126 num_ioq_vectors -= 1;
Rick Farrington0c88a762017-03-13 12:58:04 -07001127
1128 snprintf(aux_irq_name, INTRNAMSIZ,
1129 "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001130 irqret = request_irq(msix_entries[num_ioq_vectors].vector,
Rick Farrington0c88a762017-03-13 12:58:04 -07001131 liquidio_legacy_intr_handler, 0,
1132 aux_irq_name, oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001133 if (irqret) {
1134 dev_err(&oct->pci_dev->dev,
1135 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1136 irqret);
1137 pci_disable_msix(oct->pci_dev);
1138 kfree(oct->msix_entries);
1139 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001140 kfree(oct->irq_name_storage);
1141 oct->irq_name_storage = NULL;
1142 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001143 }
1144
1145 for (i = 0; i < num_ioq_vectors; i++) {
Rick Farrington0c88a762017-03-13 12:58:04 -07001146 snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
1147 "LiquidIO%u-pf%u-rxtx-%u",
1148 oct->octeon_id, oct->pf_num, i);
1149
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001150 irqret = request_irq(msix_entries[i].vector,
1151 liquidio_msix_intr_handler, 0,
Rick Farrington0c88a762017-03-13 12:58:04 -07001152 &queue_irq_names[IRQ_NAME_OFF(i)],
1153 &oct->ioq_vector[i]);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001154 if (irqret) {
1155 dev_err(&oct->pci_dev->dev,
1156 "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
1157 irqret);
1158 /** Freeing the non-ioq irq vector here . */
1159 free_irq(msix_entries[num_ioq_vectors].vector,
1160 oct);
1161
1162 while (i) {
1163 i--;
1164 /** clearing affinity mask. */
1165 irq_set_affinity_hint(
1166 msix_entries[i].vector, NULL);
1167 free_irq(msix_entries[i].vector,
1168 &oct->ioq_vector[i]);
1169 }
1170 pci_disable_msix(oct->pci_dev);
1171 kfree(oct->msix_entries);
1172 oct->msix_entries = NULL;
Rick Farrington0c88a762017-03-13 12:58:04 -07001173 kfree(oct->irq_name_storage);
1174 oct->irq_name_storage = NULL;
1175 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001176 }
1177 oct->ioq_vector[i].vector = msix_entries[i].vector;
1178 /* assign the cpu mask for this msix interrupt vector */
1179 irq_set_affinity_hint(
1180 msix_entries[i].vector,
1181 (&oct->ioq_vector[i].affinity_mask));
1182 }
1183 dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
1184 oct->octeon_id);
1185 } else {
1186 err = pci_enable_msi(oct->pci_dev);
1187 if (err)
1188 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
1189 err);
1190 else
1191 oct->flags |= LIO_FLAG_MSI_ENABLED;
1192
Rick Farrington0c88a762017-03-13 12:58:04 -07001193 /* allocate storage for the names assigned to the irq */
1194 oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
1195 if (!oct->irq_name_storage)
1196 return -ENOMEM;
1197
1198 queue_irq_names = oct->irq_name_storage;
1199
1200 snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
1201 "LiquidIO%u-pf%u-rxtx-%u",
1202 oct->octeon_id, oct->pf_num, 0);
1203
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001204 irqret = request_irq(oct->pci_dev->irq,
Rick Farrington0c88a762017-03-13 12:58:04 -07001205 liquidio_legacy_intr_handler,
1206 IRQF_SHARED,
1207 &queue_irq_names[IRQ_NAME_OFF(0)], oct);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001208 if (irqret) {
1209 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1210 pci_disable_msi(oct->pci_dev);
1211 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
1212 irqret);
Rick Farrington0c88a762017-03-13 12:58:04 -07001213 kfree(oct->irq_name_storage);
1214 oct->irq_name_storage = NULL;
1215 return irqret;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001216 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001217 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001218 return 0;
1219}
1220
Felix Manlunasbb54be52017-04-04 19:26:57 -07001221static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
1222{
1223 struct octeon_device *other_oct;
1224
1225 other_oct = lio_get_device(oct->octeon_id + 1);
1226
1227 if (other_oct && other_oct->pci_dev) {
1228 int oct_busnum, other_oct_busnum;
1229
1230 oct_busnum = oct->pci_dev->bus->number;
1231 other_oct_busnum = other_oct->pci_dev->bus->number;
1232
1233 if (oct_busnum == other_oct_busnum) {
1234 int oct_slot, other_oct_slot;
1235
1236 oct_slot = PCI_SLOT(oct->pci_dev->devfn);
1237 other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
1238
1239 if (oct_slot == other_oct_slot)
1240 return other_oct;
1241 }
1242 }
1243
1244 return NULL;
1245}
1246
1247static void disable_all_vf_links(struct octeon_device *oct)
1248{
1249 struct net_device *netdev;
1250 int max_vfs, vf, i;
1251
1252 if (!oct)
1253 return;
1254
1255 max_vfs = oct->sriov_info.max_vfs;
1256
1257 for (i = 0; i < oct->ifcount; i++) {
1258 netdev = oct->props[i].netdev;
1259 if (!netdev)
1260 continue;
1261
1262 for (vf = 0; vf < max_vfs; vf++)
1263 liquidio_set_vf_link_state(netdev, vf,
1264 IFLA_VF_LINK_STATE_DISABLE);
1265 }
1266}
1267
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001268static int liquidio_watchdog(void *param)
1269{
Felix Manlunasbb54be52017-04-04 19:26:57 -07001270 bool err_msg_was_printed[LIO_MAX_CORES];
1271 u16 mask_of_crashed_or_stuck_cores = 0;
1272 bool all_vf_links_are_disabled = false;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001273 struct octeon_device *oct = param;
Felix Manlunasbb54be52017-04-04 19:26:57 -07001274 struct octeon_device *other_oct;
1275#ifdef CONFIG_MODULE_UNLOAD
1276 long refcount, vfs_referencing_pf;
1277 u64 vfs_mask1, vfs_mask2;
1278#endif
1279 int core;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001280
Felix Manlunasbb54be52017-04-04 19:26:57 -07001281 memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001282
1283 while (!kthread_should_stop()) {
Felix Manlunasbb54be52017-04-04 19:26:57 -07001284 /* sleep for a couple of seconds so that we don't hog the CPU */
1285 set_current_state(TASK_INTERRUPTIBLE);
1286 schedule_timeout(msecs_to_jiffies(2000));
1287
1288 mask_of_crashed_or_stuck_cores =
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001289 (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
1290
Felix Manlunasbb54be52017-04-04 19:26:57 -07001291 if (!mask_of_crashed_or_stuck_cores)
1292 continue;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001293
Felix Manlunasbb54be52017-04-04 19:26:57 -07001294 WRITE_ONCE(oct->cores_crashed, true);
1295 other_oct = get_other_octeon_device(oct);
1296 if (other_oct)
1297 WRITE_ONCE(other_oct->cores_crashed, true);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001298
Felix Manlunasbb54be52017-04-04 19:26:57 -07001299 for (core = 0; core < LIO_MAX_CORES; core++) {
1300 bool core_crashed_or_got_stuck;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001301
Felix Manlunasbb54be52017-04-04 19:26:57 -07001302 core_crashed_or_got_stuck =
1303 (mask_of_crashed_or_stuck_cores
1304 >> core) & 1;
1305
1306 if (core_crashed_or_got_stuck &&
1307 !err_msg_was_printed[core]) {
1308 dev_err(&oct->pci_dev->dev,
1309 "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
1310 core);
1311 err_msg_was_printed[core] = true;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001312 }
1313 }
1314
Felix Manlunasbb54be52017-04-04 19:26:57 -07001315 if (all_vf_links_are_disabled)
1316 continue;
1317
1318 disable_all_vf_links(oct);
1319 disable_all_vf_links(other_oct);
1320 all_vf_links_are_disabled = true;
1321
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001322#ifdef CONFIG_MODULE_UNLOAD
Felix Manlunasbb54be52017-04-04 19:26:57 -07001323 vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1324 vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001325
Felix Manlunasbb54be52017-04-04 19:26:57 -07001326 vfs_referencing_pf = hweight64(vfs_mask1);
1327 vfs_referencing_pf += hweight64(vfs_mask2);
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001328
Felix Manlunasbb54be52017-04-04 19:26:57 -07001329 refcount = module_refcount(THIS_MODULE);
1330 if (refcount >= vfs_referencing_pf) {
1331 while (vfs_referencing_pf) {
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001332 module_put(THIS_MODULE);
Felix Manlunasbb54be52017-04-04 19:26:57 -07001333 vfs_referencing_pf--;
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001334 }
1335 }
1336#endif
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001337 }
1338
1339 return 0;
1340}
1341
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001342/**
1343 * \brief PCI probe handler
1344 * @param pdev PCI device structure
1345 * @param ent unused
1346 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001347static int
1348liquidio_probe(struct pci_dev *pdev,
1349 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001350{
1351 struct octeon_device *oct_dev = NULL;
1352 struct handshake *hs;
1353
1354 oct_dev = octeon_allocate_device(pdev->device,
1355 sizeof(struct octeon_device_priv));
1356 if (!oct_dev) {
1357 dev_err(&pdev->dev, "Unable to allocate device\n");
1358 return -ENOMEM;
1359 }
1360
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001361 if (pdev->device == OCTEON_CN23XX_PF_VID)
1362 oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1363
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07001364 /* Enable PTP for 6XXX Device */
1365 if (((pdev->device == OCTEON_CN66XX) ||
1366 (pdev->device == OCTEON_CN68XX)))
1367 oct_dev->ptp_enable = true;
1368 else
1369 oct_dev->ptp_enable = false;
1370
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001371 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1372 (u32)pdev->vendor, (u32)pdev->device);
1373
1374 /* Assign octeon_device for this device to the private data area. */
1375 pci_set_drvdata(pdev, oct_dev);
1376
1377 /* set linux specific device pointer */
1378 oct_dev->pci_dev = (void *)pdev;
1379
1380 hs = &handshake[oct_dev->octeon_id];
1381 init_completion(&hs->init);
1382 init_completion(&hs->started);
1383 hs->pci_dev = pdev;
1384
1385 if (oct_dev->octeon_id == 0)
1386 /* first LiquidIO NIC is detected */
1387 complete(&first_stage);
1388
1389 if (octeon_device_init(oct_dev)) {
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001390 complete(&hs->init);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001391 liquidio_remove(pdev);
1392 return -ENOMEM;
1393 }
1394
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001395 if (OCTEON_CN23XX_PF(oct_dev)) {
1396 u64 scratch1;
1397 u8 bus, device, function;
1398
1399 scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
1400 if (!(scratch1 & 4ULL)) {
1401 /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
1402 * the lio watchdog kernel thread is running for this
1403 * NIC. Each NIC gets one watchdog kernel thread.
1404 */
1405 scratch1 |= 4ULL;
1406 octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
1407 scratch1);
1408
1409 bus = pdev->bus->number;
1410 device = PCI_SLOT(pdev->devfn);
1411 function = PCI_FUNC(pdev->devfn);
1412 oct_dev->watchdog_task = kthread_create(
1413 liquidio_watchdog, oct_dev,
1414 "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001415 if (!IS_ERR(oct_dev->watchdog_task)) {
1416 wake_up_process(oct_dev->watchdog_task);
1417 } else {
1418 oct_dev->watchdog_task = NULL;
1419 dev_err(&oct_dev->pci_dev->dev,
1420 "failed to create kernel_thread\n");
1421 liquidio_remove(pdev);
1422 return -1;
1423 }
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001424 }
1425 }
1426
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001427 oct_dev->rx_pause = 1;
1428 oct_dev->tx_pause = 1;
1429
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001430 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1431
1432 return 0;
1433}
1434
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001435static bool fw_type_is_none(void)
1436{
1437 return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1438 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
1439}
1440
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001441/**
1442 *\brief Destroy resources associated with octeon device
1443 * @param pdev PCI device structure
1444 * @param ent unused
1445 */
1446static void octeon_destroy_resources(struct octeon_device *oct)
1447{
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001448 int i, refcount;
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001449 struct msix_entry *msix_entries;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001450 struct octeon_device_priv *oct_priv =
1451 (struct octeon_device_priv *)oct->priv;
1452
1453 struct handshake *hs;
1454
1455 switch (atomic_read(&oct->status)) {
1456 case OCT_DEV_RUNNING:
1457 case OCT_DEV_CORE_OK:
1458
1459 /* No more instructions will be forwarded. */
1460 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1461
1462 oct->app_mode = CVM_DRV_INVALID_APP;
1463 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1464 lio_get_state_string(&oct->status));
1465
1466 schedule_timeout_uninterruptible(HZ / 10);
1467
1468 /* fallthrough */
1469 case OCT_DEV_HOST_OK:
1470
1471 /* fallthrough */
1472 case OCT_DEV_CONSOLE_INIT_DONE:
1473 /* Remove any consoles */
1474 octeon_remove_consoles(oct);
1475
1476 /* fallthrough */
1477 case OCT_DEV_IO_QUEUES_DONE:
1478 if (wait_for_pending_requests(oct))
1479 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1480
1481 if (lio_wait_for_instr_fetch(oct))
1482 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1483
1484 /* Disable the input and output queues now. No more packets will
1485 * arrive from Octeon, but we should wait for all packet
1486 * processing to finish.
1487 */
1488 oct->fn_list.disable_io_queues(oct);
1489
1490 if (lio_wait_for_oq_pkts(oct))
1491 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1492
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001493 /* fallthrough */
1494 case OCT_DEV_INTR_SET_DONE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001495 /* Disable interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001496 oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001497
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001498 if (oct->msix_on) {
1499 msix_entries = (struct msix_entry *)oct->msix_entries;
1500 for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1501 /* clear the affinity_cpumask */
1502 irq_set_affinity_hint(msix_entries[i].vector,
1503 NULL);
1504 free_irq(msix_entries[i].vector,
1505 &oct->ioq_vector[i]);
1506 }
1507 /* non-iov vector's argument is oct struct */
1508 free_irq(msix_entries[i].vector, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001509
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001510 pci_disable_msix(oct->pci_dev);
1511 kfree(oct->msix_entries);
1512 oct->msix_entries = NULL;
1513 } else {
1514 /* Release the interrupt line */
1515 free_irq(oct->pci_dev->irq, oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001516
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001517 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1518 pci_disable_msi(oct->pci_dev);
1519 }
1520
Rick Farrington0c88a762017-03-13 12:58:04 -07001521 kfree(oct->irq_name_storage);
1522 oct->irq_name_storage = NULL;
1523
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001524 /* fallthrough */
1525 case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001526 if (OCTEON_CN23XX_PF(oct))
1527 octeon_free_ioq_vector(oct);
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08001528
1529 /* fallthrough */
1530 case OCT_DEV_MBOX_SETUP_DONE:
1531 if (OCTEON_CN23XX_PF(oct))
1532 oct->fn_list.free_mbox(oct);
1533
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001534 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001535 case OCT_DEV_IN_RESET:
1536 case OCT_DEV_DROQ_INIT_DONE:
Raghu Vatsavayi763185a2016-11-14 15:54:45 -08001537 /* Wait for any pending operations */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001538 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001539 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07001540 if (!(oct->io_qmask.oq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001541 continue;
1542 octeon_delete_droq(oct, i);
1543 }
1544
1545 /* Force any pending handshakes to complete */
1546 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1547 hs = &handshake[i];
1548
1549 if (hs->pci_dev) {
1550 handshake[oct->octeon_id].init_ok = 0;
1551 complete(&handshake[oct->octeon_id].init);
1552 handshake[oct->octeon_id].started_ok = 0;
1553 complete(&handshake[oct->octeon_id].started);
1554 }
1555 }
1556
1557 /* fallthrough */
1558 case OCT_DEV_RESP_LIST_INIT_DONE:
1559 octeon_delete_response_list(oct);
1560
1561 /* fallthrough */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001562 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001563 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001564 if (!(oct->io_qmask.iq & BIT_ULL(i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001565 continue;
1566 octeon_delete_instr_queue(oct, i);
1567 }
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08001568#ifdef CONFIG_PCI_IOV
1569 if (oct->sriov_info.sriov_enabled)
1570 pci_disable_sriov(oct->pci_dev);
1571#endif
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07001572 /* fallthrough */
1573 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1574 octeon_free_sc_buffer_pool(oct);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001575
1576 /* fallthrough */
1577 case OCT_DEV_DISPATCH_INIT_DONE:
1578 octeon_delete_dispatch_list(oct);
1579 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1580
1581 /* fallthrough */
1582 case OCT_DEV_PCI_MAP_DONE:
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001583 refcount = octeon_deregister_device(oct);
1584
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001585 if (!fw_type_is_none()) {
Rick Farringtone1e3ce62017-05-16 11:14:50 -07001586 /* Soft reset the octeon device before exiting.
1587 * Implementation note: here, we reset the device
1588 * if it is a CN6XXX OR the last CN23XX device.
1589 */
1590 if (OCTEON_CN6XXX(oct) || !refcount)
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001591 oct->fn_list.soft_reset(oct);
1592 }
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001593
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001594 octeon_unmap_pci_barx(oct, 0);
1595 octeon_unmap_pci_barx(oct, 1);
1596
1597 /* fallthrough */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001598 case OCT_DEV_PCI_ENABLE_DONE:
1599 pci_clear_master(oct->pci_dev);
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001600 /* Disable the device, releasing the PCI INT */
1601 pci_disable_device(oct->pci_dev);
1602
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001603 /* fallthrough */
1604 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001605 /* Nothing to be done here either */
1606 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001607 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001608
1609 tasklet_kill(&oct_priv->droq_tasklet);
1610}
1611
1612/**
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001613 * \brief Callback for rx ctrl
1614 * @param status status of request
1615 * @param buf pointer to resp structure
1616 */
1617static void rx_ctl_callback(struct octeon_device *oct,
1618 u32 status,
1619 void *buf)
1620{
1621 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1622 struct liquidio_rx_ctl_context *ctx;
1623
1624 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1625
1626 oct = lio_get_device(ctx->octeon_id);
1627 if (status)
1628 dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1629 CVM_CAST64(status));
1630 WRITE_ONCE(ctx->cond, 1);
1631
1632 /* This barrier is required to be sure that the response has been
1633 * written fully before waking up the handler
1634 */
1635 wmb();
1636
1637 wake_up_interruptible(&ctx->wc);
1638}
1639
1640/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001641 * \brief Send Rx control command
1642 * @param lio per-network private data
1643 * @param start_stop whether to start or stop
1644 */
1645static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1646{
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001647 struct octeon_soft_command *sc;
1648 struct liquidio_rx_ctl_context *ctx;
1649 union octnet_cmd *ncmd;
1650 int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1651 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1652 int retval;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001653
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001654 if (oct->props[lio->ifidx].rx_on == start_stop)
1655 return;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001656
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001657 sc = (struct octeon_soft_command *)
1658 octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1659 16, ctx_size);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001660
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001661 ncmd = (union octnet_cmd *)sc->virtdptr;
1662 ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1663
1664 WRITE_ONCE(ctx->cond, 0);
1665 ctx->octeon_id = lio_get_device_id(oct);
1666 init_waitqueue_head(&ctx->wc);
1667
1668 ncmd->u64 = 0;
1669 ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1670 ncmd->s.param1 = start_stop;
1671
1672 octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1673
1674 sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1675
1676 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1677 OPCODE_NIC_CMD, 0, 0, 0);
1678
1679 sc->callback = rx_ctl_callback;
1680 sc->callback_arg = sc;
1681 sc->wait_time = 5000;
1682
1683 retval = octeon_send_soft_command(oct, sc);
1684 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001685 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001686 } else {
1687 /* Sleep on a wait queue till the cond flag indicates that the
1688 * response arrived or timed-out.
1689 */
1690 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1691 return;
1692 oct->props[lio->ifidx].rx_on = start_stop;
1693 }
1694
1695 octeon_free_soft_command(oct, sc);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001696}
1697
1698/**
1699 * \brief Destroy NIC device interface
1700 * @param oct octeon device
1701 * @param ifidx which interface to destroy
1702 *
1703 * Cleanup associated with each interface for an Octeon device when NIC
1704 * module is being unloaded or if initialization fails during load.
1705 */
1706static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1707{
1708 struct net_device *netdev = oct->props[ifidx].netdev;
1709 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001710 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001711
1712 if (!netdev) {
1713 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1714 __func__, ifidx);
1715 return;
1716 }
1717
1718 lio = GET_LIO(netdev);
1719
1720 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1721
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001722 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07001723 liquidio_stop(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001724
Felix Manlunas7cc61db2017-03-23 13:26:28 -07001725 if (fw_type_is_none()) {
1726 struct octnic_ctrl_pkt nctrl;
1727
1728 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1729 nctrl.ncmd.s.cmd = OCTNET_CMD_RESET_PF;
1730 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1731 octnet_send_nic_ctrl_pkt(oct, &nctrl);
1732 }
1733
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001734 if (oct->props[lio->ifidx].napi_enabled == 1) {
1735 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1736 napi_disable(napi);
1737
1738 oct->props[lio->ifidx].napi_enabled = 0;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001739
1740 if (OCTEON_CN23XX_PF(oct))
1741 oct->droq[0]->ops.poll_mode = 0;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001742 }
1743
Intiyaz Basha42013e92017-08-08 19:34:28 -07001744 /* Delete NAPI */
1745 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1746 netif_napi_del(napi);
1747
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001748 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1749 unregister_netdev(netdev);
1750
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07001751 cleanup_link_status_change_wq(netdev);
1752
Satanand Burla031d4f12017-03-22 11:31:13 -07001753 cleanup_rx_oom_poll_fn(netdev);
1754
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001755 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001756
1757 free_netdev(netdev);
1758
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001759 oct->props[ifidx].gmxport = -1;
1760
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001761 oct->props[ifidx].netdev = NULL;
1762}
1763
1764/**
1765 * \brief Stop complete NIC functionality
1766 * @param oct octeon device
1767 */
1768static int liquidio_stop_nic_module(struct octeon_device *oct)
1769{
1770 int i, j;
1771 struct lio *lio;
1772
1773 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1774 if (!oct->ifcount) {
1775 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1776 return 1;
1777 }
1778
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001779 spin_lock_bh(&oct->cmd_resp_wqlock);
1780 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1781 spin_unlock_bh(&oct->cmd_resp_wqlock);
1782
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001783 for (i = 0; i < oct->ifcount; i++) {
1784 lio = GET_LIO(oct->props[i].netdev);
1785 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001786 octeon_unregister_droq_ops(oct,
1787 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001788 }
1789
1790 for (i = 0; i < oct->ifcount; i++)
1791 liquidio_destroy_nic_device(oct, i);
1792
1793 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1794 return 0;
1795}
1796
1797/**
1798 * \brief Cleans up resources at unload time
1799 * @param pdev PCI device structure
1800 */
1801static void liquidio_remove(struct pci_dev *pdev)
1802{
1803 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1804
1805 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1806
Raghu Vatsavayi9ff1a9b2016-09-01 11:16:09 -07001807 if (oct_dev->watchdog_task)
1808 kthread_stop(oct_dev->watchdog_task);
1809
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001810 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1811 liquidio_stop_nic_module(oct_dev);
1812
1813 /* Reset the octeon device and cleanup all memory allocated for
1814 * the octeon device by driver.
1815 */
1816 octeon_destroy_resources(oct_dev);
1817
1818 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1819
1820 /* This octeon device has been removed. Update the global
1821 * data structure to reflect this. Free the device structure.
1822 */
1823 octeon_free_device_mem(oct_dev);
1824}
1825
1826/**
1827 * \brief Identify the Octeon device and to map the BAR address space
1828 * @param oct octeon device
1829 */
1830static int octeon_chip_specific_setup(struct octeon_device *oct)
1831{
1832 u32 dev_id, rev_id;
1833 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001834 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001835
1836 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1837 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1838 oct->rev_id = rev_id & 0xff;
1839
1840 switch (dev_id) {
1841 case OCTEON_CN68XX_PCIID:
1842 oct->chip_id = OCTEON_CN68XX;
1843 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001844 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001845 break;
1846
1847 case OCTEON_CN66XX_PCIID:
1848 oct->chip_id = OCTEON_CN66XX;
1849 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001850 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001851 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001852
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001853 case OCTEON_CN23XX_PCIID_PF:
1854 oct->chip_id = OCTEON_CN23XX_PF_VID;
1855 ret = setup_cn23xx_octeon_pf_device(oct);
Derek Chicklescf19a8c2017-08-01 15:05:07 -07001856#ifdef CONFIG_PCI_IOV
1857 if (!ret)
1858 pci_sriov_set_totalvfs(oct->pci_dev,
1859 oct->sriov_info.max_vfs);
1860#endif
Raghu Vatsavayi72c00912016-08-31 11:03:25 -07001861 s = "CN23XX";
1862 break;
1863
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001864 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001865 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001866 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1867 dev_id);
1868 }
1869
1870 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001871 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001872 OCTEON_MAJOR_REV(oct),
1873 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001874 octeon_get_conf(oct)->card_name,
1875 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001876
1877 return ret;
1878}
1879
1880/**
1881 * \brief PCI initialization for each Octeon device.
1882 * @param oct octeon device
1883 */
1884static int octeon_pci_os_setup(struct octeon_device *oct)
1885{
1886 /* setup PCI stuff first */
1887 if (pci_enable_device(oct->pci_dev)) {
1888 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1889 return 1;
1890 }
1891
1892 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1893 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001894 pci_disable_device(oct->pci_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001895 return 1;
1896 }
1897
1898 /* Enable PCI DMA Master. */
1899 pci_set_master(oct->pci_dev);
1900
1901 return 0;
1902}
1903
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001904static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1905{
1906 int q = 0;
1907
1908 if (netif_is_multiqueue(lio->netdev))
1909 q = skb->queue_mapping % lio->linfo.num_txpciq;
1910
1911 return q;
1912}
1913
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001914/**
1915 * \brief Check Tx queue state for a given network buffer
1916 * @param lio per-network private data
1917 * @param skb network buffer
1918 */
1919static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1920{
1921 int q = 0, iq = 0;
1922
1923 if (netif_is_multiqueue(lio->netdev)) {
1924 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001925 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001926 } else {
1927 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001928 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001929 }
1930
1931 if (octnet_iq_is_full(lio->oct_dev, iq))
1932 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001933
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001934 if (__netif_subqueue_stopped(lio->netdev, q)) {
1935 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001936 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001937 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001938 return 1;
1939}
1940
1941/**
1942 * \brief Unmap and free network buffer
1943 * @param buf buffer
1944 */
1945static void free_netbuf(void *buf)
1946{
1947 struct sk_buff *skb;
1948 struct octnet_buf_free_info *finfo;
1949 struct lio *lio;
1950
1951 finfo = (struct octnet_buf_free_info *)buf;
1952 skb = finfo->skb;
1953 lio = finfo->lio;
1954
1955 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1956 DMA_TO_DEVICE);
1957
1958 check_txq_state(lio, skb);
1959
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001960 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001961}
1962
1963/**
1964 * \brief Unmap and free gather buffer
1965 * @param buf buffer
1966 */
1967static void free_netsgbuf(void *buf)
1968{
1969 struct octnet_buf_free_info *finfo;
1970 struct sk_buff *skb;
1971 struct lio *lio;
1972 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001973 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001974
1975 finfo = (struct octnet_buf_free_info *)buf;
1976 skb = finfo->skb;
1977 lio = finfo->lio;
1978 g = finfo->g;
1979 frags = skb_shinfo(skb)->nr_frags;
1980
1981 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1982 g->sg[0].ptr[0], (skb->len - skb->data_len),
1983 DMA_TO_DEVICE);
1984
1985 i = 1;
1986 while (frags--) {
1987 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1988
1989 pci_unmap_page((lio->oct_dev)->pci_dev,
1990 g->sg[(i >> 2)].ptr[(i & 3)],
1991 frag->size, DMA_TO_DEVICE);
1992 i++;
1993 }
1994
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001995 iq = skb_iq(lio, skb);
1996 spin_lock(&lio->glist_lock[iq]);
1997 list_add_tail(&g->list, &lio->glist[iq]);
1998 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001999
2000 check_txq_state(lio, skb); /* mq support: sub-queue state check */
2001
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002002 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002003}
2004
2005/**
2006 * \brief Unmap and free gather buffer with response
2007 * @param buf buffer
2008 */
2009static void free_netsgbuf_with_resp(void *buf)
2010{
2011 struct octeon_soft_command *sc;
2012 struct octnet_buf_free_info *finfo;
2013 struct sk_buff *skb;
2014 struct lio *lio;
2015 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002016 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002017
2018 sc = (struct octeon_soft_command *)buf;
2019 skb = (struct sk_buff *)sc->callback_arg;
2020 finfo = (struct octnet_buf_free_info *)&skb->cb;
2021
2022 lio = finfo->lio;
2023 g = finfo->g;
2024 frags = skb_shinfo(skb)->nr_frags;
2025
2026 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
2027 g->sg[0].ptr[0], (skb->len - skb->data_len),
2028 DMA_TO_DEVICE);
2029
2030 i = 1;
2031 while (frags--) {
2032 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
2033
2034 pci_unmap_page((lio->oct_dev)->pci_dev,
2035 g->sg[(i >> 2)].ptr[(i & 3)],
2036 frag->size, DMA_TO_DEVICE);
2037 i++;
2038 }
2039
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002040 iq = skb_iq(lio, skb);
2041
2042 spin_lock(&lio->glist_lock[iq]);
2043 list_add_tail(&g->list, &lio->glist[iq]);
2044 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002045
2046 /* Don't free the skb yet */
2047
2048 check_txq_state(lio, skb);
2049}
2050
2051/**
2052 * \brief Adjust ptp frequency
2053 * @param ptp PTP clock info
2054 * @param ppb how much to adjust by, in parts-per-billion
2055 */
2056static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
2057{
2058 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2059 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2060 u64 comp, delta;
2061 unsigned long flags;
2062 bool neg_adj = false;
2063
2064 if (ppb < 0) {
2065 neg_adj = true;
2066 ppb = -ppb;
2067 }
2068
2069 /* The hardware adds the clock compensation value to the
2070 * PTP clock on every coprocessor clock cycle, so we
2071 * compute the delta in terms of coprocessor clocks.
2072 */
2073 delta = (u64)ppb << 32;
2074 do_div(delta, oct->coproc_clock_rate);
2075
2076 spin_lock_irqsave(&lio->ptp_lock, flags);
2077 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
2078 if (neg_adj)
2079 comp -= delta;
2080 else
2081 comp += delta;
2082 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2083 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2084
2085 return 0;
2086}
2087
2088/**
2089 * \brief Adjust ptp time
2090 * @param ptp PTP clock info
2091 * @param delta how much to adjust by, in nanosecs
2092 */
2093static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2094{
2095 unsigned long flags;
2096 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2097
2098 spin_lock_irqsave(&lio->ptp_lock, flags);
2099 lio->ptp_adjust += delta;
2100 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2101
2102 return 0;
2103}
2104
2105/**
2106 * \brief Get hardware clock time, including any adjustment
2107 * @param ptp PTP clock info
2108 * @param ts timespec
2109 */
2110static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
2111 struct timespec64 *ts)
2112{
2113 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002114 unsigned long flags;
2115 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2116 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2117
2118 spin_lock_irqsave(&lio->ptp_lock, flags);
2119 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
2120 ns += lio->ptp_adjust;
2121 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2122
Kefeng Wang286af312016-01-27 17:34:37 +08002123 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002124
2125 return 0;
2126}
2127
2128/**
2129 * \brief Set hardware clock time. Reset adjustment
2130 * @param ptp PTP clock info
2131 * @param ts timespec
2132 */
2133static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
2134 const struct timespec64 *ts)
2135{
2136 u64 ns;
2137 unsigned long flags;
2138 struct lio *lio = container_of(ptp, struct lio, ptp_info);
2139 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2140
2141 ns = timespec_to_ns(ts);
2142
2143 spin_lock_irqsave(&lio->ptp_lock, flags);
2144 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
2145 lio->ptp_adjust = 0;
2146 spin_unlock_irqrestore(&lio->ptp_lock, flags);
2147
2148 return 0;
2149}
2150
2151/**
2152 * \brief Check if PTP is enabled
2153 * @param ptp PTP clock info
2154 * @param rq request
2155 * @param on is it on
2156 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002157static int
2158liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
2159 struct ptp_clock_request *rq __attribute__((unused)),
2160 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002161{
2162 return -EOPNOTSUPP;
2163}
2164
2165/**
2166 * \brief Open PTP clock source
2167 * @param netdev network device
2168 */
2169static void oct_ptp_open(struct net_device *netdev)
2170{
2171 struct lio *lio = GET_LIO(netdev);
2172 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
2173
2174 spin_lock_init(&lio->ptp_lock);
2175
2176 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
2177 lio->ptp_info.owner = THIS_MODULE;
2178 lio->ptp_info.max_adj = 250000000;
2179 lio->ptp_info.n_alarm = 0;
2180 lio->ptp_info.n_ext_ts = 0;
2181 lio->ptp_info.n_per_out = 0;
2182 lio->ptp_info.pps = 0;
2183 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
2184 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
2185 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
2186 lio->ptp_info.settime64 = liquidio_ptp_settime;
2187 lio->ptp_info.enable = liquidio_ptp_enable;
2188
2189 lio->ptp_adjust = 0;
2190
2191 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
2192 &oct->pci_dev->dev);
2193
2194 if (IS_ERR(lio->ptp_clock))
2195 lio->ptp_clock = NULL;
2196}
2197
2198/**
2199 * \brief Init PTP clock
2200 * @param oct octeon device
2201 */
2202static void liquidio_ptp_init(struct octeon_device *oct)
2203{
2204 u64 clock_comp, cfg;
2205
2206 clock_comp = (u64)NSEC_PER_SEC << 32;
2207 do_div(clock_comp, oct->coproc_clock_rate);
2208 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
2209
2210 /* Enable */
2211 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
2212 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
2213}
2214
2215/**
2216 * \brief Load firmware to device
2217 * @param oct octeon device
2218 *
2219 * Maps device to firmware filename, requests firmware, and downloads it
2220 */
2221static int load_firmware(struct octeon_device *oct)
2222{
2223 int ret = 0;
2224 const struct firmware *fw;
2225 char fw_name[LIO_MAX_FW_FILENAME_LEN];
2226 char *tmp_fw_type;
2227
Felix Manlunas7cc61db2017-03-23 13:26:28 -07002228 if (fw_type_is_none()) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002229 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
2230 return ret;
2231 }
2232
2233 if (fw_type[0] == '\0')
2234 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
2235 else
2236 tmp_fw_type = fw_type;
2237
2238 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
2239 octeon_get_conf(oct)->card_name, tmp_fw_type,
2240 LIO_FW_NAME_SUFFIX);
2241
2242 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
2243 if (ret) {
2244 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
2245 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002246 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002247 return ret;
2248 }
2249
2250 ret = octeon_download_firmware(oct, fw->data, fw->size);
2251
2252 release_firmware(fw);
2253
2254 return ret;
2255}
2256
2257/**
2258 * \brief Setup output queue
2259 * @param oct octeon device
2260 * @param q_no which queue
2261 * @param num_descs how many descriptors
2262 * @param desc_size size of each descriptor
2263 * @param app_ctx application context
2264 */
2265static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
2266 int desc_size, void *app_ctx)
2267{
2268 int ret_val = 0;
2269
2270 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
2271 /* droq creation and local register settings. */
2272 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05302273 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002274 return ret_val;
2275
2276 if (ret_val == 1) {
2277 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
2278 return 0;
2279 }
2280 /* tasklet creation for the droq */
2281
2282 /* Enable the droq queues */
2283 octeon_set_droq_pkt_op(oct, q_no, 1);
2284
2285 /* Send Credit for Octeon Output queues. Credits are always
2286 * sent after the output queue is enabled.
2287 */
2288 writel(oct->droq[q_no]->max_count,
2289 oct->droq[q_no]->pkts_credit_reg);
2290
2291 return ret_val;
2292}
2293
2294/**
2295 * \brief Callback for getting interface configuration
2296 * @param status status of request
2297 * @param buf pointer to resp structure
2298 */
2299static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002300 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002301 void *buf)
2302{
2303 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
2304 struct liquidio_if_cfg_resp *resp;
2305 struct liquidio_if_cfg_context *ctx;
2306
2307 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
Raghu Vatsavayi30136392016-09-01 11:16:11 -07002308 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002309
2310 oct = lio_get_device(ctx->octeon_id);
2311 if (resp->status)
Rick Farringtonc5b71e62017-03-17 11:23:08 -07002312 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
2313 CVM_CAST64(resp->status), status);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002314 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002315
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07002316 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
2317 resp->cfg_info.liquidio_firmware_version);
2318
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002319 /* This barrier is required to be sure that the response has been
2320 * written fully before waking up the handler
2321 */
2322 wmb();
2323
2324 wake_up_interruptible(&ctx->wc);
2325}
2326
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002327/** Routine to push packets arriving on Octeon interface upto network layer.
2328 * @param oct_id - octeon device id.
2329 * @param skbuff - skbuff struct to be passed to network layer.
2330 * @param len - size of total data received.
2331 * @param rh - Control header associated with the packet
2332 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002333 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002334 */
2335static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002336liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002337 void *skbuff,
2338 u32 len,
2339 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002340 void *param,
2341 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002342{
2343 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002344 struct sk_buff *skb = (struct sk_buff *)skbuff;
2345 struct skb_shared_hwtstamps *shhwtstamps;
2346 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002347 u16 vtag = 0;
Prasad Kannegantide28c992017-01-09 14:42:40 -08002348 u32 r_dh_off;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002349 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002350 struct octeon_droq *droq = container_of(param, struct octeon_droq,
2351 napi);
2352 if (netdev) {
2353 int packet_was_received;
2354 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002355 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002356
2357 /* Do not proceed if the interface is not in RUNNING state. */
2358 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
2359 recv_buffer_free(skb);
2360 droq->stats.rx_dropped++;
2361 return;
2362 }
2363
2364 skb->dev = netdev;
2365
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002366 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002367 if (likely(len > MIN_SKB_SIZE)) {
2368 struct octeon_skb_page_info *pg_info;
2369 unsigned char *va;
2370
2371 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
2372 if (pg_info->page) {
2373 /* For Paged allocation use the frags */
2374 va = page_address(pg_info->page) +
2375 pg_info->page_offset;
2376 memcpy(skb->data, va, MIN_SKB_SIZE);
2377 skb_put(skb, MIN_SKB_SIZE);
2378 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2379 pg_info->page,
2380 pg_info->page_offset +
2381 MIN_SKB_SIZE,
2382 len - MIN_SKB_SIZE,
2383 LIO_RXBUFFER_SZ);
2384 }
2385 } else {
2386 struct octeon_skb_page_info *pg_info =
2387 ((struct octeon_skb_page_info *)(skb->cb));
2388 skb_copy_to_linear_data(skb, page_address(pg_info->page)
2389 + pg_info->page_offset, len);
2390 skb_put(skb, len);
2391 put_page(pg_info->page);
2392 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002393
Prasad Kannegantide28c992017-01-09 14:42:40 -08002394 r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
2395
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002396 if (oct->ptp_enable) {
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002397 if (rh->r_dh.has_hwtstamp) {
2398 /* timestamp is included from the hardware at
2399 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002400 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002401 if (ifstate_check
2402 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
2403 /* Nanoseconds are in the first 64-bits
2404 * of the packet.
2405 */
Prasad Kannegantide28c992017-01-09 14:42:40 -08002406 memcpy(&ns, (skb->data + r_dh_off),
2407 sizeof(ns));
2408 r_dh_off -= BYTES_PER_DHLEN_UNIT;
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07002409 shhwtstamps = skb_hwtstamps(skb);
2410 shhwtstamps->hwtstamp =
2411 ns_to_ktime(ns +
2412 lio->ptp_adjust);
2413 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002414 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002415 }
2416
Prasad Kannegantide28c992017-01-09 14:42:40 -08002417 if (rh->r_dh.has_hash) {
2418 __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
2419 u32 hash = be32_to_cpu(*hash_be);
2420
2421 skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
2422 r_dh_off -= BYTES_PER_DHLEN_UNIT;
2423 }
2424
2425 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
2426
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002427 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002428 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002429 (((rh->r_dh.encap_on) &&
2430 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
2431 (!(rh->r_dh.encap_on) &&
2432 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002433 /* checksum has already been verified */
2434 skb->ip_summed = CHECKSUM_UNNECESSARY;
2435 else
2436 skb->ip_summed = CHECKSUM_NONE;
2437
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002438 /* Setting Encapsulation field on basis of status received
2439 * from the firmware
2440 */
2441 if (rh->r_dh.encap_on) {
2442 skb->encapsulation = 1;
2443 skb->csum_level = 1;
2444 droq->stats.rx_vxlan++;
2445 }
2446
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002447 /* inbound VLAN tag */
2448 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2449 (rh->r_dh.vlan != 0)) {
2450 u16 vid = rh->r_dh.vlan;
2451 u16 priority = rh->r_dh.priority;
2452
2453 vtag = priority << 13 | vid;
2454 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
2455 }
2456
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002457 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
2458
2459 if (packet_was_received) {
2460 droq->stats.rx_bytes_received += len;
2461 droq->stats.rx_pkts_received++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002462 } else {
2463 droq->stats.rx_dropped++;
2464 netif_info(lio, rx_err, lio->netdev,
2465 "droq:%d error rx_dropped:%llu\n",
2466 droq->q_no, droq->stats.rx_dropped);
2467 }
2468
2469 } else {
2470 recv_buffer_free(skb);
2471 }
2472}
2473
2474/**
2475 * \brief wrapper for calling napi_schedule
2476 * @param param parameters to pass to napi_schedule
2477 *
2478 * Used when scheduling on different CPUs
2479 */
2480static void napi_schedule_wrapper(void *param)
2481{
2482 struct napi_struct *napi = param;
2483
2484 napi_schedule(napi);
2485}
2486
2487/**
2488 * \brief callback when receive interrupt occurs and we are in NAPI mode
2489 * @param arg pointer to octeon output queue
2490 */
2491static void liquidio_napi_drv_callback(void *arg)
2492{
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002493 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002494 struct octeon_droq *droq = arg;
2495 int this_cpu = smp_processor_id();
2496
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -07002497 oct = droq->oct_dev;
2498
2499 if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
2500 napi_schedule_irqoff(&droq->napi);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002501 } else {
2502 struct call_single_data *csd = &droq->csd;
2503
2504 csd->func = napi_schedule_wrapper;
2505 csd->info = &droq->napi;
2506 csd->flags = 0;
2507
2508 smp_call_function_single_async(droq->cpu_id, csd);
2509 }
2510}
2511
2512/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002513 * \brief Entry point for NAPI polling
2514 * @param napi NAPI structure
2515 * @param budget maximum number of items to process
2516 */
2517static int liquidio_napi_poll(struct napi_struct *napi, int budget)
2518{
2519 struct octeon_droq *droq;
2520 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002521 int tx_done = 0, iq_no;
2522 struct octeon_instr_queue *iq;
2523 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002524
2525 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002526 oct = droq->oct_dev;
2527 iq_no = droq->q_no;
2528 /* Handle Droq descriptors */
2529 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2530 POLL_EVENT_PROCESS_PKTS,
2531 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002532
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002533 /* Flush the instruction queue */
2534 iq = oct->instr_queue[iq_no];
2535 if (iq) {
VSR Burru6069f3f2017-03-22 11:54:50 -07002536 if (atomic_read(&iq->instr_pending))
2537 /* Process iq buffers with in the budget limits */
2538 tx_done = octeon_flush_iq(oct, iq, budget);
2539 else
2540 tx_done = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002541 /* Update iq read-index rather than waiting for next interrupt.
2542 * Return back if tx_done is false.
2543 */
2544 update_txq_status(oct, iq_no);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002545 } else {
2546 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2547 __func__, iq_no);
2548 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002549
Satanand Burlacdb478e2017-01-31 13:04:42 -08002550 /* force enable interrupt if reg cnts are high to avoid wraparound */
2551 if ((work_done < budget && tx_done) ||
Felix Manlunas76e0e702017-02-07 12:10:58 -08002552 (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
Satanand Burlacdb478e2017-01-31 13:04:42 -08002553 (droq->pkt_count >= MAX_REG_CNT)) {
2554 tx_done = 1;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002555 napi_complete_done(napi, work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002556 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2557 POLL_EVENT_ENABLE_INTR, 0);
2558 return 0;
2559 }
2560
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002561 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002562}
2563
2564/**
2565 * \brief Setup input and output queues
2566 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002567 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002568 *
2569 * Note: Queues are with respect to the octeon device. Thus
2570 * an input queue is for egress packets, and output queues
2571 * are for ingress packets.
2572 */
2573static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002574 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002575{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002576 struct octeon_droq_ops droq_ops;
2577 struct net_device *netdev;
Gustavo A. R. Silvaeb2b9872017-07-18 15:53:48 -05002578 int cpu_id;
2579 int cpu_id_modulus;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002580 struct octeon_droq *droq;
2581 struct napi_struct *napi;
2582 int q, q_no, retval = 0;
2583 struct lio *lio;
2584 int num_tx_descs;
2585
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002586 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002587
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002588 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002589
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002590 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2591
2592 droq_ops.fptr = liquidio_push_packet;
2593 droq_ops.farg = (void *)netdev;
2594
2595 droq_ops.poll_mode = 1;
2596 droq_ops.napi_fn = liquidio_napi_drv_callback;
2597 cpu_id = 0;
2598 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002599
2600 /* set up DROQs. */
2601 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002602 q_no = lio->linfo.rxpciq[q].s.q_no;
2603 dev_dbg(&octeon_dev->pci_dev->dev,
2604 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2605 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002606 retval = octeon_setup_droq(octeon_dev, q_no,
2607 CFG_GET_NUM_RX_DESCS_NIC_IF
2608 (octeon_get_conf(octeon_dev),
2609 lio->ifidx),
2610 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2611 (octeon_get_conf(octeon_dev),
2612 lio->ifidx), NULL);
2613 if (retval) {
2614 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002615 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002616 __func__);
2617 return 1;
2618 }
2619
2620 droq = octeon_dev->droq[q_no];
2621 napi = &droq->napi;
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07002622 dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
2623 (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002624 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002625
2626 /* designate a CPU for this droq */
2627 droq->cpu_id = cpu_id;
2628 cpu_id++;
2629 if (cpu_id >= cpu_id_modulus)
2630 cpu_id = 0;
2631
2632 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2633 }
2634
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002635 if (OCTEON_CN23XX_PF(octeon_dev)) {
2636 /* 23XX PF can receive control messages (via the first PF-owned
2637 * droq) from the firmware even if the ethX interface is down,
2638 * so that's why poll_mode must be off for the first droq.
2639 */
2640 octeon_dev->droq[0]->ops.poll_mode = 0;
2641 }
2642
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002643 /* set up IQs. */
2644 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2645 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2646 (octeon_dev),
2647 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002648 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2649 lio->linfo.txpciq[q], num_tx_descs,
2650 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002651 if (retval) {
2652 dev_err(&octeon_dev->pci_dev->dev,
2653 " %s : Runtime IQ(TxQ) creation failed.\n",
2654 __func__);
2655 return 1;
2656 }
Rick Farrington35ae57e2017-03-07 11:40:41 -08002657
2658 if (octeon_dev->ioq_vector) {
2659 struct octeon_ioq_vector *ioq_vector;
2660
2661 ioq_vector = &octeon_dev->ioq_vector[q];
2662 netif_set_xps_queue(netdev,
2663 &ioq_vector->affinity_mask,
2664 ioq_vector->iq_index);
2665 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002666 }
2667
2668 return 0;
2669}
2670
2671/**
2672 * \brief Poll routine for checking transmit queue status
2673 * @param work work_struct data structure
2674 */
2675static void octnet_poll_check_txq_status(struct work_struct *work)
2676{
2677 struct cavium_wk *wk = (struct cavium_wk *)work;
2678 struct lio *lio = (struct lio *)wk->ctxptr;
2679
2680 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2681 return;
2682
2683 check_txq_status(lio);
2684 queue_delayed_work(lio->txq_status_wq.wq,
2685 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2686}
2687
2688/**
2689 * \brief Sets up the txq poll check
2690 * @param netdev network device
2691 */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002692static inline int setup_tx_poll_fn(struct net_device *netdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002693{
2694 struct lio *lio = GET_LIO(netdev);
2695 struct octeon_device *oct = lio->oct_dev;
2696
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302697 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2698 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002699 if (!lio->txq_status_wq.wq) {
2700 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002701 return -1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002702 }
2703 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2704 octnet_poll_check_txq_status);
2705 lio->txq_status_wq.wk.ctxptr = lio;
2706 queue_delayed_work(lio->txq_status_wq.wq,
2707 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002708 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002709}
2710
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002711static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2712{
2713 struct lio *lio = GET_LIO(netdev);
2714
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002715 if (lio->txq_status_wq.wq) {
2716 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2717 destroy_workqueue(lio->txq_status_wq.wq);
2718 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002719}
2720
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002721/**
2722 * \brief Net device open for LiquidIO
2723 * @param netdev network device
2724 */
2725static int liquidio_open(struct net_device *netdev)
2726{
2727 struct lio *lio = GET_LIO(netdev);
2728 struct octeon_device *oct = lio->oct_dev;
2729 struct napi_struct *napi, *n;
2730
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002731 if (oct->props[lio->ifidx].napi_enabled == 0) {
2732 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2733 napi_enable(napi);
2734
2735 oct->props[lio->ifidx].napi_enabled = 1;
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002736
2737 if (OCTEON_CN23XX_PF(oct))
2738 oct->droq[0]->ops.poll_mode = 1;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002739 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002740
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07002741 if (oct->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08002742 oct_ptp_open(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002743
2744 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002745
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07002746 /* Ready for link status updates */
2747 lio->intf_open = 1;
2748
2749 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2750
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002751 if (OCTEON_CN23XX_PF(oct)) {
2752 if (!oct->msix_on)
2753 if (setup_tx_poll_fn(netdev))
2754 return -1;
2755 } else {
2756 if (setup_tx_poll_fn(netdev))
2757 return -1;
2758 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002759
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002760 start_txq(netdev);
2761
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002762 /* tell Octeon to start forwarding packets to host */
2763 send_rx_ctrl_cmd(lio, 1);
2764
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002765 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2766 netdev->name);
2767
2768 return 0;
2769}
2770
2771/**
2772 * \brief Net device stop for LiquidIO
2773 * @param netdev network device
2774 */
2775static int liquidio_stop(struct net_device *netdev)
2776{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002777 struct lio *lio = GET_LIO(netdev);
2778 struct octeon_device *oct = lio->oct_dev;
Intiyaz Basha42013e92017-08-08 19:34:28 -07002779 struct napi_struct *napi, *n;
2780
2781 if (oct->props[lio->ifidx].napi_enabled) {
2782 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2783 napi_disable(napi);
2784
2785 oct->props[lio->ifidx].napi_enabled = 0;
2786
2787 if (OCTEON_CN23XX_PF(oct))
2788 oct->droq[0]->ops.poll_mode = 0;
2789 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002790
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002791 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2792
2793 netif_tx_disable(netdev);
2794
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002795 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002796 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002797 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002798 lio->linfo.link.s.link_up = 0;
2799 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002800
Felix Manlunascb2336b2017-01-11 17:09:02 -08002801 /* Tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002802 send_rx_ctrl_cmd(lio, 0);
2803
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07002804 if (OCTEON_CN23XX_PF(oct)) {
2805 if (!oct->msix_on)
2806 cleanup_tx_poll_fn(netdev);
2807 } else {
2808 cleanup_tx_poll_fn(netdev);
2809 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002810
2811 if (lio->ptp_clock) {
2812 ptp_clock_unregister(lio->ptp_clock);
2813 lio->ptp_clock = NULL;
2814 }
2815
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002816 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002817
2818 return 0;
2819}
2820
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002821/**
2822 * \brief Converts a mask based on net device flags
2823 * @param netdev network device
2824 *
2825 * This routine generates a octnet_ifflags mask from the net device flags
2826 * received from the OS.
2827 */
2828static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2829{
2830 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2831
2832 if (netdev->flags & IFF_PROMISC)
2833 f |= OCTNET_IFFLAG_PROMISC;
2834
2835 if (netdev->flags & IFF_ALLMULTI)
2836 f |= OCTNET_IFFLAG_ALLMULTI;
2837
2838 if (netdev->flags & IFF_MULTICAST) {
2839 f |= OCTNET_IFFLAG_MULTICAST;
2840
2841 /* Accept all multicast addresses if there are more than we
2842 * can handle
2843 */
2844 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2845 f |= OCTNET_IFFLAG_ALLMULTI;
2846 }
2847
2848 if (netdev->flags & IFF_BROADCAST)
2849 f |= OCTNET_IFFLAG_BROADCAST;
2850
2851 return f;
2852}
2853
2854/**
2855 * \brief Net device set_multicast_list
2856 * @param netdev network device
2857 */
2858static void liquidio_set_mcast_list(struct net_device *netdev)
2859{
2860 struct lio *lio = GET_LIO(netdev);
2861 struct octeon_device *oct = lio->oct_dev;
2862 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002863 struct netdev_hw_addr *ha;
2864 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002865 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002866 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2867
2868 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2869
2870 /* Create a ctrl pkt command to be sent to core app. */
2871 nctrl.ncmd.u64 = 0;
2872 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002873 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2874 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002875 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002876 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002877 nctrl.netpndev = (u64)netdev;
2878 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2879
2880 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002881 mc = &nctrl.udd[0];
2882 netdev_for_each_mc_addr(ha, netdev) {
2883 *mc = 0;
2884 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2885 /* no need to swap bytes */
2886
2887 if (++mc > &nctrl.udd[mc_count])
2888 break;
2889 }
2890
2891 /* Apparently, any activity in this call from the kernel has to
2892 * be atomic. So we won't wait for response.
2893 */
2894 nctrl.wait_time = 0;
2895
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002896 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002897 if (ret < 0) {
2898 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2899 ret);
2900 }
2901}
2902
2903/**
2904 * \brief Net device set_mac_address
2905 * @param netdev network device
2906 */
2907static int liquidio_set_mac(struct net_device *netdev, void *p)
2908{
2909 int ret = 0;
2910 struct lio *lio = GET_LIO(netdev);
2911 struct octeon_device *oct = lio->oct_dev;
2912 struct sockaddr *addr = (struct sockaddr *)p;
2913 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002914
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002915 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002916 return -EADDRNOTAVAIL;
2917
2918 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2919
2920 nctrl.ncmd.u64 = 0;
2921 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002922 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002923 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002924 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002925 nctrl.netpndev = (u64)netdev;
2926 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2927 nctrl.wait_time = 100;
2928
2929 nctrl.udd[0] = 0;
2930 /* The MAC Address is presented in network byte order. */
2931 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2932
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002933 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002934 if (ret < 0) {
2935 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2936 return -ENOMEM;
2937 }
2938 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2939 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2940
2941 return 0;
2942}
2943
2944/**
2945 * \brief Net device get_stats
2946 * @param netdev network device
2947 */
2948static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2949{
2950 struct lio *lio = GET_LIO(netdev);
2951 struct net_device_stats *stats = &netdev->stats;
2952 struct octeon_device *oct;
2953 u64 pkts = 0, drop = 0, bytes = 0;
2954 struct oct_droq_stats *oq_stats;
2955 struct oct_iq_stats *iq_stats;
2956 int i, iq_no, oq_no;
2957
2958 oct = lio->oct_dev;
2959
2960 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002961 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002962 iq_stats = &oct->instr_queue[iq_no]->stats;
2963 pkts += iq_stats->tx_done;
2964 drop += iq_stats->tx_dropped;
2965 bytes += iq_stats->tx_tot_bytes;
2966 }
2967
2968 stats->tx_packets = pkts;
2969 stats->tx_bytes = bytes;
2970 stats->tx_dropped = drop;
2971
2972 pkts = 0;
2973 drop = 0;
2974 bytes = 0;
2975
2976 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002977 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002978 oq_stats = &oct->droq[oq_no]->stats;
2979 pkts += oq_stats->rx_pkts_received;
2980 drop += (oq_stats->rx_dropped +
2981 oq_stats->dropped_nodispatch +
2982 oq_stats->dropped_toomany +
2983 oq_stats->dropped_nomem);
2984 bytes += oq_stats->rx_bytes_received;
2985 }
2986
2987 stats->rx_bytes = bytes;
2988 stats->rx_packets = pkts;
2989 stats->rx_dropped = drop;
2990
2991 return stats;
2992}
2993
2994/**
2995 * \brief Net device change_mtu
2996 * @param netdev network device
2997 */
2998static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2999{
3000 struct lio *lio = GET_LIO(netdev);
3001 struct octeon_device *oct = lio->oct_dev;
3002 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003003 int ret = 0;
3004
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003005 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3006
3007 nctrl.ncmd.u64 = 0;
3008 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003009 nctrl.ncmd.s.param1 = new_mtu;
3010 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003011 nctrl.wait_time = 100;
3012 nctrl.netpndev = (u64)netdev;
3013 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3014
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003015 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003016 if (ret < 0) {
3017 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
3018 return -1;
3019 }
3020
3021 lio->mtu = new_mtu;
3022
3023 return 0;
3024}
3025
3026/**
3027 * \brief Handler for SIOCSHWTSTAMP ioctl
3028 * @param netdev network device
3029 * @param ifr interface request
3030 * @param cmd command
3031 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003032static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003033{
3034 struct hwtstamp_config conf;
3035 struct lio *lio = GET_LIO(netdev);
3036
3037 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
3038 return -EFAULT;
3039
3040 if (conf.flags)
3041 return -EINVAL;
3042
3043 switch (conf.tx_type) {
3044 case HWTSTAMP_TX_ON:
3045 case HWTSTAMP_TX_OFF:
3046 break;
3047 default:
3048 return -ERANGE;
3049 }
3050
3051 switch (conf.rx_filter) {
3052 case HWTSTAMP_FILTER_NONE:
3053 break;
3054 case HWTSTAMP_FILTER_ALL:
3055 case HWTSTAMP_FILTER_SOME:
3056 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3057 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3058 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3059 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3060 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3061 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3062 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3063 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3064 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3065 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3066 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3067 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Miroslav Lichvare3412572017-05-19 17:52:36 +02003068 case HWTSTAMP_FILTER_NTP_ALL:
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003069 conf.rx_filter = HWTSTAMP_FILTER_ALL;
3070 break;
3071 default:
3072 return -ERANGE;
3073 }
3074
3075 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
3076 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3077
3078 else
3079 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
3080
3081 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
3082}
3083
3084/**
3085 * \brief ioctl handler
3086 * @param netdev network device
3087 * @param ifr interface request
3088 * @param cmd command
3089 */
3090static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
3091{
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003092 struct lio *lio = GET_LIO(netdev);
3093
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003094 switch (cmd) {
3095 case SIOCSHWTSTAMP:
Intiyaz Bashaaa69ff92017-08-11 11:22:09 -07003096 if (lio->oct_dev->ptp_enable)
Prasad Kanneganti9feb16a2017-01-03 11:27:33 -08003097 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003098 default:
3099 return -EOPNOTSUPP;
3100 }
3101}
3102
3103/**
3104 * \brief handle a Tx timestamp response
3105 * @param status response status
3106 * @param buf pointer to skb
3107 */
3108static void handle_timestamp(struct octeon_device *oct,
3109 u32 status,
3110 void *buf)
3111{
3112 struct octnet_buf_free_info *finfo;
3113 struct octeon_soft_command *sc;
3114 struct oct_timestamp_resp *resp;
3115 struct lio *lio;
3116 struct sk_buff *skb = (struct sk_buff *)buf;
3117
3118 finfo = (struct octnet_buf_free_info *)skb->cb;
3119 lio = finfo->lio;
3120 sc = finfo->sc;
3121 oct = lio->oct_dev;
3122 resp = (struct oct_timestamp_resp *)sc->virtrptr;
3123
3124 if (status != OCTEON_REQUEST_DONE) {
3125 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
3126 CVM_CAST64(status));
3127 resp->timestamp = 0;
3128 }
3129
3130 octeon_swap_8B_data(&resp->timestamp, 1);
3131
Colin Ian King19a6d152016-02-05 16:30:39 +00003132 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003133 struct skb_shared_hwtstamps ts;
3134 u64 ns = resp->timestamp;
3135
3136 netif_info(lio, tx_done, lio->netdev,
3137 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
3138 skb, (unsigned long long)ns);
3139 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
3140 skb_tstamp_tx(skb, &ts);
3141 }
3142
3143 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003144 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003145}
3146
3147/* \brief Send a data packet that will be timestamped
3148 * @param oct octeon device
3149 * @param ndata pointer to network data
3150 * @param finfo pointer to private network data
3151 */
3152static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
3153 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003154 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003155{
3156 int retval;
3157 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003158 struct lio *lio;
3159 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003160 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003161
3162 lio = finfo->lio;
3163
3164 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
3165 sizeof(struct oct_timestamp_resp));
3166 finfo->sc = sc;
3167
3168 if (!sc) {
3169 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
3170 return IQ_SEND_FAILED;
3171 }
3172
3173 if (ndata->reqtype == REQTYPE_NORESP_NET)
3174 ndata->reqtype = REQTYPE_RESP_NET;
3175 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
3176 ndata->reqtype = REQTYPE_RESP_NET_SG;
3177
3178 sc->callback = handle_timestamp;
3179 sc->callback_arg = finfo->skb;
3180 sc->iq_no = ndata->q_no;
3181
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003182 if (OCTEON_CN23XX_PF(oct))
3183 len = (u32)((struct octeon_instr_ih3 *)
3184 (&sc->cmd.cmd3.ih3))->dlengsz;
3185 else
3186 len = (u32)((struct octeon_instr_ih2 *)
3187 (&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003188
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003189 ring_doorbell = 1;
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003190
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003191 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003192 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003193
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003194 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003195 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
3196 retval);
3197 octeon_free_soft_command(oct, sc);
3198 } else {
3199 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
3200 }
3201
3202 return retval;
3203}
3204
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003205/** \brief Transmit networks packets to the Octeon interface
3206 * @param skbuff skbuff struct to be passed to network layer.
3207 * @param netdev pointer to network device
3208 * @returns whether the packet was transmitted to the device okay or not
3209 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
3210 */
3211static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
3212{
3213 struct lio *lio;
3214 struct octnet_buf_free_info *finfo;
3215 union octnic_cmd_setup cmdsetup;
3216 struct octnic_data_pkt ndata;
3217 struct octeon_device *oct;
3218 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003219 struct octeon_instr_irh *irh;
3220 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003221 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003222 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003223 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003224 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003225 u32 tag = 0;
3226
3227 lio = GET_LIO(netdev);
3228 oct = lio->oct_dev;
3229
3230 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003231 q_idx = skb->queue_mapping;
3232 q_idx = (q_idx % (lio->linfo.num_txpciq));
3233 tag = q_idx;
3234 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003235 } else {
3236 iq_no = lio->txq;
3237 }
3238
3239 stats = &oct->instr_queue[iq_no]->stats;
3240
3241 /* Check for all conditions in which the current packet cannot be
3242 * transmitted.
3243 */
3244 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003245 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003246 (skb->len <= 0)) {
3247 netif_info(lio, tx_err, lio->netdev,
3248 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003249 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003250 goto lio_xmit_failed;
3251 }
3252
3253 /* Use space in skb->cb to store info used to unmap and
3254 * free the buffers.
3255 */
3256 finfo = (struct octnet_buf_free_info *)skb->cb;
3257 finfo->lio = lio;
3258 finfo->skb = skb;
3259 finfo->sc = NULL;
3260
3261 /* Prepare the attributes for the data to be passed to OSI. */
3262 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
3263
3264 ndata.buf = (void *)finfo;
3265
3266 ndata.q_no = iq_no;
3267
3268 if (netif_is_multiqueue(netdev)) {
3269 if (octnet_iq_is_full(oct, ndata.q_no)) {
3270 /* defer sending if queue is full */
3271 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
3272 ndata.q_no);
3273 stats->tx_iq_busy++;
3274 return NETDEV_TX_BUSY;
3275 }
3276 } else {
3277 if (octnet_iq_is_full(oct, lio->txq)) {
3278 /* defer sending if queue is full */
3279 stats->tx_iq_busy++;
3280 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003281 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003282 return NETDEV_TX_BUSY;
3283 }
3284 }
3285 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003286 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003287 */
3288
3289 ndata.datasize = skb->len;
3290
3291 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07003292 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003293
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003294 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3295 if (skb->encapsulation) {
3296 cmdsetup.s.tnl_csum = 1;
3297 stats->tx_vxlan++;
3298 } else {
3299 cmdsetup.s.transport_csum = 1;
3300 }
3301 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003302 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
3303 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3304 cmdsetup.s.timestamp = 1;
3305 }
3306
3307 if (skb_shinfo(skb)->nr_frags == 0) {
3308 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003309 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003310
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003311 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003312 dptr = dma_map_single(&oct->pci_dev->dev,
3313 skb->data,
3314 skb->len,
3315 DMA_TO_DEVICE);
3316 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003317 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
3318 __func__);
3319 return NETDEV_TX_BUSY;
3320 }
3321
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003322 if (OCTEON_CN23XX_PF(oct))
3323 ndata.cmd.cmd3.dptr = dptr;
3324 else
3325 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003326 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003327 ndata.reqtype = REQTYPE_NORESP_NET;
3328
3329 } else {
3330 int i, frags;
3331 struct skb_frag_struct *frag;
3332 struct octnic_gather *g;
3333
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003334 spin_lock(&lio->glist_lock[q_idx]);
3335 g = (struct octnic_gather *)
3336 list_delete_head(&lio->glist[q_idx]);
3337 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003338
3339 if (!g) {
3340 netif_info(lio, tx_err, lio->netdev,
3341 "Transmit scatter gather: glist null!\n");
3342 goto lio_xmit_failed;
3343 }
3344
3345 cmdsetup.s.gather = 1;
3346 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003347 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003348
3349 memset(g->sg, 0, g->sg_size);
3350
3351 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
3352 skb->data,
3353 (skb->len - skb->data_len),
3354 DMA_TO_DEVICE);
3355 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
3356 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
3357 __func__);
3358 return NETDEV_TX_BUSY;
3359 }
3360 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
3361
3362 frags = skb_shinfo(skb)->nr_frags;
3363 i = 1;
3364 while (frags--) {
3365 frag = &skb_shinfo(skb)->frags[i - 1];
3366
3367 g->sg[(i >> 2)].ptr[(i & 3)] =
3368 dma_map_page(&oct->pci_dev->dev,
3369 frag->page.p,
3370 frag->page_offset,
3371 frag->size,
3372 DMA_TO_DEVICE);
3373
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003374 if (dma_mapping_error(&oct->pci_dev->dev,
3375 g->sg[i >> 2].ptr[i & 3])) {
3376 dma_unmap_single(&oct->pci_dev->dev,
3377 g->sg[0].ptr[0],
3378 skb->len - skb->data_len,
3379 DMA_TO_DEVICE);
3380 for (j = 1; j < i; j++) {
3381 frag = &skb_shinfo(skb)->frags[j - 1];
3382 dma_unmap_page(&oct->pci_dev->dev,
3383 g->sg[j >> 2].ptr[j & 3],
3384 frag->size,
3385 DMA_TO_DEVICE);
3386 }
3387 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
3388 __func__);
3389 return NETDEV_TX_BUSY;
3390 }
3391
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003392 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
3393 i++;
3394 }
3395
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003396 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003397
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003398 if (OCTEON_CN23XX_PF(oct))
3399 ndata.cmd.cmd3.dptr = dptr;
3400 else
3401 ndata.cmd.cmd2.dptr = dptr;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003402 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003403 finfo->g = g;
3404
3405 ndata.reqtype = REQTYPE_NORESP_NET_SG;
3406 }
3407
Raghu Vatsavayi5b823512016-09-01 11:16:07 -07003408 if (OCTEON_CN23XX_PF(oct)) {
3409 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
3410 tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
3411 } else {
3412 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
3413 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
3414 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003415
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003416 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003417 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
3418 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003419 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003420 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003421
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003422 /* HW insert VLAN tag */
3423 if (skb_vlan_tag_present(skb)) {
3424 irh->priority = skb_vlan_tag_get(skb) >> 13;
3425 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
3426 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003427
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003428 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003429 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003430 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07003431 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003432 if (status == IQ_SEND_FAILED)
3433 goto lio_xmit_failed;
3434
3435 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
3436
3437 if (status == IQ_SEND_STOP)
3438 stop_q(lio->netdev, q_idx);
3439
Florian Westphal860e9532016-05-03 16:33:13 +02003440 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003441
Satanand Burla80c8eae2017-01-26 11:52:35 -08003442 if (tx_info->s.gso_segs)
3443 stats->tx_done += tx_info->s.gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07003444 else
3445 stats->tx_done++;
Satanand Burla80c8eae2017-01-26 11:52:35 -08003446 stats->tx_tot_bytes += ndata.datasize;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003447
3448 return NETDEV_TX_OK;
3449
3450lio_xmit_failed:
3451 stats->tx_dropped++;
3452 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
3453 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07003454 if (dptr)
3455 dma_unmap_single(&oct->pci_dev->dev, dptr,
3456 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07003457 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003458 return NETDEV_TX_OK;
3459}
3460
3461/** \brief Network device Tx timeout
3462 * @param netdev pointer to network device
3463 */
3464static void liquidio_tx_timeout(struct net_device *netdev)
3465{
3466 struct lio *lio;
3467
3468 lio = GET_LIO(netdev);
3469
3470 netif_info(lio, tx_err, lio->netdev,
3471 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
3472 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02003473 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003474 txqs_wake(netdev);
3475}
3476
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003477static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
3478 __be16 proto __attribute__((unused)),
3479 u16 vid)
3480{
3481 struct lio *lio = GET_LIO(netdev);
3482 struct octeon_device *oct = lio->oct_dev;
3483 struct octnic_ctrl_pkt nctrl;
3484 int ret = 0;
3485
3486 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3487
3488 nctrl.ncmd.u64 = 0;
3489 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3490 nctrl.ncmd.s.param1 = vid;
3491 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3492 nctrl.wait_time = 100;
3493 nctrl.netpndev = (u64)netdev;
3494 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3495
3496 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3497 if (ret < 0) {
3498 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3499 ret);
3500 }
3501
3502 return ret;
3503}
3504
3505static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
3506 __be16 proto __attribute__((unused)),
3507 u16 vid)
3508{
3509 struct lio *lio = GET_LIO(netdev);
3510 struct octeon_device *oct = lio->oct_dev;
3511 struct octnic_ctrl_pkt nctrl;
3512 int ret = 0;
3513
3514 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3515
3516 nctrl.ncmd.u64 = 0;
3517 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3518 nctrl.ncmd.s.param1 = vid;
3519 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3520 nctrl.wait_time = 100;
3521 nctrl.netpndev = (u64)netdev;
3522 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3523
3524 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3525 if (ret < 0) {
3526 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
3527 ret);
3528 }
3529 return ret;
3530}
3531
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003532/** Sending command to enable/disable RX checksum offload
3533 * @param netdev pointer to network device
3534 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
3535 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
3536 * OCTNET_CMD_RXCSUM_DISABLE
3537 * @returns SUCCESS or FAILURE
3538 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02003539static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
3540 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003541{
3542 struct lio *lio = GET_LIO(netdev);
3543 struct octeon_device *oct = lio->oct_dev;
3544 struct octnic_ctrl_pkt nctrl;
3545 int ret = 0;
3546
Felix Manlunas0c264582017-04-06 19:22:22 -07003547 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3548
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003549 nctrl.ncmd.u64 = 0;
3550 nctrl.ncmd.s.cmd = command;
3551 nctrl.ncmd.s.param1 = rx_cmd;
3552 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3553 nctrl.wait_time = 100;
3554 nctrl.netpndev = (u64)netdev;
3555 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3556
3557 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3558 if (ret < 0) {
3559 dev_err(&oct->pci_dev->dev,
3560 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
3561 ret);
3562 }
3563 return ret;
3564}
3565
3566/** Sending command to add/delete VxLAN UDP port to firmware
3567 * @param netdev pointer to network device
3568 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
3569 * @param vxlan_port VxLAN port to be added or deleted
3570 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
3571 * OCTNET_CMD_VXLAN_PORT_DEL
3572 * @returns SUCCESS or FAILURE
3573 */
3574static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
3575 u16 vxlan_port, u8 vxlan_cmd_bit)
3576{
3577 struct lio *lio = GET_LIO(netdev);
3578 struct octeon_device *oct = lio->oct_dev;
3579 struct octnic_ctrl_pkt nctrl;
3580 int ret = 0;
3581
Felix Manlunas0c264582017-04-06 19:22:22 -07003582 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3583
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003584 nctrl.ncmd.u64 = 0;
3585 nctrl.ncmd.s.cmd = command;
3586 nctrl.ncmd.s.more = vxlan_cmd_bit;
3587 nctrl.ncmd.s.param1 = vxlan_port;
3588 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3589 nctrl.wait_time = 100;
3590 nctrl.netpndev = (u64)netdev;
3591 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3592
3593 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3594 if (ret < 0) {
3595 dev_err(&oct->pci_dev->dev,
3596 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3597 ret);
3598 }
3599 return ret;
3600}
3601
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003602/** \brief Net device fix features
3603 * @param netdev pointer to network device
3604 * @param request features requested
3605 * @returns updated features list
3606 */
3607static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3608 netdev_features_t request)
3609{
3610 struct lio *lio = netdev_priv(netdev);
3611
3612 if ((request & NETIF_F_RXCSUM) &&
3613 !(lio->dev_capability & NETIF_F_RXCSUM))
3614 request &= ~NETIF_F_RXCSUM;
3615
3616 if ((request & NETIF_F_HW_CSUM) &&
3617 !(lio->dev_capability & NETIF_F_HW_CSUM))
3618 request &= ~NETIF_F_HW_CSUM;
3619
3620 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3621 request &= ~NETIF_F_TSO;
3622
3623 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3624 request &= ~NETIF_F_TSO6;
3625
3626 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3627 request &= ~NETIF_F_LRO;
3628
3629 /*Disable LRO if RXCSUM is off */
3630 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3631 (lio->dev_capability & NETIF_F_LRO))
3632 request &= ~NETIF_F_LRO;
3633
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003634 if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3635 !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
3636 request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3637
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003638 return request;
3639}
3640
3641/** \brief Net device set features
3642 * @param netdev pointer to network device
3643 * @param features features to enable/disable
3644 */
3645static int liquidio_set_features(struct net_device *netdev,
3646 netdev_features_t features)
3647{
3648 struct lio *lio = netdev_priv(netdev);
3649
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003650 if ((features & NETIF_F_LRO) &&
3651 (lio->dev_capability & NETIF_F_LRO) &&
3652 !(netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003653 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3654 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003655 else if (!(features & NETIF_F_LRO) &&
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003656 (lio->dev_capability & NETIF_F_LRO) &&
3657 (netdev->features & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003658 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3659 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003660
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003661 /* Sending command to firmware to enable/disable RX checksum
3662 * offload settings using ethtool
3663 */
3664 if (!(netdev->features & NETIF_F_RXCSUM) &&
3665 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3666 (features & NETIF_F_RXCSUM))
3667 liquidio_set_rxcsum_command(netdev,
3668 OCTNET_CMD_TNL_RX_CSUM_CTL,
3669 OCTNET_CMD_RXCSUM_ENABLE);
3670 else if ((netdev->features & NETIF_F_RXCSUM) &&
3671 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3672 !(features & NETIF_F_RXCSUM))
3673 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3674 OCTNET_CMD_RXCSUM_DISABLE);
3675
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07003676 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3677 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3678 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3679 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3680 OCTNET_CMD_VLAN_FILTER_ENABLE);
3681 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3682 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
3683 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
3684 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3685 OCTNET_CMD_VLAN_FILTER_DISABLE);
3686
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003687 return 0;
3688}
3689
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003690static void liquidio_add_vxlan_port(struct net_device *netdev,
3691 struct udp_tunnel_info *ti)
3692{
3693 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3694 return;
3695
3696 liquidio_vxlan_port_command(netdev,
3697 OCTNET_CMD_VXLAN_PORT_CONFIG,
3698 htons(ti->port),
3699 OCTNET_CMD_VXLAN_PORT_ADD);
3700}
3701
3702static void liquidio_del_vxlan_port(struct net_device *netdev,
3703 struct udp_tunnel_info *ti)
3704{
3705 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3706 return;
3707
3708 liquidio_vxlan_port_command(netdev,
3709 OCTNET_CMD_VXLAN_PORT_CONFIG,
3710 htons(ti->port),
3711 OCTNET_CMD_VXLAN_PORT_DEL);
3712}
3713
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003714static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
3715 u8 *mac, bool is_admin_assigned)
3716{
3717 struct lio *lio = GET_LIO(netdev);
3718 struct octeon_device *oct = lio->oct_dev;
3719 struct octnic_ctrl_pkt nctrl;
3720
3721 if (!is_valid_ether_addr(mac))
3722 return -EINVAL;
3723
3724 if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3725 return -EINVAL;
3726
3727 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3728
3729 nctrl.ncmd.u64 = 0;
3730 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3731 /* vfidx is 0 based, but vf_num (param1) is 1 based */
3732 nctrl.ncmd.s.param1 = vfidx + 1;
3733 nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3734 nctrl.ncmd.s.more = 1;
3735 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Rick Farrington9549c6c2017-03-17 15:43:26 -07003736 nctrl.netpndev = (u64)netdev;
3737 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003738 nctrl.wait_time = LIO_CMD_WAIT_TM;
3739
3740 nctrl.udd[0] = 0;
3741 /* The MAC Address is presented in network byte order. */
3742 ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3743
3744 oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3745
3746 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3747
3748 return 0;
3749}
3750
3751static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3752{
3753 struct lio *lio = GET_LIO(netdev);
3754 struct octeon_device *oct = lio->oct_dev;
3755 int retval;
3756
Felix Manlunas0d9a5992017-05-16 11:28:00 -07003757 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3758 return -EINVAL;
3759
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003760 retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3761 if (!retval)
3762 cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3763
3764 return retval;
3765}
3766
3767static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3768 u16 vlan, u8 qos, __be16 vlan_proto)
3769{
3770 struct lio *lio = GET_LIO(netdev);
3771 struct octeon_device *oct = lio->oct_dev;
3772 struct octnic_ctrl_pkt nctrl;
3773 u16 vlantci;
3774
3775 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3776 return -EINVAL;
3777
3778 if (vlan_proto != htons(ETH_P_8021Q))
3779 return -EPROTONOSUPPORT;
3780
3781 if (vlan >= VLAN_N_VID || qos > 7)
3782 return -EINVAL;
3783
3784 if (vlan)
3785 vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3786 else
3787 vlantci = 0;
3788
3789 if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3790 return 0;
3791
3792 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3793
3794 if (vlan)
3795 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3796 else
3797 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3798
3799 nctrl.ncmd.s.param1 = vlantci;
3800 nctrl.ncmd.s.param2 =
3801 vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3802 nctrl.ncmd.s.more = 0;
3803 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3804 nctrl.cb_fn = 0;
3805 nctrl.wait_time = LIO_CMD_WAIT_TM;
3806
3807 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3808
3809 oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3810
3811 return 0;
3812}
3813
3814static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3815 struct ifla_vf_info *ivi)
3816{
3817 struct lio *lio = GET_LIO(netdev);
3818 struct octeon_device *oct = lio->oct_dev;
3819 u8 *macaddr;
3820
3821 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3822 return -EINVAL;
3823
3824 ivi->vf = vfidx;
3825 macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3826 ether_addr_copy(&ivi->mac[0], macaddr);
3827 ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3828 ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3829 ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3830 return 0;
3831}
3832
3833static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3834 int linkstate)
3835{
3836 struct lio *lio = GET_LIO(netdev);
3837 struct octeon_device *oct = lio->oct_dev;
3838 struct octnic_ctrl_pkt nctrl;
3839
3840 if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3841 return -EINVAL;
3842
3843 if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3844 return 0;
3845
3846 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3847 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3848 nctrl.ncmd.s.param1 =
3849 vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3850 nctrl.ncmd.s.param2 = linkstate;
3851 nctrl.ncmd.s.more = 0;
3852 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3853 nctrl.cb_fn = 0;
3854 nctrl.wait_time = LIO_CMD_WAIT_TM;
3855
3856 octnet_send_nic_ctrl_pkt(oct, &nctrl);
3857
3858 oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3859
3860 return 0;
3861}
3862
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003863static const struct net_device_ops lionetdevops = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003864 .ndo_open = liquidio_open,
3865 .ndo_stop = liquidio_stop,
3866 .ndo_start_xmit = liquidio_xmit,
3867 .ndo_get_stats = liquidio_get_stats,
3868 .ndo_set_mac_address = liquidio_set_mac,
3869 .ndo_set_rx_mode = liquidio_set_mcast_list,
3870 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003871
3872 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3873 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003874 .ndo_change_mtu = liquidio_change_mtu,
3875 .ndo_do_ioctl = liquidio_ioctl,
3876 .ndo_fix_features = liquidio_fix_features,
3877 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003878 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3879 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08003880 .ndo_set_vf_mac = liquidio_set_vf_mac,
3881 .ndo_set_vf_vlan = liquidio_set_vf_vlan,
3882 .ndo_get_vf_config = liquidio_get_vf_config,
3883 .ndo_set_vf_link_state = liquidio_set_vf_link_state,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003884};
3885
3886/** \brief Entry point for the liquidio module
3887 */
3888static int __init liquidio_init(void)
3889{
3890 int i;
3891 struct handshake *hs;
3892
3893 init_completion(&first_stage);
3894
Raghu Vatsavayi97a25322016-11-14 15:54:47 -08003895 octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003896
3897 if (liquidio_init_pci())
3898 return -EINVAL;
3899
3900 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3901
3902 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3903 hs = &handshake[i];
3904 if (hs->pci_dev) {
3905 wait_for_completion(&hs->init);
3906 if (!hs->init_ok) {
3907 /* init handshake failed */
3908 dev_err(&hs->pci_dev->dev,
3909 "Failed to init device\n");
3910 liquidio_deinit_pci();
3911 return -EIO;
3912 }
3913 }
3914 }
3915
3916 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3917 hs = &handshake[i];
3918 if (hs->pci_dev) {
3919 wait_for_completion_timeout(&hs->started,
3920 msecs_to_jiffies(30000));
3921 if (!hs->started_ok) {
3922 /* starter handshake failed */
3923 dev_err(&hs->pci_dev->dev,
3924 "Firmware failed to start\n");
3925 liquidio_deinit_pci();
3926 return -EIO;
3927 }
3928 }
3929 }
3930
3931 return 0;
3932}
3933
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003934static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003935{
3936 struct octeon_device *oct = (struct octeon_device *)buf;
3937 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003938 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003939 union oct_link_status *ls;
3940 int i;
3941
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003942 if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003943 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3944 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003945 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003946 goto nic_info_err;
3947 }
3948
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003949 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07003950 ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3951 OCT_DROQ_INFO_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003952
3953 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003954 for (i = 0; i < oct->ifcount; i++) {
3955 if (oct->props[i].gmxport == gmxport) {
3956 update_link_status(oct->props[i].netdev, ls);
3957 break;
3958 }
3959 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003960
3961nic_info_err:
3962 for (i = 0; i < recv_pkt->buffer_count; i++)
3963 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3964 octeon_free_recv_info(recv_info);
3965 return 0;
3966}
3967
3968/**
3969 * \brief Setup network interfaces
3970 * @param octeon_dev octeon device
3971 *
3972 * Called during init time for each device. It assumes the NIC
3973 * is already up and running. The link information for each
3974 * interface is passed in link_info.
3975 */
3976static int setup_nic_devices(struct octeon_device *octeon_dev)
3977{
3978 struct lio *lio = NULL;
3979 struct net_device *netdev;
3980 u8 mac[6], i, j;
3981 struct octeon_soft_command *sc;
3982 struct liquidio_if_cfg_context *ctx;
3983 struct liquidio_if_cfg_resp *resp;
3984 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003985 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003986 union oct_nic_if_cfg if_cfg;
3987 unsigned int base_queue;
3988 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003989 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003990 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003991 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003992
3993 /* This is to handle link status changes */
3994 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3995 OPCODE_NIC_INFO,
3996 lio_nic_info, octeon_dev);
3997
3998 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3999 * They are handled directly.
4000 */
4001 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
4002 free_netbuf);
4003
4004 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
4005 free_netsgbuf);
4006
4007 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
4008 free_netsgbuf_with_resp);
4009
4010 for (i = 0; i < octeon_dev->ifcount; i++) {
4011 resp_size = sizeof(struct liquidio_if_cfg_resp);
4012 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004013 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004014 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004015 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004016 resp_size, ctx_size);
4017 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
4018 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07004019 vdata = (struct lio_version *)sc->virtdptr;
4020
4021 *((u64 *)vdata) = 0;
4022 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
4023 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
4024 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004025
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004026 if (OCTEON_CN23XX_PF(octeon_dev)) {
4027 num_iqueues = octeon_dev->sriov_info.num_pf_rings;
4028 num_oqueues = octeon_dev->sriov_info.num_pf_rings;
4029 base_queue = octeon_dev->sriov_info.pf_srn;
4030
4031 gmx_port_id = octeon_dev->pf_num;
4032 ifidx_or_pfnum = octeon_dev->pf_num;
4033 } else {
4034 num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
4035 octeon_get_conf(octeon_dev), i);
4036 num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
4037 octeon_get_conf(octeon_dev), i);
4038 base_queue = CFG_GET_BASE_QUE_NIC_IF(
4039 octeon_get_conf(octeon_dev), i);
4040 gmx_port_id = CFG_GET_GMXID_NIC_IF(
4041 octeon_get_conf(octeon_dev), i);
4042 ifidx_or_pfnum = i;
4043 }
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07004044
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004045 dev_dbg(&octeon_dev->pci_dev->dev,
4046 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004047 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07004048 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004049 ctx->octeon_id = lio_get_device_id(octeon_dev);
4050 init_waitqueue_head(&ctx->wc);
4051
4052 if_cfg.u64 = 0;
4053 if_cfg.s.num_iqueues = num_iqueues;
4054 if_cfg.s.num_oqueues = num_oqueues;
4055 if_cfg.s.base_queue = base_queue;
4056 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004057
4058 sc->iq_no = 0;
4059
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004060 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004061 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004062 if_cfg.u64, 0);
4063
4064 sc->callback = if_cfg_callback;
4065 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07004066 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004067
4068 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07004069 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004070 dev_err(&octeon_dev->pci_dev->dev,
4071 "iq/oq config failed status: %x\n",
4072 retval);
4073 /* Soft instr is freed by driver in case of failure. */
4074 goto setup_nic_dev_fail;
4075 }
4076
4077 /* Sleep on a wait queue till the cond flag indicates that the
4078 * response arrived or timed-out.
4079 */
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004080 if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
4081 dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
4082 goto setup_nic_wait_intr;
4083 }
4084
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004085 retval = resp->status;
4086 if (retval) {
4087 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
4088 goto setup_nic_dev_fail;
4089 }
4090
4091 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
4092 (sizeof(struct liquidio_if_cfg_info)) >> 3);
4093
4094 num_iqueues = hweight64(resp->cfg_info.iqmask);
4095 num_oqueues = hweight64(resp->cfg_info.oqmask);
4096
4097 if (!(num_iqueues) || !(num_oqueues)) {
4098 dev_err(&octeon_dev->pci_dev->dev,
4099 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
4100 resp->cfg_info.iqmask,
4101 resp->cfg_info.oqmask);
4102 goto setup_nic_dev_fail;
4103 }
4104 dev_dbg(&octeon_dev->pci_dev->dev,
4105 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
4106 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
4107 num_iqueues, num_oqueues);
4108 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
4109
4110 if (!netdev) {
4111 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
4112 goto setup_nic_dev_fail;
4113 }
4114
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004115 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004116
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004117 /* Associate the routines that will handle different
4118 * netdev tasks.
4119 */
4120 netdev->netdev_ops = &lionetdevops;
4121
4122 lio = GET_LIO(netdev);
4123
4124 memset(lio, 0, sizeof(struct lio));
4125
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004126 lio->ifidx = ifidx_or_pfnum;
4127
4128 props = &octeon_dev->props[i];
4129 props->gmxport = resp->cfg_info.linfo.gmxport;
4130 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004131
4132 lio->linfo.num_rxpciq = num_oqueues;
4133 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004134 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004135 lio->linfo.rxpciq[j].u64 =
4136 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004137 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004138 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004139 lio->linfo.txpciq[j].u64 =
4140 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004141 }
4142 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
4143 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
4144 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
4145
4146 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4147
Raghu Vatsavayie86b1ab2016-08-31 11:03:24 -07004148 if (OCTEON_CN23XX_PF(octeon_dev) ||
4149 OCTEON_CN6XXX(octeon_dev)) {
4150 lio->dev_capability = NETIF_F_HIGHDMA
4151 | NETIF_F_IP_CSUM
4152 | NETIF_F_IPV6_CSUM
4153 | NETIF_F_SG | NETIF_F_RXCSUM
4154 | NETIF_F_GRO
4155 | NETIF_F_TSO | NETIF_F_TSO6
4156 | NETIF_F_LRO;
4157 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004158 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
4159
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004160 /* Copy of transmit encapsulation capabilities:
4161 * TSO, TSO6, Checksums for this device
4162 */
4163 lio->enc_dev_capability = NETIF_F_IP_CSUM
4164 | NETIF_F_IPV6_CSUM
4165 | NETIF_F_GSO_UDP_TUNNEL
4166 | NETIF_F_HW_CSUM | NETIF_F_SG
4167 | NETIF_F_RXCSUM
4168 | NETIF_F_TSO | NETIF_F_TSO6
4169 | NETIF_F_LRO;
4170
4171 netdev->hw_enc_features = (lio->enc_dev_capability &
4172 ~NETIF_F_LRO);
4173
4174 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
4175
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004176 netdev->vlan_features = lio->dev_capability;
4177 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004178 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
4179 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004180 NETIF_F_HW_VLAN_CTAG_TX;
4181
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004182 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
4183
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004184 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07004185 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
4186 netdev->hw_features = netdev->hw_features &
4187 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004188
Jarod Wilson109cc162016-10-17 15:54:13 -04004189 /* MTU range: 68 - 16000 */
4190 netdev->min_mtu = LIO_MIN_MTU_SIZE;
4191 netdev->max_mtu = LIO_MAX_MTU_SIZE;
4192
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004193 /* Point to the properties for octeon device to which this
4194 * interface belongs.
4195 */
4196 lio->oct_dev = octeon_dev;
4197 lio->octprops = props;
4198 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004199
4200 dev_dbg(&octeon_dev->pci_dev->dev,
4201 "if%d gmx: %d hw_addr: 0x%llx\n", i,
4202 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
4203
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004204 for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
4205 u8 vfmac[ETH_ALEN];
4206
4207 random_ether_addr(&vfmac[0]);
4208 if (__liquidio_set_vf_mac(netdev, j,
4209 &vfmac[0], false)) {
4210 dev_err(&octeon_dev->pci_dev->dev,
4211 "Error setting VF%d MAC address\n",
4212 j);
4213 goto setup_nic_dev_fail;
4214 }
4215 }
4216
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004217 /* 64-bit swap required on LE machines */
4218 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
4219 for (j = 0; j < 6; j++)
4220 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
4221
4222 /* Copy MAC Address to OS network device structure */
4223
4224 ether_addr_copy(netdev->dev_addr, mac);
4225
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07004226 /* By default all interfaces on a single Octeon uses the same
4227 * tx and rx queues
4228 */
4229 lio->txq = lio->linfo.txpciq[0].s.q_no;
4230 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004231 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004232 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
4233 goto setup_nic_dev_fail;
4234 }
4235
4236 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
4237
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004238 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
4239 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
4240
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07004241 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004242 dev_err(&octeon_dev->pci_dev->dev,
4243 "Gather list allocation failed\n");
4244 goto setup_nic_dev_fail;
4245 }
4246
4247 /* Register ethtool support */
4248 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004249 if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
4250 octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
4251 else
4252 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004253
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004254 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07004255 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
4256 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004257
Prasad Kanneganti836d57e2017-06-18 12:41:34 -07004258 liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
4259 OCTNET_CMD_VLAN_FILTER_ENABLE);
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004260
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004261 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07004262 liquidio_set_feature(netdev,
4263 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004264
Raghu Vatsavayi7b6b6c92016-09-01 11:16:04 -07004265 if (setup_link_status_change_wq(netdev))
4266 goto setup_nic_dev_fail;
4267
Satanand Burla031d4f12017-03-22 11:31:13 -07004268 if (setup_rx_oom_poll_fn(netdev))
4269 goto setup_nic_dev_fail;
4270
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004271 /* Register the network device with the OS */
4272 if (register_netdev(netdev)) {
4273 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
4274 goto setup_nic_dev_fail;
4275 }
4276
4277 dev_dbg(&octeon_dev->pci_dev->dev,
4278 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
4279 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
4280 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004281 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004282
4283 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
4284
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07004285 /* Sending command to firmware to enable Rx checksum offload
4286 * by default at the time of setup of Liquidio driver for
4287 * this device
4288 */
4289 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
4290 OCTNET_CMD_RXCSUM_ENABLE);
4291 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
4292 OCTNET_CMD_TXCSUM_ENABLE);
4293
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004294 dev_dbg(&octeon_dev->pci_dev->dev,
4295 "NIC ifidx:%d Setup successful\n", i);
4296
4297 octeon_free_soft_command(octeon_dev, sc);
4298 }
4299
4300 return 0;
4301
4302setup_nic_dev_fail:
4303
4304 octeon_free_soft_command(octeon_dev, sc);
4305
Raghu Vatsavayiafdf8412016-09-01 11:16:05 -07004306setup_nic_wait_intr:
4307
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004308 while (i--) {
4309 dev_err(&octeon_dev->pci_dev->dev,
4310 "NIC ifidx:%d Setup failed\n", i);
4311 liquidio_destroy_nic_device(octeon_dev, i);
4312 }
4313 return -ENODEV;
4314}
4315
Raghu Vatsavayica6139f2016-11-14 15:54:40 -08004316#ifdef CONFIG_PCI_IOV
4317static int octeon_enable_sriov(struct octeon_device *oct)
4318{
4319 unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
4320 struct pci_dev *vfdev;
4321 int err;
4322 u32 u;
4323
4324 if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
4325 err = pci_enable_sriov(oct->pci_dev,
4326 oct->sriov_info.num_vfs_alloced);
4327 if (err) {
4328 dev_err(&oct->pci_dev->dev,
4329 "OCTEON: Failed to enable PCI sriov: %d\n",
4330 err);
4331 oct->sriov_info.num_vfs_alloced = 0;
4332 return err;
4333 }
4334 oct->sriov_info.sriov_enabled = 1;
4335
4336 /* init lookup table that maps DPI ring number to VF pci_dev
4337 * struct pointer
4338 */
4339 u = 0;
4340 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4341 OCTEON_CN23XX_VF_VID, NULL);
4342 while (vfdev) {
4343 if (vfdev->is_virtfn &&
4344 (vfdev->physfn == oct->pci_dev)) {
4345 oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
4346 vfdev;
4347 u += oct->sriov_info.rings_per_vf;
4348 }
4349 vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
4350 OCTEON_CN23XX_VF_VID, vfdev);
4351 }
4352 }
4353
4354 return num_vfs_alloced;
4355}
4356
4357static int lio_pci_sriov_disable(struct octeon_device *oct)
4358{
4359 int u;
4360
4361 if (pci_vfs_assigned(oct->pci_dev)) {
4362 dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
4363 return -EPERM;
4364 }
4365
4366 pci_disable_sriov(oct->pci_dev);
4367
4368 u = 0;
4369 while (u < MAX_POSSIBLE_VFS) {
4370 oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
4371 u += oct->sriov_info.rings_per_vf;
4372 }
4373
4374 oct->sriov_info.num_vfs_alloced = 0;
4375 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
4376 oct->pf_num);
4377
4378 return 0;
4379}
4380
4381static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
4382{
4383 struct octeon_device *oct = pci_get_drvdata(dev);
4384 int ret = 0;
4385
4386 if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
4387 (oct->sriov_info.sriov_enabled)) {
4388 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
4389 oct->pf_num, num_vfs);
4390 return 0;
4391 }
4392
4393 if (!num_vfs) {
4394 ret = lio_pci_sriov_disable(oct);
4395 } else if (num_vfs > oct->sriov_info.max_vfs) {
4396 dev_err(&oct->pci_dev->dev,
4397 "OCTEON: Max allowed VFs:%d user requested:%d",
4398 oct->sriov_info.max_vfs, num_vfs);
4399 ret = -EPERM;
4400 } else {
4401 oct->sriov_info.num_vfs_alloced = num_vfs;
4402 ret = octeon_enable_sriov(oct);
4403 dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
4404 oct->pf_num, num_vfs);
4405 }
4406
4407 return ret;
4408}
4409#endif
4410
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004411/**
4412 * \brief initialize the NIC
4413 * @param oct octeon device
4414 *
4415 * This initialization routine is called once the Octeon device application is
4416 * up and running
4417 */
4418static int liquidio_init_nic_module(struct octeon_device *oct)
4419{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004420 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004421 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
4422
4423 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
4424
4425 /* only default iq and oq were initialized
4426 * initialize the rest as well
4427 */
4428 /* run port_config command for each port */
4429 oct->ifcount = num_nic_ports;
4430
Raghu Vatsavayi30136392016-09-01 11:16:11 -07004431 memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004432
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07004433 for (i = 0; i < MAX_OCTEON_LINKS; i++)
4434 oct->props[i].gmxport = -1;
4435
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004436 retval = setup_nic_devices(oct);
4437 if (retval) {
4438 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
4439 goto octnet_init_failure;
4440 }
4441
4442 liquidio_ptp_init(oct);
4443
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004444 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4445
4446 return retval;
4447
4448octnet_init_failure:
4449
4450 oct->ifcount = 0;
4451
4452 return retval;
4453}
4454
4455/**
4456 * \brief starter callback that invokes the remaining initialization work after
4457 * the NIC is up and running.
4458 * @param octptr work struct work_struct
4459 */
4460static void nic_starter(struct work_struct *work)
4461{
4462 struct octeon_device *oct;
4463 struct cavium_wk *wk = (struct cavium_wk *)work;
4464
4465 oct = (struct octeon_device *)wk->ctxptr;
4466
4467 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4468 return;
4469
4470 /* If the status of the device is CORE_OK, the core
4471 * application has reported its application type. Call
4472 * any registered handlers now and move to the RUNNING
4473 * state.
4474 */
4475 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4476 schedule_delayed_work(&oct->nic_poll_work.work,
4477 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4478 return;
4479 }
4480
4481 atomic_set(&oct->status, OCT_DEV_RUNNING);
4482
4483 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4484 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4485
4486 if (liquidio_init_nic_module(oct))
4487 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4488 else
4489 handshake[oct->octeon_id].started_ok = 1;
4490 } else {
4491 dev_err(&oct->pci_dev->dev,
4492 "Unexpected application running on NIC (%d). Check firmware.\n",
4493 oct->app_mode);
4494 }
4495
4496 complete(&handshake[oct->octeon_id].started);
4497}
4498
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004499static int
4500octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4501{
4502 struct octeon_device *oct = (struct octeon_device *)buf;
4503 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4504 int i, notice, vf_idx;
Felix Manlunasbb54be52017-04-04 19:26:57 -07004505 bool cores_crashed;
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004506 u64 *data, vf_num;
4507
4508 notice = recv_pkt->rh.r.ossp;
Prasad Kannegantic4ee5d82017-06-18 05:04:11 -07004509 data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004510
4511 /* the first 64-bit word of data is the vf_num */
4512 vf_num = data[0];
4513 octeon_swap_8B_data(&vf_num, 1);
4514 vf_idx = (int)vf_num - 1;
4515
Felix Manlunasbb54be52017-04-04 19:26:57 -07004516 cores_crashed = READ_ONCE(oct->cores_crashed);
4517
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004518 if (notice == VF_DRV_LOADED) {
4519 if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4520 oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4521 dev_info(&oct->pci_dev->dev,
4522 "driver for VF%d was loaded\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004523 if (!cores_crashed)
4524 try_module_get(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004525 }
4526 } else if (notice == VF_DRV_REMOVED) {
4527 if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4528 oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4529 dev_info(&oct->pci_dev->dev,
4530 "driver for VF%d was removed\n", vf_idx);
Felix Manlunasbb54be52017-04-04 19:26:57 -07004531 if (!cores_crashed)
4532 module_put(THIS_MODULE);
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004533 }
4534 } else if (notice == VF_DRV_MACADDR_CHANGED) {
4535 u8 *b = (u8 *)&data[1];
4536
4537 oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4538 dev_info(&oct->pci_dev->dev,
4539 "VF driver changed VF%d's MAC address to %pM\n",
4540 vf_idx, b + 2);
4541 }
4542
4543 for (i = 0; i < recv_pkt->buffer_count; i++)
4544 recv_buffer_free(recv_pkt->buffer_ptr[i]);
4545 octeon_free_recv_info(recv_info);
4546
4547 return 0;
4548}
4549
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004550/**
4551 * \brief Device initialization for each Octeon device that is probed
4552 * @param octeon_dev octeon device
4553 */
4554static int octeon_device_init(struct octeon_device *octeon_dev)
4555{
4556 int j, ret;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004557 int fw_loaded = 0;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004558 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004559 struct octeon_device_priv *oct_priv =
4560 (struct octeon_device_priv *)octeon_dev->priv;
4561 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4562
4563 /* Enable access to the octeon device and make its DMA capability
4564 * known to the OS.
4565 */
4566 if (octeon_pci_os_setup(octeon_dev))
4567 return 1;
4568
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004569 atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4570
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004571 /* Identify the Octeon type and map the BAR address space. */
4572 if (octeon_chip_specific_setup(octeon_dev)) {
4573 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4574 return 1;
4575 }
4576
4577 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4578
Rick Farringtone1e3ce62017-05-16 11:14:50 -07004579 /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
4580 * since that is what is required for the reference to be removed
4581 * during de-initialization (see 'octeon_destroy_resources').
4582 */
4583 octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
4584 PCI_SLOT(octeon_dev->pci_dev->devfn),
4585 PCI_FUNC(octeon_dev->pci_dev->devfn),
4586 true);
4587
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004588 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4589
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004590 if (OCTEON_CN23XX_PF(octeon_dev)) {
4591 if (!cn23xx_fw_loaded(octeon_dev)) {
4592 fw_loaded = 0;
Felix Manlunas7cc61db2017-03-23 13:26:28 -07004593 if (!fw_type_is_none()) {
4594 /* Do a soft reset of the Octeon device. */
4595 if (octeon_dev->fn_list.soft_reset(octeon_dev))
4596 return 1;
4597 /* things might have changed */
4598 if (!cn23xx_fw_loaded(octeon_dev))
4599 fw_loaded = 0;
4600 else
4601 fw_loaded = 1;
4602 }
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004603 } else {
4604 fw_loaded = 1;
4605 }
4606 } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004607 return 1;
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004608 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004609
4610 /* Initialize the dispatch mechanism used to push packets arriving on
4611 * Octeon Output queues.
4612 */
4613 if (octeon_init_dispatch_list(octeon_dev))
4614 return 1;
4615
4616 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4617 OPCODE_NIC_CORE_DRV_ACTIVE,
4618 octeon_core_drv_init,
4619 octeon_dev);
4620
Raghu Vatsavayi86dea552016-11-14 15:54:43 -08004621 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4622 OPCODE_NIC_VF_DRV_NOTICE,
4623 octeon_recv_vf_drv_notice, octeon_dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004624 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4625 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4626 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4627 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4628
4629 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4630
Raghu Vatsavayic865cdf2016-11-28 16:54:36 -08004631 if (octeon_set_io_queues_off(octeon_dev)) {
4632 dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4633 return 1;
4634 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004635
Raghu Vatsavayi3451b972016-08-31 11:03:26 -07004636 if (OCTEON_CN23XX_PF(octeon_dev)) {
4637 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4638 if (ret) {
4639 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4640 return ret;
4641 }
4642 }
4643
4644 /* Initialize soft command buffer pool
4645 */
4646 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4647 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4648 return 1;
4649 }
4650 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4651
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004652 /* Setup the data structures that manage this Octeon's Input queues. */
4653 if (octeon_setup_instr_queues(octeon_dev)) {
4654 dev_err(&octeon_dev->pci_dev->dev,
4655 "instruction queue initialization failed\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004656 return 1;
4657 }
4658 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4659
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004660 /* Initialize lists to manage the requests of different types that
4661 * arrive from user & kernel applications for this octeon device.
4662 */
4663 if (octeon_setup_response_list(octeon_dev)) {
4664 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4665 return 1;
4666 }
4667 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4668
4669 if (octeon_setup_output_queues(octeon_dev)) {
4670 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004671 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004672 }
4673
4674 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4675
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004676 if (OCTEON_CN23XX_PF(octeon_dev)) {
Raghu Vatsavayi5d655562016-11-14 15:54:42 -08004677 if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4678 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4679 return 1;
4680 }
4681 atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4682
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004683 if (octeon_allocate_ioq_vector(octeon_dev)) {
4684 dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4685 return 1;
4686 }
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004687 atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004688
4689 } else {
4690 /* The input and output queue registers were setup earlier (the
4691 * queues were not enabled). Any additional registers
4692 * that need to be programmed should be done now.
4693 */
4694 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4695 if (ret) {
4696 dev_err(&octeon_dev->pci_dev->dev,
4697 "Failed to configure device registers\n");
4698 return ret;
4699 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004700 }
4701
4702 /* Initialize the tasklet that handles output queue packet processing.*/
4703 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4704 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4705 (unsigned long)octeon_dev);
4706
4707 /* Setup the interrupt handler and record the INT SUM register address
4708 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07004709 if (octeon_setup_interrupt(octeon_dev))
4710 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004711
4712 /* Enable Octeon device interrupts */
Raghu Vatsavayi5b07aee2016-08-31 11:03:28 -07004713 octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004714
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08004715 atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4716
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004717 /* Enable the input and output queues for this Octeon device */
Raghu Vatsavayi1b7c55c2016-08-31 11:03:27 -07004718 ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4719 if (ret) {
4720 dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4721 return ret;
4722 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004723
4724 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4725
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004726 if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
4727 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4728 if (!ddr_timeout) {
4729 dev_info(&octeon_dev->pci_dev->dev,
4730 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4731 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004732
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004733 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004734
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004735 /* Wait for the octeon to initialize DDR after the soft-reset.*/
4736 while (!ddr_timeout) {
4737 set_current_state(TASK_INTERRUPTIBLE);
4738 if (schedule_timeout(HZ / 10)) {
4739 /* user probably pressed Control-C */
4740 return 1;
4741 }
4742 }
4743 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4744 if (ret) {
4745 dev_err(&octeon_dev->pci_dev->dev,
4746 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4747 ret);
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07004748 return 1;
4749 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004750
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004751 if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4752 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4753 return 1;
4754 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004755
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004756 /* Divert uboot to take commands from host instead. */
4757 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07004758
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004759 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4760 ret = octeon_init_consoles(octeon_dev);
4761 if (ret) {
4762 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4763 return 1;
4764 }
4765 ret = octeon_add_console(octeon_dev, 0);
4766 if (ret) {
4767 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4768 return 1;
4769 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004770
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004771 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004772
Raghu Vatsavayic0eab5b2016-08-31 11:03:29 -07004773 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4774 ret = load_firmware(octeon_dev);
4775 if (ret) {
4776 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4777 return 1;
4778 }
4779 /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
4780 * loaded
4781 */
4782 if (OCTEON_CN23XX_PF(octeon_dev))
4783 octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
4784 2ULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004785 }
4786
4787 handshake[octeon_dev->octeon_id].init_ok = 1;
4788 complete(&handshake[octeon_dev->octeon_id].init);
4789
4790 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4791
4792 /* Send Credit for Octeon Output queues. Credits are always sent after
4793 * the output queue is enabled.
4794 */
4795 for (j = 0; j < octeon_dev->num_oqs; j++)
4796 writel(octeon_dev->droq[j]->max_count,
4797 octeon_dev->droq[j]->pkts_credit_reg);
4798
4799 /* Packets can start arriving on the output queues from this point. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07004800 return 0;
4801}
4802
4803/**
4804 * \brief Exits the module
4805 */
4806static void __exit liquidio_exit(void)
4807{
4808 liquidio_deinit_pci();
4809
4810 pr_info("LiquidIO network module is now unloaded\n");
4811}
4812
4813module_init(liquidio_init);
4814module_exit(liquidio_exit);