blob: dbf80b55c2a4209ef8b8561167fb4a3c0bd3bc90 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#include <asm/vas.h>
26#include <asm/reg.h>
27
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
30MODULE_DESCRIPTION("842 H/W Compression driver for IBM PowerNV processors");
31MODULE_ALIAS_CRYPTO("842");
32MODULE_ALIAS_CRYPTO("842-nx");
33
34#define WORKMEM_ALIGN (CRB_ALIGN)
35#define CSB_WAIT_MAX (5000) /* ms */
36#define VAS_RETRIES (10)
37
38struct nx842_workmem {
39 /* Below fields must be properly aligned */
40 struct coprocessor_request_block crb; /* CRB_ALIGN align */
41 struct data_descriptor_entry ddl_in[DDL_LEN_MAX]; /* DDE_ALIGN align */
42 struct data_descriptor_entry ddl_out[DDL_LEN_MAX]; /* DDE_ALIGN align */
43 /* Above fields must be properly aligned */
44
45 ktime_t start;
46
47 struct vas_window *txwin; /* Used with VAS function */
48 char padding[WORKMEM_ALIGN]; /* unused, to allow alignment */
49} __packed __aligned(WORKMEM_ALIGN);
50
51struct nx842_coproc {
52 unsigned int chip_id;
53 unsigned int ct;
54 unsigned int ci; /* Coprocessor instance, used with icswx */
55 struct {
56 struct vas_window *rxwin;
57 int id;
58 } vas;
59 struct list_head list;
60};
61
62/*
63 * Send the request to NX engine on the chip for the corresponding CPU
64 * where the process is executing. Use with VAS function.
65 */
66static DEFINE_PER_CPU(struct nx842_coproc *, coproc_inst);
67
68/* no cpu hotplug on powernv, so this list never changes after init */
69static LIST_HEAD(nx842_coprocs);
70static unsigned int nx842_ct; /* used in icswx function */
71
72static int (*nx842_powernv_exec)(const unsigned char *in,
73 unsigned int inlen, unsigned char *out,
74 unsigned int *outlenp, void *workmem, int fc);
75
76/**
77 * setup_indirect_dde - Setup an indirect DDE
78 *
79 * The DDE is setup with the the DDE count, byte count, and address of
80 * first direct DDE in the list.
81 */
82static void setup_indirect_dde(struct data_descriptor_entry *dde,
83 struct data_descriptor_entry *ddl,
84 unsigned int dde_count, unsigned int byte_count)
85{
86 dde->flags = 0;
87 dde->count = dde_count;
88 dde->index = 0;
89 dde->length = cpu_to_be32(byte_count);
90 dde->address = cpu_to_be64(nx842_get_pa(ddl));
91}
92
93/**
94 * setup_direct_dde - Setup single DDE from buffer
95 *
96 * The DDE is setup with the buffer and length. The buffer must be properly
97 * aligned. The used length is returned.
98 * Returns:
99 * N Successfully set up DDE with N bytes
100 */
101static unsigned int setup_direct_dde(struct data_descriptor_entry *dde,
102 unsigned long pa, unsigned int len)
103{
104 unsigned int l = min_t(unsigned int, len, LEN_ON_PAGE(pa));
105
106 dde->flags = 0;
107 dde->count = 0;
108 dde->index = 0;
109 dde->length = cpu_to_be32(l);
110 dde->address = cpu_to_be64(pa);
111
112 return l;
113}
114
115/**
116 * setup_ddl - Setup DDL from buffer
117 *
118 * Returns:
119 * 0 Successfully set up DDL
120 */
121static int setup_ddl(struct data_descriptor_entry *dde,
122 struct data_descriptor_entry *ddl,
123 unsigned char *buf, unsigned int len,
124 bool in)
125{
126 unsigned long pa = nx842_get_pa(buf);
127 int i, ret, total_len = len;
128
129 if (!IS_ALIGNED(pa, DDE_BUFFER_ALIGN)) {
130 pr_debug("%s buffer pa 0x%lx not 0x%x-byte aligned\n",
131 in ? "input" : "output", pa, DDE_BUFFER_ALIGN);
132 return -EINVAL;
133 }
134
135 /* only need to check last mult; since buffer must be
136 * DDE_BUFFER_ALIGN aligned, and that is a multiple of
137 * DDE_BUFFER_SIZE_MULT, and pre-last page DDE buffers
138 * are guaranteed a multiple of DDE_BUFFER_SIZE_MULT.
139 */
140 if (len % DDE_BUFFER_LAST_MULT) {
141 pr_debug("%s buffer len 0x%x not a multiple of 0x%x\n",
142 in ? "input" : "output", len, DDE_BUFFER_LAST_MULT);
143 if (in)
144 return -EINVAL;
145 len = round_down(len, DDE_BUFFER_LAST_MULT);
146 }
147
148 /* use a single direct DDE */
149 if (len <= LEN_ON_PAGE(pa)) {
150 ret = setup_direct_dde(dde, pa, len);
151 WARN_ON(ret < len);
152 return 0;
153 }
154
155 /* use the DDL */
156 for (i = 0; i < DDL_LEN_MAX && len > 0; i++) {
157 ret = setup_direct_dde(&ddl[i], pa, len);
158 buf += ret;
159 len -= ret;
160 pa = nx842_get_pa(buf);
161 }
162
163 if (len > 0) {
164 pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n",
165 total_len, in ? "input" : "output", len);
166 if (in)
167 return -EMSGSIZE;
168 total_len -= len;
169 }
170 setup_indirect_dde(dde, ddl, i, total_len);
171
172 return 0;
173}
174
175#define CSB_ERR(csb, msg, ...) \
176 pr_err("ERROR: " msg " : %02x %02x %02x %02x %08x\n", \
177 ##__VA_ARGS__, (csb)->flags, \
178 (csb)->cs, (csb)->cc, (csb)->ce, \
179 be32_to_cpu((csb)->count))
180
181#define CSB_ERR_ADDR(csb, msg, ...) \
182 CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \
183 (unsigned long)be64_to_cpu((csb)->address))
184
185/**
186 * wait_for_csb
187 */
188static int wait_for_csb(struct nx842_workmem *wmem,
189 struct coprocessor_status_block *csb)
190{
191 ktime_t start = wmem->start, now = ktime_get();
192 ktime_t timeout = ktime_add_ms(start, CSB_WAIT_MAX);
193
194 while (!(ACCESS_ONCE(csb->flags) & CSB_V)) {
195 cpu_relax();
196 now = ktime_get();
197 if (ktime_after(now, timeout))
198 break;
199 }
200
201 /* hw has updated csb and output buffer */
202 barrier();
203
204 /* check CSB flags */
205 if (!(csb->flags & CSB_V)) {
206 CSB_ERR(csb, "CSB still not valid after %ld us, giving up",
207 (long)ktime_us_delta(now, start));
208 return -ETIMEDOUT;
209 }
210 if (csb->flags & CSB_F) {
211 CSB_ERR(csb, "Invalid CSB format");
212 return -EPROTO;
213 }
214 if (csb->flags & CSB_CH) {
215 CSB_ERR(csb, "Invalid CSB chaining state");
216 return -EPROTO;
217 }
218
219 /* verify CSB completion sequence is 0 */
220 if (csb->cs) {
221 CSB_ERR(csb, "Invalid CSB completion sequence");
222 return -EPROTO;
223 }
224
225 /* check CSB Completion Code */
226 switch (csb->cc) {
227 /* no error */
228 case CSB_CC_SUCCESS:
229 break;
230 case CSB_CC_TPBC_GT_SPBC:
231 /* not an error, but the compressed data is
232 * larger than the uncompressed data :(
233 */
234 break;
235
236 /* input data errors */
237 case CSB_CC_OPERAND_OVERLAP:
238 /* input and output buffers overlap */
239 CSB_ERR(csb, "Operand Overlap error");
240 return -EINVAL;
241 case CSB_CC_INVALID_OPERAND:
242 CSB_ERR(csb, "Invalid operand");
243 return -EINVAL;
244 case CSB_CC_NOSPC:
245 /* output buffer too small */
246 return -ENOSPC;
247 case CSB_CC_ABORT:
248 CSB_ERR(csb, "Function aborted");
249 return -EINTR;
250 case CSB_CC_CRC_MISMATCH:
251 CSB_ERR(csb, "CRC mismatch");
252 return -EINVAL;
253 case CSB_CC_TEMPL_INVALID:
254 CSB_ERR(csb, "Compressed data template invalid");
255 return -EINVAL;
256 case CSB_CC_TEMPL_OVERFLOW:
257 CSB_ERR(csb, "Compressed data template shows data past end");
258 return -EINVAL;
259 case CSB_CC_EXCEED_BYTE_COUNT: /* P9 or later */
260 /*
261 * DDE byte count exceeds the limit specified in Maximum
262 * byte count register.
263 */
264 CSB_ERR(csb, "DDE byte count exceeds the limit");
265 return -EINVAL;
266
267 /* these should not happen */
268 case CSB_CC_INVALID_ALIGN:
269 /* setup_ddl should have detected this */
270 CSB_ERR_ADDR(csb, "Invalid alignment");
271 return -EINVAL;
272 case CSB_CC_DATA_LENGTH:
273 /* setup_ddl should have detected this */
274 CSB_ERR(csb, "Invalid data length");
275 return -EINVAL;
276 case CSB_CC_WR_TRANSLATION:
277 case CSB_CC_TRANSLATION:
278 case CSB_CC_TRANSLATION_DUP1:
279 case CSB_CC_TRANSLATION_DUP2:
280 case CSB_CC_TRANSLATION_DUP3:
281 case CSB_CC_TRANSLATION_DUP4:
282 case CSB_CC_TRANSLATION_DUP5:
283 case CSB_CC_TRANSLATION_DUP6:
284 /* should not happen, we use physical addrs */
285 CSB_ERR_ADDR(csb, "Translation error");
286 return -EPROTO;
287 case CSB_CC_WR_PROTECTION:
288 case CSB_CC_PROTECTION:
289 case CSB_CC_PROTECTION_DUP1:
290 case CSB_CC_PROTECTION_DUP2:
291 case CSB_CC_PROTECTION_DUP3:
292 case CSB_CC_PROTECTION_DUP4:
293 case CSB_CC_PROTECTION_DUP5:
294 case CSB_CC_PROTECTION_DUP6:
295 /* should not happen, we use physical addrs */
296 CSB_ERR_ADDR(csb, "Protection error");
297 return -EPROTO;
298 case CSB_CC_PRIVILEGE:
299 /* shouldn't happen, we're in HYP mode */
300 CSB_ERR(csb, "Insufficient Privilege error");
301 return -EPROTO;
302 case CSB_CC_EXCESSIVE_DDE:
303 /* shouldn't happen, setup_ddl doesn't use many dde's */
304 CSB_ERR(csb, "Too many DDEs in DDL");
305 return -EINVAL;
306 case CSB_CC_TRANSPORT:
307 case CSB_CC_INVALID_CRB: /* P9 or later */
308 /* shouldn't happen, we setup CRB correctly */
309 CSB_ERR(csb, "Invalid CRB");
310 return -EINVAL;
311 case CSB_CC_INVALID_DDE: /* P9 or later */
312 /*
313 * shouldn't happen, setup_direct/indirect_dde creates
314 * DDE right
315 */
316 CSB_ERR(csb, "Invalid DDE");
317 return -EINVAL;
318 case CSB_CC_SEGMENTED_DDL:
319 /* shouldn't happen, setup_ddl creates DDL right */
320 CSB_ERR(csb, "Segmented DDL error");
321 return -EINVAL;
322 case CSB_CC_DDE_OVERFLOW:
323 /* shouldn't happen, setup_ddl creates DDL right */
324 CSB_ERR(csb, "DDE overflow error");
325 return -EINVAL;
326 case CSB_CC_SESSION:
327 /* should not happen with ICSWX */
328 CSB_ERR(csb, "Session violation error");
329 return -EPROTO;
330 case CSB_CC_CHAIN:
331 /* should not happen, we don't use chained CRBs */
332 CSB_ERR(csb, "Chained CRB error");
333 return -EPROTO;
334 case CSB_CC_SEQUENCE:
335 /* should not happen, we don't use chained CRBs */
336 CSB_ERR(csb, "CRB seqeunce number error");
337 return -EPROTO;
338 case CSB_CC_UNKNOWN_CODE:
339 CSB_ERR(csb, "Unknown subfunction code");
340 return -EPROTO;
341
342 /* hardware errors */
343 case CSB_CC_RD_EXTERNAL:
344 case CSB_CC_RD_EXTERNAL_DUP1:
345 case CSB_CC_RD_EXTERNAL_DUP2:
346 case CSB_CC_RD_EXTERNAL_DUP3:
347 CSB_ERR_ADDR(csb, "Read error outside coprocessor");
348 return -EPROTO;
349 case CSB_CC_WR_EXTERNAL:
350 CSB_ERR_ADDR(csb, "Write error outside coprocessor");
351 return -EPROTO;
352 case CSB_CC_INTERNAL:
353 CSB_ERR(csb, "Internal error in coprocessor");
354 return -EPROTO;
355 case CSB_CC_PROVISION:
356 CSB_ERR(csb, "Storage provision error");
357 return -EPROTO;
358 case CSB_CC_HW:
359 CSB_ERR(csb, "Correctable hardware error");
360 return -EPROTO;
361 case CSB_CC_HW_EXPIRED_TIMER: /* P9 or later */
362 CSB_ERR(csb, "Job did not finish within allowed time");
363 return -EPROTO;
364
365 default:
366 CSB_ERR(csb, "Invalid CC %d", csb->cc);
367 return -EPROTO;
368 }
369
370 /* check Completion Extension state */
371 if (csb->ce & CSB_CE_TERMINATION) {
372 CSB_ERR(csb, "CSB request was terminated");
373 return -EPROTO;
374 }
375 if (csb->ce & CSB_CE_INCOMPLETE) {
376 CSB_ERR(csb, "CSB request not complete");
377 return -EPROTO;
378 }
379 if (!(csb->ce & CSB_CE_TPBC)) {
380 CSB_ERR(csb, "TPBC not provided, unknown target length");
381 return -EPROTO;
382 }
383
384 /* successful completion */
385 pr_debug_ratelimited("Processed %u bytes in %lu us\n",
386 be32_to_cpu(csb->count),
387 (unsigned long)ktime_us_delta(now, start));
388
389 return 0;
390}
391
392static int nx842_config_crb(const unsigned char *in, unsigned int inlen,
393 unsigned char *out, unsigned int outlen,
394 struct nx842_workmem *wmem)
395{
396 struct coprocessor_request_block *crb;
397 struct coprocessor_status_block *csb;
398 u64 csb_addr;
399 int ret;
400
401 crb = &wmem->crb;
402 csb = &crb->csb;
403
404 /* Clear any previous values */
405 memset(crb, 0, sizeof(*crb));
406
407 /* set up DDLs */
408 ret = setup_ddl(&crb->source, wmem->ddl_in,
409 (unsigned char *)in, inlen, true);
410 if (ret)
411 return ret;
412
413 ret = setup_ddl(&crb->target, wmem->ddl_out,
414 out, outlen, false);
415 if (ret)
416 return ret;
417
418 /* set up CRB's CSB addr */
419 csb_addr = nx842_get_pa(csb) & CRB_CSB_ADDRESS;
420 csb_addr |= CRB_CSB_AT; /* Addrs are phys */
421 crb->csb_addr = cpu_to_be64(csb_addr);
422
423 return 0;
424}
425
426/**
427 * nx842_exec_icswx - compress/decompress data using the 842 algorithm
428 *
429 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
430 * This compresses or decompresses the provided input buffer into the provided
431 * output buffer.
432 *
433 * Upon return from this function @outlen contains the length of the
434 * output data. If there is an error then @outlen will be 0 and an
435 * error will be specified by the return code from this function.
436 *
437 * The @workmem buffer should only be used by one function call at a time.
438 *
439 * @in: input buffer pointer
440 * @inlen: input buffer size
441 * @out: output buffer pointer
442 * @outlenp: output buffer size pointer
443 * @workmem: working memory buffer pointer, size determined by
444 * nx842_powernv_driver.workmem_size
445 * @fc: function code, see CCW Function Codes in nx-842.h
446 *
447 * Returns:
448 * 0 Success, output of length @outlenp stored in the buffer at @out
449 * -ENODEV Hardware unavailable
450 * -ENOSPC Output buffer is to small
451 * -EMSGSIZE Input buffer too large
452 * -EINVAL buffer constraints do not fix nx842_constraints
453 * -EPROTO hardware error during operation
454 * -ETIMEDOUT hardware did not complete operation in reasonable time
455 * -EINTR operation was aborted
456 */
457static int nx842_exec_icswx(const unsigned char *in, unsigned int inlen,
458 unsigned char *out, unsigned int *outlenp,
459 void *workmem, int fc)
460{
461 struct coprocessor_request_block *crb;
462 struct coprocessor_status_block *csb;
463 struct nx842_workmem *wmem;
464 int ret;
465 u32 ccw;
466 unsigned int outlen = *outlenp;
467
468 wmem = PTR_ALIGN(workmem, WORKMEM_ALIGN);
469
470 *outlenp = 0;
471
472 /* shoudn't happen, we don't load without a coproc */
473 if (!nx842_ct) {
474 pr_err_ratelimited("coprocessor CT is 0");
475 return -ENODEV;
476 }
477
478 ret = nx842_config_crb(in, inlen, out, outlen, wmem);
479 if (ret)
480 return ret;
481
482 crb = &wmem->crb;
483 csb = &crb->csb;
484
485 /* set up CCW */
486 ccw = 0;
487 ccw = SET_FIELD(CCW_CT, ccw, nx842_ct);
488 ccw = SET_FIELD(CCW_CI_842, ccw, 0); /* use 0 for hw auto-selection */
489 ccw = SET_FIELD(CCW_FC_842, ccw, fc);
490
491 wmem->start = ktime_get();
492
493 /* do ICSWX */
494 ret = icswx(cpu_to_be32(ccw), crb);
495
496 pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret,
497 (unsigned int)ccw,
498 (unsigned int)be32_to_cpu(crb->ccw));
499
500 /*
501 * NX842 coprocessor sets 3rd bit in CR register with XER[S0].
502 * XER[S0] is the integer summary overflow bit which is nothing
503 * to do NX. Since this bit can be set with other return values,
504 * mask this bit.
505 */
506 ret &= ~ICSWX_XERS0;
507
508 switch (ret) {
509 case ICSWX_INITIATED:
510 ret = wait_for_csb(wmem, csb);
511 break;
512 case ICSWX_BUSY:
513 pr_debug_ratelimited("842 Coprocessor busy\n");
514 ret = -EBUSY;
515 break;
516 case ICSWX_REJECTED:
517 pr_err_ratelimited("ICSWX rejected\n");
518 ret = -EPROTO;
519 break;
520 }
521
522 if (!ret)
523 *outlenp = be32_to_cpu(csb->count);
524
525 return ret;
526}
527
528/**
529 * nx842_exec_vas - compress/decompress data using the 842 algorithm
530 *
531 * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems.
532 * This compresses or decompresses the provided input buffer into the provided
533 * output buffer.
534 *
535 * Upon return from this function @outlen contains the length of the
536 * output data. If there is an error then @outlen will be 0 and an
537 * error will be specified by the return code from this function.
538 *
539 * The @workmem buffer should only be used by one function call at a time.
540 *
541 * @in: input buffer pointer
542 * @inlen: input buffer size
543 * @out: output buffer pointer
544 * @outlenp: output buffer size pointer
545 * @workmem: working memory buffer pointer, size determined by
546 * nx842_powernv_driver.workmem_size
547 * @fc: function code, see CCW Function Codes in nx-842.h
548 *
549 * Returns:
550 * 0 Success, output of length @outlenp stored in the buffer
551 * at @out
552 * -ENODEV Hardware unavailable
553 * -ENOSPC Output buffer is to small
554 * -EMSGSIZE Input buffer too large
555 * -EINVAL buffer constraints do not fix nx842_constraints
556 * -EPROTO hardware error during operation
557 * -ETIMEDOUT hardware did not complete operation in reasonable time
558 * -EINTR operation was aborted
559 */
560static int nx842_exec_vas(const unsigned char *in, unsigned int inlen,
561 unsigned char *out, unsigned int *outlenp,
562 void *workmem, int fc)
563{
564 struct coprocessor_request_block *crb;
565 struct coprocessor_status_block *csb;
566 struct nx842_workmem *wmem;
567 struct vas_window *txwin;
568 int ret, i = 0;
569 u32 ccw;
570 unsigned int outlen = *outlenp;
571
572 wmem = PTR_ALIGN(workmem, WORKMEM_ALIGN);
573
574 *outlenp = 0;
575
576 crb = &wmem->crb;
577 csb = &crb->csb;
578
579 ret = nx842_config_crb(in, inlen, out, outlen, wmem);
580 if (ret)
581 return ret;
582
583 ccw = 0;
584 ccw = SET_FIELD(CCW_FC_842, ccw, fc);
585 crb->ccw = cpu_to_be32(ccw);
586
587 txwin = wmem->txwin;
588 /* shoudn't happen, we don't load without a coproc */
589 if (!txwin) {
590 pr_err_ratelimited("NX-842 coprocessor is not available");
591 return -ENODEV;
592 }
593
594 do {
595 wmem->start = ktime_get();
596 preempt_disable();
597 /*
598 * VAS copy CRB into L2 cache. Refer <asm/vas.h>.
599 * @crb and @offset.
600 */
601 vas_copy_crb(crb, 0);
602
603 /*
604 * VAS paste previously copied CRB to NX.
605 * @txwin, @offset and @last (must be true).
606 */
607 ret = vas_paste_crb(txwin, 0, 1);
608 preempt_enable();
609 /*
610 * Retry copy/paste function for VAS failures.
611 */
612 } while (ret && (i++ < VAS_RETRIES));
613
614 if (ret) {
615 pr_err_ratelimited("VAS copy/paste failed\n");
616 return ret;
617 }
618
619 ret = wait_for_csb(wmem, csb);
620 if (!ret)
621 *outlenp = be32_to_cpu(csb->count);
622
623 return ret;
624}
625
626/**
627 * nx842_powernv_compress - Compress data using the 842 algorithm
628 *
629 * Compression provided by the NX842 coprocessor on IBM PowerNV systems.
630 * The input buffer is compressed and the result is stored in the
631 * provided output buffer.
632 *
633 * Upon return from this function @outlen contains the length of the
634 * compressed data. If there is an error then @outlen will be 0 and an
635 * error will be specified by the return code from this function.
636 *
637 * @in: input buffer pointer
638 * @inlen: input buffer size
639 * @out: output buffer pointer
640 * @outlenp: output buffer size pointer
641 * @workmem: working memory buffer pointer, size determined by
642 * nx842_powernv_driver.workmem_size
643 *
644 * Returns: see @nx842_powernv_exec()
645 */
646static int nx842_powernv_compress(const unsigned char *in, unsigned int inlen,
647 unsigned char *out, unsigned int *outlenp,
648 void *wmem)
649{
650 return nx842_powernv_exec(in, inlen, out, outlenp,
651 wmem, CCW_FC_842_COMP_CRC);
652}
653
654/**
655 * nx842_powernv_decompress - Decompress data using the 842 algorithm
656 *
657 * Decompression provided by the NX842 coprocessor on IBM PowerNV systems.
658 * The input buffer is decompressed and the result is stored in the
659 * provided output buffer.
660 *
661 * Upon return from this function @outlen contains the length of the
662 * decompressed data. If there is an error then @outlen will be 0 and an
663 * error will be specified by the return code from this function.
664 *
665 * @in: input buffer pointer
666 * @inlen: input buffer size
667 * @out: output buffer pointer
668 * @outlenp: output buffer size pointer
669 * @workmem: working memory buffer pointer, size determined by
670 * nx842_powernv_driver.workmem_size
671 *
672 * Returns: see @nx842_powernv_exec()
673 */
674static int nx842_powernv_decompress(const unsigned char *in, unsigned int inlen,
675 unsigned char *out, unsigned int *outlenp,
676 void *wmem)
677{
678 return nx842_powernv_exec(in, inlen, out, outlenp,
679 wmem, CCW_FC_842_DECOMP_CRC);
680}
681
682static inline void nx842_add_coprocs_list(struct nx842_coproc *coproc,
683 int chipid)
684{
685 coproc->chip_id = chipid;
686 INIT_LIST_HEAD(&coproc->list);
687 list_add(&coproc->list, &nx842_coprocs);
688}
689
690/*
691 * Identify chip ID for each CPU and save coprocesor adddress for the
692 * corresponding NX engine in percpu coproc_inst.
693 * coproc_inst is used in crypto_init to open send window on the NX instance
694 * for the corresponding CPU / chip where the open request is executed.
695 */
696static void nx842_set_per_cpu_coproc(struct nx842_coproc *coproc)
697{
698 unsigned int i, chip_id;
699
700 for_each_possible_cpu(i) {
701 chip_id = cpu_to_chip_id(i);
702
703 if (coproc->chip_id == chip_id)
704 per_cpu(coproc_inst, i) = coproc;
705 }
706}
707
708
709static struct vas_window *nx842_alloc_txwin(struct nx842_coproc *coproc)
710{
711 struct vas_window *txwin = NULL;
712 struct vas_tx_win_attr txattr;
713
714 /*
715 * Kernel requests will be high priority. So open send
716 * windows only for high priority RxFIFO entries.
717 */
718 vas_init_tx_win_attr(&txattr, coproc->ct);
719 txattr.lpid = 0; /* lpid is 0 for kernel requests */
720 txattr.pid = 0; /* pid is 0 for kernel requests */
721
722 /*
723 * Open a VAS send window which is used to send request to NX.
724 */
725 txwin = vas_tx_win_open(coproc->vas.id, coproc->ct, &txattr);
726 if (IS_ERR(txwin)) {
727 pr_err("ibm,nx-842: Can not open TX window: %ld\n",
728 PTR_ERR(txwin));
729 return NULL;
730 }
731
732 return txwin;
733}
734
735static int __init vas_cfg_coproc_info(struct device_node *dn, int chip_id,
736 int vasid)
737{
738 struct vas_window *rxwin = NULL;
739 struct vas_rx_win_attr rxattr;
740 struct nx842_coproc *coproc;
741 u32 lpid, pid, tid, fifo_size;
742 u64 rx_fifo;
743 const char *priority;
744 int ret;
745
746 ret = of_property_read_u64(dn, "rx-fifo-address", &rx_fifo);
747 if (ret) {
748 pr_err("Missing rx-fifo-address property\n");
749 return ret;
750 }
751
752 ret = of_property_read_u32(dn, "rx-fifo-size", &fifo_size);
753 if (ret) {
754 pr_err("Missing rx-fifo-size property\n");
755 return ret;
756 }
757
758 ret = of_property_read_u32(dn, "lpid", &lpid);
759 if (ret) {
760 pr_err("Missing lpid property\n");
761 return ret;
762 }
763
764 ret = of_property_read_u32(dn, "pid", &pid);
765 if (ret) {
766 pr_err("Missing pid property\n");
767 return ret;
768 }
769
770 ret = of_property_read_u32(dn, "tid", &tid);
771 if (ret) {
772 pr_err("Missing tid property\n");
773 return ret;
774 }
775
776 ret = of_property_read_string(dn, "priority", &priority);
777 if (ret) {
778 pr_err("Missing priority property\n");
779 return ret;
780 }
781
782 coproc = kzalloc(sizeof(*coproc), GFP_KERNEL);
783 if (!coproc)
784 return -ENOMEM;
785
786 if (!strcmp(priority, "High"))
787 coproc->ct = VAS_COP_TYPE_842_HIPRI;
788 else if (!strcmp(priority, "Normal"))
789 coproc->ct = VAS_COP_TYPE_842;
790 else {
791 pr_err("Invalid RxFIFO priority value\n");
792 ret = -EINVAL;
793 goto err_out;
794 }
795
796 vas_init_rx_win_attr(&rxattr, coproc->ct);
797 rxattr.rx_fifo = (void *)rx_fifo;
798 rxattr.rx_fifo_size = fifo_size;
799 rxattr.lnotify_lpid = lpid;
800 rxattr.lnotify_pid = pid;
801 rxattr.lnotify_tid = tid;
802 /*
803 * Maximum RX window credits can not be more than #CRBs in
804 * RxFIFO. Otherwise, can get checkstop if RxFIFO overruns.
805 */
806 rxattr.wcreds_max = fifo_size / CRB_SIZE;
807
808 /*
809 * Open a VAS receice window which is used to configure RxFIFO
810 * for NX.
811 */
812 rxwin = vas_rx_win_open(vasid, coproc->ct, &rxattr);
813 if (IS_ERR(rxwin)) {
814 ret = PTR_ERR(rxwin);
815 pr_err("setting RxFIFO with VAS failed: %d\n",
816 ret);
817 goto err_out;
818 }
819
820 coproc->vas.rxwin = rxwin;
821 coproc->vas.id = vasid;
822 nx842_add_coprocs_list(coproc, chip_id);
823
824 /*
825 * Kernel requests use only high priority FIFOs. So save coproc
826 * info in percpu coproc_inst which will be used to open send
827 * windows for crypto open requests later.
828 */
829 if (coproc->ct == VAS_COP_TYPE_842_HIPRI)
830 nx842_set_per_cpu_coproc(coproc);
831
832 return 0;
833
834err_out:
835 kfree(coproc);
836 return ret;
837}
838
839
840static int __init nx842_powernv_probe_vas(struct device_node *pn)
841{
842 struct device_node *dn;
843 int chip_id, vasid, ret = 0;
844 int nx_fifo_found = 0;
845
846 chip_id = of_get_ibm_chip_id(pn);
847 if (chip_id < 0) {
848 pr_err("ibm,chip-id missing\n");
849 return -EINVAL;
850 }
851
852 for_each_compatible_node(dn, NULL, "ibm,power9-vas-x") {
853 if (of_get_ibm_chip_id(dn) == chip_id)
854 break;
855 }
856
857 if (!dn) {
858 pr_err("Missing VAS device node\n");
859 return -EINVAL;
860 }
861
862 if (of_property_read_u32(dn, "ibm,vas-id", &vasid)) {
863 pr_err("Missing ibm,vas-id device property\n");
864 of_node_put(dn);
865 return -EINVAL;
866 }
867
868 of_node_put(dn);
869
870 for_each_child_of_node(pn, dn) {
871 if (of_device_is_compatible(dn, "ibm,p9-nx-842")) {
872 ret = vas_cfg_coproc_info(dn, chip_id, vasid);
873 if (ret) {
874 of_node_put(dn);
875 return ret;
876 }
877 nx_fifo_found++;
878 }
879 }
880
881 if (!nx_fifo_found) {
882 pr_err("NX842 FIFO nodes are missing\n");
883 ret = -EINVAL;
884 }
885
886 return ret;
887}
888
889static int __init nx842_powernv_probe(struct device_node *dn)
890{
891 struct nx842_coproc *coproc;
892 unsigned int ct, ci;
893 int chip_id;
894
895 chip_id = of_get_ibm_chip_id(dn);
896 if (chip_id < 0) {
897 pr_err("ibm,chip-id missing\n");
898 return -EINVAL;
899 }
900
901 if (of_property_read_u32(dn, "ibm,842-coprocessor-type", &ct)) {
902 pr_err("ibm,842-coprocessor-type missing\n");
903 return -EINVAL;
904 }
905
906 if (of_property_read_u32(dn, "ibm,842-coprocessor-instance", &ci)) {
907 pr_err("ibm,842-coprocessor-instance missing\n");
908 return -EINVAL;
909 }
910
911 coproc = kmalloc(sizeof(*coproc), GFP_KERNEL);
912 if (!coproc)
913 return -ENOMEM;
914
915 coproc->ct = ct;
916 coproc->ci = ci;
917 nx842_add_coprocs_list(coproc, chip_id);
918
919 pr_info("coprocessor found on chip %d, CT %d CI %d\n", chip_id, ct, ci);
920
921 if (!nx842_ct)
922 nx842_ct = ct;
923 else if (nx842_ct != ct)
924 pr_err("NX842 chip %d, CT %d != first found CT %d\n",
925 chip_id, ct, nx842_ct);
926
927 return 0;
928}
929
930static void nx842_delete_coprocs(void)
931{
932 struct nx842_coproc *coproc, *n;
933
934 list_for_each_entry_safe(coproc, n, &nx842_coprocs, list) {
935 if (coproc->vas.rxwin)
936 vas_win_close(coproc->vas.rxwin);
937
938 list_del(&coproc->list);
939 kfree(coproc);
940 }
941}
942
943static struct nx842_constraints nx842_powernv_constraints = {
944 .alignment = DDE_BUFFER_ALIGN,
945 .multiple = DDE_BUFFER_LAST_MULT,
946 .minimum = DDE_BUFFER_LAST_MULT,
947 .maximum = (DDL_LEN_MAX - 1) * PAGE_SIZE,
948};
949
950static struct nx842_driver nx842_powernv_driver = {
951 .name = KBUILD_MODNAME,
952 .owner = THIS_MODULE,
953 .workmem_size = sizeof(struct nx842_workmem),
954 .constraints = &nx842_powernv_constraints,
955 .compress = nx842_powernv_compress,
956 .decompress = nx842_powernv_decompress,
957};
958
959static int nx842_powernv_crypto_init_vas(struct crypto_tfm *tfm)
960{
961 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
962 struct nx842_workmem *wmem;
963 struct nx842_coproc *coproc;
964 int ret;
965
966 ret = nx842_crypto_init(tfm, &nx842_powernv_driver);
967
968 if (ret)
969 return ret;
970
971 wmem = PTR_ALIGN((struct nx842_workmem *)ctx->wmem, WORKMEM_ALIGN);
972 coproc = per_cpu(coproc_inst, smp_processor_id());
973
974 ret = -EINVAL;
975 if (coproc && coproc->vas.rxwin) {
976 wmem->txwin = nx842_alloc_txwin(coproc);
977 if (!IS_ERR(wmem->txwin))
978 return 0;
979
980 ret = PTR_ERR(wmem->txwin);
981 }
982
983 return ret;
984}
985
986void nx842_powernv_crypto_exit_vas(struct crypto_tfm *tfm)
987{
988 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
989 struct nx842_workmem *wmem;
990
991 wmem = PTR_ALIGN((struct nx842_workmem *)ctx->wmem, WORKMEM_ALIGN);
992
993 if (wmem && wmem->txwin)
994 vas_win_close(wmem->txwin);
995
996 nx842_crypto_exit(tfm);
997}
998
999static int nx842_powernv_crypto_init(struct crypto_tfm *tfm)
1000{
1001 return nx842_crypto_init(tfm, &nx842_powernv_driver);
1002}
1003
1004static struct crypto_alg nx842_powernv_alg = {
1005 .cra_name = "842",
1006 .cra_driver_name = "842-nx",
1007 .cra_priority = 300,
1008 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
1009 .cra_ctxsize = sizeof(struct nx842_crypto_ctx),
1010 .cra_module = THIS_MODULE,
1011 .cra_init = nx842_powernv_crypto_init,
1012 .cra_exit = nx842_crypto_exit,
1013 .cra_u = { .compress = {
1014 .coa_compress = nx842_crypto_compress,
1015 .coa_decompress = nx842_crypto_decompress } }
1016};
1017
1018static __init int nx842_powernv_init(void)
1019{
1020 struct device_node *dn;
1021 int ret;
1022
1023 /* verify workmem size/align restrictions */
1024 BUILD_BUG_ON(WORKMEM_ALIGN % CRB_ALIGN);
1025 BUILD_BUG_ON(CRB_ALIGN % DDE_ALIGN);
1026 BUILD_BUG_ON(CRB_SIZE % DDE_ALIGN);
1027 /* verify buffer size/align restrictions */
1028 BUILD_BUG_ON(PAGE_SIZE % DDE_BUFFER_ALIGN);
1029 BUILD_BUG_ON(DDE_BUFFER_ALIGN % DDE_BUFFER_SIZE_MULT);
1030 BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT % DDE_BUFFER_LAST_MULT);
1031
1032 for_each_compatible_node(dn, NULL, "ibm,power9-nx") {
1033 ret = nx842_powernv_probe_vas(dn);
1034 if (ret) {
1035 nx842_delete_coprocs();
1036 return ret;
1037 }
1038 }
1039
1040 if (list_empty(&nx842_coprocs)) {
1041 for_each_compatible_node(dn, NULL, "ibm,power-nx")
1042 nx842_powernv_probe(dn);
1043
1044 if (!nx842_ct)
1045 return -ENODEV;
1046
1047 nx842_powernv_exec = nx842_exec_icswx;
1048 } else {
1049 nx842_powernv_exec = nx842_exec_vas;
1050 nx842_powernv_alg.cra_init = nx842_powernv_crypto_init_vas;
1051 nx842_powernv_alg.cra_exit = nx842_powernv_crypto_exit_vas;
1052 }
1053
1054 ret = crypto_register_alg(&nx842_powernv_alg);
1055 if (ret) {
1056 nx842_delete_coprocs();
1057 return ret;
1058 }
1059
1060 return 0;
1061}
1062module_init(nx842_powernv_init);
1063
1064static void __exit nx842_powernv_exit(void)
1065{
1066 crypto_unregister_alg(&nx842_powernv_alg);
1067
1068 nx842_delete_coprocs();
1069}
1070module_exit(nx842_powernv_exit);