]>
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> | |
25 | ||
99182a42 DS |
26 | MODULE_LICENSE("GPL"); |
27 | MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); | |
28 | MODULE_DESCRIPTION("842 H/W Compression driver for IBM PowerNV processors"); | |
03952d98 DS |
29 | MODULE_ALIAS_CRYPTO("842"); |
30 | MODULE_ALIAS_CRYPTO("842-nx"); | |
99182a42 DS |
31 | |
32 | #define WORKMEM_ALIGN (CRB_ALIGN) | |
33 | #define CSB_WAIT_MAX (5000) /* ms */ | |
34 | ||
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 */ | |
41 | ||
42 | ktime_t start; | |
43 | ||
44 | char padding[WORKMEM_ALIGN]; /* unused, to allow alignment */ | |
45 | } __packed __aligned(WORKMEM_ALIGN); | |
46 | ||
47 | struct nx842_coproc { | |
48 | unsigned int chip_id; | |
49 | unsigned int ct; | |
50 | unsigned int ci; | |
51 | struct list_head list; | |
52 | }; | |
53 | ||
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; | |
57 | ||
58 | /** | |
59 | * setup_indirect_dde - Setup an indirect DDE | |
60 | * | |
61 | * The DDE is setup with the the DDE count, byte count, and address of | |
62 | * first direct DDE in the list. | |
63 | */ | |
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) | |
67 | { | |
68 | dde->flags = 0; | |
69 | dde->count = dde_count; | |
70 | dde->index = 0; | |
71 | dde->length = cpu_to_be32(byte_count); | |
72 | dde->address = cpu_to_be64(nx842_get_pa(ddl)); | |
73 | } | |
74 | ||
75 | /** | |
76 | * setup_direct_dde - Setup single DDE from buffer | |
77 | * | |
78 | * The DDE is setup with the buffer and length. The buffer must be properly | |
79 | * aligned. The used length is returned. | |
80 | * Returns: | |
81 | * N Successfully set up DDE with N bytes | |
82 | */ | |
83 | static unsigned int setup_direct_dde(struct data_descriptor_entry *dde, | |
84 | unsigned long pa, unsigned int len) | |
85 | { | |
86 | unsigned int l = min_t(unsigned int, len, LEN_ON_PAGE(pa)); | |
87 | ||
88 | dde->flags = 0; | |
89 | dde->count = 0; | |
90 | dde->index = 0; | |
91 | dde->length = cpu_to_be32(l); | |
92 | dde->address = cpu_to_be64(pa); | |
93 | ||
94 | return l; | |
95 | } | |
96 | ||
97 | /** | |
98 | * setup_ddl - Setup DDL from buffer | |
99 | * | |
100 | * Returns: | |
101 | * 0 Successfully set up DDL | |
102 | */ | |
103 | static int setup_ddl(struct data_descriptor_entry *dde, | |
104 | struct data_descriptor_entry *ddl, | |
105 | unsigned char *buf, unsigned int len, | |
106 | bool in) | |
107 | { | |
108 | unsigned long pa = nx842_get_pa(buf); | |
109 | int i, ret, total_len = len; | |
110 | ||
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); | |
114 | return -EINVAL; | |
115 | } | |
116 | ||
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. | |
121 | */ | |
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); | |
125 | if (in) | |
126 | return -EINVAL; | |
127 | len = round_down(len, DDE_BUFFER_LAST_MULT); | |
128 | } | |
129 | ||
130 | /* use a single direct DDE */ | |
131 | if (len <= LEN_ON_PAGE(pa)) { | |
132 | ret = setup_direct_dde(dde, pa, len); | |
133 | WARN_ON(ret < len); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | /* use the DDL */ | |
138 | for (i = 0; i < DDL_LEN_MAX && len > 0; i++) { | |
139 | ret = setup_direct_dde(&ddl[i], pa, len); | |
140 | buf += ret; | |
141 | len -= ret; | |
142 | pa = nx842_get_pa(buf); | |
143 | } | |
144 | ||
145 | if (len > 0) { | |
146 | pr_debug("0x%x total %s bytes 0x%x too many for DDL.\n", | |
147 | total_len, in ? "input" : "output", len); | |
148 | if (in) | |
149 | return -EMSGSIZE; | |
150 | total_len -= len; | |
151 | } | |
152 | setup_indirect_dde(dde, ddl, i, total_len); | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
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)) | |
162 | ||
163 | #define CSB_ERR_ADDR(csb, msg, ...) \ | |
164 | CSB_ERR(csb, msg " at %lx", ##__VA_ARGS__, \ | |
165 | (unsigned long)be64_to_cpu((csb)->address)) | |
166 | ||
167 | /** | |
168 | * wait_for_csb | |
169 | */ | |
170 | static int wait_for_csb(struct nx842_workmem *wmem, | |
171 | struct coprocessor_status_block *csb) | |
172 | { | |
173 | ktime_t start = wmem->start, now = ktime_get(); | |
174 | ktime_t timeout = ktime_add_ms(start, CSB_WAIT_MAX); | |
175 | ||
176 | while (!(ACCESS_ONCE(csb->flags) & CSB_V)) { | |
177 | cpu_relax(); | |
178 | now = ktime_get(); | |
179 | if (ktime_after(now, timeout)) | |
180 | break; | |
181 | } | |
182 | ||
183 | /* hw has updated csb and output buffer */ | |
184 | barrier(); | |
185 | ||
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)); | |
190 | return -ETIMEDOUT; | |
191 | } | |
192 | if (csb->flags & CSB_F) { | |
193 | CSB_ERR(csb, "Invalid CSB format"); | |
194 | return -EPROTO; | |
195 | } | |
196 | if (csb->flags & CSB_CH) { | |
197 | CSB_ERR(csb, "Invalid CSB chaining state"); | |
198 | return -EPROTO; | |
199 | } | |
200 | ||
201 | /* verify CSB completion sequence is 0 */ | |
202 | if (csb->cs) { | |
203 | CSB_ERR(csb, "Invalid CSB completion sequence"); | |
204 | return -EPROTO; | |
205 | } | |
206 | ||
207 | /* check CSB Completion Code */ | |
208 | switch (csb->cc) { | |
209 | /* no error */ | |
210 | case CSB_CC_SUCCESS: | |
211 | break; | |
212 | case CSB_CC_TPBC_GT_SPBC: | |
213 | /* not an error, but the compressed data is | |
214 | * larger than the uncompressed data :( | |
215 | */ | |
216 | break; | |
217 | ||
218 | /* input data errors */ | |
219 | case CSB_CC_OPERAND_OVERLAP: | |
220 | /* input and output buffers overlap */ | |
221 | CSB_ERR(csb, "Operand Overlap error"); | |
222 | return -EINVAL; | |
223 | case CSB_CC_INVALID_OPERAND: | |
224 | CSB_ERR(csb, "Invalid operand"); | |
225 | return -EINVAL; | |
226 | case CSB_CC_NOSPC: | |
227 | /* output buffer too small */ | |
228 | return -ENOSPC; | |
229 | case CSB_CC_ABORT: | |
230 | CSB_ERR(csb, "Function aborted"); | |
231 | return -EINTR; | |
232 | case CSB_CC_CRC_MISMATCH: | |
233 | CSB_ERR(csb, "CRC mismatch"); | |
234 | return -EINVAL; | |
235 | case CSB_CC_TEMPL_INVALID: | |
236 | CSB_ERR(csb, "Compressed data template invalid"); | |
237 | return -EINVAL; | |
238 | case CSB_CC_TEMPL_OVERFLOW: | |
239 | CSB_ERR(csb, "Compressed data template shows data past end"); | |
240 | return -EINVAL; | |
241 | ||
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"); | |
246 | return -EINVAL; | |
247 | case CSB_CC_DATA_LENGTH: | |
248 | /* setup_ddl should have detected this */ | |
249 | CSB_ERR(csb, "Invalid data length"); | |
250 | return -EINVAL; | |
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"); | |
261 | return -EPROTO; | |
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"); | |
272 | return -EPROTO; | |
273 | case CSB_CC_PRIVILEGE: | |
274 | /* shouldn't happen, we're in HYP mode */ | |
275 | CSB_ERR(csb, "Insufficient Privilege error"); | |
276 | return -EPROTO; | |
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"); | |
280 | return -EINVAL; | |
281 | case CSB_CC_TRANSPORT: | |
282 | /* shouldn't happen, we setup CRB correctly */ | |
283 | CSB_ERR(csb, "Invalid CRB"); | |
284 | return -EINVAL; | |
285 | case CSB_CC_SEGMENTED_DDL: | |
286 | /* shouldn't happen, setup_ddl creates DDL right */ | |
287 | CSB_ERR(csb, "Segmented DDL error"); | |
288 | return -EINVAL; | |
289 | case CSB_CC_DDE_OVERFLOW: | |
290 | /* shouldn't happen, setup_ddl creates DDL right */ | |
291 | CSB_ERR(csb, "DDE overflow error"); | |
292 | return -EINVAL; | |
293 | case CSB_CC_SESSION: | |
294 | /* should not happen with ICSWX */ | |
295 | CSB_ERR(csb, "Session violation error"); | |
296 | return -EPROTO; | |
297 | case CSB_CC_CHAIN: | |
298 | /* should not happen, we don't use chained CRBs */ | |
299 | CSB_ERR(csb, "Chained CRB error"); | |
300 | return -EPROTO; | |
301 | case CSB_CC_SEQUENCE: | |
302 | /* should not happen, we don't use chained CRBs */ | |
303 | CSB_ERR(csb, "CRB seqeunce number error"); | |
304 | return -EPROTO; | |
305 | case CSB_CC_UNKNOWN_CODE: | |
306 | CSB_ERR(csb, "Unknown subfunction code"); | |
307 | return -EPROTO; | |
308 | ||
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"); | |
315 | return -EPROTO; | |
316 | case CSB_CC_WR_EXTERNAL: | |
317 | CSB_ERR_ADDR(csb, "Write error outside coprocessor"); | |
318 | return -EPROTO; | |
319 | case CSB_CC_INTERNAL: | |
320 | CSB_ERR(csb, "Internal error in coprocessor"); | |
321 | return -EPROTO; | |
322 | case CSB_CC_PROVISION: | |
323 | CSB_ERR(csb, "Storage provision error"); | |
324 | return -EPROTO; | |
325 | case CSB_CC_HW: | |
326 | CSB_ERR(csb, "Correctable hardware error"); | |
327 | return -EPROTO; | |
328 | ||
329 | default: | |
330 | CSB_ERR(csb, "Invalid CC %d", csb->cc); | |
331 | return -EPROTO; | |
332 | } | |
333 | ||
334 | /* check Completion Extension state */ | |
335 | if (csb->ce & CSB_CE_TERMINATION) { | |
336 | CSB_ERR(csb, "CSB request was terminated"); | |
337 | return -EPROTO; | |
338 | } | |
339 | if (csb->ce & CSB_CE_INCOMPLETE) { | |
340 | CSB_ERR(csb, "CSB request not complete"); | |
341 | return -EPROTO; | |
342 | } | |
343 | if (!(csb->ce & CSB_CE_TPBC)) { | |
344 | CSB_ERR(csb, "TPBC not provided, unknown target length"); | |
345 | return -EPROTO; | |
346 | } | |
347 | ||
348 | /* successful completion */ | |
844190db DS |
349 | pr_debug_ratelimited("Processed %u bytes in %lu us\n", |
350 | be32_to_cpu(csb->count), | |
99182a42 DS |
351 | (unsigned long)ktime_us_delta(now, start)); |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | /** | |
357 | * nx842_powernv_function - compress/decompress data using the 842 algorithm | |
358 | * | |
359 | * (De)compression provided by the NX842 coprocessor on IBM PowerNV systems. | |
360 | * This compresses or decompresses the provided input buffer into the provided | |
361 | * output buffer. | |
362 | * | |
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. | |
366 | * | |
367 | * The @workmem buffer should only be used by one function call at a time. | |
368 | * | |
369 | * @in: input buffer pointer | |
370 | * @inlen: input buffer size | |
371 | * @out: output buffer pointer | |
372 | * @outlenp: output buffer size pointer | |
2c6f6eab DS |
373 | * @workmem: working memory buffer pointer, size determined by |
374 | * nx842_powernv_driver.workmem_size | |
99182a42 DS |
375 | * @fc: function code, see CCW Function Codes in nx-842.h |
376 | * | |
377 | * Returns: | |
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 | |
386 | */ | |
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) | |
390 | { | |
391 | struct coprocessor_request_block *crb; | |
392 | struct coprocessor_status_block *csb; | |
393 | struct nx842_workmem *wmem; | |
394 | int ret; | |
395 | u64 csb_addr; | |
396 | u32 ccw; | |
397 | unsigned int outlen = *outlenp; | |
398 | ||
399 | wmem = PTR_ALIGN(workmem, WORKMEM_ALIGN); | |
400 | ||
401 | *outlenp = 0; | |
402 | ||
403 | /* shoudn't happen, we don't load without a coproc */ | |
404 | if (!nx842_ct) { | |
405 | pr_err_ratelimited("coprocessor CT is 0"); | |
406 | return -ENODEV; | |
407 | } | |
408 | ||
409 | crb = &wmem->crb; | |
410 | csb = &crb->csb; | |
411 | ||
412 | /* Clear any previous values */ | |
413 | memset(crb, 0, sizeof(*crb)); | |
414 | ||
415 | /* set up DDLs */ | |
416 | ret = setup_ddl(&crb->source, wmem->ddl_in, | |
417 | (unsigned char *)in, inlen, true); | |
418 | if (ret) | |
419 | return ret; | |
420 | ret = setup_ddl(&crb->target, wmem->ddl_out, | |
421 | out, outlen, false); | |
422 | if (ret) | |
423 | return ret; | |
424 | ||
425 | /* set up CCW */ | |
426 | ccw = 0; | |
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); | |
430 | ||
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); | |
435 | ||
436 | wmem->start = ktime_get(); | |
437 | ||
438 | /* do ICSWX */ | |
439 | ret = icswx(cpu_to_be32(ccw), crb); | |
440 | ||
441 | pr_debug_ratelimited("icswx CR %x ccw %x crb->ccw %x\n", ret, | |
442 | (unsigned int)ccw, | |
443 | (unsigned int)be32_to_cpu(crb->ccw)); | |
444 | ||
445 | switch (ret) { | |
446 | case ICSWX_INITIATED: | |
447 | ret = wait_for_csb(wmem, csb); | |
448 | break; | |
449 | case ICSWX_BUSY: | |
450 | pr_debug_ratelimited("842 Coprocessor busy\n"); | |
451 | ret = -EBUSY; | |
452 | break; | |
453 | case ICSWX_REJECTED: | |
454 | pr_err_ratelimited("ICSWX rejected\n"); | |
455 | ret = -EPROTO; | |
456 | break; | |
457 | default: | |
458 | pr_err_ratelimited("Invalid ICSWX return code %x\n", ret); | |
459 | ret = -EPROTO; | |
460 | break; | |
461 | } | |
462 | ||
463 | if (!ret) | |
464 | *outlenp = be32_to_cpu(csb->count); | |
465 | ||
466 | return ret; | |
467 | } | |
468 | ||
469 | /** | |
470 | * nx842_powernv_compress - Compress data using the 842 algorithm | |
471 | * | |
472 | * Compression provided by the NX842 coprocessor on IBM PowerNV systems. | |
473 | * The input buffer is compressed and the result is stored in the | |
474 | * provided output buffer. | |
475 | * | |
476 | * Upon return from this function @outlen contains the length of the | |
477 | * compressed data. If there is an error then @outlen will be 0 and an | |
478 | * error will be specified by the return code from this function. | |
479 | * | |
480 | * @in: input buffer pointer | |
481 | * @inlen: input buffer size | |
482 | * @out: output buffer pointer | |
483 | * @outlenp: output buffer size pointer | |
2c6f6eab DS |
484 | * @workmem: working memory buffer pointer, size determined by |
485 | * nx842_powernv_driver.workmem_size | |
99182a42 DS |
486 | * |
487 | * Returns: see @nx842_powernv_function() | |
488 | */ | |
489 | static int nx842_powernv_compress(const unsigned char *in, unsigned int inlen, | |
490 | unsigned char *out, unsigned int *outlenp, | |
491 | void *wmem) | |
492 | { | |
493 | return nx842_powernv_function(in, inlen, out, outlenp, | |
ea0b3984 | 494 | wmem, CCW_FC_842_COMP_CRC); |
99182a42 DS |
495 | } |
496 | ||
497 | /** | |
498 | * nx842_powernv_decompress - Decompress data using the 842 algorithm | |
499 | * | |
500 | * Decompression provided by the NX842 coprocessor on IBM PowerNV systems. | |
501 | * The input buffer is decompressed 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 | * decompressed 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 DS |
514 | * |
515 | * Returns: see @nx842_powernv_function() | |
516 | */ | |
517 | static int nx842_powernv_decompress(const unsigned char *in, unsigned int inlen, | |
518 | unsigned char *out, unsigned int *outlenp, | |
519 | void *wmem) | |
520 | { | |
521 | return nx842_powernv_function(in, inlen, out, outlenp, | |
ea0b3984 | 522 | wmem, CCW_FC_842_DECOMP_CRC); |
99182a42 DS |
523 | } |
524 | ||
525 | static int __init nx842_powernv_probe(struct device_node *dn) | |
526 | { | |
527 | struct nx842_coproc *coproc; | |
99182a42 DS |
528 | unsigned int ct, ci; |
529 | int chip_id; | |
530 | ||
531 | chip_id = of_get_ibm_chip_id(dn); | |
532 | if (chip_id < 0) { | |
533 | pr_err("ibm,chip-id missing\n"); | |
534 | return -EINVAL; | |
535 | } | |
d62112f2 | 536 | |
537 | if (of_property_read_u32(dn, "ibm,842-coprocessor-type", &ct)) { | |
99182a42 DS |
538 | pr_err("ibm,842-coprocessor-type missing\n"); |
539 | return -EINVAL; | |
540 | } | |
d62112f2 | 541 | |
542 | if (of_property_read_u32(dn, "ibm,842-coprocessor-instance", &ci)) { | |
99182a42 DS |
543 | pr_err("ibm,842-coprocessor-instance missing\n"); |
544 | return -EINVAL; | |
545 | } | |
99182a42 DS |
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 | ||
568 | static 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 | ||
575 | static 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 | ||
03952d98 DS |
584 | static int nx842_powernv_crypto_init(struct crypto_tfm *tfm) |
585 | { | |
586 | return nx842_crypto_init(tfm, &nx842_powernv_driver); | |
587 | } | |
588 | ||
589 | static struct crypto_alg nx842_powernv_alg = { | |
590 | .cra_name = "842", | |
591 | .cra_driver_name = "842-nx", | |
592 | .cra_priority = 300, | |
593 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, | |
594 | .cra_ctxsize = sizeof(struct nx842_crypto_ctx), | |
595 | .cra_module = THIS_MODULE, | |
596 | .cra_init = nx842_powernv_crypto_init, | |
597 | .cra_exit = nx842_crypto_exit, | |
598 | .cra_u = { .compress = { | |
599 | .coa_compress = nx842_crypto_compress, | |
600 | .coa_decompress = nx842_crypto_decompress } } | |
601 | }; | |
602 | ||
99182a42 DS |
603 | static __init int nx842_powernv_init(void) |
604 | { | |
605 | struct device_node *dn; | |
03952d98 | 606 | int ret; |
99182a42 DS |
607 | |
608 | /* verify workmem size/align restrictions */ | |
99182a42 DS |
609 | BUILD_BUG_ON(WORKMEM_ALIGN % CRB_ALIGN); |
610 | BUILD_BUG_ON(CRB_ALIGN % DDE_ALIGN); | |
611 | BUILD_BUG_ON(CRB_SIZE % DDE_ALIGN); | |
612 | /* verify buffer size/align restrictions */ | |
613 | BUILD_BUG_ON(PAGE_SIZE % DDE_BUFFER_ALIGN); | |
614 | BUILD_BUG_ON(DDE_BUFFER_ALIGN % DDE_BUFFER_SIZE_MULT); | |
615 | BUILD_BUG_ON(DDE_BUFFER_SIZE_MULT % DDE_BUFFER_LAST_MULT); | |
616 | ||
3e648cbe | 617 | for_each_compatible_node(dn, NULL, "ibm,power-nx") |
99182a42 DS |
618 | nx842_powernv_probe(dn); |
619 | ||
2b93f7ee | 620 | if (!nx842_ct) |
99182a42 | 621 | return -ENODEV; |
99182a42 | 622 | |
03952d98 DS |
623 | ret = crypto_register_alg(&nx842_powernv_alg); |
624 | if (ret) { | |
3e648cbe DS |
625 | struct nx842_coproc *coproc, *n; |
626 | ||
627 | list_for_each_entry_safe(coproc, n, &nx842_coprocs, list) { | |
628 | list_del(&coproc->list); | |
629 | kfree(coproc); | |
630 | } | |
631 | ||
03952d98 | 632 | return ret; |
3e648cbe | 633 | } |
99182a42 | 634 | |
99182a42 DS |
635 | return 0; |
636 | } | |
637 | module_init(nx842_powernv_init); | |
638 | ||
639 | static void __exit nx842_powernv_exit(void) | |
640 | { | |
641 | struct nx842_coproc *coproc, *n; | |
642 | ||
03952d98 | 643 | crypto_unregister_alg(&nx842_powernv_alg); |
99182a42 DS |
644 | |
645 | list_for_each_entry_safe(coproc, n, &nx842_coprocs, list) { | |
646 | list_del(&coproc->list); | |
647 | kfree(coproc); | |
648 | } | |
99182a42 DS |
649 | } |
650 | module_exit(nx842_powernv_exit); |