crypto: nx - reduce chattiness of platform drivers
[linux-2.6-block.git] / drivers / crypto / nx / nx-842-powernv.c
CommitLineData
99182a42
DS
1/*
2 * Driver for IBM PowerNV 842 compression accelerator
3 *
4 * Copyright (C) 2015 Dan Streetman, IBM Corp
5 *
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.
10 *
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.
15 */
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include "nx-842.h"
20
21#include <linux/timer.h>
22
23#include <asm/prom.h>
24#include <asm/icswx.h>
25
99182a42
DS
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
28MODULE_DESCRIPTION("842 H/W Compression driver for IBM PowerNV processors");
29
30#define WORKMEM_ALIGN (CRB_ALIGN)
31#define CSB_WAIT_MAX (5000) /* ms */
32
33struct nx842_workmem {
34 /* Below fields must be properly aligned */
35 struct coprocessor_request_block crb; /* CRB_ALIGN align */
36 struct data_descriptor_entry ddl_in[DDL_LEN_MAX]; /* DDE_ALIGN align */
37 struct data_descriptor_entry ddl_out[DDL_LEN_MAX]; /* DDE_ALIGN align */
38 /* Above fields must be properly aligned */
39
40 ktime_t start;
41
42 char padding[WORKMEM_ALIGN]; /* unused, to allow alignment */
43} __packed __aligned(WORKMEM_ALIGN);
44
45struct nx842_coproc {
46 unsigned int chip_id;
47 unsigned int ct;
48 unsigned int ci;
49 struct list_head list;
50};
51
52/* no cpu hotplug on powernv, so this list never changes after init */
53static LIST_HEAD(nx842_coprocs);
54static unsigned int nx842_ct;
55
56/**
57 * setup_indirect_dde - Setup an indirect DDE
58 *
59 * The DDE is setup with the the DDE count, byte count, and address of
60 * first direct DDE in the list.
61 */
62static void setup_indirect_dde(struct data_descriptor_entry *dde,
63 struct data_descriptor_entry *ddl,
64 unsigned int dde_count, unsigned int byte_count)
65{
66 dde->flags = 0;
67 dde->count = dde_count;
68 dde->index = 0;
69 dde->length = cpu_to_be32(byte_count);
70 dde->address = cpu_to_be64(nx842_get_pa(ddl));
71}
72
73/**
74 * setup_direct_dde - Setup single DDE from buffer
75 *
76 * The DDE is setup with the buffer and length. The buffer must be properly
77 * aligned. The used length is returned.
78 * Returns:
79 * N Successfully set up DDE with N bytes
80 */
81static unsigned int setup_direct_dde(struct data_descriptor_entry *dde,
82 unsigned long pa, unsigned int len)
83{
84 unsigned int l = min_t(unsigned int, len, LEN_ON_PAGE(pa));
85
86 dde->flags = 0;
87 dde->count = 0;
88 dde->index = 0;
89 dde->length = cpu_to_be32(l);
90 dde->address = cpu_to_be64(pa);
91
92 return l;
93}
94
95/**
96 * setup_ddl - Setup DDL from buffer
97 *
98 * Returns:
99 * 0 Successfully set up DDL
100 */
101static int setup_ddl(struct data_descriptor_entry *dde,
102 struct data_descriptor_entry *ddl,
103 unsigned char *buf, unsigned int len,
104 bool in)
105{
106 unsigned long pa = nx842_get_pa(buf);
107 int i, ret, total_len = len;
108
109 if (!IS_ALIGNED(pa, DDE_BUFFER_ALIGN)) {
110 pr_debug("%s buffer pa 0x%lx not 0x%x-byte aligned\n",
111 in ? "input" : "output", pa, DDE_BUFFER_ALIGN);
112 return -EINVAL;
113 }
114
115 /* only need to check last mult; since buffer must be
116 * DDE_BUFFER_ALIGN aligned, and that is a multiple of
117 * DDE_BUFFER_SIZE_MULT, and pre-last page DDE buffers
118 * are guaranteed a multiple of DDE_BUFFER_SIZE_MULT.
119 */
120 if (len % DDE_BUFFER_LAST_MULT) {
121 pr_debug("%s buffer len 0x%x not a multiple of 0x%x\n",
122 in ? "input" : "output", len, DDE_BUFFER_LAST_MULT);
123 if (in)
124 return -EINVAL;
125 len = round_down(len, DDE_BUFFER_LAST_MULT);
126 }
127
128 /* use a single direct DDE */
129 if (len <= LEN_ON_PAGE(pa)) {
130 ret = setup_direct_dde(dde, pa, len);
131 WARN_ON(ret < len);
132 return 0;
133 }
134
135 /* use the DDL */
136 for (i = 0; i < DDL_LEN_MAX && len > 0; i++) {
137 ret = setup_direct_dde(&ddl[i], pa, len);
138 buf += ret;
139 len -= ret;
140 pa = nx842_get_pa(buf);
141 }
142
143 if (len > 0) {
144 pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n",
145 total_len, in ? "input" : "output", len);
146 if (in)
147 return -EMSGSIZE;
148 total_len -= len;
149 }
150 setup_indirect_dde(dde, ddl, i, total_len);
151
152 return 0;
153}
154
155#define CSB_ERR(csb, msg, ...) \
156 pr_err("ERROR: " msg " : %02x %02x %02x %02x %08x\n", \
157 ##__VA_ARGS__, (csb)->flags, \
158 (csb)->cs, (csb)->cc, (csb)->ce, \
159 be32_to_cpu((csb)->count))
160
161#define CSB_ERR_ADDR(csb, msg, ...) \
162 CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \
163 (unsigned long)be64_to_cpu((csb)->address))
164
165/**
166 * wait_for_csb
167 */
168static int wait_for_csb(struct nx842_workmem *wmem,
169 struct coprocessor_status_block *csb)
170{
171 ktime_t start = wmem->start, now = ktime_get();
172 ktime_t timeout = ktime_add_ms(start, CSB_WAIT_MAX);
173
174 while (!(ACCESS_ONCE(csb->flags) & CSB_V)) {
175 cpu_relax();
176 now = ktime_get();
177 if (ktime_after(now, timeout))
178 break;
179 }
180
181 /* hw has updated csb and output buffer */
182 barrier();
183
184 /* check CSB flags */
185 if (!(csb->flags & CSB_V)) {
186 CSB_ERR(csb, "CSB still not valid after %ld us, giving up",
187 (long)ktime_us_delta(now, start));
188 return -ETIMEDOUT;
189 }
190 if (csb->flags & CSB_F) {
191 CSB_ERR(csb, "Invalid CSB format");
192 return -EPROTO;
193 }
194 if (csb->flags & CSB_CH) {
195 CSB_ERR(csb, "Invalid CSB chaining state");
196 return -EPROTO;
197 }
198
199 /* verify CSB completion sequence is 0 */
200 if (csb->cs) {
201 CSB_ERR(csb, "Invalid CSB completion sequence");
202 return -EPROTO;
203 }
204
205 /* check CSB Completion Code */
206 switch (csb->cc) {
207 /* no error */
208 case CSB_CC_SUCCESS:
209 break;
210 case CSB_CC_TPBC_GT_SPBC:
211 /* not an error, but the compressed data is
212 * larger than the uncompressed data :(
213 */
214 break;
215
216 /* input data errors */
217 case CSB_CC_OPERAND_OVERLAP:
218 /* input and output buffers overlap */
219 CSB_ERR(csb, "Operand Overlap error");
220 return -EINVAL;
221 case CSB_CC_INVALID_OPERAND:
222 CSB_ERR(csb, "Invalid operand");
223 return -EINVAL;
224 case CSB_CC_NOSPC:
225 /* output buffer too small */
226 return -ENOSPC;
227 case CSB_CC_ABORT:
228 CSB_ERR(csb, "Function aborted");
229 return -EINTR;
230 case CSB_CC_CRC_MISMATCH:
231 CSB_ERR(csb, "CRC mismatch");
232 return -EINVAL;
233 case CSB_CC_TEMPL_INVALID:
234 CSB_ERR(csb, "Compressed data template invalid");
235 return -EINVAL;
236 case CSB_CC_TEMPL_OVERFLOW:
237 CSB_ERR(csb, "Compressed data template shows data past end");
238 return -EINVAL;
239
240 /* these should not happen */
241 case CSB_CC_INVALID_ALIGN:
242 /* setup_ddl should have detected this */
243 CSB_ERR_ADDR(csb, "Invalid alignment");
244 return -EINVAL;
245 case CSB_CC_DATA_LENGTH:
246 /* setup_ddl should have detected this */
247 CSB_ERR(csb, "Invalid data length");
248 return -EINVAL;
249 case CSB_CC_WR_TRANSLATION:
250 case CSB_CC_TRANSLATION:
251 case CSB_CC_TRANSLATION_DUP1:
252 case CSB_CC_TRANSLATION_DUP2:
253 case CSB_CC_TRANSLATION_DUP3:
254 case CSB_CC_TRANSLATION_DUP4:
255 case CSB_CC_TRANSLATION_DUP5:
256 case CSB_CC_TRANSLATION_DUP6:
257 /* should not happen, we use physical addrs */
258 CSB_ERR_ADDR(csb, "Translation error");
259 return -EPROTO;
260 case CSB_CC_WR_PROTECTION:
261 case CSB_CC_PROTECTION:
262 case CSB_CC_PROTECTION_DUP1:
263 case CSB_CC_PROTECTION_DUP2:
264 case CSB_CC_PROTECTION_DUP3:
265 case CSB_CC_PROTECTION_DUP4:
266 case CSB_CC_PROTECTION_DUP5:
267 case CSB_CC_PROTECTION_DUP6:
268 /* should not happen, we use physical addrs */
269 CSB_ERR_ADDR(csb, "Protection error");
270 return -EPROTO;
271 case CSB_CC_PRIVILEGE:
272 /* shouldn't happen, we're in HYP mode */
273 CSB_ERR(csb, "Insufficient Privilege error");
274 return -EPROTO;
275 case CSB_CC_EXCESSIVE_DDE:
276 /* shouldn't happen, setup_ddl doesn't use many dde's */
277 CSB_ERR(csb, "Too many DDEs in DDL");
278 return -EINVAL;
279 case CSB_CC_TRANSPORT:
280 /* shouldn't happen, we setup CRB correctly */
281 CSB_ERR(csb, "Invalid CRB");
282 return -EINVAL;
283 case CSB_CC_SEGMENTED_DDL:
284 /* shouldn't happen, setup_ddl creates DDL right */
285 CSB_ERR(csb, "Segmented DDL error");
286 return -EINVAL;
287 case CSB_CC_DDE_OVERFLOW:
288 /* shouldn't happen, setup_ddl creates DDL right */
289 CSB_ERR(csb, "DDE overflow error");
290 return -EINVAL;
291 case CSB_CC_SESSION:
292 /* should not happen with ICSWX */
293 CSB_ERR(csb, "Session violation error");
294 return -EPROTO;
295 case CSB_CC_CHAIN:
296 /* should not happen, we don't use chained CRBs */
297 CSB_ERR(csb, "Chained CRB error");
298 return -EPROTO;
299 case CSB_CC_SEQUENCE:
300 /* should not happen, we don't use chained CRBs */
301 CSB_ERR(csb, "CRB seqeunce number error");
302 return -EPROTO;
303 case CSB_CC_UNKNOWN_CODE:
304 CSB_ERR(csb, "Unknown subfunction code");
305 return -EPROTO;
306
307 /* hardware errors */
308 case CSB_CC_RD_EXTERNAL:
309 case CSB_CC_RD_EXTERNAL_DUP1:
310 case CSB_CC_RD_EXTERNAL_DUP2:
311 case CSB_CC_RD_EXTERNAL_DUP3:
312 CSB_ERR_ADDR(csb, "Read error outside coprocessor");
313 return -EPROTO;
314 case CSB_CC_WR_EXTERNAL:
315 CSB_ERR_ADDR(csb, "Write error outside coprocessor");
316 return -EPROTO;
317 case CSB_CC_INTERNAL:
318 CSB_ERR(csb, "Internal error in coprocessor");
319 return -EPROTO;
320 case CSB_CC_PROVISION:
321 CSB_ERR(csb, "Storage provision error");
322 return -EPROTO;
323 case CSB_CC_HW:
324 CSB_ERR(csb, "Correctable hardware error");
325 return -EPROTO;
326
327 default:
328 CSB_ERR(csb, "Invalid CC %d", csb->cc);
329 return -EPROTO;
330 }
331
332 /* check Completion Extension state */
333 if (csb->ce & CSB_CE_TERMINATION) {
334 CSB_ERR(csb, "CSB request was terminated");
335 return -EPROTO;
336 }
337 if (csb->ce & CSB_CE_INCOMPLETE) {
338 CSB_ERR(csb, "CSB request not complete");
339 return -EPROTO;
340 }
341 if (!(csb->ce & CSB_CE_TPBC)) {
342 CSB_ERR(csb, "TPBC not provided, unknown target length");
343 return -EPROTO;
344 }
345
346 /* successful completion */
347 pr_debug_ratelimited("Processed %u bytes in %lu us\n", csb->count,
348 (unsigned long)ktime_us_delta(now, start));
349
350 return 0;
351}
352
353/**
354 * nx842_powernv_function - compress/decompress data using the 842 algorithm
355 *
356 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
357 * This compresses or decompresses the provided input buffer into the provided
358 * output buffer.
359 *
360 * Upon return from this function @outlen contains the length of the
361 * output data. If there is an error then @outlen will be 0 and an
362 * error will be specified by the return code from this function.
363 *
364 * The @workmem buffer should only be used by one function call at a time.
365 *
366 * @in: input buffer pointer
367 * @inlen: input buffer size
368 * @out: output buffer pointer
369 * @outlenp: output buffer size pointer
2c6f6eab
DS
370 * @workmem: working memory buffer pointer, size determined by
371 * nx842_powernv_driver.workmem_size
99182a42
DS
372 * @fc: function code, see CCW Function Codes in nx-842.h
373 *
374 * Returns:
375 * 0 Success, output of length @outlenp stored in the buffer at @out
376 * -ENODEV Hardware unavailable
377 * -ENOSPC Output buffer is to small
378 * -EMSGSIZE Input buffer too large
379 * -EINVAL buffer constraints do not fix nx842_constraints
380 * -EPROTO hardware error during operation
381 * -ETIMEDOUT hardware did not complete operation in reasonable time
382 * -EINTR operation was aborted
383 */
384static int nx842_powernv_function(const unsigned char *in, unsigned int inlen,
385 unsigned char *out, unsigned int *outlenp,
386 void *workmem, int fc)
387{
388 struct coprocessor_request_block *crb;
389 struct coprocessor_status_block *csb;
390 struct nx842_workmem *wmem;
391 int ret;
392 u64 csb_addr;
393 u32 ccw;
394 unsigned int outlen = *outlenp;
395
396 wmem = PTR_ALIGN(workmem, WORKMEM_ALIGN);
397
398 *outlenp = 0;
399
400 /* shoudn't happen, we don't load without a coproc */
401 if (!nx842_ct) {
402 pr_err_ratelimited("coprocessor CT is 0");
403 return -ENODEV;
404 }
405
406 crb = &wmem->crb;
407 csb = &crb->csb;
408
409 /* Clear any previous values */
410 memset(crb, 0, sizeof(*crb));
411
412 /* set up DDLs */
413 ret = setup_ddl(&crb->source, wmem->ddl_in,
414 (unsigned char *)in, inlen, true);
415 if (ret)
416 return ret;
417 ret = setup_ddl(&crb->target, wmem->ddl_out,
418 out, outlen, false);
419 if (ret)
420 return ret;
421
422 /* set up CCW */
423 ccw = 0;
424 ccw = SET_FIELD(ccw, CCW_CT, nx842_ct);
425 ccw = SET_FIELD(ccw, CCW_CI_842, 0); /* use 0 for hw auto-selection */
426 ccw = SET_FIELD(ccw, CCW_FC_842, fc);
427
428 /* set up CRB's CSB addr */
429 csb_addr = nx842_get_pa(csb) & CRB_CSB_ADDRESS;
430 csb_addr |= CRB_CSB_AT; /* Addrs are phys */
431 crb->csb_addr = cpu_to_be64(csb_addr);
432
433 wmem->start = ktime_get();
434
435 /* do ICSWX */
436 ret = icswx(cpu_to_be32(ccw), crb);
437
438 pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret,
439 (unsigned int)ccw,
440 (unsigned int)be32_to_cpu(crb->ccw));
441
442 switch (ret) {
443 case ICSWX_INITIATED:
444 ret = wait_for_csb(wmem, csb);
445 break;
446 case ICSWX_BUSY:
447 pr_debug_ratelimited("842 Coprocessor busy\n");
448 ret = -EBUSY;
449 break;
450 case ICSWX_REJECTED:
451 pr_err_ratelimited("ICSWX rejected\n");
452 ret = -EPROTO;
453 break;
454 default:
455 pr_err_ratelimited("Invalid ICSWX return code %x\n", ret);
456 ret = -EPROTO;
457 break;
458 }
459
460 if (!ret)
461 *outlenp = be32_to_cpu(csb->count);
462
463 return ret;
464}
465
466/**
467 * nx842_powernv_compress - Compress data using the 842 algorithm
468 *
469 * Compression provided by the NX842 coprocessor on IBM PowerNV systems.
470 * The input buffer is compressed and the result is stored in the
471 * provided output buffer.
472 *
473 * Upon return from this function @outlen contains the length of the
474 * compressed data. If there is an error then @outlen will be 0 and an
475 * error will be specified by the return code from this function.
476 *
477 * @in: input buffer pointer
478 * @inlen: input buffer size
479 * @out: output buffer pointer
480 * @outlenp: output buffer size pointer
2c6f6eab
DS
481 * @workmem: working memory buffer pointer, size determined by
482 * nx842_powernv_driver.workmem_size
99182a42
DS
483 *
484 * Returns: see @nx842_powernv_function()
485 */
486static int nx842_powernv_compress(const unsigned char *in, unsigned int inlen,
487 unsigned char *out, unsigned int *outlenp,
488 void *wmem)
489{
490 return nx842_powernv_function(in, inlen, out, outlenp,
491 wmem, CCW_FC_842_COMP_NOCRC);
492}
493
494/**
495 * nx842_powernv_decompress - Decompress data using the 842 algorithm
496 *
497 * Decompression provided by the NX842 coprocessor on IBM PowerNV systems.
498 * The input buffer is decompressed and the result is stored in the
499 * provided output buffer.
500 *
501 * Upon return from this function @outlen contains the length of the
502 * decompressed data. If there is an error then @outlen will be 0 and an
503 * error will be specified by the return code from this function.
504 *
505 * @in: input buffer pointer
506 * @inlen: input buffer size
507 * @out: output buffer pointer
508 * @outlenp: output buffer size pointer
2c6f6eab
DS
509 * @workmem: working memory buffer pointer, size determined by
510 * nx842_powernv_driver.workmem_size
99182a42
DS
511 *
512 * Returns: see @nx842_powernv_function()
513 */
514static int nx842_powernv_decompress(const unsigned char *in, unsigned int inlen,
515 unsigned char *out, unsigned int *outlenp,
516 void *wmem)
517{
518 return nx842_powernv_function(in, inlen, out, outlenp,
519 wmem, CCW_FC_842_DECOMP_NOCRC);
520}
521
522static int __init nx842_powernv_probe(struct device_node *dn)
523{
524 struct nx842_coproc *coproc;
525 struct property *ct_prop, *ci_prop;
526 unsigned int ct, ci;
527 int chip_id;
528
529 chip_id = of_get_ibm_chip_id(dn);
530 if (chip_id < 0) {
531 pr_err("ibm,chip-id missing\n");
532 return -EINVAL;
533 }
534 ct_prop = of_find_property(dn, "ibm,842-coprocessor-type", NULL);
535 if (!ct_prop) {
536 pr_err("ibm,842-coprocessor-type missing\n");
537 return -EINVAL;
538 }
539 ct = be32_to_cpu(*(unsigned int *)ct_prop->value);
540 ci_prop = of_find_property(dn, "ibm,842-coprocessor-instance", NULL);
541 if (!ci_prop) {
542 pr_err("ibm,842-coprocessor-instance missing\n");
543 return -EINVAL;
544 }
545 ci = be32_to_cpu(*(unsigned int *)ci_prop->value);
546
547 coproc = kmalloc(sizeof(*coproc), GFP_KERNEL);
548 if (!coproc)
549 return -ENOMEM;
550
551 coproc->chip_id = chip_id;
552 coproc->ct = ct;
553 coproc->ci = ci;
554 INIT_LIST_HEAD(&coproc->list);
555 list_add(&coproc->list, &nx842_coprocs);
556
557 pr_info("coprocessor found on chip %d, CT %d CI %d\n", chip_id, ct, ci);
558
559 if (!nx842_ct)
560 nx842_ct = ct;
561 else if (nx842_ct != ct)
562 pr_err("NX842 chip %d, CT %d != first found CT %d\n",
563 chip_id, ct, nx842_ct);
564
565 return 0;
566}
567
568static struct nx842_constraints nx842_powernv_constraints = {
569 .alignment = DDE_BUFFER_ALIGN,
570 .multiple = DDE_BUFFER_LAST_MULT,
571 .minimum = DDE_BUFFER_LAST_MULT,
572 .maximum = (DDL_LEN_MAX - 1) * PAGE_SIZE,
573};
574
575static struct nx842_driver nx842_powernv_driver = {
3e648cbe 576 .name = KBUILD_MODNAME,
99182a42 577 .owner = THIS_MODULE,
2c6f6eab 578 .workmem_size = sizeof(struct nx842_workmem),
99182a42
DS
579 .constraints = &nx842_powernv_constraints,
580 .compress = nx842_powernv_compress,
581 .decompress = nx842_powernv_decompress,
582};
583
584static __init int nx842_powernv_init(void)
585{
586 struct device_node *dn;
587
588 /* verify workmem size/align restrictions */
99182a42
DS
589 BUILD_BUG_ON(WORKMEM_ALIGN % CRB_ALIGN);
590 BUILD_BUG_ON(CRB_ALIGN % DDE_ALIGN);
591 BUILD_BUG_ON(CRB_SIZE % DDE_ALIGN);
592 /* verify buffer size/align restrictions */
593 BUILD_BUG_ON(PAGE_SIZE % DDE_BUFFER_ALIGN);
594 BUILD_BUG_ON(DDE_BUFFER_ALIGN % DDE_BUFFER_SIZE_MULT);
595 BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT % DDE_BUFFER_LAST_MULT);
596
3e648cbe 597 for_each_compatible_node(dn, NULL, "ibm,power-nx")
99182a42
DS
598 nx842_powernv_probe(dn);
599
2b93f7ee 600 if (!nx842_ct)
99182a42 601 return -ENODEV;
99182a42 602
3e648cbe
DS
603 if (!nx842_platform_driver_set(&nx842_powernv_driver)) {
604 struct nx842_coproc *coproc, *n;
605
606 list_for_each_entry_safe(coproc, n, &nx842_coprocs, list) {
607 list_del(&coproc->list);
608 kfree(coproc);
609 }
610
611 return -EEXIST;
612 }
99182a42 613
99182a42
DS
614 return 0;
615}
616module_init(nx842_powernv_init);
617
618static void __exit nx842_powernv_exit(void)
619{
620 struct nx842_coproc *coproc, *n;
621
3e648cbe 622 nx842_platform_driver_unset(&nx842_powernv_driver);
99182a42
DS
623
624 list_for_each_entry_safe(coproc, n, &nx842_coprocs, list) {
625 list_del(&coproc->list);
626 kfree(coproc);
627 }
99182a42
DS
628}
629module_exit(nx842_powernv_exit);