openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

linux-bde.h (7693B)


      1 /***********************************************************************
      2  *
      3  * 
      4  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      5  * 
      6  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      7  *
      8  * Linux Broadcom Device Enumerators
      9  *
     10  *
     11  * There are two Linux BDEs:
     12  *
     13  * 1. Linux Kernel BDE
     14  *
     15  *	This is a kernel module implementing a BDE
     16  *	for the driver running as part of the kernel. 
     17  * 	
     18  *	It manages the devices through the linux PCI interfaces, 
     19  *	and manages a chunk of contiguous, boot-time allocated
     20  *	DMA memory. This is all that is needed if the BCM driver
     21  *	is run as part of the kernel (in another module). 
     22  *
     23  * 2. Linux User BDE
     24  *
     25  *	This is a kernel module and userland library which implement
     26  * 	a complete BDE for applications running in userland. 
     27  *	
     28  *	The kernel module relies upon the real kernel bde, 
     29  *	and allows a user space application (through the user library)
     30  *	to talk directly to the devices. It also virtualized the device 
     31  *	interrupts, so the entire driver can be run as a userspace 
     32  *	application. 
     33  *	
     34  *	While this causes a significant degradation in performance, 
     35  *	because the system runs as a user application, the development
     36  *	and debugging process is about a gillion times easier. 
     37  *	After the core logic is debugged, it can be retargeted using
     38  *	only the kernel bde and run in the kernel. 
     39  */
     40 
     41 #ifndef __LINUX_BDE_H__
     42 #define __LINUX_BDE_H__
     43 
     44 #include <sal/types.h>
     45 
     46 #include <ibde.h>
     47 
     48 
     49 /* 
     50  * Device Major Numbers
     51  * 
     52  * The kernel and user bdes need unique major numbers
     53  * on systems that do not use devfs. 
     54  * 
     55  * They are defined here, along with the module names, 
     56  * to document them if you need to mknod them (or open) them, 
     57  * and to keep them unique. 
     58  */
     59 
     60 #include <linux/version.h>
     61 
     62 #ifdef __KERNEL__
     63 #include <linux/types.h>
     64 /* Key stone and Raptor has 2.6.21 but don't have definition */
     65 #if defined(KEYSTONE) || defined(RAPTOR)
     66 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,21))
     67     #ifdef PHYS_ADDRS_ARE_64BITS
     68     typedef u64 phys_addr_t;
     69     #else
     70     typedef u32 phys_addr_t;
     71     #endif
     72   #endif
     73 #endif
     74 #endif
     75 
     76 
     77 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
     78 #define LINUX_BDE_DMA_DEVICE_SUPPORT
     79 #endif
     80 
     81 #define LINUX_KERNEL_BDE_NAME 	"linux-kernel-bde"
     82 #define LINUX_KERNEL_BDE_MAJOR 	127
     83 
     84 #define LINUX_USER_BDE_NAME 	"linux-user-bde"
     85 #define LINUX_USER_BDE_MAJOR   	126
     86 
     87 
     88 /* Max devices */
     89 /* 16 switch chips + 2 out-of-band Ethernet + 2 CPUs */
     90 #define LINUX_BDE_MAX_SWITCH_DEVICES    16
     91 #define LINUX_BDE_MAX_ETHER_DEVICES     2
     92 #define LINUX_BDE_MAX_CPU_DEVICES       2
     93 #define LINUX_BDE_MAX_DEVICES           (LINUX_BDE_MAX_SWITCH_DEVICES + \
     94                                          LINUX_BDE_MAX_ETHER_DEVICES + \
     95                                          LINUX_BDE_MAX_CPU_DEVICES) 
     96 
     97 /* 
     98  * PCI devices will be initialized by the Linux Kernel, 
     99  * regardless of architecture.
    100  *
    101  * You need only provide bus endian settings. 
    102  */
    103 
    104 typedef struct linux_bde_bus_s {
    105     int be_pio;
    106     int be_packet;
    107     int be_other;
    108 } linux_bde_bus_t;
    109 
    110 
    111 
    112 /* Device state used for PCI hot swap case. */
    113 /*
    114  * BDE_DEV_STATE_NORMAL : A device is probed normally. Or when the device 
    115  * resource has been updated after "CHANGED", the state will move back to 
    116  * "NORMAL".
    117  */
    118 #define BDE_DEV_STATE_NORMAL       (0)
    119 /*
    120  * BDE_DEV_STATE_REMOVED : A previous probed device was removed.
    121  * We will avoid any device access while the device is in this state.
    122  * The state will be moved to "CHANGED" if the device is re-inserted
    123  * and re-probed.
    124  */
    125 #define BDE_DEV_STATE_REMOVED      (1)
    126 /* 
    127  * BDE_DEV_STATE_CHANGED : The device is re-probed after having been removed.
    128  * The resouces assigned to the device might have been changed after
    129  * re-probing, so we need to re-initialize our resource database accordingly. 
    130  * The state will change to "NORMAL" when the resource have been updated.
    131  */
    132 #define BDE_DEV_STATE_CHANGED      (2)
    133 
    134 extern int linux_bde_create(linux_bde_bus_t* bus, ibde_t** bde);
    135 extern int linux_bde_destroy(ibde_t* bde);
    136 #ifdef BCM_INSTANCE_SUPPORT
    137 extern int linux_bde_instance_attach(unsigned int dev_mask,unsigned int dma_size);
    138 #endif
    139 
    140 #ifdef __KERNEL__
    141 
    142 /*
    143  *  Backdoors provided by the kernel bde
    144  */
    145 
    146 /*
    147  * The user bde needs to get cpu physical address for
    148  * the userland code to mmap.
    149  * And the second address is bus address, it is either
    150  * identical to cpu physical address or another address
    151  * (IOVA) translated by IOMMU.
    152  */
    153 extern int lkbde_get_dma_info(phys_addr_t *cpu_pbase, phys_addr_t *dma_pbase, ssize_t *size);
    154 extern uint32 lkbde_get_dev_phys(int d);
    155 extern uint32 lkbde_get_dev_phys_hi(int d);
    156 
    157 /*
    158  * Virtual device address needed by kernel space
    159  * interrupt handler.
    160  */
    161 extern void *lkbde_get_dev_virt(int d);
    162 
    163 /*
    164  * The user bde needs to get some physical addresses for
    165  * the userland code to mmap. The following functions
    166  * supports multiple resources for a single device.
    167  */
    168 extern int lkbde_get_dev_resource(int d, int rsrc, uint32 *flags,
    169                                   uint32 *phys_lo, uint32 *phys_hi); 
    170 
    171 /*
    172  * Backdoor to retrieve OS device structure to be used for
    173  * DMA operations.
    174  */
    175 extern void *lkbde_get_dma_dev(int d);
    176 
    177 /*
    178  * Backdoor to retrieve original hardware/OS device.
    179  */
    180 extern void *lkbde_get_hw_dev(int d);
    181 
    182 /*
    183  * Backdoor to retrieve number of switch devices probed.
    184  */
    185 extern int lkbde_get_num_devices(int type);
    186 
    187 /*
    188  * Retrive the device state from Kernel BDE.
    189  * Used for KNET and User BDE for pci hot swap case.
    190  */
    191 extern int lkbde_dev_state_get(int d, uint32 *state);
    192 extern int lkbde_dev_state_set(int d, uint32 state);
    193 
    194 /*
    195  * Retrive the mapping between emulated HW device and instance id
    196  */
    197 extern int lkbde_dev_instid_get(int d, uint32 *instid);
    198 extern int lkbde_dev_instid_set(int d, uint32 instid);
    199 
    200 /*
    201  * Functions that allow an interrupt handler in user mode to
    202  * coexist with interrupt handler in kernel module.
    203  */
    204 extern int lkbde_irq_mask_set(int d, uint32 addr, uint32 mask, uint32 fmask);
    205 extern int lkbde_irq_mask_get(int d, uint32 *mask, uint32 *fmask);
    206 
    207 #ifdef BCM_SAND_SUPPORT
    208 extern int lkbde_cpu_write(int d, uint32 addr, uint32 *buf);
    209 extern int lkbde_cpu_read(int d, uint32 addr, uint32 *buf);
    210 extern int lkbde_cpu_pci_register(int d);
    211 #endif
    212 
    213 /*
    214  * This flag must be OR'ed onto the device number when calling
    215  * interrupt_connect/disconnect and irq_mask_set functions from
    216  * a secondary device driver.
    217  */
    218 #define LKBDE_ISR2_DEV  0x8000
    219 /*
    220  * This flag should be OR'ed onto the device number when calling
    221  * irq_mask_set functions from a secondary device driver if the
    222  * mask register is iProc register.
    223  */
    224 #define LKBDE_IPROC_REG 0x4000
    225 
    226 #ifdef BCM_SAND_SUPPORT
    227 #include <linux/version.h>
    228 #if defined(__DUNE_LINUX_BCM_CPU_PCIE__) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
    229 #ifndef _SIMPLE_MEMORY_ALLOCATION_
    230 #define _SIMPLE_MEMORY_ALLOCATION_ 1
    231 #endif
    232 #endif
    233 #endif
    234 
    235 #if defined(IPROC_CMICD) && defined(CONFIG_CMA)
    236 #ifndef _SIMPLE_MEMORY_ALLOCATION_
    237 #define _SIMPLE_MEMORY_ALLOCATION_ 1
    238 #endif
    239 #endif
    240 
    241 /* Don't use _SIMPLE_MEMORY_ALLOCATION_ method for newer kernel than 3.10.0 */
    242 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
    243 #ifndef _SIMPLE_MEMORY_ALLOCATION_
    244 #define _SIMPLE_MEMORY_ALLOCATION_ 0
    245 #endif
    246 #endif
    247 
    248 /* Allocation via dma_alloc_coherent is turned off by default */
    249 #ifndef _SIMPLE_MEMORY_ALLOCATION_
    250 #define _SIMPLE_MEMORY_ALLOCATION_ 9 /* compile in the allocation method, but do not use it by default */
    251 #endif
    252 
    253 /* By default we use our private mmap for DMA pool */
    254 #ifndef USE_LINUX_BDE_MMAP
    255 #define USE_LINUX_BDE_MMAP 1
    256 #endif
    257 
    258 #endif /* __KERNEL__ */
    259 
    260 #endif /* __LINUX_BDE_H__ */