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