| 1 |
/*********************************************************************** |
| 2 |
* The langford header. This includes the memory location and offset |
| 3 |
* for various items in the PCIe BAR device. |
| 4 |
* |
| 5 |
* (c) 2013 Per Vices Corporation |
| 6 |
* |
| 7 |
* This code is licensed under the GPLv3 |
| 8 |
************************************************************************/ |
| 9 |
|
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/pci.h> |
| 13 |
#include <linux/pci_regs.h> |
| 14 |
#include <linux/io.h> |
| 15 |
#include <linux/cdev.h> |
| 16 |
#include <linux/types.h> |
| 17 |
#include <linux/fs.h> |
| 18 |
#include <linux/kthread.h> |
| 19 |
#include <linux/version.h> |
| 20 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) |
| 21 |
#include <linux/vmalloc.h> |
| 22 |
#endif |
| 23 |
#include <linux/uaccess.h> |
| 24 |
#include "langford_ioctl.h" |
| 25 |
|
| 26 |
|
| 27 |
/** Data structure to hold data specific to each instance of the device on the system |
| 28 |
|
| 29 |
This data structure can be "attached" to a device using dev_set_drvdata: |
| 30 |
\code |
| 31 |
DevPrivDataType *DevPrivData; |
| 32 |
DevPrivData = kmalloc(sizeof(DevPrivDataType), GFP_KERNEL); |
| 33 |
dev_set_drvdata(&dev->dev, DevPrivData); |
| 34 |
\endcode |
| 35 |
|
| 36 |
To access the contents of this data structure after it has been allocated, you can use dev_get_drvdata: |
| 37 |
\code |
| 38 |
DevPrivDataType *DevPrivData = dev_get_drvdata(&dev->dev); |
| 39 |
\endcode |
| 40 |
|
| 41 |
This data structure should only be allocated once per device. It should be destroyed when the device is unloaded. |
| 42 |
*/ |
| 43 |
typedef struct { |
| 44 |
struct pci_dev *dev; /**< PCI device which this data structure belongs to*/ |
| 45 |
unsigned int DevVer; /**< Version of the device as seen from the PCI configuartion registers*/ |
| 46 |
void __iomem *pBar0; /**< Pointer to BAR0 accessible in program space*/ |
| 47 |
void __iomem *pBar1; /**< Pointer to BAR1 accessible in program space*/ |
| 48 |
int OpenCtr; /**< Number of open handles (need to close device as many items as it is opened)*/ |
| 49 |
dma_addr_t BusTranslationMask; /**< Mask used to remove LSBs from memory address prior to being writen to translation table*/ |
| 50 |
uint8_t **pRxDmaBuffs; /**< Rx DMA buffers*/ |
| 51 |
uint8_t **pTxDmaBuffs; /**< Rx DMA buffers*/ |
| 52 |
dma_addr_t *RxDmaBuffsBusAddr; /**< Addresses of pRxDmaBuffs as seen by the PCI bus*/ |
| 53 |
dma_addr_t *TxDmaBuffsBusAddr; /**< Addresses of pTxDmaBuffs as seen by the PCI bus*/ |
| 54 |
} DevPrivDataType; |
| 55 |
|
| 56 |
/*Delete this define and the block of code it contains in production*/ |
| 57 |
#define DELETEME 0 |
| 58 |
|
| 59 |
/*Program constants*/ |
| 60 |
#define DRIVER_NAME "langford" /**< Driver name as how it shows up in the kernel logs*/ |
| 61 |
#define VERBOSE_LOGGING_R 0 /**< Extra verbose logging in rx DMA thread*/ |
| 62 |
#define VERBOSE_LOGGING_W 0 /**< Extra verbose logging in tx DMA thread*/ |
| 63 |
#define EMPTY_LOGGING 0 /**< Print messages if read or write buffers are empty*/ |
| 64 |
#define CDEVBUFFSIZE 200000000 /**< Size of buffers for the character device read and write buffers*/ |
| 65 |
|
| 66 |
/*Data required for DMA*/ |
| 67 |
#define DMAPAGEWIDTH 20 /**< Size of address page (i.e. number of pass through bits)*/ |
| 68 |
#define DMARXBUFFS 32 /**< Number of buffers for Rx (device to memory). Combined with DMATXBUFFS must not exceed number of address pages.*/ |
| 69 |
#define DMARXDESCS 128 /**< Maximum number of descriptors for rx that the device can handle*/ |
| 70 |
#define DMARXDESCDELAY 4 /**< Number of rx descriptors to complete before a read will take place*/ |
| 71 |
#define DMATXBUFFS 32 /**< Number of buffers for Tx (memory to device). Combined with DMARXBUFFS must not exceed number of address pages.*/ |
| 72 |
#define DMATXDESCS 128 /**< Maximum number of descriptors for tx that the device can handle*/ |
| 73 |
#define DMATXDESCDELAY 4 /**< Number of rx descriptors to complete before a read will take place*/ |
| 74 |
#define DMABUFFSIZE 262144 /**< Size of each Rx or Tx buffer*/ |
| 75 |
|
| 76 |
/*Offsets for BAR0*/ |
| 77 |
#define CRA 0x00000000 /**< Base address of CRA port of the HIP*/ |
| 78 |
#define CRA_ATT 0x00001000 /**< Location of address translation table for Altera HIP (on CRA)*/ |
| 79 |
#define PIO0 0x00008000 /**< Base address of PIO0 relative to BAR0*/ |
| 80 |
#define PIO1 0x00008100 /**< Base address of PIO1 relative to BAR0*/ |
| 81 |
#define PIO2 0x00008200 /**< Base address of PIO2 relative to BAR0*/ |
| 82 |
#define PIO3 0x00008300 /**< Base address of PIO3 relative to BAR0*/ |
| 83 |
#define PIO4 0x00008400 /**< Base address of PIO4 relative to BAR0*/ |
| 84 |
#define PIO5 0x00008500 /**< Base address of PIO5 relative to BAR0*/ |
| 85 |
|
| 86 |
/*Offsets for BAR1*/ |
| 87 |
#define WRCSR 0x00000000 /**< Write DMA controller (FPGA to memory, radio rx) command and status registers*/ |
| 88 |
#define WRCSRSTAT 0x00000000 /**< Write DMA controller CSR - status register*/ |
| 89 |
#define WRCSRCTRL 0x00000004 /**< Write DMA controller CSR - control register*/ |
| 90 |
#define WRCSRFILLLEVEL 0x00000008 /**< Write DMA controller CSR - fill level of write descriptor buffer */ |
| 91 |
#define WRDESC 0x00001000 /**< Write DMA controller (FPGA to memory, radio rx) descriptor registers*/ |
| 92 |
#define WRDESCWRADDR 0x00001004 /**< Write DMA controller - Descriptor write address*/ |
| 93 |
#define WRDESCLEN 0x00001008 /**< Write DMA controller - Descriptor length*/ |
| 94 |
#define WRDESCCTRL 0x0000100C /**< Write DMA controller - Descriptor control*/ |
| 95 |
#define RDCSR 0x00002000 /**< Read DMA controller (memory to FPGA, radio tx) command and status registers*/ |
| 96 |
#define RDCSRSTAT 0x00002000 /**< Read DMA controller - status register*/ |
| 97 |
#define RDCSRCTRL 0x00002004 /**< Read DMA controller - control register*/ |
| 98 |
#define RDCSRFILLLEVEL 0x00002008 /**< Read DMA controller - fill level of read descriptor buffer */ |
| 99 |
#define RDDESC 0x00003000 /**< Read DMA controller (memory to FPGA, radio tx) descriptor registers*/ |
| 100 |
#define RDDESCRDADDR 0x00003000 /**< Read DMA controller - Descriptor read address*/ |
| 101 |
#define RDDESCLEN 0x00003008 /**< Read DMA controller - Descriptor length*/ |
| 102 |
#define RDDESCCTRL 0x0000300C /**< Read DMA controller - Descriptor control*/ |
| 103 |
|
| 104 |
/*Bits for WRCSR and RDCSR*/ |
| 105 |
#define CSR_STAT_RESETTING (1 << 6) /**< Status bit indicating that reset is still underway*/ |
| 106 |
#define CSR_CTRL_RESET (1 << 1) /**< Reset bit for the CSR control register*/ |
| 107 |
|
| 108 |
/*Bits for WRDESC and RDDESC*/ |
| 109 |
#define DESC_CTRL_GO (1 << 31) /**< Commit descriptor to buffer*/ |
| 110 |
|
| 111 |
|
| 112 |
/*Program macros*/ |
| 113 |
|
| 114 |
/**Given 2 numbers, return the larger one*/ |
| 115 |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 116 |
|
| 117 |
/**Given 2 numbers, return the smaller one*/ |
| 118 |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) |