1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for IBM Power 842 compression accelerator
5 * Copyright (C) IBM Corporation, 2012
12 #include <asm/hvcall.h>
16 #include "nx_csbcpb.h" /* struct nx_csbcpb */
18 MODULE_LICENSE("GPL");
20 MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors");
21 MODULE_ALIAS_CRYPTO("842");
22 MODULE_ALIAS_CRYPTO("842-nx");
25 * Coprocessor type specific capabilities from the hypervisor.
27 struct hv_nx_cop_caps {
29 __be64 req_max_processed_len; /* Max bytes in one GZIP request */
30 __be64 min_compress_len; /* Min compression size in bytes */
31 __be64 min_decompress_len; /* Min decompression size in bytes */
32 } __packed __aligned(0x1000);
35 * Coprocessor type specific capabilities.
39 u64 req_max_processed_len; /* Max bytes in one GZIP request */
40 u64 min_compress_len; /* Min compression in bytes */
41 u64 min_decompress_len; /* Min decompression in bytes */
45 static struct nx_cop_caps nx_cop_caps;
47 static struct nx842_constraints nx842_pseries_constraints = {
48 .alignment = DDE_BUFFER_ALIGN,
49 .multiple = DDE_BUFFER_LAST_MULT,
50 .minimum = DDE_BUFFER_LAST_MULT,
51 .maximum = PAGE_SIZE, /* dynamic, max_sync_size */
54 static int check_constraints(unsigned long buf, unsigned int *len, bool in)
56 if (!IS_ALIGNED(buf, nx842_pseries_constraints.alignment)) {
57 pr_debug("%s buffer 0x%lx not aligned to 0x%x\n",
58 in ? "input" : "output", buf,
59 nx842_pseries_constraints.alignment);
62 if (*len % nx842_pseries_constraints.multiple) {
63 pr_debug("%s buffer len 0x%x not multiple of 0x%x\n",
64 in ? "input" : "output", *len,
65 nx842_pseries_constraints.multiple);
68 *len = round_down(*len, nx842_pseries_constraints.multiple);
70 if (*len < nx842_pseries_constraints.minimum) {
71 pr_debug("%s buffer len 0x%x under minimum 0x%x\n",
72 in ? "input" : "output", *len,
73 nx842_pseries_constraints.minimum);
76 if (*len > nx842_pseries_constraints.maximum) {
77 pr_debug("%s buffer len 0x%x over maximum 0x%x\n",
78 in ? "input" : "output", *len,
79 nx842_pseries_constraints.maximum);
82 *len = nx842_pseries_constraints.maximum;
87 /* I assume we need to align the CSB? */
88 #define WORKMEM_ALIGN (256)
90 struct nx842_workmem {
94 /* coprocessor status/parameter block */
95 struct nx_csbcpb csbcpb;
97 char padding[WORKMEM_ALIGN];
98 } __aligned(WORKMEM_ALIGN);
100 /* Macros for fields within nx_csbcpb */
101 /* Check the valid bit within the csbcpb valid field */
102 #define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7))
104 /* CE macros operate on the completion_extension field bits in the csbcpb.
105 * CE0 0=full completion, 1=partial completion
106 * CE1 0=CE0 indicates completion, 1=termination (output may be modified)
107 * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */
108 #define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7))
109 #define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6))
110 #define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5))
112 /* The NX unit accepts data only on 4K page boundaries */
113 #define NX842_HW_PAGE_SIZE (4096)
114 #define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
116 struct ibm_nx842_counters {
117 atomic64_t comp_complete;
118 atomic64_t comp_failed;
119 atomic64_t decomp_complete;
120 atomic64_t decomp_failed;
122 atomic64_t comp_times[32];
123 atomic64_t decomp_times[32];
126 struct nx842_devdata {
127 struct vio_dev *vdev;
129 struct ibm_nx842_counters *counters;
130 unsigned int max_sg_len;
131 unsigned int max_sync_size;
132 unsigned int max_sync_sg;
135 static struct nx842_devdata __rcu *devdata;
136 static DEFINE_SPINLOCK(devdata_spinlock);
138 #define NX842_COUNTER_INC(_x) \
139 static inline void nx842_inc_##_x( \
140 const struct nx842_devdata *dev) { \
142 atomic64_inc(&dev->counters->_x); \
144 NX842_COUNTER_INC(comp_complete);
145 NX842_COUNTER_INC(comp_failed);
146 NX842_COUNTER_INC(decomp_complete);
147 NX842_COUNTER_INC(decomp_failed);
148 NX842_COUNTER_INC(swdecomp);
150 #define NX842_HIST_SLOTS 16
152 static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
154 int bucket = fls(time);
157 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
159 atomic64_inc(×[bucket]);
162 /* NX unit operation flags */
163 #define NX842_OP_COMPRESS 0x0
164 #define NX842_OP_CRC 0x1
165 #define NX842_OP_DECOMPRESS 0x2
166 #define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
167 #define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
168 #define NX842_OP_ASYNC (1<<23)
169 #define NX842_OP_NOTIFY (1<<22)
170 #define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
172 static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
174 /* No use of DMA mappings within the driver. */
178 struct nx842_slentry {
179 __be64 ptr; /* Real address (use __pa()) */
183 /* pHyp scatterlist entry */
184 struct nx842_scatterlist {
185 int entry_nr; /* number of slentries */
186 struct nx842_slentry *entries; /* ptr to array of slentries */
189 /* Does not include sizeof(entry_nr) in the size */
190 static inline unsigned long nx842_get_scatterlist_size(
191 struct nx842_scatterlist *sl)
193 return sl->entry_nr * sizeof(struct nx842_slentry);
196 static int nx842_build_scatterlist(unsigned long buf, int len,
197 struct nx842_scatterlist *sl)
199 unsigned long entrylen;
200 struct nx842_slentry *entry;
206 entry->ptr = cpu_to_be64(nx842_get_pa((void *)buf));
207 entrylen = min_t(int, len,
208 LEN_ON_SIZE(buf, NX842_HW_PAGE_SIZE));
209 entry->len = cpu_to_be64(entrylen);
221 static int nx842_validate_result(struct device *dev,
222 struct cop_status_block *csb)
224 /* The csb must be valid after returning from vio_h_cop_sync */
225 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
226 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
228 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
231 csb->completion_code,
232 csb->completion_extension);
233 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
234 be32_to_cpu(csb->processed_byte_count),
235 (unsigned long)be64_to_cpu(csb->address));
239 /* Check return values from the hardware in the CSB */
240 switch (csb->completion_code) {
241 case 0: /* Completed without error */
243 case 64: /* Compression ok, but output larger than input */
244 dev_dbg(dev, "%s: output size larger than input size\n",
247 case 13: /* Output buffer too small */
248 dev_dbg(dev, "%s: Out of space in output buffer\n",
251 case 65: /* Calculated CRC doesn't match the passed value */
252 dev_dbg(dev, "%s: CRC mismatch for decompression\n",
255 case 66: /* Input data contains an illegal template field */
256 case 67: /* Template indicates data past the end of the input stream */
257 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
258 __func__, csb->completion_code);
261 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
262 __func__, csb->completion_code);
266 /* Hardware sanity check */
267 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
268 dev_err(dev, "%s: No error returned by hardware, but "
269 "data returned is unusable, contact support.\n"
270 "(Additional info: csbcbp->processed bytes "
271 "does not specify processed bytes for the "
272 "target buffer.)\n", __func__);
280 * nx842_pseries_compress - Compress data using the 842 algorithm
282 * Compression provide by the NX842 coprocessor on IBM Power systems.
283 * The input buffer is compressed and the result is stored in the
284 * provided output buffer.
286 * Upon return from this function @outlen contains the length of the
287 * compressed data. If there is an error then @outlen will be 0 and an
288 * error will be specified by the return code from this function.
290 * @in: Pointer to input buffer
291 * @inlen: Length of input buffer
292 * @out: Pointer to output buffer
293 * @outlen: Length of output buffer
294 * @wmem: ptr to buffer for working memory, size determined by
295 * nx842_pseries_driver.workmem_size
298 * 0 Success, output of length @outlen stored in the buffer at @out
299 * -ENOMEM Unable to allocate internal buffers
300 * -ENOSPC Output buffer is to small
301 * -EIO Internal error
302 * -ENODEV Hardware unavailable
304 static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
305 unsigned char *out, unsigned int *outlen,
308 struct nx842_devdata *local_devdata;
309 struct device *dev = NULL;
310 struct nx842_workmem *workmem;
311 struct nx842_scatterlist slin, slout;
312 struct nx_csbcpb *csbcpb;
314 unsigned long inbuf, outbuf;
315 struct vio_pfo_op op = {
320 unsigned long start = get_tb();
322 inbuf = (unsigned long)in;
323 if (check_constraints(inbuf, &inlen, true))
326 outbuf = (unsigned long)out;
327 if (check_constraints(outbuf, outlen, false))
331 local_devdata = rcu_dereference(devdata);
332 if (!local_devdata || !local_devdata->dev) {
336 dev = local_devdata->dev;
338 /* Init scatterlist */
339 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
340 slin.entries = (struct nx842_slentry *)workmem->slin;
341 slout.entries = (struct nx842_slentry *)workmem->slout;
344 op.flags = NX842_OP_COMPRESS_CRC;
345 csbcpb = &workmem->csbcpb;
346 memset(csbcpb, 0, sizeof(*csbcpb));
347 op.csbcpb = nx842_get_pa(csbcpb);
349 if ((inbuf & NX842_HW_PAGE_MASK) ==
350 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
351 /* Create direct DDE */
352 op.in = nx842_get_pa((void *)inbuf);
355 /* Create indirect DDE (scatterlist) */
356 nx842_build_scatterlist(inbuf, inlen, &slin);
357 op.in = nx842_get_pa(slin.entries);
358 op.inlen = -nx842_get_scatterlist_size(&slin);
361 if ((outbuf & NX842_HW_PAGE_MASK) ==
362 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
363 /* Create direct DDE */
364 op.out = nx842_get_pa((void *)outbuf);
367 /* Create indirect DDE (scatterlist) */
368 nx842_build_scatterlist(outbuf, *outlen, &slout);
369 op.out = nx842_get_pa(slout.entries);
370 op.outlen = -nx842_get_scatterlist_size(&slout);
373 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
374 __func__, (unsigned long)op.in, (long)op.inlen,
375 (unsigned long)op.out, (long)op.outlen);
377 /* Send request to pHyp */
378 ret = vio_h_cop_sync(local_devdata->vdev, &op);
380 /* Check for pHyp error */
382 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
383 __func__, ret, op.hcall_err);
388 /* Check for hardware error */
389 ret = nx842_validate_result(dev, &csbcpb->csb);
393 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
394 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
398 nx842_inc_comp_failed(local_devdata);
400 nx842_inc_comp_complete(local_devdata);
401 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
402 (get_tb() - start) / tb_ticks_per_usec);
409 * nx842_pseries_decompress - Decompress data using the 842 algorithm
411 * Decompression provide by the NX842 coprocessor on IBM Power systems.
412 * The input buffer is decompressed and the result is stored in the
413 * provided output buffer. The size allocated to the output buffer is
414 * provided by the caller of this function in @outlen. Upon return from
415 * this function @outlen contains the length of the decompressed data.
416 * If there is an error then @outlen will be 0 and an error will be
417 * specified by the return code from this function.
419 * @in: Pointer to input buffer
420 * @inlen: Length of input buffer
421 * @out: Pointer to output buffer
422 * @outlen: Length of output buffer
423 * @wmem: ptr to buffer for working memory, size determined by
424 * nx842_pseries_driver.workmem_size
427 * 0 Success, output of length @outlen stored in the buffer at @out
428 * -ENODEV Hardware decompression device is unavailable
429 * -ENOMEM Unable to allocate internal buffers
430 * -ENOSPC Output buffer is to small
431 * -EINVAL Bad input data encountered when attempting decompress
432 * -EIO Internal error
434 static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
435 unsigned char *out, unsigned int *outlen,
438 struct nx842_devdata *local_devdata;
439 struct device *dev = NULL;
440 struct nx842_workmem *workmem;
441 struct nx842_scatterlist slin, slout;
442 struct nx_csbcpb *csbcpb;
444 unsigned long inbuf, outbuf;
445 struct vio_pfo_op op = {
450 unsigned long start = get_tb();
452 /* Ensure page alignment and size */
453 inbuf = (unsigned long)in;
454 if (check_constraints(inbuf, &inlen, true))
457 outbuf = (unsigned long)out;
458 if (check_constraints(outbuf, outlen, false))
462 local_devdata = rcu_dereference(devdata);
463 if (!local_devdata || !local_devdata->dev) {
467 dev = local_devdata->dev;
469 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
471 /* Init scatterlist */
472 slin.entries = (struct nx842_slentry *)workmem->slin;
473 slout.entries = (struct nx842_slentry *)workmem->slout;
476 op.flags = NX842_OP_DECOMPRESS_CRC;
477 csbcpb = &workmem->csbcpb;
478 memset(csbcpb, 0, sizeof(*csbcpb));
479 op.csbcpb = nx842_get_pa(csbcpb);
481 if ((inbuf & NX842_HW_PAGE_MASK) ==
482 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
483 /* Create direct DDE */
484 op.in = nx842_get_pa((void *)inbuf);
487 /* Create indirect DDE (scatterlist) */
488 nx842_build_scatterlist(inbuf, inlen, &slin);
489 op.in = nx842_get_pa(slin.entries);
490 op.inlen = -nx842_get_scatterlist_size(&slin);
493 if ((outbuf & NX842_HW_PAGE_MASK) ==
494 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
495 /* Create direct DDE */
496 op.out = nx842_get_pa((void *)outbuf);
499 /* Create indirect DDE (scatterlist) */
500 nx842_build_scatterlist(outbuf, *outlen, &slout);
501 op.out = nx842_get_pa(slout.entries);
502 op.outlen = -nx842_get_scatterlist_size(&slout);
505 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
506 __func__, (unsigned long)op.in, (long)op.inlen,
507 (unsigned long)op.out, (long)op.outlen);
509 /* Send request to pHyp */
510 ret = vio_h_cop_sync(local_devdata->vdev, &op);
512 /* Check for pHyp error */
514 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
515 __func__, ret, op.hcall_err);
519 /* Check for hardware error */
520 ret = nx842_validate_result(dev, &csbcpb->csb);
524 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
528 /* decompress fail */
529 nx842_inc_decomp_failed(local_devdata);
531 nx842_inc_decomp_complete(local_devdata);
532 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
533 (get_tb() - start) / tb_ticks_per_usec);
541 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
543 * @devdata: struct nx842_devdata to update
547 * -ENOENT if @devdata ptr is NULL
549 static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
552 devdata->max_sync_size = 0;
553 devdata->max_sync_sg = 0;
554 devdata->max_sg_len = 0;
561 * nx842_OF_upd_status -- Check the device info from OF status prop
563 * The status property indicates if the accelerator is enabled. If the
564 * device is in the OF tree it indicates that the hardware is present.
565 * The status field indicates if the device is enabled when the status
566 * is 'okay'. Otherwise the device driver will be disabled.
568 * @devdata: struct nx842_devdata to use for dev_info
569 * @prop: struct property point containing the maxsyncop for the update
572 * 0 - Device is available
573 * -ENODEV - Device is not available
575 static int nx842_OF_upd_status(struct nx842_devdata *devdata,
576 struct property *prop)
578 const char *status = (const char *)prop->value;
580 if (!strncmp(status, "okay", (size_t)prop->length))
582 if (!strncmp(status, "disabled", (size_t)prop->length))
584 dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status);
590 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
592 * Definition of the 'ibm,max-sg-len' OF property:
593 * This field indicates the maximum byte length of a scatter list
594 * for the platform facility. It is a single cell encoded as with encode-int.
597 * # od -x ibm,max-sg-len
600 * In this example, the maximum byte length of a scatter list is
603 * @devdata: struct nx842_devdata to update
604 * @prop: struct property point containing the maxsyncop for the update
610 static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
611 struct property *prop) {
613 const unsigned int maxsglen = of_read_number(prop->value, 1);
615 if (prop->length != sizeof(maxsglen)) {
616 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
617 dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__,
618 prop->length, sizeof(maxsglen));
621 devdata->max_sg_len = min_t(unsigned int,
622 maxsglen, NX842_HW_PAGE_SIZE);
629 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
631 * Definition of the 'ibm,max-sync-cop' OF property:
632 * Two series of cells. The first series of cells represents the maximums
633 * that can be synchronously compressed. The second series of cells
634 * represents the maximums that can be synchronously decompressed.
635 * 1. The first cell in each series contains the count of the number of
636 * data length, scatter list elements pairs that follow – each being
638 * a. One cell data byte length
639 * b. One cell total number of scatter list elements
642 * # od -x ibm,max-sync-cop
643 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
644 * 0000020 0000 1000 0000 01fe
646 * In this example, compression supports 0x1000 (4,096) data byte length
647 * and 0x1fe (510) total scatter list elements. Decompression supports
648 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
651 * @devdata: struct nx842_devdata to update
652 * @prop: struct property point containing the maxsyncop for the update
658 static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
659 struct property *prop) {
661 unsigned int comp_data_limit, decomp_data_limit;
662 unsigned int comp_sg_limit, decomp_sg_limit;
663 const struct maxsynccop_t {
664 __be32 comp_elements;
665 __be32 comp_data_limit;
666 __be32 comp_sg_limit;
667 __be32 decomp_elements;
668 __be32 decomp_data_limit;
669 __be32 decomp_sg_limit;
672 if (prop->length != sizeof(*maxsynccop)) {
673 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
674 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
675 sizeof(*maxsynccop));
680 maxsynccop = (const struct maxsynccop_t *)prop->value;
681 comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit);
682 comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit);
683 decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit);
684 decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit);
686 /* Use one limit rather than separate limits for compression and
687 * decompression. Set a maximum for this so as not to exceed the
688 * size that the header can support and round the value down to
689 * the hardware page size (4K) */
690 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
692 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
695 if (devdata->max_sync_size < 4096) {
696 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
697 "less than the driver minimum, unable to use "
698 "the hardware device\n",
699 __func__, devdata->max_sync_size);
704 nx842_pseries_constraints.maximum = devdata->max_sync_size;
706 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
707 if (devdata->max_sync_sg < 1) {
708 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
709 "less than the driver minimum, unable to use "
710 "the hardware device\n",
711 __func__, devdata->max_sync_sg);
721 * nx842_OF_upd -- Handle OF properties updates for the device.
723 * Set all properties from the OF tree. Optionally, a new property
724 * can be provided by the @new_prop pointer to overwrite an existing value.
725 * The device will remain disabled until all values are valid, this function
726 * will return an error for updates unless all values are valid.
728 * @new_prop: If not NULL, this property is being updated. If NULL, update
729 * all properties from the current values in the OF tree.
733 * -ENOMEM - Could not allocate memory for new devdata structure
734 * -EINVAL - property value not found, new_prop is not a recognized
735 * property for the device or property value is not valid.
736 * -ENODEV - Device is not available
738 static int nx842_OF_upd(struct property *new_prop)
740 struct nx842_devdata *old_devdata = NULL;
741 struct nx842_devdata *new_devdata = NULL;
742 struct device_node *of_node = NULL;
743 struct property *status = NULL;
744 struct property *maxsglen = NULL;
745 struct property *maxsyncop = NULL;
749 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
753 spin_lock_irqsave(&devdata_spinlock, flags);
754 old_devdata = rcu_dereference_check(devdata,
755 lockdep_is_held(&devdata_spinlock));
757 of_node = old_devdata->dev->of_node;
759 if (!old_devdata || !of_node) {
760 pr_err("%s: device is not available\n", __func__);
761 spin_unlock_irqrestore(&devdata_spinlock, flags);
766 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
767 new_devdata->counters = old_devdata->counters;
769 /* Set ptrs for existing properties */
770 status = of_find_property(of_node, "status", NULL);
771 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
772 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
773 if (!status || !maxsglen || !maxsyncop) {
774 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
780 * If this is a property update, there are only certain properties that
781 * we care about. Bail if it isn't in the below list
783 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
784 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
785 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
788 /* Perform property updates */
789 ret = nx842_OF_upd_status(new_devdata, status);
793 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
797 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
802 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
803 __func__, new_devdata->max_sync_size,
804 old_devdata->max_sync_size);
805 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
806 __func__, new_devdata->max_sync_sg,
807 old_devdata->max_sync_sg);
808 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
809 __func__, new_devdata->max_sg_len,
810 old_devdata->max_sg_len);
812 rcu_assign_pointer(devdata, new_devdata);
813 spin_unlock_irqrestore(&devdata_spinlock, flags);
815 dev_set_drvdata(new_devdata->dev, new_devdata);
821 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
822 nx842_OF_set_defaults(new_devdata);
823 rcu_assign_pointer(devdata, new_devdata);
824 spin_unlock_irqrestore(&devdata_spinlock, flags);
826 dev_set_drvdata(new_devdata->dev, new_devdata);
829 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
830 spin_unlock_irqrestore(&devdata_spinlock, flags);
839 * nx842_OF_notifier - Process updates to OF properties for the device
841 * @np: notifier block
842 * @action: notifier action
843 * @data: struct of_reconfig_data pointer
846 * NOTIFY_OK on success
847 * NOTIFY_BAD encoded with error number on failure, use
848 * notifier_to_errno() to decode this value
850 static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
853 struct of_reconfig_data *upd = data;
854 struct nx842_devdata *local_devdata;
855 struct device_node *node = NULL;
858 local_devdata = rcu_dereference(devdata);
860 node = local_devdata->dev->of_node;
863 action == OF_RECONFIG_UPDATE_PROPERTY &&
864 !strcmp(upd->dn->name, node->name)) {
866 nx842_OF_upd(upd->prop);
873 static struct notifier_block nx842_of_nb = {
874 .notifier_call = nx842_OF_notifier,
877 #define nx842_counter_read(_name) \
878 static ssize_t nx842_##_name##_show(struct device *dev, \
879 struct device_attribute *attr, \
881 struct nx842_devdata *local_devdata; \
884 local_devdata = rcu_dereference(devdata); \
886 p = snprintf(buf, PAGE_SIZE, "%lld\n", \
887 atomic64_read(&local_devdata->counters->_name)); \
892 #define NX842DEV_COUNTER_ATTR_RO(_name) \
893 nx842_counter_read(_name); \
894 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
896 nx842_##_name##_show,\
899 NX842DEV_COUNTER_ATTR_RO(comp_complete);
900 NX842DEV_COUNTER_ATTR_RO(comp_failed);
901 NX842DEV_COUNTER_ATTR_RO(decomp_complete);
902 NX842DEV_COUNTER_ATTR_RO(decomp_failed);
903 NX842DEV_COUNTER_ATTR_RO(swdecomp);
905 static ssize_t nx842_timehist_show(struct device *,
906 struct device_attribute *, char *);
908 static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
909 nx842_timehist_show, NULL);
910 static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
911 0444, nx842_timehist_show, NULL);
913 static ssize_t nx842_timehist_show(struct device *dev,
914 struct device_attribute *attr, char *buf) {
916 struct nx842_devdata *local_devdata;
918 int bytes_remain = PAGE_SIZE;
923 local_devdata = rcu_dereference(devdata);
924 if (!local_devdata) {
929 if (attr == &dev_attr_comp_times)
930 times = local_devdata->counters->comp_times;
931 else if (attr == &dev_attr_decomp_times)
932 times = local_devdata->counters->decomp_times;
938 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
939 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%lld\n",
940 i ? (2<<(i-1)) : 0, (2<<i)-1,
941 atomic64_read(×[i]));
942 bytes_remain -= bytes;
945 /* The last bucket holds everything over
946 * 2<<(NX842_HIST_SLOTS - 2) us */
947 bytes = snprintf(p, bytes_remain, "%uus - :\t%lld\n",
948 2<<(NX842_HIST_SLOTS - 2),
949 atomic64_read(×[(NX842_HIST_SLOTS - 1)]));
956 static struct attribute *nx842_sysfs_entries[] = {
957 &dev_attr_comp_complete.attr,
958 &dev_attr_comp_failed.attr,
959 &dev_attr_decomp_complete.attr,
960 &dev_attr_decomp_failed.attr,
961 &dev_attr_swdecomp.attr,
962 &dev_attr_comp_times.attr,
963 &dev_attr_decomp_times.attr,
967 static const struct attribute_group nx842_attribute_group = {
968 .name = NULL, /* put in device directory */
969 .attrs = nx842_sysfs_entries,
972 #define nxcop_caps_read(_name) \
973 static ssize_t nxcop_##_name##_show(struct device *dev, \
974 struct device_attribute *attr, char *buf) \
976 return sprintf(buf, "%lld\n", nx_cop_caps._name); \
979 #define NXCT_ATTR_RO(_name) \
980 nxcop_caps_read(_name); \
981 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
983 nxcop_##_name##_show, \
986 NXCT_ATTR_RO(req_max_processed_len);
987 NXCT_ATTR_RO(min_compress_len);
988 NXCT_ATTR_RO(min_decompress_len);
990 static struct attribute *nxcop_caps_sysfs_entries[] = {
991 &dev_attr_req_max_processed_len.attr,
992 &dev_attr_min_compress_len.attr,
993 &dev_attr_min_decompress_len.attr,
997 static const struct attribute_group nxcop_caps_attr_group = {
998 .name = "nx_gzip_caps",
999 .attrs = nxcop_caps_sysfs_entries,
1002 static struct nx842_driver nx842_pseries_driver = {
1003 .name = KBUILD_MODNAME,
1004 .owner = THIS_MODULE,
1005 .workmem_size = sizeof(struct nx842_workmem),
1006 .constraints = &nx842_pseries_constraints,
1007 .compress = nx842_pseries_compress,
1008 .decompress = nx842_pseries_decompress,
1011 static int nx842_pseries_crypto_init(struct crypto_tfm *tfm)
1013 return nx842_crypto_init(tfm, &nx842_pseries_driver);
1016 static struct crypto_alg nx842_pseries_alg = {
1018 .cra_driver_name = "842-nx",
1019 .cra_priority = 300,
1020 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
1021 .cra_ctxsize = sizeof(struct nx842_crypto_ctx),
1022 .cra_module = THIS_MODULE,
1023 .cra_init = nx842_pseries_crypto_init,
1024 .cra_exit = nx842_crypto_exit,
1025 .cra_u = { .compress = {
1026 .coa_compress = nx842_crypto_compress,
1027 .coa_decompress = nx842_crypto_decompress } }
1030 static int nx842_probe(struct vio_dev *viodev,
1031 const struct vio_device_id *id)
1033 struct nx842_devdata *old_devdata, *new_devdata = NULL;
1034 unsigned long flags;
1037 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
1041 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
1043 if (!new_devdata->counters) {
1048 spin_lock_irqsave(&devdata_spinlock, flags);
1049 old_devdata = rcu_dereference_check(devdata,
1050 lockdep_is_held(&devdata_spinlock));
1052 if (old_devdata && old_devdata->vdev != NULL) {
1053 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
1058 dev_set_drvdata(&viodev->dev, NULL);
1060 new_devdata->vdev = viodev;
1061 new_devdata->dev = &viodev->dev;
1062 nx842_OF_set_defaults(new_devdata);
1064 rcu_assign_pointer(devdata, new_devdata);
1065 spin_unlock_irqrestore(&devdata_spinlock, flags);
1069 of_reconfig_notifier_register(&nx842_of_nb);
1071 ret = nx842_OF_upd(NULL);
1075 ret = crypto_register_alg(&nx842_pseries_alg);
1077 dev_err(&viodev->dev, "could not register comp alg: %d\n", ret);
1082 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
1085 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1086 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1092 if (sysfs_create_group(&viodev->dev.kobj,
1093 &nxcop_caps_attr_group)) {
1094 dev_err(&viodev->dev,
1095 "Could not create sysfs NX capability entries\n");
1104 spin_unlock_irqrestore(&devdata_spinlock, flags);
1106 kfree(new_devdata->counters);
1112 static void nx842_remove(struct vio_dev *viodev)
1114 struct nx842_devdata *old_devdata;
1115 unsigned long flags;
1117 pr_info("Removing IBM Power 842 compression device\n");
1118 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1121 sysfs_remove_group(&viodev->dev.kobj, &nxcop_caps_attr_group);
1123 crypto_unregister_alg(&nx842_pseries_alg);
1125 of_reconfig_notifier_unregister(&nx842_of_nb);
1127 spin_lock_irqsave(&devdata_spinlock, flags);
1128 old_devdata = rcu_dereference_check(devdata,
1129 lockdep_is_held(&devdata_spinlock));
1130 RCU_INIT_POINTER(devdata, NULL);
1131 spin_unlock_irqrestore(&devdata_spinlock, flags);
1133 dev_set_drvdata(&viodev->dev, NULL);
1135 kfree(old_devdata->counters);
1140 * Get NX capabilities from the hypervisor.
1141 * Only NXGZIP capabilities are provided by the hypersvisor right
1142 * now and these values are available to user space with sysfs.
1144 static void __init nxcop_get_capabilities(void)
1146 struct hv_vas_all_caps *hv_caps;
1147 struct hv_nx_cop_caps *hv_nxc;
1150 hv_caps = kmalloc(sizeof(*hv_caps), GFP_KERNEL);
1154 * Get NX overall capabilities with feature type=0
1156 rc = h_query_vas_capabilities(H_QUERY_NX_CAPABILITIES, 0,
1157 (u64)virt_to_phys(hv_caps));
1161 caps_feat = be64_to_cpu(hv_caps->feat_type);
1163 * NX-GZIP feature available
1165 if (caps_feat & VAS_NX_GZIP_FEAT_BIT) {
1166 hv_nxc = kmalloc(sizeof(*hv_nxc), GFP_KERNEL);
1170 * Get capabilities for NX-GZIP feature
1172 rc = h_query_vas_capabilities(H_QUERY_NX_CAPABILITIES,
1174 (u64)virt_to_phys(hv_nxc));
1176 pr_err("NX-GZIP feature is not available\n");
1181 nx_cop_caps.descriptor = be64_to_cpu(hv_nxc->descriptor);
1182 nx_cop_caps.req_max_processed_len =
1183 be64_to_cpu(hv_nxc->req_max_processed_len);
1184 nx_cop_caps.min_compress_len =
1185 be64_to_cpu(hv_nxc->min_compress_len);
1186 nx_cop_caps.min_decompress_len =
1187 be64_to_cpu(hv_nxc->min_decompress_len);
1197 static const struct vio_device_id nx842_vio_driver_ids[] = {
1198 {"ibm,compression-v1", "ibm,compression"},
1201 MODULE_DEVICE_TABLE(vio, nx842_vio_driver_ids);
1203 static struct vio_driver nx842_vio_driver = {
1204 .name = KBUILD_MODNAME,
1205 .probe = nx842_probe,
1206 .remove = nx842_remove,
1207 .get_desired_dma = nx842_get_desired_dma,
1208 .id_table = nx842_vio_driver_ids,
1211 static int __init nx842_pseries_init(void)
1213 struct nx842_devdata *new_devdata;
1214 struct device_node *np;
1217 np = of_find_compatible_node(NULL, NULL, "ibm,compression");
1222 RCU_INIT_POINTER(devdata, NULL);
1223 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1227 RCU_INIT_POINTER(devdata, new_devdata);
1229 * Get NX capabilities from the hypervisor.
1231 nxcop_get_capabilities();
1233 ret = vio_register_driver(&nx842_vio_driver);
1235 pr_err("Could not register VIO driver %d\n", ret);
1241 ret = vas_register_api_pseries(THIS_MODULE, VAS_COP_TYPE_GZIP,
1245 pr_err("NX-GZIP is not supported. Returned=%d\n", ret);
1250 module_init(nx842_pseries_init);
1252 static void __exit nx842_pseries_exit(void)
1254 struct nx842_devdata *old_devdata;
1255 unsigned long flags;
1257 vas_unregister_api_pseries();
1259 crypto_unregister_alg(&nx842_pseries_alg);
1261 spin_lock_irqsave(&devdata_spinlock, flags);
1262 old_devdata = rcu_dereference_check(devdata,
1263 lockdep_is_held(&devdata_spinlock));
1264 RCU_INIT_POINTER(devdata, NULL);
1265 spin_unlock_irqrestore(&devdata_spinlock, flags);
1267 if (old_devdata && old_devdata->dev)
1268 dev_set_drvdata(old_devdata->dev, NULL);
1270 vio_unregister_driver(&nx842_vio_driver);
1273 module_exit(nx842_pseries_exit);