xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/zip/zip_inflate.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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_inflate.h"
51*4882a593Smuzhiyun 
prepare_inflate_zcmd(struct zip_operation * zip_ops,struct zip_state * s,union zip_inst_s * zip_cmd)52*4882a593Smuzhiyun static int prepare_inflate_zcmd(struct zip_operation *zip_ops,
53*4882a593Smuzhiyun 				struct zip_state *s, union zip_inst_s *zip_cmd)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	union zip_zres_s *result_ptr = &s->result;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	memset(zip_cmd, 0, sizeof(s->zip_cmd));
58*4882a593Smuzhiyun 	memset(result_ptr, 0, sizeof(s->result));
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* IWORD#0 */
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* Decompression History Gather list - no gather list */
63*4882a593Smuzhiyun 	zip_cmd->s.hg = 0;
64*4882a593Smuzhiyun 	/* For decompression, CE must be 0x0. */
65*4882a593Smuzhiyun 	zip_cmd->s.ce = 0;
66*4882a593Smuzhiyun 	/* For decompression, SS must be 0x0. */
67*4882a593Smuzhiyun 	zip_cmd->s.ss = 0;
68*4882a593Smuzhiyun 	/* For decompression, SF should always be set. */
69*4882a593Smuzhiyun 	zip_cmd->s.sf = 1;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* Begin File */
72*4882a593Smuzhiyun 	if (zip_ops->begin_file == 0)
73*4882a593Smuzhiyun 		zip_cmd->s.bf = 0;
74*4882a593Smuzhiyun 	else
75*4882a593Smuzhiyun 		zip_cmd->s.bf = 1;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	zip_cmd->s.ef = 1;
78*4882a593Smuzhiyun 	/* 0: for Deflate decompression, 3: for LZS decompression */
79*4882a593Smuzhiyun 	zip_cmd->s.cc = zip_ops->ccode;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* IWORD #1*/
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* adler checksum */
84*4882a593Smuzhiyun 	zip_cmd->s.adlercrc32 = zip_ops->csum;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/*
87*4882a593Smuzhiyun 	 * HISTORYLENGTH must be 0x0 for any ZIP decompress operation.
88*4882a593Smuzhiyun 	 * History data is added to a decompression operation via IWORD3.
89*4882a593Smuzhiyun 	 */
90*4882a593Smuzhiyun 	zip_cmd->s.historylength = 0;
91*4882a593Smuzhiyun 	zip_cmd->s.ds = 0;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* IWORD # 8 and 9 - Output pointer */
94*4882a593Smuzhiyun 	zip_cmd->s.out_ptr_addr.s.addr  = __pa(zip_ops->output);
95*4882a593Smuzhiyun 	zip_cmd->s.out_ptr_ctl.s.length = zip_ops->output_len;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* Maximum number of output-stream bytes that can be written */
98*4882a593Smuzhiyun 	zip_cmd->s.totaloutputlength    = zip_ops->output_len;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	zip_dbg("Data Direct Input case ");
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* IWORD # 6 and 7 - input pointer */
103*4882a593Smuzhiyun 	zip_cmd->s.dg = 0;
104*4882a593Smuzhiyun 	zip_cmd->s.inp_ptr_addr.s.addr  = __pa((u8 *)zip_ops->input);
105*4882a593Smuzhiyun 	zip_cmd->s.inp_ptr_ctl.s.length = zip_ops->input_len;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* IWORD # 10 and 11 - Result pointer */
108*4882a593Smuzhiyun 	zip_cmd->s.res_ptr_addr.s.addr = __pa(result_ptr);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* Clearing completion code */
111*4882a593Smuzhiyun 	result_ptr->s.compcode = 0;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Returning 0 for time being.*/
114*4882a593Smuzhiyun 	return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun  * zip_inflate - API to offload inflate operation to hardware
119*4882a593Smuzhiyun  * @zip_ops: Pointer to zip operation structure
120*4882a593Smuzhiyun  * @s:       Pointer to the structure representing zip state
121*4882a593Smuzhiyun  * @zip_dev: Pointer to zip device structure
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * This function prepares the zip inflate command and submits it to the zip
124*4882a593Smuzhiyun  * engine for processing.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * Return: 0 if successful or error code
127*4882a593Smuzhiyun  */
zip_inflate(struct zip_operation * zip_ops,struct zip_state * s,struct zip_device * zip_dev)128*4882a593Smuzhiyun int zip_inflate(struct zip_operation *zip_ops, struct zip_state *s,
129*4882a593Smuzhiyun 		struct zip_device *zip_dev)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	union zip_inst_s *zip_cmd    = &s->zip_cmd;
132*4882a593Smuzhiyun 	union zip_zres_s  *result_ptr = &s->result;
133*4882a593Smuzhiyun 	u32 queue;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* Prepare inflate zip command */
136*4882a593Smuzhiyun 	prepare_inflate_zcmd(zip_ops, s, zip_cmd);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	atomic64_add(zip_ops->input_len, &zip_dev->stats.decomp_in_bytes);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	/* Load inflate command to zip queue and ring the doorbell */
141*4882a593Smuzhiyun 	queue = zip_load_instr(zip_cmd, zip_dev);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	/* Decompression requests submitted stats update */
144*4882a593Smuzhiyun 	atomic64_inc(&zip_dev->stats.decomp_req_submit);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* Wait for completion or error */
147*4882a593Smuzhiyun 	zip_poll_result(result_ptr);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* Decompression requests completed stats update */
150*4882a593Smuzhiyun 	atomic64_inc(&zip_dev->stats.decomp_req_complete);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	zip_ops->compcode = result_ptr->s.compcode;
153*4882a593Smuzhiyun 	switch (zip_ops->compcode) {
154*4882a593Smuzhiyun 	case ZIP_CMD_NOTDONE:
155*4882a593Smuzhiyun 		zip_dbg("Zip Instruction not yet completed\n");
156*4882a593Smuzhiyun 		return ZIP_ERROR;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	case ZIP_CMD_SUCCESS:
159*4882a593Smuzhiyun 		zip_dbg("Zip Instruction completed successfully\n");
160*4882a593Smuzhiyun 		break;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	case ZIP_CMD_DYNAMIC_STOP:
163*4882a593Smuzhiyun 		zip_dbg(" Dynamic stop Initiated\n");
164*4882a593Smuzhiyun 		break;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	default:
167*4882a593Smuzhiyun 		zip_dbg("Instruction failed. Code = %d\n", zip_ops->compcode);
168*4882a593Smuzhiyun 		atomic64_inc(&zip_dev->stats.decomp_bad_reqs);
169*4882a593Smuzhiyun 		zip_update_cmd_bufs(zip_dev, queue);
170*4882a593Smuzhiyun 		return ZIP_ERROR;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	zip_update_cmd_bufs(zip_dev, queue);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if ((zip_ops->ccode == 3) && (zip_ops->flush == 4) &&
176*4882a593Smuzhiyun 	    (zip_ops->compcode != ZIP_CMD_DYNAMIC_STOP))
177*4882a593Smuzhiyun 		result_ptr->s.ef = 1;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	zip_ops->csum = result_ptr->s.adler32;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	atomic64_add(result_ptr->s.totalbyteswritten,
182*4882a593Smuzhiyun 		     &zip_dev->stats.decomp_out_bytes);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (zip_ops->output_len < result_ptr->s.totalbyteswritten) {
185*4882a593Smuzhiyun 		zip_err("output_len (%d) < total bytes written (%d)\n",
186*4882a593Smuzhiyun 			zip_ops->output_len, result_ptr->s.totalbyteswritten);
187*4882a593Smuzhiyun 		zip_ops->output_len = 0;
188*4882a593Smuzhiyun 	} else {
189*4882a593Smuzhiyun 		zip_ops->output_len = result_ptr->s.totalbyteswritten;
190*4882a593Smuzhiyun 	}
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	zip_ops->bytes_read = result_ptr->s.totalbytesread;
193*4882a593Smuzhiyun 	zip_ops->bits_processed = result_ptr->s.totalbitsprocessed;
194*4882a593Smuzhiyun 	zip_ops->end_file = result_ptr->s.ef;
195*4882a593Smuzhiyun 	if (zip_ops->end_file) {
196*4882a593Smuzhiyun 		switch (zip_ops->format) {
197*4882a593Smuzhiyun 		case RAW_FORMAT:
198*4882a593Smuzhiyun 			zip_dbg("RAW Format: %d ", zip_ops->format);
199*4882a593Smuzhiyun 			/* Get checksum from engine */
200*4882a593Smuzhiyun 			zip_ops->csum = result_ptr->s.adler32;
201*4882a593Smuzhiyun 			break;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		case ZLIB_FORMAT:
204*4882a593Smuzhiyun 			zip_dbg("ZLIB Format: %d ", zip_ops->format);
205*4882a593Smuzhiyun 			zip_ops->csum = result_ptr->s.adler32;
206*4882a593Smuzhiyun 			break;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 		case GZIP_FORMAT:
209*4882a593Smuzhiyun 			zip_dbg("GZIP Format: %d ", zip_ops->format);
210*4882a593Smuzhiyun 			zip_ops->csum = result_ptr->s.crc32;
211*4882a593Smuzhiyun 			break;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		case LZS_FORMAT:
214*4882a593Smuzhiyun 			zip_dbg("LZS Format: %d ", zip_ops->format);
215*4882a593Smuzhiyun 			break;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 		default:
218*4882a593Smuzhiyun 			zip_err("Format error:%d\n", zip_ops->format);
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	return 0;
223*4882a593Smuzhiyun }
224