blob: cb481d81df0603bfbace4aeaa63cc8f4c3a275fa [file] [log] [blame]
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001/*
2 * Driver for IBM Power 842 compression accelerator
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * Copyright (C) IBM Corporation, 2012
19 *
20 * Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
21 * Seth Jennings <sjenning@linux.vnet.ibm.com>
22 */
23
Michael Ellerman33b58b012012-08-03 12:23:15 +100024#include <asm/page.h>
Michael Ellerman33b58b012012-08-03 12:23:15 +100025#include <asm/vio.h>
Seth Jennings0e16aaf2012-07-19 09:42:40 -050026
Dan Streetman7011a122015-05-07 13:49:17 -040027#include "nx-842.h"
Seth Jennings0e16aaf2012-07-19 09:42:40 -050028#include "nx_csbcpb.h" /* struct nx_csbcpb */
29
Dan Streetman7011a122015-05-07 13:49:17 -040030#define MODULE_NAME NX842_PSERIES_MODULE_NAME
Seth Jennings0e16aaf2012-07-19 09:42:40 -050031MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Robert Jennings <rcj@linux.vnet.ibm.com>");
33MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors");
34
35#define SHIFT_4K 12
36#define SHIFT_64K 16
37#define SIZE_4K (1UL << SHIFT_4K)
38#define SIZE_64K (1UL << SHIFT_64K)
39
40/* IO buffer must be 128 byte aligned */
41#define IO_BUFFER_ALIGN 128
42
Dan Streetman959e6652015-05-07 13:49:18 -040043static struct nx842_constraints nx842_pseries_constraints = {
44 .alignment = IO_BUFFER_ALIGN,
45 .multiple = DDE_BUFFER_LAST_MULT,
46 .minimum = IO_BUFFER_ALIGN,
47 .maximum = PAGE_SIZE, /* dynamic, max_sync_size */
48};
49
Seth Jennings0e16aaf2012-07-19 09:42:40 -050050struct nx842_header {
51 int blocks_nr; /* number of compressed blocks */
52 int offset; /* offset of the first block (from beginning of header) */
53 int sizes[0]; /* size of compressed blocks */
54};
55
56static inline int nx842_header_size(const struct nx842_header *hdr)
57{
58 return sizeof(struct nx842_header) +
59 hdr->blocks_nr * sizeof(hdr->sizes[0]);
60}
61
62/* Macros for fields within nx_csbcpb */
63/* Check the valid bit within the csbcpb valid field */
64#define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7))
65
66/* CE macros operate on the completion_extension field bits in the csbcpb.
67 * CE0 0=full completion, 1=partial completion
68 * CE1 0=CE0 indicates completion, 1=termination (output may be modified)
69 * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */
70#define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7))
71#define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6))
72#define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5))
73
74/* The NX unit accepts data only on 4K page boundaries */
75#define NX842_HW_PAGE_SHIFT SHIFT_4K
76#define NX842_HW_PAGE_SIZE (ASM_CONST(1) << NX842_HW_PAGE_SHIFT)
77#define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
78
79enum nx842_status {
80 UNAVAILABLE,
81 AVAILABLE
82};
83
84struct ibm_nx842_counters {
85 atomic64_t comp_complete;
86 atomic64_t comp_failed;
87 atomic64_t decomp_complete;
88 atomic64_t decomp_failed;
89 atomic64_t swdecomp;
90 atomic64_t comp_times[32];
91 atomic64_t decomp_times[32];
92};
93
94static struct nx842_devdata {
95 struct vio_dev *vdev;
96 struct device *dev;
97 struct ibm_nx842_counters *counters;
98 unsigned int max_sg_len;
99 unsigned int max_sync_size;
100 unsigned int max_sync_sg;
101 enum nx842_status status;
102} __rcu *devdata;
103static DEFINE_SPINLOCK(devdata_mutex);
104
105#define NX842_COUNTER_INC(_x) \
106static inline void nx842_inc_##_x( \
107 const struct nx842_devdata *dev) { \
108 if (dev) \
109 atomic64_inc(&dev->counters->_x); \
110}
111NX842_COUNTER_INC(comp_complete);
112NX842_COUNTER_INC(comp_failed);
113NX842_COUNTER_INC(decomp_complete);
114NX842_COUNTER_INC(decomp_failed);
115NX842_COUNTER_INC(swdecomp);
116
117#define NX842_HIST_SLOTS 16
118
119static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
120{
121 int bucket = fls(time);
122
123 if (bucket)
124 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
125
126 atomic64_inc(&times[bucket]);
127}
128
129/* NX unit operation flags */
130#define NX842_OP_COMPRESS 0x0
131#define NX842_OP_CRC 0x1
132#define NX842_OP_DECOMPRESS 0x2
133#define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
134#define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
135#define NX842_OP_ASYNC (1<<23)
136#define NX842_OP_NOTIFY (1<<22)
137#define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
138
139static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
140{
141 /* No use of DMA mappings within the driver. */
142 return 0;
143}
144
145struct nx842_slentry {
Michael Ellerman33b58b012012-08-03 12:23:15 +1000146 unsigned long ptr; /* Real address (use __pa()) */
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500147 unsigned long len;
148};
149
150/* pHyp scatterlist entry */
151struct nx842_scatterlist {
152 int entry_nr; /* number of slentries */
153 struct nx842_slentry *entries; /* ptr to array of slentries */
154};
155
156/* Does not include sizeof(entry_nr) in the size */
157static inline unsigned long nx842_get_scatterlist_size(
158 struct nx842_scatterlist *sl)
159{
160 return sl->entry_nr * sizeof(struct nx842_slentry);
161}
162
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600163static inline unsigned long nx842_get_pa(void *addr)
164{
165 if (is_vmalloc_addr(addr))
166 return page_to_phys(vmalloc_to_page(addr))
167 + offset_in_page(addr);
168 else
169 return __pa(addr);
170}
171
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500172static int nx842_build_scatterlist(unsigned long buf, int len,
173 struct nx842_scatterlist *sl)
174{
175 unsigned long nextpage;
176 struct nx842_slentry *entry;
177
178 sl->entry_nr = 0;
179
180 entry = sl->entries;
181 while (len) {
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600182 entry->ptr = nx842_get_pa((void *)buf);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500183 nextpage = ALIGN(buf + 1, NX842_HW_PAGE_SIZE);
184 if (nextpage < buf + len) {
185 /* we aren't at the end yet */
186 if (IS_ALIGNED(buf, NX842_HW_PAGE_SIZE))
187 /* we are in the middle (or beginning) */
188 entry->len = NX842_HW_PAGE_SIZE;
189 else
190 /* we are at the beginning */
191 entry->len = nextpage - buf;
192 } else {
193 /* at the end */
194 entry->len = len;
195 }
196
197 len -= entry->len;
198 buf += entry->len;
199 sl->entry_nr++;
200 entry++;
201 }
202
203 return 0;
204}
205
206/*
207 * Working memory for software decompression
208 */
209struct sw842_fifo {
210 union {
211 char f8[256][8];
212 char f4[512][4];
213 };
214 char f2[256][2];
215 unsigned char f84_full;
216 unsigned char f2_full;
217 unsigned char f8_count;
218 unsigned char f2_count;
219 unsigned int f4_count;
220};
221
222/*
223 * Working memory for crypto API
224 */
225struct nx842_workmem {
226 char bounce[PAGE_SIZE]; /* bounce buffer for decompression input */
227 union {
228 /* hardware working memory */
229 struct {
230 /* scatterlist */
231 char slin[SIZE_4K];
232 char slout[SIZE_4K];
233 /* coprocessor status/parameter block */
234 struct nx_csbcpb csbcpb;
235 };
236 /* software working memory */
237 struct sw842_fifo swfifo; /* software decompression fifo */
238 };
239};
240
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500241static int nx842_validate_result(struct device *dev,
242 struct cop_status_block *csb)
243{
244 /* The csb must be valid after returning from vio_h_cop_sync */
245 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
246 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
247 __func__);
248 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
249 csb->valid,
250 csb->crb_seq_number,
251 csb->completion_code,
252 csb->completion_extension);
253 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
254 csb->processed_byte_count,
255 (unsigned long)csb->address);
256 return -EIO;
257 }
258
259 /* Check return values from the hardware in the CSB */
260 switch (csb->completion_code) {
261 case 0: /* Completed without error */
262 break;
263 case 64: /* Target bytes > Source bytes during compression */
264 case 13: /* Output buffer too small */
265 dev_dbg(dev, "%s: Compression output larger than input\n",
266 __func__);
267 return -ENOSPC;
268 case 66: /* Input data contains an illegal template field */
269 case 67: /* Template indicates data past the end of the input stream */
270 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
271 __func__, csb->completion_code);
272 return -EINVAL;
273 default:
274 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
275 __func__, csb->completion_code);
276 return -EIO;
277 }
278
279 /* Hardware sanity check */
280 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
281 dev_err(dev, "%s: No error returned by hardware, but "
282 "data returned is unusable, contact support.\n"
283 "(Additional info: csbcbp->processed bytes "
284 "does not specify processed bytes for the "
285 "target buffer.)\n", __func__);
286 return -EIO;
287 }
288
289 return 0;
290}
291
292/**
Dan Streetman7011a122015-05-07 13:49:17 -0400293 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500294 *
295 * Compression provide by the NX842 coprocessor on IBM Power systems.
296 * The input buffer is compressed and the result is stored in the
297 * provided output buffer.
298 *
299 * Upon return from this function @outlen contains the length of the
300 * compressed data. If there is an error then @outlen will be 0 and an
301 * error will be specified by the return code from this function.
302 *
303 * @in: Pointer to input buffer, must be page aligned
304 * @inlen: Length of input buffer, must be PAGE_SIZE
305 * @out: Pointer to output buffer
306 * @outlen: Length of output buffer
307 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman7011a122015-05-07 13:49:17 -0400308 * NX842_MEM_COMPRESS
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500309 *
310 * Returns:
311 * 0 Success, output of length @outlen stored in the buffer at @out
312 * -ENOMEM Unable to allocate internal buffers
313 * -ENOSPC Output buffer is to small
314 * -EMSGSIZE XXX Difficult to describe this limitation
315 * -EIO Internal error
316 * -ENODEV Hardware unavailable
317 */
Dan Streetman7011a122015-05-07 13:49:17 -0400318static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
319 unsigned char *out, unsigned int *outlen,
320 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500321{
322 struct nx842_header *hdr;
323 struct nx842_devdata *local_devdata;
324 struct device *dev = NULL;
325 struct nx842_workmem *workmem;
326 struct nx842_scatterlist slin, slout;
327 struct nx_csbcpb *csbcpb;
328 int ret = 0, max_sync_size, i, bytesleft, size, hdrsize;
329 unsigned long inbuf, outbuf, padding;
330 struct vio_pfo_op op = {
331 .done = NULL,
332 .handle = 0,
333 .timeout = 0,
334 };
335 unsigned long start_time = get_tb();
336
337 /*
338 * Make sure input buffer is 64k page aligned. This is assumed since
339 * this driver is designed for page compression only (for now). This
340 * is very nice since we can now use direct DDE(s) for the input and
341 * the alignment is guaranteed.
342 */
343 inbuf = (unsigned long)in;
344 if (!IS_ALIGNED(inbuf, PAGE_SIZE) || inlen != PAGE_SIZE)
345 return -EINVAL;
346
347 rcu_read_lock();
348 local_devdata = rcu_dereference(devdata);
349 if (!local_devdata || !local_devdata->dev) {
350 rcu_read_unlock();
351 return -ENODEV;
352 }
353 max_sync_size = local_devdata->max_sync_size;
354 dev = local_devdata->dev;
355
356 /* Create the header */
357 hdr = (struct nx842_header *)out;
358 hdr->blocks_nr = PAGE_SIZE / max_sync_size;
359 hdrsize = nx842_header_size(hdr);
360 outbuf = (unsigned long)out + hdrsize;
361 bytesleft = *outlen - hdrsize;
362
363 /* Init scatterlist */
364 workmem = (struct nx842_workmem *)ALIGN((unsigned long)wmem,
365 NX842_HW_PAGE_SIZE);
366 slin.entries = (struct nx842_slentry *)workmem->slin;
367 slout.entries = (struct nx842_slentry *)workmem->slout;
368
369 /* Init operation */
370 op.flags = NX842_OP_COMPRESS;
371 csbcpb = &workmem->csbcpb;
372 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600373 op.csbcpb = nx842_get_pa(csbcpb);
374 op.out = nx842_get_pa(slout.entries);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500375
376 for (i = 0; i < hdr->blocks_nr; i++) {
377 /*
378 * Aligning the output blocks to 128 bytes does waste space,
379 * but it prevents the need for bounce buffers and memory
380 * copies. It also simplifies the code a lot. In the worst
381 * case (64k page, 4k max_sync_size), you lose up to
382 * (128*16)/64k = ~3% the compression factor. For 64k
383 * max_sync_size, the loss would be at most 128/64k = ~0.2%.
384 */
385 padding = ALIGN(outbuf, IO_BUFFER_ALIGN) - outbuf;
386 outbuf += padding;
387 bytesleft -= padding;
388 if (i == 0)
389 /* save offset into first block in header */
390 hdr->offset = padding + hdrsize;
391
392 if (bytesleft <= 0) {
393 ret = -ENOSPC;
394 goto unlock;
395 }
396
397 /*
398 * NOTE: If the default max_sync_size is changed from 4k
399 * to 64k, remove the "likely" case below, since a
400 * scatterlist will always be needed.
401 */
402 if (likely(max_sync_size == NX842_HW_PAGE_SIZE)) {
403 /* Create direct DDE */
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600404 op.in = nx842_get_pa((void *)inbuf);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500405 op.inlen = max_sync_size;
406
407 } else {
408 /* Create indirect DDE (scatterlist) */
409 nx842_build_scatterlist(inbuf, max_sync_size, &slin);
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600410 op.in = nx842_get_pa(slin.entries);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500411 op.inlen = -nx842_get_scatterlist_size(&slin);
412 }
413
414 /*
415 * If max_sync_size != NX842_HW_PAGE_SIZE, an indirect
416 * DDE is required for the outbuf.
417 * If max_sync_size == NX842_HW_PAGE_SIZE, outbuf must
418 * also be page aligned (1 in 128/4k=32 chance) in order
419 * to use a direct DDE.
420 * This is unlikely, just use an indirect DDE always.
421 */
422 nx842_build_scatterlist(outbuf,
423 min(bytesleft, max_sync_size), &slout);
424 /* op.out set before loop */
425 op.outlen = -nx842_get_scatterlist_size(&slout);
426
427 /* Send request to pHyp */
428 ret = vio_h_cop_sync(local_devdata->vdev, &op);
429
430 /* Check for pHyp error */
431 if (ret) {
432 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
433 __func__, ret, op.hcall_err);
434 ret = -EIO;
435 goto unlock;
436 }
437
438 /* Check for hardware error */
439 ret = nx842_validate_result(dev, &csbcpb->csb);
440 if (ret && ret != -ENOSPC)
441 goto unlock;
442
443 /* Handle incompressible data */
444 if (unlikely(ret == -ENOSPC)) {
445 if (bytesleft < max_sync_size) {
446 /*
447 * Not enough space left in the output buffer
448 * to store uncompressed block
449 */
450 goto unlock;
451 } else {
452 /* Store incompressible block */
453 memcpy((void *)outbuf, (void *)inbuf,
454 max_sync_size);
455 hdr->sizes[i] = -max_sync_size;
456 outbuf += max_sync_size;
457 bytesleft -= max_sync_size;
458 /* Reset ret, incompressible data handled */
459 ret = 0;
460 }
461 } else {
462 /* Normal case, compression was successful */
463 size = csbcpb->csb.processed_byte_count;
464 dev_dbg(dev, "%s: processed_bytes=%d\n",
465 __func__, size);
466 hdr->sizes[i] = size;
467 outbuf += size;
468 bytesleft -= size;
469 }
470
471 inbuf += max_sync_size;
472 }
473
474 *outlen = (unsigned int)(outbuf - (unsigned long)out);
475
476unlock:
477 if (ret)
478 nx842_inc_comp_failed(local_devdata);
479 else {
480 nx842_inc_comp_complete(local_devdata);
481 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
482 (get_tb() - start_time) / tb_ticks_per_usec);
483 }
484 rcu_read_unlock();
485 return ret;
486}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500487
488static int sw842_decompress(const unsigned char *, int, unsigned char *, int *,
489 const void *);
490
491/**
Dan Streetman7011a122015-05-07 13:49:17 -0400492 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500493 *
494 * Decompression provide by the NX842 coprocessor on IBM Power systems.
495 * The input buffer is decompressed and the result is stored in the
496 * provided output buffer. The size allocated to the output buffer is
497 * provided by the caller of this function in @outlen. Upon return from
498 * this function @outlen contains the length of the decompressed data.
499 * If there is an error then @outlen will be 0 and an error will be
500 * specified by the return code from this function.
501 *
502 * @in: Pointer to input buffer, will use bounce buffer if not 128 byte
503 * aligned
504 * @inlen: Length of input buffer
505 * @out: Pointer to output buffer, must be page aligned
506 * @outlen: Length of output buffer, must be PAGE_SIZE
507 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman7011a122015-05-07 13:49:17 -0400508 * NX842_MEM_COMPRESS
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500509 *
510 * Returns:
511 * 0 Success, output of length @outlen stored in the buffer at @out
512 * -ENODEV Hardware decompression device is unavailable
513 * -ENOMEM Unable to allocate internal buffers
514 * -ENOSPC Output buffer is to small
515 * -EINVAL Bad input data encountered when attempting decompress
516 * -EIO Internal error
517 */
Dan Streetman7011a122015-05-07 13:49:17 -0400518static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
519 unsigned char *out, unsigned int *outlen,
520 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500521{
522 struct nx842_header *hdr;
523 struct nx842_devdata *local_devdata;
524 struct device *dev = NULL;
525 struct nx842_workmem *workmem;
526 struct nx842_scatterlist slin, slout;
527 struct nx_csbcpb *csbcpb;
528 int ret = 0, i, size, max_sync_size;
529 unsigned long inbuf, outbuf;
530 struct vio_pfo_op op = {
531 .done = NULL,
532 .handle = 0,
533 .timeout = 0,
534 };
535 unsigned long start_time = get_tb();
536
537 /* Ensure page alignment and size */
538 outbuf = (unsigned long)out;
539 if (!IS_ALIGNED(outbuf, PAGE_SIZE) || *outlen != PAGE_SIZE)
540 return -EINVAL;
541
542 rcu_read_lock();
543 local_devdata = rcu_dereference(devdata);
544 if (local_devdata)
545 dev = local_devdata->dev;
546
547 /* Get header */
548 hdr = (struct nx842_header *)in;
549
550 workmem = (struct nx842_workmem *)ALIGN((unsigned long)wmem,
551 NX842_HW_PAGE_SIZE);
552
553 inbuf = (unsigned long)in + hdr->offset;
554 if (likely(!IS_ALIGNED(inbuf, IO_BUFFER_ALIGN))) {
555 /* Copy block(s) into bounce buffer for alignment */
556 memcpy(workmem->bounce, in + hdr->offset, inlen - hdr->offset);
557 inbuf = (unsigned long)workmem->bounce;
558 }
559
560 /* Init scatterlist */
561 slin.entries = (struct nx842_slentry *)workmem->slin;
562 slout.entries = (struct nx842_slentry *)workmem->slout;
563
564 /* Init operation */
565 op.flags = NX842_OP_DECOMPRESS;
566 csbcpb = &workmem->csbcpb;
567 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600568 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500569
570 /*
571 * max_sync_size may have changed since compression,
572 * so we can't read it from the device info. We need
573 * to derive it from hdr->blocks_nr.
574 */
575 max_sync_size = PAGE_SIZE / hdr->blocks_nr;
576
577 for (i = 0; i < hdr->blocks_nr; i++) {
578 /* Skip padding */
579 inbuf = ALIGN(inbuf, IO_BUFFER_ALIGN);
580
581 if (hdr->sizes[i] < 0) {
582 /* Negative sizes indicate uncompressed data blocks */
583 size = abs(hdr->sizes[i]);
584 memcpy((void *)outbuf, (void *)inbuf, size);
585 outbuf += size;
586 inbuf += size;
587 continue;
588 }
589
590 if (!dev)
591 goto sw;
592
593 /*
594 * The better the compression, the more likely the "likely"
595 * case becomes.
596 */
597 if (likely((inbuf & NX842_HW_PAGE_MASK) ==
598 ((inbuf + hdr->sizes[i] - 1) & NX842_HW_PAGE_MASK))) {
599 /* Create direct DDE */
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600600 op.in = nx842_get_pa((void *)inbuf);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500601 op.inlen = hdr->sizes[i];
602 } else {
603 /* Create indirect DDE (scatterlist) */
604 nx842_build_scatterlist(inbuf, hdr->sizes[i] , &slin);
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600605 op.in = nx842_get_pa(slin.entries);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500606 op.inlen = -nx842_get_scatterlist_size(&slin);
607 }
608
609 /*
610 * NOTE: If the default max_sync_size is changed from 4k
611 * to 64k, remove the "likely" case below, since a
612 * scatterlist will always be needed.
613 */
614 if (likely(max_sync_size == NX842_HW_PAGE_SIZE)) {
615 /* Create direct DDE */
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600616 op.out = nx842_get_pa((void *)outbuf);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500617 op.outlen = max_sync_size;
618 } else {
619 /* Create indirect DDE (scatterlist) */
620 nx842_build_scatterlist(outbuf, max_sync_size, &slout);
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600621 op.out = nx842_get_pa(slout.entries);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500622 op.outlen = -nx842_get_scatterlist_size(&slout);
623 }
624
625 /* Send request to pHyp */
626 ret = vio_h_cop_sync(local_devdata->vdev, &op);
627
628 /* Check for pHyp error */
629 if (ret) {
630 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
631 __func__, ret, op.hcall_err);
632 dev = NULL;
633 goto sw;
634 }
635
636 /* Check for hardware error */
637 ret = nx842_validate_result(dev, &csbcpb->csb);
638 if (ret) {
639 dev = NULL;
640 goto sw;
641 }
642
643 /* HW decompression success */
644 inbuf += hdr->sizes[i];
645 outbuf += csbcpb->csb.processed_byte_count;
646 continue;
647
648sw:
649 /* software decompression */
650 size = max_sync_size;
651 ret = sw842_decompress(
652 (unsigned char *)inbuf, hdr->sizes[i],
653 (unsigned char *)outbuf, &size, wmem);
654 if (ret)
655 pr_debug("%s: sw842_decompress failed with %d\n",
656 __func__, ret);
657
658 if (ret) {
659 if (ret != -ENOSPC && ret != -EINVAL &&
660 ret != -EMSGSIZE)
661 ret = -EIO;
662 goto unlock;
663 }
664
665 /* SW decompression success */
666 inbuf += hdr->sizes[i];
667 outbuf += size;
668 }
669
670 *outlen = (unsigned int)(outbuf - (unsigned long)out);
671
672unlock:
673 if (ret)
674 /* decompress fail */
675 nx842_inc_decomp_failed(local_devdata);
676 else {
677 if (!dev)
678 /* software decompress */
679 nx842_inc_swdecomp(local_devdata);
680 nx842_inc_decomp_complete(local_devdata);
681 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
682 (get_tb() - start_time) / tb_ticks_per_usec);
683 }
684
685 rcu_read_unlock();
686 return ret;
687}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500688
689/**
690 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
691 *
692 * @devdata - struct nx842_devdata to update
693 *
694 * Returns:
695 * 0 on success
696 * -ENOENT if @devdata ptr is NULL
697 */
698static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
699{
700 if (devdata) {
701 devdata->max_sync_size = 0;
702 devdata->max_sync_sg = 0;
703 devdata->max_sg_len = 0;
704 devdata->status = UNAVAILABLE;
705 return 0;
706 } else
707 return -ENOENT;
708}
709
710/**
711 * nx842_OF_upd_status -- Update the device info from OF status prop
712 *
713 * The status property indicates if the accelerator is enabled. If the
714 * device is in the OF tree it indicates that the hardware is present.
715 * The status field indicates if the device is enabled when the status
716 * is 'okay'. Otherwise the device driver will be disabled.
717 *
718 * @devdata - struct nx842_devdata to update
719 * @prop - struct property point containing the maxsyncop for the update
720 *
721 * Returns:
722 * 0 - Device is available
723 * -EINVAL - Device is not available
724 */
725static int nx842_OF_upd_status(struct nx842_devdata *devdata,
726 struct property *prop) {
727 int ret = 0;
728 const char *status = (const char *)prop->value;
729
730 if (!strncmp(status, "okay", (size_t)prop->length)) {
731 devdata->status = AVAILABLE;
732 } else {
733 dev_info(devdata->dev, "%s: status '%s' is not 'okay'\n",
734 __func__, status);
735 devdata->status = UNAVAILABLE;
736 }
737
738 return ret;
739}
740
741/**
742 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
743 *
744 * Definition of the 'ibm,max-sg-len' OF property:
745 * This field indicates the maximum byte length of a scatter list
746 * for the platform facility. It is a single cell encoded as with encode-int.
747 *
748 * Example:
749 * # od -x ibm,max-sg-len
750 * 0000000 0000 0ff0
751 *
752 * In this example, the maximum byte length of a scatter list is
753 * 0x0ff0 (4,080).
754 *
755 * @devdata - struct nx842_devdata to update
756 * @prop - struct property point containing the maxsyncop for the update
757 *
758 * Returns:
759 * 0 on success
760 * -EINVAL on failure
761 */
762static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
763 struct property *prop) {
764 int ret = 0;
765 const int *maxsglen = prop->value;
766
767 if (prop->length != sizeof(*maxsglen)) {
768 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
769 dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__,
770 prop->length, sizeof(*maxsglen));
771 ret = -EINVAL;
772 } else {
773 devdata->max_sg_len = (unsigned int)min(*maxsglen,
774 (int)NX842_HW_PAGE_SIZE);
775 }
776
777 return ret;
778}
779
780/**
781 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
782 *
783 * Definition of the 'ibm,max-sync-cop' OF property:
784 * Two series of cells. The first series of cells represents the maximums
785 * that can be synchronously compressed. The second series of cells
786 * represents the maximums that can be synchronously decompressed.
787 * 1. The first cell in each series contains the count of the number of
788 * data length, scatter list elements pairs that follow – each being
789 * of the form
790 * a. One cell data byte length
791 * b. One cell total number of scatter list elements
792 *
793 * Example:
794 * # od -x ibm,max-sync-cop
795 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
796 * 0000020 0000 1000 0000 01fe
797 *
798 * In this example, compression supports 0x1000 (4,096) data byte length
799 * and 0x1fe (510) total scatter list elements. Decompression supports
800 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
801 * elements.
802 *
803 * @devdata - struct nx842_devdata to update
804 * @prop - struct property point containing the maxsyncop for the update
805 *
806 * Returns:
807 * 0 on success
808 * -EINVAL on failure
809 */
810static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
811 struct property *prop) {
812 int ret = 0;
813 const struct maxsynccop_t {
814 int comp_elements;
815 int comp_data_limit;
816 int comp_sg_limit;
817 int decomp_elements;
818 int decomp_data_limit;
819 int decomp_sg_limit;
820 } *maxsynccop;
821
822 if (prop->length != sizeof(*maxsynccop)) {
823 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
824 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
825 sizeof(*maxsynccop));
826 ret = -EINVAL;
827 goto out;
828 }
829
830 maxsynccop = (const struct maxsynccop_t *)prop->value;
831
832 /* Use one limit rather than separate limits for compression and
833 * decompression. Set a maximum for this so as not to exceed the
834 * size that the header can support and round the value down to
835 * the hardware page size (4K) */
836 devdata->max_sync_size =
837 (unsigned int)min(maxsynccop->comp_data_limit,
838 maxsynccop->decomp_data_limit);
839
840 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
841 SIZE_64K);
842
843 if (devdata->max_sync_size < SIZE_4K) {
844 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
845 "less than the driver minimum, unable to use "
846 "the hardware device\n",
847 __func__, devdata->max_sync_size);
848 ret = -EINVAL;
849 goto out;
850 }
851
Dan Streetman959e6652015-05-07 13:49:18 -0400852 nx842_pseries_constraints.maximum = devdata->max_sync_size;
853
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500854 devdata->max_sync_sg = (unsigned int)min(maxsynccop->comp_sg_limit,
855 maxsynccop->decomp_sg_limit);
856 if (devdata->max_sync_sg < 1) {
857 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
858 "less than the driver minimum, unable to use "
859 "the hardware device\n",
860 __func__, devdata->max_sync_sg);
861 ret = -EINVAL;
862 goto out;
863 }
864
865out:
866 return ret;
867}
868
869/**
870 *
871 * nx842_OF_upd -- Handle OF properties updates for the device.
872 *
873 * Set all properties from the OF tree. Optionally, a new property
874 * can be provided by the @new_prop pointer to overwrite an existing value.
875 * The device will remain disabled until all values are valid, this function
876 * will return an error for updates unless all values are valid.
877 *
878 * @new_prop: If not NULL, this property is being updated. If NULL, update
879 * all properties from the current values in the OF tree.
880 *
881 * Returns:
882 * 0 - Success
883 * -ENOMEM - Could not allocate memory for new devdata structure
884 * -EINVAL - property value not found, new_prop is not a recognized
885 * property for the device or property value is not valid.
886 * -ENODEV - Device is not available
887 */
888static int nx842_OF_upd(struct property *new_prop)
889{
890 struct nx842_devdata *old_devdata = NULL;
891 struct nx842_devdata *new_devdata = NULL;
892 struct device_node *of_node = NULL;
893 struct property *status = NULL;
894 struct property *maxsglen = NULL;
895 struct property *maxsyncop = NULL;
896 int ret = 0;
897 unsigned long flags;
898
899 spin_lock_irqsave(&devdata_mutex, flags);
900 old_devdata = rcu_dereference_check(devdata,
901 lockdep_is_held(&devdata_mutex));
902 if (old_devdata)
903 of_node = old_devdata->dev->of_node;
904
905 if (!old_devdata || !of_node) {
906 pr_err("%s: device is not available\n", __func__);
907 spin_unlock_irqrestore(&devdata_mutex, flags);
908 return -ENODEV;
909 }
910
911 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
912 if (!new_devdata) {
913 dev_err(old_devdata->dev, "%s: Could not allocate memory for device data\n", __func__);
914 ret = -ENOMEM;
915 goto error_out;
916 }
917
918 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
919 new_devdata->counters = old_devdata->counters;
920
921 /* Set ptrs for existing properties */
922 status = of_find_property(of_node, "status", NULL);
923 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
924 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
925 if (!status || !maxsglen || !maxsyncop) {
926 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
927 ret = -EINVAL;
928 goto error_out;
929 }
930
Grant Likely259092a2014-07-16 12:48:23 -0600931 /*
932 * If this is a property update, there are only certain properties that
933 * we care about. Bail if it isn't in the below list
934 */
935 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
936 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
937 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
938 goto out;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500939
940 /* Perform property updates */
941 ret = nx842_OF_upd_status(new_devdata, status);
942 if (ret)
943 goto error_out;
944
945 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
946 if (ret)
947 goto error_out;
948
949 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
950 if (ret)
951 goto error_out;
952
953out:
954 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
955 __func__, new_devdata->max_sync_size,
956 old_devdata->max_sync_size);
957 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
958 __func__, new_devdata->max_sync_sg,
959 old_devdata->max_sync_sg);
960 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
961 __func__, new_devdata->max_sg_len,
962 old_devdata->max_sg_len);
963
964 rcu_assign_pointer(devdata, new_devdata);
965 spin_unlock_irqrestore(&devdata_mutex, flags);
966 synchronize_rcu();
967 dev_set_drvdata(new_devdata->dev, new_devdata);
968 kfree(old_devdata);
969 return 0;
970
971error_out:
972 if (new_devdata) {
973 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
974 nx842_OF_set_defaults(new_devdata);
975 rcu_assign_pointer(devdata, new_devdata);
976 spin_unlock_irqrestore(&devdata_mutex, flags);
977 synchronize_rcu();
978 dev_set_drvdata(new_devdata->dev, new_devdata);
979 kfree(old_devdata);
980 } else {
981 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
982 spin_unlock_irqrestore(&devdata_mutex, flags);
983 }
984
985 if (!ret)
986 ret = -EINVAL;
987 return ret;
988}
989
990/**
991 * nx842_OF_notifier - Process updates to OF properties for the device
992 *
993 * @np: notifier block
994 * @action: notifier action
995 * @update: struct pSeries_reconfig_prop_update pointer if action is
996 * PSERIES_UPDATE_PROPERTY
997 *
998 * Returns:
999 * NOTIFY_OK on success
1000 * NOTIFY_BAD encoded with error number on failure, use
1001 * notifier_to_errno() to decode this value
1002 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001003static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
Grant Likelyf5242e52014-11-24 17:58:01 +00001004 void *data)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001005{
Grant Likelyf5242e52014-11-24 17:58:01 +00001006 struct of_reconfig_data *upd = data;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001007 struct nx842_devdata *local_devdata;
1008 struct device_node *node = NULL;
1009
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001010 rcu_read_lock();
1011 local_devdata = rcu_dereference(devdata);
1012 if (local_devdata)
1013 node = local_devdata->dev->of_node;
1014
1015 if (local_devdata &&
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001016 action == OF_RECONFIG_UPDATE_PROPERTY &&
1017 !strcmp(upd->dn->name, node->name)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001018 rcu_read_unlock();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001019 nx842_OF_upd(upd->prop);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001020 } else
1021 rcu_read_unlock();
1022
1023 return NOTIFY_OK;
1024}
1025
1026static struct notifier_block nx842_of_nb = {
1027 .notifier_call = nx842_OF_notifier,
1028};
1029
1030#define nx842_counter_read(_name) \
1031static ssize_t nx842_##_name##_show(struct device *dev, \
1032 struct device_attribute *attr, \
1033 char *buf) { \
1034 struct nx842_devdata *local_devdata; \
1035 int p = 0; \
1036 rcu_read_lock(); \
1037 local_devdata = rcu_dereference(devdata); \
1038 if (local_devdata) \
1039 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
1040 atomic64_read(&local_devdata->counters->_name)); \
1041 rcu_read_unlock(); \
1042 return p; \
1043}
1044
1045#define NX842DEV_COUNTER_ATTR_RO(_name) \
1046 nx842_counter_read(_name); \
1047 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
1048 0444, \
1049 nx842_##_name##_show,\
1050 NULL);
1051
1052NX842DEV_COUNTER_ATTR_RO(comp_complete);
1053NX842DEV_COUNTER_ATTR_RO(comp_failed);
1054NX842DEV_COUNTER_ATTR_RO(decomp_complete);
1055NX842DEV_COUNTER_ATTR_RO(decomp_failed);
1056NX842DEV_COUNTER_ATTR_RO(swdecomp);
1057
1058static ssize_t nx842_timehist_show(struct device *,
1059 struct device_attribute *, char *);
1060
1061static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
1062 nx842_timehist_show, NULL);
1063static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
1064 0444, nx842_timehist_show, NULL);
1065
1066static ssize_t nx842_timehist_show(struct device *dev,
1067 struct device_attribute *attr, char *buf) {
1068 char *p = buf;
1069 struct nx842_devdata *local_devdata;
1070 atomic64_t *times;
1071 int bytes_remain = PAGE_SIZE;
1072 int bytes;
1073 int i;
1074
1075 rcu_read_lock();
1076 local_devdata = rcu_dereference(devdata);
1077 if (!local_devdata) {
1078 rcu_read_unlock();
1079 return 0;
1080 }
1081
1082 if (attr == &dev_attr_comp_times)
1083 times = local_devdata->counters->comp_times;
1084 else if (attr == &dev_attr_decomp_times)
1085 times = local_devdata->counters->decomp_times;
1086 else {
1087 rcu_read_unlock();
1088 return 0;
1089 }
1090
1091 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
1092 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
1093 i ? (2<<(i-1)) : 0, (2<<i)-1,
1094 atomic64_read(&times[i]));
1095 bytes_remain -= bytes;
1096 p += bytes;
1097 }
1098 /* The last bucket holds everything over
1099 * 2<<(NX842_HIST_SLOTS - 2) us */
1100 bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
1101 2<<(NX842_HIST_SLOTS - 2),
1102 atomic64_read(&times[(NX842_HIST_SLOTS - 1)]));
1103 p += bytes;
1104
1105 rcu_read_unlock();
1106 return p - buf;
1107}
1108
1109static struct attribute *nx842_sysfs_entries[] = {
1110 &dev_attr_comp_complete.attr,
1111 &dev_attr_comp_failed.attr,
1112 &dev_attr_decomp_complete.attr,
1113 &dev_attr_decomp_failed.attr,
1114 &dev_attr_swdecomp.attr,
1115 &dev_attr_comp_times.attr,
1116 &dev_attr_decomp_times.attr,
1117 NULL,
1118};
1119
1120static struct attribute_group nx842_attribute_group = {
1121 .name = NULL, /* put in device directory */
1122 .attrs = nx842_sysfs_entries,
1123};
1124
Dan Streetman7011a122015-05-07 13:49:17 -04001125static struct nx842_driver nx842_pseries_driver = {
1126 .owner = THIS_MODULE,
Dan Streetman959e6652015-05-07 13:49:18 -04001127 .constraints = &nx842_pseries_constraints,
Dan Streetman7011a122015-05-07 13:49:17 -04001128 .compress = nx842_pseries_compress,
1129 .decompress = nx842_pseries_decompress,
1130};
1131
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001132static int __init nx842_probe(struct vio_dev *viodev,
1133 const struct vio_device_id *id)
1134{
1135 struct nx842_devdata *old_devdata, *new_devdata = NULL;
1136 unsigned long flags;
1137 int ret = 0;
1138
1139 spin_lock_irqsave(&devdata_mutex, flags);
1140 old_devdata = rcu_dereference_check(devdata,
1141 lockdep_is_held(&devdata_mutex));
1142
1143 if (old_devdata && old_devdata->vdev != NULL) {
1144 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
1145 ret = -1;
1146 goto error_unlock;
1147 }
1148
1149 dev_set_drvdata(&viodev->dev, NULL);
1150
1151 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
1152 if (!new_devdata) {
1153 dev_err(&viodev->dev, "%s: Could not allocate memory for device data\n", __func__);
1154 ret = -ENOMEM;
1155 goto error_unlock;
1156 }
1157
1158 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
1159 GFP_NOFS);
1160 if (!new_devdata->counters) {
1161 dev_err(&viodev->dev, "%s: Could not allocate memory for performance counters\n", __func__);
1162 ret = -ENOMEM;
1163 goto error_unlock;
1164 }
1165
1166 new_devdata->vdev = viodev;
1167 new_devdata->dev = &viodev->dev;
1168 nx842_OF_set_defaults(new_devdata);
1169
1170 rcu_assign_pointer(devdata, new_devdata);
1171 spin_unlock_irqrestore(&devdata_mutex, flags);
1172 synchronize_rcu();
1173 kfree(old_devdata);
1174
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001175 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001176
1177 ret = nx842_OF_upd(NULL);
1178 if (ret && ret != -ENODEV) {
1179 dev_err(&viodev->dev, "could not parse device tree. %d\n", ret);
1180 ret = -1;
1181 goto error;
1182 }
1183
1184 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001185 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001186 rcu_read_unlock();
1187
1188 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1189 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1190 ret = -1;
1191 goto error;
1192 }
1193
Dan Streetman7011a122015-05-07 13:49:17 -04001194 nx842_register_driver(&nx842_pseries_driver);
1195
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001196 return 0;
1197
1198error_unlock:
1199 spin_unlock_irqrestore(&devdata_mutex, flags);
1200 if (new_devdata)
1201 kfree(new_devdata->counters);
1202 kfree(new_devdata);
1203error:
1204 return ret;
1205}
1206
1207static int __exit nx842_remove(struct vio_dev *viodev)
1208{
1209 struct nx842_devdata *old_devdata;
1210 unsigned long flags;
1211
1212 pr_info("Removing IBM Power 842 compression device\n");
1213 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1214
1215 spin_lock_irqsave(&devdata_mutex, flags);
1216 old_devdata = rcu_dereference_check(devdata,
1217 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001218 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301219 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001220 spin_unlock_irqrestore(&devdata_mutex, flags);
1221 synchronize_rcu();
1222 dev_set_drvdata(&viodev->dev, NULL);
1223 if (old_devdata)
1224 kfree(old_devdata->counters);
1225 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001226
1227 nx842_unregister_driver(&nx842_pseries_driver);
1228
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001229 return 0;
1230}
1231
1232static struct vio_device_id nx842_driver_ids[] = {
Dan Streetman7011a122015-05-07 13:49:17 -04001233 {NX842_PSERIES_COMPAT_NAME "-v1", NX842_PSERIES_COMPAT_NAME},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001234 {"", ""},
1235};
1236
1237static struct vio_driver nx842_driver = {
1238 .name = MODULE_NAME,
1239 .probe = nx842_probe,
Jean Delvare13269ec2014-05-29 09:54:35 +02001240 .remove = __exit_p(nx842_remove),
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001241 .get_desired_dma = nx842_get_desired_dma,
1242 .id_table = nx842_driver_ids,
1243};
1244
1245static int __init nx842_init(void)
1246{
1247 struct nx842_devdata *new_devdata;
1248 pr_info("Registering IBM Power 842 compression driver\n");
1249
Dan Streetman7011a122015-05-07 13:49:17 -04001250 BUILD_BUG_ON(sizeof(struct nx842_workmem) > NX842_MEM_COMPRESS);
1251
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001252 RCU_INIT_POINTER(devdata, NULL);
1253 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1254 if (!new_devdata) {
1255 pr_err("Could not allocate memory for device data\n");
1256 return -ENOMEM;
1257 }
1258 new_devdata->status = UNAVAILABLE;
1259 RCU_INIT_POINTER(devdata, new_devdata);
1260
1261 return vio_register_driver(&nx842_driver);
1262}
1263
1264module_init(nx842_init);
1265
1266static void __exit nx842_exit(void)
1267{
1268 struct nx842_devdata *old_devdata;
1269 unsigned long flags;
1270
1271 pr_info("Exiting IBM Power 842 compression driver\n");
1272 spin_lock_irqsave(&devdata_mutex, flags);
1273 old_devdata = rcu_dereference_check(devdata,
1274 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301275 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001276 spin_unlock_irqrestore(&devdata_mutex, flags);
1277 synchronize_rcu();
1278 if (old_devdata)
1279 dev_set_drvdata(old_devdata->dev, NULL);
1280 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001281 nx842_unregister_driver(&nx842_pseries_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001282 vio_unregister_driver(&nx842_driver);
1283}
1284
1285module_exit(nx842_exit);
1286
1287/*********************************
1288 * 842 software decompressor
1289*********************************/
1290typedef int (*sw842_template_op)(const char **, int *, unsigned char **,
1291 struct sw842_fifo *);
1292
1293static int sw842_data8(const char **, int *, unsigned char **,
1294 struct sw842_fifo *);
1295static int sw842_data4(const char **, int *, unsigned char **,
1296 struct sw842_fifo *);
1297static int sw842_data2(const char **, int *, unsigned char **,
1298 struct sw842_fifo *);
1299static int sw842_ptr8(const char **, int *, unsigned char **,
1300 struct sw842_fifo *);
1301static int sw842_ptr4(const char **, int *, unsigned char **,
1302 struct sw842_fifo *);
1303static int sw842_ptr2(const char **, int *, unsigned char **,
1304 struct sw842_fifo *);
1305
1306/* special templates */
1307#define SW842_TMPL_REPEAT 0x1B
1308#define SW842_TMPL_ZEROS 0x1C
1309#define SW842_TMPL_EOF 0x1E
1310
1311static sw842_template_op sw842_tmpl_ops[26][4] = {
1312 { sw842_data8, NULL}, /* 0 (00000) */
1313 { sw842_data4, sw842_data2, sw842_ptr2, NULL},
1314 { sw842_data4, sw842_ptr2, sw842_data2, NULL},
1315 { sw842_data4, sw842_ptr2, sw842_ptr2, NULL},
1316 { sw842_data4, sw842_ptr4, NULL},
1317 { sw842_data2, sw842_ptr2, sw842_data4, NULL},
1318 { sw842_data2, sw842_ptr2, sw842_data2, sw842_ptr2},
1319 { sw842_data2, sw842_ptr2, sw842_ptr2, sw842_data2},
1320 { sw842_data2, sw842_ptr2, sw842_ptr2, sw842_ptr2,},
1321 { sw842_data2, sw842_ptr2, sw842_ptr4, NULL},
1322 { sw842_ptr2, sw842_data2, sw842_data4, NULL}, /* 10 (01010) */
1323 { sw842_ptr2, sw842_data4, sw842_ptr2, NULL},
1324 { sw842_ptr2, sw842_data2, sw842_ptr2, sw842_data2},
1325 { sw842_ptr2, sw842_data2, sw842_ptr2, sw842_ptr2},
1326 { sw842_ptr2, sw842_data2, sw842_ptr4, NULL},
1327 { sw842_ptr2, sw842_ptr2, sw842_data4, NULL},
1328 { sw842_ptr2, sw842_ptr2, sw842_data2, sw842_ptr2},
1329 { sw842_ptr2, sw842_ptr2, sw842_ptr2, sw842_data2},
1330 { sw842_ptr2, sw842_ptr2, sw842_ptr2, sw842_ptr2},
1331 { sw842_ptr2, sw842_ptr2, sw842_ptr4, NULL},
1332 { sw842_ptr4, sw842_data4, NULL}, /* 20 (10100) */
1333 { sw842_ptr4, sw842_data2, sw842_ptr2, NULL},
1334 { sw842_ptr4, sw842_ptr2, sw842_data2, NULL},
1335 { sw842_ptr4, sw842_ptr2, sw842_ptr2, NULL},
1336 { sw842_ptr4, sw842_ptr4, NULL},
1337 { sw842_ptr8, NULL}
1338};
1339
1340/* Software decompress helpers */
1341
1342static uint8_t sw842_get_byte(const char *buf, int bit)
1343{
1344 uint8_t tmpl;
1345 uint16_t tmp;
1346 tmp = htons(*(uint16_t *)(buf));
1347 tmp = (uint16_t)(tmp << bit);
1348 tmp = ntohs(tmp);
1349 memcpy(&tmpl, &tmp, 1);
1350 return tmpl;
1351}
1352
1353static uint8_t sw842_get_template(const char **buf, int *bit)
1354{
1355 uint8_t byte;
1356 byte = sw842_get_byte(*buf, *bit);
1357 byte = byte >> 3;
1358 byte &= 0x1F;
1359 *buf += (*bit + 5) / 8;
1360 *bit = (*bit + 5) % 8;
1361 return byte;
1362}
1363
1364/* repeat_count happens to be 5-bit too (like the template) */
1365static uint8_t sw842_get_repeat_count(const char **buf, int *bit)
1366{
1367 uint8_t byte;
1368 byte = sw842_get_byte(*buf, *bit);
1369 byte = byte >> 2;
1370 byte &= 0x3F;
1371 *buf += (*bit + 6) / 8;
1372 *bit = (*bit + 6) % 8;
1373 return byte;
1374}
1375
1376static uint8_t sw842_get_ptr2(const char **buf, int *bit)
1377{
1378 uint8_t ptr;
1379 ptr = sw842_get_byte(*buf, *bit);
1380 (*buf)++;
1381 return ptr;
1382}
1383
1384static uint16_t sw842_get_ptr4(const char **buf, int *bit,
1385 struct sw842_fifo *fifo)
1386{
1387 uint16_t ptr;
1388 ptr = htons(*(uint16_t *)(*buf));
1389 ptr = (uint16_t)(ptr << *bit);
1390 ptr = ptr >> 7;
1391 ptr &= 0x01FF;
1392 *buf += (*bit + 9) / 8;
1393 *bit = (*bit + 9) % 8;
1394 return ptr;
1395}
1396
1397static uint8_t sw842_get_ptr8(const char **buf, int *bit,
1398 struct sw842_fifo *fifo)
1399{
1400 return sw842_get_ptr2(buf, bit);
1401}
1402
1403/* Software decompress template ops */
1404
1405static int sw842_data8(const char **inbuf, int *inbit,
1406 unsigned char **outbuf, struct sw842_fifo *fifo)
1407{
1408 int ret;
1409
1410 ret = sw842_data4(inbuf, inbit, outbuf, fifo);
1411 if (ret)
1412 return ret;
1413 ret = sw842_data4(inbuf, inbit, outbuf, fifo);
1414 return ret;
1415}
1416
1417static int sw842_data4(const char **inbuf, int *inbit,
1418 unsigned char **outbuf, struct sw842_fifo *fifo)
1419{
1420 int ret;
1421
1422 ret = sw842_data2(inbuf, inbit, outbuf, fifo);
1423 if (ret)
1424 return ret;
1425 ret = sw842_data2(inbuf, inbit, outbuf, fifo);
1426 return ret;
1427}
1428
1429static int sw842_data2(const char **inbuf, int *inbit,
1430 unsigned char **outbuf, struct sw842_fifo *fifo)
1431{
1432 **outbuf = sw842_get_byte(*inbuf, *inbit);
1433 (*inbuf)++;
1434 (*outbuf)++;
1435 **outbuf = sw842_get_byte(*inbuf, *inbit);
1436 (*inbuf)++;
1437 (*outbuf)++;
1438 return 0;
1439}
1440
1441static int sw842_ptr8(const char **inbuf, int *inbit,
1442 unsigned char **outbuf, struct sw842_fifo *fifo)
1443{
1444 uint8_t ptr;
1445 ptr = sw842_get_ptr8(inbuf, inbit, fifo);
1446 if (!fifo->f84_full && (ptr >= fifo->f8_count))
1447 return 1;
1448 memcpy(*outbuf, fifo->f8[ptr], 8);
1449 *outbuf += 8;
1450 return 0;
1451}
1452
1453static int sw842_ptr4(const char **inbuf, int *inbit,
1454 unsigned char **outbuf, struct sw842_fifo *fifo)
1455{
1456 uint16_t ptr;
1457 ptr = sw842_get_ptr4(inbuf, inbit, fifo);
1458 if (!fifo->f84_full && (ptr >= fifo->f4_count))
1459 return 1;
1460 memcpy(*outbuf, fifo->f4[ptr], 4);
1461 *outbuf += 4;
1462 return 0;
1463}
1464
1465static int sw842_ptr2(const char **inbuf, int *inbit,
1466 unsigned char **outbuf, struct sw842_fifo *fifo)
1467{
1468 uint8_t ptr;
1469 ptr = sw842_get_ptr2(inbuf, inbit);
1470 if (!fifo->f2_full && (ptr >= fifo->f2_count))
1471 return 1;
1472 memcpy(*outbuf, fifo->f2[ptr], 2);
1473 *outbuf += 2;
1474 return 0;
1475}
1476
1477static void sw842_copy_to_fifo(const char *buf, struct sw842_fifo *fifo)
1478{
1479 unsigned char initial_f2count = fifo->f2_count;
1480
1481 memcpy(fifo->f8[fifo->f8_count], buf, 8);
1482 fifo->f4_count += 2;
1483 fifo->f8_count += 1;
1484
1485 if (!fifo->f84_full && fifo->f4_count >= 512) {
1486 fifo->f84_full = 1;
1487 fifo->f4_count /= 512;
1488 }
1489
1490 memcpy(fifo->f2[fifo->f2_count++], buf, 2);
1491 memcpy(fifo->f2[fifo->f2_count++], buf + 2, 2);
1492 memcpy(fifo->f2[fifo->f2_count++], buf + 4, 2);
1493 memcpy(fifo->f2[fifo->f2_count++], buf + 6, 2);
1494 if (fifo->f2_count < initial_f2count)
1495 fifo->f2_full = 1;
1496}
1497
1498static int sw842_decompress(const unsigned char *src, int srclen,
1499 unsigned char *dst, int *destlen,
1500 const void *wrkmem)
1501{
1502 uint8_t tmpl;
1503 const char *inbuf;
1504 int inbit = 0;
1505 unsigned char *outbuf, *outbuf_end, *origbuf, *prevbuf;
1506 const char *inbuf_end;
1507 sw842_template_op op;
1508 int opindex;
1509 int i, repeat_count;
1510 struct sw842_fifo *fifo;
1511 int ret = 0;
1512
1513 fifo = &((struct nx842_workmem *)(wrkmem))->swfifo;
1514 memset(fifo, 0, sizeof(*fifo));
1515
1516 origbuf = NULL;
1517 inbuf = src;
1518 inbuf_end = src + srclen;
1519 outbuf = dst;
1520 outbuf_end = dst + *destlen;
1521
1522 while ((tmpl = sw842_get_template(&inbuf, &inbit)) != SW842_TMPL_EOF) {
1523 if (inbuf >= inbuf_end) {
1524 ret = -EINVAL;
1525 goto out;
1526 }
1527
1528 opindex = 0;
1529 prevbuf = origbuf;
1530 origbuf = outbuf;
1531 switch (tmpl) {
1532 case SW842_TMPL_REPEAT:
1533 if (prevbuf == NULL) {
1534 ret = -EINVAL;
1535 goto out;
1536 }
1537
1538 repeat_count = sw842_get_repeat_count(&inbuf,
1539 &inbit) + 1;
1540
1541 /* Did the repeat count advance past the end of input */
1542 if (inbuf > inbuf_end) {
1543 ret = -EINVAL;
1544 goto out;
1545 }
1546
1547 for (i = 0; i < repeat_count; i++) {
1548 /* Would this overflow the output buffer */
1549 if ((outbuf + 8) > outbuf_end) {
1550 ret = -ENOSPC;
1551 goto out;
1552 }
1553
1554 memcpy(outbuf, prevbuf, 8);
1555 sw842_copy_to_fifo(outbuf, fifo);
1556 outbuf += 8;
1557 }
1558 break;
1559
1560 case SW842_TMPL_ZEROS:
1561 /* Would this overflow the output buffer */
1562 if ((outbuf + 8) > outbuf_end) {
1563 ret = -ENOSPC;
1564 goto out;
1565 }
1566
1567 memset(outbuf, 0, 8);
1568 sw842_copy_to_fifo(outbuf, fifo);
1569 outbuf += 8;
1570 break;
1571
1572 default:
1573 if (tmpl > 25) {
1574 ret = -EINVAL;
1575 goto out;
1576 }
1577
1578 /* Does this go past the end of the input buffer */
1579 if ((inbuf + 2) > inbuf_end) {
1580 ret = -EINVAL;
1581 goto out;
1582 }
1583
1584 /* Would this overflow the output buffer */
1585 if ((outbuf + 8) > outbuf_end) {
1586 ret = -ENOSPC;
1587 goto out;
1588 }
1589
1590 while (opindex < 4 &&
1591 (op = sw842_tmpl_ops[tmpl][opindex++])
1592 != NULL) {
1593 ret = (*op)(&inbuf, &inbit, &outbuf, fifo);
1594 if (ret) {
1595 ret = -EINVAL;
1596 goto out;
1597 }
1598 sw842_copy_to_fifo(origbuf, fifo);
1599 }
1600 }
1601 }
1602
1603out:
1604 if (!ret)
1605 *destlen = (unsigned int)(outbuf - dst);
1606 else
1607 *destlen = 0;
1608
1609 return ret;
1610}