blob: 3477a3c15b5672679df5fd1c7942e5bc27e97944 [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>
23#include <linux/module.h>
24#include <linux/crc32.h>
25#include <linux/dma-mapping.h>
26#include <linux/pci.h>
27#include <linux/pci_ids.h>
28#include <linux/ip.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070029#include <net/ip.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070030#include <linux/ipv6.h>
31#include <linux/net_tstamp.h>
32#include <linux/if_vlan.h>
33#include <linux/firmware.h>
34#include <linux/ethtool.h>
35#include <linux/ptp_clock_kernel.h>
36#include <linux/types.h>
37#include <linux/list.h>
38#include <linux/workqueue.h>
39#include <linux/interrupt.h>
40#include "octeon_config.h"
41#include "liquidio_common.h"
42#include "octeon_droq.h"
43#include "octeon_iq.h"
44#include "response_manager.h"
45#include "octeon_device.h"
46#include "octeon_nic.h"
47#include "octeon_main.h"
48#include "octeon_network.h"
49#include "cn66xx_regs.h"
50#include "cn66xx_device.h"
51#include "cn68xx_regs.h"
52#include "cn68xx_device.h"
53#include "liquidio_image.h"
54
55MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
56MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
57MODULE_LICENSE("GPL");
58MODULE_VERSION(LIQUIDIO_VERSION);
59MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
60MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
61MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
62
63static int ddr_timeout = 10000;
64module_param(ddr_timeout, int, 0644);
65MODULE_PARM_DESC(ddr_timeout,
66 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
67
68static u32 console_bitmask;
69module_param(console_bitmask, int, 0644);
70MODULE_PARM_DESC(console_bitmask,
71 "Bitmask indicating which consoles have debug output redirected to syslog.");
72
73#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
74
75static int debug = -1;
76module_param(debug, int, 0644);
77MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
78
79static char fw_type[LIO_MAX_FW_TYPE_LEN];
80module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
81MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
82
83static int conf_type;
84module_param(conf_type, int, 0);
85MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
86
87/* Bit mask values for lio->ifstate */
88#define LIO_IFSTATE_DROQ_OPS 0x01
89#define LIO_IFSTATE_REGISTERED 0x02
90#define LIO_IFSTATE_RUNNING 0x04
91#define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
92
93/* Polling interval for determining when NIC application is alive */
94#define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
95
96/* runtime link query interval */
97#define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
98
99struct liquidio_if_cfg_context {
100 int octeon_id;
101
102 wait_queue_head_t wc;
103
104 int cond;
105};
106
107struct liquidio_if_cfg_resp {
108 u64 rh;
109 struct liquidio_if_cfg_info cfg_info;
110 u64 status;
111};
112
113struct oct_link_status_resp {
114 u64 rh;
115 struct oct_link_info link_info;
116 u64 status;
117};
118
119struct oct_timestamp_resp {
120 u64 rh;
121 u64 timestamp;
122 u64 status;
123};
124
125#define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
126
127union tx_info {
128 u64 u64;
129 struct {
130#ifdef __BIG_ENDIAN_BITFIELD
131 u16 gso_size;
132 u16 gso_segs;
133 u32 reserved;
134#else
135 u32 reserved;
136 u16 gso_segs;
137 u16 gso_size;
138#endif
139 } s;
140};
141
142/** Octeon device properties to be used by the NIC module.
143 * Each octeon device in the system will be represented
144 * by this structure in the NIC module.
145 */
146
147#define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
148
149#define OCTNIC_GSO_MAX_HEADER_SIZE 128
150#define OCTNIC_GSO_MAX_SIZE (GSO_MAX_SIZE - OCTNIC_GSO_MAX_HEADER_SIZE)
151
152/** Structure of a node in list of gather components maintained by
153 * NIC driver for each network device.
154 */
155struct octnic_gather {
156 /** List manipulation. Next and prev pointers. */
157 struct list_head list;
158
159 /** Size of the gather component at sg in bytes. */
160 int sg_size;
161
162 /** Number of bytes that sg was adjusted to make it 8B-aligned. */
163 int adjust;
164
165 /** Gather component that can accommodate max sized fragment list
166 * received from the IP layer.
167 */
168 struct octeon_sg_entry *sg;
169};
170
171/** This structure is used by NIC driver to store information required
172 * to free the sk_buff when the packet has been fetched by Octeon.
173 * Bytes offset below assume worst-case of a 64-bit system.
174 */
175struct octnet_buf_free_info {
176 /** Bytes 1-8. Pointer to network device private structure. */
177 struct lio *lio;
178
179 /** Bytes 9-16. Pointer to sk_buff. */
180 struct sk_buff *skb;
181
182 /** Bytes 17-24. Pointer to gather list. */
183 struct octnic_gather *g;
184
185 /** Bytes 25-32. Physical address of skb->data or gather list. */
186 u64 dptr;
187
188 /** Bytes 33-47. Piggybacked soft command, if any */
189 struct octeon_soft_command *sc;
190};
191
192struct handshake {
193 struct completion init;
194 struct completion started;
195 struct pci_dev *pci_dev;
196 int init_ok;
197 int started_ok;
198};
199
200struct octeon_device_priv {
201 /** Tasklet structures for this device. */
202 struct tasklet_struct droq_tasklet;
203 unsigned long napi_mask;
204};
205
206static int octeon_device_init(struct octeon_device *);
207static void liquidio_remove(struct pci_dev *pdev);
208static int liquidio_probe(struct pci_dev *pdev,
209 const struct pci_device_id *ent);
210
211static struct handshake handshake[MAX_OCTEON_DEVICES];
212static struct completion first_stage;
213
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700214static void octeon_droq_bh(unsigned long pdev)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700215{
216 int q_no;
217 int reschedule = 0;
218 struct octeon_device *oct = (struct octeon_device *)pdev;
219 struct octeon_device_priv *oct_priv =
220 (struct octeon_device_priv *)oct->priv;
221
222 /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
223 for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES; q_no++) {
224 if (!(oct->io_qmask.oq & (1UL << q_no)))
225 continue;
226 reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
227 MAX_PACKET_BUDGET);
228 }
229
230 if (reschedule)
231 tasklet_schedule(&oct_priv->droq_tasklet);
232}
233
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -0700234static int lio_wait_for_oq_pkts(struct octeon_device *oct)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700235{
236 struct octeon_device_priv *oct_priv =
237 (struct octeon_device_priv *)oct->priv;
238 int retry = 100, pkt_cnt = 0, pending_pkts = 0;
239 int i;
240
241 do {
242 pending_pkts = 0;
243
244 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
245 if (!(oct->io_qmask.oq & (1UL << i)))
246 continue;
247 pkt_cnt += octeon_droq_check_hw_for_pkts(oct,
248 oct->droq[i]);
249 }
250 if (pkt_cnt > 0) {
251 pending_pkts += pkt_cnt;
252 tasklet_schedule(&oct_priv->droq_tasklet);
253 }
254 pkt_cnt = 0;
255 schedule_timeout_uninterruptible(1);
256
257 } while (retry-- && pending_pkts);
258
259 return pkt_cnt;
260}
261
262void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
263 unsigned int bytes_compl)
264{
265 struct netdev_queue *netdev_queue = txq;
266
267 netdev_tx_completed_queue(netdev_queue, pkts_compl, bytes_compl);
268}
269
270void octeon_update_tx_completion_counters(void *buf, int reqtype,
271 unsigned int *pkts_compl,
272 unsigned int *bytes_compl)
273{
274 struct octnet_buf_free_info *finfo;
275 struct sk_buff *skb = NULL;
276 struct octeon_soft_command *sc;
277
278 switch (reqtype) {
279 case REQTYPE_NORESP_NET:
280 case REQTYPE_NORESP_NET_SG:
281 finfo = buf;
282 skb = finfo->skb;
283 break;
284
285 case REQTYPE_RESP_NET_SG:
286 case REQTYPE_RESP_NET:
287 sc = buf;
288 skb = sc->callback_arg;
289 break;
290
291 default:
292 return;
293 }
294
295 (*pkts_compl)++;
296 *bytes_compl += skb->len;
297}
298
299void octeon_report_sent_bytes_to_bql(void *buf, int reqtype)
300{
301 struct octnet_buf_free_info *finfo;
302 struct sk_buff *skb;
303 struct octeon_soft_command *sc;
304 struct netdev_queue *txq;
305
306 switch (reqtype) {
307 case REQTYPE_NORESP_NET:
308 case REQTYPE_NORESP_NET_SG:
309 finfo = buf;
310 skb = finfo->skb;
311 break;
312
313 case REQTYPE_RESP_NET_SG:
314 case REQTYPE_RESP_NET:
315 sc = buf;
316 skb = sc->callback_arg;
317 break;
318
319 default:
320 return;
321 }
322
323 txq = netdev_get_tx_queue(skb->dev, skb_get_queue_mapping(skb));
324 netdev_tx_sent_queue(txq, skb->len);
325}
326
327int octeon_console_debug_enabled(u32 console)
328{
329 return (console_bitmask >> (console)) & 0x1;
330}
331
332/**
333 * \brief Forces all IO queues off on a given device
334 * @param oct Pointer to Octeon device
335 */
336static void force_io_queues_off(struct octeon_device *oct)
337{
338 if ((oct->chip_id == OCTEON_CN66XX) ||
339 (oct->chip_id == OCTEON_CN68XX)) {
340 /* Reset the Enable bits for Input Queues. */
341 octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
342
343 /* Reset the Enable bits for Output Queues. */
344 octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
345 }
346}
347
348/**
349 * \brief wait for all pending requests to complete
350 * @param oct Pointer to Octeon device
351 *
352 * Called during shutdown sequence
353 */
354static int wait_for_pending_requests(struct octeon_device *oct)
355{
356 int i, pcount = 0;
357
358 for (i = 0; i < 100; i++) {
359 pcount =
360 atomic_read(&oct->response_list
361 [OCTEON_ORDERED_SC_LIST].pending_req_count);
362 if (pcount)
363 schedule_timeout_uninterruptible(HZ / 10);
364 else
365 break;
366 }
367
368 if (pcount)
369 return 1;
370
371 return 0;
372}
373
374/**
375 * \brief Cause device to go quiet so it can be safely removed/reset/etc
376 * @param oct Pointer to Octeon device
377 */
378static inline void pcierror_quiesce_device(struct octeon_device *oct)
379{
380 int i;
381
382 /* Disable the input and output queues now. No more packets will
383 * arrive from Octeon, but we should wait for all packet processing
384 * to finish.
385 */
386 force_io_queues_off(oct);
387
388 /* To allow for in-flight requests */
389 schedule_timeout_uninterruptible(100);
390
391 if (wait_for_pending_requests(oct))
392 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
393
394 /* Force all requests waiting to be fetched by OCTEON to complete. */
395 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
396 struct octeon_instr_queue *iq;
397
398 if (!(oct->io_qmask.iq & (1UL << i)))
399 continue;
400 iq = oct->instr_queue[i];
401
402 if (atomic_read(&iq->instr_pending)) {
403 spin_lock_bh(&iq->lock);
404 iq->fill_cnt = 0;
405 iq->octeon_read_index = iq->host_write_index;
406 iq->stats.instr_processed +=
407 atomic_read(&iq->instr_pending);
408 lio_process_iq_request_list(oct, iq);
409 spin_unlock_bh(&iq->lock);
410 }
411 }
412
413 /* Force all pending ordered list requests to time out. */
414 lio_process_ordered_list(oct, 1);
415
416 /* We do not need to wait for output queue packets to be processed. */
417}
418
419/**
420 * \brief Cleanup PCI AER uncorrectable error status
421 * @param dev Pointer to PCI device
422 */
423static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
424{
425 int pos = 0x100;
426 u32 status, mask;
427
428 pr_info("%s :\n", __func__);
429
430 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
431 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
432 if (dev->error_state == pci_channel_io_normal)
433 status &= ~mask; /* Clear corresponding nonfatal bits */
434 else
435 status &= mask; /* Clear corresponding fatal bits */
436 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
437}
438
439/**
440 * \brief Stop all PCI IO to a given device
441 * @param dev Pointer to Octeon device
442 */
443static void stop_pci_io(struct octeon_device *oct)
444{
445 /* No more instructions will be forwarded. */
446 atomic_set(&oct->status, OCT_DEV_IN_RESET);
447
448 pci_disable_device(oct->pci_dev);
449
450 /* Disable interrupts */
451 oct->fn_list.disable_interrupt(oct->chip);
452
453 pcierror_quiesce_device(oct);
454
455 /* Release the interrupt line */
456 free_irq(oct->pci_dev->irq, oct);
457
458 if (oct->flags & LIO_FLAG_MSI_ENABLED)
459 pci_disable_msi(oct->pci_dev);
460
461 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
462 lio_get_state_string(&oct->status));
463
464 /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
465 /* making it a common function for all OCTEON models */
466 cleanup_aer_uncorrect_error_status(oct->pci_dev);
467}
468
469/**
470 * \brief called when PCI error is detected
471 * @param pdev Pointer to PCI device
472 * @param state The current pci connection state
473 *
474 * This function is called after a PCI bus error affecting
475 * this device has been detected.
476 */
477static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
478 pci_channel_state_t state)
479{
480 struct octeon_device *oct = pci_get_drvdata(pdev);
481
482 /* Non-correctable Non-fatal errors */
483 if (state == pci_channel_io_normal) {
484 dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
485 cleanup_aer_uncorrect_error_status(oct->pci_dev);
486 return PCI_ERS_RESULT_CAN_RECOVER;
487 }
488
489 /* Non-correctable Fatal errors */
490 dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
491 stop_pci_io(oct);
492
493 /* Always return a DISCONNECT. There is no support for recovery but only
494 * for a clean shutdown.
495 */
496 return PCI_ERS_RESULT_DISCONNECT;
497}
498
499/**
500 * \brief mmio handler
501 * @param pdev Pointer to PCI device
502 */
503static pci_ers_result_t liquidio_pcie_mmio_enabled(struct pci_dev *pdev)
504{
505 /* We should never hit this since we never ask for a reset for a Fatal
506 * Error. We always return DISCONNECT in io_error above.
507 * But play safe and return RECOVERED for now.
508 */
509 return PCI_ERS_RESULT_RECOVERED;
510}
511
512/**
513 * \brief called after the pci bus has been reset.
514 * @param pdev Pointer to PCI device
515 *
516 * Restart the card from scratch, as if from a cold-boot. Implementation
517 * resembles the first-half of the octeon_resume routine.
518 */
519static pci_ers_result_t liquidio_pcie_slot_reset(struct pci_dev *pdev)
520{
521 /* We should never hit this since we never ask for a reset for a Fatal
522 * Error. We always return DISCONNECT in io_error above.
523 * But play safe and return RECOVERED for now.
524 */
525 return PCI_ERS_RESULT_RECOVERED;
526}
527
528/**
529 * \brief called when traffic can start flowing again.
530 * @param pdev Pointer to PCI device
531 *
532 * This callback is called when the error recovery driver tells us that
533 * its OK to resume normal operation. Implementation resembles the
534 * second-half of the octeon_resume routine.
535 */
536static void liquidio_pcie_resume(struct pci_dev *pdev)
537{
538 /* Nothing to be done here. */
539}
540
541#ifdef CONFIG_PM
542/**
543 * \brief called when suspending
544 * @param pdev Pointer to PCI device
545 * @param state state to suspend to
546 */
547static int liquidio_suspend(struct pci_dev *pdev, pm_message_t state)
548{
549 return 0;
550}
551
552/**
553 * \brief called when resuming
554 * @param pdev Pointer to PCI device
555 */
556static int liquidio_resume(struct pci_dev *pdev)
557{
558 return 0;
559}
560#endif
561
562/* For PCI-E Advanced Error Recovery (AER) Interface */
Julia Lawall166e2362015-11-14 11:06:53 +0100563static const struct pci_error_handlers liquidio_err_handler = {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700564 .error_detected = liquidio_pcie_error_detected,
565 .mmio_enabled = liquidio_pcie_mmio_enabled,
566 .slot_reset = liquidio_pcie_slot_reset,
567 .resume = liquidio_pcie_resume,
568};
569
570static const struct pci_device_id liquidio_pci_tbl[] = {
571 { /* 68xx */
572 PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
573 },
574 { /* 66xx */
575 PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
576 },
577 {
578 0, 0, 0, 0, 0, 0, 0
579 }
580};
581MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
582
583static struct pci_driver liquidio_pci_driver = {
584 .name = "LiquidIO",
585 .id_table = liquidio_pci_tbl,
586 .probe = liquidio_probe,
587 .remove = liquidio_remove,
588 .err_handler = &liquidio_err_handler, /* For AER */
589
590#ifdef CONFIG_PM
591 .suspend = liquidio_suspend,
592 .resume = liquidio_resume,
593#endif
594
595};
596
597/**
598 * \brief register PCI driver
599 */
600static int liquidio_init_pci(void)
601{
602 return pci_register_driver(&liquidio_pci_driver);
603}
604
605/**
606 * \brief unregister PCI driver
607 */
608static void liquidio_deinit_pci(void)
609{
610 pci_unregister_driver(&liquidio_pci_driver);
611}
612
613/**
614 * \brief check interface state
615 * @param lio per-network private data
616 * @param state_flag flag state to check
617 */
618static inline int ifstate_check(struct lio *lio, int state_flag)
619{
620 return atomic_read(&lio->ifstate) & state_flag;
621}
622
623/**
624 * \brief set interface state
625 * @param lio per-network private data
626 * @param state_flag flag state to set
627 */
628static inline void ifstate_set(struct lio *lio, int state_flag)
629{
630 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
631}
632
633/**
634 * \brief clear interface state
635 * @param lio per-network private data
636 * @param state_flag flag state to clear
637 */
638static inline void ifstate_reset(struct lio *lio, int state_flag)
639{
640 atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
641}
642
643/**
644 * \brief Stop Tx queues
645 * @param netdev network device
646 */
647static inline void txqs_stop(struct net_device *netdev)
648{
649 if (netif_is_multiqueue(netdev)) {
650 int i;
651
652 for (i = 0; i < netdev->num_tx_queues; i++)
653 netif_stop_subqueue(netdev, i);
654 } else {
655 netif_stop_queue(netdev);
656 }
657}
658
659/**
660 * \brief Start Tx queues
661 * @param netdev network device
662 */
663static inline void txqs_start(struct net_device *netdev)
664{
665 if (netif_is_multiqueue(netdev)) {
666 int i;
667
668 for (i = 0; i < netdev->num_tx_queues; i++)
669 netif_start_subqueue(netdev, i);
670 } else {
671 netif_start_queue(netdev);
672 }
673}
674
675/**
676 * \brief Wake Tx queues
677 * @param netdev network device
678 */
679static inline void txqs_wake(struct net_device *netdev)
680{
681 if (netif_is_multiqueue(netdev)) {
682 int i;
683
684 for (i = 0; i < netdev->num_tx_queues; i++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700685 if (__netif_subqueue_stopped(netdev, i))
686 netif_wake_subqueue(netdev, i);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700687 } else {
688 netif_wake_queue(netdev);
689 }
690}
691
692/**
693 * \brief Stop Tx queue
694 * @param netdev network device
695 */
696static void stop_txq(struct net_device *netdev)
697{
698 txqs_stop(netdev);
699}
700
701/**
702 * \brief Start Tx queue
703 * @param netdev network device
704 */
705static void start_txq(struct net_device *netdev)
706{
707 struct lio *lio = GET_LIO(netdev);
708
709 if (lio->linfo.link.s.status) {
710 txqs_start(netdev);
711 return;
712 }
713}
714
715/**
716 * \brief Wake a queue
717 * @param netdev network device
718 * @param q which queue to wake
719 */
720static inline void wake_q(struct net_device *netdev, int q)
721{
722 if (netif_is_multiqueue(netdev))
723 netif_wake_subqueue(netdev, q);
724 else
725 netif_wake_queue(netdev);
726}
727
728/**
729 * \brief Stop a queue
730 * @param netdev network device
731 * @param q which queue to stop
732 */
733static inline void stop_q(struct net_device *netdev, int q)
734{
735 if (netif_is_multiqueue(netdev))
736 netif_stop_subqueue(netdev, q);
737 else
738 netif_stop_queue(netdev);
739}
740
741/**
742 * \brief Check Tx queue status, and take appropriate action
743 * @param lio per-network private data
744 * @returns 0 if full, number of queues woken up otherwise
745 */
746static inline int check_txq_status(struct lio *lio)
747{
748 int ret_val = 0;
749
750 if (netif_is_multiqueue(lio->netdev)) {
751 int numqs = lio->netdev->num_tx_queues;
752 int q, iq = 0;
753
754 /* check each sub-queue state */
755 for (q = 0; q < numqs; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700756 iq = lio->linfo.txpciq[q %
757 (lio->linfo.num_txpciq)].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700758 if (octnet_iq_is_full(lio->oct_dev, iq))
759 continue;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -0700760 if (__netif_subqueue_stopped(lio->netdev, q)) {
761 wake_q(lio->netdev, q);
762 ret_val++;
763 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700764 }
765 } else {
766 if (octnet_iq_is_full(lio->oct_dev, lio->txq))
767 return 0;
768 wake_q(lio->netdev, lio->txq);
769 ret_val = 1;
770 }
771 return ret_val;
772}
773
774/**
775 * Remove the node at the head of the list. The list would be empty at
776 * the end of this call if there are no more nodes in the list.
777 */
778static inline struct list_head *list_delete_head(struct list_head *root)
779{
780 struct list_head *node;
781
782 if ((root->prev == root) && (root->next == root))
783 node = NULL;
784 else
785 node = root->next;
786
787 if (node)
788 list_del(node);
789
790 return node;
791}
792
793/**
794 * \brief Delete gather list
795 * @param lio per-network private data
796 */
797static void delete_glist(struct lio *lio)
798{
799 struct octnic_gather *g;
800
801 do {
802 g = (struct octnic_gather *)
803 list_delete_head(&lio->glist);
804 if (g) {
805 if (g->sg)
806 kfree((void *)((unsigned long)g->sg -
807 g->adjust));
808 kfree(g);
809 }
810 } while (g);
811}
812
813/**
814 * \brief Setup gather list
815 * @param lio per-network private data
816 */
817static int setup_glist(struct lio *lio)
818{
819 int i;
820 struct octnic_gather *g;
821
822 INIT_LIST_HEAD(&lio->glist);
823
824 for (i = 0; i < lio->tx_qsize; i++) {
Rasmus Villemoesce8e5c72015-09-09 10:38:02 +0200825 g = kzalloc(sizeof(*g), GFP_KERNEL);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700826 if (!g)
827 break;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700828
829 g->sg_size =
830 ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
831
832 g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
833 if (!g->sg) {
834 kfree(g);
835 break;
836 }
837
838 /* The gather component should be aligned on 64-bit boundary */
839 if (((unsigned long)g->sg) & 7) {
840 g->adjust = 8 - (((unsigned long)g->sg) & 7);
841 g->sg = (struct octeon_sg_entry *)
842 ((unsigned long)g->sg + g->adjust);
843 }
844 list_add_tail(&g->list, &lio->glist);
845 }
846
847 if (i == lio->tx_qsize)
848 return 0;
849
850 delete_glist(lio);
851 return 1;
852}
853
854/**
855 * \brief Print link information
856 * @param netdev network device
857 */
858static void print_link_info(struct net_device *netdev)
859{
860 struct lio *lio = GET_LIO(netdev);
861
862 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
863 struct oct_link_info *linfo = &lio->linfo;
864
865 if (linfo->link.s.status) {
866 netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
867 linfo->link.s.speed,
868 (linfo->link.s.duplex) ? "Full" : "Half");
869 } else {
870 netif_info(lio, link, lio->netdev, "Link Down\n");
871 }
872 }
873}
874
875/**
876 * \brief Update link status
877 * @param netdev network device
878 * @param ls link status structure
879 *
880 * Called on receipt of a link status response from the core application to
881 * update each interface's link status.
882 */
883static inline void update_link_status(struct net_device *netdev,
884 union oct_link_status *ls)
885{
886 struct lio *lio = GET_LIO(netdev);
887
888 if ((lio->intf_open) && (lio->linfo.link.u64 != ls->u64)) {
889 lio->linfo.link.u64 = ls->u64;
890
891 print_link_info(netdev);
892
893 if (lio->linfo.link.s.status) {
894 netif_carrier_on(netdev);
895 /* start_txq(netdev); */
896 txqs_wake(netdev);
897 } else {
898 netif_carrier_off(netdev);
899 stop_txq(netdev);
900 }
901 }
902}
903
904/**
905 * \brief Droq packet processor sceduler
906 * @param oct octeon device
907 */
908static
909void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
910{
911 struct octeon_device_priv *oct_priv =
912 (struct octeon_device_priv *)oct->priv;
913 u64 oq_no;
914 struct octeon_droq *droq;
915
916 if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
917 for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES; oq_no++) {
918 if (!(oct->droq_intr & (1 << oq_no)))
919 continue;
920
921 droq = oct->droq[oq_no];
922
923 if (droq->ops.poll_mode) {
924 droq->ops.napi_fn(droq);
925 oct_priv->napi_mask |= (1 << oq_no);
926 } else {
927 tasklet_schedule(&oct_priv->droq_tasklet);
928 }
929 }
930 }
931}
932
933/**
934 * \brief Interrupt handler for octeon
935 * @param irq unused
936 * @param dev octeon device
937 */
938static
939irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
940{
941 struct octeon_device *oct = (struct octeon_device *)dev;
942 irqreturn_t ret;
943
944 /* Disable our interrupts for the duration of ISR */
945 oct->fn_list.disable_interrupt(oct->chip);
946
947 ret = oct->fn_list.process_interrupt_regs(oct);
948
949 if (ret == IRQ_HANDLED)
950 liquidio_schedule_droq_pkt_handlers(oct);
951
952 /* Re-enable our interrupts */
953 if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
954 oct->fn_list.enable_interrupt(oct->chip);
955
956 return ret;
957}
958
959/**
960 * \brief Setup interrupt for octeon device
961 * @param oct octeon device
962 *
963 * Enable interrupt in Octeon device as given in the PCI interrupt mask.
964 */
965static int octeon_setup_interrupt(struct octeon_device *oct)
966{
967 int irqret, err;
968
969 err = pci_enable_msi(oct->pci_dev);
970 if (err)
971 dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
972 err);
973 else
974 oct->flags |= LIO_FLAG_MSI_ENABLED;
975
976 irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
977 IRQF_SHARED, "octeon", oct);
978 if (irqret) {
979 if (oct->flags & LIO_FLAG_MSI_ENABLED)
980 pci_disable_msi(oct->pci_dev);
981 dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
982 irqret);
983 return 1;
984 }
985
986 return 0;
987}
988
989/**
990 * \brief PCI probe handler
991 * @param pdev PCI device structure
992 * @param ent unused
993 */
994static int liquidio_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
995{
996 struct octeon_device *oct_dev = NULL;
997 struct handshake *hs;
998
999 oct_dev = octeon_allocate_device(pdev->device,
1000 sizeof(struct octeon_device_priv));
1001 if (!oct_dev) {
1002 dev_err(&pdev->dev, "Unable to allocate device\n");
1003 return -ENOMEM;
1004 }
1005
1006 dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1007 (u32)pdev->vendor, (u32)pdev->device);
1008
1009 /* Assign octeon_device for this device to the private data area. */
1010 pci_set_drvdata(pdev, oct_dev);
1011
1012 /* set linux specific device pointer */
1013 oct_dev->pci_dev = (void *)pdev;
1014
1015 hs = &handshake[oct_dev->octeon_id];
1016 init_completion(&hs->init);
1017 init_completion(&hs->started);
1018 hs->pci_dev = pdev;
1019
1020 if (oct_dev->octeon_id == 0)
1021 /* first LiquidIO NIC is detected */
1022 complete(&first_stage);
1023
1024 if (octeon_device_init(oct_dev)) {
1025 liquidio_remove(pdev);
1026 return -ENOMEM;
1027 }
1028
1029 dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1030
1031 return 0;
1032}
1033
1034/**
1035 *\brief Destroy resources associated with octeon device
1036 * @param pdev PCI device structure
1037 * @param ent unused
1038 */
1039static void octeon_destroy_resources(struct octeon_device *oct)
1040{
1041 int i;
1042 struct octeon_device_priv *oct_priv =
1043 (struct octeon_device_priv *)oct->priv;
1044
1045 struct handshake *hs;
1046
1047 switch (atomic_read(&oct->status)) {
1048 case OCT_DEV_RUNNING:
1049 case OCT_DEV_CORE_OK:
1050
1051 /* No more instructions will be forwarded. */
1052 atomic_set(&oct->status, OCT_DEV_IN_RESET);
1053
1054 oct->app_mode = CVM_DRV_INVALID_APP;
1055 dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1056 lio_get_state_string(&oct->status));
1057
1058 schedule_timeout_uninterruptible(HZ / 10);
1059
1060 /* fallthrough */
1061 case OCT_DEV_HOST_OK:
1062
1063 /* fallthrough */
1064 case OCT_DEV_CONSOLE_INIT_DONE:
1065 /* Remove any consoles */
1066 octeon_remove_consoles(oct);
1067
1068 /* fallthrough */
1069 case OCT_DEV_IO_QUEUES_DONE:
1070 if (wait_for_pending_requests(oct))
1071 dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1072
1073 if (lio_wait_for_instr_fetch(oct))
1074 dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1075
1076 /* Disable the input and output queues now. No more packets will
1077 * arrive from Octeon, but we should wait for all packet
1078 * processing to finish.
1079 */
1080 oct->fn_list.disable_io_queues(oct);
1081
1082 if (lio_wait_for_oq_pkts(oct))
1083 dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1084
1085 /* Disable interrupts */
1086 oct->fn_list.disable_interrupt(oct->chip);
1087
1088 /* Release the interrupt line */
1089 free_irq(oct->pci_dev->irq, oct);
1090
1091 if (oct->flags & LIO_FLAG_MSI_ENABLED)
1092 pci_disable_msi(oct->pci_dev);
1093
1094 /* Soft reset the octeon device before exiting */
1095 oct->fn_list.soft_reset(oct);
1096
1097 /* Disable the device, releasing the PCI INT */
1098 pci_disable_device(oct->pci_dev);
1099
1100 /* fallthrough */
1101 case OCT_DEV_IN_RESET:
1102 case OCT_DEV_DROQ_INIT_DONE:
1103 /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
1104 mdelay(100);
1105 for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
1106 if (!(oct->io_qmask.oq & (1UL << i)))
1107 continue;
1108 octeon_delete_droq(oct, i);
1109 }
1110
1111 /* Force any pending handshakes to complete */
1112 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1113 hs = &handshake[i];
1114
1115 if (hs->pci_dev) {
1116 handshake[oct->octeon_id].init_ok = 0;
1117 complete(&handshake[oct->octeon_id].init);
1118 handshake[oct->octeon_id].started_ok = 0;
1119 complete(&handshake[oct->octeon_id].started);
1120 }
1121 }
1122
1123 /* fallthrough */
1124 case OCT_DEV_RESP_LIST_INIT_DONE:
1125 octeon_delete_response_list(oct);
1126
1127 /* fallthrough */
1128 case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1129 octeon_free_sc_buffer_pool(oct);
1130
1131 /* fallthrough */
1132 case OCT_DEV_INSTR_QUEUE_INIT_DONE:
1133 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
1134 if (!(oct->io_qmask.iq & (1UL << i)))
1135 continue;
1136 octeon_delete_instr_queue(oct, i);
1137 }
1138
1139 /* fallthrough */
1140 case OCT_DEV_DISPATCH_INIT_DONE:
1141 octeon_delete_dispatch_list(oct);
1142 cancel_delayed_work_sync(&oct->nic_poll_work.work);
1143
1144 /* fallthrough */
1145 case OCT_DEV_PCI_MAP_DONE:
1146 octeon_unmap_pci_barx(oct, 0);
1147 octeon_unmap_pci_barx(oct, 1);
1148
1149 /* fallthrough */
1150 case OCT_DEV_BEGIN_STATE:
1151 /* Nothing to be done here either */
1152 break;
1153 } /* end switch(oct->status) */
1154
1155 tasklet_kill(&oct_priv->droq_tasklet);
1156}
1157
1158/**
1159 * \brief Send Rx control command
1160 * @param lio per-network private data
1161 * @param start_stop whether to start or stop
1162 */
1163static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1164{
1165 struct octnic_ctrl_pkt nctrl;
1166 struct octnic_ctrl_params nparams;
1167
1168 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
1169
1170 nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
1171 nctrl.ncmd.s.param1 = lio->linfo.ifidx;
1172 nctrl.ncmd.s.param2 = start_stop;
1173 nctrl.netpndev = (u64)lio->netdev;
1174
1175 nparams.resp_order = OCTEON_RESP_NORESPONSE;
1176
1177 if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams) < 0)
1178 netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1179}
1180
1181/**
1182 * \brief Destroy NIC device interface
1183 * @param oct octeon device
1184 * @param ifidx which interface to destroy
1185 *
1186 * Cleanup associated with each interface for an Octeon device when NIC
1187 * module is being unloaded or if initialization fails during load.
1188 */
1189static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1190{
1191 struct net_device *netdev = oct->props[ifidx].netdev;
1192 struct lio *lio;
1193
1194 if (!netdev) {
1195 dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1196 __func__, ifidx);
1197 return;
1198 }
1199
1200 lio = GET_LIO(netdev);
1201
1202 dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1203
1204 send_rx_ctrl_cmd(lio, 0);
1205
1206 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1207 txqs_stop(netdev);
1208
1209 if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1210 unregister_netdev(netdev);
1211
1212 delete_glist(lio);
1213
1214 free_netdev(netdev);
1215
1216 oct->props[ifidx].netdev = NULL;
1217}
1218
1219/**
1220 * \brief Stop complete NIC functionality
1221 * @param oct octeon device
1222 */
1223static int liquidio_stop_nic_module(struct octeon_device *oct)
1224{
1225 int i, j;
1226 struct lio *lio;
1227
1228 dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1229 if (!oct->ifcount) {
1230 dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1231 return 1;
1232 }
1233
1234 for (i = 0; i < oct->ifcount; i++) {
1235 lio = GET_LIO(oct->props[i].netdev);
1236 for (j = 0; j < lio->linfo.num_rxpciq; j++)
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001237 octeon_unregister_droq_ops(oct,
1238 lio->linfo.rxpciq[j].s.q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001239 }
1240
1241 for (i = 0; i < oct->ifcount; i++)
1242 liquidio_destroy_nic_device(oct, i);
1243
1244 dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1245 return 0;
1246}
1247
1248/**
1249 * \brief Cleans up resources at unload time
1250 * @param pdev PCI device structure
1251 */
1252static void liquidio_remove(struct pci_dev *pdev)
1253{
1254 struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1255
1256 dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1257
1258 if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1259 liquidio_stop_nic_module(oct_dev);
1260
1261 /* Reset the octeon device and cleanup all memory allocated for
1262 * the octeon device by driver.
1263 */
1264 octeon_destroy_resources(oct_dev);
1265
1266 dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1267
1268 /* This octeon device has been removed. Update the global
1269 * data structure to reflect this. Free the device structure.
1270 */
1271 octeon_free_device_mem(oct_dev);
1272}
1273
1274/**
1275 * \brief Identify the Octeon device and to map the BAR address space
1276 * @param oct octeon device
1277 */
1278static int octeon_chip_specific_setup(struct octeon_device *oct)
1279{
1280 u32 dev_id, rev_id;
1281 int ret = 1;
1282
1283 pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1284 pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1285 oct->rev_id = rev_id & 0xff;
1286
1287 switch (dev_id) {
1288 case OCTEON_CN68XX_PCIID:
1289 oct->chip_id = OCTEON_CN68XX;
1290 ret = lio_setup_cn68xx_octeon_device(oct);
1291 break;
1292
1293 case OCTEON_CN66XX_PCIID:
1294 oct->chip_id = OCTEON_CN66XX;
1295 ret = lio_setup_cn66xx_octeon_device(oct);
1296 break;
1297 default:
1298 dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1299 dev_id);
1300 }
1301
1302 if (!ret)
1303 dev_info(&oct->pci_dev->dev, "CN68XX PASS%d.%d %s\n",
1304 OCTEON_MAJOR_REV(oct),
1305 OCTEON_MINOR_REV(oct),
1306 octeon_get_conf(oct)->card_name);
1307
1308 return ret;
1309}
1310
1311/**
1312 * \brief PCI initialization for each Octeon device.
1313 * @param oct octeon device
1314 */
1315static int octeon_pci_os_setup(struct octeon_device *oct)
1316{
1317 /* setup PCI stuff first */
1318 if (pci_enable_device(oct->pci_dev)) {
1319 dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1320 return 1;
1321 }
1322
1323 if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1324 dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1325 return 1;
1326 }
1327
1328 /* Enable PCI DMA Master. */
1329 pci_set_master(oct->pci_dev);
1330
1331 return 0;
1332}
1333
1334/**
1335 * \brief Check Tx queue state for a given network buffer
1336 * @param lio per-network private data
1337 * @param skb network buffer
1338 */
1339static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
1340{
1341 int q = 0, iq = 0;
1342
1343 if (netif_is_multiqueue(lio->netdev)) {
1344 q = skb->queue_mapping;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001345 iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001346 } else {
1347 iq = lio->txq;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001348 q = iq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001349 }
1350
1351 if (octnet_iq_is_full(lio->oct_dev, iq))
1352 return 0;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001353
1354 if (__netif_subqueue_stopped(lio->netdev, q))
1355 wake_q(lio->netdev, q);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001356 return 1;
1357}
1358
1359/**
1360 * \brief Unmap and free network buffer
1361 * @param buf buffer
1362 */
1363static void free_netbuf(void *buf)
1364{
1365 struct sk_buff *skb;
1366 struct octnet_buf_free_info *finfo;
1367 struct lio *lio;
1368
1369 finfo = (struct octnet_buf_free_info *)buf;
1370 skb = finfo->skb;
1371 lio = finfo->lio;
1372
1373 dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1374 DMA_TO_DEVICE);
1375
1376 check_txq_state(lio, skb);
1377
1378 recv_buffer_free((struct sk_buff *)skb);
1379}
1380
1381/**
1382 * \brief Unmap and free gather buffer
1383 * @param buf buffer
1384 */
1385static void free_netsgbuf(void *buf)
1386{
1387 struct octnet_buf_free_info *finfo;
1388 struct sk_buff *skb;
1389 struct lio *lio;
1390 struct octnic_gather *g;
1391 int i, frags;
1392
1393 finfo = (struct octnet_buf_free_info *)buf;
1394 skb = finfo->skb;
1395 lio = finfo->lio;
1396 g = finfo->g;
1397 frags = skb_shinfo(skb)->nr_frags;
1398
1399 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1400 g->sg[0].ptr[0], (skb->len - skb->data_len),
1401 DMA_TO_DEVICE);
1402
1403 i = 1;
1404 while (frags--) {
1405 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1406
1407 pci_unmap_page((lio->oct_dev)->pci_dev,
1408 g->sg[(i >> 2)].ptr[(i & 3)],
1409 frag->size, DMA_TO_DEVICE);
1410 i++;
1411 }
1412
1413 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1414 finfo->dptr, g->sg_size,
1415 DMA_TO_DEVICE);
1416
1417 spin_lock(&lio->lock);
1418 list_add_tail(&g->list, &lio->glist);
1419 spin_unlock(&lio->lock);
1420
1421 check_txq_state(lio, skb); /* mq support: sub-queue state check */
1422
1423 recv_buffer_free((struct sk_buff *)skb);
1424}
1425
1426/**
1427 * \brief Unmap and free gather buffer with response
1428 * @param buf buffer
1429 */
1430static void free_netsgbuf_with_resp(void *buf)
1431{
1432 struct octeon_soft_command *sc;
1433 struct octnet_buf_free_info *finfo;
1434 struct sk_buff *skb;
1435 struct lio *lio;
1436 struct octnic_gather *g;
1437 int i, frags;
1438
1439 sc = (struct octeon_soft_command *)buf;
1440 skb = (struct sk_buff *)sc->callback_arg;
1441 finfo = (struct octnet_buf_free_info *)&skb->cb;
1442
1443 lio = finfo->lio;
1444 g = finfo->g;
1445 frags = skb_shinfo(skb)->nr_frags;
1446
1447 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1448 g->sg[0].ptr[0], (skb->len - skb->data_len),
1449 DMA_TO_DEVICE);
1450
1451 i = 1;
1452 while (frags--) {
1453 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1454
1455 pci_unmap_page((lio->oct_dev)->pci_dev,
1456 g->sg[(i >> 2)].ptr[(i & 3)],
1457 frag->size, DMA_TO_DEVICE);
1458 i++;
1459 }
1460
1461 dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1462 finfo->dptr, g->sg_size,
1463 DMA_TO_DEVICE);
1464
1465 spin_lock(&lio->lock);
1466 list_add_tail(&g->list, &lio->glist);
1467 spin_unlock(&lio->lock);
1468
1469 /* Don't free the skb yet */
1470
1471 check_txq_state(lio, skb);
1472}
1473
1474/**
1475 * \brief Adjust ptp frequency
1476 * @param ptp PTP clock info
1477 * @param ppb how much to adjust by, in parts-per-billion
1478 */
1479static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1480{
1481 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1482 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1483 u64 comp, delta;
1484 unsigned long flags;
1485 bool neg_adj = false;
1486
1487 if (ppb < 0) {
1488 neg_adj = true;
1489 ppb = -ppb;
1490 }
1491
1492 /* The hardware adds the clock compensation value to the
1493 * PTP clock on every coprocessor clock cycle, so we
1494 * compute the delta in terms of coprocessor clocks.
1495 */
1496 delta = (u64)ppb << 32;
1497 do_div(delta, oct->coproc_clock_rate);
1498
1499 spin_lock_irqsave(&lio->ptp_lock, flags);
1500 comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1501 if (neg_adj)
1502 comp -= delta;
1503 else
1504 comp += delta;
1505 lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1506 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1507
1508 return 0;
1509}
1510
1511/**
1512 * \brief Adjust ptp time
1513 * @param ptp PTP clock info
1514 * @param delta how much to adjust by, in nanosecs
1515 */
1516static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1517{
1518 unsigned long flags;
1519 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1520
1521 spin_lock_irqsave(&lio->ptp_lock, flags);
1522 lio->ptp_adjust += delta;
1523 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1524
1525 return 0;
1526}
1527
1528/**
1529 * \brief Get hardware clock time, including any adjustment
1530 * @param ptp PTP clock info
1531 * @param ts timespec
1532 */
1533static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1534 struct timespec64 *ts)
1535{
1536 u64 ns;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001537 unsigned long flags;
1538 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1539 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1540
1541 spin_lock_irqsave(&lio->ptp_lock, flags);
1542 ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1543 ns += lio->ptp_adjust;
1544 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1545
Kefeng Wang286af312016-01-27 17:34:37 +08001546 *ts = ns_to_timespec64(ns);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001547
1548 return 0;
1549}
1550
1551/**
1552 * \brief Set hardware clock time. Reset adjustment
1553 * @param ptp PTP clock info
1554 * @param ts timespec
1555 */
1556static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1557 const struct timespec64 *ts)
1558{
1559 u64 ns;
1560 unsigned long flags;
1561 struct lio *lio = container_of(ptp, struct lio, ptp_info);
1562 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1563
1564 ns = timespec_to_ns(ts);
1565
1566 spin_lock_irqsave(&lio->ptp_lock, flags);
1567 lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1568 lio->ptp_adjust = 0;
1569 spin_unlock_irqrestore(&lio->ptp_lock, flags);
1570
1571 return 0;
1572}
1573
1574/**
1575 * \brief Check if PTP is enabled
1576 * @param ptp PTP clock info
1577 * @param rq request
1578 * @param on is it on
1579 */
1580static int liquidio_ptp_enable(struct ptp_clock_info *ptp,
1581 struct ptp_clock_request *rq, int on)
1582{
1583 return -EOPNOTSUPP;
1584}
1585
1586/**
1587 * \brief Open PTP clock source
1588 * @param netdev network device
1589 */
1590static void oct_ptp_open(struct net_device *netdev)
1591{
1592 struct lio *lio = GET_LIO(netdev);
1593 struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1594
1595 spin_lock_init(&lio->ptp_lock);
1596
1597 snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1598 lio->ptp_info.owner = THIS_MODULE;
1599 lio->ptp_info.max_adj = 250000000;
1600 lio->ptp_info.n_alarm = 0;
1601 lio->ptp_info.n_ext_ts = 0;
1602 lio->ptp_info.n_per_out = 0;
1603 lio->ptp_info.pps = 0;
1604 lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1605 lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1606 lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1607 lio->ptp_info.settime64 = liquidio_ptp_settime;
1608 lio->ptp_info.enable = liquidio_ptp_enable;
1609
1610 lio->ptp_adjust = 0;
1611
1612 lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1613 &oct->pci_dev->dev);
1614
1615 if (IS_ERR(lio->ptp_clock))
1616 lio->ptp_clock = NULL;
1617}
1618
1619/**
1620 * \brief Init PTP clock
1621 * @param oct octeon device
1622 */
1623static void liquidio_ptp_init(struct octeon_device *oct)
1624{
1625 u64 clock_comp, cfg;
1626
1627 clock_comp = (u64)NSEC_PER_SEC << 32;
1628 do_div(clock_comp, oct->coproc_clock_rate);
1629 lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1630
1631 /* Enable */
1632 cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1633 lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1634}
1635
1636/**
1637 * \brief Load firmware to device
1638 * @param oct octeon device
1639 *
1640 * Maps device to firmware filename, requests firmware, and downloads it
1641 */
1642static int load_firmware(struct octeon_device *oct)
1643{
1644 int ret = 0;
1645 const struct firmware *fw;
1646 char fw_name[LIO_MAX_FW_FILENAME_LEN];
1647 char *tmp_fw_type;
1648
1649 if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
1650 sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
1651 dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
1652 return ret;
1653 }
1654
1655 if (fw_type[0] == '\0')
1656 tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1657 else
1658 tmp_fw_type = fw_type;
1659
1660 sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1661 octeon_get_conf(oct)->card_name, tmp_fw_type,
1662 LIO_FW_NAME_SUFFIX);
1663
1664 ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1665 if (ret) {
1666 dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1667 fw_name);
1668 return ret;
1669 }
1670
1671 ret = octeon_download_firmware(oct, fw->data, fw->size);
1672
1673 release_firmware(fw);
1674
1675 return ret;
1676}
1677
1678/**
1679 * \brief Setup output queue
1680 * @param oct octeon device
1681 * @param q_no which queue
1682 * @param num_descs how many descriptors
1683 * @param desc_size size of each descriptor
1684 * @param app_ctx application context
1685 */
1686static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
1687 int desc_size, void *app_ctx)
1688{
1689 int ret_val = 0;
1690
1691 dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
1692 /* droq creation and local register settings. */
1693 ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301694 if (ret_val < 0)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001695 return ret_val;
1696
1697 if (ret_val == 1) {
1698 dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
1699 return 0;
1700 }
1701 /* tasklet creation for the droq */
1702
1703 /* Enable the droq queues */
1704 octeon_set_droq_pkt_op(oct, q_no, 1);
1705
1706 /* Send Credit for Octeon Output queues. Credits are always
1707 * sent after the output queue is enabled.
1708 */
1709 writel(oct->droq[q_no]->max_count,
1710 oct->droq[q_no]->pkts_credit_reg);
1711
1712 return ret_val;
1713}
1714
1715/**
1716 * \brief Callback for getting interface configuration
1717 * @param status status of request
1718 * @param buf pointer to resp structure
1719 */
1720static void if_cfg_callback(struct octeon_device *oct,
1721 u32 status,
1722 void *buf)
1723{
1724 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1725 struct liquidio_if_cfg_resp *resp;
1726 struct liquidio_if_cfg_context *ctx;
1727
1728 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1729 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1730
1731 oct = lio_get_device(ctx->octeon_id);
1732 if (resp->status)
1733 dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
1734 CVM_CAST64(resp->status));
1735 ACCESS_ONCE(ctx->cond) = 1;
1736
1737 /* This barrier is required to be sure that the response has been
1738 * written fully before waking up the handler
1739 */
1740 wmb();
1741
1742 wake_up_interruptible(&ctx->wc);
1743}
1744
1745/**
1746 * \brief Select queue based on hash
1747 * @param dev Net device
1748 * @param skb sk_buff structure
1749 * @returns selected queue number
1750 */
1751static u16 select_q(struct net_device *dev, struct sk_buff *skb,
1752 void *accel_priv, select_queue_fallback_t fallback)
1753{
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001754 u32 qindex = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001755 struct lio *lio;
1756
1757 lio = GET_LIO(dev);
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001758 qindex = skb_tx_hash(dev, skb);
1759
1760 return (u16)(qindex % (lio->linfo.num_txpciq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001761}
1762
1763/** Routine to push packets arriving on Octeon interface upto network layer.
1764 * @param oct_id - octeon device id.
1765 * @param skbuff - skbuff struct to be passed to network layer.
1766 * @param len - size of total data received.
1767 * @param rh - Control header associated with the packet
1768 * @param param - additional control data with the packet
1769 */
1770static void
1771liquidio_push_packet(u32 octeon_id,
1772 void *skbuff,
1773 u32 len,
1774 union octeon_rh *rh,
1775 void *param)
1776{
1777 struct napi_struct *napi = param;
1778 struct octeon_device *oct = lio_get_device(octeon_id);
1779 struct sk_buff *skb = (struct sk_buff *)skbuff;
1780 struct skb_shared_hwtstamps *shhwtstamps;
1781 u64 ns;
1782 struct net_device *netdev =
1783 (struct net_device *)oct->props[rh->r_dh.link].netdev;
1784 struct octeon_droq *droq = container_of(param, struct octeon_droq,
1785 napi);
1786 if (netdev) {
1787 int packet_was_received;
1788 struct lio *lio = GET_LIO(netdev);
1789
1790 /* Do not proceed if the interface is not in RUNNING state. */
1791 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1792 recv_buffer_free(skb);
1793 droq->stats.rx_dropped++;
1794 return;
1795 }
1796
1797 skb->dev = netdev;
1798
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001799 skb_record_rx_queue(skb, droq->q_no);
1800
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001801 if (rh->r_dh.has_hwtstamp) {
1802 /* timestamp is included from the hardware at the
1803 * beginning of the packet.
1804 */
1805 if (ifstate_check(lio,
1806 LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
1807 /* Nanoseconds are in the first 64-bits
1808 * of the packet.
1809 */
1810 memcpy(&ns, (skb->data), sizeof(ns));
1811 shhwtstamps = skb_hwtstamps(skb);
1812 shhwtstamps->hwtstamp =
1813 ns_to_ktime(ns + lio->ptp_adjust);
1814 }
1815 skb_pull(skb, sizeof(ns));
1816 }
1817
1818 skb->protocol = eth_type_trans(skb, skb->dev);
1819
1820 if ((netdev->features & NETIF_F_RXCSUM) &&
1821 (rh->r_dh.csum_verified == CNNIC_CSUM_VERIFIED))
1822 /* checksum has already been verified */
1823 skb->ip_summed = CHECKSUM_UNNECESSARY;
1824 else
1825 skb->ip_summed = CHECKSUM_NONE;
1826
1827 packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
1828
1829 if (packet_was_received) {
1830 droq->stats.rx_bytes_received += len;
1831 droq->stats.rx_pkts_received++;
1832 netdev->last_rx = jiffies;
1833 } else {
1834 droq->stats.rx_dropped++;
1835 netif_info(lio, rx_err, lio->netdev,
1836 "droq:%d error rx_dropped:%llu\n",
1837 droq->q_no, droq->stats.rx_dropped);
1838 }
1839
1840 } else {
1841 recv_buffer_free(skb);
1842 }
1843}
1844
1845/**
1846 * \brief wrapper for calling napi_schedule
1847 * @param param parameters to pass to napi_schedule
1848 *
1849 * Used when scheduling on different CPUs
1850 */
1851static void napi_schedule_wrapper(void *param)
1852{
1853 struct napi_struct *napi = param;
1854
1855 napi_schedule(napi);
1856}
1857
1858/**
1859 * \brief callback when receive interrupt occurs and we are in NAPI mode
1860 * @param arg pointer to octeon output queue
1861 */
1862static void liquidio_napi_drv_callback(void *arg)
1863{
1864 struct octeon_droq *droq = arg;
1865 int this_cpu = smp_processor_id();
1866
1867 if (droq->cpu_id == this_cpu) {
1868 napi_schedule(&droq->napi);
1869 } else {
1870 struct call_single_data *csd = &droq->csd;
1871
1872 csd->func = napi_schedule_wrapper;
1873 csd->info = &droq->napi;
1874 csd->flags = 0;
1875
1876 smp_call_function_single_async(droq->cpu_id, csd);
1877 }
1878}
1879
1880/**
1881 * \brief Main NAPI poll function
1882 * @param droq octeon output queue
1883 * @param budget maximum number of items to process
1884 */
1885static int liquidio_napi_do_rx(struct octeon_droq *droq, int budget)
1886{
1887 int work_done;
1888 struct lio *lio = GET_LIO(droq->napi.dev);
1889 struct octeon_device *oct = lio->oct_dev;
1890
1891 work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
1892 POLL_EVENT_PROCESS_PKTS,
1893 budget);
1894 if (work_done < 0) {
1895 netif_info(lio, rx_err, lio->netdev,
1896 "Receive work_done < 0, rxq:%d\n", droq->q_no);
1897 goto octnet_napi_finish;
1898 }
1899
1900 if (work_done > budget)
1901 dev_err(&oct->pci_dev->dev, ">>>> %s work_done: %d budget: %d\n",
1902 __func__, work_done, budget);
1903
1904 return work_done;
1905
1906octnet_napi_finish:
1907 napi_complete(&droq->napi);
1908 octeon_process_droq_poll_cmd(oct, droq->q_no, POLL_EVENT_ENABLE_INTR,
1909 0);
1910 return 0;
1911}
1912
1913/**
1914 * \brief Entry point for NAPI polling
1915 * @param napi NAPI structure
1916 * @param budget maximum number of items to process
1917 */
1918static int liquidio_napi_poll(struct napi_struct *napi, int budget)
1919{
1920 struct octeon_droq *droq;
1921 int work_done;
1922
1923 droq = container_of(napi, struct octeon_droq, napi);
1924
1925 work_done = liquidio_napi_do_rx(droq, budget);
1926
1927 if (work_done < budget) {
1928 napi_complete(napi);
1929 octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
1930 POLL_EVENT_ENABLE_INTR, 0);
1931 return 0;
1932 }
1933
1934 return work_done;
1935}
1936
1937/**
1938 * \brief Setup input and output queues
1939 * @param octeon_dev octeon device
1940 * @param net_device Net device
1941 *
1942 * Note: Queues are with respect to the octeon device. Thus
1943 * an input queue is for egress packets, and output queues
1944 * are for ingress packets.
1945 */
1946static inline int setup_io_queues(struct octeon_device *octeon_dev,
1947 struct net_device *net_device)
1948{
1949 static int first_time = 1;
1950 static struct octeon_droq_ops droq_ops;
1951 static int cpu_id;
1952 static int cpu_id_modulus;
1953 struct octeon_droq *droq;
1954 struct napi_struct *napi;
1955 int q, q_no, retval = 0;
1956 struct lio *lio;
1957 int num_tx_descs;
1958
1959 lio = GET_LIO(net_device);
1960 if (first_time) {
1961 first_time = 0;
1962 memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
1963
1964 droq_ops.fptr = liquidio_push_packet;
1965
1966 droq_ops.poll_mode = 1;
1967 droq_ops.napi_fn = liquidio_napi_drv_callback;
1968 cpu_id = 0;
1969 cpu_id_modulus = num_present_cpus();
1970 }
1971
1972 /* set up DROQs. */
1973 for (q = 0; q < lio->linfo.num_rxpciq; q++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07001974 q_no = lio->linfo.rxpciq[q].s.q_no;
1975 dev_dbg(&octeon_dev->pci_dev->dev,
1976 "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
1977 q, q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001978 retval = octeon_setup_droq(octeon_dev, q_no,
1979 CFG_GET_NUM_RX_DESCS_NIC_IF
1980 (octeon_get_conf(octeon_dev),
1981 lio->ifidx),
1982 CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
1983 (octeon_get_conf(octeon_dev),
1984 lio->ifidx), NULL);
1985 if (retval) {
1986 dev_err(&octeon_dev->pci_dev->dev,
1987 " %s : Runtime DROQ(RxQ) creation failed.\n",
1988 __func__);
1989 return 1;
1990 }
1991
1992 droq = octeon_dev->droq[q_no];
1993 napi = &droq->napi;
1994 netif_napi_add(net_device, napi, liquidio_napi_poll, 64);
1995
1996 /* designate a CPU for this droq */
1997 droq->cpu_id = cpu_id;
1998 cpu_id++;
1999 if (cpu_id >= cpu_id_modulus)
2000 cpu_id = 0;
2001
2002 octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
2003 }
2004
2005 /* set up IQs. */
2006 for (q = 0; q < lio->linfo.num_txpciq; q++) {
2007 num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
2008 (octeon_dev),
2009 lio->ifidx);
2010 retval = octeon_setup_iq(octeon_dev, lio->linfo.txpciq[q],
2011 num_tx_descs,
2012 netdev_get_tx_queue(net_device, q));
2013 if (retval) {
2014 dev_err(&octeon_dev->pci_dev->dev,
2015 " %s : Runtime IQ(TxQ) creation failed.\n",
2016 __func__);
2017 return 1;
2018 }
2019 }
2020
2021 return 0;
2022}
2023
2024/**
2025 * \brief Poll routine for checking transmit queue status
2026 * @param work work_struct data structure
2027 */
2028static void octnet_poll_check_txq_status(struct work_struct *work)
2029{
2030 struct cavium_wk *wk = (struct cavium_wk *)work;
2031 struct lio *lio = (struct lio *)wk->ctxptr;
2032
2033 if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
2034 return;
2035
2036 check_txq_status(lio);
2037 queue_delayed_work(lio->txq_status_wq.wq,
2038 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2039}
2040
2041/**
2042 * \brief Sets up the txq poll check
2043 * @param netdev network device
2044 */
2045static inline void setup_tx_poll_fn(struct net_device *netdev)
2046{
2047 struct lio *lio = GET_LIO(netdev);
2048 struct octeon_device *oct = lio->oct_dev;
2049
Bhaktipriya Shridhar292b9da2016-06-08 01:47:59 +05302050 lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2051 WQ_MEM_RECLAIM, 0);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002052 if (!lio->txq_status_wq.wq) {
2053 dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2054 return;
2055 }
2056 INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2057 octnet_poll_check_txq_status);
2058 lio->txq_status_wq.wk.ctxptr = lio;
2059 queue_delayed_work(lio->txq_status_wq.wq,
2060 &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2061}
2062
2063/**
2064 * \brief Net device open for LiquidIO
2065 * @param netdev network device
2066 */
2067static int liquidio_open(struct net_device *netdev)
2068{
2069 struct lio *lio = GET_LIO(netdev);
2070 struct octeon_device *oct = lio->oct_dev;
2071 struct napi_struct *napi, *n;
2072
2073 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2074 napi_enable(napi);
2075
2076 oct_ptp_open(netdev);
2077
2078 ifstate_set(lio, LIO_IFSTATE_RUNNING);
2079 setup_tx_poll_fn(netdev);
2080 start_txq(netdev);
2081
2082 netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2083 try_module_get(THIS_MODULE);
2084
2085 /* tell Octeon to start forwarding packets to host */
2086 send_rx_ctrl_cmd(lio, 1);
2087
2088 /* Ready for link status updates */
2089 lio->intf_open = 1;
2090
2091 dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2092 netdev->name);
2093
2094 return 0;
2095}
2096
2097/**
2098 * \brief Net device stop for LiquidIO
2099 * @param netdev network device
2100 */
2101static int liquidio_stop(struct net_device *netdev)
2102{
2103 struct napi_struct *napi, *n;
2104 struct lio *lio = GET_LIO(netdev);
2105 struct octeon_device *oct = lio->oct_dev;
2106
2107 netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
2108 /* Inform that netif carrier is down */
2109 lio->intf_open = 0;
2110 lio->linfo.link.s.status = 0;
2111
2112 netif_carrier_off(netdev);
2113
2114 /* tell Octeon to stop forwarding packets to host */
2115 send_rx_ctrl_cmd(lio, 0);
2116
2117 cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002118 destroy_workqueue(lio->txq_status_wq.wq);
2119
2120 if (lio->ptp_clock) {
2121 ptp_clock_unregister(lio->ptp_clock);
2122 lio->ptp_clock = NULL;
2123 }
2124
2125 ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2126
2127 /* This is a hack that allows DHCP to continue working. */
2128 set_bit(__LINK_STATE_START, &lio->netdev->state);
2129
2130 list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2131 napi_disable(napi);
2132
2133 txqs_stop(netdev);
2134
2135 dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2136 module_put(THIS_MODULE);
2137
2138 return 0;
2139}
2140
2141void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr)
2142{
2143 struct octnic_ctrl_pkt *nctrl = (struct octnic_ctrl_pkt *)nctrl_ptr;
2144 struct net_device *netdev = (struct net_device *)nctrl->netpndev;
2145 struct lio *lio = GET_LIO(netdev);
2146 struct octeon_device *oct = lio->oct_dev;
2147
2148 switch (nctrl->ncmd.s.cmd) {
2149 case OCTNET_CMD_CHANGE_DEVFLAGS:
2150 case OCTNET_CMD_SET_MULTI_LIST:
2151 break;
2152
2153 case OCTNET_CMD_CHANGE_MACADDR:
2154 /* If command is successful, change the MACADDR. */
2155 netif_info(lio, probe, lio->netdev, " MACAddr changed to 0x%llx\n",
2156 CVM_CAST64(nctrl->udd[0]));
2157 dev_info(&oct->pci_dev->dev, "%s MACAddr changed to 0x%llx\n",
2158 netdev->name, CVM_CAST64(nctrl->udd[0]));
2159 memcpy(netdev->dev_addr, ((u8 *)&nctrl->udd[0]) + 2, ETH_ALEN);
2160 break;
2161
2162 case OCTNET_CMD_CHANGE_MTU:
2163 /* If command is successful, change the MTU. */
2164 netif_info(lio, probe, lio->netdev, " MTU Changed from %d to %d\n",
2165 netdev->mtu, nctrl->ncmd.s.param2);
2166 dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
2167 netdev->name, netdev->mtu,
2168 nctrl->ncmd.s.param2);
2169 netdev->mtu = nctrl->ncmd.s.param2;
2170 break;
2171
2172 case OCTNET_CMD_GPIO_ACCESS:
2173 netif_info(lio, probe, lio->netdev, "LED Flashing visual identification\n");
2174
2175 break;
2176
2177 case OCTNET_CMD_LRO_ENABLE:
2178 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2179 break;
2180
2181 case OCTNET_CMD_LRO_DISABLE:
2182 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2183 netdev->name);
2184 break;
2185
2186 case OCTNET_CMD_VERBOSE_ENABLE:
2187 dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
2188 break;
2189
2190 case OCTNET_CMD_VERBOSE_DISABLE:
2191 dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
2192 netdev->name);
2193 break;
2194
2195 case OCTNET_CMD_SET_SETTINGS:
2196 dev_info(&oct->pci_dev->dev, "%s settings changed\n",
2197 netdev->name);
2198
2199 break;
2200
2201 default:
2202 dev_err(&oct->pci_dev->dev, "%s Unknown cmd %d\n", __func__,
2203 nctrl->ncmd.s.cmd);
2204 }
2205}
2206
2207/**
2208 * \brief Converts a mask based on net device flags
2209 * @param netdev network device
2210 *
2211 * This routine generates a octnet_ifflags mask from the net device flags
2212 * received from the OS.
2213 */
2214static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2215{
2216 enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2217
2218 if (netdev->flags & IFF_PROMISC)
2219 f |= OCTNET_IFFLAG_PROMISC;
2220
2221 if (netdev->flags & IFF_ALLMULTI)
2222 f |= OCTNET_IFFLAG_ALLMULTI;
2223
2224 if (netdev->flags & IFF_MULTICAST) {
2225 f |= OCTNET_IFFLAG_MULTICAST;
2226
2227 /* Accept all multicast addresses if there are more than we
2228 * can handle
2229 */
2230 if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2231 f |= OCTNET_IFFLAG_ALLMULTI;
2232 }
2233
2234 if (netdev->flags & IFF_BROADCAST)
2235 f |= OCTNET_IFFLAG_BROADCAST;
2236
2237 return f;
2238}
2239
2240/**
2241 * \brief Net device set_multicast_list
2242 * @param netdev network device
2243 */
2244static void liquidio_set_mcast_list(struct net_device *netdev)
2245{
2246 struct lio *lio = GET_LIO(netdev);
2247 struct octeon_device *oct = lio->oct_dev;
2248 struct octnic_ctrl_pkt nctrl;
2249 struct octnic_ctrl_params nparams;
2250 struct netdev_hw_addr *ha;
2251 u64 *mc;
2252 int ret, i;
2253 int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2254
2255 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2256
2257 /* Create a ctrl pkt command to be sent to core app. */
2258 nctrl.ncmd.u64 = 0;
2259 nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
2260 nctrl.ncmd.s.param1 = lio->linfo.ifidx;
2261 nctrl.ncmd.s.param2 = get_new_flags(netdev);
2262 nctrl.ncmd.s.param3 = mc_count;
2263 nctrl.ncmd.s.more = mc_count;
2264 nctrl.netpndev = (u64)netdev;
2265 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2266
2267 /* copy all the addresses into the udd */
2268 i = 0;
2269 mc = &nctrl.udd[0];
2270 netdev_for_each_mc_addr(ha, netdev) {
2271 *mc = 0;
2272 memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2273 /* no need to swap bytes */
2274
2275 if (++mc > &nctrl.udd[mc_count])
2276 break;
2277 }
2278
2279 /* Apparently, any activity in this call from the kernel has to
2280 * be atomic. So we won't wait for response.
2281 */
2282 nctrl.wait_time = 0;
2283
2284 nparams.resp_order = OCTEON_RESP_NORESPONSE;
2285
2286 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
2287 if (ret < 0) {
2288 dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2289 ret);
2290 }
2291}
2292
2293/**
2294 * \brief Net device set_mac_address
2295 * @param netdev network device
2296 */
2297static int liquidio_set_mac(struct net_device *netdev, void *p)
2298{
2299 int ret = 0;
2300 struct lio *lio = GET_LIO(netdev);
2301 struct octeon_device *oct = lio->oct_dev;
2302 struct sockaddr *addr = (struct sockaddr *)p;
2303 struct octnic_ctrl_pkt nctrl;
2304 struct octnic_ctrl_params nparams;
2305
2306 if ((!is_valid_ether_addr(addr->sa_data)) ||
2307 (ifstate_check(lio, LIO_IFSTATE_RUNNING)))
2308 return -EADDRNOTAVAIL;
2309
2310 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2311
2312 nctrl.ncmd.u64 = 0;
2313 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
2314 nctrl.ncmd.s.param1 = lio->linfo.ifidx;
2315 nctrl.ncmd.s.param2 = 0;
2316 nctrl.ncmd.s.more = 1;
2317 nctrl.netpndev = (u64)netdev;
2318 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2319 nctrl.wait_time = 100;
2320
2321 nctrl.udd[0] = 0;
2322 /* The MAC Address is presented in network byte order. */
2323 memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2324
2325 nparams.resp_order = OCTEON_RESP_ORDERED;
2326
2327 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
2328 if (ret < 0) {
2329 dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2330 return -ENOMEM;
2331 }
2332 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2333 memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2334
2335 return 0;
2336}
2337
2338/**
2339 * \brief Net device get_stats
2340 * @param netdev network device
2341 */
2342static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2343{
2344 struct lio *lio = GET_LIO(netdev);
2345 struct net_device_stats *stats = &netdev->stats;
2346 struct octeon_device *oct;
2347 u64 pkts = 0, drop = 0, bytes = 0;
2348 struct oct_droq_stats *oq_stats;
2349 struct oct_iq_stats *iq_stats;
2350 int i, iq_no, oq_no;
2351
2352 oct = lio->oct_dev;
2353
2354 for (i = 0; i < lio->linfo.num_txpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002355 iq_no = lio->linfo.txpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002356 iq_stats = &oct->instr_queue[iq_no]->stats;
2357 pkts += iq_stats->tx_done;
2358 drop += iq_stats->tx_dropped;
2359 bytes += iq_stats->tx_tot_bytes;
2360 }
2361
2362 stats->tx_packets = pkts;
2363 stats->tx_bytes = bytes;
2364 stats->tx_dropped = drop;
2365
2366 pkts = 0;
2367 drop = 0;
2368 bytes = 0;
2369
2370 for (i = 0; i < lio->linfo.num_rxpciq; i++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002371 oq_no = lio->linfo.rxpciq[i].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002372 oq_stats = &oct->droq[oq_no]->stats;
2373 pkts += oq_stats->rx_pkts_received;
2374 drop += (oq_stats->rx_dropped +
2375 oq_stats->dropped_nodispatch +
2376 oq_stats->dropped_toomany +
2377 oq_stats->dropped_nomem);
2378 bytes += oq_stats->rx_bytes_received;
2379 }
2380
2381 stats->rx_bytes = bytes;
2382 stats->rx_packets = pkts;
2383 stats->rx_dropped = drop;
2384
2385 return stats;
2386}
2387
2388/**
2389 * \brief Net device change_mtu
2390 * @param netdev network device
2391 */
2392static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
2393{
2394 struct lio *lio = GET_LIO(netdev);
2395 struct octeon_device *oct = lio->oct_dev;
2396 struct octnic_ctrl_pkt nctrl;
2397 struct octnic_ctrl_params nparams;
2398 int max_frm_size = new_mtu + OCTNET_FRM_HEADER_SIZE;
2399 int ret = 0;
2400
2401 /* Limit the MTU to make sure the ethernet packets are between 64 bytes
2402 * and 65535 bytes
2403 */
2404 if ((max_frm_size < OCTNET_MIN_FRM_SIZE) ||
2405 (max_frm_size > OCTNET_MAX_FRM_SIZE)) {
2406 dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
2407 dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
2408 (OCTNET_MIN_FRM_SIZE - OCTNET_FRM_HEADER_SIZE),
2409 (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE));
2410 return -EINVAL;
2411 }
2412
2413 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2414
2415 nctrl.ncmd.u64 = 0;
2416 nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
2417 nctrl.ncmd.s.param1 = lio->linfo.ifidx;
2418 nctrl.ncmd.s.param2 = new_mtu;
2419 nctrl.wait_time = 100;
2420 nctrl.netpndev = (u64)netdev;
2421 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2422
2423 nparams.resp_order = OCTEON_RESP_ORDERED;
2424
2425 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
2426 if (ret < 0) {
2427 dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
2428 return -1;
2429 }
2430
2431 lio->mtu = new_mtu;
2432
2433 return 0;
2434}
2435
2436/**
2437 * \brief Handler for SIOCSHWTSTAMP ioctl
2438 * @param netdev network device
2439 * @param ifr interface request
2440 * @param cmd command
2441 */
2442static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2443{
2444 struct hwtstamp_config conf;
2445 struct lio *lio = GET_LIO(netdev);
2446
2447 if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2448 return -EFAULT;
2449
2450 if (conf.flags)
2451 return -EINVAL;
2452
2453 switch (conf.tx_type) {
2454 case HWTSTAMP_TX_ON:
2455 case HWTSTAMP_TX_OFF:
2456 break;
2457 default:
2458 return -ERANGE;
2459 }
2460
2461 switch (conf.rx_filter) {
2462 case HWTSTAMP_FILTER_NONE:
2463 break;
2464 case HWTSTAMP_FILTER_ALL:
2465 case HWTSTAMP_FILTER_SOME:
2466 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2467 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2468 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2469 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2470 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2471 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2472 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2473 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2474 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2475 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2476 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2477 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2478 conf.rx_filter = HWTSTAMP_FILTER_ALL;
2479 break;
2480 default:
2481 return -ERANGE;
2482 }
2483
2484 if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2485 ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2486
2487 else
2488 ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2489
2490 return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2491}
2492
2493/**
2494 * \brief ioctl handler
2495 * @param netdev network device
2496 * @param ifr interface request
2497 * @param cmd command
2498 */
2499static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2500{
2501 switch (cmd) {
2502 case SIOCSHWTSTAMP:
2503 return hwtstamp_ioctl(netdev, ifr, cmd);
2504 default:
2505 return -EOPNOTSUPP;
2506 }
2507}
2508
2509/**
2510 * \brief handle a Tx timestamp response
2511 * @param status response status
2512 * @param buf pointer to skb
2513 */
2514static void handle_timestamp(struct octeon_device *oct,
2515 u32 status,
2516 void *buf)
2517{
2518 struct octnet_buf_free_info *finfo;
2519 struct octeon_soft_command *sc;
2520 struct oct_timestamp_resp *resp;
2521 struct lio *lio;
2522 struct sk_buff *skb = (struct sk_buff *)buf;
2523
2524 finfo = (struct octnet_buf_free_info *)skb->cb;
2525 lio = finfo->lio;
2526 sc = finfo->sc;
2527 oct = lio->oct_dev;
2528 resp = (struct oct_timestamp_resp *)sc->virtrptr;
2529
2530 if (status != OCTEON_REQUEST_DONE) {
2531 dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2532 CVM_CAST64(status));
2533 resp->timestamp = 0;
2534 }
2535
2536 octeon_swap_8B_data(&resp->timestamp, 1);
2537
Colin Ian King19a6d152016-02-05 16:30:39 +00002538 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002539 struct skb_shared_hwtstamps ts;
2540 u64 ns = resp->timestamp;
2541
2542 netif_info(lio, tx_done, lio->netdev,
2543 "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2544 skb, (unsigned long long)ns);
2545 ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2546 skb_tstamp_tx(skb, &ts);
2547 }
2548
2549 octeon_free_soft_command(oct, sc);
2550 recv_buffer_free(skb);
2551}
2552
2553/* \brief Send a data packet that will be timestamped
2554 * @param oct octeon device
2555 * @param ndata pointer to network data
2556 * @param finfo pointer to private network data
2557 */
2558static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2559 struct octnic_data_pkt *ndata,
2560 struct octnet_buf_free_info *finfo,
2561 int xmit_more)
2562{
2563 int retval;
2564 struct octeon_soft_command *sc;
2565 struct octeon_instr_ih *ih;
2566 struct octeon_instr_rdp *rdp;
2567 struct lio *lio;
2568 int ring_doorbell;
2569
2570 lio = finfo->lio;
2571
2572 sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2573 sizeof(struct oct_timestamp_resp));
2574 finfo->sc = sc;
2575
2576 if (!sc) {
2577 dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2578 return IQ_SEND_FAILED;
2579 }
2580
2581 if (ndata->reqtype == REQTYPE_NORESP_NET)
2582 ndata->reqtype = REQTYPE_RESP_NET;
2583 else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2584 ndata->reqtype = REQTYPE_RESP_NET_SG;
2585
2586 sc->callback = handle_timestamp;
2587 sc->callback_arg = finfo->skb;
2588 sc->iq_no = ndata->q_no;
2589
2590 ih = (struct octeon_instr_ih *)&sc->cmd.ih;
2591 rdp = (struct octeon_instr_rdp *)&sc->cmd.rdp;
2592
2593 ring_doorbell = !xmit_more;
2594 retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
2595 sc, ih->dlengsz, ndata->reqtype);
2596
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07002597 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002598 dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2599 retval);
2600 octeon_free_soft_command(oct, sc);
2601 } else {
2602 netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2603 }
2604
2605 return retval;
2606}
2607
2608static inline int is_ipv4(struct sk_buff *skb)
2609{
2610 return (skb->protocol == htons(ETH_P_IP)) &&
2611 (ip_hdr(skb)->version == 4);
2612}
2613
2614static inline int is_vlan(struct sk_buff *skb)
2615{
2616 return skb->protocol == htons(ETH_P_8021Q);
2617}
2618
2619static inline int is_ip_fragmented(struct sk_buff *skb)
2620{
2621 /* The Don't fragment and Reserved flag fields are ignored.
2622 * IP is fragmented if
2623 * - the More fragments bit is set (indicating this IP is a fragment
2624 * with more to follow; the current offset could be 0 ).
2625 * - ths offset field is non-zero.
2626 */
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07002627 return (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ? 1 : 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002628}
2629
2630static inline int is_ipv6(struct sk_buff *skb)
2631{
2632 return (skb->protocol == htons(ETH_P_IPV6)) &&
2633 (ipv6_hdr(skb)->version == 6);
2634}
2635
2636static inline int is_with_extn_hdr(struct sk_buff *skb)
2637{
2638 return (ipv6_hdr(skb)->nexthdr != IPPROTO_TCP) &&
2639 (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP);
2640}
2641
2642static inline int is_tcpudp(struct sk_buff *skb)
2643{
2644 return (ip_hdr(skb)->protocol == IPPROTO_TCP) ||
2645 (ip_hdr(skb)->protocol == IPPROTO_UDP);
2646}
2647
2648static inline u32 get_ipv4_5tuple_tag(struct sk_buff *skb)
2649{
2650 u32 tag;
2651 struct iphdr *iphdr = ip_hdr(skb);
2652
2653 tag = crc32(0, &iphdr->protocol, 1);
2654 tag = crc32(tag, (u8 *)&iphdr->saddr, 8);
2655 tag = crc32(tag, skb_transport_header(skb), 4);
2656 return tag;
2657}
2658
2659static inline u32 get_ipv6_5tuple_tag(struct sk_buff *skb)
2660{
2661 u32 tag;
2662 struct ipv6hdr *ipv6hdr = ipv6_hdr(skb);
2663
2664 tag = crc32(0, &ipv6hdr->nexthdr, 1);
2665 tag = crc32(tag, (u8 *)&ipv6hdr->saddr, 32);
2666 tag = crc32(tag, skb_transport_header(skb), 4);
2667 return tag;
2668}
2669
2670/** \brief Transmit networks packets to the Octeon interface
2671 * @param skbuff skbuff struct to be passed to network layer.
2672 * @param netdev pointer to network device
2673 * @returns whether the packet was transmitted to the device okay or not
2674 * (NETDEV_TX_OK or NETDEV_TX_BUSY)
2675 */
2676static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2677{
2678 struct lio *lio;
2679 struct octnet_buf_free_info *finfo;
2680 union octnic_cmd_setup cmdsetup;
2681 struct octnic_data_pkt ndata;
2682 struct octeon_device *oct;
2683 struct oct_iq_stats *stats;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002684 int status = 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002685 int q_idx = 0, iq_no = 0;
2686 int xmit_more;
2687 u32 tag = 0;
2688
2689 lio = GET_LIO(netdev);
2690 oct = lio->oct_dev;
2691
2692 if (netif_is_multiqueue(netdev)) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07002693 q_idx = skb->queue_mapping;
2694 q_idx = (q_idx % (lio->linfo.num_txpciq));
2695 tag = q_idx;
2696 iq_no = lio->linfo.txpciq[q_idx].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002697 } else {
2698 iq_no = lio->txq;
2699 }
2700
2701 stats = &oct->instr_queue[iq_no]->stats;
2702
2703 /* Check for all conditions in which the current packet cannot be
2704 * transmitted.
2705 */
2706 if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
2707 (!lio->linfo.link.s.status) ||
2708 (skb->len <= 0)) {
2709 netif_info(lio, tx_err, lio->netdev,
2710 "Transmit failed link_status : %d\n",
2711 lio->linfo.link.s.status);
2712 goto lio_xmit_failed;
2713 }
2714
2715 /* Use space in skb->cb to store info used to unmap and
2716 * free the buffers.
2717 */
2718 finfo = (struct octnet_buf_free_info *)skb->cb;
2719 finfo->lio = lio;
2720 finfo->skb = skb;
2721 finfo->sc = NULL;
2722
2723 /* Prepare the attributes for the data to be passed to OSI. */
2724 memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2725
2726 ndata.buf = (void *)finfo;
2727
2728 ndata.q_no = iq_no;
2729
2730 if (netif_is_multiqueue(netdev)) {
2731 if (octnet_iq_is_full(oct, ndata.q_no)) {
2732 /* defer sending if queue is full */
2733 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2734 ndata.q_no);
2735 stats->tx_iq_busy++;
2736 return NETDEV_TX_BUSY;
2737 }
2738 } else {
2739 if (octnet_iq_is_full(oct, lio->txq)) {
2740 /* defer sending if queue is full */
2741 stats->tx_iq_busy++;
2742 netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2743 ndata.q_no);
2744 return NETDEV_TX_BUSY;
2745 }
2746 }
2747 /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
2748 * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no );
2749 */
2750
2751 ndata.datasize = skb->len;
2752
2753 cmdsetup.u64 = 0;
2754 cmdsetup.s.ifidx = lio->linfo.ifidx;
2755
2756 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2757 if (is_ipv4(skb) && !is_ip_fragmented(skb) && is_tcpudp(skb)) {
2758 tag = get_ipv4_5tuple_tag(skb);
2759
2760 cmdsetup.s.cksum_offset = sizeof(struct ethhdr) + 1;
2761
2762 if (ip_hdr(skb)->ihl > 5)
2763 cmdsetup.s.ipv4opts_ipv6exthdr =
2764 OCT_PKT_PARAM_IPV4OPTS;
2765
2766 } else if (is_ipv6(skb)) {
2767 tag = get_ipv6_5tuple_tag(skb);
2768
2769 cmdsetup.s.cksum_offset = sizeof(struct ethhdr) + 1;
2770
2771 if (is_with_extn_hdr(skb))
2772 cmdsetup.s.ipv4opts_ipv6exthdr =
2773 OCT_PKT_PARAM_IPV6EXTHDR;
2774
2775 } else if (is_vlan(skb)) {
2776 if (vlan_eth_hdr(skb)->h_vlan_encapsulated_proto
2777 == htons(ETH_P_IP) &&
2778 !is_ip_fragmented(skb) && is_tcpudp(skb)) {
2779 tag = get_ipv4_5tuple_tag(skb);
2780
2781 cmdsetup.s.cksum_offset =
2782 sizeof(struct vlan_ethhdr) + 1;
2783
2784 if (ip_hdr(skb)->ihl > 5)
2785 cmdsetup.s.ipv4opts_ipv6exthdr =
2786 OCT_PKT_PARAM_IPV4OPTS;
2787
2788 } else if (vlan_eth_hdr(skb)->h_vlan_encapsulated_proto
2789 == htons(ETH_P_IPV6)) {
2790 tag = get_ipv6_5tuple_tag(skb);
2791
2792 cmdsetup.s.cksum_offset =
2793 sizeof(struct vlan_ethhdr) + 1;
2794
2795 if (is_with_extn_hdr(skb))
2796 cmdsetup.s.ipv4opts_ipv6exthdr =
2797 OCT_PKT_PARAM_IPV6EXTHDR;
2798 }
2799 }
2800 }
2801 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2802 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2803 cmdsetup.s.timestamp = 1;
2804 }
2805
2806 if (skb_shinfo(skb)->nr_frags == 0) {
2807 cmdsetup.s.u.datasize = skb->len;
2808 octnet_prepare_pci_cmd(&ndata.cmd, &cmdsetup, tag);
2809 /* Offload checksum calculation for TCP/UDP packets */
2810 ndata.cmd.dptr = dma_map_single(&oct->pci_dev->dev,
2811 skb->data,
2812 skb->len,
2813 DMA_TO_DEVICE);
2814 if (dma_mapping_error(&oct->pci_dev->dev, ndata.cmd.dptr)) {
2815 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2816 __func__);
2817 return NETDEV_TX_BUSY;
2818 }
2819
2820 finfo->dptr = ndata.cmd.dptr;
2821
2822 ndata.reqtype = REQTYPE_NORESP_NET;
2823
2824 } else {
2825 int i, frags;
2826 struct skb_frag_struct *frag;
2827 struct octnic_gather *g;
2828
2829 spin_lock(&lio->lock);
2830 g = (struct octnic_gather *)list_delete_head(&lio->glist);
2831 spin_unlock(&lio->lock);
2832
2833 if (!g) {
2834 netif_info(lio, tx_err, lio->netdev,
2835 "Transmit scatter gather: glist null!\n");
2836 goto lio_xmit_failed;
2837 }
2838
2839 cmdsetup.s.gather = 1;
2840 cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
2841 octnet_prepare_pci_cmd(&ndata.cmd, &cmdsetup, tag);
2842
2843 memset(g->sg, 0, g->sg_size);
2844
2845 g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2846 skb->data,
2847 (skb->len - skb->data_len),
2848 DMA_TO_DEVICE);
2849 if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2850 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2851 __func__);
2852 return NETDEV_TX_BUSY;
2853 }
2854 add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2855
2856 frags = skb_shinfo(skb)->nr_frags;
2857 i = 1;
2858 while (frags--) {
2859 frag = &skb_shinfo(skb)->frags[i - 1];
2860
2861 g->sg[(i >> 2)].ptr[(i & 3)] =
2862 dma_map_page(&oct->pci_dev->dev,
2863 frag->page.p,
2864 frag->page_offset,
2865 frag->size,
2866 DMA_TO_DEVICE);
2867
2868 add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2869 i++;
2870 }
2871
2872 ndata.cmd.dptr = dma_map_single(&oct->pci_dev->dev,
2873 g->sg, g->sg_size,
2874 DMA_TO_DEVICE);
2875 if (dma_mapping_error(&oct->pci_dev->dev, ndata.cmd.dptr)) {
2876 dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2877 __func__);
2878 dma_unmap_single(&oct->pci_dev->dev, g->sg[0].ptr[0],
2879 skb->len - skb->data_len,
2880 DMA_TO_DEVICE);
2881 return NETDEV_TX_BUSY;
2882 }
2883
2884 finfo->dptr = ndata.cmd.dptr;
2885 finfo->g = g;
2886
2887 ndata.reqtype = REQTYPE_NORESP_NET_SG;
2888 }
2889
2890 if (skb_shinfo(skb)->gso_size) {
2891 struct octeon_instr_irh *irh =
2892 (struct octeon_instr_irh *)&ndata.cmd.irh;
2893 union tx_info *tx_info = (union tx_info *)&ndata.cmd.ossp[0];
2894
2895 irh->len = 1; /* to indicate that ossp[0] contains tx_info */
2896 tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2897 tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
2898 }
2899
2900 xmit_more = skb->xmit_more;
2901
2902 if (unlikely(cmdsetup.s.timestamp))
2903 status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
2904 else
2905 status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
2906 if (status == IQ_SEND_FAILED)
2907 goto lio_xmit_failed;
2908
2909 netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2910
2911 if (status == IQ_SEND_STOP)
2912 stop_q(lio->netdev, q_idx);
2913
Florian Westphal860e9532016-05-03 16:33:13 +02002914 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002915
2916 stats->tx_done++;
2917 stats->tx_tot_bytes += skb->len;
2918
2919 return NETDEV_TX_OK;
2920
2921lio_xmit_failed:
2922 stats->tx_dropped++;
2923 netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2924 iq_no, stats->tx_dropped);
2925 dma_unmap_single(&oct->pci_dev->dev, ndata.cmd.dptr,
2926 ndata.datasize, DMA_TO_DEVICE);
2927 recv_buffer_free(skb);
2928 return NETDEV_TX_OK;
2929}
2930
2931/** \brief Network device Tx timeout
2932 * @param netdev pointer to network device
2933 */
2934static void liquidio_tx_timeout(struct net_device *netdev)
2935{
2936 struct lio *lio;
2937
2938 lio = GET_LIO(netdev);
2939
2940 netif_info(lio, tx_err, lio->netdev,
2941 "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2942 netdev->stats.tx_dropped);
Florian Westphal860e9532016-05-03 16:33:13 +02002943 netif_trans_update(netdev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07002944 txqs_wake(netdev);
2945}
2946
2947int liquidio_set_feature(struct net_device *netdev, int cmd)
2948{
2949 struct lio *lio = GET_LIO(netdev);
2950 struct octeon_device *oct = lio->oct_dev;
2951 struct octnic_ctrl_pkt nctrl;
2952 struct octnic_ctrl_params nparams;
2953 int ret = 0;
2954
2955 memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2956
2957 nctrl.ncmd.u64 = 0;
2958 nctrl.ncmd.s.cmd = cmd;
2959 nctrl.ncmd.s.param1 = lio->linfo.ifidx;
2960 nctrl.ncmd.s.param2 = OCTNIC_LROIPV4 | OCTNIC_LROIPV6;
2961 nctrl.wait_time = 100;
2962 nctrl.netpndev = (u64)netdev;
2963 nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2964
2965 nparams.resp_order = OCTEON_RESP_NORESPONSE;
2966
2967 ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
2968 if (ret < 0) {
2969 dev_err(&oct->pci_dev->dev, "Feature change failed in core (ret: 0x%x)\n",
2970 ret);
2971 }
2972 return ret;
2973}
2974
2975/** \brief Net device fix features
2976 * @param netdev pointer to network device
2977 * @param request features requested
2978 * @returns updated features list
2979 */
2980static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2981 netdev_features_t request)
2982{
2983 struct lio *lio = netdev_priv(netdev);
2984
2985 if ((request & NETIF_F_RXCSUM) &&
2986 !(lio->dev_capability & NETIF_F_RXCSUM))
2987 request &= ~NETIF_F_RXCSUM;
2988
2989 if ((request & NETIF_F_HW_CSUM) &&
2990 !(lio->dev_capability & NETIF_F_HW_CSUM))
2991 request &= ~NETIF_F_HW_CSUM;
2992
2993 if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
2994 request &= ~NETIF_F_TSO;
2995
2996 if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
2997 request &= ~NETIF_F_TSO6;
2998
2999 if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
3000 request &= ~NETIF_F_LRO;
3001
3002 /*Disable LRO if RXCSUM is off */
3003 if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
3004 (lio->dev_capability & NETIF_F_LRO))
3005 request &= ~NETIF_F_LRO;
3006
3007 return request;
3008}
3009
3010/** \brief Net device set features
3011 * @param netdev pointer to network device
3012 * @param features features to enable/disable
3013 */
3014static int liquidio_set_features(struct net_device *netdev,
3015 netdev_features_t features)
3016{
3017 struct lio *lio = netdev_priv(netdev);
3018
3019 if (!((netdev->features ^ features) & NETIF_F_LRO))
3020 return 0;
3021
3022 if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
3023 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE);
3024 else if (!(features & NETIF_F_LRO) &&
3025 (lio->dev_capability & NETIF_F_LRO))
3026 liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE);
3027
3028 return 0;
3029}
3030
3031static struct net_device_ops lionetdevops = {
3032 .ndo_open = liquidio_open,
3033 .ndo_stop = liquidio_stop,
3034 .ndo_start_xmit = liquidio_xmit,
3035 .ndo_get_stats = liquidio_get_stats,
3036 .ndo_set_mac_address = liquidio_set_mac,
3037 .ndo_set_rx_mode = liquidio_set_mcast_list,
3038 .ndo_tx_timeout = liquidio_tx_timeout,
3039 .ndo_change_mtu = liquidio_change_mtu,
3040 .ndo_do_ioctl = liquidio_ioctl,
3041 .ndo_fix_features = liquidio_fix_features,
3042 .ndo_set_features = liquidio_set_features,
3043};
3044
3045/** \brief Entry point for the liquidio module
3046 */
3047static int __init liquidio_init(void)
3048{
3049 int i;
3050 struct handshake *hs;
3051
3052 init_completion(&first_stage);
3053
3054 octeon_init_device_list(conf_type);
3055
3056 if (liquidio_init_pci())
3057 return -EINVAL;
3058
3059 wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3060
3061 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3062 hs = &handshake[i];
3063 if (hs->pci_dev) {
3064 wait_for_completion(&hs->init);
3065 if (!hs->init_ok) {
3066 /* init handshake failed */
3067 dev_err(&hs->pci_dev->dev,
3068 "Failed to init device\n");
3069 liquidio_deinit_pci();
3070 return -EIO;
3071 }
3072 }
3073 }
3074
3075 for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3076 hs = &handshake[i];
3077 if (hs->pci_dev) {
3078 wait_for_completion_timeout(&hs->started,
3079 msecs_to_jiffies(30000));
3080 if (!hs->started_ok) {
3081 /* starter handshake failed */
3082 dev_err(&hs->pci_dev->dev,
3083 "Firmware failed to start\n");
3084 liquidio_deinit_pci();
3085 return -EIO;
3086 }
3087 }
3088 }
3089
3090 return 0;
3091}
3092
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -07003093static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003094{
3095 struct octeon_device *oct = (struct octeon_device *)buf;
3096 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
3097 int ifidx = 0;
3098 union oct_link_status *ls;
3099 int i;
3100
3101 if ((recv_pkt->buffer_size[0] != sizeof(*ls)) ||
3102 (recv_pkt->rh.r_nic_info.ifidx > oct->ifcount)) {
3103 dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3104 recv_pkt->buffer_size[0],
3105 recv_pkt->rh.r_nic_info.ifidx);
3106 goto nic_info_err;
3107 }
3108
3109 ifidx = recv_pkt->rh.r_nic_info.ifidx;
3110 ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
3111
3112 octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
3113
3114 update_link_status(oct->props[ifidx].netdev, ls);
3115
3116nic_info_err:
3117 for (i = 0; i < recv_pkt->buffer_count; i++)
3118 recv_buffer_free(recv_pkt->buffer_ptr[i]);
3119 octeon_free_recv_info(recv_info);
3120 return 0;
3121}
3122
3123/**
3124 * \brief Setup network interfaces
3125 * @param octeon_dev octeon device
3126 *
3127 * Called during init time for each device. It assumes the NIC
3128 * is already up and running. The link information for each
3129 * interface is passed in link_info.
3130 */
3131static int setup_nic_devices(struct octeon_device *octeon_dev)
3132{
3133 struct lio *lio = NULL;
3134 struct net_device *netdev;
3135 u8 mac[6], i, j;
3136 struct octeon_soft_command *sc;
3137 struct liquidio_if_cfg_context *ctx;
3138 struct liquidio_if_cfg_resp *resp;
3139 struct octdev_props *props;
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003140 int retval, num_iqueues, num_oqueues;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003141 u64 q_mask;
3142 int num_cpus = num_online_cpus();
3143 union oct_nic_if_cfg if_cfg;
3144 unsigned int base_queue;
3145 unsigned int gmx_port_id;
3146 u32 resp_size, ctx_size;
3147
3148 /* This is to handle link status changes */
3149 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3150 OPCODE_NIC_INFO,
3151 lio_nic_info, octeon_dev);
3152
3153 /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3154 * They are handled directly.
3155 */
3156 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3157 free_netbuf);
3158
3159 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3160 free_netsgbuf);
3161
3162 octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3163 free_netsgbuf_with_resp);
3164
3165 for (i = 0; i < octeon_dev->ifcount; i++) {
3166 resp_size = sizeof(struct liquidio_if_cfg_resp);
3167 ctx_size = sizeof(struct liquidio_if_cfg_context);
3168 sc = (struct octeon_soft_command *)
3169 octeon_alloc_soft_command(octeon_dev, 0,
3170 resp_size, ctx_size);
3171 resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3172 ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
3173
3174 num_iqueues =
3175 CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3176 num_oqueues =
3177 CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
3178 base_queue =
3179 CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
3180 gmx_port_id =
3181 CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
3182 if (num_iqueues > num_cpus)
3183 num_iqueues = num_cpus;
3184 if (num_oqueues > num_cpus)
3185 num_oqueues = num_cpus;
3186 dev_dbg(&octeon_dev->pci_dev->dev,
3187 "requesting config for interface %d, iqs %d, oqs %d\n",
3188 i, num_iqueues, num_oqueues);
3189 ACCESS_ONCE(ctx->cond) = 0;
3190 ctx->octeon_id = lio_get_device_id(octeon_dev);
3191 init_waitqueue_head(&ctx->wc);
3192
3193 if_cfg.u64 = 0;
3194 if_cfg.s.num_iqueues = num_iqueues;
3195 if_cfg.s.num_oqueues = num_oqueues;
3196 if_cfg.s.base_queue = base_queue;
3197 if_cfg.s.gmx_port_id = gmx_port_id;
3198 octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
3199 OPCODE_NIC_IF_CFG, i,
3200 if_cfg.u64, 0);
3201
3202 sc->callback = if_cfg_callback;
3203 sc->callback_arg = sc;
3204 sc->wait_time = 1000;
3205
3206 retval = octeon_send_soft_command(octeon_dev, sc);
Raghu Vatsavayiddc173a2016-06-14 16:54:43 -07003207 if (retval == IQ_SEND_FAILED) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003208 dev_err(&octeon_dev->pci_dev->dev,
3209 "iq/oq config failed status: %x\n",
3210 retval);
3211 /* Soft instr is freed by driver in case of failure. */
3212 goto setup_nic_dev_fail;
3213 }
3214
3215 /* Sleep on a wait queue till the cond flag indicates that the
3216 * response arrived or timed-out.
3217 */
3218 sleep_cond(&ctx->wc, &ctx->cond);
3219 retval = resp->status;
3220 if (retval) {
3221 dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3222 goto setup_nic_dev_fail;
3223 }
3224
3225 octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3226 (sizeof(struct liquidio_if_cfg_info)) >> 3);
3227
3228 num_iqueues = hweight64(resp->cfg_info.iqmask);
3229 num_oqueues = hweight64(resp->cfg_info.oqmask);
3230
3231 if (!(num_iqueues) || !(num_oqueues)) {
3232 dev_err(&octeon_dev->pci_dev->dev,
3233 "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3234 resp->cfg_info.iqmask,
3235 resp->cfg_info.oqmask);
3236 goto setup_nic_dev_fail;
3237 }
3238 dev_dbg(&octeon_dev->pci_dev->dev,
3239 "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3240 i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3241 num_iqueues, num_oqueues);
3242 netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3243
3244 if (!netdev) {
3245 dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3246 goto setup_nic_dev_fail;
3247 }
3248
3249 props = &octeon_dev->props[i];
3250 props->netdev = netdev;
3251
3252 if (num_iqueues > 1)
3253 lionetdevops.ndo_select_queue = select_q;
3254
3255 /* Associate the routines that will handle different
3256 * netdev tasks.
3257 */
3258 netdev->netdev_ops = &lionetdevops;
3259
3260 lio = GET_LIO(netdev);
3261
3262 memset(lio, 0, sizeof(struct lio));
3263
3264 lio->linfo.ifidx = resp->cfg_info.ifidx;
3265 lio->ifidx = resp->cfg_info.ifidx;
3266
3267 lio->linfo.num_rxpciq = num_oqueues;
3268 lio->linfo.num_txpciq = num_iqueues;
3269 q_mask = resp->cfg_info.oqmask;
3270 /* q_mask is 0-based and already verified mask is nonzero */
3271 for (j = 0; j < num_oqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003272 lio->linfo.rxpciq[j].u64 =
3273 resp->cfg_info.linfo.rxpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003274 }
3275 q_mask = resp->cfg_info.iqmask;
3276 for (j = 0; j < num_iqueues; j++) {
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003277 lio->linfo.txpciq[j].u64 =
3278 resp->cfg_info.linfo.txpciq[j].u64;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003279 }
3280 lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3281 lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3282 lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3283
3284 lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3285
3286 lio->dev_capability = NETIF_F_HIGHDMA
3287 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
3288 | NETIF_F_SG | NETIF_F_RXCSUM
3289 | NETIF_F_TSO | NETIF_F_TSO6
3290 | NETIF_F_LRO;
3291 netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3292
3293 netdev->features = lio->dev_capability;
3294 netdev->vlan_features = lio->dev_capability;
3295
3296 netdev->hw_features = lio->dev_capability;
3297
3298 /* Point to the properties for octeon device to which this
3299 * interface belongs.
3300 */
3301 lio->oct_dev = octeon_dev;
3302 lio->octprops = props;
3303 lio->netdev = netdev;
3304 spin_lock_init(&lio->lock);
3305
3306 dev_dbg(&octeon_dev->pci_dev->dev,
3307 "if%d gmx: %d hw_addr: 0x%llx\n", i,
3308 lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3309
3310 /* 64-bit swap required on LE machines */
3311 octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3312 for (j = 0; j < 6; j++)
3313 mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3314
3315 /* Copy MAC Address to OS network device structure */
3316
3317 ether_addr_copy(netdev->dev_addr, mac);
3318
Raghu Vatsavayi26236fa2016-06-14 16:54:44 -07003319 /* By default all interfaces on a single Octeon uses the same
3320 * tx and rx queues
3321 */
3322 lio->txq = lio->linfo.txpciq[0].s.q_no;
3323 lio->rxq = lio->linfo.rxpciq[0].s.q_no;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003324 if (setup_io_queues(octeon_dev, netdev)) {
3325 dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3326 goto setup_nic_dev_fail;
3327 }
3328
3329 ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3330
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07003331 lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3332 lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3333
3334 if (setup_glist(lio)) {
3335 dev_err(&octeon_dev->pci_dev->dev,
3336 "Gather list allocation failed\n");
3337 goto setup_nic_dev_fail;
3338 }
3339
3340 /* Register ethtool support */
3341 liquidio_set_ethtool_ops(netdev);
3342
3343 liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE);
3344
3345 if ((debug != -1) && (debug & NETIF_MSG_HW))
3346 liquidio_set_feature(netdev, OCTNET_CMD_VERBOSE_ENABLE);
3347
3348 /* Register the network device with the OS */
3349 if (register_netdev(netdev)) {
3350 dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3351 goto setup_nic_dev_fail;
3352 }
3353
3354 dev_dbg(&octeon_dev->pci_dev->dev,
3355 "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3356 i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3357 netif_carrier_off(netdev);
3358
3359 if (lio->linfo.link.s.status) {
3360 netif_carrier_on(netdev);
3361 start_txq(netdev);
3362 } else {
3363 netif_carrier_off(netdev);
3364 }
3365
3366 ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3367
3368 dev_dbg(&octeon_dev->pci_dev->dev,
3369 "NIC ifidx:%d Setup successful\n", i);
3370
3371 octeon_free_soft_command(octeon_dev, sc);
3372 }
3373
3374 return 0;
3375
3376setup_nic_dev_fail:
3377
3378 octeon_free_soft_command(octeon_dev, sc);
3379
3380 while (i--) {
3381 dev_err(&octeon_dev->pci_dev->dev,
3382 "NIC ifidx:%d Setup failed\n", i);
3383 liquidio_destroy_nic_device(octeon_dev, i);
3384 }
3385 return -ENODEV;
3386}
3387
3388/**
3389 * \brief initialize the NIC
3390 * @param oct octeon device
3391 *
3392 * This initialization routine is called once the Octeon device application is
3393 * up and running
3394 */
3395static int liquidio_init_nic_module(struct octeon_device *oct)
3396{
3397 struct oct_intrmod_cfg *intrmod_cfg;
3398 int retval = 0;
3399 int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3400
3401 dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3402
3403 /* only default iq and oq were initialized
3404 * initialize the rest as well
3405 */
3406 /* run port_config command for each port */
3407 oct->ifcount = num_nic_ports;
3408
3409 memset(oct->props, 0,
3410 sizeof(struct octdev_props) * num_nic_ports);
3411
3412 retval = setup_nic_devices(oct);
3413 if (retval) {
3414 dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3415 goto octnet_init_failure;
3416 }
3417
3418 liquidio_ptp_init(oct);
3419
3420 /* Initialize interrupt moderation params */
3421 intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
3422 intrmod_cfg->intrmod_enable = 1;
3423 intrmod_cfg->intrmod_check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
3424 intrmod_cfg->intrmod_maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
3425 intrmod_cfg->intrmod_minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
3426 intrmod_cfg->intrmod_maxcnt_trigger = LIO_INTRMOD_MAXCNT_TRIGGER;
3427 intrmod_cfg->intrmod_maxtmr_trigger = LIO_INTRMOD_MAXTMR_TRIGGER;
3428 intrmod_cfg->intrmod_mintmr_trigger = LIO_INTRMOD_MINTMR_TRIGGER;
3429 intrmod_cfg->intrmod_mincnt_trigger = LIO_INTRMOD_MINCNT_TRIGGER;
3430
3431 dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
3432
3433 return retval;
3434
3435octnet_init_failure:
3436
3437 oct->ifcount = 0;
3438
3439 return retval;
3440}
3441
3442/**
3443 * \brief starter callback that invokes the remaining initialization work after
3444 * the NIC is up and running.
3445 * @param octptr work struct work_struct
3446 */
3447static void nic_starter(struct work_struct *work)
3448{
3449 struct octeon_device *oct;
3450 struct cavium_wk *wk = (struct cavium_wk *)work;
3451
3452 oct = (struct octeon_device *)wk->ctxptr;
3453
3454 if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
3455 return;
3456
3457 /* If the status of the device is CORE_OK, the core
3458 * application has reported its application type. Call
3459 * any registered handlers now and move to the RUNNING
3460 * state.
3461 */
3462 if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
3463 schedule_delayed_work(&oct->nic_poll_work.work,
3464 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3465 return;
3466 }
3467
3468 atomic_set(&oct->status, OCT_DEV_RUNNING);
3469
3470 if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
3471 dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
3472
3473 if (liquidio_init_nic_module(oct))
3474 dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
3475 else
3476 handshake[oct->octeon_id].started_ok = 1;
3477 } else {
3478 dev_err(&oct->pci_dev->dev,
3479 "Unexpected application running on NIC (%d). Check firmware.\n",
3480 oct->app_mode);
3481 }
3482
3483 complete(&handshake[oct->octeon_id].started);
3484}
3485
3486/**
3487 * \brief Device initialization for each Octeon device that is probed
3488 * @param octeon_dev octeon device
3489 */
3490static int octeon_device_init(struct octeon_device *octeon_dev)
3491{
3492 int j, ret;
3493 struct octeon_device_priv *oct_priv =
3494 (struct octeon_device_priv *)octeon_dev->priv;
3495 atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
3496
3497 /* Enable access to the octeon device and make its DMA capability
3498 * known to the OS.
3499 */
3500 if (octeon_pci_os_setup(octeon_dev))
3501 return 1;
3502
3503 /* Identify the Octeon type and map the BAR address space. */
3504 if (octeon_chip_specific_setup(octeon_dev)) {
3505 dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
3506 return 1;
3507 }
3508
3509 atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
3510
3511 octeon_dev->app_mode = CVM_DRV_INVALID_APP;
3512
3513 /* Do a soft reset of the Octeon device. */
3514 if (octeon_dev->fn_list.soft_reset(octeon_dev))
3515 return 1;
3516
3517 /* Initialize the dispatch mechanism used to push packets arriving on
3518 * Octeon Output queues.
3519 */
3520 if (octeon_init_dispatch_list(octeon_dev))
3521 return 1;
3522
3523 octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3524 OPCODE_NIC_CORE_DRV_ACTIVE,
3525 octeon_core_drv_init,
3526 octeon_dev);
3527
3528 INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
3529 octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
3530 schedule_delayed_work(&octeon_dev->nic_poll_work.work,
3531 LIQUIDIO_STARTER_POLL_INTERVAL_MS);
3532
3533 atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
3534
3535 octeon_set_io_queues_off(octeon_dev);
3536
3537 /* Setup the data structures that manage this Octeon's Input queues. */
3538 if (octeon_setup_instr_queues(octeon_dev)) {
3539 dev_err(&octeon_dev->pci_dev->dev,
3540 "instruction queue initialization failed\n");
3541 /* On error, release any previously allocated queues */
3542 for (j = 0; j < octeon_dev->num_iqs; j++)
3543 octeon_delete_instr_queue(octeon_dev, j);
3544 return 1;
3545 }
3546 atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
3547
3548 /* Initialize soft command buffer pool
3549 */
3550 if (octeon_setup_sc_buffer_pool(octeon_dev)) {
3551 dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
3552 return 1;
3553 }
3554 atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
3555
3556 /* Initialize lists to manage the requests of different types that
3557 * arrive from user & kernel applications for this octeon device.
3558 */
3559 if (octeon_setup_response_list(octeon_dev)) {
3560 dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
3561 return 1;
3562 }
3563 atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
3564
3565 if (octeon_setup_output_queues(octeon_dev)) {
3566 dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
3567 /* Release any previously allocated queues */
3568 for (j = 0; j < octeon_dev->num_oqs; j++)
3569 octeon_delete_droq(octeon_dev, j);
3570 }
3571
3572 atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
3573
3574 /* The input and output queue registers were setup earlier (the queues
3575 * were not enabled). Any additional registers that need to be
3576 * programmed should be done now.
3577 */
3578 ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
3579 if (ret) {
3580 dev_err(&octeon_dev->pci_dev->dev,
3581 "Failed to configure device registers\n");
3582 return ret;
3583 }
3584
3585 /* Initialize the tasklet that handles output queue packet processing.*/
3586 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
3587 tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
3588 (unsigned long)octeon_dev);
3589
3590 /* Setup the interrupt handler and record the INT SUM register address
3591 */
3592 octeon_setup_interrupt(octeon_dev);
3593
3594 /* Enable Octeon device interrupts */
3595 octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
3596
3597 /* Enable the input and output queues for this Octeon device */
3598 octeon_dev->fn_list.enable_io_queues(octeon_dev);
3599
3600 atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
3601
3602 dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
3603
3604 if (ddr_timeout == 0) {
3605 dev_info(&octeon_dev->pci_dev->dev,
3606 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
3607 }
3608
3609 schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
3610
3611 /* Wait for the octeon to initialize DDR after the soft-reset. */
3612 ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
3613 if (ret) {
3614 dev_err(&octeon_dev->pci_dev->dev,
3615 "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
3616 ret);
3617 return 1;
3618 }
3619
3620 if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
3621 dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
3622 return 1;
3623 }
3624
3625 dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
3626 ret = octeon_init_consoles(octeon_dev);
3627 if (ret) {
3628 dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
3629 return 1;
3630 }
3631 ret = octeon_add_console(octeon_dev, 0);
3632 if (ret) {
3633 dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
3634 return 1;
3635 }
3636
3637 atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
3638
3639 dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
3640 ret = load_firmware(octeon_dev);
3641 if (ret) {
3642 dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
3643 return 1;
3644 }
3645
3646 handshake[octeon_dev->octeon_id].init_ok = 1;
3647 complete(&handshake[octeon_dev->octeon_id].init);
3648
3649 atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
3650
3651 /* Send Credit for Octeon Output queues. Credits are always sent after
3652 * the output queue is enabled.
3653 */
3654 for (j = 0; j < octeon_dev->num_oqs; j++)
3655 writel(octeon_dev->droq[j]->max_count,
3656 octeon_dev->droq[j]->pkts_credit_reg);
3657
3658 /* Packets can start arriving on the output queues from this point. */
3659
3660 return 0;
3661}
3662
3663/**
3664 * \brief Exits the module
3665 */
3666static void __exit liquidio_exit(void)
3667{
3668 liquidio_deinit_pci();
3669
3670 pr_info("LiquidIO network module is now unloaded\n");
3671}
3672
3673module_init(liquidio_init);
3674module_exit(liquidio_exit);