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>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
29 MODULE_DESCRIPTION("842 H/W Compression driver for IBM PowerNV processors");
30 MODULE_ALIAS_CRYPTO("842");
31 MODULE_ALIAS_CRYPTO("842-nx");
33 #define WORKMEM_ALIGN (CRB_ALIGN)
34 #define CSB_WAIT_MAX (5000) /* ms */
36 struct nx842_workmem
{
37 /* Below fields must be properly aligned */
38 struct coprocessor_request_block crb
; /* CRB_ALIGN align */
39 struct data_descriptor_entry ddl_in
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
40 struct data_descriptor_entry ddl_out
[DDL_LEN_MAX
]; /* DDE_ALIGN align */
41 /* Above fields must be properly aligned */
45 char padding
[WORKMEM_ALIGN
]; /* unused, to allow alignment */
46 } __packed
__aligned(WORKMEM_ALIGN
);
52 struct list_head list
;
55 /* no cpu hotplug on powernv, so this list never changes after init */
56 static LIST_HEAD(nx842_coprocs
);
57 static unsigned int nx842_ct
;
60 * setup_indirect_dde - Setup an indirect DDE
62 * The DDE is setup with the the DDE count, byte count, and address of
63 * first direct DDE in the list.
65 static void setup_indirect_dde(struct data_descriptor_entry
*dde
,
66 struct data_descriptor_entry
*ddl
,
67 unsigned int dde_count
, unsigned int byte_count
)
70 dde
->count
= dde_count
;
72 dde
->length
= cpu_to_be32(byte_count
);
73 dde
->address
= cpu_to_be64(nx842_get_pa(ddl
));
77 * setup_direct_dde - Setup single DDE from buffer
79 * The DDE is setup with the buffer and length. The buffer must be properly
80 * aligned. The used length is returned.
82 * N Successfully set up DDE with N bytes
84 static unsigned int setup_direct_dde(struct data_descriptor_entry
*dde
,
85 unsigned long pa
, unsigned int len
)
87 unsigned int l
= min_t(unsigned int, len
, LEN_ON_PAGE(pa
));
92 dde
->length
= cpu_to_be32(l
);
93 dde
->address
= cpu_to_be64(pa
);
99 * setup_ddl - Setup DDL from buffer
102 * 0 Successfully set up DDL
104 static int setup_ddl(struct data_descriptor_entry
*dde
,
105 struct data_descriptor_entry
*ddl
,
106 unsigned char *buf
, unsigned int len
,
109 unsigned long pa
= nx842_get_pa(buf
);
110 int i
, ret
, total_len
= len
;
112 if (!IS_ALIGNED(pa
, DDE_BUFFER_ALIGN
)) {
113 pr_debug("%s buffer pa 0x%lx not 0x%x-byte aligned\n",
114 in
? "input" : "output", pa
, DDE_BUFFER_ALIGN
);
118 /* only need to check last mult; since buffer must be
119 * DDE_BUFFER_ALIGN aligned, and that is a multiple of
120 * DDE_BUFFER_SIZE_MULT, and pre-last page DDE buffers
121 * are guaranteed a multiple of DDE_BUFFER_SIZE_MULT.
123 if (len
% DDE_BUFFER_LAST_MULT
) {
124 pr_debug("%s buffer len 0x%x not a multiple of 0x%x\n",
125 in
? "input" : "output", len
, DDE_BUFFER_LAST_MULT
);
128 len
= round_down(len
, DDE_BUFFER_LAST_MULT
);
131 /* use a single direct DDE */
132 if (len
<= LEN_ON_PAGE(pa
)) {
133 ret
= setup_direct_dde(dde
, pa
, len
);
139 for (i
= 0; i
< DDL_LEN_MAX
&& len
> 0; i
++) {
140 ret
= setup_direct_dde(&ddl
[i
], pa
, len
);
143 pa
= nx842_get_pa(buf
);
147 pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n",
148 total_len
, in
? "input" : "output", len
);
153 setup_indirect_dde(dde
, ddl
, i
, total_len
);
158 #define CSB_ERR(csb, msg, ...) \
159 pr_err("ERROR: " msg " : %02x %02x %02x %02x %08x\n", \
160 ##__VA_ARGS__, (csb)->flags, \
161 (csb)->cs, (csb)->cc, (csb)->ce, \
162 be32_to_cpu((csb)->count))
164 #define CSB_ERR_ADDR(csb, msg, ...) \
165 CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \
166 (unsigned long)be64_to_cpu((csb)->address))
171 static int wait_for_csb(struct nx842_workmem
*wmem
,
172 struct coprocessor_status_block
*csb
)
174 ktime_t start
= wmem
->start
, now
= ktime_get();
175 ktime_t timeout
= ktime_add_ms(start
, CSB_WAIT_MAX
);
177 while (!(ACCESS_ONCE(csb
->flags
) & CSB_V
)) {
180 if (ktime_after(now
, timeout
))
184 /* hw has updated csb and output buffer */
187 /* check CSB flags */
188 if (!(csb
->flags
& CSB_V
)) {
189 CSB_ERR(csb
, "CSB still not valid after %ld us, giving up",
190 (long)ktime_us_delta(now
, start
));
193 if (csb
->flags
& CSB_F
) {
194 CSB_ERR(csb
, "Invalid CSB format");
197 if (csb
->flags
& CSB_CH
) {
198 CSB_ERR(csb
, "Invalid CSB chaining state");
202 /* verify CSB completion sequence is 0 */
204 CSB_ERR(csb
, "Invalid CSB completion sequence");
208 /* check CSB Completion Code */
213 case CSB_CC_TPBC_GT_SPBC
:
214 /* not an error, but the compressed data is
215 * larger than the uncompressed data :(
219 /* input data errors */
220 case CSB_CC_OPERAND_OVERLAP
:
221 /* input and output buffers overlap */
222 CSB_ERR(csb
, "Operand Overlap error");
224 case CSB_CC_INVALID_OPERAND
:
225 CSB_ERR(csb
, "Invalid operand");
228 /* output buffer too small */
231 CSB_ERR(csb
, "Function aborted");
233 case CSB_CC_CRC_MISMATCH
:
234 CSB_ERR(csb
, "CRC mismatch");
236 case CSB_CC_TEMPL_INVALID
:
237 CSB_ERR(csb
, "Compressed data template invalid");
239 case CSB_CC_TEMPL_OVERFLOW
:
240 CSB_ERR(csb
, "Compressed data template shows data past end");
243 /* these should not happen */
244 case CSB_CC_INVALID_ALIGN
:
245 /* setup_ddl should have detected this */
246 CSB_ERR_ADDR(csb
, "Invalid alignment");
248 case CSB_CC_DATA_LENGTH
:
249 /* setup_ddl should have detected this */
250 CSB_ERR(csb
, "Invalid data length");
252 case CSB_CC_WR_TRANSLATION
:
253 case CSB_CC_TRANSLATION
:
254 case CSB_CC_TRANSLATION_DUP1
:
255 case CSB_CC_TRANSLATION_DUP2
:
256 case CSB_CC_TRANSLATION_DUP3
:
257 case CSB_CC_TRANSLATION_DUP4
:
258 case CSB_CC_TRANSLATION_DUP5
:
259 case CSB_CC_TRANSLATION_DUP6
:
260 /* should not happen, we use physical addrs */
261 CSB_ERR_ADDR(csb
, "Translation error");
263 case CSB_CC_WR_PROTECTION
:
264 case CSB_CC_PROTECTION
:
265 case CSB_CC_PROTECTION_DUP1
:
266 case CSB_CC_PROTECTION_DUP2
:
267 case CSB_CC_PROTECTION_DUP3
:
268 case CSB_CC_PROTECTION_DUP4
:
269 case CSB_CC_PROTECTION_DUP5
:
270 case CSB_CC_PROTECTION_DUP6
:
271 /* should not happen, we use physical addrs */
272 CSB_ERR_ADDR(csb
, "Protection error");
274 case CSB_CC_PRIVILEGE
:
275 /* shouldn't happen, we're in HYP mode */
276 CSB_ERR(csb
, "Insufficient Privilege error");
278 case CSB_CC_EXCESSIVE_DDE
:
279 /* shouldn't happen, setup_ddl doesn't use many dde's */
280 CSB_ERR(csb
, "Too many DDEs in DDL");
282 case CSB_CC_TRANSPORT
:
283 /* shouldn't happen, we setup CRB correctly */
284 CSB_ERR(csb
, "Invalid CRB");
286 case CSB_CC_SEGMENTED_DDL
:
287 /* shouldn't happen, setup_ddl creates DDL right */
288 CSB_ERR(csb
, "Segmented DDL error");
290 case CSB_CC_DDE_OVERFLOW
:
291 /* shouldn't happen, setup_ddl creates DDL right */
292 CSB_ERR(csb
, "DDE overflow error");
295 /* should not happen with ICSWX */
296 CSB_ERR(csb
, "Session violation error");
299 /* should not happen, we don't use chained CRBs */
300 CSB_ERR(csb
, "Chained CRB error");
302 case CSB_CC_SEQUENCE
:
303 /* should not happen, we don't use chained CRBs */
304 CSB_ERR(csb
, "CRB seqeunce number error");
306 case CSB_CC_UNKNOWN_CODE
:
307 CSB_ERR(csb
, "Unknown subfunction code");
310 /* hardware errors */
311 case CSB_CC_RD_EXTERNAL
:
312 case CSB_CC_RD_EXTERNAL_DUP1
:
313 case CSB_CC_RD_EXTERNAL_DUP2
:
314 case CSB_CC_RD_EXTERNAL_DUP3
:
315 CSB_ERR_ADDR(csb
, "Read error outside coprocessor");
317 case CSB_CC_WR_EXTERNAL
:
318 CSB_ERR_ADDR(csb
, "Write error outside coprocessor");
320 case CSB_CC_INTERNAL
:
321 CSB_ERR(csb
, "Internal error in coprocessor");
323 case CSB_CC_PROVISION
:
324 CSB_ERR(csb
, "Storage provision error");
327 CSB_ERR(csb
, "Correctable hardware error");
331 CSB_ERR(csb
, "Invalid CC %d", csb
->cc
);
335 /* check Completion Extension state */
336 if (csb
->ce
& CSB_CE_TERMINATION
) {
337 CSB_ERR(csb
, "CSB request was terminated");
340 if (csb
->ce
& CSB_CE_INCOMPLETE
) {
341 CSB_ERR(csb
, "CSB request not complete");
344 if (!(csb
->ce
& CSB_CE_TPBC
)) {
345 CSB_ERR(csb
, "TPBC not provided, unknown target length");
349 /* successful completion */
350 pr_debug_ratelimited("Processed %u bytes in %lu us\n",
351 be32_to_cpu(csb
->count
),
352 (unsigned long)ktime_us_delta(now
, start
));
358 * nx842_powernv_function - compress/decompress data using the 842 algorithm
360 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
361 * This compresses or decompresses the provided input buffer into the provided
364 * Upon return from this function @outlen contains the length of the
365 * output data. If there is an error then @outlen will be 0 and an
366 * error will be specified by the return code from this function.
368 * The @workmem buffer should only be used by one function call at a time.
370 * @in: input buffer pointer
371 * @inlen: input buffer size
372 * @out: output buffer pointer
373 * @outlenp: output buffer size pointer
374 * @workmem: working memory buffer pointer, size determined by
375 * nx842_powernv_driver.workmem_size
376 * @fc: function code, see CCW Function Codes in nx-842.h
379 * 0 Success, output of length @outlenp stored in the buffer at @out
380 * -ENODEV Hardware unavailable
381 * -ENOSPC Output buffer is to small
382 * -EMSGSIZE Input buffer too large
383 * -EINVAL buffer constraints do not fix nx842_constraints
384 * -EPROTO hardware error during operation
385 * -ETIMEDOUT hardware did not complete operation in reasonable time
386 * -EINTR operation was aborted
388 static int nx842_powernv_function(const unsigned char *in
, unsigned int inlen
,
389 unsigned char *out
, unsigned int *outlenp
,
390 void *workmem
, int fc
)
392 struct coprocessor_request_block
*crb
;
393 struct coprocessor_status_block
*csb
;
394 struct nx842_workmem
*wmem
;
398 unsigned int outlen
= *outlenp
;
400 wmem
= PTR_ALIGN(workmem
, WORKMEM_ALIGN
);
404 /* shoudn't happen, we don't load without a coproc */
406 pr_err_ratelimited("coprocessor CT is 0");
413 /* Clear any previous values */
414 memset(crb
, 0, sizeof(*crb
));
417 ret
= setup_ddl(&crb
->source
, wmem
->ddl_in
,
418 (unsigned char *)in
, inlen
, true);
421 ret
= setup_ddl(&crb
->target
, wmem
->ddl_out
,
428 ccw
= SET_FIELD(CCW_CT
, ccw
, nx842_ct
);
429 ccw
= SET_FIELD(CCW_CI_842
, ccw
, 0); /* use 0 for hw auto-selection */
430 ccw
= SET_FIELD(CCW_FC_842
, ccw
, fc
);
432 /* set up CRB's CSB addr */
433 csb_addr
= nx842_get_pa(csb
) & CRB_CSB_ADDRESS
;
434 csb_addr
|= CRB_CSB_AT
; /* Addrs are phys */
435 crb
->csb_addr
= cpu_to_be64(csb_addr
);
437 wmem
->start
= ktime_get();
440 ret
= icswx(cpu_to_be32(ccw
), crb
);
442 pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret
,
444 (unsigned int)be32_to_cpu(crb
->ccw
));
447 * NX842 coprocessor sets 3rd bit in CR register with XER[S0].
448 * XER[S0] is the integer summary overflow bit which is nothing
449 * to do NX. Since this bit can be set with other return values,
455 case ICSWX_INITIATED
:
456 ret
= wait_for_csb(wmem
, csb
);
459 pr_debug_ratelimited("842 Coprocessor busy\n");
463 pr_err_ratelimited("ICSWX rejected\n");
469 *outlenp
= be32_to_cpu(csb
->count
);
475 * nx842_powernv_compress - Compress data using the 842 algorithm
477 * Compression provided by the NX842 coprocessor on IBM PowerNV systems.
478 * The input buffer is compressed and the result is stored in the
479 * provided output buffer.
481 * Upon return from this function @outlen contains the length of the
482 * compressed data. If there is an error then @outlen will be 0 and an
483 * error will be specified by the return code from this function.
485 * @in: input buffer pointer
486 * @inlen: input buffer size
487 * @out: output buffer pointer
488 * @outlenp: output buffer size pointer
489 * @workmem: working memory buffer pointer, size determined by
490 * nx842_powernv_driver.workmem_size
492 * Returns: see @nx842_powernv_function()
494 static int nx842_powernv_compress(const unsigned char *in
, unsigned int inlen
,
495 unsigned char *out
, unsigned int *outlenp
,
498 return nx842_powernv_function(in
, inlen
, out
, outlenp
,
499 wmem
, CCW_FC_842_COMP_CRC
);
503 * nx842_powernv_decompress - Decompress data using the 842 algorithm
505 * Decompression provided by the NX842 coprocessor on IBM PowerNV systems.
506 * The input buffer is decompressed and the result is stored in the
507 * provided output buffer.
509 * Upon return from this function @outlen contains the length of the
510 * decompressed data. If there is an error then @outlen will be 0 and an
511 * error will be specified by the return code from this function.
513 * @in: input buffer pointer
514 * @inlen: input buffer size
515 * @out: output buffer pointer
516 * @outlenp: output buffer size pointer
517 * @workmem: working memory buffer pointer, size determined by
518 * nx842_powernv_driver.workmem_size
520 * Returns: see @nx842_powernv_function()
522 static int nx842_powernv_decompress(const unsigned char *in
, unsigned int inlen
,
523 unsigned char *out
, unsigned int *outlenp
,
526 return nx842_powernv_function(in
, inlen
, out
, outlenp
,
527 wmem
, CCW_FC_842_DECOMP_CRC
);
530 static int __init
nx842_powernv_probe(struct device_node
*dn
)
532 struct nx842_coproc
*coproc
;
536 chip_id
= of_get_ibm_chip_id(dn
);
538 pr_err("ibm,chip-id missing\n");
542 if (of_property_read_u32(dn
, "ibm,842-coprocessor-type", &ct
)) {
543 pr_err("ibm,842-coprocessor-type missing\n");
547 if (of_property_read_u32(dn
, "ibm,842-coprocessor-instance", &ci
)) {
548 pr_err("ibm,842-coprocessor-instance missing\n");
552 coproc
= kmalloc(sizeof(*coproc
), GFP_KERNEL
);
556 coproc
->chip_id
= chip_id
;
559 INIT_LIST_HEAD(&coproc
->list
);
560 list_add(&coproc
->list
, &nx842_coprocs
);
562 pr_info("coprocessor found on chip %d, CT %d CI %d\n", chip_id
, ct
, ci
);
566 else if (nx842_ct
!= ct
)
567 pr_err("NX842 chip %d, CT %d != first found CT %d\n",
568 chip_id
, ct
, nx842_ct
);
573 static struct nx842_constraints nx842_powernv_constraints
= {
574 .alignment
= DDE_BUFFER_ALIGN
,
575 .multiple
= DDE_BUFFER_LAST_MULT
,
576 .minimum
= DDE_BUFFER_LAST_MULT
,
577 .maximum
= (DDL_LEN_MAX
- 1) * PAGE_SIZE
,
580 static struct nx842_driver nx842_powernv_driver
= {
581 .name
= KBUILD_MODNAME
,
582 .owner
= THIS_MODULE
,
583 .workmem_size
= sizeof(struct nx842_workmem
),
584 .constraints
= &nx842_powernv_constraints
,
585 .compress
= nx842_powernv_compress
,
586 .decompress
= nx842_powernv_decompress
,
589 static int nx842_powernv_crypto_init(struct crypto_tfm
*tfm
)
591 return nx842_crypto_init(tfm
, &nx842_powernv_driver
);
594 static struct crypto_alg nx842_powernv_alg
= {
596 .cra_driver_name
= "842-nx",
598 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
599 .cra_ctxsize
= sizeof(struct nx842_crypto_ctx
),
600 .cra_module
= THIS_MODULE
,
601 .cra_init
= nx842_powernv_crypto_init
,
602 .cra_exit
= nx842_crypto_exit
,
603 .cra_u
= { .compress
= {
604 .coa_compress
= nx842_crypto_compress
,
605 .coa_decompress
= nx842_crypto_decompress
} }
608 static __init
int nx842_powernv_init(void)
610 struct device_node
*dn
;
613 /* verify workmem size/align restrictions */
614 BUILD_BUG_ON(WORKMEM_ALIGN
% CRB_ALIGN
);
615 BUILD_BUG_ON(CRB_ALIGN
% DDE_ALIGN
);
616 BUILD_BUG_ON(CRB_SIZE
% DDE_ALIGN
);
617 /* verify buffer size/align restrictions */
618 BUILD_BUG_ON(PAGE_SIZE
% DDE_BUFFER_ALIGN
);
619 BUILD_BUG_ON(DDE_BUFFER_ALIGN
% DDE_BUFFER_SIZE_MULT
);
620 BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT
% DDE_BUFFER_LAST_MULT
);
622 for_each_compatible_node(dn
, NULL
, "ibm,power-nx")
623 nx842_powernv_probe(dn
);
628 ret
= crypto_register_alg(&nx842_powernv_alg
);
630 struct nx842_coproc
*coproc
, *n
;
632 list_for_each_entry_safe(coproc
, n
, &nx842_coprocs
, list
) {
633 list_del(&coproc
->list
);
642 module_init(nx842_powernv_init
);
644 static void __exit
nx842_powernv_exit(void)
646 struct nx842_coproc
*coproc
, *n
;
648 crypto_unregister_alg(&nx842_powernv_alg
);
650 list_for_each_entry_safe(coproc
, n
, &nx842_coprocs
, list
) {
651 list_del(&coproc
->list
);
655 module_exit(nx842_powernv_exit
);