1*4882a593Smuzhiyun /***********************license start************************************
2*4882a593Smuzhiyun * Copyright (c) 2003-2017 Cavium, Inc.
3*4882a593Smuzhiyun * All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file is provided under the terms of the Cavium License (see below)
8*4882a593Smuzhiyun * or under the terms of GNU General Public License, Version 2, as
9*4882a593Smuzhiyun * published by the Free Software Foundation. When using or redistributing
10*4882a593Smuzhiyun * this file, you may do so under either license.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Cavium License: Redistribution and use in source and binary forms, with
13*4882a593Smuzhiyun * or without modification, are permitted provided that the following
14*4882a593Smuzhiyun * conditions are met:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun * with the distribution.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * * Neither the name of Cavium Inc. nor the names of its contributors may be
25*4882a593Smuzhiyun * used to endorse or promote products derived from this software without
26*4882a593Smuzhiyun * specific prior written permission.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * This Software, including technical data, may be subject to U.S. export
29*4882a593Smuzhiyun * control laws, including the U.S. Export Administration Act and its
30*4882a593Smuzhiyun * associated regulations, and may be subject to export or import
31*4882a593Smuzhiyun * regulations in other countries.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34*4882a593Smuzhiyun * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35*4882a593Smuzhiyun * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36*4882a593Smuzhiyun * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37*4882a593Smuzhiyun * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38*4882a593Smuzhiyun * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39*4882a593Smuzhiyun * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40*4882a593Smuzhiyun * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41*4882a593Smuzhiyun * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42*4882a593Smuzhiyun * ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43*4882a593Smuzhiyun * WITH YOU.
44*4882a593Smuzhiyun ***********************license end**************************************/
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #include <linux/delay.h>
47*4882a593Smuzhiyun #include <linux/sched.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #include "common.h"
50*4882a593Smuzhiyun #include "zip_deflate.h"
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* Prepares the deflate zip command */
prepare_zip_command(struct zip_operation * zip_ops,struct zip_state * s,union zip_inst_s * zip_cmd)53*4882a593Smuzhiyun static int prepare_zip_command(struct zip_operation *zip_ops,
54*4882a593Smuzhiyun struct zip_state *s, union zip_inst_s *zip_cmd)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun union zip_zres_s *result_ptr = &s->result;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun memset(zip_cmd, 0, sizeof(s->zip_cmd));
59*4882a593Smuzhiyun memset(result_ptr, 0, sizeof(s->result));
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* IWORD #0 */
62*4882a593Smuzhiyun /* History gather */
63*4882a593Smuzhiyun zip_cmd->s.hg = 0;
64*4882a593Smuzhiyun /* compression enable = 1 for deflate */
65*4882a593Smuzhiyun zip_cmd->s.ce = 1;
66*4882a593Smuzhiyun /* sf (sync flush) */
67*4882a593Smuzhiyun zip_cmd->s.sf = 1;
68*4882a593Smuzhiyun /* ef (end of file) */
69*4882a593Smuzhiyun if (zip_ops->flush == ZIP_FLUSH_FINISH) {
70*4882a593Smuzhiyun zip_cmd->s.ef = 1;
71*4882a593Smuzhiyun zip_cmd->s.sf = 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun zip_cmd->s.cc = zip_ops->ccode;
75*4882a593Smuzhiyun /* ss (compression speed/storage) */
76*4882a593Smuzhiyun zip_cmd->s.ss = zip_ops->speed;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* IWORD #1 */
79*4882a593Smuzhiyun /* adler checksum */
80*4882a593Smuzhiyun zip_cmd->s.adlercrc32 = zip_ops->csum;
81*4882a593Smuzhiyun zip_cmd->s.historylength = zip_ops->history_len;
82*4882a593Smuzhiyun zip_cmd->s.dg = 0;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* IWORD # 6 and 7 - compression input/history pointer */
85*4882a593Smuzhiyun zip_cmd->s.inp_ptr_addr.s.addr = __pa(zip_ops->input);
86*4882a593Smuzhiyun zip_cmd->s.inp_ptr_ctl.s.length = (zip_ops->input_len +
87*4882a593Smuzhiyun zip_ops->history_len);
88*4882a593Smuzhiyun zip_cmd->s.ds = 0;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* IWORD # 8 and 9 - Output pointer */
91*4882a593Smuzhiyun zip_cmd->s.out_ptr_addr.s.addr = __pa(zip_ops->output);
92*4882a593Smuzhiyun zip_cmd->s.out_ptr_ctl.s.length = zip_ops->output_len;
93*4882a593Smuzhiyun /* maximum number of output-stream bytes that can be written */
94*4882a593Smuzhiyun zip_cmd->s.totaloutputlength = zip_ops->output_len;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* IWORD # 10 and 11 - Result pointer */
97*4882a593Smuzhiyun zip_cmd->s.res_ptr_addr.s.addr = __pa(result_ptr);
98*4882a593Smuzhiyun /* Clearing completion code */
99*4882a593Smuzhiyun result_ptr->s.compcode = 0;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * zip_deflate - API to offload deflate operation to hardware
106*4882a593Smuzhiyun * @zip_ops: Pointer to zip operation structure
107*4882a593Smuzhiyun * @s: Pointer to the structure representing zip state
108*4882a593Smuzhiyun * @zip_dev: Pointer to zip device structure
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * This function prepares the zip deflate command and submits it to the zip
111*4882a593Smuzhiyun * engine for processing.
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * Return: 0 if successful or error code
114*4882a593Smuzhiyun */
zip_deflate(struct zip_operation * zip_ops,struct zip_state * s,struct zip_device * zip_dev)115*4882a593Smuzhiyun int zip_deflate(struct zip_operation *zip_ops, struct zip_state *s,
116*4882a593Smuzhiyun struct zip_device *zip_dev)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun union zip_inst_s *zip_cmd = &s->zip_cmd;
119*4882a593Smuzhiyun union zip_zres_s *result_ptr = &s->result;
120*4882a593Smuzhiyun u32 queue;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Prepares zip command based on the input parameters */
123*4882a593Smuzhiyun prepare_zip_command(zip_ops, s, zip_cmd);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun atomic64_add(zip_ops->input_len, &zip_dev->stats.comp_in_bytes);
126*4882a593Smuzhiyun /* Loads zip command into command queues and rings door bell */
127*4882a593Smuzhiyun queue = zip_load_instr(zip_cmd, zip_dev);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* Stats update for compression requests submitted */
130*4882a593Smuzhiyun atomic64_inc(&zip_dev->stats.comp_req_submit);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* Wait for completion or error */
133*4882a593Smuzhiyun zip_poll_result(result_ptr);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* Stats update for compression requests completed */
136*4882a593Smuzhiyun atomic64_inc(&zip_dev->stats.comp_req_complete);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun zip_ops->compcode = result_ptr->s.compcode;
139*4882a593Smuzhiyun switch (zip_ops->compcode) {
140*4882a593Smuzhiyun case ZIP_CMD_NOTDONE:
141*4882a593Smuzhiyun zip_dbg("Zip instruction not yet completed");
142*4882a593Smuzhiyun return ZIP_ERROR;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun case ZIP_CMD_SUCCESS:
145*4882a593Smuzhiyun zip_dbg("Zip instruction completed successfully");
146*4882a593Smuzhiyun zip_update_cmd_bufs(zip_dev, queue);
147*4882a593Smuzhiyun break;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun case ZIP_CMD_DTRUNC:
150*4882a593Smuzhiyun zip_dbg("Output Truncate error");
151*4882a593Smuzhiyun /* Returning ZIP_ERROR to avoid copy to user */
152*4882a593Smuzhiyun return ZIP_ERROR;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun default:
155*4882a593Smuzhiyun zip_err("Zip instruction failed. Code:%d", zip_ops->compcode);
156*4882a593Smuzhiyun return ZIP_ERROR;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Update the CRC depending on the format */
160*4882a593Smuzhiyun switch (zip_ops->format) {
161*4882a593Smuzhiyun case RAW_FORMAT:
162*4882a593Smuzhiyun zip_dbg("RAW Format: %d ", zip_ops->format);
163*4882a593Smuzhiyun /* Get checksum from engine, need to feed it again */
164*4882a593Smuzhiyun zip_ops->csum = result_ptr->s.adler32;
165*4882a593Smuzhiyun break;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun case ZLIB_FORMAT:
168*4882a593Smuzhiyun zip_dbg("ZLIB Format: %d ", zip_ops->format);
169*4882a593Smuzhiyun zip_ops->csum = result_ptr->s.adler32;
170*4882a593Smuzhiyun break;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun case GZIP_FORMAT:
173*4882a593Smuzhiyun zip_dbg("GZIP Format: %d ", zip_ops->format);
174*4882a593Smuzhiyun zip_ops->csum = result_ptr->s.crc32;
175*4882a593Smuzhiyun break;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun case LZS_FORMAT:
178*4882a593Smuzhiyun zip_dbg("LZS Format: %d ", zip_ops->format);
179*4882a593Smuzhiyun break;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun default:
182*4882a593Smuzhiyun zip_err("Unknown Format:%d\n", zip_ops->format);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun atomic64_add(result_ptr->s.totalbyteswritten,
186*4882a593Smuzhiyun &zip_dev->stats.comp_out_bytes);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Update output_len */
189*4882a593Smuzhiyun if (zip_ops->output_len < result_ptr->s.totalbyteswritten) {
190*4882a593Smuzhiyun /* Dynamic stop && strm->output_len < zipconstants[onfsize] */
191*4882a593Smuzhiyun zip_err("output_len (%d) < total bytes written(%d)\n",
192*4882a593Smuzhiyun zip_ops->output_len, result_ptr->s.totalbyteswritten);
193*4882a593Smuzhiyun zip_ops->output_len = 0;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun } else {
196*4882a593Smuzhiyun zip_ops->output_len = result_ptr->s.totalbyteswritten;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun return 0;
200*4882a593Smuzhiyun }
201