1/***********************license start************************************
2 * Copyright (c) 2003-2017 Cavium, Inc.
3 * All rights reserved.
4 *
5 * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6 *
7 * This file is provided under the terms of the Cavium License (see below)
8 * or under the terms of GNU General Public License, Version 2, as
9 * published by the Free Software Foundation. When using or redistributing
10 * this file, you may do so under either license.
11 *
12 * Cavium License:  Redistribution and use in source and binary forms, with
13 * or without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 *  * Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 *
19 *  * Redistributions in binary form must reproduce the above
20 *    copyright notice, this list of conditions and the following
21 *    disclaimer in the documentation and/or other materials provided
22 *    with the distribution.
23 *
24 *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25 *    used to endorse or promote products derived from this software without
26 *    specific prior written permission.
27 *
28 * This Software, including technical data, may be subject to U.S. export
29 * control laws, including the U.S. Export Administration Act and its
30 * associated regulations, and may be subject to export or import
31 * regulations in other countries.
32 *
33 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39 * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40 * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41 * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42 * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43 * WITH YOU.
44 ***********************license end**************************************/
45
46#ifndef __COMMON_H__
47#define __COMMON_H__
48
49#include <linux/delay.h>
50#include <linux/init.h>
51#include <linux/interrupt.h>
52#include <linux/io.h>
53#include <linux/kernel.h>
54#include <linux/module.h>
55#include <linux/pci.h>
56#include <linux/seq_file.h>
57#include <linux/string.h>
58#include <linux/types.h>
59#include <linux/version.h>
60
61/* Device specific zlib function definitions */
62#include "zip_device.h"
63
64/* ZIP device definitions */
65#include "zip_main.h"
66
67/* ZIP memory allocation/deallocation related definitions */
68#include "zip_mem.h"
69
70/* Device specific structure definitions */
71#include "zip_regs.h"
72
73#define ZIP_ERROR    -1
74
75#define ZIP_FLUSH_FINISH  4
76
77#define RAW_FORMAT		0  /* for rawpipe */
78#define ZLIB_FORMAT		1  /* for zpipe */
79#define GZIP_FORMAT		2  /* for gzpipe */
80#define LZS_FORMAT		3  /* for lzspipe */
81
82/* Max number of ZIP devices supported */
83#define MAX_ZIP_DEVICES		2
84
85/* Configures the number of zip queues to be used */
86#define ZIP_NUM_QUEUES		2
87
88#define DYNAMIC_STOP_EXCESS	1024
89
90/* Maximum buffer sizes in direct mode */
91#define MAX_INPUT_BUFFER_SIZE   (64 * 1024)
92#define MAX_OUTPUT_BUFFER_SIZE  (64 * 1024)
93
94/**
95 * struct zip_operation - common data structure for comp and decomp operations
96 * @input:               Next input byte is read from here
97 * @output:              Next output byte written here
98 * @ctx_addr:            Inflate context buffer address
99 * @history:             Pointer to the history buffer
100 * @input_len:           Number of bytes available at next_in
101 * @input_total_len:     Total number of input bytes read
102 * @output_len:          Remaining free space at next_out
103 * @output_total_len:    Total number of bytes output so far
104 * @csum:                Checksum value of the uncompressed data
105 * @flush:               Flush flag
106 * @format:              Format (depends on stream's wrap)
107 * @speed:               Speed depends on stream's level
108 * @ccode:               Compression code ( stream's strategy)
109 * @lzs_flag:            Flag for LZS support
110 * @begin_file:          Beginning of file indication for inflate
111 * @history_len:         Size of the history data
112 * @end_file:            Ending of the file indication for inflate
113 * @compcode:            Completion status of the ZIP invocation
114 * @bytes_read:          Input bytes read in current instruction
115 * @bits_processed:      Total bits processed for entire file
116 * @sizeofptr:           To distinguish between ILP32 and LP64
117 * @sizeofzops:          Optional just for padding
118 *
119 * This structure is used to maintain the required meta data for the
120 * comp and decomp operations.
121 */
122struct zip_operation {
123	u8    *input;
124	u8    *output;
125	u64   ctx_addr;
126	u64   history;
127
128	u32   input_len;
129	u32   input_total_len;
130
131	u32   output_len;
132	u32   output_total_len;
133
134	u32   csum;
135	u32   flush;
136
137	u32   format;
138	u32   speed;
139	u32   ccode;
140	u32   lzs_flag;
141
142	u32   begin_file;
143	u32   history_len;
144
145	u32   end_file;
146	u32   compcode;
147	u32   bytes_read;
148	u32   bits_processed;
149
150	u32   sizeofptr;
151	u32   sizeofzops;
152};
153
154static inline int zip_poll_result(union zip_zres_s *result)
155{
156	int retries = 1000;
157
158	while (!result->s.compcode) {
159		if (!--retries) {
160			pr_err("ZIP ERR: request timed out");
161			return -ETIMEDOUT;
162		}
163		udelay(10);
164		/*
165		 * Force re-reading of compcode which is updated
166		 * by the ZIP coprocessor.
167		 */
168		rmb();
169	}
170	return 0;
171}
172
173/* error messages */
174#define zip_err(fmt, args...) pr_err("ZIP ERR:%s():%d: " \
175			      fmt "\n", __func__, __LINE__, ## args)
176
177#ifdef MSG_ENABLE
178/* Enable all messages */
179#define zip_msg(fmt, args...) pr_info("ZIP_MSG:" fmt "\n", ## args)
180#else
181#define zip_msg(fmt, args...)
182#endif
183
184#if defined(ZIP_DEBUG_ENABLE) && defined(MSG_ENABLE)
185
186#ifdef DEBUG_LEVEL
187
188#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
189	strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
190
191#if DEBUG_LEVEL >= 4
192
193#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
194			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
195
196#elif DEBUG_LEVEL >= 3
197
198#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s: %s() : %d: " \
199			      fmt "\n", FILE_NAME, __func__, __LINE__, ## args)
200
201#elif DEBUG_LEVEL >= 2
202
203#define zip_dbg(fmt, args...) pr_info("ZIP DBG: %s() : %d: " \
204			      fmt "\n", __func__, __LINE__, ## args)
205
206#else
207
208#define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
209
210#endif /* DEBUG LEVEL >=4 */
211
212#else
213
214#define zip_dbg(fmt, args...) pr_info("ZIP DBG:" fmt "\n", ## args)
215
216#endif /* DEBUG_LEVEL */
217#else
218
219#define zip_dbg(fmt, args...)
220
221#endif /* ZIP_DEBUG_ENABLE && MSG_ENABLE*/
222
223#endif
224