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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* Copyright 2015, International Business Machines
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __WRAPPER_H__
#define __WRAPPER_H__
/*
* Switching between software and hardware implementation of zlib. The
* hardware implementation is not implementing the full set of
* interfaces but enough to do commonly used functionality for
* compression and decompression.
*
* The hardware implementation is using the h_ prefix, the software
* implementation is using a z_ prefix.
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <pthread.h>
#include <zaddons.h>
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
#endif
#ifndef MIN
# define MIN(a,b) ({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#endif
#ifndef MAX
# define MAX(a,b) ({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif
#ifndef __unused
# define __unused __attribute__((unused))
#endif
extern FILE *zlib_log;
extern int zlib_trace;
extern int zlib_accelerator;
extern int zlib_card;
extern unsigned int zlib_inflate_impl;
extern unsigned int zlib_deflate_impl;
extern unsigned int zlib_inflate_flags;
extern unsigned int zlib_deflate_flags;
#define zlib_trace_enabled() (zlib_trace & 0x1)
#define zlib_hw_trace_enabled() (zlib_trace & 0x2)
#define zlib_sw_trace_enabled() (zlib_trace & 0x4)
#define zlib_gather_statistics() (zlib_trace & 0x8)
/* Use in case of an error */
#define pr_err(fmt, ...) do { \
fprintf(zlib_log, "%s:%u: Error: " fmt, \
__FILE__, __LINE__, ## __VA_ARGS__); \
} while (0)
/* Use in case of an warning */
#define pr_warn(fmt, ...) do { \
fprintf(zlib_log, "%s:%u: Warning: " fmt, \
__FILE__, __LINE__, ## __VA_ARGS__); \
} while (0)
/* Informational printouts */
#define pr_info(fmt, ...) do { \
fprintf(zlib_log, "Info: " fmt, ## __VA_ARGS__); \
} while (0)
/* Trace zlib wrapper code */
#define pr_trace(fmt, ...) do { \
if (zlib_trace_enabled()) \
fprintf(zlib_log, "### " fmt, ## __VA_ARGS__); \
} while (0)
/* Trace zlib hardware implementation */
#define hw_trace(fmt, ...) do { \
if (zlib_hw_trace_enabled()) \
fprintf(zlib_log, "hhh " fmt, ## __VA_ARGS__); \
} while (0)
/* Trace zlib software implementation */
#define sw_trace(fmt, ...) do { \
if (zlib_sw_trace_enabled()) \
fprintf(zlib_log, "sss " fmt, ## __VA_ARGS__); \
} while (0)
#define Z_UNSUPPORTED (-7)
#define ZLIB_SIZE_SLOTS 256 /* Each slot represents 4KiB, the last
slot is represending everything
which larger or equal 1024KiB */
struct zlib_stats {
unsigned long deflateInit;
unsigned long deflate[ZLIB_MAX_IMPL];
unsigned long deflate_avail_in[ZLIB_SIZE_SLOTS];
unsigned long deflate_avail_out[ZLIB_SIZE_SLOTS];
unsigned long deflateReset;
unsigned long deflate_total_in[ZLIB_SIZE_SLOTS];
unsigned long deflate_total_out[ZLIB_SIZE_SLOTS];
unsigned long deflateSetDictionary;
unsigned long deflateSetHeader;
unsigned long deflateParams;
unsigned long deflateBound;
unsigned long deflatePrime;
unsigned long deflateCopy;
unsigned long deflateEnd;
unsigned long inflateInit;
unsigned long inflate[ZLIB_MAX_IMPL];
unsigned long inflate_avail_in[ZLIB_SIZE_SLOTS];
unsigned long inflate_avail_out[ZLIB_SIZE_SLOTS];
unsigned long inflateReset;
unsigned long inflateReset2;
unsigned long inflate_total_in[ZLIB_SIZE_SLOTS];
unsigned long inflate_total_out[ZLIB_SIZE_SLOTS];
unsigned long inflateSetDictionary;
unsigned long inflateGetDictionary;
unsigned long inflateGetHeader;
unsigned long inflateSync;
unsigned long inflatePrime;
unsigned long inflateCopy;
unsigned long inflateEnd;
unsigned long adler32;
unsigned long adler32_combine;
unsigned long crc32;
unsigned long crc32_combine;
unsigned long gzopen64;
unsigned long gzopen;
unsigned long gzdopen;
unsigned long gzbuffer;
unsigned long gztell64;
unsigned long gztell;
unsigned long gzseek64;
unsigned long gzseek;
unsigned long gzwrite;
unsigned long gzread;
unsigned long gzclose;
unsigned long gzoffset64;
unsigned long gzoffset;
unsigned long gzrewind;
unsigned long gzputs;
unsigned long gzgets;
unsigned long gzputc;
unsigned long gzgetc;
unsigned long gzungetc;
unsigned long gzprintf;
unsigned long gzerror;
unsigned long gzeof;
unsigned long gzflush;
unsigned long compress;
unsigned long compress2;
unsigned long compressBound;
unsigned long uncompress;
unsigned long adler32_combine64;
unsigned long crc32_combine64;
unsigned long get_crc_table;
};
extern pthread_mutex_t zlib_stats_mutex; /* mutex to protect zlib_stats */
extern struct zlib_stats zlib_stats;
static inline void zlib_stats_inc(unsigned long *count)
{
if (!zlib_gather_statistics())
return;
pthread_mutex_lock(&zlib_stats_mutex);
*count = *count + 1;
pthread_mutex_unlock(&zlib_stats_mutex);
}
/* Hardware implementation */
int h_deflateInit2_(z_streamp strm, int level, int method,
int windowBits, int memLevel,
int strategy, const char *version,
int stream_size);
int h_deflateParams(z_streamp strm, int level, int strategy);
uLong h_deflateBound(z_streamp strm, uLong sourceLen);
int h_deflateReset(z_streamp strm);
int h_deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
uInt dictLength);
int h_deflateSetHeader(z_streamp strm, gz_headerp head);
int h_deflate(z_streamp strm, int flush);
int h_deflateEnd(z_streamp strm);
int h_inflateInit2_(z_streamp strm, int windowBits, const char *version,
int stream_size);
int h_inflateReset(z_streamp strm);
int h_inflateReset2(z_streamp strm, int windowBits);
int h_inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
uInt dictLength);
/* inflateGetDictionary is only available for 1.2.8 or later */
bool z_hasGetDictionary(void);
int h_inflateGetDictionary(z_streamp strm, Bytef *dictionary,
uInt *dictLength);
int h_inflateGetHeader(z_streamp strm, gz_headerp head);
int h_deflateCopy(z_streamp dest, z_streamp source);
int h_inflate(z_streamp strm, int flush);
int h_inflateEnd(z_streamp strm);
/* Software implementation */
int z_deflateInit2_(z_streamp strm, int level, int method,
int windowBits, int memLevel, int strategy,
const char *version, int stream_size);
int z_deflateParams(z_streamp strm, int level, int strategy);
uLong z_deflateBound(z_streamp strm, uLong sourceLen);
int z_deflateReset(z_streamp strm);
int z_deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
uInt dictLength);
int z_deflateSetHeader(z_streamp strm, gz_headerp head);
int z_deflatePrime(z_streamp strm, int bits, int value);
int z_deflateCopy(z_streamp dest, z_streamp source);
int z_deflate(z_streamp strm, int flush);
int z_deflateEnd(z_streamp strm);
int z_inflateInit2_(z_streamp strm, int windowBits, const char *version,
int stream_size);
int z_inflateReset(z_streamp strm);
int z_inflateReset2(z_streamp strm, int windowBits);
int z_inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
uInt dictLength);
int z_inflateGetDictionary(z_streamp strm, const Bytef *dictionary,
uInt *dictLength);
int z_inflateGetHeader(z_streamp strm, gz_headerp head);
int z_inflatePrime(z_streamp strm, int bits, int value);
int z_inflateSync(z_streamp strm);
int z_inflate(z_streamp strm, int flush);
int z_inflateEnd(z_streamp strm);
int z_inflateBackInit_(z_streamp strm, int windowBits, unsigned char *window,
const char *version, int stream_size);
int z_inflateBack(z_streamp strm, in_func in, void *in_desc, out_func out,
void *out_desc);
int z_inflateBackEnd(z_streamp strm);
uLong z_adler32(uLong adler, const Bytef *buf, uInt len);
uLong z_adler32_combine(uLong adler1, uLong adler2, z_off_t len2);
uLong z_crc32(uLong crc, const Bytef *buf, uInt len);
uLong z_crc32_combine(uLong crc1, uLong crc2, z_off_t len2);
const char *z_zError(int err);
uLong z_compressBound(uLong sourceLen);
/* PCIe trigger function. Writes to register 0x0 which normally non-sense. */
void error_trigger(void);
/* Constructors/destructors */
void zedc_hw_init(void);
void zedc_hw_done(void);
void zedc_sw_init(void);
void zedc_sw_done(void);
/* Circumvention for missing prototypes */
const char *z_zlibVersion(void);
uLong z_zlibCompileFlags(void);
/* Misc helper functions */
uint64_t str_to_num(char *str);
const char *ret_to_str(int ret);
const char *flush_to_str(int flush);
#endif /* __WRAPPER_H__ */
|