forked from ecdufcdrvr/bcmufctdrvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocs_impl.h
186 lines (159 loc) · 5.41 KB
/
ocs_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* BSD LICENSE
*
* Copyright (c) 2018 Broadcom. All Rights Reserved.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ocs_IMPL_H__
#define __ocs_IMPL_H__
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <rte_config.h>
#include <rte_malloc.h>
#include <rte_atomic.h>
#include <rte_cycles.h>
#include <rte_version.h>
#include <rte_bus_pci.h>
#include <stdbool.h>
#include "spdk/env.h"
#include "ocs_pci.h"
#include "../../lib/env_dpdk/env_internal.h"
/**
* \file
*
* This file describes the functions required to integrate
* the userspace OCS driver for a specific implementation. This
* implementation is specific for DPDK. Users would revise it as
* necessary for their own particular environment if not using it
* within the SPDK framework.
*/
/**
* Allocate a pinned, physically contiguous memory buffer with the
* given size and alignment.
*/
static inline void *
ocs_spdk_zmalloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr)
{
void *buf_ptr = NULL;
if (tag) {
buf_ptr = spdk_memzone_reserve_aligned(tag, size, SPDK_ENV_SOCKET_ID_ANY, 0, align);
if (buf_ptr) {
*phys_addr = spdk_vtophys(buf_ptr, &size);
}
}
else {
ocs_log_err(NULL, "ocs_zmalloc() call without a tag!\n");
}
return buf_ptr;
}
/**
* Free a memory buffer previously allocated with ocsu_zmalloc.
*/
#define ocs_spdk_free(mz_name) spdk_memzone_free(mz_name)
/**
* Return the physical address for the specified virtual address.
*/
#define ocs_vtophys(buf) spdk_vtophys(buf, NULL)
/**
* Delay us.
*/
#define ocs_delay_us(us) rte_delay_us(us)
/**
* Assert a condition and panic/abort as desired. Failures of these
* assertions indicate catastrophic failures within the driver.
*/
#define ocs_spdk_assert(check) assert(check)
/**
* Log or print a message from the driver.
*/
#define ocs_spdk_printf(chan, fmt, args...) printf(fmt, ##args)
/**
*
*/
#define ocs_pcicfg_read32(handle, var, offset) spdk_pci_device_cfg_read32(handle, var, offset)
#define ocs_pcicfg_write32(handle, var, offset) spdk_pci_device_cfg_write32(handle, var, offset)
#define SPDK_OCS_PCI_DEVICE(DEVICE_ID) RTE_PCI_DEVICE(SPDK_PCI_VID_OCS, DEVICE_ID)
static struct rte_pci_id ocs_driver_id[] = {
{SPDK_OCS_PCI_DEVICE(PCI_DEVICE_ID_OCS_LANCERG5)},
{SPDK_OCS_PCI_DEVICE(PCI_DEVICE_ID_OCS_LANCERG6)},
{ .vendor_id = 0, /* sentinel */ },
};
static struct spdk_pci_driver ocs_rte_driver = {
.driver = {
.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
.id_table = ocs_driver_id,
.probe = spdk_pci_device_init,
.driver.name = "ocs_driver",
},
.cb_fn = NULL,
.cb_arg = NULL,
.is_registered = false,
};
struct ocs_pci_enum_ctx {
int (*user_enum_cb)(void *enum_ctx, struct spdk_pci_device *pci_dev);
void *user_enum_ctx;
};
static inline bool
ocs_pci_device_match_id(uint16_t vendor_id, uint16_t device_id)
{
if (vendor_id != SPDK_PCI_VID_OCS) {
return false;
}
switch (device_id) {
case PCI_DEVICE_ID_OCS_LANCERG5:
case PCI_DEVICE_ID_OCS_LANCERG6:
return true;
}
return false;
}
static int
ocs_pci_enum_cb(void *enum_ctx, struct spdk_pci_device *pci_dev)
{
struct ocs_pci_enum_ctx *ctx = enum_ctx;
uint16_t vendor_id = spdk_pci_device_get_vendor_id(pci_dev);
uint16_t device_id = spdk_pci_device_get_device_id(pci_dev);
if (!ocs_pci_device_match_id(vendor_id, device_id)) {
return 0;
}
return ctx->user_enum_cb(ctx->user_enum_ctx, pci_dev);
}
static inline int
ocs_pci_enumerate(int (*enum_cb)(void *enum_ctx, struct spdk_pci_device *pci_dev), void *enum_ctx)
{
struct ocs_pci_enum_ctx ocs_enum_ctx;
ocs_enum_ctx.user_enum_cb = enum_cb;
ocs_enum_ctx.user_enum_ctx = enum_ctx;
return spdk_pci_enumerate(&ocs_rte_driver, ocs_pci_enum_cb, &ocs_enum_ctx);
}
typedef pthread_mutex_t ocs_mutex_t;
#define ocs_mutex_lock pthread_mutex_lock
#define ocs_mutex_unlock pthread_mutex_unlock
#define OCS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#endif /* __OCS_IMPL_H__ */