2 * Driver for IBM PowerNV 842 compression accelerator
4 * Copyright (C) 2015 Dan Streetman, IBM Corp
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/timer.h>
24 #include <asm/icswx.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
28 MODULE_DESCRIPTION("842 H/W Compression driver for IBM PowerNV processors");
29 MODULE_ALIAS_CRYPTO("842");
30 MODULE_ALIAS_CRYPTO("842-nx");
32 #define WORKMEM_ALIGN (CRB_ALIGN)
33 #define CSB_WAIT_MAX (5000) /* ms */
35 struct nx842_workmem
{
36 /* Below fields must be properly aligned */
37 struct coprocessor_request_block crb
; /* CRB_ALIGN align */
38 struct data_descriptor_entry ddl_in
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
39 struct data_descriptor_entry ddl_out
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
40 /* Above fields must be properly aligned */
44 char padding
[WORKMEM_ALIGN
]; /* unused, to allow alignment */
45 } __packed
__aligned(WORKMEM_ALIGN
);
51 struct list_head list
;
54 /* no cpu hotplug on powernv, so this list never changes after init */
55 static LIST_HEAD(nx842_coprocs
);
56 static unsigned int nx842_ct
;
59 * setup_indirect_dde - Setup an indirect DDE
61 * The DDE is setup with the the DDE count, byte count, and address of
62 * first direct DDE in the list.
64 static void setup_indirect_dde(struct data_descriptor_entry
*dde
,
65 struct data_descriptor_entry
*ddl
,
66 unsigned int dde_count
, unsigned int byte_count
)
69 dde
->count
= dde_count
;
71 dde
->length
= cpu_to_be32(byte_count
);
72 dde
->address
= cpu_to_be64(nx842_get_pa(ddl
));
76 * setup_direct_dde - Setup single DDE from buffer
78 * The DDE is setup with the buffer and length. The buffer must be properly
79 * aligned. The used length is returned.
81 * N Successfully set up DDE with N bytes
83 static unsigned int setup_direct_dde(struct data_descriptor_entry
*dde
,
84 unsigned long pa
, unsigned int len
)
86 unsigned int l
= min_t(unsigned int, len
, LEN_ON_PAGE(pa
));
91 dde
->length
= cpu_to_be32(l
);
92 dde
->address
= cpu_to_be64(pa
);
98 * setup_ddl - Setup DDL from buffer
101 * 0 Successfully set up DDL
103 static int setup_ddl(struct data_descriptor_entry
*dde
,
104 struct data_descriptor_entry
*ddl
,
105 unsigned char *buf
, unsigned int len
,
108 unsigned long pa
= nx842_get_pa(buf
);
109 int i
, ret
, total_len
= len
;
111 if (!IS_ALIGNED(pa
, DDE_BUFFER_ALIGN
)) {
112 pr_debug("%s buffer pa 0x%lx not 0x%x-byte aligned\n",
113 in
? "input" : "output", pa
, DDE_BUFFER_ALIGN
);
117 /* only need to check last mult; since buffer must be
118 * DDE_BUFFER_ALIGN aligned, and that is a multiple of
119 * DDE_BUFFER_SIZE_MULT, and pre-last page DDE buffers
120 * are guaranteed a multiple of DDE_BUFFER_SIZE_MULT.
122 if (len
% DDE_BUFFER_LAST_MULT
) {
123 pr_debug("%s buffer len 0x%x not a multiple of 0x%x\n",
124 in
? "input" : "output", len
, DDE_BUFFER_LAST_MULT
);
127 len
= round_down(len
, DDE_BUFFER_LAST_MULT
);
130 /* use a single direct DDE */
131 if (len
<= LEN_ON_PAGE(pa
)) {
132 ret
= setup_direct_dde(dde
, pa
, len
);
138 for (i
= 0; i
< DDL_LEN_MAX
&& len
> 0; i
++) {
139 ret
= setup_direct_dde(&ddl
[i
], pa
, len
);
142 pa
= nx842_get_pa(buf
);
146 pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n",
147 total_len
, in
? "input" : "output", len
);
152 setup_indirect_dde(dde
, ddl
, i
, total_len
);
157 #define CSB_ERR(csb, msg, ...) \
158 pr_err("ERROR: " msg " : %02x %02x %02x %02x %08x\n", \
159 ##__VA_ARGS__, (csb)->flags, \
160 (csb)->cs, (csb)->cc, (csb)->ce, \
161 be32_to_cpu((csb)->count))
163 #define CSB_ERR_ADDR(csb, msg, ...) \
164 CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \
165 (unsigned long)be64_to_cpu((csb)->address))
170 static int wait_for_csb(struct nx842_workmem
*wmem
,
171 struct coprocessor_status_block
*csb
)
173 ktime_t start
= wmem
->start
, now
= ktime_get();
174 ktime_t timeout
= ktime_add_ms(start
, CSB_WAIT_MAX
);
176 while (!(ACCESS_ONCE(csb
->flags
) & CSB_V
)) {
179 if (ktime_after(now
, timeout
))
183 /* hw has updated csb and output buffer */
186 /* check CSB flags */
187 if (!(csb
->flags
& CSB_V
)) {
188 CSB_ERR(csb
, "CSB still not valid after %ld us, giving up",
189 (long)ktime_us_delta(now
, start
));
192 if (csb
->flags
& CSB_F
) {
193 CSB_ERR(csb
, "Invalid CSB format");
196 if (csb
->flags
& CSB_CH
) {
197 CSB_ERR(csb
, "Invalid CSB chaining state");
201 /* verify CSB completion sequence is 0 */
203 CSB_ERR(csb
, "Invalid CSB completion sequence");
207 /* check CSB Completion Code */
212 case CSB_CC_TPBC_GT_SPBC
:
213 /* not an error, but the compressed data is
214 * larger than the uncompressed data :(
218 /* input data errors */
219 case CSB_CC_OPERAND_OVERLAP
:
220 /* input and output buffers overlap */
221 CSB_ERR(csb
, "Operand Overlap error");
223 case CSB_CC_INVALID_OPERAND
:
224 CSB_ERR(csb
, "Invalid operand");
227 /* output buffer too small */
230 CSB_ERR(csb
, "Function aborted");
232 case CSB_CC_CRC_MISMATCH
:
233 CSB_ERR(csb
, "CRC mismatch");
235 case CSB_CC_TEMPL_INVALID
:
236 CSB_ERR(csb
, "Compressed data template invalid");
238 case CSB_CC_TEMPL_OVERFLOW
:
239 CSB_ERR(csb
, "Compressed data template shows data past end");
242 /* these should not happen */
243 case CSB_CC_INVALID_ALIGN
:
244 /* setup_ddl should have detected this */
245 CSB_ERR_ADDR(csb
, "Invalid alignment");
247 case CSB_CC_DATA_LENGTH
:
248 /* setup_ddl should have detected this */
249 CSB_ERR(csb
, "Invalid data length");
251 case CSB_CC_WR_TRANSLATION
:
252 case CSB_CC_TRANSLATION
:
253 case CSB_CC_TRANSLATION_DUP1
:
254 case CSB_CC_TRANSLATION_DUP2
:
255 case CSB_CC_TRANSLATION_DUP3
:
256 case CSB_CC_TRANSLATION_DUP4
:
257 case CSB_CC_TRANSLATION_DUP5
:
258 case CSB_CC_TRANSLATION_DUP6
:
259 /* should not happen, we use physical addrs */
260 CSB_ERR_ADDR(csb
, "Translation error");
262 case CSB_CC_WR_PROTECTION
:
263 case CSB_CC_PROTECTION
:
264 case CSB_CC_PROTECTION_DUP1
:
265 case CSB_CC_PROTECTION_DUP2
:
266 case CSB_CC_PROTECTION_DUP3
:
267 case CSB_CC_PROTECTION_DUP4
:
268 case CSB_CC_PROTECTION_DUP5
:
269 case CSB_CC_PROTECTION_DUP6
:
270 /* should not happen, we use physical addrs */
271 CSB_ERR_ADDR(csb
, "Protection error");
273 case CSB_CC_PRIVILEGE
:
274 /* shouldn't happen, we're in HYP mode */
275 CSB_ERR(csb
, "Insufficient Privilege error");
277 case CSB_CC_EXCESSIVE_DDE
:
278 /* shouldn't happen, setup_ddl doesn't use many dde's */
279 CSB_ERR(csb
, "Too many DDEs in DDL");
281 case CSB_CC_TRANSPORT
:
282 /* shouldn't happen, we setup CRB correctly */
283 CSB_ERR(csb
, "Invalid CRB");
285 case CSB_CC_SEGMENTED_DDL
:
286 /* shouldn't happen, setup_ddl creates DDL right */
287 CSB_ERR(csb
, "Segmented DDL error");
289 case CSB_CC_DDE_OVERFLOW
:
290 /* shouldn't happen, setup_ddl creates DDL right */
291 CSB_ERR(csb
, "DDE overflow error");
294 /* should not happen with ICSWX */
295 CSB_ERR(csb
, "Session violation error");
298 /* should not happen, we don't use chained CRBs */
299 CSB_ERR(csb
, "Chained CRB error");
301 case CSB_CC_SEQUENCE
:
302 /* should not happen, we don't use chained CRBs */
303 CSB_ERR(csb
, "CRB seqeunce number error");
305 case CSB_CC_UNKNOWN_CODE
:
306 CSB_ERR(csb
, "Unknown subfunction code");
309 /* hardware errors */
310 case CSB_CC_RD_EXTERNAL
:
311 case CSB_CC_RD_EXTERNAL_DUP1
:
312 case CSB_CC_RD_EXTERNAL_DUP2
:
313 case CSB_CC_RD_EXTERNAL_DUP3
:
314 CSB_ERR_ADDR(csb
, "Read error outside coprocessor");
316 case CSB_CC_WR_EXTERNAL
:
317 CSB_ERR_ADDR(csb
, "Write error outside coprocessor");
319 case CSB_CC_INTERNAL
:
320 CSB_ERR(csb
, "Internal error in coprocessor");
322 case CSB_CC_PROVISION
:
323 CSB_ERR(csb
, "Storage provision error");
326 CSB_ERR(csb
, "Correctable hardware error");
330 CSB_ERR(csb
, "Invalid CC %d", csb
->cc
);
334 /* check Completion Extension state */
335 if (csb
->ce
& CSB_CE_TERMINATION
) {
336 CSB_ERR(csb
, "CSB request was terminated");
339 if (csb
->ce
& CSB_CE_INCOMPLETE
) {
340 CSB_ERR(csb
, "CSB request not complete");
343 if (!(csb
->ce
& CSB_CE_TPBC
)) {
344 CSB_ERR(csb
, "TPBC not provided, unknown target length");
348 /* successful completion */
349 pr_debug_ratelimited("Processed %u bytes in %lu us\n",
350 be32_to_cpu(csb
->count
),
351 (unsigned long)ktime_us_delta(now
, start
));
357 * nx842_powernv_function - compress/decompress data using the 842 algorithm
359 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
360 * This compresses or decompresses the provided input buffer into the provided
363 * Upon return from this function @outlen contains the length of the
364 * output data. If there is an error then @outlen will be 0 and an
365 * error will be specified by the return code from this function.
367 * The @workmem buffer should only be used by one function call at a time.
369 * @in: input buffer pointer
370 * @inlen: input buffer size
371 * @out: output buffer pointer
372 * @outlenp: output buffer size pointer
373 * @workmem: working memory buffer pointer, size determined by
374 * nx842_powernv_driver.workmem_size
375 * @fc: function code, see CCW Function Codes in nx-842.h
378 * 0 Success, output of length @outlenp stored in the buffer at @out
379 * -ENODEV Hardware unavailable
380 * -ENOSPC Output buffer is to small
381 * -EMSGSIZE Input buffer too large
382 * -EINVAL buffer constraints do not fix nx842_constraints
383 * -EPROTO hardware error during operation
384 * -ETIMEDOUT hardware did not complete operation in reasonable time
385 * -EINTR operation was aborted
387 static int nx842_powernv_function(const unsigned char *in
, unsigned int inlen
,
388 unsigned char *out
, unsigned int *outlenp
,
389 void *workmem
, int fc
)
391 struct coprocessor_request_block
*crb
;
392 struct coprocessor_status_block
*csb
;
393 struct nx842_workmem
*wmem
;
397 unsigned int outlen
= *outlenp
;
399 wmem
= PTR_ALIGN(workmem
, WORKMEM_ALIGN
);
403 /* shoudn't happen, we don't load without a coproc */
405 pr_err_ratelimited("coprocessor CT is 0");
412 /* Clear any previous values */
413 memset(crb
, 0, sizeof(*crb
));
416 ret
= setup_ddl(&crb
->source
, wmem
->ddl_in
,
417 (unsigned char *)in
, inlen
, true);
420 ret
= setup_ddl(&crb
->target
, wmem
->ddl_out
,
427 ccw
= SET_FIELD(ccw
, CCW_CT
, nx842_ct
);
428 ccw
= SET_FIELD(ccw
, CCW_CI_842
, 0); /* use 0 for hw auto-selection */
429 ccw
= SET_FIELD(ccw
, CCW_FC_842
, fc
);
431 /* set up CRB's CSB addr */
432 csb_addr
= nx842_get_pa(csb
) & CRB_CSB_ADDRESS
;
433 csb_addr
|= CRB_CSB_AT
; /* Addrs are phys */
434 crb
->csb_addr
= cpu_to_be64(csb_addr
);
436 wmem
->start
= ktime_get();
439 ret
= icswx(cpu_to_be32(ccw
), crb
);
441 pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret
,
443 (unsigned int)be32_to_cpu(crb
->ccw
));
446 * NX842 coprocessor sets 3rd bit in CR register with XER[S0].
447 * XER[S0] is the integer summary overflow bit which is nothing
448 * to do NX. Since this bit can be set with other return values,
454 case ICSWX_INITIATED
:
455 ret
= wait_for_csb(wmem
, csb
);
458 pr_debug_ratelimited("842 Coprocessor busy\n");
462 pr_err_ratelimited("ICSWX rejected\n");
468 *outlenp
= be32_to_cpu(csb
->count
);
474 * nx842_powernv_compress - Compress data using the 842 algorithm
476 * Compression provided by the NX842 coprocessor on IBM PowerNV systems.
477 * The input buffer is compressed and the result is stored in the
478 * provided output buffer.
480 * Upon return from this function @outlen contains the length of the
481 * compressed data. If there is an error then @outlen will be 0 and an
482 * error will be specified by the return code from this function.
484 * @in: input buffer pointer
485 * @inlen: input buffer size
486 * @out: output buffer pointer
487 * @outlenp: output buffer size pointer
488 * @workmem: working memory buffer pointer, size determined by
489 * nx842_powernv_driver.workmem_size
491 * Returns: see @nx842_powernv_function()
493 static int nx842_powernv_compress(const unsigned char *in
, unsigned int inlen
,
494 unsigned char *out
, unsigned int *outlenp
,
497 return nx842_powernv_function(in
, inlen
, out
, outlenp
,
498 wmem
, CCW_FC_842_COMP_CRC
);
502 * nx842_powernv_decompress - Decompress data using the 842 algorithm
504 * Decompression provided by the NX842 coprocessor on IBM PowerNV systems.
505 * The input buffer is decompressed and the result is stored in the
506 * provided output buffer.
508 * Upon return from this function @outlen contains the length of the
509 * decompressed data. If there is an error then @outlen will be 0 and an
510 * error will be specified by the return code from this function.
512 * @in: input buffer pointer
513 * @inlen: input buffer size
514 * @out: output buffer pointer
515 * @outlenp: output buffer size pointer
516 * @workmem: working memory buffer pointer, size determined by
517 * nx842_powernv_driver.workmem_size
519 * Returns: see @nx842_powernv_function()
521 static int nx842_powernv_decompress(const unsigned char *in
, unsigned int inlen
,
522 unsigned char *out
, unsigned int *outlenp
,
525 return nx842_powernv_function(in
, inlen
, out
, outlenp
,
526 wmem
, CCW_FC_842_DECOMP_CRC
);
529 static int __init
nx842_powernv_probe(struct device_node
*dn
)
531 struct nx842_coproc
*coproc
;
535 chip_id
= of_get_ibm_chip_id(dn
);
537 pr_err("ibm,chip-id missing\n");
541 if (of_property_read_u32(dn
, "ibm,842-coprocessor-type", &ct
)) {
542 pr_err("ibm,842-coprocessor-type missing\n");
546 if (of_property_read_u32(dn
, "ibm,842-coprocessor-instance", &ci
)) {
547 pr_err("ibm,842-coprocessor-instance missing\n");
551 coproc
= kmalloc(sizeof(*coproc
), GFP_KERNEL
);
555 coproc
->chip_id
= chip_id
;
558 INIT_LIST_HEAD(&coproc
->list
);
559 list_add(&coproc
->list
, &nx842_coprocs
);
561 pr_info("coprocessor found on chip %d, CT %d CI %d\n", chip_id
, ct
, ci
);
565 else if (nx842_ct
!= ct
)
566 pr_err("NX842 chip %d, CT %d != first found CT %d\n",
567 chip_id
, ct
, nx842_ct
);
572 static struct nx842_constraints nx842_powernv_constraints
= {
573 .alignment
= DDE_BUFFER_ALIGN
,
574 .multiple
= DDE_BUFFER_LAST_MULT
,
575 .minimum
= DDE_BUFFER_LAST_MULT
,
576 .maximum
= (DDL_LEN_MAX
- 1) * PAGE_SIZE
,
579 static struct nx842_driver nx842_powernv_driver
= {
580 .name
= KBUILD_MODNAME
,
581 .owner
= THIS_MODULE
,
582 .workmem_size
= sizeof(struct nx842_workmem
),
583 .constraints
= &nx842_powernv_constraints
,
584 .compress
= nx842_powernv_compress
,
585 .decompress
= nx842_powernv_decompress
,
588 static int nx842_powernv_crypto_init(struct crypto_tfm
*tfm
)
590 return nx842_crypto_init(tfm
, &nx842_powernv_driver
);
593 static struct crypto_alg nx842_powernv_alg
= {
595 .cra_driver_name
= "842-nx",
597 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
598 .cra_ctxsize
= sizeof(struct nx842_crypto_ctx
),
599 .cra_module
= THIS_MODULE
,
600 .cra_init
= nx842_powernv_crypto_init
,
601 .cra_exit
= nx842_crypto_exit
,
602 .cra_u
= { .compress
= {
603 .coa_compress
= nx842_crypto_compress
,
604 .coa_decompress
= nx842_crypto_decompress
} }
607 static __init
int nx842_powernv_init(void)
609 struct device_node
*dn
;
612 /* verify workmem size/align restrictions */
613 BUILD_BUG_ON(WORKMEM_ALIGN
% CRB_ALIGN
);
614 BUILD_BUG_ON(CRB_ALIGN
% DDE_ALIGN
);
615 BUILD_BUG_ON(CRB_SIZE
% DDE_ALIGN
);
616 /* verify buffer size/align restrictions */
617 BUILD_BUG_ON(PAGE_SIZE
% DDE_BUFFER_ALIGN
);
618 BUILD_BUG_ON(DDE_BUFFER_ALIGN
% DDE_BUFFER_SIZE_MULT
);
619 BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT
% DDE_BUFFER_LAST_MULT
);
621 for_each_compatible_node(dn
, NULL
, "ibm,power-nx")
622 nx842_powernv_probe(dn
);
627 ret
= crypto_register_alg(&nx842_powernv_alg
);
629 struct nx842_coproc
*coproc
, *n
;
631 list_for_each_entry_safe(coproc
, n
, &nx842_coprocs
, list
) {
632 list_del(&coproc
->list
);
641 module_init(nx842_powernv_init
);
643 static void __exit
nx842_powernv_exit(void)
645 struct nx842_coproc
*coproc
, *n
;
647 crypto_unregister_alg(&nx842_powernv_alg
);
649 list_for_each_entry_safe(coproc
, n
, &nx842_coprocs
, list
) {
650 list_del(&coproc
->list
);
654 module_exit(nx842_powernv_exit
);