1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy * CDDL HEADER START
4eda14cbcSMatt Macy *
5eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy *
9eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy * See the License for the specific language governing permissions
12eda14cbcSMatt Macy * and limitations under the License.
13eda14cbcSMatt Macy *
14eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy *
20eda14cbcSMatt Macy * CDDL HEADER END
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25eda14cbcSMatt Macy * Use is subject to license terms.
26eda14cbcSMatt Macy */
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy #include <sys/debug.h>
31eda14cbcSMatt Macy #include <sys/types.h>
32eda14cbcSMatt Macy #include <sys/qat.h>
33eda14cbcSMatt Macy #include <sys/zio_compress.h>
34eda14cbcSMatt Macy
35eda14cbcSMatt Macy #ifdef _KERNEL
36eda14cbcSMatt Macy
37eda14cbcSMatt Macy #include <sys/zmod.h>
38eda14cbcSMatt Macy typedef size_t zlen_t;
39eda14cbcSMatt Macy #define compress_func z_compress_level
40eda14cbcSMatt Macy #define uncompress_func z_uncompress
41eda14cbcSMatt Macy
42eda14cbcSMatt Macy #else /* _KERNEL */
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy #include <zlib.h>
45eda14cbcSMatt Macy typedef uLongf zlen_t;
46eda14cbcSMatt Macy #define compress_func compress2
47eda14cbcSMatt Macy #define uncompress_func uncompress
48eda14cbcSMatt Macy
49eda14cbcSMatt Macy #endif
50eda14cbcSMatt Macy
51e2df9bb4SMartin Matuska static size_t
zfs_gzip_compress_buf(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)52e2df9bb4SMartin Matuska zfs_gzip_compress_buf(void *s_start, void *d_start, size_t s_len,
53e2df9bb4SMartin Matuska size_t d_len, int n)
54eda14cbcSMatt Macy {
55eda14cbcSMatt Macy int ret;
56eda14cbcSMatt Macy zlen_t dstlen = d_len;
57eda14cbcSMatt Macy
58eda14cbcSMatt Macy ASSERT(d_len <= s_len);
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy /* check if hardware accelerator can be used */
61eda14cbcSMatt Macy if (qat_dc_use_accel(s_len)) {
62eda14cbcSMatt Macy ret = qat_compress(QAT_COMPRESS, s_start, s_len, d_start,
63eda14cbcSMatt Macy d_len, &dstlen);
64eda14cbcSMatt Macy if (ret == CPA_STATUS_SUCCESS) {
65eda14cbcSMatt Macy return ((size_t)dstlen);
66eda14cbcSMatt Macy } else if (ret == CPA_STATUS_INCOMPRESSIBLE) {
67eda14cbcSMatt Macy if (d_len != s_len)
68eda14cbcSMatt Macy return (s_len);
69eda14cbcSMatt Macy
70da5137abSMartin Matuska memcpy(d_start, s_start, s_len);
71eda14cbcSMatt Macy return (s_len);
72eda14cbcSMatt Macy }
73eda14cbcSMatt Macy /* if hardware compression fails, do it again with software */
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy
76eda14cbcSMatt Macy if (compress_func(d_start, &dstlen, s_start, s_len, n) != Z_OK) {
77eda14cbcSMatt Macy if (d_len != s_len)
78eda14cbcSMatt Macy return (s_len);
79eda14cbcSMatt Macy
80da5137abSMartin Matuska memcpy(d_start, s_start, s_len);
81eda14cbcSMatt Macy return (s_len);
82eda14cbcSMatt Macy }
83eda14cbcSMatt Macy
84eda14cbcSMatt Macy return ((size_t)dstlen);
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy
87e2df9bb4SMartin Matuska static int
zfs_gzip_decompress_buf(void * s_start,void * d_start,size_t s_len,size_t d_len,int n)88e2df9bb4SMartin Matuska zfs_gzip_decompress_buf(void *s_start, void *d_start, size_t s_len,
89e2df9bb4SMartin Matuska size_t d_len, int n)
90eda14cbcSMatt Macy {
91e92ffd9bSMartin Matuska (void) n;
92eda14cbcSMatt Macy zlen_t dstlen = d_len;
93eda14cbcSMatt Macy
94eda14cbcSMatt Macy ASSERT(d_len >= s_len);
95eda14cbcSMatt Macy
96eda14cbcSMatt Macy /* check if hardware accelerator can be used */
97eda14cbcSMatt Macy if (qat_dc_use_accel(d_len)) {
98eda14cbcSMatt Macy if (qat_compress(QAT_DECOMPRESS, s_start, s_len,
99eda14cbcSMatt Macy d_start, d_len, &dstlen) == CPA_STATUS_SUCCESS)
100eda14cbcSMatt Macy return (0);
101eda14cbcSMatt Macy /* if hardware de-compress fail, do it again with software */
102eda14cbcSMatt Macy }
103eda14cbcSMatt Macy
104eda14cbcSMatt Macy if (uncompress_func(d_start, &dstlen, s_start, s_len) != Z_OK)
105eda14cbcSMatt Macy return (-1);
106eda14cbcSMatt Macy
107eda14cbcSMatt Macy return (0);
108eda14cbcSMatt Macy }
109e2df9bb4SMartin Matuska
110e2df9bb4SMartin Matuska ZFS_COMPRESS_WRAP_DECL(zfs_gzip_compress)
111e2df9bb4SMartin Matuska ZFS_DECOMPRESS_WRAP_DECL(zfs_gzip_decompress)
112