2 * Driver for IBM Power 842 compression accelerator
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2012
20 * Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
21 * Seth Jennings <sjenning@linux.vnet.ibm.com>
27 #include "nx_csbcpb.h" /* struct nx_csbcpb */
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Robert Jennings <rcj@linux.vnet.ibm.com>");
31 MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors");
32 MODULE_ALIAS_CRYPTO("842");
33 MODULE_ALIAS_CRYPTO("842-nx");
35 static struct nx842_constraints nx842_pseries_constraints = {
36 .alignment = DDE_BUFFER_ALIGN,
37 .multiple = DDE_BUFFER_LAST_MULT,
38 .minimum = DDE_BUFFER_LAST_MULT,
39 .maximum = PAGE_SIZE, /* dynamic, max_sync_size */
42 static int check_constraints(unsigned long buf, unsigned int *len, bool in)
44 if (!IS_ALIGNED(buf, nx842_pseries_constraints.alignment)) {
45 pr_debug("%s buffer 0x%lx not aligned to 0x%x\n",
46 in ? "input" : "output", buf,
47 nx842_pseries_constraints.alignment);
50 if (*len % nx842_pseries_constraints.multiple) {
51 pr_debug("%s buffer len 0x%x not multiple of 0x%x\n",
52 in ? "input" : "output", *len,
53 nx842_pseries_constraints.multiple);
56 *len = round_down(*len, nx842_pseries_constraints.multiple);
58 if (*len < nx842_pseries_constraints.minimum) {
59 pr_debug("%s buffer len 0x%x under minimum 0x%x\n",
60 in ? "input" : "output", *len,
61 nx842_pseries_constraints.minimum);
64 if (*len > nx842_pseries_constraints.maximum) {
65 pr_debug("%s buffer len 0x%x over maximum 0x%x\n",
66 in ? "input" : "output", *len,
67 nx842_pseries_constraints.maximum);
70 *len = nx842_pseries_constraints.maximum;
75 /* I assume we need to align the CSB? */
76 #define WORKMEM_ALIGN (256)
78 struct nx842_workmem {
82 /* coprocessor status/parameter block */
83 struct nx_csbcpb csbcpb;
85 char padding[WORKMEM_ALIGN];
86 } __aligned(WORKMEM_ALIGN);
88 /* Macros for fields within nx_csbcpb */
89 /* Check the valid bit within the csbcpb valid field */
90 #define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7))
92 /* CE macros operate on the completion_extension field bits in the csbcpb.
93 * CE0 0=full completion, 1=partial completion
94 * CE1 0=CE0 indicates completion, 1=termination (output may be modified)
95 * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */
96 #define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7))
97 #define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6))
98 #define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5))
100 /* The NX unit accepts data only on 4K page boundaries */
101 #define NX842_HW_PAGE_SIZE (4096)
102 #define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
104 struct ibm_nx842_counters {
105 atomic64_t comp_complete;
106 atomic64_t comp_failed;
107 atomic64_t decomp_complete;
108 atomic64_t decomp_failed;
110 atomic64_t comp_times[32];
111 atomic64_t decomp_times[32];
114 static struct nx842_devdata {
115 struct vio_dev *vdev;
117 struct ibm_nx842_counters *counters;
118 unsigned int max_sg_len;
119 unsigned int max_sync_size;
120 unsigned int max_sync_sg;
122 static DEFINE_SPINLOCK(devdata_mutex);
124 #define NX842_COUNTER_INC(_x) \
125 static inline void nx842_inc_##_x( \
126 const struct nx842_devdata *dev) { \
128 atomic64_inc(&dev->counters->_x); \
130 NX842_COUNTER_INC(comp_complete);
131 NX842_COUNTER_INC(comp_failed);
132 NX842_COUNTER_INC(decomp_complete);
133 NX842_COUNTER_INC(decomp_failed);
134 NX842_COUNTER_INC(swdecomp);
136 #define NX842_HIST_SLOTS 16
138 static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
140 int bucket = fls(time);
143 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
145 atomic64_inc(×[bucket]);
148 /* NX unit operation flags */
149 #define NX842_OP_COMPRESS 0x0
150 #define NX842_OP_CRC 0x1
151 #define NX842_OP_DECOMPRESS 0x2
152 #define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
153 #define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
154 #define NX842_OP_ASYNC (1<<23)
155 #define NX842_OP_NOTIFY (1<<22)
156 #define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
158 static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
160 /* No use of DMA mappings within the driver. */
164 struct nx842_slentry {
165 __be64 ptr; /* Real address (use __pa()) */
169 /* pHyp scatterlist entry */
170 struct nx842_scatterlist {
171 int entry_nr; /* number of slentries */
172 struct nx842_slentry *entries; /* ptr to array of slentries */
175 /* Does not include sizeof(entry_nr) in the size */
176 static inline unsigned long nx842_get_scatterlist_size(
177 struct nx842_scatterlist *sl)
179 return sl->entry_nr * sizeof(struct nx842_slentry);
182 static int nx842_build_scatterlist(unsigned long buf, int len,
183 struct nx842_scatterlist *sl)
185 unsigned long entrylen;
186 struct nx842_slentry *entry;
192 entry->ptr = cpu_to_be64(nx842_get_pa((void *)buf));
193 entrylen = min_t(int, len,
194 LEN_ON_SIZE(buf, NX842_HW_PAGE_SIZE));
195 entry->len = cpu_to_be64(entrylen);
207 static int nx842_validate_result(struct device *dev,
208 struct cop_status_block *csb)
210 /* The csb must be valid after returning from vio_h_cop_sync */
211 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
212 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
214 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
217 csb->completion_code,
218 csb->completion_extension);
219 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
220 be32_to_cpu(csb->processed_byte_count),
221 (unsigned long)be64_to_cpu(csb->address));
225 /* Check return values from the hardware in the CSB */
226 switch (csb->completion_code) {
227 case 0: /* Completed without error */
229 case 64: /* Compression ok, but output larger than input */
230 dev_dbg(dev, "%s: output size larger than input size\n",
233 case 13: /* Output buffer too small */
234 dev_dbg(dev, "%s: Out of space in output buffer\n",
237 case 66: /* Input data contains an illegal template field */
238 case 67: /* Template indicates data past the end of the input stream */
239 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
240 __func__, csb->completion_code);
243 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
244 __func__, csb->completion_code);
248 /* Hardware sanity check */
249 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
250 dev_err(dev, "%s: No error returned by hardware, but "
251 "data returned is unusable, contact support.\n"
252 "(Additional info: csbcbp->processed bytes "
253 "does not specify processed bytes for the "
254 "target buffer.)\n", __func__);
262 * nx842_pseries_compress - Compress data using the 842 algorithm
264 * Compression provide by the NX842 coprocessor on IBM Power systems.
265 * The input buffer is compressed and the result is stored in the
266 * provided output buffer.
268 * Upon return from this function @outlen contains the length of the
269 * compressed data. If there is an error then @outlen will be 0 and an
270 * error will be specified by the return code from this function.
272 * @in: Pointer to input buffer
273 * @inlen: Length of input buffer
274 * @out: Pointer to output buffer
275 * @outlen: Length of output buffer
276 * @wrkmem: ptr to buffer for working memory, size determined by
277 * nx842_pseries_driver.workmem_size
280 * 0 Success, output of length @outlen stored in the buffer at @out
281 * -ENOMEM Unable to allocate internal buffers
282 * -ENOSPC Output buffer is to small
283 * -EIO Internal error
284 * -ENODEV Hardware unavailable
286 static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
287 unsigned char *out, unsigned int *outlen,
290 struct nx842_devdata *local_devdata;
291 struct device *dev = NULL;
292 struct nx842_workmem *workmem;
293 struct nx842_scatterlist slin, slout;
294 struct nx_csbcpb *csbcpb;
295 int ret = 0, max_sync_size;
296 unsigned long inbuf, outbuf;
297 struct vio_pfo_op op = {
302 unsigned long start = get_tb();
304 inbuf = (unsigned long)in;
305 if (check_constraints(inbuf, &inlen, true))
308 outbuf = (unsigned long)out;
309 if (check_constraints(outbuf, outlen, false))
313 local_devdata = rcu_dereference(devdata);
314 if (!local_devdata || !local_devdata->dev) {
318 max_sync_size = local_devdata->max_sync_size;
319 dev = local_devdata->dev;
321 /* Init scatterlist */
322 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
323 slin.entries = (struct nx842_slentry *)workmem->slin;
324 slout.entries = (struct nx842_slentry *)workmem->slout;
327 op.flags = NX842_OP_COMPRESS;
328 csbcpb = &workmem->csbcpb;
329 memset(csbcpb, 0, sizeof(*csbcpb));
330 op.csbcpb = nx842_get_pa(csbcpb);
332 if ((inbuf & NX842_HW_PAGE_MASK) ==
333 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
334 /* Create direct DDE */
335 op.in = nx842_get_pa((void *)inbuf);
338 /* Create indirect DDE (scatterlist) */
339 nx842_build_scatterlist(inbuf, inlen, &slin);
340 op.in = nx842_get_pa(slin.entries);
341 op.inlen = -nx842_get_scatterlist_size(&slin);
344 if ((outbuf & NX842_HW_PAGE_MASK) ==
345 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
346 /* Create direct DDE */
347 op.out = nx842_get_pa((void *)outbuf);
350 /* Create indirect DDE (scatterlist) */
351 nx842_build_scatterlist(outbuf, *outlen, &slout);
352 op.out = nx842_get_pa(slout.entries);
353 op.outlen = -nx842_get_scatterlist_size(&slout);
356 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
357 __func__, (unsigned long)op.in, (long)op.inlen,
358 (unsigned long)op.out, (long)op.outlen);
360 /* Send request to pHyp */
361 ret = vio_h_cop_sync(local_devdata->vdev, &op);
363 /* Check for pHyp error */
365 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
366 __func__, ret, op.hcall_err);
371 /* Check for hardware error */
372 ret = nx842_validate_result(dev, &csbcpb->csb);
376 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
377 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
381 nx842_inc_comp_failed(local_devdata);
383 nx842_inc_comp_complete(local_devdata);
384 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
385 (get_tb() - start) / tb_ticks_per_usec);
392 * nx842_pseries_decompress - Decompress data using the 842 algorithm
394 * Decompression provide by the NX842 coprocessor on IBM Power systems.
395 * The input buffer is decompressed and the result is stored in the
396 * provided output buffer. The size allocated to the output buffer is
397 * provided by the caller of this function in @outlen. Upon return from
398 * this function @outlen contains the length of the decompressed data.
399 * If there is an error then @outlen will be 0 and an error will be
400 * specified by the return code from this function.
402 * @in: Pointer to input buffer
403 * @inlen: Length of input buffer
404 * @out: Pointer to output buffer
405 * @outlen: Length of output buffer
406 * @wrkmem: ptr to buffer for working memory, size determined by
407 * nx842_pseries_driver.workmem_size
410 * 0 Success, output of length @outlen stored in the buffer at @out
411 * -ENODEV Hardware decompression device is unavailable
412 * -ENOMEM Unable to allocate internal buffers
413 * -ENOSPC Output buffer is to small
414 * -EINVAL Bad input data encountered when attempting decompress
415 * -EIO Internal error
417 static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
418 unsigned char *out, unsigned int *outlen,
421 struct nx842_devdata *local_devdata;
422 struct device *dev = NULL;
423 struct nx842_workmem *workmem;
424 struct nx842_scatterlist slin, slout;
425 struct nx_csbcpb *csbcpb;
426 int ret = 0, max_sync_size;
427 unsigned long inbuf, outbuf;
428 struct vio_pfo_op op = {
433 unsigned long start = get_tb();
435 /* Ensure page alignment and size */
436 inbuf = (unsigned long)in;
437 if (check_constraints(inbuf, &inlen, true))
440 outbuf = (unsigned long)out;
441 if (check_constraints(outbuf, outlen, false))
445 local_devdata = rcu_dereference(devdata);
446 if (!local_devdata || !local_devdata->dev) {
450 max_sync_size = local_devdata->max_sync_size;
451 dev = local_devdata->dev;
453 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
455 /* Init scatterlist */
456 slin.entries = (struct nx842_slentry *)workmem->slin;
457 slout.entries = (struct nx842_slentry *)workmem->slout;
460 op.flags = NX842_OP_DECOMPRESS;
461 csbcpb = &workmem->csbcpb;
462 memset(csbcpb, 0, sizeof(*csbcpb));
463 op.csbcpb = nx842_get_pa(csbcpb);
465 if ((inbuf & NX842_HW_PAGE_MASK) ==
466 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
467 /* Create direct DDE */
468 op.in = nx842_get_pa((void *)inbuf);
471 /* Create indirect DDE (scatterlist) */
472 nx842_build_scatterlist(inbuf, inlen, &slin);
473 op.in = nx842_get_pa(slin.entries);
474 op.inlen = -nx842_get_scatterlist_size(&slin);
477 if ((outbuf & NX842_HW_PAGE_MASK) ==
478 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
479 /* Create direct DDE */
480 op.out = nx842_get_pa((void *)outbuf);
483 /* Create indirect DDE (scatterlist) */
484 nx842_build_scatterlist(outbuf, *outlen, &slout);
485 op.out = nx842_get_pa(slout.entries);
486 op.outlen = -nx842_get_scatterlist_size(&slout);
489 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
490 __func__, (unsigned long)op.in, (long)op.inlen,
491 (unsigned long)op.out, (long)op.outlen);
493 /* Send request to pHyp */
494 ret = vio_h_cop_sync(local_devdata->vdev, &op);
496 /* Check for pHyp error */
498 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
499 __func__, ret, op.hcall_err);
503 /* Check for hardware error */
504 ret = nx842_validate_result(dev, &csbcpb->csb);
508 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
512 /* decompress fail */
513 nx842_inc_decomp_failed(local_devdata);
515 nx842_inc_decomp_complete(local_devdata);
516 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
517 (get_tb() - start) / tb_ticks_per_usec);
525 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
527 * @devdata - struct nx842_devdata to update
531 * -ENOENT if @devdata ptr is NULL
533 static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
536 devdata->max_sync_size = 0;
537 devdata->max_sync_sg = 0;
538 devdata->max_sg_len = 0;
545 * nx842_OF_upd_status -- Check the device info from OF status prop
547 * The status property indicates if the accelerator is enabled. If the
548 * device is in the OF tree it indicates that the hardware is present.
549 * The status field indicates if the device is enabled when the status
550 * is 'okay'. Otherwise the device driver will be disabled.
552 * @prop - struct property point containing the maxsyncop for the update
555 * 0 - Device is available
556 * -ENODEV - Device is not available
558 static int nx842_OF_upd_status(struct property *prop)
560 const char *status = (const char *)prop->value;
562 if (!strncmp(status, "okay", (size_t)prop->length))
564 if (!strncmp(status, "disabled", (size_t)prop->length))
566 dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status);
572 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
574 * Definition of the 'ibm,max-sg-len' OF property:
575 * This field indicates the maximum byte length of a scatter list
576 * for the platform facility. It is a single cell encoded as with encode-int.
579 * # od -x ibm,max-sg-len
582 * In this example, the maximum byte length of a scatter list is
585 * @devdata - struct nx842_devdata to update
586 * @prop - struct property point containing the maxsyncop for the update
592 static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
593 struct property *prop) {
595 const unsigned int maxsglen = of_read_number(prop->value, 1);
597 if (prop->length != sizeof(maxsglen)) {
598 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
599 dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__,
600 prop->length, sizeof(maxsglen));
603 devdata->max_sg_len = min_t(unsigned int,
604 maxsglen, NX842_HW_PAGE_SIZE);
611 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
613 * Definition of the 'ibm,max-sync-cop' OF property:
614 * Two series of cells. The first series of cells represents the maximums
615 * that can be synchronously compressed. The second series of cells
616 * represents the maximums that can be synchronously decompressed.
617 * 1. The first cell in each series contains the count of the number of
618 * data length, scatter list elements pairs that follow – each being
620 * a. One cell data byte length
621 * b. One cell total number of scatter list elements
624 * # od -x ibm,max-sync-cop
625 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
626 * 0000020 0000 1000 0000 01fe
628 * In this example, compression supports 0x1000 (4,096) data byte length
629 * and 0x1fe (510) total scatter list elements. Decompression supports
630 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
633 * @devdata - struct nx842_devdata to update
634 * @prop - struct property point containing the maxsyncop for the update
640 static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
641 struct property *prop) {
643 unsigned int comp_data_limit, decomp_data_limit;
644 unsigned int comp_sg_limit, decomp_sg_limit;
645 const struct maxsynccop_t {
646 __be32 comp_elements;
647 __be32 comp_data_limit;
648 __be32 comp_sg_limit;
649 __be32 decomp_elements;
650 __be32 decomp_data_limit;
651 __be32 decomp_sg_limit;
654 if (prop->length != sizeof(*maxsynccop)) {
655 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
656 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
657 sizeof(*maxsynccop));
662 maxsynccop = (const struct maxsynccop_t *)prop->value;
663 comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit);
664 comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit);
665 decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit);
666 decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit);
668 /* Use one limit rather than separate limits for compression and
669 * decompression. Set a maximum for this so as not to exceed the
670 * size that the header can support and round the value down to
671 * the hardware page size (4K) */
672 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
674 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
677 if (devdata->max_sync_size < 4096) {
678 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
679 "less than the driver minimum, unable to use "
680 "the hardware device\n",
681 __func__, devdata->max_sync_size);
686 nx842_pseries_constraints.maximum = devdata->max_sync_size;
688 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
689 if (devdata->max_sync_sg < 1) {
690 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
691 "less than the driver minimum, unable to use "
692 "the hardware device\n",
693 __func__, devdata->max_sync_sg);
704 * nx842_OF_upd -- Handle OF properties updates for the device.
706 * Set all properties from the OF tree. Optionally, a new property
707 * can be provided by the @new_prop pointer to overwrite an existing value.
708 * The device will remain disabled until all values are valid, this function
709 * will return an error for updates unless all values are valid.
711 * @new_prop: If not NULL, this property is being updated. If NULL, update
712 * all properties from the current values in the OF tree.
716 * -ENOMEM - Could not allocate memory for new devdata structure
717 * -EINVAL - property value not found, new_prop is not a recognized
718 * property for the device or property value is not valid.
719 * -ENODEV - Device is not available
721 static int nx842_OF_upd(struct property *new_prop)
723 struct nx842_devdata *old_devdata = NULL;
724 struct nx842_devdata *new_devdata = NULL;
725 struct device_node *of_node = NULL;
726 struct property *status = NULL;
727 struct property *maxsglen = NULL;
728 struct property *maxsyncop = NULL;
732 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
736 spin_lock_irqsave(&devdata_mutex, flags);
737 old_devdata = rcu_dereference_check(devdata,
738 lockdep_is_held(&devdata_mutex));
740 of_node = old_devdata->dev->of_node;
742 if (!old_devdata || !of_node) {
743 pr_err("%s: device is not available\n", __func__);
744 spin_unlock_irqrestore(&devdata_mutex, flags);
749 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
750 new_devdata->counters = old_devdata->counters;
752 /* Set ptrs for existing properties */
753 status = of_find_property(of_node, "status", NULL);
754 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
755 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
756 if (!status || !maxsglen || !maxsyncop) {
757 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
763 * If this is a property update, there are only certain properties that
764 * we care about. Bail if it isn't in the below list
766 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
767 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
768 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
771 /* Perform property updates */
772 ret = nx842_OF_upd_status(status);
776 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
780 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
785 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
786 __func__, new_devdata->max_sync_size,
787 old_devdata->max_sync_size);
788 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
789 __func__, new_devdata->max_sync_sg,
790 old_devdata->max_sync_sg);
791 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
792 __func__, new_devdata->max_sg_len,
793 old_devdata->max_sg_len);
795 rcu_assign_pointer(devdata, new_devdata);
796 spin_unlock_irqrestore(&devdata_mutex, flags);
798 dev_set_drvdata(new_devdata->dev, new_devdata);
804 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
805 nx842_OF_set_defaults(new_devdata);
806 rcu_assign_pointer(devdata, new_devdata);
807 spin_unlock_irqrestore(&devdata_mutex, flags);
809 dev_set_drvdata(new_devdata->dev, new_devdata);
812 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
813 spin_unlock_irqrestore(&devdata_mutex, flags);
822 * nx842_OF_notifier - Process updates to OF properties for the device
824 * @np: notifier block
825 * @action: notifier action
826 * @update: struct pSeries_reconfig_prop_update pointer if action is
827 * PSERIES_UPDATE_PROPERTY
830 * NOTIFY_OK on success
831 * NOTIFY_BAD encoded with error number on failure, use
832 * notifier_to_errno() to decode this value
834 static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
837 struct of_reconfig_data *upd = data;
838 struct nx842_devdata *local_devdata;
839 struct device_node *node = NULL;
842 local_devdata = rcu_dereference(devdata);
844 node = local_devdata->dev->of_node;
847 action == OF_RECONFIG_UPDATE_PROPERTY &&
848 !strcmp(upd->dn->name, node->name)) {
850 nx842_OF_upd(upd->prop);
857 static struct notifier_block nx842_of_nb = {
858 .notifier_call = nx842_OF_notifier,
861 #define nx842_counter_read(_name) \
862 static ssize_t nx842_##_name##_show(struct device *dev, \
863 struct device_attribute *attr, \
865 struct nx842_devdata *local_devdata; \
868 local_devdata = rcu_dereference(devdata); \
870 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
871 atomic64_read(&local_devdata->counters->_name)); \
876 #define NX842DEV_COUNTER_ATTR_RO(_name) \
877 nx842_counter_read(_name); \
878 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
880 nx842_##_name##_show,\
883 NX842DEV_COUNTER_ATTR_RO(comp_complete);
884 NX842DEV_COUNTER_ATTR_RO(comp_failed);
885 NX842DEV_COUNTER_ATTR_RO(decomp_complete);
886 NX842DEV_COUNTER_ATTR_RO(decomp_failed);
887 NX842DEV_COUNTER_ATTR_RO(swdecomp);
889 static ssize_t nx842_timehist_show(struct device *,
890 struct device_attribute *, char *);
892 static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
893 nx842_timehist_show, NULL);
894 static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
895 0444, nx842_timehist_show, NULL);
897 static ssize_t nx842_timehist_show(struct device *dev,
898 struct device_attribute *attr, char *buf) {
900 struct nx842_devdata *local_devdata;
902 int bytes_remain = PAGE_SIZE;
907 local_devdata = rcu_dereference(devdata);
908 if (!local_devdata) {
913 if (attr == &dev_attr_comp_times)
914 times = local_devdata->counters->comp_times;
915 else if (attr == &dev_attr_decomp_times)
916 times = local_devdata->counters->decomp_times;
922 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
923 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
924 i ? (2<<(i-1)) : 0, (2<<i)-1,
925 atomic64_read(×[i]));
926 bytes_remain -= bytes;
929 /* The last bucket holds everything over
930 * 2<<(NX842_HIST_SLOTS - 2) us */
931 bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
932 2<<(NX842_HIST_SLOTS - 2),
933 atomic64_read(×[(NX842_HIST_SLOTS - 1)]));
940 static struct attribute *nx842_sysfs_entries[] = {
941 &dev_attr_comp_complete.attr,
942 &dev_attr_comp_failed.attr,
943 &dev_attr_decomp_complete.attr,
944 &dev_attr_decomp_failed.attr,
945 &dev_attr_swdecomp.attr,
946 &dev_attr_comp_times.attr,
947 &dev_attr_decomp_times.attr,
951 static struct attribute_group nx842_attribute_group = {
952 .name = NULL, /* put in device directory */
953 .attrs = nx842_sysfs_entries,
956 static struct nx842_driver nx842_pseries_driver = {
957 .name = KBUILD_MODNAME,
958 .owner = THIS_MODULE,
959 .workmem_size = sizeof(struct nx842_workmem),
960 .constraints = &nx842_pseries_constraints,
961 .compress = nx842_pseries_compress,
962 .decompress = nx842_pseries_decompress,
965 static int nx842_pseries_crypto_init(struct crypto_tfm *tfm)
967 return nx842_crypto_init(tfm, &nx842_pseries_driver);
970 static struct crypto_alg nx842_pseries_alg = {
972 .cra_driver_name = "842-nx",
974 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
975 .cra_ctxsize = sizeof(struct nx842_crypto_ctx),
976 .cra_module = THIS_MODULE,
977 .cra_init = nx842_pseries_crypto_init,
978 .cra_exit = nx842_crypto_exit,
979 .cra_u = { .compress = {
980 .coa_compress = nx842_crypto_compress,
981 .coa_decompress = nx842_crypto_decompress } }
984 static int nx842_probe(struct vio_dev *viodev,
985 const struct vio_device_id *id)
987 struct nx842_devdata *old_devdata, *new_devdata = NULL;
991 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
995 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
997 if (!new_devdata->counters) {
1002 spin_lock_irqsave(&devdata_mutex, flags);
1003 old_devdata = rcu_dereference_check(devdata,
1004 lockdep_is_held(&devdata_mutex));
1006 if (old_devdata && old_devdata->vdev != NULL) {
1007 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
1012 dev_set_drvdata(&viodev->dev, NULL);
1014 new_devdata->vdev = viodev;
1015 new_devdata->dev = &viodev->dev;
1016 nx842_OF_set_defaults(new_devdata);
1018 rcu_assign_pointer(devdata, new_devdata);
1019 spin_unlock_irqrestore(&devdata_mutex, flags);
1023 of_reconfig_notifier_register(&nx842_of_nb);
1025 ret = nx842_OF_upd(NULL);
1029 ret = crypto_register_alg(&nx842_pseries_alg);
1031 dev_err(&viodev->dev, "could not register comp alg: %d\n", ret);
1036 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
1039 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1040 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1048 spin_unlock_irqrestore(&devdata_mutex, flags);
1050 kfree(new_devdata->counters);
1056 static int nx842_remove(struct vio_dev *viodev)
1058 struct nx842_devdata *old_devdata;
1059 unsigned long flags;
1061 pr_info("Removing IBM Power 842 compression device\n");
1062 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1064 crypto_unregister_alg(&nx842_pseries_alg);
1066 spin_lock_irqsave(&devdata_mutex, flags);
1067 old_devdata = rcu_dereference_check(devdata,
1068 lockdep_is_held(&devdata_mutex));
1069 of_reconfig_notifier_unregister(&nx842_of_nb);
1070 RCU_INIT_POINTER(devdata, NULL);
1071 spin_unlock_irqrestore(&devdata_mutex, flags);
1073 dev_set_drvdata(&viodev->dev, NULL);
1075 kfree(old_devdata->counters);
1081 static struct vio_device_id nx842_vio_driver_ids[] = {
1082 {"ibm,compression-v1", "ibm,compression"},
1086 static struct vio_driver nx842_vio_driver = {
1087 .name = KBUILD_MODNAME,
1088 .probe = nx842_probe,
1089 .remove = nx842_remove,
1090 .get_desired_dma = nx842_get_desired_dma,
1091 .id_table = nx842_vio_driver_ids,
1094 static int __init nx842_pseries_init(void)
1096 struct nx842_devdata *new_devdata;
1099 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1102 RCU_INIT_POINTER(devdata, NULL);
1103 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1105 pr_err("Could not allocate memory for device data\n");
1108 RCU_INIT_POINTER(devdata, new_devdata);
1110 ret = vio_register_driver(&nx842_vio_driver);
1112 pr_err("Could not register VIO driver %d\n", ret);
1121 module_init(nx842_pseries_init);
1123 static void __exit nx842_pseries_exit(void)
1125 struct nx842_devdata *old_devdata;
1126 unsigned long flags;
1128 crypto_unregister_alg(&nx842_pseries_alg);
1130 spin_lock_irqsave(&devdata_mutex, flags);
1131 old_devdata = rcu_dereference_check(devdata,
1132 lockdep_is_held(&devdata_mutex));
1133 RCU_INIT_POINTER(devdata, NULL);
1134 spin_unlock_irqrestore(&devdata_mutex, flags);
1136 if (old_devdata && old_devdata->dev)
1137 dev_set_drvdata(old_devdata->dev, NULL);
1139 vio_unregister_driver(&nx842_vio_driver);
1142 module_exit(nx842_pseries_exit);