blob: 8f11a0b4a7adb58ce29a38cda9936aced7bae16f [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2* Author: Cavium, Inc.
3*
4* Contact: support@cavium.com
5* Please include "LiquidIO" in the subject.
6*
7* Copyright (c) 2003-2015 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
17* details.
18*
19* This file may also be available under a different license from Cavium.
20* Contact Cavium, Inc. for more information
21**********************************************************************/
22#include <linux/version.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070023#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070024#include <linux/firmware.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070025#include <linux/ptp_clock_kernel.h>
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -070026#include <net/vxlan.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070027#include "liquidio_common.h"
28#include "octeon_droq.h"
29#include "octeon_iq.h"
30#include "response_manager.h"
31#include "octeon_device.h"
32#include "octeon_nic.h"
33#include "octeon_main.h"
34#include "octeon_network.h"
35#include "cn66xx_regs.h"
36#include "cn66xx_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070037#include "cn68xx_device.h"
38#include "liquidio_image.h"
39
40MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
41MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
42MODULE_LICENSE("GPL");
43MODULE_VERSION(LIQUIDIO_VERSION);
44MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
45MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
46MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
47
48static int ddr_timeout = 10000;
49module_param(ddr_timeout, int, 0644);
50MODULE_PARM_DESC(ddr_timeout,
51 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
52
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070053#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
54
Raghu Vatsavayi1f164712016-06-21 22:53:11 -070055#define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \
56 (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count)
57
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070058static 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
66static int conf_type;
67module_param(conf_type, int, 0);
68MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
69
Raghu Vatsavayia5b37882016-06-14 16:54:48 -070070static int ptp_enable = 1;
71
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070072/* Bit mask values for lio->ifstate */
73#define LIO_IFSTATE_DROQ_OPS 0x01
74#define LIO_IFSTATE_REGISTERED 0x02
75#define LIO_IFSTATE_RUNNING 0x04
76#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
77
78/* Polling interval for determining when NIC application is alive */
79#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
80
81/* runtime link query interval */
82#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
83
84struct liquidio_if_cfg_context {
85 int octeon_id;
86
87 wait_queue_head_t wc;
88
89 int cond;
90};
91
92struct liquidio_if_cfg_resp {
93 u64 rh;
94 struct liquidio_if_cfg_info cfg_info;
95 u64 status;
96};
97
98struct oct_link_status_resp {
99 u64 rh;
100 struct oct_link_info link_info;
101 u64 status;
102};
103
104struct oct_timestamp_resp {
105 u64 rh;
106 u64 timestamp;
107 u64 status;
108};
109
110#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
111
112union tx_info {
113 u64 u64;
114 struct {
115#ifdef __BIG_ENDIAN_BITFIELD
116 u16 gso_size;
117 u16 gso_segs;
118 u32 reserved;
119#else
120 u32 reserved;
121 u16 gso_segs;
122 u16 gso_size;
123#endif
124 } s;
125};
126
127/** Octeon device properties to be used by the NIC module.
128 * Each octeon device in the system will be represented
129 * by this structure in the NIC module.
130 */
131
132#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
133
134#define OCTNIC_GSO_MAX_HEADER_SIZE 128
135#define OCTNIC_GSO_MAX_SIZE (GSO_MAX_SIZE - OCTNIC_GSO_MAX_HEADER_SIZE)
136
137/** Structure of a node in list of gather components maintained by
138 * NIC driver for each network device.
139 */
140struct octnic_gather {
141 /** List manipulation. Next and prev pointers. */
142 struct list_head list;
143
144 /** Size of the gather component at sg in bytes. */
145 int sg_size;
146
147 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
148 int adjust;
149
150 /** Gather component that can accommodate max sized fragment list
151 * received from the IP layer.
152 */
153 struct octeon_sg_entry *sg;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700154
155 u64 sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700156};
157
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700158struct handshake {
159 struct completion init;
160 struct completion started;
161 struct pci_dev *pci_dev;
162 int init_ok;
163 int started_ok;
164};
165
166struct octeon_device_priv {
167 /** Tasklet structures for this device. */
168 struct tasklet_struct droq_tasklet;
169 unsigned long napi_mask;
170};
171
172static int octeon_device_init(struct octeon_device *);
Raghu Vatsavayi32581242016-08-31 11:03:20 -0700173static int liquidio_stop(struct net_device *netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700174static void liquidio_remove(struct pci_dev *pdev);
175static int liquidio_probe(struct pci_dev *pdev,
176 const struct pci_device_id *ent);
177
178static struct handshake handshake[MAX_OCTEON_DEVICES];
179static struct completion first_stage;
180
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700181static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700182{
183 int q_no;
184 int reschedule = 0;
185 struct octeon_device *oct = (struct octeon_device *)pdev;
186 struct octeon_device_priv *oct_priv =
187 (struct octeon_device_priv *)oct->priv;
188
189 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700190 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
191 if (!(oct->io_qmask.oq & (1ULL << q_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700192 continue;
193 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
194 MAX_PACKET_BUDGET);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700195 lio_enable_irq(oct->droq[q_no], NULL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700196 }
197
198 if (reschedule)
199 tasklet_schedule(&oct_priv->droq_tasklet);
200}
201
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700202static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700203{
204 struct octeon_device_priv *oct_priv =
205 (struct octeon_device_priv *)oct->priv;
206 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
207 int i;
208
209 do {
210 pending_pkts = 0;
211
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700212 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
213 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700214 continue;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700215 pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700216 }
217 if (pkt_cnt > 0) {
218 pending_pkts += pkt_cnt;
219 tasklet_schedule(&oct_priv->droq_tasklet);
220 }
221 pkt_cnt = 0;
222 schedule_timeout_uninterruptible(1);
223
224 } while (retry-- && pending_pkts);
225
226 return pkt_cnt;
227}
228
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700229/**
230 * \brief Forces all IO queues off on a given device
231 * @param oct Pointer to Octeon device
232 */
233static void force_io_queues_off(struct octeon_device *oct)
234{
235 if ((oct->chip_id == OCTEON_CN66XX) ||
236 (oct->chip_id == OCTEON_CN68XX)) {
237 /* Reset the Enable bits for Input Queues. */
238 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
239
240 /* Reset the Enable bits for Output Queues. */
241 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
242 }
243}
244
245/**
246 * \brief wait for all pending requests to complete
247 * @param oct Pointer to Octeon device
248 *
249 * Called during shutdown sequence
250 */
251static int wait_for_pending_requests(struct octeon_device *oct)
252{
253 int i, pcount = 0;
254
255 for (i = 0; i < 100; i++) {
256 pcount =
257 atomic_read(&oct->response_list
258 [OCTEON_ORDERED_SC_LIST].pending_req_count);
259 if (pcount)
260 schedule_timeout_uninterruptible(HZ / 10);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700261 else
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700262 break;
263 }
264
265 if (pcount)
266 return 1;
267
268 return 0;
269}
270
271/**
272 * \brief Cause device to go quiet so it can be safely removed/reset/etc
273 * @param oct Pointer to Octeon device
274 */
275static inline void pcierror_quiesce_device(struct octeon_device *oct)
276{
277 int i;
278
279 /* Disable the input and output queues now. No more packets will
280 * arrive from Octeon, but we should wait for all packet processing
281 * to finish.
282 */
283 force_io_queues_off(oct);
284
285 /* To allow for in-flight requests */
286 schedule_timeout_uninterruptible(100);
287
288 if (wait_for_pending_requests(oct))
289 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
290
291 /* Force all requests waiting to be fetched by OCTEON to complete. */
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700292 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700293 struct octeon_instr_queue *iq;
294
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700295 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700296 continue;
297 iq = oct->instr_queue[i];
298
299 if (atomic_read(&iq->instr_pending)) {
300 spin_lock_bh(&iq->lock);
301 iq->fill_cnt = 0;
302 iq->octeon_read_index = iq->host_write_index;
303 iq->stats.instr_processed +=
304 atomic_read(&iq->instr_pending);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700305 lio_process_iq_request_list(oct, iq, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700306 spin_unlock_bh(&iq->lock);
307 }
308 }
309
310 /* Force all pending ordered list requests to time out. */
311 lio_process_ordered_list(oct, 1);
312
313 /* We do not need to wait for output queue packets to be processed. */
314}
315
316/**
317 * \brief Cleanup PCI AER uncorrectable error status
318 * @param dev Pointer to PCI device
319 */
320static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
321{
322 int pos = 0x100;
323 u32 status, mask;
324
325 pr_info("%s :\n", __func__);
326
327 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
328 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
329 if (dev->error_state == pci_channel_io_normal)
330 status &= ~mask; /* Clear corresponding nonfatal bits */
331 else
332 status &= mask; /* Clear corresponding fatal bits */
333 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
334}
335
336/**
337 * \brief Stop all PCI IO to a given device
338 * @param dev Pointer to Octeon device
339 */
340static void stop_pci_io(struct octeon_device *oct)
341{
342 /* No more instructions will be forwarded. */
343 atomic_set(&oct->status, OCT_DEV_IN_RESET);
344
345 pci_disable_device(oct->pci_dev);
346
347 /* Disable interrupts */
348 oct->fn_list.disable_interrupt(oct->chip);
349
350 pcierror_quiesce_device(oct);
351
352 /* Release the interrupt line */
353 free_irq(oct->pci_dev->irq, oct);
354
355 if (oct->flags & LIO_FLAG_MSI_ENABLED)
356 pci_disable_msi(oct->pci_dev);
357
358 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
359 lio_get_state_string(&oct->status));
360
361 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
362 /* making it a common function for all OCTEON models */
363 cleanup_aer_uncorrect_error_status(oct->pci_dev);
364}
365
366/**
367 * \brief called when PCI error is detected
368 * @param pdev Pointer to PCI device
369 * @param state The current pci connection state
370 *
371 * This function is called after a PCI bus error affecting
372 * this device has been detected.
373 */
374static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
375 pci_channel_state_t state)
376{
377 struct octeon_device *oct = pci_get_drvdata(pdev);
378
379 /* Non-correctable Non-fatal errors */
380 if (state == pci_channel_io_normal) {
381 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
382 cleanup_aer_uncorrect_error_status(oct->pci_dev);
383 return PCI_ERS_RESULT_CAN_RECOVER;
384 }
385
386 /* Non-correctable Fatal errors */
387 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
388 stop_pci_io(oct);
389
390 /* Always return a DISCONNECT. There is no support for recovery but only
391 * for a clean shutdown.
392 */
393 return PCI_ERS_RESULT_DISCONNECT;
394}
395
396/**
397 * \brief mmio handler
398 * @param pdev Pointer to PCI device
399 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700400static pci_ers_result_t liquidio_pcie_mmio_enabled(
401 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700402{
403 /* We should never hit this since we never ask for a reset for a Fatal
404 * Error. We always return DISCONNECT in io_error above.
405 * But play safe and return RECOVERED for now.
406 */
407 return PCI_ERS_RESULT_RECOVERED;
408}
409
410/**
411 * \brief called after the pci bus has been reset.
412 * @param pdev Pointer to PCI device
413 *
414 * Restart the card from scratch, as if from a cold-boot. Implementation
415 * resembles the first-half of the octeon_resume routine.
416 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700417static pci_ers_result_t liquidio_pcie_slot_reset(
418 struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700419{
420 /* We should never hit this since we never ask for a reset for a Fatal
421 * Error. We always return DISCONNECT in io_error above.
422 * But play safe and return RECOVERED for now.
423 */
424 return PCI_ERS_RESULT_RECOVERED;
425}
426
427/**
428 * \brief called when traffic can start flowing again.
429 * @param pdev Pointer to PCI device
430 *
431 * This callback is called when the error recovery driver tells us that
432 * its OK to resume normal operation. Implementation resembles the
433 * second-half of the octeon_resume routine.
434 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700435static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700436{
437 /* Nothing to be done here. */
438}
439
440#ifdef CONFIG_PM
441/**
442 * \brief called when suspending
443 * @param pdev Pointer to PCI device
444 * @param state state to suspend to
445 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700446static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
447 pm_message_t state __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700448{
449 return 0;
450}
451
452/**
453 * \brief called when resuming
454 * @param pdev Pointer to PCI device
455 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -0700456static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700457{
458 return 0;
459}
460#endif
461
462/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100463static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700464 .error_detected = liquidio_pcie_error_detected,
465 .mmio_enabled = liquidio_pcie_mmio_enabled,
466 .slot_reset = liquidio_pcie_slot_reset,
467 .resume = liquidio_pcie_resume,
468};
469
470static const struct pci_device_id liquidio_pci_tbl[] = {
471 { /* 68xx */
472 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
473 },
474 { /* 66xx */
475 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
476 },
477 {
478 0, 0, 0, 0, 0, 0, 0
479 }
480};
481MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
482
483static struct pci_driver liquidio_pci_driver = {
484 .name = "LiquidIO",
485 .id_table = liquidio_pci_tbl,
486 .probe = liquidio_probe,
487 .remove = liquidio_remove,
488 .err_handler = &liquidio_err_handler, /* For AER */
489
490#ifdef CONFIG_PM
491 .suspend = liquidio_suspend,
492 .resume = liquidio_resume,
493#endif
494
495};
496
497/**
498 * \brief register PCI driver
499 */
500static int liquidio_init_pci(void)
501{
502 return pci_register_driver(&liquidio_pci_driver);
503}
504
505/**
506 * \brief unregister PCI driver
507 */
508static void liquidio_deinit_pci(void)
509{
510 pci_unregister_driver(&liquidio_pci_driver);
511}
512
513/**
514 * \brief check interface state
515 * @param lio per-network private data
516 * @param state_flag flag state to check
517 */
518static inline int ifstate_check(struct lio *lio, int state_flag)
519{
520 return atomic_read(&lio->ifstate) & state_flag;
521}
522
523/**
524 * \brief set interface state
525 * @param lio per-network private data
526 * @param state_flag flag state to set
527 */
528static inline void ifstate_set(struct lio *lio, int state_flag)
529{
530 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
531}
532
533/**
534 * \brief clear interface state
535 * @param lio per-network private data
536 * @param state_flag flag state to clear
537 */
538static inline void ifstate_reset(struct lio *lio, int state_flag)
539{
540 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
541}
542
543/**
544 * \brief Stop Tx queues
545 * @param netdev network device
546 */
547static inline void txqs_stop(struct net_device *netdev)
548{
549 if (netif_is_multiqueue(netdev)) {
550 int i;
551
552 for (i = 0; i < netdev->num_tx_queues; i++)
553 netif_stop_subqueue(netdev, i);
554 } else {
555 netif_stop_queue(netdev);
556 }
557}
558
559/**
560 * \brief Start Tx queues
561 * @param netdev network device
562 */
563static inline void txqs_start(struct net_device *netdev)
564{
565 if (netif_is_multiqueue(netdev)) {
566 int i;
567
568 for (i = 0; i < netdev->num_tx_queues; i++)
569 netif_start_subqueue(netdev, i);
570 } else {
571 netif_start_queue(netdev);
572 }
573}
574
575/**
576 * \brief Wake Tx queues
577 * @param netdev network device
578 */
579static inline void txqs_wake(struct net_device *netdev)
580{
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700581 struct lio *lio = GET_LIO(netdev);
582
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700583 if (netif_is_multiqueue(netdev)) {
584 int i;
585
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700586 for (i = 0; i < netdev->num_tx_queues; i++) {
587 int qno = lio->linfo.txpciq[i %
588 (lio->linfo.num_txpciq)].s.q_no;
589
590 if (__netif_subqueue_stopped(netdev, i)) {
591 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
592 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700593 netif_wake_subqueue(netdev, i);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700594 }
595 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700596 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700597 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
598 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700599 netif_wake_queue(netdev);
600 }
601}
602
603/**
604 * \brief Stop Tx queue
605 * @param netdev network device
606 */
607static void stop_txq(struct net_device *netdev)
608{
609 txqs_stop(netdev);
610}
611
612/**
613 * \brief Start Tx queue
614 * @param netdev network device
615 */
616static void start_txq(struct net_device *netdev)
617{
618 struct lio *lio = GET_LIO(netdev);
619
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700620 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700621 txqs_start(netdev);
622 return;
623 }
624}
625
626/**
627 * \brief Wake a queue
628 * @param netdev network device
629 * @param q which queue to wake
630 */
631static inline void wake_q(struct net_device *netdev, int q)
632{
633 if (netif_is_multiqueue(netdev))
634 netif_wake_subqueue(netdev, q);
635 else
636 netif_wake_queue(netdev);
637}
638
639/**
640 * \brief Stop a queue
641 * @param netdev network device
642 * @param q which queue to stop
643 */
644static inline void stop_q(struct net_device *netdev, int q)
645{
646 if (netif_is_multiqueue(netdev))
647 netif_stop_subqueue(netdev, q);
648 else
649 netif_stop_queue(netdev);
650}
651
652/**
653 * \brief Check Tx queue status, and take appropriate action
654 * @param lio per-network private data
655 * @returns 0 if full, number of queues woken up otherwise
656 */
657static inline int check_txq_status(struct lio *lio)
658{
659 int ret_val = 0;
660
661 if (netif_is_multiqueue(lio->netdev)) {
662 int numqs = lio->netdev->num_tx_queues;
663 int q, iq = 0;
664
665 /* check each sub-queue state */
666 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700667 iq = lio->linfo.txpciq[q %
668 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700669 if (octnet_iq_is_full(lio->oct_dev, iq))
670 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700671 if (__netif_subqueue_stopped(lio->netdev, q)) {
672 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700673 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
674 tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700675 ret_val++;
676 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700677 }
678 } else {
679 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
680 return 0;
681 wake_q(lio->netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700682 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
683 tx_restart, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700684 ret_val = 1;
685 }
686 return ret_val;
687}
688
689/**
690 * Remove the node at the head of the list. The list would be empty at
691 * the end of this call if there are no more nodes in the list.
692 */
693static inline struct list_head *list_delete_head(struct list_head *root)
694{
695 struct list_head *node;
696
697 if ((root->prev == root) && (root->next == root))
698 node = NULL;
699 else
700 node = root->next;
701
702 if (node)
703 list_del(node);
704
705 return node;
706}
707
708/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700709 * \brief Delete gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700710 * @param lio per-network private data
711 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700712static void delete_glists(struct lio *lio)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700713{
714 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700715 int i;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700716
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700717 if (!lio->glist)
718 return;
719
720 for (i = 0; i < lio->linfo.num_txpciq; i++) {
721 do {
722 g = (struct octnic_gather *)
723 list_delete_head(&lio->glist[i]);
724 if (g) {
725 if (g->sg) {
726 dma_unmap_single(&lio->oct_dev->
727 pci_dev->dev,
728 g->sg_dma_ptr,
729 g->sg_size,
730 DMA_TO_DEVICE);
731 kfree((void *)((unsigned long)g->sg -
732 g->adjust));
733 }
734 kfree(g);
735 }
736 } while (g);
737 }
738
739 kfree((void *)lio->glist);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700740}
741
742/**
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700743 * \brief Setup gather lists
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700744 * @param lio per-network private data
745 */
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700746static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700747{
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700748 int i, j;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700749 struct octnic_gather *g;
750
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700751 lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
752 GFP_KERNEL);
753 if (!lio->glist_lock)
754 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700755
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700756 lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
757 GFP_KERNEL);
758 if (!lio->glist) {
759 kfree((void *)lio->glist_lock);
760 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700761 }
762
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700763 for (i = 0; i < num_iqs; i++) {
764 int numa_node = cpu_to_node(i % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700765
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -0700766 spin_lock_init(&lio->glist_lock[i]);
767
768 INIT_LIST_HEAD(&lio->glist[i]);
769
770 for (j = 0; j < lio->tx_qsize; j++) {
771 g = kzalloc_node(sizeof(*g), GFP_KERNEL,
772 numa_node);
773 if (!g)
774 g = kzalloc(sizeof(*g), GFP_KERNEL);
775 if (!g)
776 break;
777
778 g->sg_size = ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) *
779 OCT_SG_ENTRY_SIZE);
780
781 g->sg = kmalloc_node(g->sg_size + 8,
782 GFP_KERNEL, numa_node);
783 if (!g->sg)
784 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
785 if (!g->sg) {
786 kfree(g);
787 break;
788 }
789
790 /* The gather component should be aligned on 64-bit
791 * boundary
792 */
793 if (((unsigned long)g->sg) & 7) {
794 g->adjust = 8 - (((unsigned long)g->sg) & 7);
795 g->sg = (struct octeon_sg_entry *)
796 ((unsigned long)g->sg + g->adjust);
797 }
798 g->sg_dma_ptr = dma_map_single(&oct->pci_dev->dev,
799 g->sg, g->sg_size,
800 DMA_TO_DEVICE);
801 if (dma_mapping_error(&oct->pci_dev->dev,
802 g->sg_dma_ptr)) {
803 kfree((void *)((unsigned long)g->sg -
804 g->adjust));
805 kfree(g);
806 break;
807 }
808
809 list_add_tail(&g->list, &lio->glist[i]);
810 }
811
812 if (j != lio->tx_qsize) {
813 delete_glists(lio);
814 return 1;
815 }
816 }
817
818 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700819}
820
821/**
822 * \brief Print link information
823 * @param netdev network device
824 */
825static void print_link_info(struct net_device *netdev)
826{
827 struct lio *lio = GET_LIO(netdev);
828
829 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
830 struct oct_link_info *linfo = &lio->linfo;
831
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700832 if (linfo->link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700833 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
834 linfo->link.s.speed,
835 (linfo->link.s.duplex) ? "Full" : "Half");
836 } else {
837 netif_info(lio, link, lio->netdev, "Link Down\n");
838 }
839 }
840}
841
842/**
843 * \brief Update link status
844 * @param netdev network device
845 * @param ls link status structure
846 *
847 * Called on receipt of a link status response from the core application to
848 * update each interface's link status.
849 */
850static inline void update_link_status(struct net_device *netdev,
851 union oct_link_status *ls)
852{
853 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700854 int changed = (lio->linfo.link.u64 != ls->u64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700855
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700856 lio->linfo.link.u64 = ls->u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700857
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700858 if ((lio->intf_open) && (changed)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700859 print_link_info(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700860 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700861
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700862 if (lio->linfo.link.s.link_up) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700863 netif_carrier_on(netdev);
864 /* start_txq(netdev); */
865 txqs_wake(netdev);
866 } else {
867 netif_carrier_off(netdev);
868 stop_txq(netdev);
869 }
870 }
871}
872
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700873/* Runs in interrupt context. */
874static void update_txq_status(struct octeon_device *oct, int iq_num)
875{
876 struct net_device *netdev;
877 struct lio *lio;
878 struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
879
880 /*octeon_update_iq_read_idx(oct, iq);*/
881
882 netdev = oct->props[iq->ifidx].netdev;
883
884 /* This is needed because the first IQ does not have
885 * a netdev associated with it.
886 */
887 if (!netdev)
888 return;
889
890 lio = GET_LIO(netdev);
891 if (netif_is_multiqueue(netdev)) {
892 if (__netif_subqueue_stopped(netdev, iq->q_index) &&
893 lio->linfo.link.s.link_up &&
894 (!octnet_iq_is_full(oct, iq_num))) {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700895 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
896 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700897 netif_wake_subqueue(netdev, iq->q_index);
898 } else {
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700899 if (!octnet_iq_is_full(oct, lio->txq)) {
900 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
901 lio->txq,
902 tx_restart, 1);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700903 wake_q(netdev, lio->txq);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -0700904 }
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -0700905 }
906 }
907}
908
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700909/**
910 * \brief Droq packet processor sceduler
911 * @param oct octeon device
912 */
913static
914void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
915{
916 struct octeon_device_priv *oct_priv =
917 (struct octeon_device_priv *)oct->priv;
918 u64 oq_no;
919 struct octeon_droq *droq;
920
921 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
Raghu Vatsavayi63da8402016-06-21 22:53:03 -0700922 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
923 oq_no++) {
924 if (!(oct->droq_intr & (1ULL << oq_no)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700925 continue;
926
927 droq = oct->droq[oq_no];
928
929 if (droq->ops.poll_mode) {
930 droq->ops.napi_fn(droq);
931 oct_priv->napi_mask |= (1 << oq_no);
932 } else {
933 tasklet_schedule(&oct_priv->droq_tasklet);
934 }
935 }
936 }
937}
938
939/**
940 * \brief Interrupt handler for octeon
941 * @param irq unused
942 * @param dev octeon device
943 */
944static
945irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
946{
947 struct octeon_device *oct = (struct octeon_device *)dev;
948 irqreturn_t ret;
949
950 /* Disable our interrupts for the duration of ISR */
951 oct->fn_list.disable_interrupt(oct->chip);
952
953 ret = oct->fn_list.process_interrupt_regs(oct);
954
955 if (ret == IRQ_HANDLED)
956 liquidio_schedule_droq_pkt_handlers(oct);
957
958 /* Re-enable our interrupts */
959 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
960 oct->fn_list.enable_interrupt(oct->chip);
961
962 return ret;
963}
964
965/**
966 * \brief Setup interrupt for octeon device
967 * @param oct octeon device
968 *
969 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
970 */
971static int octeon_setup_interrupt(struct octeon_device *oct)
972{
973 int irqret, err;
974
975 err = pci_enable_msi(oct->pci_dev);
976 if (err)
977 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
978 err);
979 else
980 oct->flags |= LIO_FLAG_MSI_ENABLED;
981
982 irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
983 IRQF_SHARED, "octeon", oct);
984 if (irqret) {
985 if (oct->flags & LIO_FLAG_MSI_ENABLED)
986 pci_disable_msi(oct->pci_dev);
987 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
988 irqret);
989 return 1;
990 }
991
992 return 0;
993}
994
995/**
996 * \brief PCI probe handler
997 * @param pdev PCI device structure
998 * @param ent unused
999 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001000static int
1001liquidio_probe(struct pci_dev *pdev,
1002 const struct pci_device_id *ent __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001003{
1004 struct octeon_device *oct_dev = NULL;
1005 struct handshake *hs;
1006
1007 oct_dev = octeon_allocate_device(pdev->device,
1008 sizeof(struct octeon_device_priv));
1009 if (!oct_dev) {
1010 dev_err(&pdev->dev, "Unable to allocate device\n");
1011 return -ENOMEM;
1012 }
1013
1014 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1015 (u32)pdev->vendor, (u32)pdev->device);
1016
1017 /* Assign octeon_device for this device to the private data area. */
1018 pci_set_drvdata(pdev, oct_dev);
1019
1020 /* set linux specific device pointer */
1021 oct_dev->pci_dev = (void *)pdev;
1022
1023 hs = &handshake[oct_dev->octeon_id];
1024 init_completion(&hs->init);
1025 init_completion(&hs->started);
1026 hs->pci_dev = pdev;
1027
1028 if (oct_dev->octeon_id == 0)
1029 /* first LiquidIO NIC is detected */
1030 complete(&first_stage);
1031
1032 if (octeon_device_init(oct_dev)) {
1033 liquidio_remove(pdev);
1034 return -ENOMEM;
1035 }
1036
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001037 oct_dev->rx_pause = 1;
1038 oct_dev->tx_pause = 1;
1039
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001040 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1041
1042 return 0;
1043}
1044
1045/**
1046 *\brief Destroy resources associated with octeon device
1047 * @param pdev PCI device structure
1048 * @param ent unused
1049 */
1050static void octeon_destroy_resources(struct octeon_device *oct)
1051{
1052 int i;
1053 struct octeon_device_priv *oct_priv =
1054 (struct octeon_device_priv *)oct->priv;
1055
1056 struct handshake *hs;
1057
1058 switch (atomic_read(&oct->status)) {
1059 case OCT_DEV_RUNNING:
1060 case OCT_DEV_CORE_OK:
1061
1062 /* No more instructions will be forwarded. */
1063 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1064
1065 oct->app_mode = CVM_DRV_INVALID_APP;
1066 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1067 lio_get_state_string(&oct->status));
1068
1069 schedule_timeout_uninterruptible(HZ / 10);
1070
1071 /* fallthrough */
1072 case OCT_DEV_HOST_OK:
1073
1074 /* fallthrough */
1075 case OCT_DEV_CONSOLE_INIT_DONE:
1076 /* Remove any consoles */
1077 octeon_remove_consoles(oct);
1078
1079 /* fallthrough */
1080 case OCT_DEV_IO_QUEUES_DONE:
1081 if (wait_for_pending_requests(oct))
1082 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1083
1084 if (lio_wait_for_instr_fetch(oct))
1085 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1086
1087 /* Disable the input and output queues now. No more packets will
1088 * arrive from Octeon, but we should wait for all packet
1089 * processing to finish.
1090 */
1091 oct->fn_list.disable_io_queues(oct);
1092
1093 if (lio_wait_for_oq_pkts(oct))
1094 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1095
1096 /* Disable interrupts */
1097 oct->fn_list.disable_interrupt(oct->chip);
1098
1099 /* Release the interrupt line */
1100 free_irq(oct->pci_dev->irq, oct);
1101
1102 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1103 pci_disable_msi(oct->pci_dev);
1104
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001105 /* fallthrough */
1106 case OCT_DEV_IN_RESET:
1107 case OCT_DEV_DROQ_INIT_DONE:
1108 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1109 mdelay(100);
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001110 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
1111 if (!(oct->io_qmask.oq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001112 continue;
1113 octeon_delete_droq(oct, i);
1114 }
1115
1116 /* Force any pending handshakes to complete */
1117 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1118 hs = &handshake[i];
1119
1120 if (hs->pci_dev) {
1121 handshake[oct->octeon_id].init_ok = 0;
1122 complete(&handshake[oct->octeon_id].init);
1123 handshake[oct->octeon_id].started_ok = 0;
1124 complete(&handshake[oct->octeon_id].started);
1125 }
1126 }
1127
1128 /* fallthrough */
1129 case OCT_DEV_RESP_LIST_INIT_DONE:
1130 octeon_delete_response_list(oct);
1131
1132 /* fallthrough */
1133 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1134 octeon_free_sc_buffer_pool(oct);
1135
1136 /* fallthrough */
1137 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
Raghu Vatsavayi63da8402016-06-21 22:53:03 -07001138 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
1139 if (!(oct->io_qmask.iq & (1ULL << i)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001140 continue;
1141 octeon_delete_instr_queue(oct, i);
1142 }
1143
1144 /* fallthrough */
1145 case OCT_DEV_DISPATCH_INIT_DONE:
1146 octeon_delete_dispatch_list(oct);
1147 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1148
1149 /* fallthrough */
1150 case OCT_DEV_PCI_MAP_DONE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001151
1152 /* Soft reset the octeon device before exiting */
1153 oct->fn_list.soft_reset(oct);
1154
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001155 octeon_unmap_pci_barx(oct, 0);
1156 octeon_unmap_pci_barx(oct, 1);
1157
1158 /* fallthrough */
1159 case OCT_DEV_BEGIN_STATE:
Raghu Vatsavayi60b48c52016-06-21 22:53:09 -07001160 /* Disable the device, releasing the PCI INT */
1161 pci_disable_device(oct->pci_dev);
1162
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001163 /* Nothing to be done here either */
1164 break;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07001165 } /* end switch (oct->status) */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001166
1167 tasklet_kill(&oct_priv->droq_tasklet);
1168}
1169
1170/**
1171 * \brief Send Rx control command
1172 * @param lio per-network private data
1173 * @param start_stop whether to start or stop
1174 */
1175static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1176{
1177 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001178
1179 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1180
1181 nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001182 nctrl.ncmd.s.param1 = start_stop;
1183 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001184 nctrl.netpndev = (u64)lio->netdev;
1185
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001186 if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl) < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001187 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1188}
1189
1190/**
1191 * \brief Destroy NIC device interface
1192 * @param oct octeon device
1193 * @param ifidx which interface to destroy
1194 *
1195 * Cleanup associated with each interface for an Octeon device when NIC
1196 * module is being unloaded or if initialization fails during load.
1197 */
1198static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1199{
1200 struct net_device *netdev = oct->props[ifidx].netdev;
1201 struct lio *lio;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001202 struct napi_struct *napi, *n;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001203
1204 if (!netdev) {
1205 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1206 __func__, ifidx);
1207 return;
1208 }
1209
1210 lio = GET_LIO(netdev);
1211
1212 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1213
1214 send_rx_ctrl_cmd(lio, 0);
1215
1216 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1217 txqs_stop(netdev);
1218
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001219 if (oct->props[lio->ifidx].napi_enabled == 1) {
1220 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1221 napi_disable(napi);
1222
1223 oct->props[lio->ifidx].napi_enabled = 0;
1224 }
1225
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001226 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1227 unregister_netdev(netdev);
1228
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001229 delete_glists(lio);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001230
1231 free_netdev(netdev);
1232
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001233 oct->props[ifidx].gmxport = -1;
1234
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001235 oct->props[ifidx].netdev = NULL;
1236}
1237
1238/**
1239 * \brief Stop complete NIC functionality
1240 * @param oct octeon device
1241 */
1242static int liquidio_stop_nic_module(struct octeon_device *oct)
1243{
1244 int i, j;
1245 struct lio *lio;
1246
1247 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1248 if (!oct->ifcount) {
1249 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1250 return 1;
1251 }
1252
Raghu Vatsavayi60441882016-06-21 22:53:08 -07001253 spin_lock_bh(&oct->cmd_resp_wqlock);
1254 oct->cmd_resp_state = OCT_DRV_OFFLINE;
1255 spin_unlock_bh(&oct->cmd_resp_wqlock);
1256
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001257 for (i = 0; i < oct->ifcount; i++) {
1258 lio = GET_LIO(oct->props[i].netdev);
1259 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001260 octeon_unregister_droq_ops(oct,
1261 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001262 }
1263
1264 for (i = 0; i < oct->ifcount; i++)
1265 liquidio_destroy_nic_device(oct, i);
1266
1267 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1268 return 0;
1269}
1270
1271/**
1272 * \brief Cleans up resources at unload time
1273 * @param pdev PCI device structure
1274 */
1275static void liquidio_remove(struct pci_dev *pdev)
1276{
1277 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1278
1279 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1280
1281 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1282 liquidio_stop_nic_module(oct_dev);
1283
1284 /* Reset the octeon device and cleanup all memory allocated for
1285 * the octeon device by driver.
1286 */
1287 octeon_destroy_resources(oct_dev);
1288
1289 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1290
1291 /* This octeon device has been removed. Update the global
1292 * data structure to reflect this. Free the device structure.
1293 */
1294 octeon_free_device_mem(oct_dev);
1295}
1296
1297/**
1298 * \brief Identify the Octeon device and to map the BAR address space
1299 * @param oct octeon device
1300 */
1301static int octeon_chip_specific_setup(struct octeon_device *oct)
1302{
1303 u32 dev_id, rev_id;
1304 int ret = 1;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001305 char *s;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001306
1307 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1308 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1309 oct->rev_id = rev_id & 0xff;
1310
1311 switch (dev_id) {
1312 case OCTEON_CN68XX_PCIID:
1313 oct->chip_id = OCTEON_CN68XX;
1314 ret = lio_setup_cn68xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001315 s = "CN68XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001316 break;
1317
1318 case OCTEON_CN66XX_PCIID:
1319 oct->chip_id = OCTEON_CN66XX;
1320 ret = lio_setup_cn66xx_octeon_device(oct);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001321 s = "CN66XX";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001322 break;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001323
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001324 default:
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001325 s = "?";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001326 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1327 dev_id);
1328 }
1329
1330 if (!ret)
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001331 dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001332 OCTEON_MAJOR_REV(oct),
1333 OCTEON_MINOR_REV(oct),
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001334 octeon_get_conf(oct)->card_name,
1335 LIQUIDIO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001336
1337 return ret;
1338}
1339
1340/**
1341 * \brief PCI initialization for each Octeon device.
1342 * @param oct octeon device
1343 */
1344static int octeon_pci_os_setup(struct octeon_device *oct)
1345{
1346 /* setup PCI stuff first */
1347 if (pci_enable_device(oct->pci_dev)) {
1348 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1349 return 1;
1350 }
1351
1352 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1353 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1354 return 1;
1355 }
1356
1357 /* Enable PCI DMA Master. */
1358 pci_set_master(oct->pci_dev);
1359
1360 return 0;
1361}
1362
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001363static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
1364{
1365 int q = 0;
1366
1367 if (netif_is_multiqueue(lio->netdev))
1368 q = skb->queue_mapping % lio->linfo.num_txpciq;
1369
1370 return q;
1371}
1372
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001373/**
1374 * \brief Check Tx queue state for a given network buffer
1375 * @param lio per-network private data
1376 * @param skb network buffer
1377 */
1378static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1379{
1380 int q = 0, iq = 0;
1381
1382 if (netif_is_multiqueue(lio->netdev)) {
1383 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001384 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001385 } else {
1386 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001387 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001388 }
1389
1390 if (octnet_iq_is_full(lio->oct_dev, iq))
1391 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001392
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001393 if (__netif_subqueue_stopped(lio->netdev, q)) {
1394 INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001395 wake_q(lio->netdev, q);
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07001396 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001397 return 1;
1398}
1399
1400/**
1401 * \brief Unmap and free network buffer
1402 * @param buf buffer
1403 */
1404static void free_netbuf(void *buf)
1405{
1406 struct sk_buff *skb;
1407 struct octnet_buf_free_info *finfo;
1408 struct lio *lio;
1409
1410 finfo = (struct octnet_buf_free_info *)buf;
1411 skb = finfo->skb;
1412 lio = finfo->lio;
1413
1414 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1415 DMA_TO_DEVICE);
1416
1417 check_txq_state(lio, skb);
1418
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001419 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001420}
1421
1422/**
1423 * \brief Unmap and free gather buffer
1424 * @param buf buffer
1425 */
1426static void free_netsgbuf(void *buf)
1427{
1428 struct octnet_buf_free_info *finfo;
1429 struct sk_buff *skb;
1430 struct lio *lio;
1431 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001432 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001433
1434 finfo = (struct octnet_buf_free_info *)buf;
1435 skb = finfo->skb;
1436 lio = finfo->lio;
1437 g = finfo->g;
1438 frags = skb_shinfo(skb)->nr_frags;
1439
1440 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1441 g->sg[0].ptr[0], (skb->len - skb->data_len),
1442 DMA_TO_DEVICE);
1443
1444 i = 1;
1445 while (frags--) {
1446 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1447
1448 pci_unmap_page((lio->oct_dev)->pci_dev,
1449 g->sg[(i >> 2)].ptr[(i & 3)],
1450 frag->size, DMA_TO_DEVICE);
1451 i++;
1452 }
1453
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001454 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1455 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001456
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001457 iq = skb_iq(lio, skb);
1458 spin_lock(&lio->glist_lock[iq]);
1459 list_add_tail(&g->list, &lio->glist[iq]);
1460 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001461
1462 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1463
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001464 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001465}
1466
1467/**
1468 * \brief Unmap and free gather buffer with response
1469 * @param buf buffer
1470 */
1471static void free_netsgbuf_with_resp(void *buf)
1472{
1473 struct octeon_soft_command *sc;
1474 struct octnet_buf_free_info *finfo;
1475 struct sk_buff *skb;
1476 struct lio *lio;
1477 struct octnic_gather *g;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001478 int i, frags, iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001479
1480 sc = (struct octeon_soft_command *)buf;
1481 skb = (struct sk_buff *)sc->callback_arg;
1482 finfo = (struct octnet_buf_free_info *)&skb->cb;
1483
1484 lio = finfo->lio;
1485 g = finfo->g;
1486 frags = skb_shinfo(skb)->nr_frags;
1487
1488 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1489 g->sg[0].ptr[0], (skb->len - skb->data_len),
1490 DMA_TO_DEVICE);
1491
1492 i = 1;
1493 while (frags--) {
1494 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1495
1496 pci_unmap_page((lio->oct_dev)->pci_dev,
1497 g->sg[(i >> 2)].ptr[(i & 3)],
1498 frag->size, DMA_TO_DEVICE);
1499 i++;
1500 }
1501
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001502 dma_sync_single_for_cpu(&lio->oct_dev->pci_dev->dev,
1503 g->sg_dma_ptr, g->sg_size, DMA_TO_DEVICE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001504
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07001505 iq = skb_iq(lio, skb);
1506
1507 spin_lock(&lio->glist_lock[iq]);
1508 list_add_tail(&g->list, &lio->glist[iq]);
1509 spin_unlock(&lio->glist_lock[iq]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001510
1511 /* Don't free the skb yet */
1512
1513 check_txq_state(lio, skb);
1514}
1515
1516/**
1517 * \brief Adjust ptp frequency
1518 * @param ptp PTP clock info
1519 * @param ppb how much to adjust by, in parts-per-billion
1520 */
1521static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1522{
1523 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1524 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1525 u64 comp, delta;
1526 unsigned long flags;
1527 bool neg_adj = false;
1528
1529 if (ppb < 0) {
1530 neg_adj = true;
1531 ppb = -ppb;
1532 }
1533
1534 /* The hardware adds the clock compensation value to the
1535 * PTP clock on every coprocessor clock cycle, so we
1536 * compute the delta in terms of coprocessor clocks.
1537 */
1538 delta = (u64)ppb << 32;
1539 do_div(delta, oct->coproc_clock_rate);
1540
1541 spin_lock_irqsave(&lio->ptp_lock, flags);
1542 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1543 if (neg_adj)
1544 comp -= delta;
1545 else
1546 comp += delta;
1547 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1548 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1549
1550 return 0;
1551}
1552
1553/**
1554 * \brief Adjust ptp time
1555 * @param ptp PTP clock info
1556 * @param delta how much to adjust by, in nanosecs
1557 */
1558static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1559{
1560 unsigned long flags;
1561 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1562
1563 spin_lock_irqsave(&lio->ptp_lock, flags);
1564 lio->ptp_adjust += delta;
1565 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1566
1567 return 0;
1568}
1569
1570/**
1571 * \brief Get hardware clock time, including any adjustment
1572 * @param ptp PTP clock info
1573 * @param ts timespec
1574 */
1575static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1576 struct timespec64 *ts)
1577{
1578 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001579 unsigned long flags;
1580 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1581 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1582
1583 spin_lock_irqsave(&lio->ptp_lock, flags);
1584 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1585 ns += lio->ptp_adjust;
1586 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1587
Kefeng Wang286af312016-01-27 17:34:37 +08001588 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001589
1590 return 0;
1591}
1592
1593/**
1594 * \brief Set hardware clock time. Reset adjustment
1595 * @param ptp PTP clock info
1596 * @param ts timespec
1597 */
1598static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1599 const struct timespec64 *ts)
1600{
1601 u64 ns;
1602 unsigned long flags;
1603 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1604 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1605
1606 ns = timespec_to_ns(ts);
1607
1608 spin_lock_irqsave(&lio->ptp_lock, flags);
1609 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1610 lio->ptp_adjust = 0;
1611 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1612
1613 return 0;
1614}
1615
1616/**
1617 * \brief Check if PTP is enabled
1618 * @param ptp PTP clock info
1619 * @param rq request
1620 * @param on is it on
1621 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001622static int
1623liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
1624 struct ptp_clock_request *rq __attribute__((unused)),
1625 int on __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001626{
1627 return -EOPNOTSUPP;
1628}
1629
1630/**
1631 * \brief Open PTP clock source
1632 * @param netdev network device
1633 */
1634static void oct_ptp_open(struct net_device *netdev)
1635{
1636 struct lio *lio = GET_LIO(netdev);
1637 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1638
1639 spin_lock_init(&lio->ptp_lock);
1640
1641 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1642 lio->ptp_info.owner = THIS_MODULE;
1643 lio->ptp_info.max_adj = 250000000;
1644 lio->ptp_info.n_alarm = 0;
1645 lio->ptp_info.n_ext_ts = 0;
1646 lio->ptp_info.n_per_out = 0;
1647 lio->ptp_info.pps = 0;
1648 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1649 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1650 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1651 lio->ptp_info.settime64 = liquidio_ptp_settime;
1652 lio->ptp_info.enable = liquidio_ptp_enable;
1653
1654 lio->ptp_adjust = 0;
1655
1656 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1657 &oct->pci_dev->dev);
1658
1659 if (IS_ERR(lio->ptp_clock))
1660 lio->ptp_clock = NULL;
1661}
1662
1663/**
1664 * \brief Init PTP clock
1665 * @param oct octeon device
1666 */
1667static void liquidio_ptp_init(struct octeon_device *oct)
1668{
1669 u64 clock_comp, cfg;
1670
1671 clock_comp = (u64)NSEC_PER_SEC << 32;
1672 do_div(clock_comp, oct->coproc_clock_rate);
1673 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1674
1675 /* Enable */
1676 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1677 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1678}
1679
1680/**
1681 * \brief Load firmware to device
1682 * @param oct octeon device
1683 *
1684 * Maps device to firmware filename, requests firmware, and downloads it
1685 */
1686static int load_firmware(struct octeon_device *oct)
1687{
1688 int ret = 0;
1689 const struct firmware *fw;
1690 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1691 char *tmp_fw_type;
1692
1693 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1694 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1695 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1696 return ret;
1697 }
1698
1699 if (fw_type[0] == '\0')
1700 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1701 else
1702 tmp_fw_type = fw_type;
1703
1704 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1705 octeon_get_conf(oct)->card_name, tmp_fw_type,
1706 LIO_FW_NAME_SUFFIX);
1707
1708 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1709 if (ret) {
1710 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1711 fw_name);
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001712 release_firmware(fw);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001713 return ret;
1714 }
1715
1716 ret = octeon_download_firmware(oct, fw->data, fw->size);
1717
1718 release_firmware(fw);
1719
1720 return ret;
1721}
1722
1723/**
1724 * \brief Setup output queue
1725 * @param oct octeon device
1726 * @param q_no which queue
1727 * @param num_descs how many descriptors
1728 * @param desc_size size of each descriptor
1729 * @param app_ctx application context
1730 */
1731static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
1732 int desc_size, void *app_ctx)
1733{
1734 int ret_val = 0;
1735
1736 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
1737 /* droq creation and local register settings. */
1738 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301739 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001740 return ret_val;
1741
1742 if (ret_val == 1) {
1743 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
1744 return 0;
1745 }
1746 /* tasklet creation for the droq */
1747
1748 /* Enable the droq queues */
1749 octeon_set_droq_pkt_op(oct, q_no, 1);
1750
1751 /* Send Credit for Octeon Output queues. Credits are always
1752 * sent after the output queue is enabled.
1753 */
1754 writel(oct->droq[q_no]->max_count,
1755 oct->droq[q_no]->pkts_credit_reg);
1756
1757 return ret_val;
1758}
1759
1760/**
1761 * \brief Callback for getting interface configuration
1762 * @param status status of request
1763 * @param buf pointer to resp structure
1764 */
1765static void if_cfg_callback(struct octeon_device *oct,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001766 u32 status __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001767 void *buf)
1768{
1769 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1770 struct liquidio_if_cfg_resp *resp;
1771 struct liquidio_if_cfg_context *ctx;
1772
1773 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1774 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1775
1776 oct = lio_get_device(ctx->octeon_id);
1777 if (resp->status)
1778 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1779 CVM_CAST64(resp->status));
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001780 WRITE_ONCE(ctx->cond, 1);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001781
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07001782 snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
1783 resp->cfg_info.liquidio_firmware_version);
1784
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001785 /* This barrier is required to be sure that the response has been
1786 * written fully before waking up the handler
1787 */
1788 wmb();
1789
1790 wake_up_interruptible(&ctx->wc);
1791}
1792
1793/**
1794 * \brief Select queue based on hash
1795 * @param dev Net device
1796 * @param skb sk_buff structure
1797 * @returns selected queue number
1798 */
1799static u16 select_q(struct net_device *dev, struct sk_buff *skb,
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001800 void *accel_priv __attribute__((unused)),
1801 select_queue_fallback_t fallback __attribute__((unused)))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001802{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001803 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001804 struct lio *lio;
1805
1806 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001807 qindex = skb_tx_hash(dev, skb);
1808
1809 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001810}
1811
1812/** Routine to push packets arriving on Octeon interface upto network layer.
1813 * @param oct_id - octeon device id.
1814 * @param skbuff - skbuff struct to be passed to network layer.
1815 * @param len - size of total data received.
1816 * @param rh - Control header associated with the packet
1817 * @param param - additional control data with the packet
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001818 * @param arg - farg registered in droq_ops
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001819 */
1820static void
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07001821liquidio_push_packet(u32 octeon_id __attribute__((unused)),
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001822 void *skbuff,
1823 u32 len,
1824 union octeon_rh *rh,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001825 void *param,
1826 void *arg)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001827{
1828 struct napi_struct *napi = param;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001829 struct sk_buff *skb = (struct sk_buff *)skbuff;
1830 struct skb_shared_hwtstamps *shhwtstamps;
1831 u64 ns;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07001832 u16 vtag = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07001833 struct net_device *netdev = (struct net_device *)arg;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001834 struct octeon_droq *droq = container_of(param, struct octeon_droq,
1835 napi);
1836 if (netdev) {
1837 int packet_was_received;
1838 struct lio *lio = GET_LIO(netdev);
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001839 struct octeon_device *oct = lio->oct_dev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001840
1841 /* Do not proceed if the interface is not in RUNNING state. */
1842 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1843 recv_buffer_free(skb);
1844 droq->stats.rx_dropped++;
1845 return;
1846 }
1847
1848 skb->dev = netdev;
1849
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001850 skb_record_rx_queue(skb, droq->q_no);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07001851 if (likely(len > MIN_SKB_SIZE)) {
1852 struct octeon_skb_page_info *pg_info;
1853 unsigned char *va;
1854
1855 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
1856 if (pg_info->page) {
1857 /* For Paged allocation use the frags */
1858 va = page_address(pg_info->page) +
1859 pg_info->page_offset;
1860 memcpy(skb->data, va, MIN_SKB_SIZE);
1861 skb_put(skb, MIN_SKB_SIZE);
1862 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1863 pg_info->page,
1864 pg_info->page_offset +
1865 MIN_SKB_SIZE,
1866 len - MIN_SKB_SIZE,
1867 LIO_RXBUFFER_SZ);
1868 }
1869 } else {
1870 struct octeon_skb_page_info *pg_info =
1871 ((struct octeon_skb_page_info *)(skb->cb));
1872 skb_copy_to_linear_data(skb, page_address(pg_info->page)
1873 + pg_info->page_offset, len);
1874 skb_put(skb, len);
1875 put_page(pg_info->page);
1876 }
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001877
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001878 if (((oct->chip_id == OCTEON_CN66XX) ||
1879 (oct->chip_id == OCTEON_CN68XX)) &&
1880 ptp_enable) {
1881 if (rh->r_dh.has_hwtstamp) {
1882 /* timestamp is included from the hardware at
1883 * the beginning of the packet.
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001884 */
Raghu Vatsavayia5b37882016-06-14 16:54:48 -07001885 if (ifstate_check
1886 (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
1887 /* Nanoseconds are in the first 64-bits
1888 * of the packet.
1889 */
1890 memcpy(&ns, (skb->data), sizeof(ns));
1891 shhwtstamps = skb_hwtstamps(skb);
1892 shhwtstamps->hwtstamp =
1893 ns_to_ktime(ns +
1894 lio->ptp_adjust);
1895 }
1896 skb_pull(skb, sizeof(ns));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001897 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001898 }
1899
1900 skb->protocol = eth_type_trans(skb, skb->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001901 if ((netdev->features & NETIF_F_RXCSUM) &&
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07001902 (((rh->r_dh.encap_on) &&
1903 (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
1904 (!(rh->r_dh.encap_on) &&
1905 (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001906 /* checksum has already been verified */
1907 skb->ip_summed = CHECKSUM_UNNECESSARY;
1908 else
1909 skb->ip_summed = CHECKSUM_NONE;
1910
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07001911 /* Setting Encapsulation field on basis of status received
1912 * from the firmware
1913 */
1914 if (rh->r_dh.encap_on) {
1915 skb->encapsulation = 1;
1916 skb->csum_level = 1;
1917 droq->stats.rx_vxlan++;
1918 }
1919
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07001920 /* inbound VLAN tag */
1921 if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1922 (rh->r_dh.vlan != 0)) {
1923 u16 vid = rh->r_dh.vlan;
1924 u16 priority = rh->r_dh.priority;
1925
1926 vtag = priority << 13 | vid;
1927 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
1928 }
1929
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001930 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
1931
1932 if (packet_was_received) {
1933 droq->stats.rx_bytes_received += len;
1934 droq->stats.rx_pkts_received++;
1935 netdev->last_rx = jiffies;
1936 } else {
1937 droq->stats.rx_dropped++;
1938 netif_info(lio, rx_err, lio->netdev,
1939 "droq:%d error rx_dropped:%llu\n",
1940 droq->q_no, droq->stats.rx_dropped);
1941 }
1942
1943 } else {
1944 recv_buffer_free(skb);
1945 }
1946}
1947
1948/**
1949 * \brief wrapper for calling napi_schedule
1950 * @param param parameters to pass to napi_schedule
1951 *
1952 * Used when scheduling on different CPUs
1953 */
1954static void napi_schedule_wrapper(void *param)
1955{
1956 struct napi_struct *napi = param;
1957
1958 napi_schedule(napi);
1959}
1960
1961/**
1962 * \brief callback when receive interrupt occurs and we are in NAPI mode
1963 * @param arg pointer to octeon output queue
1964 */
1965static void liquidio_napi_drv_callback(void *arg)
1966{
1967 struct octeon_droq *droq = arg;
1968 int this_cpu = smp_processor_id();
1969
1970 if (droq->cpu_id == this_cpu) {
1971 napi_schedule(&droq->napi);
1972 } else {
1973 struct call_single_data *csd = &droq->csd;
1974
1975 csd->func = napi_schedule_wrapper;
1976 csd->info = &droq->napi;
1977 csd->flags = 0;
1978
1979 smp_call_function_single_async(droq->cpu_id, csd);
1980 }
1981}
1982
1983/**
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001984 * \brief Entry point for NAPI polling
1985 * @param napi NAPI structure
1986 * @param budget maximum number of items to process
1987 */
1988static int liquidio_napi_poll(struct napi_struct *napi, int budget)
1989{
1990 struct octeon_droq *droq;
1991 int work_done;
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001992 int tx_done = 0, iq_no;
1993 struct octeon_instr_queue *iq;
1994 struct octeon_device *oct;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001995
1996 droq = container_of(napi, struct octeon_droq, napi);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07001997 oct = droq->oct_dev;
1998 iq_no = droq->q_no;
1999 /* Handle Droq descriptors */
2000 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
2001 POLL_EVENT_PROCESS_PKTS,
2002 budget);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002003
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002004 /* Flush the instruction queue */
2005 iq = oct->instr_queue[iq_no];
2006 if (iq) {
2007 /* Process iq buffers with in the budget limits */
2008 tx_done = octeon_flush_iq(oct, iq, 1, budget);
2009 /* Update iq read-index rather than waiting for next interrupt.
2010 * Return back if tx_done is false.
2011 */
2012 update_txq_status(oct, iq_no);
2013 /*tx_done = (iq->flush_index == iq->octeon_read_index);*/
2014 } else {
2015 dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
2016 __func__, iq_no);
2017 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002018
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002019 if ((work_done < budget) && (tx_done)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002020 napi_complete(napi);
2021 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
2022 POLL_EVENT_ENABLE_INTR, 0);
2023 return 0;
2024 }
2025
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002026 return (!tx_done) ? (budget) : (work_done);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002027}
2028
2029/**
2030 * \brief Setup input and output queues
2031 * @param octeon_dev octeon device
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002032 * @param ifidx Interface Index
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002033 *
2034 * Note: Queues are with respect to the octeon device. Thus
2035 * an input queue is for egress packets, and output queues
2036 * are for ingress packets.
2037 */
2038static inline int setup_io_queues(struct octeon_device *octeon_dev,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002039 int ifidx)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002040{
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002041 struct octeon_droq_ops droq_ops;
2042 struct net_device *netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002043 static int cpu_id;
2044 static int cpu_id_modulus;
2045 struct octeon_droq *droq;
2046 struct napi_struct *napi;
2047 int q, q_no, retval = 0;
2048 struct lio *lio;
2049 int num_tx_descs;
2050
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002051 netdev = octeon_dev->props[ifidx].netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002052
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002053 lio = GET_LIO(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002054
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002055 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
2056
2057 droq_ops.fptr = liquidio_push_packet;
2058 droq_ops.farg = (void *)netdev;
2059
2060 droq_ops.poll_mode = 1;
2061 droq_ops.napi_fn = liquidio_napi_drv_callback;
2062 cpu_id = 0;
2063 cpu_id_modulus = num_present_cpus();
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002064
2065 /* set up DROQs. */
2066 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002067 q_no = lio->linfo.rxpciq[q].s.q_no;
2068 dev_dbg(&octeon_dev->pci_dev->dev,
2069 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
2070 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002071 retval = octeon_setup_droq(octeon_dev, q_no,
2072 CFG_GET_NUM_RX_DESCS_NIC_IF
2073 (octeon_get_conf(octeon_dev),
2074 lio->ifidx),
2075 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
2076 (octeon_get_conf(octeon_dev),
2077 lio->ifidx), NULL);
2078 if (retval) {
2079 dev_err(&octeon_dev->pci_dev->dev,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002080 "%s : Runtime DROQ(RxQ) creation failed.\n",
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002081 __func__);
2082 return 1;
2083 }
2084
2085 droq = octeon_dev->droq[q_no];
2086 napi = &droq->napi;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002087 dev_dbg(&octeon_dev->pci_dev->dev,
2088 "netif_napi_add netdev:%llx oct:%llx\n",
2089 (u64)netdev,
2090 (u64)octeon_dev);
2091 netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002092
2093 /* designate a CPU for this droq */
2094 droq->cpu_id = cpu_id;
2095 cpu_id++;
2096 if (cpu_id >= cpu_id_modulus)
2097 cpu_id = 0;
2098
2099 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2100 }
2101
2102 /* set up IQs. */
2103 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2104 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2105 (octeon_dev),
2106 lio->ifidx);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002107 retval = octeon_setup_iq(octeon_dev, ifidx, q,
2108 lio->linfo.txpciq[q], num_tx_descs,
2109 netdev_get_tx_queue(netdev, q));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002110 if (retval) {
2111 dev_err(&octeon_dev->pci_dev->dev,
2112 " %s : Runtime IQ(TxQ) creation failed.\n",
2113 __func__);
2114 return 1;
2115 }
2116 }
2117
2118 return 0;
2119}
2120
2121/**
2122 * \brief Poll routine for checking transmit queue status
2123 * @param work work_struct data structure
2124 */
2125static void octnet_poll_check_txq_status(struct work_struct *work)
2126{
2127 struct cavium_wk *wk = (struct cavium_wk *)work;
2128 struct lio *lio = (struct lio *)wk->ctxptr;
2129
2130 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2131 return;
2132
2133 check_txq_status(lio);
2134 queue_delayed_work(lio->txq_status_wq.wq,
2135 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2136}
2137
2138/**
2139 * \brief Sets up the txq poll check
2140 * @param netdev network device
2141 */
2142static inline void setup_tx_poll_fn(struct net_device *netdev)
2143{
2144 struct lio *lio = GET_LIO(netdev);
2145 struct octeon_device *oct = lio->oct_dev;
2146
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302147 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2148 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002149 if (!lio->txq_status_wq.wq) {
2150 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2151 return;
2152 }
2153 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2154 octnet_poll_check_txq_status);
2155 lio->txq_status_wq.wk.ctxptr = lio;
2156 queue_delayed_work(lio->txq_status_wq.wq,
2157 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2158}
2159
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002160static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2161{
2162 struct lio *lio = GET_LIO(netdev);
2163
2164 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2165 destroy_workqueue(lio->txq_status_wq.wq);
2166}
2167
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002168/**
2169 * \brief Net device open for LiquidIO
2170 * @param netdev network device
2171 */
2172static int liquidio_open(struct net_device *netdev)
2173{
2174 struct lio *lio = GET_LIO(netdev);
2175 struct octeon_device *oct = lio->oct_dev;
2176 struct napi_struct *napi, *n;
2177
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002178 if (oct->props[lio->ifidx].napi_enabled == 0) {
2179 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2180 napi_enable(napi);
2181
2182 oct->props[lio->ifidx].napi_enabled = 1;
2183 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002184
2185 oct_ptp_open(netdev);
2186
2187 ifstate_set(lio, LIO_IFSTATE_RUNNING);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002188
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002189 setup_tx_poll_fn(netdev);
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002190
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002191 start_txq(netdev);
2192
2193 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002194
2195 /* tell Octeon to start forwarding packets to host */
2196 send_rx_ctrl_cmd(lio, 1);
2197
2198 /* Ready for link status updates */
2199 lio->intf_open = 1;
2200
2201 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2202 netdev->name);
2203
2204 return 0;
2205}
2206
2207/**
2208 * \brief Net device stop for LiquidIO
2209 * @param netdev network device
2210 */
2211static int liquidio_stop(struct net_device *netdev)
2212{
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002213 struct lio *lio = GET_LIO(netdev);
2214 struct octeon_device *oct = lio->oct_dev;
2215
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002216 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2217
2218 netif_tx_disable(netdev);
2219
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002220 /* Inform that netif carrier is down */
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002221 netif_carrier_off(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002222 lio->intf_open = 0;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002223 lio->linfo.link.s.link_up = 0;
2224 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002225
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002226 /* Pause for a moment and wait for Octeon to flush out (to the wire) any
2227 * egress packets that are in-flight.
2228 */
2229 set_current_state(TASK_INTERRUPTIBLE);
2230 schedule_timeout(msecs_to_jiffies(100));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002231
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002232 /* Now it should be safe to tell Octeon that nic interface is down. */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002233 send_rx_ctrl_cmd(lio, 0);
2234
Raghu Vatsavayi9a96bde2016-06-21 22:53:06 -07002235 cleanup_tx_poll_fn(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002236
2237 if (lio->ptp_clock) {
2238 ptp_clock_unregister(lio->ptp_clock);
2239 lio->ptp_clock = NULL;
2240 }
2241
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002242 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002243
2244 return 0;
2245}
2246
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002247/**
2248 * \brief Converts a mask based on net device flags
2249 * @param netdev network device
2250 *
2251 * This routine generates a octnet_ifflags mask from the net device flags
2252 * received from the OS.
2253 */
2254static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2255{
2256 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2257
2258 if (netdev->flags & IFF_PROMISC)
2259 f |= OCTNET_IFFLAG_PROMISC;
2260
2261 if (netdev->flags & IFF_ALLMULTI)
2262 f |= OCTNET_IFFLAG_ALLMULTI;
2263
2264 if (netdev->flags & IFF_MULTICAST) {
2265 f |= OCTNET_IFFLAG_MULTICAST;
2266
2267 /* Accept all multicast addresses if there are more than we
2268 * can handle
2269 */
2270 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2271 f |= OCTNET_IFFLAG_ALLMULTI;
2272 }
2273
2274 if (netdev->flags & IFF_BROADCAST)
2275 f |= OCTNET_IFFLAG_BROADCAST;
2276
2277 return f;
2278}
2279
2280/**
2281 * \brief Net device set_multicast_list
2282 * @param netdev network device
2283 */
2284static void liquidio_set_mcast_list(struct net_device *netdev)
2285{
2286 struct lio *lio = GET_LIO(netdev);
2287 struct octeon_device *oct = lio->oct_dev;
2288 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002289 struct netdev_hw_addr *ha;
2290 u64 *mc;
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002291 int ret;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002292 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2293
2294 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2295
2296 /* Create a ctrl pkt command to be sent to core app. */
2297 nctrl.ncmd.u64 = 0;
2298 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002299 nctrl.ncmd.s.param1 = get_new_flags(netdev);
2300 nctrl.ncmd.s.param2 = mc_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002301 nctrl.ncmd.s.more = mc_count;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002302 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002303 nctrl.netpndev = (u64)netdev;
2304 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2305
2306 /* copy all the addresses into the udd */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002307 mc = &nctrl.udd[0];
2308 netdev_for_each_mc_addr(ha, netdev) {
2309 *mc = 0;
2310 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2311 /* no need to swap bytes */
2312
2313 if (++mc > &nctrl.udd[mc_count])
2314 break;
2315 }
2316
2317 /* Apparently, any activity in this call from the kernel has to
2318 * be atomic. So we won't wait for response.
2319 */
2320 nctrl.wait_time = 0;
2321
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002322 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002323 if (ret < 0) {
2324 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2325 ret);
2326 }
2327}
2328
2329/**
2330 * \brief Net device set_mac_address
2331 * @param netdev network device
2332 */
2333static int liquidio_set_mac(struct net_device *netdev, void *p)
2334{
2335 int ret = 0;
2336 struct lio *lio = GET_LIO(netdev);
2337 struct octeon_device *oct = lio->oct_dev;
2338 struct sockaddr *addr = (struct sockaddr *)p;
2339 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002340
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002341 if (!is_valid_ether_addr(addr->sa_data))
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002342 return -EADDRNOTAVAIL;
2343
2344 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2345
2346 nctrl.ncmd.u64 = 0;
2347 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002348 nctrl.ncmd.s.param1 = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002349 nctrl.ncmd.s.more = 1;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002350 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002351 nctrl.netpndev = (u64)netdev;
2352 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2353 nctrl.wait_time = 100;
2354
2355 nctrl.udd[0] = 0;
2356 /* The MAC Address is presented in network byte order. */
2357 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2358
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002359 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002360 if (ret < 0) {
2361 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2362 return -ENOMEM;
2363 }
2364 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2365 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2366
2367 return 0;
2368}
2369
2370/**
2371 * \brief Net device get_stats
2372 * @param netdev network device
2373 */
2374static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2375{
2376 struct lio *lio = GET_LIO(netdev);
2377 struct net_device_stats *stats = &netdev->stats;
2378 struct octeon_device *oct;
2379 u64 pkts = 0, drop = 0, bytes = 0;
2380 struct oct_droq_stats *oq_stats;
2381 struct oct_iq_stats *iq_stats;
2382 int i, iq_no, oq_no;
2383
2384 oct = lio->oct_dev;
2385
2386 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002387 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002388 iq_stats = &oct->instr_queue[iq_no]->stats;
2389 pkts += iq_stats->tx_done;
2390 drop += iq_stats->tx_dropped;
2391 bytes += iq_stats->tx_tot_bytes;
2392 }
2393
2394 stats->tx_packets = pkts;
2395 stats->tx_bytes = bytes;
2396 stats->tx_dropped = drop;
2397
2398 pkts = 0;
2399 drop = 0;
2400 bytes = 0;
2401
2402 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002403 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002404 oq_stats = &oct->droq[oq_no]->stats;
2405 pkts += oq_stats->rx_pkts_received;
2406 drop += (oq_stats->rx_dropped +
2407 oq_stats->dropped_nodispatch +
2408 oq_stats->dropped_toomany +
2409 oq_stats->dropped_nomem);
2410 bytes += oq_stats->rx_bytes_received;
2411 }
2412
2413 stats->rx_bytes = bytes;
2414 stats->rx_packets = pkts;
2415 stats->rx_dropped = drop;
2416
2417 return stats;
2418}
2419
2420/**
2421 * \brief Net device change_mtu
2422 * @param netdev network device
2423 */
2424static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2425{
2426 struct lio *lio = GET_LIO(netdev);
2427 struct octeon_device *oct = lio->oct_dev;
2428 struct octnic_ctrl_pkt nctrl;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002429 int ret = 0;
2430
Raghu Vatsavayi4c2743f2016-07-03 13:56:53 -07002431 /* Limit the MTU to make sure the ethernet packets are between 68 bytes
2432 * and 16000 bytes
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002433 */
Raghu Vatsavayi4c2743f2016-07-03 13:56:53 -07002434 if ((new_mtu < LIO_MIN_MTU_SIZE) ||
2435 (new_mtu > LIO_MAX_MTU_SIZE)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002436 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2437 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
Raghu Vatsavayi4c2743f2016-07-03 13:56:53 -07002438 LIO_MIN_MTU_SIZE, LIO_MAX_MTU_SIZE);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002439 return -EINVAL;
2440 }
2441
2442 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2443
2444 nctrl.ncmd.u64 = 0;
2445 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002446 nctrl.ncmd.s.param1 = new_mtu;
2447 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002448 nctrl.wait_time = 100;
2449 nctrl.netpndev = (u64)netdev;
2450 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2451
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002452 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002453 if (ret < 0) {
2454 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2455 return -1;
2456 }
2457
2458 lio->mtu = new_mtu;
2459
2460 return 0;
2461}
2462
2463/**
2464 * \brief Handler for SIOCSHWTSTAMP ioctl
2465 * @param netdev network device
2466 * @param ifr interface request
2467 * @param cmd command
2468 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002469static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002470{
2471 struct hwtstamp_config conf;
2472 struct lio *lio = GET_LIO(netdev);
2473
2474 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2475 return -EFAULT;
2476
2477 if (conf.flags)
2478 return -EINVAL;
2479
2480 switch (conf.tx_type) {
2481 case HWTSTAMP_TX_ON:
2482 case HWTSTAMP_TX_OFF:
2483 break;
2484 default:
2485 return -ERANGE;
2486 }
2487
2488 switch (conf.rx_filter) {
2489 case HWTSTAMP_FILTER_NONE:
2490 break;
2491 case HWTSTAMP_FILTER_ALL:
2492 case HWTSTAMP_FILTER_SOME:
2493 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2494 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2495 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2496 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2497 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2498 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2499 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2500 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2501 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2502 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2503 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2504 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2505 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2506 break;
2507 default:
2508 return -ERANGE;
2509 }
2510
2511 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2512 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2513
2514 else
2515 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2516
2517 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2518}
2519
2520/**
2521 * \brief ioctl handler
2522 * @param netdev network device
2523 * @param ifr interface request
2524 * @param cmd command
2525 */
2526static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2527{
2528 switch (cmd) {
2529 case SIOCSHWTSTAMP:
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07002530 return hwtstamp_ioctl(netdev, ifr);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002531 default:
2532 return -EOPNOTSUPP;
2533 }
2534}
2535
2536/**
2537 * \brief handle a Tx timestamp response
2538 * @param status response status
2539 * @param buf pointer to skb
2540 */
2541static void handle_timestamp(struct octeon_device *oct,
2542 u32 status,
2543 void *buf)
2544{
2545 struct octnet_buf_free_info *finfo;
2546 struct octeon_soft_command *sc;
2547 struct oct_timestamp_resp *resp;
2548 struct lio *lio;
2549 struct sk_buff *skb = (struct sk_buff *)buf;
2550
2551 finfo = (struct octnet_buf_free_info *)skb->cb;
2552 lio = finfo->lio;
2553 sc = finfo->sc;
2554 oct = lio->oct_dev;
2555 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2556
2557 if (status != OCTEON_REQUEST_DONE) {
2558 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2559 CVM_CAST64(status));
2560 resp->timestamp = 0;
2561 }
2562
2563 octeon_swap_8B_data(&resp->timestamp, 1);
2564
Colin Ian King19a6d152016-02-05 16:30:39 +00002565 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002566 struct skb_shared_hwtstamps ts;
2567 u64 ns = resp->timestamp;
2568
2569 netif_info(lio, tx_done, lio->netdev,
2570 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2571 skb, (unsigned long long)ns);
2572 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2573 skb_tstamp_tx(skb, &ts);
2574 }
2575
2576 octeon_free_soft_command(oct, sc);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002577 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002578}
2579
2580/* \brief Send a data packet that will be timestamped
2581 * @param oct octeon device
2582 * @param ndata pointer to network data
2583 * @param finfo pointer to private network data
2584 */
2585static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2586 struct octnic_data_pkt *ndata,
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002587 struct octnet_buf_free_info *finfo)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002588{
2589 int retval;
2590 struct octeon_soft_command *sc;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002591 struct lio *lio;
2592 int ring_doorbell;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002593 u32 len;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002594
2595 lio = finfo->lio;
2596
2597 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2598 sizeof(struct oct_timestamp_resp));
2599 finfo->sc = sc;
2600
2601 if (!sc) {
2602 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2603 return IQ_SEND_FAILED;
2604 }
2605
2606 if (ndata->reqtype == REQTYPE_NORESP_NET)
2607 ndata->reqtype = REQTYPE_RESP_NET;
2608 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2609 ndata->reqtype = REQTYPE_RESP_NET_SG;
2610
2611 sc->callback = handle_timestamp;
2612 sc->callback_arg = finfo->skb;
2613 sc->iq_no = ndata->q_no;
2614
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002615 len = (u32)((struct octeon_instr_ih2 *)(&sc->cmd.cmd2.ih2))->dlengsz;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002616
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002617 ring_doorbell = 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002618 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002619 sc, len, ndata->reqtype);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002620
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002621 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002622 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2623 retval);
2624 octeon_free_soft_command(oct, sc);
2625 } else {
2626 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2627 }
2628
2629 return retval;
2630}
2631
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002632/** \brief Transmit networks packets to the Octeon interface
2633 * @param skbuff skbuff struct to be passed to network layer.
2634 * @param netdev pointer to network device
2635 * @returns whether the packet was transmitted to the device okay or not
2636 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2637 */
2638static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2639{
2640 struct lio *lio;
2641 struct octnet_buf_free_info *finfo;
2642 union octnic_cmd_setup cmdsetup;
2643 struct octnic_data_pkt ndata;
2644 struct octeon_device *oct;
2645 struct oct_iq_stats *stats;
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002646 struct octeon_instr_irh *irh;
2647 union tx_info *tx_info;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002648 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002649 int q_idx = 0, iq_no = 0;
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002650 int j;
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002651 u64 dptr = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002652 u32 tag = 0;
2653
2654 lio = GET_LIO(netdev);
2655 oct = lio->oct_dev;
2656
2657 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002658 q_idx = skb->queue_mapping;
2659 q_idx = (q_idx % (lio->linfo.num_txpciq));
2660 tag = q_idx;
2661 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002662 } else {
2663 iq_no = lio->txq;
2664 }
2665
2666 stats = &oct->instr_queue[iq_no]->stats;
2667
2668 /* Check for all conditions in which the current packet cannot be
2669 * transmitted.
2670 */
2671 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002672 (!lio->linfo.link.s.link_up) ||
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002673 (skb->len <= 0)) {
2674 netif_info(lio, tx_err, lio->netdev,
2675 "Transmit failed link_status : %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002676 lio->linfo.link.s.link_up);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002677 goto lio_xmit_failed;
2678 }
2679
2680 /* Use space in skb->cb to store info used to unmap and
2681 * free the buffers.
2682 */
2683 finfo = (struct octnet_buf_free_info *)skb->cb;
2684 finfo->lio = lio;
2685 finfo->skb = skb;
2686 finfo->sc = NULL;
2687
2688 /* Prepare the attributes for the data to be passed to OSI. */
2689 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2690
2691 ndata.buf = (void *)finfo;
2692
2693 ndata.q_no = iq_no;
2694
2695 if (netif_is_multiqueue(netdev)) {
2696 if (octnet_iq_is_full(oct, ndata.q_no)) {
2697 /* defer sending if queue is full */
2698 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2699 ndata.q_no);
2700 stats->tx_iq_busy++;
2701 return NETDEV_TX_BUSY;
2702 }
2703 } else {
2704 if (octnet_iq_is_full(oct, lio->txq)) {
2705 /* defer sending if queue is full */
2706 stats->tx_iq_busy++;
2707 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002708 lio->txq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002709 return NETDEV_TX_BUSY;
2710 }
2711 }
2712 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002713 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002714 */
2715
2716 ndata.datasize = skb->len;
2717
2718 cmdsetup.u64 = 0;
Raghu Vatsavayi7275ebf2016-06-14 16:54:49 -07002719 cmdsetup.s.iq_no = iq_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002720
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002721 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2722 if (skb->encapsulation) {
2723 cmdsetup.s.tnl_csum = 1;
2724 stats->tx_vxlan++;
2725 } else {
2726 cmdsetup.s.transport_csum = 1;
2727 }
2728 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002729 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2730 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2731 cmdsetup.s.timestamp = 1;
2732 }
2733
2734 if (skb_shinfo(skb)->nr_frags == 0) {
2735 cmdsetup.s.u.datasize = skb->len;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002736 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07002737
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002738 /* Offload checksum calculation for TCP/UDP packets */
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002739 dptr = dma_map_single(&oct->pci_dev->dev,
2740 skb->data,
2741 skb->len,
2742 DMA_TO_DEVICE);
2743 if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002744 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2745 __func__);
2746 return NETDEV_TX_BUSY;
2747 }
2748
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002749 ndata.cmd.cmd2.dptr = dptr;
2750 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002751 ndata.reqtype = REQTYPE_NORESP_NET;
2752
2753 } else {
2754 int i, frags;
2755 struct skb_frag_struct *frag;
2756 struct octnic_gather *g;
2757
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002758 spin_lock(&lio->glist_lock[q_idx]);
2759 g = (struct octnic_gather *)
2760 list_delete_head(&lio->glist[q_idx]);
2761 spin_unlock(&lio->glist_lock[q_idx]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002762
2763 if (!g) {
2764 netif_info(lio, tx_err, lio->netdev,
2765 "Transmit scatter gather: glist null!\n");
2766 goto lio_xmit_failed;
2767 }
2768
2769 cmdsetup.s.gather = 1;
2770 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07002771 octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002772
2773 memset(g->sg, 0, g->sg_size);
2774
2775 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2776 skb->data,
2777 (skb->len - skb->data_len),
2778 DMA_TO_DEVICE);
2779 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2780 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2781 __func__);
2782 return NETDEV_TX_BUSY;
2783 }
2784 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2785
2786 frags = skb_shinfo(skb)->nr_frags;
2787 i = 1;
2788 while (frags--) {
2789 frag = &skb_shinfo(skb)->frags[i - 1];
2790
2791 g->sg[(i >> 2)].ptr[(i & 3)] =
2792 dma_map_page(&oct->pci_dev->dev,
2793 frag->page.p,
2794 frag->page_offset,
2795 frag->size,
2796 DMA_TO_DEVICE);
2797
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002798 if (dma_mapping_error(&oct->pci_dev->dev,
2799 g->sg[i >> 2].ptr[i & 3])) {
2800 dma_unmap_single(&oct->pci_dev->dev,
2801 g->sg[0].ptr[0],
2802 skb->len - skb->data_len,
2803 DMA_TO_DEVICE);
2804 for (j = 1; j < i; j++) {
2805 frag = &skb_shinfo(skb)->frags[j - 1];
2806 dma_unmap_page(&oct->pci_dev->dev,
2807 g->sg[j >> 2].ptr[j & 3],
2808 frag->size,
2809 DMA_TO_DEVICE);
2810 }
2811 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2812 __func__);
2813 return NETDEV_TX_BUSY;
2814 }
2815
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002816 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2817 i++;
2818 }
2819
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07002820 dma_sync_single_for_device(&oct->pci_dev->dev, g->sg_dma_ptr,
2821 g->sg_size, DMA_TO_DEVICE);
2822 dptr = g->sg_dma_ptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002823
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002824 ndata.cmd.cmd2.dptr = dptr;
2825 finfo->dptr = dptr;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002826 finfo->g = g;
2827
2828 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2829 }
2830
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002831 irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2832 tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002833
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002834 if (skb_shinfo(skb)->gso_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002835 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2836 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002837 stats->tx_gso++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002838 }
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002839
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07002840 /* HW insert VLAN tag */
2841 if (skb_vlan_tag_present(skb)) {
2842 irh->priority = skb_vlan_tag_get(skb) >> 13;
2843 irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2844 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002845
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002846 if (unlikely(cmdsetup.s.timestamp))
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002847 status = send_nic_timestamp_pkt(oct, &ndata, finfo);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002848 else
Raghu Vatsavayi32581242016-08-31 11:03:20 -07002849 status = octnet_send_nic_data_pkt(oct, &ndata);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002850 if (status == IQ_SEND_FAILED)
2851 goto lio_xmit_failed;
2852
2853 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2854
2855 if (status == IQ_SEND_STOP)
2856 stop_q(lio->netdev, q_idx);
2857
Florian Westphal860e9532016-05-03 16:33:13 +02002858 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002859
Raghu Vatsavayi1f164712016-06-21 22:53:11 -07002860 if (skb_shinfo(skb)->gso_size)
2861 stats->tx_done += skb_shinfo(skb)->gso_segs;
2862 else
2863 stats->tx_done++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002864 stats->tx_tot_bytes += skb->len;
2865
2866 return NETDEV_TX_OK;
2867
2868lio_xmit_failed:
2869 stats->tx_dropped++;
2870 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2871 iq_no, stats->tx_dropped);
Raghu Vatsavayi6a885b62016-06-14 16:54:51 -07002872 if (dptr)
2873 dma_unmap_single(&oct->pci_dev->dev, dptr,
2874 ndata.datasize, DMA_TO_DEVICE);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -07002875 tx_buffer_free(skb);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002876 return NETDEV_TX_OK;
2877}
2878
2879/** \brief Network device Tx timeout
2880 * @param netdev pointer to network device
2881 */
2882static void liquidio_tx_timeout(struct net_device *netdev)
2883{
2884 struct lio *lio;
2885
2886 lio = GET_LIO(netdev);
2887
2888 netif_info(lio, tx_err, lio->netdev,
2889 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2890 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002891 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002892 txqs_wake(netdev);
2893}
2894
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07002895static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
2896 __be16 proto __attribute__((unused)),
2897 u16 vid)
2898{
2899 struct lio *lio = GET_LIO(netdev);
2900 struct octeon_device *oct = lio->oct_dev;
2901 struct octnic_ctrl_pkt nctrl;
2902 int ret = 0;
2903
2904 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2905
2906 nctrl.ncmd.u64 = 0;
2907 nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
2908 nctrl.ncmd.s.param1 = vid;
2909 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2910 nctrl.wait_time = 100;
2911 nctrl.netpndev = (u64)netdev;
2912 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2913
2914 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2915 if (ret < 0) {
2916 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2917 ret);
2918 }
2919
2920 return ret;
2921}
2922
2923static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
2924 __be16 proto __attribute__((unused)),
2925 u16 vid)
2926{
2927 struct lio *lio = GET_LIO(netdev);
2928 struct octeon_device *oct = lio->oct_dev;
2929 struct octnic_ctrl_pkt nctrl;
2930 int ret = 0;
2931
2932 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2933
2934 nctrl.ncmd.u64 = 0;
2935 nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
2936 nctrl.ncmd.s.param1 = vid;
2937 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2938 nctrl.wait_time = 100;
2939 nctrl.netpndev = (u64)netdev;
2940 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2941
2942 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2943 if (ret < 0) {
2944 dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2945 ret);
2946 }
2947 return ret;
2948}
2949
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002950/** Sending command to enable/disable RX checksum offload
2951 * @param netdev pointer to network device
2952 * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
2953 * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
2954 * OCTNET_CMD_RXCSUM_DISABLE
2955 * @returns SUCCESS or FAILURE
2956 */
Nicholas Mc Guirec41419b2016-08-22 17:52:00 +02002957static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
2958 u8 rx_cmd)
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07002959{
2960 struct lio *lio = GET_LIO(netdev);
2961 struct octeon_device *oct = lio->oct_dev;
2962 struct octnic_ctrl_pkt nctrl;
2963 int ret = 0;
2964
2965 nctrl.ncmd.u64 = 0;
2966 nctrl.ncmd.s.cmd = command;
2967 nctrl.ncmd.s.param1 = rx_cmd;
2968 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2969 nctrl.wait_time = 100;
2970 nctrl.netpndev = (u64)netdev;
2971 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2972
2973 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2974 if (ret < 0) {
2975 dev_err(&oct->pci_dev->dev,
2976 "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
2977 ret);
2978 }
2979 return ret;
2980}
2981
2982/** Sending command to add/delete VxLAN UDP port to firmware
2983 * @param netdev pointer to network device
2984 * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
2985 * @param vxlan_port VxLAN port to be added or deleted
2986 * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
2987 * OCTNET_CMD_VXLAN_PORT_DEL
2988 * @returns SUCCESS or FAILURE
2989 */
2990static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
2991 u16 vxlan_port, u8 vxlan_cmd_bit)
2992{
2993 struct lio *lio = GET_LIO(netdev);
2994 struct octeon_device *oct = lio->oct_dev;
2995 struct octnic_ctrl_pkt nctrl;
2996 int ret = 0;
2997
2998 nctrl.ncmd.u64 = 0;
2999 nctrl.ncmd.s.cmd = command;
3000 nctrl.ncmd.s.more = vxlan_cmd_bit;
3001 nctrl.ncmd.s.param1 = vxlan_port;
3002 nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3003 nctrl.wait_time = 100;
3004 nctrl.netpndev = (u64)netdev;
3005 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3006
3007 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
3008 if (ret < 0) {
3009 dev_err(&oct->pci_dev->dev,
3010 "VxLAN port add/delete failed in core (ret:0x%x)\n",
3011 ret);
3012 }
3013 return ret;
3014}
3015
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003016/** \brief Net device fix features
3017 * @param netdev pointer to network device
3018 * @param request features requested
3019 * @returns updated features list
3020 */
3021static netdev_features_t liquidio_fix_features(struct net_device *netdev,
3022 netdev_features_t request)
3023{
3024 struct lio *lio = netdev_priv(netdev);
3025
3026 if ((request & NETIF_F_RXCSUM) &&
3027 !(lio->dev_capability & NETIF_F_RXCSUM))
3028 request &= ~NETIF_F_RXCSUM;
3029
3030 if ((request & NETIF_F_HW_CSUM) &&
3031 !(lio->dev_capability & NETIF_F_HW_CSUM))
3032 request &= ~NETIF_F_HW_CSUM;
3033
3034 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
3035 request &= ~NETIF_F_TSO;
3036
3037 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
3038 request &= ~NETIF_F_TSO6;
3039
3040 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3041 request &= ~NETIF_F_LRO;
3042
3043 /*Disable LRO if RXCSUM is off */
3044 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3045 (lio->dev_capability & NETIF_F_LRO))
3046 request &= ~NETIF_F_LRO;
3047
3048 return request;
3049}
3050
3051/** \brief Net device set features
3052 * @param netdev pointer to network device
3053 * @param features features to enable/disable
3054 */
3055static int liquidio_set_features(struct net_device *netdev,
3056 netdev_features_t features)
3057{
3058 struct lio *lio = netdev_priv(netdev);
3059
3060 if (!((netdev->features ^ features) & NETIF_F_LRO))
3061 return 0;
3062
3063 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003064 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3065 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003066 else if (!(features & NETIF_F_LRO) &&
3067 (lio->dev_capability & NETIF_F_LRO))
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003068 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
3069 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003070
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003071 /* Sending command to firmware to enable/disable RX checksum
3072 * offload settings using ethtool
3073 */
3074 if (!(netdev->features & NETIF_F_RXCSUM) &&
3075 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3076 (features & NETIF_F_RXCSUM))
3077 liquidio_set_rxcsum_command(netdev,
3078 OCTNET_CMD_TNL_RX_CSUM_CTL,
3079 OCTNET_CMD_RXCSUM_ENABLE);
3080 else if ((netdev->features & NETIF_F_RXCSUM) &&
3081 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
3082 !(features & NETIF_F_RXCSUM))
3083 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3084 OCTNET_CMD_RXCSUM_DISABLE);
3085
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003086 return 0;
3087}
3088
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003089static void liquidio_add_vxlan_port(struct net_device *netdev,
3090 struct udp_tunnel_info *ti)
3091{
3092 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3093 return;
3094
3095 liquidio_vxlan_port_command(netdev,
3096 OCTNET_CMD_VXLAN_PORT_CONFIG,
3097 htons(ti->port),
3098 OCTNET_CMD_VXLAN_PORT_ADD);
3099}
3100
3101static void liquidio_del_vxlan_port(struct net_device *netdev,
3102 struct udp_tunnel_info *ti)
3103{
3104 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3105 return;
3106
3107 liquidio_vxlan_port_command(netdev,
3108 OCTNET_CMD_VXLAN_PORT_CONFIG,
3109 htons(ti->port),
3110 OCTNET_CMD_VXLAN_PORT_DEL);
3111}
3112
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003113static struct net_device_ops lionetdevops = {
3114 .ndo_open = liquidio_open,
3115 .ndo_stop = liquidio_stop,
3116 .ndo_start_xmit = liquidio_xmit,
3117 .ndo_get_stats = liquidio_get_stats,
3118 .ndo_set_mac_address = liquidio_set_mac,
3119 .ndo_set_rx_mode = liquidio_set_mcast_list,
3120 .ndo_tx_timeout = liquidio_tx_timeout,
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003121
3122 .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
3123 .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003124 .ndo_change_mtu = liquidio_change_mtu,
3125 .ndo_do_ioctl = liquidio_ioctl,
3126 .ndo_fix_features = liquidio_fix_features,
3127 .ndo_set_features = liquidio_set_features,
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003128 .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
3129 .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003130};
3131
3132/** \brief Entry point for the liquidio module
3133 */
3134static int __init liquidio_init(void)
3135{
3136 int i;
3137 struct handshake *hs;
3138
3139 init_completion(&first_stage);
3140
3141 octeon_init_device_list(conf_type);
3142
3143 if (liquidio_init_pci())
3144 return -EINVAL;
3145
3146 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3147
3148 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3149 hs = &handshake[i];
3150 if (hs->pci_dev) {
3151 wait_for_completion(&hs->init);
3152 if (!hs->init_ok) {
3153 /* init handshake failed */
3154 dev_err(&hs->pci_dev->dev,
3155 "Failed to init device\n");
3156 liquidio_deinit_pci();
3157 return -EIO;
3158 }
3159 }
3160 }
3161
3162 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3163 hs = &handshake[i];
3164 if (hs->pci_dev) {
3165 wait_for_completion_timeout(&hs->started,
3166 msecs_to_jiffies(30000));
3167 if (!hs->started_ok) {
3168 /* starter handshake failed */
3169 dev_err(&hs->pci_dev->dev,
3170 "Firmware failed to start\n");
3171 liquidio_deinit_pci();
3172 return -EIO;
3173 }
3174 }
3175 }
3176
3177 return 0;
3178}
3179
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003180static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003181{
3182 struct octeon_device *oct = (struct octeon_device *)buf;
3183 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003184 int gmxport = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003185 union oct_link_status *ls;
3186 int i;
3187
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003188 if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003189 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3190 recv_pkt->buffer_size[0],
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003191 recv_pkt->rh.r_nic_info.gmxport);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003192 goto nic_info_err;
3193 }
3194
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003195 gmxport = recv_pkt->rh.r_nic_info.gmxport;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003196 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3197
3198 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003199 for (i = 0; i < oct->ifcount; i++) {
3200 if (oct->props[i].gmxport == gmxport) {
3201 update_link_status(oct->props[i].netdev, ls);
3202 break;
3203 }
3204 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003205
3206nic_info_err:
3207 for (i = 0; i < recv_pkt->buffer_count; i++)
3208 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3209 octeon_free_recv_info(recv_info);
3210 return 0;
3211}
3212
3213/**
3214 * \brief Setup network interfaces
3215 * @param octeon_dev octeon device
3216 *
3217 * Called during init time for each device. It assumes the NIC
3218 * is already up and running. The link information for each
3219 * interface is passed in link_info.
3220 */
3221static int setup_nic_devices(struct octeon_device *octeon_dev)
3222{
3223 struct lio *lio = NULL;
3224 struct net_device *netdev;
3225 u8 mac[6], i, j;
3226 struct octeon_soft_command *sc;
3227 struct liquidio_if_cfg_context *ctx;
3228 struct liquidio_if_cfg_resp *resp;
3229 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003230 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003231 union oct_nic_if_cfg if_cfg;
3232 unsigned int base_queue;
3233 unsigned int gmx_port_id;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003234 u32 resp_size, ctx_size, data_size;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003235 u32 ifidx_or_pfnum;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003236 struct lio_version *vdata;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003237
3238 /* This is to handle link status changes */
3239 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3240 OPCODE_NIC_INFO,
3241 lio_nic_info, octeon_dev);
3242
3243 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3244 * They are handled directly.
3245 */
3246 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3247 free_netbuf);
3248
3249 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3250 free_netsgbuf);
3251
3252 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3253 free_netsgbuf_with_resp);
3254
3255 for (i = 0; i < octeon_dev->ifcount; i++) {
3256 resp_size = sizeof(struct liquidio_if_cfg_resp);
3257 ctx_size = sizeof(struct liquidio_if_cfg_context);
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003258 data_size = sizeof(struct lio_version);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003259 sc = (struct octeon_soft_command *)
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003260 octeon_alloc_soft_command(octeon_dev, data_size,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003261 resp_size, ctx_size);
3262 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3263 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
Raghu Vatsavayi83101ce2016-08-31 11:03:21 -07003264 vdata = (struct lio_version *)sc->virtdptr;
3265
3266 *((u64 *)vdata) = 0;
3267 vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3268 vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3269 vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003270
3271 num_iqueues =
3272 CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3273 num_oqueues =
3274 CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3275 base_queue =
3276 CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
3277 gmx_port_id =
3278 CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003279 ifidx_or_pfnum = i;
Raghu Vatsavayi3dcef2c2016-07-03 13:56:51 -07003280
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003281 dev_dbg(&octeon_dev->pci_dev->dev,
3282 "requesting config for interface %d, iqs %d, oqs %d\n",
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003283 ifidx_or_pfnum, num_iqueues, num_oqueues);
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -07003284 WRITE_ONCE(ctx->cond, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003285 ctx->octeon_id = lio_get_device_id(octeon_dev);
3286 init_waitqueue_head(&ctx->wc);
3287
3288 if_cfg.u64 = 0;
3289 if_cfg.s.num_iqueues = num_iqueues;
3290 if_cfg.s.num_oqueues = num_oqueues;
3291 if_cfg.s.base_queue = base_queue;
3292 if_cfg.s.gmx_port_id = gmx_port_id;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003293
3294 sc->iq_no = 0;
3295
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003296 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003297 OPCODE_NIC_IF_CFG, 0,
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003298 if_cfg.u64, 0);
3299
3300 sc->callback = if_cfg_callback;
3301 sc->callback_arg = sc;
Raghu Vatsavayi55893a62016-07-03 13:56:50 -07003302 sc->wait_time = 3000;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003303
3304 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003305 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003306 dev_err(&octeon_dev->pci_dev->dev,
3307 "iq/oq config failed status: %x\n",
3308 retval);
3309 /* Soft instr is freed by driver in case of failure. */
3310 goto setup_nic_dev_fail;
3311 }
3312
3313 /* Sleep on a wait queue till the cond flag indicates that the
3314 * response arrived or timed-out.
3315 */
3316 sleep_cond(&ctx->wc, &ctx->cond);
3317 retval = resp->status;
3318 if (retval) {
3319 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3320 goto setup_nic_dev_fail;
3321 }
3322
3323 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3324 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3325
3326 num_iqueues = hweight64(resp->cfg_info.iqmask);
3327 num_oqueues = hweight64(resp->cfg_info.oqmask);
3328
3329 if (!(num_iqueues) || !(num_oqueues)) {
3330 dev_err(&octeon_dev->pci_dev->dev,
3331 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3332 resp->cfg_info.iqmask,
3333 resp->cfg_info.oqmask);
3334 goto setup_nic_dev_fail;
3335 }
3336 dev_dbg(&octeon_dev->pci_dev->dev,
3337 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3338 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3339 num_iqueues, num_oqueues);
3340 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3341
3342 if (!netdev) {
3343 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3344 goto setup_nic_dev_fail;
3345 }
3346
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003347 SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003348
3349 if (num_iqueues > 1)
3350 lionetdevops.ndo_select_queue = select_q;
3351
3352 /* Associate the routines that will handle different
3353 * netdev tasks.
3354 */
3355 netdev->netdev_ops = &lionetdevops;
3356
3357 lio = GET_LIO(netdev);
3358
3359 memset(lio, 0, sizeof(struct lio));
3360
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003361 lio->ifidx = ifidx_or_pfnum;
3362
3363 props = &octeon_dev->props[i];
3364 props->gmxport = resp->cfg_info.linfo.gmxport;
3365 props->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003366
3367 lio->linfo.num_rxpciq = num_oqueues;
3368 lio->linfo.num_txpciq = num_iqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003369 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003370 lio->linfo.rxpciq[j].u64 =
3371 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003372 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003373 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003374 lio->linfo.txpciq[j].u64 =
3375 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003376 }
3377 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3378 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3379 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3380
3381 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3382
3383 lio->dev_capability = NETIF_F_HIGHDMA
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003384 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3385 | NETIF_F_SG | NETIF_F_RXCSUM
3386 | NETIF_F_GRO
3387 | NETIF_F_TSO | NETIF_F_TSO6
3388 | NETIF_F_LRO;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003389 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3390
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003391 /* Copy of transmit encapsulation capabilities:
3392 * TSO, TSO6, Checksums for this device
3393 */
3394 lio->enc_dev_capability = NETIF_F_IP_CSUM
3395 | NETIF_F_IPV6_CSUM
3396 | NETIF_F_GSO_UDP_TUNNEL
3397 | NETIF_F_HW_CSUM | NETIF_F_SG
3398 | NETIF_F_RXCSUM
3399 | NETIF_F_TSO | NETIF_F_TSO6
3400 | NETIF_F_LRO;
3401
3402 netdev->hw_enc_features = (lio->enc_dev_capability &
3403 ~NETIF_F_LRO);
3404
3405 lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3406
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003407 netdev->vlan_features = lio->dev_capability;
3408 /* Add any unchangeable hw features */
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003409 lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
3410 NETIF_F_HW_VLAN_CTAG_RX |
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003411 NETIF_F_HW_VLAN_CTAG_TX;
3412
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003413 netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3414
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003415 netdev->hw_features = lio->dev_capability;
Raghu Vatsavayi0da0b772016-06-21 22:53:04 -07003416 /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3417 netdev->hw_features = netdev->hw_features &
3418 ~NETIF_F_HW_VLAN_CTAG_RX;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003419
3420 /* Point to the properties for octeon device to which this
3421 * interface belongs.
3422 */
3423 lio->oct_dev = octeon_dev;
3424 lio->octprops = props;
3425 lio->netdev = netdev;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003426
3427 dev_dbg(&octeon_dev->pci_dev->dev,
3428 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3429 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3430
3431 /* 64-bit swap required on LE machines */
3432 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3433 for (j = 0; j < 6; j++)
3434 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3435
3436 /* Copy MAC Address to OS network device structure */
3437
3438 ether_addr_copy(netdev->dev_addr, mac);
3439
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003440 /* By default all interfaces on a single Octeon uses the same
3441 * tx and rx queues
3442 */
3443 lio->txq = lio->linfo.txpciq[0].s.q_no;
3444 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003445 if (setup_io_queues(octeon_dev, i)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003446 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3447 goto setup_nic_dev_fail;
3448 }
3449
3450 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3451
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003452 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3453 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3454
Raghu Vatsavayifcd2b5e2016-06-14 16:54:45 -07003455 if (setup_glists(octeon_dev, lio, num_iqueues)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003456 dev_err(&octeon_dev->pci_dev->dev,
3457 "Gather list allocation failed\n");
3458 goto setup_nic_dev_fail;
3459 }
3460
3461 /* Register ethtool support */
3462 liquidio_set_ethtool_ops(netdev);
Raghu Vatsavayif5a20472016-06-21 22:53:14 -07003463 octeon_dev->priv_flags = 0x0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003464
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003465 if (netdev->features & NETIF_F_LRO)
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -07003466 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3467 OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003468
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003469 liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
3470
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003471 if ((debug != -1) && (debug & NETIF_MSG_HW))
Raghu Vatsavayi63245f22016-06-21 22:53:05 -07003472 liquidio_set_feature(netdev,
3473 OCTNET_CMD_VERBOSE_ENABLE, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003474
3475 /* Register the network device with the OS */
3476 if (register_netdev(netdev)) {
3477 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3478 goto setup_nic_dev_fail;
3479 }
3480
3481 dev_dbg(&octeon_dev->pci_dev->dev,
3482 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3483 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3484 netif_carrier_off(netdev);
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003485 lio->link_changes++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003486
3487 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3488
Raghu Vatsavayi01fb2372016-07-03 13:56:47 -07003489 /* Sending command to firmware to enable Rx checksum offload
3490 * by default at the time of setup of Liquidio driver for
3491 * this device
3492 */
3493 liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3494 OCTNET_CMD_RXCSUM_ENABLE);
3495 liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3496 OCTNET_CMD_TXCSUM_ENABLE);
3497
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003498 dev_dbg(&octeon_dev->pci_dev->dev,
3499 "NIC ifidx:%d Setup successful\n", i);
3500
3501 octeon_free_soft_command(octeon_dev, sc);
3502 }
3503
3504 return 0;
3505
3506setup_nic_dev_fail:
3507
3508 octeon_free_soft_command(octeon_dev, sc);
3509
3510 while (i--) {
3511 dev_err(&octeon_dev->pci_dev->dev,
3512 "NIC ifidx:%d Setup failed\n", i);
3513 liquidio_destroy_nic_device(octeon_dev, i);
3514 }
3515 return -ENODEV;
3516}
3517
3518/**
3519 * \brief initialize the NIC
3520 * @param oct octeon device
3521 *
3522 * This initialization routine is called once the Octeon device application is
3523 * up and running
3524 */
3525static int liquidio_init_nic_module(struct octeon_device *oct)
3526{
3527 struct oct_intrmod_cfg *intrmod_cfg;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003528 int i, retval = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003529 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3530
3531 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3532
3533 /* only default iq and oq were initialized
3534 * initialize the rest as well
3535 */
3536 /* run port_config command for each port */
3537 oct->ifcount = num_nic_ports;
3538
3539 memset(oct->props, 0,
3540 sizeof(struct octdev_props) * num_nic_ports);
3541
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -07003542 for (i = 0; i < MAX_OCTEON_LINKS; i++)
3543 oct->props[i].gmxport = -1;
3544
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003545 retval = setup_nic_devices(oct);
3546 if (retval) {
3547 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3548 goto octnet_init_failure;
3549 }
3550
3551 liquidio_ptp_init(oct);
3552
3553 /* Initialize interrupt moderation params */
3554 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
Raghu Vatsavayi78e6a9b2016-06-21 22:53:10 -07003555 intrmod_cfg->rx_enable = 1;
3556 intrmod_cfg->check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3557 intrmod_cfg->maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3558 intrmod_cfg->minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3559 intrmod_cfg->rx_maxcnt_trigger = LIO_INTRMOD_RXMAXCNT_TRIGGER;
3560 intrmod_cfg->rx_maxtmr_trigger = LIO_INTRMOD_RXMAXTMR_TRIGGER;
3561 intrmod_cfg->rx_mintmr_trigger = LIO_INTRMOD_RXMINTMR_TRIGGER;
3562 intrmod_cfg->rx_mincnt_trigger = LIO_INTRMOD_RXMINCNT_TRIGGER;
3563 intrmod_cfg->tx_enable = 1;
3564 intrmod_cfg->tx_maxcnt_trigger = LIO_INTRMOD_TXMAXCNT_TRIGGER;
3565 intrmod_cfg->tx_mincnt_trigger = LIO_INTRMOD_TXMINCNT_TRIGGER;
3566 intrmod_cfg->rx_frames = CFG_GET_OQ_INTR_PKT(octeon_get_conf(oct));
3567 intrmod_cfg->rx_usecs = CFG_GET_OQ_INTR_TIME(octeon_get_conf(oct));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003568 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3569
3570 return retval;
3571
3572octnet_init_failure:
3573
3574 oct->ifcount = 0;
3575
3576 return retval;
3577}
3578
3579/**
3580 * \brief starter callback that invokes the remaining initialization work after
3581 * the NIC is up and running.
3582 * @param octptr work struct work_struct
3583 */
3584static void nic_starter(struct work_struct *work)
3585{
3586 struct octeon_device *oct;
3587 struct cavium_wk *wk = (struct cavium_wk *)work;
3588
3589 oct = (struct octeon_device *)wk->ctxptr;
3590
3591 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3592 return;
3593
3594 /* If the status of the device is CORE_OK, the core
3595 * application has reported its application type. Call
3596 * any registered handlers now and move to the RUNNING
3597 * state.
3598 */
3599 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3600 schedule_delayed_work(&oct->nic_poll_work.work,
3601 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3602 return;
3603 }
3604
3605 atomic_set(&oct->status, OCT_DEV_RUNNING);
3606
3607 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3608 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3609
3610 if (liquidio_init_nic_module(oct))
3611 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3612 else
3613 handshake[oct->octeon_id].started_ok = 1;
3614 } else {
3615 dev_err(&oct->pci_dev->dev,
3616 "Unexpected application running on NIC (%d). Check firmware.\n",
3617 oct->app_mode);
3618 }
3619
3620 complete(&handshake[oct->octeon_id].started);
3621}
3622
3623/**
3624 * \brief Device initialization for each Octeon device that is probed
3625 * @param octeon_dev octeon device
3626 */
3627static int octeon_device_init(struct octeon_device *octeon_dev)
3628{
3629 int j, ret;
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07003630 char bootcmd[] = "\n";
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003631 struct octeon_device_priv *oct_priv =
3632 (struct octeon_device_priv *)octeon_dev->priv;
3633 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3634
3635 /* Enable access to the octeon device and make its DMA capability
3636 * known to the OS.
3637 */
3638 if (octeon_pci_os_setup(octeon_dev))
3639 return 1;
3640
3641 /* Identify the Octeon type and map the BAR address space. */
3642 if (octeon_chip_specific_setup(octeon_dev)) {
3643 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3644 return 1;
3645 }
3646
3647 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3648
3649 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3650
3651 /* Do a soft reset of the Octeon device. */
3652 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3653 return 1;
3654
3655 /* Initialize the dispatch mechanism used to push packets arriving on
3656 * Octeon Output queues.
3657 */
3658 if (octeon_init_dispatch_list(octeon_dev))
3659 return 1;
3660
3661 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3662 OPCODE_NIC_CORE_DRV_ACTIVE,
3663 octeon_core_drv_init,
3664 octeon_dev);
3665
3666 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3667 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3668 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3669 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3670
3671 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3672
3673 octeon_set_io_queues_off(octeon_dev);
3674
3675 /* Setup the data structures that manage this Octeon's Input queues. */
3676 if (octeon_setup_instr_queues(octeon_dev)) {
3677 dev_err(&octeon_dev->pci_dev->dev,
3678 "instruction queue initialization failed\n");
3679 /* On error, release any previously allocated queues */
3680 for (j = 0; j < octeon_dev->num_iqs; j++)
3681 octeon_delete_instr_queue(octeon_dev, j);
3682 return 1;
3683 }
3684 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3685
3686 /* Initialize soft command buffer pool
3687 */
3688 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3689 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3690 return 1;
3691 }
3692 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3693
3694 /* Initialize lists to manage the requests of different types that
3695 * arrive from user & kernel applications for this octeon device.
3696 */
3697 if (octeon_setup_response_list(octeon_dev)) {
3698 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3699 return 1;
3700 }
3701 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3702
3703 if (octeon_setup_output_queues(octeon_dev)) {
3704 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
3705 /* Release any previously allocated queues */
3706 for (j = 0; j < octeon_dev->num_oqs; j++)
3707 octeon_delete_droq(octeon_dev, j);
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07003708 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003709 }
3710
3711 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
3712
3713 /* The input and output queue registers were setup earlier (the queues
3714 * were not enabled). Any additional registers that need to be
3715 * programmed should be done now.
3716 */
3717 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3718 if (ret) {
3719 dev_err(&octeon_dev->pci_dev->dev,
3720 "Failed to configure device registers\n");
3721 return ret;
3722 }
3723
3724 /* Initialize the tasklet that handles output queue packet processing.*/
3725 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
3726 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
3727 (unsigned long)octeon_dev);
3728
3729 /* Setup the interrupt handler and record the INT SUM register address
3730 */
Raghu Vatsavayi1e0d30f2016-07-03 13:56:52 -07003731 if (octeon_setup_interrupt(octeon_dev))
3732 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003733
3734 /* Enable Octeon device interrupts */
3735 octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
3736
3737 /* Enable the input and output queues for this Octeon device */
3738 octeon_dev->fn_list.enable_io_queues(octeon_dev);
3739
3740 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
3741
3742 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
3743
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07003744 if (ddr_timeout == 0)
3745 dev_info(&octeon_dev->pci_dev->dev, "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003746
3747 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
3748
3749 /* Wait for the octeon to initialize DDR after the soft-reset. */
Raghu Vatsavayi4b129ae2016-06-21 22:53:15 -07003750 while (ddr_timeout == 0) {
3751 set_current_state(TASK_INTERRUPTIBLE);
3752 if (schedule_timeout(HZ / 10)) {
3753 /* user probably pressed Control-C */
3754 return 1;
3755 }
3756 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003757 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
3758 if (ret) {
3759 dev_err(&octeon_dev->pci_dev->dev,
3760 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
3761 ret);
3762 return 1;
3763 }
3764
3765 if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
3766 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
3767 return 1;
3768 }
3769
Raghu Vatsavayid3d7e6c2016-06-21 22:53:07 -07003770 /* Divert uboot to take commands from host instead. */
3771 ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
3772
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003773 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
3774 ret = octeon_init_consoles(octeon_dev);
3775 if (ret) {
3776 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
3777 return 1;
3778 }
3779 ret = octeon_add_console(octeon_dev, 0);
3780 if (ret) {
3781 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
3782 return 1;
3783 }
3784
3785 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
3786
3787 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
3788 ret = load_firmware(octeon_dev);
3789 if (ret) {
3790 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
3791 return 1;
3792 }
3793
3794 handshake[octeon_dev->octeon_id].init_ok = 1;
3795 complete(&handshake[octeon_dev->octeon_id].init);
3796
3797 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
3798
3799 /* Send Credit for Octeon Output queues. Credits are always sent after
3800 * the output queue is enabled.
3801 */
3802 for (j = 0; j < octeon_dev->num_oqs; j++)
3803 writel(octeon_dev->droq[j]->max_count,
3804 octeon_dev->droq[j]->pkts_credit_reg);
3805
3806 /* Packets can start arriving on the output queues from this point. */
3807
3808 return 0;
3809}
3810
3811/**
3812 * \brief Exits the module
3813 */
3814static void __exit liquidio_exit(void)
3815{
3816 liquidio_deinit_pci();
3817
3818 pr_info("LiquidIO network module is now unloaded\n");
3819}
3820
3821module_init(liquidio_init);
3822module_exit(liquidio_exit);