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